fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / usb / host / ehci-hub.c
1 /*
2  * Copyright (C) 2001-2004 by David Brownell
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 /* this file is part of ehci-hcd.c */
20
21 /*-------------------------------------------------------------------------*/
22
23 /*
24  * EHCI Root Hub ... the nonsharable stuff
25  *
26  * Registers don't need cpu_to_le32, that happens transparently
27  */
28
29 /*-------------------------------------------------------------------------*/
30
31 #ifdef  CONFIG_PM
32
33 static int ehci_bus_suspend (struct usb_hcd *hcd)
34 {
35         struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
36         int                     port;
37         int                     mask;
38
39         if (time_before (jiffies, ehci->next_statechange))
40                 msleep(5);
41
42         port = HCS_N_PORTS (ehci->hcs_params);
43         spin_lock_irq (&ehci->lock);
44
45         /* stop schedules, clean any completed work */
46         if (HC_IS_RUNNING(hcd->state)) {
47                 ehci_quiesce (ehci);
48                 hcd->state = HC_STATE_QUIESCING;
49         }
50         ehci->command = readl (&ehci->regs->command);
51         if (ehci->reclaim)
52                 ehci->reclaim_ready = 1;
53         ehci_work(ehci);
54
55         /* Unlike other USB host controller types, EHCI doesn't have
56          * any notion of "global" or bus-wide suspend.  The driver has
57          * to manually suspend all the active unsuspended ports, and
58          * then manually resume them in the bus_resume() routine.
59          */
60         ehci->bus_suspended = 0;
61         while (port--) {
62                 u32 __iomem     *reg = &ehci->regs->port_status [port];
63                 u32             t1 = readl (reg) & ~PORT_RWC_BITS;
64                 u32             t2 = t1;
65
66                 /* keep track of which ports we suspend */
67                 if ((t1 & PORT_PE) && !(t1 & PORT_OWNER) &&
68                                 !(t1 & PORT_SUSPEND)) {
69                         t2 |= PORT_SUSPEND;
70                         set_bit(port, &ehci->bus_suspended);
71                 }
72
73                 /* enable remote wakeup on all ports */
74                 if (device_may_wakeup(&hcd->self.root_hub->dev))
75                         t2 |= PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E;
76                 else
77                         t2 &= ~(PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E);
78
79                 if (t1 != t2) {
80                         ehci_vdbg (ehci, "port %d, %08x -> %08x\n",
81                                 port + 1, t1, t2);
82                         writel (t2, reg);
83                 }
84         }
85
86         /* turn off now-idle HC */
87         del_timer_sync (&ehci->watchdog);
88         ehci_halt (ehci);
89         hcd->state = HC_STATE_SUSPENDED;
90
91         /* allow remote wakeup */
92         mask = INTR_MASK;
93         if (!device_may_wakeup(&hcd->self.root_hub->dev))
94                 mask &= ~STS_PCD;
95         writel(mask, &ehci->regs->intr_enable);
96         readl(&ehci->regs->intr_enable);
97
98         ehci->next_statechange = jiffies + msecs_to_jiffies(10);
99         spin_unlock_irq (&ehci->lock);
100         return 0;
101 }
102
103
104 /* caller has locked the root hub, and should reset/reinit on error */
105 static int ehci_bus_resume (struct usb_hcd *hcd)
106 {
107         struct ehci_hcd         *ehci = hcd_to_ehci (hcd);
108         u32                     temp;
109         int                     i;
110
111         if (time_before (jiffies, ehci->next_statechange))
112                 msleep(5);
113         spin_lock_irq (&ehci->lock);
114
115         /* Ideally and we've got a real resume here, and no port's power
116          * was lost.  (For PCI, that means Vaux was maintained.)  But we
117          * could instead be restoring a swsusp snapshot -- so that BIOS was
118          * the last user of the controller, not reset/pm hardware keeping
119          * state we gave to it.
120          */
121         temp = readl(&ehci->regs->intr_enable);
122         ehci_dbg(ehci, "resume root hub%s\n", temp ? "" : " after power loss");
123
124         /* at least some APM implementations will try to deliver
125          * IRQs right away, so delay them until we're ready.
126          */
127         writel(0, &ehci->regs->intr_enable);
128
129         /* re-init operational registers */
130         writel(0, &ehci->regs->segment);
131         writel(ehci->periodic_dma, &ehci->regs->frame_list);
132         writel((u32) ehci->async->qh_dma, &ehci->regs->async_next);
133
134         /* restore CMD_RUN, framelist size, and irq threshold */
135         writel (ehci->command, &ehci->regs->command);
136
137         /* Some controller/firmware combinations need a delay during which
138          * they set up the port statuses.  See Bugzilla #8190. */
139         mdelay(8);
140
141         /* manually resume the ports we suspended during bus_suspend() */
142         i = HCS_N_PORTS (ehci->hcs_params);
143         while (i--) {
144                 temp = readl (&ehci->regs->port_status [i]);
145                 temp &= ~(PORT_RWC_BITS
146                         | PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E);
147                 if (test_bit(i, &ehci->bus_suspended) &&
148                                 (temp & PORT_SUSPEND)) {
149                         ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
150                         temp |= PORT_RESUME;
151                 }
152                 writel (temp, &ehci->regs->port_status [i]);
153         }
154         i = HCS_N_PORTS (ehci->hcs_params);
155         mdelay (20);
156         while (i--) {
157                 temp = readl (&ehci->regs->port_status [i]);
158                 if (test_bit(i, &ehci->bus_suspended) &&
159                                 (temp & PORT_SUSPEND)) {
160                         temp &= ~(PORT_RWC_BITS | PORT_RESUME);
161                         writel (temp, &ehci->regs->port_status [i]);
162                         ehci_vdbg (ehci, "resumed port %d\n", i + 1);
163                 }
164         }
165         (void) readl (&ehci->regs->command);
166
167         /* maybe re-activate the schedule(s) */
168         temp = 0;
169         if (ehci->async->qh_next.qh)
170                 temp |= CMD_ASE;
171         if (ehci->periodic_sched)
172                 temp |= CMD_PSE;
173         if (temp) {
174                 ehci->command |= temp;
175                 writel (ehci->command, &ehci->regs->command);
176         }
177
178         ehci->next_statechange = jiffies + msecs_to_jiffies(5);
179         hcd->state = HC_STATE_RUNNING;
180
181         /* Now we can safely re-enable irqs */
182         writel(INTR_MASK, &ehci->regs->intr_enable);
183
184         spin_unlock_irq (&ehci->lock);
185         return 0;
186 }
187
188 #else
189
190 #define ehci_bus_suspend        NULL
191 #define ehci_bus_resume         NULL
192
193 #endif  /* CONFIG_PM */
194
195 /*-------------------------------------------------------------------------*/
196
197 static int check_reset_complete (
198         struct ehci_hcd *ehci,
199         int             index,
200         int             port_status
201 ) {
202         if (!(port_status & PORT_CONNECT)) {
203                 ehci->reset_done [index] = 0;
204                 return port_status;
205         }
206
207         /* if reset finished and it's still not enabled -- handoff */
208         if (!(port_status & PORT_PE)) {
209
210                 /* with integrated TT, there's nobody to hand it to! */
211                 if (ehci_is_TDI(ehci)) {
212                         ehci_dbg (ehci,
213                                 "Failed to enable port %d on root hub TT\n",
214                                 index+1);
215                         return port_status;
216                 }
217
218                 ehci_dbg (ehci, "port %d full speed --> companion\n",
219                         index + 1);
220
221                 // what happens if HCS_N_CC(params) == 0 ?
222                 port_status |= PORT_OWNER;
223                 port_status &= ~PORT_RWC_BITS;
224                 writel (port_status, &ehci->regs->port_status [index]);
225
226         } else
227                 ehci_dbg (ehci, "port %d high speed\n", index + 1);
228
229         return port_status;
230 }
231
232 /*-------------------------------------------------------------------------*/
233
234
235 /* build "status change" packet (one or two bytes) from HC registers */
236
237 static int
238 ehci_hub_status_data (struct usb_hcd *hcd, char *buf)
239 {
240         struct ehci_hcd *ehci = hcd_to_ehci (hcd);
241         u32             temp, status = 0;
242         u32             mask;
243         int             ports, i, retval = 1;
244         unsigned long   flags;
245
246         /* if !USB_SUSPEND, root hub timers won't get shut down ... */
247         if (!HC_IS_RUNNING(hcd->state))
248                 return 0;
249
250         /* init status to no-changes */
251         buf [0] = 0;
252         ports = HCS_N_PORTS (ehci->hcs_params);
253         if (ports > 7) {
254                 buf [1] = 0;
255                 retval++;
256         }
257
258         /* Some boards (mostly VIA?) report bogus overcurrent indications,
259          * causing massive log spam unless we completely ignore them.  It
260          * may be relevant that VIA VT8235 controlers, where PORT_POWER is
261          * always set, seem to clear PORT_OCC and PORT_CSC when writing to
262          * PORT_POWER; that's surprising, but maybe within-spec.
263          */
264         if (!ignore_oc)
265                 mask = PORT_CSC | PORT_PEC | PORT_OCC;
266         else
267                 mask = PORT_CSC | PORT_PEC;
268         // PORT_RESUME from hardware ~= PORT_STAT_C_SUSPEND
269
270         /* no hub change reports (bit 0) for now (power, ...) */
271
272         /* port N changes (bit N)? */
273         spin_lock_irqsave (&ehci->lock, flags);
274         for (i = 0; i < ports; i++) {
275                 temp = readl (&ehci->regs->port_status [i]);
276                 if (temp & PORT_OWNER) {
277                         /* don't report this in GetPortStatus */
278                         if (temp & PORT_CSC) {
279                                 temp &= ~PORT_RWC_BITS;
280                                 temp |= PORT_CSC;
281                                 writel (temp, &ehci->regs->port_status [i]);
282                         }
283                         continue;
284                 }
285                 if (!(temp & PORT_CONNECT))
286                         ehci->reset_done [i] = 0;
287                 if ((temp & mask) != 0
288                                 || ((temp & PORT_RESUME) != 0
289                                         && time_after (jiffies,
290                                                 ehci->reset_done [i]))) {
291                         if (i < 7)
292                             buf [0] |= 1 << (i + 1);
293                         else
294                             buf [1] |= 1 << (i - 7);
295                         status = STS_PCD;
296                 }
297         }
298         /* FIXME autosuspend idle root hubs */
299         spin_unlock_irqrestore (&ehci->lock, flags);
300         return status ? retval : 0;
301 }
302
303 /*-------------------------------------------------------------------------*/
304
305 static void
306 ehci_hub_descriptor (
307         struct ehci_hcd                 *ehci,
308         struct usb_hub_descriptor       *desc
309 ) {
310         int             ports = HCS_N_PORTS (ehci->hcs_params);
311         u16             temp;
312
313         desc->bDescriptorType = 0x29;
314         desc->bPwrOn2PwrGood = 10;      /* ehci 1.0, 2.3.9 says 20ms max */
315         desc->bHubContrCurrent = 0;
316
317         desc->bNbrPorts = ports;
318         temp = 1 + (ports / 8);
319         desc->bDescLength = 7 + 2 * temp;
320
321         /* two bitmaps:  ports removable, and usb 1.0 legacy PortPwrCtrlMask */
322         memset (&desc->bitmap [0], 0, temp);
323         memset (&desc->bitmap [temp], 0xff, temp);
324
325         temp = 0x0008;                  /* per-port overcurrent reporting */
326         if (HCS_PPC (ehci->hcs_params))
327                 temp |= 0x0001;         /* per-port power control */
328         else
329                 temp |= 0x0002;         /* no power switching */
330 #if 0
331 // re-enable when we support USB_PORT_FEAT_INDICATOR below.
332         if (HCS_INDICATOR (ehci->hcs_params))
333                 temp |= 0x0080;         /* per-port indicators (LEDs) */
334 #endif
335         desc->wHubCharacteristics = (__force __u16)cpu_to_le16 (temp);
336 }
337
338 /*-------------------------------------------------------------------------*/
339
340 #define PORT_WAKE_BITS  (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
341
342 static int ehci_hub_control (
343         struct usb_hcd  *hcd,
344         u16             typeReq,
345         u16             wValue,
346         u16             wIndex,
347         char            *buf,
348         u16             wLength
349 ) {
350         struct ehci_hcd *ehci = hcd_to_ehci (hcd);
351         int             ports = HCS_N_PORTS (ehci->hcs_params);
352         u32             temp, status;
353         unsigned long   flags;
354         int             retval = 0;
355         unsigned        selector;
356
357         /*
358          * FIXME:  support SetPortFeatures USB_PORT_FEAT_INDICATOR.
359          * HCS_INDICATOR may say we can change LEDs to off/amber/green.
360          * (track current state ourselves) ... blink for diagnostics,
361          * power, "this is the one", etc.  EHCI spec supports this.
362          */
363
364         spin_lock_irqsave (&ehci->lock, flags);
365         switch (typeReq) {
366         case ClearHubFeature:
367                 switch (wValue) {
368                 case C_HUB_LOCAL_POWER:
369                 case C_HUB_OVER_CURRENT:
370                         /* no hub-wide feature/status flags */
371                         break;
372                 default:
373                         goto error;
374                 }
375                 break;
376         case ClearPortFeature:
377                 if (!wIndex || wIndex > ports)
378                         goto error;
379                 wIndex--;
380                 temp = readl (&ehci->regs->port_status [wIndex]);
381                 if (temp & PORT_OWNER)
382                         break;
383
384                 switch (wValue) {
385                 case USB_PORT_FEAT_ENABLE:
386                         writel (temp & ~PORT_PE,
387                                 &ehci->regs->port_status [wIndex]);
388                         break;
389                 case USB_PORT_FEAT_C_ENABLE:
390                         writel((temp & ~PORT_RWC_BITS) | PORT_PEC,
391                                 &ehci->regs->port_status [wIndex]);
392                         break;
393                 case USB_PORT_FEAT_SUSPEND:
394                         if (temp & PORT_RESET)
395                                 goto error;
396                         if (ehci->no_selective_suspend)
397                                 break;
398                         if (temp & PORT_SUSPEND) {
399                                 if ((temp & PORT_PE) == 0)
400                                         goto error;
401                                 /* resume signaling for 20 msec */
402                                 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
403                                 writel (temp | PORT_RESUME,
404                                         &ehci->regs->port_status [wIndex]);
405                                 ehci->reset_done [wIndex] = jiffies
406                                                 + msecs_to_jiffies (20);
407                         }
408                         break;
409                 case USB_PORT_FEAT_C_SUSPEND:
410                         /* we auto-clear this feature */
411                         break;
412                 case USB_PORT_FEAT_POWER:
413                         if (HCS_PPC (ehci->hcs_params))
414                                 writel (temp & ~(PORT_RWC_BITS | PORT_POWER),
415                                         &ehci->regs->port_status [wIndex]);
416                         break;
417                 case USB_PORT_FEAT_C_CONNECTION:
418                         writel((temp & ~PORT_RWC_BITS) | PORT_CSC,
419                                 &ehci->regs->port_status [wIndex]);
420                         break;
421                 case USB_PORT_FEAT_C_OVER_CURRENT:
422                         writel((temp & ~PORT_RWC_BITS) | PORT_OCC,
423                                 &ehci->regs->port_status [wIndex]);
424                         break;
425                 case USB_PORT_FEAT_C_RESET:
426                         /* GetPortStatus clears reset */
427                         break;
428                 default:
429                         goto error;
430                 }
431                 readl (&ehci->regs->command);   /* unblock posted write */
432                 break;
433         case GetHubDescriptor:
434                 ehci_hub_descriptor (ehci, (struct usb_hub_descriptor *)
435                         buf);
436                 break;
437         case GetHubStatus:
438                 /* no hub-wide feature/status flags */
439                 memset (buf, 0, 4);
440                 //cpu_to_le32s ((u32 *) buf);
441                 break;
442         case GetPortStatus:
443                 if (!wIndex || wIndex > ports)
444                         goto error;
445                 wIndex--;
446                 status = 0;
447                 temp = readl (&ehci->regs->port_status [wIndex]);
448
449                 // wPortChange bits
450                 if (temp & PORT_CSC)
451                         status |= 1 << USB_PORT_FEAT_C_CONNECTION;
452                 if (temp & PORT_PEC)
453                         status |= 1 << USB_PORT_FEAT_C_ENABLE;
454                 if ((temp & PORT_OCC) && !ignore_oc)
455                         status |= 1 << USB_PORT_FEAT_C_OVER_CURRENT;
456
457                 /* whoever resumes must GetPortStatus to complete it!! */
458                 if ((temp & PORT_RESUME)
459                                 && time_after (jiffies,
460                                         ehci->reset_done [wIndex])) {
461                         status |= 1 << USB_PORT_FEAT_C_SUSPEND;
462                         ehci->reset_done [wIndex] = 0;
463
464                         /* stop resume signaling */
465                         temp = readl (&ehci->regs->port_status [wIndex]);
466                         writel (temp & ~(PORT_RWC_BITS | PORT_RESUME),
467                                 &ehci->regs->port_status [wIndex]);
468                         retval = handshake (
469                                         &ehci->regs->port_status [wIndex],
470                                         PORT_RESUME, 0, 2000 /* 2msec */);
471                         if (retval != 0) {
472                                 ehci_err (ehci, "port %d resume error %d\n",
473                                         wIndex + 1, retval);
474                                 goto error;
475                         }
476                         temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
477                 }
478
479                 /* whoever resets must GetPortStatus to complete it!! */
480                 if ((temp & PORT_RESET)
481                                 && time_after (jiffies,
482                                         ehci->reset_done [wIndex])) {
483                         status |= 1 << USB_PORT_FEAT_C_RESET;
484                         ehci->reset_done [wIndex] = 0;
485
486                         /* force reset to complete */
487                         writel (temp & ~(PORT_RWC_BITS | PORT_RESET),
488                                         &ehci->regs->port_status [wIndex]);
489                         /* REVISIT:  some hardware needs 550+ usec to clear
490                          * this bit; seems too long to spin routinely...
491                          */
492                         retval = handshake (
493                                         &ehci->regs->port_status [wIndex],
494                                         PORT_RESET, 0, 750);
495                         if (retval != 0) {
496                                 ehci_err (ehci, "port %d reset error %d\n",
497                                         wIndex + 1, retval);
498                                 goto error;
499                         }
500
501                         /* see what we found out */
502                         temp = check_reset_complete (ehci, wIndex,
503                                 readl (&ehci->regs->port_status [wIndex]));
504                 }
505
506                 // don't show wPortStatus if it's owned by a companion hc
507                 if (!(temp & PORT_OWNER)) {
508                         if (temp & PORT_CONNECT) {
509                                 status |= 1 << USB_PORT_FEAT_CONNECTION;
510                                 // status may be from integrated TT
511                                 status |= ehci_port_speed(ehci, temp);
512                         }
513                         if (temp & PORT_PE)
514                                 status |= 1 << USB_PORT_FEAT_ENABLE;
515                         if (temp & (PORT_SUSPEND|PORT_RESUME))
516                                 status |= 1 << USB_PORT_FEAT_SUSPEND;
517                         if (temp & PORT_OC)
518                                 status |= 1 << USB_PORT_FEAT_OVER_CURRENT;
519                         if (temp & PORT_RESET)
520                                 status |= 1 << USB_PORT_FEAT_RESET;
521                         if (temp & PORT_POWER)
522                                 status |= 1 << USB_PORT_FEAT_POWER;
523                 }
524
525 #ifndef EHCI_VERBOSE_DEBUG
526         if (status & ~0xffff)   /* only if wPortChange is interesting */
527 #endif
528                 dbg_port (ehci, "GetStatus", wIndex + 1, temp);
529                 // we "know" this alignment is good, caller used kmalloc()...
530                 *((__le32 *) buf) = cpu_to_le32 (status);
531                 break;
532         case SetHubFeature:
533                 switch (wValue) {
534                 case C_HUB_LOCAL_POWER:
535                 case C_HUB_OVER_CURRENT:
536                         /* no hub-wide feature/status flags */
537                         break;
538                 default:
539                         goto error;
540                 }
541                 break;
542         case SetPortFeature:
543                 selector = wIndex >> 8;
544                 wIndex &= 0xff;
545                 if (!wIndex || wIndex > ports)
546                         goto error;
547                 wIndex--;
548                 temp = readl (&ehci->regs->port_status [wIndex]);
549                 if (temp & PORT_OWNER)
550                         break;
551
552                 temp &= ~PORT_RWC_BITS;
553                 switch (wValue) {
554                 case USB_PORT_FEAT_SUSPEND:
555                         if (ehci->no_selective_suspend)
556                                 break;
557                         if ((temp & PORT_PE) == 0
558                                         || (temp & PORT_RESET) != 0)
559                                 goto error;
560                         if (device_may_wakeup(&hcd->self.root_hub->dev))
561                                 temp |= PORT_WAKE_BITS;
562                         writel (temp | PORT_SUSPEND,
563                                 &ehci->regs->port_status [wIndex]);
564                         break;
565                 case USB_PORT_FEAT_POWER:
566                         if (HCS_PPC (ehci->hcs_params))
567                                 writel (temp | PORT_POWER,
568                                         &ehci->regs->port_status [wIndex]);
569                         break;
570                 case USB_PORT_FEAT_RESET:
571                         if (temp & PORT_RESUME)
572                                 goto error;
573                         /* line status bits may report this as low speed,
574                          * which can be fine if this root hub has a
575                          * transaction translator built in.
576                          */
577                         if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
578                                         && !ehci_is_TDI(ehci)
579                                         && PORT_USB11 (temp)) {
580                                 ehci_dbg (ehci,
581                                         "port %d low speed --> companion\n",
582                                         wIndex + 1);
583                                 temp |= PORT_OWNER;
584                         } else {
585                                 ehci_vdbg (ehci, "port %d reset\n", wIndex + 1);
586                                 temp |= PORT_RESET;
587                                 temp &= ~PORT_PE;
588
589                                 /*
590                                  * caller must wait, then call GetPortStatus
591                                  * usb 2.0 spec says 50 ms resets on root
592                                  */
593                                 ehci->reset_done [wIndex] = jiffies
594                                                 + msecs_to_jiffies (50);
595                         }
596                         writel (temp, &ehci->regs->port_status [wIndex]);
597                         break;
598
599                 /* For downstream facing ports (these):  one hub port is put
600                  * into test mode according to USB2 11.24.2.13, then the hub
601                  * must be reset (which for root hub now means rmmod+modprobe,
602                  * or else system reboot).  See EHCI 2.3.9 and 4.14 for info
603                  * about the EHCI-specific stuff.
604                  */
605                 case USB_PORT_FEAT_TEST:
606                         if (!selector || selector > 5)
607                                 goto error;
608                         ehci_quiesce(ehci);
609                         ehci_halt(ehci);
610                         temp |= selector << 16;
611                         writel (temp, &ehci->regs->port_status [wIndex]);
612                         break;
613
614                 default:
615                         goto error;
616                 }
617                 readl (&ehci->regs->command);   /* unblock posted writes */
618                 break;
619
620         default:
621 error:
622                 /* "stall" on error */
623                 retval = -EPIPE;
624         }
625         spin_unlock_irqrestore (&ehci->lock, flags);
626         return retval;
627 }