usb_hub.c 25 KB

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