blob: f1b1ed086bde25d1859d3cb79e3ed7ff905d7a92 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
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
44 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
45}
46
David Brownell8ad7fe12005-09-13 19:59:11 -070047#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define OHCI_SCHED_ENABLES \
50 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
51
52static void dl_done_list (struct ohci_hcd *, struct pt_regs *);
53static void finish_unlinks (struct ohci_hcd *, u16 , struct pt_regs *);
54static int ohci_restart (struct ohci_hcd *ohci);
55
Alan Stern0c0382e2005-10-13 17:08:02 -040056static int ohci_bus_suspend (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
59 int status = 0;
60 unsigned long flags;
61
62 spin_lock_irqsave (&ohci->lock, flags);
63
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +110064 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
65 spin_unlock_irqrestore (&ohci->lock, flags);
66 return -ESHUTDOWN;
67 }
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
70 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
71 case OHCI_USB_RESUME:
72 ohci_dbg (ohci, "resume/suspend?\n");
73 ohci->hc_control &= ~OHCI_CTRL_HCFS;
74 ohci->hc_control |= OHCI_USB_RESET;
75 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
76 (void) ohci_readl (ohci, &ohci->regs->control);
77 /* FALL THROUGH */
78 case OHCI_USB_RESET:
79 status = -EBUSY;
80 ohci_dbg (ohci, "needs reinit!\n");
81 goto done;
82 case OHCI_USB_SUSPEND:
83 ohci_dbg (ohci, "already suspended\n");
84 goto done;
85 }
86 ohci_dbg (ohci, "suspend root hub\n");
87
88 /* First stop any processing */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (ohci->hc_control & OHCI_SCHED_ENABLES) {
90 int limit;
91
92 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
93 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
94 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
95 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
96
97 /* sched disables take effect on the next frame,
98 * then the last WDH could take 6+ msec
99 */
100 ohci_dbg (ohci, "stopping schedules ...\n");
101 limit = 2000;
102 while (limit > 0) {
103 udelay (250);
104 limit =- 250;
105 if (ohci_readl (ohci, &ohci->regs->intrstatus)
106 & OHCI_INTR_SF)
107 break;
108 }
109 dl_done_list (ohci, NULL);
110 mdelay (7);
111 }
112 dl_done_list (ohci, NULL);
113 finish_unlinks (ohci, ohci_frame_no(ohci), NULL);
114 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
115 &ohci->regs->intrstatus);
116
117 /* maybe resume can wake root hub */
David Brownell6a9062f2006-01-23 15:28:07 -0800118 if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 ohci->hc_control |= OHCI_CTRL_RWE;
120 else
121 ohci->hc_control &= ~OHCI_CTRL_RWE;
122
David Brownellf197b2c2005-09-22 22:42:53 -0700123 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
124 * which doesn't imply ports will first be individually suspended.
125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 ohci->hc_control &= ~OHCI_CTRL_HCFS;
127 ohci->hc_control |= OHCI_USB_SUSPEND;
128 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
129 (void) ohci_readl (ohci, &ohci->regs->control);
130
131 /* no resumes until devices finish suspending */
132 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
133
David Brownelld4139842006-08-04 11:31:55 -0700134 /* no timer polling */
135 hcd->poll_rh = 0;
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137done:
David Brownellf197b2c2005-09-22 22:42:53 -0700138 /* external suspend vs self autosuspend ... same effect */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (status == 0)
David Brownellf197b2c2005-09-22 22:42:53 -0700140 usb_hcd_suspend_root_hub(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 spin_unlock_irqrestore (&ohci->lock, flags);
142 return status;
143}
144
145static inline struct ed *find_head (struct ed *ed)
146{
147 /* for bulk and control lists */
148 while (ed->ed_prev)
149 ed = ed->ed_prev;
150 return ed;
151}
152
153/* caller has locked the root hub */
Alan Stern0c0382e2005-10-13 17:08:02 -0400154static int ohci_bus_resume (struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
157 u32 temp, enables;
158 int status = -EINPROGRESS;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100159 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (time_before (jiffies, ohci->next_statechange))
162 msleep(5);
163
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100164 spin_lock_irqsave (&ohci->lock, flags);
165
166 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
167 spin_unlock_irqrestore (&ohci->lock, flags);
168 return -ESHUTDOWN;
169 }
170
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
173
174 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
David Brownellf197b2c2005-09-22 22:42:53 -0700175 /* this can happen after resuming a swsusp snapshot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (hcd->state == HC_STATE_RESUMING) {
177 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
178 ohci->hc_control);
179 status = -EBUSY;
180 /* this happens when pmcore resumes HC then root */
181 } else {
182 ohci_dbg (ohci, "duplicate resume\n");
183 status = 0;
184 }
185 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
186 case OHCI_USB_SUSPEND:
187 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
188 ohci->hc_control |= OHCI_USB_RESUME;
189 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
190 (void) ohci_readl (ohci, &ohci->regs->control);
191 ohci_dbg (ohci, "resume root hub\n");
192 break;
193 case OHCI_USB_RESUME:
194 /* HCFS changes sometime after INTR_RD */
195 ohci_info (ohci, "wakeup\n");
196 break;
197 case OHCI_USB_OPER:
David Brownellf197b2c2005-09-22 22:42:53 -0700198 /* this can happen after resuming a swsusp snapshot */
199 ohci_dbg (ohci, "snapshot resume? reinit\n");
200 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 break;
202 default: /* RESET, we lost power */
David Brownellf197b2c2005-09-22 22:42:53 -0700203 ohci_dbg (ohci, "lost power\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 status = -EBUSY;
205 }
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100206 spin_unlock_irqrestore (&ohci->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (status == -EBUSY) {
208 (void) ohci_init (ohci);
209 return ohci_restart (ohci);
210 }
211 if (status != -EINPROGRESS)
212 return status;
213
David Brownellfdd13b32005-08-31 11:52:57 -0700214 temp = ohci->num_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 enables = 0;
216 while (temp--) {
217 u32 stat = ohci_readl (ohci,
218 &ohci->regs->roothub.portstatus [temp]);
219
220 /* force global, not selective, resume */
221 if (!(stat & RH_PS_PSS))
222 continue;
223 ohci_writel (ohci, RH_PS_POCI,
224 &ohci->regs->roothub.portstatus [temp]);
225 }
226
227 /* Some controllers (lucent erratum) need extra-long delays */
David Brownellf197b2c2005-09-22 22:42:53 -0700228 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 temp = ohci_readl (ohci, &ohci->regs->control);
231 temp &= OHCI_CTRL_HCFS;
232 if (temp != OHCI_USB_RESUME) {
233 ohci_err (ohci, "controller won't resume\n");
234 return -EBUSY;
235 }
236
237 /* disable old schedule state, reinit from scratch */
238 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
239 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
240 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
241 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
242 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
243 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
244
245 /* Sometimes PCI D3 suspend trashes frame timings ... */
246 periodic_reinit (ohci);
247
248 /* interrupts might have been disabled */
249 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
250 if (ohci->ed_rm_list)
251 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
252 ohci_writel (ohci, ohci_readl (ohci, &ohci->regs->intrstatus),
253 &ohci->regs->intrstatus);
254
255 /* Then re-enable operations */
256 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
257 (void) ohci_readl (ohci, &ohci->regs->control);
258 msleep (3);
259
David Brownell6a9062f2006-01-23 15:28:07 -0800260 temp = ohci->hc_control;
261 temp &= OHCI_CTRL_RWC;
262 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ohci->hc_control = temp;
264 ohci_writel (ohci, temp, &ohci->regs->control);
265 (void) ohci_readl (ohci, &ohci->regs->control);
266
267 /* TRSMRCY */
268 msleep (10);
269
David Brownelld4139842006-08-04 11:31:55 -0700270 /* keep it alive for more than ~5x suspend + resume costs */
271 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /* maybe turn schedules back on */
274 enables = 0;
275 temp = 0;
276 if (!ohci->ed_rm_list) {
277 if (ohci->ed_controltail) {
278 ohci_writel (ohci,
279 find_head (ohci->ed_controltail)->dma,
280 &ohci->regs->ed_controlhead);
281 enables |= OHCI_CTRL_CLE;
282 temp |= OHCI_CLF;
283 }
284 if (ohci->ed_bulktail) {
285 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
286 &ohci->regs->ed_bulkhead);
287 enables |= OHCI_CTRL_BLE;
288 temp |= OHCI_BLF;
289 }
290 }
291 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
292 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
293 if (enables) {
294 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
295 ohci->hc_control |= enables;
296 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
297 if (temp)
298 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
299 (void) ohci_readl (ohci, &ohci->regs->control);
300 }
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return 0;
303}
304
David Brownell8ad7fe12005-09-13 19:59:11 -0700305#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307/*-------------------------------------------------------------------------*/
308
309/* build "status change" packet (one or two bytes) from HC registers */
310
311static int
312ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
313{
314 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
David Brownellfdd13b32005-08-31 11:52:57 -0700315 int i, changed = 0, length = 1;
David Brownelld4139842006-08-04 11:31:55 -0700316 int can_suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned long flags;
318
David Brownelld4139842006-08-04 11:31:55 -0700319 can_suspend = device_may_wakeup(&hcd->self.root_hub->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 spin_lock_irqsave (&ohci->lock, flags);
321
322 /* handle autosuspended root: finish resuming before
323 * letting khubd or root hub timer see state changes.
324 */
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100325 if (unlikely((ohci->hc_control & OHCI_CTRL_HCFS) != OHCI_USB_OPER
326 || !HC_IS_RUNNING(hcd->state))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 can_suspend = 0;
328 goto done;
329 }
330
David Brownellfdd13b32005-08-31 11:52:57 -0700331 /* undocumented erratum seen on at least rev D */
332 if ((ohci->flags & OHCI_QUIRK_AMD756)
333 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
334 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
336 /* retry later; "should not happen" */
337 goto done;
338 }
339
340 /* init status */
341 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
342 buf [0] = changed = 1;
343 else
344 buf [0] = 0;
David Brownellfdd13b32005-08-31 11:52:57 -0700345 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 buf [1] = 0;
347 length++;
348 }
349
350 /* look at each port */
David Brownellfdd13b32005-08-31 11:52:57 -0700351 for (i = 0; i < ohci->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 u32 status = roothub_portstatus (ohci, i);
353
David Brownelld4139842006-08-04 11:31:55 -0700354 /* can't autosuspend with active ports */
355 if ((status & RH_PS_PES) && !(status & RH_PS_PSS))
356 can_suspend = 0;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
359 | RH_PS_OCIC | RH_PS_PRSC)) {
360 changed = 1;
361 if (i < 7)
362 buf [0] |= 1 << (i + 1);
363 else
364 buf [1] |= 1 << (i - 7);
365 continue;
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
David Brownelld4139842006-08-04 11:31:55 -0700369 /* after root hub changes, stop polling after debouncing
370 * for a while and maybe kicking in autosuspend
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
David Brownelld4139842006-08-04 11:31:55 -0700372 if (changed) {
373 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
374 can_suspend = 0;
375 } else if (time_before (jiffies, ohci->next_statechange)) {
376 can_suspend = 0;
377 } else {
378#ifdef CONFIG_PM
379 can_suspend = can_suspend
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 && !ohci->ed_rm_list
381 && ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES)
382 & ohci->hc_control)
David Brownelld4139842006-08-04 11:31:55 -0700383 == OHCI_USB_OPER;
384#endif
385 if (hcd->uses_new_polling) {
386 hcd->poll_rh = 0;
387 /* use INTR_RHSC iff INTR_RD won't apply */
388 if (!can_suspend)
389 ohci_writel (ohci, OHCI_INTR_RHSC,
390 &ohci->regs->intrenable);
391 }
392 }
393
394done:
395 spin_unlock_irqrestore (&ohci->lock, flags);
396
397#ifdef CONFIG_PM
398 /* save power by autosuspending idle root hubs;
399 * INTR_RD wakes us when there's work
400 */
401 if (can_suspend && usb_trylock_device (hcd->self.root_hub) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 ohci_vdbg (ohci, "autosuspend\n");
Alan Stern0c0382e2005-10-13 17:08:02 -0400403 (void) ohci_bus_suspend (hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 usb_unlock_device (hcd->self.root_hub);
405 }
406#endif
407
408 return changed ? length : 0;
409}
410
411/*-------------------------------------------------------------------------*/
412
413static void
414ohci_hub_descriptor (
415 struct ohci_hcd *ohci,
416 struct usb_hub_descriptor *desc
417) {
418 u32 rh = roothub_a (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 u16 temp;
420
421 desc->bDescriptorType = 0x29;
422 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
423 desc->bHubContrCurrent = 0;
424
David Brownellfdd13b32005-08-31 11:52:57 -0700425 desc->bNbrPorts = ohci->num_ports;
426 temp = 1 + (ohci->num_ports / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 desc->bDescLength = 7 + 2 * temp;
428
429 temp = 0;
430 if (rh & RH_A_NPS) /* no power switching? */
431 temp |= 0x0002;
432 if (rh & RH_A_PSM) /* per-port power switching? */
433 temp |= 0x0001;
434 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
435 temp |= 0x0010;
436 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
437 temp |= 0x0008;
438 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
439
440 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
441 rh = roothub_b (ohci);
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700442 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 desc->bitmap [0] = rh & RH_B_DR;
David Brownellfdd13b32005-08-31 11:52:57 -0700444 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700446 desc->bitmap [2] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 } else
448 desc->bitmap [1] = 0xff;
449}
450
451/*-------------------------------------------------------------------------*/
452
453#ifdef CONFIG_USB_OTG
454
455static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
456{
457 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
458 u32 status;
459
460 if (!port)
461 return -EINVAL;
462 port--;
463
464 /* start port reset before HNP protocol times out */
465 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
466 if (!(status & RH_PS_CCS))
467 return -ENODEV;
468
469 /* khubd will finish the reset later */
470 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
471 return 0;
472}
473
474static void start_hnp(struct ohci_hcd *ohci);
475
476#else
477
478#define ohci_start_port_reset NULL
479
480#endif
481
482/*-------------------------------------------------------------------------*/
483
484
485/* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
486 * not necessarily continuous ... to guard against resume signaling.
487 * The short timeout is safe for non-root hubs, and is backward-compatible
488 * with earlier Linux hosts.
489 */
490#ifdef CONFIG_USB_SUSPEND
491#define PORT_RESET_MSEC 50
492#else
493#define PORT_RESET_MSEC 10
494#endif
495
496/* this timer value might be vendor-specific ... */
497#define PORT_RESET_HW_MSEC 10
498
499/* wrap-aware logic morphed from <linux/jiffies.h> */
500#define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
501
502/* called from some task, normally khubd */
503static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
504{
505 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
506 u32 temp;
507 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
508 u16 reset_done = now + PORT_RESET_MSEC;
509
510 /* build a "continuous enough" reset signal, with up to
511 * 3msec gap between pulses. scheduler HZ==100 must work;
512 * this might need to be deadline-scheduled.
513 */
514 do {
515 /* spin until any current reset finishes */
516 for (;;) {
517 temp = ohci_readl (ohci, portstat);
518 if (!(temp & RH_PS_PRS))
519 break;
520 udelay (500);
521 }
522
523 if (!(temp & RH_PS_CCS))
524 break;
525 if (temp & RH_PS_PRSC)
526 ohci_writel (ohci, RH_PS_PRSC, portstat);
527
528 /* start the next reset, sleep till it's probably done */
529 ohci_writel (ohci, RH_PS_PRS, portstat);
530 msleep(PORT_RESET_HW_MSEC);
531 now = ohci_readl(ohci, &ohci->regs->fmnumber);
532 } while (tick_before(now, reset_done));
533 /* caller synchronizes using PRSC */
534}
535
536static int ohci_hub_control (
537 struct usb_hcd *hcd,
538 u16 typeReq,
539 u16 wValue,
540 u16 wIndex,
541 char *buf,
542 u16 wLength
543) {
544 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
545 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
546 u32 temp;
547 int retval = 0;
548
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100549 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
550 return -ESHUTDOWN;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 switch (typeReq) {
553 case ClearHubFeature:
554 switch (wValue) {
555 case C_HUB_OVER_CURRENT:
556 ohci_writel (ohci, RH_HS_OCIC,
557 &ohci->regs->roothub.status);
558 case C_HUB_LOCAL_POWER:
559 break;
560 default:
561 goto error;
562 }
563 break;
564 case ClearPortFeature:
565 if (!wIndex || wIndex > ports)
566 goto error;
567 wIndex--;
568
569 switch (wValue) {
570 case USB_PORT_FEAT_ENABLE:
571 temp = RH_PS_CCS;
572 break;
573 case USB_PORT_FEAT_C_ENABLE:
574 temp = RH_PS_PESC;
575 break;
576 case USB_PORT_FEAT_SUSPEND:
577 temp = RH_PS_POCI;
578 if ((ohci->hc_control & OHCI_CTRL_HCFS)
579 != OHCI_USB_OPER)
David Brownellf197b2c2005-09-22 22:42:53 -0700580 usb_hcd_resume_root_hub(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 break;
582 case USB_PORT_FEAT_C_SUSPEND:
583 temp = RH_PS_PSSC;
584 break;
585 case USB_PORT_FEAT_POWER:
586 temp = RH_PS_LSDA;
587 break;
588 case USB_PORT_FEAT_C_CONNECTION:
589 temp = RH_PS_CSC;
590 break;
591 case USB_PORT_FEAT_C_OVER_CURRENT:
592 temp = RH_PS_OCIC;
593 break;
594 case USB_PORT_FEAT_C_RESET:
595 temp = RH_PS_PRSC;
596 break;
597 default:
598 goto error;
599 }
600 ohci_writel (ohci, temp,
601 &ohci->regs->roothub.portstatus [wIndex]);
602 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
603 break;
604 case GetHubDescriptor:
605 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
606 break;
607 case GetHubStatus:
608 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
David Miller92164c5d2006-06-21 22:26:09 -0700609 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
611 case GetPortStatus:
612 if (!wIndex || wIndex > ports)
613 goto error;
614 wIndex--;
615 temp = roothub_portstatus (ohci, wIndex);
David Miller92164c5d2006-06-21 22:26:09 -0700616 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618#ifndef OHCI_VERBOSE_DEBUG
619 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
620#endif
621 dbg_port (ohci, "GetStatus", wIndex, temp);
622 break;
623 case SetHubFeature:
624 switch (wValue) {
625 case C_HUB_OVER_CURRENT:
626 // FIXME: this can be cleared, yes?
627 case C_HUB_LOCAL_POWER:
628 break;
629 default:
630 goto error;
631 }
632 break;
633 case SetPortFeature:
634 if (!wIndex || wIndex > ports)
635 goto error;
636 wIndex--;
637 switch (wValue) {
638 case USB_PORT_FEAT_SUSPEND:
639#ifdef CONFIG_USB_OTG
640 if (hcd->self.otg_port == (wIndex + 1)
641 && hcd->self.b_hnp_enable)
642 start_hnp(ohci);
643 else
644#endif
645 ohci_writel (ohci, RH_PS_PSS,
646 &ohci->regs->roothub.portstatus [wIndex]);
647 break;
648 case USB_PORT_FEAT_POWER:
649 ohci_writel (ohci, RH_PS_PPS,
650 &ohci->regs->roothub.portstatus [wIndex]);
651 break;
652 case USB_PORT_FEAT_RESET:
653 root_port_reset (ohci, wIndex);
654 break;
655 default:
656 goto error;
657 }
658 break;
659
660 default:
661error:
662 /* "protocol stall" on error */
663 retval = -EPIPE;
664 }
665 return retval;
666}
667