usb_hub.c 15 KB

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