Backported fix from 2.6.17-rc1: Wait for the UHCI controller in HP's iLO2 server...
[linux-2.6.git] / drivers / usb / host / uhci-hub.c
1 /*
2  * Universal Host Controller Interface driver for USB.
3  *
4  * Maintainer: Alan Stern <stern@rowland.harvard.edu>
5  *
6  * (C) Copyright 1999 Linus Torvalds
7  * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
8  * (C) Copyright 1999 Randy Dunlap
9  * (C) Copyright 1999 Georg Acher, acher@in.tum.de
10  * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
11  * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
12  * (C) Copyright 2004 Alan Stern, stern@rowland.harvard.edu
13  */
14
15 static __u8 root_hub_hub_des[] =
16 {
17         0x09,                   /*  __u8  bLength; */
18         0x29,                   /*  __u8  bDescriptorType; Hub-descriptor */
19         0x02,                   /*  __u8  bNbrPorts; */
20         0x0a,                   /* __u16  wHubCharacteristics; */
21         0x00,                   /*   (per-port OC, no power switching) */
22         0x01,                   /*  __u8  bPwrOn2pwrGood; 2ms */
23         0x00,                   /*  __u8  bHubContrCurrent; 0 mA */
24         0x00,                   /*  __u8  DeviceRemovable; *** 7 Ports max *** */
25         0xff                    /*  __u8  PortPwrCtrlMask; *** 7 ports max *** */
26 };
27
28 #define UHCI_RH_MAXCHILD        7
29
30 /* must write as zeroes */
31 #define WZ_BITS         (USBPORTSC_RES2 | USBPORTSC_RES3 | USBPORTSC_RES4)
32
33 /* status change bits:  nonzero writes will clear */
34 #define RWC_BITS        (USBPORTSC_OCC | USBPORTSC_PEC | USBPORTSC_CSC)
35
36 static int uhci_hub_status_data(struct usb_hcd *hcd, char *buf)
37 {
38         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
39         int port;
40
41         *buf = 0;
42         for (port = 0; port < uhci->rh_numports; ++port) {
43                 if ((inw(uhci->io_addr + USBPORTSC1 + port * 2) & RWC_BITS) ||
44                                 test_bit(port, &uhci->port_c_suspend))
45                         *buf |= (1 << (port + 1));
46         }
47         if (*buf && uhci->state == UHCI_SUSPENDED)
48                 uhci->resume_detect = 1;
49         return !!*buf;
50 }
51
52 #define OK(x)                   len = (x); break
53
54 #define CLR_RH_PORTSTAT(x) \
55         status = inw(port_addr); \
56         status &= ~(RWC_BITS|WZ_BITS); \
57         status &= ~(x); \
58         status |= RWC_BITS & (x); \
59         outw(status, port_addr)
60
61 #define SET_RH_PORTSTAT(x) \
62         status = inw(port_addr); \
63         status |= (x); \
64         status &= ~(RWC_BITS|WZ_BITS); \
65         outw(status, port_addr)
66
67 /* UHCI controllers don't automatically stop resume signalling after 20 msec,
68  * so we have to poll and check timeouts in order to take care of it.
69  */
70 static void uhci_finish_suspend(struct uhci_hcd *uhci, int port,
71                 unsigned long port_addr)
72 {
73         int status;
74
75         if (test_bit(port, &uhci->suspended_ports)) {
76                 CLR_RH_PORTSTAT(USBPORTSC_SUSP | USBPORTSC_RD);
77                 clear_bit(port, &uhci->suspended_ports);
78                 clear_bit(port, &uhci->resuming_ports);
79                 set_bit(port, &uhci->port_c_suspend);
80
81                 /* The controller won't actually turn off the RD bit until
82                  * it has had a chance to send a low-speed EOP sequence,
83                  * which takes 3 bit times (= 2 microseconds).  We'll delay
84                  * slightly longer for good luck. */
85                 udelay(4);
86         }
87 }
88
89 /* Wait for the UHCI controller in HP's iLO2 server management chip.
90  * It can take up to 250 us to finish a reset and set the CSC bit.
91  */
92 static void wait_for_HP(unsigned long port_addr)
93 {
94         int i;
95
96         for (i = 10; i < 250; i += 10) {
97                 if (inw(port_addr) & USBPORTSC_CSC)
98                         return;
99         udelay(10);
100         }
101         /* Log a warning? */
102 }
103
104 static void uhci_check_ports(struct uhci_hcd *uhci)
105 {
106         unsigned int port;
107         unsigned long port_addr;
108         int status;
109
110         for (port = 0; port < uhci->rh_numports; ++port) {
111                 port_addr = uhci->io_addr + USBPORTSC1 + 2 * port;
112                 status = inw(port_addr);
113                 if (unlikely(status & USBPORTSC_PR)) {
114                         if (time_after_eq(jiffies, uhci->ports_timeout)) {
115                                 CLR_RH_PORTSTAT(USBPORTSC_PR);
116                                 udelay(10);
117                                 /* HP's server management chip requires
118                                  * a longer delay. */
119                                 if (to_pci_dev(uhci_dev(uhci))->vendor ==
120                                                 PCI_VENDOR_ID_HP)
121                                         wait_for_HP(port_addr);
122
123                                 /* If the port was enabled before, turning
124                                  * reset on caused a port enable change.
125                                  * Turning reset off causes a port connect
126                                  * status change.  Clear these changes. */
127                                 CLR_RH_PORTSTAT(USBPORTSC_CSC | USBPORTSC_PEC);
128                                 SET_RH_PORTSTAT(USBPORTSC_PE);
129                         }
130                 }
131                 if (unlikely(status & USBPORTSC_RD)) {
132                         if (!test_bit(port, &uhci->resuming_ports)) {
133
134                                 /* Port received a wakeup request */
135                                 set_bit(port, &uhci->resuming_ports);
136                                 uhci->ports_timeout = jiffies +
137                                                 msecs_to_jiffies(20);
138                         } else if (time_after_eq(jiffies,
139                                                 uhci->ports_timeout)) {
140                                 uhci_finish_suspend(uhci, port, port_addr);
141                         }
142                 }
143         }
144 }
145
146 /* size of returned buffer is part of USB spec */
147 static int uhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
148                         u16 wIndex, char *buf, u16 wLength)
149 {
150         struct uhci_hcd *uhci = hcd_to_uhci(hcd);
151         int status, lstatus, retval = 0, len = 0;
152         unsigned int port = wIndex - 1;
153         unsigned long port_addr = uhci->io_addr + USBPORTSC1 + 2 * port;
154         u16 wPortChange, wPortStatus;
155         unsigned long flags;
156
157         spin_lock_irqsave(&uhci->lock, flags);
158         switch (typeReq) {
159
160         case GetHubStatus:
161                 *(__le32 *)buf = cpu_to_le32(0);
162                 OK(4);          /* hub power */
163         case GetPortStatus:
164                 if (port >= uhci->rh_numports)
165                         goto err;
166
167                 uhci_check_ports(uhci);
168                 status = inw(port_addr);
169
170                 /* Intel controllers report the OverCurrent bit active on.
171                  * VIA controllers report it active off, so we'll adjust the
172                  * bit value.  (It's not standardized in the UHCI spec.)
173                  */
174                 if (to_pci_dev(hcd->self.controller)->vendor ==
175                                 PCI_VENDOR_ID_VIA)
176                         status ^= USBPORTSC_OC;
177
178                 /* UHCI doesn't support C_RESET (always false) */
179                 wPortChange = lstatus = 0;
180                 if (status & USBPORTSC_CSC)
181                         wPortChange |= USB_PORT_STAT_C_CONNECTION;
182                 if (status & USBPORTSC_PEC)
183                         wPortChange |= USB_PORT_STAT_C_ENABLE;
184                 if (status & USBPORTSC_OCC)
185                         wPortChange |= USB_PORT_STAT_C_OVERCURRENT;
186
187                 if (test_bit(port, &uhci->port_c_suspend)) {
188                         wPortChange |= USB_PORT_STAT_C_SUSPEND;
189                         lstatus |= 1;
190                 }
191                 if (test_bit(port, &uhci->suspended_ports))
192                         lstatus |= 2;
193                 if (test_bit(port, &uhci->resuming_ports))
194                         lstatus |= 4;
195
196                 /* UHCI has no power switching (always on) */
197                 wPortStatus = USB_PORT_STAT_POWER;
198                 if (status & USBPORTSC_CCS)
199                         wPortStatus |= USB_PORT_STAT_CONNECTION;
200                 if (status & USBPORTSC_PE) {
201                         wPortStatus |= USB_PORT_STAT_ENABLE;
202                         if (status & (USBPORTSC_SUSP | USBPORTSC_RD))
203                                 wPortStatus |= USB_PORT_STAT_SUSPEND;
204                 }
205                 if (status & USBPORTSC_OC)
206                         wPortStatus |= USB_PORT_STAT_OVERCURRENT;
207                 if (status & USBPORTSC_PR)
208                         wPortStatus |= USB_PORT_STAT_RESET;
209                 if (status & USBPORTSC_LSDA)
210                         wPortStatus |= USB_PORT_STAT_LOW_SPEED;
211
212                 if (wPortChange)
213                         dev_dbg(uhci_dev(uhci), "port %d portsc %04x,%02x\n",
214                                         wIndex, status, lstatus);
215
216                 *(__le16 *)buf = cpu_to_le16(wPortStatus);
217                 *(__le16 *)(buf + 2) = cpu_to_le16(wPortChange);
218                 OK(4);
219         case SetHubFeature:             /* We don't implement these */
220         case ClearHubFeature:
221                 switch (wValue) {
222                 case C_HUB_OVER_CURRENT:
223                 case C_HUB_LOCAL_POWER:
224                         OK(0);
225                 default:
226                         goto err;
227                 }
228                 break;
229         case SetPortFeature:
230                 if (port >= uhci->rh_numports)
231                         goto err;
232
233                 switch (wValue) {
234                 case USB_PORT_FEAT_SUSPEND:
235                         set_bit(port, &uhci->suspended_ports);
236                         SET_RH_PORTSTAT(USBPORTSC_SUSP);
237                         OK(0);
238                 case USB_PORT_FEAT_RESET:
239                         SET_RH_PORTSTAT(USBPORTSC_PR);
240
241                         /* Reset terminates Resume signalling */
242                         uhci_finish_suspend(uhci, port, port_addr);
243
244                         /* USB v2.0 7.1.7.5 */
245                         uhci->ports_timeout = jiffies + msecs_to_jiffies(50);
246                         OK(0);
247                 case USB_PORT_FEAT_POWER:
248                         /* UHCI has no power switching */
249                         OK(0);
250                 default:
251                         goto err;
252                 }
253                 break;
254         case ClearPortFeature:
255                 if (port >= uhci->rh_numports)
256                         goto err;
257
258                 switch (wValue) {
259                 case USB_PORT_FEAT_ENABLE:
260                         CLR_RH_PORTSTAT(USBPORTSC_PE);
261
262                         /* Disable terminates Resume signalling */
263                         uhci_finish_suspend(uhci, port, port_addr);
264                         OK(0);
265                 case USB_PORT_FEAT_C_ENABLE:
266                         CLR_RH_PORTSTAT(USBPORTSC_PEC);
267                         OK(0);
268                 case USB_PORT_FEAT_SUSPEND:
269                         if (test_bit(port, &uhci->suspended_ports) &&
270                                         !test_and_set_bit(port,
271                                                 &uhci->resuming_ports)) {
272                                 SET_RH_PORTSTAT(USBPORTSC_RD);
273
274                                 /* The controller won't allow RD to be set
275                                  * if the port is disabled.  When this happens
276                                  * just skip the Resume signalling.
277                                  */
278                                 if (!(inw(port_addr) & USBPORTSC_RD))
279                                         uhci_finish_suspend(uhci, port,
280                                                         port_addr);
281                                 else
282                                         /* USB v2.0 7.1.7.7 */
283                                         uhci->ports_timeout = jiffies +
284                                                 msecs_to_jiffies(20);
285                         }
286                         OK(0);
287                 case USB_PORT_FEAT_C_SUSPEND:
288                         clear_bit(port, &uhci->port_c_suspend);
289                         OK(0);
290                 case USB_PORT_FEAT_POWER:
291                         /* UHCI has no power switching */
292                         goto err;
293                 case USB_PORT_FEAT_C_CONNECTION:
294                         CLR_RH_PORTSTAT(USBPORTSC_CSC);
295                         OK(0);
296                 case USB_PORT_FEAT_C_OVER_CURRENT:
297                         CLR_RH_PORTSTAT(USBPORTSC_OCC);
298                         OK(0);
299                 case USB_PORT_FEAT_C_RESET:
300                         /* this driver won't report these */
301                         OK(0);
302                 default:
303                         goto err;
304                 }
305                 break;
306         case GetHubDescriptor:
307                 len = min_t(unsigned int, sizeof(root_hub_hub_des), wLength);
308                 memcpy(buf, root_hub_hub_des, len);
309                 if (len > 2)
310                         buf[2] = uhci->rh_numports;
311                 OK(len);
312         default:
313 err:
314                 retval = -EPIPE;
315         }
316         spin_unlock_irqrestore(&uhci->lock, flags);
317
318         return retval;
319 }