usb_hub.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  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. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  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. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, USB_BUFSIZ);
  225. unsigned char *bitmap;
  226. struct usb_hub_descriptor *descriptor;
  227. struct usb_hub_device *hub;
  228. #ifdef USB_HUB_DEBUG
  229. struct usb_hub_status *hubsts;
  230. #endif
  231. /* "allocate" Hub device */
  232. hub = usb_hub_allocate();
  233. if (hub == NULL)
  234. return -1;
  235. hub->pusb_dev = dev;
  236. /* Get the the hub descriptor */
  237. if (usb_get_hub_descriptor(dev, buffer, 4) < 0) {
  238. USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
  239. "descriptor, giving up %lX\n", dev->status);
  240. return -1;
  241. }
  242. descriptor = (struct usb_hub_descriptor *)buffer;
  243. /* silence compiler warning if USB_BUFSIZ is > 256 [= sizeof(char)] */
  244. i = descriptor->bLength;
  245. if (i > USB_BUFSIZ) {
  246. USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
  247. "descriptor - too long: %d\n",
  248. descriptor->bLength);
  249. return -1;
  250. }
  251. if (usb_get_hub_descriptor(dev, buffer, descriptor->bLength) < 0) {
  252. USB_HUB_PRINTF("usb_hub_configure: failed to get hub " \
  253. "descriptor 2nd giving up %lX\n", dev->status);
  254. return -1;
  255. }
  256. memcpy((unsigned char *)&hub->desc, buffer, descriptor->bLength);
  257. /* adjust 16bit values */
  258. hub->desc.wHubCharacteristics =
  259. le16_to_cpu(descriptor->wHubCharacteristics);
  260. /* set the bitmap */
  261. bitmap = (unsigned char *)&hub->desc.DeviceRemovable[0];
  262. /* devices not removable by default */
  263. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8);
  264. bitmap = (unsigned char *)&hub->desc.PortPowerCtrlMask[0];
  265. memset(bitmap, 0xff, (USB_MAXCHILDREN+1+7)/8); /* PowerMask = 1B */
  266. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  267. hub->desc.DeviceRemovable[i] = descriptor->DeviceRemovable[i];
  268. for (i = 0; i < ((hub->desc.bNbrPorts + 1 + 7)/8); i++)
  269. hub->desc.PortPowerCtrlMask[i] = descriptor->PortPowerCtrlMask[i];
  270. dev->maxchild = descriptor->bNbrPorts;
  271. USB_HUB_PRINTF("%d ports detected\n", dev->maxchild);
  272. switch (hub->desc.wHubCharacteristics & HUB_CHAR_LPSM) {
  273. case 0x00:
  274. USB_HUB_PRINTF("ganged power switching\n");
  275. break;
  276. case 0x01:
  277. USB_HUB_PRINTF("individual port power switching\n");
  278. break;
  279. case 0x02:
  280. case 0x03:
  281. USB_HUB_PRINTF("unknown reserved power switching mode\n");
  282. break;
  283. }
  284. if (hub->desc.wHubCharacteristics & HUB_CHAR_COMPOUND)
  285. USB_HUB_PRINTF("part of a compound device\n");
  286. else
  287. USB_HUB_PRINTF("standalone hub\n");
  288. switch (hub->desc.wHubCharacteristics & HUB_CHAR_OCPM) {
  289. case 0x00:
  290. USB_HUB_PRINTF("global over-current protection\n");
  291. break;
  292. case 0x08:
  293. USB_HUB_PRINTF("individual port over-current protection\n");
  294. break;
  295. case 0x10:
  296. case 0x18:
  297. USB_HUB_PRINTF("no over-current protection\n");
  298. break;
  299. }
  300. USB_HUB_PRINTF("power on to power good time: %dms\n",
  301. descriptor->bPwrOn2PwrGood * 2);
  302. USB_HUB_PRINTF("hub controller current requirement: %dmA\n",
  303. descriptor->bHubContrCurrent);
  304. for (i = 0; i < dev->maxchild; i++)
  305. USB_HUB_PRINTF("port %d is%s removable\n", i + 1,
  306. hub->desc.DeviceRemovable[(i + 1) / 8] & \
  307. (1 << ((i + 1) % 8)) ? " not" : "");
  308. if (sizeof(struct usb_hub_status) > USB_BUFSIZ) {
  309. USB_HUB_PRINTF("usb_hub_configure: failed to get Status - " \
  310. "too long: %d\n", descriptor->bLength);
  311. return -1;
  312. }
  313. if (usb_get_hub_status(dev, buffer) < 0) {
  314. USB_HUB_PRINTF("usb_hub_configure: failed to get Status %lX\n",
  315. dev->status);
  316. return -1;
  317. }
  318. #ifdef USB_HUB_DEBUG
  319. hubsts = (struct usb_hub_status *)buffer;
  320. #endif
  321. USB_HUB_PRINTF("get_hub_status returned status %X, change %X\n",
  322. le16_to_cpu(hubsts->wHubStatus),
  323. le16_to_cpu(hubsts->wHubChange));
  324. USB_HUB_PRINTF("local power source is %s\n",
  325. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_LOCAL_POWER) ? \
  326. "lost (inactive)" : "good");
  327. USB_HUB_PRINTF("%sover-current condition exists\n",
  328. (le16_to_cpu(hubsts->wHubStatus) & HUB_STATUS_OVERCURRENT) ? \
  329. "" : "no ");
  330. usb_hub_power_on(hub);
  331. for (i = 0; i < dev->maxchild; i++) {
  332. ALLOC_CACHE_ALIGN_BUFFER(struct usb_port_status, portsts, 1);
  333. unsigned short portstatus, portchange;
  334. if (usb_get_port_status(dev, i + 1, portsts) < 0) {
  335. USB_HUB_PRINTF("get_port_status failed\n");
  336. continue;
  337. }
  338. portstatus = le16_to_cpu(portsts->wPortStatus);
  339. portchange = le16_to_cpu(portsts->wPortChange);
  340. USB_HUB_PRINTF("Port %d Status %X Change %X\n",
  341. i + 1, portstatus, portchange);
  342. if (portchange & USB_PORT_STAT_C_CONNECTION) {
  343. USB_HUB_PRINTF("port %d connection change\n", i + 1);
  344. usb_hub_port_connect_change(dev, i);
  345. }
  346. if (portchange & USB_PORT_STAT_C_ENABLE) {
  347. USB_HUB_PRINTF("port %d enable change, status %x\n",
  348. i + 1, portstatus);
  349. usb_clear_port_feature(dev, i + 1,
  350. USB_PORT_FEAT_C_ENABLE);
  351. /* EM interference sometimes causes bad shielded USB
  352. * devices to be shutdown by the hub, this hack enables
  353. * them again. Works at least with mouse driver */
  354. if (!(portstatus & USB_PORT_STAT_ENABLE) &&
  355. (portstatus & USB_PORT_STAT_CONNECTION) &&
  356. ((dev->children[i]))) {
  357. USB_HUB_PRINTF("already running port %i " \
  358. "disabled by hub (EMI?), " \
  359. "re-enabling...\n", i + 1);
  360. usb_hub_port_connect_change(dev, i);
  361. }
  362. }
  363. if (portstatus & USB_PORT_STAT_SUSPEND) {
  364. USB_HUB_PRINTF("port %d suspend change\n", i + 1);
  365. usb_clear_port_feature(dev, i + 1,
  366. USB_PORT_FEAT_SUSPEND);
  367. }
  368. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  369. USB_HUB_PRINTF("port %d over-current change\n", i + 1);
  370. usb_clear_port_feature(dev, i + 1,
  371. USB_PORT_FEAT_C_OVER_CURRENT);
  372. usb_hub_power_on(hub);
  373. }
  374. if (portchange & USB_PORT_STAT_C_RESET) {
  375. USB_HUB_PRINTF("port %d reset change\n", i + 1);
  376. usb_clear_port_feature(dev, i + 1,
  377. USB_PORT_FEAT_C_RESET);
  378. }
  379. } /* end for i all ports */
  380. return 0;
  381. }
  382. int usb_hub_probe(struct usb_device *dev, int ifnum)
  383. {
  384. struct usb_interface *iface;
  385. struct usb_endpoint_descriptor *ep;
  386. int ret;
  387. iface = &dev->config.if_desc[ifnum];
  388. /* Is it a hub? */
  389. if (iface->desc.bInterfaceClass != USB_CLASS_HUB)
  390. return 0;
  391. /* Some hubs have a subclass of 1, which AFAICT according to the */
  392. /* specs is not defined, but it works */
  393. if ((iface->desc.bInterfaceSubClass != 0) &&
  394. (iface->desc.bInterfaceSubClass != 1))
  395. return 0;
  396. /* Multiple endpoints? What kind of mutant ninja-hub is this? */
  397. if (iface->desc.bNumEndpoints != 1)
  398. return 0;
  399. ep = &iface->ep_desc[0];
  400. /* Output endpoint? Curiousier and curiousier.. */
  401. if (!(ep->bEndpointAddress & USB_DIR_IN))
  402. return 0;
  403. /* If it's not an interrupt endpoint, we'd better punt! */
  404. if ((ep->bmAttributes & 3) != 3)
  405. return 0;
  406. /* We found a hub */
  407. USB_HUB_PRINTF("USB hub found\n");
  408. ret = usb_hub_configure(dev);
  409. return ret;
  410. }