usbhub.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /* usb.c - USB Hub Support. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/mm.h>
  21. #include <grub/usb.h>
  22. #include <grub/misc.h>
  23. #include <grub/time.h>
  24. #define GRUB_USBHUB_MAX_DEVICES 128
  25. /* USB Supports 127 devices, with device 0 as special case. */
  26. static struct grub_usb_device *grub_usb_devs[GRUB_USBHUB_MAX_DEVICES];
  27. static int rescan = 0;
  28. static int npending = 0;
  29. struct grub_usb_hub
  30. {
  31. struct grub_usb_hub *next;
  32. grub_usb_controller_t controller;
  33. int nports;
  34. struct grub_usb_device **devices;
  35. struct grub_usb_hub_port *ports;
  36. grub_usb_device_t dev;
  37. };
  38. static struct grub_usb_hub *hubs;
  39. static grub_usb_controller_dev_t grub_usb_list;
  40. /* Add a device that currently has device number 0 and resides on
  41. CONTROLLER, the Hub reported that the device speed is SPEED. */
  42. static grub_usb_device_t
  43. grub_usb_hub_add_dev (grub_usb_controller_t controller,
  44. grub_usb_speed_t speed,
  45. int split_hubport, int split_hubaddr)
  46. {
  47. grub_usb_device_t dev;
  48. int i;
  49. grub_usb_err_t err;
  50. grub_boot_time ("Attaching USB device");
  51. dev = grub_zalloc (sizeof (struct grub_usb_device));
  52. if (! dev)
  53. return NULL;
  54. dev->controller = *controller;
  55. dev->speed = speed;
  56. dev->split_hubport = split_hubport;
  57. dev->split_hubaddr = split_hubaddr;
  58. err = grub_usb_device_initialize (dev);
  59. if (err)
  60. {
  61. grub_free (dev);
  62. return NULL;
  63. }
  64. /* Assign a new address to the device. */
  65. for (i = 1; i < GRUB_USBHUB_MAX_DEVICES; i++)
  66. {
  67. if (! grub_usb_devs[i])
  68. break;
  69. }
  70. if (i == GRUB_USBHUB_MAX_DEVICES)
  71. {
  72. grub_error (GRUB_ERR_IO, "can't assign address to USB device");
  73. for (i = 0; i < GRUB_USB_MAX_CONF; i++)
  74. grub_free (dev->config[i].descconf);
  75. grub_free (dev);
  76. return NULL;
  77. }
  78. err = grub_usb_control_msg (dev,
  79. (GRUB_USB_REQTYPE_OUT
  80. | GRUB_USB_REQTYPE_STANDARD
  81. | GRUB_USB_REQTYPE_TARGET_DEV),
  82. GRUB_USB_REQ_SET_ADDRESS,
  83. i, 0, 0, NULL);
  84. if (err)
  85. {
  86. for (i = 0; i < GRUB_USB_MAX_CONF; i++)
  87. grub_free (dev->config[i].descconf);
  88. grub_free (dev);
  89. return NULL;
  90. }
  91. dev->addr = i;
  92. dev->initialized = 1;
  93. grub_usb_devs[i] = dev;
  94. grub_dprintf ("usb", "Added new usb device: %p, addr=%d\n",
  95. dev, i);
  96. grub_dprintf ("usb", "speed=%d, split_hubport=%d, split_hubaddr=%d\n",
  97. speed, split_hubport, split_hubaddr);
  98. /* Wait "recovery interval", spec. says 2ms */
  99. grub_millisleep (2);
  100. grub_boot_time ("Probing USB device driver");
  101. grub_usb_device_attach (dev);
  102. grub_boot_time ("Attached USB device");
  103. return dev;
  104. }
  105. static grub_usb_err_t
  106. grub_usb_add_hub (grub_usb_device_t dev)
  107. {
  108. struct grub_usb_usb_hubdesc hubdesc;
  109. grub_usb_err_t err;
  110. int i;
  111. err = grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_IN
  112. | GRUB_USB_REQTYPE_CLASS
  113. | GRUB_USB_REQTYPE_TARGET_DEV),
  114. GRUB_USB_REQ_GET_DESCRIPTOR,
  115. (GRUB_USB_DESCRIPTOR_HUB << 8) | 0,
  116. 0, sizeof (hubdesc), (char *) &hubdesc);
  117. if (err)
  118. return err;
  119. grub_dprintf ("usb", "Hub descriptor:\n\t\t len:%d, typ:0x%02x, cnt:%d, char:0x%02x, pwg:%d, curr:%d\n",
  120. hubdesc.length, hubdesc.type, hubdesc.portcnt,
  121. hubdesc.characteristics, hubdesc.pwdgood,
  122. hubdesc.current);
  123. /* Activate the first configuration. Hubs should have only one conf. */
  124. grub_dprintf ("usb", "Hub set configuration\n");
  125. grub_usb_set_configuration (dev, 1);
  126. dev->nports = hubdesc.portcnt;
  127. dev->children = grub_calloc (hubdesc.portcnt, sizeof (dev->children[0]));
  128. dev->ports = grub_calloc (dev->nports, sizeof (dev->ports[0]));
  129. if (!dev->children || !dev->ports)
  130. {
  131. grub_free (dev->children);
  132. grub_free (dev->ports);
  133. return GRUB_USB_ERR_INTERNAL;
  134. }
  135. /* Power on all Hub ports. */
  136. for (i = 1; i <= hubdesc.portcnt; i++)
  137. {
  138. grub_dprintf ("usb", "Power on - port %d\n", i);
  139. /* Power on the port and wait for possible device connect */
  140. grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
  141. | GRUB_USB_REQTYPE_CLASS
  142. | GRUB_USB_REQTYPE_TARGET_OTHER),
  143. GRUB_USB_REQ_SET_FEATURE,
  144. GRUB_USB_HUB_FEATURE_PORT_POWER,
  145. i, 0, NULL);
  146. }
  147. /* Rest will be done on next usb poll. */
  148. for (i = 0; i < dev->config[0].interf[0].descif->endpointcnt;
  149. i++)
  150. {
  151. struct grub_usb_desc_endp *endp = NULL;
  152. endp = &dev->config[0].interf[0].descendp[i];
  153. if ((endp->endp_addr & 128) && grub_usb_get_ep_type(endp)
  154. == GRUB_USB_EP_INTERRUPT)
  155. {
  156. grub_size_t len;
  157. dev->hub_endpoint = endp;
  158. len = endp->maxpacket;
  159. if (len > sizeof (dev->statuschange))
  160. len = sizeof (dev->statuschange);
  161. dev->hub_transfer
  162. = grub_usb_bulk_read_background (dev, endp, len,
  163. (char *) &dev->statuschange);
  164. break;
  165. }
  166. }
  167. rescan = 1;
  168. return GRUB_USB_ERR_NONE;
  169. }
  170. static void
  171. attach_root_port (struct grub_usb_hub *hub, int portno,
  172. grub_usb_speed_t speed)
  173. {
  174. grub_usb_device_t dev;
  175. grub_usb_err_t err;
  176. grub_boot_time ("After detect_dev");
  177. /* Enable the port. */
  178. err = hub->controller->dev->portstatus (hub->controller, portno, 1);
  179. if (err)
  180. return;
  181. hub->controller->dev->pending_reset = grub_get_time_ms () + 5000;
  182. npending++;
  183. grub_millisleep (10);
  184. grub_boot_time ("Port enabled");
  185. /* Enable the port and create a device. */
  186. /* High speed device needs not transaction translation
  187. and full/low speed device cannot be connected to EHCI root hub
  188. and full/low speed device connected to OHCI/UHCI needs not
  189. transaction translation - e.g. hubport and hubaddr should be
  190. always none (zero) for any device connected to any root hub. */
  191. dev = grub_usb_hub_add_dev (hub->controller, speed, 0, 0);
  192. hub->controller->dev->pending_reset = 0;
  193. npending--;
  194. if (! dev)
  195. return;
  196. hub->devices[portno] = dev;
  197. /* If the device is a Hub, scan it for more devices. */
  198. if (dev->descdev.class == 0x09)
  199. grub_usb_add_hub (dev);
  200. grub_boot_time ("Attached root port");
  201. }
  202. /* Iterate over all controllers found by the driver. */
  203. static int
  204. grub_usb_controller_dev_register_iter (grub_usb_controller_t controller, void *data)
  205. {
  206. grub_usb_controller_dev_t usb = data;
  207. struct grub_usb_hub *hub;
  208. controller->dev = usb;
  209. grub_boot_time ("Registering USB root hub");
  210. hub = grub_malloc (sizeof (*hub));
  211. if (!hub)
  212. return GRUB_USB_ERR_INTERNAL;
  213. hub->next = hubs;
  214. hubs = hub;
  215. hub->controller = grub_malloc (sizeof (*controller));
  216. if (!hub->controller)
  217. {
  218. grub_free (hub);
  219. return GRUB_USB_ERR_INTERNAL;
  220. }
  221. grub_memcpy (hub->controller, controller, sizeof (*controller));
  222. hub->dev = 0;
  223. /* Query the number of ports the root Hub has. */
  224. hub->nports = controller->dev->hubports (controller);
  225. hub->devices = grub_calloc (hub->nports, sizeof (hub->devices[0]));
  226. hub->ports = grub_calloc (hub->nports, sizeof (hub->ports[0]));
  227. if (!hub->devices || !hub->ports)
  228. {
  229. grub_free (hub->devices);
  230. grub_free (hub->ports);
  231. grub_free (hub->controller);
  232. grub_free (hub);
  233. grub_print_error ();
  234. return 0;
  235. }
  236. return 0;
  237. }
  238. void
  239. grub_usb_controller_dev_unregister (grub_usb_controller_dev_t usb)
  240. {
  241. grub_usb_controller_dev_t *p, q;
  242. for (p = &grub_usb_list, q = *p; q; p = &(q->next), q = q->next)
  243. if (q == usb)
  244. {
  245. *p = q->next;
  246. break;
  247. }
  248. }
  249. void
  250. grub_usb_controller_dev_register (grub_usb_controller_dev_t usb)
  251. {
  252. int portno;
  253. int continue_waiting = 0;
  254. struct grub_usb_hub *hub;
  255. usb->next = grub_usb_list;
  256. grub_usb_list = usb;
  257. if (usb->iterate)
  258. usb->iterate (grub_usb_controller_dev_register_iter, usb);
  259. grub_boot_time ("waiting for stable power on USB root\n");
  260. while (1)
  261. {
  262. for (hub = hubs; hub; hub = hub->next)
  263. if (hub->controller->dev == usb)
  264. {
  265. /* Wait for completion of insertion and stable power (USB spec.)
  266. * Should be at least 100ms, some devices requires more...
  267. * There is also another thing - some devices have worse contacts
  268. * and connected signal is unstable for some time - we should handle
  269. * it - but prevent deadlock in case when device is too faulty... */
  270. for (portno = 0; portno < hub->nports; portno++)
  271. {
  272. grub_usb_speed_t speed;
  273. int changed = 0;
  274. speed = hub->controller->dev->detect_dev (hub->controller, portno,
  275. &changed);
  276. if (hub->ports[portno].state == PORT_STATE_NORMAL
  277. && speed != GRUB_USB_SPEED_NONE)
  278. {
  279. hub->ports[portno].soft_limit_time = grub_get_time_ms () + 250;
  280. hub->ports[portno].hard_limit_time = hub->ports[portno].soft_limit_time + 1750;
  281. hub->ports[portno].state = PORT_STATE_WAITING_FOR_STABLE_POWER;
  282. grub_boot_time ("Scheduling stable power wait for port %p:%d",
  283. usb, portno);
  284. continue_waiting++;
  285. continue;
  286. }
  287. if (hub->ports[portno].state == PORT_STATE_WAITING_FOR_STABLE_POWER
  288. && speed == GRUB_USB_SPEED_NONE)
  289. {
  290. hub->ports[portno].soft_limit_time = grub_get_time_ms () + 250;
  291. continue;
  292. }
  293. if (hub->ports[portno].state == PORT_STATE_WAITING_FOR_STABLE_POWER
  294. && grub_get_time_ms () > hub->ports[portno].soft_limit_time)
  295. {
  296. hub->ports[portno].state = PORT_STATE_STABLE_POWER;
  297. grub_boot_time ("Got stable power wait for port %p:%d",
  298. usb, portno);
  299. continue_waiting--;
  300. continue;
  301. }
  302. if (hub->ports[portno].state == PORT_STATE_WAITING_FOR_STABLE_POWER
  303. && grub_get_time_ms () > hub->ports[portno].hard_limit_time)
  304. {
  305. hub->ports[portno].state = PORT_STATE_FAILED_DEVICE;
  306. continue_waiting--;
  307. continue;
  308. }
  309. }
  310. }
  311. if (!continue_waiting)
  312. break;
  313. grub_millisleep (1);
  314. }
  315. grub_boot_time ("After the stable power wait on USB root");
  316. for (hub = hubs; hub; hub = hub->next)
  317. if (hub->controller->dev == usb)
  318. for (portno = 0; portno < hub->nports; portno++)
  319. if (hub->ports[portno].state == PORT_STATE_STABLE_POWER)
  320. {
  321. grub_usb_speed_t speed;
  322. int changed = 0;
  323. hub->ports[portno].state = PORT_STATE_NORMAL;
  324. speed = hub->controller->dev->detect_dev (hub->controller, portno, &changed);
  325. attach_root_port (hub, portno, speed);
  326. }
  327. grub_boot_time ("USB root hub registered");
  328. }
  329. static void detach_device (grub_usb_device_t dev);
  330. static void
  331. detach_device (grub_usb_device_t dev)
  332. {
  333. unsigned i;
  334. int k;
  335. if (!dev)
  336. return;
  337. if (dev->descdev.class == GRUB_USB_CLASS_HUB)
  338. {
  339. if (dev->hub_transfer)
  340. grub_usb_cancel_transfer (dev->hub_transfer);
  341. for (i = 0; i < dev->nports; i++)
  342. detach_device (dev->children[i]);
  343. grub_free (dev->children);
  344. }
  345. for (i = 0; i < ARRAY_SIZE (dev->config); i++)
  346. if (dev->config[i].descconf)
  347. for (k = 0; k < dev->config[i].descconf->numif; k++)
  348. {
  349. struct grub_usb_interface *inter = &dev->config[i].interf[k];
  350. if (inter && inter->detach_hook)
  351. inter->detach_hook (dev, i, k);
  352. }
  353. grub_usb_devs[dev->addr] = 0;
  354. }
  355. static int
  356. wait_power_nonroot_hub (grub_usb_device_t dev)
  357. {
  358. grub_usb_err_t err;
  359. int continue_waiting = 0;
  360. unsigned i;
  361. for (i = 1; i <= dev->nports; i++)
  362. if (dev->ports[i - 1].state == PORT_STATE_WAITING_FOR_STABLE_POWER)
  363. {
  364. grub_uint64_t tm;
  365. grub_uint32_t current_status = 0;
  366. /* Get the port status. */
  367. err = grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_IN
  368. | GRUB_USB_REQTYPE_CLASS
  369. | GRUB_USB_REQTYPE_TARGET_OTHER),
  370. GRUB_USB_REQ_GET_STATUS,
  371. 0, i,
  372. sizeof (current_status),
  373. (char *) &current_status);
  374. if (err)
  375. {
  376. dev->ports[i - 1].state = PORT_STATE_FAILED_DEVICE;
  377. continue;
  378. }
  379. tm = grub_get_time_ms ();
  380. if (!(current_status & GRUB_USB_HUB_STATUS_PORT_CONNECTED))
  381. dev->ports[i - 1].soft_limit_time = tm + 250;
  382. if (tm >= dev->ports[i - 1].soft_limit_time)
  383. {
  384. if (dev->controller.dev->pending_reset)
  385. continue;
  386. /* Now do reset of port. */
  387. grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
  388. | GRUB_USB_REQTYPE_CLASS
  389. | GRUB_USB_REQTYPE_TARGET_OTHER),
  390. GRUB_USB_REQ_SET_FEATURE,
  391. GRUB_USB_HUB_FEATURE_PORT_RESET,
  392. i, 0, 0);
  393. dev->ports[i - 1].state = PORT_STATE_NORMAL;
  394. grub_boot_time ("Resetting port %p:%d", dev, i - 1);
  395. rescan = 1;
  396. /* We cannot reset more than one device at the same time !
  397. * Resetting more devices together results in very bad
  398. * situation: more than one device has default address 0
  399. * at the same time !!!
  400. * Additionaly, we cannot perform another reset
  401. * anywhere on the same OHCI controller until
  402. * we will finish addressing of reseted device ! */
  403. dev->controller.dev->pending_reset = grub_get_time_ms () + 5000;
  404. npending++;
  405. continue;
  406. }
  407. if (tm >= dev->ports[i - 1].hard_limit_time)
  408. {
  409. dev->ports[i - 1].state = PORT_STATE_FAILED_DEVICE;
  410. continue;
  411. }
  412. continue_waiting = 1;
  413. }
  414. return continue_waiting && dev->controller.dev->pending_reset == 0;
  415. }
  416. static void
  417. poll_nonroot_hub (grub_usb_device_t dev)
  418. {
  419. grub_usb_err_t err;
  420. unsigned i;
  421. grub_uint32_t changed;
  422. grub_size_t actual, len;
  423. if (!dev->hub_transfer)
  424. return;
  425. err = grub_usb_check_transfer (dev->hub_transfer, &actual);
  426. if (err == GRUB_USB_ERR_WAIT)
  427. return;
  428. changed = dev->statuschange;
  429. len = dev->hub_endpoint->maxpacket;
  430. if (len > sizeof (dev->statuschange))
  431. len = sizeof (dev->statuschange);
  432. dev->hub_transfer
  433. = grub_usb_bulk_read_background (dev, dev->hub_endpoint, len,
  434. (char *) &dev->statuschange);
  435. if (err || actual == 0 || changed == 0)
  436. return;
  437. /* Iterate over the Hub ports. */
  438. for (i = 1; i <= dev->nports; i++)
  439. {
  440. grub_uint32_t status;
  441. if (!(changed & (1 << i))
  442. || dev->ports[i - 1].state == PORT_STATE_WAITING_FOR_STABLE_POWER)
  443. continue;
  444. /* Get the port status. */
  445. err = grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_IN
  446. | GRUB_USB_REQTYPE_CLASS
  447. | GRUB_USB_REQTYPE_TARGET_OTHER),
  448. GRUB_USB_REQ_GET_STATUS,
  449. 0, i, sizeof (status), (char *) &status);
  450. grub_dprintf ("usb", "dev = %p, i = %d, status = %08x\n",
  451. dev, i, status);
  452. if (err)
  453. continue;
  454. /* FIXME: properly handle these conditions. */
  455. if (status & GRUB_USB_HUB_STATUS_C_PORT_ENABLED)
  456. grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
  457. | GRUB_USB_REQTYPE_CLASS
  458. | GRUB_USB_REQTYPE_TARGET_OTHER),
  459. GRUB_USB_REQ_CLEAR_FEATURE,
  460. GRUB_USB_HUB_FEATURE_C_PORT_ENABLED, i, 0, 0);
  461. if (status & GRUB_USB_HUB_STATUS_C_PORT_SUSPEND)
  462. grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
  463. | GRUB_USB_REQTYPE_CLASS
  464. | GRUB_USB_REQTYPE_TARGET_OTHER),
  465. GRUB_USB_REQ_CLEAR_FEATURE,
  466. GRUB_USB_HUB_FEATURE_C_PORT_SUSPEND, i, 0, 0);
  467. if (status & GRUB_USB_HUB_STATUS_C_PORT_OVERCURRENT)
  468. grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
  469. | GRUB_USB_REQTYPE_CLASS
  470. | GRUB_USB_REQTYPE_TARGET_OTHER),
  471. GRUB_USB_REQ_CLEAR_FEATURE,
  472. GRUB_USB_HUB_FEATURE_C_PORT_OVERCURRENT, i, 0, 0);
  473. if (!dev->controller.dev->pending_reset &&
  474. (status & GRUB_USB_HUB_STATUS_C_PORT_CONNECTED))
  475. {
  476. grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
  477. | GRUB_USB_REQTYPE_CLASS
  478. | GRUB_USB_REQTYPE_TARGET_OTHER),
  479. GRUB_USB_REQ_CLEAR_FEATURE,
  480. GRUB_USB_HUB_FEATURE_C_PORT_CONNECTED, i, 0, 0);
  481. detach_device (dev->children[i - 1]);
  482. dev->children[i - 1] = NULL;
  483. /* Connected and status of connection changed ? */
  484. if (status & GRUB_USB_HUB_STATUS_PORT_CONNECTED)
  485. {
  486. grub_boot_time ("Before the stable power wait portno=%d", i);
  487. /* A device is actually connected to this port. */
  488. /* Wait for completion of insertion and stable power (USB spec.)
  489. * Should be at least 100ms, some devices requires more...
  490. * There is also another thing - some devices have worse contacts
  491. * and connected signal is unstable for some time - we should handle
  492. * it - but prevent deadlock in case when device is too faulty... */
  493. dev->ports[i - 1].soft_limit_time = grub_get_time_ms () + 250;
  494. dev->ports[i - 1].hard_limit_time = dev->ports[i - 1].soft_limit_time + 1750;
  495. dev->ports[i - 1].state = PORT_STATE_WAITING_FOR_STABLE_POWER;
  496. grub_boot_time ("Scheduling stable power wait for port %p:%d",
  497. dev, i - 1);
  498. continue;
  499. }
  500. }
  501. if (status & GRUB_USB_HUB_STATUS_C_PORT_RESET)
  502. {
  503. grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
  504. | GRUB_USB_REQTYPE_CLASS
  505. | GRUB_USB_REQTYPE_TARGET_OTHER),
  506. GRUB_USB_REQ_CLEAR_FEATURE,
  507. GRUB_USB_HUB_FEATURE_C_PORT_RESET, i, 0, 0);
  508. grub_boot_time ("Port %d reset", i);
  509. if (status & GRUB_USB_HUB_STATUS_PORT_CONNECTED)
  510. {
  511. grub_usb_speed_t speed;
  512. grub_usb_device_t next_dev;
  513. int split_hubport = 0;
  514. int split_hubaddr = 0;
  515. /* Determine the device speed. */
  516. if (status & GRUB_USB_HUB_STATUS_PORT_LOWSPEED)
  517. speed = GRUB_USB_SPEED_LOW;
  518. else
  519. {
  520. if (status & GRUB_USB_HUB_STATUS_PORT_HIGHSPEED)
  521. speed = GRUB_USB_SPEED_HIGH;
  522. else
  523. speed = GRUB_USB_SPEED_FULL;
  524. }
  525. /* Wait a recovery time after reset, spec. says 10ms */
  526. grub_millisleep (10);
  527. /* Find correct values for SPLIT hubport and hubaddr */
  528. if (speed == GRUB_USB_SPEED_HIGH)
  529. {
  530. /* HIGH speed device needs not transaction translation */
  531. split_hubport = 0;
  532. split_hubaddr = 0;
  533. }
  534. else
  535. /* FULL/LOW device needs hub port and hub address
  536. for transaction translation (if connected to EHCI) */
  537. if (dev->speed == GRUB_USB_SPEED_HIGH)
  538. {
  539. /* This port is the first FULL/LOW speed port
  540. in the chain from root hub. Attached device
  541. should use its port number and hub address */
  542. split_hubport = i;
  543. split_hubaddr = dev->addr;
  544. }
  545. else
  546. {
  547. /* This port is NOT the first FULL/LOW speed port
  548. in the chain from root hub. Attached device
  549. should use values inherited from some parent
  550. HIGH speed hub - if any. */
  551. split_hubport = dev->split_hubport;
  552. split_hubaddr = dev->split_hubaddr;
  553. }
  554. /* Add the device and assign a device address to it. */
  555. next_dev = grub_usb_hub_add_dev (&dev->controller, speed,
  556. split_hubport, split_hubaddr);
  557. if (dev->controller.dev->pending_reset)
  558. {
  559. dev->controller.dev->pending_reset = 0;
  560. npending--;
  561. }
  562. if (! next_dev)
  563. continue;
  564. dev->children[i - 1] = next_dev;
  565. /* If the device is a Hub, scan it for more devices. */
  566. if (next_dev->descdev.class == 0x09)
  567. grub_usb_add_hub (next_dev);
  568. }
  569. }
  570. }
  571. }
  572. void
  573. grub_usb_poll_devices (int wait_for_completion)
  574. {
  575. struct grub_usb_hub *hub;
  576. int i;
  577. for (hub = hubs; hub; hub = hub->next)
  578. {
  579. /* Do we have to recheck number of ports? */
  580. /* No, it should be never changed, it should be constant. */
  581. for (i = 0; i < hub->nports; i++)
  582. {
  583. grub_usb_speed_t speed = GRUB_USB_SPEED_NONE;
  584. int changed = 0;
  585. if (hub->controller->dev->pending_reset)
  586. {
  587. /* Check for possible timeout */
  588. if (grub_get_time_ms () > hub->controller->dev->pending_reset)
  589. {
  590. /* Something went wrong, reset device was not
  591. * addressed properly, timeout happened */
  592. hub->controller->dev->pending_reset = 0;
  593. npending--;
  594. }
  595. }
  596. if (!hub->controller->dev->pending_reset)
  597. speed = hub->controller->dev->detect_dev (hub->controller,
  598. i, &changed);
  599. if (changed)
  600. {
  601. detach_device (hub->devices[i]);
  602. hub->devices[i] = NULL;
  603. if (speed != GRUB_USB_SPEED_NONE)
  604. attach_root_port (hub, i, speed);
  605. }
  606. }
  607. }
  608. while (1)
  609. {
  610. rescan = 0;
  611. /* We should check changes of non-root hubs too. */
  612. for (i = 0; i < GRUB_USBHUB_MAX_DEVICES; i++)
  613. {
  614. grub_usb_device_t dev = grub_usb_devs[i];
  615. if (dev && dev->descdev.class == 0x09)
  616. poll_nonroot_hub (dev);
  617. }
  618. while (1)
  619. {
  620. int continue_waiting = 0;
  621. for (i = 0; i < GRUB_USBHUB_MAX_DEVICES; i++)
  622. {
  623. grub_usb_device_t dev = grub_usb_devs[i];
  624. if (dev && dev->descdev.class == 0x09)
  625. continue_waiting = continue_waiting || wait_power_nonroot_hub (dev);
  626. }
  627. if (!continue_waiting)
  628. break;
  629. grub_millisleep (1);
  630. }
  631. if (!(rescan || (npending && wait_for_completion)))
  632. break;
  633. grub_millisleep (25);
  634. }
  635. }
  636. int
  637. grub_usb_iterate (grub_usb_iterate_hook_t hook, void *hook_data)
  638. {
  639. int i;
  640. for (i = 0; i < GRUB_USBHUB_MAX_DEVICES; i++)
  641. {
  642. if (grub_usb_devs[i])
  643. {
  644. if (hook (grub_usb_devs[i], hook_data))
  645. return 1;
  646. }
  647. }
  648. return 0;
  649. }