usb_hub.c 15 KB

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