usb.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. /* feature selectors */
  97. DEVICE_REMOTE_WAKEUP = 1,
  98. ENDPOINT_STALL = 0,
  99. /* report types */
  100. Tmtype = 3<<2,
  101. Tmitem = 0xF0,
  102. Tmain = 0<<2,
  103. Tinput = 0x80,
  104. Toutput = 0x90,
  105. Tfeature = 0xB0,
  106. Tcoll = 0xA0,
  107. Tecoll = 0xC0,
  108. Tglobal = 1<<2,
  109. Tusagepage = 0x00,
  110. Tlmin = 0x10,
  111. Tlmax = 0x20,
  112. Tpmin = 0x30,
  113. Tpmax = 0x40,
  114. Tunitexp = 0x50,
  115. Tunit = 0x60,
  116. Trepsize = 0x70,
  117. TrepID = 0x80,
  118. Trepcount = 0x90,
  119. Tpush = 0xA0,
  120. Tpop = 0xB0,
  121. Tlocal = 2<<2,
  122. Tusage = 0x00,
  123. Tumin = 0x10,
  124. Tumax = 0x20,
  125. Tdindex = 0x30,
  126. Tdmin = 0x40,
  127. Tdmax = 0x50,
  128. Tsindex = 0x70,
  129. Tsmin = 0x80,
  130. Tsmax = 0x90,
  131. Tsetdelim = 0xA0,
  132. Treserved = 3<<2,
  133. Tlong = 0xFE,
  134. /* parameters */
  135. Nendpt = 16,
  136. /* device state */
  137. Detached = 0,
  138. Attached,
  139. Enabled,
  140. Assigned,
  141. Configured,
  142. /* classes */
  143. Noclass = 0,
  144. Hubclass,
  145. Otherclass,
  146. };
  147. enum
  148. {
  149. CL_AUDIO = 1,
  150. CL_COMMS = 2,
  151. CL_HID = 3,
  152. CL_PRINTER = 7,
  153. CL_STORAGE = 8,
  154. CL_HUB = 9,
  155. CL_DATA = 10,
  156. };
  157. struct Endpt
  158. {
  159. uchar addr; /* endpoint address, 0-15 */
  160. uchar dir; /* direction, Ein/Eout */
  161. uchar type; /* Econtrol, Eiso, Ebulk, Eintr */
  162. uchar isotype; /* Eunknown, Easync, Eadapt, Esync */
  163. int id;
  164. int class;
  165. ulong csp;
  166. int maxpkt;
  167. Device* dev;
  168. Dconf* conf;
  169. Dinf* iface;
  170. };
  171. struct Dalt
  172. {
  173. int attrib;
  174. int interval;
  175. void* devspec; /* device specific settings */
  176. };
  177. struct Dinf
  178. {
  179. int interface; /* interface number */
  180. ulong csp; /* USB class/subclass/proto */
  181. Dalt* dalt[16];
  182. Endpt* endpt[16];
  183. };
  184. struct Dconf
  185. {
  186. ulong csp; /* USB class/subclass/proto */
  187. int nif; /* number of interfaces */
  188. int cval; /* value for set configuration */
  189. int attrib;
  190. int milliamps; /* maximum power in this configuration */
  191. Dinf* iface[16]; /* up to 16 interfaces */
  192. };
  193. /* Dconf.attrib */
  194. enum
  195. {
  196. Cbuspowered = 1<<7,
  197. Cselfpowered = 1<<6,
  198. Cremotewakeup = 1<<5,
  199. };
  200. struct Device
  201. {
  202. Ref;
  203. int ctlrno;
  204. int ctl;
  205. int setup;
  206. int status;
  207. int state;
  208. int id;
  209. int class;
  210. int npt;
  211. int ls; /* low speed */
  212. ulong csp; /* USB class/subclass/proto */
  213. int nconf;
  214. int nif; /* number of interfaces (sum of per-conf `nif's) */
  215. int vid; /* vendor id */
  216. int did; /* product (device) id */
  217. Dconf* config[16];
  218. Endpt* ep[Nendpt];
  219. };
  220. /*
  221. * layout of standard descriptor types
  222. */
  223. struct DDevice
  224. {
  225. byte bLength;
  226. byte bDescriptorType;
  227. byte bcdUSB[2];
  228. byte bDeviceClass;
  229. byte bDeviceSubClass;
  230. byte bDeviceProtocol;
  231. byte bMaxPacketSize0;
  232. byte idVendor[2];
  233. byte idProduct[2];
  234. byte bcdDevice[2];
  235. byte iManufacturer;
  236. byte iProduct;
  237. byte iSerialNumber;
  238. byte bNumConfigurations;
  239. };
  240. #define DDEVLEN 18
  241. struct DConfig
  242. {
  243. byte bLength;
  244. byte bDescriptorType;
  245. byte wTotalLength[2];
  246. byte bNumInterfaces;
  247. byte bConfigurationValue;
  248. byte iConfiguration;
  249. byte bmAttributes;
  250. byte MaxPower;
  251. };
  252. #define DCONFLEN 9
  253. struct DInterface
  254. {
  255. byte bLength;
  256. byte bDescriptorType;
  257. byte bInterfaceNumber;
  258. byte bAlternateSetting;
  259. byte bNumEndpoints;
  260. byte bInterfaceClass;
  261. byte bInterfaceSubClass;
  262. byte bInterfaceProtocol;
  263. byte iInterface;
  264. };
  265. #define DINTERLEN 9
  266. struct DEndpoint
  267. {
  268. byte bLength;
  269. byte bDescriptorType;
  270. byte bEndpointAddress;
  271. byte bmAttributes;
  272. byte wMaxPacketSize[2];
  273. byte bInterval;
  274. };
  275. #define DENDPLEN 7
  276. struct DHid
  277. {
  278. byte bLength;
  279. byte bDescriptorType;
  280. byte bcdHID[2];
  281. byte bCountryCode;
  282. byte bNumDescriptors;
  283. byte bClassDescriptorType;
  284. byte wItemLength[2];
  285. };
  286. #define DHIDLEN 9
  287. struct DHub
  288. {
  289. byte bLength;
  290. byte bDescriptorType;
  291. byte bNbrPorts;
  292. byte wHubCharacteristics[2];
  293. byte bPwrOn2PwrGood;
  294. byte bHubContrCurrent;
  295. byte DeviceRemovable[1]; /* variable length */
  296. /* byte PortPwrCtrlMask; /* variable length, deprecated in USB v1.1 */
  297. };
  298. #define DHUBLEN 9
  299. struct Namelist
  300. {
  301. short index;
  302. char *name;
  303. };
  304. typedef struct Drivetab
  305. {
  306. ulong csp;
  307. void (*driver)(Device *d);
  308. } Drivetab;
  309. #define Class(csp) ((csp)&0xff)
  310. #define Subclass(csp) (((csp)>>8)&0xff)
  311. #define Proto(csp) (((csp)>>16)&0xff)
  312. #define CSP(c, s, p) ((c) | ((s)<<8) | ((p)<<16))
  313. extern void (*dprinter[0x100])(Device *, int, ulong, void *b, int n);
  314. /*
  315. * format routines
  316. */
  317. void pdesc (Device *, int, ulong, byte *, int);
  318. void preport (Device *, int, ulong, byte *, int);
  319. void pstring (Device *, int, ulong, void *, int);
  320. void phub (Device *, int, ulong, void *, int);
  321. void pdevice (Device *, int, ulong, void *, int);
  322. void phid (Device *, int, ulong, void *, int);
  323. void pcs_raw(char *tag, byte *b, int n);
  324. /*
  325. * interface
  326. */
  327. void usbfmtinit(void);
  328. Device* opendev(int, int);
  329. void closedev(Device*);
  330. int describedevice(Device*);
  331. int loadconfig(Device *d, int n);
  332. Endpt * newendpt(Device *d, int id, ulong csp);
  333. int setupcmd(Endpt*, int, int, int, int, byte*, int);
  334. int setupreq(Endpt*, int, int, int, int, int);
  335. int setupreply(Endpt*, void*, int);
  336. void setdevclass(Device *d, int n);
  337. void * emalloc(ulong);
  338. void * emallocz(ulong, int);
  339. char * namefor(Namelist *, int);
  340. #pragma varargck type "D" Device*