usb.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /*
  10. * common USB definitions.
  11. */
  12. #define dprint if(debug)print
  13. #define ddprint if(debug>1)print
  14. #define deprint if(debug || ep->debug)print
  15. #define ddeprint if(debug>1 || ep->debug>1)print
  16. #define GET2(p) ((((p)[1]&0xFF)<<8)|((p)[0]&0xFF))
  17. #define PUT2(p,v) {((p)[0] = (v)); ((p)[1] = (v)>>8);}
  18. typedef struct Udev Udev; /* USB device */
  19. typedef struct Ep Ep; /* Endpoint */
  20. typedef struct Hci Hci; /* Host Controller Interface */
  21. typedef struct Hciimpl Hciimpl; /* Link to the controller impl. */
  22. enum
  23. {
  24. /* fundamental constants */
  25. Ndeveps = 16, /* max nb. of endpoints per device */
  26. /* tunable parameters */
  27. Nhcis = 16, /* max nb. of HCIs */
  28. Neps = 64, /* max nb. of endpoints */
  29. Maxctllen = 32*1024, /* max allowed sized for ctl. xfers; see Maxdevconf */
  30. Xfertmout = 2000, /* default request time out (ms) */
  31. /* transfer types. keep this order */
  32. Tnone = 0, /* no tranfer type configured */
  33. Tctl, /* wr req + rd/wr data + wr/rd sts */
  34. Tiso, /* stream rd or wr (real time) */
  35. Tbulk, /* stream rd or wr */
  36. Tintr, /* msg rd or wr */
  37. Nttypes, /* number of transfer types */
  38. Epmax = 0xF, /* max ep. addr */
  39. Devmax = 0x7F, /* max dev. addr */
  40. /* Speeds */
  41. Fullspeed = 0,
  42. Lowspeed,
  43. Highspeed,
  44. Nospeed,
  45. /* request type */
  46. Rh2d = 0<<7,
  47. Rd2h = 1<<7,
  48. Rstd = 0<<5,
  49. Rclass = 1<<5,
  50. Rdev = 0,
  51. Rep = 2,
  52. Rother = 3,
  53. /* req offsets */
  54. Rtype = 0,
  55. Rreq = 1,
  56. Rvalue = 2,
  57. Rindex = 4,
  58. Rcount = 6,
  59. Rsetuplen = 8,
  60. /* standard requests */
  61. Rgetstatus = 0,
  62. Rclearfeature = 1,
  63. Rsetfeature = 3,
  64. Rsetaddr = 5,
  65. Rgetdesc = 6,
  66. /* device states */
  67. Dconfig = 0, /* configuration in progress */
  68. Denabled, /* address assigned */
  69. Ddetach, /* device is detached */
  70. Dreset, /* its port is being reset */
  71. /* (root) Hub reply to port status (reported to usbd) */
  72. HPpresent = 0x1,
  73. HPenable = 0x2,
  74. HPsuspend = 0x4,
  75. HPovercurrent = 0x8,
  76. HPreset = 0x10,
  77. HPpower = 0x100,
  78. HPslow = 0x200,
  79. HPhigh = 0x400,
  80. HPstatuschg = 0x10000,
  81. HPchange = 0x20000,
  82. };
  83. /*
  84. * Services provided by the driver.
  85. * epopen allocates hardware structures to prepare the endpoint
  86. * for I/O. This happens when the user opens the data file.
  87. * epclose releases them. This happens when the data file is closed.
  88. * epwrite tries to write the given bytes, waiting until all of them
  89. * have been written (or failed) before returning; but not for Iso.
  90. * epread does the same for reading.
  91. * It can be assumed that endpoints are DMEXCL but concurrent
  92. * read/writes may be issued and the controller must take care.
  93. * For control endpoints, device-to-host requests must be followed by
  94. * a read of the expected length if needed.
  95. * The port requests are called when usbd issues commands for root
  96. * hubs. Port status must return bits as a hub request would do.
  97. * Toggle handling and other details are left for the controller driver
  98. * to avoid mixing too much the controller and the comon device.
  99. * While an endpoint is closed, its toggles are saved in the Ep struct.
  100. */
  101. struct Hciimpl
  102. {
  103. void *aux; /* for controller info */
  104. void (*init)(Hci*); /* init. controller */
  105. void (*dump)(Hci*); /* debug */
  106. void (*interrupt)(Ureg*, void*); /* service interrupt */
  107. void (*epopen)(Ep*); /* prepare ep. for I/O */
  108. void (*epclose)(Ep*); /* terminate I/O on ep. */
  109. int32_t (*epread)(Ep*,void*,int32_t); /* transmit data for ep */
  110. int32_t (*epwrite)(Ep*,void*,int32_t); /* receive data for ep */
  111. char* (*seprintep)(char*,char*,Ep*); /* debug */
  112. int (*portenable)(Hci*, int, int); /* enable/disable port */
  113. int (*portreset)(Hci*, int, int); /* set/clear port reset */
  114. int (*portstatus)(Hci*, int); /* get port status */
  115. void (*shutdown)(Hci*); /* shutdown for reboot */
  116. void (*debug)(Hci*, int); /* set/clear debug flag */
  117. };
  118. struct Hci
  119. {
  120. ISAConf ISAConf; /* hardware info */
  121. int tbdf; /* type+busno+devno+funcno */
  122. int ctlrno; /* controller number */
  123. int nports; /* number of ports in hub */
  124. int highspeed;
  125. Hciimpl Hciimpl; /* HCI driver */
  126. };
  127. /*
  128. * USB endpoint.
  129. * All endpoints are kept in a global array. The first
  130. * block of fields is constant after endpoint creation.
  131. * The rest is configuration information given to all controllers.
  132. * The first endpoint for a device (known as ep0) represents the
  133. * device and is used to configure it and create other endpoints.
  134. * Its QLock also protects per-device data in dev.
  135. * See Hciimpl for clues regarding how this is used by controllers.
  136. */
  137. struct Ep
  138. {
  139. Ref r; /* one per fid (and per dev ep for ep0s) */
  140. /* const once inited. */
  141. int idx; /* index in global eps array */
  142. int nb; /* endpoint number in device */
  143. Hci* hp; /* HCI it beint32_ts to */
  144. Udev* dev; /* device for the endpoint */
  145. Ep* ep0; /* control endpoint for its device */
  146. QLock ql; /* protect fields below */
  147. char* name; /* for ep file names at #u/ */
  148. int inuse; /* endpoint is open */
  149. int mode; /* OREAD, OWRITE, or ORDWR */
  150. int clrhalt; /* true if halt was cleared on ep. */
  151. int debug; /* per endpoint debug flag */
  152. char* info; /* for humans to read */
  153. int32_t maxpkt; /* maximum packet size */
  154. int ttype; /* tranfer type */
  155. uint32_t load; /* in µs, for a fransfer of maxpkt bytes */
  156. void* aux; /* for controller specific info */
  157. int rhrepl; /* fake root hub replies */
  158. int toggle[2]; /* saved toggles (while ep is not in use) */
  159. int32_t pollival; /* poll interval ([µ]frames; intr/iso) */
  160. int32_t hz; /* poll frequency (iso) */
  161. int32_t samplesz; /* sample size (iso) */
  162. int ntds; /* nb. of Tds per µframe */
  163. int tmout; /* 0 or timeout for transfers (ms) */
  164. };
  165. /*
  166. * Per-device configuration and cached list of endpoints.
  167. * eps[0]->QLock protects it.
  168. */
  169. struct Udev
  170. {
  171. int nb; /* USB device number */
  172. int state; /* state for the device */
  173. int ishub; /* hubs can allocate devices */
  174. int isroot; /* is a root hub */
  175. int speed; /* Full/Low/High/No -speed */
  176. int hub; /* dev number for the parent hub */
  177. int port; /* port number in the parent hub */
  178. Ep* eps[Ndeveps]; /* end points for this device (cached) */
  179. };
  180. void addhcitype(char *type, int (*reset)(Hci*));
  181. extern char *usbmodename[];
  182. extern char Estalled[];
  183. extern char *seprintdata(char*,char*,unsigned char*,int);