blob: 99357b99d133bbe8c83581ad1df9b81bf66ef528 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define OHCI_SCHED_ENABLES \
48 (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
49
David Howells7d12e782006-10-05 14:55:46 +010050static void dl_done_list (struct ohci_hcd *);
51static void finish_unlinks (struct ohci_hcd *, u16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Alan Sternb1878442006-10-24 12:02:31 -040053#ifdef CONFIG_PM
54static int ohci_restart(struct ohci_hcd *ohci);
55#endif
56
Alan Stern8d1a2432006-09-26 14:46:16 -040057static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
58__releases(ohci->lock)
59__acquires(ohci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int status = 0;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +110062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
64 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
65 case OHCI_USB_RESUME:
66 ohci_dbg (ohci, "resume/suspend?\n");
67 ohci->hc_control &= ~OHCI_CTRL_HCFS;
68 ohci->hc_control |= OHCI_USB_RESET;
69 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
70 (void) ohci_readl (ohci, &ohci->regs->control);
71 /* FALL THROUGH */
72 case OHCI_USB_RESET:
73 status = -EBUSY;
74 ohci_dbg (ohci, "needs reinit!\n");
75 goto done;
76 case OHCI_USB_SUSPEND:
Alan Stern8d1a2432006-09-26 14:46:16 -040077 if (!ohci->autostop) {
78 ohci_dbg (ohci, "already suspended\n");
79 goto done;
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
Alan Stern8d1a2432006-09-26 14:46:16 -040082 ohci_dbg (ohci, "%s root hub\n",
83 autostop ? "auto-stop" : "suspend");
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /* First stop any processing */
Alan Stern8d1a2432006-09-26 14:46:16 -040086 if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
88 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
89 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
90 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
91
92 /* sched disables take effect on the next frame,
93 * then the last WDH could take 6+ msec
94 */
95 ohci_dbg (ohci, "stopping schedules ...\n");
Alan Stern8d1a2432006-09-26 14:46:16 -040096 ohci->autostop = 0;
97 spin_unlock_irq (&ohci->lock);
98 msleep (8);
99 spin_lock_irq (&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
David Howells7d12e782006-10-05 14:55:46 +0100101 dl_done_list (ohci);
102 finish_unlinks (ohci, ohci_frame_no(ohci));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /* maybe resume can wake root hub */
Alan Stern8d1a2432006-09-26 14:46:16 -0400105 if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev) ||
106 autostop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 ohci->hc_control |= OHCI_CTRL_RWE;
Alan Stern1f7e1a32006-09-25 15:41:21 -0400108 else {
109 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 Stern583cead2006-10-24 12:04:22 -0400174 ohci_info(ohci, "%swakeup\n",
175 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 }
Alan Stern8d1a2432006-09-26 14:46:16 -0400186#ifdef CONFIG_PM
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);
192 spin_lock_irq (&ohci->lock);
193 }
194 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Alan Stern8d1a2432006-09-26 14:46:16 -0400196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (status != -EINPROGRESS)
198 return status;
Alan Stern8d1a2432006-09-26 14:46:16 -0400199 if (autostopped)
200 goto skip_resume;
201 spin_unlock_irq (&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
David Brownellfdd13b32005-08-31 11:52:57 -0700203 temp = ohci->num_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 while (temp--) {
205 u32 stat = ohci_readl (ohci,
206 &ohci->regs->roothub.portstatus [temp]);
207
208 /* force global, not selective, resume */
209 if (!(stat & RH_PS_PSS))
210 continue;
211 ohci_writel (ohci, RH_PS_POCI,
212 &ohci->regs->roothub.portstatus [temp]);
213 }
214
215 /* Some controllers (lucent erratum) need extra-long delays */
David Brownellf197b2c2005-09-22 22:42:53 -0700216 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 temp = ohci_readl (ohci, &ohci->regs->control);
219 temp &= OHCI_CTRL_HCFS;
220 if (temp != OHCI_USB_RESUME) {
221 ohci_err (ohci, "controller won't resume\n");
222 return -EBUSY;
223 }
224
225 /* disable old schedule state, reinit from scratch */
226 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
227 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
228 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
229 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
230 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
231 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
232
233 /* Sometimes PCI D3 suspend trashes frame timings ... */
234 periodic_reinit (ohci);
235
Alan Stern8d1a2432006-09-26 14:46:16 -0400236 /* the following code is executed with ohci->lock held and
237 * irqs disabled if and only if autostopped is true
238 */
239
240skip_resume:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /* interrupts might have been disabled */
242 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
243 if (ohci->ed_rm_list)
244 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* Then re-enable operations */
247 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
248 (void) ohci_readl (ohci, &ohci->regs->control);
Alan Stern8d1a2432006-09-26 14:46:16 -0400249 if (!autostopped)
250 msleep (3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David Brownell6a9062f2006-01-23 15:28:07 -0800252 temp = ohci->hc_control;
253 temp &= OHCI_CTRL_RWC;
254 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 ohci->hc_control = temp;
256 ohci_writel (ohci, temp, &ohci->regs->control);
257 (void) ohci_readl (ohci, &ohci->regs->control);
258
259 /* TRSMRCY */
Alan Stern8d1a2432006-09-26 14:46:16 -0400260 if (!autostopped) {
261 msleep (10);
262 spin_lock_irq (&ohci->lock);
263 }
264 /* now ohci->lock is always held and irqs are always disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
David Brownelld4139842006-08-04 11:31:55 -0700266 /* keep it alive for more than ~5x suspend + resume costs */
267 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* maybe turn schedules back on */
270 enables = 0;
271 temp = 0;
272 if (!ohci->ed_rm_list) {
273 if (ohci->ed_controltail) {
274 ohci_writel (ohci,
275 find_head (ohci->ed_controltail)->dma,
276 &ohci->regs->ed_controlhead);
277 enables |= OHCI_CTRL_CLE;
278 temp |= OHCI_CLF;
279 }
280 if (ohci->ed_bulktail) {
281 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
282 &ohci->regs->ed_bulkhead);
283 enables |= OHCI_CTRL_BLE;
284 temp |= OHCI_BLF;
285 }
286 }
287 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
288 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
289 if (enables) {
290 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
291 ohci->hc_control |= enables;
292 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
293 if (temp)
294 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
295 (void) ohci_readl (ohci, &ohci->regs->control);
296 }
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return 0;
299}
300
Alan Stern8d1a2432006-09-26 14:46:16 -0400301#ifdef CONFIG_PM
302
303static int ohci_bus_suspend (struct usb_hcd *hcd)
304{
305 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
306 int rc;
307
308 spin_lock_irq (&ohci->lock);
309
310 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
311 rc = -ESHUTDOWN;
312 else
313 rc = ohci_rh_suspend (ohci, 0);
314 spin_unlock_irq (&ohci->lock);
315 return rc;
316}
317
318static int ohci_bus_resume (struct usb_hcd *hcd)
319{
320 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
321 int rc;
322
323 if (time_before (jiffies, ohci->next_statechange))
324 msleep(5);
325
326 spin_lock_irq (&ohci->lock);
327
328 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
329 rc = -ESHUTDOWN;
330 else
331 rc = ohci_rh_resume (ohci);
332 spin_unlock_irq (&ohci->lock);
333
334 /* poll until we know a device is connected or we autostop */
335 if (rc == 0)
336 usb_hcd_poll_rh_status(hcd);
337 return rc;
338}
339
David Brownell8ad7fe12005-09-13 19:59:11 -0700340#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342/*-------------------------------------------------------------------------*/
343
344/* build "status change" packet (one or two bytes) from HC registers */
345
346static int
347ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
348{
349 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
David Brownellfdd13b32005-08-31 11:52:57 -0700350 int i, changed = 0, length = 1;
Alan Stern8d1a2432006-09-26 14:46:16 -0400351 int any_connected = 0, rhsc_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 unsigned long flags;
353
354 spin_lock_irqsave (&ohci->lock, flags);
355
David Brownellfdd13b32005-08-31 11:52:57 -0700356 /* undocumented erratum seen on at least rev D */
357 if ((ohci->flags & OHCI_QUIRK_AMD756)
358 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
359 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
361 /* retry later; "should not happen" */
362 goto done;
363 }
364
365 /* init status */
366 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
367 buf [0] = changed = 1;
368 else
369 buf [0] = 0;
David Brownellfdd13b32005-08-31 11:52:57 -0700370 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 buf [1] = 0;
372 length++;
373 }
374
375 /* look at each port */
David Brownellfdd13b32005-08-31 11:52:57 -0700376 for (i = 0; i < ohci->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 u32 status = roothub_portstatus (ohci, i);
378
Alan Stern8d1a2432006-09-26 14:46:16 -0400379 /* can't autostop if ports are connected */
380 any_connected |= (status & RH_PS_CCS);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
383 | RH_PS_OCIC | RH_PS_PRSC)) {
384 changed = 1;
385 if (i < 7)
386 buf [0] |= 1 << (i + 1);
387 else
388 buf [1] |= 1 << (i - 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Alan Stern8d1a2432006-09-26 14:46:16 -0400392 /* NOTE: vendors didn't always make the same implementation
393 * choices for RHSC. Sometimes it triggers on an edge (like
394 * setting and maybe clearing a port status change bit); and
395 * it's level-triggered on other silicon, active until khubd
396 * clears all active port status change bits. If it's still
397 * set (level-triggered) we must disable it and rely on
398 * polling until khubd re-enables it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Alan Stern8d1a2432006-09-26 14:46:16 -0400400 if (ohci_readl (ohci, &ohci->regs->intrstatus) & OHCI_INTR_RHSC) {
401 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
402 (void) ohci_readl (ohci, &ohci->regs->intrdisable);
403 rhsc_enabled = 0;
404 }
405 hcd->poll_rh = 1;
406
407 /* carry out appropriate state changes */
408 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
409
410 case OHCI_USB_OPER:
411 /* keep on polling until we know a device is connected
412 * and RHSC is enabled */
413 if (!ohci->autostop) {
414 if (any_connected) {
415 if (rhsc_enabled)
416 hcd->poll_rh = 0;
417 } else {
418 ohci->autostop = 1;
419 ohci->next_statechange = jiffies + HZ;
420 }
421
422 /* if no devices have been attached for one second, autostop */
423 } else {
424 if (changed || any_connected) {
425 ohci->autostop = 0;
426 ohci->next_statechange = jiffies +
427 STATECHANGE_DELAY;
Alan Stern3da24952006-11-14 16:28:01 -0500428 } else if (device_may_wakeup(&hcd->self.root_hub->dev)
429 && time_after_eq(jiffies,
Alan Stern8d1a2432006-09-26 14:46:16 -0400430 ohci->next_statechange)
431 && !ohci->ed_rm_list
432 && !(ohci->hc_control &
433 OHCI_SCHED_ENABLES)) {
434 ohci_rh_suspend (ohci, 1);
435 }
436 }
437 break;
438
439 /* if there is a port change, autostart or ask to be resumed */
440 case OHCI_USB_SUSPEND:
441 case OHCI_USB_RESUME:
442 if (changed) {
443 if (ohci->autostop)
444 ohci_rh_resume (ohci);
445 else
446 usb_hcd_resume_root_hub (hcd);
447 } else {
448 /* everything is idle, no need for polling */
449 hcd->poll_rh = 0;
450 }
451 break;
452 }
David Brownelld4139842006-08-04 11:31:55 -0700453
454done:
455 spin_unlock_irqrestore (&ohci->lock, flags);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return changed ? length : 0;
458}
459
460/*-------------------------------------------------------------------------*/
461
462static void
463ohci_hub_descriptor (
464 struct ohci_hcd *ohci,
465 struct usb_hub_descriptor *desc
466) {
467 u32 rh = roothub_a (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 u16 temp;
469
470 desc->bDescriptorType = 0x29;
471 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
472 desc->bHubContrCurrent = 0;
473
David Brownellfdd13b32005-08-31 11:52:57 -0700474 desc->bNbrPorts = ohci->num_ports;
475 temp = 1 + (ohci->num_ports / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 desc->bDescLength = 7 + 2 * temp;
477
478 temp = 0;
479 if (rh & RH_A_NPS) /* no power switching? */
480 temp |= 0x0002;
481 if (rh & RH_A_PSM) /* per-port power switching? */
482 temp |= 0x0001;
483 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
484 temp |= 0x0010;
485 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
486 temp |= 0x0008;
487 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
488
489 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
490 rh = roothub_b (ohci);
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700491 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 desc->bitmap [0] = rh & RH_B_DR;
David Brownellfdd13b32005-08-31 11:52:57 -0700493 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700495 desc->bitmap [2] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 } else
497 desc->bitmap [1] = 0xff;
498}
499
500/*-------------------------------------------------------------------------*/
501
502#ifdef CONFIG_USB_OTG
503
504static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
505{
506 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
507 u32 status;
508
509 if (!port)
510 return -EINVAL;
511 port--;
512
513 /* start port reset before HNP protocol times out */
514 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
515 if (!(status & RH_PS_CCS))
516 return -ENODEV;
517
518 /* khubd will finish the reset later */
519 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
520 return 0;
521}
522
523static void start_hnp(struct ohci_hcd *ohci);
524
525#else
526
527#define ohci_start_port_reset NULL
528
529#endif
530
531/*-------------------------------------------------------------------------*/
532
533
534/* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
535 * not necessarily continuous ... to guard against resume signaling.
536 * The short timeout is safe for non-root hubs, and is backward-compatible
537 * with earlier Linux hosts.
538 */
539#ifdef CONFIG_USB_SUSPEND
540#define PORT_RESET_MSEC 50
541#else
542#define PORT_RESET_MSEC 10
543#endif
544
545/* this timer value might be vendor-specific ... */
546#define PORT_RESET_HW_MSEC 10
547
548/* wrap-aware logic morphed from <linux/jiffies.h> */
549#define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
550
551/* called from some task, normally khubd */
552static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
553{
554 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
555 u32 temp;
556 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
557 u16 reset_done = now + PORT_RESET_MSEC;
558
559 /* build a "continuous enough" reset signal, with up to
560 * 3msec gap between pulses. scheduler HZ==100 must work;
561 * this might need to be deadline-scheduled.
562 */
563 do {
564 /* spin until any current reset finishes */
565 for (;;) {
566 temp = ohci_readl (ohci, portstat);
567 if (!(temp & RH_PS_PRS))
568 break;
569 udelay (500);
570 }
571
572 if (!(temp & RH_PS_CCS))
573 break;
574 if (temp & RH_PS_PRSC)
575 ohci_writel (ohci, RH_PS_PRSC, portstat);
576
577 /* start the next reset, sleep till it's probably done */
578 ohci_writel (ohci, RH_PS_PRS, portstat);
579 msleep(PORT_RESET_HW_MSEC);
580 now = ohci_readl(ohci, &ohci->regs->fmnumber);
581 } while (tick_before(now, reset_done));
582 /* caller synchronizes using PRSC */
583}
584
585static int ohci_hub_control (
586 struct usb_hcd *hcd,
587 u16 typeReq,
588 u16 wValue,
589 u16 wIndex,
590 char *buf,
591 u16 wLength
592) {
593 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
594 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
595 u32 temp;
596 int retval = 0;
597
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100598 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
599 return -ESHUTDOWN;
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 switch (typeReq) {
602 case ClearHubFeature:
603 switch (wValue) {
604 case C_HUB_OVER_CURRENT:
605 ohci_writel (ohci, RH_HS_OCIC,
606 &ohci->regs->roothub.status);
607 case C_HUB_LOCAL_POWER:
608 break;
609 default:
610 goto error;
611 }
612 break;
613 case ClearPortFeature:
614 if (!wIndex || wIndex > ports)
615 goto error;
616 wIndex--;
617
618 switch (wValue) {
619 case USB_PORT_FEAT_ENABLE:
620 temp = RH_PS_CCS;
621 break;
622 case USB_PORT_FEAT_C_ENABLE:
623 temp = RH_PS_PESC;
624 break;
625 case USB_PORT_FEAT_SUSPEND:
626 temp = RH_PS_POCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
628 case USB_PORT_FEAT_C_SUSPEND:
629 temp = RH_PS_PSSC;
630 break;
631 case USB_PORT_FEAT_POWER:
632 temp = RH_PS_LSDA;
633 break;
634 case USB_PORT_FEAT_C_CONNECTION:
635 temp = RH_PS_CSC;
636 break;
637 case USB_PORT_FEAT_C_OVER_CURRENT:
638 temp = RH_PS_OCIC;
639 break;
640 case USB_PORT_FEAT_C_RESET:
641 temp = RH_PS_PRSC;
642 break;
643 default:
644 goto error;
645 }
646 ohci_writel (ohci, temp,
647 &ohci->regs->roothub.portstatus [wIndex]);
648 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
649 break;
650 case GetHubDescriptor:
651 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
652 break;
653 case GetHubStatus:
654 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
David Miller92164c5d2006-06-21 22:26:09 -0700655 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 break;
657 case GetPortStatus:
658 if (!wIndex || wIndex > ports)
659 goto error;
660 wIndex--;
661 temp = roothub_portstatus (ohci, wIndex);
David Miller92164c5d2006-06-21 22:26:09 -0700662 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664#ifndef OHCI_VERBOSE_DEBUG
665 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
666#endif
667 dbg_port (ohci, "GetStatus", wIndex, temp);
668 break;
669 case SetHubFeature:
670 switch (wValue) {
671 case C_HUB_OVER_CURRENT:
672 // FIXME: this can be cleared, yes?
673 case C_HUB_LOCAL_POWER:
674 break;
675 default:
676 goto error;
677 }
678 break;
679 case SetPortFeature:
680 if (!wIndex || wIndex > ports)
681 goto error;
682 wIndex--;
683 switch (wValue) {
684 case USB_PORT_FEAT_SUSPEND:
685#ifdef CONFIG_USB_OTG
686 if (hcd->self.otg_port == (wIndex + 1)
687 && hcd->self.b_hnp_enable)
688 start_hnp(ohci);
689 else
690#endif
691 ohci_writel (ohci, RH_PS_PSS,
692 &ohci->regs->roothub.portstatus [wIndex]);
693 break;
694 case USB_PORT_FEAT_POWER:
695 ohci_writel (ohci, RH_PS_PPS,
696 &ohci->regs->roothub.portstatus [wIndex]);
697 break;
698 case USB_PORT_FEAT_RESET:
699 root_port_reset (ohci, wIndex);
700 break;
701 default:
702 goto error;
703 }
704 break;
705
706 default:
707error:
708 /* "protocol stall" on error */
709 retval = -EPIPE;
710 }
711 return retval;
712}
713