blob: da09e7930c1b4baf0db594de23bc84fcdf84bc83 [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 Stern8d1a2432006-09-26 14:46:16 -040053static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
54__releases(ohci->lock)
55__acquires(ohci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int status = 0;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +110058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
60 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
61 case OHCI_USB_RESUME:
62 ohci_dbg (ohci, "resume/suspend?\n");
63 ohci->hc_control &= ~OHCI_CTRL_HCFS;
64 ohci->hc_control |= OHCI_USB_RESET;
65 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
66 (void) ohci_readl (ohci, &ohci->regs->control);
67 /* FALL THROUGH */
68 case OHCI_USB_RESET:
69 status = -EBUSY;
70 ohci_dbg (ohci, "needs reinit!\n");
71 goto done;
72 case OHCI_USB_SUSPEND:
Alan Stern8d1a2432006-09-26 14:46:16 -040073 if (!ohci->autostop) {
74 ohci_dbg (ohci, "already suspended\n");
75 goto done;
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
Alan Stern8d1a2432006-09-26 14:46:16 -040078 ohci_dbg (ohci, "%s root hub\n",
79 autostop ? "auto-stop" : "suspend");
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /* First stop any processing */
Alan Stern8d1a2432006-09-26 14:46:16 -040082 if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
84 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
85 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
86 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
87
88 /* sched disables take effect on the next frame,
89 * then the last WDH could take 6+ msec
90 */
91 ohci_dbg (ohci, "stopping schedules ...\n");
Alan Stern8d1a2432006-09-26 14:46:16 -040092 ohci->autostop = 0;
93 spin_unlock_irq (&ohci->lock);
94 msleep (8);
95 spin_lock_irq (&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
David Howells7d12e782006-10-05 14:55:46 +010097 dl_done_list (ohci);
98 finish_unlinks (ohci, ohci_frame_no(ohci));
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /* maybe resume can wake root hub */
Alan Stern8d1a2432006-09-26 14:46:16 -0400101 if (device_may_wakeup(&ohci_to_hcd(ohci)->self.root_hub->dev) ||
102 autostop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 ohci->hc_control |= OHCI_CTRL_RWE;
Alan Stern1f7e1a32006-09-25 15:41:21 -0400104 else {
105 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 ohci->hc_control &= ~OHCI_CTRL_RWE;
Alan Stern1f7e1a32006-09-25 15:41:21 -0400107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
David Brownellf197b2c2005-09-22 22:42:53 -0700109 /* Suspend hub ... this is the "global (to this bus) suspend" mode,
110 * which doesn't imply ports will first be individually suspended.
111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 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 */
Alan Stern8d1a2432006-09-26 14:46:16 -0400118 if (!autostop) {
119 ohci->next_statechange = jiffies + msecs_to_jiffies (5);
120 ohci->autostop = 0;
121 }
David Brownelld4139842006-08-04 11:31:55 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return status;
125}
126
127static 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
Greg Kroah-Hartman6e3ce3a2006-09-28 17:06:45 -0700135static int ohci_restart (struct ohci_hcd *ohci);
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/* caller has locked the root hub */
Alan Stern8d1a2432006-09-26 14:46:16 -0400138static int ohci_rh_resume (struct ohci_hcd *ohci)
139__releases(ohci->lock)
140__acquires(ohci->lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Alan Stern8d1a2432006-09-26 14:46:16 -0400142 struct usb_hcd *hcd = ohci_to_hcd (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 u32 temp, enables;
144 int status = -EINPROGRESS;
Alan Stern8d1a2432006-09-26 14:46:16 -0400145 int autostopped = ohci->autostop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Alan Stern8d1a2432006-09-26 14:46:16 -0400147 ohci->autostop = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
149
150 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
David Brownellf197b2c2005-09-22 22:42:53 -0700151 /* this can happen after resuming a swsusp snapshot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (hcd->state == HC_STATE_RESUMING) {
153 ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
154 ohci->hc_control);
155 status = -EBUSY;
156 /* this happens when pmcore resumes HC then root */
157 } else {
158 ohci_dbg (ohci, "duplicate resume\n");
159 status = 0;
160 }
161 } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
162 case OHCI_USB_SUSPEND:
163 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
164 ohci->hc_control |= OHCI_USB_RESUME;
165 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
166 (void) ohci_readl (ohci, &ohci->regs->control);
Alan Stern8d1a2432006-09-26 14:46:16 -0400167 ohci_dbg (ohci, "%s root hub\n",
168 autostopped ? "auto-start" : "resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 case OHCI_USB_RESUME:
171 /* HCFS changes sometime after INTR_RD */
172 ohci_info (ohci, "wakeup\n");
173 break;
174 case OHCI_USB_OPER:
David Brownellf197b2c2005-09-22 22:42:53 -0700175 /* this can happen after resuming a swsusp snapshot */
176 ohci_dbg (ohci, "snapshot resume? reinit\n");
177 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
179 default: /* RESET, we lost power */
David Brownellf197b2c2005-09-22 22:42:53 -0700180 ohci_dbg (ohci, "lost power\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 status = -EBUSY;
182 }
Alan Stern8d1a2432006-09-26 14:46:16 -0400183#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (status == -EBUSY) {
Alan Stern8d1a2432006-09-26 14:46:16 -0400185 if (!autostopped) {
Alan Stern8d1a2432006-09-26 14:46:16 -0400186 spin_unlock_irq (&ohci->lock);
187 (void) ohci_init (ohci);
188 status = ohci_restart (ohci);
189 spin_lock_irq (&ohci->lock);
190 }
191 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Alan Stern8d1a2432006-09-26 14:46:16 -0400193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (status != -EINPROGRESS)
195 return status;
Alan Stern8d1a2432006-09-26 14:46:16 -0400196 if (autostopped)
197 goto skip_resume;
198 spin_unlock_irq (&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
David Brownellfdd13b32005-08-31 11:52:57 -0700200 temp = ohci->num_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 while (temp--) {
202 u32 stat = ohci_readl (ohci,
203 &ohci->regs->roothub.portstatus [temp]);
204
205 /* force global, not selective, resume */
206 if (!(stat & RH_PS_PSS))
207 continue;
208 ohci_writel (ohci, RH_PS_POCI,
209 &ohci->regs->roothub.portstatus [temp]);
210 }
211
212 /* Some controllers (lucent erratum) need extra-long delays */
David Brownellf197b2c2005-09-22 22:42:53 -0700213 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 temp = ohci_readl (ohci, &ohci->regs->control);
216 temp &= OHCI_CTRL_HCFS;
217 if (temp != OHCI_USB_RESUME) {
218 ohci_err (ohci, "controller won't resume\n");
219 return -EBUSY;
220 }
221
222 /* disable old schedule state, reinit from scratch */
223 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
224 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
225 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
226 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
227 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
228 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
229
230 /* Sometimes PCI D3 suspend trashes frame timings ... */
231 periodic_reinit (ohci);
232
Alan Stern8d1a2432006-09-26 14:46:16 -0400233 /* the following code is executed with ohci->lock held and
234 * irqs disabled if and only if autostopped is true
235 */
236
237skip_resume:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* interrupts might have been disabled */
239 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
240 if (ohci->ed_rm_list)
241 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* Then re-enable operations */
244 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
245 (void) ohci_readl (ohci, &ohci->regs->control);
Alan Stern8d1a2432006-09-26 14:46:16 -0400246 if (!autostopped)
247 msleep (3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
David Brownell6a9062f2006-01-23 15:28:07 -0800249 temp = ohci->hc_control;
250 temp &= OHCI_CTRL_RWC;
251 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 ohci->hc_control = temp;
253 ohci_writel (ohci, temp, &ohci->regs->control);
254 (void) ohci_readl (ohci, &ohci->regs->control);
255
256 /* TRSMRCY */
Alan Stern8d1a2432006-09-26 14:46:16 -0400257 if (!autostopped) {
258 msleep (10);
259 spin_lock_irq (&ohci->lock);
260 }
261 /* now ohci->lock is always held and irqs are always disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
David Brownelld4139842006-08-04 11:31:55 -0700263 /* keep it alive for more than ~5x suspend + resume costs */
264 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* maybe turn schedules back on */
267 enables = 0;
268 temp = 0;
269 if (!ohci->ed_rm_list) {
270 if (ohci->ed_controltail) {
271 ohci_writel (ohci,
272 find_head (ohci->ed_controltail)->dma,
273 &ohci->regs->ed_controlhead);
274 enables |= OHCI_CTRL_CLE;
275 temp |= OHCI_CLF;
276 }
277 if (ohci->ed_bulktail) {
278 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
279 &ohci->regs->ed_bulkhead);
280 enables |= OHCI_CTRL_BLE;
281 temp |= OHCI_BLF;
282 }
283 }
284 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
285 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
286 if (enables) {
287 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
288 ohci->hc_control |= enables;
289 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
290 if (temp)
291 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
292 (void) ohci_readl (ohci, &ohci->regs->control);
293 }
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296}
297
Alan Stern8d1a2432006-09-26 14:46:16 -0400298#ifdef CONFIG_PM
299
300static int ohci_bus_suspend (struct usb_hcd *hcd)
301{
302 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
303 int rc;
304
305 spin_lock_irq (&ohci->lock);
306
307 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
308 rc = -ESHUTDOWN;
309 else
310 rc = ohci_rh_suspend (ohci, 0);
311 spin_unlock_irq (&ohci->lock);
312 return rc;
313}
314
315static int ohci_bus_resume (struct usb_hcd *hcd)
316{
317 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
318 int rc;
319
320 if (time_before (jiffies, ohci->next_statechange))
321 msleep(5);
322
323 spin_lock_irq (&ohci->lock);
324
325 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
326 rc = -ESHUTDOWN;
327 else
328 rc = ohci_rh_resume (ohci);
329 spin_unlock_irq (&ohci->lock);
330
331 /* poll until we know a device is connected or we autostop */
332 if (rc == 0)
333 usb_hcd_poll_rh_status(hcd);
334 return rc;
335}
336
David Brownell8ad7fe12005-09-13 19:59:11 -0700337#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/*-------------------------------------------------------------------------*/
340
341/* build "status change" packet (one or two bytes) from HC registers */
342
343static int
344ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
345{
346 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
David Brownellfdd13b32005-08-31 11:52:57 -0700347 int i, changed = 0, length = 1;
Alan Stern8d1a2432006-09-26 14:46:16 -0400348 int any_connected = 0, rhsc_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 unsigned long flags;
350
351 spin_lock_irqsave (&ohci->lock, flags);
352
David Brownellfdd13b32005-08-31 11:52:57 -0700353 /* undocumented erratum seen on at least rev D */
354 if ((ohci->flags & OHCI_QUIRK_AMD756)
355 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
356 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
358 /* retry later; "should not happen" */
359 goto done;
360 }
361
362 /* init status */
363 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
364 buf [0] = changed = 1;
365 else
366 buf [0] = 0;
David Brownellfdd13b32005-08-31 11:52:57 -0700367 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 buf [1] = 0;
369 length++;
370 }
371
372 /* look at each port */
David Brownellfdd13b32005-08-31 11:52:57 -0700373 for (i = 0; i < ohci->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 u32 status = roothub_portstatus (ohci, i);
375
Alan Stern8d1a2432006-09-26 14:46:16 -0400376 /* can't autostop if ports are connected */
377 any_connected |= (status & RH_PS_CCS);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
380 | RH_PS_OCIC | RH_PS_PRSC)) {
381 changed = 1;
382 if (i < 7)
383 buf [0] |= 1 << (i + 1);
384 else
385 buf [1] |= 1 << (i - 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Alan Stern8d1a2432006-09-26 14:46:16 -0400389 /* NOTE: vendors didn't always make the same implementation
390 * choices for RHSC. Sometimes it triggers on an edge (like
391 * setting and maybe clearing a port status change bit); and
392 * it's level-triggered on other silicon, active until khubd
393 * clears all active port status change bits. If it's still
394 * set (level-triggered) we must disable it and rely on
395 * polling until khubd re-enables it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
Alan Stern8d1a2432006-09-26 14:46:16 -0400397 if (ohci_readl (ohci, &ohci->regs->intrstatus) & OHCI_INTR_RHSC) {
398 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
399 (void) ohci_readl (ohci, &ohci->regs->intrdisable);
400 rhsc_enabled = 0;
401 }
402 hcd->poll_rh = 1;
403
404 /* carry out appropriate state changes */
405 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
406
407 case OHCI_USB_OPER:
408 /* keep on polling until we know a device is connected
409 * and RHSC is enabled */
410 if (!ohci->autostop) {
411 if (any_connected) {
412 if (rhsc_enabled)
413 hcd->poll_rh = 0;
414 } else {
415 ohci->autostop = 1;
416 ohci->next_statechange = jiffies + HZ;
417 }
418
419 /* if no devices have been attached for one second, autostop */
420 } else {
421 if (changed || any_connected) {
422 ohci->autostop = 0;
423 ohci->next_statechange = jiffies +
424 STATECHANGE_DELAY;
Alan Stern3da24952006-11-14 16:28:01 -0500425 } else if (device_may_wakeup(&hcd->self.root_hub->dev)
426 && time_after_eq(jiffies,
Alan Stern8d1a2432006-09-26 14:46:16 -0400427 ohci->next_statechange)
428 && !ohci->ed_rm_list
429 && !(ohci->hc_control &
430 OHCI_SCHED_ENABLES)) {
431 ohci_rh_suspend (ohci, 1);
432 }
433 }
434 break;
435
436 /* if there is a port change, autostart or ask to be resumed */
437 case OHCI_USB_SUSPEND:
438 case OHCI_USB_RESUME:
439 if (changed) {
440 if (ohci->autostop)
441 ohci_rh_resume (ohci);
442 else
443 usb_hcd_resume_root_hub (hcd);
444 } else {
445 /* everything is idle, no need for polling */
446 hcd->poll_rh = 0;
447 }
448 break;
449 }
David Brownelld4139842006-08-04 11:31:55 -0700450
451done:
452 spin_unlock_irqrestore (&ohci->lock, flags);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return changed ? length : 0;
455}
456
457/*-------------------------------------------------------------------------*/
458
459static void
460ohci_hub_descriptor (
461 struct ohci_hcd *ohci,
462 struct usb_hub_descriptor *desc
463) {
464 u32 rh = roothub_a (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 u16 temp;
466
467 desc->bDescriptorType = 0x29;
468 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
469 desc->bHubContrCurrent = 0;
470
David Brownellfdd13b32005-08-31 11:52:57 -0700471 desc->bNbrPorts = ohci->num_ports;
472 temp = 1 + (ohci->num_ports / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 desc->bDescLength = 7 + 2 * temp;
474
475 temp = 0;
476 if (rh & RH_A_NPS) /* no power switching? */
477 temp |= 0x0002;
478 if (rh & RH_A_PSM) /* per-port power switching? */
479 temp |= 0x0001;
480 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
481 temp |= 0x0010;
482 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
483 temp |= 0x0008;
484 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
485
486 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
487 rh = roothub_b (ohci);
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700488 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 desc->bitmap [0] = rh & RH_B_DR;
David Brownellfdd13b32005-08-31 11:52:57 -0700490 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700492 desc->bitmap [2] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else
494 desc->bitmap [1] = 0xff;
495}
496
497/*-------------------------------------------------------------------------*/
498
499#ifdef CONFIG_USB_OTG
500
501static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
502{
503 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
504 u32 status;
505
506 if (!port)
507 return -EINVAL;
508 port--;
509
510 /* start port reset before HNP protocol times out */
511 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
512 if (!(status & RH_PS_CCS))
513 return -ENODEV;
514
515 /* khubd will finish the reset later */
516 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
517 return 0;
518}
519
520static void start_hnp(struct ohci_hcd *ohci);
521
522#else
523
524#define ohci_start_port_reset NULL
525
526#endif
527
528/*-------------------------------------------------------------------------*/
529
530
531/* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
532 * not necessarily continuous ... to guard against resume signaling.
533 * The short timeout is safe for non-root hubs, and is backward-compatible
534 * with earlier Linux hosts.
535 */
536#ifdef CONFIG_USB_SUSPEND
537#define PORT_RESET_MSEC 50
538#else
539#define PORT_RESET_MSEC 10
540#endif
541
542/* this timer value might be vendor-specific ... */
543#define PORT_RESET_HW_MSEC 10
544
545/* wrap-aware logic morphed from <linux/jiffies.h> */
546#define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
547
548/* called from some task, normally khubd */
549static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
550{
551 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
552 u32 temp;
553 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
554 u16 reset_done = now + PORT_RESET_MSEC;
555
556 /* build a "continuous enough" reset signal, with up to
557 * 3msec gap between pulses. scheduler HZ==100 must work;
558 * this might need to be deadline-scheduled.
559 */
560 do {
561 /* spin until any current reset finishes */
562 for (;;) {
563 temp = ohci_readl (ohci, portstat);
564 if (!(temp & RH_PS_PRS))
565 break;
566 udelay (500);
567 }
568
569 if (!(temp & RH_PS_CCS))
570 break;
571 if (temp & RH_PS_PRSC)
572 ohci_writel (ohci, RH_PS_PRSC, portstat);
573
574 /* start the next reset, sleep till it's probably done */
575 ohci_writel (ohci, RH_PS_PRS, portstat);
576 msleep(PORT_RESET_HW_MSEC);
577 now = ohci_readl(ohci, &ohci->regs->fmnumber);
578 } while (tick_before(now, reset_done));
579 /* caller synchronizes using PRSC */
580}
581
582static int ohci_hub_control (
583 struct usb_hcd *hcd,
584 u16 typeReq,
585 u16 wValue,
586 u16 wIndex,
587 char *buf,
588 u16 wLength
589) {
590 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
591 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
592 u32 temp;
593 int retval = 0;
594
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100595 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
596 return -ESHUTDOWN;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 switch (typeReq) {
599 case ClearHubFeature:
600 switch (wValue) {
601 case C_HUB_OVER_CURRENT:
602 ohci_writel (ohci, RH_HS_OCIC,
603 &ohci->regs->roothub.status);
604 case C_HUB_LOCAL_POWER:
605 break;
606 default:
607 goto error;
608 }
609 break;
610 case ClearPortFeature:
611 if (!wIndex || wIndex > ports)
612 goto error;
613 wIndex--;
614
615 switch (wValue) {
616 case USB_PORT_FEAT_ENABLE:
617 temp = RH_PS_CCS;
618 break;
619 case USB_PORT_FEAT_C_ENABLE:
620 temp = RH_PS_PESC;
621 break;
622 case USB_PORT_FEAT_SUSPEND:
623 temp = RH_PS_POCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
625 case USB_PORT_FEAT_C_SUSPEND:
626 temp = RH_PS_PSSC;
627 break;
628 case USB_PORT_FEAT_POWER:
629 temp = RH_PS_LSDA;
630 break;
631 case USB_PORT_FEAT_C_CONNECTION:
632 temp = RH_PS_CSC;
633 break;
634 case USB_PORT_FEAT_C_OVER_CURRENT:
635 temp = RH_PS_OCIC;
636 break;
637 case USB_PORT_FEAT_C_RESET:
638 temp = RH_PS_PRSC;
639 break;
640 default:
641 goto error;
642 }
643 ohci_writel (ohci, temp,
644 &ohci->regs->roothub.portstatus [wIndex]);
645 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
646 break;
647 case GetHubDescriptor:
648 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
649 break;
650 case GetHubStatus:
651 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
David Miller92164c5d2006-06-21 22:26:09 -0700652 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 break;
654 case GetPortStatus:
655 if (!wIndex || wIndex > ports)
656 goto error;
657 wIndex--;
658 temp = roothub_portstatus (ohci, wIndex);
David Miller92164c5d2006-06-21 22:26:09 -0700659 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661#ifndef OHCI_VERBOSE_DEBUG
662 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
663#endif
664 dbg_port (ohci, "GetStatus", wIndex, temp);
665 break;
666 case SetHubFeature:
667 switch (wValue) {
668 case C_HUB_OVER_CURRENT:
669 // FIXME: this can be cleared, yes?
670 case C_HUB_LOCAL_POWER:
671 break;
672 default:
673 goto error;
674 }
675 break;
676 case SetPortFeature:
677 if (!wIndex || wIndex > ports)
678 goto error;
679 wIndex--;
680 switch (wValue) {
681 case USB_PORT_FEAT_SUSPEND:
682#ifdef CONFIG_USB_OTG
683 if (hcd->self.otg_port == (wIndex + 1)
684 && hcd->self.b_hnp_enable)
685 start_hnp(ohci);
686 else
687#endif
688 ohci_writel (ohci, RH_PS_PSS,
689 &ohci->regs->roothub.portstatus [wIndex]);
690 break;
691 case USB_PORT_FEAT_POWER:
692 ohci_writel (ohci, RH_PS_PPS,
693 &ohci->regs->roothub.portstatus [wIndex]);
694 break;
695 case USB_PORT_FEAT_RESET:
696 root_port_reset (ohci, wIndex);
697 break;
698 default:
699 goto error;
700 }
701 break;
702
703 default:
704error:
705 /* "protocol stall" on error */
706 retval = -EPIPE;
707 }
708 return retval;
709}
710