usb_hub.c 15 KB

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