usb_hub.c 26 KB

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