1 /*
2 * OHCI HCD (Host Controller Driver) for USB.
3 *
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
6 *
7 * 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" : "", \
26 \
27 (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" : "", \
32 \
33 (temp & RH_PS_PES) ? " PES" : "", \
34 (temp & RH_PS_CCS) ? " CCS" : "" \
35 );
36
37 /*-------------------------------------------------------------------------*/
38
39 #if defined(CONFIG_USB_SUSPEND) || defined(CONFIG_PM)
40
41 #define OHCI_SCHED_ENABLES \
42 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
43
44 static void dl_done_list (struct ohci_hcd *, struct pt_regs *);
45 static void finish_unlinks (struct ohci_hcd *, u16 , struct pt_regs *);
46 static int ohci_restart (struct ohci_hcd *ohci);
47
48 static int ohci_hub_suspend (struct usb_hcd *hcd)
49 {
50 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
51 int status = 0;
52 unsigned long flags;
53
54 spin_lock_irqsave (&ohci->lock, flags);
55
56 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
57 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
58 case OHCI_USB_RESUME:
59 ohci_dbg (ohci, "resume/suspend?\n");
60 ohci->hc_control &= ~OHCI_CTRL_HCFS;
61 ohci->hc_control |= OHCI_USB_RESET;
62 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
63 (void) ohci_readl (ohci, &ohci->regs->control);
64 /* FALL THROUGH */
65 case OHCI_USB_RESET:
66 status = -EBUSY;
67 ohci_dbg (ohci, "needs reinit!\n");
68 goto done;
69 case OHCI_USB_SUSPEND:
70 ohci_dbg (ohci, "already suspended\n");
71 goto done;
72 }
73 ohci_dbg (ohci, "suspend root hub\n");
74
75 /* First stop any processing */
76 hcd->state = USB_STATE_QUIESCING;
77 if (ohci->hc_control & OHCI_SCHED_ENABLES) {
78 int limit;
79
80 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
81 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
82 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
83 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
84
85 /* sched disables take effect on the next frame,
86 * then the last WDH could take 6+ msec
87 */
88 ohci_dbg (ohci, "stopping schedules ...\n");
89 limit = 2000;
90 while (limit > 0) {
91 udelay (250);
92 limit =- 250;
93 if (ohci_readl (ohci, &ohci->regs->intrstatus)
94 & OHCI_INTR_SF)
95 break;
96 }
97 dl_done_list (ohci, NULL);
98 mdelay (7);
99 }
100 dl_done_list (ohci, NULL);
101 finish_unlinks (ohci, ohci_frame_no(ohci), NULL);
102 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
103 &ohci->regs->intrstatus);
104
105 /* maybe resume can wake root hub */
106 if (hcd->remote_wakeup)
107 ohci->hc_control |= OHCI_CTRL_RWE;
108 else
109 ohci->hc_control &= ~OHCI_CTRL_RWE;
110
111 /* Suspend hub */
112 ohci->hc_control &= ~OHCI_CTRL_HCFS;
113 ohci->hc_control |= OHCI_USB_SUSPEND;
114 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
115 (void) ohci_readl (ohci, &ohci->regs->control);
116
117 /* no resumes until devices finish suspending */
118 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
119
120 done:
121 if (status == 0)
122 hcd->state = HCD_STATE_SUSPENDED;
123 spin_unlock_irqrestore (&ohci->lock, flags);
124 return status;
125 }
126
127 static inline struct ed *find_head (struct ed *ed)
128 {
129 /* for bulk and control lists */
130 while (ed->ed_prev)
131 ed = ed->ed_prev;
132 return ed;
133 }
134
135 /* caller has locked the root hub */
136 static int ohci_hub_resume (struct usb_hcd *hcd)
137 {
138 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
139 u32 temp, enables;
140 int status = -EINPROGRESS;
141
142 if (time_before (jiffies, ohci->next_statechange))
143 msleep(5);
144
145 spin_lock_irq (&ohci->lock);
146 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
147
148 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
149 /* this can happen after suspend-to-disk */
150 if (hcd->state == USB_STATE_RESUMING) {
151 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
152 ohci->hc_control);
153 status = -EBUSY;
154 /* this happens when pmcore resumes HC then root */
155 } else {
156 ohci_dbg (ohci, "duplicate resume\n");
157 status = 0;
158 }
159 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
160 case OHCI_USB_SUSPEND:
161 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
162 ohci->hc_control |= OHCI_USB_RESUME;
163 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
164 (void) ohci_readl (ohci, &ohci->regs->control);
165 ohci_dbg (ohci, "resume root hub\n");
166 break;
167 case OHCI_USB_RESUME:
168 /* HCFS changes sometime after INTR_RD */
169 ohci_info (ohci, "wakeup\n");
170 break;
171 case OHCI_USB_OPER:
172 ohci_dbg (ohci, "odd resume\n");
173 status = 0;
174 break;
175 default: /* RESET, we lost power */
176 ohci_dbg (ohci, "root hub hardware reset\n");
177 status = -EBUSY;
178 }
179 spin_unlock_irq (&ohci->lock);
180 if (status == -EBUSY) {
181 (void) ohci_init (ohci);
182 return ohci_restart (ohci);
183 }
184 if (status != -EINPROGRESS)
185 return status;
186
187 temp = roothub_a (ohci) & RH_A_NDP;
188 enables = 0;
189 while (temp--) {
190 u32 stat = ohci_readl (ohci,
191 &ohci->regs->roothub.portstatus [temp]);
192
193 /* force global, not selective, resume */
194 if (!(stat & RH_PS_PSS))
195 continue;
196 ohci_writel (ohci, RH_PS_POCI,
197 &ohci->regs->roothub.portstatus [temp]);
198 }
199
200 /* Some controllers (lucent) need extra-long delays */
201 hcd->state = USB_STATE_RESUMING;
202 mdelay (20 /* usb 11.5.1.10 */ + 15);
203
204 temp = ohci_readl (ohci, &ohci->regs->control);
205 temp &= OHCI_CTRL_HCFS;
206 if (temp != OHCI_USB_RESUME) {
207 ohci_err (ohci, "controller won't resume\n");
208 return -EBUSY;
209 }
210
211 /* disable old schedule state, reinit from scratch */
212 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
213 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
214 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
215 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
216 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
217 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
218
219 periodic_reinit (ohci);
220
221 /* interrupts might have been disabled */
222 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
223 if (ohci->ed_rm_list)
224 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
225 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
226 &ohci->regs->intrstatus);
227
228 /* Then re-enable operations */
229 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
230 (void) ohci_readl (ohci, &ohci->regs->control);
231 msleep (3);
232
233 temp = OHCI_CONTROL_INIT | OHCI_USB_OPER;
234 if (hcd->can_wakeup)
235 temp |= OHCI_CTRL_RWC;
236 ohci->hc_control = temp;
237 ohci_writel (ohci, temp, &ohci->regs->control);
238 (void) ohci_readl (ohci, &ohci->regs->control);
239
240 /* TRSMRCY */
241 msleep (10);
242
243 /* keep it alive for ~5x suspend + resume costs */
244 ohci->next_statechange = jiffies + msecs_to_jiffies (250);
245
246 /* maybe turn schedules back on */
247 enables = 0;
248 temp = 0;
249 if (!ohci->ed_rm_list) {
250 if (ohci->ed_controltail) {
251 ohci_writel (ohci,
252 find_head (ohci->ed_controltail)->dma,
253 &ohci->regs->ed_controlhead);
254 enables |= OHCI_CTRL_CLE;
255 temp |= OHCI_CLF;
256 }
257 if (ohci->ed_bulktail) {
258 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
259 &ohci->regs->ed_bulkhead);
260 enables |= OHCI_CTRL_BLE;
261 temp |= OHCI_BLF;
262 }
263 }
264 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
265 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
266 if (enables) {
267 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
268 ohci->hc_control |= enables;
269 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
270 if (temp)
271 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
272 (void) ohci_readl (ohci, &ohci->regs->control);
273 }
274
275 hcd->state = USB_STATE_RUNNING;
276 return 0;
277 }
278
279 static void ohci_rh_resume (void *_hcd)
280 {
281 struct usb_hcd *hcd = _hcd;
282
283 usb_lock_device (hcd->self.root_hub);
284 (void) ohci_hub_resume (hcd);
285 usb_unlock_device (hcd->self.root_hub);
286 }
287
288 #else
289
290 static void ohci_rh_resume (void *_hcd)
291 {
292 struct ohci_hcd *ohci = hcd_to_ohci (_hcd);
293 ohci_dbg(ohci, "rh_resume ??\n");
294 }
295
296 #endif /* CONFIG_USB_SUSPEND || CONFIG_PM */
297
298 /*-------------------------------------------------------------------------*/
299
300 /* build "status change" packet (one or two bytes) from HC registers */
301
302 static int
303 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
304 {
305 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
306 int ports, i, changed = 0, length = 1;
307 int can_suspend = hcd->can_wakeup;
308 unsigned long flags;
309
310 spin_lock_irqsave (&ohci->lock, flags);
311
312 /* handle autosuspended root: finish resuming before
313 * letting khubd or root hub timer see state changes.
314 */
315 if ((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER
316 || !HCD_IS_RUNNING(hcd->state)) {
317 can_suspend = 0;
318 goto done;
319 }
320
321 ports = roothub_a (ohci) & RH_A_NDP;
322 if (ports > MAX_ROOT_PORTS) {
323 ohci_err (ohci, "bogus NDP=%d, rereads as NDP=%d\n", ports,
324 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
325 /* retry later; "should not happen" */
326 goto done;
327 }
328
329 /* init status */
330 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
331 buf [0] = changed = 1;
332 else
333 buf [0] = 0;
334 if (ports > 7) {
335 buf [1] = 0;
336 length++;
337 }
338
339 /* look at each port */
340 for (i = 0; i < ports; i++) {
341 u32 status = roothub_portstatus (ohci, i);
342
343 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
344 | RH_PS_OCIC | RH_PS_PRSC)) {
345 changed = 1;
346 if (i < 7)
347 buf [0] |= 1 << (i + 1);
348 else
349 buf [1] |= 1 << (i - 7);
350 continue;
351 }
352
353 /* can suspend if no ports are enabled; or if all all
354 * enabled ports are suspended AND remote wakeup is on.
355 */
356 if (!(status & RH_PS_CCS))
357 continue;
358 if ((status & RH_PS_PSS) && hcd->remote_wakeup)
359 continue;
360 can_suspend = 0;
361 }
362 done:
363 spin_unlock_irqrestore (&ohci->lock, flags);
364
365 #ifdef CONFIG_PM
366 /* save power by suspending idle root hubs;
367 * INTR_RD wakes us when there's work
368 * NOTE: if we can do this, we don't need a root hub timer!
369 */
370 if (can_suspend
371 && !changed
372 && !ohci->ed_rm_list
373 && ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES)
374 & ohci->hc_control)
375 == OHCI_USB_OPER
376 && time_after (jiffies, ohci->next_statechange)
377 && usb_trylock_device (hcd->self.root_hub)
378 ) {
379 ohci_vdbg (ohci, "autosuspend\n");
380 (void) ohci_hub_suspend (hcd);
381 hcd->state = USB_STATE_RUNNING;
382 usb_unlock_device (hcd->self.root_hub);
383 }
384 #endif
385
386 return changed ? length : 0;
387 }
388
389 /*-------------------------------------------------------------------------*/
390
391 static void
392 ohci_hub_descriptor (
393 struct ohci_hcd *ohci,
394 struct usb_hub_descriptor *desc
395 ) {
396 u32 rh = roothub_a (ohci);
397 int ports = rh & RH_A_NDP;
398 u16 temp;
399
400 desc->bDescriptorType = 0x29;
401 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
402 desc->bHubContrCurrent = 0;
403
404 desc->bNbrPorts = ports;
405 temp = 1 + (ports / 8);
406 desc->bDescLength = 7 + 2 * temp;
407
408 temp = 0;
409 if (rh & RH_A_NPS) /* no power switching? */
410 temp |= 0x0002;
411 if (rh & RH_A_PSM) /* per-port power switching? */
412 temp |= 0x0001;
413 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
414 temp |= 0x0010;
415 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
416 temp |= 0x0008;
417 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
418
419 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
420 rh = roothub_b (ohci);
421 desc->bitmap [0] = rh & RH_B_DR;
422 if (ports > 7) {
423 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
424 desc->bitmap [2] = desc->bitmap [3] = 0xff;
425 } else
426 desc->bitmap [1] = 0xff;
427 }
428
429 /*-------------------------------------------------------------------------*/
430
431 #ifdef CONFIG_USB_OTG
432
433 static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
434 {
435 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
436 u32 status;
437
438 if (!port)
439 return -EINVAL;
440 port--;
441
442 /* start port reset before HNP protocol times out */
443 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
444 if (!(status & RH_PS_CCS))
445 return -ENODEV;
446
447 /* khubd will finish the reset later */
448 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
449 return 0;
450 }
451
452 static void start_hnp(struct ohci_hcd *ohci);
453
454 #else
455
456 #define ohci_start_port_reset NULL
457
458 #endif
459
460 /*-------------------------------------------------------------------------*/
461
462
463 /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
464 * not necessarily continuous ... to guard against resume signaling.
465 * The short timeout is safe for non-root hubs, and is backward-compatible
466 * with earlier Linux hosts.
467 */
468 #ifdef CONFIG_USB_SUSPEND
469 #define PORT_RESET_MSEC 50
470 #else
471 #define PORT_RESET_MSEC 10
472 #endif
473
474 /* this timer value might be vendor-specific ... */
475 #define PORT_RESET_HW_MSEC 10
476
477 /* wrap-aware logic morphed from <linux/jiffies.h> */
478 #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
479
480 /* called from some task, normally khubd */
481 static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
482 {
483 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
484 u32 temp;
485 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
486 u16 reset_done = now + PORT_RESET_MSEC;
487
488 /* build a "continuous enough" reset signal, with up to
489 * 3msec gap between pulses. scheduler HZ==100 must work;
490 * this might need to be deadline-scheduled.
491 */
492 do {
493 /* spin until any current reset finishes */
494 for (;;) {
495 temp = ohci_readl (ohci, portstat);
496 if (!(temp & RH_PS_PRS))
497 break;
498 udelay (500);
499 }
500
501 if (!(temp & RH_PS_CCS))
502 break;
503 if (temp & RH_PS_PRSC)
504 ohci_writel (ohci, RH_PS_PRSC, portstat);
505
506 /* start the next reset, sleep till it's probably done */
507 ohci_writel (ohci, RH_PS_PRS, portstat);
508 msleep(PORT_RESET_HW_MSEC);
509 now = ohci_readl(ohci, &ohci->regs->fmnumber);
510 } while (tick_before(now, reset_done));
511 /* caller synchronizes using PRSC */
512 }
513
514 static int ohci_hub_control (
515 struct usb_hcd *hcd,
516 u16 typeReq,
517 u16 wValue,
518 u16 wIndex,
519 char *buf,
520 u16 wLength
521 ) {
522 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
523 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
524 u32 temp;
525 int retval = 0;
526
527 switch (typeReq) {
528 case ClearHubFeature:
529 switch (wValue) {
530 case C_HUB_OVER_CURRENT:
531 ohci_writel (ohci, RH_HS_OCIC,
532 &ohci->regs->roothub.status);
533 case C_HUB_LOCAL_POWER:
534 break;
535 default:
536 goto error;
537 }
538 break;
539 case ClearPortFeature:
540 if (!wIndex || wIndex > ports)
541 goto error;
542 wIndex--;
543
544 switch (wValue) {
545 case USB_PORT_FEAT_ENABLE:
546 temp = RH_PS_CCS;
547 break;
548 case USB_PORT_FEAT_C_ENABLE:
549 temp = RH_PS_PESC;
550 break;
551 case USB_PORT_FEAT_SUSPEND:
552 temp = RH_PS_POCI;
553 if ((ohci->hc_control & OHCI_CTRL_HCFS)
554 != OHCI_USB_OPER)
555 schedule_work (&ohci->rh_resume);
556 break;
557 case USB_PORT_FEAT_C_SUSPEND:
558 temp = RH_PS_PSSC;
559 break;
560 case USB_PORT_FEAT_POWER:
561 temp = RH_PS_LSDA;
562 break;
563 case USB_PORT_FEAT_C_CONNECTION:
564 temp = RH_PS_CSC;
565 break;
566 case USB_PORT_FEAT_C_OVER_CURRENT:
567 temp = RH_PS_OCIC;
568 break;
569 case USB_PORT_FEAT_C_RESET:
570 temp = RH_PS_PRSC;
571 break;
572 default:
573 goto error;
574 }
575 ohci_writel (ohci, temp,
576 &ohci->regs->roothub.portstatus [wIndex]);
577 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
578 break;
579 case GetHubDescriptor:
580 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
581 break;
582 case GetHubStatus:
583 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
584 *(__le32 *) buf = cpu_to_le32 (temp);
585 break;
586 case GetPortStatus:
587 if (!wIndex || wIndex > ports)
588 goto error;
589 wIndex--;
590 temp = roothub_portstatus (ohci, wIndex);
591 *(__le32 *) buf = cpu_to_le32 (temp);
592
593 #ifndef OHCI_VERBOSE_DEBUG
594 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
595 #endif
596 dbg_port (ohci, "GetStatus", wIndex, temp);
597 break;
598 case SetHubFeature:
599 switch (wValue) {
600 case C_HUB_OVER_CURRENT:
601 // FIXME: this can be cleared, yes?
602 case C_HUB_LOCAL_POWER:
603 break;
604 default:
605 goto error;
606 }
607 break;
608 case SetPortFeature:
609 if (!wIndex || wIndex > ports)
610 goto error;
611 wIndex--;
612 switch (wValue) {
613 case USB_PORT_FEAT_SUSPEND:
614 #ifdef CONFIG_USB_OTG
615 if (hcd->self.otg_port == (wIndex + 1)
616 && hcd->self.b_hnp_enable)
617 start_hnp(ohci);
618 else
619 #endif
620 ohci_writel (ohci, RH_PS_PSS,
621 &ohci->regs->roothub.portstatus [wIndex]);
622 break;
623 case USB_PORT_FEAT_POWER:
624 ohci_writel (ohci, RH_PS_PPS,
625 &ohci->regs->roothub.portstatus [wIndex]);
626 break;
627 case USB_PORT_FEAT_RESET:
628 root_port_reset (ohci, wIndex);
629 break;
630 default:
631 goto error;
632 }
633 break;
634
635 default:
636 error:
637 /* "protocol stall" on error */
638 retval = -EPIPE;
639 }
640 return retval;
641 }
642
643
|
This page was automatically generated by the
LXR engine.
|