Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * (C) Copyright Linus Torvalds 1999 | |
3 | * (C) Copyright Johannes Erdfelt 1999-2001 | |
4 | * (C) Copyright Andreas Gal 1999 | |
5 | * (C) Copyright Gregory P. Smith 1999 | |
6 | * (C) Copyright Deti Fliegl 1999 | |
7 | * (C) Copyright Randy Dunlap 2000 | |
8 | * (C) Copyright David Brownell 2000-2002 | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify it | |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation; either version 2 of the License, or (at your | |
13 | * option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, but | |
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
17 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 | * for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software Foundation, | |
22 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | */ | |
24 | ||
1da177e4 LT |
25 | #include <linux/module.h> |
26 | #include <linux/version.h> | |
27 | #include <linux/kernel.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/completion.h> | |
30 | #include <linux/utsname.h> | |
31 | #include <linux/mm.h> | |
32 | #include <asm/io.h> | |
1da177e4 LT |
33 | #include <linux/device.h> |
34 | #include <linux/dma-mapping.h> | |
4186ecf8 | 35 | #include <linux/mutex.h> |
1da177e4 LT |
36 | #include <asm/irq.h> |
37 | #include <asm/byteorder.h> | |
b3476675 | 38 | #include <asm/unaligned.h> |
64a21d02 | 39 | #include <linux/platform_device.h> |
6b157c9b | 40 | #include <linux/workqueue.h> |
1da177e4 LT |
41 | |
42 | #include <linux/usb.h> | |
43 | ||
44 | #include "usb.h" | |
45 | #include "hcd.h" | |
46 | #include "hub.h" | |
47 | ||
48 | ||
1da177e4 LT |
49 | /*-------------------------------------------------------------------------*/ |
50 | ||
51 | /* | |
52 | * USB Host Controller Driver framework | |
53 | * | |
54 | * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing | |
55 | * HCD-specific behaviors/bugs. | |
56 | * | |
57 | * This does error checks, tracks devices and urbs, and delegates to a | |
58 | * "hc_driver" only for code (and data) that really needs to know about | |
59 | * hardware differences. That includes root hub registers, i/o queues, | |
60 | * and so on ... but as little else as possible. | |
61 | * | |
62 | * Shared code includes most of the "root hub" code (these are emulated, | |
63 | * though each HC's hardware works differently) and PCI glue, plus request | |
64 | * tracking overhead. The HCD code should only block on spinlocks or on | |
65 | * hardware handshaking; blocking on software events (such as other kernel | |
66 | * threads releasing resources, or completing actions) is all generic. | |
67 | * | |
68 | * Happens the USB 2.0 spec says this would be invisible inside the "USBD", | |
69 | * and includes mostly a "HCDI" (HCD Interface) along with some APIs used | |
70 | * only by the hub driver ... and that neither should be seen or used by | |
71 | * usb client device drivers. | |
72 | * | |
73 | * Contributors of ideas or unattributed patches include: David Brownell, | |
74 | * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ... | |
75 | * | |
76 | * HISTORY: | |
77 | * 2002-02-21 Pull in most of the usb_bus support from usb.c; some | |
78 | * associated cleanup. "usb_hcd" still != "usb_bus". | |
79 | * 2001-12-12 Initial patch version for Linux 2.5.1 kernel. | |
80 | */ | |
81 | ||
82 | /*-------------------------------------------------------------------------*/ | |
83 | ||
9beeee65 AS |
84 | /* Keep track of which host controller drivers are loaded */ |
85 | unsigned long usb_hcds_loaded; | |
86 | EXPORT_SYMBOL_GPL(usb_hcds_loaded); | |
87 | ||
1da177e4 LT |
88 | /* host controllers we manage */ |
89 | LIST_HEAD (usb_bus_list); | |
90 | EXPORT_SYMBOL_GPL (usb_bus_list); | |
91 | ||
92 | /* used when allocating bus numbers */ | |
93 | #define USB_MAXBUS 64 | |
94 | struct usb_busmap { | |
95 | unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))]; | |
96 | }; | |
97 | static struct usb_busmap busmap; | |
98 | ||
99 | /* used when updating list of hcds */ | |
4186ecf8 | 100 | DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */ |
1da177e4 LT |
101 | EXPORT_SYMBOL_GPL (usb_bus_list_lock); |
102 | ||
103 | /* used for controlling access to virtual root hubs */ | |
104 | static DEFINE_SPINLOCK(hcd_root_hub_lock); | |
105 | ||
809a58b8 AS |
106 | /* used when updating an endpoint's URB list */ |
107 | static DEFINE_SPINLOCK(hcd_urb_list_lock); | |
1da177e4 | 108 | |
cde217a5 AS |
109 | /* used to protect against unlinking URBs after the device is gone */ |
110 | static DEFINE_SPINLOCK(hcd_urb_unlink_lock); | |
111 | ||
1da177e4 LT |
112 | /* wait queue for synchronous unlinks */ |
113 | DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue); | |
114 | ||
809a58b8 AS |
115 | static inline int is_root_hub(struct usb_device *udev) |
116 | { | |
117 | return (udev->parent == NULL); | |
118 | } | |
119 | ||
1da177e4 LT |
120 | /*-------------------------------------------------------------------------*/ |
121 | ||
122 | /* | |
123 | * Sharable chunks of root hub code. | |
124 | */ | |
125 | ||
126 | /*-------------------------------------------------------------------------*/ | |
127 | ||
128 | #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff) | |
129 | #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff) | |
130 | ||
d2e9b4d6 SS |
131 | /* usb 3.0 root hub device descriptor */ |
132 | static const u8 usb3_rh_dev_descriptor[18] = { | |
133 | 0x12, /* __u8 bLength; */ | |
134 | 0x01, /* __u8 bDescriptorType; Device */ | |
135 | 0x00, 0x03, /* __le16 bcdUSB; v3.0 */ | |
136 | ||
137 | 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ | |
138 | 0x00, /* __u8 bDeviceSubClass; */ | |
139 | 0x03, /* __u8 bDeviceProtocol; USB 3.0 hub */ | |
140 | 0x09, /* __u8 bMaxPacketSize0; 2^9 = 512 Bytes */ | |
141 | ||
142 | 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */ | |
143 | 0x02, 0x00, /* __le16 idProduct; device 0x0002 */ | |
144 | KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ | |
145 | ||
146 | 0x03, /* __u8 iManufacturer; */ | |
147 | 0x02, /* __u8 iProduct; */ | |
148 | 0x01, /* __u8 iSerialNumber; */ | |
149 | 0x01 /* __u8 bNumConfigurations; */ | |
150 | }; | |
151 | ||
1da177e4 LT |
152 | /* usb 2.0 root hub device descriptor */ |
153 | static const u8 usb2_rh_dev_descriptor [18] = { | |
154 | 0x12, /* __u8 bLength; */ | |
155 | 0x01, /* __u8 bDescriptorType; Device */ | |
156 | 0x00, 0x02, /* __le16 bcdUSB; v2.0 */ | |
157 | ||
158 | 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ | |
159 | 0x00, /* __u8 bDeviceSubClass; */ | |
7329e211 | 160 | 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */ |
16f16d11 | 161 | 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ |
1da177e4 | 162 | |
667d691e GKH |
163 | 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */ |
164 | 0x02, 0x00, /* __le16 idProduct; device 0x0002 */ | |
1da177e4 LT |
165 | KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
166 | ||
167 | 0x03, /* __u8 iManufacturer; */ | |
168 | 0x02, /* __u8 iProduct; */ | |
169 | 0x01, /* __u8 iSerialNumber; */ | |
170 | 0x01 /* __u8 bNumConfigurations; */ | |
171 | }; | |
172 | ||
173 | /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */ | |
174 | ||
175 | /* usb 1.1 root hub device descriptor */ | |
176 | static const u8 usb11_rh_dev_descriptor [18] = { | |
177 | 0x12, /* __u8 bLength; */ | |
178 | 0x01, /* __u8 bDescriptorType; Device */ | |
179 | 0x10, 0x01, /* __le16 bcdUSB; v1.1 */ | |
180 | ||
181 | 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ | |
182 | 0x00, /* __u8 bDeviceSubClass; */ | |
183 | 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */ | |
16f16d11 | 184 | 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ |
1da177e4 | 185 | |
667d691e GKH |
186 | 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */ |
187 | 0x01, 0x00, /* __le16 idProduct; device 0x0001 */ | |
1da177e4 LT |
188 | KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
189 | ||
190 | 0x03, /* __u8 iManufacturer; */ | |
191 | 0x02, /* __u8 iProduct; */ | |
192 | 0x01, /* __u8 iSerialNumber; */ | |
193 | 0x01 /* __u8 bNumConfigurations; */ | |
194 | }; | |
195 | ||
196 | ||
197 | /*-------------------------------------------------------------------------*/ | |
198 | ||
199 | /* Configuration descriptors for our root hubs */ | |
200 | ||
201 | static const u8 fs_rh_config_descriptor [] = { | |
202 | ||
203 | /* one configuration */ | |
204 | 0x09, /* __u8 bLength; */ | |
205 | 0x02, /* __u8 bDescriptorType; Configuration */ | |
206 | 0x19, 0x00, /* __le16 wTotalLength; */ | |
207 | 0x01, /* __u8 bNumInterfaces; (1) */ | |
208 | 0x01, /* __u8 bConfigurationValue; */ | |
209 | 0x00, /* __u8 iConfiguration; */ | |
210 | 0xc0, /* __u8 bmAttributes; | |
211 | Bit 7: must be set, | |
212 | 6: Self-powered, | |
213 | 5: Remote wakeup, | |
214 | 4..0: resvd */ | |
215 | 0x00, /* __u8 MaxPower; */ | |
216 | ||
217 | /* USB 1.1: | |
218 | * USB 2.0, single TT organization (mandatory): | |
219 | * one interface, protocol 0 | |
220 | * | |
221 | * USB 2.0, multiple TT organization (optional): | |
222 | * two interfaces, protocols 1 (like single TT) | |
223 | * and 2 (multiple TT mode) ... config is | |
224 | * sometimes settable | |
225 | * NOT IMPLEMENTED | |
226 | */ | |
227 | ||
228 | /* one interface */ | |
229 | 0x09, /* __u8 if_bLength; */ | |
230 | 0x04, /* __u8 if_bDescriptorType; Interface */ | |
231 | 0x00, /* __u8 if_bInterfaceNumber; */ | |
232 | 0x00, /* __u8 if_bAlternateSetting; */ | |
233 | 0x01, /* __u8 if_bNumEndpoints; */ | |
234 | 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ | |
235 | 0x00, /* __u8 if_bInterfaceSubClass; */ | |
236 | 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ | |
237 | 0x00, /* __u8 if_iInterface; */ | |
238 | ||
239 | /* one endpoint (status change endpoint) */ | |
240 | 0x07, /* __u8 ep_bLength; */ | |
241 | 0x05, /* __u8 ep_bDescriptorType; Endpoint */ | |
242 | 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ | |
243 | 0x03, /* __u8 ep_bmAttributes; Interrupt */ | |
244 | 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */ | |
245 | 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */ | |
246 | }; | |
247 | ||
248 | static const u8 hs_rh_config_descriptor [] = { | |
249 | ||
250 | /* one configuration */ | |
251 | 0x09, /* __u8 bLength; */ | |
252 | 0x02, /* __u8 bDescriptorType; Configuration */ | |
253 | 0x19, 0x00, /* __le16 wTotalLength; */ | |
254 | 0x01, /* __u8 bNumInterfaces; (1) */ | |
255 | 0x01, /* __u8 bConfigurationValue; */ | |
256 | 0x00, /* __u8 iConfiguration; */ | |
257 | 0xc0, /* __u8 bmAttributes; | |
258 | Bit 7: must be set, | |
259 | 6: Self-powered, | |
260 | 5: Remote wakeup, | |
261 | 4..0: resvd */ | |
262 | 0x00, /* __u8 MaxPower; */ | |
263 | ||
264 | /* USB 1.1: | |
265 | * USB 2.0, single TT organization (mandatory): | |
266 | * one interface, protocol 0 | |
267 | * | |
268 | * USB 2.0, multiple TT organization (optional): | |
269 | * two interfaces, protocols 1 (like single TT) | |
270 | * and 2 (multiple TT mode) ... config is | |
271 | * sometimes settable | |
272 | * NOT IMPLEMENTED | |
273 | */ | |
274 | ||
275 | /* one interface */ | |
276 | 0x09, /* __u8 if_bLength; */ | |
277 | 0x04, /* __u8 if_bDescriptorType; Interface */ | |
278 | 0x00, /* __u8 if_bInterfaceNumber; */ | |
279 | 0x00, /* __u8 if_bAlternateSetting; */ | |
280 | 0x01, /* __u8 if_bNumEndpoints; */ | |
281 | 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ | |
282 | 0x00, /* __u8 if_bInterfaceSubClass; */ | |
283 | 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ | |
284 | 0x00, /* __u8 if_iInterface; */ | |
285 | ||
286 | /* one endpoint (status change endpoint) */ | |
287 | 0x07, /* __u8 ep_bLength; */ | |
288 | 0x05, /* __u8 ep_bDescriptorType; Endpoint */ | |
289 | 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ | |
290 | 0x03, /* __u8 ep_bmAttributes; Interrupt */ | |
88fafff9 | 291 | /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) |
292 | * see hub.c:hub_configure() for details. */ | |
293 | (USB_MAXCHILDREN + 1 + 7) / 8, 0x00, | |
1da177e4 LT |
294 | 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ |
295 | }; | |
296 | ||
d2e9b4d6 SS |
297 | static const u8 ss_rh_config_descriptor[] = { |
298 | /* one configuration */ | |
299 | 0x09, /* __u8 bLength; */ | |
300 | 0x02, /* __u8 bDescriptorType; Configuration */ | |
301 | 0x19, 0x00, /* __le16 wTotalLength; FIXME */ | |
302 | 0x01, /* __u8 bNumInterfaces; (1) */ | |
303 | 0x01, /* __u8 bConfigurationValue; */ | |
304 | 0x00, /* __u8 iConfiguration; */ | |
305 | 0xc0, /* __u8 bmAttributes; | |
306 | Bit 7: must be set, | |
307 | 6: Self-powered, | |
308 | 5: Remote wakeup, | |
309 | 4..0: resvd */ | |
310 | 0x00, /* __u8 MaxPower; */ | |
311 | ||
312 | /* one interface */ | |
313 | 0x09, /* __u8 if_bLength; */ | |
314 | 0x04, /* __u8 if_bDescriptorType; Interface */ | |
315 | 0x00, /* __u8 if_bInterfaceNumber; */ | |
316 | 0x00, /* __u8 if_bAlternateSetting; */ | |
317 | 0x01, /* __u8 if_bNumEndpoints; */ | |
318 | 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ | |
319 | 0x00, /* __u8 if_bInterfaceSubClass; */ | |
320 | 0x00, /* __u8 if_bInterfaceProtocol; */ | |
321 | 0x00, /* __u8 if_iInterface; */ | |
322 | ||
323 | /* one endpoint (status change endpoint) */ | |
324 | 0x07, /* __u8 ep_bLength; */ | |
325 | 0x05, /* __u8 ep_bDescriptorType; Endpoint */ | |
326 | 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ | |
327 | 0x03, /* __u8 ep_bmAttributes; Interrupt */ | |
328 | /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) | |
329 | * see hub.c:hub_configure() for details. */ | |
330 | (USB_MAXCHILDREN + 1 + 7) / 8, 0x00, | |
331 | 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ | |
332 | /* | |
333 | * All 3.0 hubs should have an endpoint companion descriptor, | |
334 | * but we're ignoring that for now. FIXME? | |
335 | */ | |
336 | }; | |
337 | ||
1da177e4 LT |
338 | /*-------------------------------------------------------------------------*/ |
339 | ||
340 | /* | |
341 | * helper routine for returning string descriptors in UTF-16LE | |
342 | * input can actually be ISO-8859-1; ASCII is its 7-bit subset | |
343 | */ | |
71d2718f | 344 | static unsigned ascii2utf(char *s, u8 *utf, int utfmax) |
1da177e4 | 345 | { |
71d2718f | 346 | unsigned retval; |
1da177e4 LT |
347 | |
348 | for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) { | |
349 | *utf++ = *s++; | |
350 | *utf++ = 0; | |
351 | } | |
352 | if (utfmax > 0) { | |
353 | *utf = *s; | |
354 | ++retval; | |
355 | } | |
356 | return retval; | |
357 | } | |
358 | ||
359 | /* | |
360 | * rh_string - provides manufacturer, product and serial strings for root hub | |
361 | * @id: the string ID number (1: serial number, 2: product, 3: vendor) | |
362 | * @hcd: the host controller for this root hub | |
1da177e4 LT |
363 | * @data: return packet in UTF-16 LE |
364 | * @len: length of the return packet | |
365 | * | |
366 | * Produces either a manufacturer, product or serial number string for the | |
367 | * virtual root hub device. | |
368 | */ | |
71d2718f RK |
369 | static unsigned rh_string(int id, struct usb_hcd *hcd, u8 *data, unsigned len) |
370 | { | |
1da177e4 LT |
371 | char buf [100]; |
372 | ||
373 | // language ids | |
374 | if (id == 0) { | |
375 | buf[0] = 4; buf[1] = 3; /* 4 bytes string data */ | |
376 | buf[2] = 0x09; buf[3] = 0x04; /* MSFT-speak for "en-us" */ | |
71d2718f | 377 | len = min_t(unsigned, len, 4); |
1da177e4 LT |
378 | memcpy (data, buf, len); |
379 | return len; | |
380 | ||
381 | // serial number | |
382 | } else if (id == 1) { | |
383 | strlcpy (buf, hcd->self.bus_name, sizeof buf); | |
384 | ||
385 | // product description | |
386 | } else if (id == 2) { | |
387 | strlcpy (buf, hcd->product_desc, sizeof buf); | |
388 | ||
389 | // id 3 == vendor description | |
390 | } else if (id == 3) { | |
96b644bd SH |
391 | snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname, |
392 | init_utsname()->release, hcd->driver->description); | |
71d2718f | 393 | } |
1da177e4 LT |
394 | |
395 | switch (len) { /* All cases fall through */ | |
396 | default: | |
397 | len = 2 + ascii2utf (buf, data + 2, len - 2); | |
398 | case 2: | |
399 | data [1] = 3; /* type == string */ | |
400 | case 1: | |
401 | data [0] = 2 * (strlen (buf) + 1); | |
402 | case 0: | |
403 | ; /* Compiler wants a statement here */ | |
404 | } | |
405 | return len; | |
406 | } | |
407 | ||
408 | ||
409 | /* Root hub control transfers execute synchronously */ | |
410 | static int rh_call_control (struct usb_hcd *hcd, struct urb *urb) | |
411 | { | |
412 | struct usb_ctrlrequest *cmd; | |
413 | u16 typeReq, wValue, wIndex, wLength; | |
414 | u8 *ubuf = urb->transfer_buffer; | |
54bee6e1 MP |
415 | u8 tbuf [sizeof (struct usb_hub_descriptor)] |
416 | __attribute__((aligned(4))); | |
1da177e4 | 417 | const u8 *bufp = tbuf; |
71d2718f | 418 | unsigned len = 0; |
e9df41c5 | 419 | int status; |
7329e211 AS |
420 | u8 patch_wakeup = 0; |
421 | u8 patch_protocol = 0; | |
1da177e4 | 422 | |
9439eb94 AS |
423 | might_sleep(); |
424 | ||
e9df41c5 AS |
425 | spin_lock_irq(&hcd_root_hub_lock); |
426 | status = usb_hcd_link_urb_to_ep(hcd, urb); | |
427 | spin_unlock_irq(&hcd_root_hub_lock); | |
428 | if (status) | |
429 | return status; | |
b0d9efba | 430 | urb->hcpriv = hcd; /* Indicate it's queued */ |
e9df41c5 | 431 | |
1da177e4 LT |
432 | cmd = (struct usb_ctrlrequest *) urb->setup_packet; |
433 | typeReq = (cmd->bRequestType << 8) | cmd->bRequest; | |
434 | wValue = le16_to_cpu (cmd->wValue); | |
435 | wIndex = le16_to_cpu (cmd->wIndex); | |
436 | wLength = le16_to_cpu (cmd->wLength); | |
437 | ||
438 | if (wLength > urb->transfer_buffer_length) | |
439 | goto error; | |
440 | ||
441 | urb->actual_length = 0; | |
442 | switch (typeReq) { | |
443 | ||
444 | /* DEVICE REQUESTS */ | |
445 | ||
fb669cc0 DB |
446 | /* The root hub's remote wakeup enable bit is implemented using |
447 | * driver model wakeup flags. If this system supports wakeup | |
448 | * through USB, userspace may change the default "allow wakeup" | |
449 | * policy through sysfs or these calls. | |
450 | * | |
451 | * Most root hubs support wakeup from downstream devices, for | |
452 | * runtime power management (disabling USB clocks and reducing | |
453 | * VBUS power usage). However, not all of them do so; silicon, | |
454 | * board, and BIOS bugs here are not uncommon, so these can't | |
455 | * be treated quite like external hubs. | |
456 | * | |
457 | * Likewise, not all root hubs will pass wakeup events upstream, | |
458 | * to wake up the whole system. So don't assume root hub and | |
459 | * controller capabilities are identical. | |
460 | */ | |
461 | ||
1da177e4 | 462 | case DeviceRequest | USB_REQ_GET_STATUS: |
fb669cc0 DB |
463 | tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev) |
464 | << USB_DEVICE_REMOTE_WAKEUP) | |
1da177e4 LT |
465 | | (1 << USB_DEVICE_SELF_POWERED); |
466 | tbuf [1] = 0; | |
467 | len = 2; | |
468 | break; | |
469 | case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: | |
470 | if (wValue == USB_DEVICE_REMOTE_WAKEUP) | |
fb669cc0 | 471 | device_set_wakeup_enable(&hcd->self.root_hub->dev, 0); |
1da177e4 LT |
472 | else |
473 | goto error; | |
474 | break; | |
475 | case DeviceOutRequest | USB_REQ_SET_FEATURE: | |
fb669cc0 DB |
476 | if (device_can_wakeup(&hcd->self.root_hub->dev) |
477 | && wValue == USB_DEVICE_REMOTE_WAKEUP) | |
478 | device_set_wakeup_enable(&hcd->self.root_hub->dev, 1); | |
1da177e4 LT |
479 | else |
480 | goto error; | |
481 | break; | |
482 | case DeviceRequest | USB_REQ_GET_CONFIGURATION: | |
483 | tbuf [0] = 1; | |
484 | len = 1; | |
485 | /* FALLTHROUGH */ | |
486 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: | |
487 | break; | |
488 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: | |
489 | switch (wValue & 0xff00) { | |
490 | case USB_DT_DEVICE << 8: | |
d2e9b4d6 SS |
491 | if (hcd->driver->flags & HCD_USB3) |
492 | bufp = usb3_rh_dev_descriptor; | |
493 | else if (hcd->driver->flags & HCD_USB2) | |
1da177e4 LT |
494 | bufp = usb2_rh_dev_descriptor; |
495 | else if (hcd->driver->flags & HCD_USB11) | |
496 | bufp = usb11_rh_dev_descriptor; | |
497 | else | |
498 | goto error; | |
499 | len = 18; | |
7329e211 AS |
500 | if (hcd->has_tt) |
501 | patch_protocol = 1; | |
1da177e4 LT |
502 | break; |
503 | case USB_DT_CONFIG << 8: | |
d2e9b4d6 SS |
504 | if (hcd->driver->flags & HCD_USB3) { |
505 | bufp = ss_rh_config_descriptor; | |
506 | len = sizeof ss_rh_config_descriptor; | |
507 | } else if (hcd->driver->flags & HCD_USB2) { | |
1da177e4 LT |
508 | bufp = hs_rh_config_descriptor; |
509 | len = sizeof hs_rh_config_descriptor; | |
510 | } else { | |
511 | bufp = fs_rh_config_descriptor; | |
512 | len = sizeof fs_rh_config_descriptor; | |
513 | } | |
fb669cc0 | 514 | if (device_can_wakeup(&hcd->self.root_hub->dev)) |
1da177e4 LT |
515 | patch_wakeup = 1; |
516 | break; | |
517 | case USB_DT_STRING << 8: | |
71d2718f RK |
518 | if ((wValue & 0xff) < 4) |
519 | urb->actual_length = rh_string(wValue & 0xff, | |
520 | hcd, ubuf, wLength); | |
521 | else /* unsupported IDs --> "protocol stall" */ | |
1da177e4 | 522 | goto error; |
1da177e4 LT |
523 | break; |
524 | default: | |
525 | goto error; | |
526 | } | |
527 | break; | |
528 | case DeviceRequest | USB_REQ_GET_INTERFACE: | |
529 | tbuf [0] = 0; | |
530 | len = 1; | |
531 | /* FALLTHROUGH */ | |
532 | case DeviceOutRequest | USB_REQ_SET_INTERFACE: | |
533 | break; | |
534 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: | |
535 | // wValue == urb->dev->devaddr | |
536 | dev_dbg (hcd->self.controller, "root hub device address %d\n", | |
537 | wValue); | |
538 | break; | |
539 | ||
540 | /* INTERFACE REQUESTS (no defined feature/status flags) */ | |
541 | ||
542 | /* ENDPOINT REQUESTS */ | |
543 | ||
544 | case EndpointRequest | USB_REQ_GET_STATUS: | |
545 | // ENDPOINT_HALT flag | |
546 | tbuf [0] = 0; | |
547 | tbuf [1] = 0; | |
548 | len = 2; | |
549 | /* FALLTHROUGH */ | |
550 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: | |
551 | case EndpointOutRequest | USB_REQ_SET_FEATURE: | |
552 | dev_dbg (hcd->self.controller, "no endpoint features yet\n"); | |
553 | break; | |
554 | ||
555 | /* CLASS REQUESTS (and errors) */ | |
556 | ||
557 | default: | |
558 | /* non-generic request */ | |
b13296c6 DB |
559 | switch (typeReq) { |
560 | case GetHubStatus: | |
561 | case GetPortStatus: | |
562 | len = 4; | |
563 | break; | |
564 | case GetHubDescriptor: | |
565 | len = sizeof (struct usb_hub_descriptor); | |
566 | break; | |
1da177e4 | 567 | } |
b13296c6 DB |
568 | status = hcd->driver->hub_control (hcd, |
569 | typeReq, wValue, wIndex, | |
570 | tbuf, wLength); | |
1da177e4 LT |
571 | break; |
572 | error: | |
573 | /* "protocol stall" on error */ | |
574 | status = -EPIPE; | |
575 | } | |
576 | ||
577 | if (status) { | |
578 | len = 0; | |
579 | if (status != -EPIPE) { | |
580 | dev_dbg (hcd->self.controller, | |
581 | "CTRL: TypeReq=0x%x val=0x%x " | |
582 | "idx=0x%x len=%d ==> %d\n", | |
583 | typeReq, wValue, wIndex, | |
b13296c6 | 584 | wLength, status); |
1da177e4 LT |
585 | } |
586 | } | |
587 | if (len) { | |
588 | if (urb->transfer_buffer_length < len) | |
589 | len = urb->transfer_buffer_length; | |
590 | urb->actual_length = len; | |
591 | // always USB_DIR_IN, toward host | |
592 | memcpy (ubuf, bufp, len); | |
593 | ||
594 | /* report whether RH hardware supports remote wakeup */ | |
595 | if (patch_wakeup && | |
596 | len > offsetof (struct usb_config_descriptor, | |
597 | bmAttributes)) | |
598 | ((struct usb_config_descriptor *)ubuf)->bmAttributes | |
599 | |= USB_CONFIG_ATT_WAKEUP; | |
7329e211 AS |
600 | |
601 | /* report whether RH hardware has an integrated TT */ | |
602 | if (patch_protocol && | |
603 | len > offsetof(struct usb_device_descriptor, | |
604 | bDeviceProtocol)) | |
605 | ((struct usb_device_descriptor *) ubuf)-> | |
606 | bDeviceProtocol = 1; | |
1da177e4 LT |
607 | } |
608 | ||
609 | /* any errors get returned through the urb completion */ | |
9439eb94 | 610 | spin_lock_irq(&hcd_root_hub_lock); |
e9df41c5 | 611 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
9439eb94 AS |
612 | |
613 | /* This peculiar use of spinlocks echoes what real HC drivers do. | |
614 | * Avoiding calls to local_irq_disable/enable makes the code | |
615 | * RT-friendly. | |
616 | */ | |
617 | spin_unlock(&hcd_root_hub_lock); | |
4a00027d | 618 | usb_hcd_giveback_urb(hcd, urb, status); |
9439eb94 AS |
619 | spin_lock(&hcd_root_hub_lock); |
620 | ||
621 | spin_unlock_irq(&hcd_root_hub_lock); | |
1da177e4 LT |
622 | return 0; |
623 | } | |
624 | ||
625 | /*-------------------------------------------------------------------------*/ | |
626 | ||
627 | /* | |
d5926ae7 AS |
628 | * Root Hub interrupt transfers are polled using a timer if the |
629 | * driver requests it; otherwise the driver is responsible for | |
630 | * calling usb_hcd_poll_rh_status() when an event occurs. | |
1da177e4 | 631 | * |
d5926ae7 AS |
632 | * Completions are called in_interrupt(), but they may or may not |
633 | * be in_irq(). | |
1da177e4 | 634 | */ |
d5926ae7 AS |
635 | void usb_hcd_poll_rh_status(struct usb_hcd *hcd) |
636 | { | |
637 | struct urb *urb; | |
638 | int length; | |
639 | unsigned long flags; | |
640 | char buffer[4]; /* Any root hubs with > 31 ports? */ | |
1da177e4 | 641 | |
1b42ae6d AS |
642 | if (unlikely(!hcd->rh_registered)) |
643 | return; | |
d5926ae7 AS |
644 | if (!hcd->uses_new_polling && !hcd->status_urb) |
645 | return; | |
1da177e4 | 646 | |
d5926ae7 AS |
647 | length = hcd->driver->hub_status_data(hcd, buffer); |
648 | if (length > 0) { | |
1da177e4 | 649 | |
d5926ae7 | 650 | /* try to complete the status urb */ |
9439eb94 | 651 | spin_lock_irqsave(&hcd_root_hub_lock, flags); |
d5926ae7 AS |
652 | urb = hcd->status_urb; |
653 | if (urb) { | |
e9df41c5 AS |
654 | hcd->poll_pending = 0; |
655 | hcd->status_urb = NULL; | |
e9df41c5 AS |
656 | urb->actual_length = length; |
657 | memcpy(urb->transfer_buffer, buffer, length); | |
9439eb94 | 658 | |
e9df41c5 | 659 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
9439eb94 | 660 | spin_unlock(&hcd_root_hub_lock); |
4a00027d | 661 | usb_hcd_giveback_urb(hcd, urb, 0); |
9439eb94 | 662 | spin_lock(&hcd_root_hub_lock); |
e9df41c5 | 663 | } else { |
d5926ae7 | 664 | length = 0; |
d5926ae7 | 665 | hcd->poll_pending = 1; |
e9df41c5 | 666 | } |
9439eb94 | 667 | spin_unlock_irqrestore(&hcd_root_hub_lock, flags); |
1da177e4 LT |
668 | } |
669 | ||
d5926ae7 | 670 | /* The USB 2.0 spec says 256 ms. This is close enough and won't |
01cd0819 AV |
671 | * exceed that limit if HZ is 100. The math is more clunky than |
672 | * maybe expected, this is to make sure that all timers for USB devices | |
673 | * fire at the same time to give the CPU a break inbetween */ | |
d5926ae7 AS |
674 | if (hcd->uses_new_polling ? hcd->poll_rh : |
675 | (length == 0 && hcd->status_urb != NULL)) | |
01cd0819 | 676 | mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); |
1da177e4 | 677 | } |
d5926ae7 | 678 | EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status); |
1da177e4 LT |
679 | |
680 | /* timer callback */ | |
d5926ae7 AS |
681 | static void rh_timer_func (unsigned long _hcd) |
682 | { | |
683 | usb_hcd_poll_rh_status((struct usb_hcd *) _hcd); | |
684 | } | |
685 | ||
686 | /*-------------------------------------------------------------------------*/ | |
1da177e4 | 687 | |
d5926ae7 | 688 | static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb) |
1da177e4 | 689 | { |
d5926ae7 | 690 | int retval; |
1da177e4 | 691 | unsigned long flags; |
71d2718f | 692 | unsigned len = 1 + (urb->dev->maxchild / 8); |
1da177e4 | 693 | |
d5926ae7 | 694 | spin_lock_irqsave (&hcd_root_hub_lock, flags); |
e9df41c5 | 695 | if (hcd->status_urb || urb->transfer_buffer_length < len) { |
d5926ae7 AS |
696 | dev_dbg (hcd->self.controller, "not queuing rh status urb\n"); |
697 | retval = -EINVAL; | |
e9df41c5 AS |
698 | goto done; |
699 | } | |
1da177e4 | 700 | |
e9df41c5 AS |
701 | retval = usb_hcd_link_urb_to_ep(hcd, urb); |
702 | if (retval) | |
703 | goto done; | |
1da177e4 | 704 | |
e9df41c5 AS |
705 | hcd->status_urb = urb; |
706 | urb->hcpriv = hcd; /* indicate it's queued */ | |
707 | if (!hcd->uses_new_polling) | |
708 | mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); | |
709 | ||
710 | /* If a status change has already occurred, report it ASAP */ | |
711 | else if (hcd->poll_pending) | |
712 | mod_timer(&hcd->rh_timer, jiffies); | |
713 | retval = 0; | |
714 | done: | |
d5926ae7 AS |
715 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
716 | return retval; | |
1da177e4 LT |
717 | } |
718 | ||
1da177e4 LT |
719 | static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb) |
720 | { | |
5e60a161 | 721 | if (usb_endpoint_xfer_int(&urb->ep->desc)) |
d5926ae7 | 722 | return rh_queue_status (hcd, urb); |
5e60a161 | 723 | if (usb_endpoint_xfer_control(&urb->ep->desc)) |
1da177e4 | 724 | return rh_call_control (hcd, urb); |
d5926ae7 | 725 | return -EINVAL; |
1da177e4 LT |
726 | } |
727 | ||
728 | /*-------------------------------------------------------------------------*/ | |
729 | ||
455b25fb AS |
730 | /* Unlinks of root-hub control URBs are legal, but they don't do anything |
731 | * since these URBs always execute synchronously. | |
d5926ae7 | 732 | */ |
e9df41c5 | 733 | static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
1da177e4 | 734 | { |
455b25fb | 735 | unsigned long flags; |
e9df41c5 | 736 | int rc; |
1da177e4 | 737 | |
9439eb94 | 738 | spin_lock_irqsave(&hcd_root_hub_lock, flags); |
e9df41c5 AS |
739 | rc = usb_hcd_check_unlink_urb(hcd, urb, status); |
740 | if (rc) | |
741 | goto done; | |
742 | ||
5e60a161 | 743 | if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */ |
455b25fb | 744 | ; /* Do nothing */ |
d5926ae7 AS |
745 | |
746 | } else { /* Status URB */ | |
747 | if (!hcd->uses_new_polling) | |
455b25fb | 748 | del_timer (&hcd->rh_timer); |
d5926ae7 AS |
749 | if (urb == hcd->status_urb) { |
750 | hcd->status_urb = NULL; | |
e9df41c5 | 751 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
1da177e4 | 752 | |
9439eb94 | 753 | spin_unlock(&hcd_root_hub_lock); |
4a00027d | 754 | usb_hcd_giveback_urb(hcd, urb, status); |
9439eb94 AS |
755 | spin_lock(&hcd_root_hub_lock); |
756 | } | |
757 | } | |
e9df41c5 | 758 | done: |
9439eb94 | 759 | spin_unlock_irqrestore(&hcd_root_hub_lock, flags); |
e9df41c5 | 760 | return rc; |
1da177e4 LT |
761 | } |
762 | ||
5234ce1b IPG |
763 | |
764 | ||
765 | /* | |
766 | * Show & store the current value of authorized_default | |
767 | */ | |
768 | static ssize_t usb_host_authorized_default_show(struct device *dev, | |
769 | struct device_attribute *attr, | |
770 | char *buf) | |
771 | { | |
772 | struct usb_device *rh_usb_dev = to_usb_device(dev); | |
773 | struct usb_bus *usb_bus = rh_usb_dev->bus; | |
774 | struct usb_hcd *usb_hcd; | |
775 | ||
776 | if (usb_bus == NULL) /* FIXME: not sure if this case is possible */ | |
777 | return -ENODEV; | |
778 | usb_hcd = bus_to_hcd(usb_bus); | |
779 | return snprintf(buf, PAGE_SIZE, "%u\n", usb_hcd->authorized_default); | |
780 | } | |
781 | ||
782 | static ssize_t usb_host_authorized_default_store(struct device *dev, | |
783 | struct device_attribute *attr, | |
784 | const char *buf, size_t size) | |
785 | { | |
786 | ssize_t result; | |
787 | unsigned val; | |
788 | struct usb_device *rh_usb_dev = to_usb_device(dev); | |
789 | struct usb_bus *usb_bus = rh_usb_dev->bus; | |
790 | struct usb_hcd *usb_hcd; | |
791 | ||
792 | if (usb_bus == NULL) /* FIXME: not sure if this case is possible */ | |
793 | return -ENODEV; | |
794 | usb_hcd = bus_to_hcd(usb_bus); | |
795 | result = sscanf(buf, "%u\n", &val); | |
796 | if (result == 1) { | |
797 | usb_hcd->authorized_default = val? 1 : 0; | |
798 | result = size; | |
799 | } | |
800 | else | |
801 | result = -EINVAL; | |
802 | return result; | |
803 | } | |
804 | ||
805 | static DEVICE_ATTR(authorized_default, 0644, | |
806 | usb_host_authorized_default_show, | |
807 | usb_host_authorized_default_store); | |
808 | ||
809 | ||
810 | /* Group all the USB bus attributes */ | |
811 | static struct attribute *usb_bus_attrs[] = { | |
812 | &dev_attr_authorized_default.attr, | |
813 | NULL, | |
814 | }; | |
815 | ||
816 | static struct attribute_group usb_bus_attr_group = { | |
817 | .name = NULL, /* we want them in the same directory */ | |
818 | .attrs = usb_bus_attrs, | |
819 | }; | |
820 | ||
821 | ||
822 | ||
1da177e4 LT |
823 | /*-------------------------------------------------------------------------*/ |
824 | ||
1da177e4 LT |
825 | /** |
826 | * usb_bus_init - shared initialization code | |
827 | * @bus: the bus structure being initialized | |
828 | * | |
829 | * This code is used to initialize a usb_bus structure, memory for which is | |
830 | * separately managed. | |
831 | */ | |
832 | static void usb_bus_init (struct usb_bus *bus) | |
833 | { | |
834 | memset (&bus->devmap, 0, sizeof(struct usb_devmap)); | |
835 | ||
836 | bus->devnum_next = 1; | |
837 | ||
838 | bus->root_hub = NULL; | |
1da177e4 LT |
839 | bus->busnum = -1; |
840 | bus->bandwidth_allocated = 0; | |
841 | bus->bandwidth_int_reqs = 0; | |
842 | bus->bandwidth_isoc_reqs = 0; | |
843 | ||
844 | INIT_LIST_HEAD (&bus->bus_list); | |
1da177e4 LT |
845 | } |
846 | ||
1da177e4 LT |
847 | /*-------------------------------------------------------------------------*/ |
848 | ||
849 | /** | |
850 | * usb_register_bus - registers the USB host controller with the usb core | |
851 | * @bus: pointer to the bus to register | |
852 | * Context: !in_interrupt() | |
853 | * | |
854 | * Assigns a bus number, and links the controller into usbcore data | |
855 | * structures so that it can be seen by scanning the bus list. | |
856 | */ | |
857 | static int usb_register_bus(struct usb_bus *bus) | |
858 | { | |
eb579f58 | 859 | int result = -E2BIG; |
1da177e4 | 860 | int busnum; |
1da177e4 | 861 | |
4186ecf8 | 862 | mutex_lock(&usb_bus_list_lock); |
1da177e4 | 863 | busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1); |
eb579f58 | 864 | if (busnum >= USB_MAXBUS) { |
1da177e4 | 865 | printk (KERN_ERR "%s: too many buses\n", usbcore_name); |
eb579f58 | 866 | goto error_find_busnum; |
1da177e4 | 867 | } |
eb579f58 IPG |
868 | set_bit (busnum, busmap.busmap); |
869 | bus->busnum = busnum; | |
5a3201b2 | 870 | |
1da177e4 LT |
871 | /* Add it to the local list of buses */ |
872 | list_add (&bus->bus_list, &usb_bus_list); | |
4186ecf8 | 873 | mutex_unlock(&usb_bus_list_lock); |
1da177e4 | 874 | |
3099e75a | 875 | usb_notify_add_bus(bus); |
1da177e4 | 876 | |
eb579f58 IPG |
877 | dev_info (bus->controller, "new USB bus registered, assigned bus " |
878 | "number %d\n", bus->busnum); | |
1da177e4 | 879 | return 0; |
eb579f58 | 880 | |
eb579f58 IPG |
881 | error_find_busnum: |
882 | mutex_unlock(&usb_bus_list_lock); | |
883 | return result; | |
1da177e4 LT |
884 | } |
885 | ||
886 | /** | |
887 | * usb_deregister_bus - deregisters the USB host controller | |
888 | * @bus: pointer to the bus to deregister | |
889 | * Context: !in_interrupt() | |
890 | * | |
891 | * Recycles the bus number, and unlinks the controller from usbcore data | |
892 | * structures so that it won't be seen by scanning the bus list. | |
893 | */ | |
894 | static void usb_deregister_bus (struct usb_bus *bus) | |
895 | { | |
896 | dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum); | |
897 | ||
898 | /* | |
899 | * NOTE: make sure that all the devices are removed by the | |
900 | * controller code, as well as having it call this when cleaning | |
901 | * itself up | |
902 | */ | |
4186ecf8 | 903 | mutex_lock(&usb_bus_list_lock); |
1da177e4 | 904 | list_del (&bus->bus_list); |
4186ecf8 | 905 | mutex_unlock(&usb_bus_list_lock); |
1da177e4 | 906 | |
3099e75a | 907 | usb_notify_remove_bus(bus); |
1da177e4 LT |
908 | |
909 | clear_bit (bus->busnum, busmap.busmap); | |
1da177e4 LT |
910 | } |
911 | ||
912 | /** | |
8ec8d20b | 913 | * register_root_hub - called by usb_add_hcd() to register a root hub |
1da177e4 LT |
914 | * @hcd: host controller for this root hub |
915 | * | |
8ec8d20b | 916 | * This function registers the root hub with the USB subsystem. It sets up |
b1e8f0a6 DB |
917 | * the device properly in the device tree and then calls usb_new_device() |
918 | * to register the usb device. It also assigns the root hub's USB address | |
919 | * (always 1). | |
1da177e4 | 920 | */ |
b1e8f0a6 | 921 | static int register_root_hub(struct usb_hcd *hcd) |
1da177e4 LT |
922 | { |
923 | struct device *parent_dev = hcd->self.controller; | |
b1e8f0a6 | 924 | struct usb_device *usb_dev = hcd->self.root_hub; |
1da177e4 LT |
925 | const int devnum = 1; |
926 | int retval; | |
927 | ||
1da177e4 LT |
928 | usb_dev->devnum = devnum; |
929 | usb_dev->bus->devnum_next = devnum + 1; | |
930 | memset (&usb_dev->bus->devmap.devicemap, 0, | |
931 | sizeof usb_dev->bus->devmap.devicemap); | |
932 | set_bit (devnum, usb_dev->bus->devmap.devicemap); | |
933 | usb_set_device_state(usb_dev, USB_STATE_ADDRESS); | |
934 | ||
4186ecf8 | 935 | mutex_lock(&usb_bus_list_lock); |
1da177e4 | 936 | |
551509d2 | 937 | usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
1da177e4 LT |
938 | retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE); |
939 | if (retval != sizeof usb_dev->descriptor) { | |
4186ecf8 | 940 | mutex_unlock(&usb_bus_list_lock); |
1da177e4 | 941 | dev_dbg (parent_dev, "can't read %s device descriptor %d\n", |
7071a3ce | 942 | dev_name(&usb_dev->dev), retval); |
1da177e4 LT |
943 | return (retval < 0) ? retval : -EMSGSIZE; |
944 | } | |
945 | ||
1da177e4 | 946 | retval = usb_new_device (usb_dev); |
1da177e4 | 947 | if (retval) { |
1da177e4 | 948 | dev_err (parent_dev, "can't register root hub for %s, %d\n", |
7071a3ce | 949 | dev_name(&usb_dev->dev), retval); |
1da177e4 | 950 | } |
4186ecf8 | 951 | mutex_unlock(&usb_bus_list_lock); |
1da177e4 LT |
952 | |
953 | if (retval == 0) { | |
954 | spin_lock_irq (&hcd_root_hub_lock); | |
955 | hcd->rh_registered = 1; | |
956 | spin_unlock_irq (&hcd_root_hub_lock); | |
957 | ||
958 | /* Did the HC die before the root hub was registered? */ | |
959 | if (hcd->state == HC_STATE_HALT) | |
960 | usb_hc_died (hcd); /* This time clean up */ | |
961 | } | |
962 | ||
963 | return retval; | |
964 | } | |
1da177e4 LT |
965 | |
966 | ||
967 | /*-------------------------------------------------------------------------*/ | |
968 | ||
969 | /** | |
970 | * usb_calc_bus_time - approximate periodic transaction time in nanoseconds | |
971 | * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH} | |
972 | * @is_input: true iff the transaction sends data to the host | |
973 | * @isoc: true for isochronous transactions, false for interrupt ones | |
974 | * @bytecount: how many bytes in the transaction. | |
975 | * | |
976 | * Returns approximate bus time in nanoseconds for a periodic transaction. | |
977 | * See USB 2.0 spec section 5.11.3; only periodic transfers need to be | |
978 | * scheduled in software, this function is only used for such scheduling. | |
979 | */ | |
980 | long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount) | |
981 | { | |
982 | unsigned long tmp; | |
983 | ||
984 | switch (speed) { | |
985 | case USB_SPEED_LOW: /* INTR only */ | |
986 | if (is_input) { | |
987 | tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L; | |
988 | return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp); | |
989 | } else { | |
990 | tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L; | |
991 | return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp); | |
992 | } | |
993 | case USB_SPEED_FULL: /* ISOC or INTR */ | |
994 | if (isoc) { | |
995 | tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; | |
996 | return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp); | |
997 | } else { | |
998 | tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; | |
999 | return (9107L + BW_HOST_DELAY + tmp); | |
1000 | } | |
1001 | case USB_SPEED_HIGH: /* ISOC or INTR */ | |
1002 | // FIXME adjust for input vs output | |
1003 | if (isoc) | |
498f78e6 | 1004 | tmp = HS_NSECS_ISO (bytecount); |
1da177e4 | 1005 | else |
498f78e6 | 1006 | tmp = HS_NSECS (bytecount); |
1da177e4 LT |
1007 | return tmp; |
1008 | default: | |
1009 | pr_debug ("%s: bogus device speed!\n", usbcore_name); | |
1010 | return -1; | |
1011 | } | |
1012 | } | |
782e70c6 | 1013 | EXPORT_SYMBOL_GPL(usb_calc_bus_time); |
1da177e4 | 1014 | |
1da177e4 LT |
1015 | |
1016 | /*-------------------------------------------------------------------------*/ | |
1017 | ||
1018 | /* | |
1019 | * Generic HC operations. | |
1020 | */ | |
1021 | ||
1022 | /*-------------------------------------------------------------------------*/ | |
1023 | ||
e9df41c5 AS |
1024 | /** |
1025 | * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue | |
1026 | * @hcd: host controller to which @urb was submitted | |
1027 | * @urb: URB being submitted | |
1028 | * | |
1029 | * Host controller drivers should call this routine in their enqueue() | |
1030 | * method. The HCD's private spinlock must be held and interrupts must | |
1031 | * be disabled. The actions carried out here are required for URB | |
1032 | * submission, as well as for endpoint shutdown and for usb_kill_urb. | |
1033 | * | |
1034 | * Returns 0 for no error, otherwise a negative error code (in which case | |
1035 | * the enqueue() method must fail). If no error occurs but enqueue() fails | |
1036 | * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing | |
1037 | * the private spinlock and returning. | |
1038 | */ | |
1039 | int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb) | |
1da177e4 | 1040 | { |
9a9bf406 | 1041 | int rc = 0; |
1da177e4 | 1042 | |
e9df41c5 | 1043 | spin_lock(&hcd_urb_list_lock); |
1da177e4 | 1044 | |
9a9bf406 | 1045 | /* Check that the URB isn't being killed */ |
49367d8f | 1046 | if (unlikely(atomic_read(&urb->reject))) { |
9a9bf406 AS |
1047 | rc = -EPERM; |
1048 | goto done; | |
9f6a93f7 | 1049 | } |
1da177e4 | 1050 | |
9a9bf406 AS |
1051 | if (unlikely(!urb->ep->enabled)) { |
1052 | rc = -ENOENT; | |
1053 | goto done; | |
1054 | } | |
1da177e4 | 1055 | |
6840d255 AS |
1056 | if (unlikely(!urb->dev->can_submit)) { |
1057 | rc = -EHOSTUNREACH; | |
1058 | goto done; | |
1059 | } | |
1060 | ||
1da177e4 | 1061 | /* |
9a9bf406 AS |
1062 | * Check the host controller's state and add the URB to the |
1063 | * endpoint's queue. | |
1da177e4 | 1064 | */ |
9a9bf406 | 1065 | switch (hcd->state) { |
1da177e4 LT |
1066 | case HC_STATE_RUNNING: |
1067 | case HC_STATE_RESUMING: | |
eb231054 | 1068 | urb->unlinked = 0; |
9a9bf406 | 1069 | list_add_tail(&urb->urb_list, &urb->ep->urb_list); |
1da177e4 LT |
1070 | break; |
1071 | default: | |
9a9bf406 AS |
1072 | rc = -ESHUTDOWN; |
1073 | goto done; | |
1da177e4 | 1074 | } |
9a9bf406 | 1075 | done: |
e9df41c5 | 1076 | spin_unlock(&hcd_urb_list_lock); |
9a9bf406 AS |
1077 | return rc; |
1078 | } | |
e9df41c5 | 1079 | EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep); |
9a9bf406 | 1080 | |
e9df41c5 AS |
1081 | /** |
1082 | * usb_hcd_check_unlink_urb - check whether an URB may be unlinked | |
1083 | * @hcd: host controller to which @urb was submitted | |
1084 | * @urb: URB being checked for unlinkability | |
1085 | * @status: error code to store in @urb if the unlink succeeds | |
1086 | * | |
1087 | * Host controller drivers should call this routine in their dequeue() | |
1088 | * method. The HCD's private spinlock must be held and interrupts must | |
1089 | * be disabled. The actions carried out here are required for making | |
1090 | * sure than an unlink is valid. | |
1091 | * | |
1092 | * Returns 0 for no error, otherwise a negative error code (in which case | |
1093 | * the dequeue() method must fail). The possible error codes are: | |
1094 | * | |
1095 | * -EIDRM: @urb was not submitted or has already completed. | |
1096 | * The completion function may not have been called yet. | |
1097 | * | |
1098 | * -EBUSY: @urb has already been unlinked. | |
1099 | */ | |
1100 | int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb, | |
9a9bf406 AS |
1101 | int status) |
1102 | { | |
9a9bf406 | 1103 | struct list_head *tmp; |
9a9bf406 AS |
1104 | |
1105 | /* insist the urb is still queued */ | |
1106 | list_for_each(tmp, &urb->ep->urb_list) { | |
1107 | if (tmp == &urb->urb_list) | |
1108 | break; | |
1109 | } | |
e9df41c5 AS |
1110 | if (tmp != &urb->urb_list) |
1111 | return -EIDRM; | |
1da177e4 | 1112 | |
9a9bf406 AS |
1113 | /* Any status except -EINPROGRESS means something already started to |
1114 | * unlink this URB from the hardware. So there's no more work to do. | |
1da177e4 | 1115 | */ |
eb231054 | 1116 | if (urb->unlinked) |
e9df41c5 | 1117 | return -EBUSY; |
eb231054 | 1118 | urb->unlinked = status; |
1da177e4 | 1119 | |
9a9bf406 AS |
1120 | /* IRQ setup can easily be broken so that USB controllers |
1121 | * never get completion IRQs ... maybe even the ones we need to | |
1122 | * finish unlinking the initial failed usb_set_address() | |
1123 | * or device descriptor fetch. | |
1124 | */ | |
1125 | if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags) && | |
1126 | !is_root_hub(urb->dev)) { | |
1127 | dev_warn(hcd->self.controller, "Unlink after no-IRQ? " | |
1128 | "Controller is probably using the wrong IRQ.\n"); | |
1129 | set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); | |
1130 | } | |
1131 | ||
e9df41c5 | 1132 | return 0; |
9a9bf406 | 1133 | } |
e9df41c5 | 1134 | EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb); |
9a9bf406 | 1135 | |
e9df41c5 AS |
1136 | /** |
1137 | * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue | |
1138 | * @hcd: host controller to which @urb was submitted | |
1139 | * @urb: URB being unlinked | |
1140 | * | |
1141 | * Host controller drivers should call this routine before calling | |
1142 | * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and | |
1143 | * interrupts must be disabled. The actions carried out here are required | |
1144 | * for URB completion. | |
1145 | */ | |
1146 | void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb) | |
9a9bf406 | 1147 | { |
9a9bf406 | 1148 | /* clear all state linking urb to this dev (and hcd) */ |
e9df41c5 | 1149 | spin_lock(&hcd_urb_list_lock); |
9a9bf406 | 1150 | list_del_init(&urb->urb_list); |
e9df41c5 | 1151 | spin_unlock(&hcd_urb_list_lock); |
9a9bf406 | 1152 | } |
e9df41c5 | 1153 | EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep); |
9a9bf406 | 1154 | |
b3476675 MD |
1155 | /* |
1156 | * Some usb host controllers can only perform dma using a small SRAM area. | |
1157 | * The usb core itself is however optimized for host controllers that can dma | |
1158 | * using regular system memory - like pci devices doing bus mastering. | |
1159 | * | |
1160 | * To support host controllers with limited dma capabilites we provide dma | |
1161 | * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag. | |
1162 | * For this to work properly the host controller code must first use the | |
1163 | * function dma_declare_coherent_memory() to point out which memory area | |
1164 | * that should be used for dma allocations. | |
1165 | * | |
1166 | * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for | |
1167 | * dma using dma_alloc_coherent() which in turn allocates from the memory | |
1168 | * area pointed out with dma_declare_coherent_memory(). | |
1169 | * | |
1170 | * So, to summarize... | |
1171 | * | |
1172 | * - We need "local" memory, canonical example being | |
1173 | * a small SRAM on a discrete controller being the | |
1174 | * only memory that the controller can read ... | |
1175 | * (a) "normal" kernel memory is no good, and | |
1176 | * (b) there's not enough to share | |
1177 | * | |
1178 | * - The only *portable* hook for such stuff in the | |
1179 | * DMA framework is dma_declare_coherent_memory() | |
1180 | * | |
1181 | * - So we use that, even though the primary requirement | |
1182 | * is that the memory be "local" (hence addressible | |
1183 | * by that device), not "coherent". | |
1184 | * | |
1185 | */ | |
1186 | ||
1187 | static int hcd_alloc_coherent(struct usb_bus *bus, | |
1188 | gfp_t mem_flags, dma_addr_t *dma_handle, | |
1189 | void **vaddr_handle, size_t size, | |
1190 | enum dma_data_direction dir) | |
1191 | { | |
1192 | unsigned char *vaddr; | |
1193 | ||
1194 | vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr), | |
1195 | mem_flags, dma_handle); | |
1196 | if (!vaddr) | |
1197 | return -ENOMEM; | |
1198 | ||
1199 | /* | |
1200 | * Store the virtual address of the buffer at the end | |
1201 | * of the allocated dma buffer. The size of the buffer | |
1202 | * may be uneven so use unaligned functions instead | |
1203 | * of just rounding up. It makes sense to optimize for | |
1204 | * memory footprint over access speed since the amount | |
1205 | * of memory available for dma may be limited. | |
1206 | */ | |
1207 | put_unaligned((unsigned long)*vaddr_handle, | |
1208 | (unsigned long *)(vaddr + size)); | |
1209 | ||
1210 | if (dir == DMA_TO_DEVICE) | |
1211 | memcpy(vaddr, *vaddr_handle, size); | |
1212 | ||
1213 | *vaddr_handle = vaddr; | |
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle, | |
1218 | void **vaddr_handle, size_t size, | |
1219 | enum dma_data_direction dir) | |
1220 | { | |
1221 | unsigned char *vaddr = *vaddr_handle; | |
1222 | ||
1223 | vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size)); | |
1224 | ||
1225 | if (dir == DMA_FROM_DEVICE) | |
1226 | memcpy(vaddr, *vaddr_handle, size); | |
1227 | ||
1228 | hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle); | |
1229 | ||
1230 | *vaddr_handle = vaddr; | |
1231 | *dma_handle = 0; | |
1232 | } | |
1233 | ||
1234 | static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, | |
1235 | gfp_t mem_flags) | |
9a9bf406 | 1236 | { |
b3476675 MD |
1237 | enum dma_data_direction dir; |
1238 | int ret = 0; | |
1239 | ||
9a9bf406 AS |
1240 | /* Map the URB's buffers for DMA access. |
1241 | * Lower level HCD code should use *_dma exclusively, | |
e04748e3 SS |
1242 | * unless it uses pio or talks to another transport, |
1243 | * or uses the provided scatter gather list for bulk. | |
1da177e4 | 1244 | */ |
b3476675 MD |
1245 | if (is_root_hub(urb->dev)) |
1246 | return 0; | |
1247 | ||
1248 | if (usb_endpoint_xfer_control(&urb->ep->desc) | |
1249 | && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) { | |
1250 | if (hcd->self.uses_dma) | |
1251 | urb->setup_dma = dma_map_single( | |
1da177e4 LT |
1252 | hcd->self.controller, |
1253 | urb->setup_packet, | |
b3476675 | 1254 | sizeof(struct usb_ctrlrequest), |
1da177e4 | 1255 | DMA_TO_DEVICE); |
b3476675 MD |
1256 | else if (hcd->driver->flags & HCD_LOCAL_MEM) |
1257 | ret = hcd_alloc_coherent( | |
1258 | urb->dev->bus, mem_flags, | |
1259 | &urb->setup_dma, | |
1260 | (void **)&urb->setup_packet, | |
1261 | sizeof(struct usb_ctrlrequest), | |
1262 | DMA_TO_DEVICE); | |
1263 | } | |
1264 | ||
1265 | dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; | |
1266 | if (ret == 0 && urb->transfer_buffer_length != 0 | |
1267 | && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { | |
1268 | if (hcd->self.uses_dma) | |
1da177e4 LT |
1269 | urb->transfer_dma = dma_map_single ( |
1270 | hcd->self.controller, | |
1271 | urb->transfer_buffer, | |
1272 | urb->transfer_buffer_length, | |
b3476675 MD |
1273 | dir); |
1274 | else if (hcd->driver->flags & HCD_LOCAL_MEM) { | |
1275 | ret = hcd_alloc_coherent( | |
1276 | urb->dev->bus, mem_flags, | |
1277 | &urb->transfer_dma, | |
1278 | &urb->transfer_buffer, | |
1279 | urb->transfer_buffer_length, | |
1280 | dir); | |
1281 | ||
1282 | if (ret && usb_endpoint_xfer_control(&urb->ep->desc) | |
1283 | && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) | |
1284 | hcd_free_coherent(urb->dev->bus, | |
1285 | &urb->setup_dma, | |
1286 | (void **)&urb->setup_packet, | |
1287 | sizeof(struct usb_ctrlrequest), | |
1288 | DMA_TO_DEVICE); | |
1289 | } | |
1da177e4 | 1290 | } |
b3476675 | 1291 | return ret; |
9a9bf406 | 1292 | } |
1da177e4 | 1293 | |
9a9bf406 AS |
1294 | static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) |
1295 | { | |
b3476675 MD |
1296 | enum dma_data_direction dir; |
1297 | ||
1298 | if (is_root_hub(urb->dev)) | |
1299 | return; | |
1300 | ||
1301 | if (usb_endpoint_xfer_control(&urb->ep->desc) | |
1302 | && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) { | |
1303 | if (hcd->self.uses_dma) | |
9a9bf406 AS |
1304 | dma_unmap_single(hcd->self.controller, urb->setup_dma, |
1305 | sizeof(struct usb_ctrlrequest), | |
1306 | DMA_TO_DEVICE); | |
b3476675 MD |
1307 | else if (hcd->driver->flags & HCD_LOCAL_MEM) |
1308 | hcd_free_coherent(urb->dev->bus, &urb->setup_dma, | |
1309 | (void **)&urb->setup_packet, | |
1310 | sizeof(struct usb_ctrlrequest), | |
1311 | DMA_TO_DEVICE); | |
1312 | } | |
1313 | ||
1314 | dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; | |
1315 | if (urb->transfer_buffer_length != 0 | |
1316 | && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { | |
1317 | if (hcd->self.uses_dma) | |
9a9bf406 AS |
1318 | dma_unmap_single(hcd->self.controller, |
1319 | urb->transfer_dma, | |
1320 | urb->transfer_buffer_length, | |
b3476675 MD |
1321 | dir); |
1322 | else if (hcd->driver->flags & HCD_LOCAL_MEM) | |
1323 | hcd_free_coherent(urb->dev->bus, &urb->transfer_dma, | |
1324 | &urb->transfer_buffer, | |
1325 | urb->transfer_buffer_length, | |
1326 | dir); | |
9a9bf406 AS |
1327 | } |
1328 | } | |
1329 | ||
1330 | /*-------------------------------------------------------------------------*/ | |
1331 | ||
1332 | /* may be called in any context with a valid urb->dev usecount | |
1333 | * caller surrenders "ownership" of urb | |
1334 | * expects usb_submit_urb() to have sanity checked and conditioned all | |
1335 | * inputs in the urb | |
1336 | */ | |
1337 | int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags) | |
1338 | { | |
1339 | int status; | |
1340 | struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); | |
1341 | ||
1342 | /* increment urb's reference count as part of giving it to the HCD | |
1343 | * (which will control it). HCD guarantees that it either returns | |
1344 | * an error or calls giveback(), but not both. | |
1345 | */ | |
1346 | usb_get_urb(urb); | |
1347 | atomic_inc(&urb->use_count); | |
4d59d8a1 | 1348 | atomic_inc(&urb->dev->urbnum); |
9a9bf406 AS |
1349 | usbmon_urb_submit(&hcd->self, urb); |
1350 | ||
1351 | /* NOTE requirements on root-hub callers (usbfs and the hub | |
1352 | * driver, for now): URBs' urb->transfer_buffer must be | |
1353 | * valid and usb_buffer_{sync,unmap}() not be needed, since | |
1354 | * they could clobber root hub response data. Also, control | |
1355 | * URBs must be submitted in process context with interrupts | |
1356 | * enabled. | |
1357 | */ | |
b3476675 MD |
1358 | status = map_urb_for_dma(hcd, urb, mem_flags); |
1359 | if (unlikely(status)) { | |
1360 | usbmon_urb_submit_error(&hcd->self, urb, status); | |
1361 | goto error; | |
1362 | } | |
1363 | ||
e9df41c5 AS |
1364 | if (is_root_hub(urb->dev)) |
1365 | status = rh_urb_enqueue(hcd, urb); | |
1366 | else | |
1367 | status = hcd->driver->urb_enqueue(hcd, urb, mem_flags); | |
9a9bf406 AS |
1368 | |
1369 | if (unlikely(status)) { | |
1da177e4 | 1370 | usbmon_urb_submit_error(&hcd->self, urb, status); |
9a9bf406 | 1371 | unmap_urb_for_dma(hcd, urb); |
b3476675 | 1372 | error: |
b0d9efba | 1373 | urb->hcpriv = NULL; |
9a9bf406 AS |
1374 | INIT_LIST_HEAD(&urb->urb_list); |
1375 | atomic_dec(&urb->use_count); | |
4d59d8a1 | 1376 | atomic_dec(&urb->dev->urbnum); |
49367d8f | 1377 | if (atomic_read(&urb->reject)) |
9a9bf406 AS |
1378 | wake_up(&usb_kill_urb_queue); |
1379 | usb_put_urb(urb); | |
1da177e4 LT |
1380 | } |
1381 | return status; | |
1382 | } | |
1383 | ||
1384 | /*-------------------------------------------------------------------------*/ | |
1385 | ||
1da177e4 LT |
1386 | /* this makes the hcd giveback() the urb more quickly, by kicking it |
1387 | * off hardware queues (which may take a while) and returning it as | |
1388 | * soon as practical. we've already set up the urb's return status, | |
1389 | * but we can't know if the callback completed already. | |
1390 | */ | |
e9df41c5 | 1391 | static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status) |
1da177e4 LT |
1392 | { |
1393 | int value; | |
1394 | ||
809a58b8 | 1395 | if (is_root_hub(urb->dev)) |
e9df41c5 | 1396 | value = usb_rh_urb_dequeue(hcd, urb, status); |
1da177e4 LT |
1397 | else { |
1398 | ||
1399 | /* The only reason an HCD might fail this call is if | |
1400 | * it has not yet fully queued the urb to begin with. | |
1401 | * Such failures should be harmless. */ | |
e9df41c5 | 1402 | value = hcd->driver->urb_dequeue(hcd, urb, status); |
1da177e4 | 1403 | } |
1da177e4 LT |
1404 | return value; |
1405 | } | |
1406 | ||
1407 | /* | |
1408 | * called in any context | |
1409 | * | |
1410 | * caller guarantees urb won't be recycled till both unlink() | |
1411 | * and the urb's completion function return | |
1412 | */ | |
a6d2bb9f | 1413 | int usb_hcd_unlink_urb (struct urb *urb, int status) |
1da177e4 | 1414 | { |
9a9bf406 | 1415 | struct usb_hcd *hcd; |
cde217a5 AS |
1416 | int retval = -EIDRM; |
1417 | unsigned long flags; | |
1da177e4 | 1418 | |
cde217a5 AS |
1419 | /* Prevent the device and bus from going away while |
1420 | * the unlink is carried out. If they are already gone | |
1421 | * then urb->use_count must be 0, since disconnected | |
1422 | * devices can't have any active URBs. | |
1423 | */ | |
1424 | spin_lock_irqsave(&hcd_urb_unlink_lock, flags); | |
1425 | if (atomic_read(&urb->use_count) > 0) { | |
1426 | retval = 0; | |
1427 | usb_get_dev(urb->dev); | |
1428 | } | |
1429 | spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags); | |
1430 | if (retval == 0) { | |
1431 | hcd = bus_to_hcd(urb->dev->bus); | |
1432 | retval = unlink1(hcd, urb, status); | |
1433 | usb_put_dev(urb->dev); | |
1434 | } | |
1da177e4 | 1435 | |
1da177e4 LT |
1436 | if (retval == 0) |
1437 | retval = -EINPROGRESS; | |
e9df41c5 | 1438 | else if (retval != -EIDRM && retval != -EBUSY) |
9a9bf406 AS |
1439 | dev_dbg(&urb->dev->dev, "hcd_unlink_urb %p fail %d\n", |
1440 | urb, retval); | |
1da177e4 LT |
1441 | return retval; |
1442 | } | |
1443 | ||
1444 | /*-------------------------------------------------------------------------*/ | |
1445 | ||
32aca560 AS |
1446 | /** |
1447 | * usb_hcd_giveback_urb - return URB from HCD to device driver | |
1448 | * @hcd: host controller returning the URB | |
1449 | * @urb: urb being returned to the USB device driver. | |
4a00027d | 1450 | * @status: completion status code for the URB. |
32aca560 AS |
1451 | * Context: in_interrupt() |
1452 | * | |
1453 | * This hands the URB from HCD to its USB device driver, using its | |
1454 | * completion function. The HCD has freed all per-urb resources | |
1455 | * (and is done using urb->hcpriv). It also released all HCD locks; | |
1456 | * the device driver won't cause problems if it frees, modifies, | |
1457 | * or resubmits this URB. | |
eb231054 | 1458 | * |
4a00027d | 1459 | * If @urb was unlinked, the value of @status will be overridden by |
eb231054 AS |
1460 | * @urb->unlinked. Erroneous short transfers are detected in case |
1461 | * the HCD hasn't checked for them. | |
32aca560 | 1462 | */ |
4a00027d | 1463 | void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status) |
32aca560 | 1464 | { |
b0d9efba | 1465 | urb->hcpriv = NULL; |
eb231054 | 1466 | if (unlikely(urb->unlinked)) |
4a00027d | 1467 | status = urb->unlinked; |
eb231054 | 1468 | else if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) && |
b0d9efba | 1469 | urb->actual_length < urb->transfer_buffer_length && |
4a00027d AS |
1470 | !status)) |
1471 | status = -EREMOTEIO; | |
32aca560 | 1472 | |
1f5a3d0f | 1473 | unmap_urb_for_dma(hcd, urb); |
4a00027d | 1474 | usbmon_urb_complete(&hcd->self, urb, status); |
1f5a3d0f AS |
1475 | usb_unanchor_urb(urb); |
1476 | ||
32aca560 | 1477 | /* pass ownership to the completion handler */ |
4a00027d | 1478 | urb->status = status; |
32aca560 AS |
1479 | urb->complete (urb); |
1480 | atomic_dec (&urb->use_count); | |
49367d8f | 1481 | if (unlikely(atomic_read(&urb->reject))) |
32aca560 AS |
1482 | wake_up (&usb_kill_urb_queue); |
1483 | usb_put_urb (urb); | |
1484 | } | |
782e70c6 | 1485 | EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb); |
32aca560 AS |
1486 | |
1487 | /*-------------------------------------------------------------------------*/ | |
1488 | ||
95cf82f9 AS |
1489 | /* Cancel all URBs pending on this endpoint and wait for the endpoint's |
1490 | * queue to drain completely. The caller must first insure that no more | |
1491 | * URBs can be submitted for this endpoint. | |
1da177e4 | 1492 | */ |
95cf82f9 | 1493 | void usb_hcd_flush_endpoint(struct usb_device *udev, |
a6d2bb9f | 1494 | struct usb_host_endpoint *ep) |
1da177e4 LT |
1495 | { |
1496 | struct usb_hcd *hcd; | |
1497 | struct urb *urb; | |
1498 | ||
95cf82f9 AS |
1499 | if (!ep) |
1500 | return; | |
9a9bf406 | 1501 | might_sleep(); |
17200583 | 1502 | hcd = bus_to_hcd(udev->bus); |
1da177e4 | 1503 | |
95cf82f9 | 1504 | /* No more submits can occur */ |
9a9bf406 | 1505 | spin_lock_irq(&hcd_urb_list_lock); |
ddc1fd6a | 1506 | rescan: |
1da177e4 | 1507 | list_for_each_entry (urb, &ep->urb_list, urb_list) { |
5e60a161 | 1508 | int is_in; |
1da177e4 | 1509 | |
eb231054 | 1510 | if (urb->unlinked) |
1da177e4 LT |
1511 | continue; |
1512 | usb_get_urb (urb); | |
5e60a161 | 1513 | is_in = usb_urb_dir_in(urb); |
809a58b8 | 1514 | spin_unlock(&hcd_urb_list_lock); |
1da177e4 | 1515 | |
e9df41c5 AS |
1516 | /* kick hcd */ |
1517 | unlink1(hcd, urb, -ESHUTDOWN); | |
1518 | dev_dbg (hcd->self.controller, | |
1519 | "shutdown urb %p ep%d%s%s\n", | |
1520 | urb, usb_endpoint_num(&ep->desc), | |
1521 | is_in ? "in" : "out", | |
1522 | ({ char *s; | |
1523 | ||
1524 | switch (usb_endpoint_type(&ep->desc)) { | |
1525 | case USB_ENDPOINT_XFER_CONTROL: | |
1526 | s = ""; break; | |
1527 | case USB_ENDPOINT_XFER_BULK: | |
1528 | s = "-bulk"; break; | |
1529 | case USB_ENDPOINT_XFER_INT: | |
1530 | s = "-intr"; break; | |
1531 | default: | |
1532 | s = "-iso"; break; | |
1533 | }; | |
1534 | s; | |
1535 | })); | |
1da177e4 LT |
1536 | usb_put_urb (urb); |
1537 | ||
1538 | /* list contents may have changed */ | |
ddc1fd6a | 1539 | spin_lock(&hcd_urb_list_lock); |
1da177e4 LT |
1540 | goto rescan; |
1541 | } | |
9a9bf406 | 1542 | spin_unlock_irq(&hcd_urb_list_lock); |
1da177e4 | 1543 | |
95cf82f9 | 1544 | /* Wait until the endpoint queue is completely empty */ |
455b25fb | 1545 | while (!list_empty (&ep->urb_list)) { |
809a58b8 | 1546 | spin_lock_irq(&hcd_urb_list_lock); |
455b25fb AS |
1547 | |
1548 | /* The list may have changed while we acquired the spinlock */ | |
1549 | urb = NULL; | |
1550 | if (!list_empty (&ep->urb_list)) { | |
1551 | urb = list_entry (ep->urb_list.prev, struct urb, | |
1552 | urb_list); | |
1553 | usb_get_urb (urb); | |
1554 | } | |
809a58b8 | 1555 | spin_unlock_irq(&hcd_urb_list_lock); |
455b25fb AS |
1556 | |
1557 | if (urb) { | |
1558 | usb_kill_urb (urb); | |
1559 | usb_put_urb (urb); | |
1560 | } | |
1561 | } | |
1da177e4 LT |
1562 | } |
1563 | ||
79abb1ab SS |
1564 | /* Check whether a new configuration or alt setting for an interface |
1565 | * will exceed the bandwidth for the bus (or the host controller resources). | |
1566 | * Only pass in a non-NULL config or interface, not both! | |
1567 | * Passing NULL for both new_config and new_intf means the device will be | |
1568 | * de-configured by issuing a set configuration 0 command. | |
1569 | */ | |
1570 | int usb_hcd_check_bandwidth(struct usb_device *udev, | |
1571 | struct usb_host_config *new_config, | |
1572 | struct usb_interface *new_intf) | |
1573 | { | |
1574 | int num_intfs, i, j; | |
1575 | struct usb_interface_cache *intf_cache; | |
1576 | struct usb_host_interface *alt = 0; | |
1577 | int ret = 0; | |
1578 | struct usb_hcd *hcd; | |
1579 | struct usb_host_endpoint *ep; | |
1580 | ||
1581 | hcd = bus_to_hcd(udev->bus); | |
1582 | if (!hcd->driver->check_bandwidth) | |
1583 | return 0; | |
1584 | ||
1585 | /* Configuration is being removed - set configuration 0 */ | |
1586 | if (!new_config && !new_intf) { | |
1587 | for (i = 1; i < 16; ++i) { | |
1588 | ep = udev->ep_out[i]; | |
1589 | if (ep) | |
1590 | hcd->driver->drop_endpoint(hcd, udev, ep); | |
1591 | ep = udev->ep_in[i]; | |
1592 | if (ep) | |
1593 | hcd->driver->drop_endpoint(hcd, udev, ep); | |
1594 | } | |
1595 | hcd->driver->check_bandwidth(hcd, udev); | |
1596 | return 0; | |
1597 | } | |
1598 | /* Check if the HCD says there's enough bandwidth. Enable all endpoints | |
1599 | * each interface's alt setting 0 and ask the HCD to check the bandwidth | |
1600 | * of the bus. There will always be bandwidth for endpoint 0, so it's | |
1601 | * ok to exclude it. | |
1602 | */ | |
1603 | if (new_config) { | |
1604 | num_intfs = new_config->desc.bNumInterfaces; | |
1605 | /* Remove endpoints (except endpoint 0, which is always on the | |
1606 | * schedule) from the old config from the schedule | |
1607 | */ | |
1608 | for (i = 1; i < 16; ++i) { | |
1609 | ep = udev->ep_out[i]; | |
1610 | if (ep) { | |
1611 | ret = hcd->driver->drop_endpoint(hcd, udev, ep); | |
1612 | if (ret < 0) | |
1613 | goto reset; | |
1614 | } | |
1615 | ep = udev->ep_in[i]; | |
1616 | if (ep) { | |
1617 | ret = hcd->driver->drop_endpoint(hcd, udev, ep); | |
1618 | if (ret < 0) | |
1619 | goto reset; | |
1620 | } | |
1621 | } | |
1622 | for (i = 0; i < num_intfs; ++i) { | |
1623 | ||
1624 | /* Dig the endpoints for alt setting 0 out of the | |
1625 | * interface cache for this interface | |
1626 | */ | |
1627 | intf_cache = new_config->intf_cache[i]; | |
1628 | for (j = 0; j < intf_cache->num_altsetting; j++) { | |
1629 | if (intf_cache->altsetting[j].desc.bAlternateSetting == 0) | |
1630 | alt = &intf_cache->altsetting[j]; | |
1631 | } | |
1632 | if (!alt) { | |
1633 | printk(KERN_DEBUG "Did not find alt setting 0 for intf %d\n", i); | |
1634 | continue; | |
1635 | } | |
1636 | for (j = 0; j < alt->desc.bNumEndpoints; j++) { | |
1637 | ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]); | |
1638 | if (ret < 0) | |
1639 | goto reset; | |
1640 | } | |
1641 | } | |
1642 | } | |
1643 | ret = hcd->driver->check_bandwidth(hcd, udev); | |
1644 | reset: | |
1645 | if (ret < 0) | |
1646 | hcd->driver->reset_bandwidth(hcd, udev); | |
1647 | return ret; | |
1648 | } | |
1649 | ||
95cf82f9 AS |
1650 | /* Disables the endpoint: synchronizes with the hcd to make sure all |
1651 | * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must | |
1652 | * have been called previously. Use for set_configuration, set_interface, | |
1653 | * driver removal, physical disconnect. | |
1654 | * | |
1655 | * example: a qh stored in ep->hcpriv, holding state related to endpoint | |
1656 | * type, maxpacket size, toggle, halt status, and scheduling. | |
1657 | */ | |
1658 | void usb_hcd_disable_endpoint(struct usb_device *udev, | |
1659 | struct usb_host_endpoint *ep) | |
1660 | { | |
1661 | struct usb_hcd *hcd; | |
1662 | ||
1663 | might_sleep(); | |
1664 | hcd = bus_to_hcd(udev->bus); | |
1665 | if (hcd->driver->endpoint_disable) | |
1666 | hcd->driver->endpoint_disable(hcd, ep); | |
1667 | } | |
1668 | ||
3444b26a DV |
1669 | /** |
1670 | * usb_hcd_reset_endpoint - reset host endpoint state | |
1671 | * @udev: USB device. | |
1672 | * @ep: the endpoint to reset. | |
1673 | * | |
1674 | * Resets any host endpoint state such as the toggle bit, sequence | |
1675 | * number and current window. | |
1676 | */ | |
1677 | void usb_hcd_reset_endpoint(struct usb_device *udev, | |
1678 | struct usb_host_endpoint *ep) | |
1679 | { | |
1680 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); | |
1681 | ||
1682 | if (hcd->driver->endpoint_reset) | |
1683 | hcd->driver->endpoint_reset(hcd, ep); | |
1684 | else { | |
1685 | int epnum = usb_endpoint_num(&ep->desc); | |
1686 | int is_out = usb_endpoint_dir_out(&ep->desc); | |
1687 | int is_control = usb_endpoint_xfer_control(&ep->desc); | |
1688 | ||
1689 | usb_settoggle(udev, epnum, is_out, 0); | |
1690 | if (is_control) | |
1691 | usb_settoggle(udev, epnum, !is_out, 0); | |
1692 | } | |
1693 | } | |
1694 | ||
cde217a5 AS |
1695 | /* Protect against drivers that try to unlink URBs after the device |
1696 | * is gone, by waiting until all unlinks for @udev are finished. | |
1697 | * Since we don't currently track URBs by device, simply wait until | |
1698 | * nothing is running in the locked region of usb_hcd_unlink_urb(). | |
1699 | */ | |
1700 | void usb_hcd_synchronize_unlinks(struct usb_device *udev) | |
1701 | { | |
1702 | spin_lock_irq(&hcd_urb_unlink_lock); | |
1703 | spin_unlock_irq(&hcd_urb_unlink_lock); | |
1704 | } | |
1705 | ||
1da177e4 LT |
1706 | /*-------------------------------------------------------------------------*/ |
1707 | ||
32aca560 AS |
1708 | /* called in any context */ |
1709 | int usb_hcd_get_frame_number (struct usb_device *udev) | |
1710 | { | |
1711 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); | |
1712 | ||
1713 | if (!HC_IS_RUNNING (hcd->state)) | |
1714 | return -ESHUTDOWN; | |
1715 | return hcd->driver->get_frame_number (hcd); | |
1716 | } | |
1717 | ||
1718 | /*-------------------------------------------------------------------------*/ | |
1719 | ||
9293677a | 1720 | #ifdef CONFIG_PM |
1da177e4 | 1721 | |
65bfd296 | 1722 | int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg) |
1da177e4 | 1723 | { |
686314cf AS |
1724 | struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self); |
1725 | int status; | |
1726 | int old_state = hcd->state; | |
1da177e4 | 1727 | |
686314cf | 1728 | dev_dbg(&rhdev->dev, "bus %s%s\n", |
65bfd296 | 1729 | (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "suspend"); |
686314cf AS |
1730 | if (!hcd->driver->bus_suspend) { |
1731 | status = -ENOENT; | |
1732 | } else { | |
1733 | hcd->state = HC_STATE_QUIESCING; | |
1734 | status = hcd->driver->bus_suspend(hcd); | |
1735 | } | |
1736 | if (status == 0) { | |
1737 | usb_set_device_state(rhdev, USB_STATE_SUSPENDED); | |
9293677a | 1738 | hcd->state = HC_STATE_SUSPENDED; |
686314cf AS |
1739 | } else { |
1740 | hcd->state = old_state; | |
1741 | dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", | |
9293677a | 1742 | "suspend", status); |
686314cf | 1743 | } |
9293677a | 1744 | return status; |
1da177e4 LT |
1745 | } |
1746 | ||
65bfd296 | 1747 | int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg) |
1da177e4 | 1748 | { |
686314cf AS |
1749 | struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self); |
1750 | int status; | |
cfa59dab | 1751 | int old_state = hcd->state; |
1da177e4 | 1752 | |
686314cf | 1753 | dev_dbg(&rhdev->dev, "usb %s%s\n", |
65bfd296 | 1754 | (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "resume"); |
0c0382e3 | 1755 | if (!hcd->driver->bus_resume) |
9293677a | 1756 | return -ENOENT; |
979d5199 DB |
1757 | if (hcd->state == HC_STATE_RUNNING) |
1758 | return 0; | |
686314cf | 1759 | |
9293677a | 1760 | hcd->state = HC_STATE_RESUMING; |
686314cf AS |
1761 | status = hcd->driver->bus_resume(hcd); |
1762 | if (status == 0) { | |
1763 | /* TRSMRCY = 10 msec */ | |
1764 | msleep(10); | |
1765 | usb_set_device_state(rhdev, rhdev->actconfig | |
1766 | ? USB_STATE_CONFIGURED | |
1767 | : USB_STATE_ADDRESS); | |
9293677a | 1768 | hcd->state = HC_STATE_RUNNING; |
686314cf | 1769 | } else { |
cfa59dab | 1770 | hcd->state = old_state; |
686314cf | 1771 | dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", |
9293677a | 1772 | "resume", status); |
cfa59dab AS |
1773 | if (status != -ESHUTDOWN) |
1774 | usb_hc_died(hcd); | |
9293677a DB |
1775 | } |
1776 | return status; | |
1da177e4 LT |
1777 | } |
1778 | ||
6b157c9b AS |
1779 | /* Workqueue routine for root-hub remote wakeup */ |
1780 | static void hcd_resume_work(struct work_struct *work) | |
1781 | { | |
1782 | struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work); | |
1783 | struct usb_device *udev = hcd->self.root_hub; | |
1784 | ||
1785 | usb_lock_device(udev); | |
1941044a | 1786 | usb_mark_last_busy(udev); |
65bfd296 | 1787 | usb_external_resume_device(udev, PMSG_REMOTE_RESUME); |
6b157c9b AS |
1788 | usb_unlock_device(udev); |
1789 | } | |
1790 | ||
1da177e4 LT |
1791 | /** |
1792 | * usb_hcd_resume_root_hub - called by HCD to resume its root hub | |
1793 | * @hcd: host controller for this root hub | |
1794 | * | |
1795 | * The USB host controller calls this function when its root hub is | |
1796 | * suspended (with the remote wakeup feature enabled) and a remote | |
6b157c9b AS |
1797 | * wakeup request is received. The routine submits a workqueue request |
1798 | * to resume the root hub (that is, manage its downstream ports again). | |
1da177e4 LT |
1799 | */ |
1800 | void usb_hcd_resume_root_hub (struct usb_hcd *hcd) | |
1801 | { | |
1802 | unsigned long flags; | |
1803 | ||
1804 | spin_lock_irqsave (&hcd_root_hub_lock, flags); | |
1805 | if (hcd->rh_registered) | |
6b157c9b | 1806 | queue_work(ksuspend_usb_wq, &hcd->wakeup_work); |
1da177e4 LT |
1807 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
1808 | } | |
9293677a | 1809 | EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); |
1da177e4 | 1810 | |
1da177e4 | 1811 | #endif |
1da177e4 LT |
1812 | |
1813 | /*-------------------------------------------------------------------------*/ | |
1814 | ||
1815 | #ifdef CONFIG_USB_OTG | |
1816 | ||
1817 | /** | |
1818 | * usb_bus_start_enum - start immediate enumeration (for OTG) | |
1819 | * @bus: the bus (must use hcd framework) | |
1820 | * @port_num: 1-based number of port; usually bus->otg_port | |
1821 | * Context: in_interrupt() | |
1822 | * | |
1823 | * Starts enumeration, with an immediate reset followed later by | |
1824 | * khubd identifying and possibly configuring the device. | |
1825 | * This is needed by OTG controller drivers, where it helps meet | |
1826 | * HNP protocol timing requirements for starting a port reset. | |
1827 | */ | |
1828 | int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num) | |
1829 | { | |
1830 | struct usb_hcd *hcd; | |
1831 | int status = -EOPNOTSUPP; | |
1832 | ||
1833 | /* NOTE: since HNP can't start by grabbing the bus's address0_sem, | |
1834 | * boards with root hubs hooked up to internal devices (instead of | |
1835 | * just the OTG port) may need more attention to resetting... | |
1836 | */ | |
1837 | hcd = container_of (bus, struct usb_hcd, self); | |
1838 | if (port_num && hcd->driver->start_port_reset) | |
1839 | status = hcd->driver->start_port_reset(hcd, port_num); | |
1840 | ||
1841 | /* run khubd shortly after (first) root port reset finishes; | |
1842 | * it may issue others, until at least 50 msecs have passed. | |
1843 | */ | |
1844 | if (status == 0) | |
1845 | mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10)); | |
1846 | return status; | |
1847 | } | |
782e70c6 | 1848 | EXPORT_SYMBOL_GPL(usb_bus_start_enum); |
1da177e4 LT |
1849 | |
1850 | #endif | |
1851 | ||
1852 | /*-------------------------------------------------------------------------*/ | |
1853 | ||
1da177e4 LT |
1854 | /** |
1855 | * usb_hcd_irq - hook IRQs to HCD framework (bus glue) | |
1856 | * @irq: the IRQ being raised | |
1857 | * @__hcd: pointer to the HCD whose IRQ is being signaled | |
1da177e4 LT |
1858 | * |
1859 | * If the controller isn't HALTed, calls the driver's irq handler. | |
1860 | * Checks whether the controller is now dead. | |
1861 | */ | |
7d12e780 | 1862 | irqreturn_t usb_hcd_irq (int irq, void *__hcd) |
1da177e4 LT |
1863 | { |
1864 | struct usb_hcd *hcd = __hcd; | |
de85422b SB |
1865 | unsigned long flags; |
1866 | irqreturn_t rc; | |
1da177e4 | 1867 | |
de85422b SB |
1868 | /* IRQF_DISABLED doesn't work correctly with shared IRQs |
1869 | * when the first handler doesn't use it. So let's just | |
1870 | * assume it's never used. | |
1871 | */ | |
1872 | local_irq_save(flags); | |
1da177e4 | 1873 | |
de85422b SB |
1874 | if (unlikely(hcd->state == HC_STATE_HALT || |
1875 | !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) { | |
1876 | rc = IRQ_NONE; | |
1877 | } else if (hcd->driver->irq(hcd) == IRQ_NONE) { | |
1878 | rc = IRQ_NONE; | |
1879 | } else { | |
1880 | set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); | |
8de98402 | 1881 | |
de85422b SB |
1882 | if (unlikely(hcd->state == HC_STATE_HALT)) |
1883 | usb_hc_died(hcd); | |
1884 | rc = IRQ_HANDLED; | |
1885 | } | |
1886 | ||
1887 | local_irq_restore(flags); | |
1888 | return rc; | |
1da177e4 LT |
1889 | } |
1890 | ||
1891 | /*-------------------------------------------------------------------------*/ | |
1892 | ||
1893 | /** | |
1894 | * usb_hc_died - report abnormal shutdown of a host controller (bus glue) | |
1895 | * @hcd: pointer to the HCD representing the controller | |
1896 | * | |
1897 | * This is called by bus glue to report a USB host controller that died | |
1898 | * while operations may still have been pending. It's called automatically | |
1899 | * by the PCI glue, so only glue for non-PCI busses should need to call it. | |
1900 | */ | |
1901 | void usb_hc_died (struct usb_hcd *hcd) | |
1902 | { | |
1903 | unsigned long flags; | |
1904 | ||
1905 | dev_err (hcd->self.controller, "HC died; cleaning up\n"); | |
1906 | ||
1907 | spin_lock_irqsave (&hcd_root_hub_lock, flags); | |
1908 | if (hcd->rh_registered) { | |
d5926ae7 | 1909 | hcd->poll_rh = 0; |
1da177e4 LT |
1910 | |
1911 | /* make khubd clean up old urbs and devices */ | |
1912 | usb_set_device_state (hcd->self.root_hub, | |
1913 | USB_STATE_NOTATTACHED); | |
1914 | usb_kick_khubd (hcd->self.root_hub); | |
1915 | } | |
1916 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); | |
1917 | } | |
1918 | EXPORT_SYMBOL_GPL (usb_hc_died); | |
1919 | ||
1920 | /*-------------------------------------------------------------------------*/ | |
1921 | ||
1da177e4 LT |
1922 | /** |
1923 | * usb_create_hcd - create and initialize an HCD structure | |
1924 | * @driver: HC driver that will use this hcd | |
1925 | * @dev: device for this HC, stored in hcd->self.controller | |
1926 | * @bus_name: value to store in hcd->self.bus_name | |
1927 | * Context: !in_interrupt() | |
1928 | * | |
1929 | * Allocate a struct usb_hcd, with extra space at the end for the | |
1930 | * HC driver's private data. Initialize the generic members of the | |
1931 | * hcd structure. | |
1932 | * | |
1933 | * If memory is unavailable, returns NULL. | |
1934 | */ | |
1935 | struct usb_hcd *usb_create_hcd (const struct hc_driver *driver, | |
1b26da15 | 1936 | struct device *dev, const char *bus_name) |
1da177e4 LT |
1937 | { |
1938 | struct usb_hcd *hcd; | |
1939 | ||
7b842b6e | 1940 | hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL); |
1da177e4 LT |
1941 | if (!hcd) { |
1942 | dev_dbg (dev, "hcd alloc failed\n"); | |
1943 | return NULL; | |
1944 | } | |
1945 | dev_set_drvdata(dev, hcd); | |
17200583 | 1946 | kref_init(&hcd->kref); |
1da177e4 LT |
1947 | |
1948 | usb_bus_init(&hcd->self); | |
1da177e4 LT |
1949 | hcd->self.controller = dev; |
1950 | hcd->self.bus_name = bus_name; | |
dd990f16 | 1951 | hcd->self.uses_dma = (dev->dma_mask != NULL); |
1da177e4 LT |
1952 | |
1953 | init_timer(&hcd->rh_timer); | |
d5926ae7 AS |
1954 | hcd->rh_timer.function = rh_timer_func; |
1955 | hcd->rh_timer.data = (unsigned long) hcd; | |
6b157c9b AS |
1956 | #ifdef CONFIG_PM |
1957 | INIT_WORK(&hcd->wakeup_work, hcd_resume_work); | |
1958 | #endif | |
1da177e4 LT |
1959 | |
1960 | hcd->driver = driver; | |
1961 | hcd->product_desc = (driver->product_desc) ? driver->product_desc : | |
1962 | "USB Host Controller"; | |
1da177e4 LT |
1963 | return hcd; |
1964 | } | |
782e70c6 | 1965 | EXPORT_SYMBOL_GPL(usb_create_hcd); |
1da177e4 | 1966 | |
17200583 AS |
1967 | static void hcd_release (struct kref *kref) |
1968 | { | |
1969 | struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref); | |
1970 | ||
1971 | kfree(hcd); | |
1972 | } | |
1973 | ||
1974 | struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd) | |
1975 | { | |
1976 | if (hcd) | |
1977 | kref_get (&hcd->kref); | |
1978 | return hcd; | |
1979 | } | |
782e70c6 | 1980 | EXPORT_SYMBOL_GPL(usb_get_hcd); |
17200583 | 1981 | |
1da177e4 LT |
1982 | void usb_put_hcd (struct usb_hcd *hcd) |
1983 | { | |
17200583 AS |
1984 | if (hcd) |
1985 | kref_put (&hcd->kref, hcd_release); | |
1da177e4 | 1986 | } |
782e70c6 | 1987 | EXPORT_SYMBOL_GPL(usb_put_hcd); |
1da177e4 LT |
1988 | |
1989 | /** | |
1990 | * usb_add_hcd - finish generic HCD structure initialization and register | |
1991 | * @hcd: the usb_hcd structure to initialize | |
1992 | * @irqnum: Interrupt line to allocate | |
1993 | * @irqflags: Interrupt type flags | |
1994 | * | |
1995 | * Finish the remaining parts of generic HCD initialization: allocate the | |
1996 | * buffers of consistent memory, register the bus, request the IRQ line, | |
1997 | * and call the driver's reset() and start() routines. | |
1998 | */ | |
1999 | int usb_add_hcd(struct usb_hcd *hcd, | |
2000 | unsigned int irqnum, unsigned long irqflags) | |
2001 | { | |
8ec8d20b AS |
2002 | int retval; |
2003 | struct usb_device *rhdev; | |
1da177e4 LT |
2004 | |
2005 | dev_info(hcd->self.controller, "%s\n", hcd->product_desc); | |
2006 | ||
5234ce1b | 2007 | hcd->authorized_default = hcd->wireless? 0 : 1; |
8de98402 BH |
2008 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
2009 | ||
b1e8f0a6 DB |
2010 | /* HC is in reset state, but accessible. Now do the one-time init, |
2011 | * bottom up so that hcds can customize the root hubs before khubd | |
2012 | * starts talking to them. (Note, bus id is assigned early too.) | |
2013 | */ | |
1da177e4 LT |
2014 | if ((retval = hcd_buffer_create(hcd)) != 0) { |
2015 | dev_dbg(hcd->self.controller, "pool alloc failed\n"); | |
2016 | return retval; | |
2017 | } | |
2018 | ||
2019 | if ((retval = usb_register_bus(&hcd->self)) < 0) | |
8ec8d20b | 2020 | goto err_register_bus; |
1da177e4 | 2021 | |
b1e8f0a6 DB |
2022 | if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) { |
2023 | dev_err(hcd->self.controller, "unable to allocate root hub\n"); | |
2024 | retval = -ENOMEM; | |
2025 | goto err_allocate_root_hub; | |
2026 | } | |
6b403b02 SS |
2027 | |
2028 | switch (hcd->driver->flags & HCD_MASK) { | |
2029 | case HCD_USB11: | |
2030 | rhdev->speed = USB_SPEED_FULL; | |
2031 | break; | |
2032 | case HCD_USB2: | |
2033 | rhdev->speed = USB_SPEED_HIGH; | |
2034 | break; | |
2035 | case HCD_USB3: | |
2036 | rhdev->speed = USB_SPEED_SUPER; | |
2037 | break; | |
2038 | default: | |
2039 | goto err_allocate_root_hub; | |
2040 | } | |
b1e8f0a6 DB |
2041 | hcd->self.root_hub = rhdev; |
2042 | ||
db4cefaa DB |
2043 | /* wakeup flag init defaults to "everything works" for root hubs, |
2044 | * but drivers can override it in reset() if needed, along with | |
2045 | * recording the overall controller's system wakeup capability. | |
2046 | */ | |
2047 | device_init_wakeup(&rhdev->dev, 1); | |
2048 | ||
b1e8f0a6 DB |
2049 | /* "reset" is misnamed; its role is now one-time init. the controller |
2050 | * should already have been reset (and boot firmware kicked off etc). | |
2051 | */ | |
2052 | if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) { | |
2053 | dev_err(hcd->self.controller, "can't setup\n"); | |
2054 | goto err_hcd_driver_setup; | |
2055 | } | |
2056 | ||
fb669cc0 DB |
2057 | /* NOTE: root hub and controller capabilities may not be the same */ |
2058 | if (device_can_wakeup(hcd->self.controller) | |
2059 | && device_can_wakeup(&hcd->self.root_hub->dev)) | |
b1e8f0a6 | 2060 | dev_dbg(hcd->self.controller, "supports USB remote wakeup\n"); |
b1e8f0a6 DB |
2061 | |
2062 | /* enable irqs just before we start the controller */ | |
1da177e4 | 2063 | if (hcd->driver->irq) { |
de85422b SB |
2064 | |
2065 | /* IRQF_DISABLED doesn't work as advertised when used together | |
2066 | * with IRQF_SHARED. As usb_hcd_irq() will always disable | |
2067 | * interrupts we can remove it here. | |
2068 | */ | |
83a79820 GL |
2069 | if (irqflags & IRQF_SHARED) |
2070 | irqflags &= ~IRQF_DISABLED; | |
de85422b | 2071 | |
1da177e4 LT |
2072 | snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d", |
2073 | hcd->driver->description, hcd->self.busnum); | |
2074 | if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags, | |
2075 | hcd->irq_descr, hcd)) != 0) { | |
2076 | dev_err(hcd->self.controller, | |
c6387a48 | 2077 | "request interrupt %d failed\n", irqnum); |
8ec8d20b | 2078 | goto err_request_irq; |
1da177e4 LT |
2079 | } |
2080 | hcd->irq = irqnum; | |
c6387a48 | 2081 | dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum, |
1da177e4 LT |
2082 | (hcd->driver->flags & HCD_MEMORY) ? |
2083 | "io mem" : "io base", | |
2084 | (unsigned long long)hcd->rsrc_start); | |
2085 | } else { | |
2086 | hcd->irq = -1; | |
2087 | if (hcd->rsrc_start) | |
2088 | dev_info(hcd->self.controller, "%s 0x%08llx\n", | |
2089 | (hcd->driver->flags & HCD_MEMORY) ? | |
2090 | "io mem" : "io base", | |
2091 | (unsigned long long)hcd->rsrc_start); | |
2092 | } | |
2093 | ||
2094 | if ((retval = hcd->driver->start(hcd)) < 0) { | |
2095 | dev_err(hcd->self.controller, "startup error %d\n", retval); | |
8ec8d20b | 2096 | goto err_hcd_driver_start; |
1da177e4 LT |
2097 | } |
2098 | ||
b1e8f0a6 | 2099 | /* starting here, usbcore will pay attention to this root hub */ |
55c52718 | 2100 | rhdev->bus_mA = min(500u, hcd->power_budget); |
b1e8f0a6 | 2101 | if ((retval = register_root_hub(hcd)) != 0) |
8ec8d20b AS |
2102 | goto err_register_root_hub; |
2103 | ||
5234ce1b IPG |
2104 | retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group); |
2105 | if (retval < 0) { | |
2106 | printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n", | |
2107 | retval); | |
2108 | goto error_create_attr_group; | |
2109 | } | |
d5926ae7 AS |
2110 | if (hcd->uses_new_polling && hcd->poll_rh) |
2111 | usb_hcd_poll_rh_status(hcd); | |
1da177e4 LT |
2112 | return retval; |
2113 | ||
5234ce1b IPG |
2114 | error_create_attr_group: |
2115 | mutex_lock(&usb_bus_list_lock); | |
2116 | usb_disconnect(&hcd->self.root_hub); | |
2117 | mutex_unlock(&usb_bus_list_lock); | |
b1e8f0a6 | 2118 | err_register_root_hub: |
8ec8d20b | 2119 | hcd->driver->stop(hcd); |
b1e8f0a6 | 2120 | err_hcd_driver_start: |
1da177e4 LT |
2121 | if (hcd->irq >= 0) |
2122 | free_irq(irqnum, hcd); | |
b1e8f0a6 DB |
2123 | err_request_irq: |
2124 | err_hcd_driver_setup: | |
2125 | hcd->self.root_hub = NULL; | |
2126 | usb_put_dev(rhdev); | |
2127 | err_allocate_root_hub: | |
1da177e4 | 2128 | usb_deregister_bus(&hcd->self); |
b1e8f0a6 | 2129 | err_register_bus: |
1da177e4 LT |
2130 | hcd_buffer_destroy(hcd); |
2131 | return retval; | |
2132 | } | |
782e70c6 | 2133 | EXPORT_SYMBOL_GPL(usb_add_hcd); |
1da177e4 LT |
2134 | |
2135 | /** | |
2136 | * usb_remove_hcd - shutdown processing for generic HCDs | |
2137 | * @hcd: the usb_hcd structure to remove | |
2138 | * Context: !in_interrupt() | |
2139 | * | |
2140 | * Disconnects the root hub, then reverses the effects of usb_add_hcd(), | |
2141 | * invoking the HCD's stop() method. | |
2142 | */ | |
2143 | void usb_remove_hcd(struct usb_hcd *hcd) | |
2144 | { | |
2145 | dev_info(hcd->self.controller, "remove, state %x\n", hcd->state); | |
2146 | ||
2147 | if (HC_IS_RUNNING (hcd->state)) | |
2148 | hcd->state = HC_STATE_QUIESCING; | |
2149 | ||
2150 | dev_dbg(hcd->self.controller, "roothub graceful disconnect\n"); | |
2151 | spin_lock_irq (&hcd_root_hub_lock); | |
2152 | hcd->rh_registered = 0; | |
2153 | spin_unlock_irq (&hcd_root_hub_lock); | |
9ad3d6cc | 2154 | |
6b157c9b | 2155 | #ifdef CONFIG_PM |
d5d4db70 | 2156 | cancel_work_sync(&hcd->wakeup_work); |
6b157c9b AS |
2157 | #endif |
2158 | ||
5234ce1b | 2159 | sysfs_remove_group(&hcd->self.root_hub->dev.kobj, &usb_bus_attr_group); |
4186ecf8 | 2160 | mutex_lock(&usb_bus_list_lock); |
1da177e4 | 2161 | usb_disconnect(&hcd->self.root_hub); |
4186ecf8 | 2162 | mutex_unlock(&usb_bus_list_lock); |
1da177e4 LT |
2163 | |
2164 | hcd->driver->stop(hcd); | |
2165 | hcd->state = HC_STATE_HALT; | |
2166 | ||
1b42ae6d AS |
2167 | hcd->poll_rh = 0; |
2168 | del_timer_sync(&hcd->rh_timer); | |
2169 | ||
1da177e4 LT |
2170 | if (hcd->irq >= 0) |
2171 | free_irq(hcd->irq, hcd); | |
2172 | usb_deregister_bus(&hcd->self); | |
2173 | hcd_buffer_destroy(hcd); | |
2174 | } | |
782e70c6 | 2175 | EXPORT_SYMBOL_GPL(usb_remove_hcd); |
1da177e4 | 2176 | |
64a21d02 AG |
2177 | void |
2178 | usb_hcd_platform_shutdown(struct platform_device* dev) | |
2179 | { | |
2180 | struct usb_hcd *hcd = platform_get_drvdata(dev); | |
2181 | ||
2182 | if (hcd->driver->shutdown) | |
2183 | hcd->driver->shutdown(hcd); | |
2184 | } | |
782e70c6 | 2185 | EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown); |
64a21d02 | 2186 | |
1da177e4 LT |
2187 | /*-------------------------------------------------------------------------*/ |
2188 | ||
f150fa1a | 2189 | #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE) |
1da177e4 LT |
2190 | |
2191 | struct usb_mon_operations *mon_ops; | |
2192 | ||
2193 | /* | |
2194 | * The registration is unlocked. | |
2195 | * We do it this way because we do not want to lock in hot paths. | |
2196 | * | |
2197 | * Notice that the code is minimally error-proof. Because usbmon needs | |
2198 | * symbols from usbcore, usbcore gets referenced and cannot be unloaded first. | |
2199 | */ | |
2200 | ||
2201 | int usb_mon_register (struct usb_mon_operations *ops) | |
2202 | { | |
2203 | ||
2204 | if (mon_ops) | |
2205 | return -EBUSY; | |
2206 | ||
2207 | mon_ops = ops; | |
2208 | mb(); | |
2209 | return 0; | |
2210 | } | |
2211 | EXPORT_SYMBOL_GPL (usb_mon_register); | |
2212 | ||
2213 | void usb_mon_deregister (void) | |
2214 | { | |
2215 | ||
2216 | if (mon_ops == NULL) { | |
2217 | printk(KERN_ERR "USB: monitor was not registered\n"); | |
2218 | return; | |
2219 | } | |
2220 | mon_ops = NULL; | |
2221 | mb(); | |
2222 | } | |
2223 | EXPORT_SYMBOL_GPL (usb_mon_deregister); | |
2224 | ||
f150fa1a | 2225 | #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */ |