patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / usb / host / ohci-hub.c
1 /*
2  * OHCI HCD (Host Controller Driver) for USB.
3  * 
4  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5  * (C) Copyright 2000-2002 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  * Registers don't need cpu_to_le32, that happens transparently
16  */
17
18 /* AMD-756 (D2 rev) reports corrupt register contents in some cases.
19  * The erratum (#4) description is incorrect.  AMD's workaround waits
20  * till some bits (mostly reserved) are clear; ok for all revs.
21  */
22 #define read_roothub(hc, register, mask) ({ \
23         u32 temp = readl (&hc->regs->roothub.register); \
24         if (temp == -1) \
25                 disable (hc); \
26         else if (hc->flags & OHCI_QUIRK_AMD756) \
27                 while (temp & mask) \
28                         temp = readl (&hc->regs->roothub.register); \
29         temp; })
30
31 static u32 roothub_a (struct ohci_hcd *hc)
32         { return read_roothub (hc, a, 0xfc0fe000); }
33 static inline u32 roothub_b (struct ohci_hcd *hc)
34         { return readl (&hc->regs->roothub.b); }
35 static inline u32 roothub_status (struct ohci_hcd *hc)
36         { return readl (&hc->regs->roothub.status); }
37 static u32 roothub_portstatus (struct ohci_hcd *hc, int i)
38         { return read_roothub (hc, portstatus [i], 0xffe0fce0); }
39
40 /*-------------------------------------------------------------------------*/
41
42 #define dbg_port(hc,label,num,value) \
43         ohci_dbg (hc, \
44                 "%s roothub.portstatus [%d] " \
45                 "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
46                 label, num, temp, \
47                 (temp & RH_PS_PRSC) ? " PRSC" : "", \
48                 (temp & RH_PS_OCIC) ? " OCIC" : "", \
49                 (temp & RH_PS_PSSC) ? " PSSC" : "", \
50                 (temp & RH_PS_PESC) ? " PESC" : "", \
51                 (temp & RH_PS_CSC) ? " CSC" : "", \
52                 \
53                 (temp & RH_PS_LSDA) ? " LSDA" : "", \
54                 (temp & RH_PS_PPS) ? " PPS" : "", \
55                 (temp & RH_PS_PRS) ? " PRS" : "", \
56                 (temp & RH_PS_POCI) ? " POCI" : "", \
57                 (temp & RH_PS_PSS) ? " PSS" : "", \
58                 \
59                 (temp & RH_PS_PES) ? " PES" : "", \
60                 (temp & RH_PS_CCS) ? " CCS" : "" \
61                 );
62
63 /*-------------------------------------------------------------------------*/
64
65 #if     defined(CONFIG_USB_SUSPEND) || defined(CONFIG_PM)
66
67 #define OHCI_SCHED_ENABLES \
68         (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
69
70 static void dl_done_list (struct ohci_hcd *, struct pt_regs *);
71 static void finish_unlinks (struct ohci_hcd *, u16 , struct pt_regs *);
72
73 static int ohci_hub_suspend (struct usb_hcd *hcd)
74 {
75         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
76         struct usb_device       *root = hcd_to_bus (&ohci->hcd)->root_hub;
77         int                     status = 0;
78
79         if (root->dev.power.power_state != 0)
80                 return 0;
81         if (time_before (jiffies, ohci->next_statechange))
82                 return -EAGAIN;
83
84         spin_lock_irq (&ohci->lock);
85
86         ohci->hc_control = readl (&ohci->regs->control);
87         switch (ohci->hc_control & OHCI_CTRL_HCFS) {
88         case OHCI_USB_RESUME:
89                 ohci_dbg (ohci, "resume/suspend?\n");
90                 ohci->hc_control &= ~OHCI_CTRL_HCFS;
91                 ohci->hc_control |= OHCI_USB_RESET;
92                 writel (ohci->hc_control, &ohci->regs->control);
93                 (void) readl (&ohci->regs->control);
94                 /* FALL THROUGH */
95         case OHCI_USB_RESET:
96                 status = -EBUSY;
97                 ohci_dbg (ohci, "needs reinit!\n");
98                 goto done;
99         case OHCI_USB_SUSPEND:
100                 ohci_dbg (ohci, "already suspended?\n");
101                 goto succeed;
102         }
103         ohci_dbg (ohci, "suspend root hub\n");
104
105         /* First stop any processing */
106         ohci->hcd.state = USB_STATE_QUIESCING;
107         if (ohci->hc_control & OHCI_SCHED_ENABLES) {
108                 int             limit;
109
110                 ohci->hc_control &= ~OHCI_SCHED_ENABLES;
111                 writel (ohci->hc_control, &ohci->regs->control);
112                 ohci->hc_control = readl (&ohci->regs->control);
113                 writel (OHCI_INTR_SF, &ohci->regs->intrstatus);
114
115                 /* sched disables take effect on the next frame,
116                  * then the last WDH could take 6+ msec
117                  */
118                 ohci_dbg (ohci, "stopping schedules ...\n");
119                 limit = 2000;
120                 while (limit > 0) {
121                         udelay (250);
122                         limit =- 250;
123                         if (readl (&ohci->regs->intrstatus) & OHCI_INTR_SF)
124                                 break;
125                 }
126                 dl_done_list (ohci, 0);
127                 mdelay (7);
128         }
129         dl_done_list (ohci, 0);
130         finish_unlinks (ohci, OHCI_FRAME_NO(ohci->hcca), 0);
131         writel (readl (&ohci->regs->intrstatus), &ohci->regs->intrstatus);
132
133         /* maybe resume can wake root hub */
134         if (ohci->hcd.remote_wakeup)
135                 ohci->hc_control |= OHCI_CTRL_RWE;
136         else
137                 ohci->hc_control &= ~OHCI_CTRL_RWE;
138
139         /* Suspend hub */
140         ohci->hc_control &= ~OHCI_CTRL_HCFS;
141         ohci->hc_control |= OHCI_USB_SUSPEND;
142         writel (ohci->hc_control, &ohci->regs->control);
143         (void) readl (&ohci->regs->control);
144
145         /* no resumes until devices finish suspending */
146         ohci->next_statechange = jiffies + msecs_to_jiffies (5);
147
148 succeed:
149         /* it's not USB_STATE_SUSPENDED unless access to this
150          * hub from the non-usb side (PCI, SOC, etc) stopped 
151          */
152         root->dev.power.power_state = 3;
153 done:
154         spin_unlock_irq (&ohci->lock);
155         return status;
156 }
157
158 static inline struct ed *find_head (struct ed *ed)
159 {
160         /* for bulk and control lists */
161         while (ed->ed_prev)
162                 ed = ed->ed_prev;
163         return ed;
164 }
165
166 static int hc_restart (struct ohci_hcd *ohci);
167
168 /* caller owns root->serialize */
169 static int ohci_hub_resume (struct usb_hcd *hcd)
170 {
171         struct ohci_hcd         *ohci = hcd_to_ohci (hcd);
172         struct usb_device       *root = hcd_to_bus (&ohci->hcd)->root_hub;
173         u32                     temp, enables;
174         int                     status = -EINPROGRESS;
175
176         if (!root->dev.power.power_state)
177                 return 0;
178         if (time_before (jiffies, ohci->next_statechange))
179                 return -EAGAIN;
180
181         spin_lock_irq (&ohci->lock);
182         ohci->hc_control = readl (&ohci->regs->control);
183         switch (ohci->hc_control & OHCI_CTRL_HCFS) {
184         case OHCI_USB_SUSPEND:
185                 ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
186                 ohci->hc_control |= OHCI_USB_RESUME;
187                 writel (ohci->hc_control, &ohci->regs->control);
188                 (void) readl (&ohci->regs->control);
189                 ohci_dbg (ohci, "resume root hub\n");
190                 break;
191         case OHCI_USB_RESUME:
192                 /* HCFS changes sometime after INTR_RD */
193                 ohci_info (ohci, "remote wakeup\n");
194                 break;
195         case OHCI_USB_OPER:
196                 ohci_dbg (ohci, "odd resume\n");
197                 root->dev.power.power_state = 0;
198                 status = 0;
199                 break;
200         default:                /* RESET, we lost power */
201                 ohci_dbg (ohci, "root hub hardware reset\n");
202                 status = -EBUSY;
203         }
204         spin_unlock_irq (&ohci->lock);
205         if (status == -EBUSY)
206                 return hc_restart (ohci);
207         if (status != -EINPROGRESS)
208                 return status;
209
210         temp = roothub_a (ohci) & RH_A_NDP;
211         enables = 0;
212         while (temp--) {
213                 u32 stat = readl (&ohci->regs->roothub.portstatus [temp]);
214
215                 /* force global, not selective, resume */
216                 if (!(stat & RH_PS_PSS))
217                         continue;
218                 writel (RH_PS_POCI, &ohci->regs->roothub.portstatus [temp]);
219         }
220
221         /* Some controllers (lucent) need extra-long delays */
222         ohci->hcd.state = USB_STATE_RESUMING;
223         mdelay (20 /* usb 11.5.1.10 */ + 15);
224
225         temp = readl (&ohci->regs->control);
226         temp &= OHCI_CTRL_HCFS;
227         if (temp != OHCI_USB_RESUME) {
228                 ohci_err (ohci, "controller won't resume\n");
229                 return -EBUSY;
230         }
231
232         /* disable old schedule state, reinit from scratch */
233         writel (0, &ohci->regs->ed_controlhead);
234         writel (0, &ohci->regs->ed_controlcurrent);
235         writel (0, &ohci->regs->ed_bulkhead);
236         writel (0, &ohci->regs->ed_bulkcurrent);
237         writel (0, &ohci->regs->ed_periodcurrent);
238         writel ((u32) ohci->hcca_dma, &ohci->regs->hcca);
239
240         periodic_reinit (ohci);
241
242         /* interrupts might have been disabled */
243         writel (OHCI_INTR_INIT, &ohci->regs->intrenable);
244         if (ohci->ed_rm_list)
245                 writel (OHCI_INTR_SF, &ohci->regs->intrenable);
246         writel (readl (&ohci->regs->intrstatus), &ohci->regs->intrstatus);
247
248         /* Then re-enable operations */
249         writel (OHCI_USB_OPER, &ohci->regs->control);
250         (void) readl (&ohci->regs->control);
251         msleep (3);
252
253         temp = OHCI_CONTROL_INIT | OHCI_USB_OPER;
254         if (ohci->hcd.can_wakeup)
255                 temp |= OHCI_CTRL_RWC;
256         ohci->hc_control = temp;
257         writel (temp, &ohci->regs->control);
258         (void) readl (&ohci->regs->control);
259
260         /* TRSMRCY */
261         msleep (10);
262         root->dev.power.power_state = 0;
263
264         /* keep it alive for ~5x suspend + resume costs */
265         ohci->next_statechange = jiffies + msecs_to_jiffies (250);
266
267         /* maybe turn schedules back on */
268         enables = 0;
269         temp = 0;
270         if (!ohci->ed_rm_list) {
271                 if (ohci->ed_controltail) {
272                         writel (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                         writel (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_to_bus (&ohci->hcd)->bandwidth_isoc_reqs
285                         || hcd_to_bus (&ohci->hcd)->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                 writel (ohci->hc_control, &ohci->regs->control);
291                 if (temp)
292                         writel (status, &ohci->regs->cmdstatus);
293                 (void) readl (&ohci->regs->control);
294         }
295
296         ohci->hcd.state = USB_STATE_RUNNING;
297         return 0;
298 }
299
300 static void ohci_rh_resume (void *_hcd)
301 {
302         struct usb_hcd  *hcd = _hcd;
303
304         down (&hcd->self.root_hub->serialize);
305         (void) ohci_hub_resume (hcd);
306         up (&hcd->self.root_hub->serialize);
307 }
308
309 #else
310
311 static void ohci_rh_resume (void *_hcd)
312 {
313         struct ohci_hcd *ohci = hcd_to_ohci (_hcd);
314         ohci_dbg(ohci, "rh_resume ??\n");
315 }
316
317 #endif  /* CONFIG_USB_SUSPEND || CONFIG_PM */
318
319 /*-------------------------------------------------------------------------*/
320
321 /* build "status change" packet (one or two bytes) from HC registers */
322
323 static int
324 ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
325 {
326         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
327         int             ports, i, changed = 0, length = 1;
328         int             can_suspend = 1;
329
330         ports = roothub_a (ohci) & RH_A_NDP; 
331         if (ports > MAX_ROOT_PORTS) {
332                 if (!HCD_IS_RUNNING(ohci->hcd.state))
333                         return -ESHUTDOWN;
334                 ohci_err (ohci, "bogus NDP=%d, rereads as NDP=%d\n",
335                         ports, readl (&ohci->regs->roothub.a) & RH_A_NDP);
336                 /* retry later; "should not happen" */
337                 return 0;
338         }
339
340         /* init status */
341         if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
342                 buf [0] = changed = 1;
343         else
344                 buf [0] = 0;
345         if (ports > 7) {
346                 buf [1] = 0;
347                 length++;
348         }
349
350         /* look at each port */
351         for (i = 0; i < ports; i++) {
352                 u32     status = roothub_portstatus (ohci, i);
353
354                 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
355                                 | RH_PS_OCIC | RH_PS_PRSC)) {
356                         changed = 1;
357                         if (i < 7)
358                             buf [0] |= 1 << (i + 1);
359                         else
360                             buf [1] |= 1 << (i - 7);
361                         continue;
362                 }
363
364                 /* can suspend if no ports are enabled; or if all all
365                  * enabled ports are suspended AND remote wakeup is on.
366                  */
367                 if (!(status & RH_PS_CCS))
368                         continue;
369                 if ((status & RH_PS_PSS) && ohci->hcd.remote_wakeup)
370                         continue;
371                 can_suspend = 0;
372         }
373
374 #ifdef CONFIG_PM
375         /* save power by suspending idle root hubs;
376          * INTR_RD wakes us when there's work
377          */
378         if (can_suspend
379                         && !changed
380                         && !ohci->ed_rm_list
381                         && ((OHCI_CTRL_HCFS | OHCI_SCHED_ENABLES)
382                                         & ohci->hc_control)
383                                 == OHCI_USB_OPER
384                         && down_trylock (&hcd->self.root_hub->serialize) == 0
385                         ) {
386                 ohci_vdbg (ohci, "autosuspend\n");
387                 (void) ohci_hub_suspend (&ohci->hcd);
388                 ohci->hcd.state = USB_STATE_RUNNING;
389                 up (&hcd->self.root_hub->serialize);
390         }
391 #endif
392
393         return changed ? length : 0;
394 }
395
396 /*-------------------------------------------------------------------------*/
397
398 static void
399 ohci_hub_descriptor (
400         struct ohci_hcd                 *ohci,
401         struct usb_hub_descriptor       *desc
402 ) {
403         u32             rh = roothub_a (ohci);
404         int             ports = rh & RH_A_NDP; 
405         u16             temp;
406
407         desc->bDescriptorType = 0x29;
408         desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
409         desc->bHubContrCurrent = 0;
410
411         desc->bNbrPorts = ports;
412         temp = 1 + (ports / 8);
413         desc->bDescLength = 7 + 2 * temp;
414
415         temp = 0;
416         if (rh & RH_A_NPS)              /* no power switching? */
417             temp |= 0x0002;
418         if (rh & RH_A_PSM)              /* per-port power switching? */
419             temp |= 0x0001;
420         if (rh & RH_A_NOCP)             /* no overcurrent reporting? */
421             temp |= 0x0010;
422         else if (rh & RH_A_OCPM)        /* per-port overcurrent reporting? */
423             temp |= 0x0008;
424         desc->wHubCharacteristics = cpu_to_le16 (temp);
425
426         /* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
427         rh = roothub_b (ohci);
428         desc->bitmap [0] = rh & RH_B_DR;
429         if (ports > 7) {
430                 desc->bitmap [1] = (rh & RH_B_DR) >> 8;
431                 desc->bitmap [2] = desc->bitmap [3] = 0xff;
432         } else
433                 desc->bitmap [1] = 0xff;
434 }
435
436 /*-------------------------------------------------------------------------*/
437
438 static int ohci_hub_control (
439         struct usb_hcd  *hcd,
440         u16             typeReq,
441         u16             wValue,
442         u16             wIndex,
443         char            *buf,
444         u16             wLength
445 ) {
446         struct ohci_hcd *ohci = hcd_to_ohci (hcd);
447         int             ports = hcd_to_bus (hcd)->root_hub->maxchild;
448         u32             temp;
449         int             retval = 0;
450
451         switch (typeReq) {
452         case ClearHubFeature:
453                 switch (wValue) {
454                 case C_HUB_OVER_CURRENT:
455                         writel (RH_HS_OCIC, &ohci->regs->roothub.status);
456                 case C_HUB_LOCAL_POWER:
457                         break;
458                 default:
459                         goto error;
460                 }
461                 break;
462         case ClearPortFeature:
463                 if (!wIndex || wIndex > ports)
464                         goto error;
465                 wIndex--;
466
467                 switch (wValue) {
468                 case USB_PORT_FEAT_ENABLE:
469                         temp = RH_PS_CCS;
470                         break;
471                 case USB_PORT_FEAT_C_ENABLE:
472                         temp = RH_PS_PESC;
473                         break;
474                 case USB_PORT_FEAT_SUSPEND:
475                         temp = RH_PS_POCI;
476                         if ((ohci->hc_control & OHCI_CTRL_HCFS)
477                                         != OHCI_USB_OPER)
478                                 schedule_work (&ohci->rh_resume);
479                         break;
480                 case USB_PORT_FEAT_C_SUSPEND:
481                         temp = RH_PS_PSSC;
482                         break;
483                 case USB_PORT_FEAT_POWER:
484                         temp = RH_PS_LSDA;
485                         break;
486                 case USB_PORT_FEAT_C_CONNECTION:
487                         temp = RH_PS_CSC;
488                         break;
489                 case USB_PORT_FEAT_C_OVER_CURRENT:
490                         temp = RH_PS_OCIC;
491                         break;
492                 case USB_PORT_FEAT_C_RESET:
493                         temp = RH_PS_PRSC;
494                         break;
495                 default:
496                         goto error;
497                 }
498                 writel (temp, &ohci->regs->roothub.portstatus [wIndex]);
499                 // readl (&ohci->regs->roothub.portstatus [wIndex]);
500                 break;
501         case GetHubDescriptor:
502                 ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
503                 break;
504         case GetHubStatus:
505                 temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
506                 *(u32 *) buf = cpu_to_le32 (temp);
507                 break;
508         case GetPortStatus:
509                 if (!wIndex || wIndex > ports)
510                         goto error;
511                 wIndex--;
512                 temp = roothub_portstatus (ohci, wIndex);
513                 *(u32 *) buf = cpu_to_le32 (temp);
514
515 #ifndef OHCI_VERBOSE_DEBUG
516         if (*(u16*)(buf+2))     /* only if wPortChange is interesting */
517 #endif
518                 dbg_port (ohci, "GetStatus", wIndex + 1, temp);
519                 break;
520         case SetHubFeature:
521                 switch (wValue) {
522                 case C_HUB_OVER_CURRENT:
523                         // FIXME:  this can be cleared, yes?
524                 case C_HUB_LOCAL_POWER:
525                         break;
526                 default:
527                         goto error;
528                 }
529                 break;
530         case SetPortFeature:
531                 if (!wIndex || wIndex > ports)
532                         goto error;
533                 wIndex--;
534                 switch (wValue) {
535                 case USB_PORT_FEAT_SUSPEND:
536                         writel (RH_PS_PSS,
537                                 &ohci->regs->roothub.portstatus [wIndex]);
538                         break;
539                 case USB_PORT_FEAT_POWER:
540                         writel (RH_PS_PPS,
541                                 &ohci->regs->roothub.portstatus [wIndex]);
542                         break;
543                 case USB_PORT_FEAT_RESET:
544                         temp = readl (&ohci->regs->roothub.portstatus [wIndex]);
545                         if (temp & RH_PS_CCS)
546                                 writel (RH_PS_PRS,
547                                     &ohci->regs->roothub.portstatus [wIndex]);
548                         break;
549                 default:
550                         goto error;
551                 }
552                 break;
553
554         default:
555 error:
556                 /* "protocol stall" on error */
557                 retval = -EPIPE;
558         }
559         return retval;
560 }
561