usb.c 25 KB

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