blob: 21ac3781f21abe7bc0d628593e6174c122d87c36 [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 Sternfeccc302008-03-03 15:15:59 -050031#ifdef CONFIG_PM
Alan Stern383975d2007-05-04 11:52:40 -040032
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
Alan Stern0c0382e2005-10-13 17:08:02 -0400107static int ehci_bus_suspend (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
110 int port;
Alan Stern8c033562006-11-09 14:42:16 -0500111 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Alan Stern8c774fe2007-02-01 16:09:59 -0500113 ehci_dbg(ehci, "suspend root hub\n");
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (time_before (jiffies, ehci->next_statechange))
116 msleep(5);
Alan Sternf8fa7572008-01-10 16:43:15 -0500117 del_timer_sync(&ehci->watchdog);
118 del_timer_sync(&ehci->iaa_watchdog);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 port = HCS_N_PORTS (ehci->hcs_params);
121 spin_lock_irq (&ehci->lock);
122
123 /* stop schedules, clean any completed work */
124 if (HC_IS_RUNNING(hcd->state)) {
125 ehci_quiesce (ehci);
126 hcd->state = HC_STATE_QUIESCING;
127 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100128 ehci->command = ehci_readl(ehci, &ehci->regs->command);
David Howells7d12e782006-10-05 14:55:46 +0100129 ehci_work(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Alan Stern8c033562006-11-09 14:42:16 -0500131 /* Unlike other USB host controller types, EHCI doesn't have
132 * any notion of "global" or bus-wide suspend. The driver has
133 * to manually suspend all the active unsuspended ports, and
134 * then manually resume them in the bus_resume() routine.
135 */
136 ehci->bus_suspended = 0;
Alan Stern383975d2007-05-04 11:52:40 -0400137 ehci->owned_ports = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 while (port--) {
139 u32 __iomem *reg = &ehci->regs->port_status [port];
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100140 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 u32 t2 = t1;
142
Alan Stern8c033562006-11-09 14:42:16 -0500143 /* keep track of which ports we suspend */
Alan Stern383975d2007-05-04 11:52:40 -0400144 if (t1 & PORT_OWNER)
145 set_bit(port, &ehci->owned_ports);
146 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 t2 |= PORT_SUSPEND;
Alan Stern8c033562006-11-09 14:42:16 -0500148 set_bit(port, &ehci->bus_suspended);
149 }
150
151 /* enable remote wakeup on all ports */
David Brownell2c1c3c42005-11-07 15:24:46 -0800152 if (device_may_wakeup(&hcd->self.root_hub->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
154 else
155 t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
156
157 if (t1 != t2) {
158 ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
159 port + 1, t1, t2);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100160 ehci_writel(ehci, t2, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162 }
163
Alan Sterncd930c92008-01-10 11:14:53 -0500164 /* Apparently some devices need a >= 1-uframe delay here */
165 if (ehci->bus_suspended)
166 udelay(150);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* turn off now-idle HC */
169 ehci_halt (ehci);
170 hcd->state = HC_STATE_SUSPENDED;
171
David Brownellcdc647a2008-04-02 13:40:20 -0700172 if (ehci->reclaim)
173 end_unlink_async(ehci);
174
Alan Stern8c033562006-11-09 14:42:16 -0500175 /* allow remote wakeup */
176 mask = INTR_MASK;
177 if (!device_may_wakeup(&hcd->self.root_hub->dev))
178 mask &= ~STS_PCD;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100179 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
180 ehci_readl(ehci, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
183 spin_unlock_irq (&ehci->lock);
184 return 0;
185}
186
187
188/* caller has locked the root hub, and should reset/reinit on error */
Alan Stern0c0382e2005-10-13 17:08:02 -0400189static int ehci_bus_resume (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
192 u32 temp;
Alan Stern383975d2007-05-04 11:52:40 -0400193 u32 power_okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (time_before (jiffies, ehci->next_statechange))
197 msleep(5);
198 spin_lock_irq (&ehci->lock);
Alan Sterncfa59da2007-06-21 16:25:35 -0400199 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
200 spin_unlock_irq(&ehci->lock);
201 return -ESHUTDOWN;
202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
David Brownellf03c17f2005-11-23 15:45:28 -0800204 /* Ideally and we've got a real resume here, and no port's power
205 * was lost. (For PCI, that means Vaux was maintained.) But we
206 * could instead be restoring a swsusp snapshot -- so that BIOS was
207 * the last user of the controller, not reset/pm hardware keeping
208 * state we gave to it.
209 */
Alan Stern383975d2007-05-04 11:52:40 -0400210 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
211 ehci_dbg(ehci, "resume root hub%s\n",
212 power_okay ? "" : " after power loss");
David Brownellf03c17f2005-11-23 15:45:28 -0800213
Alan Stern8c033562006-11-09 14:42:16 -0500214 /* at least some APM implementations will try to deliver
215 * IRQs right away, so delay them until we're ready.
216 */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100217 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
Alan Stern8c033562006-11-09 14:42:16 -0500218
219 /* re-init operational registers */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100220 ehci_writel(ehci, 0, &ehci->regs->segment);
221 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
222 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 /* restore CMD_RUN, framelist size, and irq threshold */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100225 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Alan Sterne198a312007-03-15 15:54:30 -0400227 /* Some controller/firmware combinations need a delay during which
228 * they set up the port statuses. See Bugzilla #8190. */
229 mdelay(8);
230
Alan Stern8c033562006-11-09 14:42:16 -0500231 /* manually resume the ports we suspended during bus_suspend() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 i = HCS_N_PORTS (ehci->hcs_params);
233 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100234 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
David Brownell10f65242005-08-31 10:55:38 -0700235 temp &= ~(PORT_RWC_BITS
236 | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
Alan Stern8c033562006-11-09 14:42:16 -0500237 if (test_bit(i, &ehci->bus_suspended) &&
238 (temp & PORT_SUSPEND)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
240 temp |= PORT_RESUME;
241 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100242 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 i = HCS_N_PORTS (ehci->hcs_params);
245 mdelay (20);
246 while (i--) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100247 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500248 if (test_bit(i, &ehci->bus_suspended) &&
249 (temp & PORT_SUSPEND)) {
250 temp &= ~(PORT_RWC_BITS | PORT_RESUME);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100251 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
Alan Stern8c033562006-11-09 14:42:16 -0500252 ehci_vdbg (ehci, "resumed port %d\n", i + 1);
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100255 (void) ehci_readl(ehci, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 /* maybe re-activate the schedule(s) */
258 temp = 0;
259 if (ehci->async->qh_next.qh)
260 temp |= CMD_ASE;
261 if (ehci->periodic_sched)
262 temp |= CMD_PSE;
263 if (temp) {
264 ehci->command |= temp;
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100265 ehci_writel(ehci, ehci->command, &ehci->regs->command);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
268 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
269 hcd->state = HC_STATE_RUNNING;
270
271 /* Now we can safely re-enable irqs */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100272 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 spin_unlock_irq (&ehci->lock);
Alan Stern3bb1af52008-03-03 15:15:36 -0500275 ehci_handover_companion_ports(ehci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 0;
277}
278
279#else
280
Alan Stern0c0382e2005-10-13 17:08:02 -0400281#define ehci_bus_suspend NULL
282#define ehci_bus_resume NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284#endif /* CONFIG_PM */
285
286/*-------------------------------------------------------------------------*/
287
Alan Stern57e06c12007-01-16 11:59:45 -0500288/* Display the ports dedicated to the companion controller */
Tony Jones5a3201b2007-09-11 14:07:31 -0700289static ssize_t show_companion(struct device *dev,
290 struct device_attribute *attr,
291 char *buf)
Alan Stern57e06c12007-01-16 11:59:45 -0500292{
293 struct ehci_hcd *ehci;
294 int nports, index, n;
295 int count = PAGE_SIZE;
296 char *ptr = buf;
297
Tony Jones5a3201b2007-09-11 14:07:31 -0700298 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
Alan Stern57e06c12007-01-16 11:59:45 -0500299 nports = HCS_N_PORTS(ehci->hcs_params);
300
301 for (index = 0; index < nports; ++index) {
302 if (test_bit(index, &ehci->companion_ports)) {
303 n = scnprintf(ptr, count, "%d\n", index + 1);
304 ptr += n;
305 count -= n;
306 }
307 }
308 return ptr - buf;
309}
310
311/*
Balaji Rao90da0962007-11-22 01:58:14 +0530312 * Sets the owner of a port
Alan Stern57e06c12007-01-16 11:59:45 -0500313 */
Balaji Rao90da0962007-11-22 01:58:14 +0530314static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
Alan Stern57e06c12007-01-16 11:59:45 -0500315{
Alan Stern57e06c12007-01-16 11:59:45 -0500316 u32 __iomem *status_reg;
317 u32 port_status;
Balaji Rao90da0962007-11-22 01:58:14 +0530318 int try;
Alan Stern57e06c12007-01-16 11:59:45 -0500319
Balaji Rao90da0962007-11-22 01:58:14 +0530320 status_reg = &ehci->regs->port_status[portnum];
Alan Stern57e06c12007-01-16 11:59:45 -0500321
322 /*
323 * The controller won't set the OWNER bit if the port is
324 * enabled, so this loop will sometimes require at least two
325 * iterations: one to disable the port and one to set OWNER.
326 */
Alan Stern57e06c12007-01-16 11:59:45 -0500327 for (try = 4; try > 0; --try) {
328 spin_lock_irq(&ehci->lock);
329 port_status = ehci_readl(ehci, status_reg);
330 if ((port_status & PORT_OWNER) == new_owner
331 || (port_status & (PORT_OWNER | PORT_CONNECT))
332 == 0)
333 try = 0;
334 else {
335 port_status ^= PORT_OWNER;
336 port_status &= ~(PORT_PE | PORT_RWC_BITS);
337 ehci_writel(ehci, port_status, status_reg);
338 }
339 spin_unlock_irq(&ehci->lock);
340 if (try > 1)
341 msleep(5);
342 }
Balaji Rao90da0962007-11-22 01:58:14 +0530343}
344
345/*
346 * Dedicate or undedicate a port to the companion controller.
347 * Syntax is "[-]portnum", where a leading '-' sign means
348 * return control of the port to the EHCI controller.
349 */
350static ssize_t store_companion(struct device *dev,
351 struct device_attribute *attr,
352 const char *buf, size_t count)
353{
354 struct ehci_hcd *ehci;
355 int portnum, new_owner;
356
357 ehci = hcd_to_ehci(bus_to_hcd(dev_get_drvdata(dev)));
358 new_owner = PORT_OWNER; /* Owned by companion */
359 if (sscanf(buf, "%d", &portnum) != 1)
360 return -EINVAL;
361 if (portnum < 0) {
362 portnum = - portnum;
363 new_owner = 0; /* Owned by EHCI */
364 }
365 if (portnum <= 0 || portnum > HCS_N_PORTS(ehci->hcs_params))
366 return -ENOENT;
367 portnum--;
368 if (new_owner)
369 set_bit(portnum, &ehci->companion_ports);
370 else
371 clear_bit(portnum, &ehci->companion_ports);
372 set_owner(ehci, portnum, new_owner);
Alan Stern57e06c12007-01-16 11:59:45 -0500373 return count;
374}
Tony Jones5a3201b2007-09-11 14:07:31 -0700375static DEVICE_ATTR(companion, 0644, show_companion, store_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500376
377static inline void create_companion_file(struct ehci_hcd *ehci)
378{
379 int i;
380
381 /* with integrated TT there is no companion! */
382 if (!ehci_is_TDI(ehci))
Tony Jones5a3201b2007-09-11 14:07:31 -0700383 i = device_create_file(ehci_to_hcd(ehci)->self.dev,
384 &dev_attr_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500385}
386
387static inline void remove_companion_file(struct ehci_hcd *ehci)
388{
389 /* with integrated TT there is no companion! */
390 if (!ehci_is_TDI(ehci))
Tony Jones5a3201b2007-09-11 14:07:31 -0700391 device_remove_file(ehci_to_hcd(ehci)->self.dev,
392 &dev_attr_companion);
Alan Stern57e06c12007-01-16 11:59:45 -0500393}
394
395
396/*-------------------------------------------------------------------------*/
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static int check_reset_complete (
399 struct ehci_hcd *ehci,
400 int index,
Alan Sterne6316562007-01-16 11:58:00 -0500401 u32 __iomem *status_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int port_status
403) {
David Brownellcd4cdc92008-01-24 12:39:43 -0800404 if (!(port_status & PORT_CONNECT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return port_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /* if reset finished and it's still not enabled -- handoff */
408 if (!(port_status & PORT_PE)) {
409
410 /* with integrated TT, there's nobody to hand it to! */
411 if (ehci_is_TDI(ehci)) {
412 ehci_dbg (ehci,
413 "Failed to enable port %d on root hub TT\n",
414 index+1);
415 return port_status;
416 }
417
418 ehci_dbg (ehci, "port %d full speed --> companion\n",
419 index + 1);
420
421 // what happens if HCS_N_CC(params) == 0 ?
422 port_status |= PORT_OWNER;
David Brownell10f65242005-08-31 10:55:38 -0700423 port_status &= ~PORT_RWC_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500424 ehci_writel(ehci, port_status, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 } else
427 ehci_dbg (ehci, "port %d high speed\n", index + 1);
428
429 return port_status;
430}
431
432/*-------------------------------------------------------------------------*/
433
434
435/* build "status change" packet (one or two bytes) from HC registers */
436
437static int
438ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
439{
440 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
441 u32 temp, status = 0;
David Brownell93f1a472006-11-16 23:34:58 -0800442 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 int ports, i, retval = 1;
444 unsigned long flags;
445
446 /* if !USB_SUSPEND, root hub timers won't get shut down ... */
447 if (!HC_IS_RUNNING(hcd->state))
448 return 0;
449
450 /* init status to no-changes */
451 buf [0] = 0;
452 ports = HCS_N_PORTS (ehci->hcs_params);
453 if (ports > 7) {
454 buf [1] = 0;
455 retval++;
456 }
David Brownell53bd6a62006-08-30 14:50:06 -0700457
David Brownell93f1a472006-11-16 23:34:58 -0800458 /* Some boards (mostly VIA?) report bogus overcurrent indications,
459 * causing massive log spam unless we completely ignore them. It
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200460 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
David Brownell93f1a472006-11-16 23:34:58 -0800461 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
462 * PORT_POWER; that's surprising, but maybe within-spec.
463 */
464 if (!ignore_oc)
465 mask = PORT_CSC | PORT_PEC | PORT_OCC;
466 else
467 mask = PORT_CSC | PORT_PEC;
468 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 /* no hub change reports (bit 0) for now (power, ...) */
471
472 /* port N changes (bit N)? */
473 spin_lock_irqsave (&ehci->lock, flags);
474 for (i = 0; i < ports; i++) {
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100475 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
Alan Stern625b5c92007-01-16 11:58:47 -0500476
477 /*
478 * Return status information even for ports with OWNER set.
479 * Otherwise khubd wouldn't see the disconnect event when a
480 * high-speed device is switched over to the companion
481 * controller by the user.
482 */
483
David Brownell93f1a472006-11-16 23:34:58 -0800484 if ((temp & mask) != 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 || ((temp & PORT_RESUME) != 0
Alan Stern629e4422007-01-22 16:08:53 -0500486 && time_after_eq(jiffies,
487 ehci->reset_done[i]))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (i < 7)
489 buf [0] |= 1 << (i + 1);
490 else
491 buf [1] |= 1 << (i - 7);
492 status = STS_PCD;
493 }
494 }
495 /* FIXME autosuspend idle root hubs */
496 spin_unlock_irqrestore (&ehci->lock, flags);
497 return status ? retval : 0;
498}
499
500/*-------------------------------------------------------------------------*/
501
502static void
503ehci_hub_descriptor (
504 struct ehci_hcd *ehci,
505 struct usb_hub_descriptor *desc
506) {
507 int ports = HCS_N_PORTS (ehci->hcs_params);
508 u16 temp;
509
510 desc->bDescriptorType = 0x29;
511 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
512 desc->bHubContrCurrent = 0;
513
514 desc->bNbrPorts = ports;
515 temp = 1 + (ports / 8);
516 desc->bDescLength = 7 + 2 * temp;
517
518 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
519 memset (&desc->bitmap [0], 0, temp);
520 memset (&desc->bitmap [temp], 0xff, temp);
521
522 temp = 0x0008; /* per-port overcurrent reporting */
523 if (HCS_PPC (ehci->hcs_params))
524 temp |= 0x0001; /* per-port power control */
David Brownell56c1e262005-04-09 09:00:29 -0700525 else
526 temp |= 0x0002; /* no power switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#if 0
528// re-enable when we support USB_PORT_FEAT_INDICATOR below.
529 if (HCS_INDICATOR (ehci->hcs_params))
530 temp |= 0x0080; /* per-port indicators (LEDs) */
531#endif
532 desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
533}
534
535/*-------------------------------------------------------------------------*/
536
David Brownell53bd6a62006-08-30 14:50:06 -0700537#define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539static int ehci_hub_control (
540 struct usb_hcd *hcd,
541 u16 typeReq,
542 u16 wValue,
543 u16 wIndex,
544 char *buf,
545 u16 wLength
546) {
547 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
548 int ports = HCS_N_PORTS (ehci->hcs_params);
Alan Stern383975d2007-05-04 11:52:40 -0400549 u32 __iomem *status_reg = &ehci->regs->port_status[
550 (wIndex & 0xff) - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 u32 temp, status;
552 unsigned long flags;
553 int retval = 0;
David Brownellf0d7f272006-11-16 23:56:15 -0800554 unsigned selector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /*
557 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
558 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
559 * (track current state ourselves) ... blink for diagnostics,
560 * power, "this is the one", etc. EHCI spec supports this.
561 */
562
563 spin_lock_irqsave (&ehci->lock, flags);
564 switch (typeReq) {
565 case ClearHubFeature:
566 switch (wValue) {
567 case C_HUB_LOCAL_POWER:
568 case C_HUB_OVER_CURRENT:
569 /* no hub-wide feature/status flags */
570 break;
571 default:
572 goto error;
573 }
574 break;
575 case ClearPortFeature:
576 if (!wIndex || wIndex > ports)
577 goto error;
578 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500579 temp = ehci_readl(ehci, status_reg);
Alan Stern625b5c92007-01-16 11:58:47 -0500580
581 /*
582 * Even if OWNER is set, so the port is owned by the
583 * companion controller, khubd needs to be able to clear
584 * the port-change status bits (especially
585 * USB_PORT_FEAT_C_CONNECTION).
586 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 switch (wValue) {
589 case USB_PORT_FEAT_ENABLE:
Alan Sterne6316562007-01-16 11:58:00 -0500590 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 break;
592 case USB_PORT_FEAT_C_ENABLE:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100593 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_PEC,
Alan Sterne6316562007-01-16 11:58:00 -0500594 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
596 case USB_PORT_FEAT_SUSPEND:
597 if (temp & PORT_RESET)
598 goto error;
David Brownellf8aeb3b2006-01-20 13:55:14 -0800599 if (ehci->no_selective_suspend)
600 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (temp & PORT_SUSPEND) {
602 if ((temp & PORT_PE) == 0)
603 goto error;
604 /* resume signaling for 20 msec */
David Brownell10f65242005-08-31 10:55:38 -0700605 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100606 ehci_writel(ehci, temp | PORT_RESUME,
Alan Sterne6316562007-01-16 11:58:00 -0500607 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 ehci->reset_done [wIndex] = jiffies
609 + msecs_to_jiffies (20);
610 }
611 break;
612 case USB_PORT_FEAT_C_SUSPEND:
613 /* we auto-clear this feature */
614 break;
615 case USB_PORT_FEAT_POWER:
616 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100617 ehci_writel(ehci,
618 temp & ~(PORT_RWC_BITS | PORT_POWER),
Alan Sterne6316562007-01-16 11:58:00 -0500619 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
621 case USB_PORT_FEAT_C_CONNECTION:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100622 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_CSC,
Alan Sterne6316562007-01-16 11:58:00 -0500623 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
625 case USB_PORT_FEAT_C_OVER_CURRENT:
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100626 ehci_writel(ehci, (temp & ~PORT_RWC_BITS) | PORT_OCC,
Alan Sterne6316562007-01-16 11:58:00 -0500627 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 break;
629 case USB_PORT_FEAT_C_RESET:
630 /* GetPortStatus clears reset */
631 break;
632 default:
633 goto error;
634 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100635 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
637 case GetHubDescriptor:
638 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
639 buf);
640 break;
641 case GetHubStatus:
642 /* no hub-wide feature/status flags */
643 memset (buf, 0, 4);
644 //cpu_to_le32s ((u32 *) buf);
645 break;
646 case GetPortStatus:
647 if (!wIndex || wIndex > ports)
648 goto error;
649 wIndex--;
650 status = 0;
Alan Sterne6316562007-01-16 11:58:00 -0500651 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 // wPortChange bits
654 if (temp & PORT_CSC)
655 status |= 1 << USB_PORT_FEAT_C_CONNECTION;
656 if (temp & PORT_PEC)
657 status |= 1 << USB_PORT_FEAT_C_ENABLE;
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700658
659 if ((temp & PORT_OCC) && !ignore_oc){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
661
Christian Engelmayer756aa6b2007-05-30 11:04:48 -0700662 /*
663 * Hubs should disable port power on over-current.
664 * However, not all EHCI implementations do this
665 * automatically, even if they _do_ support per-port
666 * power switching; they're allowed to just limit the
667 * current. khubd will turn the power back on.
668 */
669 if (HCS_PPC (ehci->hcs_params)){
670 ehci_writel(ehci,
671 temp & ~(PORT_RWC_BITS | PORT_POWER),
672 status_reg);
673 }
674 }
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* whoever resumes must GetPortStatus to complete it!! */
Alan Stern629e4422007-01-22 16:08:53 -0500677 if (temp & PORT_RESUME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Alan Stern629e4422007-01-22 16:08:53 -0500679 /* Remote Wakeup received? */
680 if (!ehci->reset_done[wIndex]) {
681 /* resume signaling for 20 msec */
682 ehci->reset_done[wIndex] = jiffies
683 + msecs_to_jiffies(20);
684 /* check the port again */
685 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
686 ehci->reset_done[wIndex]);
687 }
688
689 /* resume completed? */
690 else if (time_after_eq(jiffies,
691 ehci->reset_done[wIndex])) {
692 status |= 1 << USB_PORT_FEAT_C_SUSPEND;
693 ehci->reset_done[wIndex] = 0;
694
695 /* stop resume signaling */
696 temp = ehci_readl(ehci, status_reg);
697 ehci_writel(ehci,
Alan Sterne6316562007-01-16 11:58:00 -0500698 temp & ~(PORT_RWC_BITS | PORT_RESUME),
699 status_reg);
Alan Stern629e4422007-01-22 16:08:53 -0500700 retval = handshake(ehci, status_reg,
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100701 PORT_RESUME, 0, 2000 /* 2msec */);
Alan Stern629e4422007-01-22 16:08:53 -0500702 if (retval != 0) {
703 ehci_err(ehci,
704 "port %d resume error %d\n",
705 wIndex + 1, retval);
706 goto error;
707 }
708 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
712 /* whoever resets must GetPortStatus to complete it!! */
713 if ((temp & PORT_RESET)
Alan Stern629e4422007-01-22 16:08:53 -0500714 && time_after_eq(jiffies,
715 ehci->reset_done[wIndex])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 status |= 1 << USB_PORT_FEAT_C_RESET;
717 ehci->reset_done [wIndex] = 0;
718
719 /* force reset to complete */
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100720 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
Alan Sterne6316562007-01-16 11:58:00 -0500721 status_reg);
David Brownellc22fa3a2005-06-13 07:15:28 -0700722 /* REVISIT: some hardware needs 550+ usec to clear
723 * this bit; seems too long to spin routinely...
724 */
Alan Sterne6316562007-01-16 11:58:00 -0500725 retval = handshake(ehci, status_reg,
David Brownellc22fa3a2005-06-13 07:15:28 -0700726 PORT_RESET, 0, 750);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (retval != 0) {
728 ehci_err (ehci, "port %d reset error %d\n",
729 wIndex + 1, retval);
730 goto error;
731 }
732
733 /* see what we found out */
Alan Sterne6316562007-01-16 11:58:00 -0500734 temp = check_reset_complete (ehci, wIndex, status_reg,
735 ehci_readl(ehci, status_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
Alan Stern57e06c12007-01-16 11:59:45 -0500738 /* transfer dedicated ports to the companion hc */
739 if ((temp & PORT_CONNECT) &&
740 test_bit(wIndex, &ehci->companion_ports)) {
741 temp &= ~PORT_RWC_BITS;
742 temp |= PORT_OWNER;
743 ehci_writel(ehci, temp, status_reg);
744 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
745 temp = ehci_readl(ehci, status_reg);
746 }
747
Alan Stern625b5c92007-01-16 11:58:47 -0500748 /*
749 * Even if OWNER is set, there's no harm letting khubd
750 * see the wPortStatus values (they should all be 0 except
751 * for PORT_POWER anyway).
752 */
753
754 if (temp & PORT_CONNECT) {
755 status |= 1 << USB_PORT_FEAT_CONNECTION;
756 // status may be from integrated TT
757 status |= ehci_port_speed(ehci, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Alan Stern625b5c92007-01-16 11:58:47 -0500759 if (temp & PORT_PE)
760 status |= 1 << USB_PORT_FEAT_ENABLE;
761 if (temp & (PORT_SUSPEND|PORT_RESUME))
762 status |= 1 << USB_PORT_FEAT_SUSPEND;
763 if (temp & PORT_OC)
764 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
765 if (temp & PORT_RESET)
766 status |= 1 << USB_PORT_FEAT_RESET;
767 if (temp & PORT_POWER)
768 status |= 1 << USB_PORT_FEAT_POWER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
David Brownell9776afc2008-02-01 11:42:05 -0800770#ifndef VERBOSE_DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (status & ~0xffff) /* only if wPortChange is interesting */
772#endif
773 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
Max Dmitrichenko64543652007-03-06 02:45:01 +0300774 put_unaligned(cpu_to_le32 (status), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
776 case SetHubFeature:
777 switch (wValue) {
778 case C_HUB_LOCAL_POWER:
779 case C_HUB_OVER_CURRENT:
780 /* no hub-wide feature/status flags */
781 break;
782 default:
783 goto error;
784 }
785 break;
786 case SetPortFeature:
David Brownellf0d7f272006-11-16 23:56:15 -0800787 selector = wIndex >> 8;
788 wIndex &= 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (!wIndex || wIndex > ports)
790 goto error;
791 wIndex--;
Alan Sterne6316562007-01-16 11:58:00 -0500792 temp = ehci_readl(ehci, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (temp & PORT_OWNER)
794 break;
795
David Brownell10f65242005-08-31 10:55:38 -0700796 temp &= ~PORT_RWC_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 switch (wValue) {
798 case USB_PORT_FEAT_SUSPEND:
David Brownellf8aeb3b2006-01-20 13:55:14 -0800799 if (ehci->no_selective_suspend)
800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if ((temp & PORT_PE) == 0
802 || (temp & PORT_RESET) != 0)
803 goto error;
David Brownell2c1c3c42005-11-07 15:24:46 -0800804 if (device_may_wakeup(&hcd->self.root_hub->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 temp |= PORT_WAKE_BITS;
Alan Sterne6316562007-01-16 11:58:00 -0500806 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 break;
808 case USB_PORT_FEAT_POWER:
809 if (HCS_PPC (ehci->hcs_params))
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100810 ehci_writel(ehci, temp | PORT_POWER,
Alan Sterne6316562007-01-16 11:58:00 -0500811 status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813 case USB_PORT_FEAT_RESET:
814 if (temp & PORT_RESUME)
815 goto error;
816 /* line status bits may report this as low speed,
817 * which can be fine if this root hub has a
818 * transaction translator built in.
819 */
820 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
821 && !ehci_is_TDI(ehci)
822 && PORT_USB11 (temp)) {
823 ehci_dbg (ehci,
824 "port %d low speed --> companion\n",
825 wIndex + 1);
826 temp |= PORT_OWNER;
827 } else {
828 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
829 temp |= PORT_RESET;
830 temp &= ~PORT_PE;
831
832 /*
833 * caller must wait, then call GetPortStatus
834 * usb 2.0 spec says 50 ms resets on root
835 */
836 ehci->reset_done [wIndex] = jiffies
837 + msecs_to_jiffies (50);
838 }
Alan Sterne6316562007-01-16 11:58:00 -0500839 ehci_writel(ehci, temp, status_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 break;
David Brownellf0d7f272006-11-16 23:56:15 -0800841
842 /* For downstream facing ports (these): one hub port is put
843 * into test mode according to USB2 11.24.2.13, then the hub
844 * must be reset (which for root hub now means rmmod+modprobe,
845 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
846 * about the EHCI-specific stuff.
847 */
848 case USB_PORT_FEAT_TEST:
849 if (!selector || selector > 5)
850 goto error;
851 ehci_quiesce(ehci);
852 ehci_halt(ehci);
853 temp |= selector << 16;
Alan Sterne6316562007-01-16 11:58:00 -0500854 ehci_writel(ehci, temp, status_reg);
David Brownellf0d7f272006-11-16 23:56:15 -0800855 break;
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 default:
858 goto error;
859 }
Benjamin Herrenschmidt083522d2006-12-15 06:54:08 +1100860 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862
863 default:
864error:
865 /* "stall" on error */
866 retval = -EPIPE;
867 }
868 spin_unlock_irqrestore (&ehci->lock, flags);
869 return retval;
870}
Balaji Rao90da0962007-11-22 01:58:14 +0530871
872static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
873{
874 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
875
876 if (ehci_is_TDI(ehci))
877 return;
878 set_owner(ehci, --portnum, PORT_OWNER);
879}
880