usb.c 24 KB

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