usb_hub.c 21 KB

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