usb_hub.c 26 KB

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