blob: 3e80de7c7f5b11bead160d2df617f7050d9acc06 [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
Alan Stern383975d2007-05-04 11:52:40 -040031#ifdef CONFIG_USB_PERSIST
32
33static int ehci_hub_control(
34 struct usb_hcd *hcd,
35 u16 typeReq,
36 u16 wValue,
37 u16 wIndex,
38 char *buf,
39 u16 wLength
40);
41
42/* After a power loss, ports that were owned by the companion must be
43 * reset so that the companion can still own them.
44 */
45static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
46{
47 u32 __iomem *reg;
48 u32 status;
49 int port;
50 __le32 buf;
51 struct usb_hcd *hcd = ehci_to_hcd(ehci);
52
53 if (!ehci->owned_ports)
54 return;
55
56 /* Give the connections some time to appear */
57 msleep(20);
58
59 port = HCS_N_PORTS(ehci->hcs_params);
60 while (port--) {
61 if (test_bit(port, &ehci->owned_ports)) {
62 reg = &ehci->regs->port_status[port];
63 status = ehci_readl(ehci, reg);
64
65 /* Port already owned by companion? */
66 if (status & PORT_OWNER)
67 clear_bit(port, &ehci->owned_ports);
68 else
69 ehci_hub_control(hcd, SetPortFeature,
70 USB_PORT_FEAT_RESET, port + 1,
71 NULL, 0);
72 }
73 }
74
75 if (!ehci->owned_ports)
76 return;
77 msleep(90); /* Wait for resets to complete */
78
79 port = HCS_N_PORTS(ehci->hcs_params);
80 while (port--) {
81 if (test_bit(port, &ehci->owned_ports)) {
82 ehci_hub_control(hcd, GetPortStatus,
83 0, port + 1,
84 (char *) &buf, sizeof(buf));
85
86 /* The companion should now own the port,
87 * but if something went wrong the port must not
88 * remain enabled.
89 */
90 reg = &ehci->regs->port_status[port];
91 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
92 if (status & PORT_OWNER)
93 ehci_writel(ehci, status | PORT_CSC, reg);
94 else {
95 ehci_dbg(ehci, "failed handover port %d: %x\n",
96 port + 1, status);
97 ehci_writel(ehci, status & ~PORT_PE, reg);
98 }
99 }
100 }
101
102 ehci->owned_ports = 0;
103}
104
105#else /* CONFIG_USB_PERSIST */
106
107static inline void ehci_handover_companion_ports(struct ehci_hcd *ehci)
108{ }
109
110#endif
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#ifdef CONFIG_PM
113
Alan Stern0c0382e2005-10-13 17:08:02 -0400114static int ehci_bus_suspend (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
117 int port;
Alan Stern8c033562006-11-09 14:42:16 -0500118 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Alan Stern8c774fe2007-02-01 16:09:59 -0500120 ehci_dbg(ehci, "suspend root hub\n");
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (time_before (jiffies, ehci->next_statechange))
123 msleep(5);
124
125 port = HCS_N_PORTS (ehci->hcs_params);
126 spin_lock_irq (&ehci->lock);
127
128 /* stop schedules, clean any completed work */
129 if (HC_IS_RUNNING(hcd->state)) {
130 ehci_quiesce (ehci);
131 hcd->state = HC_STATE_QUIESCING;
132 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100133 ehci->command = ehci_readl(ehci, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (ehci->reclaim)
Greg Kroah-Hartman64f89792006-10-17 13:57:18 -0700135 ehci->reclaim_ready = 1;
David Howells7d12e782006-10-05 14:55:46 +0100136 ehci_work(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Alan Stern8c033562006-11-09 14:42:16 -0500138 /* Unlike other USB host controller types, EHCI doesn't have
139 * any notion of "global" or bus-wide suspend. The driver has
140 * to manually suspend all the active unsuspended ports, and
141 * then manually resume them in the bus_resume() routine.
142 */
143 ehci->bus_suspended = 0;
Alan Stern383975d2007-05-04 11:52:40 -0400144 ehci->owned_ports = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 while (port--) {
146 u32 __iomem *reg = &ehci->regs->port_status [port];
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100147 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 u32 t2 = t1;
149
Alan Stern8c033562006-11-09 14:42:16 -0500150 /* keep track of which ports we suspend */
Alan Stern383975d2007-05-04 11:52:40 -0400151 if (t1 & PORT_OWNER)
152 set_bit(port, &ehci->owned_ports);
153 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 t2 |= PORT_SUSPEND;
Alan Stern8c033562006-11-09 14:42:16 -0500155 set_bit(port, &ehci->bus_suspended);
156 }
157
158 /* enable remote wakeup on all ports */
David Brownell2c1c3c42005-11-07 15:24:46 -0800159 if (device_may_wakeup(&hcd->self.root_hub->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
161 else
162 t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
163
164 if (t1 != t2) {
165 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
166 port + 1, t1, t2);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100167 ehci_writel(ehci, t2, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 }
170
171 /* turn off now-idle HC */
David Brownell4756ae52005-05-09 17:23:51 -0700172 del_timer_sync (&ehci->watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 ehci_halt (ehci);
174 hcd->state = HC_STATE_SUSPENDED;
175
Alan Stern8c033562006-11-09 14:42:16 -0500176 /* allow remote wakeup */
177 mask = INTR_MASK;
178 if (!device_may_wakeup(&hcd->self.root_hub->dev))
179 mask &= ~STS_PCD;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100180 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
181 ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
184 spin_unlock_irq (&ehci->lock);
185 return 0;
186}
187
188
189/* caller has locked the root hub, and should reset/reinit on error */
Alan Stern0c0382e2005-10-13 17:08:02 -0400190static int ehci_bus_resume (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
193 u32 temp;
Alan Stern383975d2007-05-04 11:52:40 -0400194 u32 power_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if (time_before (jiffies, ehci->next_statechange))
198 msleep(5);
199 spin_lock_irq (&ehci->lock);
200
David Brownellf03c17f2005-11-23 15:45:28 -0800201 /* Ideally and we've got a real resume here, and no port's power
202 * was lost. (For PCI, that means Vaux was maintained.) But we
203 * could instead be restoring a swsusp snapshot -- so that BIOS was
204 * the last user of the controller, not reset/pm hardware keeping
205 * state we gave to it.
206 */
Alan Stern383975d2007-05-04 11:52:40 -0400207 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
208 ehci_dbg(ehci, "resume root hub%s\n",
209 power_okay ? "" : " after power loss");
David Brownellf03c17f2005-11-23 15:45:28 -0800210
Alan Stern8c033562006-11-09 14:42:16 -0500211 /* at least some APM implementations will try to deliver
212 * IRQs right away, so delay them until we're ready.
213 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100214 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500215
216 /* re-init operational registers */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100217 ehci_writel(ehci, 0, &ehci->regs->segment);
218 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
219 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* restore CMD_RUN, framelist size, and irq threshold */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100222 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Alan Sterne198a312007-03-15 15:54:30 -0400224 /* Some controller/firmware combinations need a delay during which
225 * they set up the port statuses. See Bugzilla #8190. */
226 mdelay(8);
227
Alan Stern8c033562006-11-09 14:42:16 -0500228 /* manually resume the ports we suspended during bus_suspend() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 i = HCS_N_PORTS (ehci->hcs_params);
230 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100231 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
David Brownell10f65242005-08-31 10:55:38 -0700232 temp &= ~(PORT_RWC_BITS
233 | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
Alan Stern8c033562006-11-09 14:42:16 -0500234 if (test_bit(i, &ehci->bus_suspended) &&
235 (temp & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
237 temp |= PORT_RESUME;
238 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100239 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 i = HCS_N_PORTS (ehci->hcs_params);
242 mdelay (20);
243 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100244 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500245 if (test_bit(i, &ehci->bus_suspended) &&
246 (temp & PORT_SUSPEND)) {
247 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100248 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500249 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100252 (void) ehci_readl(ehci, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* maybe re-activate the schedule(s) */
255 temp = 0;
256 if (ehci->async->qh_next.qh)
257 temp |= CMD_ASE;
258 if (ehci->periodic_sched)
259 temp |= CMD_PSE;
260 if (temp) {
261 ehci->command |= temp;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100262 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264
265 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
266 hcd->state = HC_STATE_RUNNING;
267
268 /* Now we can safely re-enable irqs */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100269 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 spin_unlock_irq (&ehci->lock);
Alan Stern383975d2007-05-04 11:52:40 -0400272
273 if (!power_okay)
274 ehci_handover_companion_ports(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 0;
276}
277
278#else
279
Alan Stern0c0382e2005-10-13 17:08:02 -0400280#define ehci_bus_suspend NULL
281#define ehci_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283#endif /* CONFIG_PM */
284
285/*-------------------------------------------------------------------------*/
286
Alan Stern57e06c12007-01-16 11:59:45 -0500287/* Display the ports dedicated to the companion controller */
288static ssize_t show_companion(struct class_device *class_dev, char *buf)
289{
290 struct ehci_hcd *ehci;
291 int nports, index, n;
292 int count = PAGE_SIZE;
293 char *ptr = buf;
294
295 ehci = hcd_to_ehci(bus_to_hcd(class_get_devdata(class_dev)));
296 nports = HCS_N_PORTS(ehci->hcs_params);
297
298 for (index = 0; index < nports; ++index) {
299 if (test_bit(index, &ehci->companion_ports)) {
300 n = scnprintf(ptr, count, "%d\n", index + 1);
301 ptr += n;
302 count -= n;
303 }
304 }
305 return ptr - buf;
306}
307
308/*
309 * Dedicate or undedicate a port to the companion controller.
310 * Syntax is "[-]portnum", where a leading '-' sign means
311 * return control of the port to the EHCI controller.
312 */
313static ssize_t store_companion(struct class_device *class_dev,
314 const char *buf, size_t count)
315{
316 struct ehci_hcd *ehci;
317 int portnum, new_owner, try;
318 u32 __iomem *status_reg;
319 u32 port_status;
320
321 ehci = hcd_to_ehci(bus_to_hcd(class_get_devdata(class_dev)));
322 new_owner = PORT_OWNER; /* Owned by companion */
323 if (sscanf(buf, "%d", &portnum) != 1)
324 return -EINVAL;
325 if (portnum < 0) {
326 portnum = - portnum;
327 new_owner = 0; /* Owned by EHCI */
328 }
329 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
330 return -ENOENT;
331 status_reg = &ehci->regs->port_status[--portnum];
332 if (new_owner)
333 set_bit(portnum, &ehci->companion_ports);
334 else
335 clear_bit(portnum, &ehci->companion_ports);
336
337 /*
338 * The controller won't set the OWNER bit if the port is
339 * enabled, so this loop will sometimes require at least two
340 * iterations: one to disable the port and one to set OWNER.
341 */
342
343 for (try = 4; try > 0; --try) {
344 spin_lock_irq(&ehci->lock);
345 port_status = ehci_readl(ehci, status_reg);
346 if ((port_status & PORT_OWNER) == new_owner
347 || (port_status & (PORT_OWNER | PORT_CONNECT))
348 == 0)
349 try = 0;
350 else {
351 port_status ^= PORT_OWNER;
352 port_status &= ~(PORT_PE | PORT_RWC_BITS);
353 ehci_writel(ehci, port_status, status_reg);
354 }
355 spin_unlock_irq(&ehci->lock);
356 if (try > 1)
357 msleep(5);
358 }
359 return count;
360}
361static CLASS_DEVICE_ATTR(companion, 0644, show_companion, store_companion);
362
363static inline void create_companion_file(struct ehci_hcd *ehci)
364{
365 int i;
366
367 /* with integrated TT there is no companion! */
368 if (!ehci_is_TDI(ehci))
369 i = class_device_create_file(ehci_to_hcd(ehci)->self.class_dev,
370 &class_device_attr_companion);
371}
372
373static inline void remove_companion_file(struct ehci_hcd *ehci)
374{
375 /* with integrated TT there is no companion! */
376 if (!ehci_is_TDI(ehci))
377 class_device_remove_file(ehci_to_hcd(ehci)->self.class_dev,
378 &class_device_attr_companion);
379}
380
381
382/*-------------------------------------------------------------------------*/
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static int check_reset_complete (
385 struct ehci_hcd *ehci,
386 int index,
Alan Sterne6316562007-01-16 11:58:00 -0500387 u32 __iomem *status_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int port_status
389) {
390 if (!(port_status & PORT_CONNECT)) {
391 ehci->reset_done [index] = 0;
392 return port_status;
393 }
394
395 /* if reset finished and it's still not enabled -- handoff */
396 if (!(port_status & PORT_PE)) {
397
398 /* with integrated TT, there's nobody to hand it to! */
399 if (ehci_is_TDI(ehci)) {
400 ehci_dbg (ehci,
401 "Failed to enable port %d on root hub TT\n",
402 index+1);
403 return port_status;
404 }
405
406 ehci_dbg (ehci, "port %d full speed --> companion\n",
407 index + 1);
408
409 // what happens if HCS_N_CC(params) == 0 ?
410 port_status |= PORT_OWNER;
David Brownell10f65242005-08-31 10:55:38 -0700411 port_status &= ~PORT_RWC_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500412 ehci_writel(ehci, port_status, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 } else
415 ehci_dbg (ehci, "port %d high speed\n", index + 1);
416
417 return port_status;
418}
419
420/*-------------------------------------------------------------------------*/
421
422
423/* build "status change" packet (one or two bytes) from HC registers */
424
425static int
426ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
427{
428 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
429 u32 temp, status = 0;
David Brownell93f1a472006-11-16 23:34:58 -0800430 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 int ports, i, retval = 1;
432 unsigned long flags;
433
434 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
435 if (!HC_IS_RUNNING(hcd->state))
436 return 0;
437
438 /* init status to no-changes */
439 buf [0] = 0;
440 ports = HCS_N_PORTS (ehci->hcs_params);
441 if (ports > 7) {
442 buf [1] = 0;
443 retval++;
444 }
David Brownell53bd6a62006-08-30 14:50:06 -0700445
David Brownell93f1a472006-11-16 23:34:58 -0800446 /* Some boards (mostly VIA?) report bogus overcurrent indications,
447 * causing massive log spam unless we completely ignore them. It
448 * may be relevant that VIA VT8235 controlers, where PORT_POWER is
449 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
450 * PORT_POWER; that's surprising, but maybe within-spec.
451 */
452 if (!ignore_oc)
453 mask = PORT_CSC | PORT_PEC | PORT_OCC;
454 else
455 mask = PORT_CSC | PORT_PEC;
456 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 /* no hub change reports (bit 0) for now (power, ...) */
459
460 /* port N changes (bit N)? */
461 spin_lock_irqsave (&ehci->lock, flags);
462 for (i = 0; i < ports; i++) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100463 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern625b5c92007-01-16 11:58:47 -0500464
465 /*
466 * Return status information even for ports with OWNER set.
467 * Otherwise khubd wouldn't see the disconnect event when a
468 * high-speed device is switched over to the companion
469 * controller by the user.
470 */
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!(temp & PORT_CONNECT))
473 ehci->reset_done [i] = 0;
David Brownell93f1a472006-11-16 23:34:58 -0800474 if ((temp & mask) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 || ((temp & PORT_RESUME) != 0
Alan Stern629e4422007-01-22 16:08:53 -0500476 && time_after_eq(jiffies,
477 ehci->reset_done[i]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (i < 7)
479 buf [0] |= 1 << (i + 1);
480 else
481 buf [1] |= 1 << (i - 7);
482 status = STS_PCD;
483 }
484 }
485 /* FIXME autosuspend idle root hubs */
486 spin_unlock_irqrestore (&ehci->lock, flags);
487 return status ? retval : 0;
488}
489
490/*-------------------------------------------------------------------------*/
491
492static void
493ehci_hub_descriptor (
494 struct ehci_hcd *ehci,
495 struct usb_hub_descriptor *desc
496) {
497 int ports = HCS_N_PORTS (ehci->hcs_params);
498 u16 temp;
499
500 desc->bDescriptorType = 0x29;
501 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
502 desc->bHubContrCurrent = 0;
503
504 desc->bNbrPorts = ports;
505 temp = 1 + (ports / 8);
506 desc->bDescLength = 7 + 2 * temp;
507
508 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
509 memset (&desc->bitmap [0], 0, temp);
510 memset (&desc->bitmap [temp], 0xff, temp);
511
512 temp = 0x0008; /* per-port overcurrent reporting */
513 if (HCS_PPC (ehci->hcs_params))
514 temp |= 0x0001; /* per-port power control */
David Brownell56c1e262005-04-09 09:00:29 -0700515 else
516 temp |= 0x0002; /* no power switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#if 0
518// re-enable when we support USB_PORT_FEAT_INDICATOR below.
519 if (HCS_INDICATOR (ehci->hcs_params))
520 temp |= 0x0080; /* per-port indicators (LEDs) */
521#endif
522 desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
523}
524
525/*-------------------------------------------------------------------------*/
526
David Brownell53bd6a62006-08-30 14:50:06 -0700527#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529static int ehci_hub_control (
530 struct usb_hcd *hcd,
531 u16 typeReq,
532 u16 wValue,
533 u16 wIndex,
534 char *buf,
535 u16 wLength
536) {
537 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
538 int ports = HCS_N_PORTS (ehci->hcs_params);
Alan Stern383975d2007-05-04 11:52:40 -0400539 u32 __iomem *status_reg = &ehci->regs->port_status[
540 (wIndex & 0xff) - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 u32 temp, status;
542 unsigned long flags;
543 int retval = 0;
David Brownellf0d7f272006-11-16 23:56:15 -0800544 unsigned selector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 /*
547 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
548 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
549 * (track current state ourselves) ... blink for diagnostics,
550 * power, "this is the one", etc. EHCI spec supports this.
551 */
552
553 spin_lock_irqsave (&ehci->lock, flags);
554 switch (typeReq) {
555 case ClearHubFeature:
556 switch (wValue) {
557 case C_HUB_LOCAL_POWER:
558 case C_HUB_OVER_CURRENT:
559 /* no hub-wide feature/status flags */
560 break;
561 default:
562 goto error;
563 }
564 break;
565 case ClearPortFeature:
566 if (!wIndex || wIndex > ports)
567 goto error;
568 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500569 temp = ehci_readl(ehci, status_reg);
Alan Stern625b5c92007-01-16 11:58:47 -0500570
571 /*
572 * Even if OWNER is set, so the port is owned by the
573 * companion controller, khubd needs to be able to clear
574 * the port-change status bits (especially
575 * USB_PORT_FEAT_C_CONNECTION).
576 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 switch (wValue) {
579 case USB_PORT_FEAT_ENABLE:
Alan Sterne6316562007-01-16 11:58:00 -0500580 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
582 case USB_PORT_FEAT_C_ENABLE:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100583 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
Alan Sterne6316562007-01-16 11:58:00 -0500584 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
586 case USB_PORT_FEAT_SUSPEND:
587 if (temp & PORT_RESET)
588 goto error;
David Brownellf8aeb3b2006-01-20 13:55:14 -0800589 if (ehci->no_selective_suspend)
590 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (temp & PORT_SUSPEND) {
592 if ((temp & PORT_PE) == 0)
593 goto error;
594 /* resume signaling for 20 msec */
David Brownell10f65242005-08-31 10:55:38 -0700595 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100596 ehci_writel(ehci, temp | PORT_RESUME,
Alan Sterne6316562007-01-16 11:58:00 -0500597 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ehci->reset_done [wIndex] = jiffies
599 + msecs_to_jiffies (20);
600 }
601 break;
602 case USB_PORT_FEAT_C_SUSPEND:
603 /* we auto-clear this feature */
604 break;
605 case USB_PORT_FEAT_POWER:
606 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100607 ehci_writel(ehci,
608 temp & ~(PORT_RWC_BITS | PORT_POWER),
Alan Sterne6316562007-01-16 11:58:00 -0500609 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
611 case USB_PORT_FEAT_C_CONNECTION:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100612 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
Alan Sterne6316562007-01-16 11:58:00 -0500613 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 break;
615 case USB_PORT_FEAT_C_OVER_CURRENT:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100616 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
Alan Sterne6316562007-01-16 11:58:00 -0500617 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
619 case USB_PORT_FEAT_C_RESET:
620 /* GetPortStatus clears reset */
621 break;
622 default:
623 goto error;
624 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100625 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 break;
627 case GetHubDescriptor:
628 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
629 buf);
630 break;
631 case GetHubStatus:
632 /* no hub-wide feature/status flags */
633 memset (buf, 0, 4);
634 //cpu_to_le32s ((u32 *) buf);
635 break;
636 case GetPortStatus:
637 if (!wIndex || wIndex > ports)
638 goto error;
639 wIndex--;
640 status = 0;
Alan Sterne6316562007-01-16 11:58:00 -0500641 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 // wPortChange bits
644 if (temp & PORT_CSC)
645 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
646 if (temp & PORT_PEC)
647 status |= 1 << USB_PORT_FEAT_C_ENABLE;
David Brownell93f1a472006-11-16 23:34:58 -0800648 if ((temp & PORT_OCC) && !ignore_oc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
650
651 /* whoever resumes must GetPortStatus to complete it!! */
Alan Stern629e4422007-01-22 16:08:53 -0500652 if (temp & PORT_RESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Alan Stern629e4422007-01-22 16:08:53 -0500654 /* Remote Wakeup received? */
655 if (!ehci->reset_done[wIndex]) {
656 /* resume signaling for 20 msec */
657 ehci->reset_done[wIndex] = jiffies
658 + msecs_to_jiffies(20);
659 /* check the port again */
660 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
661 ehci->reset_done[wIndex]);
662 }
663
664 /* resume completed? */
665 else if (time_after_eq(jiffies,
666 ehci->reset_done[wIndex])) {
667 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
668 ehci->reset_done[wIndex] = 0;
669
670 /* stop resume signaling */
671 temp = ehci_readl(ehci, status_reg);
672 ehci_writel(ehci,
Alan Sterne6316562007-01-16 11:58:00 -0500673 temp & ~(PORT_RWC_BITS | PORT_RESUME),
674 status_reg);
Alan Stern629e4422007-01-22 16:08:53 -0500675 retval = handshake(ehci, status_reg,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100676 PORT_RESUME, 0, 2000 /* 2msec */);
Alan Stern629e4422007-01-22 16:08:53 -0500677 if (retval != 0) {
678 ehci_err(ehci,
679 "port %d resume error %d\n",
680 wIndex + 1, retval);
681 goto error;
682 }
683 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 /* whoever resets must GetPortStatus to complete it!! */
688 if ((temp & PORT_RESET)
Alan Stern629e4422007-01-22 16:08:53 -0500689 && time_after_eq(jiffies,
690 ehci->reset_done[wIndex])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 status |= 1 << USB_PORT_FEAT_C_RESET;
692 ehci->reset_done [wIndex] = 0;
693
694 /* force reset to complete */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100695 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
Alan Sterne6316562007-01-16 11:58:00 -0500696 status_reg);
David Brownellc22fa3a2005-06-13 07:15:28 -0700697 /* REVISIT: some hardware needs 550+ usec to clear
698 * this bit; seems too long to spin routinely...
699 */
Alan Sterne6316562007-01-16 11:58:00 -0500700 retval = handshake(ehci, status_reg,
David Brownellc22fa3a2005-06-13 07:15:28 -0700701 PORT_RESET, 0, 750);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (retval != 0) {
703 ehci_err (ehci, "port %d reset error %d\n",
704 wIndex + 1, retval);
705 goto error;
706 }
707
708 /* see what we found out */
Alan Sterne6316562007-01-16 11:58:00 -0500709 temp = check_reset_complete (ehci, wIndex, status_reg,
710 ehci_readl(ehci, status_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712
Alan Stern57e06c12007-01-16 11:59:45 -0500713 /* transfer dedicated ports to the companion hc */
714 if ((temp & PORT_CONNECT) &&
715 test_bit(wIndex, &ehci->companion_ports)) {
716 temp &= ~PORT_RWC_BITS;
717 temp |= PORT_OWNER;
718 ehci_writel(ehci, temp, status_reg);
719 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
720 temp = ehci_readl(ehci, status_reg);
721 }
722
Alan Stern625b5c92007-01-16 11:58:47 -0500723 /*
724 * Even if OWNER is set, there's no harm letting khubd
725 * see the wPortStatus values (they should all be 0 except
726 * for PORT_POWER anyway).
727 */
728
729 if (temp & PORT_CONNECT) {
730 status |= 1 << USB_PORT_FEAT_CONNECTION;
731 // status may be from integrated TT
732 status |= ehci_port_speed(ehci, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Alan Stern625b5c92007-01-16 11:58:47 -0500734 if (temp & PORT_PE)
735 status |= 1 << USB_PORT_FEAT_ENABLE;
736 if (temp & (PORT_SUSPEND|PORT_RESUME))
737 status |= 1 << USB_PORT_FEAT_SUSPEND;
738 if (temp & PORT_OC)
739 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
740 if (temp & PORT_RESET)
741 status |= 1 << USB_PORT_FEAT_RESET;
742 if (temp & PORT_POWER)
743 status |= 1 << USB_PORT_FEAT_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745#ifndef EHCI_VERBOSE_DEBUG
746 if (status & ~0xffff) /* only if wPortChange is interesting */
747#endif
748 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
Max Dmitrichenko64543652007-03-06 02:45:01 +0300749 put_unaligned(cpu_to_le32 (status), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
751 case SetHubFeature:
752 switch (wValue) {
753 case C_HUB_LOCAL_POWER:
754 case C_HUB_OVER_CURRENT:
755 /* no hub-wide feature/status flags */
756 break;
757 default:
758 goto error;
759 }
760 break;
761 case SetPortFeature:
David Brownellf0d7f272006-11-16 23:56:15 -0800762 selector = wIndex >> 8;
763 wIndex &= 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (!wIndex || wIndex > ports)
765 goto error;
766 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500767 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (temp & PORT_OWNER)
769 break;
770
David Brownell10f65242005-08-31 10:55:38 -0700771 temp &= ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 switch (wValue) {
773 case USB_PORT_FEAT_SUSPEND:
David Brownellf8aeb3b2006-01-20 13:55:14 -0800774 if (ehci->no_selective_suspend)
775 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if ((temp & PORT_PE) == 0
777 || (temp & PORT_RESET) != 0)
778 goto error;
David Brownell2c1c3c42005-11-07 15:24:46 -0800779 if (device_may_wakeup(&hcd->self.root_hub->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 temp |= PORT_WAKE_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500781 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
783 case USB_PORT_FEAT_POWER:
784 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100785 ehci_writel(ehci, temp | PORT_POWER,
Alan Sterne6316562007-01-16 11:58:00 -0500786 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 break;
788 case USB_PORT_FEAT_RESET:
789 if (temp & PORT_RESUME)
790 goto error;
791 /* line status bits may report this as low speed,
792 * which can be fine if this root hub has a
793 * transaction translator built in.
794 */
795 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
796 && !ehci_is_TDI(ehci)
797 && PORT_USB11 (temp)) {
798 ehci_dbg (ehci,
799 "port %d low speed --> companion\n",
800 wIndex + 1);
801 temp |= PORT_OWNER;
802 } else {
803 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
804 temp |= PORT_RESET;
805 temp &= ~PORT_PE;
806
807 /*
808 * caller must wait, then call GetPortStatus
809 * usb 2.0 spec says 50 ms resets on root
810 */
811 ehci->reset_done [wIndex] = jiffies
812 + msecs_to_jiffies (50);
813 }
Alan Sterne6316562007-01-16 11:58:00 -0500814 ehci_writel(ehci, temp, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 break;
David Brownellf0d7f272006-11-16 23:56:15 -0800816
817 /* For downstream facing ports (these): one hub port is put
818 * into test mode according to USB2 11.24.2.13, then the hub
819 * must be reset (which for root hub now means rmmod+modprobe,
820 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
821 * about the EHCI-specific stuff.
822 */
823 case USB_PORT_FEAT_TEST:
824 if (!selector || selector > 5)
825 goto error;
826 ehci_quiesce(ehci);
827 ehci_halt(ehci);
828 temp |= selector << 16;
Alan Sterne6316562007-01-16 11:58:00 -0500829 ehci_writel(ehci, temp, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -0800830 break;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 default:
833 goto error;
834 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100835 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
837
838 default:
839error:
840 /* "stall" on error */
841 retval = -EPIPE;
842 }
843 spin_unlock_irqrestore (&ehci->lock, flags);
844 return retval;
845}