usb.c 17 KB

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