usb_hub.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * Most of this source has been derived from the Linux USB
  3. * project:
  4. * (C) Copyright Linus Torvalds 1999
  5. * (C) Copyright Johannes Erdfelt 1999-2001
  6. * (C) Copyright Andreas Gal 1999
  7. * (C) Copyright Gregory P. Smith 1999
  8. * (C) Copyright Deti Fliegl 1999 (new USB architecture)
  9. * (C) Copyright Randy Dunlap 2000
  10. * (C) Copyright David Brownell 2000 (kernel hotplug, usb_device_id)
  11. * (C) Copyright Yggdrasil Computing, Inc. 2000
  12. * (usb_device_id matching changes by Adam J. Richter)
  13. *
  14. * Adapted for U-Boot:
  15. * (C) Copyright 2001 Denis Peter, MPL AG Switzerland
  16. *
  17. * SPDX-License-Identifier: GPL-2.0+
  18. */
  19. /****************************************************************************
  20. * HUB "Driver"
  21. * Probes device for being a hub and configurate it
  22. */
  23. #include <common.h>
  24. #include <command.h>
  25. #include <asm/processor.h>
  26. #include <asm/unaligned.h>
  27. #include <linux/ctype.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/unaligned.h>
  30. #include <usb.h>
  31. #ifdef CONFIG_4xx
  32. #include <asm/4xx_pci.h>
  33. #endif
  34. #define USB_BUFSIZ 512
  35. static struct usb_hub_device hub_dev[USB_MAX_HUB];
  36. static int usb_hub_index;
  37. __weak void usb_hub_reset_devices(int port)
  38. {
  39. return;
  40. }
  41. static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
  42. {
  43. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  44. USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
  45. USB_DT_HUB << 8, 0, data, size, USB_CNTL_TIMEOUT);
  46. }
  47. static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
  48. {
  49. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  50. USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature,
  51. port, NULL, 0, USB_CNTL_TIMEOUT);
  52. }
  53. static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
  54. {
  55. return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  56. USB_REQ_SET_FEATURE, USB_RT_PORT, feature,
  57. port, NULL, 0, USB_CNTL_TIMEOUT);
  58. }
  59. static int usb_get_hub_status(struct usb_device *dev, void *data)
  60. {
  61. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  62. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
  63. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  64. }
  65. static int usb_get_port_status(struct usb_device *dev, int port, void *data)
  66. {
  67. return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  68. USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
  69. data, sizeof(struct usb_hub_status), USB_CNTL_TIMEOUT);
  70. }
  71. static void usb_hub_power_on(struct usb_hub_device *hub)
  72. {
  73. int i;
  74. struct usb_device *dev;
  75. unsigned pgood_delay = hub->desc.bPwrOn2PwrGood * 2;
  76. const char *env;
  77. dev = hub->pusb_dev;
  78. debug("enabling power on all ports\n");
  79. for (i = 0; i < dev->maxchild; i++) {
  80. usb_set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
  81. debug("port %d returns %lX\n", i + 1, dev->status);
  82. }
  83. /*
  84. * Wait for power to become stable,
  85. * plus spec-defined max time for device to connect
  86. * but allow this time to be increased via env variable as some
  87. * devices break the spec and require longer warm-up times
  88. */
  89. env = getenv("usb_pgood_delay");
  90. if (env)
  91. pgood_delay = max(pgood_delay,
  92. (unsigned)simple_strtol(env, NULL, 0));
  93. debug("pgood_delay=%dms\n", pgood_delay);
  94. mdelay(pgood_delay + 1000);
  95. }
  96. void usb_hub_reset(void)
  97. {
  98. usb_hub_index = 0;
  99. }
  100. static struct usb_hub_device *usb_hub_allocate(void)
  101. {
  102. if (usb_hub_index < USB_MAX_HUB)
  103. return &hub_dev[usb_hub_index++];
  104. printf("ERROR: USB_MAX_HUB (%d) reached\n", USB_MAX_HUB);
  105. return NULL;
  106. }
  107. #define MAX_TRIES 5
  108. static inline char *portspeed(int portstatus)
  109. {
  110. char *speed_str;
  111. switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
  112. case USB_PORT_STAT_SUPER_SPEED:
  113. speed_str = "5 Gb/s";
  114. break;
  115. case USB_PORT_STAT_HIGH_SPEED:
  116. speed_str = "480 Mb/s";
  117. break;
  118. case USB_PORT_STAT_LOW_SPEED:
  119. speed_str = "1.5 Mb/s";
  120. break;
  121. default:
  122. speed_str = "12 Mb/s";
  123. break;
  124. }
  125. return speed_str;
  126. }
  127. int hub_port_reset(struct usb_device *dev, int port,
  128. unsigned short *portstat)
  129. {
  130. int tries;
  131. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  132. unsigned short portstatus, portchange;
  133. debug("hub_port_reset: resetting port %d...\n", port);
  134. for (tries = 0; tries < MAX_TRIES; tries++) {
  135. usb_set_port_feature(dev, port + 1, USB_PORT_FEAT_RESET);
  136. mdelay(200);
  137. if (usb_get_port_status(dev, port + 1, portsts) < 0) {
  138. debug("get_port_status failed status %lX\n",
  139. dev->status);
  140. return -1;
  141. }
  142. portstatus = le16_to_cpu(portsts->wPortStatus);
  143. portchange = le16_to_cpu(portsts->wPortChange);
  144. debug("portstatus %x, change %x, %s\n", portstatus, portchange,
  145. portspeed(portstatus));
  146. debug("STAT_C_CONNECTION = %d STAT_CONNECTION = %d" \
  147. " USB_PORT_STAT_ENABLE %d\n",
  148. (portchange & USB_PORT_STAT_C_CONNECTION) ? 1 : 0,
  149. (portstatus & USB_PORT_STAT_CONNECTION) ? 1 : 0,
  150. (portstatus & USB_PORT_STAT_ENABLE) ? 1 : 0);
  151. /*
  152. * Perhaps we should check for the following here:
  153. * - C_CONNECTION hasn't been set.
  154. * - CONNECTION is still set.
  155. *
  156. * Doing so would ensure that the device is still connected
  157. * to the bus, and hasn't been unplugged or replaced while the
  158. * USB bus reset was going on.
  159. *
  160. * However, if we do that, then (at least) a San Disk Ultra
  161. * USB 3.0 16GB device fails to reset on (at least) an NVIDIA
  162. * Tegra Jetson TK1 board. For some reason, the device appears
  163. * to briefly drop off the bus when this second bus reset is
  164. * executed, yet if we retry this loop, it'll eventually come
  165. * back after another reset or two.
  166. */
  167. if (portstatus & USB_PORT_STAT_ENABLE)
  168. break;
  169. mdelay(200);
  170. }
  171. if (tries == MAX_TRIES) {
  172. debug("Cannot enable port %i after %i retries, " \
  173. "disabling port.\n", port + 1, MAX_TRIES);
  174. debug("Maybe the USB cable is bad?\n");
  175. return -1;
  176. }
  177. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_RESET);
  178. *portstat = portstatus;
  179. return 0;
  180. }
  181. void usb_hub_port_connect_change(struct usb_device *dev, int port)
  182. {
  183. struct usb_device *usb;
  184. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  185. unsigned short portstatus;
  186. /* Check status */
  187. if (usb_get_port_status(dev, port + 1, portsts) < 0) {
  188. debug("get_port_status failed\n");
  189. return;
  190. }
  191. portstatus = le16_to_cpu(portsts->wPortStatus);
  192. debug("portstatus %x, change %x, %s\n",
  193. portstatus,
  194. le16_to_cpu(portsts->wPortChange),
  195. portspeed(portstatus));
  196. /* Clear the connection change status */
  197. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_C_CONNECTION);
  198. /* Disconnect any existing devices under this port */
  199. if (((!(portstatus & USB_PORT_STAT_CONNECTION)) &&
  200. (!(portstatus & USB_PORT_STAT_ENABLE))) || (dev->children[port])) {
  201. debug("usb_disconnect(&hub->children[port]);\n");
  202. /* Return now if nothing is connected */
  203. if (!(portstatus & USB_PORT_STAT_CONNECTION))
  204. return;
  205. }
  206. mdelay(200);
  207. /* Reset the port */
  208. if (hub_port_reset(dev, port, &portstatus) < 0) {
  209. printf("cannot reset port %i!?\n", port + 1);
  210. return;
  211. }
  212. mdelay(200);
  213. /* Allocate a new device struct for it */
  214. usb = usb_alloc_new_device(dev->controller);
  215. switch (portstatus & USB_PORT_STAT_SPEED_MASK) {
  216. case USB_PORT_STAT_SUPER_SPEED:
  217. usb->speed = USB_SPEED_SUPER;
  218. break;
  219. case USB_PORT_STAT_HIGH_SPEED:
  220. usb->speed = USB_SPEED_HIGH;
  221. break;
  222. case USB_PORT_STAT_LOW_SPEED:
  223. usb->speed = USB_SPEED_LOW;
  224. break;
  225. default:
  226. usb->speed = USB_SPEED_FULL;
  227. break;
  228. }
  229. dev->children[port] = usb;
  230. usb->parent = dev;
  231. usb->portnr = port + 1;
  232. /* Run it through the hoops (find a driver, etc) */
  233. if (usb_new_device(usb)) {
  234. /* Woops, disable the port */
  235. usb_free_device();
  236. dev->children[port] = NULL;
  237. debug("hub: disabling port %d\n", port + 1);
  238. usb_clear_port_feature(dev, port + 1, USB_PORT_FEAT_ENABLE);
  239. }
  240. }
  241. static int usb_hub_configure(struct usb_device *dev)
  242. {
  243. int i, length;
  244. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
  245. unsigned char *bitmap;
  246. short hubCharacteristics;
  247. struct usb_hub_descriptor *descriptor;
  248. struct usb_hub_device *hub;
  249. __maybe_unused struct usb_hub_status *hubsts;
  250. /* "allocate" Hub device */
  251. hub = usb_hub_allocate();
  252. if (hub == NULL)
  253. return -1;
  254. hub->pusb_dev = dev;
  255. /* Get the the hub descriptor */
  256. if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
  257. debug("usb_hub_configure: failed to get hub " \
  258. "descriptor, giving up %lX\n", dev->status);
  259. return -1;
  260. }
  261. descriptor = (struct usb_hub_descriptor *)buffer;
  262. length = min_t(int, descriptor->bLength,
  263. sizeof(struct usb_hub_descriptor));
  264. if (usb_get_hub_descriptor(dev, buffer, length) < 0) {
  265. debug("usb_hub_configure: failed to get hub " \
  266. "descriptor 2nd giving up %lX\n", dev->status);
  267. return -1;
  268. }
  269. memcpy((unsigned char *)&hub->desc, buffer, length);
  270. /* adjust 16bit values */
  271. put_unaligned(le16_to_cpu(get_unaligned(
  272. &descriptor->wHubCharacteristics)),
  273. &hub->desc.wHubCharacteristics);
  274. /* set the bitmap */
  275. bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
  276. /* devices not removable by default */
  277. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
  278. bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
  279. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
  280. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  281. hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
  282. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  283. hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
  284. dev->maxchild = descriptor->bNbrPorts;
  285. debug("%d ports detected\n", dev->maxchild);
  286. hubCharacteristics = get_unaligned(&hub->desc.wHubCharacteristics);
  287. switch (hubCharacteristics & HUB_CHAR_LPSM) {
  288. case 0x00:
  289. debug("ganged power switching\n");
  290. break;
  291. case 0x01:
  292. debug("individual port power switching\n");
  293. break;
  294. case 0x02:
  295. case 0x03:
  296. debug("unknown reserved power switching mode\n");
  297. break;
  298. }
  299. if (hubCharacteristics & HUB_CHAR_COMPOUND)
  300. debug("part of a compound device\n");
  301. else
  302. debug("standalone hub\n");
  303. switch (hubCharacteristics & HUB_CHAR_OCPM) {
  304. case 0x00:
  305. debug("global over-current protection\n");
  306. break;
  307. case 0x08:
  308. debug("individual port over-current protection\n");
  309. break;
  310. case 0x10:
  311. case 0x18:
  312. debug("no over-current protection\n");
  313. break;
  314. }
  315. debug("power on to power good time: %dms\n",
  316. descriptor->bPwrOn2PwrGood * 2);
  317. debug("hub controller current requirement: %dmA\n",
  318. descriptor->bHubContrCurrent);
  319. for (i = 0; i < dev->maxchild; i++)
  320. debug("port %d is%s removable\n", i + 1,
  321. hub->desc.DeviceRemovable[(i + 1) / 8] & \
  322. (1 << ((i + 1) % 8)) ? " not" : "");
  323. if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
  324. debug("usb_hub_configure: failed to get Status - " \
  325. "too long: %d\n", descriptor->bLength);
  326. return -1;
  327. }
  328. if (usb_get_hub_status(dev, buffer) < 0) {
  329. debug("usb_hub_configure: failed to get Status %lX\n",
  330. dev->status);
  331. return -1;
  332. }
  333. #ifdef DEBUG
  334. hubsts = (struct usb_hub_status *)buffer;
  335. #endif
  336. debug("get_hub_status returned status %X, change %X\n",
  337. le16_to_cpu(hubsts->wHubStatus),
  338. le16_to_cpu(hubsts->wHubChange));
  339. debug("local power source is %s\n",
  340. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
  341. "lost (inactive)" : "good");
  342. debug("%sover-current condition exists\n",
  343. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
  344. "" : "no ");
  345. usb_hub_power_on(hub);
  346. /*
  347. * Reset any devices that may be in a bad state when applying
  348. * the power. This is a __weak function. Resetting of the devices
  349. * should occur in the board file of the device.
  350. */
  351. for (i = 0; i < dev->maxchild; i++)
  352. usb_hub_reset_devices(i + 1);
  353. for (i = 0; i < dev->maxchild; i++) {
  354. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  355. unsigned short portstatus, portchange;
  356. int ret;
  357. ulong start = get_timer(0);
  358. /*
  359. * Wait for (whichever finishes first)
  360. * - A maximum of 10 seconds
  361. * This is a purely observational value driven by connecting
  362. * a few broken pen drives and taking the max * 1.5 approach
  363. * - connection_change and connection state to report same
  364. * state
  365. */
  366. do {
  367. ret = usb_get_port_status(dev, i + 1, portsts);
  368. if (ret < 0) {
  369. debug("get_port_status failed\n");
  370. break;
  371. }
  372. portstatus = le16_to_cpu(portsts->wPortStatus);
  373. portchange = le16_to_cpu(portsts->wPortChange);
  374. if ((portchange & USB_PORT_STAT_C_CONNECTION) ==
  375. (portstatus & USB_PORT_STAT_CONNECTION))
  376. break;
  377. } while (get_timer(start) < CONFIG_SYS_HZ * 10);
  378. if (ret < 0)
  379. continue;
  380. debug("Port %d Status %X Change %X\n",
  381. i + 1, portstatus, portchange);
  382. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  383. debug("port %d connection change\n", i + 1);
  384. usb_hub_port_connect_change(dev, i);
  385. }
  386. if (portchange & USB_PORT_STAT_C_ENABLE) {
  387. debug("port %d enable change, status %x\n",
  388. i + 1, portstatus);
  389. usb_clear_port_feature(dev, i + 1,
  390. USB_PORT_FEAT_C_ENABLE);
  391. /*
  392. * The following hack causes a ghost device problem
  393. * to Faraday EHCI
  394. */
  395. #ifndef CONFIG_USB_EHCI_FARADAY
  396. /* EM interference sometimes causes bad shielded USB
  397. * devices to be shutdown by the hub, this hack enables
  398. * them again. Works at least with mouse driver */
  399. if (!(portstatus & USB_PORT_STAT_ENABLE) &&
  400. (portstatus & USB_PORT_STAT_CONNECTION) &&
  401. ((dev->children[i]))) {
  402. debug("already running port %i " \
  403. "disabled by hub (EMI?), " \
  404. "re-enabling...\n", i + 1);
  405. usb_hub_port_connect_change(dev, i);
  406. }
  407. #endif
  408. }
  409. if (portstatus & USB_PORT_STAT_SUSPEND) {
  410. debug("port %d suspend change\n", i + 1);
  411. usb_clear_port_feature(dev, i + 1,
  412. USB_PORT_FEAT_SUSPEND);
  413. }
  414. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  415. debug("port %d over-current change\n", i + 1);
  416. usb_clear_port_feature(dev, i + 1,
  417. USB_PORT_FEAT_C_OVER_CURRENT);
  418. usb_hub_power_on(hub);
  419. }
  420. if (portchange & USB_PORT_STAT_C_RESET) {
  421. debug("port %d reset change\n", i + 1);
  422. usb_clear_port_feature(dev, i + 1,
  423. USB_PORT_FEAT_C_RESET);
  424. }
  425. } /* end for i all ports */
  426. return 0;
  427. }
  428. int usb_hub_probe(struct usb_device *dev, int ifnum)
  429. {
  430. struct usb_interface *iface;
  431. struct usb_endpoint_descriptor *ep;
  432. int ret;
  433. iface = &dev->config.if_desc[ifnum];
  434. /* Is it a hub? */
  435. if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
  436. return 0;
  437. /* Some hubs have a subclass of 1, which AFAICT according to the */
  438. /* specs is not defined, but it works */
  439. if ((iface->desc.bInterfaceSubClass != 0) &&
  440. (iface->desc.bInterfaceSubClass != 1))
  441. return 0;
  442. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  443. if (iface->desc.bNumEndpoints != 1)
  444. return 0;
  445. ep = &iface->ep_desc[0];
  446. /* Output endpoint? Curiousier and curiousier.. */
  447. if (!(ep->bEndpointAddress & USB_DIR_IN))
  448. return 0;
  449. /* If it's not an interrupt endpoint, we'd better punt! */
  450. if ((ep->bmAttributes & 3) != 3)
  451. return 0;
  452. /* We found a hub */
  453. debug("USB hub found\n");
  454. ret = usb_hub_configure(dev);
  455. return ret;
  456. }