uhci-hub.c 11 KB

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