]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/usb/host/ehci-hub.c
spi: s3c64xx: Let spi core handle validating transfer length
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / host / ehci-hub.c
1 /*
2 * Copyright (C) 2001-2004 by David Brownell
3 *
4 * 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 #include <linux/usb/otg.h>
31
32 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
33
34 #ifdef CONFIG_PM
35
36 static int ehci_hub_control(
37 struct usb_hcd *hcd,
38 u16 typeReq,
39 u16 wValue,
40 u16 wIndex,
41 char *buf,
42 u16 wLength
43 );
44
45 static int persist_enabled_on_companion(struct usb_device *udev, void *unused)
46 {
47 return !udev->maxchild && udev->persist_enabled &&
48 udev->bus->root_hub->speed < USB_SPEED_HIGH;
49 }
50
51 /* After a power loss, ports that were owned by the companion must be
52 * reset so that the companion can still own them.
53 */
54 static void ehci_handover_companion_ports(struct ehci_hcd *ehci)
55 {
56 u32 __iomem *reg;
57 u32 status;
58 int port;
59 __le32 buf;
60 struct usb_hcd *hcd = ehci_to_hcd(ehci);
61
62 if (!ehci->owned_ports)
63 return;
64
65 /*
66 * USB 1.1 devices are mostly HIDs, which don't need to persist across
67 * suspends. If we ensure that none of our companion's devices have
68 * persist_enabled (by looking through all USB 1.1 buses in the system),
69 * we can skip this and avoid slowing resume down. Devices without
70 * persist will just get reenumerated shortly after resume anyway.
71 */
72 if (!usb_for_each_dev(NULL, persist_enabled_on_companion))
73 return;
74
75 /* Make sure the ports are powered */
76 port = HCS_N_PORTS(ehci->hcs_params);
77 while (port--) {
78 if (test_bit(port, &ehci->owned_ports)) {
79 reg = &ehci->regs->port_status[port];
80 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
81 if (!(status & PORT_POWER)) {
82 status |= PORT_POWER;
83 ehci_writel(ehci, status, reg);
84 }
85 }
86 }
87
88 /* Give the connections some time to appear */
89 msleep(20);
90
91 spin_lock_irq(&ehci->lock);
92 port = HCS_N_PORTS(ehci->hcs_params);
93 while (port--) {
94 if (test_bit(port, &ehci->owned_ports)) {
95 reg = &ehci->regs->port_status[port];
96 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
97
98 /* Port already owned by companion? */
99 if (status & PORT_OWNER)
100 clear_bit(port, &ehci->owned_ports);
101 else if (test_bit(port, &ehci->companion_ports))
102 ehci_writel(ehci, status & ~PORT_PE, reg);
103 else {
104 spin_unlock_irq(&ehci->lock);
105 ehci_hub_control(hcd, SetPortFeature,
106 USB_PORT_FEAT_RESET, port + 1,
107 NULL, 0);
108 spin_lock_irq(&ehci->lock);
109 }
110 }
111 }
112 spin_unlock_irq(&ehci->lock);
113
114 if (!ehci->owned_ports)
115 return;
116 msleep(90); /* Wait for resets to complete */
117
118 spin_lock_irq(&ehci->lock);
119 port = HCS_N_PORTS(ehci->hcs_params);
120 while (port--) {
121 if (test_bit(port, &ehci->owned_ports)) {
122 spin_unlock_irq(&ehci->lock);
123 ehci_hub_control(hcd, GetPortStatus,
124 0, port + 1,
125 (char *) &buf, sizeof(buf));
126 spin_lock_irq(&ehci->lock);
127
128 /* The companion should now own the port,
129 * but if something went wrong the port must not
130 * remain enabled.
131 */
132 reg = &ehci->regs->port_status[port];
133 status = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
134 if (status & PORT_OWNER)
135 ehci_writel(ehci, status | PORT_CSC, reg);
136 else {
137 ehci_dbg(ehci, "failed handover port %d: %x\n",
138 port + 1, status);
139 ehci_writel(ehci, status & ~PORT_PE, reg);
140 }
141 }
142 }
143
144 ehci->owned_ports = 0;
145 spin_unlock_irq(&ehci->lock);
146 }
147
148 static int ehci_port_change(struct ehci_hcd *ehci)
149 {
150 int i = HCS_N_PORTS(ehci->hcs_params);
151
152 /* First check if the controller indicates a change event */
153
154 if (ehci_readl(ehci, &ehci->regs->status) & STS_PCD)
155 return 1;
156
157 /*
158 * Not all controllers appear to update this while going from D3 to D0,
159 * so check the individual port status registers as well
160 */
161
162 while (i--)
163 if (ehci_readl(ehci, &ehci->regs->port_status[i]) & PORT_CSC)
164 return 1;
165
166 return 0;
167 }
168
169 static void ehci_adjust_port_wakeup_flags(struct ehci_hcd *ehci,
170 bool suspending, bool do_wakeup)
171 {
172 int port;
173 u32 temp;
174
175 /* If remote wakeup is enabled for the root hub but disabled
176 * for the controller, we must adjust all the port wakeup flags
177 * when the controller is suspended or resumed. In all other
178 * cases they don't need to be changed.
179 */
180 if (!ehci_to_hcd(ehci)->self.root_hub->do_remote_wakeup || do_wakeup)
181 return;
182
183 spin_lock_irq(&ehci->lock);
184
185 /* clear phy low-power mode before changing wakeup flags */
186 if (ehci->has_tdi_phy_lpm) {
187 port = HCS_N_PORTS(ehci->hcs_params);
188 while (port--) {
189 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
190
191 temp = ehci_readl(ehci, hostpc_reg);
192 ehci_writel(ehci, temp & ~HOSTPC_PHCD, hostpc_reg);
193 }
194 spin_unlock_irq(&ehci->lock);
195 msleep(5);
196 spin_lock_irq(&ehci->lock);
197 }
198
199 port = HCS_N_PORTS(ehci->hcs_params);
200 while (port--) {
201 u32 __iomem *reg = &ehci->regs->port_status[port];
202 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
203 u32 t2 = t1 & ~PORT_WAKE_BITS;
204
205 /* If we are suspending the controller, clear the flags.
206 * If we are resuming the controller, set the wakeup flags.
207 */
208 if (!suspending) {
209 if (t1 & PORT_CONNECT)
210 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
211 else
212 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
213 }
214 ehci_writel(ehci, t2, reg);
215 }
216
217 /* enter phy low-power mode again */
218 if (ehci->has_tdi_phy_lpm) {
219 port = HCS_N_PORTS(ehci->hcs_params);
220 while (port--) {
221 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
222
223 temp = ehci_readl(ehci, hostpc_reg);
224 ehci_writel(ehci, temp | HOSTPC_PHCD, hostpc_reg);
225 }
226 }
227
228 /* Does the root hub have a port wakeup pending? */
229 if (!suspending && ehci_port_change(ehci))
230 usb_hcd_resume_root_hub(ehci_to_hcd(ehci));
231
232 spin_unlock_irq(&ehci->lock);
233 }
234
235 static int ehci_bus_suspend (struct usb_hcd *hcd)
236 {
237 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
238 int port;
239 int mask;
240 int changed;
241
242 ehci_dbg(ehci, "suspend root hub\n");
243
244 if (time_before (jiffies, ehci->next_statechange))
245 msleep(5);
246
247 /* stop the schedules */
248 ehci_quiesce(ehci);
249
250 spin_lock_irq (&ehci->lock);
251 if (ehci->rh_state < EHCI_RH_RUNNING)
252 goto done;
253
254 /* Once the controller is stopped, port resumes that are already
255 * in progress won't complete. Hence if remote wakeup is enabled
256 * for the root hub and any ports are in the middle of a resume or
257 * remote wakeup, we must fail the suspend.
258 */
259 if (hcd->self.root_hub->do_remote_wakeup) {
260 if (ehci->resuming_ports) {
261 spin_unlock_irq(&ehci->lock);
262 ehci_dbg(ehci, "suspend failed because a port is resuming\n");
263 return -EBUSY;
264 }
265 }
266
267 /* Unlike other USB host controller types, EHCI doesn't have
268 * any notion of "global" or bus-wide suspend. The driver has
269 * to manually suspend all the active unsuspended ports, and
270 * then manually resume them in the bus_resume() routine.
271 */
272 ehci->bus_suspended = 0;
273 ehci->owned_ports = 0;
274 changed = 0;
275 port = HCS_N_PORTS(ehci->hcs_params);
276 while (port--) {
277 u32 __iomem *reg = &ehci->regs->port_status [port];
278 u32 t1 = ehci_readl(ehci, reg) & ~PORT_RWC_BITS;
279 u32 t2 = t1 & ~PORT_WAKE_BITS;
280
281 /* keep track of which ports we suspend */
282 if (t1 & PORT_OWNER)
283 set_bit(port, &ehci->owned_ports);
284 else if ((t1 & PORT_PE) && !(t1 & PORT_SUSPEND)) {
285 t2 |= PORT_SUSPEND;
286 set_bit(port, &ehci->bus_suspended);
287 }
288
289 /* enable remote wakeup on all ports, if told to do so */
290 if (hcd->self.root_hub->do_remote_wakeup) {
291 /* only enable appropriate wake bits, otherwise the
292 * hardware can not go phy low power mode. If a race
293 * condition happens here(connection change during bits
294 * set), the port change detection will finally fix it.
295 */
296 if (t1 & PORT_CONNECT)
297 t2 |= PORT_WKOC_E | PORT_WKDISC_E;
298 else
299 t2 |= PORT_WKOC_E | PORT_WKCONN_E;
300 }
301
302 if (t1 != t2) {
303 ehci_writel(ehci, t2, reg);
304 changed = 1;
305 }
306 }
307
308 if (changed && ehci->has_tdi_phy_lpm) {
309 spin_unlock_irq(&ehci->lock);
310 msleep(5); /* 5 ms for HCD to enter low-power mode */
311 spin_lock_irq(&ehci->lock);
312
313 port = HCS_N_PORTS(ehci->hcs_params);
314 while (port--) {
315 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[port];
316 u32 t3;
317
318 t3 = ehci_readl(ehci, hostpc_reg);
319 ehci_writel(ehci, t3 | HOSTPC_PHCD, hostpc_reg);
320 t3 = ehci_readl(ehci, hostpc_reg);
321 ehci_dbg(ehci, "Port %d phy low-power mode %s\n",
322 port, (t3 & HOSTPC_PHCD) ?
323 "succeeded" : "failed");
324 }
325 }
326 spin_unlock_irq(&ehci->lock);
327
328 /* Apparently some devices need a >= 1-uframe delay here */
329 if (ehci->bus_suspended)
330 udelay(150);
331
332 /* turn off now-idle HC */
333 ehci_halt (ehci);
334
335 spin_lock_irq(&ehci->lock);
336 if (ehci->enabled_hrtimer_events & BIT(EHCI_HRTIMER_POLL_DEAD))
337 ehci_handle_controller_death(ehci);
338 if (ehci->rh_state != EHCI_RH_RUNNING)
339 goto done;
340 ehci->rh_state = EHCI_RH_SUSPENDED;
341
342 end_unlink_async(ehci);
343 unlink_empty_async_suspended(ehci);
344 ehci_handle_start_intr_unlinks(ehci);
345 ehci_handle_intr_unlinks(ehci);
346 end_free_itds(ehci);
347
348 /* allow remote wakeup */
349 mask = INTR_MASK;
350 if (!hcd->self.root_hub->do_remote_wakeup)
351 mask &= ~STS_PCD;
352 ehci_writel(ehci, mask, &ehci->regs->intr_enable);
353 ehci_readl(ehci, &ehci->regs->intr_enable);
354
355 done:
356 ehci->next_statechange = jiffies + msecs_to_jiffies(10);
357 ehci->enabled_hrtimer_events = 0;
358 ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
359 spin_unlock_irq (&ehci->lock);
360
361 hrtimer_cancel(&ehci->hrtimer);
362 return 0;
363 }
364
365
366 /* caller has locked the root hub, and should reset/reinit on error */
367 static int ehci_bus_resume (struct usb_hcd *hcd)
368 {
369 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
370 u32 temp;
371 u32 power_okay;
372 int i;
373 unsigned long resume_needed = 0;
374
375 if (time_before (jiffies, ehci->next_statechange))
376 msleep(5);
377 spin_lock_irq (&ehci->lock);
378 if (!HCD_HW_ACCESSIBLE(hcd) || ehci->shutdown)
379 goto shutdown;
380
381 if (unlikely(ehci->debug)) {
382 if (!dbgp_reset_prep(hcd))
383 ehci->debug = NULL;
384 else
385 dbgp_external_startup(hcd);
386 }
387
388 /* Ideally and we've got a real resume here, and no port's power
389 * was lost. (For PCI, that means Vaux was maintained.) But we
390 * could instead be restoring a swsusp snapshot -- so that BIOS was
391 * the last user of the controller, not reset/pm hardware keeping
392 * state we gave to it.
393 */
394 power_okay = ehci_readl(ehci, &ehci->regs->intr_enable);
395 ehci_dbg(ehci, "resume root hub%s\n",
396 power_okay ? "" : " after power loss");
397
398 /* at least some APM implementations will try to deliver
399 * IRQs right away, so delay them until we're ready.
400 */
401 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
402
403 /* re-init operational registers */
404 ehci_writel(ehci, 0, &ehci->regs->segment);
405 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
406 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
407
408 /* restore CMD_RUN, framelist size, and irq threshold */
409 ehci->command |= CMD_RUN;
410 ehci_writel(ehci, ehci->command, &ehci->regs->command);
411 ehci->rh_state = EHCI_RH_RUNNING;
412
413 /*
414 * According to Bugzilla #8190, the port status for some controllers
415 * will be wrong without a delay. At their wrong status, the port
416 * is enabled, but not suspended neither resumed.
417 */
418 i = HCS_N_PORTS(ehci->hcs_params);
419 while (i--) {
420 temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
421 if ((temp & PORT_PE) &&
422 !(temp & (PORT_SUSPEND | PORT_RESUME))) {
423 ehci_dbg(ehci, "Port status(0x%x) is wrong\n", temp);
424 spin_unlock_irq(&ehci->lock);
425 msleep(8);
426 spin_lock_irq(&ehci->lock);
427 break;
428 }
429 }
430
431 if (ehci->shutdown)
432 goto shutdown;
433
434 /* clear phy low-power mode before resume */
435 if (ehci->bus_suspended && ehci->has_tdi_phy_lpm) {
436 i = HCS_N_PORTS(ehci->hcs_params);
437 while (i--) {
438 if (test_bit(i, &ehci->bus_suspended)) {
439 u32 __iomem *hostpc_reg =
440 &ehci->regs->hostpc[i];
441
442 temp = ehci_readl(ehci, hostpc_reg);
443 ehci_writel(ehci, temp & ~HOSTPC_PHCD,
444 hostpc_reg);
445 }
446 }
447 spin_unlock_irq(&ehci->lock);
448 msleep(5);
449 spin_lock_irq(&ehci->lock);
450 if (ehci->shutdown)
451 goto shutdown;
452 }
453
454 /* manually resume the ports we suspended during bus_suspend() */
455 i = HCS_N_PORTS (ehci->hcs_params);
456 while (i--) {
457 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
458 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
459 if (test_bit(i, &ehci->bus_suspended) &&
460 (temp & PORT_SUSPEND)) {
461 temp |= PORT_RESUME;
462 set_bit(i, &resume_needed);
463 }
464 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
465 }
466
467 /* msleep for 20ms only if code is trying to resume port */
468 if (resume_needed) {
469 spin_unlock_irq(&ehci->lock);
470 msleep(20);
471 spin_lock_irq(&ehci->lock);
472 if (ehci->shutdown)
473 goto shutdown;
474 }
475
476 i = HCS_N_PORTS (ehci->hcs_params);
477 while (i--) {
478 temp = ehci_readl(ehci, &ehci->regs->port_status [i]);
479 if (test_bit(i, &resume_needed)) {
480 temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
481 ehci_writel(ehci, temp, &ehci->regs->port_status [i]);
482 }
483 }
484
485 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
486 spin_unlock_irq(&ehci->lock);
487
488 ehci_handover_companion_ports(ehci);
489
490 /* Now we can safely re-enable irqs */
491 spin_lock_irq(&ehci->lock);
492 if (ehci->shutdown)
493 goto shutdown;
494 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
495 (void) ehci_readl(ehci, &ehci->regs->intr_enable);
496 spin_unlock_irq(&ehci->lock);
497
498 return 0;
499
500 shutdown:
501 spin_unlock_irq(&ehci->lock);
502 return -ESHUTDOWN;
503 }
504
505 #else
506
507 #define ehci_bus_suspend NULL
508 #define ehci_bus_resume NULL
509
510 #endif /* CONFIG_PM */
511
512 /*-------------------------------------------------------------------------*/
513
514 /*
515 * Sets the owner of a port
516 */
517 static void set_owner(struct ehci_hcd *ehci, int portnum, int new_owner)
518 {
519 u32 __iomem *status_reg;
520 u32 port_status;
521 int try;
522
523 status_reg = &ehci->regs->port_status[portnum];
524
525 /*
526 * The controller won't set the OWNER bit if the port is
527 * enabled, so this loop will sometimes require at least two
528 * iterations: one to disable the port and one to set OWNER.
529 */
530 for (try = 4; try > 0; --try) {
531 spin_lock_irq(&ehci->lock);
532 port_status = ehci_readl(ehci, status_reg);
533 if ((port_status & PORT_OWNER) == new_owner
534 || (port_status & (PORT_OWNER | PORT_CONNECT))
535 == 0)
536 try = 0;
537 else {
538 port_status ^= PORT_OWNER;
539 port_status &= ~(PORT_PE | PORT_RWC_BITS);
540 ehci_writel(ehci, port_status, status_reg);
541 }
542 spin_unlock_irq(&ehci->lock);
543 if (try > 1)
544 msleep(5);
545 }
546 }
547
548 /*-------------------------------------------------------------------------*/
549
550 static int check_reset_complete (
551 struct ehci_hcd *ehci,
552 int index,
553 u32 __iomem *status_reg,
554 int port_status
555 ) {
556 if (!(port_status & PORT_CONNECT))
557 return port_status;
558
559 /* if reset finished and it's still not enabled -- handoff */
560 if (!(port_status & PORT_PE)) {
561
562 /* with integrated TT, there's nobody to hand it to! */
563 if (ehci_is_TDI(ehci)) {
564 ehci_dbg (ehci,
565 "Failed to enable port %d on root hub TT\n",
566 index+1);
567 return port_status;
568 }
569
570 ehci_dbg (ehci, "port %d full speed --> companion\n",
571 index + 1);
572
573 // what happens if HCS_N_CC(params) == 0 ?
574 port_status |= PORT_OWNER;
575 port_status &= ~PORT_RWC_BITS;
576 ehci_writel(ehci, port_status, status_reg);
577
578 /* ensure 440EPX ohci controller state is operational */
579 if (ehci->has_amcc_usb23)
580 set_ohci_hcfs(ehci, 1);
581 } else {
582 ehci_dbg(ehci, "port %d reset complete, port enabled\n",
583 index + 1);
584 /* ensure 440EPx ohci controller state is suspended */
585 if (ehci->has_amcc_usb23)
586 set_ohci_hcfs(ehci, 0);
587 }
588
589 return port_status;
590 }
591
592 /*-------------------------------------------------------------------------*/
593
594
595 /* build "status change" packet (one or two bytes) from HC registers */
596
597 static int
598 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
599 {
600 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
601 u32 temp, status;
602 u32 mask;
603 int ports, i, retval = 1;
604 unsigned long flags;
605 u32 ppcd = ~0;
606
607 /* init status to no-changes */
608 buf [0] = 0;
609 ports = HCS_N_PORTS (ehci->hcs_params);
610 if (ports > 7) {
611 buf [1] = 0;
612 retval++;
613 }
614
615 /* Inform the core about resumes-in-progress by returning
616 * a non-zero value even if there are no status changes.
617 */
618 status = ehci->resuming_ports;
619
620 /* Some boards (mostly VIA?) report bogus overcurrent indications,
621 * causing massive log spam unless we completely ignore them. It
622 * may be relevant that VIA VT8235 controllers, where PORT_POWER is
623 * always set, seem to clear PORT_OCC and PORT_CSC when writing to
624 * PORT_POWER; that's surprising, but maybe within-spec.
625 */
626 if (!ignore_oc)
627 mask = PORT_CSC | PORT_PEC | PORT_OCC;
628 else
629 mask = PORT_CSC | PORT_PEC;
630 // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
631
632 /* no hub change reports (bit 0) for now (power, ...) */
633
634 /* port N changes (bit N)? */
635 spin_lock_irqsave (&ehci->lock, flags);
636
637 /* get per-port change detect bits */
638 if (ehci->has_ppcd)
639 ppcd = ehci_readl(ehci, &ehci->regs->status) >> 16;
640
641 for (i = 0; i < ports; i++) {
642 /* leverage per-port change bits feature */
643 if (ppcd & (1 << i))
644 temp = ehci_readl(ehci, &ehci->regs->port_status[i]);
645 else
646 temp = 0;
647
648 /*
649 * Return status information even for ports with OWNER set.
650 * Otherwise khubd wouldn't see the disconnect event when a
651 * high-speed device is switched over to the companion
652 * controller by the user.
653 */
654
655 if ((temp & mask) != 0 || test_bit(i, &ehci->port_c_suspend)
656 || (ehci->reset_done[i] && time_after_eq(
657 jiffies, ehci->reset_done[i]))) {
658 if (i < 7)
659 buf [0] |= 1 << (i + 1);
660 else
661 buf [1] |= 1 << (i - 7);
662 status = STS_PCD;
663 }
664 }
665
666 /* If a resume is in progress, make sure it can finish */
667 if (ehci->resuming_ports)
668 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(25));
669
670 spin_unlock_irqrestore (&ehci->lock, flags);
671 return status ? retval : 0;
672 }
673
674 /*-------------------------------------------------------------------------*/
675
676 static void
677 ehci_hub_descriptor (
678 struct ehci_hcd *ehci,
679 struct usb_hub_descriptor *desc
680 ) {
681 int ports = HCS_N_PORTS (ehci->hcs_params);
682 u16 temp;
683
684 desc->bDescriptorType = 0x29;
685 desc->bPwrOn2PwrGood = 10; /* ehci 1.0, 2.3.9 says 20ms max */
686 desc->bHubContrCurrent = 0;
687
688 desc->bNbrPorts = ports;
689 temp = 1 + (ports / 8);
690 desc->bDescLength = 7 + 2 * temp;
691
692 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
693 memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
694 memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
695
696 temp = 0x0008; /* per-port overcurrent reporting */
697 if (HCS_PPC (ehci->hcs_params))
698 temp |= 0x0001; /* per-port power control */
699 else
700 temp |= 0x0002; /* no power switching */
701 #if 0
702 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
703 if (HCS_INDICATOR (ehci->hcs_params))
704 temp |= 0x0080; /* per-port indicators (LEDs) */
705 #endif
706 desc->wHubCharacteristics = cpu_to_le16(temp);
707 }
708
709 /*-------------------------------------------------------------------------*/
710 #ifdef CONFIG_USB_HCD_TEST_MODE
711
712 #define EHSET_TEST_SINGLE_STEP_SET_FEATURE 0x06
713
714 static void usb_ehset_completion(struct urb *urb)
715 {
716 struct completion *done = urb->context;
717
718 complete(done);
719 }
720 static int submit_single_step_set_feature(
721 struct usb_hcd *hcd,
722 struct urb *urb,
723 int is_setup
724 );
725
726 /*
727 * Allocate and initialize a control URB. This request will be used by the
728 * EHSET SINGLE_STEP_SET_FEATURE test in which the DATA and STATUS stages
729 * of the GetDescriptor request are sent 15 seconds after the SETUP stage.
730 * Return NULL if failed.
731 */
732 static struct urb *request_single_step_set_feature_urb(
733 struct usb_device *udev,
734 void *dr,
735 void *buf,
736 struct completion *done
737 ) {
738 struct urb *urb;
739 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
740 struct usb_host_endpoint *ep;
741
742 urb = usb_alloc_urb(0, GFP_KERNEL);
743 if (!urb)
744 return NULL;
745
746 urb->pipe = usb_rcvctrlpipe(udev, 0);
747 ep = (usb_pipein(urb->pipe) ? udev->ep_in : udev->ep_out)
748 [usb_pipeendpoint(urb->pipe)];
749 if (!ep) {
750 usb_free_urb(urb);
751 return NULL;
752 }
753
754 urb->ep = ep;
755 urb->dev = udev;
756 urb->setup_packet = (void *)dr;
757 urb->transfer_buffer = buf;
758 urb->transfer_buffer_length = USB_DT_DEVICE_SIZE;
759 urb->complete = usb_ehset_completion;
760 urb->status = -EINPROGRESS;
761 urb->actual_length = 0;
762 urb->transfer_flags = URB_DIR_IN;
763 usb_get_urb(urb);
764 atomic_inc(&urb->use_count);
765 atomic_inc(&urb->dev->urbnum);
766 urb->setup_dma = dma_map_single(
767 hcd->self.controller,
768 urb->setup_packet,
769 sizeof(struct usb_ctrlrequest),
770 DMA_TO_DEVICE);
771 urb->transfer_dma = dma_map_single(
772 hcd->self.controller,
773 urb->transfer_buffer,
774 urb->transfer_buffer_length,
775 DMA_FROM_DEVICE);
776 urb->context = done;
777 return urb;
778 }
779
780 static int ehset_single_step_set_feature(struct usb_hcd *hcd, int port)
781 {
782 int retval = -ENOMEM;
783 struct usb_ctrlrequest *dr;
784 struct urb *urb;
785 struct usb_device *udev;
786 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
787 struct usb_device_descriptor *buf;
788 DECLARE_COMPLETION_ONSTACK(done);
789
790 /* Obtain udev of the rhub's child port */
791 udev = usb_hub_find_child(hcd->self.root_hub, port);
792 if (!udev) {
793 ehci_err(ehci, "No device attached to the RootHub\n");
794 return -ENODEV;
795 }
796 buf = kmalloc(USB_DT_DEVICE_SIZE, GFP_KERNEL);
797 if (!buf)
798 return -ENOMEM;
799
800 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
801 if (!dr) {
802 kfree(buf);
803 return -ENOMEM;
804 }
805
806 /* Fill Setup packet for GetDescriptor */
807 dr->bRequestType = USB_DIR_IN;
808 dr->bRequest = USB_REQ_GET_DESCRIPTOR;
809 dr->wValue = cpu_to_le16(USB_DT_DEVICE << 8);
810 dr->wIndex = 0;
811 dr->wLength = cpu_to_le16(USB_DT_DEVICE_SIZE);
812 urb = request_single_step_set_feature_urb(udev, dr, buf, &done);
813 if (!urb)
814 goto cleanup;
815
816 /* Submit just the SETUP stage */
817 retval = submit_single_step_set_feature(hcd, urb, 1);
818 if (retval)
819 goto out1;
820 if (!wait_for_completion_timeout(&done, msecs_to_jiffies(2000))) {
821 usb_kill_urb(urb);
822 retval = -ETIMEDOUT;
823 ehci_err(ehci, "%s SETUP stage timed out on ep0\n", __func__);
824 goto out1;
825 }
826 msleep(15 * 1000);
827
828 /* Complete remaining DATA and STATUS stages using the same URB */
829 urb->status = -EINPROGRESS;
830 usb_get_urb(urb);
831 atomic_inc(&urb->use_count);
832 atomic_inc(&urb->dev->urbnum);
833 retval = submit_single_step_set_feature(hcd, urb, 0);
834 if (!retval && !wait_for_completion_timeout(&done,
835 msecs_to_jiffies(2000))) {
836 usb_kill_urb(urb);
837 retval = -ETIMEDOUT;
838 ehci_err(ehci, "%s IN stage timed out on ep0\n", __func__);
839 }
840 out1:
841 usb_free_urb(urb);
842 cleanup:
843 kfree(dr);
844 kfree(buf);
845 return retval;
846 }
847 #endif /* CONFIG_USB_HCD_TEST_MODE */
848 /*-------------------------------------------------------------------------*/
849
850 static int ehci_hub_control (
851 struct usb_hcd *hcd,
852 u16 typeReq,
853 u16 wValue,
854 u16 wIndex,
855 char *buf,
856 u16 wLength
857 ) {
858 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
859 int ports = HCS_N_PORTS (ehci->hcs_params);
860 u32 __iomem *status_reg = &ehci->regs->port_status[
861 (wIndex & 0xff) - 1];
862 u32 __iomem *hostpc_reg = &ehci->regs->hostpc[(wIndex & 0xff) - 1];
863 u32 temp, temp1, status;
864 unsigned long flags;
865 int retval = 0;
866 unsigned selector;
867
868 /*
869 * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
870 * HCS_INDICATOR may say we can change LEDs to off/amber/green.
871 * (track current state ourselves) ... blink for diagnostics,
872 * power, "this is the one", etc. EHCI spec supports this.
873 */
874
875 spin_lock_irqsave (&ehci->lock, flags);
876 switch (typeReq) {
877 case ClearHubFeature:
878 switch (wValue) {
879 case C_HUB_LOCAL_POWER:
880 case C_HUB_OVER_CURRENT:
881 /* no hub-wide feature/status flags */
882 break;
883 default:
884 goto error;
885 }
886 break;
887 case ClearPortFeature:
888 if (!wIndex || wIndex > ports)
889 goto error;
890 wIndex--;
891 temp = ehci_readl(ehci, status_reg);
892 temp &= ~PORT_RWC_BITS;
893
894 /*
895 * Even if OWNER is set, so the port is owned by the
896 * companion controller, khubd needs to be able to clear
897 * the port-change status bits (especially
898 * USB_PORT_STAT_C_CONNECTION).
899 */
900
901 switch (wValue) {
902 case USB_PORT_FEAT_ENABLE:
903 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
904 break;
905 case USB_PORT_FEAT_C_ENABLE:
906 ehci_writel(ehci, temp | PORT_PEC, status_reg);
907 break;
908 case USB_PORT_FEAT_SUSPEND:
909 if (temp & PORT_RESET)
910 goto error;
911 if (ehci->no_selective_suspend)
912 break;
913 #ifdef CONFIG_USB_OTG
914 if ((hcd->self.otg_port == (wIndex + 1))
915 && hcd->self.b_hnp_enable) {
916 otg_start_hnp(hcd->phy->otg);
917 break;
918 }
919 #endif
920 if (!(temp & PORT_SUSPEND))
921 break;
922 if ((temp & PORT_PE) == 0)
923 goto error;
924
925 /* clear phy low-power mode before resume */
926 if (ehci->has_tdi_phy_lpm) {
927 temp1 = ehci_readl(ehci, hostpc_reg);
928 ehci_writel(ehci, temp1 & ~HOSTPC_PHCD,
929 hostpc_reg);
930 spin_unlock_irqrestore(&ehci->lock, flags);
931 msleep(5);/* wait to leave low-power mode */
932 spin_lock_irqsave(&ehci->lock, flags);
933 }
934 /* resume signaling for 20 msec */
935 temp &= ~PORT_WAKE_BITS;
936 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
937 ehci->reset_done[wIndex] = jiffies
938 + msecs_to_jiffies(20);
939 set_bit(wIndex, &ehci->resuming_ports);
940 usb_hcd_start_port_resume(&hcd->self, wIndex);
941 break;
942 case USB_PORT_FEAT_C_SUSPEND:
943 clear_bit(wIndex, &ehci->port_c_suspend);
944 break;
945 case USB_PORT_FEAT_POWER:
946 if (HCS_PPC (ehci->hcs_params))
947 ehci_writel(ehci, temp & ~PORT_POWER,
948 status_reg);
949 break;
950 case USB_PORT_FEAT_C_CONNECTION:
951 ehci_writel(ehci, temp | PORT_CSC, status_reg);
952 break;
953 case USB_PORT_FEAT_C_OVER_CURRENT:
954 ehci_writel(ehci, temp | PORT_OCC, status_reg);
955 break;
956 case USB_PORT_FEAT_C_RESET:
957 /* GetPortStatus clears reset */
958 break;
959 default:
960 goto error;
961 }
962 ehci_readl(ehci, &ehci->regs->command); /* unblock posted write */
963 break;
964 case GetHubDescriptor:
965 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
966 buf);
967 break;
968 case GetHubStatus:
969 /* no hub-wide feature/status flags */
970 memset (buf, 0, 4);
971 //cpu_to_le32s ((u32 *) buf);
972 break;
973 case GetPortStatus:
974 if (!wIndex || wIndex > ports)
975 goto error;
976 wIndex--;
977 status = 0;
978 temp = ehci_readl(ehci, status_reg);
979
980 // wPortChange bits
981 if (temp & PORT_CSC)
982 status |= USB_PORT_STAT_C_CONNECTION << 16;
983 if (temp & PORT_PEC)
984 status |= USB_PORT_STAT_C_ENABLE << 16;
985
986 if ((temp & PORT_OCC) && !ignore_oc){
987 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
988
989 /*
990 * Hubs should disable port power on over-current.
991 * However, not all EHCI implementations do this
992 * automatically, even if they _do_ support per-port
993 * power switching; they're allowed to just limit the
994 * current. khubd will turn the power back on.
995 */
996 if (((temp & PORT_OC) || (ehci->need_oc_pp_cycle))
997 && HCS_PPC(ehci->hcs_params)) {
998 ehci_writel(ehci,
999 temp & ~(PORT_RWC_BITS | PORT_POWER),
1000 status_reg);
1001 temp = ehci_readl(ehci, status_reg);
1002 }
1003 }
1004
1005 /* no reset or resume pending */
1006 if (!ehci->reset_done[wIndex]) {
1007
1008 /* Remote Wakeup received? */
1009 if (temp & PORT_RESUME) {
1010 /* resume signaling for 20 msec */
1011 ehci->reset_done[wIndex] = jiffies
1012 + msecs_to_jiffies(20);
1013 usb_hcd_start_port_resume(&hcd->self, wIndex);
1014 set_bit(wIndex, &ehci->resuming_ports);
1015 /* check the port again */
1016 mod_timer(&ehci_to_hcd(ehci)->rh_timer,
1017 ehci->reset_done[wIndex]);
1018 }
1019
1020 /* reset or resume not yet complete */
1021 } else if (!time_after_eq(jiffies, ehci->reset_done[wIndex])) {
1022 ; /* wait until it is complete */
1023
1024 /* resume completed */
1025 } else if (test_bit(wIndex, &ehci->resuming_ports)) {
1026 clear_bit(wIndex, &ehci->suspended_ports);
1027 set_bit(wIndex, &ehci->port_c_suspend);
1028 ehci->reset_done[wIndex] = 0;
1029 usb_hcd_end_port_resume(&hcd->self, wIndex);
1030
1031 /* stop resume signaling */
1032 temp &= ~(PORT_RWC_BITS | PORT_SUSPEND | PORT_RESUME);
1033 ehci_writel(ehci, temp, status_reg);
1034 clear_bit(wIndex, &ehci->resuming_ports);
1035 retval = ehci_handshake(ehci, status_reg,
1036 PORT_RESUME, 0, 2000 /* 2msec */);
1037 if (retval != 0) {
1038 ehci_err(ehci, "port %d resume error %d\n",
1039 wIndex + 1, retval);
1040 goto error;
1041 }
1042 temp = ehci_readl(ehci, status_reg);
1043
1044 /* whoever resets must GetPortStatus to complete it!! */
1045 } else {
1046 status |= USB_PORT_STAT_C_RESET << 16;
1047 ehci->reset_done [wIndex] = 0;
1048
1049 /* force reset to complete */
1050 ehci_writel(ehci, temp & ~(PORT_RWC_BITS | PORT_RESET),
1051 status_reg);
1052 /* REVISIT: some hardware needs 550+ usec to clear
1053 * this bit; seems too long to spin routinely...
1054 */
1055 retval = ehci_handshake(ehci, status_reg,
1056 PORT_RESET, 0, 1000);
1057 if (retval != 0) {
1058 ehci_err (ehci, "port %d reset error %d\n",
1059 wIndex + 1, retval);
1060 goto error;
1061 }
1062
1063 /* see what we found out */
1064 temp = check_reset_complete (ehci, wIndex, status_reg,
1065 ehci_readl(ehci, status_reg));
1066 }
1067
1068 /* transfer dedicated ports to the companion hc */
1069 if ((temp & PORT_CONNECT) &&
1070 test_bit(wIndex, &ehci->companion_ports)) {
1071 temp &= ~PORT_RWC_BITS;
1072 temp |= PORT_OWNER;
1073 ehci_writel(ehci, temp, status_reg);
1074 ehci_dbg(ehci, "port %d --> companion\n", wIndex + 1);
1075 temp = ehci_readl(ehci, status_reg);
1076 }
1077
1078 /*
1079 * Even if OWNER is set, there's no harm letting khubd
1080 * see the wPortStatus values (they should all be 0 except
1081 * for PORT_POWER anyway).
1082 */
1083
1084 if (temp & PORT_CONNECT) {
1085 status |= USB_PORT_STAT_CONNECTION;
1086 // status may be from integrated TT
1087 if (ehci->has_hostpc) {
1088 temp1 = ehci_readl(ehci, hostpc_reg);
1089 status |= ehci_port_speed(ehci, temp1);
1090 } else
1091 status |= ehci_port_speed(ehci, temp);
1092 }
1093 if (temp & PORT_PE)
1094 status |= USB_PORT_STAT_ENABLE;
1095
1096 /* maybe the port was unsuspended without our knowledge */
1097 if (temp & (PORT_SUSPEND|PORT_RESUME)) {
1098 status |= USB_PORT_STAT_SUSPEND;
1099 } else if (test_bit(wIndex, &ehci->suspended_ports)) {
1100 clear_bit(wIndex, &ehci->suspended_ports);
1101 clear_bit(wIndex, &ehci->resuming_ports);
1102 ehci->reset_done[wIndex] = 0;
1103 if (temp & PORT_PE)
1104 set_bit(wIndex, &ehci->port_c_suspend);
1105 usb_hcd_end_port_resume(&hcd->self, wIndex);
1106 }
1107
1108 if (temp & PORT_OC)
1109 status |= USB_PORT_STAT_OVERCURRENT;
1110 if (temp & PORT_RESET)
1111 status |= USB_PORT_STAT_RESET;
1112 if (temp & PORT_POWER)
1113 status |= USB_PORT_STAT_POWER;
1114 if (test_bit(wIndex, &ehci->port_c_suspend))
1115 status |= USB_PORT_STAT_C_SUSPEND << 16;
1116
1117 if (status & ~0xffff) /* only if wPortChange is interesting */
1118 dbg_port(ehci, "GetStatus", wIndex + 1, temp);
1119 put_unaligned_le32(status, buf);
1120 break;
1121 case SetHubFeature:
1122 switch (wValue) {
1123 case C_HUB_LOCAL_POWER:
1124 case C_HUB_OVER_CURRENT:
1125 /* no hub-wide feature/status flags */
1126 break;
1127 default:
1128 goto error;
1129 }
1130 break;
1131 case SetPortFeature:
1132 selector = wIndex >> 8;
1133 wIndex &= 0xff;
1134 if (unlikely(ehci->debug)) {
1135 /* If the debug port is active any port
1136 * feature requests should get denied */
1137 if (wIndex == HCS_DEBUG_PORT(ehci->hcs_params) &&
1138 (readl(&ehci->debug->control) & DBGP_ENABLED)) {
1139 retval = -ENODEV;
1140 goto error_exit;
1141 }
1142 }
1143 if (!wIndex || wIndex > ports)
1144 goto error;
1145 wIndex--;
1146 temp = ehci_readl(ehci, status_reg);
1147 if (temp & PORT_OWNER)
1148 break;
1149
1150 temp &= ~PORT_RWC_BITS;
1151 switch (wValue) {
1152 case USB_PORT_FEAT_SUSPEND:
1153 if (ehci->no_selective_suspend)
1154 break;
1155 if ((temp & PORT_PE) == 0
1156 || (temp & PORT_RESET) != 0)
1157 goto error;
1158
1159 /* After above check the port must be connected.
1160 * Set appropriate bit thus could put phy into low power
1161 * mode if we have tdi_phy_lpm feature
1162 */
1163 temp &= ~PORT_WKCONN_E;
1164 temp |= PORT_WKDISC_E | PORT_WKOC_E;
1165 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
1166 if (ehci->has_tdi_phy_lpm) {
1167 spin_unlock_irqrestore(&ehci->lock, flags);
1168 msleep(5);/* 5ms for HCD enter low pwr mode */
1169 spin_lock_irqsave(&ehci->lock, flags);
1170 temp1 = ehci_readl(ehci, hostpc_reg);
1171 ehci_writel(ehci, temp1 | HOSTPC_PHCD,
1172 hostpc_reg);
1173 temp1 = ehci_readl(ehci, hostpc_reg);
1174 ehci_dbg(ehci, "Port%d phy low pwr mode %s\n",
1175 wIndex, (temp1 & HOSTPC_PHCD) ?
1176 "succeeded" : "failed");
1177 }
1178 set_bit(wIndex, &ehci->suspended_ports);
1179 break;
1180 case USB_PORT_FEAT_POWER:
1181 if (HCS_PPC (ehci->hcs_params))
1182 ehci_writel(ehci, temp | PORT_POWER,
1183 status_reg);
1184 break;
1185 case USB_PORT_FEAT_RESET:
1186 if (temp & (PORT_SUSPEND|PORT_RESUME))
1187 goto error;
1188 /* line status bits may report this as low speed,
1189 * which can be fine if this root hub has a
1190 * transaction translator built in.
1191 */
1192 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
1193 && !ehci_is_TDI(ehci)
1194 && PORT_USB11 (temp)) {
1195 ehci_dbg (ehci,
1196 "port %d low speed --> companion\n",
1197 wIndex + 1);
1198 temp |= PORT_OWNER;
1199 } else {
1200 temp |= PORT_RESET;
1201 temp &= ~PORT_PE;
1202
1203 /*
1204 * caller must wait, then call GetPortStatus
1205 * usb 2.0 spec says 50 ms resets on root
1206 */
1207 ehci->reset_done [wIndex] = jiffies
1208 + msecs_to_jiffies (50);
1209 }
1210 ehci_writel(ehci, temp, status_reg);
1211 break;
1212
1213 /* For downstream facing ports (these): one hub port is put
1214 * into test mode according to USB2 11.24.2.13, then the hub
1215 * must be reset (which for root hub now means rmmod+modprobe,
1216 * or else system reboot). See EHCI 2.3.9 and 4.14 for info
1217 * about the EHCI-specific stuff.
1218 */
1219 case USB_PORT_FEAT_TEST:
1220 #ifdef CONFIG_USB_HCD_TEST_MODE
1221 if (selector == EHSET_TEST_SINGLE_STEP_SET_FEATURE) {
1222 spin_unlock_irqrestore(&ehci->lock, flags);
1223 retval = ehset_single_step_set_feature(hcd,
1224 wIndex);
1225 spin_lock_irqsave(&ehci->lock, flags);
1226 break;
1227 }
1228 #endif
1229 if (!selector || selector > 5)
1230 goto error;
1231 spin_unlock_irqrestore(&ehci->lock, flags);
1232 ehci_quiesce(ehci);
1233 spin_lock_irqsave(&ehci->lock, flags);
1234
1235 /* Put all enabled ports into suspend */
1236 while (ports--) {
1237 u32 __iomem *sreg =
1238 &ehci->regs->port_status[ports];
1239
1240 temp = ehci_readl(ehci, sreg) & ~PORT_RWC_BITS;
1241 if (temp & PORT_PE)
1242 ehci_writel(ehci, temp | PORT_SUSPEND,
1243 sreg);
1244 }
1245
1246 spin_unlock_irqrestore(&ehci->lock, flags);
1247 ehci_halt(ehci);
1248 spin_lock_irqsave(&ehci->lock, flags);
1249
1250 temp = ehci_readl(ehci, status_reg);
1251 temp |= selector << 16;
1252 ehci_writel(ehci, temp, status_reg);
1253 break;
1254
1255 default:
1256 goto error;
1257 }
1258 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
1259 break;
1260
1261 default:
1262 error:
1263 /* "stall" on error */
1264 retval = -EPIPE;
1265 }
1266 error_exit:
1267 spin_unlock_irqrestore (&ehci->lock, flags);
1268 return retval;
1269 }
1270
1271 static void ehci_relinquish_port(struct usb_hcd *hcd, int portnum)
1272 {
1273 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1274
1275 if (ehci_is_TDI(ehci))
1276 return;
1277 set_owner(ehci, --portnum, PORT_OWNER);
1278 }
1279
1280 static int ehci_port_handed_over(struct usb_hcd *hcd, int portnum)
1281 {
1282 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1283 u32 __iomem *reg;
1284
1285 if (ehci_is_TDI(ehci))
1286 return 0;
1287 reg = &ehci->regs->port_status[portnum - 1];
1288 return ehci_readl(ehci, reg) & PORT_OWNER;
1289 }