blob: dccb90edd66efaed6423ad56e2e88daac5403a99 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * OHCI HCD (Host Controller Driver) for USB.
David Brownelldd9048a2006-12-05 03:18:31 -08003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
David Brownelldd9048a2006-12-05 03:18:31 -08006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This file is licenced under GPL
8 */
9
10/*-------------------------------------------------------------------------*/
11
12/*
13 * OHCI Root Hub ... the nonsharable stuff
14 */
15
16#define dbg_port(hc,label,num,value) \
17 ohci_dbg (hc, \
18 "%s roothub.portstatus [%d] " \
19 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
20 label, num, temp, \
21 (temp & RH_PS_PRSC) ? " PRSC" : "", \
22 (temp & RH_PS_OCIC) ? " OCIC" : "", \
23 (temp & RH_PS_PSSC) ? " PSSC" : "", \
24 (temp & RH_PS_PESC) ? " PESC" : "", \
25 (temp & RH_PS_CSC) ? " CSC" : "", \
David Brownelldd9048a2006-12-05 03:18:31 -080026 \
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 (temp & RH_PS_LSDA) ? " LSDA" : "", \
28 (temp & RH_PS_PPS) ? " PPS" : "", \
29 (temp & RH_PS_PRS) ? " PRS" : "", \
30 (temp & RH_PS_POCI) ? " POCI" : "", \
31 (temp & RH_PS_PSS) ? " PSS" : "", \
David Brownelldd9048a2006-12-05 03:18:31 -080032 \
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 (temp & RH_PS_PES) ? " PES" : "", \
34 (temp & RH_PS_CCS) ? " CCS" : "" \
35 );
36
37/*-------------------------------------------------------------------------*/
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define OHCI_SCHED_ENABLES \
40 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
41
Alan Sternc6fcb852014-07-18 16:25:59 -040042static void update_done_list(struct ohci_hcd *);
43static void process_done_list(struct ohci_hcd *);
David Howells7d12e782006-10-05 14:55:46 +010044static void finish_unlinks (struct ohci_hcd *, u16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Alan Sternb1878442006-10-24 12:02:31 -040046#ifdef CONFIG_PM
Alan Stern8d1a2432006-09-26 14:46:16 -040047static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
48__releases(ohci->lock)
49__acquires(ohci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int status = 0;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +110052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
54 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
55 case OHCI_USB_RESUME:
56 ohci_dbg (ohci, "resume/suspend?\n");
57 ohci->hc_control &= ~OHCI_CTRL_HCFS;
58 ohci->hc_control |= OHCI_USB_RESET;
59 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
60 (void) ohci_readl (ohci, &ohci->regs->control);
61 /* FALL THROUGH */
62 case OHCI_USB_RESET:
63 status = -EBUSY;
64 ohci_dbg (ohci, "needs reinit!\n");
65 goto done;
66 case OHCI_USB_SUSPEND:
Alan Stern8d1a2432006-09-26 14:46:16 -040067 if (!ohci->autostop) {
68 ohci_dbg (ohci, "already suspended\n");
69 goto done;
70 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 }
Alan Stern8d1a2432006-09-26 14:46:16 -040072 ohci_dbg (ohci, "%s root hub\n",
73 autostop ? "auto-stop" : "suspend");
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 /* First stop any processing */
Alan Stern8d1a2432006-09-26 14:46:16 -040076 if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
78 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
79 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
80 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
81
82 /* sched disables take effect on the next frame,
83 * then the last WDH could take 6+ msec
84 */
85 ohci_dbg (ohci, "stopping schedules ...\n");
Alan Stern8d1a2432006-09-26 14:46:16 -040086 ohci->autostop = 0;
87 spin_unlock_irq (&ohci->lock);
88 msleep (8);
89 spin_lock_irq (&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Alan Sternc6fcb852014-07-18 16:25:59 -040091 update_done_list(ohci);
92 process_done_list(ohci);
David Howells7d12e782006-10-05 14:55:46 +010093 finish_unlinks (ohci, ohci_frame_no(ohci));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Alan Sternc1db30a2014-05-01 15:21:42 -040095 /*
96 * Some controllers don't handle "global" suspend properly if
97 * there are unsuspended ports. For these controllers, put all
98 * the enabled ports into suspend before suspending the root hub.
99 */
100 if (ohci->flags & OHCI_QUIRK_GLOBAL_SUSPEND) {
101 __hc32 __iomem *portstat = ohci->regs->roothub.portstatus;
102 int i;
103 unsigned temp;
104
105 for (i = 0; i < ohci->num_ports; (++i, ++portstat)) {
106 temp = ohci_readl(ohci, portstat);
107 if ((temp & (RH_PS_PES | RH_PS_PSS)) ==
108 RH_PS_PES)
109 ohci_writel(ohci, RH_PS_PSS, portstat);
110 }
111 }
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* maybe resume can wake root hub */
Alan Stern58a97ff2008-04-14 12:17:10 -0400114 if (ohci_to_hcd(ohci)->self.root_hub->do_remote_wakeup || autostop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 ohci->hc_control |= OHCI_CTRL_RWE;
Alan Stern58a97ff2008-04-14 12:17:10 -0400116 } else {
Alan Stern5f474932008-04-14 12:17:49 -0400117 ohci_writel(ohci, OHCI_INTR_RHSC | OHCI_INTR_RD,
118 &ohci->regs->intrdisable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 ohci->hc_control &= ~OHCI_CTRL_RWE;
Alan Stern1f7e1a32006-09-25 15:41:21 -0400120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
David Brownellf197b2c2005-09-22 22:42:53 -0700122 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
123 * which doesn't imply ports will first be individually suspended.
124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 ohci->hc_control &= ~OHCI_CTRL_HCFS;
126 ohci->hc_control |= OHCI_USB_SUSPEND;
127 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
128 (void) ohci_readl (ohci, &ohci->regs->control);
129
130 /* no resumes until devices finish suspending */
Alan Stern8d1a2432006-09-26 14:46:16 -0400131 if (!autostop) {
132 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
133 ohci->autostop = 0;
Alan Sternb7463c72011-11-17 16:41:56 -0500134 ohci->rh_state = OHCI_RH_SUSPENDED;
Alan Stern8d1a2432006-09-26 14:46:16 -0400135 }
David Brownelld4139842006-08-04 11:31:55 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return status;
139}
140
141static inline struct ed *find_head (struct ed *ed)
142{
143 /* for bulk and control lists */
144 while (ed->ed_prev)
145 ed = ed->ed_prev;
146 return ed;
147}
148
149/* caller has locked the root hub */
Alan Stern8d1a2432006-09-26 14:46:16 -0400150static int ohci_rh_resume (struct ohci_hcd *ohci)
151__releases(ohci->lock)
152__acquires(ohci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Alan Stern8d1a2432006-09-26 14:46:16 -0400154 struct usb_hcd *hcd = ohci_to_hcd (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 u32 temp, enables;
156 int status = -EINPROGRESS;
Alan Stern8d1a2432006-09-26 14:46:16 -0400157 int autostopped = ohci->autostop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Alan Stern8d1a2432006-09-26 14:46:16 -0400159 ohci->autostop = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
161
162 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
David Brownellf197b2c2005-09-22 22:42:53 -0700163 /* this can happen after resuming a swsusp snapshot */
Alan Sternb7463c72011-11-17 16:41:56 -0500164 if (ohci->rh_state != OHCI_RH_RUNNING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
166 ohci->hc_control);
167 status = -EBUSY;
168 /* this happens when pmcore resumes HC then root */
169 } else {
170 ohci_dbg (ohci, "duplicate resume\n");
171 status = 0;
172 }
173 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
174 case OHCI_USB_SUSPEND:
175 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
176 ohci->hc_control |= OHCI_USB_RESUME;
177 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
178 (void) ohci_readl (ohci, &ohci->regs->control);
Alan Stern8d1a2432006-09-26 14:46:16 -0400179 ohci_dbg (ohci, "%s root hub\n",
180 autostopped ? "auto-start" : "resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
182 case OHCI_USB_RESUME:
183 /* HCFS changes sometime after INTR_RD */
Alan Stern1b7be3c2006-11-06 12:05:00 -0500184 ohci_dbg(ohci, "%swakeup root hub\n",
Alan Stern583cead2006-10-24 12:04:22 -0400185 autostopped ? "auto-" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break;
187 case OHCI_USB_OPER:
David Brownellf197b2c2005-09-22 22:42:53 -0700188 /* this can happen after resuming a swsusp snapshot */
189 ohci_dbg (ohci, "snapshot resume? reinit\n");
190 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 break;
192 default: /* RESET, we lost power */
David Brownellf197b2c2005-09-22 22:42:53 -0700193 ohci_dbg (ohci, "lost power\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 status = -EBUSY;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (status == -EBUSY) {
Alan Stern8d1a2432006-09-26 14:46:16 -0400197 if (!autostopped) {
Alan Stern8d1a2432006-09-26 14:46:16 -0400198 spin_unlock_irq (&ohci->lock);
Alan Stern8d1a2432006-09-26 14:46:16 -0400199 status = ohci_restart (ohci);
Michael Hanselmannd576bb92007-05-31 23:34:27 +0200200
201 usb_root_hub_lost_power(hcd->self.root_hub);
202
Alan Stern8d1a2432006-09-26 14:46:16 -0400203 spin_lock_irq (&ohci->lock);
204 }
205 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 if (status != -EINPROGRESS)
208 return status;
Alan Stern8d1a2432006-09-26 14:46:16 -0400209 if (autostopped)
210 goto skip_resume;
211 spin_unlock_irq (&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Some controllers (lucent erratum) need extra-long delays */
David Brownellf197b2c2005-09-22 22:42:53 -0700214 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 temp = ohci_readl (ohci, &ohci->regs->control);
217 temp &= OHCI_CTRL_HCFS;
218 if (temp != OHCI_USB_RESUME) {
219 ohci_err (ohci, "controller won't resume\n");
Alan Stern565402b2006-10-27 10:35:01 -0400220 spin_lock_irq(&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -EBUSY;
222 }
223
224 /* disable old schedule state, reinit from scratch */
225 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
226 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
227 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
228 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
229 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
230 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
231
232 /* Sometimes PCI D3 suspend trashes frame timings ... */
233 periodic_reinit (ohci);
234
Alan Stern7c71c6e2013-10-18 11:16:07 -0400235 /*
236 * The following code is executed with ohci->lock held and
237 * irqs disabled if and only if autostopped is true. This
238 * will cause sparse to warn about a "context imbalance".
Alan Stern8d1a2432006-09-26 14:46:16 -0400239 */
Alan Stern8d1a2432006-09-26 14:46:16 -0400240skip_resume:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* interrupts might have been disabled */
242 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
243 if (ohci->ed_rm_list)
244 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* Then re-enable operations */
247 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
248 (void) ohci_readl (ohci, &ohci->regs->control);
Alan Stern8d1a2432006-09-26 14:46:16 -0400249 if (!autostopped)
250 msleep (3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David Brownell6a9062f2006-01-23 15:28:07 -0800252 temp = ohci->hc_control;
253 temp &= OHCI_CTRL_RWC;
254 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 ohci->hc_control = temp;
256 ohci_writel (ohci, temp, &ohci->regs->control);
257 (void) ohci_readl (ohci, &ohci->regs->control);
258
259 /* TRSMRCY */
Alan Stern8d1a2432006-09-26 14:46:16 -0400260 if (!autostopped) {
261 msleep (10);
262 spin_lock_irq (&ohci->lock);
263 }
264 /* now ohci->lock is always held and irqs are always disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
David Brownelld4139842006-08-04 11:31:55 -0700266 /* keep it alive for more than ~5x suspend + resume costs */
267 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* maybe turn schedules back on */
270 enables = 0;
271 temp = 0;
272 if (!ohci->ed_rm_list) {
273 if (ohci->ed_controltail) {
274 ohci_writel (ohci,
275 find_head (ohci->ed_controltail)->dma,
276 &ohci->regs->ed_controlhead);
277 enables |= OHCI_CTRL_CLE;
278 temp |= OHCI_CLF;
279 }
280 if (ohci->ed_bulktail) {
281 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
282 &ohci->regs->ed_bulkhead);
283 enables |= OHCI_CTRL_BLE;
284 temp |= OHCI_BLF;
285 }
286 }
287 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
288 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
289 if (enables) {
290 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
291 ohci->hc_control |= enables;
292 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
293 if (temp)
294 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
295 (void) ohci_readl (ohci, &ohci->regs->control);
296 }
297
Alan Sternb7463c72011-11-17 16:41:56 -0500298 ohci->rh_state = OHCI_RH_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return 0;
300}
301
Alan Stern8d1a2432006-09-26 14:46:16 -0400302static int ohci_bus_suspend (struct usb_hcd *hcd)
303{
304 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
305 int rc;
306
307 spin_lock_irq (&ohci->lock);
308
Alan Stern541c7d42010-06-22 16:39:10 -0400309 if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
Alan Stern8d1a2432006-09-26 14:46:16 -0400310 rc = -ESHUTDOWN;
311 else
312 rc = ohci_rh_suspend (ohci, 0);
313 spin_unlock_irq (&ohci->lock);
314 return rc;
315}
316
317static int ohci_bus_resume (struct usb_hcd *hcd)
318{
319 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
320 int rc;
321
322 if (time_before (jiffies, ohci->next_statechange))
323 msleep(5);
324
325 spin_lock_irq (&ohci->lock);
326
Alan Stern541c7d42010-06-22 16:39:10 -0400327 if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
Alan Stern8d1a2432006-09-26 14:46:16 -0400328 rc = -ESHUTDOWN;
329 else
330 rc = ohci_rh_resume (ohci);
331 spin_unlock_irq (&ohci->lock);
332
333 /* poll until we know a device is connected or we autostop */
334 if (rc == 0)
335 usb_hcd_poll_rh_status(hcd);
336 return rc;
337}
338
Alan Stern1f9fc882006-11-20 11:06:59 -0500339/* Carry out polling-, autostop-, and autoresume-related state changes */
340static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
Alan Stern71b74972008-10-09 15:40:23 -0400341 int any_connected, int rhsc_status)
Alan Stern1f9fc882006-11-20 11:06:59 -0500342{
343 int poll_rh = 1;
Alan Stern71b74972008-10-09 15:40:23 -0400344 int rhsc_enable;
Alan Stern1f9fc882006-11-20 11:06:59 -0500345
Alan Sternee0b9be2010-06-25 14:02:24 -0400346 /* Some broken controllers never turn off RHSC in the interrupt
Alan Stern4a511bc2008-09-03 16:38:32 -0400347 * status register. For their sake we won't re-enable RHSC
348 * interrupts if the interrupt bit is already active.
349 */
Alan Stern4a511bc2008-09-03 16:38:32 -0400350 rhsc_enable = ohci_readl(ohci, &ohci->regs->intrenable) &
351 OHCI_INTR_RHSC;
352
Alan Stern1f9fc882006-11-20 11:06:59 -0500353 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
Alan Stern1f9fc882006-11-20 11:06:59 -0500354 case OHCI_USB_OPER:
Alan Stern4a511bc2008-09-03 16:38:32 -0400355 /* If no status changes are pending, enable RHSC interrupts. */
356 if (!rhsc_enable && !rhsc_status && !changed) {
357 rhsc_enable = OHCI_INTR_RHSC;
358 ohci_writel(ohci, rhsc_enable, &ohci->regs->intrenable);
Alan Sternb5fb4542008-08-20 17:22:05 -0400359 }
360
361 /* Keep on polling until we know a device is connected
362 * and RHSC is enabled, or until we autostop.
363 */
Alan Stern1f9fc882006-11-20 11:06:59 -0500364 if (!ohci->autostop) {
365 if (any_connected ||
366 !device_may_wakeup(&ohci_to_hcd(ohci)
367 ->self.root_hub->dev)) {
Alan Stern4a511bc2008-09-03 16:38:32 -0400368 if (rhsc_enable)
Alan Stern1f9fc882006-11-20 11:06:59 -0500369 poll_rh = 0;
370 } else {
371 ohci->autostop = 1;
372 ohci->next_statechange = jiffies + HZ;
373 }
374
375 /* if no devices have been attached for one second, autostop */
376 } else {
377 if (changed || any_connected) {
378 ohci->autostop = 0;
379 ohci->next_statechange = jiffies +
380 STATECHANGE_DELAY;
Alan Stern4a511bc2008-09-03 16:38:32 -0400381 } else if (time_after_eq(jiffies,
Alan Stern1f9fc882006-11-20 11:06:59 -0500382 ohci->next_statechange)
383 && !ohci->ed_rm_list
384 && !(ohci->hc_control &
385 OHCI_SCHED_ENABLES)) {
386 ohci_rh_suspend(ohci, 1);
Alan Stern4a511bc2008-09-03 16:38:32 -0400387 if (rhsc_enable)
388 poll_rh = 0;
Alan Stern1f9fc882006-11-20 11:06:59 -0500389 }
390 }
391 break;
392
Alan Stern1f9fc882006-11-20 11:06:59 -0500393 case OHCI_USB_SUSPEND:
394 case OHCI_USB_RESUME:
Alan Stern4a511bc2008-09-03 16:38:32 -0400395 /* if there is a port change, autostart or ask to be resumed */
Alan Stern1f9fc882006-11-20 11:06:59 -0500396 if (changed) {
397 if (ohci->autostop)
398 ohci_rh_resume(ohci);
399 else
400 usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
Alan Stern71b74972008-10-09 15:40:23 -0400401
402 /* If remote wakeup is disabled, stop polling */
403 } else if (!ohci->autostop &&
404 !ohci_to_hcd(ohci)->self.root_hub->
405 do_remote_wakeup) {
406 poll_rh = 0;
407
Alan Stern1f9fc882006-11-20 11:06:59 -0500408 } else {
Alan Stern71b74972008-10-09 15:40:23 -0400409 /* If no status changes are pending,
410 * enable RHSC interrupts
411 */
412 if (!rhsc_enable && !rhsc_status) {
Alan Stern4a511bc2008-09-03 16:38:32 -0400413 rhsc_enable = OHCI_INTR_RHSC;
414 ohci_writel(ohci, rhsc_enable,
Alan Sternb5fb4542008-08-20 17:22:05 -0400415 &ohci->regs->intrenable);
Alan Stern4a511bc2008-09-03 16:38:32 -0400416 }
Alan Stern71b74972008-10-09 15:40:23 -0400417 /* Keep polling until RHSC is enabled */
Alan Stern4a511bc2008-09-03 16:38:32 -0400418 if (rhsc_enable)
419 poll_rh = 0;
Alan Stern1f9fc882006-11-20 11:06:59 -0500420 }
421 break;
422 }
423 return poll_rh;
424}
425
426#else /* CONFIG_PM */
427
428static inline int ohci_rh_resume(struct ohci_hcd *ohci)
429{
430 return 0;
431}
432
433/* Carry out polling-related state changes.
434 * autostop isn't used when CONFIG_PM is turned off.
435 */
436static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
Alan Stern71b74972008-10-09 15:40:23 -0400437 int any_connected, int rhsc_status)
Alan Stern1f9fc882006-11-20 11:06:59 -0500438{
Alan Sternb5fb4542008-08-20 17:22:05 -0400439 /* If RHSC is enabled, don't poll */
Linus Torvalds09ca8ad2008-07-06 10:27:25 -0700440 if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
Alan Sternb5fb4542008-08-20 17:22:05 -0400441 return 0;
442
Alan Stern71b74972008-10-09 15:40:23 -0400443 /* If status changes are pending, continue polling.
444 * Conversely, if no status changes are pending but the RHSC
445 * status bit was set, then RHSC may be broken so continue polling.
446 */
447 if (changed || rhsc_status)
448 return 1;
449
450 /* It's safe to re-enable RHSC interrupts */
451 ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
452 return 0;
Alan Stern1f9fc882006-11-20 11:06:59 -0500453}
454
David Brownell8ad7fe12005-09-13 19:59:11 -0700455#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457/*-------------------------------------------------------------------------*/
458
459/* build "status change" packet (one or two bytes) from HC registers */
460
Laurent Pinchart42b59eb2014-04-16 18:00:09 +0200461int ohci_hub_status_data(struct usb_hcd *hcd, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
David Brownellfdd13b32005-08-31 11:52:57 -0700464 int i, changed = 0, length = 1;
Alan Stern052ac012006-10-27 10:33:11 -0400465 int any_connected = 0;
Alan Stern71b74972008-10-09 15:40:23 -0400466 int rhsc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 unsigned long flags;
468
469 spin_lock_irqsave (&ohci->lock, flags);
Alan Stern541c7d42010-06-22 16:39:10 -0400470 if (!HCD_HW_ACCESSIBLE(hcd))
Alan Stern6fd75b12007-05-04 11:57:00 -0400471 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
David Brownellfdd13b32005-08-31 11:52:57 -0700473 /* undocumented erratum seen on at least rev D */
474 if ((ohci->flags & OHCI_QUIRK_AMD756)
475 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
476 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
478 /* retry later; "should not happen" */
479 goto done;
480 }
481
482 /* init status */
483 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
484 buf [0] = changed = 1;
485 else
486 buf [0] = 0;
David Brownellfdd13b32005-08-31 11:52:57 -0700487 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 buf [1] = 0;
489 length++;
490 }
491
Alan Stern71b74972008-10-09 15:40:23 -0400492 /* Clear the RHSC status flag before reading the port statuses */
493 ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrstatus);
494 rhsc_status = ohci_readl(ohci, &ohci->regs->intrstatus) &
495 OHCI_INTR_RHSC;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* look at each port */
David Brownellfdd13b32005-08-31 11:52:57 -0700498 for (i = 0; i < ohci->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 u32 status = roothub_portstatus (ohci, i);
500
Alan Stern8d1a2432006-09-26 14:46:16 -0400501 /* can't autostop if ports are connected */
502 any_connected |= (status & RH_PS_CCS);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
505 | RH_PS_OCIC | RH_PS_PRSC)) {
506 changed = 1;
507 if (i < 7)
508 buf [0] |= 1 << (i + 1);
509 else
510 buf [1] |= 1 << (i - 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Alan Stern541c7d42010-06-22 16:39:10 -0400514 if (ohci_root_hub_state_changes(ohci, changed,
515 any_connected, rhsc_status))
516 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
517 else
518 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
519
David Brownelld4139842006-08-04 11:31:55 -0700520
521done:
522 spin_unlock_irqrestore (&ohci->lock, flags);
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return changed ? length : 0;
525}
Laurent Pinchart42b59eb2014-04-16 18:00:09 +0200526EXPORT_SYMBOL_GPL(ohci_hub_status_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528/*-------------------------------------------------------------------------*/
529
530static void
531ohci_hub_descriptor (
532 struct ohci_hcd *ohci,
533 struct usb_hub_descriptor *desc
534) {
535 u32 rh = roothub_a (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 u16 temp;
537
538 desc->bDescriptorType = 0x29;
539 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
540 desc->bHubContrCurrent = 0;
541
David Brownellfdd13b32005-08-31 11:52:57 -0700542 desc->bNbrPorts = ohci->num_ports;
543 temp = 1 + (ohci->num_ports / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 desc->bDescLength = 7 + 2 * temp;
545
546 temp = 0;
547 if (rh & RH_A_NPS) /* no power switching? */
548 temp |= 0x0002;
David Brownelldd9048a2006-12-05 03:18:31 -0800549 if (rh & RH_A_PSM) /* per-port power switching? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 temp |= 0x0001;
551 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
552 temp |= 0x0010;
553 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
554 temp |= 0x0008;
Alan Stern7c71c6e2013-10-18 11:16:07 -0400555 desc->wHubCharacteristics = cpu_to_le16(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Sarah Sharpda130512010-11-30 15:55:51 -0800557 /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 rh = roothub_b (ohci);
John Youndbe79bb2001-09-17 00:00:00 -0700559 memset(desc->u.hs.DeviceRemovable, 0xff,
560 sizeof(desc->u.hs.DeviceRemovable));
561 desc->u.hs.DeviceRemovable[0] = rh & RH_B_DR;
David Brownellfdd13b32005-08-31 11:52:57 -0700562 if (ohci->num_ports > 7) {
John Youndbe79bb2001-09-17 00:00:00 -0700563 desc->u.hs.DeviceRemovable[1] = (rh & RH_B_DR) >> 8;
564 desc->u.hs.DeviceRemovable[2] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 } else
John Youndbe79bb2001-09-17 00:00:00 -0700566 desc->u.hs.DeviceRemovable[1] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
569/*-------------------------------------------------------------------------*/
570
571#ifdef CONFIG_USB_OTG
572
573static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
574{
575 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
576 u32 status;
577
578 if (!port)
579 return -EINVAL;
580 port--;
581
582 /* start port reset before HNP protocol times out */
583 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
584 if (!(status & RH_PS_CCS))
585 return -ENODEV;
586
587 /* khubd will finish the reset later */
588 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
589 return 0;
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592#else
593
594#define ohci_start_port_reset NULL
595
596#endif
597
598/*-------------------------------------------------------------------------*/
599
600
601/* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
602 * not necessarily continuous ... to guard against resume signaling.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604#define PORT_RESET_MSEC 50
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606/* this timer value might be vendor-specific ... */
607#define PORT_RESET_HW_MSEC 10
608
609/* wrap-aware logic morphed from <linux/jiffies.h> */
610#define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
611
612/* called from some task, normally khubd */
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800613static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
Alan Stern1b7b61c2008-04-22 10:50:18 -0400616 u32 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
618 u16 reset_done = now + PORT_RESET_MSEC;
David Brownelle01e7fe2008-02-02 02:42:52 -0800619 int limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 /* build a "continuous enough" reset signal, with up to
622 * 3msec gap between pulses. scheduler HZ==100 must work;
623 * this might need to be deadline-scheduled.
624 */
625 do {
David Brownelle01e7fe2008-02-02 02:42:52 -0800626 int limit_2;
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /* spin until any current reset finishes */
David Brownelle01e7fe2008-02-02 02:42:52 -0800629 limit_2 = PORT_RESET_HW_MSEC * 2;
630 while (--limit_2 >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 temp = ohci_readl (ohci, portstat);
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800632 /* handle e.g. CardBus eject */
633 if (temp == ~(u32)0)
634 return -ESHUTDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (!(temp & RH_PS_PRS))
636 break;
637 udelay (500);
David Brownelldd9048a2006-12-05 03:18:31 -0800638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
David Brownelle01e7fe2008-02-02 02:42:52 -0800640 /* timeout (a hardware error) has been observed when
641 * EHCI sets CF while this driver is resetting a port;
642 * presumably other disconnect paths might do it too.
643 */
644 if (limit_2 < 0) {
645 ohci_dbg(ohci,
646 "port[%d] reset timeout, stat %08x\n",
647 port, temp);
648 break;
649 }
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (!(temp & RH_PS_CCS))
652 break;
653 if (temp & RH_PS_PRSC)
654 ohci_writel (ohci, RH_PS_PRSC, portstat);
655
656 /* start the next reset, sleep till it's probably done */
657 ohci_writel (ohci, RH_PS_PRS, portstat);
658 msleep(PORT_RESET_HW_MSEC);
659 now = ohci_readl(ohci, &ohci->regs->fmnumber);
David Brownelle01e7fe2008-02-02 02:42:52 -0800660 } while (tick_before(now, reset_done) && --limit_1 >= 0);
661
662 /* caller synchronizes using PRSC ... and handles PRS
663 * still being set when this returns.
664 */
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800665
666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Laurent Pinchart42b59eb2014-04-16 18:00:09 +0200669int ohci_hub_control(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct usb_hcd *hcd,
671 u16 typeReq,
672 u16 wValue,
673 u16 wIndex,
674 char *buf,
675 u16 wLength
676) {
677 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
Alan Sternfcf7d212010-04-20 10:37:57 -0400678 int ports = ohci->num_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 u32 temp;
680 int retval = 0;
681
Alan Stern541c7d42010-06-22 16:39:10 -0400682 if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100683 return -ESHUTDOWN;
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 switch (typeReq) {
686 case ClearHubFeature:
687 switch (wValue) {
688 case C_HUB_OVER_CURRENT:
689 ohci_writel (ohci, RH_HS_OCIC,
690 &ohci->regs->roothub.status);
691 case C_HUB_LOCAL_POWER:
692 break;
693 default:
694 goto error;
695 }
696 break;
697 case ClearPortFeature:
698 if (!wIndex || wIndex > ports)
699 goto error;
700 wIndex--;
701
702 switch (wValue) {
703 case USB_PORT_FEAT_ENABLE:
704 temp = RH_PS_CCS;
705 break;
706 case USB_PORT_FEAT_C_ENABLE:
707 temp = RH_PS_PESC;
708 break;
709 case USB_PORT_FEAT_SUSPEND:
710 temp = RH_PS_POCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 break;
712 case USB_PORT_FEAT_C_SUSPEND:
713 temp = RH_PS_PSSC;
714 break;
715 case USB_PORT_FEAT_POWER:
716 temp = RH_PS_LSDA;
717 break;
718 case USB_PORT_FEAT_C_CONNECTION:
719 temp = RH_PS_CSC;
720 break;
721 case USB_PORT_FEAT_C_OVER_CURRENT:
722 temp = RH_PS_OCIC;
723 break;
724 case USB_PORT_FEAT_C_RESET:
725 temp = RH_PS_PRSC;
726 break;
727 default:
728 goto error;
729 }
730 ohci_writel (ohci, temp,
731 &ohci->regs->roothub.portstatus [wIndex]);
732 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
733 break;
734 case GetHubDescriptor:
735 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
736 break;
737 case GetHubStatus:
738 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700739 put_unaligned_le32(temp, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
741 case GetPortStatus:
742 if (!wIndex || wIndex > ports)
743 goto error;
744 wIndex--;
745 temp = roothub_portstatus (ohci, wIndex);
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700746 put_unaligned_le32(temp, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Oliver Neukum00b033b2013-11-18 13:23:00 +0100748 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
749 dbg_port(ohci, "GetStatus", wIndex, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 break;
751 case SetHubFeature:
752 switch (wValue) {
753 case C_HUB_OVER_CURRENT:
754 // FIXME: this can be cleared, yes?
755 case C_HUB_LOCAL_POWER:
756 break;
757 default:
758 goto error;
759 }
760 break;
761 case SetPortFeature:
762 if (!wIndex || wIndex > ports)
763 goto error;
764 wIndex--;
765 switch (wValue) {
766 case USB_PORT_FEAT_SUSPEND:
767#ifdef CONFIG_USB_OTG
768 if (hcd->self.otg_port == (wIndex + 1)
769 && hcd->self.b_hnp_enable)
Dmitry Baryshkove8b24452008-07-06 14:26:30 +0400770 ohci->start_hnp(ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 else
772#endif
773 ohci_writel (ohci, RH_PS_PSS,
774 &ohci->regs->roothub.portstatus [wIndex]);
775 break;
776 case USB_PORT_FEAT_POWER:
777 ohci_writel (ohci, RH_PS_PPS,
778 &ohci->regs->roothub.portstatus [wIndex]);
779 break;
780 case USB_PORT_FEAT_RESET:
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800781 retval = root_port_reset (ohci, wIndex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 break;
783 default:
784 goto error;
785 }
786 break;
787
788 default:
789error:
790 /* "protocol stall" on error */
791 retval = -EPIPE;
792 }
793 return retval;
794}
Laurent Pinchart42b59eb2014-04-16 18:00:09 +0200795EXPORT_SYMBOL_GPL(ohci_hub_control);