usb_hub.c 26 KB

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