usb_hub.c 21 KB

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