blob: b56739221d11f27d6d6e1b8d04c833fe56bdbd6c [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 Torvalds09ca8ad2008-07-06 10:27:25 -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
44 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);
49}
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 Stern5f474932008-04-14 12:17:49 -0400109 ohci_writel(ohci, OHCI_INTR_RHSC | OHCI_INTR_RD,
110 &ohci->regs->intrdisable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 ohci->hc_control &= ~OHCI_CTRL_RWE;
Alan Stern1f7e1a32006-09-25 15:41:21 -0400112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
David Brownellf197b2c2005-09-22 22:42:53 -0700114 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
115 * which doesn't imply ports will first be individually suspended.
116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 ohci->hc_control &= ~OHCI_CTRL_HCFS;
118 ohci->hc_control |= OHCI_USB_SUSPEND;
119 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
120 (void) ohci_readl (ohci, &ohci->regs->control);
121
122 /* no resumes until devices finish suspending */
Alan Stern8d1a2432006-09-26 14:46:16 -0400123 if (!autostop) {
124 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
125 ohci->autostop = 0;
126 }
David Brownelld4139842006-08-04 11:31:55 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return status;
130}
131
132static inline struct ed *find_head (struct ed *ed)
133{
134 /* for bulk and control lists */
135 while (ed->ed_prev)
136 ed = ed->ed_prev;
137 return ed;
138}
139
140/* caller has locked the root hub */
Alan Stern8d1a2432006-09-26 14:46:16 -0400141static int ohci_rh_resume (struct ohci_hcd *ohci)
142__releases(ohci->lock)
143__acquires(ohci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Alan Stern8d1a2432006-09-26 14:46:16 -0400145 struct usb_hcd *hcd = ohci_to_hcd (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 u32 temp, enables;
147 int status = -EINPROGRESS;
Alan Stern8d1a2432006-09-26 14:46:16 -0400148 int autostopped = ohci->autostop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Alan Stern8d1a2432006-09-26 14:46:16 -0400150 ohci->autostop = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
152
153 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
David Brownellf197b2c2005-09-22 22:42:53 -0700154 /* this can happen after resuming a swsusp snapshot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (hcd->state == HC_STATE_RESUMING) {
156 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
157 ohci->hc_control);
158 status = -EBUSY;
159 /* this happens when pmcore resumes HC then root */
160 } else {
161 ohci_dbg (ohci, "duplicate resume\n");
162 status = 0;
163 }
164 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
165 case OHCI_USB_SUSPEND:
166 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
167 ohci->hc_control |= OHCI_USB_RESUME;
168 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
169 (void) ohci_readl (ohci, &ohci->regs->control);
Alan Stern8d1a2432006-09-26 14:46:16 -0400170 ohci_dbg (ohci, "%s root hub\n",
171 autostopped ? "auto-start" : "resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 break;
173 case OHCI_USB_RESUME:
174 /* HCFS changes sometime after INTR_RD */
Alan Stern1b7be3c2006-11-06 12:05:00 -0500175 ohci_dbg(ohci, "%swakeup root hub\n",
Alan Stern583cead2006-10-24 12:04:22 -0400176 autostopped ? "auto-" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 break;
178 case OHCI_USB_OPER:
David Brownellf197b2c2005-09-22 22:42:53 -0700179 /* this can happen after resuming a swsusp snapshot */
180 ohci_dbg (ohci, "snapshot resume? reinit\n");
181 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 break;
183 default: /* RESET, we lost power */
David Brownellf197b2c2005-09-22 22:42:53 -0700184 ohci_dbg (ohci, "lost power\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 status = -EBUSY;
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (status == -EBUSY) {
Alan Stern8d1a2432006-09-26 14:46:16 -0400188 if (!autostopped) {
Alan Stern8d1a2432006-09-26 14:46:16 -0400189 spin_unlock_irq (&ohci->lock);
190 (void) ohci_init (ohci);
191 status = ohci_restart (ohci);
Michael Hanselmannd576bb92007-05-31 23:34:27 +0200192
193 usb_root_hub_lost_power(hcd->self.root_hub);
194
Alan Stern8d1a2432006-09-26 14:46:16 -0400195 spin_lock_irq (&ohci->lock);
196 }
197 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 if (status != -EINPROGRESS)
200 return status;
Alan Stern8d1a2432006-09-26 14:46:16 -0400201 if (autostopped)
202 goto skip_resume;
203 spin_unlock_irq (&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Some controllers (lucent erratum) need extra-long delays */
David Brownellf197b2c2005-09-22 22:42:53 -0700206 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 temp = ohci_readl (ohci, &ohci->regs->control);
209 temp &= OHCI_CTRL_HCFS;
210 if (temp != OHCI_USB_RESUME) {
211 ohci_err (ohci, "controller won't resume\n");
Alan Stern565402b2006-10-27 10:35:01 -0400212 spin_lock_irq(&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return -EBUSY;
214 }
215
216 /* disable old schedule state, reinit from scratch */
217 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
218 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
219 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
220 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
221 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
222 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
223
224 /* Sometimes PCI D3 suspend trashes frame timings ... */
225 periodic_reinit (ohci);
226
Alan Stern8d1a2432006-09-26 14:46:16 -0400227 /* the following code is executed with ohci->lock held and
228 * irqs disabled if and only if autostopped is true
229 */
230
231skip_resume:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* interrupts might have been disabled */
233 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
234 if (ohci->ed_rm_list)
235 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /* Then re-enable operations */
238 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
239 (void) ohci_readl (ohci, &ohci->regs->control);
Alan Stern8d1a2432006-09-26 14:46:16 -0400240 if (!autostopped)
241 msleep (3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
David Brownell6a9062f2006-01-23 15:28:07 -0800243 temp = ohci->hc_control;
244 temp &= OHCI_CTRL_RWC;
245 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 ohci->hc_control = temp;
247 ohci_writel (ohci, temp, &ohci->regs->control);
248 (void) ohci_readl (ohci, &ohci->regs->control);
249
250 /* TRSMRCY */
Alan Stern8d1a2432006-09-26 14:46:16 -0400251 if (!autostopped) {
252 msleep (10);
253 spin_lock_irq (&ohci->lock);
254 }
255 /* now ohci->lock is always held and irqs are always disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
David Brownelld4139842006-08-04 11:31:55 -0700257 /* keep it alive for more than ~5x suspend + resume costs */
258 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /* maybe turn schedules back on */
261 enables = 0;
262 temp = 0;
263 if (!ohci->ed_rm_list) {
264 if (ohci->ed_controltail) {
265 ohci_writel (ohci,
266 find_head (ohci->ed_controltail)->dma,
267 &ohci->regs->ed_controlhead);
268 enables |= OHCI_CTRL_CLE;
269 temp |= OHCI_CLF;
270 }
271 if (ohci->ed_bulktail) {
272 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
273 &ohci->regs->ed_bulkhead);
274 enables |= OHCI_CTRL_BLE;
275 temp |= OHCI_BLF;
276 }
277 }
278 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
279 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
280 if (enables) {
281 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
282 ohci->hc_control |= enables;
283 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
284 if (temp)
285 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
286 (void) ohci_readl (ohci, &ohci->regs->control);
287 }
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return 0;
290}
291
Alan Stern8d1a2432006-09-26 14:46:16 -0400292static int ohci_bus_suspend (struct usb_hcd *hcd)
293{
294 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
295 int rc;
296
297 spin_lock_irq (&ohci->lock);
298
299 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
300 rc = -ESHUTDOWN;
301 else
302 rc = ohci_rh_suspend (ohci, 0);
303 spin_unlock_irq (&ohci->lock);
304 return rc;
305}
306
307static int ohci_bus_resume (struct usb_hcd *hcd)
308{
309 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
310 int rc;
311
312 if (time_before (jiffies, ohci->next_statechange))
313 msleep(5);
314
315 spin_lock_irq (&ohci->lock);
316
317 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
318 rc = -ESHUTDOWN;
319 else
320 rc = ohci_rh_resume (ohci);
321 spin_unlock_irq (&ohci->lock);
322
323 /* poll until we know a device is connected or we autostop */
324 if (rc == 0)
325 usb_hcd_poll_rh_status(hcd);
326 return rc;
327}
328
Alan Stern43bbb7e2008-04-03 18:03:17 -0400329/* Carry out the final steps of resuming the controller device */
330static void ohci_finish_controller_resume(struct usb_hcd *hcd)
331{
332 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
333 int port;
334 bool need_reinit = false;
335
336 /* See if the controller is already running or has been reset */
337 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
338 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
339 need_reinit = true;
340 } else {
341 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
342 case OHCI_USB_OPER:
343 case OHCI_USB_RESET:
344 need_reinit = true;
345 }
346 }
347
348 /* If needed, reinitialize and suspend the root hub */
349 if (need_reinit) {
350 spin_lock_irq(&ohci->lock);
351 hcd->state = HC_STATE_RESUMING;
352 ohci_rh_resume(ohci);
353 hcd->state = HC_STATE_QUIESCING;
354 ohci_rh_suspend(ohci, 0);
355 hcd->state = HC_STATE_SUSPENDED;
356 spin_unlock_irq(&ohci->lock);
357 }
358
359 /* Normally just turn on port power and enable interrupts */
360 else {
361 ohci_dbg(ohci, "powerup ports\n");
362 for (port = 0; port < ohci->num_ports; port++)
363 ohci_writel(ohci, RH_PS_PPS,
364 &ohci->regs->roothub.portstatus[port]);
365
366 ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrenable);
367 ohci_readl(ohci, &ohci->regs->intrenable);
368 msleep(20);
369 }
370}
371
Alan Stern1f9fc882006-11-20 11:06:59 -0500372/* Carry out polling-, autostop-, and autoresume-related state changes */
373static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
374 int any_connected)
375{
376 int poll_rh = 1;
377
378 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
379
380 case OHCI_USB_OPER:
Linus Torvalds09ca8ad2008-07-06 10:27:25 -0700381 /* keep on polling until we know a device is connected
382 * and RHSC is enabled */
Alan Stern1f9fc882006-11-20 11:06:59 -0500383 if (!ohci->autostop) {
384 if (any_connected ||
385 !device_may_wakeup(&ohci_to_hcd(ohci)
386 ->self.root_hub->dev)) {
Linus Torvalds09ca8ad2008-07-06 10:27:25 -0700387 if (ohci_readl(ohci, &ohci->regs->intrenable) &
388 OHCI_INTR_RHSC)
Alan Stern1f9fc882006-11-20 11:06:59 -0500389 poll_rh = 0;
390 } else {
391 ohci->autostop = 1;
392 ohci->next_statechange = jiffies + HZ;
393 }
394
395 /* if no devices have been attached for one second, autostop */
396 } else {
397 if (changed || any_connected) {
398 ohci->autostop = 0;
399 ohci->next_statechange = jiffies +
400 STATECHANGE_DELAY;
Linus Torvalds09ca8ad2008-07-06 10:27:25 -0700401 } else if (time_after_eq(jiffies,
Alan Stern1f9fc882006-11-20 11:06:59 -0500402 ohci->next_statechange)
403 && !ohci->ed_rm_list
404 && !(ohci->hc_control &
405 OHCI_SCHED_ENABLES)) {
406 ohci_rh_suspend(ohci, 1);
407 }
408 }
409 break;
410
411 /* if there is a port change, autostart or ask to be resumed */
412 case OHCI_USB_SUSPEND:
413 case OHCI_USB_RESUME:
414 if (changed) {
415 if (ohci->autostop)
416 ohci_rh_resume(ohci);
417 else
418 usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
419 } else {
420 /* everything is idle, no need for polling */
421 poll_rh = 0;
422 }
423 break;
424 }
425 return poll_rh;
426}
427
428#else /* CONFIG_PM */
429
430static inline int ohci_rh_resume(struct ohci_hcd *ohci)
431{
432 return 0;
433}
434
435/* Carry out polling-related state changes.
436 * autostop isn't used when CONFIG_PM is turned off.
437 */
438static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
439 int any_connected)
440{
Linus Torvalds09ca8ad2008-07-06 10:27:25 -0700441 int poll_rh = 1;
Alan Sterne8721542008-04-14 12:17:56 -0400442
Linus Torvalds09ca8ad2008-07-06 10:27:25 -0700443 /* keep on polling until RHSC is enabled */
444 if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
445 poll_rh = 0;
446 return poll_rh;
Alan Stern1f9fc882006-11-20 11:06:59 -0500447}
448
David Brownell8ad7fe12005-09-13 19:59:11 -0700449#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451/*-------------------------------------------------------------------------*/
452
453/* build "status change" packet (one or two bytes) from HC registers */
454
455static int
456ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
457{
458 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
David Brownellfdd13b32005-08-31 11:52:57 -0700459 int i, changed = 0, length = 1;
Alan Stern052ac012006-10-27 10:33:11 -0400460 int any_connected = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 unsigned long flags;
462
463 spin_lock_irqsave (&ohci->lock, flags);
Alan Stern6fd75b12007-05-04 11:57:00 -0400464 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
465 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
David Brownellfdd13b32005-08-31 11:52:57 -0700467 /* undocumented erratum seen on at least rev D */
468 if ((ohci->flags & OHCI_QUIRK_AMD756)
469 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
470 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
472 /* retry later; "should not happen" */
473 goto done;
474 }
475
476 /* init status */
477 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
478 buf [0] = changed = 1;
479 else
480 buf [0] = 0;
David Brownellfdd13b32005-08-31 11:52:57 -0700481 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 buf [1] = 0;
483 length++;
484 }
485
486 /* look at each port */
David Brownellfdd13b32005-08-31 11:52:57 -0700487 for (i = 0; i < ohci->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 u32 status = roothub_portstatus (ohci, i);
489
Alan Stern8d1a2432006-09-26 14:46:16 -0400490 /* can't autostop if ports are connected */
491 any_connected |= (status & RH_PS_CCS);
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
494 | RH_PS_OCIC | RH_PS_PRSC)) {
495 changed = 1;
496 if (i < 7)
497 buf [0] |= 1 << (i + 1);
498 else
499 buf [1] |= 1 << (i - 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Alan Stern1f9fc882006-11-20 11:06:59 -0500503 hcd->poll_rh = ohci_root_hub_state_changes(ohci, changed,
504 any_connected);
David Brownelld4139842006-08-04 11:31:55 -0700505
506done:
507 spin_unlock_irqrestore (&ohci->lock, flags);
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return changed ? length : 0;
510}
511
512/*-------------------------------------------------------------------------*/
513
514static void
515ohci_hub_descriptor (
516 struct ohci_hcd *ohci,
517 struct usb_hub_descriptor *desc
518) {
519 u32 rh = roothub_a (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 u16 temp;
521
522 desc->bDescriptorType = 0x29;
523 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
524 desc->bHubContrCurrent = 0;
525
David Brownellfdd13b32005-08-31 11:52:57 -0700526 desc->bNbrPorts = ohci->num_ports;
527 temp = 1 + (ohci->num_ports / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 desc->bDescLength = 7 + 2 * temp;
529
530 temp = 0;
531 if (rh & RH_A_NPS) /* no power switching? */
532 temp |= 0x0002;
David Brownelldd9048a2006-12-05 03:18:31 -0800533 if (rh & RH_A_PSM) /* per-port power switching? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 temp |= 0x0001;
535 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
536 temp |= 0x0010;
537 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
538 temp |= 0x0008;
539 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
540
541 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
542 rh = roothub_b (ohci);
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700543 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 desc->bitmap [0] = rh & RH_B_DR;
David Brownellfdd13b32005-08-31 11:52:57 -0700545 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700547 desc->bitmap [2] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 } else
549 desc->bitmap [1] = 0xff;
550}
551
552/*-------------------------------------------------------------------------*/
553
554#ifdef CONFIG_USB_OTG
555
556static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
557{
558 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
559 u32 status;
560
561 if (!port)
562 return -EINVAL;
563 port--;
564
565 /* start port reset before HNP protocol times out */
566 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
567 if (!(status & RH_PS_CCS))
568 return -ENODEV;
569
570 /* khubd will finish the reset later */
571 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
572 return 0;
573}
574
575static void start_hnp(struct ohci_hcd *ohci);
576
577#else
578
579#define ohci_start_port_reset NULL
580
581#endif
582
583/*-------------------------------------------------------------------------*/
584
585
586/* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
587 * not necessarily continuous ... to guard against resume signaling.
588 * The short timeout is safe for non-root hubs, and is backward-compatible
589 * with earlier Linux hosts.
590 */
591#ifdef CONFIG_USB_SUSPEND
592#define PORT_RESET_MSEC 50
593#else
594#define PORT_RESET_MSEC 10
595#endif
596
597/* this timer value might be vendor-specific ... */
598#define PORT_RESET_HW_MSEC 10
599
600/* wrap-aware logic morphed from <linux/jiffies.h> */
601#define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
602
603/* called from some task, normally khubd */
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800604static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
Alan Stern1b7b61c2008-04-22 10:50:18 -0400607 u32 temp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
609 u16 reset_done = now + PORT_RESET_MSEC;
David Brownelle01e7fe2008-02-02 02:42:52 -0800610 int limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 /* build a "continuous enough" reset signal, with up to
613 * 3msec gap between pulses. scheduler HZ==100 must work;
614 * this might need to be deadline-scheduled.
615 */
616 do {
David Brownelle01e7fe2008-02-02 02:42:52 -0800617 int limit_2;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* spin until any current reset finishes */
David Brownelle01e7fe2008-02-02 02:42:52 -0800620 limit_2 = PORT_RESET_HW_MSEC * 2;
621 while (--limit_2 >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 temp = ohci_readl (ohci, portstat);
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800623 /* handle e.g. CardBus eject */
624 if (temp == ~(u32)0)
625 return -ESHUTDOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!(temp & RH_PS_PRS))
627 break;
628 udelay (500);
David Brownelldd9048a2006-12-05 03:18:31 -0800629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
David Brownelle01e7fe2008-02-02 02:42:52 -0800631 /* timeout (a hardware error) has been observed when
632 * EHCI sets CF while this driver is resetting a port;
633 * presumably other disconnect paths might do it too.
634 */
635 if (limit_2 < 0) {
636 ohci_dbg(ohci,
637 "port[%d] reset timeout, stat %08x\n",
638 port, temp);
639 break;
640 }
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!(temp & RH_PS_CCS))
643 break;
644 if (temp & RH_PS_PRSC)
645 ohci_writel (ohci, RH_PS_PRSC, portstat);
646
647 /* start the next reset, sleep till it's probably done */
648 ohci_writel (ohci, RH_PS_PRS, portstat);
649 msleep(PORT_RESET_HW_MSEC);
650 now = ohci_readl(ohci, &ohci->regs->fmnumber);
David Brownelle01e7fe2008-02-02 02:42:52 -0800651 } while (tick_before(now, reset_done) && --limit_1 >= 0);
652
653 /* caller synchronizes using PRSC ... and handles PRS
654 * still being set when this returns.
655 */
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800656
657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660static int ohci_hub_control (
661 struct usb_hcd *hcd,
662 u16 typeReq,
663 u16 wValue,
664 u16 wIndex,
665 char *buf,
666 u16 wLength
667) {
668 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
669 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
670 u32 temp;
671 int retval = 0;
672
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100673 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
674 return -ESHUTDOWN;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 switch (typeReq) {
677 case ClearHubFeature:
678 switch (wValue) {
679 case C_HUB_OVER_CURRENT:
680 ohci_writel (ohci, RH_HS_OCIC,
681 &ohci->regs->roothub.status);
682 case C_HUB_LOCAL_POWER:
683 break;
684 default:
685 goto error;
686 }
687 break;
688 case ClearPortFeature:
689 if (!wIndex || wIndex > ports)
690 goto error;
691 wIndex--;
692
693 switch (wValue) {
694 case USB_PORT_FEAT_ENABLE:
695 temp = RH_PS_CCS;
696 break;
697 case USB_PORT_FEAT_C_ENABLE:
698 temp = RH_PS_PESC;
699 break;
700 case USB_PORT_FEAT_SUSPEND:
701 temp = RH_PS_POCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 break;
703 case USB_PORT_FEAT_C_SUSPEND:
704 temp = RH_PS_PSSC;
705 break;
706 case USB_PORT_FEAT_POWER:
707 temp = RH_PS_LSDA;
708 break;
709 case USB_PORT_FEAT_C_CONNECTION:
710 temp = RH_PS_CSC;
711 break;
712 case USB_PORT_FEAT_C_OVER_CURRENT:
713 temp = RH_PS_OCIC;
714 break;
715 case USB_PORT_FEAT_C_RESET:
716 temp = RH_PS_PRSC;
717 break;
718 default:
719 goto error;
720 }
721 ohci_writel (ohci, temp,
722 &ohci->regs->roothub.portstatus [wIndex]);
723 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
724 break;
725 case GetHubDescriptor:
726 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
727 break;
728 case GetHubStatus:
729 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700730 put_unaligned_le32(temp, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 break;
732 case GetPortStatus:
733 if (!wIndex || wIndex > ports)
734 goto error;
735 wIndex--;
736 temp = roothub_portstatus (ohci, wIndex);
Harvey Harrisona5abdea2008-04-29 01:03:40 -0700737 put_unaligned_le32(temp, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739#ifndef OHCI_VERBOSE_DEBUG
740 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
741#endif
742 dbg_port (ohci, "GetStatus", wIndex, temp);
743 break;
744 case SetHubFeature:
745 switch (wValue) {
746 case C_HUB_OVER_CURRENT:
747 // FIXME: this can be cleared, yes?
748 case C_HUB_LOCAL_POWER:
749 break;
750 default:
751 goto error;
752 }
753 break;
754 case SetPortFeature:
755 if (!wIndex || wIndex > ports)
756 goto error;
757 wIndex--;
758 switch (wValue) {
759 case USB_PORT_FEAT_SUSPEND:
760#ifdef CONFIG_USB_OTG
761 if (hcd->self.otg_port == (wIndex + 1)
762 && hcd->self.b_hnp_enable)
763 start_hnp(ohci);
764 else
765#endif
766 ohci_writel (ohci, RH_PS_PSS,
767 &ohci->regs->roothub.portstatus [wIndex]);
768 break;
769 case USB_PORT_FEAT_POWER:
770 ohci_writel (ohci, RH_PS_PPS,
771 &ohci->regs->roothub.portstatus [wIndex]);
772 break;
773 case USB_PORT_FEAT_RESET:
Takamasa Ohtake23d10a92006-12-06 17:04:15 -0800774 retval = root_port_reset (ohci, wIndex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
776 default:
777 goto error;
778 }
779 break;
780
781 default:
782error:
783 /* "protocol stall" on error */
784 retval = -EPIPE;
785 }
786 return retval;
787}
788