uhci-hub.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Universal Host Controller Interface driver for USB.
  3. *
  4. * Maintainer: Alan Stern <stern@rowland.harvard.edu>
  5. *
  6. * (C) Copyright 1999 Linus Torvalds
  7. * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
  8. * (C) Copyright 1999 Randy Dunlap
  9. * (C) Copyright 1999 Georg Acher, acher@in.tum.de
  10. * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
  11. * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
  12. * (C) Copyright 2004 Alan Stern, stern@rowland.harvard.edu
  13. */
  14. static __u8 root_hub_hub_des[] =
  15. {
  16. 0x09, /* __u8 bLength; */
  17. 0x29, /* __u8 bDescriptorType; Hub-descriptor */
  18. 0x02, /* __u8 bNbrPorts; */
  19. 0x0a, /* __u16 wHubCharacteristics; */
  20. 0x00, /* (per-port OC, no power switching) */
  21. 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
  22. 0x00, /* __u8 bHubContrCurrent; 0 mA */
  23. 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
  24. 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
  25. };
  26. #define UHCI_RH_MAXCHILD 7
  27. /* must write as zeroes */
  28. #define WZ_BITS (USBPORTSC_RES2 | USBPORTSC_RES3 | USBPORTSC_RES4)
  29. /* status change bits: nonzero writes will clear */
  30. #define RWC_BITS (USBPORTSC_OCC | USBPORTSC_PEC | USBPORTSC_CSC)
  31. static int uhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  32. {
  33. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  34. int port;
  35. *buf = 0;
  36. for (port = 0; port < uhci->rh_numports; ++port) {
  37. if ((inw(uhci->io_addr + USBPORTSC1 + port * 2) & RWC_BITS) ||
  38. test_bit(port, &uhci->port_c_suspend))
  39. *buf |= (1 << (port + 1));
  40. }
  41. if (*buf && uhci->state == UHCI_SUSPENDED)
  42. uhci->resume_detect = 1;
  43. return !!*buf;
  44. }
  45. #define OK(x) len = (x); break
  46. #define CLR_RH_PORTSTAT(x) \
  47. status = inw(port_addr); \
  48. status &= ~(RWC_BITS|WZ_BITS); \
  49. status &= ~(x); \
  50. status |= RWC_BITS & (x); \
  51. outw(status, port_addr)
  52. #define SET_RH_PORTSTAT(x) \
  53. status = inw(port_addr); \
  54. status |= (x); \
  55. status &= ~(RWC_BITS|WZ_BITS); \
  56. outw(status, port_addr)
  57. /* UHCI controllers don't automatically stop resume signalling after 20 msec,
  58. * so we have to poll and check timeouts in order to take care of it.
  59. */
  60. static void uhci_finish_suspend(struct uhci_hcd *uhci, int port,
  61. unsigned long port_addr)
  62. {
  63. int status;
  64. if (test_bit(port, &uhci->suspended_ports)) {
  65. CLR_RH_PORTSTAT(USBPORTSC_SUSP | USBPORTSC_RD);
  66. clear_bit(port, &uhci->suspended_ports);
  67. clear_bit(port, &uhci->resuming_ports);
  68. set_bit(port, &uhci->port_c_suspend);
  69. /* The controller won't actually turn off the RD bit until
  70. * it has had a chance to send a low-speed EOP sequence,
  71. * which takes 3 bit times (= 2 microseconds). We'll delay
  72. * slightly longer for good luck. */
  73. udelay(4);
  74. }
  75. }
  76. static void uhci_check_ports(struct uhci_hcd *uhci)
  77. {
  78. unsigned int port;
  79. unsigned long port_addr;
  80. int status;
  81. for (port = 0; port < uhci->rh_numports; ++port) {
  82. port_addr = uhci->io_addr + USBPORTSC1 + 2 * port;
  83. status = inw(port_addr);
  84. if (unlikely(status & USBPORTSC_PR)) {
  85. if (time_after_eq(jiffies, uhci->ports_timeout)) {
  86. CLR_RH_PORTSTAT(USBPORTSC_PR);
  87. udelay(10);
  88. /* If the port was enabled before, turning
  89. * reset on caused a port enable change.
  90. * Turning reset off causes a port connect
  91. * status change. Clear these changes. */
  92. CLR_RH_PORTSTAT(USBPORTSC_CSC | USBPORTSC_PEC);
  93. SET_RH_PORTSTAT(USBPORTSC_PE);
  94. }
  95. }
  96. if (unlikely(status & USBPORTSC_RD)) {
  97. if (!test_bit(port, &uhci->resuming_ports)) {
  98. /* Port received a wakeup request */
  99. set_bit(port, &uhci->resuming_ports);
  100. uhci->ports_timeout = jiffies +
  101. msecs_to_jiffies(20);
  102. } else if (time_after_eq(jiffies,
  103. uhci->ports_timeout)) {
  104. uhci_finish_suspend(uhci, port, port_addr);
  105. }
  106. }
  107. }
  108. }
  109. /* size of returned buffer is part of USB spec */
  110. static int uhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  111. u16 wIndex, char *buf, u16 wLength)
  112. {
  113. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  114. int status, lstatus, retval = 0, len = 0;
  115. unsigned int port = wIndex - 1;
  116. unsigned long port_addr = uhci->io_addr + USBPORTSC1 + 2 * port;
  117. u16 wPortChange, wPortStatus;
  118. unsigned long flags;
  119. spin_lock_irqsave(&uhci->lock, flags);
  120. switch (typeReq) {
  121. case GetHubStatus:
  122. *(__le32 *)buf = cpu_to_le32(0);
  123. OK(4); /* hub power */
  124. case GetPortStatus:
  125. if (port >= uhci->rh_numports)
  126. goto err;
  127. uhci_check_ports(uhci);
  128. status = inw(port_addr);
  129. /* Intel controllers report the OverCurrent bit active on.
  130. * VIA controllers report it active off, so we'll adjust the
  131. * bit value. (It's not standardized in the UHCI spec.)
  132. */
  133. if (to_pci_dev(hcd->self.controller)->vendor ==
  134. PCI_VENDOR_ID_VIA)
  135. status ^= USBPORTSC_OC;
  136. /* UHCI doesn't support C_RESET (always false) */
  137. wPortChange = lstatus = 0;
  138. if (status & USBPORTSC_CSC)
  139. wPortChange |= USB_PORT_STAT_C_CONNECTION;
  140. if (status & USBPORTSC_PEC)
  141. wPortChange |= USB_PORT_STAT_C_ENABLE;
  142. if (status & USBPORTSC_OCC)
  143. wPortChange |= USB_PORT_STAT_C_OVERCURRENT;
  144. if (test_bit(port, &uhci->port_c_suspend)) {
  145. wPortChange |= USB_PORT_STAT_C_SUSPEND;
  146. lstatus |= 1;
  147. }
  148. if (test_bit(port, &uhci->suspended_ports))
  149. lstatus |= 2;
  150. if (test_bit(port, &uhci->resuming_ports))
  151. lstatus |= 4;
  152. /* UHCI has no power switching (always on) */
  153. wPortStatus = USB_PORT_STAT_POWER;
  154. if (status & USBPORTSC_CCS)
  155. wPortStatus |= USB_PORT_STAT_CONNECTION;
  156. if (status & USBPORTSC_PE) {
  157. wPortStatus |= USB_PORT_STAT_ENABLE;
  158. if (status & (USBPORTSC_SUSP | USBPORTSC_RD))
  159. wPortStatus |= USB_PORT_STAT_SUSPEND;
  160. }
  161. if (status & USBPORTSC_OC)
  162. wPortStatus |= USB_PORT_STAT_OVERCURRENT;
  163. if (status & USBPORTSC_PR)
  164. wPortStatus |= USB_PORT_STAT_RESET;
  165. if (status & USBPORTSC_LSDA)
  166. wPortStatus |= USB_PORT_STAT_LOW_SPEED;
  167. if (wPortChange)
  168. dev_dbg(uhci_dev(uhci), "port %d portsc %04x,%02x\n",
  169. wIndex, status, lstatus);
  170. *(__le16 *)buf = cpu_to_le16(wPortStatus);
  171. *(__le16 *)(buf + 2) = cpu_to_le16(wPortChange);
  172. OK(4);
  173. case SetHubFeature: /* We don't implement these */
  174. case ClearHubFeature:
  175. switch (wValue) {
  176. case C_HUB_OVER_CURRENT:
  177. case C_HUB_LOCAL_POWER:
  178. OK(0);
  179. default:
  180. goto err;
  181. }
  182. break;
  183. case SetPortFeature:
  184. if (port >= uhci->rh_numports)
  185. goto err;
  186. switch (wValue) {
  187. case USB_PORT_FEAT_SUSPEND:
  188. set_bit(port, &uhci->suspended_ports);
  189. SET_RH_PORTSTAT(USBPORTSC_SUSP);
  190. OK(0);
  191. case USB_PORT_FEAT_RESET:
  192. SET_RH_PORTSTAT(USBPORTSC_PR);
  193. /* Reset terminates Resume signalling */
  194. uhci_finish_suspend(uhci, port, port_addr);
  195. /* USB v2.0 7.1.7.5 */
  196. uhci->ports_timeout = jiffies + msecs_to_jiffies(50);
  197. OK(0);
  198. case USB_PORT_FEAT_POWER:
  199. /* UHCI has no power switching */
  200. OK(0);
  201. default:
  202. goto err;
  203. }
  204. break;
  205. case ClearPortFeature:
  206. if (port >= uhci->rh_numports)
  207. goto err;
  208. switch (wValue) {
  209. case USB_PORT_FEAT_ENABLE:
  210. CLR_RH_PORTSTAT(USBPORTSC_PE);
  211. /* Disable terminates Resume signalling */
  212. uhci_finish_suspend(uhci, port, port_addr);
  213. OK(0);
  214. case USB_PORT_FEAT_C_ENABLE:
  215. CLR_RH_PORTSTAT(USBPORTSC_PEC);
  216. OK(0);
  217. case USB_PORT_FEAT_SUSPEND:
  218. if (test_bit(port, &uhci->suspended_ports) &&
  219. !test_and_set_bit(port,
  220. &uhci->resuming_ports)) {
  221. SET_RH_PORTSTAT(USBPORTSC_RD);
  222. /* The controller won't allow RD to be set
  223. * if the port is disabled. When this happens
  224. * just skip the Resume signalling.
  225. */
  226. if (!(inw(port_addr) & USBPORTSC_RD))
  227. uhci_finish_suspend(uhci, port,
  228. port_addr);
  229. else
  230. /* USB v2.0 7.1.7.7 */
  231. uhci->ports_timeout = jiffies +
  232. msecs_to_jiffies(20);
  233. }
  234. OK(0);
  235. case USB_PORT_FEAT_C_SUSPEND:
  236. clear_bit(port, &uhci->port_c_suspend);
  237. OK(0);
  238. case USB_PORT_FEAT_POWER:
  239. /* UHCI has no power switching */
  240. goto err;
  241. case USB_PORT_FEAT_C_CONNECTION:
  242. CLR_RH_PORTSTAT(USBPORTSC_CSC);
  243. OK(0);
  244. case USB_PORT_FEAT_C_OVER_CURRENT:
  245. CLR_RH_PORTSTAT(USBPORTSC_OCC);
  246. OK(0);
  247. case USB_PORT_FEAT_C_RESET:
  248. /* this driver won't report these */
  249. OK(0);
  250. default:
  251. goto err;
  252. }
  253. break;
  254. case GetHubDescriptor:
  255. len = min_t(unsigned int, sizeof(root_hub_hub_des), wLength);
  256. memcpy(buf, root_hub_hub_des, len);
  257. if (len > 2)
  258. buf[2] = uhci->rh_numports;
  259. OK(len);
  260. default:
  261. err:
  262. retval = -EPIPE;
  263. }
  264. spin_unlock_irqrestore(&uhci->lock, flags);
  265. return retval;
  266. }