usb_hub.c 14 KB

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