blob: 9189bc984c98ce9804d51ec94c4aebe4147b8f3f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Ming Leibef46652008-06-08 16:44:40 +080015static const __u8 root_hub_hub_des[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070016{
17 0x09, /* __u8 bLength; */
18 0x29, /* __u8 bDescriptorType; Hub-descriptor */
19 0x02, /* __u8 bNbrPorts; */
20 0x0a, /* __u16 wHubCharacteristics; */
21 0x00, /* (per-port OC, no power switching) */
22 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
23 0x00, /* __u8 bHubContrCurrent; 0 mA */
Chen Gang3171fca2013-01-24 09:41:45 +080024 0x00, /* __u8 DeviceRemovable; *** 7 Ports max */
25 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
28#define UHCI_RH_MAXCHILD 7
29
30/* must write as zeroes */
31#define WZ_BITS (USBPORTSC_RES2 | USBPORTSC_RES3 | USBPORTSC_RES4)
32
33/* status change bits: nonzero writes will clear */
34#define RWC_BITS (USBPORTSC_OCC | USBPORTSC_PEC | USBPORTSC_CSC)
35
Alan Stern88018152007-02-26 17:16:06 -050036/* suspend/resume bits: port suspended or port resuming */
37#define SUSPEND_BITS (USBPORTSC_SUSP | USBPORTSC_RD)
38
Alan Sternf5946f82005-04-09 17:26:00 -040039/* A port that either is connected or has a changed-bit set will prevent
40 * us from AUTO_STOPPING.
41 */
42static int any_ports_active(struct uhci_hcd *uhci)
43{
44 int port;
45
46 for (port = 0; port < uhci->rh_numports; ++port) {
Jan Andersson9faa0912011-05-06 12:00:16 +020047 if ((uhci_readw(uhci, USBPORTSC1 + port * 2) &
Alan Sternf5946f82005-04-09 17:26:00 -040048 (USBPORTSC_CCS | RWC_BITS)) ||
49 test_bit(port, &uhci->port_c_suspend))
50 return 1;
51 }
52 return 0;
53}
54
Alan Stern6c1b4452005-04-21 16:04:58 -040055static inline int get_hub_status_data(struct uhci_hcd *uhci, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int port;
Alan Stern5f8364b2006-12-05 16:29:55 -050058 int mask = RWC_BITS;
59
60 /* Some boards (both VIA and Intel apparently) report bogus
61 * overcurrent indications, causing massive log spam unless
62 * we completely ignore them. This doesn't seem to be a problem
63 * with the chipset so much as with the way it is connected on
64 * the motherboard; if the overcurrent input is left to float
65 * then it may constantly register false positives. */
66 if (ignore_oc)
67 mask &= ~USBPORTSC_OCC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 *buf = 0;
70 for (port = 0; port < uhci->rh_numports; ++port) {
Jan Andersson9faa0912011-05-06 12:00:16 +020071 if ((uhci_readw(uhci, USBPORTSC1 + port * 2) & mask) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 test_bit(port, &uhci->port_c_suspend))
73 *buf |= (1 << (port + 1));
74 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return !!*buf;
76}
77
78#define OK(x) len = (x); break
79
80#define CLR_RH_PORTSTAT(x) \
Jan Andersson9faa0912011-05-06 12:00:16 +020081 status = uhci_readw(uhci, port_addr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 status &= ~(RWC_BITS|WZ_BITS); \
83 status &= ~(x); \
84 status |= RWC_BITS & (x); \
Jan Andersson9faa0912011-05-06 12:00:16 +020085 uhci_writew(uhci, status, port_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87#define SET_RH_PORTSTAT(x) \
Jan Andersson9faa0912011-05-06 12:00:16 +020088 status = uhci_readw(uhci, port_addr); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 status |= (x); \
90 status &= ~(RWC_BITS|WZ_BITS); \
Jan Andersson9faa0912011-05-06 12:00:16 +020091 uhci_writew(uhci, status, port_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93/* UHCI controllers don't automatically stop resume signalling after 20 msec,
94 * so we have to poll and check timeouts in order to take care of it.
95 */
96static void uhci_finish_suspend(struct uhci_hcd *uhci, int port,
97 unsigned long port_addr)
98{
99 int status;
Alan Sternde06a3b2006-08-11 11:33:58 -0400100 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Jan Andersson9faa0912011-05-06 12:00:16 +0200102 if (uhci_readw(uhci, port_addr) & SUSPEND_BITS) {
Alan Stern88018152007-02-26 17:16:06 -0500103 CLR_RH_PORTSTAT(SUSPEND_BITS);
Alan Stern8e326402006-04-04 14:47:44 -0400104 if (test_bit(port, &uhci->resuming_ports))
105 set_bit(port, &uhci->port_c_suspend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /* The controller won't actually turn off the RD bit until
108 * it has had a chance to send a low-speed EOP sequence,
Alan Sternde06a3b2006-08-11 11:33:58 -0400109 * which is supposed to take 3 bit times (= 2 microseconds).
110 * Experiments show that some controllers take longer, so
111 * we'll poll for completion. */
112 for (i = 0; i < 10; ++i) {
Jan Andersson9faa0912011-05-06 12:00:16 +0200113 if (!(uhci_readw(uhci, port_addr) & SUSPEND_BITS))
Alan Sternde06a3b2006-08-11 11:33:58 -0400114 break;
115 udelay(1);
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Alan Stern8e326402006-04-04 14:47:44 -0400118 clear_bit(port, &uhci->resuming_ports);
Alan Stern840008b2013-01-25 17:09:55 -0500119 usb_hcd_end_port_resume(&uhci_to_hcd(uhci)->self, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Alan Sternae557172006-02-28 10:16:12 -0500122/* Wait for the UHCI controller in HP's iLO2 server management chip.
123 * It can take up to 250 us to finish a reset and set the CSC bit.
124 */
Jan Andersson9faa0912011-05-06 12:00:16 +0200125static void wait_for_HP(struct uhci_hcd *uhci, unsigned long port_addr)
Alan Sternae557172006-02-28 10:16:12 -0500126{
127 int i;
128
129 for (i = 10; i < 250; i += 10) {
Jan Andersson9faa0912011-05-06 12:00:16 +0200130 if (uhci_readw(uhci, port_addr) & USBPORTSC_CSC)
Alan Sternae557172006-02-28 10:16:12 -0500131 return;
132 udelay(10);
133 }
134 /* Log a warning? */
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static void uhci_check_ports(struct uhci_hcd *uhci)
138{
139 unsigned int port;
140 unsigned long port_addr;
141 int status;
142
143 for (port = 0; port < uhci->rh_numports; ++port) {
Jan Andersson9faa0912011-05-06 12:00:16 +0200144 port_addr = USBPORTSC1 + 2 * port;
145 status = uhci_readw(uhci, port_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (unlikely(status & USBPORTSC_PR)) {
147 if (time_after_eq(jiffies, uhci->ports_timeout)) {
148 CLR_RH_PORTSTAT(USBPORTSC_PR);
149 udelay(10);
150
Alan Sternae557172006-02-28 10:16:12 -0500151 /* HP's server management chip requires
152 * a longer delay. */
Jan Anderssondfeca7a2011-05-06 12:00:12 +0200153 if (uhci->wait_for_hp)
Jan Andersson9faa0912011-05-06 12:00:16 +0200154 wait_for_HP(uhci, port_addr);
Alan Sternae557172006-02-28 10:16:12 -0500155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 /* If the port was enabled before, turning
157 * reset on caused a port enable change.
158 * Turning reset off causes a port connect
159 * status change. Clear these changes. */
160 CLR_RH_PORTSTAT(USBPORTSC_CSC | USBPORTSC_PEC);
161 SET_RH_PORTSTAT(USBPORTSC_PE);
162 }
163 }
164 if (unlikely(status & USBPORTSC_RD)) {
165 if (!test_bit(port, &uhci->resuming_ports)) {
166
167 /* Port received a wakeup request */
168 set_bit(port, &uhci->resuming_ports);
169 uhci->ports_timeout = jiffies +
Alan Stern49d0f072010-01-08 11:18:38 -0500170 msecs_to_jiffies(25);
Alan Stern840008b2013-01-25 17:09:55 -0500171 usb_hcd_start_port_resume(
172 &uhci_to_hcd(uhci)->self, port);
Alan Stern6c1b4452005-04-21 16:04:58 -0400173
174 /* Make sure we see the port again
175 * after the resuming period is over. */
176 mod_timer(&uhci_to_hcd(uhci)->rh_timer,
177 uhci->ports_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 } else if (time_after_eq(jiffies,
179 uhci->ports_timeout)) {
180 uhci_finish_suspend(uhci, port, port_addr);
181 }
182 }
183 }
184}
185
Alan Stern6c1b4452005-04-21 16:04:58 -0400186static int uhci_hub_status_data(struct usb_hcd *hcd, char *buf)
187{
188 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
189 unsigned long flags;
Alan Stern1f09df82005-09-05 13:59:51 -0400190 int status = 0;
Alan Stern6c1b4452005-04-21 16:04:58 -0400191
192 spin_lock_irqsave(&uhci->lock, flags);
Alan Stern6c1b4452005-04-21 16:04:58 -0400193
David Howells7d12e782006-10-05 14:55:46 +0100194 uhci_scan_schedule(uhci);
Alan Stern541c7d42010-06-22 16:39:10 -0400195 if (!HCD_HW_ACCESSIBLE(hcd) || uhci->dead)
Alan Stern1f09df82005-09-05 13:59:51 -0400196 goto done;
Alan Stern6c1b4452005-04-21 16:04:58 -0400197 uhci_check_ports(uhci);
Alan Stern1f09df82005-09-05 13:59:51 -0400198
Alan Stern6c1b4452005-04-21 16:04:58 -0400199 status = get_hub_status_data(uhci, buf);
200
201 switch (uhci->rh_state) {
Alan Stern6c1b4452005-04-21 16:04:58 -0400202 case UHCI_RH_SUSPENDED:
203 /* if port change, ask to be resumed */
Alan Sternb446b962012-04-03 15:24:43 -0400204 if (status || uhci->resuming_ports) {
205 status = 1;
Alan Stern6c1b4452005-04-21 16:04:58 -0400206 usb_hcd_resume_root_hub(hcd);
Alan Sternb446b962012-04-03 15:24:43 -0400207 }
Alan Stern6c1b4452005-04-21 16:04:58 -0400208 break;
209
210 case UHCI_RH_AUTO_STOPPED:
211 /* if port change, auto start */
212 if (status)
213 wakeup_rh(uhci);
214 break;
215
216 case UHCI_RH_RUNNING:
217 /* are any devices attached? */
218 if (!any_ports_active(uhci)) {
219 uhci->rh_state = UHCI_RH_RUNNING_NODEVS;
220 uhci->auto_stop_time = jiffies + HZ;
221 }
222 break;
223
224 case UHCI_RH_RUNNING_NODEVS:
225 /* auto-stop if nothing connected for 1 second */
226 if (any_ports_active(uhci))
227 uhci->rh_state = UHCI_RH_RUNNING;
Alan Stern997ff892013-05-14 13:55:29 -0400228 else if (time_after_eq(jiffies, uhci->auto_stop_time) &&
229 !uhci->wait_for_hp)
Alan Stern6c1b4452005-04-21 16:04:58 -0400230 suspend_rh(uhci, UHCI_RH_AUTO_STOPPED);
231 break;
232
233 default:
234 break;
235 }
236
237done:
238 spin_unlock_irqrestore(&uhci->lock, flags);
239 return status;
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/* size of returned buffer is part of USB spec */
243static int uhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
244 u16 wIndex, char *buf, u16 wLength)
245{
246 struct uhci_hcd *uhci = hcd_to_uhci(hcd);
247 int status, lstatus, retval = 0, len = 0;
248 unsigned int port = wIndex - 1;
Jan Andersson9faa0912011-05-06 12:00:16 +0200249 unsigned long port_addr = USBPORTSC1 + 2 * port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 u16 wPortChange, wPortStatus;
251 unsigned long flags;
252
Alan Stern541c7d42010-06-22 16:39:10 -0400253 if (!HCD_HW_ACCESSIBLE(hcd) || uhci->dead)
Alan Sterna8bed8b2005-04-09 17:29:00 -0400254 return -ETIMEDOUT;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 spin_lock_irqsave(&uhci->lock, flags);
257 switch (typeReq) {
258
259 case GetHubStatus:
260 *(__le32 *)buf = cpu_to_le32(0);
261 OK(4); /* hub power */
262 case GetPortStatus:
263 if (port >= uhci->rh_numports)
264 goto err;
265
266 uhci_check_ports(uhci);
Jan Andersson9faa0912011-05-06 12:00:16 +0200267 status = uhci_readw(uhci, port_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* Intel controllers report the OverCurrent bit active on.
270 * VIA controllers report it active off, so we'll adjust the
271 * bit value. (It's not standardized in the UHCI spec.)
272 */
Jan Anderssondfeca7a2011-05-06 12:00:12 +0200273 if (uhci->oc_low)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 status ^= USBPORTSC_OC;
275
276 /* UHCI doesn't support C_RESET (always false) */
277 wPortChange = lstatus = 0;
278 if (status & USBPORTSC_CSC)
279 wPortChange |= USB_PORT_STAT_C_CONNECTION;
280 if (status & USBPORTSC_PEC)
281 wPortChange |= USB_PORT_STAT_C_ENABLE;
Alan Stern5f8364b2006-12-05 16:29:55 -0500282 if ((status & USBPORTSC_OCC) && !ignore_oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 wPortChange |= USB_PORT_STAT_C_OVERCURRENT;
284
285 if (test_bit(port, &uhci->port_c_suspend)) {
286 wPortChange |= USB_PORT_STAT_C_SUSPEND;
287 lstatus |= 1;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (test_bit(port, &uhci->resuming_ports))
290 lstatus |= 4;
291
292 /* UHCI has no power switching (always on) */
293 wPortStatus = USB_PORT_STAT_POWER;
294 if (status & USBPORTSC_CCS)
295 wPortStatus |= USB_PORT_STAT_CONNECTION;
296 if (status & USBPORTSC_PE) {
297 wPortStatus |= USB_PORT_STAT_ENABLE;
Alan Stern88018152007-02-26 17:16:06 -0500298 if (status & SUSPEND_BITS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 wPortStatus |= USB_PORT_STAT_SUSPEND;
300 }
301 if (status & USBPORTSC_OC)
302 wPortStatus |= USB_PORT_STAT_OVERCURRENT;
303 if (status & USBPORTSC_PR)
304 wPortStatus |= USB_PORT_STAT_RESET;
305 if (status & USBPORTSC_LSDA)
306 wPortStatus |= USB_PORT_STAT_LOW_SPEED;
307
308 if (wPortChange)
309 dev_dbg(uhci_dev(uhci), "port %d portsc %04x,%02x\n",
310 wIndex, status, lstatus);
311
312 *(__le16 *)buf = cpu_to_le16(wPortStatus);
313 *(__le16 *)(buf + 2) = cpu_to_le16(wPortChange);
314 OK(4);
315 case SetHubFeature: /* We don't implement these */
316 case ClearHubFeature:
317 switch (wValue) {
318 case C_HUB_OVER_CURRENT:
319 case C_HUB_LOCAL_POWER:
320 OK(0);
321 default:
322 goto err;
323 }
324 break;
325 case SetPortFeature:
326 if (port >= uhci->rh_numports)
327 goto err;
328
329 switch (wValue) {
330 case USB_PORT_FEAT_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 SET_RH_PORTSTAT(USBPORTSC_SUSP);
332 OK(0);
333 case USB_PORT_FEAT_RESET:
334 SET_RH_PORTSTAT(USBPORTSC_PR);
335
336 /* Reset terminates Resume signalling */
337 uhci_finish_suspend(uhci, port, port_addr);
338
339 /* USB v2.0 7.1.7.5 */
340 uhci->ports_timeout = jiffies + msecs_to_jiffies(50);
341 OK(0);
342 case USB_PORT_FEAT_POWER:
343 /* UHCI has no power switching */
344 OK(0);
345 default:
346 goto err;
347 }
348 break;
349 case ClearPortFeature:
350 if (port >= uhci->rh_numports)
351 goto err;
352
353 switch (wValue) {
354 case USB_PORT_FEAT_ENABLE:
355 CLR_RH_PORTSTAT(USBPORTSC_PE);
356
357 /* Disable terminates Resume signalling */
358 uhci_finish_suspend(uhci, port, port_addr);
359 OK(0);
360 case USB_PORT_FEAT_C_ENABLE:
361 CLR_RH_PORTSTAT(USBPORTSC_PEC);
362 OK(0);
363 case USB_PORT_FEAT_SUSPEND:
Jan Andersson9faa0912011-05-06 12:00:16 +0200364 if (!(uhci_readw(uhci, port_addr) & USBPORTSC_SUSP)) {
Alan Stern8e326402006-04-04 14:47:44 -0400365
366 /* Make certain the port isn't suspended */
367 uhci_finish_suspend(uhci, port, port_addr);
368 } else if (!test_and_set_bit(port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 &uhci->resuming_ports)) {
370 SET_RH_PORTSTAT(USBPORTSC_RD);
371
372 /* The controller won't allow RD to be set
373 * if the port is disabled. When this happens
374 * just skip the Resume signalling.
375 */
Jan Andersson9faa0912011-05-06 12:00:16 +0200376 if (!(uhci_readw(uhci, port_addr) &
377 USBPORTSC_RD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 uhci_finish_suspend(uhci, port,
379 port_addr);
380 else
381 /* USB v2.0 7.1.7.7 */
382 uhci->ports_timeout = jiffies +
383 msecs_to_jiffies(20);
384 }
385 OK(0);
386 case USB_PORT_FEAT_C_SUSPEND:
387 clear_bit(port, &uhci->port_c_suspend);
388 OK(0);
389 case USB_PORT_FEAT_POWER:
390 /* UHCI has no power switching */
391 goto err;
392 case USB_PORT_FEAT_C_CONNECTION:
393 CLR_RH_PORTSTAT(USBPORTSC_CSC);
394 OK(0);
395 case USB_PORT_FEAT_C_OVER_CURRENT:
396 CLR_RH_PORTSTAT(USBPORTSC_OCC);
397 OK(0);
398 case USB_PORT_FEAT_C_RESET:
399 /* this driver won't report these */
400 OK(0);
401 default:
402 goto err;
403 }
404 break;
405 case GetHubDescriptor:
406 len = min_t(unsigned int, sizeof(root_hub_hub_des), wLength);
407 memcpy(buf, root_hub_hub_des, len);
408 if (len > 2)
409 buf[2] = uhci->rh_numports;
410 OK(len);
411 default:
412err:
413 retval = -EPIPE;
414 }
415 spin_unlock_irqrestore(&uhci->lock, flags);
416
417 return retval;
418}