blob: 6f113596af66c1660ecc2548f6b8c40b5c26d4e5 [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;
425 } else if (time_after_eq (jiffies,
426 ohci->next_statechange)
427 && !ohci->ed_rm_list
428 && !(ohci->hc_control &
429 OHCI_SCHED_ENABLES)) {
430 ohci_rh_suspend (ohci, 1);
431 }
432 }
433 break;
434
435 /* if there is a port change, autostart or ask to be resumed */
436 case OHCI_USB_SUSPEND:
437 case OHCI_USB_RESUME:
438 if (changed) {
439 if (ohci->autostop)
440 ohci_rh_resume (ohci);
441 else
442 usb_hcd_resume_root_hub (hcd);
443 } else {
444 /* everything is idle, no need for polling */
445 hcd->poll_rh = 0;
446 }
447 break;
448 }
David Brownelld4139842006-08-04 11:31:55 -0700449
450done:
451 spin_unlock_irqrestore (&ohci->lock, flags);
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return changed ? length : 0;
454}
455
456/*-------------------------------------------------------------------------*/
457
458static void
459ohci_hub_descriptor (
460 struct ohci_hcd *ohci,
461 struct usb_hub_descriptor *desc
462) {
463 u32 rh = roothub_a (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 u16 temp;
465
466 desc->bDescriptorType = 0x29;
467 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
468 desc->bHubContrCurrent = 0;
469
David Brownellfdd13b32005-08-31 11:52:57 -0700470 desc->bNbrPorts = ohci->num_ports;
471 temp = 1 + (ohci->num_ports / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 desc->bDescLength = 7 + 2 * temp;
473
474 temp = 0;
475 if (rh & RH_A_NPS) /* no power switching? */
476 temp |= 0x0002;
477 if (rh & RH_A_PSM) /* per-port power switching? */
478 temp |= 0x0001;
479 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
480 temp |= 0x0010;
481 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
482 temp |= 0x0008;
483 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
484
485 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
486 rh = roothub_b (ohci);
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700487 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 desc->bitmap [0] = rh & RH_B_DR;
David Brownellfdd13b32005-08-31 11:52:57 -0700489 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700491 desc->bitmap [2] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else
493 desc->bitmap [1] = 0xff;
494}
495
496/*-------------------------------------------------------------------------*/
497
498#ifdef CONFIG_USB_OTG
499
500static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
501{
502 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
503 u32 status;
504
505 if (!port)
506 return -EINVAL;
507 port--;
508
509 /* start port reset before HNP protocol times out */
510 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
511 if (!(status & RH_PS_CCS))
512 return -ENODEV;
513
514 /* khubd will finish the reset later */
515 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
516 return 0;
517}
518
519static void start_hnp(struct ohci_hcd *ohci);
520
521#else
522
523#define ohci_start_port_reset NULL
524
525#endif
526
527/*-------------------------------------------------------------------------*/
528
529
530/* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
531 * not necessarily continuous ... to guard against resume signaling.
532 * The short timeout is safe for non-root hubs, and is backward-compatible
533 * with earlier Linux hosts.
534 */
535#ifdef CONFIG_USB_SUSPEND
536#define PORT_RESET_MSEC 50
537#else
538#define PORT_RESET_MSEC 10
539#endif
540
541/* this timer value might be vendor-specific ... */
542#define PORT_RESET_HW_MSEC 10
543
544/* wrap-aware logic morphed from <linux/jiffies.h> */
545#define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
546
547/* called from some task, normally khubd */
548static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
549{
550 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
551 u32 temp;
552 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
553 u16 reset_done = now + PORT_RESET_MSEC;
554
555 /* build a "continuous enough" reset signal, with up to
556 * 3msec gap between pulses. scheduler HZ==100 must work;
557 * this might need to be deadline-scheduled.
558 */
559 do {
560 /* spin until any current reset finishes */
561 for (;;) {
562 temp = ohci_readl (ohci, portstat);
563 if (!(temp & RH_PS_PRS))
564 break;
565 udelay (500);
566 }
567
568 if (!(temp & RH_PS_CCS))
569 break;
570 if (temp & RH_PS_PRSC)
571 ohci_writel (ohci, RH_PS_PRSC, portstat);
572
573 /* start the next reset, sleep till it's probably done */
574 ohci_writel (ohci, RH_PS_PRS, portstat);
575 msleep(PORT_RESET_HW_MSEC);
576 now = ohci_readl(ohci, &ohci->regs->fmnumber);
577 } while (tick_before(now, reset_done));
578 /* caller synchronizes using PRSC */
579}
580
581static int ohci_hub_control (
582 struct usb_hcd *hcd,
583 u16 typeReq,
584 u16 wValue,
585 u16 wIndex,
586 char *buf,
587 u16 wLength
588) {
589 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
590 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
591 u32 temp;
592 int retval = 0;
593
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100594 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
595 return -ESHUTDOWN;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 switch (typeReq) {
598 case ClearHubFeature:
599 switch (wValue) {
600 case C_HUB_OVER_CURRENT:
601 ohci_writel (ohci, RH_HS_OCIC,
602 &ohci->regs->roothub.status);
603 case C_HUB_LOCAL_POWER:
604 break;
605 default:
606 goto error;
607 }
608 break;
609 case ClearPortFeature:
610 if (!wIndex || wIndex > ports)
611 goto error;
612 wIndex--;
613
614 switch (wValue) {
615 case USB_PORT_FEAT_ENABLE:
616 temp = RH_PS_CCS;
617 break;
618 case USB_PORT_FEAT_C_ENABLE:
619 temp = RH_PS_PESC;
620 break;
621 case USB_PORT_FEAT_SUSPEND:
622 temp = RH_PS_POCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
624 case USB_PORT_FEAT_C_SUSPEND:
625 temp = RH_PS_PSSC;
626 break;
627 case USB_PORT_FEAT_POWER:
628 temp = RH_PS_LSDA;
629 break;
630 case USB_PORT_FEAT_C_CONNECTION:
631 temp = RH_PS_CSC;
632 break;
633 case USB_PORT_FEAT_C_OVER_CURRENT:
634 temp = RH_PS_OCIC;
635 break;
636 case USB_PORT_FEAT_C_RESET:
637 temp = RH_PS_PRSC;
638 break;
639 default:
640 goto error;
641 }
642 ohci_writel (ohci, temp,
643 &ohci->regs->roothub.portstatus [wIndex]);
644 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
645 break;
646 case GetHubDescriptor:
647 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
648 break;
649 case GetHubStatus:
650 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
David Miller92164c5d2006-06-21 22:26:09 -0700651 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 break;
653 case GetPortStatus:
654 if (!wIndex || wIndex > ports)
655 goto error;
656 wIndex--;
657 temp = roothub_portstatus (ohci, wIndex);
David Miller92164c5d2006-06-21 22:26:09 -0700658 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660#ifndef OHCI_VERBOSE_DEBUG
661 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
662#endif
663 dbg_port (ohci, "GetStatus", wIndex, temp);
664 break;
665 case SetHubFeature:
666 switch (wValue) {
667 case C_HUB_OVER_CURRENT:
668 // FIXME: this can be cleared, yes?
669 case C_HUB_LOCAL_POWER:
670 break;
671 default:
672 goto error;
673 }
674 break;
675 case SetPortFeature:
676 if (!wIndex || wIndex > ports)
677 goto error;
678 wIndex--;
679 switch (wValue) {
680 case USB_PORT_FEAT_SUSPEND:
681#ifdef CONFIG_USB_OTG
682 if (hcd->self.otg_port == (wIndex + 1)
683 && hcd->self.b_hnp_enable)
684 start_hnp(ohci);
685 else
686#endif
687 ohci_writel (ohci, RH_PS_PSS,
688 &ohci->regs->roothub.portstatus [wIndex]);
689 break;
690 case USB_PORT_FEAT_POWER:
691 ohci_writel (ohci, RH_PS_PPS,
692 &ohci->regs->roothub.portstatus [wIndex]);
693 break;
694 case USB_PORT_FEAT_RESET:
695 root_port_reset (ohci, wIndex);
696 break;
697 default:
698 goto error;
699 }
700 break;
701
702 default:
703error:
704 /* "protocol stall" on error */
705 retval = -EPIPE;
706 }
707 return retval;
708}
709