usb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 / ehci.c / xhci.c). */
  4. #include "io.h"
  5. #include "timer.h"
  6. #include "usb.h"
  7. #include "usb_hcd.h"
  8. /* One global controller index space. Each entry maps to one instance of
  9. one controller type; a machine with EHCI + UHCI companions gets several.
  10. USB_HCD_MAX_CTRL is the per-type limit, this is the total. */
  11. #define USB_MAX_CTRL 8
  12. struct usb_ctrl_ref {
  13. const struct usb_hcd_ops *ops;
  14. unsigned int local;
  15. };
  16. /* Standard request types. */
  17. #define RT_DEV2HOST_STD 0x80u
  18. #define RT_HOST2DEV_STD 0x00u
  19. #define RT_DEV2HOST_CLS 0xA0u
  20. #define RT_DEV2HOST_OTH 0xA3u
  21. #define RT_HOST2DEV_OTH 0x23u
  22. /* Standard requests. */
  23. #define REQ_GET_STATUS 0x00u
  24. #define REQ_CLEAR_FEATURE 0x01u
  25. #define REQ_SET_FEATURE 0x03u
  26. #define REQ_SET_ADDRESS 0x05u
  27. #define REQ_GET_DESCRIPTOR 0x06u
  28. #define REQ_SET_CONFIGURATION 0x09u
  29. /* Descriptor types. */
  30. #define DESC_DEVICE 0x01u
  31. #define DESC_CONFIG 0x02u
  32. #define DESC_INTERFACE 0x04u
  33. #define DESC_ENDPOINT 0x05u
  34. #define DESC_HUB 0x29u
  35. /* Hub feature selectors. */
  36. #define FEAT_PORT_RESET 4u
  37. #define FEAT_PORT_POWER 8u
  38. #define FEAT_C_PORT_CONNECTION 16u
  39. #define FEAT_C_PORT_RESET 20u
  40. /* Hub port status bits. */
  41. #define PSTAT_CONNECT 0x0001u
  42. #define PSTAT_ENABLE 0x0002u
  43. #define PSTAT_LOW_SPEED 0x0200u
  44. /* How much of a configuration descriptor we read/parse. */
  45. #define USB_CONFIG_MAX 128
  46. /* Hotplug watcher period. */
  47. #define HOTPLUG_MS 250
  48. /* Bring-up diagnostics to COM1 (also mirrored by the network console on
  49. boards without serial). Set to 0 to silence. */
  50. #define USB_DEBUG 0
  51. #if USB_DEBUG
  52. #include "serial.h"
  53. static void dbg(const char *s)
  54. {
  55. serial_puts(SERIAL_COM1, s);
  56. }
  57. static void dbg_dec(const char *s, unsigned int n)
  58. {
  59. serial_puts(SERIAL_COM1, s);
  60. serial_putdec(SERIAL_COM1, n);
  61. serial_putc(SERIAL_COM1, '\n');
  62. }
  63. static void dbg_port(unsigned int ctrl, unsigned int port, unsigned int conn,
  64. unsigned int low)
  65. {
  66. serial_puts(SERIAL_COM1, "usb: ctrl ");
  67. serial_putdec(SERIAL_COM1, ctrl);
  68. serial_puts(SERIAL_COM1, " port ");
  69. serial_putdec(SERIAL_COM1, port);
  70. serial_puts(SERIAL_COM1, " connected ");
  71. serial_putdec(SERIAL_COM1, conn);
  72. serial_puts(SERIAL_COM1, " ls ");
  73. serial_putdec(SERIAL_COM1, low);
  74. serial_putc(SERIAL_COM1, '\n');
  75. }
  76. #else
  77. static void dbg(const char *s) { (void)s; }
  78. static void dbg_dec(const char *s, unsigned int n) { (void)s; (void)n; }
  79. static void dbg_port(unsigned int ctrl, unsigned int port, unsigned int conn,
  80. unsigned int low)
  81. {
  82. (void)ctrl;
  83. (void)port;
  84. (void)conn;
  85. (void)low;
  86. }
  87. #endif
  88. static struct usb_ctrl_ref usb_ctrls[USB_MAX_CTRL];
  89. static unsigned int n_usb_ctrls;
  90. static int hcd_init_done;
  91. static unsigned int generation;
  92. static unsigned int next_addr[USB_MAX_CTRL];
  93. static const struct usb_hcd_ops *slot_ops[USB_HCD_INT_SLOTS];
  94. static usb_probe_fn current_probe;
  95. static usb_device_probe_fn device_probe;
  96. static int print_devices;
  97. static int scan_busy;
  98. static unsigned char dev_desc[18];
  99. static unsigned char desc8[8];
  100. static unsigned char hub_desc[16];
  101. static unsigned char port_st[4];
  102. static unsigned char conf_hdr[9];
  103. static unsigned char conf_buf[USB_CONFIG_MAX];
  104. static void put_uint(unsigned int n)
  105. {
  106. char buf[12];
  107. char *p = buf + sizeof(buf) - 1;
  108. *p = '\0';
  109. if (n == 0) {
  110. io_putchar('0');
  111. return;
  112. }
  113. while (n) {
  114. *--p = (char)('0' + (n % 10));
  115. n /= 10;
  116. }
  117. while (*p)
  118. io_putchar(*p++);
  119. }
  120. static void put_hex(unsigned int n, unsigned int digits)
  121. {
  122. static const char hex[] = "0123456789ABCDEF";
  123. unsigned int i;
  124. for (i = 0; i < digits; i++)
  125. io_putchar(hex[(n >> ((digits - 1 - i) * 4)) & 0xF]);
  126. }
  127. static void msleep(unsigned int ms)
  128. {
  129. unsigned int t0 = get_ticks_ms();
  130. while (get_ticks_ms() - t0 < ms)
  131. ;
  132. }
  133. static void indent(unsigned int depth)
  134. {
  135. unsigned int i;
  136. for (i = 0; i < depth; i++)
  137. io_putstr(" ");
  138. }
  139. static const char *class_name(unsigned int cls)
  140. {
  141. switch (cls) {
  142. case 0x00: return "Per-interface";
  143. case 0x01: return "Audio";
  144. case 0x02: return "Communications";
  145. case 0x03: return "Human Interface";
  146. case 0x05: return "Physical";
  147. case 0x06: return "Image";
  148. case 0x07: return "Printer";
  149. case 0x08: return "Mass Storage";
  150. case 0x09: return "Hub";
  151. case 0x0A: return "CDC Data";
  152. case 0x0B: return "Smart Card";
  153. case 0x0D: return "Content Security";
  154. case 0x0E: return "Video";
  155. case 0x0F: return "Personal Healthcare";
  156. case 0xE0: return "Wireless";
  157. case 0xEF: return "Miscellaneous";
  158. case 0xFE: return "Application";
  159. case 0xFF: return "Vendor-specific";
  160. default: return "Unknown";
  161. }
  162. }
  163. /* wPortStatus (little-endian) from the GET_PORT_STATUS buffer. */
  164. static unsigned int port_status_word(void)
  165. {
  166. return (unsigned int)port_st[0] | ((unsigned int)port_st[1] << 8);
  167. }
  168. int usb_control_in(unsigned int ctrl, unsigned int addr, unsigned int mps,
  169. int low_speed, unsigned char rt, unsigned char req,
  170. unsigned short value, unsigned short index,
  171. unsigned char *buf, unsigned int len)
  172. {
  173. const struct usb_hcd_ops *ops;
  174. unsigned char setup[8];
  175. if (ctrl >= n_usb_ctrls)
  176. return -1;
  177. ops = usb_ctrls[ctrl].ops;
  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] = (unsigned char)(len & 0xFF);
  185. setup[7] = (unsigned char)((len >> 8) & 0xFF);
  186. return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
  187. low_speed, setup, buf, len, 1);
  188. }
  189. int usb_control_out(unsigned int ctrl, unsigned int addr, unsigned int mps,
  190. int low_speed, unsigned char rt, unsigned char req,
  191. unsigned short value, unsigned short index)
  192. {
  193. const struct usb_hcd_ops *ops;
  194. unsigned char setup[8];
  195. if (ctrl >= n_usb_ctrls)
  196. return -1;
  197. ops = usb_ctrls[ctrl].ops;
  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] = 0;
  205. setup[7] = 0;
  206. return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
  207. low_speed, setup, 0, 0, 0);
  208. }
  209. int usb_control_out_data(unsigned int ctrl, unsigned int addr,
  210. unsigned int mps, int low_speed, unsigned char rt,
  211. unsigned char req, unsigned short value,
  212. unsigned short index, const unsigned char *buf,
  213. unsigned int len)
  214. {
  215. const struct usb_hcd_ops *ops;
  216. unsigned char setup[8];
  217. if (ctrl >= n_usb_ctrls)
  218. return -1;
  219. ops = usb_ctrls[ctrl].ops;
  220. setup[0] = rt;
  221. setup[1] = req;
  222. setup[2] = (unsigned char)(value & 0xFF);
  223. setup[3] = (unsigned char)((value >> 8) & 0xFF);
  224. setup[4] = (unsigned char)(index & 0xFF);
  225. setup[5] = (unsigned char)((index >> 8) & 0xFF);
  226. setup[6] = (unsigned char)(len & 0xFF);
  227. setup[7] = (unsigned char)((len >> 8) & 0xFF);
  228. return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
  229. low_speed, setup, (unsigned char *)buf,
  230. len, 0);
  231. }
  232. /* Read the configuration descriptor and hand every interface with an
  233. interrupt-IN endpoint to the active probe. */
  234. static void probe_config(unsigned int ctrl, unsigned int addr, unsigned int mps,
  235. int low, unsigned int port, unsigned int vendor,
  236. unsigned int product)
  237. {
  238. struct usb_iface_info info;
  239. unsigned int total, i;
  240. int have_iface = 0;
  241. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  242. REQ_GET_DESCRIPTOR,
  243. (unsigned short)(DESC_CONFIG << 8), 0,
  244. conf_hdr, 9) < 0) {
  245. dbg("usb: config descriptor header failed\n");
  246. return;
  247. }
  248. total = (unsigned int)conf_hdr[2] | ((unsigned int)conf_hdr[3] << 8);
  249. if (total < 9)
  250. return;
  251. if (total > USB_CONFIG_MAX)
  252. total = USB_CONFIG_MAX;
  253. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  254. REQ_GET_DESCRIPTOR,
  255. (unsigned short)(DESC_CONFIG << 8), 0,
  256. conf_buf, total) < 0) {
  257. dbg("usb: config descriptor read failed\n");
  258. return;
  259. }
  260. dbg_dec("usb: config descriptor len ", total);
  261. info.ctrl = ctrl;
  262. info.addr = addr;
  263. info.mps = mps;
  264. info.low_speed = low;
  265. info.port = port;
  266. info.vendor = vendor;
  267. info.product = product;
  268. info.config_value = conf_buf[5];
  269. info.cls = 0;
  270. info.subcls = 0;
  271. info.proto = 0;
  272. info.iface_num = 0;
  273. info.ep_addr = 0;
  274. info.ep_mps = 0;
  275. info.ep_interval = 0;
  276. i = 0;
  277. while (i + 2 <= total) {
  278. unsigned int blen = conf_buf[i];
  279. unsigned int btype = conf_buf[i + 1];
  280. if (blen < 2 || i + blen > total)
  281. break;
  282. if (btype == DESC_INTERFACE && blen >= 9) {
  283. if (have_iface && info.ep_addr) {
  284. dbg("usb: interface has interrupt-IN ep\n");
  285. if (current_probe)
  286. current_probe(&info);
  287. }
  288. info.cls = conf_buf[i + 5];
  289. info.subcls = conf_buf[i + 6];
  290. info.proto = conf_buf[i + 7];
  291. info.iface_num = conf_buf[i + 2];
  292. if (print_devices) {
  293. io_putstr(" Interface ");
  294. put_uint(info.iface_num);
  295. io_putstr(": ");
  296. put_hex(info.cls, 2);
  297. io_putchar('/');
  298. put_hex(info.subcls, 2);
  299. io_putchar('/');
  300. put_hex(info.proto, 2);
  301. io_putchar('\n');
  302. } else {
  303. dbg_dec("usb: iface class ", info.cls);
  304. dbg_dec("usb: iface subclass ", info.subcls);
  305. dbg_dec("usb: iface protocol ", info.proto);
  306. }
  307. info.ep_addr = 0;
  308. info.ep_mps = 0;
  309. info.ep_interval = 0;
  310. have_iface = 1;
  311. } else if (btype == DESC_ENDPOINT && blen >= 7 && have_iface) {
  312. unsigned int epa = conf_buf[i + 2];
  313. unsigned int attr = conf_buf[i + 3] & 3u;
  314. unsigned int mps = ((unsigned int)conf_buf[i + 4] |
  315. ((unsigned int)conf_buf[i + 5] << 8)) & 0x7FFu;
  316. if (print_devices) {
  317. io_putstr(" Endpoint 0x");
  318. put_hex(epa, 2);
  319. io_putstr(attr == 3u ? " interrupt" : " other");
  320. io_putstr(", mps ");
  321. put_uint(mps);
  322. io_putchar('\n');
  323. }
  324. if (!info.ep_addr && (epa & 0x80u) && attr == 3u) {
  325. info.ep_addr = epa;
  326. info.ep_mps = mps;
  327. info.ep_interval = conf_buf[i + 6];
  328. dbg_dec("usb: interrupt-IN endpoint ", epa & 0x0Fu);
  329. }
  330. }
  331. i += blen;
  332. }
  333. if (have_iface && info.ep_addr && current_probe)
  334. current_probe(&info);
  335. }
  336. static void enumerate_hub(unsigned int ctrl, unsigned int addr, unsigned int mps,
  337. int low, unsigned int port, unsigned int depth);
  338. static void enumerate_device(unsigned int ctrl, unsigned int addr,
  339. unsigned int mps, int low, unsigned int port,
  340. unsigned int depth);
  341. static void enumerate_new_device(unsigned int ctrl, unsigned int port, int low,
  342. unsigned int depth);
  343. static void enumerate_device(unsigned int ctrl, unsigned int addr,
  344. unsigned int mps, int low, unsigned int port,
  345. unsigned int depth)
  346. {
  347. unsigned int vendor, product, cls, subcls, proto;
  348. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  349. REQ_GET_DESCRIPTOR,
  350. (unsigned short)(DESC_DEVICE << 8), 0,
  351. dev_desc, 18) < 0) {
  352. dbg("usb: device descriptor read failed\n");
  353. if (print_devices) {
  354. indent(depth);
  355. io_putstr("(device descriptor read failed)\n");
  356. }
  357. return;
  358. }
  359. vendor = dev_desc[8] | ((unsigned int)dev_desc[9] << 8);
  360. product = dev_desc[10] | ((unsigned int)dev_desc[11] << 8);
  361. cls = dev_desc[4];
  362. subcls = dev_desc[5];
  363. proto = dev_desc[6];
  364. if (device_probe)
  365. device_probe(ctrl, addr, mps, low, port, vendor, product);
  366. if (print_devices) {
  367. indent(depth);
  368. io_putstr("Bus ");
  369. put_uint(ctrl + 1);
  370. io_putstr(" Device ");
  371. put_uint(addr);
  372. io_putstr(": ID ");
  373. put_hex(vendor, 4);
  374. io_putchar(':');
  375. put_hex(product, 4);
  376. io_putchar(' ');
  377. io_putstr(class_name(cls));
  378. io_putstr(" [");
  379. put_hex(cls, 2);
  380. io_putchar('/');
  381. put_hex(subcls, 2);
  382. io_putchar('/');
  383. put_hex(proto, 2);
  384. io_putstr("]\n");
  385. }
  386. if ((current_probe || print_devices) && cls != 0x09)
  387. probe_config(ctrl, addr, mps, low, port, vendor, product);
  388. if (cls == 0x09)
  389. enumerate_hub(ctrl, addr, mps, low, port, depth + 1);
  390. }
  391. static void enumerate_hub(unsigned int ctrl, unsigned int addr, unsigned int mps,
  392. int low, unsigned int port, unsigned int depth)
  393. {
  394. unsigned char cfg[9];
  395. unsigned int nports, p, i, power_good;
  396. /* Put the hub in its configured state before issuing hub-class
  397. requests. The core does not configure non-HID devices itself, and
  398. some hubs (notably Intel PCH internal hubs) require this. */
  399. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  400. REQ_GET_DESCRIPTOR,
  401. (unsigned short)(DESC_CONFIG << 8), 0, cfg,
  402. sizeof(cfg)) >= 0 && cfg[5] != 0) {
  403. usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_STD,
  404. REQ_SET_CONFIGURATION, (unsigned short)cfg[5],
  405. 0);
  406. msleep(5);
  407. }
  408. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_CLS,
  409. REQ_GET_DESCRIPTOR,
  410. (unsigned short)(DESC_HUB << 8), 0, hub_desc,
  411. sizeof(hub_desc)) < 0)
  412. return;
  413. nports = hub_desc[2];
  414. if (nports > 8)
  415. nports = 8;
  416. /* Power every downstream port before looking at connect status.
  417. An unpowered port reports no connection, so the old order could
  418. miss devices behind a switched hub. */
  419. power_good = (unsigned int)hub_desc[5] * 2;
  420. if (power_good < 20)
  421. power_good = 20;
  422. if (power_good > 500)
  423. power_good = 500;
  424. for (p = 1; p <= nports; p++)
  425. usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
  426. REQ_SET_FEATURE, FEAT_PORT_POWER,
  427. (unsigned short)p);
  428. msleep(power_good);
  429. for (p = 1; p <= nports; p++) {
  430. unsigned int st = 0;
  431. int low2;
  432. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_OTH,
  433. REQ_GET_STATUS, 0, (unsigned short)p,
  434. port_st, 4) < 0)
  435. continue;
  436. st = port_status_word();
  437. if (print_devices) {
  438. unsigned int chg = (unsigned int)port_st[2] |
  439. ((unsigned int)port_st[3] << 8);
  440. indent(depth);
  441. io_putstr("Hub port ");
  442. put_uint(p);
  443. io_putstr(": status 0x");
  444. put_hex(st, 4);
  445. io_putstr(" change 0x");
  446. put_hex(chg, 4);
  447. io_putchar('\n');
  448. }
  449. if (!(st & PSTAT_CONNECT))
  450. continue;
  451. /* Reset the downstream port and wait for it to enable. */
  452. if (usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
  453. REQ_SET_FEATURE, FEAT_PORT_RESET,
  454. (unsigned short)p) < 0)
  455. continue;
  456. for (i = 0; i < 100; i++) {
  457. if (usb_control_in(ctrl, addr, mps, low,
  458. RT_DEV2HOST_OTH, REQ_GET_STATUS, 0,
  459. (unsigned short)p, port_st, 4) < 0)
  460. break;
  461. st = port_status_word();
  462. if (st & PSTAT_ENABLE)
  463. break;
  464. if (port_st[2] & 0x10u) /* C_PORT_RESET */
  465. break;
  466. msleep(5);
  467. }
  468. msleep(10);
  469. usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
  470. REQ_CLEAR_FEATURE, FEAT_C_PORT_RESET,
  471. (unsigned short)p);
  472. usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
  473. REQ_CLEAR_FEATURE, FEAT_C_PORT_CONNECTION,
  474. (unsigned short)p);
  475. /* Re-read status to learn the child's speed. */
  476. low2 = 0;
  477. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_OTH,
  478. REQ_GET_STATUS, 0, (unsigned short)p,
  479. port_st, 4) >= 0) {
  480. st = port_status_word();
  481. if (st & PSTAT_LOW_SPEED)
  482. low2 = 1;
  483. }
  484. if (!(st & PSTAT_ENABLE))
  485. continue;
  486. if (print_devices) {
  487. unsigned int chg = (unsigned int)port_st[2] |
  488. ((unsigned int)port_st[3] << 8);
  489. indent(depth);
  490. io_putstr("Hub port ");
  491. put_uint(p);
  492. io_putstr(" after reset: status 0x");
  493. put_hex(st, 4);
  494. io_putstr(" change 0x");
  495. put_hex(chg, 4);
  496. io_putchar('\n');
  497. }
  498. enumerate_new_device(ctrl, port, low2, depth);
  499. }
  500. }
  501. static void enumerate_new_device(unsigned int ctrl, unsigned int port, int low,
  502. unsigned int depth)
  503. {
  504. unsigned int mps;
  505. unsigned int addr = next_addr[ctrl];
  506. if (addr > 127) {
  507. if (print_devices) {
  508. indent(depth);
  509. io_putstr("(too many devices)\n");
  510. }
  511. return;
  512. }
  513. /* First 8 bytes at address 0 (learn max packet size). */
  514. if (usb_control_in(ctrl, 0, 8, low, RT_DEV2HOST_STD,
  515. REQ_GET_DESCRIPTOR,
  516. (unsigned short)(DESC_DEVICE << 8), 0,
  517. desc8, 8) < 0) {
  518. dbg("usb: no response at address 0\n");
  519. if (print_devices) {
  520. indent(depth);
  521. io_putstr("(no response at address 0)\n");
  522. }
  523. return;
  524. }
  525. mps = desc8[7];
  526. if (mps == 0)
  527. mps = 8;
  528. /* Assign an address. */
  529. if (usb_control_out(ctrl, 0, mps, low, RT_HOST2DEV_STD,
  530. REQ_SET_ADDRESS, (unsigned short)addr, 0) < 0) {
  531. dbg("usb: SET_ADDRESS failed\n");
  532. if (print_devices) {
  533. indent(depth);
  534. io_putstr("(SET_ADDRESS failed)\n");
  535. }
  536. return;
  537. }
  538. dbg_dec("usb: enumerated device, addr ", addr);
  539. msleep(5);
  540. next_addr[ctrl] = addr + 1;
  541. enumerate_device(ctrl, addr, mps, low, port, depth);
  542. }
  543. static void add_ctrl(const struct usb_hcd_ops *ops, unsigned int count)
  544. {
  545. unsigned int i;
  546. for (i = 0; i < count && n_usb_ctrls < USB_MAX_CTRL; i++) {
  547. usb_ctrls[n_usb_ctrls].ops = ops;
  548. usb_ctrls[n_usb_ctrls].local = i;
  549. n_usb_ctrls++;
  550. }
  551. }
  552. int usb_hcd_init(void)
  553. {
  554. unsigned int uhci, ohci, ehci, xhci;
  555. if (hcd_init_done)
  556. return n_usb_ctrls ? 0 : -1;
  557. hcd_init_done = 1;
  558. /* Companions first, so EHCI's CONFIGFLAG claim is the last thing
  559. that happens to the shared ports. */
  560. uhci = (unsigned int)usb_uhci_ops.init();
  561. ohci = (unsigned int)usb_ohci_ops.init();
  562. ehci = (unsigned int)usb_ehci_ops.init();
  563. xhci = (unsigned int)usb_xhci_ops.init();
  564. /* Scan EHCI first: resetting its ports hands low/full-speed devices
  565. back to the companions before those are scanned. */
  566. add_ctrl(&usb_ehci_ops, ehci);
  567. add_ctrl(&usb_uhci_ops, uhci);
  568. add_ctrl(&usb_ohci_ops, ohci);
  569. add_ctrl(&usb_xhci_ops, xhci);
  570. return n_usb_ctrls ? 0 : -1;
  571. }
  572. const char *usb_hcd_name(void)
  573. {
  574. if (!n_usb_ctrls)
  575. return "none";
  576. if (n_usb_ctrls == 1)
  577. return usb_ctrls[0].ops->name;
  578. return "USB";
  579. }
  580. const char *usb_hcd_name_ctrl(unsigned int ctrl)
  581. {
  582. if (ctrl >= n_usb_ctrls)
  583. return "none";
  584. return usb_ctrls[ctrl].ops->name;
  585. }
  586. unsigned int usb_generation(void)
  587. {
  588. return generation;
  589. }
  590. int usb_port_connected(unsigned int ctrl, unsigned int port)
  591. {
  592. if (ctrl >= n_usb_ctrls)
  593. return 0;
  594. return usb_ctrls[ctrl].ops->port_connected(usb_ctrls[ctrl].local,
  595. port);
  596. }
  597. int usb_int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr,
  598. unsigned int ep, unsigned int mps, int low_speed)
  599. {
  600. const struct usb_hcd_ops *ops;
  601. int r;
  602. if (ctrl >= n_usb_ctrls || slot >= USB_HCD_INT_SLOTS)
  603. return -1;
  604. ops = usb_ctrls[ctrl].ops;
  605. if (slot_ops[slot]) { /* stale owner from another HCD */
  606. slot_ops[slot]->int_in_stop(slot);
  607. slot_ops[slot] = 0;
  608. }
  609. r = ops->int_in_start(usb_ctrls[ctrl].local, slot, addr, ep, mps,
  610. low_speed);
  611. slot_ops[slot] = r == 0 ? ops : 0;
  612. return r;
  613. }
  614. int usb_int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len)
  615. {
  616. if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot])
  617. return 0;
  618. return slot_ops[slot]->int_in_poll(slot, buf, len);
  619. }
  620. void usb_int_in_stop(unsigned int slot)
  621. {
  622. if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot])
  623. return;
  624. slot_ops[slot]->int_in_stop(slot);
  625. slot_ops[slot] = 0;
  626. }
  627. void usb_register_device_probe(usb_device_probe_fn fn)
  628. {
  629. device_probe = fn;
  630. }
  631. int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
  632. unsigned int mps, const unsigned char *buf,
  633. unsigned int len)
  634. {
  635. if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_out)
  636. return -1;
  637. return usb_ctrls[ctrl].ops->bulk_out(usb_ctrls[ctrl].local, addr, ep,
  638. mps, buf, len);
  639. }
  640. int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
  641. unsigned int mps, unsigned char *buf, unsigned int len)
  642. {
  643. if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_in)
  644. return -1;
  645. return usb_ctrls[ctrl].ops->bulk_in(usb_ctrls[ctrl].local, addr, ep,
  646. mps, buf, len);
  647. }
  648. int usb_scan(usb_probe_fn probe)
  649. {
  650. unsigned int c, p;
  651. if (scan_busy)
  652. return -2;
  653. if (usb_hcd_init() != 0)
  654. return -1;
  655. scan_busy = 1;
  656. generation++;
  657. current_probe = probe;
  658. print_devices = (probe == 0);
  659. for (c = 0; c < n_usb_ctrls; c++) {
  660. const struct usb_hcd_ops *ops = usb_ctrls[c].ops;
  661. unsigned int local = usb_ctrls[c].local;
  662. next_addr[c] = 1;
  663. if (print_devices) {
  664. unsigned int bus, dev, func, base;
  665. int mmio;
  666. ops->info(local, &bus, &dev, &func, &base, &mmio);
  667. io_putstr(ops->name);
  668. io_putstr(" Host Controller (PCI ");
  669. put_hex(bus, 2);
  670. io_putchar(':');
  671. put_hex(dev, 2);
  672. io_putchar('.');
  673. put_hex(func, 1);
  674. io_putstr(mmio ? ", MMIO 0x" : ", I/O 0x");
  675. put_hex(base, mmio ? 8 : 4);
  676. io_putstr(")\n");
  677. if (ops->print_ports)
  678. ops->print_ports(local);
  679. }
  680. for (p = 0; p < ops->port_count(local); p++) {
  681. unsigned int conn =
  682. (unsigned int)ops->port_connected(local, p);
  683. dbg_port(c, p, conn,
  684. (unsigned int)ops->port_low_speed(local, p));
  685. if (!conn)
  686. continue;
  687. ops->port_take_change(local, p); /* clear stale CSC */
  688. if (ops->port_reset(local, p) != 0) {
  689. dbg("usb: port reset failed\n");
  690. continue;
  691. }
  692. dbg("usb: port reset ok\n");
  693. enumerate_new_device(c, p,
  694. ops->port_low_speed(local, p), 1);
  695. }
  696. if (print_devices && ops->print_ports) {
  697. io_putstr(" After scan:\n");
  698. ops->print_ports(local);
  699. }
  700. }
  701. current_probe = 0;
  702. print_devices = 0;
  703. scan_busy = 0;
  704. return 0;
  705. }
  706. int usb_hotplug_poll(usb_probe_fn probe)
  707. {
  708. static unsigned int last;
  709. unsigned int now, c, p;
  710. int n = 0;
  711. if (!n_usb_ctrls || scan_busy)
  712. return 0;
  713. now = get_ticks_ms();
  714. if (now - last < HOTPLUG_MS)
  715. return 0;
  716. last = now;
  717. scan_busy = 1;
  718. current_probe = probe;
  719. for (c = 0; c < n_usb_ctrls; c++) {
  720. const struct usb_hcd_ops *ops = usb_ctrls[c].ops;
  721. unsigned int local = usb_ctrls[c].local;
  722. for (p = 0; p < ops->port_count(local); p++) {
  723. if (!ops->port_take_change(local, p))
  724. continue;
  725. if (!ops->port_connected(local, p))
  726. continue;
  727. if (ops->port_reset(local, p) != 0)
  728. continue;
  729. enumerate_new_device(c, p,
  730. ops->port_low_speed(local, p), 1);
  731. n++;
  732. }
  733. }
  734. current_probe = 0;
  735. scan_busy = 0;
  736. return n;
  737. }
  738. void usb_enumerate_and_print(void)
  739. {
  740. int r = usb_scan(0);
  741. if (r == -1)
  742. io_putstr("lsusb: no USB host controller found\n");
  743. else if (r == -2)
  744. io_putstr("lsusb: a USB scan is already running\n");
  745. }