blob: 3cfba69e0767be172305f49f2181458896c85f69 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Brownelld49d4312005-05-07 13:21:50 -07002 * Copyright (C) 2001-2004 by David Brownell
David Brownell53bd6a62006-08-30 14:50:06 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/* this file is part of ehci-hcd.c */
20
21/*-------------------------------------------------------------------------*/
22
23/*
24 * EHCI Root Hub ... the nonsharable stuff
25 *
26 * Registers don't need cpu_to_le32, that happens transparently
27 */
28
29/*-------------------------------------------------------------------------*/
30
31#ifdef CONFIG_PM
32
Alan Stern0c0382e2005-10-13 17:08:02 -040033static int ehci_bus_suspend (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
36 int port;
Alan Stern8c033562006-11-09 14:42:16 -050037 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 if (time_before (jiffies, ehci->next_statechange))
40 msleep(5);
41
42 port = HCS_N_PORTS (ehci->hcs_params);
43 spin_lock_irq (&ehci->lock);
44
45 /* stop schedules, clean any completed work */
46 if (HC_IS_RUNNING(hcd->state)) {
47 ehci_quiesce (ehci);
48 hcd->state = HC_STATE_QUIESCING;
49 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +110050 ehci->command = ehci_readl(ehci, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 if (ehci->reclaim)
Greg Kroah-Hartman64f89792006-10-17 13:57:18 -070052 ehci->reclaim_ready = 1;
David Howells7d12e782006-10-05 14:55:46 +010053 ehci_work(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Alan Stern8c033562006-11-09 14:42:16 -050055 /* Unlike other USB host controller types, EHCI doesn't have
56 * any notion of "global" or bus-wide suspend. The driver has
57 * to manually suspend all the active unsuspended ports, and
58 * then manually resume them in the bus_resume() routine.
59 */
60 ehci->bus_suspended = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 while (port--) {
62 u32 __iomem *reg = &ehci->regs->port_status [port];
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +110063 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 u32 t2 = t1;
65
Alan Stern8c033562006-11-09 14:42:16 -050066 /* keep track of which ports we suspend */
67 if ((t1 & PORT_PE) && !(t1 & PORT_OWNER) &&
68 !(t1 & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 t2 |= PORT_SUSPEND;
Alan Stern8c033562006-11-09 14:42:16 -050070 set_bit(port, &ehci->bus_suspended);
71 }
72
73 /* enable remote wakeup on all ports */
David Brownell2c1c3c42005-11-07 15:24:46 -080074 if (device_may_wakeup(&hcd->self.root_hub->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
76 else
77 t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
78
79 if (t1 != t2) {
80 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
81 port + 1, t1, t2);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +110082 ehci_writel(ehci, t2, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84 }
85
86 /* turn off now-idle HC */
David Brownell4756ae52005-05-09 17:23:51 -070087 del_timer_sync (&ehci->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 ehci_halt (ehci);
89 hcd->state = HC_STATE_SUSPENDED;
90
Alan Stern8c033562006-11-09 14:42:16 -050091 /* allow remote wakeup */
92 mask = INTR_MASK;
93 if (!device_may_wakeup(&hcd->self.root_hub->dev))
94 mask &= ~STS_PCD;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +110095 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
96 ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -050097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
99 spin_unlock_irq (&ehci->lock);
100 return 0;
101}
102
103
104/* caller has locked the root hub, and should reset/reinit on error */
Alan Stern0c0382e2005-10-13 17:08:02 -0400105static int ehci_bus_resume (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
108 u32 temp;
109 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 if (time_before (jiffies, ehci->next_statechange))
112 msleep(5);
113 spin_lock_irq (&ehci->lock);
114
David Brownellf03c17f2005-11-23 15:45:28 -0800115 /* Ideally and we've got a real resume here, and no port's power
116 * was lost. (For PCI, that means Vaux was maintained.) But we
117 * could instead be restoring a swsusp snapshot -- so that BIOS was
118 * the last user of the controller, not reset/pm hardware keeping
119 * state we gave to it.
120 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100121 temp = ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500122 ehci_dbg(ehci, "resume root hub%s\n", temp ? "" : " after power loss");
David Brownellf03c17f2005-11-23 15:45:28 -0800123
Alan Stern8c033562006-11-09 14:42:16 -0500124 /* at least some APM implementations will try to deliver
125 * IRQs right away, so delay them until we're ready.
126 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100127 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500128
129 /* re-init operational registers */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100130 ehci_writel(ehci, 0, &ehci->regs->segment);
131 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
132 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 /* restore CMD_RUN, framelist size, and irq threshold */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100135 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Alan Stern8c033562006-11-09 14:42:16 -0500137 /* manually resume the ports we suspended during bus_suspend() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 i = HCS_N_PORTS (ehci->hcs_params);
139 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100140 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
David Brownell10f65242005-08-31 10:55:38 -0700141 temp &= ~(PORT_RWC_BITS
142 | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
Alan Stern8c033562006-11-09 14:42:16 -0500143 if (test_bit(i, &ehci->bus_suspended) &&
144 (temp & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
146 temp |= PORT_RESUME;
147 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100148 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150 i = HCS_N_PORTS (ehci->hcs_params);
151 mdelay (20);
152 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100153 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500154 if (test_bit(i, &ehci->bus_suspended) &&
155 (temp & PORT_SUSPEND)) {
156 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100157 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500158 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100161 (void) ehci_readl(ehci, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 /* maybe re-activate the schedule(s) */
164 temp = 0;
165 if (ehci->async->qh_next.qh)
166 temp |= CMD_ASE;
167 if (ehci->periodic_sched)
168 temp |= CMD_PSE;
169 if (temp) {
170 ehci->command |= temp;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100171 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
174 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
175 hcd->state = HC_STATE_RUNNING;
176
177 /* Now we can safely re-enable irqs */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100178 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 spin_unlock_irq (&ehci->lock);
181 return 0;
182}
183
184#else
185
Alan Stern0c0382e2005-10-13 17:08:02 -0400186#define ehci_bus_suspend NULL
187#define ehci_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189#endif /* CONFIG_PM */
190
191/*-------------------------------------------------------------------------*/
192
Alan Stern57e06c12007-01-16 11:59:45 -0500193/* Display the ports dedicated to the companion controller */
194static ssize_t show_companion(struct class_device *class_dev, char *buf)
195{
196 struct ehci_hcd *ehci;
197 int nports, index, n;
198 int count = PAGE_SIZE;
199 char *ptr = buf;
200
201 ehci = hcd_to_ehci(bus_to_hcd(class_get_devdata(class_dev)));
202 nports = HCS_N_PORTS(ehci->hcs_params);
203
204 for (index = 0; index < nports; ++index) {
205 if (test_bit(index, &ehci->companion_ports)) {
206 n = scnprintf(ptr, count, "%d\n", index + 1);
207 ptr += n;
208 count -= n;
209 }
210 }
211 return ptr - buf;
212}
213
214/*
215 * Dedicate or undedicate a port to the companion controller.
216 * Syntax is "[-]portnum", where a leading '-' sign means
217 * return control of the port to the EHCI controller.
218 */
219static ssize_t store_companion(struct class_device *class_dev,
220 const char *buf, size_t count)
221{
222 struct ehci_hcd *ehci;
223 int portnum, new_owner, try;
224 u32 __iomem *status_reg;
225 u32 port_status;
226
227 ehci = hcd_to_ehci(bus_to_hcd(class_get_devdata(class_dev)));
228 new_owner = PORT_OWNER; /* Owned by companion */
229 if (sscanf(buf, "%d", &portnum) != 1)
230 return -EINVAL;
231 if (portnum < 0) {
232 portnum = - portnum;
233 new_owner = 0; /* Owned by EHCI */
234 }
235 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
236 return -ENOENT;
237 status_reg = &ehci->regs->port_status[--portnum];
238 if (new_owner)
239 set_bit(portnum, &ehci->companion_ports);
240 else
241 clear_bit(portnum, &ehci->companion_ports);
242
243 /*
244 * The controller won't set the OWNER bit if the port is
245 * enabled, so this loop will sometimes require at least two
246 * iterations: one to disable the port and one to set OWNER.
247 */
248
249 for (try = 4; try > 0; --try) {
250 spin_lock_irq(&ehci->lock);
251 port_status = ehci_readl(ehci, status_reg);
252 if ((port_status & PORT_OWNER) == new_owner
253 || (port_status & (PORT_OWNER | PORT_CONNECT))
254 == 0)
255 try = 0;
256 else {
257 port_status ^= PORT_OWNER;
258 port_status &= ~(PORT_PE | PORT_RWC_BITS);
259 ehci_writel(ehci, port_status, status_reg);
260 }
261 spin_unlock_irq(&ehci->lock);
262 if (try > 1)
263 msleep(5);
264 }
265 return count;
266}
267static CLASS_DEVICE_ATTR(companion, 0644, show_companion, store_companion);
268
269static inline void create_companion_file(struct ehci_hcd *ehci)
270{
271 int i;
272
273 /* with integrated TT there is no companion! */
274 if (!ehci_is_TDI(ehci))
275 i = class_device_create_file(ehci_to_hcd(ehci)->self.class_dev,
276 &class_device_attr_companion);
277}
278
279static inline void remove_companion_file(struct ehci_hcd *ehci)
280{
281 /* with integrated TT there is no companion! */
282 if (!ehci_is_TDI(ehci))
283 class_device_remove_file(ehci_to_hcd(ehci)->self.class_dev,
284 &class_device_attr_companion);
285}
286
287
288/*-------------------------------------------------------------------------*/
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static int check_reset_complete (
291 struct ehci_hcd *ehci,
292 int index,
Alan Sterne6316562007-01-16 11:58:00 -0500293 u32 __iomem *status_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 int port_status
295) {
296 if (!(port_status & PORT_CONNECT)) {
297 ehci->reset_done [index] = 0;
298 return port_status;
299 }
300
301 /* if reset finished and it's still not enabled -- handoff */
302 if (!(port_status & PORT_PE)) {
303
304 /* with integrated TT, there's nobody to hand it to! */
305 if (ehci_is_TDI(ehci)) {
306 ehci_dbg (ehci,
307 "Failed to enable port %d on root hub TT\n",
308 index+1);
309 return port_status;
310 }
311
312 ehci_dbg (ehci, "port %d full speed --> companion\n",
313 index + 1);
314
315 // what happens if HCS_N_CC(params) == 0 ?
316 port_status |= PORT_OWNER;
David Brownell10f65242005-08-31 10:55:38 -0700317 port_status &= ~PORT_RWC_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500318 ehci_writel(ehci, port_status, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 } else
321 ehci_dbg (ehci, "port %d high speed\n", index + 1);
322
323 return port_status;
324}
325
326/*-------------------------------------------------------------------------*/
327
328
329/* build "status change" packet (one or two bytes) from HC registers */
330
331static int
332ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
333{
334 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
335 u32 temp, status = 0;
David Brownell93f1a472006-11-16 23:34:58 -0800336 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 int ports, i, retval = 1;
338 unsigned long flags;
339
340 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
341 if (!HC_IS_RUNNING(hcd->state))
342 return 0;
343
344 /* init status to no-changes */
345 buf [0] = 0;
346 ports = HCS_N_PORTS (ehci->hcs_params);
347 if (ports > 7) {
348 buf [1] = 0;
349 retval++;
350 }
David Brownell53bd6a62006-08-30 14:50:06 -0700351
David Brownell93f1a472006-11-16 23:34:58 -0800352 /* Some boards (mostly VIA?) report bogus overcurrent indications,
353 * causing massive log spam unless we completely ignore them. It
354 * may be relevant that VIA VT8235 controlers, where PORT_POWER is
355 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
356 * PORT_POWER; that's surprising, but maybe within-spec.
357 */
358 if (!ignore_oc)
359 mask = PORT_CSC | PORT_PEC | PORT_OCC;
360 else
361 mask = PORT_CSC | PORT_PEC;
362 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /* no hub change reports (bit 0) for now (power, ...) */
365
366 /* port N changes (bit N)? */
367 spin_lock_irqsave (&ehci->lock, flags);
368 for (i = 0; i < ports; i++) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100369 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern625b5c92007-01-16 11:58:47 -0500370
371 /*
372 * Return status information even for ports with OWNER set.
373 * Otherwise khubd wouldn't see the disconnect event when a
374 * high-speed device is switched over to the companion
375 * controller by the user.
376 */
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (!(temp & PORT_CONNECT))
379 ehci->reset_done [i] = 0;
David Brownell93f1a472006-11-16 23:34:58 -0800380 if ((temp & mask) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 || ((temp & PORT_RESUME) != 0
382 && time_after (jiffies,
383 ehci->reset_done [i]))) {
384 if (i < 7)
385 buf [0] |= 1 << (i + 1);
386 else
387 buf [1] |= 1 << (i - 7);
388 status = STS_PCD;
389 }
390 }
391 /* FIXME autosuspend idle root hubs */
392 spin_unlock_irqrestore (&ehci->lock, flags);
393 return status ? retval : 0;
394}
395
396/*-------------------------------------------------------------------------*/
397
398static void
399ehci_hub_descriptor (
400 struct ehci_hcd *ehci,
401 struct usb_hub_descriptor *desc
402) {
403 int ports = HCS_N_PORTS (ehci->hcs_params);
404 u16 temp;
405
406 desc->bDescriptorType = 0x29;
407 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
408 desc->bHubContrCurrent = 0;
409
410 desc->bNbrPorts = ports;
411 temp = 1 + (ports / 8);
412 desc->bDescLength = 7 + 2 * temp;
413
414 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
415 memset (&desc->bitmap [0], 0, temp);
416 memset (&desc->bitmap [temp], 0xff, temp);
417
418 temp = 0x0008; /* per-port overcurrent reporting */
419 if (HCS_PPC (ehci->hcs_params))
420 temp |= 0x0001; /* per-port power control */
David Brownell56c1e262005-04-09 09:00:29 -0700421 else
422 temp |= 0x0002; /* no power switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#if 0
424// re-enable when we support USB_PORT_FEAT_INDICATOR below.
425 if (HCS_INDICATOR (ehci->hcs_params))
426 temp |= 0x0080; /* per-port indicators (LEDs) */
427#endif
428 desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
429}
430
431/*-------------------------------------------------------------------------*/
432
David Brownell53bd6a62006-08-30 14:50:06 -0700433#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435static int ehci_hub_control (
436 struct usb_hcd *hcd,
437 u16 typeReq,
438 u16 wValue,
439 u16 wIndex,
440 char *buf,
441 u16 wLength
442) {
443 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
444 int ports = HCS_N_PORTS (ehci->hcs_params);
Alan Sterne6316562007-01-16 11:58:00 -0500445 u32 __iomem *status_reg = &ehci->regs->port_status[wIndex - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 u32 temp, status;
447 unsigned long flags;
448 int retval = 0;
David Brownellf0d7f272006-11-16 23:56:15 -0800449 unsigned selector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /*
452 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
453 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
454 * (track current state ourselves) ... blink for diagnostics,
455 * power, "this is the one", etc. EHCI spec supports this.
456 */
457
458 spin_lock_irqsave (&ehci->lock, flags);
459 switch (typeReq) {
460 case ClearHubFeature:
461 switch (wValue) {
462 case C_HUB_LOCAL_POWER:
463 case C_HUB_OVER_CURRENT:
464 /* no hub-wide feature/status flags */
465 break;
466 default:
467 goto error;
468 }
469 break;
470 case ClearPortFeature:
471 if (!wIndex || wIndex > ports)
472 goto error;
473 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500474 temp = ehci_readl(ehci, status_reg);
Alan Stern625b5c92007-01-16 11:58:47 -0500475
476 /*
477 * Even if OWNER is set, so the port is owned by the
478 * companion controller, khubd needs to be able to clear
479 * the port-change status bits (especially
480 * USB_PORT_FEAT_C_CONNECTION).
481 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 switch (wValue) {
484 case USB_PORT_FEAT_ENABLE:
Alan Sterne6316562007-01-16 11:58:00 -0500485 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487 case USB_PORT_FEAT_C_ENABLE:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100488 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
Alan Sterne6316562007-01-16 11:58:00 -0500489 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
491 case USB_PORT_FEAT_SUSPEND:
492 if (temp & PORT_RESET)
493 goto error;
David Brownellf8aeb3b2006-01-20 13:55:14 -0800494 if (ehci->no_selective_suspend)
495 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (temp & PORT_SUSPEND) {
497 if ((temp & PORT_PE) == 0)
498 goto error;
499 /* resume signaling for 20 msec */
David Brownell10f65242005-08-31 10:55:38 -0700500 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100501 ehci_writel(ehci, temp | PORT_RESUME,
Alan Sterne6316562007-01-16 11:58:00 -0500502 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 ehci->reset_done [wIndex] = jiffies
504 + msecs_to_jiffies (20);
505 }
506 break;
507 case USB_PORT_FEAT_C_SUSPEND:
508 /* we auto-clear this feature */
509 break;
510 case USB_PORT_FEAT_POWER:
511 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100512 ehci_writel(ehci,
513 temp & ~(PORT_RWC_BITS | PORT_POWER),
Alan Sterne6316562007-01-16 11:58:00 -0500514 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
516 case USB_PORT_FEAT_C_CONNECTION:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100517 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
Alan Sterne6316562007-01-16 11:58:00 -0500518 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 break;
520 case USB_PORT_FEAT_C_OVER_CURRENT:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100521 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
Alan Sterne6316562007-01-16 11:58:00 -0500522 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
524 case USB_PORT_FEAT_C_RESET:
525 /* GetPortStatus clears reset */
526 break;
527 default:
528 goto error;
529 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100530 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 break;
532 case GetHubDescriptor:
533 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
534 buf);
535 break;
536 case GetHubStatus:
537 /* no hub-wide feature/status flags */
538 memset (buf, 0, 4);
539 //cpu_to_le32s ((u32 *) buf);
540 break;
541 case GetPortStatus:
542 if (!wIndex || wIndex > ports)
543 goto error;
544 wIndex--;
545 status = 0;
Alan Sterne6316562007-01-16 11:58:00 -0500546 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 // wPortChange bits
549 if (temp & PORT_CSC)
550 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
551 if (temp & PORT_PEC)
552 status |= 1 << USB_PORT_FEAT_C_ENABLE;
David Brownell93f1a472006-11-16 23:34:58 -0800553 if ((temp & PORT_OCC) && !ignore_oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
555
556 /* whoever resumes must GetPortStatus to complete it!! */
557 if ((temp & PORT_RESUME)
558 && time_after (jiffies,
559 ehci->reset_done [wIndex])) {
560 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
561 ehci->reset_done [wIndex] = 0;
562
563 /* stop resume signaling */
Alan Sterne6316562007-01-16 11:58:00 -0500564 temp = ehci_readl(ehci, status_reg);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100565 ehci_writel(ehci,
Alan Sterne6316562007-01-16 11:58:00 -0500566 temp & ~(PORT_RWC_BITS | PORT_RESUME),
567 status_reg);
568 retval = handshake(ehci, status_reg,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100569 PORT_RESUME, 0, 2000 /* 2msec */);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (retval != 0) {
571 ehci_err (ehci, "port %d resume error %d\n",
572 wIndex + 1, retval);
573 goto error;
574 }
575 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
576 }
577
578 /* whoever resets must GetPortStatus to complete it!! */
579 if ((temp & PORT_RESET)
580 && time_after (jiffies,
581 ehci->reset_done [wIndex])) {
582 status |= 1 << USB_PORT_FEAT_C_RESET;
583 ehci->reset_done [wIndex] = 0;
584
585 /* force reset to complete */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100586 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
Alan Sterne6316562007-01-16 11:58:00 -0500587 status_reg);
David Brownellc22fa3a2005-06-13 07:15:28 -0700588 /* REVISIT: some hardware needs 550+ usec to clear
589 * this bit; seems too long to spin routinely...
590 */
Alan Sterne6316562007-01-16 11:58:00 -0500591 retval = handshake(ehci, status_reg,
David Brownellc22fa3a2005-06-13 07:15:28 -0700592 PORT_RESET, 0, 750);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (retval != 0) {
594 ehci_err (ehci, "port %d reset error %d\n",
595 wIndex + 1, retval);
596 goto error;
597 }
598
599 /* see what we found out */
Alan Sterne6316562007-01-16 11:58:00 -0500600 temp = check_reset_complete (ehci, wIndex, status_reg,
601 ehci_readl(ehci, status_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
Alan Stern57e06c12007-01-16 11:59:45 -0500604 /* transfer dedicated ports to the companion hc */
605 if ((temp & PORT_CONNECT) &&
606 test_bit(wIndex, &ehci->companion_ports)) {
607 temp &= ~PORT_RWC_BITS;
608 temp |= PORT_OWNER;
609 ehci_writel(ehci, temp, status_reg);
610 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
611 temp = ehci_readl(ehci, status_reg);
612 }
613
Alan Stern625b5c92007-01-16 11:58:47 -0500614 /*
615 * Even if OWNER is set, there's no harm letting khubd
616 * see the wPortStatus values (they should all be 0 except
617 * for PORT_POWER anyway).
618 */
619
620 if (temp & PORT_CONNECT) {
621 status |= 1 << USB_PORT_FEAT_CONNECTION;
622 // status may be from integrated TT
623 status |= ehci_port_speed(ehci, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Alan Stern625b5c92007-01-16 11:58:47 -0500625 if (temp & PORT_PE)
626 status |= 1 << USB_PORT_FEAT_ENABLE;
627 if (temp & (PORT_SUSPEND|PORT_RESUME))
628 status |= 1 << USB_PORT_FEAT_SUSPEND;
629 if (temp & PORT_OC)
630 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
631 if (temp & PORT_RESET)
632 status |= 1 << USB_PORT_FEAT_RESET;
633 if (temp & PORT_POWER)
634 status |= 1 << USB_PORT_FEAT_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636#ifndef EHCI_VERBOSE_DEBUG
637 if (status & ~0xffff) /* only if wPortChange is interesting */
638#endif
639 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
640 // we "know" this alignment is good, caller used kmalloc()...
641 *((__le32 *) buf) = cpu_to_le32 (status);
642 break;
643 case SetHubFeature:
644 switch (wValue) {
645 case C_HUB_LOCAL_POWER:
646 case C_HUB_OVER_CURRENT:
647 /* no hub-wide feature/status flags */
648 break;
649 default:
650 goto error;
651 }
652 break;
653 case SetPortFeature:
David Brownellf0d7f272006-11-16 23:56:15 -0800654 selector = wIndex >> 8;
655 wIndex &= 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (!wIndex || wIndex > ports)
657 goto error;
658 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500659 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (temp & PORT_OWNER)
661 break;
662
David Brownell10f65242005-08-31 10:55:38 -0700663 temp &= ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 switch (wValue) {
665 case USB_PORT_FEAT_SUSPEND:
David Brownellf8aeb3b2006-01-20 13:55:14 -0800666 if (ehci->no_selective_suspend)
667 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if ((temp & PORT_PE) == 0
669 || (temp & PORT_RESET) != 0)
670 goto error;
David Brownell2c1c3c42005-11-07 15:24:46 -0800671 if (device_may_wakeup(&hcd->self.root_hub->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 temp |= PORT_WAKE_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500673 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 break;
675 case USB_PORT_FEAT_POWER:
676 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100677 ehci_writel(ehci, temp | PORT_POWER,
Alan Sterne6316562007-01-16 11:58:00 -0500678 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 break;
680 case USB_PORT_FEAT_RESET:
681 if (temp & PORT_RESUME)
682 goto error;
683 /* line status bits may report this as low speed,
684 * which can be fine if this root hub has a
685 * transaction translator built in.
686 */
687 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
688 && !ehci_is_TDI(ehci)
689 && PORT_USB11 (temp)) {
690 ehci_dbg (ehci,
691 "port %d low speed --> companion\n",
692 wIndex + 1);
693 temp |= PORT_OWNER;
694 } else {
695 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
696 temp |= PORT_RESET;
697 temp &= ~PORT_PE;
698
699 /*
700 * caller must wait, then call GetPortStatus
701 * usb 2.0 spec says 50 ms resets on root
702 */
703 ehci->reset_done [wIndex] = jiffies
704 + msecs_to_jiffies (50);
705 }
Alan Sterne6316562007-01-16 11:58:00 -0500706 ehci_writel(ehci, temp, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
David Brownellf0d7f272006-11-16 23:56:15 -0800708
709 /* For downstream facing ports (these): one hub port is put
710 * into test mode according to USB2 11.24.2.13, then the hub
711 * must be reset (which for root hub now means rmmod+modprobe,
712 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
713 * about the EHCI-specific stuff.
714 */
715 case USB_PORT_FEAT_TEST:
716 if (!selector || selector > 5)
717 goto error;
718 ehci_quiesce(ehci);
719 ehci_halt(ehci);
720 temp |= selector << 16;
Alan Sterne6316562007-01-16 11:58:00 -0500721 ehci_writel(ehci, temp, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -0800722 break;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 default:
725 goto error;
726 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100727 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
729
730 default:
731error:
732 /* "stall" on error */
733 retval = -EPIPE;
734 }
735 spin_unlock_irqrestore (&ehci->lock, flags);
736 return retval;
737}