usb_hub.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. #include <dm/root.h>
  38. DECLARE_GLOBAL_DATA_PTR;
  39. #include <usb.h>
  40. #ifdef CONFIG_4xx
  41. #include <asm/4xx_pci.h>
  42. #endif
  43. #define USB_BUFSIZ 512
  44. #define HUB_SHORT_RESET_TIME 20
  45. #define HUB_LONG_RESET_TIME 200
  46. #define PORT_OVERCURRENT_MAX_SCAN_COUNT 3
  47. struct usb_device_scan {
  48. struct usb_device *dev; /* USB hub device to scan */
  49. struct usb_hub_device *hub; /* USB hub struct */
  50. int port; /* USB port to scan */
  51. struct list_head list;
  52. };
  53. /* TODO(sjg@chromium.org): Remove this when CONFIG_DM_USB is defined */
  54. static struct usb_hub_device hub_dev[USB_MAX_HUB];
  55. static int usb_hub_index;
  56. static LIST_HEAD(usb_scan_list);
  57. __weak void usb_hub_reset_devices(int port)
  58. {
  59. return;
  60. }
  61. static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
  62. {
  63. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  64. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  65. USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
  66. }
  67. static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
  68. {
  69. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  70. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
  71. port, NULL, 0, USB_CNTL_TIMEOUT);
  72. }
  73. static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
  74. {
  75. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  76. USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
  77. port, NULL, 0, USB_CNTL_TIMEOUT);
  78. }
  79. static int usb_get_hub_status(struct usb_device *dev, void *data)
  80. {
  81. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  82. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
  83. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  84. }
  85. int usb_get_port_status(struct usb_device *dev, int port, void *data)
  86. {
  87. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  88. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
  89. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  90. }
  91. static void usb_hub_power_on(struct usb_hub_device *hub)
  92. {
  93. int i;
  94. struct usb_device *dev;
  95. unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
  96. const char *env;
  97. dev = hub->pusb_dev;
  98. debug("enabling power on all ports\n");
  99. for (i = 0; i < dev->maxchild; i++) {
  100. usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
  101. debug("port %d returns %lX\n", i + 1, dev->status);
  102. }
  103. #ifdef CONFIG_SANDBOX
  104. /*
  105. * Don't set timeout / delay values here. This results
  106. * in these values still being reset to 0.
  107. */
  108. if (state_get_skip_delays())
  109. return;
  110. #endif
  111. /*
  112. * Wait for power to become stable,
  113. * plus spec-defined max time for device to connect
  114. * but allow this time to be increased via env variable as some
  115. * devices break the spec and require longer warm-up times
  116. */
  117. env = getenv("usb_pgood_delay");
  118. if (env)
  119. pgood_delay = max(pgood_delay,
  120. (unsigned)simple_strtol(env, NULL, 0));
  121. debug("pgood_delay=%dms\n", pgood_delay);
  122. /*
  123. * Do a minimum delay of the larger value of 100ms or pgood_delay
  124. * so that the power can stablize before the devices are queried
  125. */
  126. hub->query_delay = get_timer(0) + max(100, (int)pgood_delay);
  127. /*
  128. * Record the power-on timeout here. The max. delay (timeout)
  129. * will be done based on this value in the USB port loop in
  130. * usb_hub_configure() later.
  131. */
  132. hub->connect_timeout = hub->query_delay + 1000;
  133. debug("devnum=%d poweron: query_delay=%d connect_timeout=%d\n",
  134. dev->devnum, max(100, (int)pgood_delay),
  135. max(100, (int)pgood_delay) + 1000);
  136. }
  137. void usb_hub_reset(void)
  138. {
  139. usb_hub_index = 0;
  140. /* Zero out global hub_dev in case its re-used again */
  141. memset(hub_dev, 0, sizeof(hub_dev));
  142. }
  143. static struct usb_hub_device *usb_hub_allocate(void)
  144. {
  145. if (usb_hub_index < USB_MAX_HUB)
  146. return &hub_dev[usb_hub_index++];
  147. printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
  148. return NULL;
  149. }
  150. #define MAX_TRIES 5
  151. static inline char *portspeed(int portstatus)
  152. {
  153. char *speed_str;
  154. switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
  155. case USB_PORT_STAT_SUPER_SPEED:
  156. speed_str = "5 Gb/s";
  157. break;
  158. case USB_PORT_STAT_HIGH_SPEED:
  159. speed_str = "480 Mb/s";
  160. break;
  161. case USB_PORT_STAT_LOW_SPEED:
  162. speed_str = "1.5 Mb/s";
  163. break;
  164. default:
  165. speed_str = "12 Mb/s";
  166. break;
  167. }
  168. return speed_str;
  169. }
  170. int legacy_hub_port_reset(struct usb_device *dev, int port,
  171. unsigned short *portstat)
  172. {
  173. int err, tries;
  174. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  175. unsigned short portstatus, portchange;
  176. int delay = HUB_SHORT_RESET_TIME; /* start with short reset delay */
  177. #ifdef CONFIG_DM_USB
  178. debug("%s: resetting '%s' port %d...\n", __func__, dev->dev->name,
  179. port + 1);
  180. #else
  181. debug("%s: resetting port %d...\n", __func__, port + 1);
  182. #endif
  183. for (tries = 0; tries < MAX_TRIES; tries++) {
  184. err = usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
  185. if (err < 0)
  186. return err;
  187. mdelay(delay);
  188. if (usb_get_port_status(dev, port + 1, portsts) < 0) {
  189. debug("get_port_status failed status %lX\n",
  190. dev->status);
  191. return -1;
  192. }
  193. portstatus = le16_to_cpu(portsts->wPortStatus);
  194. portchange = le16_to_cpu(portsts->wPortChange);
  195. debug("portstatus %x, change %x, %s\n", portstatus, portchange,
  196. portspeed(portstatus));
  197. debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
  198. " USB_PORT_STAT_ENABLE %d\n",
  199. (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
  200. (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
  201. (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
  202. /*
  203. * Perhaps we should check for the following here:
  204. * - C_CONNECTION hasn't been set.
  205. * - CONNECTION is still set.
  206. *
  207. * Doing so would ensure that the device is still connected
  208. * to the bus, and hasn't been unplugged or replaced while the
  209. * USB bus reset was going on.
  210. *
  211. * However, if we do that, then (at least) a San Disk Ultra
  212. * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
  213. * Tegra Jetson TK1 board. For some reason, the device appears
  214. * to briefly drop off the bus when this second bus reset is
  215. * executed, yet if we retry this loop, it'll eventually come
  216. * back after another reset or two.
  217. */
  218. if (portstatus & USB_PORT_STAT_ENABLE)
  219. break;
  220. /* Switch to long reset delay for the next round */
  221. delay = HUB_LONG_RESET_TIME;
  222. }
  223. if (tries == MAX_TRIES) {
  224. debug("Cannot enable port %i after %i retries, " \
  225. "disabling port.\n", port + 1, MAX_TRIES);
  226. debug("Maybe the USB cable is bad?\n");
  227. return -1;
  228. }
  229. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
  230. *portstat = portstatus;
  231. return 0;
  232. }
  233. #ifdef CONFIG_DM_USB
  234. int hub_port_reset(struct udevice *dev, int port, unsigned short *portstat)
  235. {
  236. struct usb_device *udev = dev_get_parent_priv(dev);
  237. return legacy_hub_port_reset(udev, port, portstat);
  238. }
  239. #endif
  240. int usb_hub_port_connect_change(struct usb_device *dev, int port)
  241. {
  242. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  243. unsigned short portstatus;
  244. int ret, speed;
  245. /* Check status */
  246. ret = usb_get_port_status(dev, port + 1, portsts);
  247. if (ret < 0) {
  248. debug("get_port_status failed\n");
  249. return ret;
  250. }
  251. portstatus = le16_to_cpu(portsts->wPortStatus);
  252. debug("portstatus %x, change %x, %s\n",
  253. portstatus,
  254. le16_to_cpu(portsts->wPortChange),
  255. portspeed(portstatus));
  256. /* Clear the connection change status */
  257. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
  258. /* Disconnect any existing devices under this port */
  259. if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
  260. (!(portstatus & USB_PORT_STAT_ENABLE))) ||
  261. usb_device_has_child_on_port(dev, port)) {
  262. debug("usb_disconnect(&hub->children[port]);\n");
  263. /* Return now if nothing is connected */
  264. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  265. return -ENOTCONN;
  266. }
  267. /* Reset the port */
  268. ret = legacy_hub_port_reset(dev, port, &portstatus);
  269. if (ret < 0) {
  270. if (ret != -ENXIO)
  271. printf("cannot reset port %i!?\n", port + 1);
  272. return ret;
  273. }
  274. switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
  275. case USB_PORT_STAT_SUPER_SPEED:
  276. speed = USB_SPEED_SUPER;
  277. break;
  278. case USB_PORT_STAT_HIGH_SPEED:
  279. speed = USB_SPEED_HIGH;
  280. break;
  281. case USB_PORT_STAT_LOW_SPEED:
  282. speed = USB_SPEED_LOW;
  283. break;
  284. default:
  285. speed = USB_SPEED_FULL;
  286. break;
  287. }
  288. #ifdef CONFIG_DM_USB
  289. struct udevice *child;
  290. ret = usb_scan_device(dev->dev, port + 1, speed, &child);
  291. #else
  292. struct usb_device *usb;
  293. ret = usb_alloc_new_device(dev->controller, &usb);
  294. if (ret) {
  295. printf("cannot create new device: ret=%d", ret);
  296. return ret;
  297. }
  298. dev->children[port] = usb;
  299. usb->speed = speed;
  300. usb->parent = dev;
  301. usb->portnr = port + 1;
  302. /* Run it through the hoops (find a driver, etc) */
  303. ret = usb_new_device(usb);
  304. if (ret < 0) {
  305. /* Woops, disable the port */
  306. usb_free_device(dev->controller);
  307. dev->children[port] = NULL;
  308. }
  309. #endif
  310. if (ret < 0) {
  311. debug("hub: disabling port %d\n", port + 1);
  312. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
  313. }
  314. return ret;
  315. }
  316. static int usb_scan_port(struct usb_device_scan *usb_scan)
  317. {
  318. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  319. unsigned short portstatus;
  320. unsigned short portchange;
  321. struct usb_device *dev;
  322. struct usb_hub_device *hub;
  323. int ret = 0;
  324. int i;
  325. dev = usb_scan->dev;
  326. hub = usb_scan->hub;
  327. i = usb_scan->port;
  328. /*
  329. * Don't talk to the device before the query delay is expired.
  330. * This is needed for voltages to stabalize.
  331. */
  332. if (get_timer(0) < hub->query_delay)
  333. return 0;
  334. ret = usb_get_port_status(dev, i + 1, portsts);
  335. if (ret < 0) {
  336. debug("get_port_status failed\n");
  337. if (get_timer(0) >= hub->connect_timeout) {
  338. debug("devnum=%d port=%d: timeout\n",
  339. dev->devnum, i + 1);
  340. /* Remove this device from scanning list */
  341. list_del(&usb_scan->list);
  342. free(usb_scan);
  343. return 0;
  344. }
  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_bind(struct udevice *dev)
  647. {
  648. /* Scan the bus for devices */
  649. return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
  650. }
  651. static int usb_hub_post_probe(struct udevice *dev)
  652. {
  653. debug("%s\n", __func__);
  654. return usb_hub_scan(dev);
  655. }
  656. static const struct udevice_id usb_hub_ids[] = {
  657. { .compatible = "usb-hub" },
  658. { }
  659. };
  660. U_BOOT_DRIVER(usb_generic_hub) = {
  661. .name = "usb_hub",
  662. .id = UCLASS_USB_HUB,
  663. .of_match = usb_hub_ids,
  664. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  665. };
  666. UCLASS_DRIVER(usb_hub) = {
  667. .id = UCLASS_USB_HUB,
  668. .name = "usb_hub",
  669. .post_bind = usb_hub_post_bind,
  670. .post_probe = usb_hub_post_probe,
  671. .child_pre_probe = usb_child_pre_probe,
  672. .per_child_auto_alloc_size = sizeof(struct usb_device),
  673. .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
  674. };
  675. static const struct usb_device_id hub_id_table[] = {
  676. {
  677. .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
  678. .bDeviceClass = USB_CLASS_HUB
  679. },
  680. { } /* Terminating entry */
  681. };
  682. U_BOOT_USB_DEVICE(usb_generic_hub, hub_id_table);
  683. #endif