usb.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. typedef struct Altc Altc;
  10. typedef struct Conf Conf;
  11. typedef struct DConf DConf;
  12. typedef struct DDesc DDesc;
  13. typedef struct DDev DDev;
  14. typedef struct DEp DEp;
  15. typedef struct DIface DIface;
  16. typedef struct Desc Desc;
  17. typedef struct Dev Dev;
  18. typedef struct Ep Ep;
  19. typedef struct Iface Iface;
  20. typedef struct Usbdev Usbdev;
  21. enum {
  22. /* fundamental constants */
  23. Nep = 16, /* max. endpoints per usb device & per interface */
  24. /* tunable parameters */
  25. Nconf = 16, /* max. configurations per usb device */
  26. Nddesc = 8*Nep, /* max. device-specific descriptors per usb device */
  27. Niface = 16, /* max. interfaces per configuration */
  28. Naltc = 16, /* max. alt configurations per interface */
  29. Uctries = 4, /* no. of tries for usbcmd */
  30. Ucdelay = 50, /* delay before retrying */
  31. /* request type */
  32. Rh2d = 0<<7, /* host to device */
  33. Rd2h = 1<<7, /* device to host */
  34. Rstd = 0<<5, /* types */
  35. Rclass = 1<<5,
  36. Rvendor = 2<<5,
  37. Rdev = 0, /* recipients */
  38. Riface = 1,
  39. Rep = 2, /* endpoint */
  40. Rother = 3,
  41. /* standard requests */
  42. Rgetstatus = 0,
  43. Rclearfeature = 1,
  44. Rsetfeature = 3,
  45. Rsetaddress = 5,
  46. Rgetdesc = 6,
  47. Rsetdesc = 7,
  48. Rgetconf = 8,
  49. Rsetconf = 9,
  50. Rgetiface = 10,
  51. Rsetiface = 11,
  52. Rsynchframe = 12,
  53. Rgetcur = 0x81,
  54. Rgetmin = 0x82,
  55. Rgetmax = 0x83,
  56. Rgetres = 0x84,
  57. Rsetcur = 0x01,
  58. Rsetmin = 0x02,
  59. Rsetmax = 0x03,
  60. Rsetres = 0x04,
  61. /* dev classes */
  62. Clnone = 0, /* not in usb */
  63. Claudio = 1,
  64. Clcomms = 2,
  65. Clhid = 3,
  66. Clprinter = 7,
  67. Clstorage = 8,
  68. Clhub = 9,
  69. Cldata = 10,
  70. /* standard descriptor sizes */
  71. Ddevlen = 18,
  72. Dconflen = 9,
  73. Difacelen = 9,
  74. Deplen = 7,
  75. /* descriptor types */
  76. Ddev = 1,
  77. Dconf = 2,
  78. Dstr = 3,
  79. Diface = 4,
  80. Dep = 5,
  81. Dreport = 0x22,
  82. Dfunction = 0x24,
  83. Dphysical = 0x23,
  84. /* feature selectors */
  85. Fdevremotewakeup = 1,
  86. Fhalt = 0,
  87. /* device state */
  88. Detached = 0,
  89. Attached,
  90. Enabled,
  91. Assigned,
  92. Configured,
  93. /* endpoint direction */
  94. Ein = 0,
  95. Eout,
  96. Eboth,
  97. /* endpoint type */
  98. Econtrol = 0,
  99. Eiso = 1,
  100. Ebulk = 2,
  101. Eintr = 3,
  102. /* endpoint isotype */
  103. Eunknown = 0,
  104. Easync = 1,
  105. Eadapt = 2,
  106. Esync = 3,
  107. /* config attrib */
  108. Cbuspowered = 1<<7,
  109. Cselfpowered = 1<<6,
  110. Cremotewakeup = 1<<5,
  111. /* report types */
  112. Tmtype = 3<<2,
  113. Tmitem = 0xF0,
  114. Tmain = 0<<2,
  115. Tinput = 0x80,
  116. Toutput = 0x90,
  117. Tfeature = 0xB0,
  118. Tcoll = 0xA0,
  119. Tecoll = 0xC0,
  120. Tglobal = 1<<2,
  121. Tusagepage = 0x00,
  122. Tlmin = 0x10,
  123. Tlmax = 0x20,
  124. Tpmin = 0x30,
  125. Tpmax = 0x40,
  126. Tunitexp = 0x50,
  127. Tunit = 0x60,
  128. Trepsize = 0x70,
  129. TrepID = 0x80,
  130. Trepcount = 0x90,
  131. Tpush = 0xA0,
  132. Tpop = 0xB0,
  133. Tlocal = 2<<2,
  134. Tusage = 0x00,
  135. Tumin = 0x10,
  136. Tumax = 0x20,
  137. Tdindex = 0x30,
  138. Tdmin = 0x40,
  139. Tdmax = 0x50,
  140. Tsindex = 0x70,
  141. Tsmin = 0x80,
  142. Tsmax = 0x90,
  143. Tsetdelim = 0xA0,
  144. Treserved = 3<<2,
  145. Tlong = 0xFE,
  146. };
  147. /*
  148. * Usb device (when used for ep0s) or endpoint.
  149. * RC: One ref because of existing, another one per ogoing I/O.
  150. * per-driver resources (including FS if any) are released by aux
  151. * once the last ref is gone. This may include other Devs using
  152. * to access endpoints for actual I/O.
  153. */
  154. struct Dev
  155. {
  156. Ref Ref;
  157. char* dir; /* path for the endpoint dir */
  158. int id; /* usb id for device or ep. number */
  159. int dfd; /* descriptor for the data file */
  160. int cfd; /* descriptor for the control file */
  161. int maxpkt; /* cached from usb description */
  162. Ref nerrs; /* number of errors in requests */
  163. Usbdev* usb; /* USB description */
  164. void* aux; /* for the device driver */
  165. void (*free)(void*); /* idem. to release aux */
  166. };
  167. /*
  168. * device description as reported by USB (unpacked).
  169. */
  170. struct Usbdev
  171. {
  172. u32 csp; /* USB class/subclass/proto */
  173. int vid; /* vendor id */
  174. int did; /* product (device) id */
  175. int dno; /* device release number */
  176. char* vendor;
  177. char* product;
  178. char* serial;
  179. int vsid;
  180. int psid;
  181. int ssid;
  182. int class; /* from descriptor */
  183. int nconf; /* from descriptor */
  184. Conf* conf[Nconf]; /* configurations */
  185. Ep* ep[Nep]; /* all endpoints in device */
  186. Desc* ddesc[Nddesc]; /* (raw) device specific descriptors */
  187. };
  188. struct Ep
  189. {
  190. u8 addr; /* endpt address, 0-15 (|0x80 if Ein) */
  191. u8 dir; /* direction, Ein/Eout */
  192. u8 type; /* Econtrol, Eiso, Ebulk, Eintr */
  193. u8 isotype; /* Eunknown, Easync, Eadapt, Esync */
  194. int id;
  195. int maxpkt; /* max. packet size */
  196. int ntds; /* nb. of Tds per µframe */
  197. Conf* conf; /* the endpoint belongs to */
  198. Iface* iface; /* the endpoint belongs to */
  199. };
  200. struct Altc
  201. {
  202. int attrib;
  203. int interval;
  204. void* aux; /* for the driver program */
  205. };
  206. struct Iface
  207. {
  208. int id; /* interface number */
  209. u32 csp; /* USB class/subclass/proto */
  210. Altc* altc[Naltc];
  211. Ep* ep[Nep];
  212. void* aux; /* for the driver program */
  213. };
  214. struct Conf
  215. {
  216. int cval; /* value for set configuration */
  217. int attrib;
  218. int milliamps; /* maximum power in this config. */
  219. Iface* iface[Niface];
  220. };
  221. /*
  222. * Device-specific descriptors.
  223. * They show up mixed with other descriptors
  224. * within a configuration.
  225. * These are unknown to the library but handed to the driver.
  226. */
  227. struct DDesc
  228. {
  229. u8 bLength;
  230. u8 bDescriptorType;
  231. u8 bbytes[1];
  232. /* extra bytes allocated here to keep the rest of it */
  233. };
  234. struct Desc
  235. {
  236. Conf* conf; /* where this descriptor was read */
  237. Iface* iface; /* last iface before desc in conf. */
  238. Ep* ep; /* last endpt before desc in conf. */
  239. Altc* altc; /* last alt.c. before desc in conf. */
  240. DDesc data; /* unparsed standard USB descriptor */
  241. };
  242. /*
  243. * layout of standard descriptor types
  244. */
  245. struct DDev
  246. {
  247. u8 bLength;
  248. u8 bDescriptorType;
  249. u8 bcdUSB[2];
  250. u8 bDevClass;
  251. u8 bDevSubClass;
  252. u8 bDevProtocol;
  253. u8 bMaxPacketSize0;
  254. u8 idVendor[2];
  255. u8 idProduct[2];
  256. u8 bcdDev[2];
  257. u8 iManufacturer;
  258. u8 iProduct;
  259. u8 iSerialNumber;
  260. u8 bNumConfigurations;
  261. };
  262. struct DConf
  263. {
  264. u8 bLength;
  265. u8 bDescriptorType;
  266. u8 wTotalLength[2];
  267. u8 bNumInterfaces;
  268. u8 bConfigurationValue;
  269. u8 iConfiguration;
  270. u8 bmAttributes;
  271. u8 MaxPower;
  272. };
  273. struct DIface
  274. {
  275. u8 bLength;
  276. u8 bDescriptorType;
  277. u8 bInterfaceNumber;
  278. u8 bAlternateSetting;
  279. u8 bNumEndpoints;
  280. u8 bInterfaceClass;
  281. u8 bInterfaceSubClass;
  282. u8 bInterfaceProtocol;
  283. u8 iInterface;
  284. };
  285. struct DEp
  286. {
  287. u8 bLength;
  288. u8 bDescriptorType;
  289. u8 bEndpointAddress;
  290. u8 bmAttributes;
  291. u8 wMaxPacketSize[2];
  292. u8 bInterval;
  293. };
  294. #define Class(csp) ((csp) & 0xff)
  295. #define Subclass(csp) (((csp)>>8) & 0xff)
  296. #define Proto(csp) (((csp)>>16) & 0xff)
  297. #define CSP(c, s, p) ((c) | (s)<<8 | (p)<<16)
  298. #define GET2(p) (((p)[1] & 0xFF)<<8 | ((p)[0] & 0xFF))
  299. #define PUT2(p,v) {(p)[0] = (v); (p)[1] = (v)>>8;}
  300. #define GET4(p) (((p)[3]&0xFF)<<24 | ((p)[2]&0xFF)<<16 | \
  301. ((p)[1]&0xFF)<<8 | ((p)[0]&0xFF))
  302. /* These were macros. Let's start making them type safe with inline includes.
  303. * The use of macros is a puzzle given the Ken C toolchain's inlining at link time
  304. * abilities, but ...
  305. */
  306. static inline void PUT4(u8 *p, u32 v)
  307. {
  308. (p)[0] = (v);
  309. (p)[1] = (v)>>8;
  310. (p)[2] = (v)>>16;
  311. (p)[3] = (v)>>24;
  312. }
  313. #define dprint if(usbdebug)fprint
  314. #define ddprint if(usbdebug > 1)fprint
  315. int Ufmt(Fmt *f);
  316. char* classname(int c);
  317. void closedev(Dev *d);
  318. int configdev(Dev *d);
  319. int devctl(Dev *dev, char *fmt, ...);
  320. void* emallocz(u32 size, int zero);
  321. char* estrdup(char *s);
  322. int matchdevcsp(char *info, void *a);
  323. int finddevs(int (*matchf)(char*,void*), void *farg, char** dirs, int ndirs);
  324. char* hexstr(void *a, int n);
  325. int loaddevconf(Dev *d, int n);
  326. int loaddevdesc(Dev *d);
  327. char* loaddevstr(Dev *d, int sid);
  328. Dev* opendev(char *fn);
  329. int opendevdata(Dev *d, int mode);
  330. Dev* openep(Dev *d, int id);
  331. int parseconf(Usbdev *d, Conf *c, u8 *b, int n);
  332. int parsedesc(Usbdev *d, Conf *c, u8 *b, int n);
  333. int parsedev(Dev *xd, u8 *b, int n);
  334. void startdevs(char *args, char *argv[], int argc, int (*mf)(char*,void*), void*ma, int (*df)(Dev*,int,char**));
  335. int unstall(Dev *dev, Dev *ep, int dir);
  336. int usbcmd(Dev *d, int type, int req, int value, int index, u8 *data,
  337. int count);
  338. extern int usbdebug; /* more messages for bigger values */