usb_hub.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 <asm/byteorder.h>
  32. #ifdef CONFIG_SANDBOX
  33. #include <asm/state.h>
  34. #endif
  35. #include <asm/unaligned.h>
  36. #include <dm/root.h>
  37. DECLARE_GLOBAL_DATA_PTR;
  38. #include <usb.h>
  39. #ifdef CONFIG_4xx
  40. #include <asm/4xx_pci.h>
  41. #endif
  42. #define USB_BUFSIZ 512
  43. /* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */
  44. static struct usb_hub_device hub_dev[USB_MAX_HUB];
  45. static int usb_hub_index;
  46. __weak void usb_hub_reset_devices(int port)
  47. {
  48. return;
  49. }
  50. static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
  51. {
  52. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  53. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  54. USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
  55. }
  56. static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
  57. {
  58. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  59. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
  60. port, NULL, 0, USB_CNTL_TIMEOUT);
  61. }
  62. static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
  63. {
  64. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  65. USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
  66. port, NULL, 0, USB_CNTL_TIMEOUT);
  67. }
  68. static int usb_get_hub_status(struct usb_device *dev, void *data)
  69. {
  70. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  71. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
  72. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  73. }
  74. int usb_get_port_status(struct usb_device *dev, int port, void *data)
  75. {
  76. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  77. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
  78. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  79. }
  80. static void usb_hub_power_on(struct usb_hub_device *hub)
  81. {
  82. int i;
  83. struct usb_device *dev;
  84. unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
  85. const char *env;
  86. dev = hub->pusb_dev;
  87. debug("enabling power on all ports\n");
  88. for (i = 0; i < dev->maxchild; i++) {
  89. usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
  90. debug("port %d returns %lX\n", i + 1, dev->status);
  91. }
  92. /*
  93. * Wait for power to become stable,
  94. * plus spec-defined max time for device to connect
  95. * but allow this time to be increased via env variable as some
  96. * devices break the spec and require longer warm-up times
  97. */
  98. env = getenv("usb_pgood_delay");
  99. if (env)
  100. pgood_delay = max(pgood_delay,
  101. (unsigned)simple_strtol(env, NULL, 0));
  102. debug("pgood_delay=%dms\n", pgood_delay);
  103. mdelay(pgood_delay + 1000);
  104. }
  105. void usb_hub_reset(void)
  106. {
  107. usb_hub_index = 0;
  108. }
  109. static struct usb_hub_device *usb_hub_allocate(void)
  110. {
  111. if (usb_hub_index < USB_MAX_HUB)
  112. return &hub_dev[usb_hub_index++];
  113. printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
  114. return NULL;
  115. }
  116. #define MAX_TRIES 5
  117. static inline char *portspeed(int portstatus)
  118. {
  119. char *speed_str;
  120. switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
  121. case USB_PORT_STAT_SUPER_SPEED:
  122. speed_str = "5 Gb/s";
  123. break;
  124. case USB_PORT_STAT_HIGH_SPEED:
  125. speed_str = "480 Mb/s";
  126. break;
  127. case USB_PORT_STAT_LOW_SPEED:
  128. speed_str = "1.5 Mb/s";
  129. break;
  130. default:
  131. speed_str = "12 Mb/s";
  132. break;
  133. }
  134. return speed_str;
  135. }
  136. int legacy_hub_port_reset(struct usb_device *dev, int port,
  137. unsigned short *portstat)
  138. {
  139. int err, tries;
  140. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  141. unsigned short portstatus, portchange;
  142. #ifdef CONFIG_DM_USB
  143. debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name,
  144. port + 1);
  145. #else
  146. debug("%s: resetting port %d...\n", __func__, port + 1);
  147. #endif
  148. for (tries = 0; tries < MAX_TRIES; tries++) {
  149. err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
  150. if (err < 0)
  151. return err;
  152. mdelay(200);
  153. if (usb_get_port_status(dev, port + 1, portsts) < 0) {
  154. debug("get_port_status failed status %lX\n",
  155. dev->status);
  156. return -1;
  157. }
  158. portstatus = le16_to_cpu(portsts->wPortStatus);
  159. portchange = le16_to_cpu(portsts->wPortChange);
  160. debug("portstatus %x, change %x, %s\n", portstatus, portchange,
  161. portspeed(portstatus));
  162. debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
  163. " USB_PORT_STAT_ENABLE %d\n",
  164. (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
  165. (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
  166. (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
  167. /*
  168. * Perhaps we should check for the following here:
  169. * - C_CONNECTION hasn't been set.
  170. * - CONNECTION is still set.
  171. *
  172. * Doing so would ensure that the device is still connected
  173. * to the bus, and hasn't been unplugged or replaced while the
  174. * USB bus reset was going on.
  175. *
  176. * However, if we do that, then (at least) a San Disk Ultra
  177. * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
  178. * Tegra Jetson TK1 board. For some reason, the device appears
  179. * to briefly drop off the bus when this second bus reset is
  180. * executed, yet if we retry this loop, it'll eventually come
  181. * back after another reset or two.
  182. */
  183. if (portstatus & USB_PORT_STAT_ENABLE)
  184. break;
  185. mdelay(200);
  186. }
  187. if (tries == MAX_TRIES) {
  188. debug("Cannot enable port %i after %i retries, " \
  189. "disabling port.\n", port + 1, MAX_TRIES);
  190. debug("Maybe the USB cable is bad?\n");
  191. return -1;
  192. }
  193. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
  194. *portstat = portstatus;
  195. return 0;
  196. }
  197. #ifdef CONFIG_DM_USB
  198. int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat)
  199. {
  200. struct usb_device *udev = dev_get_parent_priv(dev);
  201. return legacy_hub_port_reset(udev, port, portstat);
  202. }
  203. #endif
  204. int usb_hub_port_connect_change(struct usb_device *dev, int port)
  205. {
  206. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  207. unsigned short portstatus;
  208. int ret, speed;
  209. /* Check status */
  210. ret = usb_get_port_status(dev, port + 1, portsts);
  211. if (ret < 0) {
  212. debug("get_port_status failed\n");
  213. return ret;
  214. }
  215. portstatus = le16_to_cpu(portsts->wPortStatus);
  216. debug("portstatus %x, change %x, %s\n",
  217. portstatus,
  218. le16_to_cpu(portsts->wPortChange),
  219. portspeed(portstatus));
  220. /* Clear the connection change status */
  221. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
  222. /* Disconnect any existing devices under this port */
  223. if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
  224. (!(portstatus & USB_PORT_STAT_ENABLE))) ||
  225. usb_device_has_child_on_port(dev, port)) {
  226. debug("usb_disconnect(&hub->children[port]);\n");
  227. /* Return now if nothing is connected */
  228. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  229. return -ENOTCONN;
  230. }
  231. mdelay(200);
  232. /* Reset the port */
  233. ret = legacy_hub_port_reset(dev, port, &portstatus);
  234. if (ret < 0) {
  235. if (ret != -ENXIO)
  236. printf("cannot reset port %i!?\n", port + 1);
  237. return ret;
  238. }
  239. mdelay(200);
  240. switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
  241. case USB_PORT_STAT_SUPER_SPEED:
  242. speed = USB_SPEED_SUPER;
  243. break;
  244. case USB_PORT_STAT_HIGH_SPEED:
  245. speed = USB_SPEED_HIGH;
  246. break;
  247. case USB_PORT_STAT_LOW_SPEED:
  248. speed = USB_SPEED_LOW;
  249. break;
  250. default:
  251. speed = USB_SPEED_FULL;
  252. break;
  253. }
  254. #ifdef CONFIG_DM_USB
  255. struct udevice *child;
  256. ret = usb_scan_device(dev->dev, port + 1, speed, &child);
  257. #else
  258. struct usb_device *usb;
  259. ret = usb_alloc_new_device(dev->controller, &usb);
  260. if (ret) {
  261. printf("cannot create new device: ret=%d", ret);
  262. return ret;
  263. }
  264. dev->children[port] = usb;
  265. usb->speed = speed;
  266. usb->parent = dev;
  267. usb->portnr = port + 1;
  268. /* Run it through the hoops (find a driver, etc) */
  269. ret = usb_new_device(usb);
  270. if (ret < 0) {
  271. /* Woops, disable the port */
  272. usb_free_device(dev->controller);
  273. dev->children[port] = NULL;
  274. }
  275. #endif
  276. if (ret < 0) {
  277. debug("hub: disabling port %d\n", port + 1);
  278. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
  279. }
  280. return ret;
  281. }
  282. static int usb_hub_configure(struct usb_device *dev)
  283. {
  284. int i, length;
  285. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
  286. unsigned char *bitmap;
  287. short hubCharacteristics;
  288. struct usb_hub_descriptor *descriptor;
  289. struct usb_hub_device *hub;
  290. __maybe_unused struct usb_hub_status *hubsts;
  291. int ret;
  292. /* "allocate" Hub device */
  293. hub = usb_hub_allocate();
  294. if (hub == NULL)
  295. return -ENOMEM;
  296. hub->pusb_dev = dev;
  297. /* Get the the hub descriptor */
  298. ret = usb_get_hub_descriptor(dev, buffer, 4);
  299. if (ret < 0) {
  300. debug("usb_hub_configure: failed to get hub " \
  301. "descriptor, giving up %lX\n", dev->status);
  302. return ret;
  303. }
  304. descriptor = (struct usb_hub_descriptor *)buffer;
  305. length = min_t(int, descriptor->bLength,
  306. sizeof(struct usb_hub_descriptor));
  307. ret = usb_get_hub_descriptor(dev, buffer, length);
  308. if (ret < 0) {
  309. debug("usb_hub_configure: failed to get hub " \
  310. "descriptor 2nd giving up %lX\n", dev->status);
  311. return ret;
  312. }
  313. memcpy((unsigned char *)&hub->desc, buffer, length);
  314. /* adjust 16bit values */
  315. put_unaligned(le16_to_cpu(get_unaligned(
  316. &descriptor->wHubCharacteristics)),
  317. &hub->desc.wHubCharacteristics);
  318. /* set the bitmap */
  319. bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
  320. /* devices not removable by default */
  321. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
  322. bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
  323. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
  324. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  325. hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
  326. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  327. hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
  328. dev->maxchild = descriptor->bNbrPorts;
  329. debug("%d ports detected\n", dev->maxchild);
  330. hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
  331. switch (hubCharacteristics & HUB_CHAR_LPSM) {
  332. case 0x00:
  333. debug("ganged power switching\n");
  334. break;
  335. case 0x01:
  336. debug("individual port power switching\n");
  337. break;
  338. case 0x02:
  339. case 0x03:
  340. debug("unknown reserved power switching mode\n");
  341. break;
  342. }
  343. if (hubCharacteristics & HUB_CHAR_COMPOUND)
  344. debug("part of a compound device\n");
  345. else
  346. debug("standalone hub\n");
  347. switch (hubCharacteristics & HUB_CHAR_OCPM) {
  348. case 0x00:
  349. debug("global over-current protection\n");
  350. break;
  351. case 0x08:
  352. debug("individual port over-current protection\n");
  353. break;
  354. case 0x10:
  355. case 0x18:
  356. debug("no over-current protection\n");
  357. break;
  358. }
  359. debug("power on to power good time: %dms\n",
  360. descriptor->bPwrOn2PwrGood * 2);
  361. debug("hub controller current requirement: %dmA\n",
  362. descriptor->bHubContrCurrent);
  363. for (i = 0; i < dev->maxchild; i++)
  364. debug("port %d is%s removable\n", i + 1,
  365. hub->desc.DeviceRemovable[(i + 1) / 8] & \
  366. (1 << ((i + 1) % 8)) ? " not" : "");
  367. if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
  368. debug("usb_hub_configure: failed to get Status - " \
  369. "too long: %d\n", descriptor->bLength);
  370. return -EFBIG;
  371. }
  372. ret = usb_get_hub_status(dev, buffer);
  373. if (ret < 0) {
  374. debug("usb_hub_configure: failed to get Status %lX\n",
  375. dev->status);
  376. return ret;
  377. }
  378. #ifdef DEBUG
  379. hubsts = (struct usb_hub_status *)buffer;
  380. #endif
  381. debug("get_hub_status returned status %X, change %X\n",
  382. le16_to_cpu(hubsts->wHubStatus),
  383. le16_to_cpu(hubsts->wHubChange));
  384. debug("local power source is %s\n",
  385. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
  386. "lost (inactive)" : "good");
  387. debug("%sover-current condition exists\n",
  388. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
  389. "" : "no ");
  390. usb_hub_power_on(hub);
  391. /*
  392. * Reset any devices that may be in a bad state when applying
  393. * the power. This is a __weak function. Resetting of the devices
  394. * should occur in the board file of the device.
  395. */
  396. for (i = 0; i < dev->maxchild; i++)
  397. usb_hub_reset_devices(i + 1);
  398. for (i = 0; i < dev->maxchild; i++) {
  399. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  400. unsigned short portstatus, portchange;
  401. int ret;
  402. ulong start = get_timer(0);
  403. uint delay = CONFIG_SYS_HZ;
  404. #ifdef CONFIG_SANDBOX
  405. if (state_get_skip_delays())
  406. delay = 0;
  407. #endif
  408. #ifdef CONFIG_DM_USB
  409. debug("\n\nScanning '%s' port %d\n", dev->dev->name, i + 1);
  410. #else
  411. debug("\n\nScanning port %d\n", i + 1);
  412. #endif
  413. /*
  414. * Wait for (whichever finishes first)
  415. * - A maximum of 10 seconds
  416. * This is a purely observational value driven by connecting
  417. * a few broken pen drives and taking the max * 1.5 approach
  418. * - connection_change and connection state to report same
  419. * state
  420. */
  421. do {
  422. ret = usb_get_port_status(dev, i + 1, portsts);
  423. if (ret < 0) {
  424. debug("get_port_status failed\n");
  425. break;
  426. }
  427. portstatus = le16_to_cpu(portsts->wPortStatus);
  428. portchange = le16_to_cpu(portsts->wPortChange);
  429. /* No connection change happened, wait a bit more. */
  430. if (!(portchange & USB_PORT_STAT_C_CONNECTION))
  431. continue;
  432. /* Test if the connection came up, and if so, exit. */
  433. if (portstatus & USB_PORT_STAT_CONNECTION)
  434. break;
  435. } while (get_timer(start) < delay);
  436. if (ret < 0)
  437. continue;
  438. debug("Port %d Status %X Change %X\n",
  439. i + 1, portstatus, portchange);
  440. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  441. debug("port %d connection change\n", i + 1);
  442. usb_hub_port_connect_change(dev, i);
  443. }
  444. if (portchange & USB_PORT_STAT_C_ENABLE) {
  445. debug("port %d enable change, status %x\n",
  446. i + 1, portstatus);
  447. usb_clear_port_feature(dev, i + 1,
  448. USB_PORT_FEAT_C_ENABLE);
  449. /*
  450. * The following hack causes a ghost device problem
  451. * to Faraday EHCI
  452. */
  453. #ifndef CONFIG_USB_EHCI_FARADAY
  454. /* EM interference sometimes causes bad shielded USB
  455. * devices to be shutdown by the hub, this hack enables
  456. * them again. Works at least with mouse driver */
  457. if (!(portstatus & USB_PORT_STAT_ENABLE) &&
  458. (portstatus & USB_PORT_STAT_CONNECTION) &&
  459. usb_device_has_child_on_port(dev, i)) {
  460. debug("already running port %i " \
  461. "disabled by hub (EMI?), " \
  462. "re-enabling...\n", i + 1);
  463. usb_hub_port_connect_change(dev, i);
  464. }
  465. #endif
  466. }
  467. if (portstatus & USB_PORT_STAT_SUSPEND) {
  468. debug("port %d suspend change\n", i + 1);
  469. usb_clear_port_feature(dev, i + 1,
  470. USB_PORT_FEAT_SUSPEND);
  471. }
  472. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  473. debug("port %d over-current change\n", i + 1);
  474. usb_clear_port_feature(dev, i + 1,
  475. USB_PORT_FEAT_C_OVER_CURRENT);
  476. usb_hub_power_on(hub);
  477. }
  478. if (portchange & USB_PORT_STAT_C_RESET) {
  479. debug("port %d reset change\n", i + 1);
  480. usb_clear_port_feature(dev, i + 1,
  481. USB_PORT_FEAT_C_RESET);
  482. }
  483. } /* end for i all ports */
  484. return 0;
  485. }
  486. static int usb_hub_check(struct usb_device *dev, int ifnum)
  487. {
  488. struct usb_interface *iface;
  489. struct usb_endpoint_descriptor *ep = NULL;
  490. iface = &dev->config.if_desc[ifnum];
  491. /* Is it a hub? */
  492. if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
  493. goto err;
  494. /* Some hubs have a subclass of 1, which AFAICT according to the */
  495. /* specs is not defined, but it works */
  496. if ((iface->desc.bInterfaceSubClass != 0) &&
  497. (iface->desc.bInterfaceSubClass != 1))
  498. goto err;
  499. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  500. if (iface->desc.bNumEndpoints != 1)
  501. goto err;
  502. ep = &iface->ep_desc[0];
  503. /* Output endpoint? Curiousier and curiousier.. */
  504. if (!(ep->bEndpointAddress & USB_DIR_IN))
  505. goto err;
  506. /* If it's not an interrupt endpoint, we'd better punt! */
  507. if ((ep->bmAttributes & 3) != 3)
  508. goto err;
  509. /* We found a hub */
  510. debug("USB hub found\n");
  511. return 0;
  512. err:
  513. debug("USB hub not found: bInterfaceClass=%d, bInterfaceSubClass=%d, bNumEndpoints=%d\n",
  514. iface->desc.bInterfaceClass, iface->desc.bInterfaceSubClass,
  515. iface->desc.bNumEndpoints);
  516. if (ep) {
  517. debug(" bEndpointAddress=%#x, bmAttributes=%d",
  518. ep->bEndpointAddress, ep->bmAttributes);
  519. }
  520. return -ENOENT;
  521. }
  522. int usb_hub_probe(struct usb_device *dev, int ifnum)
  523. {
  524. int ret;
  525. ret = usb_hub_check(dev, ifnum);
  526. if (ret)
  527. return 0;
  528. ret = usb_hub_configure(dev);
  529. return ret;
  530. }
  531. #ifdef CONFIG_DM_USB
  532. int usb_hub_scan(struct udevice *hub)
  533. {
  534. struct usb_device *udev = dev_get_parent_priv(hub);
  535. return usb_hub_configure(udev);
  536. }
  537. static int usb_hub_post_bind(struct udevice *dev)
  538. {
  539. /* Scan the bus for devices */
  540. return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  541. }
  542. static int usb_hub_post_probe(struct udevice *dev)
  543. {
  544. debug("%s\n", __func__);
  545. return usb_hub_scan(dev);
  546. }
  547. static const struct udevice_id usb_hub_ids[] = {
  548. { .compatible = "usb-hub" },
  549. { }
  550. };
  551. U_BOOT_DRIVER(usb_generic_hub) = {
  552. .name = "usb_hub",
  553. .id = UCLASS_USB_HUB,
  554. .of_match = usb_hub_ids,
  555. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  556. };
  557. UCLASS_DRIVER(usb_hub) = {
  558. .id = UCLASS_USB_HUB,
  559. .name = "usb_hub",
  560. .post_bind = usb_hub_post_bind,
  561. .post_probe = usb_hub_post_probe,
  562. .child_pre_probe = usb_child_pre_probe,
  563. .per_child_auto_alloc_size = sizeof(struct usb_device),
  564. .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
  565. };
  566. static const struct usb_device_id hub_id_table[] = {
  567. {
  568. .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
  569. .bDeviceClass = USB_CLASS_HUB
  570. },
  571. { } /* Terminating entry */
  572. };
  573. U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table);
  574. #endif