usb_hub.c 18 KB

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