blob: 6995ea36f2e874499ae407e9b699931ceba117bd [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 */
Alan Stern583cead2006-10-24 12:04:22 -0400172 ohci_info(ohci, "%swakeup\n",
173 autostopped ? "auto-" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 break;
175 case OHCI_USB_OPER:
David Brownellf197b2c2005-09-22 22:42:53 -0700176 /* this can happen after resuming a swsusp snapshot */
177 ohci_dbg (ohci, "snapshot resume? reinit\n");
178 status = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 break;
180 default: /* RESET, we lost power */
David Brownellf197b2c2005-09-22 22:42:53 -0700181 ohci_dbg (ohci, "lost power\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 status = -EBUSY;
183 }
Alan Stern8d1a2432006-09-26 14:46:16 -0400184#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (status == -EBUSY) {
Alan Stern8d1a2432006-09-26 14:46:16 -0400186 if (!autostopped) {
Alan Stern8d1a2432006-09-26 14:46:16 -0400187 spin_unlock_irq (&ohci->lock);
188 (void) ohci_init (ohci);
189 status = ohci_restart (ohci);
190 spin_lock_irq (&ohci->lock);
191 }
192 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Alan Stern8d1a2432006-09-26 14:46:16 -0400194#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (status != -EINPROGRESS)
196 return status;
Alan Stern8d1a2432006-09-26 14:46:16 -0400197 if (autostopped)
198 goto skip_resume;
199 spin_unlock_irq (&ohci->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
David Brownellfdd13b32005-08-31 11:52:57 -0700201 temp = ohci->num_ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 while (temp--) {
203 u32 stat = ohci_readl (ohci,
204 &ohci->regs->roothub.portstatus [temp]);
205
206 /* force global, not selective, resume */
207 if (!(stat & RH_PS_PSS))
208 continue;
209 ohci_writel (ohci, RH_PS_POCI,
210 &ohci->regs->roothub.portstatus [temp]);
211 }
212
213 /* Some controllers (lucent erratum) need extra-long delays */
David Brownellf197b2c2005-09-22 22:42:53 -0700214 msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 temp = ohci_readl (ohci, &ohci->regs->control);
217 temp &= OHCI_CTRL_HCFS;
218 if (temp != OHCI_USB_RESUME) {
219 ohci_err (ohci, "controller won't resume\n");
220 return -EBUSY;
221 }
222
223 /* disable old schedule state, reinit from scratch */
224 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
225 ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
226 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
227 ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
228 ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
229 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
230
231 /* Sometimes PCI D3 suspend trashes frame timings ... */
232 periodic_reinit (ohci);
233
Alan Stern8d1a2432006-09-26 14:46:16 -0400234 /* the following code is executed with ohci->lock held and
235 * irqs disabled if and only if autostopped is true
236 */
237
238skip_resume:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 /* interrupts might have been disabled */
240 ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
241 if (ohci->ed_rm_list)
242 ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /* Then re-enable operations */
245 ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
246 (void) ohci_readl (ohci, &ohci->regs->control);
Alan Stern8d1a2432006-09-26 14:46:16 -0400247 if (!autostopped)
248 msleep (3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
David Brownell6a9062f2006-01-23 15:28:07 -0800250 temp = ohci->hc_control;
251 temp &= OHCI_CTRL_RWC;
252 temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ohci->hc_control = temp;
254 ohci_writel (ohci, temp, &ohci->regs->control);
255 (void) ohci_readl (ohci, &ohci->regs->control);
256
257 /* TRSMRCY */
Alan Stern8d1a2432006-09-26 14:46:16 -0400258 if (!autostopped) {
259 msleep (10);
260 spin_lock_irq (&ohci->lock);
261 }
262 /* now ohci->lock is always held and irqs are always disabled */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
David Brownelld4139842006-08-04 11:31:55 -0700264 /* keep it alive for more than ~5x suspend + resume costs */
265 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 /* maybe turn schedules back on */
268 enables = 0;
269 temp = 0;
270 if (!ohci->ed_rm_list) {
271 if (ohci->ed_controltail) {
272 ohci_writel (ohci,
273 find_head (ohci->ed_controltail)->dma,
274 &ohci->regs->ed_controlhead);
275 enables |= OHCI_CTRL_CLE;
276 temp |= OHCI_CLF;
277 }
278 if (ohci->ed_bulktail) {
279 ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
280 &ohci->regs->ed_bulkhead);
281 enables |= OHCI_CTRL_BLE;
282 temp |= OHCI_BLF;
283 }
284 }
285 if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
286 enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
287 if (enables) {
288 ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
289 ohci->hc_control |= enables;
290 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
291 if (temp)
292 ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
293 (void) ohci_readl (ohci, &ohci->regs->control);
294 }
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return 0;
297}
298
Alan Stern8d1a2432006-09-26 14:46:16 -0400299#ifdef CONFIG_PM
300
301static int ohci_bus_suspend (struct usb_hcd *hcd)
302{
303 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
304 int rc;
305
306 spin_lock_irq (&ohci->lock);
307
308 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
309 rc = -ESHUTDOWN;
310 else
311 rc = ohci_rh_suspend (ohci, 0);
312 spin_unlock_irq (&ohci->lock);
313 return rc;
314}
315
316static int ohci_bus_resume (struct usb_hcd *hcd)
317{
318 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
319 int rc;
320
321 if (time_before (jiffies, ohci->next_statechange))
322 msleep(5);
323
324 spin_lock_irq (&ohci->lock);
325
326 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
327 rc = -ESHUTDOWN;
328 else
329 rc = ohci_rh_resume (ohci);
330 spin_unlock_irq (&ohci->lock);
331
332 /* poll until we know a device is connected or we autostop */
333 if (rc == 0)
334 usb_hcd_poll_rh_status(hcd);
335 return rc;
336}
337
David Brownell8ad7fe12005-09-13 19:59:11 -0700338#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/*-------------------------------------------------------------------------*/
341
342/* build "status change" packet (one or two bytes) from HC registers */
343
344static int
345ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
346{
347 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
David Brownellfdd13b32005-08-31 11:52:57 -0700348 int i, changed = 0, length = 1;
Alan Stern8d1a2432006-09-26 14:46:16 -0400349 int any_connected = 0, rhsc_enabled = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 unsigned long flags;
351
352 spin_lock_irqsave (&ohci->lock, flags);
353
David Brownellfdd13b32005-08-31 11:52:57 -0700354 /* undocumented erratum seen on at least rev D */
355 if ((ohci->flags & OHCI_QUIRK_AMD756)
356 && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
357 ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
359 /* retry later; "should not happen" */
360 goto done;
361 }
362
363 /* init status */
364 if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
365 buf [0] = changed = 1;
366 else
367 buf [0] = 0;
David Brownellfdd13b32005-08-31 11:52:57 -0700368 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 buf [1] = 0;
370 length++;
371 }
372
373 /* look at each port */
David Brownellfdd13b32005-08-31 11:52:57 -0700374 for (i = 0; i < ohci->num_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 u32 status = roothub_portstatus (ohci, i);
376
Alan Stern8d1a2432006-09-26 14:46:16 -0400377 /* can't autostop if ports are connected */
378 any_connected |= (status & RH_PS_CCS);
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
381 | RH_PS_OCIC | RH_PS_PRSC)) {
382 changed = 1;
383 if (i < 7)
384 buf [0] |= 1 << (i + 1);
385 else
386 buf [1] |= 1 << (i - 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Alan Stern8d1a2432006-09-26 14:46:16 -0400390 /* NOTE: vendors didn't always make the same implementation
391 * choices for RHSC. Sometimes it triggers on an edge (like
392 * setting and maybe clearing a port status change bit); and
393 * it's level-triggered on other silicon, active until khubd
394 * clears all active port status change bits. If it's still
395 * set (level-triggered) we must disable it and rely on
396 * polling until khubd re-enables it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Alan Stern8d1a2432006-09-26 14:46:16 -0400398 if (ohci_readl (ohci, &ohci->regs->intrstatus) & OHCI_INTR_RHSC) {
399 ohci_writel (ohci, OHCI_INTR_RHSC, &ohci->regs->intrdisable);
400 (void) ohci_readl (ohci, &ohci->regs->intrdisable);
401 rhsc_enabled = 0;
402 }
403 hcd->poll_rh = 1;
404
405 /* carry out appropriate state changes */
406 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
407
408 case OHCI_USB_OPER:
409 /* keep on polling until we know a device is connected
410 * and RHSC is enabled */
411 if (!ohci->autostop) {
412 if (any_connected) {
413 if (rhsc_enabled)
414 hcd->poll_rh = 0;
415 } else {
416 ohci->autostop = 1;
417 ohci->next_statechange = jiffies + HZ;
418 }
419
420 /* if no devices have been attached for one second, autostop */
421 } else {
422 if (changed || any_connected) {
423 ohci->autostop = 0;
424 ohci->next_statechange = jiffies +
425 STATECHANGE_DELAY;
Alan Stern3da24952006-11-14 16:28:01 -0500426 } else if (device_may_wakeup(&hcd->self.root_hub->dev)
427 && time_after_eq(jiffies,
Alan Stern8d1a2432006-09-26 14:46:16 -0400428 ohci->next_statechange)
429 && !ohci->ed_rm_list
430 && !(ohci->hc_control &
431 OHCI_SCHED_ENABLES)) {
432 ohci_rh_suspend (ohci, 1);
433 }
434 }
435 break;
436
437 /* if there is a port change, autostart or ask to be resumed */
438 case OHCI_USB_SUSPEND:
439 case OHCI_USB_RESUME:
440 if (changed) {
441 if (ohci->autostop)
442 ohci_rh_resume (ohci);
443 else
444 usb_hcd_resume_root_hub (hcd);
445 } else {
446 /* everything is idle, no need for polling */
447 hcd->poll_rh = 0;
448 }
449 break;
450 }
David Brownelld4139842006-08-04 11:31:55 -0700451
452done:
453 spin_unlock_irqrestore (&ohci->lock, flags);
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return changed ? length : 0;
456}
457
458/*-------------------------------------------------------------------------*/
459
460static void
461ohci_hub_descriptor (
462 struct ohci_hcd *ohci,
463 struct usb_hub_descriptor *desc
464) {
465 u32 rh = roothub_a (ohci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 u16 temp;
467
468 desc->bDescriptorType = 0x29;
469 desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
470 desc->bHubContrCurrent = 0;
471
David Brownellfdd13b32005-08-31 11:52:57 -0700472 desc->bNbrPorts = ohci->num_ports;
473 temp = 1 + (ohci->num_ports / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 desc->bDescLength = 7 + 2 * temp;
475
476 temp = 0;
477 if (rh & RH_A_NPS) /* no power switching? */
478 temp |= 0x0002;
479 if (rh & RH_A_PSM) /* per-port power switching? */
480 temp |= 0x0001;
481 if (rh & RH_A_NOCP) /* no overcurrent reporting? */
482 temp |= 0x0010;
483 else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
484 temp |= 0x0008;
485 desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
486
487 /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
488 rh = roothub_b (ohci);
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700489 memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 desc->bitmap [0] = rh & RH_B_DR;
David Brownellfdd13b32005-08-31 11:52:57 -0700491 if (ohci->num_ports > 7) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
KAMBAROV, ZAURb2134bc2005-06-24 22:20:35 -0700493 desc->bitmap [2] = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 } else
495 desc->bitmap [1] = 0xff;
496}
497
498/*-------------------------------------------------------------------------*/
499
500#ifdef CONFIG_USB_OTG
501
502static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
503{
504 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
505 u32 status;
506
507 if (!port)
508 return -EINVAL;
509 port--;
510
511 /* start port reset before HNP protocol times out */
512 status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
513 if (!(status & RH_PS_CCS))
514 return -ENODEV;
515
516 /* khubd will finish the reset later */
517 ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
518 return 0;
519}
520
521static void start_hnp(struct ohci_hcd *ohci);
522
523#else
524
525#define ohci_start_port_reset NULL
526
527#endif
528
529/*-------------------------------------------------------------------------*/
530
531
532/* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
533 * not necessarily continuous ... to guard against resume signaling.
534 * The short timeout is safe for non-root hubs, and is backward-compatible
535 * with earlier Linux hosts.
536 */
537#ifdef CONFIG_USB_SUSPEND
538#define PORT_RESET_MSEC 50
539#else
540#define PORT_RESET_MSEC 10
541#endif
542
543/* this timer value might be vendor-specific ... */
544#define PORT_RESET_HW_MSEC 10
545
546/* wrap-aware logic morphed from <linux/jiffies.h> */
547#define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
548
549/* called from some task, normally khubd */
550static inline void root_port_reset (struct ohci_hcd *ohci, unsigned port)
551{
552 __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
553 u32 temp;
554 u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
555 u16 reset_done = now + PORT_RESET_MSEC;
556
557 /* build a "continuous enough" reset signal, with up to
558 * 3msec gap between pulses. scheduler HZ==100 must work;
559 * this might need to be deadline-scheduled.
560 */
561 do {
562 /* spin until any current reset finishes */
563 for (;;) {
564 temp = ohci_readl (ohci, portstat);
565 if (!(temp & RH_PS_PRS))
566 break;
567 udelay (500);
568 }
569
570 if (!(temp & RH_PS_CCS))
571 break;
572 if (temp & RH_PS_PRSC)
573 ohci_writel (ohci, RH_PS_PRSC, portstat);
574
575 /* start the next reset, sleep till it's probably done */
576 ohci_writel (ohci, RH_PS_PRS, portstat);
577 msleep(PORT_RESET_HW_MSEC);
578 now = ohci_readl(ohci, &ohci->regs->fmnumber);
579 } while (tick_before(now, reset_done));
580 /* caller synchronizes using PRSC */
581}
582
583static int ohci_hub_control (
584 struct usb_hcd *hcd,
585 u16 typeReq,
586 u16 wValue,
587 u16 wIndex,
588 char *buf,
589 u16 wLength
590) {
591 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
592 int ports = hcd_to_bus (hcd)->root_hub->maxchild;
593 u32 temp;
594 int retval = 0;
595
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100596 if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
597 return -ESHUTDOWN;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 switch (typeReq) {
600 case ClearHubFeature:
601 switch (wValue) {
602 case C_HUB_OVER_CURRENT:
603 ohci_writel (ohci, RH_HS_OCIC,
604 &ohci->regs->roothub.status);
605 case C_HUB_LOCAL_POWER:
606 break;
607 default:
608 goto error;
609 }
610 break;
611 case ClearPortFeature:
612 if (!wIndex || wIndex > ports)
613 goto error;
614 wIndex--;
615
616 switch (wValue) {
617 case USB_PORT_FEAT_ENABLE:
618 temp = RH_PS_CCS;
619 break;
620 case USB_PORT_FEAT_C_ENABLE:
621 temp = RH_PS_PESC;
622 break;
623 case USB_PORT_FEAT_SUSPEND:
624 temp = RH_PS_POCI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 break;
626 case USB_PORT_FEAT_C_SUSPEND:
627 temp = RH_PS_PSSC;
628 break;
629 case USB_PORT_FEAT_POWER:
630 temp = RH_PS_LSDA;
631 break;
632 case USB_PORT_FEAT_C_CONNECTION:
633 temp = RH_PS_CSC;
634 break;
635 case USB_PORT_FEAT_C_OVER_CURRENT:
636 temp = RH_PS_OCIC;
637 break;
638 case USB_PORT_FEAT_C_RESET:
639 temp = RH_PS_PRSC;
640 break;
641 default:
642 goto error;
643 }
644 ohci_writel (ohci, temp,
645 &ohci->regs->roothub.portstatus [wIndex]);
646 // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
647 break;
648 case GetHubDescriptor:
649 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
650 break;
651 case GetHubStatus:
652 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
David Miller92164c5d2006-06-21 22:26:09 -0700653 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 break;
655 case GetPortStatus:
656 if (!wIndex || wIndex > ports)
657 goto error;
658 wIndex--;
659 temp = roothub_portstatus (ohci, wIndex);
David Miller92164c5d2006-06-21 22:26:09 -0700660 put_unaligned(cpu_to_le32 (temp), (__le32 *) buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662#ifndef OHCI_VERBOSE_DEBUG
663 if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
664#endif
665 dbg_port (ohci, "GetStatus", wIndex, temp);
666 break;
667 case SetHubFeature:
668 switch (wValue) {
669 case C_HUB_OVER_CURRENT:
670 // FIXME: this can be cleared, yes?
671 case C_HUB_LOCAL_POWER:
672 break;
673 default:
674 goto error;
675 }
676 break;
677 case SetPortFeature:
678 if (!wIndex || wIndex > ports)
679 goto error;
680 wIndex--;
681 switch (wValue) {
682 case USB_PORT_FEAT_SUSPEND:
683#ifdef CONFIG_USB_OTG
684 if (hcd->self.otg_port == (wIndex + 1)
685 && hcd->self.b_hnp_enable)
686 start_hnp(ohci);
687 else
688#endif
689 ohci_writel (ohci, RH_PS_PSS,
690 &ohci->regs->roothub.portstatus [wIndex]);
691 break;
692 case USB_PORT_FEAT_POWER:
693 ohci_writel (ohci, RH_PS_PPS,
694 &ohci->regs->roothub.portstatus [wIndex]);
695 break;
696 case USB_PORT_FEAT_RESET:
697 root_port_reset (ohci, wIndex);
698 break;
699 default:
700 goto error;
701 }
702 break;
703
704 default:
705error:
706 /* "protocol stall" on error */
707 retval = -EPIPE;
708 }
709 return retval;
710}
711