usb.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* USB core: device enumeration, descriptor parsing, and hub recursion.
  2. Drives the UHCI controller to build the device tree for `lsusb`. */
  3. #include "io.h"
  4. #include "timer.h"
  5. #include "uhci.h"
  6. #include "usb.h"
  7. /* Standard request types. */
  8. #define RT_DEV2HOST_STD 0x80u
  9. #define RT_HOST2DEV_STD 0x00u
  10. #define RT_DEV2HOST_CLS 0xA0u
  11. #define RT_DEV2HOST_OTH 0xA3u
  12. #define RT_HOST2DEV_OTH 0x23u
  13. /* Standard requests. */
  14. #define REQ_GET_STATUS 0x00u
  15. #define REQ_CLEAR_FEATURE 0x01u
  16. #define REQ_SET_FEATURE 0x03u
  17. #define REQ_SET_ADDRESS 0x05u
  18. #define REQ_GET_DESCRIPTOR 0x06u
  19. /* Descriptor types. */
  20. #define DESC_DEVICE 0x01u
  21. #define DESC_HUB 0x29u
  22. /* Hub feature selectors. */
  23. #define FEAT_PORT_RESET 4u
  24. #define FEAT_PORT_POWER 8u
  25. #define FEAT_C_PORT_CONNECTION 16u
  26. /* Hub port status bits. */
  27. #define PSTAT_CONNECT 0x0001u
  28. #define PSTAT_LOW_SPEED 0x0200u
  29. static unsigned int devnum;
  30. static unsigned char dev_desc[18];
  31. static unsigned char desc8[8];
  32. static unsigned char hub_desc[16];
  33. static unsigned char port_st[4];
  34. static void put_uint(unsigned int n)
  35. {
  36. char buf[12];
  37. char *p = buf + sizeof(buf) - 1;
  38. *p = '\0';
  39. if (n == 0) {
  40. io_putchar('0');
  41. return;
  42. }
  43. while (n) {
  44. *--p = (char)('0' + (n % 10));
  45. n /= 10;
  46. }
  47. while (*p)
  48. io_putchar(*p++);
  49. }
  50. static void put_hex(unsigned int n, unsigned int digits)
  51. {
  52. static const char hex[] = "0123456789ABCDEF";
  53. unsigned int i;
  54. for (i = 0; i < digits; i++)
  55. io_putchar(hex[(n >> ((digits - 1 - i) * 4)) & 0xF]);
  56. }
  57. static void msleep(unsigned int ms)
  58. {
  59. unsigned int t0 = get_ticks_ms();
  60. while (get_ticks_ms() - t0 < ms)
  61. ;
  62. }
  63. static int ctrl_in(unsigned int addr, unsigned int mps, int low,
  64. unsigned char rt, unsigned char req, unsigned short value,
  65. unsigned short index, unsigned char *buf, unsigned int len)
  66. {
  67. unsigned char setup[8];
  68. setup[0] = rt;
  69. setup[1] = req;
  70. setup[2] = (unsigned char)(value & 0xFF);
  71. setup[3] = (unsigned char)((value >> 8) & 0xFF);
  72. setup[4] = (unsigned char)(index & 0xFF);
  73. setup[5] = (unsigned char)((index >> 8) & 0xFF);
  74. setup[6] = (unsigned char)(len & 0xFF);
  75. setup[7] = (unsigned char)((len >> 8) & 0xFF);
  76. return uhci_control_transfer(addr, mps, low, setup, buf, len, 1);
  77. }
  78. static int ctrl_out(unsigned int addr, unsigned int mps, int low,
  79. unsigned char rt, unsigned char req, unsigned short value,
  80. unsigned short index)
  81. {
  82. unsigned char setup[8];
  83. setup[0] = rt;
  84. setup[1] = req;
  85. setup[2] = (unsigned char)(value & 0xFF);
  86. setup[3] = (unsigned char)((value >> 8) & 0xFF);
  87. setup[4] = (unsigned char)(index & 0xFF);
  88. setup[5] = (unsigned char)((index >> 8) & 0xFF);
  89. setup[6] = 0;
  90. setup[7] = 0;
  91. return uhci_control_transfer(addr, mps, low, setup, 0, 0, 0);
  92. }
  93. static const char *class_name(unsigned int cls)
  94. {
  95. switch (cls) {
  96. case 0x00: return "Per-interface";
  97. case 0x01: return "Audio";
  98. case 0x02: return "Communications";
  99. case 0x03: return "Human Interface";
  100. case 0x05: return "Physical";
  101. case 0x06: return "Image";
  102. case 0x07: return "Printer";
  103. case 0x08: return "Mass Storage";
  104. case 0x09: return "Hub";
  105. case 0x0A: return "CDC Data";
  106. case 0x0B: return "Smart Card";
  107. case 0x0D: return "Content Security";
  108. case 0x0E: return "Video";
  109. case 0x0F: return "Personal Healthcare";
  110. case 0xE0: return "Wireless";
  111. case 0xEF: return "Miscellaneous";
  112. case 0xFE: return "Application";
  113. case 0xFF: return "Vendor-specific";
  114. default: return "Unknown";
  115. }
  116. }
  117. static void indent(unsigned int depth)
  118. {
  119. unsigned int i;
  120. for (i = 0; i < depth; i++)
  121. io_putstr(" ");
  122. }
  123. /* wPortStatus (little-endian) from the GET_PORT_STATUS buffer. */
  124. static unsigned int port_status_word(void)
  125. {
  126. return (unsigned int)port_st[0] | ((unsigned int)port_st[1] << 8);
  127. }
  128. static void enumerate_hub(unsigned int addr, unsigned int mps, int low,
  129. unsigned int depth);
  130. static void enumerate_device(unsigned int addr, unsigned int mps, int low,
  131. unsigned int depth);
  132. static void enumerate_new_device(int low, unsigned int depth);
  133. static void enumerate_device(unsigned int addr, unsigned int mps, int low,
  134. unsigned int depth)
  135. {
  136. unsigned int vendor, product, cls, subcls, proto;
  137. if (ctrl_in(addr, mps, low, RT_DEV2HOST_STD, REQ_GET_DESCRIPTOR,
  138. (unsigned short)(DESC_DEVICE << 8), 0, dev_desc, 18) < 0) {
  139. indent(depth);
  140. io_putstr("(device descriptor read failed)\n");
  141. return;
  142. }
  143. vendor = dev_desc[8] | ((unsigned int)dev_desc[9] << 8);
  144. product = dev_desc[10] | ((unsigned int)dev_desc[11] << 8);
  145. cls = dev_desc[4];
  146. subcls = dev_desc[5];
  147. proto = dev_desc[6];
  148. indent(depth);
  149. io_putstr("Bus 001 Device ");
  150. put_uint(addr);
  151. io_putstr(": ID ");
  152. put_hex(vendor, 4);
  153. io_putchar(':');
  154. put_hex(product, 4);
  155. io_putchar(' ');
  156. io_putstr(class_name(cls));
  157. io_putstr(" [");
  158. put_hex(cls, 2);
  159. io_putchar('/');
  160. put_hex(subcls, 2);
  161. io_putchar('/');
  162. put_hex(proto, 2);
  163. io_putstr("]\n");
  164. if (cls == 0x09)
  165. enumerate_hub(addr, mps, low, depth + 1);
  166. }
  167. static void enumerate_hub(unsigned int addr, unsigned int mps, int low,
  168. unsigned int depth)
  169. {
  170. unsigned int nports, p;
  171. if (ctrl_in(addr, mps, low, RT_DEV2HOST_CLS, REQ_GET_DESCRIPTOR,
  172. (unsigned short)(DESC_HUB << 8), 0, hub_desc,
  173. sizeof(hub_desc)) < 0)
  174. return;
  175. nports = hub_desc[2];
  176. if (nports > 8)
  177. nports = 8;
  178. for (p = 1; p <= nports; p++) {
  179. int low2;
  180. if (ctrl_in(addr, mps, low, RT_DEV2HOST_OTH, REQ_GET_STATUS,
  181. 0, (unsigned short)p, port_st, 4) < 0)
  182. continue;
  183. if (!(port_status_word() & PSTAT_CONNECT))
  184. continue;
  185. /* Power and reset the downstream port. */
  186. ctrl_out(addr, mps, low, RT_HOST2DEV_OTH, REQ_SET_FEATURE,
  187. FEAT_PORT_POWER, (unsigned short)p);
  188. if (ctrl_out(addr, mps, low, RT_HOST2DEV_OTH, REQ_SET_FEATURE,
  189. FEAT_PORT_RESET, (unsigned short)p) < 0)
  190. continue;
  191. msleep(30);
  192. ctrl_out(addr, mps, low, RT_HOST2DEV_OTH, REQ_CLEAR_FEATURE,
  193. FEAT_C_PORT_CONNECTION, (unsigned short)p);
  194. /* Re-read status to learn the child's speed. */
  195. low2 = 0;
  196. if (ctrl_in(addr, mps, low, RT_DEV2HOST_OTH, REQ_GET_STATUS,
  197. 0, (unsigned short)p, port_st, 4) >= 0) {
  198. if (port_status_word() & PSTAT_LOW_SPEED)
  199. low2 = 1;
  200. }
  201. enumerate_new_device(low2, depth);
  202. }
  203. }
  204. static void enumerate_new_device(int low, unsigned int depth)
  205. {
  206. unsigned int mps;
  207. devnum++;
  208. if (devnum > 127) {
  209. indent(depth);
  210. io_putstr("(too many devices)\n");
  211. return;
  212. }
  213. /* First 8 bytes at address 0 (learn max packet size). */
  214. if (ctrl_in(0, 8, low, RT_DEV2HOST_STD, REQ_GET_DESCRIPTOR,
  215. (unsigned short)(DESC_DEVICE << 8), 0, desc8, 8) < 0) {
  216. indent(depth);
  217. io_putstr("(no response at address 0)\n");
  218. return;
  219. }
  220. mps = desc8[7];
  221. if (mps == 0)
  222. mps = 8;
  223. /* Assign an address. */
  224. if (ctrl_out(0, 8, low, RT_HOST2DEV_STD, REQ_SET_ADDRESS,
  225. (unsigned short)devnum, 0) < 0) {
  226. indent(depth);
  227. io_putstr("(SET_ADDRESS failed)\n");
  228. return;
  229. }
  230. msleep(5);
  231. enumerate_device(devnum, mps, low, depth);
  232. }
  233. void usb_enumerate_and_print(void)
  234. {
  235. unsigned int bus, dev, func, base;
  236. unsigned int p;
  237. devnum = 0;
  238. if (uhci_init() != 0) {
  239. io_putstr("lsusb: no UHCI host controller found\n");
  240. return;
  241. }
  242. uhci_pci_info(&bus, &dev, &func, &base);
  243. io_putstr("UHCI Host Controller (PCI ");
  244. put_hex(bus, 2);
  245. io_putchar(':');
  246. put_hex(dev, 2);
  247. io_putchar('.');
  248. put_hex(func, 1);
  249. io_putstr(", I/O 0x");
  250. put_hex(base, 4);
  251. io_putstr(")\n");
  252. for (p = 0; p < uhci_root_port_count(); p++) {
  253. if (!uhci_root_port_connected(p))
  254. continue;
  255. if (uhci_root_port_reset(p) != 0)
  256. continue;
  257. enumerate_new_device(uhci_root_port_low_speed(p), 1);
  258. }
  259. }