usb.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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 "process.h"
  6. #include "sched.h"
  7. #include "serial.h"
  8. #include "timer.h"
  9. #include "usb.h"
  10. #include "usb_hcd.h"
  11. #include "vfs.h"
  12. /* One global controller index space. Each entry maps to one instance of
  13. one controller type; a machine with EHCI + UHCI companions gets several.
  14. USB_HCD_MAX_CTRL is the per-type limit, this is the total. */
  15. #define USB_MAX_CTRL 8
  16. struct usb_ctrl_ref {
  17. const struct usb_hcd_ops *ops;
  18. unsigned int local;
  19. };
  20. /* Standard request types. */
  21. #define RT_DEV2HOST_STD 0x80u
  22. #define RT_HOST2DEV_STD 0x00u
  23. #define RT_DEV2HOST_CLS 0xA0u
  24. #define RT_DEV2HOST_OTH 0xA3u
  25. #define RT_HOST2DEV_OTH 0x23u
  26. /* Standard requests. */
  27. #define REQ_GET_STATUS 0x00u
  28. #define REQ_CLEAR_FEATURE 0x01u
  29. #define REQ_SET_FEATURE 0x03u
  30. #define REQ_SET_ADDRESS 0x05u
  31. #define REQ_GET_DESCRIPTOR 0x06u
  32. #define REQ_SET_CONFIGURATION 0x09u
  33. /* Descriptor types. */
  34. #define DESC_DEVICE 0x01u
  35. #define DESC_CONFIG 0x02u
  36. #define DESC_INTERFACE 0x04u
  37. #define DESC_ENDPOINT 0x05u
  38. #define DESC_HUB 0x29u
  39. /* Hub feature selectors. */
  40. #define FEAT_PORT_RESET 4u
  41. #define FEAT_PORT_POWER 8u
  42. #define FEAT_C_PORT_CONNECTION 16u
  43. #define FEAT_C_PORT_RESET 20u
  44. /* Hub port status bits. */
  45. #define PSTAT_CONNECT 0x0001u
  46. #define PSTAT_ENABLE 0x0002u
  47. #define PSTAT_LOW_SPEED 0x0200u
  48. /* How much of a configuration descriptor we read/parse. */
  49. #define USB_CONFIG_MAX 128
  50. /* Hotplug watcher period. */
  51. #define HOTPLUG_MS 250
  52. /* Bring-up diagnostics to COM1 (also mirrored by the network console on
  53. boards without serial). Set to 0 to silence. */
  54. #define USB_DEBUG 0
  55. #if USB_DEBUG
  56. #include "serial.h"
  57. static void dbg(const char *s)
  58. {
  59. serial_puts(SERIAL_COM1, s);
  60. }
  61. static void dbg_dec(const char *s, unsigned int n)
  62. {
  63. serial_puts(SERIAL_COM1, s);
  64. serial_putdec(SERIAL_COM1, n);
  65. serial_putc(SERIAL_COM1, '\n');
  66. }
  67. static void dbg_port(unsigned int ctrl, unsigned int port, unsigned int conn,
  68. unsigned int low)
  69. {
  70. serial_puts(SERIAL_COM1, "usb: ctrl ");
  71. serial_putdec(SERIAL_COM1, ctrl);
  72. serial_puts(SERIAL_COM1, " port ");
  73. serial_putdec(SERIAL_COM1, port);
  74. serial_puts(SERIAL_COM1, " connected ");
  75. serial_putdec(SERIAL_COM1, conn);
  76. serial_puts(SERIAL_COM1, " ls ");
  77. serial_putdec(SERIAL_COM1, low);
  78. serial_putc(SERIAL_COM1, '\n');
  79. }
  80. #else
  81. static void dbg(const char *s) { (void)s; }
  82. static void dbg_dec(const char *s, unsigned int n) { (void)s; (void)n; }
  83. static void dbg_port(unsigned int ctrl, unsigned int port, unsigned int conn,
  84. unsigned int low)
  85. {
  86. (void)ctrl;
  87. (void)port;
  88. (void)conn;
  89. (void)low;
  90. }
  91. #endif
  92. static struct usb_ctrl_ref usb_ctrls[USB_MAX_CTRL];
  93. static unsigned int n_usb_ctrls;
  94. static int hcd_init_done;
  95. static unsigned int generation;
  96. static unsigned int next_addr[USB_MAX_CTRL];
  97. static const struct usb_hcd_ops *slot_ops[USB_HCD_INT_SLOTS];
  98. static usb_probe_fn current_probe;
  99. #define USB_MAX_DEVICE_PROBES 6
  100. static usb_device_probe_fn device_probes[USB_MAX_DEVICE_PROBES];
  101. static unsigned int n_device_probes;
  102. static int print_devices;
  103. static int scan_busy; /* a usb_scan/hotplug pass runs */
  104. /* Bus ownership: a scan, or a mass-storage command issued by the storage
  105. * rescan, claims the controllers for one process. While the owner sleeps
  106. * in a wait loop, the transfer wrappers refuse every other context. */
  107. static unsigned int bus_depth;
  108. static unsigned int bus_owner_pid;
  109. static unsigned int cur_pid(void)
  110. {
  111. struct process *p = current_proc();
  112. return p ? p->pid : 0;
  113. }
  114. int usb_bus_claim(void)
  115. {
  116. unsigned int pid = cur_pid();
  117. if (bus_depth && pid != bus_owner_pid)
  118. return -1;
  119. if (!bus_depth++)
  120. bus_owner_pid = pid;
  121. return 0;
  122. }
  123. void usb_bus_release(void)
  124. {
  125. if (bus_depth && !--bus_depth)
  126. bus_owner_pid = 0;
  127. }
  128. int usb_scan_foreign(void)
  129. {
  130. return bus_depth && cur_pid() != bus_owner_pid;
  131. }
  132. /* 1 when the caller may sleep inside a USB wait: it owns the bus for a scan
  133. * (lsusb, the kworker's HID rescan/hotplug) or for a command issued by the
  134. * storage rescan, and it is not the idle/boot context (pid 0), not inside a
  135. * non-preemptible section and not in an interrupts-off syscall (SYS_WIFI
  136. * runs with IF clear). Other callers (normal file I/O, the installer, NIC
  137. * paths) keep busy-waiting exactly as before. */
  138. static int usb_can_sleep(void)
  139. {
  140. unsigned int flags, pid = cur_pid();
  141. if (!bus_depth || !bus_owner_pid || pid != bus_owner_pid)
  142. return 0;
  143. if (!scan_busy && !(vfs_scan_busy && vfs_scan_owner_pid == pid))
  144. return 0;
  145. if (this_cpu()->preempt_count)
  146. return 0;
  147. __asm__ volatile ("pushfl; popl %0" : "=r"(flags));
  148. return (flags & 0x200u) != 0;
  149. }
  150. void usb_delay_ms(unsigned int ms)
  151. {
  152. unsigned int t0;
  153. if (usb_can_sleep()) {
  154. process_sleep_ms(ms ? ms : 1);
  155. return;
  156. }
  157. t0 = get_ticks_ms();
  158. while (get_ticks_ms() - t0 < ms)
  159. ;
  160. }
  161. /* A yield, not a sleep: it hands the BKL to a waiting process (the WM, a
  162. * terminal) and returns at once when nothing else is runnable, so a
  163. * completion that lands a few ms later is still seen promptly. A 1 ms
  164. * sleep would wake on the 100 Hz idle tick and made every slow transfer
  165. * cost ~10 ms. */
  166. void usb_poll_relax(unsigned int t0)
  167. {
  168. if (get_ticks_ms() - t0 >= 2 && usb_can_sleep())
  169. sched_yield();
  170. }
  171. static unsigned char dev_desc[18];
  172. static unsigned char desc8[64];
  173. static unsigned char hub_desc[16];
  174. static unsigned char port_st[4];
  175. static unsigned char conf_hdr[9];
  176. static unsigned char conf_buf[USB_CONFIG_MAX];
  177. static void put_uint(unsigned int n)
  178. {
  179. char buf[12];
  180. char *p = buf + sizeof(buf) - 1;
  181. *p = '\0';
  182. if (n == 0) {
  183. io_putchar('0');
  184. return;
  185. }
  186. while (n) {
  187. *--p = (char)('0' + (n % 10));
  188. n /= 10;
  189. }
  190. while (*p)
  191. io_putchar(*p++);
  192. }
  193. static void put_hex(unsigned int n, unsigned int digits)
  194. {
  195. static const char hex[] = "0123456789ABCDEF";
  196. unsigned int i;
  197. for (i = 0; i < digits; i++)
  198. io_putchar(hex[(n >> ((digits - 1 - i) * 4)) & 0xF]);
  199. }
  200. static void msleep(unsigned int ms)
  201. {
  202. usb_delay_ms(ms);
  203. }
  204. static void indent(unsigned int depth)
  205. {
  206. unsigned int i;
  207. for (i = 0; i < depth; i++)
  208. io_putstr(" ");
  209. }
  210. static const char *class_name(unsigned int cls)
  211. {
  212. switch (cls) {
  213. case 0x00: return "Per-interface";
  214. case 0x01: return "Audio";
  215. case 0x02: return "Communications";
  216. case 0x03: return "Human Interface";
  217. case 0x05: return "Physical";
  218. case 0x06: return "Image";
  219. case 0x07: return "Printer";
  220. case 0x08: return "Mass Storage";
  221. case 0x09: return "Hub";
  222. case 0x0A: return "CDC Data";
  223. case 0x0B: return "Smart Card";
  224. case 0x0D: return "Content Security";
  225. case 0x0E: return "Video";
  226. case 0x0F: return "Personal Healthcare";
  227. case 0xE0: return "Wireless";
  228. case 0xEF: return "Miscellaneous";
  229. case 0xFE: return "Application";
  230. case 0xFF: return "Vendor-specific";
  231. default: return "Unknown";
  232. }
  233. }
  234. /* wPortStatus (little-endian) from the GET_PORT_STATUS buffer. */
  235. static unsigned int port_status_word(void)
  236. {
  237. return (unsigned int)port_st[0] | ((unsigned int)port_st[1] << 8);
  238. }
  239. int usb_control_in(unsigned int ctrl, unsigned int addr, unsigned int mps,
  240. int low_speed, unsigned char rt, unsigned char req,
  241. unsigned short value, unsigned short index,
  242. unsigned char *buf, unsigned int len)
  243. {
  244. const struct usb_hcd_ops *ops;
  245. unsigned char setup[8];
  246. if (ctrl >= n_usb_ctrls || usb_scan_foreign())
  247. return -1;
  248. ops = usb_ctrls[ctrl].ops;
  249. setup[0] = rt;
  250. setup[1] = req;
  251. setup[2] = (unsigned char)(value & 0xFF);
  252. setup[3] = (unsigned char)((value >> 8) & 0xFF);
  253. setup[4] = (unsigned char)(index & 0xFF);
  254. setup[5] = (unsigned char)((index >> 8) & 0xFF);
  255. setup[6] = (unsigned char)(len & 0xFF);
  256. setup[7] = (unsigned char)((len >> 8) & 0xFF);
  257. return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
  258. low_speed, setup, buf, len, 1);
  259. }
  260. int usb_control_out(unsigned int ctrl, unsigned int addr, unsigned int mps,
  261. int low_speed, unsigned char rt, unsigned char req,
  262. unsigned short value, unsigned short index)
  263. {
  264. const struct usb_hcd_ops *ops;
  265. unsigned char setup[8];
  266. if (ctrl >= n_usb_ctrls || usb_scan_foreign())
  267. return -1;
  268. ops = usb_ctrls[ctrl].ops;
  269. setup[0] = rt;
  270. setup[1] = req;
  271. setup[2] = (unsigned char)(value & 0xFF);
  272. setup[3] = (unsigned char)((value >> 8) & 0xFF);
  273. setup[4] = (unsigned char)(index & 0xFF);
  274. setup[5] = (unsigned char)((index >> 8) & 0xFF);
  275. setup[6] = 0;
  276. setup[7] = 0;
  277. return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
  278. low_speed, setup, 0, 0, 0);
  279. }
  280. int usb_control_out_data(unsigned int ctrl, unsigned int addr,
  281. unsigned int mps, int low_speed, unsigned char rt,
  282. unsigned char req, unsigned short value,
  283. unsigned short index, const unsigned char *buf,
  284. unsigned int len)
  285. {
  286. const struct usb_hcd_ops *ops;
  287. unsigned char setup[8];
  288. if (ctrl >= n_usb_ctrls || usb_scan_foreign())
  289. return -1;
  290. ops = usb_ctrls[ctrl].ops;
  291. setup[0] = rt;
  292. setup[1] = req;
  293. setup[2] = (unsigned char)(value & 0xFF);
  294. setup[3] = (unsigned char)((value >> 8) & 0xFF);
  295. setup[4] = (unsigned char)(index & 0xFF);
  296. setup[5] = (unsigned char)((index >> 8) & 0xFF);
  297. setup[6] = (unsigned char)(len & 0xFF);
  298. setup[7] = (unsigned char)((len >> 8) & 0xFF);
  299. return ops->control_transfer(usb_ctrls[ctrl].local, addr, mps,
  300. low_speed, setup, (unsigned char *)buf,
  301. len, 0);
  302. }
  303. /* Always-on failure trail (COM1/netmon): a device that never enumerates
  304. must not be silent, since boot-time input may depend on it. */
  305. static void usb_fail(const char *what, unsigned int ctrl, unsigned int port,
  306. unsigned int addr)
  307. {
  308. serial_puts(SERIAL_COM1, "usb: ");
  309. serial_puts(SERIAL_COM1, what);
  310. serial_puts(SERIAL_COM1, " ctrl ");
  311. serial_putdec(SERIAL_COM1, ctrl);
  312. serial_puts(SERIAL_COM1, " port ");
  313. serial_putdec(SERIAL_COM1, port);
  314. serial_puts(SERIAL_COM1, " addr ");
  315. serial_putdec(SERIAL_COM1, addr);
  316. serial_putc(SERIAL_COM1, '\n');
  317. }
  318. /* Read the configuration descriptor and hand every interface with an
  319. interrupt-IN endpoint to the active probe. Returns 1 when the device
  320. answered and its configuration was parsed. */
  321. static int probe_config(unsigned int ctrl, unsigned int addr, unsigned int mps,
  322. int low, unsigned int port, unsigned int vendor,
  323. unsigned int product)
  324. {
  325. struct usb_iface_info info;
  326. unsigned int total, i;
  327. int have_iface = 0;
  328. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  329. REQ_GET_DESCRIPTOR,
  330. (unsigned short)(DESC_CONFIG << 8), 0,
  331. conf_hdr, 9) < 0) {
  332. usb_fail("config header failed", ctrl, port, addr);
  333. return 0;
  334. }
  335. total = (unsigned int)conf_hdr[2] | ((unsigned int)conf_hdr[3] << 8);
  336. if (total < 9)
  337. return 0;
  338. if (total > USB_CONFIG_MAX)
  339. total = USB_CONFIG_MAX;
  340. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  341. REQ_GET_DESCRIPTOR,
  342. (unsigned short)(DESC_CONFIG << 8), 0,
  343. conf_buf, total) < 0) {
  344. usb_fail("config read failed", ctrl, port, addr);
  345. return 0;
  346. }
  347. dbg_dec("usb: config descriptor len ", total);
  348. info.ctrl = ctrl;
  349. info.addr = addr;
  350. info.mps = mps;
  351. info.low_speed = low;
  352. info.port = port;
  353. info.vendor = vendor;
  354. info.product = product;
  355. info.config_value = conf_buf[5];
  356. info.cls = 0;
  357. info.subcls = 0;
  358. info.proto = 0;
  359. info.iface_num = 0;
  360. info.ep_addr = 0;
  361. info.ep_mps = 0;
  362. info.ep_interval = 0;
  363. i = 0;
  364. while (i + 2 <= total) {
  365. unsigned int blen = conf_buf[i];
  366. unsigned int btype = conf_buf[i + 1];
  367. if (blen < 2 || i + blen > total)
  368. break;
  369. if (btype == DESC_INTERFACE && blen >= 9) {
  370. if (have_iface && info.ep_addr) {
  371. dbg("usb: interface has interrupt-IN ep\n");
  372. if (current_probe)
  373. current_probe(&info);
  374. }
  375. info.cls = conf_buf[i + 5];
  376. info.subcls = conf_buf[i + 6];
  377. info.proto = conf_buf[i + 7];
  378. info.iface_num = conf_buf[i + 2];
  379. if (print_devices) {
  380. io_putstr(" Interface ");
  381. put_uint(info.iface_num);
  382. io_putstr(": ");
  383. put_hex(info.cls, 2);
  384. io_putchar('/');
  385. put_hex(info.subcls, 2);
  386. io_putchar('/');
  387. put_hex(info.proto, 2);
  388. io_putchar('\n');
  389. } else {
  390. dbg_dec("usb: iface class ", info.cls);
  391. dbg_dec("usb: iface subclass ", info.subcls);
  392. dbg_dec("usb: iface protocol ", info.proto);
  393. }
  394. info.ep_addr = 0;
  395. info.ep_mps = 0;
  396. info.ep_interval = 0;
  397. have_iface = 1;
  398. } else if (btype == DESC_ENDPOINT && blen >= 7 && have_iface) {
  399. unsigned int epa = conf_buf[i + 2];
  400. unsigned int attr = conf_buf[i + 3] & 3u;
  401. unsigned int mps = ((unsigned int)conf_buf[i + 4] |
  402. ((unsigned int)conf_buf[i + 5] << 8)) & 0x7FFu;
  403. if (print_devices) {
  404. io_putstr(" Endpoint 0x");
  405. put_hex(epa, 2);
  406. io_putstr(attr == 3u ? " interrupt" : " other");
  407. io_putstr(", mps ");
  408. put_uint(mps);
  409. io_putchar('\n');
  410. }
  411. if (!info.ep_addr && (epa & 0x80u) && attr == 3u) {
  412. info.ep_addr = epa;
  413. info.ep_mps = mps;
  414. info.ep_interval = conf_buf[i + 6];
  415. dbg_dec("usb: interrupt-IN endpoint ", epa & 0x0Fu);
  416. }
  417. }
  418. i += blen;
  419. }
  420. if (have_iface && info.ep_addr && current_probe)
  421. current_probe(&info);
  422. return 1;
  423. }
  424. static void enumerate_hub(unsigned int ctrl, unsigned int addr, unsigned int mps,
  425. int low, unsigned int port, unsigned int depth);
  426. static int enumerate_device(unsigned int ctrl, unsigned int addr,
  427. unsigned int mps, int low, unsigned int port,
  428. unsigned int depth);
  429. static int enumerate_new_device(unsigned int ctrl, unsigned int port, int low,
  430. unsigned int depth);
  431. static int enumerate_device(unsigned int ctrl, unsigned int addr,
  432. unsigned int mps, int low, unsigned int port,
  433. unsigned int depth)
  434. {
  435. unsigned int vendor, product, cls, subcls, proto;
  436. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  437. REQ_GET_DESCRIPTOR,
  438. (unsigned short)(DESC_DEVICE << 8), 0,
  439. dev_desc, 18) < 0) {
  440. usb_fail("device descriptor failed", ctrl, port, addr);
  441. if (print_devices) {
  442. indent(depth);
  443. io_putstr("(device descriptor read failed)\n");
  444. }
  445. return 0;
  446. }
  447. vendor = dev_desc[8] | ((unsigned int)dev_desc[9] << 8);
  448. product = dev_desc[10] | ((unsigned int)dev_desc[11] << 8);
  449. cls = dev_desc[4];
  450. subcls = dev_desc[5];
  451. proto = dev_desc[6];
  452. {
  453. unsigned int i;
  454. for (i = 0; i < n_device_probes; i++)
  455. if (device_probes[i](ctrl, addr, mps, low, port,
  456. vendor, product))
  457. break;
  458. }
  459. if (print_devices) {
  460. indent(depth);
  461. io_putstr("Bus ");
  462. put_uint(ctrl + 1);
  463. io_putstr(" Device ");
  464. put_uint(addr);
  465. io_putstr(": ID ");
  466. put_hex(vendor, 4);
  467. io_putchar(':');
  468. put_hex(product, 4);
  469. io_putchar(' ');
  470. io_putstr(class_name(cls));
  471. io_putstr(" [");
  472. put_hex(cls, 2);
  473. io_putchar('/');
  474. put_hex(subcls, 2);
  475. io_putchar('/');
  476. put_hex(proto, 2);
  477. io_putstr("]\n");
  478. }
  479. if (cls == 0x09) {
  480. enumerate_hub(ctrl, addr, mps, low, port, depth + 1);
  481. return 1;
  482. }
  483. if (current_probe || print_devices)
  484. return probe_config(ctrl, addr, mps, low, port, vendor, product);
  485. return 1;
  486. }
  487. static void enumerate_hub(unsigned int ctrl, unsigned int addr, unsigned int mps,
  488. int low, unsigned int port, unsigned int depth)
  489. {
  490. unsigned char cfg[9];
  491. unsigned int nports, p, i, power_good;
  492. /* Put the hub in its configured state before issuing hub-class
  493. requests. The core does not configure non-HID devices itself, and
  494. some hubs (notably Intel PCH internal hubs) require this. */
  495. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  496. REQ_GET_DESCRIPTOR,
  497. (unsigned short)(DESC_CONFIG << 8), 0, cfg,
  498. sizeof(cfg)) >= 0 && cfg[5] != 0) {
  499. usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_STD,
  500. REQ_SET_CONFIGURATION, (unsigned short)cfg[5],
  501. 0);
  502. msleep(5);
  503. }
  504. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_CLS,
  505. REQ_GET_DESCRIPTOR,
  506. (unsigned short)(DESC_HUB << 8), 0, hub_desc,
  507. sizeof(hub_desc)) < 0)
  508. return;
  509. nports = hub_desc[2];
  510. if (nports > 8)
  511. nports = 8;
  512. /* Power every downstream port before looking at connect status.
  513. An unpowered port reports no connection, so the old order could
  514. miss devices behind a switched hub. */
  515. power_good = (unsigned int)hub_desc[5] * 2;
  516. if (power_good < 20)
  517. power_good = 20;
  518. if (power_good > 500)
  519. power_good = 500;
  520. for (p = 1; p <= nports; p++)
  521. usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
  522. REQ_SET_FEATURE, FEAT_PORT_POWER,
  523. (unsigned short)p);
  524. msleep(power_good);
  525. for (p = 1; p <= nports; p++) {
  526. unsigned int st = 0;
  527. int low2;
  528. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_OTH,
  529. REQ_GET_STATUS, 0, (unsigned short)p,
  530. port_st, 4) < 0)
  531. continue;
  532. st = port_status_word();
  533. if (print_devices) {
  534. unsigned int chg = (unsigned int)port_st[2] |
  535. ((unsigned int)port_st[3] << 8);
  536. indent(depth);
  537. io_putstr("Hub port ");
  538. put_uint(p);
  539. io_putstr(": status 0x");
  540. put_hex(st, 4);
  541. io_putstr(" change 0x");
  542. put_hex(chg, 4);
  543. io_putchar('\n');
  544. }
  545. if (!(st & PSTAT_CONNECT))
  546. continue;
  547. /* Reset the downstream port and wait for it to enable. */
  548. if (usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
  549. REQ_SET_FEATURE, FEAT_PORT_RESET,
  550. (unsigned short)p) < 0)
  551. continue;
  552. for (i = 0; i < 100; i++) {
  553. if (usb_control_in(ctrl, addr, mps, low,
  554. RT_DEV2HOST_OTH, REQ_GET_STATUS, 0,
  555. (unsigned short)p, port_st, 4) < 0)
  556. break;
  557. st = port_status_word();
  558. if (st & PSTAT_ENABLE)
  559. break;
  560. if (port_st[2] & 0x10u) /* C_PORT_RESET */
  561. break;
  562. msleep(5);
  563. }
  564. msleep(10);
  565. usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
  566. REQ_CLEAR_FEATURE, FEAT_C_PORT_RESET,
  567. (unsigned short)p);
  568. usb_control_out(ctrl, addr, mps, low, RT_HOST2DEV_OTH,
  569. REQ_CLEAR_FEATURE, FEAT_C_PORT_CONNECTION,
  570. (unsigned short)p);
  571. /* Re-read status to learn the child's speed. */
  572. low2 = 0;
  573. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_OTH,
  574. REQ_GET_STATUS, 0, (unsigned short)p,
  575. port_st, 4) >= 0) {
  576. st = port_status_word();
  577. if (st & PSTAT_LOW_SPEED)
  578. low2 = 1;
  579. }
  580. if (!(st & PSTAT_ENABLE))
  581. continue;
  582. if (print_devices) {
  583. unsigned int chg = (unsigned int)port_st[2] |
  584. ((unsigned int)port_st[3] << 8);
  585. indent(depth);
  586. io_putstr("Hub port ");
  587. put_uint(p);
  588. io_putstr(" after reset: status 0x");
  589. put_hex(st, 4);
  590. io_putstr(" change 0x");
  591. put_hex(chg, 4);
  592. io_putchar('\n');
  593. }
  594. enumerate_new_device(ctrl, port, low2, depth);
  595. }
  596. }
  597. static int enumerate_new_device(unsigned int ctrl, unsigned int port, int low,
  598. unsigned int depth)
  599. {
  600. unsigned int mps;
  601. unsigned int addr = next_addr[ctrl];
  602. if (addr > 127) {
  603. if (print_devices) {
  604. indent(depth);
  605. io_putstr("(too many devices)\n");
  606. }
  607. return 0;
  608. }
  609. /* Some low/full-speed devices need a little more settle time after
  610. * the port reset than the OHCI spec's 10 ms. Without this the 970A's
  611. * OHCI mouse was intermittently dropped before its first control
  612. * transfer. */
  613. msleep(20);
  614. /* Traditional ("old") enumeration scheme: assign the address before
  615. * reading the descriptor. The xHCI Default-state EP0 transfer is
  616. * unreliable on this hardware, while a slot in the Addressed state
  617. * works. The device's real max packet size is not known yet, so use
  618. * the default 8-byte pipe for the first SET_ADDRESS/GET_DESCRIPTOR.
  619. * A full-speed device may have an 8-byte EP0 (the 64-byte guess made
  620. * the 970A's OHCI drop a mouse during descriptor read), and a
  621. * high-speed device accepts the short transfer and reports its real
  622. * bMaxPacketSize0 in the descriptor. */
  623. mps = 8;
  624. if (usb_control_out(ctrl, 0, mps, low, RT_HOST2DEV_STD,
  625. REQ_SET_ADDRESS, (unsigned short)addr, 0) < 0) {
  626. usb_fail("SET_ADDRESS failed", ctrl, port, 0);
  627. if (print_devices) {
  628. indent(depth);
  629. io_putstr("(SET_ADDRESS failed)\n");
  630. }
  631. return 0;
  632. }
  633. next_addr[ctrl] = addr + 1;
  634. msleep(20);
  635. /* First 8 bytes at the assigned address (learn max packet size). */
  636. if (usb_control_in(ctrl, addr, mps, low, RT_DEV2HOST_STD,
  637. REQ_GET_DESCRIPTOR,
  638. (unsigned short)(DESC_DEVICE << 8), 0,
  639. desc8, 8) < 0) {
  640. usb_fail("descriptor8 failed", ctrl, port, addr);
  641. if (print_devices) {
  642. indent(depth);
  643. io_putstr("(device descriptor read failed)\n");
  644. }
  645. return 0;
  646. }
  647. mps = desc8[7];
  648. if (mps == 0)
  649. mps = 8;
  650. dbg_dec("usb: enumerated device, addr ", addr);
  651. return enumerate_device(ctrl, addr, mps, low, port, depth);
  652. }
  653. /* A port reset hands the device to its companion controller, and the first
  654. * control transfer after it is occasionally ignored by the SB950 OHCI
  655. * (every TD stays NotAccessed for the full 2 s timeout). A fresh port
  656. * reset and a short settle makes the retry work, so keep trying a couple
  657. * of times instead of leaving the port without a device until the next
  658. * connect-change. */
  659. static int enum_port_retry(unsigned int ctrl, unsigned int port,
  660. const struct usb_hcd_ops *ops, unsigned int local)
  661. {
  662. unsigned int try;
  663. for (try = 0;; try++) {
  664. if (enumerate_new_device(ctrl, port,
  665. ops->port_low_speed(local, port), 1))
  666. return 1;
  667. if (try >= 2)
  668. return 0;
  669. msleep(30);
  670. if (ops->port_reset(local, port) != 0)
  671. return 0;
  672. }
  673. }
  674. static void add_ctrl(const struct usb_hcd_ops *ops, unsigned int count)
  675. {
  676. unsigned int i;
  677. for (i = 0; i < count && n_usb_ctrls < USB_MAX_CTRL; i++) {
  678. usb_ctrls[n_usb_ctrls].ops = ops;
  679. usb_ctrls[n_usb_ctrls].local = i;
  680. n_usb_ctrls++;
  681. }
  682. }
  683. int usb_hcd_init(void)
  684. {
  685. unsigned int uhci, ohci, ehci, xhci;
  686. if (hcd_init_done)
  687. return n_usb_ctrls ? 0 : -1;
  688. hcd_init_done = 1;
  689. /* Companions first, so EHCI's CONFIGFLAG claim is the last thing
  690. that happens to the shared ports. */
  691. uhci = (unsigned int)usb_uhci_ops.init();
  692. ohci = (unsigned int)usb_ohci_ops.init();
  693. ehci = (unsigned int)usb_ehci_ops.init();
  694. xhci = (unsigned int)usb_xhci_ops.init();
  695. /* Scan EHCI first: resetting its ports hands low/full-speed devices
  696. back to the companions before those are scanned. */
  697. add_ctrl(&usb_ehci_ops, ehci);
  698. add_ctrl(&usb_uhci_ops, uhci);
  699. add_ctrl(&usb_ohci_ops, ohci);
  700. add_ctrl(&usb_xhci_ops, xhci);
  701. return n_usb_ctrls ? 0 : -1;
  702. }
  703. const char *usb_hcd_name(void)
  704. {
  705. if (!n_usb_ctrls)
  706. return "none";
  707. if (n_usb_ctrls == 1)
  708. return usb_ctrls[0].ops->name;
  709. return "USB";
  710. }
  711. const char *usb_hcd_name_ctrl(unsigned int ctrl)
  712. {
  713. if (ctrl >= n_usb_ctrls)
  714. return "none";
  715. return usb_ctrls[ctrl].ops->name;
  716. }
  717. unsigned int usb_generation(void)
  718. {
  719. return generation;
  720. }
  721. int usb_port_connected(unsigned int ctrl, unsigned int port)
  722. {
  723. if (ctrl >= n_usb_ctrls)
  724. return 0;
  725. if (usb_scan_foreign())
  726. return 1; /* do not detach anything mid-scan */
  727. return usb_ctrls[ctrl].ops->port_connected(usb_ctrls[ctrl].local,
  728. port);
  729. }
  730. int usb_int_in_start(unsigned int ctrl, unsigned int slot, unsigned int addr,
  731. unsigned int ep, unsigned int mps, int low_speed)
  732. {
  733. const struct usb_hcd_ops *ops;
  734. int r;
  735. if (ctrl >= n_usb_ctrls || slot >= USB_HCD_INT_SLOTS ||
  736. usb_scan_foreign())
  737. return -1;
  738. ops = usb_ctrls[ctrl].ops;
  739. if (slot_ops[slot]) { /* stale owner from another HCD */
  740. slot_ops[slot]->int_in_stop(slot);
  741. slot_ops[slot] = 0;
  742. }
  743. r = ops->int_in_start(usb_ctrls[ctrl].local, slot, addr, ep, mps,
  744. low_speed);
  745. slot_ops[slot] = r == 0 ? ops : 0;
  746. return r;
  747. }
  748. int usb_int_in_poll(unsigned int slot, unsigned char *buf, unsigned int len)
  749. {
  750. if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot] || usb_scan_foreign())
  751. return 0;
  752. return slot_ops[slot]->int_in_poll(slot, buf, len);
  753. }
  754. void usb_int_in_stop(unsigned int slot)
  755. {
  756. if (slot >= USB_HCD_INT_SLOTS || !slot_ops[slot] || usb_scan_foreign())
  757. return;
  758. slot_ops[slot]->int_in_stop(slot);
  759. slot_ops[slot] = 0;
  760. }
  761. void usb_register_device_probe(usb_device_probe_fn fn)
  762. {
  763. unsigned int i;
  764. if (!fn)
  765. return;
  766. for (i = 0; i < n_device_probes; i++)
  767. if (device_probes[i] == fn)
  768. return;
  769. if (n_device_probes < USB_MAX_DEVICE_PROBES)
  770. device_probes[n_device_probes++] = fn;
  771. }
  772. int usb_bulk_out(unsigned int ctrl, unsigned int addr, unsigned int ep,
  773. unsigned int mps, const unsigned char *buf,
  774. unsigned int len)
  775. {
  776. if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_out ||
  777. usb_scan_foreign())
  778. return -1;
  779. return usb_ctrls[ctrl].ops->bulk_out(usb_ctrls[ctrl].local, addr, ep,
  780. mps, buf, len);
  781. }
  782. int usb_bulk_in(unsigned int ctrl, unsigned int addr, unsigned int ep,
  783. unsigned int mps, unsigned char *buf, unsigned int len)
  784. {
  785. if (ctrl >= n_usb_ctrls || !usb_ctrls[ctrl].ops->bulk_in ||
  786. usb_scan_foreign())
  787. return -1;
  788. return usb_ctrls[ctrl].ops->bulk_in(usb_ctrls[ctrl].local, addr, ep,
  789. mps, buf, len);
  790. }
  791. int usb_bulk_supported(unsigned int ctrl)
  792. {
  793. if (ctrl >= n_usb_ctrls)
  794. return 0;
  795. return usb_ctrls[ctrl].ops->bulk_in != 0 &&
  796. usb_ctrls[ctrl].ops->bulk_out != 0;
  797. }
  798. int usb_scan(usb_probe_fn probe)
  799. {
  800. unsigned int c, p;
  801. unsigned int seen[USB_MAX_CTRL];
  802. if (scan_busy || usb_bus_claim() != 0)
  803. return -2;
  804. /* The bus is claimed before the first controller bring-up: the boot
  805. * scan runs while the WM and the first terminal are already up, and
  806. * their USB calls must see the scan (usb_scan_foreign) rather than
  807. * half-initialized controllers. */
  808. scan_busy = 1;
  809. if (usb_hcd_init() != 0) {
  810. scan_busy = 0;
  811. usb_bus_release();
  812. return -1;
  813. }
  814. generation++;
  815. current_probe = probe;
  816. print_devices = (probe == 0);
  817. for (c = 0; c < USB_MAX_CTRL; c++)
  818. seen[c] = 0;
  819. for (c = 0; c < n_usb_ctrls; c++) {
  820. const struct usb_hcd_ops *ops = usb_ctrls[c].ops;
  821. unsigned int local = usb_ctrls[c].local;
  822. next_addr[c] = 1;
  823. if (print_devices) {
  824. unsigned int bus, dev, func, base;
  825. int mmio;
  826. ops->info(local, &bus, &dev, &func, &base, &mmio);
  827. io_putstr(ops->name);
  828. io_putstr(" Host Controller (PCI ");
  829. put_hex(bus, 2);
  830. io_putchar(':');
  831. put_hex(dev, 2);
  832. io_putchar('.');
  833. put_hex(func, 1);
  834. io_putstr(mmio ? ", MMIO 0x" : ", I/O 0x");
  835. put_hex(base, mmio ? 8 : 4);
  836. io_putstr(")\n");
  837. if (ops->print_ports)
  838. ops->print_ports(local);
  839. }
  840. for (p = 0; p < ops->port_count(local); p++) {
  841. unsigned int conn =
  842. (unsigned int)ops->port_connected(local, p);
  843. dbg_port(c, p, conn,
  844. (unsigned int)ops->port_low_speed(local, p));
  845. if (!conn)
  846. continue;
  847. ops->port_take_change(local, p); /* clear stale CSC */
  848. if (ops->port_reset(local, p) != 0) {
  849. dbg("usb: port reset failed\n");
  850. continue;
  851. }
  852. dbg("usb: port reset ok\n");
  853. if (enum_port_retry(c, p, ops, local) && p < 32)
  854. seen[c] |= 1u << p;
  855. }
  856. if (print_devices && ops->print_ports) {
  857. io_putstr(" After scan:\n");
  858. ops->print_ports(local);
  859. }
  860. }
  861. /* EHCI is scanned first so it can hand low/full-speed devices back to
  862. * their OHCI companion. That handback can leave the companion port
  863. * briefly reporting "disconnected" exactly while the OHCI pass runs
  864. * (the 970A's mouse was missed that way). A delayed second OHCI-only
  865. * pass catches ports that stayed connected but produced no device. */
  866. msleep(60);
  867. for (c = 0; c < n_usb_ctrls; c++) {
  868. const struct usb_hcd_ops *ops = usb_ctrls[c].ops;
  869. unsigned int local = usb_ctrls[c].local;
  870. if (ops != &usb_ohci_ops)
  871. continue;
  872. for (p = 0; p < ops->port_count(local) && p < 32; p++) {
  873. if (seen[c] & (1u << p))
  874. continue;
  875. if (!ops->port_connected(local, p))
  876. continue;
  877. ops->port_take_change(local, p);
  878. if (ops->port_reset(local, p) != 0)
  879. continue;
  880. dbg("usb: companion retry\n");
  881. enum_port_retry(c, p, ops, local);
  882. }
  883. }
  884. current_probe = 0;
  885. print_devices = 0;
  886. scan_busy = 0;
  887. usb_bus_release();
  888. return 0;
  889. }
  890. int usb_hotplug_poll(usb_probe_fn probe)
  891. {
  892. static unsigned int last;
  893. unsigned int now, c, p;
  894. int n = 0;
  895. if (!n_usb_ctrls || scan_busy || bus_depth)
  896. return 0;
  897. now = get_ticks_ms();
  898. if (now - last < HOTPLUG_MS)
  899. return 0;
  900. last = now;
  901. usb_bus_claim();
  902. scan_busy = 1;
  903. current_probe = probe;
  904. for (c = 0; c < n_usb_ctrls; c++) {
  905. const struct usb_hcd_ops *ops = usb_ctrls[c].ops;
  906. unsigned int local = usb_ctrls[c].local;
  907. for (p = 0; p < ops->port_count(local); p++) {
  908. if (!ops->port_take_change(local, p))
  909. continue;
  910. if (!ops->port_connected(local, p))
  911. continue;
  912. if (ops->port_reset(local, p) != 0)
  913. continue;
  914. enum_port_retry(c, p, ops, local);
  915. n++;
  916. }
  917. }
  918. current_probe = 0;
  919. scan_busy = 0;
  920. usb_bus_release();
  921. return n;
  922. }
  923. void usb_enumerate_and_print(void)
  924. {
  925. int r = usb_scan(0);
  926. if (r == -1)
  927. io_putstr("lsusb: no USB host controller found\n");
  928. else if (r == -2)
  929. io_putstr("lsusb: a USB scan is already running\n");
  930. }