blob: 8d513a15d0cd9686a0545feca605247da52cf925 [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];
Alan Stern3c519b82007-05-04 11:55:31 -040063 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Alan Stern383975d2007-05-04 11:52:40 -040064
65 /* Port already owned by companion? */
66 if (status & PORT_OWNER)
67 clear_bit(port, &ehci->owned_ports);
Alan Stern3c519b82007-05-04 11:55:31 -040068 else if (test_bit(port, &ehci->companion_ports))
69 ehci_writel(ehci, status & ~PORT_PE, reg);
Alan Stern383975d2007-05-04 11:52:40 -040070 else
71 ehci_hub_control(hcd, SetPortFeature,
72 USB_PORT_FEAT_RESET, port + 1,
73 NULL, 0);
74 }
75 }
76
77 if (!ehci->owned_ports)
78 return;
79 msleep(90); /* Wait for resets to complete */
80
81 port = HCS_N_PORTS(ehci->hcs_params);
82 while (port--) {
83 if (test_bit(port, &ehci->owned_ports)) {
84 ehci_hub_control(hcd, GetPortStatus,
85 0, port + 1,
86 (char *) &buf, sizeof(buf));
87
88 /* The companion should now own the port,
89 * but if something went wrong the port must not
90 * remain enabled.
91 */
92 reg = &ehci->regs->port_status[port];
93 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
94 if (status & PORT_OWNER)
95 ehci_writel(ehci, status | PORT_CSC, reg);
96 else {
97 ehci_dbg(ehci, "failed handover port %d: %x\n",
98 port + 1, status);
99 ehci_writel(ehci, status & ~PORT_PE, reg);
100 }
101 }
102 }
103
104 ehci->owned_ports = 0;
105}
106
107#else /* CONFIG_USB_PERSIST */
108
109static inline void ehci_handover_companion_ports(struct ehci_hcd *ehci)
110{ }
111
112#endif
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#ifdef CONFIG_PM
115
Alan Stern0c0382e2005-10-13 17:08:02 -0400116static int ehci_bus_suspend (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
119 int port;
Alan Stern8c033562006-11-09 14:42:16 -0500120 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Alan Stern8c774fe2007-02-01 16:09:59 -0500122 ehci_dbg(ehci, "suspend root hub\n");
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (time_before (jiffies, ehci->next_statechange))
125 msleep(5);
Alan Sternf8fa7572008-01-10 16:43:15 -0500126 del_timer_sync(&ehci->watchdog);
127 del_timer_sync(&ehci->iaa_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 port = HCS_N_PORTS (ehci->hcs_params);
130 spin_lock_irq (&ehci->lock);
131
132 /* stop schedules, clean any completed work */
133 if (HC_IS_RUNNING(hcd->state)) {
134 ehci_quiesce (ehci);
135 hcd->state = HC_STATE_QUIESCING;
136 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100137 ehci->command = ehci_readl(ehci, &ehci->regs->command);
David Howells7d12e782006-10-05 14:55:46 +0100138 ehci_work(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Alan Stern8c033562006-11-09 14:42:16 -0500140 /* Unlike other USB host controller types, EHCI doesn't have
141 * any notion of "global" or bus-wide suspend. The driver has
142 * to manually suspend all the active unsuspended ports, and
143 * then manually resume them in the bus_resume() routine.
144 */
145 ehci->bus_suspended = 0;
Alan Stern383975d2007-05-04 11:52:40 -0400146 ehci->owned_ports = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 while (port--) {
148 u32 __iomem *reg = &ehci->regs->port_status [port];
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100149 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 u32 t2 = t1;
151
Alan Stern8c033562006-11-09 14:42:16 -0500152 /* keep track of which ports we suspend */
Alan Stern383975d2007-05-04 11:52:40 -0400153 if (t1 & PORT_OWNER)
154 set_bit(port, &ehci->owned_ports);
155 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 t2 |= PORT_SUSPEND;
Alan Stern8c033562006-11-09 14:42:16 -0500157 set_bit(port, &ehci->bus_suspended);
158 }
159
160 /* enable remote wakeup on all ports */
David Brownell2c1c3c42005-11-07 15:24:46 -0800161 if (device_may_wakeup(&hcd->self.root_hub->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
163 else
164 t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
165
166 if (t1 != t2) {
167 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
168 port + 1, t1, t2);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100169 ehci_writel(ehci, t2, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 }
172
Alan Sterncd930c92008-01-10 11:14:53 -0500173 /* Apparently some devices need a >= 1-uframe delay here */
174 if (ehci->bus_suspended)
175 udelay(150);
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 /* turn off now-idle HC */
178 ehci_halt (ehci);
179 hcd->state = HC_STATE_SUSPENDED;
180
David Brownellcdc647a2008-04-02 13:40:20 -0700181 if (ehci->reclaim)
182 end_unlink_async(ehci);
183
Alan Stern8c033562006-11-09 14:42:16 -0500184 /* allow remote wakeup */
185 mask = INTR_MASK;
186 if (!device_may_wakeup(&hcd->self.root_hub->dev))
187 mask &= ~STS_PCD;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100188 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
189 ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
192 spin_unlock_irq (&ehci->lock);
193 return 0;
194}
195
196
197/* caller has locked the root hub, and should reset/reinit on error */
Alan Stern0c0382e2005-10-13 17:08:02 -0400198static int ehci_bus_resume (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
201 u32 temp;
Alan Stern383975d2007-05-04 11:52:40 -0400202 u32 power_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 if (time_before (jiffies, ehci->next_statechange))
206 msleep(5);
207 spin_lock_irq (&ehci->lock);
Alan Sterncfa59da2007-06-21 16:25:35 -0400208 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
209 spin_unlock_irq(&ehci->lock);
210 return -ESHUTDOWN;
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
David Brownellf03c17f2005-11-23 15:45:28 -0800213 /* Ideally and we've got a real resume here, and no port's power
214 * was lost. (For PCI, that means Vaux was maintained.) But we
215 * could instead be restoring a swsusp snapshot -- so that BIOS was
216 * the last user of the controller, not reset/pm hardware keeping
217 * state we gave to it.
218 */
Alan Stern383975d2007-05-04 11:52:40 -0400219 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
220 ehci_dbg(ehci, "resume root hub%s\n",
221 power_okay ? "" : " after power loss");
David Brownellf03c17f2005-11-23 15:45:28 -0800222
Alan Stern8c033562006-11-09 14:42:16 -0500223 /* at least some APM implementations will try to deliver
224 * IRQs right away, so delay them until we're ready.
225 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100226 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500227
228 /* re-init operational registers */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100229 ehci_writel(ehci, 0, &ehci->regs->segment);
230 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
231 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* restore CMD_RUN, framelist size, and irq threshold */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100234 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Alan Sterne198a312007-03-15 15:54:30 -0400236 /* Some controller/firmware combinations need a delay during which
237 * they set up the port statuses. See Bugzilla #8190. */
238 mdelay(8);
239
Alan Stern8c033562006-11-09 14:42:16 -0500240 /* manually resume the ports we suspended during bus_suspend() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 i = HCS_N_PORTS (ehci->hcs_params);
242 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100243 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
David Brownell10f65242005-08-31 10:55:38 -0700244 temp &= ~(PORT_RWC_BITS
245 | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
Alan Stern8c033562006-11-09 14:42:16 -0500246 if (test_bit(i, &ehci->bus_suspended) &&
247 (temp & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
249 temp |= PORT_RESUME;
250 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100251 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 i = HCS_N_PORTS (ehci->hcs_params);
254 mdelay (20);
255 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100256 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500257 if (test_bit(i, &ehci->bus_suspended) &&
258 (temp & PORT_SUSPEND)) {
259 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100260 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500261 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100264 (void) ehci_readl(ehci, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* maybe re-activate the schedule(s) */
267 temp = 0;
268 if (ehci->async->qh_next.qh)
269 temp |= CMD_ASE;
270 if (ehci->periodic_sched)
271 temp |= CMD_PSE;
272 if (temp) {
273 ehci->command |= temp;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100274 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
277 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
278 hcd->state = HC_STATE_RUNNING;
279
280 /* Now we can safely re-enable irqs */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100281 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 spin_unlock_irq (&ehci->lock);
Alan Stern3bb1af52008-03-03 15:15:36 -0500284 ehci_handover_companion_ports(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return 0;
286}
287
288#else
289
Alan Stern0c0382e2005-10-13 17:08:02 -0400290#define ehci_bus_suspend NULL
291#define ehci_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293#endif /* CONFIG_PM */
294
295/*-------------------------------------------------------------------------*/
296
Alan Stern57e06c12007-01-16 11:59:45 -0500297/* Display the ports dedicated to the companion controller */
Tony Jones5a3201b2007-09-11 14:07:31 -0700298static ssize_t show_companion(struct device *dev,
299 struct device_attribute *attr,
300 char *buf)
Alan Stern57e06c12007-01-16 11:59:45 -0500301{
302 struct ehci_hcd *ehci;
303 int nports, index, n;
304 int count = PAGE_SIZE;
305 char *ptr = buf;
306
Tony Jones5a3201b2007-09-11 14:07:31 -0700307 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
Alan Stern57e06c12007-01-16 11:59:45 -0500308 nports = HCS_N_PORTS(ehci->hcs_params);
309
310 for (index = 0; index < nports; ++index) {
311 if (test_bit(index, &ehci->companion_ports)) {
312 n = scnprintf(ptr, count, "%d\n", index + 1);
313 ptr += n;
314 count -= n;
315 }
316 }
317 return ptr - buf;
318}
319
320/*
Balaji Rao90da0962007-11-22 01:58:14 +0530321 * Sets the owner of a port
Alan Stern57e06c12007-01-16 11:59:45 -0500322 */
Balaji Rao90da0962007-11-22 01:58:14 +0530323static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
Alan Stern57e06c12007-01-16 11:59:45 -0500324{
Alan Stern57e06c12007-01-16 11:59:45 -0500325 u32 __iomem *status_reg;
326 u32 port_status;
Balaji Rao90da0962007-11-22 01:58:14 +0530327 int try;
Alan Stern57e06c12007-01-16 11:59:45 -0500328
Balaji Rao90da0962007-11-22 01:58:14 +0530329 status_reg = &ehci->regs->port_status[portnum];
Alan Stern57e06c12007-01-16 11:59:45 -0500330
331 /*
332 * The controller won't set the OWNER bit if the port is
333 * enabled, so this loop will sometimes require at least two
334 * iterations: one to disable the port and one to set OWNER.
335 */
Alan Stern57e06c12007-01-16 11:59:45 -0500336 for (try = 4; try > 0; --try) {
337 spin_lock_irq(&ehci->lock);
338 port_status = ehci_readl(ehci, status_reg);
339 if ((port_status & PORT_OWNER) == new_owner
340 || (port_status & (PORT_OWNER | PORT_CONNECT))
341 == 0)
342 try = 0;
343 else {
344 port_status ^= PORT_OWNER;
345 port_status &= ~(PORT_PE | PORT_RWC_BITS);
346 ehci_writel(ehci, port_status, status_reg);
347 }
348 spin_unlock_irq(&ehci->lock);
349 if (try > 1)
350 msleep(5);
351 }
Balaji Rao90da0962007-11-22 01:58:14 +0530352}
353
354/*
355 * Dedicate or undedicate a port to the companion controller.
356 * Syntax is "[-]portnum", where a leading '-' sign means
357 * return control of the port to the EHCI controller.
358 */
359static ssize_t store_companion(struct device *dev,
360 struct device_attribute *attr,
361 const char *buf, size_t count)
362{
363 struct ehci_hcd *ehci;
364 int portnum, new_owner;
365
366 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
367 new_owner = PORT_OWNER; /* Owned by companion */
368 if (sscanf(buf, "%d", &portnum) != 1)
369 return -EINVAL;
370 if (portnum < 0) {
371 portnum = - portnum;
372 new_owner = 0; /* Owned by EHCI */
373 }
374 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
375 return -ENOENT;
376 portnum--;
377 if (new_owner)
378 set_bit(portnum, &ehci->companion_ports);
379 else
380 clear_bit(portnum, &ehci->companion_ports);
381 set_owner(ehci, portnum, new_owner);
Alan Stern57e06c12007-01-16 11:59:45 -0500382 return count;
383}
Tony Jones5a3201b2007-09-11 14:07:31 -0700384static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500385
386static inline void create_companion_file(struct ehci_hcd *ehci)
387{
388 int i;
389
390 /* with integrated TT there is no companion! */
391 if (!ehci_is_TDI(ehci))
Tony Jones5a3201b2007-09-11 14:07:31 -0700392 i = device_create_file(ehci_to_hcd(ehci)->self.dev,
393 &dev_attr_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500394}
395
396static inline void remove_companion_file(struct ehci_hcd *ehci)
397{
398 /* with integrated TT there is no companion! */
399 if (!ehci_is_TDI(ehci))
Tony Jones5a3201b2007-09-11 14:07:31 -0700400 device_remove_file(ehci_to_hcd(ehci)->self.dev,
401 &dev_attr_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500402}
403
404
405/*-------------------------------------------------------------------------*/
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static int check_reset_complete (
408 struct ehci_hcd *ehci,
409 int index,
Alan Sterne6316562007-01-16 11:58:00 -0500410 u32 __iomem *status_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int port_status
412) {
David Brownellcd4cdc92008-01-24 12:39:43 -0800413 if (!(port_status & PORT_CONNECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return port_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* if reset finished and it's still not enabled -- handoff */
417 if (!(port_status & PORT_PE)) {
418
419 /* with integrated TT, there's nobody to hand it to! */
420 if (ehci_is_TDI(ehci)) {
421 ehci_dbg (ehci,
422 "Failed to enable port %d on root hub TT\n",
423 index+1);
424 return port_status;
425 }
426
427 ehci_dbg (ehci, "port %d full speed --> companion\n",
428 index + 1);
429
430 // what happens if HCS_N_CC(params) == 0 ?
431 port_status |= PORT_OWNER;
David Brownell10f65242005-08-31 10:55:38 -0700432 port_status &= ~PORT_RWC_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500433 ehci_writel(ehci, port_status, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 } else
436 ehci_dbg (ehci, "port %d high speed\n", index + 1);
437
438 return port_status;
439}
440
441/*-------------------------------------------------------------------------*/
442
443
444/* build "status change" packet (one or two bytes) from HC registers */
445
446static int
447ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
448{
449 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
450 u32 temp, status = 0;
David Brownell93f1a472006-11-16 23:34:58 -0800451 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int ports, i, retval = 1;
453 unsigned long flags;
454
455 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
456 if (!HC_IS_RUNNING(hcd->state))
457 return 0;
458
459 /* init status to no-changes */
460 buf [0] = 0;
461 ports = HCS_N_PORTS (ehci->hcs_params);
462 if (ports > 7) {
463 buf [1] = 0;
464 retval++;
465 }
David Brownell53bd6a62006-08-30 14:50:06 -0700466
David Brownell93f1a472006-11-16 23:34:58 -0800467 /* Some boards (mostly VIA?) report bogus overcurrent indications,
468 * causing massive log spam unless we completely ignore them. It
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200469 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
David Brownell93f1a472006-11-16 23:34:58 -0800470 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
471 * PORT_POWER; that's surprising, but maybe within-spec.
472 */
473 if (!ignore_oc)
474 mask = PORT_CSC | PORT_PEC | PORT_OCC;
475 else
476 mask = PORT_CSC | PORT_PEC;
477 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* no hub change reports (bit 0) for now (power, ...) */
480
481 /* port N changes (bit N)? */
482 spin_lock_irqsave (&ehci->lock, flags);
483 for (i = 0; i < ports; i++) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100484 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern625b5c92007-01-16 11:58:47 -0500485
486 /*
487 * Return status information even for ports with OWNER set.
488 * Otherwise khubd wouldn't see the disconnect event when a
489 * high-speed device is switched over to the companion
490 * controller by the user.
491 */
492
David Brownell93f1a472006-11-16 23:34:58 -0800493 if ((temp & mask) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 || ((temp & PORT_RESUME) != 0
Alan Stern629e4422007-01-22 16:08:53 -0500495 && time_after_eq(jiffies,
496 ehci->reset_done[i]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (i < 7)
498 buf [0] |= 1 << (i + 1);
499 else
500 buf [1] |= 1 << (i - 7);
501 status = STS_PCD;
502 }
503 }
504 /* FIXME autosuspend idle root hubs */
505 spin_unlock_irqrestore (&ehci->lock, flags);
506 return status ? retval : 0;
507}
508
509/*-------------------------------------------------------------------------*/
510
511static void
512ehci_hub_descriptor (
513 struct ehci_hcd *ehci,
514 struct usb_hub_descriptor *desc
515) {
516 int ports = HCS_N_PORTS (ehci->hcs_params);
517 u16 temp;
518
519 desc->bDescriptorType = 0x29;
520 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
521 desc->bHubContrCurrent = 0;
522
523 desc->bNbrPorts = ports;
524 temp = 1 + (ports / 8);
525 desc->bDescLength = 7 + 2 * temp;
526
527 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
528 memset (&desc->bitmap [0], 0, temp);
529 memset (&desc->bitmap [temp], 0xff, temp);
530
531 temp = 0x0008; /* per-port overcurrent reporting */
532 if (HCS_PPC (ehci->hcs_params))
533 temp |= 0x0001; /* per-port power control */
David Brownell56c1e262005-04-09 09:00:29 -0700534 else
535 temp |= 0x0002; /* no power switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536#if 0
537// re-enable when we support USB_PORT_FEAT_INDICATOR below.
538 if (HCS_INDICATOR (ehci->hcs_params))
539 temp |= 0x0080; /* per-port indicators (LEDs) */
540#endif
541 desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
542}
543
544/*-------------------------------------------------------------------------*/
545
David Brownell53bd6a62006-08-30 14:50:06 -0700546#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548static int ehci_hub_control (
549 struct usb_hcd *hcd,
550 u16 typeReq,
551 u16 wValue,
552 u16 wIndex,
553 char *buf,
554 u16 wLength
555) {
556 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
557 int ports = HCS_N_PORTS (ehci->hcs_params);
Alan Stern383975d2007-05-04 11:52:40 -0400558 u32 __iomem *status_reg = &ehci->regs->port_status[
559 (wIndex & 0xff) - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 u32 temp, status;
561 unsigned long flags;
562 int retval = 0;
David Brownellf0d7f272006-11-16 23:56:15 -0800563 unsigned selector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /*
566 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
567 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
568 * (track current state ourselves) ... blink for diagnostics,
569 * power, "this is the one", etc. EHCI spec supports this.
570 */
571
572 spin_lock_irqsave (&ehci->lock, flags);
573 switch (typeReq) {
574 case ClearHubFeature:
575 switch (wValue) {
576 case C_HUB_LOCAL_POWER:
577 case C_HUB_OVER_CURRENT:
578 /* no hub-wide feature/status flags */
579 break;
580 default:
581 goto error;
582 }
583 break;
584 case ClearPortFeature:
585 if (!wIndex || wIndex > ports)
586 goto error;
587 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500588 temp = ehci_readl(ehci, status_reg);
Alan Stern625b5c92007-01-16 11:58:47 -0500589
590 /*
591 * Even if OWNER is set, so the port is owned by the
592 * companion controller, khubd needs to be able to clear
593 * the port-change status bits (especially
594 * USB_PORT_FEAT_C_CONNECTION).
595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 switch (wValue) {
598 case USB_PORT_FEAT_ENABLE:
Alan Sterne6316562007-01-16 11:58:00 -0500599 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
601 case USB_PORT_FEAT_C_ENABLE:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100602 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
Alan Sterne6316562007-01-16 11:58:00 -0500603 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
605 case USB_PORT_FEAT_SUSPEND:
606 if (temp & PORT_RESET)
607 goto error;
David Brownellf8aeb3b2006-01-20 13:55:14 -0800608 if (ehci->no_selective_suspend)
609 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (temp & PORT_SUSPEND) {
611 if ((temp & PORT_PE) == 0)
612 goto error;
613 /* resume signaling for 20 msec */
David Brownell10f65242005-08-31 10:55:38 -0700614 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100615 ehci_writel(ehci, temp | PORT_RESUME,
Alan Sterne6316562007-01-16 11:58:00 -0500616 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ehci->reset_done [wIndex] = jiffies
618 + msecs_to_jiffies (20);
619 }
620 break;
621 case USB_PORT_FEAT_C_SUSPEND:
622 /* we auto-clear this feature */
623 break;
624 case USB_PORT_FEAT_POWER:
625 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100626 ehci_writel(ehci,
627 temp & ~(PORT_RWC_BITS | PORT_POWER),
Alan Sterne6316562007-01-16 11:58:00 -0500628 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 break;
630 case USB_PORT_FEAT_C_CONNECTION:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100631 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
Alan Sterne6316562007-01-16 11:58:00 -0500632 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
634 case USB_PORT_FEAT_C_OVER_CURRENT:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100635 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
Alan Sterne6316562007-01-16 11:58:00 -0500636 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
638 case USB_PORT_FEAT_C_RESET:
639 /* GetPortStatus clears reset */
640 break;
641 default:
642 goto error;
643 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100644 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646 case GetHubDescriptor:
647 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
648 buf);
649 break;
650 case GetHubStatus:
651 /* no hub-wide feature/status flags */
652 memset (buf, 0, 4);
653 //cpu_to_le32s ((u32 *) buf);
654 break;
655 case GetPortStatus:
656 if (!wIndex || wIndex > ports)
657 goto error;
658 wIndex--;
659 status = 0;
Alan Sterne6316562007-01-16 11:58:00 -0500660 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 // wPortChange bits
663 if (temp & PORT_CSC)
664 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
665 if (temp & PORT_PEC)
666 status |= 1 << USB_PORT_FEAT_C_ENABLE;
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700667
668 if ((temp & PORT_OCC) && !ignore_oc){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
670
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700671 /*
672 * Hubs should disable port power on over-current.
673 * However, not all EHCI implementations do this
674 * automatically, even if they _do_ support per-port
675 * power switching; they're allowed to just limit the
676 * current. khubd will turn the power back on.
677 */
678 if (HCS_PPC (ehci->hcs_params)){
679 ehci_writel(ehci,
680 temp & ~(PORT_RWC_BITS | PORT_POWER),
681 status_reg);
682 }
683 }
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* whoever resumes must GetPortStatus to complete it!! */
Alan Stern629e4422007-01-22 16:08:53 -0500686 if (temp & PORT_RESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Alan Stern629e4422007-01-22 16:08:53 -0500688 /* Remote Wakeup received? */
689 if (!ehci->reset_done[wIndex]) {
690 /* resume signaling for 20 msec */
691 ehci->reset_done[wIndex] = jiffies
692 + msecs_to_jiffies(20);
693 /* check the port again */
694 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
695 ehci->reset_done[wIndex]);
696 }
697
698 /* resume completed? */
699 else if (time_after_eq(jiffies,
700 ehci->reset_done[wIndex])) {
701 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
702 ehci->reset_done[wIndex] = 0;
703
704 /* stop resume signaling */
705 temp = ehci_readl(ehci, status_reg);
706 ehci_writel(ehci,
Alan Sterne6316562007-01-16 11:58:00 -0500707 temp & ~(PORT_RWC_BITS | PORT_RESUME),
708 status_reg);
Alan Stern629e4422007-01-22 16:08:53 -0500709 retval = handshake(ehci, status_reg,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100710 PORT_RESUME, 0, 2000 /* 2msec */);
Alan Stern629e4422007-01-22 16:08:53 -0500711 if (retval != 0) {
712 ehci_err(ehci,
713 "port %d resume error %d\n",
714 wIndex + 1, retval);
715 goto error;
716 }
717 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
721 /* whoever resets must GetPortStatus to complete it!! */
722 if ((temp & PORT_RESET)
Alan Stern629e4422007-01-22 16:08:53 -0500723 && time_after_eq(jiffies,
724 ehci->reset_done[wIndex])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 status |= 1 << USB_PORT_FEAT_C_RESET;
726 ehci->reset_done [wIndex] = 0;
727
728 /* force reset to complete */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100729 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
Alan Sterne6316562007-01-16 11:58:00 -0500730 status_reg);
David Brownellc22fa3a2005-06-13 07:15:28 -0700731 /* REVISIT: some hardware needs 550+ usec to clear
732 * this bit; seems too long to spin routinely...
733 */
Alan Sterne6316562007-01-16 11:58:00 -0500734 retval = handshake(ehci, status_reg,
David Brownellc22fa3a2005-06-13 07:15:28 -0700735 PORT_RESET, 0, 750);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (retval != 0) {
737 ehci_err (ehci, "port %d reset error %d\n",
738 wIndex + 1, retval);
739 goto error;
740 }
741
742 /* see what we found out */
Alan Sterne6316562007-01-16 11:58:00 -0500743 temp = check_reset_complete (ehci, wIndex, status_reg,
744 ehci_readl(ehci, status_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
Alan Stern57e06c12007-01-16 11:59:45 -0500747 /* transfer dedicated ports to the companion hc */
748 if ((temp & PORT_CONNECT) &&
749 test_bit(wIndex, &ehci->companion_ports)) {
750 temp &= ~PORT_RWC_BITS;
751 temp |= PORT_OWNER;
752 ehci_writel(ehci, temp, status_reg);
753 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
754 temp = ehci_readl(ehci, status_reg);
755 }
756
Alan Stern625b5c92007-01-16 11:58:47 -0500757 /*
758 * Even if OWNER is set, there's no harm letting khubd
759 * see the wPortStatus values (they should all be 0 except
760 * for PORT_POWER anyway).
761 */
762
763 if (temp & PORT_CONNECT) {
764 status |= 1 << USB_PORT_FEAT_CONNECTION;
765 // status may be from integrated TT
766 status |= ehci_port_speed(ehci, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Alan Stern625b5c92007-01-16 11:58:47 -0500768 if (temp & PORT_PE)
769 status |= 1 << USB_PORT_FEAT_ENABLE;
770 if (temp & (PORT_SUSPEND|PORT_RESUME))
771 status |= 1 << USB_PORT_FEAT_SUSPEND;
772 if (temp & PORT_OC)
773 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
774 if (temp & PORT_RESET)
775 status |= 1 << USB_PORT_FEAT_RESET;
776 if (temp & PORT_POWER)
777 status |= 1 << USB_PORT_FEAT_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779#ifndef EHCI_VERBOSE_DEBUG
780 if (status & ~0xffff) /* only if wPortChange is interesting */
781#endif
782 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
Max Dmitrichenko64543652007-03-06 02:45:01 +0300783 put_unaligned(cpu_to_le32 (status), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 break;
785 case SetHubFeature:
786 switch (wValue) {
787 case C_HUB_LOCAL_POWER:
788 case C_HUB_OVER_CURRENT:
789 /* no hub-wide feature/status flags */
790 break;
791 default:
792 goto error;
793 }
794 break;
795 case SetPortFeature:
David Brownellf0d7f272006-11-16 23:56:15 -0800796 selector = wIndex >> 8;
797 wIndex &= 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!wIndex || wIndex > ports)
799 goto error;
800 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500801 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (temp & PORT_OWNER)
803 break;
804
David Brownell10f65242005-08-31 10:55:38 -0700805 temp &= ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 switch (wValue) {
807 case USB_PORT_FEAT_SUSPEND:
David Brownellf8aeb3b2006-01-20 13:55:14 -0800808 if (ehci->no_selective_suspend)
809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if ((temp & PORT_PE) == 0
811 || (temp & PORT_RESET) != 0)
812 goto error;
David Brownell2c1c3c42005-11-07 15:24:46 -0800813 if (device_may_wakeup(&hcd->self.root_hub->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 temp |= PORT_WAKE_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500815 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 break;
817 case USB_PORT_FEAT_POWER:
818 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100819 ehci_writel(ehci, temp | PORT_POWER,
Alan Sterne6316562007-01-16 11:58:00 -0500820 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 break;
822 case USB_PORT_FEAT_RESET:
823 if (temp & PORT_RESUME)
824 goto error;
825 /* line status bits may report this as low speed,
826 * which can be fine if this root hub has a
827 * transaction translator built in.
828 */
829 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
830 && !ehci_is_TDI(ehci)
831 && PORT_USB11 (temp)) {
832 ehci_dbg (ehci,
833 "port %d low speed --> companion\n",
834 wIndex + 1);
835 temp |= PORT_OWNER;
836 } else {
837 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
838 temp |= PORT_RESET;
839 temp &= ~PORT_PE;
840
841 /*
842 * caller must wait, then call GetPortStatus
843 * usb 2.0 spec says 50 ms resets on root
844 */
845 ehci->reset_done [wIndex] = jiffies
846 + msecs_to_jiffies (50);
847 }
Alan Sterne6316562007-01-16 11:58:00 -0500848 ehci_writel(ehci, temp, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 break;
David Brownellf0d7f272006-11-16 23:56:15 -0800850
851 /* For downstream facing ports (these): one hub port is put
852 * into test mode according to USB2 11.24.2.13, then the hub
853 * must be reset (which for root hub now means rmmod+modprobe,
854 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
855 * about the EHCI-specific stuff.
856 */
857 case USB_PORT_FEAT_TEST:
858 if (!selector || selector > 5)
859 goto error;
860 ehci_quiesce(ehci);
861 ehci_halt(ehci);
862 temp |= selector << 16;
Alan Sterne6316562007-01-16 11:58:00 -0500863 ehci_writel(ehci, temp, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -0800864 break;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 default:
867 goto error;
868 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100869 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 break;
871
872 default:
873error:
874 /* "stall" on error */
875 retval = -EPIPE;
876 }
877 spin_unlock_irqrestore (&ehci->lock, flags);
878 return retval;
879}
Balaji Rao90da0962007-11-22 01:58:14 +0530880
881static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
882{
883 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
884
885 if (ehci_is_TDI(ehci))
886 return;
887 set_owner(ehci, --portnum, PORT_OWNER);
888}
889