usb.h 5.9 KB

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