usb_hub.c 14 KB

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