usb_hub.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * Most of this source has been derived from the Linux USB
  3. * project:
  4. * (C) Copyright Linus Torvalds 1999
  5. * (C) Copyright Johannes Erdfelt 1999-2001
  6. * (C) Copyright Andreas Gal 1999
  7. * (C) Copyright Gregory P. Smith 1999
  8. * (C) Copyright Deti Fliegl 1999 (new USB architecture)
  9. * (C) Copyright Randy Dunlap 2000
  10. * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id)
  11. * (C) Copyright Yggdrasil Computing, Inc. 2000
  12. * (usb_device_id matching changes by Adam J. Richter)
  13. *
  14. * Adapted for U-Boot:
  15. * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
  16. *
  17. * SPDX-License-Identifier: GPL-2.0+
  18. */
  19. /****************************************************************************
  20. * HUB "Driver"
  21. * Probes device for being a hub and configurate it
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <dm.h>
  26. #include <errno.h>
  27. #include <memalign.h>
  28. #include <asm/processor.h>
  29. #include <asm/unaligned.h>
  30. #include <linux/ctype.h>
  31. #include <linux/list.h>
  32. #include <asm/byteorder.h>
  33. #ifdef CONFIG_SANDBOX
  34. #include <asm/state.h>
  35. #endif
  36. #include <asm/unaligned.h>
  37. DECLARE_GLOBAL_DATA_PTR;
  38. #include <usb.h>
  39. #define USB_BUFSIZ 512
  40. #define HUB_SHORT_RESET_TIME 20
  41. #define HUB_LONG_RESET_TIME 200
  42. #define PORT_OVERCURRENT_MAX_SCAN_COUNT 3
  43. struct usb_device_scan {
  44. struct usb_device *dev; /* USB hub device to scan */
  45. struct usb_hub_device *hub; /* USB hub struct */
  46. int port; /* USB port to scan */
  47. struct list_head list;
  48. };
  49. /* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */
  50. static struct usb_hub_device hub_dev[USB_MAX_HUB];
  51. static int usb_hub_index;
  52. static LIST_HEAD(usb_scan_list);
  53. __weak void usb_hub_reset_devices(int port)
  54. {
  55. return;
  56. }
  57. static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
  58. {
  59. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  60. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  61. USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
  62. }
  63. static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
  64. {
  65. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  66. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
  67. port, NULL, 0, USB_CNTL_TIMEOUT);
  68. }
  69. static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
  70. {
  71. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  72. USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
  73. port, NULL, 0, USB_CNTL_TIMEOUT);
  74. }
  75. static int usb_get_hub_status(struct usb_device *dev, void *data)
  76. {
  77. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  78. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
  79. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  80. }
  81. int usb_get_port_status(struct usb_device *dev, int port, void *data)
  82. {
  83. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  84. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
  85. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  86. }
  87. static void usb_hub_power_on(struct usb_hub_device *hub)
  88. {
  89. int i;
  90. struct usb_device *dev;
  91. unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
  92. const char *env;
  93. dev = hub->pusb_dev;
  94. debug("enabling power on all ports\n");
  95. for (i = 0; i < dev->maxchild; i++) {
  96. usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
  97. debug("port %d returns %lX\n", i + 1, dev->status);
  98. }
  99. #ifdef CONFIG_SANDBOX
  100. /*
  101. * Don't set timeout / delay values here. This results
  102. * in these values still being reset to 0.
  103. */
  104. if (state_get_skip_delays())
  105. return;
  106. #endif
  107. /*
  108. * Wait for power to become stable,
  109. * plus spec-defined max time for device to connect
  110. * but allow this time to be increased via env variable as some
  111. * devices break the spec and require longer warm-up times
  112. */
  113. env = getenv("usb_pgood_delay");
  114. if (env)
  115. pgood_delay = max(pgood_delay,
  116. (unsigned)simple_strtol(env, NULL, 0));
  117. debug("pgood_delay=%dms\n", pgood_delay);
  118. /*
  119. * Do a minimum delay of the larger value of 100ms or pgood_delay
  120. * so that the power can stablize before the devices are queried
  121. */
  122. hub->query_delay = get_timer(0) + max(100, (int)pgood_delay);
  123. /*
  124. * Record the power-on timeout here. The max. delay (timeout)
  125. * will be done based on this value in the USB port loop in
  126. * usb_hub_configure() later.
  127. */
  128. hub->connect_timeout = hub->query_delay + 1000;
  129. debug("devnum=%d poweron: query_delay=%d connect_timeout=%d\n",
  130. dev->devnum, max(100, (int)pgood_delay),
  131. max(100, (int)pgood_delay) + 1000);
  132. }
  133. void usb_hub_reset(void)
  134. {
  135. usb_hub_index = 0;
  136. /* Zero out global hub_dev in case its re-used again */
  137. memset(hub_dev, 0, sizeof(hub_dev));
  138. }
  139. static struct usb_hub_device *usb_hub_allocate(void)
  140. {
  141. if (usb_hub_index < USB_MAX_HUB)
  142. return &hub_dev[usb_hub_index++];
  143. printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
  144. return NULL;
  145. }
  146. #define MAX_TRIES 5
  147. static inline char *portspeed(int portstatus)
  148. {
  149. char *speed_str;
  150. switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
  151. case USB_PORT_STAT_SUPER_SPEED:
  152. speed_str = "5 Gb/s";
  153. break;
  154. case USB_PORT_STAT_HIGH_SPEED:
  155. speed_str = "480 Mb/s";
  156. break;
  157. case USB_PORT_STAT_LOW_SPEED:
  158. speed_str = "1.5 Mb/s";
  159. break;
  160. default:
  161. speed_str = "12 Mb/s";
  162. break;
  163. }
  164. return speed_str;
  165. }
  166. int legacy_hub_port_reset(struct usb_device *dev, int port,
  167. unsigned short *portstat)
  168. {
  169. int err, tries;
  170. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  171. unsigned short portstatus, portchange;
  172. int delay = HUB_SHORT_RESET_TIME; /* start with short reset delay */
  173. #ifdef CONFIG_DM_USB
  174. debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name,
  175. port + 1);
  176. #else
  177. debug("%s: resetting port %d...\n", __func__, port + 1);
  178. #endif
  179. for (tries = 0; tries < MAX_TRIES; tries++) {
  180. err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
  181. if (err < 0)
  182. return err;
  183. mdelay(delay);
  184. if (usb_get_port_status(dev, port + 1, portsts) < 0) {
  185. debug("get_port_status failed status %lX\n",
  186. dev->status);
  187. return -1;
  188. }
  189. portstatus = le16_to_cpu(portsts->wPortStatus);
  190. portchange = le16_to_cpu(portsts->wPortChange);
  191. debug("portstatus %x, change %x, %s\n", portstatus, portchange,
  192. portspeed(portstatus));
  193. debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
  194. " USB_PORT_STAT_ENABLE %d\n",
  195. (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
  196. (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
  197. (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
  198. /*
  199. * Perhaps we should check for the following here:
  200. * - C_CONNECTION hasn't been set.
  201. * - CONNECTION is still set.
  202. *
  203. * Doing so would ensure that the device is still connected
  204. * to the bus, and hasn't been unplugged or replaced while the
  205. * USB bus reset was going on.
  206. *
  207. * However, if we do that, then (at least) a San Disk Ultra
  208. * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
  209. * Tegra Jetson TK1 board. For some reason, the device appears
  210. * to briefly drop off the bus when this second bus reset is
  211. * executed, yet if we retry this loop, it'll eventually come
  212. * back after another reset or two.
  213. */
  214. if (portstatus & USB_PORT_STAT_ENABLE)
  215. break;
  216. /* Switch to long reset delay for the next round */
  217. delay = HUB_LONG_RESET_TIME;
  218. }
  219. if (tries == MAX_TRIES) {
  220. debug("Cannot enable port %i after %i retries, " \
  221. "disabling port.\n", port + 1, MAX_TRIES);
  222. debug("Maybe the USB cable is bad?\n");
  223. return -1;
  224. }
  225. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
  226. *portstat = portstatus;
  227. return 0;
  228. }
  229. #ifdef CONFIG_DM_USB
  230. int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat)
  231. {
  232. struct usb_device *udev = dev_get_parent_priv(dev);
  233. return legacy_hub_port_reset(udev, port, portstat);
  234. }
  235. #endif
  236. int usb_hub_port_connect_change(struct usb_device *dev, int port)
  237. {
  238. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  239. unsigned short portstatus;
  240. int ret, speed;
  241. /* Check status */
  242. ret = usb_get_port_status(dev, port + 1, portsts);
  243. if (ret < 0) {
  244. debug("get_port_status failed\n");
  245. return ret;
  246. }
  247. portstatus = le16_to_cpu(portsts->wPortStatus);
  248. debug("portstatus %x, change %x, %s\n",
  249. portstatus,
  250. le16_to_cpu(portsts->wPortChange),
  251. portspeed(portstatus));
  252. /* Clear the connection change status */
  253. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
  254. /* Disconnect any existing devices under this port */
  255. if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
  256. (!(portstatus & USB_PORT_STAT_ENABLE))) ||
  257. usb_device_has_child_on_port(dev, port)) {
  258. debug("usb_disconnect(&hub->children[port]);\n");
  259. /* Return now if nothing is connected */
  260. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  261. return -ENOTCONN;
  262. }
  263. /* Reset the port */
  264. ret = legacy_hub_port_reset(dev, port, &portstatus);
  265. if (ret < 0) {
  266. if (ret != -ENXIO)
  267. printf("cannot reset port %i!?\n", port + 1);
  268. return ret;
  269. }
  270. switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
  271. case USB_PORT_STAT_SUPER_SPEED:
  272. speed = USB_SPEED_SUPER;
  273. break;
  274. case USB_PORT_STAT_HIGH_SPEED:
  275. speed = USB_SPEED_HIGH;
  276. break;
  277. case USB_PORT_STAT_LOW_SPEED:
  278. speed = USB_SPEED_LOW;
  279. break;
  280. default:
  281. speed = USB_SPEED_FULL;
  282. break;
  283. }
  284. #ifdef CONFIG_DM_USB
  285. struct udevice *child;
  286. ret = usb_scan_device(dev->dev, port + 1, speed, &child);
  287. #else
  288. struct usb_device *usb;
  289. ret = usb_alloc_new_device(dev->controller, &usb);
  290. if (ret) {
  291. printf("cannot create new device: ret=%d", ret);
  292. return ret;
  293. }
  294. dev->children[port] = usb;
  295. usb->speed = speed;
  296. usb->parent = dev;
  297. usb->portnr = port + 1;
  298. /* Run it through the hoops (find a driver, etc) */
  299. ret = usb_new_device(usb);
  300. if (ret < 0) {
  301. /* Woops, disable the port */
  302. usb_free_device(dev->controller);
  303. dev->children[port] = NULL;
  304. }
  305. #endif
  306. if (ret < 0) {
  307. debug("hub: disabling port %d\n", port + 1);
  308. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
  309. }
  310. return ret;
  311. }
  312. static int usb_scan_port(struct usb_device_scan *usb_scan)
  313. {
  314. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  315. unsigned short portstatus;
  316. unsigned short portchange;
  317. struct usb_device *dev;
  318. struct usb_hub_device *hub;
  319. int ret = 0;
  320. int i;
  321. dev = usb_scan->dev;
  322. hub = usb_scan->hub;
  323. i = usb_scan->port;
  324. /*
  325. * Don't talk to the device before the query delay is expired.
  326. * This is needed for voltages to stabalize.
  327. */
  328. if (get_timer(0) < hub->query_delay)
  329. return 0;
  330. ret = usb_get_port_status(dev, i + 1, portsts);
  331. if (ret < 0) {
  332. debug("get_port_status failed\n");
  333. if (get_timer(0) >= hub->connect_timeout) {
  334. debug("devnum=%d port=%d: timeout\n",
  335. dev->devnum, i + 1);
  336. /* Remove this device from scanning list */
  337. list_del(&usb_scan->list);
  338. free(usb_scan);
  339. return 0;
  340. }
  341. return 0;
  342. }
  343. portstatus = le16_to_cpu(portsts->wPortStatus);
  344. portchange = le16_to_cpu(portsts->wPortChange);
  345. debug("Port %d Status %X Change %X\n", i + 1, portstatus, portchange);
  346. /* No connection change happened, wait a bit more. */
  347. if (!(portchange & USB_PORT_STAT_C_CONNECTION)) {
  348. if (get_timer(0) >= hub->connect_timeout) {
  349. debug("devnum=%d port=%d: timeout\n",
  350. dev->devnum, i + 1);
  351. /* Remove this device from scanning list */
  352. list_del(&usb_scan->list);
  353. free(usb_scan);
  354. return 0;
  355. }
  356. return 0;
  357. }
  358. /* Test if the connection came up, and if not exit */
  359. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  360. return 0;
  361. /* A new USB device is ready at this point */
  362. debug("devnum=%d port=%d: USB dev found\n", dev->devnum, i + 1);
  363. usb_hub_port_connect_change(dev, i);
  364. if (portchange & USB_PORT_STAT_C_ENABLE) {
  365. debug("port %d enable change, status %x\n", i + 1, portstatus);
  366. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
  367. /*
  368. * The following hack causes a ghost device problem
  369. * to Faraday EHCI
  370. */
  371. #ifndef CONFIG_USB_EHCI_FARADAY
  372. /*
  373. * EM interference sometimes causes bad shielded USB
  374. * devices to be shutdown by the hub, this hack enables
  375. * them again. Works at least with mouse driver
  376. */
  377. if (!(portstatus & USB_PORT_STAT_ENABLE) &&
  378. (portstatus & USB_PORT_STAT_CONNECTION) &&
  379. usb_device_has_child_on_port(dev, i)) {
  380. debug("already running port %i disabled by hub (EMI?), re-enabling...\n",
  381. i + 1);
  382. usb_hub_port_connect_change(dev, i);
  383. }
  384. #endif
  385. }
  386. if (portstatus & USB_PORT_STAT_SUSPEND) {
  387. debug("port %d suspend change\n", i + 1);
  388. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_SUSPEND);
  389. }
  390. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  391. debug("port %d over-current change\n", i + 1);
  392. usb_clear_port_feature(dev, i + 1,
  393. USB_PORT_FEAT_C_OVER_CURRENT);
  394. /* Only power-on this one port */
  395. usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
  396. hub->overcurrent_count[i]++;
  397. /*
  398. * If the max-scan-count is not reached, return without removing
  399. * the device from scan-list. This will re-issue a new scan.
  400. */
  401. if (hub->overcurrent_count[i] <=
  402. PORT_OVERCURRENT_MAX_SCAN_COUNT)
  403. return 0;
  404. /* Otherwise the device will get removed */
  405. printf("Port %d over-current occurred %d times\n", i + 1,
  406. hub->overcurrent_count[i]);
  407. }
  408. if (portchange & USB_PORT_STAT_C_RESET) {
  409. debug("port %d reset change\n", i + 1);
  410. usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
  411. }
  412. /*
  413. * We're done with this device, so let's remove this device from
  414. * scanning list
  415. */
  416. list_del(&usb_scan->list);
  417. free(usb_scan);
  418. return 0;
  419. }
  420. static int usb_device_list_scan(void)
  421. {
  422. struct usb_device_scan *usb_scan;
  423. struct usb_device_scan *tmp;
  424. static int running;
  425. int ret = 0;
  426. /* Only run this loop once for each controller */
  427. if (running)
  428. return 0;
  429. running = 1;
  430. while (1) {
  431. /* We're done, once the list is empty again */
  432. if (list_empty(&usb_scan_list))
  433. goto out;
  434. list_for_each_entry_safe(usb_scan, tmp, &usb_scan_list, list) {
  435. int ret;
  436. /* Scan this port */
  437. ret = usb_scan_port(usb_scan);
  438. if (ret)
  439. goto out;
  440. }
  441. }
  442. out:
  443. /*
  444. * This USB controller has finished scanning all its connected
  445. * USB devices. Set "running" back to 0, so that other USB controllers
  446. * will scan their devices too.
  447. */
  448. running = 0;
  449. return ret;
  450. }
  451. static int usb_hub_configure(struct usb_device *dev)
  452. {
  453. int i, length;
  454. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
  455. unsigned char *bitmap;
  456. short hubCharacteristics;
  457. struct usb_hub_descriptor *descriptor;
  458. struct usb_hub_device *hub;
  459. __maybe_unused struct usb_hub_status *hubsts;
  460. int ret;
  461. /* "allocate" Hub device */
  462. hub = usb_hub_allocate();
  463. if (hub == NULL)
  464. return -ENOMEM;
  465. hub->pusb_dev = dev;
  466. /* Get the the hub descriptor */
  467. ret = usb_get_hub_descriptor(dev, buffer, 4);
  468. if (ret < 0) {
  469. debug("usb_hub_configure: failed to get hub " \
  470. "descriptor, giving up %lX\n", dev->status);
  471. return ret;
  472. }
  473. descriptor = (struct usb_hub_descriptor *)buffer;
  474. length = min_t(int, descriptor->bLength,
  475. sizeof(struct usb_hub_descriptor));
  476. ret = usb_get_hub_descriptor(dev, buffer, length);
  477. if (ret < 0) {
  478. debug("usb_hub_configure: failed to get hub " \
  479. "descriptor 2nd giving up %lX\n", dev->status);
  480. return ret;
  481. }
  482. memcpy((unsigned char *)&hub->desc, buffer, length);
  483. /* adjust 16bit values */
  484. put_unaligned(le16_to_cpu(get_unaligned(
  485. &descriptor->wHubCharacteristics)),
  486. &hub->desc.wHubCharacteristics);
  487. /* set the bitmap */
  488. bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
  489. /* devices not removable by default */
  490. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
  491. bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
  492. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
  493. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  494. hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
  495. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  496. hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
  497. dev->maxchild = descriptor->bNbrPorts;
  498. debug("%d ports detected\n", dev->maxchild);
  499. hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
  500. switch (hubCharacteristics & HUB_CHAR_LPSM) {
  501. case 0x00:
  502. debug("ganged power switching\n");
  503. break;
  504. case 0x01:
  505. debug("individual port power switching\n");
  506. break;
  507. case 0x02:
  508. case 0x03:
  509. debug("unknown reserved power switching mode\n");
  510. break;
  511. }
  512. if (hubCharacteristics & HUB_CHAR_COMPOUND)
  513. debug("part of a compound device\n");
  514. else
  515. debug("standalone hub\n");
  516. switch (hubCharacteristics & HUB_CHAR_OCPM) {
  517. case 0x00:
  518. debug("global over-current protection\n");
  519. break;
  520. case 0x08:
  521. debug("individual port over-current protection\n");
  522. break;
  523. case 0x10:
  524. case 0x18:
  525. debug("no over-current protection\n");
  526. break;
  527. }
  528. debug("power on to power good time: %dms\n",
  529. descriptor->bPwrOn2PwrGood * 2);
  530. debug("hub controller current requirement: %dmA\n",
  531. descriptor->bHubContrCurrent);
  532. for (i = 0; i < dev->maxchild; i++)
  533. debug("port %d is%s removable\n", i + 1,
  534. hub->desc.DeviceRemovable[(i + 1) / 8] & \
  535. (1 << ((i + 1) % 8)) ? " not" : "");
  536. if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
  537. debug("usb_hub_configure: failed to get Status - " \
  538. "too long: %d\n", descriptor->bLength);
  539. return -EFBIG;
  540. }
  541. ret = usb_get_hub_status(dev, buffer);
  542. if (ret < 0) {
  543. debug("usb_hub_configure: failed to get Status %lX\n",
  544. dev->status);
  545. return ret;
  546. }
  547. #ifdef DEBUG
  548. hubsts = (struct usb_hub_status *)buffer;
  549. #endif
  550. debug("get_hub_status returned status %X, change %X\n",
  551. le16_to_cpu(hubsts->wHubStatus),
  552. le16_to_cpu(hubsts->wHubChange));
  553. debug("local power source is %s\n",
  554. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
  555. "lost (inactive)" : "good");
  556. debug("%sover-current condition exists\n",
  557. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
  558. "" : "no ");
  559. usb_hub_power_on(hub);
  560. /*
  561. * Reset any devices that may be in a bad state when applying
  562. * the power. This is a __weak function. Resetting of the devices
  563. * should occur in the board file of the device.
  564. */
  565. for (i = 0; i < dev->maxchild; i++)
  566. usb_hub_reset_devices(i + 1);
  567. /*
  568. * Only add the connected USB devices, including potential hubs,
  569. * to a scanning list. This list will get scanned and devices that
  570. * are detected (either via port connected or via port timeout)
  571. * will get removed from this list. Scanning of the devices on this
  572. * list will continue until all devices are removed.
  573. */
  574. for (i = 0; i < dev->maxchild; i++) {
  575. struct usb_device_scan *usb_scan;
  576. usb_scan = calloc(1, sizeof(*usb_scan));
  577. if (!usb_scan) {
  578. printf("Can't allocate memory for USB device!\n");
  579. return -ENOMEM;
  580. }
  581. usb_scan->dev = dev;
  582. usb_scan->hub = hub;
  583. usb_scan->port = i;
  584. list_add_tail(&usb_scan->list, &usb_scan_list);
  585. }
  586. /*
  587. * And now call the scanning code which loops over the generated list
  588. */
  589. ret = usb_device_list_scan();
  590. return ret;
  591. }
  592. static int usb_hub_check(struct usb_device *dev, int ifnum)
  593. {
  594. struct usb_interface *iface;
  595. struct usb_endpoint_descriptor *ep = NULL;
  596. iface = &dev->config.if_desc[ifnum];
  597. /* Is it a hub? */
  598. if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
  599. goto err;
  600. /* Some hubs have a subclass of 1, which AFAICT according to the */
  601. /* specs is not defined, but it works */
  602. if ((iface->desc.bInterfaceSubClass != 0) &&
  603. (iface->desc.bInterfaceSubClass != 1))
  604. goto err;
  605. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  606. if (iface->desc.bNumEndpoints != 1)
  607. goto err;
  608. ep = &iface->ep_desc[0];
  609. /* Output endpoint? Curiousier and curiousier.. */
  610. if (!(ep->bEndpointAddress & USB_DIR_IN))
  611. goto err;
  612. /* If it's not an interrupt endpoint, we'd better punt! */
  613. if ((ep->bmAttributes & 3) != 3)
  614. goto err;
  615. /* We found a hub */
  616. debug("USB hub found\n");
  617. return 0;
  618. err:
  619. debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
  620. iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass,
  621. iface->desc.bNumEndpoints);
  622. if (ep) {
  623. debug(" bEndpointAddress=%#x, bmAttributes=%d",
  624. ep->bEndpointAddress, ep->bmAttributes);
  625. }
  626. return -ENOENT;
  627. }
  628. int usb_hub_probe(struct usb_device *dev, int ifnum)
  629. {
  630. int ret;
  631. ret = usb_hub_check(dev, ifnum);
  632. if (ret)
  633. return 0;
  634. ret = usb_hub_configure(dev);
  635. return ret;
  636. }
  637. #ifdef CONFIG_DM_USB
  638. int usb_hub_scan(struct udevice *hub)
  639. {
  640. struct usb_device *udev = dev_get_parent_priv(hub);
  641. return usb_hub_configure(udev);
  642. }
  643. static int usb_hub_post_probe(struct udevice *dev)
  644. {
  645. debug("%s\n", __func__);
  646. return usb_hub_scan(dev);
  647. }
  648. static const struct udevice_id usb_hub_ids[] = {
  649. { .compatible = "usb-hub" },
  650. { }
  651. };
  652. U_BOOT_DRIVER(usb_generic_hub) = {
  653. .name = "usb_hub",
  654. .id = UCLASS_USB_HUB,
  655. .of_match = usb_hub_ids,
  656. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  657. };
  658. UCLASS_DRIVER(usb_hub) = {
  659. .id = UCLASS_USB_HUB,
  660. .name = "usb_hub",
  661. .post_bind = dm_scan_fdt_dev,
  662. .post_probe = usb_hub_post_probe,
  663. .child_pre_probe = usb_child_pre_probe,
  664. .per_child_auto_alloc_size = sizeof(struct usb_device),
  665. .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
  666. };
  667. static const struct usb_device_id hub_id_table[] = {
  668. {
  669. .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
  670. .bDeviceClass = USB_CLASS_HUB
  671. },
  672. { } /* Terminating entry */
  673. };
  674. U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table);
  675. #endif