uhci-hub.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. /* A port that either is connected or has a changed-bit set will prevent
  32. * us from AUTO_STOPPING.
  33. */
  34. static int any_ports_active(struct uhci_hcd *uhci)
  35. {
  36. int port;
  37. for (port = 0; port < uhci->rh_numports; ++port) {
  38. if ((inw(uhci->io_addr + USBPORTSC1 + port * 2) &
  39. (USBPORTSC_CCS | RWC_BITS)) ||
  40. test_bit(port, &uhci->port_c_suspend))
  41. return 1;
  42. }
  43. return 0;
  44. }
  45. static inline int get_hub_status_data(struct uhci_hcd *uhci, char *buf)
  46. {
  47. int port;
  48. int mask = RWC_BITS;
  49. /* Some boards (both VIA and Intel apparently) report bogus
  50. * overcurrent indications, causing massive log spam unless
  51. * we completely ignore them. This doesn't seem to be a problem
  52. * with the chipset so much as with the way it is connected on
  53. * the motherboard; if the overcurrent input is left to float
  54. * then it may constantly register false positives. */
  55. if (ignore_oc)
  56. mask &= ~USBPORTSC_OCC;
  57. *buf = 0;
  58. for (port = 0; port < uhci->rh_numports; ++port) {
  59. if ((inw(uhci->io_addr + USBPORTSC1 + port * 2) & mask) ||
  60. test_bit(port, &uhci->port_c_suspend))
  61. *buf |= (1 << (port + 1));
  62. }
  63. return !!*buf;
  64. }
  65. #define OK(x) len = (x); break
  66. #define CLR_RH_PORTSTAT(x) \
  67. status = inw(port_addr); \
  68. status &= ~(RWC_BITS|WZ_BITS); \
  69. status &= ~(x); \
  70. status |= RWC_BITS & (x); \
  71. outw(status, port_addr)
  72. #define SET_RH_PORTSTAT(x) \
  73. status = inw(port_addr); \
  74. status |= (x); \
  75. status &= ~(RWC_BITS|WZ_BITS); \
  76. outw(status, port_addr)
  77. /* UHCI controllers don't automatically stop resume signalling after 20 msec,
  78. * so we have to poll and check timeouts in order to take care of it.
  79. */
  80. static void uhci_finish_suspend(struct uhci_hcd *uhci, int port,
  81. unsigned long port_addr)
  82. {
  83. int status;
  84. int i;
  85. if (inw(port_addr) & (USBPORTSC_SUSP | USBPORTSC_RD)) {
  86. CLR_RH_PORTSTAT(USBPORTSC_SUSP | USBPORTSC_RD);
  87. if (test_bit(port, &uhci->resuming_ports))
  88. set_bit(port, &uhci->port_c_suspend);
  89. /* The controller won't actually turn off the RD bit until
  90. * it has had a chance to send a low-speed EOP sequence,
  91. * which is supposed to take 3 bit times (= 2 microseconds).
  92. * Experiments show that some controllers take longer, so
  93. * we'll poll for completion. */
  94. for (i = 0; i < 10; ++i) {
  95. if (!(inw(port_addr) & USBPORTSC_RD))
  96. break;
  97. udelay(1);
  98. }
  99. }
  100. clear_bit(port, &uhci->resuming_ports);
  101. }
  102. /* Wait for the UHCI controller in HP's iLO2 server management chip.
  103. * It can take up to 250 us to finish a reset and set the CSC bit.
  104. */
  105. static void wait_for_HP(unsigned long port_addr)
  106. {
  107. int i;
  108. for (i = 10; i < 250; i += 10) {
  109. if (inw(port_addr) & USBPORTSC_CSC)
  110. return;
  111. udelay(10);
  112. }
  113. /* Log a warning? */
  114. }
  115. static void uhci_check_ports(struct uhci_hcd *uhci)
  116. {
  117. unsigned int port;
  118. unsigned long port_addr;
  119. int status;
  120. for (port = 0; port < uhci->rh_numports; ++port) {
  121. port_addr = uhci->io_addr + USBPORTSC1 + 2 * port;
  122. status = inw(port_addr);
  123. if (unlikely(status & USBPORTSC_PR)) {
  124. if (time_after_eq(jiffies, uhci->ports_timeout)) {
  125. CLR_RH_PORTSTAT(USBPORTSC_PR);
  126. udelay(10);
  127. /* HP's server management chip requires
  128. * a longer delay. */
  129. if (to_pci_dev(uhci_dev(uhci))->vendor ==
  130. PCI_VENDOR_ID_HP)
  131. wait_for_HP(port_addr);
  132. /* If the port was enabled before, turning
  133. * reset on caused a port enable change.
  134. * Turning reset off causes a port connect
  135. * status change. Clear these changes. */
  136. CLR_RH_PORTSTAT(USBPORTSC_CSC | USBPORTSC_PEC);
  137. SET_RH_PORTSTAT(USBPORTSC_PE);
  138. }
  139. }
  140. if (unlikely(status & USBPORTSC_RD)) {
  141. if (!test_bit(port, &uhci->resuming_ports)) {
  142. /* Port received a wakeup request */
  143. set_bit(port, &uhci->resuming_ports);
  144. uhci->ports_timeout = jiffies +
  145. msecs_to_jiffies(20);
  146. /* Make sure we see the port again
  147. * after the resuming period is over. */
  148. mod_timer(&uhci_to_hcd(uhci)->rh_timer,
  149. uhci->ports_timeout);
  150. } else if (time_after_eq(jiffies,
  151. uhci->ports_timeout)) {
  152. uhci_finish_suspend(uhci, port, port_addr);
  153. }
  154. }
  155. }
  156. }
  157. static int uhci_hub_status_data(struct usb_hcd *hcd, char *buf)
  158. {
  159. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  160. unsigned long flags;
  161. int status = 0;
  162. spin_lock_irqsave(&uhci->lock, flags);
  163. uhci_scan_schedule(uhci);
  164. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) || uhci->dead)
  165. goto done;
  166. uhci_check_ports(uhci);
  167. status = get_hub_status_data(uhci, buf);
  168. switch (uhci->rh_state) {
  169. case UHCI_RH_SUSPENDING:
  170. case UHCI_RH_SUSPENDED:
  171. /* if port change, ask to be resumed */
  172. if (status)
  173. usb_hcd_resume_root_hub(hcd);
  174. break;
  175. case UHCI_RH_AUTO_STOPPED:
  176. /* if port change, auto start */
  177. if (status)
  178. wakeup_rh(uhci);
  179. break;
  180. case UHCI_RH_RUNNING:
  181. /* are any devices attached? */
  182. if (!any_ports_active(uhci)) {
  183. uhci->rh_state = UHCI_RH_RUNNING_NODEVS;
  184. uhci->auto_stop_time = jiffies + HZ;
  185. }
  186. break;
  187. case UHCI_RH_RUNNING_NODEVS:
  188. /* auto-stop if nothing connected for 1 second */
  189. if (any_ports_active(uhci))
  190. uhci->rh_state = UHCI_RH_RUNNING;
  191. else if (time_after_eq(jiffies, uhci->auto_stop_time))
  192. suspend_rh(uhci, UHCI_RH_AUTO_STOPPED);
  193. break;
  194. default:
  195. break;
  196. }
  197. done:
  198. spin_unlock_irqrestore(&uhci->lock, flags);
  199. return status;
  200. }
  201. /* size of returned buffer is part of USB spec */
  202. static int uhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  203. u16 wIndex, char *buf, u16 wLength)
  204. {
  205. struct uhci_hcd *uhci = hcd_to_uhci(hcd);
  206. int status, lstatus, retval = 0, len = 0;
  207. unsigned int port = wIndex - 1;
  208. unsigned long port_addr = uhci->io_addr + USBPORTSC1 + 2 * port;
  209. u16 wPortChange, wPortStatus;
  210. unsigned long flags;
  211. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) || uhci->dead)
  212. return -ETIMEDOUT;
  213. spin_lock_irqsave(&uhci->lock, flags);
  214. switch (typeReq) {
  215. case GetHubStatus:
  216. *(__le32 *)buf = cpu_to_le32(0);
  217. OK(4); /* hub power */
  218. case GetPortStatus:
  219. if (port >= uhci->rh_numports)
  220. goto err;
  221. uhci_check_ports(uhci);
  222. status = inw(port_addr);
  223. /* Intel controllers report the OverCurrent bit active on.
  224. * VIA controllers report it active off, so we'll adjust the
  225. * bit value. (It's not standardized in the UHCI spec.)
  226. */
  227. if (to_pci_dev(hcd->self.controller)->vendor ==
  228. PCI_VENDOR_ID_VIA)
  229. status ^= USBPORTSC_OC;
  230. /* UHCI doesn't support C_RESET (always false) */
  231. wPortChange = lstatus = 0;
  232. if (status & USBPORTSC_CSC)
  233. wPortChange |= USB_PORT_STAT_C_CONNECTION;
  234. if (status & USBPORTSC_PEC)
  235. wPortChange |= USB_PORT_STAT_C_ENABLE;
  236. if ((status & USBPORTSC_OCC) && !ignore_oc)
  237. wPortChange |= USB_PORT_STAT_C_OVERCURRENT;
  238. if (test_bit(port, &uhci->port_c_suspend)) {
  239. wPortChange |= USB_PORT_STAT_C_SUSPEND;
  240. lstatus |= 1;
  241. }
  242. if (test_bit(port, &uhci->resuming_ports))
  243. lstatus |= 4;
  244. /* UHCI has no power switching (always on) */
  245. wPortStatus = USB_PORT_STAT_POWER;
  246. if (status & USBPORTSC_CCS)
  247. wPortStatus |= USB_PORT_STAT_CONNECTION;
  248. if (status & USBPORTSC_PE) {
  249. wPortStatus |= USB_PORT_STAT_ENABLE;
  250. if (status & (USBPORTSC_SUSP | USBPORTSC_RD))
  251. wPortStatus |= USB_PORT_STAT_SUSPEND;
  252. }
  253. if (status & USBPORTSC_OC)
  254. wPortStatus |= USB_PORT_STAT_OVERCURRENT;
  255. if (status & USBPORTSC_PR)
  256. wPortStatus |= USB_PORT_STAT_RESET;
  257. if (status & USBPORTSC_LSDA)
  258. wPortStatus |= USB_PORT_STAT_LOW_SPEED;
  259. if (wPortChange)
  260. dev_dbg(uhci_dev(uhci), "port %d portsc %04x,%02x\n",
  261. wIndex, status, lstatus);
  262. *(__le16 *)buf = cpu_to_le16(wPortStatus);
  263. *(__le16 *)(buf + 2) = cpu_to_le16(wPortChange);
  264. OK(4);
  265. case SetHubFeature: /* We don't implement these */
  266. case ClearHubFeature:
  267. switch (wValue) {
  268. case C_HUB_OVER_CURRENT:
  269. case C_HUB_LOCAL_POWER:
  270. OK(0);
  271. default:
  272. goto err;
  273. }
  274. break;
  275. case SetPortFeature:
  276. if (port >= uhci->rh_numports)
  277. goto err;
  278. switch (wValue) {
  279. case USB_PORT_FEAT_SUSPEND:
  280. SET_RH_PORTSTAT(USBPORTSC_SUSP);
  281. OK(0);
  282. case USB_PORT_FEAT_RESET:
  283. SET_RH_PORTSTAT(USBPORTSC_PR);
  284. /* Reset terminates Resume signalling */
  285. uhci_finish_suspend(uhci, port, port_addr);
  286. /* USB v2.0 7.1.7.5 */
  287. uhci->ports_timeout = jiffies + msecs_to_jiffies(50);
  288. OK(0);
  289. case USB_PORT_FEAT_POWER:
  290. /* UHCI has no power switching */
  291. OK(0);
  292. default:
  293. goto err;
  294. }
  295. break;
  296. case ClearPortFeature:
  297. if (port >= uhci->rh_numports)
  298. goto err;
  299. switch (wValue) {
  300. case USB_PORT_FEAT_ENABLE:
  301. CLR_RH_PORTSTAT(USBPORTSC_PE);
  302. /* Disable terminates Resume signalling */
  303. uhci_finish_suspend(uhci, port, port_addr);
  304. OK(0);
  305. case USB_PORT_FEAT_C_ENABLE:
  306. CLR_RH_PORTSTAT(USBPORTSC_PEC);
  307. OK(0);
  308. case USB_PORT_FEAT_SUSPEND:
  309. if (!(inw(port_addr) & USBPORTSC_SUSP)) {
  310. /* Make certain the port isn't suspended */
  311. uhci_finish_suspend(uhci, port, port_addr);
  312. } else if (!test_and_set_bit(port,
  313. &uhci->resuming_ports)) {
  314. SET_RH_PORTSTAT(USBPORTSC_RD);
  315. /* The controller won't allow RD to be set
  316. * if the port is disabled. When this happens
  317. * just skip the Resume signalling.
  318. */
  319. if (!(inw(port_addr) & USBPORTSC_RD))
  320. uhci_finish_suspend(uhci, port,
  321. port_addr);
  322. else
  323. /* USB v2.0 7.1.7.7 */
  324. uhci->ports_timeout = jiffies +
  325. msecs_to_jiffies(20);
  326. }
  327. OK(0);
  328. case USB_PORT_FEAT_C_SUSPEND:
  329. clear_bit(port, &uhci->port_c_suspend);
  330. OK(0);
  331. case USB_PORT_FEAT_POWER:
  332. /* UHCI has no power switching */
  333. goto err;
  334. case USB_PORT_FEAT_C_CONNECTION:
  335. CLR_RH_PORTSTAT(USBPORTSC_CSC);
  336. OK(0);
  337. case USB_PORT_FEAT_C_OVER_CURRENT:
  338. CLR_RH_PORTSTAT(USBPORTSC_OCC);
  339. OK(0);
  340. case USB_PORT_FEAT_C_RESET:
  341. /* this driver won't report these */
  342. OK(0);
  343. default:
  344. goto err;
  345. }
  346. break;
  347. case GetHubDescriptor:
  348. len = min_t(unsigned int, sizeof(root_hub_hub_des), wLength);
  349. memcpy(buf, root_hub_hub_des, len);
  350. if (len > 2)
  351. buf[2] = uhci->rh_numports;
  352. OK(len);
  353. default:
  354. err:
  355. retval = -EPIPE;
  356. }
  357. spin_unlock_irqrestore(&uhci->lock, flags);
  358. return retval;
  359. }