usb.h 6.0 KB

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