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