usb.c 22 KB

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