uhci-hub.c 11 KB

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