usb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /* USB core: device enumeration, descriptor parsing, hub recursion, bus scans
  2. and the hotplug watcher. Controller-specific work goes through the
  3. usb_hcd_ops vtable (uhci.c / ohci.c). */
  4. #include "io.h"
  5. #include "timer.h"
  6. #include "usb.h"
  7. #include "usb_hcd.h"
  8. /* Standard request types. */
  9. #define RT_DEV2HOST_STD 0x80u
  10. #define RT_HOST2DEV_STD 0x00u
  11. #define RT_DEV2HOST_CLS 0xA0u
  12. #define RT_DEV2HOST_OTH 0xA3u
  13. #define RT_HOST2DEV_OTH 0x23u
  14. /* Standard requests. */
  15. #define REQ_GET_STATUS 0x00u
  16. #define REQ_CLEAR_FEATURE 0x01u
  17. #define REQ_SET_FEATURE 0x03u
  18. #define REQ_SET_ADDRESS 0x05u
  19. #define REQ_GET_DESCRIPTOR 0x06u
  20. /* Descriptor types. */
  21. #define DESC_DEVICE 0x01u
  22. #define DESC_CONFIG 0x02u
  23. #define DESC_INTERFACE 0x04u
  24. #define DESC_ENDPOINT 0x05u
  25. #define DESC_HUB 0x29u
  26. /* Hub feature selectors. */
  27. #define FEAT_PORT_RESET 4u
  28. #define FEAT_PORT_POWER 8u
  29. #define FEAT_C_PORT_CONNECTION 16u
  30. /* Hub port status bits. */
  31. #define PSTAT_CONNECT 0x0001u
  32. #define PSTAT_LOW_SPEED 0x0200u
  33. /* How much of a configuration descriptor we read/parse. */
  34. #define USB_CONFIG_MAX 128
  35. /* Hotplug watcher period. */
  36. #define HOTPLUG_MS 250
  37. /* Bring-up diagnostics to COM1 (also mirrored by the network console on
  38. boards without serial). Set to 0 to silence. */
  39. #define USB_DEBUG 0
  40. #if USB_DEBUG
  41. #include "serial.h"
  42. static void dbg(const char *s)
  43. {
  44. serial_puts(SERIAL_COM1, s);
  45. }
  46. static void dbg_dec(const char *s, unsigned int n)
  47. {
  48. serial_puts(SERIAL_COM1, s);
  49. serial_putdec(SERIAL_COM1, n);
  50. serial_putc(SERIAL_COM1, '\n');
  51. }
  52. static void dbg_port(unsigned int ctrl, unsigned int port, unsigned int conn,
  53. unsigned int low)
  54. {
  55. serial_puts(SERIAL_COM1, "usb: ctrl ");
  56. serial_putdec(SERIAL_COM1, ctrl);
  57. serial_puts(SERIAL_COM1, " port ");
  58. serial_putdec(SERIAL_COM1, port);
  59. serial_puts(SERIAL_COM1, " connected ");
  60. serial_putdec(SERIAL_COM1, conn);
  61. serial_puts(SERIAL_COM1, " ls ");
  62. serial_putdec(SERIAL_COM1, low);
  63. serial_putc(SERIAL_COM1, '\n');
  64. }
  65. #else
  66. static void dbg(const char *s) { (void)s; }
  67. static void dbg_dec(const char *s, unsigned int n) { (void)s; (void)n; }
  68. static void dbg_port(unsigned int ctrl, unsigned int port, unsigned int conn,
  69. unsigned int low)
  70. {
  71. (void)ctrl;
  72. (void)port;
  73. (void)conn;
  74. (void)low;
  75. }
  76. #endif
  77. static const struct usb_hcd_ops *hcd;
  78. static unsigned int generation;
  79. static unsigned int next_addr[USB_HCD_MAX_CTRL];
  80. static usb_probe_fn current_probe;
  81. static int print_devices;
  82. static int scan_busy;
  83. static unsigned char dev_desc[18];
  84. static unsigned char desc8[8];
  85. static unsigned char hub_desc[16];
  86. static unsigned char port_st[4];
  87. static unsigned char conf_hdr[9];
  88. static unsigned char conf_buf[USB_CONFIG_MAX];
  89. static void put_uint(unsigned int n)
  90. {
  91. char buf[12];
  92. char *p = buf + sizeof(buf) - 1;
  93. *p = '\0';
  94. if (n == 0) {
  95. io_putchar('0');
  96. return;
  97. }
  98. while (n) {
  99. *--p = (char)('0' + (n % 10));
  100. n /= 10;
  101. }
  102. while (*p)
  103. io_putchar(*p++);
  104. }
  105. static void put_hex(unsigned int n, unsigned int digits)
  106. {
  107. static const char hex[] = "0123456789ABCDEF";
  108. unsigned int i;
  109. for (i = 0; i < digits; i++)
  110. io_putchar(hex[(n >> ((digits - 1 - i) * 4)) & 0xF]);
  111. }
  112. static void msleep(unsigned int ms)
  113. {
  114. unsigned int t0 = get_ticks_ms();
  115. while (get_ticks_ms() - t0 < ms)
  116. ;
  117. }
  118. static void indent(unsigned int depth)
  119. {
  120. unsigned int i;
  121. for (i = 0; i < depth; i++)
  122. io_putstr(" ");
  123. }
  124. static const char *class_name(unsigned int cls)
  125. {
  126. switch (cls) {
  127. case 0x00: return "Per-interface";
  128. case 0x01: return "Audio";
  129. case 0x02: return "Communications";
  130. case 0x03: return "Human Interface";
  131. case 0x05: return "Physical";
  132. case 0x06: return "Image";
  133. case 0x07: return "Printer";
  134. case 0x08: return "Mass Storage";
  135. case 0x09: return "Hub";
  136. case 0x0A: return "CDC Data";
  137. case 0x0B: return "Smart Card";
  138. case 0x0D: return "Content Security";
  139. case 0x0E: return "Video";
  140. case 0x0F: return "Personal Healthcare";
  141. case 0xE0: return "Wireless";
  142. case 0xEF: return "Miscellaneous";
  143. case 0xFE: return "Application";
  144. case 0xFF: return "Vendor-specific";
  145. default: return "Unknown";
  146. }
  147. }
  148. /* wPortStatus (little-endian) from the GET_PORT_STATUS buffer. */
  149. static unsigned int port_status_word(void)
  150. {
  151. return (unsigned int)port_st[0] | ((unsigned int)port_st[1] << 8);
  152. }
  153. int usb_control_in(unsigned int ctrl, unsigned int addr, unsigned int mps,
  154. int low_speed, unsigned char rt, unsigned char req,
  155. unsigned short value, unsigned short index,
  156. unsigned char *buf, unsigned int len)
  157. {
  158. unsigned char setup[8];
  159. if (!hcd)
  160. return -1;
  161. setup[0] = rt;
  162. setup[1] = req;
  163. setup[2] = (unsigned char)(value & 0xFF);
  164. setup[3] = (unsigned char)((value >> 8) & 0xFF);
  165. setup[4] = (unsigned char)(index & 0xFF);
  166. setup[5] = (unsigned char)((index >> 8) & 0xFF);
  167. setup[6] = (unsigned char)(len & 0xFF);
  168. setup[7] = (unsigned char)((len >> 8) & 0xFF);
  169. return hcd->control_transfer(ctrl, addr, mps, low_speed, setup, buf, len, 1);
  170. }
  171. int usb_control_out(unsigned int ctrl, unsigned int addr, unsigned int mps,
  172. int low_speed, unsigned char rt, unsigned char req,
  173. unsigned short value, unsigned short index)
  174. {
  175. unsigned char setup[8];
  176. if (!hcd)
  177. return -1;
  178. setup[0] = rt;
  179. setup[1] = req;
  180. setup[2] = (unsigned char)(value & 0xFF);
  181. setup[3] = (unsigned char)((value >> 8) & 0xFF);
  182. setup[4] = (unsigned char)(index & 0xFF);
  183. setup[5] = (unsigned char)((index >> 8) & 0xFF);
  184. setup[6] = 0;
  185. setup[7] = 0;
  186. return hcd->control_transfer(ctrl, addr, mps, low_speed, setup, 0, 0, 0);
  187. }
  188. /* Read the configuration descriptor and hand every interface with an
  189. interrupt-IN endpoint to the active probe. */
  190. static void probe_config(unsigned int ctrl, unsigned int addr, unsigned int mps,
  191. int low, unsigned int port, unsigned int vendor,
  192. unsigned int product)
  193. {
  194. struct usb_iface_info info;
  195. unsigned int total, i;
  196. int have_iface = 0;
  197. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  198. REQ_GET_DESCRIPTOR,
  199. (unsigned short)(DESC_CONFIG << 8), 0,
  200. conf_hdr, 9) < 0) {
  201. dbg("usb: config descriptor header failed\n");
  202. return;
  203. }
  204. total = (unsigned int)conf_hdr[2] | ((unsigned int)conf_hdr[3] << 8);
  205. if (total < 9)
  206. return;
  207. if (total > USB_CONFIG_MAX)
  208. total = USB_CONFIG_MAX;
  209. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  210. REQ_GET_DESCRIPTOR,
  211. (unsigned short)(DESC_CONFIG << 8), 0,
  212. conf_buf, total) < 0) {
  213. dbg("usb: config descriptor read failed\n");
  214. return;
  215. }
  216. dbg_dec("usb: config descriptor len ", total);
  217. info.ctrl = ctrl;
  218. info.addr = addr;
  219. info.mps = mps;
  220. info.low_speed = low;
  221. info.port = port;
  222. info.vendor = vendor;
  223. info.product = product;
  224. info.config_value = conf_buf[5];
  225. info.cls = 0;
  226. info.subcls = 0;
  227. info.proto = 0;
  228. info.iface_num = 0;
  229. info.ep_addr = 0;
  230. info.ep_mps = 0;
  231. info.ep_interval = 0;
  232. i = 0;
  233. while (i + 2 <= total) {
  234. unsigned int blen = conf_buf[i];
  235. unsigned int btype = conf_buf[i + 1];
  236. if (blen < 2 || i + blen > total)
  237. break;
  238. if (btype == DESC_INTERFACE && blen >= 9) {
  239. if (have_iface && info.ep_addr) {
  240. dbg("usb: interface has interrupt-IN ep\n");
  241. if (current_probe)
  242. current_probe(&info);
  243. }
  244. info.cls = conf_buf[i + 5];
  245. info.subcls = conf_buf[i + 6];
  246. info.proto = conf_buf[i + 7];
  247. info.iface_num = conf_buf[i + 2];
  248. if (print_devices) {
  249. io_putstr(" Interface ");
  250. put_uint(info.iface_num);
  251. io_putstr(": ");
  252. put_hex(info.cls, 2);
  253. io_putchar('/');
  254. put_hex(info.subcls, 2);
  255. io_putchar('/');
  256. put_hex(info.proto, 2);
  257. io_putchar('\n');
  258. } else {
  259. dbg_dec("usb: iface class ", info.cls);
  260. dbg_dec("usb: iface subclass ", info.subcls);
  261. dbg_dec("usb: iface protocol ", info.proto);
  262. }
  263. info.ep_addr = 0;
  264. info.ep_mps = 0;
  265. info.ep_interval = 0;
  266. have_iface = 1;
  267. } else if (btype == DESC_ENDPOINT && blen >= 7 && have_iface) {
  268. unsigned int epa = conf_buf[i + 2];
  269. unsigned int attr = conf_buf[i + 3] & 3u;
  270. unsigned int mps = ((unsigned int)conf_buf[i + 4] |
  271. ((unsigned int)conf_buf[i + 5] << 8)) & 0x7FFu;
  272. if (print_devices) {
  273. io_putstr(" Endpoint 0x");
  274. put_hex(epa, 2);
  275. io_putstr(attr == 3u ? " interrupt" : " other");
  276. io_putstr(", mps ");
  277. put_uint(mps);
  278. io_putchar('\n');
  279. }
  280. if (!info.ep_addr && (epa & 0x80u) && attr == 3u) {
  281. info.ep_addr = epa;
  282. info.ep_mps = mps;
  283. info.ep_interval = conf_buf[i + 6];
  284. dbg_dec("usb: interrupt-IN endpoint ", epa & 0x0Fu);
  285. }
  286. }
  287. i += blen;
  288. }
  289. if (have_iface && info.ep_addr && current_probe)
  290. current_probe(&info);
  291. }
  292. static void enumerate_hub(unsigned int ctrl, unsigned int addr, unsigned int mps,
  293. int low, unsigned int port, unsigned int depth);
  294. static void enumerate_device(unsigned int ctrl, unsigned int addr,
  295. unsigned int mps, int low, unsigned int port,
  296. unsigned int depth);
  297. static void enumerate_new_device(unsigned int ctrl, unsigned int port, int low,
  298. unsigned int depth);
  299. static void enumerate_device(unsigned int ctrl, unsigned int addr,
  300. unsigned int mps, int low, unsigned int port,
  301. unsigned int depth)
  302. {
  303. unsigned int vendor, product, cls, subcls, proto;
  304. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  305. REQ_GET_DESCRIPTOR,
  306. (unsigned short)(DESC_DEVICE << 8), 0,
  307. dev_desc, 18) < 0) {
  308. dbg("usb: device descriptor read failed\n");
  309. if (print_devices) {
  310. indent(depth);
  311. io_putstr("(device descriptor read failed)\n");
  312. }
  313. return;
  314. }
  315. vendor = dev_desc[8] | ((unsigned int)dev_desc[9] << 8);
  316. product = dev_desc[10] | ((unsigned int)dev_desc[11] << 8);
  317. cls = dev_desc[4];
  318. subcls = dev_desc[5];
  319. proto = dev_desc[6];
  320. if (print_devices) {
  321. indent(depth);
  322. io_putstr("Bus ");
  323. put_uint(ctrl + 1);
  324. io_putstr(" Device ");
  325. put_uint(addr);
  326. io_putstr(": ID ");
  327. put_hex(vendor, 4);
  328. io_putchar(':');
  329. put_hex(product, 4);
  330. io_putchar(' ');
  331. io_putstr(class_name(cls));
  332. io_putstr(" [");
  333. put_hex(cls, 2);
  334. io_putchar('/');
  335. put_hex(subcls, 2);
  336. io_putchar('/');
  337. put_hex(proto, 2);
  338. io_putstr("]\n");
  339. }
  340. if ((current_probe || print_devices) && cls != 0x09)
  341. probe_config(ctrl, addr, mps, low, port, vendor, product);
  342. if (cls == 0x09)
  343. enumerate_hub(ctrl, addr, mps, low, port, depth + 1);
  344. }
  345. static void enumerate_hub(unsigned int ctrl, unsigned int addr, unsigned int mps,
  346. int low, unsigned int port, unsigned int depth)
  347. {
  348. unsigned int nports, p;
  349. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_CLS,
  350. REQ_GET_DESCRIPTOR,
  351. (unsigned short)(DESC_HUB << 8), 0, hub_desc,
  352. sizeof(hub_desc)) < 0)
  353. return;
  354. nports = hub_desc[2];
  355. if (nports > 8)
  356. nports = 8;
  357. for (p = 1; p <= nports; p++) {
  358. int low2;
  359. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_OTH,
  360. REQ_GET_STATUS, 0, (unsigned short)p,
  361. port_st, 4) < 0)
  362. continue;
  363. if (!(port_status_word() & PSTAT_CONNECT))
  364. continue;
  365. /* Power and reset the downstream port. */
  366. usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
  367. REQ_SET_FEATURE, FEAT_PORT_POWER,
  368. (unsigned short)p);
  369. if (usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
  370. REQ_SET_FEATURE, FEAT_PORT_RESET,
  371. (unsigned short)p) < 0)
  372. continue;
  373. msleep(30);
  374. usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
  375. REQ_CLEAR_FEATURE, FEAT_C_PORT_CONNECTION,
  376. (unsigned short)p);
  377. /* Re-read status to learn the child's speed. */
  378. low2 = 0;
  379. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_OTH,
  380. REQ_GET_STATUS, 0, (unsigned short)p,
  381. port_st, 4) >= 0) {
  382. if (port_status_word() & PSTAT_LOW_SPEED)
  383. low2 = 1;
  384. }
  385. enumerate_new_device(ctrl, port, low2, depth);
  386. }
  387. }
  388. static void enumerate_new_device(unsigned int ctrl, unsigned int port, int low,
  389. unsigned int depth)
  390. {
  391. unsigned int mps;
  392. unsigned int addr = next_addr[ctrl];
  393. if (addr > 127) {
  394. if (print_devices) {
  395. indent(depth);
  396. io_putstr("(too many devices)\n");
  397. }
  398. return;
  399. }
  400. /* First 8 bytes at address 0 (learn max packet size). */
  401. if (usb_control_in(ctrl, 0, 8, low, RT_DEV2HOST_STD,
  402. REQ_GET_DESCRIPTOR,
  403. (unsigned short)(DESC_DEVICE << 8), 0,
  404. desc8, 8) < 0) {
  405. dbg("usb: no response at address 0\n");
  406. if (print_devices) {
  407. indent(depth);
  408. io_putstr("(no response at address 0)\n");
  409. }
  410. return;
  411. }
  412. mps = desc8[7];
  413. if (mps == 0)
  414. mps = 8;
  415. /* Assign an address. */
  416. if (usb_control_out(ctrl, 0, 8, low, RT_HOST2DEV_STD,
  417. REQ_SET_ADDRESS, (unsigned short)addr, 0) < 0) {
  418. dbg("usb: SET_ADDRESS failed\n");
  419. if (print_devices) {
  420. indent(depth);
  421. io_putstr("(SET_ADDRESS failed)\n");
  422. }
  423. return;
  424. }
  425. dbg_dec("usb: enumerated device, addr ", addr);
  426. msleep(5);
  427. next_addr[ctrl] = addr + 1;
  428. enumerate_device(ctrl, addr, mps, low, port, depth);
  429. }
  430. int usb_hcd_init(void)
  431. {
  432. if (hcd)
  433. return 0;
  434. if (usb_uhci_ops.init() > 0) {
  435. hcd = &usb_uhci_ops;
  436. return 0;
  437. }
  438. if (usb_ohci_ops.init() > 0) {
  439. hcd = &usb_ohci_ops;
  440. return 0;
  441. }
  442. return -1;
  443. }
  444. const char *usb_hcd_name(void)
  445. {
  446. return hcd ? hcd->name : "none";
  447. }
  448. unsigned int usb_generation(void)
  449. {
  450. return generation;
  451. }
  452. int usb_port_connected(unsigned int ctrl, unsigned int port)
  453. {
  454. if (!hcd)
  455. return 0;
  456. return hcd->port_connected(ctrl, port);
  457. }
  458. int usb_int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr,
  459. unsigned int ep, unsigned int mps, int low_speed)
  460. {
  461. if (!hcd || slot >= USB_HCD_INT_SLOTS)
  462. return -1;
  463. return hcd->int_in_start(ctrl, slot, addr, ep, mps, low_speed);
  464. }
  465. int usb_int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len)
  466. {
  467. if (!hcd || slot >= USB_HCD_INT_SLOTS)
  468. return 0;
  469. return hcd->int_in_poll(slot, buf, len);
  470. }
  471. void usb_int_in_stop(unsigned int slot)
  472. {
  473. if (hcd && slot < USB_HCD_INT_SLOTS)
  474. hcd->int_in_stop(slot);
  475. }
  476. int usb_scan(usb_probe_fn probe)
  477. {
  478. unsigned int c, p;
  479. if (scan_busy)
  480. return -2;
  481. if (usb_hcd_init() != 0)
  482. return -1;
  483. scan_busy = 1;
  484. generation++;
  485. current_probe = probe;
  486. print_devices = (probe == 0);
  487. for (c = 0; c < hcd->count() && c < USB_HCD_MAX_CTRL; c++) {
  488. next_addr[c] = 1;
  489. if (print_devices) {
  490. unsigned int bus, dev, func, base;
  491. int mmio;
  492. hcd->info(c, &bus, &dev, &func, &base, &mmio);
  493. io_putstr(hcd->name);
  494. io_putstr(" Host Controller (PCI ");
  495. put_hex(bus, 2);
  496. io_putchar(':');
  497. put_hex(dev, 2);
  498. io_putchar('.');
  499. put_hex(func, 1);
  500. io_putstr(mmio ? ", MMIO 0x" : ", I/O 0x");
  501. put_hex(base, mmio ? 8 : 4);
  502. io_putstr(")\n");
  503. }
  504. for (p = 0; p < hcd->port_count(c); p++) {
  505. unsigned int conn = (unsigned int)hcd->port_connected(c, p);
  506. dbg_port(c, p, conn,
  507. (unsigned int)hcd->port_low_speed(c, p));
  508. if (!conn)
  509. continue;
  510. hcd->port_take_change(c, p); /* clear stale CSC */
  511. if (hcd->port_reset(c, p) != 0) {
  512. dbg("usb: port reset failed\n");
  513. continue;
  514. }
  515. dbg("usb: port reset ok\n");
  516. enumerate_new_device(c, p, hcd->port_low_speed(c, p), 1);
  517. }
  518. }
  519. current_probe = 0;
  520. print_devices = 0;
  521. scan_busy = 0;
  522. return 0;
  523. }
  524. int usb_hotplug_poll(usb_probe_fn probe)
  525. {
  526. static unsigned int last;
  527. unsigned int now, c, p;
  528. int n = 0;
  529. if (!hcd || scan_busy)
  530. return 0;
  531. now = get_ticks_ms();
  532. if (now - last < HOTPLUG_MS)
  533. return 0;
  534. last = now;
  535. scan_busy = 1;
  536. current_probe = probe;
  537. for (c = 0; c < hcd->count() && c < USB_HCD_MAX_CTRL; c++) {
  538. for (p = 0; p < hcd->port_count(c); p++) {
  539. if (!hcd->port_take_change(c, p))
  540. continue;
  541. if (!hcd->port_connected(c, p))
  542. continue;
  543. if (hcd->port_reset(c, p) != 0)
  544. continue;
  545. enumerate_new_device(c, p, hcd->port_low_speed(c, p), 1);
  546. n++;
  547. }
  548. }
  549. current_probe = 0;
  550. scan_busy = 0;
  551. return n;
  552. }
  553. void usb_enumerate_and_print(void)
  554. {
  555. int r = usb_scan(0);
  556. if (r == -1)
  557. io_putstr("lsusb: no USB host controller found\n");
  558. else if (r == -2)
  559. io_putstr("lsusb: a USB scan is already running\n");
  560. }