usb.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * USB implementation for Plan 9
  3. * (c) 1998, 1999 C H Forsyth
  4. */
  5. enum {
  6. Dbginfo = 0x01,
  7. Dbgfs = 0x02,
  8. Dbgproc = 0x04,
  9. Dbgcontrol = 0x08,
  10. };
  11. extern int debug, debugdebug, verbose;
  12. typedef uchar byte;
  13. #ifndef CHANNOP
  14. typedef struct Ref Ref;
  15. #define threadprint fprint
  16. #endif
  17. /*
  18. * USB definitions
  19. */
  20. typedef struct DConfig DConfig;
  21. typedef struct DDevice DDevice;
  22. typedef struct DEndpoint DEndpoint;
  23. typedef struct DHid DHid;
  24. typedef struct DHub DHub;
  25. typedef struct DInterface DInterface;
  26. typedef struct Dconf Dconf;
  27. typedef struct Dalt Dalt;
  28. typedef struct Device Device;
  29. typedef struct Dinf Dinf;
  30. typedef struct Endpt Endpt;
  31. typedef struct Namelist Namelist;
  32. #define GET2(p) ((((p)[1]&0xFF)<<8)|((p)[0]&0xFF))
  33. #define PUT2(p,v) {((p)[0] = (v)); ((p)[1] = (v)>>8);}
  34. enum
  35. {
  36. /* request type */
  37. RH2D = 0<<7,
  38. RD2H = 1<<7,
  39. Rstandard = 0<<5, /* types */
  40. Rclass = 1<<5,
  41. Rvendor = 2<<5,
  42. Rdevice = 0, /* recipients */
  43. Rinterface = 1,
  44. Rendpt = 2,
  45. Rother = 3,
  46. /* standard requests */
  47. GET_STATUS = 0,
  48. CLEAR_FEATURE = 1,
  49. SET_FEATURE = 3,
  50. SET_ADDRESS = 5,
  51. GET_DESCRIPTOR = 6,
  52. SET_DESCRIPTOR = 7,
  53. GET_CONFIGURATION = 8,
  54. SET_CONFIGURATION = 9,
  55. GET_INTERFACE = 10,
  56. SET_INTERFACE = 11,
  57. SYNCH_FRAME = 12,
  58. GET_CUR = 0x81,
  59. GET_MIN = 0x82,
  60. GET_MAX = 0x83,
  61. GET_RES = 0x84,
  62. SET_CUR = 0x01,
  63. SET_MIN = 0x02,
  64. SET_MAX = 0x03,
  65. SET_RES = 0x04,
  66. /* hub class feature selectors */
  67. C_HUB_LOCAL_POWER = 0,
  68. C_HUB_OVER_CURRENT,
  69. PORT_CONNECTION = 0,
  70. PORT_ENABLE = 1,
  71. PORT_SUSPEND = 2,
  72. PORT_OVER_CURRENT = 3,
  73. PORT_RESET = 4,
  74. PORT_POWER = 8,
  75. PORT_LOW_SPEED = 9,
  76. C_PORT_CONNECTION = 16,
  77. C_PORT_ENABLE,
  78. C_PORT_SUSPEND,
  79. C_PORT_OVER_CURRENT,
  80. C_PORT_RESET,
  81. /* descriptor types */
  82. DEVICE = 1,
  83. CONFIGURATION = 2,
  84. STRING = 3,
  85. INTERFACE = 4,
  86. ENDPOINT = 5,
  87. HID = 0x21,
  88. REPORT = 0x22,
  89. PHYSICAL = 0x23,
  90. HUB = 0x29,
  91. /* feature selectors */
  92. DEVICE_REMOTE_WAKEUP = 1,
  93. ENDPOINT_STALL = 0,
  94. /* report types */
  95. Tmtype = 3<<2,
  96. Tmitem = 0xF0,
  97. Tmain = 0<<2,
  98. Tinput = 0x80,
  99. Toutput = 0x90,
  100. Tfeature = 0xB0,
  101. Tcoll = 0xA0,
  102. Tecoll = 0xC0,
  103. Tglobal = 1<<2,
  104. Tusagepage = 0x00,
  105. Tlmin = 0x10,
  106. Tlmax = 0x20,
  107. Tpmin = 0x30,
  108. Tpmax = 0x40,
  109. Tunitexp = 0x50,
  110. Tunit = 0x60,
  111. Trepsize = 0x70,
  112. TrepID = 0x80,
  113. Trepcount = 0x90,
  114. Tpush = 0xA0,
  115. Tpop = 0xB0,
  116. Tlocal = 2<<2,
  117. Tusage = 0x00,
  118. Tumin = 0x10,
  119. Tumax = 0x20,
  120. Tdindex = 0x30,
  121. Tdmin = 0x40,
  122. Tdmax = 0x50,
  123. Tsindex = 0x70,
  124. Tsmin = 0x80,
  125. Tsmax = 0x90,
  126. Tsetdelim = 0xA0,
  127. Treserved = 3<<2,
  128. Tlong = 0xFE,
  129. /* parameters */
  130. Nendpt = 16,
  131. /* device state */
  132. Detached = 0,
  133. Attached,
  134. Enabled,
  135. Assigned,
  136. Configured,
  137. /* classes */
  138. Noclass = 0,
  139. Hubclass,
  140. Otherclass,
  141. /* endpoint direction */
  142. Ein = 0,
  143. Eout,
  144. Eboth,
  145. /* endpoint type */
  146. Econtrol = 0,
  147. Eiso = 1,
  148. Ebulk = 2,
  149. Eintr = 3,
  150. /* endpoint isotype */
  151. Eunknown = 0,
  152. Easync = 1,
  153. Eadapt = 2,
  154. Esync = 3,
  155. };
  156. enum
  157. {
  158. CL_AUDIO = 1,
  159. CL_COMMS = 2,
  160. CL_HID = 3,
  161. CL_PRINTER = 7,
  162. CL_STORAGE = 8,
  163. CL_HUB = 9,
  164. CL_DATA = 10,
  165. };
  166. struct Endpt
  167. {
  168. uchar addr; /* endpoint address, 0-15 (|0x80 if direction==Ein) */
  169. uchar dir; /* direction, Ein/Eout */
  170. uchar type; /* Econtrol, Eiso, Ebulk, Eintr */
  171. uchar isotype; /* Eunknown, Easync, Eadapt, Esync */
  172. int id;
  173. int class;
  174. ulong csp;
  175. int maxpkt;
  176. Device* dev;
  177. Dconf* conf;
  178. Dinf* iface;
  179. };
  180. struct Dalt
  181. {
  182. int attrib;
  183. int interval;
  184. void* devspec; /* device specific settings */
  185. };
  186. struct Dinf
  187. {
  188. int interface; /* interface number */
  189. ulong csp; /* USB class/subclass/proto */
  190. Dalt* dalt[16];
  191. Endpt* endpt[16];
  192. };
  193. struct Dconf
  194. {
  195. ulong csp; /* USB class/subclass/proto */
  196. int nif; /* number of interfaces */
  197. int cval; /* value for set configuration */
  198. int attrib;
  199. int milliamps; /* maximum power in this configuration */
  200. Dinf* iface[16]; /* up to 16 interfaces */
  201. };
  202. /* Dconf.attrib */
  203. enum
  204. {
  205. Cbuspowered = 1<<7,
  206. Cselfpowered = 1<<6,
  207. Cremotewakeup = 1<<5,
  208. };
  209. struct Device
  210. {
  211. Ref;
  212. int ctlrno;
  213. int ctl; /* fd */
  214. int setup; /* fd */
  215. int status; /* fd */
  216. int state;
  217. int id;
  218. int class;
  219. int npt;
  220. int ls; /* low speed */
  221. ulong csp; /* USB class/subclass/proto */
  222. int nconf;
  223. int nif; /* # of interfaces (sum of per-conf `nif's) */
  224. int vid; /* vendor id */
  225. int did; /* product (device) id */
  226. Dconf* config[16];
  227. Endpt* ep[Nendpt];
  228. Device* setupfd; /* for usbprobe */
  229. Device* cfd; /* for usbprobe */
  230. };
  231. /*
  232. * layout of standard descriptor types
  233. */
  234. struct DDevice
  235. {
  236. byte bLength;
  237. byte bDescriptorType;
  238. byte bcdUSB[2];
  239. byte bDeviceClass;
  240. byte bDeviceSubClass;
  241. byte bDeviceProtocol;
  242. byte bMaxPacketSize0;
  243. byte idVendor[2];
  244. byte idProduct[2];
  245. byte bcdDevice[2];
  246. byte iManufacturer;
  247. byte iProduct;
  248. byte iSerialNumber;
  249. byte bNumConfigurations;
  250. };
  251. #define DDEVLEN 18
  252. struct DConfig
  253. {
  254. byte bLength;
  255. byte bDescriptorType;
  256. byte wTotalLength[2];
  257. byte bNumInterfaces;
  258. byte bConfigurationValue;
  259. byte iConfiguration;
  260. byte bmAttributes;
  261. byte MaxPower;
  262. };
  263. #define DCONFLEN 9
  264. struct DInterface
  265. {
  266. byte bLength;
  267. byte bDescriptorType;
  268. byte bInterfaceNumber;
  269. byte bAlternateSetting;
  270. byte bNumEndpoints;
  271. byte bInterfaceClass;
  272. byte bInterfaceSubClass;
  273. byte bInterfaceProtocol;
  274. byte iInterface;
  275. };
  276. #define DINTERLEN 9
  277. struct DEndpoint
  278. {
  279. byte bLength;
  280. byte bDescriptorType;
  281. byte bEndpointAddress;
  282. byte bmAttributes;
  283. byte wMaxPacketSize[2];
  284. byte bInterval;
  285. };
  286. #define DENDPLEN 7
  287. struct DHid
  288. {
  289. byte bLength;
  290. byte bDescriptorType;
  291. byte bcdHID[2];
  292. byte bCountryCode;
  293. byte bNumDescriptors;
  294. byte bClassDescriptorType;
  295. byte wItemLength[2];
  296. };
  297. #define DHIDLEN 9
  298. struct DHub
  299. {
  300. byte bLength;
  301. byte bDescriptorType;
  302. byte bNbrPorts;
  303. byte wHubCharacteristics[2];
  304. byte bPwrOn2PwrGood;
  305. byte bHubContrCurrent;
  306. byte DeviceRemovable[1]; /* variable length */
  307. /* byte PortPwrCtrlMask; /* variable length, deprecated in USB v1.1 */
  308. };
  309. #define DHUBLEN 9
  310. struct Namelist
  311. {
  312. short index;
  313. char *name;
  314. };
  315. typedef struct Drivetab
  316. {
  317. ulong csp;
  318. void (*driver)(Device *d);
  319. } Drivetab;
  320. #define Class(csp) ((csp)&0xff)
  321. #define Subclass(csp) (((csp)>>8)&0xff)
  322. #define Proto(csp) (((csp)>>16)&0xff)
  323. #define CSP(c, s, p) ((c) | ((s)<<8) | ((p)<<16))
  324. extern void (*dprinter[0x100])(Device *, int, ulong, void *b, int n);
  325. /*
  326. * format routines
  327. */
  328. void pdesc (Device *, int, ulong, byte *, int);
  329. void preport (Device *, int, ulong, byte *, int);
  330. void pstring (Device *, int, ulong, void *, int);
  331. void phub (Device *, int, ulong, void *, int);
  332. void pdevice (Device *, int, ulong, void *, int);
  333. void phid (Device *, int, ulong, void *, int);
  334. void pcs_raw(char *tag, byte *b, int n);
  335. /*
  336. * interface
  337. */
  338. void usbfmtinit(void);
  339. Device* opendev(int, int);
  340. void closedev(Device*);
  341. int describedevice(Device*);
  342. int loadconfig(Device *d, int n);
  343. Endpt * newendpt(Device *d, int id, ulong csp);
  344. int setupcmd(Endpt*, int, int, int, int, byte*, int);
  345. int setupreq(Endpt*, int, int, int, int, int);
  346. int setupreply(Endpt*, void*, int);
  347. void setdevclass(Device *d, int n);
  348. void * emalloc(ulong);
  349. void * emallocz(ulong, int);
  350. char * namefor(Namelist *, int);
  351. #pragma varargck type "D" Device*