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