usb.c 29 KB

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