usb.h 7.0 KB

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