blob: cf3e1d255639f7a9f0a696c757f34351e1ebd61e [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
David Brownelld4139842006-08-04 11:31:55 -070039/* hcd->hub_irq_enable() */
40static void ohci_rhsc_enable (struct usb_hcd *hcd)
41{
42 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
43
Alan Stern052ac012006-10-27 10:33:11 -040044 spin_lock_irq(&ohci->lock);
45 if (!ohci->autostop)
46 del_timer(&hcd->rh_timer); /* Prevent next poll */
47 ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
48 spin_unlock_irq(&ohci->lock);
David Brownelld4139842006-08-04 11:31:55 -070049}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define OHCI_SCHED_ENABLES \
52 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
53
David Howells7d12e782006-10-05 14:55:46 +010054static void dl_done_list (struct ohci_hcd *);
55static void finish_unlinks (struct ohci_hcd *, u16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Alan Sternb1878442006-10-24 12:02:31 -040057#ifdef CONFIG_PM
Alan Stern8d1a2432006-09-26 14:46:16 -040058static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
59__releases(ohci->lock)
60__acquires(ohci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 int status = 0;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +110063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
65 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
66 case OHCI_USB_RESUME:
67 ohci_dbg (ohci, "resume/suspend?\n");
68 ohci->hc_control &= ~OHCI_CTRL_HCFS;
69 ohci->hc_control |= OHCI_USB_RESET;
70 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
71 (void) ohci_readl (ohci, &ohci->regs->control);
72 /* FALL THROUGH */
73 case OHCI_USB_RESET:
74 status = -EBUSY;
75 ohci_dbg (ohci, "needs reinit!\n");
76 goto done;
77 case OHCI_USB_SUSPEND:
Alan Stern8d1a2432006-09-26 14:46:16 -040078 if (!ohci->autostop) {
79 ohci_dbg (ohci, "already suspended\n");
80 goto done;
81 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Alan Stern8d1a2432006-09-26 14:46:16 -040083 ohci_dbg (ohci, "%s root hub\n",
84 autostop ? "auto-stop" : "suspend");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /* First stop any processing */
Alan Stern8d1a2432006-09-26 14:46:16 -040087 if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
89 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
90 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
91 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
92
93 /* sched disables take effect on the next frame,
94 * then the last WDH could take 6+ msec
95 */
96 ohci_dbg (ohci, "stopping schedules ...\n");
Alan Stern8d1a2432006-09-26 14:46:16 -040097 ohci->autostop = 0;
98 spin_unlock_irq (&ohci->lock);
99 msleep (8);
100 spin_lock_irq (&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
David Howells7d12e782006-10-05 14:55:46 +0100102 dl_done_list (ohci);
103 finish_unlinks (ohci, ohci_frame_no(ohci));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 /* maybe resume can wake root hub */
Alan Stern58a97ff2008-04-14 12:17:10 -0400106 if (ohci_to_hcd(ohci)->self.root_hub->do_remote_wakeup || autostop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ohci->hc_control |= OHCI_CTRL_RWE;
Alan Stern58a97ff2008-04-14 12:17:10 -0400108 } else {
Alan Stern1f7e1a32006-09-25 15:41:21 -0400109 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 ohci->hc_control &= ~OHCI_CTRL_RWE;
Alan Stern1f7e1a32006-09-25 15:41:21 -0400111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
David Brownellf197b2c2005-09-22 22:42:53 -0700113 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
114 * which doesn't imply ports will first be individually suspended.
115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 ohci->hc_control &= ~OHCI_CTRL_HCFS;
117 ohci->hc_control |= OHCI_USB_SUSPEND;
118 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
119 (void) ohci_readl (ohci, &ohci->regs->control);
120
121 /* no resumes until devices finish suspending */
Alan Stern8d1a2432006-09-26 14:46:16 -0400122 if (!autostop) {
123 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
124 ohci->autostop = 0;
125 }
David Brownelld4139842006-08-04 11:31:55 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return status;
129}
130
131static inline struct ed *find_head (struct ed *ed)
132{
133 /* for bulk and control lists */
134 while (ed->ed_prev)
135 ed = ed->ed_prev;
136 return ed;
137}
138
139/* caller has locked the root hub */
Alan Stern8d1a2432006-09-26 14:46:16 -0400140static int ohci_rh_resume (struct ohci_hcd *ohci)
141__releases(ohci->lock)
142__acquires(ohci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Alan Stern8d1a2432006-09-26 14:46:16 -0400144 struct usb_hcd *hcd = ohci_to_hcd (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 u32 temp, enables;
146 int status = -EINPROGRESS;
Alan Stern8d1a2432006-09-26 14:46:16 -0400147 int autostopped = ohci->autostop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Alan Stern8d1a2432006-09-26 14:46:16 -0400149 ohci->autostop = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
151
152 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
David Brownellf197b2c2005-09-22 22:42:53 -0700153 /* this can happen after resuming a swsusp snapshot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (hcd->state == HC_STATE_RESUMING) {
155 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
156 ohci->hc_control);
157 status = -EBUSY;
158 /* this happens when pmcore resumes HC then root */
159 } else {
160 ohci_dbg (ohci, "duplicate resume\n");
161 status = 0;
162 }
163 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
164 case OHCI_USB_SUSPEND:
165 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
166 ohci->hc_control |= OHCI_USB_RESUME;
167 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
168 (void) ohci_readl (ohci, &ohci->regs->control);
Alan Stern8d1a2432006-09-26 14:46:16 -0400169 ohci_dbg (ohci, "%s root hub\n",
170 autostopped ? "auto-start" : "resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 break;
172 case OHCI_USB_RESUME:
173 /* HCFS changes sometime after INTR_RD */
Alan Stern1b7be3c2006-11-06 12:05:00 -0500174 ohci_dbg(ohci, "%swakeup root hub\n",
Alan Stern583cead2006-10-24 12:04:22 -0400175 autostopped ? "auto-" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177 case OHCI_USB_OPER:
David Brownellf197b2c2005-09-22 22:42:53 -0700178 /* this can happen after resuming a swsusp snapshot */
179 ohci_dbg (ohci, "snapshot resume? reinit\n");
180 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
182 default: /* RESET, we lost power */
David Brownellf197b2c2005-09-22 22:42:53 -0700183 ohci_dbg (ohci, "lost power\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 status = -EBUSY;
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (status == -EBUSY) {
Alan Stern8d1a2432006-09-26 14:46:16 -0400187 if (!autostopped) {
Alan Stern8d1a2432006-09-26 14:46:16 -0400188 spin_unlock_irq (&ohci->lock);
189 (void) ohci_init (ohci);
190 status = ohci_restart (ohci);
Michael Hanselmannd576bb92007-05-31 23:34:27 +0200191
192 usb_root_hub_lost_power(hcd->self.root_hub);
193
Alan Stern8d1a2432006-09-26 14:46:16 -0400194 spin_lock_irq (&ohci->lock);
195 }
196 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198 if (status != -EINPROGRESS)
199 return status;
Alan Stern8d1a2432006-09-26 14:46:16 -0400200 if (autostopped)
201 goto skip_resume;
202 spin_unlock_irq (&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* Some controllers (lucent erratum) need extra-long delays */
David Brownellf197b2c2005-09-22 22:42:53 -0700205 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 temp = ohci_readl (ohci, &ohci->regs->control);
208 temp &= OHCI_CTRL_HCFS;
209 if (temp != OHCI_USB_RESUME) {
210 ohci_err (ohci, "controller won't resume\n");
Alan Stern565402b2006-10-27 10:35:01 -0400211 spin_lock_irq(&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return -EBUSY;
213 }
214
215 /* disable old schedule state, reinit from scratch */
216 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
217 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
218 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
219 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
220 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
221 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
222
223 /* Sometimes PCI D3 suspend trashes frame timings ... */
224 periodic_reinit (ohci);
225
Alan Stern8d1a2432006-09-26 14:46:16 -0400226 /* the following code is executed with ohci->lock held and
227 * irqs disabled if and only if autostopped is true
228 */
229
230skip_resume:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* interrupts might have been disabled */
232 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
233 if (ohci->ed_rm_list)
234 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 /* Then re-enable operations */
237 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
238 (void) ohci_readl (ohci, &ohci->regs->control);
Alan Stern8d1a2432006-09-26 14:46:16 -0400239 if (!autostopped)
240 msleep (3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
David Brownell6a9062f2006-01-23 15:28:07 -0800242 temp = ohci->hc_control;
243 temp &= OHCI_CTRL_RWC;
244 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 ohci->hc_control = temp;
246 ohci_writel (ohci, temp, &ohci->regs->control);
247 (void) ohci_readl (ohci, &ohci->regs->control);
248
249 /* TRSMRCY */
Alan Stern8d1a2432006-09-26 14:46:16 -0400250 if (!autostopped) {
251 msleep (10);
252 spin_lock_irq (&ohci->lock);
253 }
254 /* now ohci->lock is always held and irqs are always disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
David Brownelld4139842006-08-04 11:31:55 -0700256 /* keep it alive for more than ~5x suspend + resume costs */
257 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* maybe turn schedules back on */
260 enables = 0;
261 temp = 0;
262 if (!ohci->ed_rm_list) {
263 if (ohci->ed_controltail) {
264 ohci_writel (ohci,
265 find_head (ohci->ed_controltail)->dma,
266 &ohci->regs->ed_controlhead);
267 enables |= OHCI_CTRL_CLE;
268 temp |= OHCI_CLF;
269 }
270 if (ohci->ed_bulktail) {
271 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
272 &ohci->regs->ed_bulkhead);
273 enables |= OHCI_CTRL_BLE;
274 temp |= OHCI_BLF;
275 }
276 }
277 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
278 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
279 if (enables) {
280 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
281 ohci->hc_control |= enables;
282 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
283 if (temp)
284 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
285 (void) ohci_readl (ohci, &ohci->regs->control);
286 }
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 0;
289}
290
Alan Stern8d1a2432006-09-26 14:46:16 -0400291static int ohci_bus_suspend (struct usb_hcd *hcd)
292{
293 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
294 int rc;
295
296 spin_lock_irq (&ohci->lock);
297
298 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
299 rc = -ESHUTDOWN;
300 else
301 rc = ohci_rh_suspend (ohci, 0);
302 spin_unlock_irq (&ohci->lock);
303 return rc;
304}
305
306static int ohci_bus_resume (struct usb_hcd *hcd)
307{
308 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
309 int rc;
310
311 if (time_before (jiffies, ohci->next_statechange))
312 msleep(5);
313
314 spin_lock_irq (&ohci->lock);
315
316 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
317 rc = -ESHUTDOWN;
318 else
319 rc = ohci_rh_resume (ohci);
320 spin_unlock_irq (&ohci->lock);
321
322 /* poll until we know a device is connected or we autostop */
323 if (rc == 0)
324 usb_hcd_poll_rh_status(hcd);
325 return rc;
326}
327
Alan Stern43bbb7e2008-04-03 18:03:17 -0400328/* Carry out the final steps of resuming the controller device */
329static void ohci_finish_controller_resume(struct usb_hcd *hcd)
330{
331 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
332 int port;
333 bool need_reinit = false;
334
335 /* See if the controller is already running or has been reset */
336 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
337 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
338 need_reinit = true;
339 } else {
340 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
341 case OHCI_USB_OPER:
342 case OHCI_USB_RESET:
343 need_reinit = true;
344 }
345 }
346
347 /* If needed, reinitialize and suspend the root hub */
348 if (need_reinit) {
349 spin_lock_irq(&ohci->lock);
350 hcd->state = HC_STATE_RESUMING;
351 ohci_rh_resume(ohci);
352 hcd->state = HC_STATE_QUIESCING;
353 ohci_rh_suspend(ohci, 0);
354 hcd->state = HC_STATE_SUSPENDED;
355 spin_unlock_irq(&ohci->lock);
356 }
357
358 /* Normally just turn on port power and enable interrupts */
359 else {
360 ohci_dbg(ohci, "powerup ports\n");
361 for (port = 0; port < ohci->num_ports; port++)
362 ohci_writel(ohci, RH_PS_PPS,
363 &ohci->regs->roothub.portstatus[port]);
364
365 ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrenable);
366 ohci_readl(ohci, &ohci->regs->intrenable);
367 msleep(20);
368 }
369}
370
Alan Stern1f9fc882006-11-20 11:06:59 -0500371/* Carry out polling-, autostop-, and autoresume-related state changes */
372static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
373 int any_connected)
374{
375 int poll_rh = 1;
376
377 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
378
379 case OHCI_USB_OPER:
380 /* keep on polling until we know a device is connected
381 * and RHSC is enabled */
382 if (!ohci->autostop) {
383 if (any_connected ||
384 !device_may_wakeup(&ohci_to_hcd(ohci)
385 ->self.root_hub->dev)) {
386 if (ohci_readl(ohci, &ohci->regs->intrenable) &
387 OHCI_INTR_RHSC)
388 poll_rh = 0;
389 } else {
390 ohci->autostop = 1;
391 ohci->next_statechange = jiffies + HZ;
392 }
393
394 /* if no devices have been attached for one second, autostop */
395 } else {
396 if (changed || any_connected) {
397 ohci->autostop = 0;
398 ohci->next_statechange = jiffies +
399 STATECHANGE_DELAY;
400 } else if (time_after_eq(jiffies,
401 ohci->next_statechange)
402 && !ohci->ed_rm_list
403 && !(ohci->hc_control &
404 OHCI_SCHED_ENABLES)) {
405 ohci_rh_suspend(ohci, 1);
406 }
407 }
408 break;
409
410 /* if there is a port change, autostart or ask to be resumed */
411 case OHCI_USB_SUSPEND:
412 case OHCI_USB_RESUME:
413 if (changed) {
414 if (ohci->autostop)
415 ohci_rh_resume(ohci);
416 else
417 usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
418 } else {
419 /* everything is idle, no need for polling */
420 poll_rh = 0;
421 }
422 break;
423 }
424 return poll_rh;
425}
426
427#else /* CONFIG_PM */
428
429static inline int ohci_rh_resume(struct ohci_hcd *ohci)
430{
431 return 0;
432}
433
434/* Carry out polling-related state changes.
435 * autostop isn't used when CONFIG_PM is turned off.
436 */
437static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
438 int any_connected)
439{
440 int poll_rh = 1;
441
442 /* keep on polling until RHSC is enabled */
443 if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
444 poll_rh = 0;
445 return poll_rh;
446}
447
David Brownell8ad7fe12005-09-13 19:59:11 -0700448#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450/*-------------------------------------------------------------------------*/
451
452/* build "status change" packet (one or two bytes) from HC registers */
453
454static int
455ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
456{
457 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
David Brownellfdd13b32005-08-31 11:52:57 -0700458 int i, changed = 0, length = 1;
Alan Stern052ac012006-10-27 10:33:11 -0400459 int any_connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 unsigned long flags;
461
462 spin_lock_irqsave (&ohci->lock, flags);
Alan Stern6fd75b12007-05-04 11:57:00 -0400463 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
464 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
David Brownellfdd13b32005-08-31 11:52:57 -0700466 /* undocumented erratum seen on at least rev D */
467 if ((ohci->flags & OHCI_QUIRK_AMD756)
468 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
469 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
471 /* retry later; "should not happen" */
472 goto done;
473 }
474
475 /* init status */
476 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
477 buf [0] = changed = 1;
478 else
479 buf [0] = 0;
David Brownellfdd13b32005-08-31 11:52:57 -0700480 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 buf [1] = 0;
482 length++;
483 }
484
485 /* look at each port */
David Brownellfdd13b32005-08-31 11:52:57 -0700486 for (i = 0; i < ohci->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 u32 status = roothub_portstatus (ohci, i);
488
Alan Stern8d1a2432006-09-26 14:46:16 -0400489 /* can't autostop if ports are connected */
490 any_connected |= (status & RH_PS_CCS);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
493 | RH_PS_OCIC | RH_PS_PRSC)) {
494 changed = 1;
495 if (i < 7)
496 buf [0] |= 1 << (i + 1);
497 else
498 buf [1] |= 1 << (i - 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Alan Stern1f9fc882006-11-20 11:06:59 -0500502 hcd->poll_rh = ohci_root_hub_state_changes(ohci, changed,
503 any_connected);
David Brownelld4139842006-08-04 11:31:55 -0700504
505done:
506 spin_unlock_irqrestore (&ohci->lock, flags);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return changed ? length : 0;
509}
510
511/*-------------------------------------------------------------------------*/
512
513static void
514ohci_hub_descriptor (
515 struct ohci_hcd *ohci,
516 struct usb_hub_descriptor *desc
517) {
518 u32 rh = roothub_a (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 u16 temp;
520
521 desc->bDescriptorType = 0x29;
522 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
523 desc->bHubContrCurrent = 0;
524
David Brownellfdd13b32005-08-31 11:52:57 -0700525 desc->bNbrPorts = ohci->num_ports;
526 temp = 1 + (ohci->num_ports / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 desc->bDescLength = 7 + 2 * temp;
528
529 temp = 0;
530 if (rh & RH_A_NPS) /* no power switching? */
531 temp |= 0x0002;
David Brownelldd9048a2006-12-05 03:18:31 -0800532 if (rh & RH_A_PSM) /* per-port power switching? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 temp |= 0x0001;
534 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
535 temp |= 0x0010;
536 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
537 temp |= 0x0008;
538 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
539
540 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
541 rh = roothub_b (ohci);
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700542 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 desc->bitmap [0] = rh & RH_B_DR;
David Brownellfdd13b32005-08-31 11:52:57 -0700544 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700546 desc->bitmap [2] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 } else
548 desc->bitmap [1] = 0xff;
549}
550
551/*-------------------------------------------------------------------------*/
552
553#ifdef CONFIG_USB_OTG
554
555static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
556{
557 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
558 u32 status;
559
560 if (!port)
561 return -EINVAL;
562 port--;
563
564 /* start port reset before HNP protocol times out */
565 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
566 if (!(status & RH_PS_CCS))
567 return -ENODEV;
568
569 /* khubd will finish the reset later */
570 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
571 return 0;
572}
573
574static void start_hnp(struct ohci_hcd *ohci);
575
576#else
577
578#define ohci_start_port_reset NULL
579
580#endif
581
582/*-------------------------------------------------------------------------*/
583
584
585/* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
586 * not necessarily continuous ... to guard against resume signaling.
587 * The short timeout is safe for non-root hubs, and is backward-compatible
588 * with earlier Linux hosts.
589 */
590#ifdef CONFIG_USB_SUSPEND
591#define PORT_RESET_MSEC 50
592#else
593#define PORT_RESET_MSEC 10
594#endif
595
596/* this timer value might be vendor-specific ... */
597#define PORT_RESET_HW_MSEC 10
598
599/* wrap-aware logic morphed from <linux/jiffies.h> */
600#define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
601
602/* called from some task, normally khubd */
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800603static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
606 u32 temp;
607 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
608 u16 reset_done = now + PORT_RESET_MSEC;
David Brownelle01e7fe2008-02-02 02:42:52 -0800609 int limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /* build a "continuous enough" reset signal, with up to
612 * 3msec gap between pulses. scheduler HZ==100 must work;
613 * this might need to be deadline-scheduled.
614 */
615 do {
David Brownelle01e7fe2008-02-02 02:42:52 -0800616 int limit_2;
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* spin until any current reset finishes */
David Brownelle01e7fe2008-02-02 02:42:52 -0800619 limit_2 = PORT_RESET_HW_MSEC * 2;
620 while (--limit_2 >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 temp = ohci_readl (ohci, portstat);
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800622 /* handle e.g. CardBus eject */
623 if (temp == ~(u32)0)
624 return -ESHUTDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (!(temp & RH_PS_PRS))
626 break;
627 udelay (500);
David Brownelldd9048a2006-12-05 03:18:31 -0800628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
David Brownelle01e7fe2008-02-02 02:42:52 -0800630 /* timeout (a hardware error) has been observed when
631 * EHCI sets CF while this driver is resetting a port;
632 * presumably other disconnect paths might do it too.
633 */
634 if (limit_2 < 0) {
635 ohci_dbg(ohci,
636 "port[%d] reset timeout, stat %08x\n",
637 port, temp);
638 break;
639 }
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (!(temp & RH_PS_CCS))
642 break;
643 if (temp & RH_PS_PRSC)
644 ohci_writel (ohci, RH_PS_PRSC, portstat);
645
646 /* start the next reset, sleep till it's probably done */
647 ohci_writel (ohci, RH_PS_PRS, portstat);
648 msleep(PORT_RESET_HW_MSEC);
649 now = ohci_readl(ohci, &ohci->regs->fmnumber);
David Brownelle01e7fe2008-02-02 02:42:52 -0800650 } while (tick_before(now, reset_done) && --limit_1 >= 0);
651
652 /* caller synchronizes using PRSC ... and handles PRS
653 * still being set when this returns.
654 */
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800655
656 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
659static int ohci_hub_control (
660 struct usb_hcd *hcd,
661 u16 typeReq,
662 u16 wValue,
663 u16 wIndex,
664 char *buf,
665 u16 wLength
666) {
667 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
668 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
669 u32 temp;
670 int retval = 0;
671
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100672 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
673 return -ESHUTDOWN;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 switch (typeReq) {
676 case ClearHubFeature:
677 switch (wValue) {
678 case C_HUB_OVER_CURRENT:
679 ohci_writel (ohci, RH_HS_OCIC,
680 &ohci->regs->roothub.status);
681 case C_HUB_LOCAL_POWER:
682 break;
683 default:
684 goto error;
685 }
686 break;
687 case ClearPortFeature:
688 if (!wIndex || wIndex > ports)
689 goto error;
690 wIndex--;
691
692 switch (wValue) {
693 case USB_PORT_FEAT_ENABLE:
694 temp = RH_PS_CCS;
695 break;
696 case USB_PORT_FEAT_C_ENABLE:
697 temp = RH_PS_PESC;
698 break;
699 case USB_PORT_FEAT_SUSPEND:
700 temp = RH_PS_POCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 case USB_PORT_FEAT_C_SUSPEND:
703 temp = RH_PS_PSSC;
704 break;
705 case USB_PORT_FEAT_POWER:
706 temp = RH_PS_LSDA;
707 break;
708 case USB_PORT_FEAT_C_CONNECTION:
709 temp = RH_PS_CSC;
710 break;
711 case USB_PORT_FEAT_C_OVER_CURRENT:
712 temp = RH_PS_OCIC;
713 break;
714 case USB_PORT_FEAT_C_RESET:
715 temp = RH_PS_PRSC;
716 break;
717 default:
718 goto error;
719 }
720 ohci_writel (ohci, temp,
721 &ohci->regs->roothub.portstatus [wIndex]);
722 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
723 break;
724 case GetHubDescriptor:
725 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
726 break;
727 case GetHubStatus:
728 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
David Miller92164c5d2006-06-21 22:26:09 -0700729 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 break;
731 case GetPortStatus:
732 if (!wIndex || wIndex > ports)
733 goto error;
734 wIndex--;
735 temp = roothub_portstatus (ohci, wIndex);
David Miller92164c5d2006-06-21 22:26:09 -0700736 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738#ifndef OHCI_VERBOSE_DEBUG
739 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
740#endif
741 dbg_port (ohci, "GetStatus", wIndex, temp);
742 break;
743 case SetHubFeature:
744 switch (wValue) {
745 case C_HUB_OVER_CURRENT:
746 // FIXME: this can be cleared, yes?
747 case C_HUB_LOCAL_POWER:
748 break;
749 default:
750 goto error;
751 }
752 break;
753 case SetPortFeature:
754 if (!wIndex || wIndex > ports)
755 goto error;
756 wIndex--;
757 switch (wValue) {
758 case USB_PORT_FEAT_SUSPEND:
759#ifdef CONFIG_USB_OTG
760 if (hcd->self.otg_port == (wIndex + 1)
761 && hcd->self.b_hnp_enable)
762 start_hnp(ohci);
763 else
764#endif
765 ohci_writel (ohci, RH_PS_PSS,
766 &ohci->regs->roothub.portstatus [wIndex]);
767 break;
768 case USB_PORT_FEAT_POWER:
769 ohci_writel (ohci, RH_PS_PPS,
770 &ohci->regs->roothub.portstatus [wIndex]);
771 break;
772 case USB_PORT_FEAT_RESET:
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800773 retval = root_port_reset (ohci, wIndex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 break;
775 default:
776 goto error;
777 }
778 break;
779
780 default:
781error:
782 /* "protocol stall" on error */
783 retval = -EPIPE;
784 }
785 return retval;
786}
787