Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
f5946f82 AS |
36 | /* A port that either is connected or has a changed-bit set will prevent |
37 | * us from AUTO_STOPPING. | |
38 | */ | |
39 | static int any_ports_active(struct uhci_hcd *uhci) | |
40 | { | |
41 | int port; | |
42 | ||
43 | for (port = 0; port < uhci->rh_numports; ++port) { | |
44 | if ((inw(uhci->io_addr + USBPORTSC1 + port * 2) & | |
45 | (USBPORTSC_CCS | RWC_BITS)) || | |
46 | test_bit(port, &uhci->port_c_suspend)) | |
47 | return 1; | |
48 | } | |
49 | return 0; | |
50 | } | |
51 | ||
6c1b445c | 52 | static inline int get_hub_status_data(struct uhci_hcd *uhci, char *buf) |
1da177e4 | 53 | { |
1da177e4 LT |
54 | int port; |
55 | ||
56 | *buf = 0; | |
57 | for (port = 0; port < uhci->rh_numports; ++port) { | |
58 | if ((inw(uhci->io_addr + USBPORTSC1 + port * 2) & RWC_BITS) || | |
59 | test_bit(port, &uhci->port_c_suspend)) | |
60 | *buf |= (1 << (port + 1)); | |
61 | } | |
1da177e4 LT |
62 | return !!*buf; |
63 | } | |
64 | ||
65 | #define OK(x) len = (x); break | |
66 | ||
67 | #define CLR_RH_PORTSTAT(x) \ | |
68 | status = inw(port_addr); \ | |
69 | status &= ~(RWC_BITS|WZ_BITS); \ | |
70 | status &= ~(x); \ | |
71 | status |= RWC_BITS & (x); \ | |
72 | outw(status, port_addr) | |
73 | ||
74 | #define SET_RH_PORTSTAT(x) \ | |
75 | status = inw(port_addr); \ | |
76 | status |= (x); \ | |
77 | status &= ~(RWC_BITS|WZ_BITS); \ | |
78 | outw(status, port_addr) | |
79 | ||
80 | /* UHCI controllers don't automatically stop resume signalling after 20 msec, | |
81 | * so we have to poll and check timeouts in order to take care of it. | |
82 | */ | |
83 | static void uhci_finish_suspend(struct uhci_hcd *uhci, int port, | |
84 | unsigned long port_addr) | |
85 | { | |
86 | int status; | |
87 | ||
88 | if (test_bit(port, &uhci->suspended_ports)) { | |
89 | CLR_RH_PORTSTAT(USBPORTSC_SUSP | USBPORTSC_RD); | |
90 | clear_bit(port, &uhci->suspended_ports); | |
91 | clear_bit(port, &uhci->resuming_ports); | |
92 | set_bit(port, &uhci->port_c_suspend); | |
93 | ||
94 | /* The controller won't actually turn off the RD bit until | |
95 | * it has had a chance to send a low-speed EOP sequence, | |
96 | * which takes 3 bit times (= 2 microseconds). We'll delay | |
97 | * slightly longer for good luck. */ | |
98 | udelay(4); | |
99 | } | |
100 | } | |
101 | ||
ae557175 AS |
102 | /* Wait for the UHCI controller in HP's iLO2 server management chip. |
103 | * It can take up to 250 us to finish a reset and set the CSC bit. | |
104 | */ | |
105 | static void wait_for_HP(unsigned long port_addr) | |
106 | { | |
107 | int i; | |
108 | ||
109 | for (i = 10; i < 250; i += 10) { | |
110 | if (inw(port_addr) & USBPORTSC_CSC) | |
111 | return; | |
112 | udelay(10); | |
113 | } | |
114 | /* Log a warning? */ | |
115 | } | |
116 | ||
1da177e4 LT |
117 | static void uhci_check_ports(struct uhci_hcd *uhci) |
118 | { | |
119 | unsigned int port; | |
120 | unsigned long port_addr; | |
121 | int status; | |
122 | ||
123 | for (port = 0; port < uhci->rh_numports; ++port) { | |
124 | port_addr = uhci->io_addr + USBPORTSC1 + 2 * port; | |
125 | status = inw(port_addr); | |
126 | if (unlikely(status & USBPORTSC_PR)) { | |
127 | if (time_after_eq(jiffies, uhci->ports_timeout)) { | |
128 | CLR_RH_PORTSTAT(USBPORTSC_PR); | |
129 | udelay(10); | |
130 | ||
ae557175 AS |
131 | /* HP's server management chip requires |
132 | * a longer delay. */ | |
133 | if (to_pci_dev(uhci_dev(uhci))->vendor == | |
134 | PCI_VENDOR_ID_HP) | |
135 | wait_for_HP(port_addr); | |
136 | ||
1da177e4 LT |
137 | /* If the port was enabled before, turning |
138 | * reset on caused a port enable change. | |
139 | * Turning reset off causes a port connect | |
140 | * status change. Clear these changes. */ | |
141 | CLR_RH_PORTSTAT(USBPORTSC_CSC | USBPORTSC_PEC); | |
142 | SET_RH_PORTSTAT(USBPORTSC_PE); | |
143 | } | |
144 | } | |
145 | if (unlikely(status & USBPORTSC_RD)) { | |
146 | if (!test_bit(port, &uhci->resuming_ports)) { | |
147 | ||
148 | /* Port received a wakeup request */ | |
149 | set_bit(port, &uhci->resuming_ports); | |
150 | uhci->ports_timeout = jiffies + | |
151 | msecs_to_jiffies(20); | |
6c1b445c AS |
152 | |
153 | /* Make sure we see the port again | |
154 | * after the resuming period is over. */ | |
155 | mod_timer(&uhci_to_hcd(uhci)->rh_timer, | |
156 | uhci->ports_timeout); | |
1da177e4 LT |
157 | } else if (time_after_eq(jiffies, |
158 | uhci->ports_timeout)) { | |
159 | uhci_finish_suspend(uhci, port, port_addr); | |
160 | } | |
161 | } | |
162 | } | |
163 | } | |
164 | ||
6c1b445c AS |
165 | static int uhci_hub_status_data(struct usb_hcd *hcd, char *buf) |
166 | { | |
167 | struct uhci_hcd *uhci = hcd_to_uhci(hcd); | |
168 | unsigned long flags; | |
1f09df8b | 169 | int status = 0; |
6c1b445c AS |
170 | |
171 | spin_lock_irqsave(&uhci->lock, flags); | |
6c1b445c | 172 | |
1f09df8b AS |
173 | uhci_scan_schedule(uhci, NULL); |
174 | if (uhci->hc_inaccessible) | |
175 | goto done; | |
176 | check_fsbr(uhci); | |
6c1b445c | 177 | uhci_check_ports(uhci); |
1f09df8b | 178 | |
6c1b445c AS |
179 | status = get_hub_status_data(uhci, buf); |
180 | ||
181 | switch (uhci->rh_state) { | |
182 | case UHCI_RH_SUSPENDING: | |
183 | case UHCI_RH_SUSPENDED: | |
184 | /* if port change, ask to be resumed */ | |
185 | if (status) | |
186 | usb_hcd_resume_root_hub(hcd); | |
187 | break; | |
188 | ||
189 | case UHCI_RH_AUTO_STOPPED: | |
190 | /* if port change, auto start */ | |
191 | if (status) | |
192 | wakeup_rh(uhci); | |
193 | break; | |
194 | ||
195 | case UHCI_RH_RUNNING: | |
196 | /* are any devices attached? */ | |
197 | if (!any_ports_active(uhci)) { | |
198 | uhci->rh_state = UHCI_RH_RUNNING_NODEVS; | |
199 | uhci->auto_stop_time = jiffies + HZ; | |
200 | } | |
201 | break; | |
202 | ||
203 | case UHCI_RH_RUNNING_NODEVS: | |
204 | /* auto-stop if nothing connected for 1 second */ | |
205 | if (any_ports_active(uhci)) | |
206 | uhci->rh_state = UHCI_RH_RUNNING; | |
207 | else if (time_after_eq(jiffies, uhci->auto_stop_time)) | |
208 | suspend_rh(uhci, UHCI_RH_AUTO_STOPPED); | |
209 | break; | |
210 | ||
211 | default: | |
212 | break; | |
213 | } | |
214 | ||
215 | done: | |
216 | spin_unlock_irqrestore(&uhci->lock, flags); | |
217 | return status; | |
218 | } | |
219 | ||
1da177e4 LT |
220 | /* size of returned buffer is part of USB spec */ |
221 | static int uhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, | |
222 | u16 wIndex, char *buf, u16 wLength) | |
223 | { | |
224 | struct uhci_hcd *uhci = hcd_to_uhci(hcd); | |
225 | int status, lstatus, retval = 0, len = 0; | |
226 | unsigned int port = wIndex - 1; | |
227 | unsigned long port_addr = uhci->io_addr + USBPORTSC1 + 2 * port; | |
228 | u16 wPortChange, wPortStatus; | |
229 | unsigned long flags; | |
230 | ||
a8bed8b6 AS |
231 | if (uhci->hc_inaccessible) |
232 | return -ETIMEDOUT; | |
233 | ||
1da177e4 LT |
234 | spin_lock_irqsave(&uhci->lock, flags); |
235 | switch (typeReq) { | |
236 | ||
237 | case GetHubStatus: | |
238 | *(__le32 *)buf = cpu_to_le32(0); | |
239 | OK(4); /* hub power */ | |
240 | case GetPortStatus: | |
241 | if (port >= uhci->rh_numports) | |
242 | goto err; | |
243 | ||
244 | uhci_check_ports(uhci); | |
245 | status = inw(port_addr); | |
246 | ||
247 | /* Intel controllers report the OverCurrent bit active on. | |
248 | * VIA controllers report it active off, so we'll adjust the | |
249 | * bit value. (It's not standardized in the UHCI spec.) | |
250 | */ | |
251 | if (to_pci_dev(hcd->self.controller)->vendor == | |
252 | PCI_VENDOR_ID_VIA) | |
253 | status ^= USBPORTSC_OC; | |
254 | ||
255 | /* UHCI doesn't support C_RESET (always false) */ | |
256 | wPortChange = lstatus = 0; | |
257 | if (status & USBPORTSC_CSC) | |
258 | wPortChange |= USB_PORT_STAT_C_CONNECTION; | |
259 | if (status & USBPORTSC_PEC) | |
260 | wPortChange |= USB_PORT_STAT_C_ENABLE; | |
261 | if (status & USBPORTSC_OCC) | |
262 | wPortChange |= USB_PORT_STAT_C_OVERCURRENT; | |
263 | ||
264 | if (test_bit(port, &uhci->port_c_suspend)) { | |
265 | wPortChange |= USB_PORT_STAT_C_SUSPEND; | |
266 | lstatus |= 1; | |
267 | } | |
268 | if (test_bit(port, &uhci->suspended_ports)) | |
269 | lstatus |= 2; | |
270 | if (test_bit(port, &uhci->resuming_ports)) | |
271 | lstatus |= 4; | |
272 | ||
273 | /* UHCI has no power switching (always on) */ | |
274 | wPortStatus = USB_PORT_STAT_POWER; | |
275 | if (status & USBPORTSC_CCS) | |
276 | wPortStatus |= USB_PORT_STAT_CONNECTION; | |
277 | if (status & USBPORTSC_PE) { | |
278 | wPortStatus |= USB_PORT_STAT_ENABLE; | |
279 | if (status & (USBPORTSC_SUSP | USBPORTSC_RD)) | |
280 | wPortStatus |= USB_PORT_STAT_SUSPEND; | |
281 | } | |
282 | if (status & USBPORTSC_OC) | |
283 | wPortStatus |= USB_PORT_STAT_OVERCURRENT; | |
284 | if (status & USBPORTSC_PR) | |
285 | wPortStatus |= USB_PORT_STAT_RESET; | |
286 | if (status & USBPORTSC_LSDA) | |
287 | wPortStatus |= USB_PORT_STAT_LOW_SPEED; | |
288 | ||
289 | if (wPortChange) | |
290 | dev_dbg(uhci_dev(uhci), "port %d portsc %04x,%02x\n", | |
291 | wIndex, status, lstatus); | |
292 | ||
293 | *(__le16 *)buf = cpu_to_le16(wPortStatus); | |
294 | *(__le16 *)(buf + 2) = cpu_to_le16(wPortChange); | |
295 | OK(4); | |
296 | case SetHubFeature: /* We don't implement these */ | |
297 | case ClearHubFeature: | |
298 | switch (wValue) { | |
299 | case C_HUB_OVER_CURRENT: | |
300 | case C_HUB_LOCAL_POWER: | |
301 | OK(0); | |
302 | default: | |
303 | goto err; | |
304 | } | |
305 | break; | |
306 | case SetPortFeature: | |
307 | if (port >= uhci->rh_numports) | |
308 | goto err; | |
309 | ||
310 | switch (wValue) { | |
311 | case USB_PORT_FEAT_SUSPEND: | |
312 | set_bit(port, &uhci->suspended_ports); | |
313 | SET_RH_PORTSTAT(USBPORTSC_SUSP); | |
314 | OK(0); | |
315 | case USB_PORT_FEAT_RESET: | |
316 | SET_RH_PORTSTAT(USBPORTSC_PR); | |
317 | ||
318 | /* Reset terminates Resume signalling */ | |
319 | uhci_finish_suspend(uhci, port, port_addr); | |
320 | ||
321 | /* USB v2.0 7.1.7.5 */ | |
322 | uhci->ports_timeout = jiffies + msecs_to_jiffies(50); | |
323 | OK(0); | |
324 | case USB_PORT_FEAT_POWER: | |
325 | /* UHCI has no power switching */ | |
326 | OK(0); | |
327 | default: | |
328 | goto err; | |
329 | } | |
330 | break; | |
331 | case ClearPortFeature: | |
332 | if (port >= uhci->rh_numports) | |
333 | goto err; | |
334 | ||
335 | switch (wValue) { | |
336 | case USB_PORT_FEAT_ENABLE: | |
337 | CLR_RH_PORTSTAT(USBPORTSC_PE); | |
338 | ||
339 | /* Disable terminates Resume signalling */ | |
340 | uhci_finish_suspend(uhci, port, port_addr); | |
341 | OK(0); | |
342 | case USB_PORT_FEAT_C_ENABLE: | |
343 | CLR_RH_PORTSTAT(USBPORTSC_PEC); | |
344 | OK(0); | |
345 | case USB_PORT_FEAT_SUSPEND: | |
346 | if (test_bit(port, &uhci->suspended_ports) && | |
347 | !test_and_set_bit(port, | |
348 | &uhci->resuming_ports)) { | |
349 | SET_RH_PORTSTAT(USBPORTSC_RD); | |
350 | ||
351 | /* The controller won't allow RD to be set | |
352 | * if the port is disabled. When this happens | |
353 | * just skip the Resume signalling. | |
354 | */ | |
355 | if (!(inw(port_addr) & USBPORTSC_RD)) | |
356 | uhci_finish_suspend(uhci, port, | |
357 | port_addr); | |
358 | else | |
359 | /* USB v2.0 7.1.7.7 */ | |
360 | uhci->ports_timeout = jiffies + | |
361 | msecs_to_jiffies(20); | |
362 | } | |
363 | OK(0); | |
364 | case USB_PORT_FEAT_C_SUSPEND: | |
365 | clear_bit(port, &uhci->port_c_suspend); | |
366 | OK(0); | |
367 | case USB_PORT_FEAT_POWER: | |
368 | /* UHCI has no power switching */ | |
369 | goto err; | |
370 | case USB_PORT_FEAT_C_CONNECTION: | |
371 | CLR_RH_PORTSTAT(USBPORTSC_CSC); | |
372 | OK(0); | |
373 | case USB_PORT_FEAT_C_OVER_CURRENT: | |
374 | CLR_RH_PORTSTAT(USBPORTSC_OCC); | |
375 | OK(0); | |
376 | case USB_PORT_FEAT_C_RESET: | |
377 | /* this driver won't report these */ | |
378 | OK(0); | |
379 | default: | |
380 | goto err; | |
381 | } | |
382 | break; | |
383 | case GetHubDescriptor: | |
384 | len = min_t(unsigned int, sizeof(root_hub_hub_des), wLength); | |
385 | memcpy(buf, root_hub_hub_des, len); | |
386 | if (len > 2) | |
387 | buf[2] = uhci->rh_numports; | |
388 | OK(len); | |
389 | default: | |
390 | err: | |
391 | retval = -EPIPE; | |
392 | } | |
393 | spin_unlock_irqrestore(&uhci->lock, flags); | |
394 | ||
395 | return retval; | |
396 | } |