usb_hub.c 16 KB

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