usb.h 6.0 KB

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