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 | |
14557359 | 9 | * |
1da177e4 LT |
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 | ||
b53d657d | 25 | #include <linux/bcd.h> |
1da177e4 LT |
26 | #include <linux/module.h> |
27 | #include <linux/version.h> | |
28 | #include <linux/kernel.h> | |
29 | #include <linux/slab.h> | |
30 | #include <linux/completion.h> | |
31 | #include <linux/utsname.h> | |
32 | #include <linux/mm.h> | |
33 | #include <asm/io.h> | |
1da177e4 LT |
34 | #include <linux/device.h> |
35 | #include <linux/dma-mapping.h> | |
4186ecf8 | 36 | #include <linux/mutex.h> |
1da177e4 LT |
37 | #include <asm/irq.h> |
38 | #include <asm/byteorder.h> | |
b3476675 | 39 | #include <asm/unaligned.h> |
64a21d02 | 40 | #include <linux/platform_device.h> |
6b157c9b | 41 | #include <linux/workqueue.h> |
da0aa716 | 42 | #include <linux/pm_runtime.h> |
059239ad | 43 | #include <linux/types.h> |
1da177e4 | 44 | |
00433254 | 45 | #include <linux/phy/phy.h> |
1da177e4 | 46 | #include <linux/usb.h> |
27729aad | 47 | #include <linux/usb/hcd.h> |
103e127d | 48 | #include <linux/usb/phy.h> |
1da177e4 LT |
49 | |
50 | #include "usb.h" | |
1da177e4 LT |
51 | |
52 | ||
1da177e4 LT |
53 | /*-------------------------------------------------------------------------*/ |
54 | ||
55 | /* | |
56 | * USB Host Controller Driver framework | |
57 | * | |
58 | * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing | |
59 | * HCD-specific behaviors/bugs. | |
60 | * | |
61 | * This does error checks, tracks devices and urbs, and delegates to a | |
62 | * "hc_driver" only for code (and data) that really needs to know about | |
63 | * hardware differences. That includes root hub registers, i/o queues, | |
64 | * and so on ... but as little else as possible. | |
65 | * | |
66 | * Shared code includes most of the "root hub" code (these are emulated, | |
67 | * though each HC's hardware works differently) and PCI glue, plus request | |
68 | * tracking overhead. The HCD code should only block on spinlocks or on | |
69 | * hardware handshaking; blocking on software events (such as other kernel | |
70 | * threads releasing resources, or completing actions) is all generic. | |
71 | * | |
72 | * Happens the USB 2.0 spec says this would be invisible inside the "USBD", | |
73 | * and includes mostly a "HCDI" (HCD Interface) along with some APIs used | |
74 | * only by the hub driver ... and that neither should be seen or used by | |
75 | * usb client device drivers. | |
76 | * | |
77 | * Contributors of ideas or unattributed patches include: David Brownell, | |
78 | * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ... | |
79 | * | |
80 | * HISTORY: | |
81 | * 2002-02-21 Pull in most of the usb_bus support from usb.c; some | |
82 | * associated cleanup. "usb_hcd" still != "usb_bus". | |
83 | * 2001-12-12 Initial patch version for Linux 2.5.1 kernel. | |
84 | */ | |
85 | ||
86 | /*-------------------------------------------------------------------------*/ | |
87 | ||
9beeee65 AS |
88 | /* Keep track of which host controller drivers are loaded */ |
89 | unsigned long usb_hcds_loaded; | |
90 | EXPORT_SYMBOL_GPL(usb_hcds_loaded); | |
91 | ||
1da177e4 LT |
92 | /* host controllers we manage */ |
93 | LIST_HEAD (usb_bus_list); | |
94 | EXPORT_SYMBOL_GPL (usb_bus_list); | |
95 | ||
96 | /* used when allocating bus numbers */ | |
97 | #define USB_MAXBUS 64 | |
059239ad | 98 | static DECLARE_BITMAP(busmap, USB_MAXBUS); |
1da177e4 LT |
99 | |
100 | /* used when updating list of hcds */ | |
4186ecf8 | 101 | DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */ |
1da177e4 LT |
102 | EXPORT_SYMBOL_GPL (usb_bus_list_lock); |
103 | ||
104 | /* used for controlling access to virtual root hubs */ | |
105 | static DEFINE_SPINLOCK(hcd_root_hub_lock); | |
106 | ||
809a58b8 AS |
107 | /* used when updating an endpoint's URB list */ |
108 | static DEFINE_SPINLOCK(hcd_urb_list_lock); | |
1da177e4 | 109 | |
cde217a5 AS |
110 | /* used to protect against unlinking URBs after the device is gone */ |
111 | static DEFINE_SPINLOCK(hcd_urb_unlink_lock); | |
112 | ||
1da177e4 LT |
113 | /* wait queue for synchronous unlinks */ |
114 | DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue); | |
115 | ||
809a58b8 AS |
116 | static inline int is_root_hub(struct usb_device *udev) |
117 | { | |
118 | return (udev->parent == NULL); | |
119 | } | |
120 | ||
1da177e4 LT |
121 | /*-------------------------------------------------------------------------*/ |
122 | ||
123 | /* | |
124 | * Sharable chunks of root hub code. | |
125 | */ | |
126 | ||
127 | /*-------------------------------------------------------------------------*/ | |
b53d657d SAS |
128 | #define KERNEL_REL bin2bcd(((LINUX_VERSION_CODE >> 16) & 0x0ff)) |
129 | #define KERNEL_VER bin2bcd(((LINUX_VERSION_CODE >> 8) & 0x0ff)) | |
1da177e4 | 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 | ||
bfb8bfad | 142 | 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ |
cd780694 | 143 | 0x03, 0x00, /* __le16 idProduct; device 0x0003 */ |
d2e9b4d6 SS |
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 | ||
1a81f881 TP |
152 | /* usb 2.5 (wireless USB 1.0) root hub device descriptor */ |
153 | static const u8 usb25_rh_dev_descriptor[18] = { | |
154 | 0x12, /* __u8 bLength; */ | |
155 | 0x01, /* __u8 bDescriptorType; Device */ | |
156 | 0x50, 0x02, /* __le16 bcdUSB; v2.5 */ | |
157 | ||
158 | 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ | |
159 | 0x00, /* __u8 bDeviceSubClass; */ | |
160 | 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */ | |
161 | 0xFF, /* __u8 bMaxPacketSize0; always 0xFF (WUSB Spec 7.4.1). */ | |
162 | ||
163 | 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ | |
164 | 0x02, 0x00, /* __le16 idProduct; device 0x0002 */ | |
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 | ||
1da177e4 | 173 | /* usb 2.0 root hub device descriptor */ |
14557359 | 174 | static const u8 usb2_rh_dev_descriptor[18] = { |
1da177e4 LT |
175 | 0x12, /* __u8 bLength; */ |
176 | 0x01, /* __u8 bDescriptorType; Device */ | |
177 | 0x00, 0x02, /* __le16 bcdUSB; v2.0 */ | |
178 | ||
179 | 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ | |
180 | 0x00, /* __u8 bDeviceSubClass; */ | |
7329e211 | 181 | 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */ |
16f16d11 | 182 | 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ |
1da177e4 | 183 | |
bfb8bfad | 184 | 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ |
667d691e | 185 | 0x02, 0x00, /* __le16 idProduct; device 0x0002 */ |
1da177e4 LT |
186 | KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
187 | ||
188 | 0x03, /* __u8 iManufacturer; */ | |
189 | 0x02, /* __u8 iProduct; */ | |
190 | 0x01, /* __u8 iSerialNumber; */ | |
191 | 0x01 /* __u8 bNumConfigurations; */ | |
192 | }; | |
193 | ||
194 | /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */ | |
195 | ||
196 | /* usb 1.1 root hub device descriptor */ | |
14557359 | 197 | static const u8 usb11_rh_dev_descriptor[18] = { |
1da177e4 LT |
198 | 0x12, /* __u8 bLength; */ |
199 | 0x01, /* __u8 bDescriptorType; Device */ | |
200 | 0x10, 0x01, /* __le16 bcdUSB; v1.1 */ | |
201 | ||
202 | 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ | |
203 | 0x00, /* __u8 bDeviceSubClass; */ | |
204 | 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */ | |
16f16d11 | 205 | 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */ |
1da177e4 | 206 | |
bfb8bfad | 207 | 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation 0x1d6b */ |
667d691e | 208 | 0x01, 0x00, /* __le16 idProduct; device 0x0001 */ |
1da177e4 LT |
209 | KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */ |
210 | ||
211 | 0x03, /* __u8 iManufacturer; */ | |
212 | 0x02, /* __u8 iProduct; */ | |
213 | 0x01, /* __u8 iSerialNumber; */ | |
214 | 0x01 /* __u8 bNumConfigurations; */ | |
215 | }; | |
216 | ||
217 | ||
218 | /*-------------------------------------------------------------------------*/ | |
219 | ||
220 | /* Configuration descriptors for our root hubs */ | |
221 | ||
14557359 | 222 | static const u8 fs_rh_config_descriptor[] = { |
1da177e4 LT |
223 | |
224 | /* one configuration */ | |
225 | 0x09, /* __u8 bLength; */ | |
226 | 0x02, /* __u8 bDescriptorType; Configuration */ | |
227 | 0x19, 0x00, /* __le16 wTotalLength; */ | |
228 | 0x01, /* __u8 bNumInterfaces; (1) */ | |
229 | 0x01, /* __u8 bConfigurationValue; */ | |
230 | 0x00, /* __u8 iConfiguration; */ | |
14557359 | 231 | 0xc0, /* __u8 bmAttributes; |
1da177e4 LT |
232 | Bit 7: must be set, |
233 | 6: Self-powered, | |
234 | 5: Remote wakeup, | |
235 | 4..0: resvd */ | |
236 | 0x00, /* __u8 MaxPower; */ | |
14557359 | 237 | |
1da177e4 LT |
238 | /* USB 1.1: |
239 | * USB 2.0, single TT organization (mandatory): | |
240 | * one interface, protocol 0 | |
241 | * | |
242 | * USB 2.0, multiple TT organization (optional): | |
243 | * two interfaces, protocols 1 (like single TT) | |
244 | * and 2 (multiple TT mode) ... config is | |
245 | * sometimes settable | |
246 | * NOT IMPLEMENTED | |
247 | */ | |
248 | ||
249 | /* one interface */ | |
250 | 0x09, /* __u8 if_bLength; */ | |
251 | 0x04, /* __u8 if_bDescriptorType; Interface */ | |
252 | 0x00, /* __u8 if_bInterfaceNumber; */ | |
253 | 0x00, /* __u8 if_bAlternateSetting; */ | |
254 | 0x01, /* __u8 if_bNumEndpoints; */ | |
255 | 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ | |
256 | 0x00, /* __u8 if_bInterfaceSubClass; */ | |
257 | 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ | |
258 | 0x00, /* __u8 if_iInterface; */ | |
14557359 | 259 | |
1da177e4 LT |
260 | /* one endpoint (status change endpoint) */ |
261 | 0x07, /* __u8 ep_bLength; */ | |
262 | 0x05, /* __u8 ep_bDescriptorType; Endpoint */ | |
263 | 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ | |
14557359 MB |
264 | 0x03, /* __u8 ep_bmAttributes; Interrupt */ |
265 | 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */ | |
1da177e4 LT |
266 | 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */ |
267 | }; | |
268 | ||
14557359 | 269 | static const u8 hs_rh_config_descriptor[] = { |
1da177e4 LT |
270 | |
271 | /* one configuration */ | |
272 | 0x09, /* __u8 bLength; */ | |
273 | 0x02, /* __u8 bDescriptorType; Configuration */ | |
274 | 0x19, 0x00, /* __le16 wTotalLength; */ | |
275 | 0x01, /* __u8 bNumInterfaces; (1) */ | |
276 | 0x01, /* __u8 bConfigurationValue; */ | |
277 | 0x00, /* __u8 iConfiguration; */ | |
14557359 | 278 | 0xc0, /* __u8 bmAttributes; |
1da177e4 LT |
279 | Bit 7: must be set, |
280 | 6: Self-powered, | |
281 | 5: Remote wakeup, | |
282 | 4..0: resvd */ | |
283 | 0x00, /* __u8 MaxPower; */ | |
14557359 | 284 | |
1da177e4 LT |
285 | /* USB 1.1: |
286 | * USB 2.0, single TT organization (mandatory): | |
287 | * one interface, protocol 0 | |
288 | * | |
289 | * USB 2.0, multiple TT organization (optional): | |
290 | * two interfaces, protocols 1 (like single TT) | |
291 | * and 2 (multiple TT mode) ... config is | |
292 | * sometimes settable | |
293 | * NOT IMPLEMENTED | |
294 | */ | |
295 | ||
296 | /* one interface */ | |
297 | 0x09, /* __u8 if_bLength; */ | |
298 | 0x04, /* __u8 if_bDescriptorType; Interface */ | |
299 | 0x00, /* __u8 if_bInterfaceNumber; */ | |
300 | 0x00, /* __u8 if_bAlternateSetting; */ | |
301 | 0x01, /* __u8 if_bNumEndpoints; */ | |
302 | 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ | |
303 | 0x00, /* __u8 if_bInterfaceSubClass; */ | |
304 | 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */ | |
305 | 0x00, /* __u8 if_iInterface; */ | |
14557359 | 306 | |
1da177e4 LT |
307 | /* one endpoint (status change endpoint) */ |
308 | 0x07, /* __u8 ep_bLength; */ | |
309 | 0x05, /* __u8 ep_bDescriptorType; Endpoint */ | |
310 | 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ | |
14557359 | 311 | 0x03, /* __u8 ep_bmAttributes; Interrupt */ |
88fafff9 | 312 | /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) |
313 | * see hub.c:hub_configure() for details. */ | |
314 | (USB_MAXCHILDREN + 1 + 7) / 8, 0x00, | |
1da177e4 LT |
315 | 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ |
316 | }; | |
317 | ||
d2e9b4d6 SS |
318 | static const u8 ss_rh_config_descriptor[] = { |
319 | /* one configuration */ | |
320 | 0x09, /* __u8 bLength; */ | |
321 | 0x02, /* __u8 bDescriptorType; Configuration */ | |
22c6a35d | 322 | 0x1f, 0x00, /* __le16 wTotalLength; */ |
d2e9b4d6 SS |
323 | 0x01, /* __u8 bNumInterfaces; (1) */ |
324 | 0x01, /* __u8 bConfigurationValue; */ | |
325 | 0x00, /* __u8 iConfiguration; */ | |
326 | 0xc0, /* __u8 bmAttributes; | |
327 | Bit 7: must be set, | |
328 | 6: Self-powered, | |
329 | 5: Remote wakeup, | |
330 | 4..0: resvd */ | |
331 | 0x00, /* __u8 MaxPower; */ | |
332 | ||
333 | /* one interface */ | |
334 | 0x09, /* __u8 if_bLength; */ | |
335 | 0x04, /* __u8 if_bDescriptorType; Interface */ | |
336 | 0x00, /* __u8 if_bInterfaceNumber; */ | |
337 | 0x00, /* __u8 if_bAlternateSetting; */ | |
338 | 0x01, /* __u8 if_bNumEndpoints; */ | |
339 | 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ | |
340 | 0x00, /* __u8 if_bInterfaceSubClass; */ | |
341 | 0x00, /* __u8 if_bInterfaceProtocol; */ | |
342 | 0x00, /* __u8 if_iInterface; */ | |
343 | ||
344 | /* one endpoint (status change endpoint) */ | |
345 | 0x07, /* __u8 ep_bLength; */ | |
346 | 0x05, /* __u8 ep_bDescriptorType; Endpoint */ | |
347 | 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ | |
348 | 0x03, /* __u8 ep_bmAttributes; Interrupt */ | |
349 | /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) | |
350 | * see hub.c:hub_configure() for details. */ | |
351 | (USB_MAXCHILDREN + 1 + 7) / 8, 0x00, | |
22c6a35d SS |
352 | 0x0c, /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */ |
353 | ||
354 | /* one SuperSpeed endpoint companion descriptor */ | |
355 | 0x06, /* __u8 ss_bLength */ | |
356 | 0x30, /* __u8 ss_bDescriptorType; SuperSpeed EP Companion */ | |
357 | 0x00, /* __u8 ss_bMaxBurst; allows 1 TX between ACKs */ | |
358 | 0x00, /* __u8 ss_bmAttributes; 1 packet per service interval */ | |
359 | 0x02, 0x00 /* __le16 ss_wBytesPerInterval; 15 bits for max 15 ports */ | |
d2e9b4d6 SS |
360 | }; |
361 | ||
c4fc2342 CDH |
362 | /* authorized_default behaviour: |
363 | * -1 is authorized for all devices except wireless (old behaviour) | |
364 | * 0 is unauthorized for all devices | |
365 | * 1 is authorized for all devices | |
366 | */ | |
367 | static int authorized_default = -1; | |
368 | module_param(authorized_default, int, S_IRUGO|S_IWUSR); | |
369 | MODULE_PARM_DESC(authorized_default, | |
370 | "Default USB device authorization: 0 is not authorized, 1 is " | |
371 | "authorized, -1 is authorized except for wireless USB (default, " | |
372 | "old behaviour"); | |
1da177e4 LT |
373 | /*-------------------------------------------------------------------------*/ |
374 | ||
392ca68b GS |
375 | /** |
376 | * ascii2desc() - Helper routine for producing UTF-16LE string descriptors | |
377 | * @s: Null-terminated ASCII (actually ISO-8859-1) string | |
378 | * @buf: Buffer for USB string descriptor (header + UTF-16LE) | |
379 | * @len: Length (in bytes; may be odd) of descriptor buffer. | |
380 | * | |
626f090c YB |
381 | * Return: The number of bytes filled in: 2 + 2*strlen(s) or @len, |
382 | * whichever is less. | |
392ca68b | 383 | * |
626f090c | 384 | * Note: |
392ca68b GS |
385 | * USB String descriptors can contain at most 126 characters; input |
386 | * strings longer than that are truncated. | |
1da177e4 | 387 | */ |
392ca68b GS |
388 | static unsigned |
389 | ascii2desc(char const *s, u8 *buf, unsigned len) | |
1da177e4 | 390 | { |
392ca68b GS |
391 | unsigned n, t = 2 + 2*strlen(s); |
392 | ||
393 | if (t > 254) | |
394 | t = 254; /* Longest possible UTF string descriptor */ | |
395 | if (len > t) | |
396 | len = t; | |
397 | ||
398 | t += USB_DT_STRING << 8; /* Now t is first 16 bits to store */ | |
399 | ||
400 | n = len; | |
401 | while (n--) { | |
402 | *buf++ = t; | |
403 | if (!n--) | |
404 | break; | |
405 | *buf++ = t >> 8; | |
406 | t = (unsigned char)*s++; | |
1da177e4 | 407 | } |
392ca68b | 408 | return len; |
1da177e4 LT |
409 | } |
410 | ||
392ca68b GS |
411 | /** |
412 | * rh_string() - provides string descriptors for root hub | |
413 | * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor) | |
1da177e4 | 414 | * @hcd: the host controller for this root hub |
392ca68b GS |
415 | * @data: buffer for output packet |
416 | * @len: length of the provided buffer | |
1da177e4 LT |
417 | * |
418 | * Produces either a manufacturer, product or serial number string for the | |
419 | * virtual root hub device. | |
626f090c YB |
420 | * |
421 | * Return: The number of bytes filled in: the length of the descriptor or | |
392ca68b | 422 | * of the provided buffer, whichever is less. |
1da177e4 | 423 | */ |
392ca68b GS |
424 | static unsigned |
425 | rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len) | |
71d2718f | 426 | { |
392ca68b GS |
427 | char buf[100]; |
428 | char const *s; | |
429 | static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04}; | |
1da177e4 | 430 | |
048cb21c | 431 | /* language ids */ |
392ca68b GS |
432 | switch (id) { |
433 | case 0: | |
434 | /* Array of LANGID codes (0x0409 is MSFT-speak for "en-us") */ | |
435 | /* See http://www.usb.org/developers/docs/USB_LANGIDs.pdf */ | |
436 | if (len > 4) | |
437 | len = 4; | |
438 | memcpy(data, langids, len); | |
1da177e4 | 439 | return len; |
392ca68b GS |
440 | case 1: |
441 | /* Serial number */ | |
442 | s = hcd->self.bus_name; | |
443 | break; | |
444 | case 2: | |
445 | /* Product name */ | |
446 | s = hcd->product_desc; | |
447 | break; | |
448 | case 3: | |
449 | /* Manufacturer */ | |
96b644bd SH |
450 | snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname, |
451 | init_utsname()->release, hcd->driver->description); | |
392ca68b GS |
452 | s = buf; |
453 | break; | |
1da177e4 | 454 | default: |
392ca68b GS |
455 | /* Can't happen; caller guarantees it */ |
456 | return 0; | |
1da177e4 | 457 | } |
392ca68b GS |
458 | |
459 | return ascii2desc(s, data, len); | |
1da177e4 LT |
460 | } |
461 | ||
462 | ||
463 | /* Root hub control transfers execute synchronously */ | |
464 | static int rh_call_control (struct usb_hcd *hcd, struct urb *urb) | |
465 | { | |
466 | struct usb_ctrlrequest *cmd; | |
14557359 | 467 | u16 typeReq, wValue, wIndex, wLength; |
1da177e4 | 468 | u8 *ubuf = urb->transfer_buffer; |
71d2718f | 469 | unsigned len = 0; |
e9df41c5 | 470 | int status; |
7329e211 AS |
471 | u8 patch_wakeup = 0; |
472 | u8 patch_protocol = 0; | |
e57e780b SS |
473 | u16 tbuf_size; |
474 | u8 *tbuf = NULL; | |
475 | const u8 *bufp; | |
1da177e4 | 476 | |
9439eb94 AS |
477 | might_sleep(); |
478 | ||
e9df41c5 AS |
479 | spin_lock_irq(&hcd_root_hub_lock); |
480 | status = usb_hcd_link_urb_to_ep(hcd, urb); | |
481 | spin_unlock_irq(&hcd_root_hub_lock); | |
482 | if (status) | |
483 | return status; | |
b0d9efba | 484 | urb->hcpriv = hcd; /* Indicate it's queued */ |
e9df41c5 | 485 | |
1da177e4 LT |
486 | cmd = (struct usb_ctrlrequest *) urb->setup_packet; |
487 | typeReq = (cmd->bRequestType << 8) | cmd->bRequest; | |
488 | wValue = le16_to_cpu (cmd->wValue); | |
489 | wIndex = le16_to_cpu (cmd->wIndex); | |
490 | wLength = le16_to_cpu (cmd->wLength); | |
491 | ||
492 | if (wLength > urb->transfer_buffer_length) | |
493 | goto error; | |
494 | ||
e57e780b SS |
495 | /* |
496 | * tbuf should be at least as big as the | |
497 | * USB hub descriptor. | |
498 | */ | |
499 | tbuf_size = max_t(u16, sizeof(struct usb_hub_descriptor), wLength); | |
500 | tbuf = kzalloc(tbuf_size, GFP_KERNEL); | |
501 | if (!tbuf) | |
502 | return -ENOMEM; | |
503 | ||
504 | bufp = tbuf; | |
505 | ||
506 | ||
1da177e4 LT |
507 | urb->actual_length = 0; |
508 | switch (typeReq) { | |
509 | ||
510 | /* DEVICE REQUESTS */ | |
511 | ||
fb669cc0 DB |
512 | /* The root hub's remote wakeup enable bit is implemented using |
513 | * driver model wakeup flags. If this system supports wakeup | |
514 | * through USB, userspace may change the default "allow wakeup" | |
515 | * policy through sysfs or these calls. | |
516 | * | |
517 | * Most root hubs support wakeup from downstream devices, for | |
518 | * runtime power management (disabling USB clocks and reducing | |
519 | * VBUS power usage). However, not all of them do so; silicon, | |
520 | * board, and BIOS bugs here are not uncommon, so these can't | |
521 | * be treated quite like external hubs. | |
522 | * | |
523 | * Likewise, not all root hubs will pass wakeup events upstream, | |
524 | * to wake up the whole system. So don't assume root hub and | |
525 | * controller capabilities are identical. | |
526 | */ | |
527 | ||
1da177e4 | 528 | case DeviceRequest | USB_REQ_GET_STATUS: |
14557359 | 529 | tbuf[0] = (device_may_wakeup(&hcd->self.root_hub->dev) |
fb669cc0 | 530 | << USB_DEVICE_REMOTE_WAKEUP) |
1da177e4 | 531 | | (1 << USB_DEVICE_SELF_POWERED); |
14557359 | 532 | tbuf[1] = 0; |
1da177e4 LT |
533 | len = 2; |
534 | break; | |
535 | case DeviceOutRequest | USB_REQ_CLEAR_FEATURE: | |
536 | if (wValue == USB_DEVICE_REMOTE_WAKEUP) | |
fb669cc0 | 537 | device_set_wakeup_enable(&hcd->self.root_hub->dev, 0); |
1da177e4 LT |
538 | else |
539 | goto error; | |
540 | break; | |
541 | case DeviceOutRequest | USB_REQ_SET_FEATURE: | |
fb669cc0 DB |
542 | if (device_can_wakeup(&hcd->self.root_hub->dev) |
543 | && wValue == USB_DEVICE_REMOTE_WAKEUP) | |
544 | device_set_wakeup_enable(&hcd->self.root_hub->dev, 1); | |
1da177e4 LT |
545 | else |
546 | goto error; | |
547 | break; | |
548 | case DeviceRequest | USB_REQ_GET_CONFIGURATION: | |
14557359 | 549 | tbuf[0] = 1; |
1da177e4 LT |
550 | len = 1; |
551 | /* FALLTHROUGH */ | |
552 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: | |
553 | break; | |
554 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: | |
555 | switch (wValue & 0xff00) { | |
556 | case USB_DT_DEVICE << 8: | |
83de4b2b | 557 | switch (hcd->speed) { |
71175225 | 558 | case HCD_USB31: |
7dd19e69 | 559 | case HCD_USB3: |
d2e9b4d6 | 560 | bufp = usb3_rh_dev_descriptor; |
7dd19e69 | 561 | break; |
1a81f881 TP |
562 | case HCD_USB25: |
563 | bufp = usb25_rh_dev_descriptor; | |
564 | break; | |
7dd19e69 | 565 | case HCD_USB2: |
1da177e4 | 566 | bufp = usb2_rh_dev_descriptor; |
7dd19e69 VM |
567 | break; |
568 | case HCD_USB11: | |
1da177e4 | 569 | bufp = usb11_rh_dev_descriptor; |
7dd19e69 VM |
570 | break; |
571 | default: | |
1da177e4 | 572 | goto error; |
7dd19e69 | 573 | } |
1da177e4 | 574 | len = 18; |
7329e211 AS |
575 | if (hcd->has_tt) |
576 | patch_protocol = 1; | |
1da177e4 LT |
577 | break; |
578 | case USB_DT_CONFIG << 8: | |
83de4b2b | 579 | switch (hcd->speed) { |
71175225 | 580 | case HCD_USB31: |
7dd19e69 | 581 | case HCD_USB3: |
d2e9b4d6 SS |
582 | bufp = ss_rh_config_descriptor; |
583 | len = sizeof ss_rh_config_descriptor; | |
7dd19e69 | 584 | break; |
1a81f881 | 585 | case HCD_USB25: |
7dd19e69 | 586 | case HCD_USB2: |
1da177e4 LT |
587 | bufp = hs_rh_config_descriptor; |
588 | len = sizeof hs_rh_config_descriptor; | |
7dd19e69 VM |
589 | break; |
590 | case HCD_USB11: | |
1da177e4 LT |
591 | bufp = fs_rh_config_descriptor; |
592 | len = sizeof fs_rh_config_descriptor; | |
7dd19e69 VM |
593 | break; |
594 | default: | |
595 | goto error; | |
1da177e4 | 596 | } |
fb669cc0 | 597 | if (device_can_wakeup(&hcd->self.root_hub->dev)) |
1da177e4 LT |
598 | patch_wakeup = 1; |
599 | break; | |
600 | case USB_DT_STRING << 8: | |
71d2718f RK |
601 | if ((wValue & 0xff) < 4) |
602 | urb->actual_length = rh_string(wValue & 0xff, | |
603 | hcd, ubuf, wLength); | |
604 | else /* unsupported IDs --> "protocol stall" */ | |
1da177e4 | 605 | goto error; |
1da177e4 | 606 | break; |
48e82361 SS |
607 | case USB_DT_BOS << 8: |
608 | goto nongeneric; | |
1da177e4 LT |
609 | default: |
610 | goto error; | |
611 | } | |
612 | break; | |
613 | case DeviceRequest | USB_REQ_GET_INTERFACE: | |
14557359 | 614 | tbuf[0] = 0; |
1da177e4 LT |
615 | len = 1; |
616 | /* FALLTHROUGH */ | |
617 | case DeviceOutRequest | USB_REQ_SET_INTERFACE: | |
618 | break; | |
619 | case DeviceOutRequest | USB_REQ_SET_ADDRESS: | |
048cb21c | 620 | /* wValue == urb->dev->devaddr */ |
1da177e4 LT |
621 | dev_dbg (hcd->self.controller, "root hub device address %d\n", |
622 | wValue); | |
623 | break; | |
624 | ||
625 | /* INTERFACE REQUESTS (no defined feature/status flags) */ | |
626 | ||
627 | /* ENDPOINT REQUESTS */ | |
628 | ||
629 | case EndpointRequest | USB_REQ_GET_STATUS: | |
048cb21c | 630 | /* ENDPOINT_HALT flag */ |
14557359 MB |
631 | tbuf[0] = 0; |
632 | tbuf[1] = 0; | |
1da177e4 LT |
633 | len = 2; |
634 | /* FALLTHROUGH */ | |
635 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: | |
636 | case EndpointOutRequest | USB_REQ_SET_FEATURE: | |
637 | dev_dbg (hcd->self.controller, "no endpoint features yet\n"); | |
638 | break; | |
639 | ||
640 | /* CLASS REQUESTS (and errors) */ | |
641 | ||
642 | default: | |
48e82361 | 643 | nongeneric: |
1da177e4 | 644 | /* non-generic request */ |
b13296c6 DB |
645 | switch (typeReq) { |
646 | case GetHubStatus: | |
647 | case GetPortStatus: | |
648 | len = 4; | |
649 | break; | |
650 | case GetHubDescriptor: | |
651 | len = sizeof (struct usb_hub_descriptor); | |
652 | break; | |
48e82361 SS |
653 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
654 | /* len is returned by hub_control */ | |
655 | break; | |
1da177e4 | 656 | } |
b13296c6 DB |
657 | status = hcd->driver->hub_control (hcd, |
658 | typeReq, wValue, wIndex, | |
659 | tbuf, wLength); | |
d2123fd9 LT |
660 | |
661 | if (typeReq == GetHubDescriptor) | |
662 | usb_hub_adjust_deviceremovable(hcd->self.root_hub, | |
663 | (struct usb_hub_descriptor *)tbuf); | |
1da177e4 LT |
664 | break; |
665 | error: | |
666 | /* "protocol stall" on error */ | |
667 | status = -EPIPE; | |
668 | } | |
669 | ||
48e82361 | 670 | if (status < 0) { |
1da177e4 LT |
671 | len = 0; |
672 | if (status != -EPIPE) { | |
673 | dev_dbg (hcd->self.controller, | |
674 | "CTRL: TypeReq=0x%x val=0x%x " | |
675 | "idx=0x%x len=%d ==> %d\n", | |
676 | typeReq, wValue, wIndex, | |
b13296c6 | 677 | wLength, status); |
1da177e4 | 678 | } |
48e82361 SS |
679 | } else if (status > 0) { |
680 | /* hub_control may return the length of data copied. */ | |
681 | len = status; | |
682 | status = 0; | |
1da177e4 LT |
683 | } |
684 | if (len) { | |
685 | if (urb->transfer_buffer_length < len) | |
686 | len = urb->transfer_buffer_length; | |
687 | urb->actual_length = len; | |
048cb21c | 688 | /* always USB_DIR_IN, toward host */ |
1da177e4 LT |
689 | memcpy (ubuf, bufp, len); |
690 | ||
691 | /* report whether RH hardware supports remote wakeup */ | |
692 | if (patch_wakeup && | |
693 | len > offsetof (struct usb_config_descriptor, | |
694 | bmAttributes)) | |
695 | ((struct usb_config_descriptor *)ubuf)->bmAttributes | |
696 | |= USB_CONFIG_ATT_WAKEUP; | |
7329e211 AS |
697 | |
698 | /* report whether RH hardware has an integrated TT */ | |
699 | if (patch_protocol && | |
700 | len > offsetof(struct usb_device_descriptor, | |
701 | bDeviceProtocol)) | |
702 | ((struct usb_device_descriptor *) ubuf)-> | |
7bf01185 | 703 | bDeviceProtocol = USB_HUB_PR_HS_SINGLE_TT; |
1da177e4 LT |
704 | } |
705 | ||
e57e780b SS |
706 | kfree(tbuf); |
707 | ||
1da177e4 | 708 | /* any errors get returned through the urb completion */ |
9439eb94 | 709 | spin_lock_irq(&hcd_root_hub_lock); |
e9df41c5 | 710 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
4a00027d | 711 | usb_hcd_giveback_urb(hcd, urb, status); |
9439eb94 | 712 | spin_unlock_irq(&hcd_root_hub_lock); |
1da177e4 LT |
713 | return 0; |
714 | } | |
715 | ||
716 | /*-------------------------------------------------------------------------*/ | |
717 | ||
718 | /* | |
d5926ae7 AS |
719 | * Root Hub interrupt transfers are polled using a timer if the |
720 | * driver requests it; otherwise the driver is responsible for | |
721 | * calling usb_hcd_poll_rh_status() when an event occurs. | |
1da177e4 | 722 | * |
d5926ae7 AS |
723 | * Completions are called in_interrupt(), but they may or may not |
724 | * be in_irq(). | |
1da177e4 | 725 | */ |
d5926ae7 AS |
726 | void usb_hcd_poll_rh_status(struct usb_hcd *hcd) |
727 | { | |
728 | struct urb *urb; | |
729 | int length; | |
730 | unsigned long flags; | |
ad361c98 | 731 | char buffer[6]; /* Any root hubs with > 31 ports? */ |
1da177e4 | 732 | |
6d88e679 | 733 | if (unlikely(!hcd->rh_pollable)) |
1b42ae6d | 734 | return; |
d5926ae7 AS |
735 | if (!hcd->uses_new_polling && !hcd->status_urb) |
736 | return; | |
1da177e4 | 737 | |
d5926ae7 AS |
738 | length = hcd->driver->hub_status_data(hcd, buffer); |
739 | if (length > 0) { | |
1da177e4 | 740 | |
d5926ae7 | 741 | /* try to complete the status urb */ |
9439eb94 | 742 | spin_lock_irqsave(&hcd_root_hub_lock, flags); |
d5926ae7 AS |
743 | urb = hcd->status_urb; |
744 | if (urb) { | |
541c7d43 | 745 | clear_bit(HCD_FLAG_POLL_PENDING, &hcd->flags); |
e9df41c5 | 746 | hcd->status_urb = NULL; |
e9df41c5 AS |
747 | urb->actual_length = length; |
748 | memcpy(urb->transfer_buffer, buffer, length); | |
9439eb94 | 749 | |
e9df41c5 | 750 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
4a00027d | 751 | usb_hcd_giveback_urb(hcd, urb, 0); |
e9df41c5 | 752 | } else { |
d5926ae7 | 753 | length = 0; |
541c7d43 | 754 | set_bit(HCD_FLAG_POLL_PENDING, &hcd->flags); |
e9df41c5 | 755 | } |
9439eb94 | 756 | spin_unlock_irqrestore(&hcd_root_hub_lock, flags); |
1da177e4 LT |
757 | } |
758 | ||
d5926ae7 | 759 | /* The USB 2.0 spec says 256 ms. This is close enough and won't |
01cd0819 AV |
760 | * exceed that limit if HZ is 100. The math is more clunky than |
761 | * maybe expected, this is to make sure that all timers for USB devices | |
25985edc | 762 | * fire at the same time to give the CPU a break in between */ |
541c7d43 | 763 | if (hcd->uses_new_polling ? HCD_POLL_RH(hcd) : |
d5926ae7 | 764 | (length == 0 && hcd->status_urb != NULL)) |
01cd0819 | 765 | mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); |
1da177e4 | 766 | } |
d5926ae7 | 767 | EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status); |
1da177e4 LT |
768 | |
769 | /* timer callback */ | |
d5926ae7 AS |
770 | static void rh_timer_func (unsigned long _hcd) |
771 | { | |
772 | usb_hcd_poll_rh_status((struct usb_hcd *) _hcd); | |
773 | } | |
774 | ||
775 | /*-------------------------------------------------------------------------*/ | |
1da177e4 | 776 | |
d5926ae7 | 777 | static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb) |
1da177e4 | 778 | { |
d5926ae7 | 779 | int retval; |
1da177e4 | 780 | unsigned long flags; |
71d2718f | 781 | unsigned len = 1 + (urb->dev->maxchild / 8); |
1da177e4 | 782 | |
d5926ae7 | 783 | spin_lock_irqsave (&hcd_root_hub_lock, flags); |
e9df41c5 | 784 | if (hcd->status_urb || urb->transfer_buffer_length < len) { |
d5926ae7 AS |
785 | dev_dbg (hcd->self.controller, "not queuing rh status urb\n"); |
786 | retval = -EINVAL; | |
e9df41c5 AS |
787 | goto done; |
788 | } | |
1da177e4 | 789 | |
e9df41c5 AS |
790 | retval = usb_hcd_link_urb_to_ep(hcd, urb); |
791 | if (retval) | |
792 | goto done; | |
1da177e4 | 793 | |
e9df41c5 AS |
794 | hcd->status_urb = urb; |
795 | urb->hcpriv = hcd; /* indicate it's queued */ | |
796 | if (!hcd->uses_new_polling) | |
797 | mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4)); | |
798 | ||
799 | /* If a status change has already occurred, report it ASAP */ | |
541c7d43 | 800 | else if (HCD_POLL_PENDING(hcd)) |
e9df41c5 AS |
801 | mod_timer(&hcd->rh_timer, jiffies); |
802 | retval = 0; | |
803 | done: | |
d5926ae7 AS |
804 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
805 | return retval; | |
1da177e4 LT |
806 | } |
807 | ||
1da177e4 LT |
808 | static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb) |
809 | { | |
5e60a161 | 810 | if (usb_endpoint_xfer_int(&urb->ep->desc)) |
d5926ae7 | 811 | return rh_queue_status (hcd, urb); |
5e60a161 | 812 | if (usb_endpoint_xfer_control(&urb->ep->desc)) |
1da177e4 | 813 | return rh_call_control (hcd, urb); |
d5926ae7 | 814 | return -EINVAL; |
1da177e4 LT |
815 | } |
816 | ||
817 | /*-------------------------------------------------------------------------*/ | |
818 | ||
455b25fb AS |
819 | /* Unlinks of root-hub control URBs are legal, but they don't do anything |
820 | * since these URBs always execute synchronously. | |
d5926ae7 | 821 | */ |
e9df41c5 | 822 | static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
1da177e4 | 823 | { |
455b25fb | 824 | unsigned long flags; |
e9df41c5 | 825 | int rc; |
1da177e4 | 826 | |
9439eb94 | 827 | spin_lock_irqsave(&hcd_root_hub_lock, flags); |
e9df41c5 AS |
828 | rc = usb_hcd_check_unlink_urb(hcd, urb, status); |
829 | if (rc) | |
830 | goto done; | |
831 | ||
5e60a161 | 832 | if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */ |
455b25fb | 833 | ; /* Do nothing */ |
d5926ae7 AS |
834 | |
835 | } else { /* Status URB */ | |
836 | if (!hcd->uses_new_polling) | |
455b25fb | 837 | del_timer (&hcd->rh_timer); |
d5926ae7 AS |
838 | if (urb == hcd->status_urb) { |
839 | hcd->status_urb = NULL; | |
e9df41c5 | 840 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
4a00027d | 841 | usb_hcd_giveback_urb(hcd, urb, status); |
9439eb94 AS |
842 | } |
843 | } | |
e9df41c5 | 844 | done: |
9439eb94 | 845 | spin_unlock_irqrestore(&hcd_root_hub_lock, flags); |
e9df41c5 | 846 | return rc; |
1da177e4 LT |
847 | } |
848 | ||
5234ce1b IPG |
849 | |
850 | ||
851 | /* | |
852 | * Show & store the current value of authorized_default | |
853 | */ | |
d03f254f GKH |
854 | static ssize_t authorized_default_show(struct device *dev, |
855 | struct device_attribute *attr, char *buf) | |
5234ce1b IPG |
856 | { |
857 | struct usb_device *rh_usb_dev = to_usb_device(dev); | |
858 | struct usb_bus *usb_bus = rh_usb_dev->bus; | |
ff8e2c56 | 859 | struct usb_hcd *hcd; |
5234ce1b | 860 | |
ff8e2c56 SK |
861 | hcd = bus_to_hcd(usb_bus); |
862 | return snprintf(buf, PAGE_SIZE, "%u\n", !!HCD_DEV_AUTHORIZED(hcd)); | |
5234ce1b IPG |
863 | } |
864 | ||
d03f254f GKH |
865 | static ssize_t authorized_default_store(struct device *dev, |
866 | struct device_attribute *attr, | |
867 | const char *buf, size_t size) | |
5234ce1b IPG |
868 | { |
869 | ssize_t result; | |
870 | unsigned val; | |
871 | struct usb_device *rh_usb_dev = to_usb_device(dev); | |
872 | struct usb_bus *usb_bus = rh_usb_dev->bus; | |
ff8e2c56 | 873 | struct usb_hcd *hcd; |
5234ce1b | 874 | |
ff8e2c56 | 875 | hcd = bus_to_hcd(usb_bus); |
5234ce1b IPG |
876 | result = sscanf(buf, "%u\n", &val); |
877 | if (result == 1) { | |
ff8e2c56 SK |
878 | if (val) |
879 | set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); | |
880 | else | |
881 | clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); | |
882 | ||
5234ce1b | 883 | result = size; |
d1ddb0a6 | 884 | } else { |
5234ce1b | 885 | result = -EINVAL; |
d1ddb0a6 | 886 | } |
5234ce1b IPG |
887 | return result; |
888 | } | |
d03f254f | 889 | static DEVICE_ATTR_RW(authorized_default); |
5234ce1b | 890 | |
6b2bd3c8 SK |
891 | /* |
892 | * interface_authorized_default_show - show default authorization status | |
893 | * for USB interfaces | |
894 | * | |
895 | * note: interface_authorized_default is the default value | |
896 | * for initializing the authorized attribute of interfaces | |
897 | */ | |
898 | static ssize_t interface_authorized_default_show(struct device *dev, | |
899 | struct device_attribute *attr, char *buf) | |
900 | { | |
901 | struct usb_device *usb_dev = to_usb_device(dev); | |
902 | struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus); | |
903 | ||
904 | return sprintf(buf, "%u\n", !!HCD_INTF_AUTHORIZED(hcd)); | |
905 | } | |
906 | ||
907 | /* | |
908 | * interface_authorized_default_store - store default authorization status | |
909 | * for USB interfaces | |
910 | * | |
911 | * note: interface_authorized_default is the default value | |
912 | * for initializing the authorized attribute of interfaces | |
913 | */ | |
914 | static ssize_t interface_authorized_default_store(struct device *dev, | |
915 | struct device_attribute *attr, const char *buf, size_t count) | |
916 | { | |
917 | struct usb_device *usb_dev = to_usb_device(dev); | |
918 | struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus); | |
919 | int rc = count; | |
920 | bool val; | |
921 | ||
922 | if (strtobool(buf, &val) != 0) | |
923 | return -EINVAL; | |
924 | ||
925 | if (val) | |
926 | set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags); | |
927 | else | |
928 | clear_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags); | |
929 | ||
930 | return rc; | |
931 | } | |
932 | static DEVICE_ATTR_RW(interface_authorized_default); | |
933 | ||
5234ce1b IPG |
934 | /* Group all the USB bus attributes */ |
935 | static struct attribute *usb_bus_attrs[] = { | |
936 | &dev_attr_authorized_default.attr, | |
6b2bd3c8 | 937 | &dev_attr_interface_authorized_default.attr, |
5234ce1b IPG |
938 | NULL, |
939 | }; | |
940 | ||
941 | static struct attribute_group usb_bus_attr_group = { | |
942 | .name = NULL, /* we want them in the same directory */ | |
943 | .attrs = usb_bus_attrs, | |
944 | }; | |
945 | ||
946 | ||
947 | ||
1da177e4 LT |
948 | /*-------------------------------------------------------------------------*/ |
949 | ||
1da177e4 LT |
950 | /** |
951 | * usb_bus_init - shared initialization code | |
952 | * @bus: the bus structure being initialized | |
953 | * | |
954 | * This code is used to initialize a usb_bus structure, memory for which is | |
955 | * separately managed. | |
956 | */ | |
957 | static void usb_bus_init (struct usb_bus *bus) | |
958 | { | |
959 | memset (&bus->devmap, 0, sizeof(struct usb_devmap)); | |
960 | ||
961 | bus->devnum_next = 1; | |
962 | ||
963 | bus->root_hub = NULL; | |
1da177e4 LT |
964 | bus->busnum = -1; |
965 | bus->bandwidth_allocated = 0; | |
966 | bus->bandwidth_int_reqs = 0; | |
967 | bus->bandwidth_isoc_reqs = 0; | |
6fecd4f2 | 968 | mutex_init(&bus->usb_address0_mutex); |
1da177e4 LT |
969 | |
970 | INIT_LIST_HEAD (&bus->bus_list); | |
1da177e4 LT |
971 | } |
972 | ||
1da177e4 LT |
973 | /*-------------------------------------------------------------------------*/ |
974 | ||
975 | /** | |
976 | * usb_register_bus - registers the USB host controller with the usb core | |
977 | * @bus: pointer to the bus to register | |
978 | * Context: !in_interrupt() | |
979 | * | |
980 | * Assigns a bus number, and links the controller into usbcore data | |
981 | * structures so that it can be seen by scanning the bus list. | |
626f090c YB |
982 | * |
983 | * Return: 0 if successful. A negative error code otherwise. | |
1da177e4 LT |
984 | */ |
985 | static int usb_register_bus(struct usb_bus *bus) | |
986 | { | |
eb579f58 | 987 | int result = -E2BIG; |
1da177e4 | 988 | int busnum; |
1da177e4 | 989 | |
4186ecf8 | 990 | mutex_lock(&usb_bus_list_lock); |
059239ad | 991 | busnum = find_next_zero_bit(busmap, USB_MAXBUS, 1); |
eb579f58 | 992 | if (busnum >= USB_MAXBUS) { |
1da177e4 | 993 | printk (KERN_ERR "%s: too many buses\n", usbcore_name); |
eb579f58 | 994 | goto error_find_busnum; |
1da177e4 | 995 | } |
059239ad | 996 | set_bit(busnum, busmap); |
eb579f58 | 997 | bus->busnum = busnum; |
5a3201b2 | 998 | |
1da177e4 LT |
999 | /* Add it to the local list of buses */ |
1000 | list_add (&bus->bus_list, &usb_bus_list); | |
4186ecf8 | 1001 | mutex_unlock(&usb_bus_list_lock); |
1da177e4 | 1002 | |
3099e75a | 1003 | usb_notify_add_bus(bus); |
1da177e4 | 1004 | |
eb579f58 IPG |
1005 | dev_info (bus->controller, "new USB bus registered, assigned bus " |
1006 | "number %d\n", bus->busnum); | |
1da177e4 | 1007 | return 0; |
eb579f58 | 1008 | |
eb579f58 IPG |
1009 | error_find_busnum: |
1010 | mutex_unlock(&usb_bus_list_lock); | |
1011 | return result; | |
1da177e4 LT |
1012 | } |
1013 | ||
1014 | /** | |
1015 | * usb_deregister_bus - deregisters the USB host controller | |
1016 | * @bus: pointer to the bus to deregister | |
1017 | * Context: !in_interrupt() | |
1018 | * | |
1019 | * Recycles the bus number, and unlinks the controller from usbcore data | |
1020 | * structures so that it won't be seen by scanning the bus list. | |
1021 | */ | |
1022 | static void usb_deregister_bus (struct usb_bus *bus) | |
1023 | { | |
1024 | dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum); | |
1025 | ||
1026 | /* | |
1027 | * NOTE: make sure that all the devices are removed by the | |
1028 | * controller code, as well as having it call this when cleaning | |
1029 | * itself up | |
1030 | */ | |
4186ecf8 | 1031 | mutex_lock(&usb_bus_list_lock); |
1da177e4 | 1032 | list_del (&bus->bus_list); |
4186ecf8 | 1033 | mutex_unlock(&usb_bus_list_lock); |
1da177e4 | 1034 | |
3099e75a | 1035 | usb_notify_remove_bus(bus); |
1da177e4 | 1036 | |
059239ad | 1037 | clear_bit(bus->busnum, busmap); |
1da177e4 LT |
1038 | } |
1039 | ||
1040 | /** | |
8ec8d20b | 1041 | * register_root_hub - called by usb_add_hcd() to register a root hub |
1da177e4 LT |
1042 | * @hcd: host controller for this root hub |
1043 | * | |
8ec8d20b | 1044 | * This function registers the root hub with the USB subsystem. It sets up |
b1e8f0a6 DB |
1045 | * the device properly in the device tree and then calls usb_new_device() |
1046 | * to register the usb device. It also assigns the root hub's USB address | |
1047 | * (always 1). | |
626f090c YB |
1048 | * |
1049 | * Return: 0 if successful. A negative error code otherwise. | |
1da177e4 | 1050 | */ |
b1e8f0a6 | 1051 | static int register_root_hub(struct usb_hcd *hcd) |
1da177e4 LT |
1052 | { |
1053 | struct device *parent_dev = hcd->self.controller; | |
b1e8f0a6 | 1054 | struct usb_device *usb_dev = hcd->self.root_hub; |
1da177e4 LT |
1055 | const int devnum = 1; |
1056 | int retval; | |
1057 | ||
1da177e4 LT |
1058 | usb_dev->devnum = devnum; |
1059 | usb_dev->bus->devnum_next = devnum + 1; | |
1060 | memset (&usb_dev->bus->devmap.devicemap, 0, | |
1061 | sizeof usb_dev->bus->devmap.devicemap); | |
1062 | set_bit (devnum, usb_dev->bus->devmap.devicemap); | |
1063 | usb_set_device_state(usb_dev, USB_STATE_ADDRESS); | |
1064 | ||
4186ecf8 | 1065 | mutex_lock(&usb_bus_list_lock); |
1da177e4 | 1066 | |
551509d2 | 1067 | usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
1da177e4 LT |
1068 | retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE); |
1069 | if (retval != sizeof usb_dev->descriptor) { | |
4186ecf8 | 1070 | mutex_unlock(&usb_bus_list_lock); |
1da177e4 | 1071 | dev_dbg (parent_dev, "can't read %s device descriptor %d\n", |
7071a3ce | 1072 | dev_name(&usb_dev->dev), retval); |
1da177e4 LT |
1073 | return (retval < 0) ? retval : -EMSGSIZE; |
1074 | } | |
2d2a3167 LB |
1075 | |
1076 | if (le16_to_cpu(usb_dev->descriptor.bcdUSB) >= 0x0201) { | |
448b6eb1 | 1077 | retval = usb_get_bos_descriptor(usb_dev); |
2d2a3167 LB |
1078 | if (!retval) { |
1079 | usb_dev->lpm_capable = usb_device_supports_lpm(usb_dev); | |
1080 | } else if (usb_dev->speed == USB_SPEED_SUPER) { | |
448b6eb1 SS |
1081 | mutex_unlock(&usb_bus_list_lock); |
1082 | dev_dbg(parent_dev, "can't read %s bos descriptor %d\n", | |
1083 | dev_name(&usb_dev->dev), retval); | |
1084 | return retval; | |
1085 | } | |
1086 | } | |
1da177e4 | 1087 | |
1da177e4 | 1088 | retval = usb_new_device (usb_dev); |
1da177e4 | 1089 | if (retval) { |
1da177e4 | 1090 | dev_err (parent_dev, "can't register root hub for %s, %d\n", |
7071a3ce | 1091 | dev_name(&usb_dev->dev), retval); |
0a231403 | 1092 | } else { |
1da177e4 LT |
1093 | spin_lock_irq (&hcd_root_hub_lock); |
1094 | hcd->rh_registered = 1; | |
1095 | spin_unlock_irq (&hcd_root_hub_lock); | |
1096 | ||
1097 | /* Did the HC die before the root hub was registered? */ | |
69fff59d | 1098 | if (HCD_DEAD(hcd)) |
1da177e4 LT |
1099 | usb_hc_died (hcd); /* This time clean up */ |
1100 | } | |
0a231403 | 1101 | mutex_unlock(&usb_bus_list_lock); |
1da177e4 LT |
1102 | |
1103 | return retval; | |
1104 | } | |
1da177e4 | 1105 | |
da0aa716 AS |
1106 | /* |
1107 | * usb_hcd_start_port_resume - a root-hub port is sending a resume signal | |
1108 | * @bus: the bus which the root hub belongs to | |
1109 | * @portnum: the port which is being resumed | |
1110 | * | |
1111 | * HCDs should call this function when they know that a resume signal is | |
1112 | * being sent to a root-hub port. The root hub will be prevented from | |
1113 | * going into autosuspend until usb_hcd_end_port_resume() is called. | |
1114 | * | |
1115 | * The bus's private lock must be held by the caller. | |
1116 | */ | |
1117 | void usb_hcd_start_port_resume(struct usb_bus *bus, int portnum) | |
1118 | { | |
1119 | unsigned bit = 1 << portnum; | |
1120 | ||
1121 | if (!(bus->resuming_ports & bit)) { | |
1122 | bus->resuming_ports |= bit; | |
1123 | pm_runtime_get_noresume(&bus->root_hub->dev); | |
1124 | } | |
1125 | } | |
1126 | EXPORT_SYMBOL_GPL(usb_hcd_start_port_resume); | |
1127 | ||
1128 | /* | |
1129 | * usb_hcd_end_port_resume - a root-hub port has stopped sending a resume signal | |
1130 | * @bus: the bus which the root hub belongs to | |
1131 | * @portnum: the port which is being resumed | |
1132 | * | |
1133 | * HCDs should call this function when they know that a resume signal has | |
1134 | * stopped being sent to a root-hub port. The root hub will be allowed to | |
1135 | * autosuspend again. | |
1136 | * | |
1137 | * The bus's private lock must be held by the caller. | |
1138 | */ | |
1139 | void usb_hcd_end_port_resume(struct usb_bus *bus, int portnum) | |
1140 | { | |
1141 | unsigned bit = 1 << portnum; | |
1142 | ||
1143 | if (bus->resuming_ports & bit) { | |
1144 | bus->resuming_ports &= ~bit; | |
1145 | pm_runtime_put_noidle(&bus->root_hub->dev); | |
1146 | } | |
1147 | } | |
1148 | EXPORT_SYMBOL_GPL(usb_hcd_end_port_resume); | |
1da177e4 LT |
1149 | |
1150 | /*-------------------------------------------------------------------------*/ | |
1151 | ||
1152 | /** | |
1153 | * usb_calc_bus_time - approximate periodic transaction time in nanoseconds | |
1154 | * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH} | |
1155 | * @is_input: true iff the transaction sends data to the host | |
1156 | * @isoc: true for isochronous transactions, false for interrupt ones | |
1157 | * @bytecount: how many bytes in the transaction. | |
1158 | * | |
626f090c YB |
1159 | * Return: Approximate bus time in nanoseconds for a periodic transaction. |
1160 | * | |
1161 | * Note: | |
1da177e4 LT |
1162 | * See USB 2.0 spec section 5.11.3; only periodic transfers need to be |
1163 | * scheduled in software, this function is only used for such scheduling. | |
1164 | */ | |
1165 | long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount) | |
1166 | { | |
1167 | unsigned long tmp; | |
1168 | ||
1169 | switch (speed) { | |
1170 | case USB_SPEED_LOW: /* INTR only */ | |
1171 | if (is_input) { | |
1172 | tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L; | |
07010496 | 1173 | return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp; |
1da177e4 LT |
1174 | } else { |
1175 | tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L; | |
07010496 | 1176 | return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp; |
1da177e4 LT |
1177 | } |
1178 | case USB_SPEED_FULL: /* ISOC or INTR */ | |
1179 | if (isoc) { | |
1180 | tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; | |
07010496 | 1181 | return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp; |
1da177e4 LT |
1182 | } else { |
1183 | tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L; | |
07010496 | 1184 | return 9107L + BW_HOST_DELAY + tmp; |
1da177e4 LT |
1185 | } |
1186 | case USB_SPEED_HIGH: /* ISOC or INTR */ | |
048cb21c | 1187 | /* FIXME adjust for input vs output */ |
1da177e4 | 1188 | if (isoc) |
498f78e6 | 1189 | tmp = HS_NSECS_ISO (bytecount); |
1da177e4 | 1190 | else |
498f78e6 | 1191 | tmp = HS_NSECS (bytecount); |
1da177e4 LT |
1192 | return tmp; |
1193 | default: | |
1194 | pr_debug ("%s: bogus device speed!\n", usbcore_name); | |
1195 | return -1; | |
1196 | } | |
1197 | } | |
782e70c6 | 1198 | EXPORT_SYMBOL_GPL(usb_calc_bus_time); |
1da177e4 | 1199 | |
1da177e4 LT |
1200 | |
1201 | /*-------------------------------------------------------------------------*/ | |
1202 | ||
1203 | /* | |
1204 | * Generic HC operations. | |
1205 | */ | |
1206 | ||
1207 | /*-------------------------------------------------------------------------*/ | |
1208 | ||
e9df41c5 AS |
1209 | /** |
1210 | * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue | |
1211 | * @hcd: host controller to which @urb was submitted | |
1212 | * @urb: URB being submitted | |
1213 | * | |
1214 | * Host controller drivers should call this routine in their enqueue() | |
1215 | * method. The HCD's private spinlock must be held and interrupts must | |
1216 | * be disabled. The actions carried out here are required for URB | |
1217 | * submission, as well as for endpoint shutdown and for usb_kill_urb. | |
1218 | * | |
626f090c | 1219 | * Return: 0 for no error, otherwise a negative error code (in which case |
e9df41c5 AS |
1220 | * the enqueue() method must fail). If no error occurs but enqueue() fails |
1221 | * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing | |
1222 | * the private spinlock and returning. | |
1223 | */ | |
1224 | int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb) | |
1da177e4 | 1225 | { |
9a9bf406 | 1226 | int rc = 0; |
1da177e4 | 1227 | |
e9df41c5 | 1228 | spin_lock(&hcd_urb_list_lock); |
1da177e4 | 1229 | |
9a9bf406 | 1230 | /* Check that the URB isn't being killed */ |
49367d8f | 1231 | if (unlikely(atomic_read(&urb->reject))) { |
9a9bf406 AS |
1232 | rc = -EPERM; |
1233 | goto done; | |
9f6a93f7 | 1234 | } |
1da177e4 | 1235 | |
9a9bf406 AS |
1236 | if (unlikely(!urb->ep->enabled)) { |
1237 | rc = -ENOENT; | |
1238 | goto done; | |
1239 | } | |
1da177e4 | 1240 | |
6840d255 AS |
1241 | if (unlikely(!urb->dev->can_submit)) { |
1242 | rc = -EHOSTUNREACH; | |
1243 | goto done; | |
1244 | } | |
1245 | ||
1da177e4 | 1246 | /* |
9a9bf406 AS |
1247 | * Check the host controller's state and add the URB to the |
1248 | * endpoint's queue. | |
1da177e4 | 1249 | */ |
9b37596a | 1250 | if (HCD_RH_RUNNING(hcd)) { |
eb231054 | 1251 | urb->unlinked = 0; |
9a9bf406 | 1252 | list_add_tail(&urb->urb_list, &urb->ep->urb_list); |
9b37596a | 1253 | } else { |
9a9bf406 AS |
1254 | rc = -ESHUTDOWN; |
1255 | goto done; | |
1da177e4 | 1256 | } |
9a9bf406 | 1257 | done: |
e9df41c5 | 1258 | spin_unlock(&hcd_urb_list_lock); |
9a9bf406 AS |
1259 | return rc; |
1260 | } | |
e9df41c5 | 1261 | EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep); |
9a9bf406 | 1262 | |
e9df41c5 AS |
1263 | /** |
1264 | * usb_hcd_check_unlink_urb - check whether an URB may be unlinked | |
1265 | * @hcd: host controller to which @urb was submitted | |
1266 | * @urb: URB being checked for unlinkability | |
1267 | * @status: error code to store in @urb if the unlink succeeds | |
1268 | * | |
1269 | * Host controller drivers should call this routine in their dequeue() | |
1270 | * method. The HCD's private spinlock must be held and interrupts must | |
1271 | * be disabled. The actions carried out here are required for making | |
1272 | * sure than an unlink is valid. | |
1273 | * | |
626f090c | 1274 | * Return: 0 for no error, otherwise a negative error code (in which case |
e9df41c5 AS |
1275 | * the dequeue() method must fail). The possible error codes are: |
1276 | * | |
1277 | * -EIDRM: @urb was not submitted or has already completed. | |
1278 | * The completion function may not have been called yet. | |
1279 | * | |
1280 | * -EBUSY: @urb has already been unlinked. | |
1281 | */ | |
1282 | int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb, | |
9a9bf406 AS |
1283 | int status) |
1284 | { | |
9a9bf406 | 1285 | struct list_head *tmp; |
9a9bf406 AS |
1286 | |
1287 | /* insist the urb is still queued */ | |
1288 | list_for_each(tmp, &urb->ep->urb_list) { | |
1289 | if (tmp == &urb->urb_list) | |
1290 | break; | |
1291 | } | |
e9df41c5 AS |
1292 | if (tmp != &urb->urb_list) |
1293 | return -EIDRM; | |
1da177e4 | 1294 | |
9a9bf406 AS |
1295 | /* Any status except -EINPROGRESS means something already started to |
1296 | * unlink this URB from the hardware. So there's no more work to do. | |
1da177e4 | 1297 | */ |
eb231054 | 1298 | if (urb->unlinked) |
e9df41c5 | 1299 | return -EBUSY; |
eb231054 | 1300 | urb->unlinked = status; |
e9df41c5 | 1301 | return 0; |
9a9bf406 | 1302 | } |
e9df41c5 | 1303 | EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb); |
9a9bf406 | 1304 | |
e9df41c5 AS |
1305 | /** |
1306 | * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue | |
1307 | * @hcd: host controller to which @urb was submitted | |
1308 | * @urb: URB being unlinked | |
1309 | * | |
1310 | * Host controller drivers should call this routine before calling | |
1311 | * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and | |
1312 | * interrupts must be disabled. The actions carried out here are required | |
1313 | * for URB completion. | |
1314 | */ | |
1315 | void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb) | |
9a9bf406 | 1316 | { |
9a9bf406 | 1317 | /* clear all state linking urb to this dev (and hcd) */ |
e9df41c5 | 1318 | spin_lock(&hcd_urb_list_lock); |
9a9bf406 | 1319 | list_del_init(&urb->urb_list); |
e9df41c5 | 1320 | spin_unlock(&hcd_urb_list_lock); |
9a9bf406 | 1321 | } |
e9df41c5 | 1322 | EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep); |
9a9bf406 | 1323 | |
b3476675 MD |
1324 | /* |
1325 | * Some usb host controllers can only perform dma using a small SRAM area. | |
1326 | * The usb core itself is however optimized for host controllers that can dma | |
1327 | * using regular system memory - like pci devices doing bus mastering. | |
1328 | * | |
a6cd244b | 1329 | * To support host controllers with limited dma capabilities we provide dma |
b3476675 MD |
1330 | * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag. |
1331 | * For this to work properly the host controller code must first use the | |
1332 | * function dma_declare_coherent_memory() to point out which memory area | |
1333 | * that should be used for dma allocations. | |
1334 | * | |
1335 | * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for | |
1336 | * dma using dma_alloc_coherent() which in turn allocates from the memory | |
1337 | * area pointed out with dma_declare_coherent_memory(). | |
1338 | * | |
1339 | * So, to summarize... | |
1340 | * | |
1341 | * - We need "local" memory, canonical example being | |
1342 | * a small SRAM on a discrete controller being the | |
1343 | * only memory that the controller can read ... | |
1344 | * (a) "normal" kernel memory is no good, and | |
1345 | * (b) there's not enough to share | |
1346 | * | |
1347 | * - The only *portable* hook for such stuff in the | |
1348 | * DMA framework is dma_declare_coherent_memory() | |
1349 | * | |
1350 | * - So we use that, even though the primary requirement | |
025d4430 | 1351 | * is that the memory be "local" (hence addressable |
b3476675 MD |
1352 | * by that device), not "coherent". |
1353 | * | |
1354 | */ | |
1355 | ||
1356 | static int hcd_alloc_coherent(struct usb_bus *bus, | |
1357 | gfp_t mem_flags, dma_addr_t *dma_handle, | |
1358 | void **vaddr_handle, size_t size, | |
1359 | enum dma_data_direction dir) | |
1360 | { | |
1361 | unsigned char *vaddr; | |
1362 | ||
4307a28e AR |
1363 | if (*vaddr_handle == NULL) { |
1364 | WARN_ON_ONCE(1); | |
1365 | return -EFAULT; | |
1366 | } | |
1367 | ||
b3476675 MD |
1368 | vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr), |
1369 | mem_flags, dma_handle); | |
1370 | if (!vaddr) | |
1371 | return -ENOMEM; | |
1372 | ||
1373 | /* | |
1374 | * Store the virtual address of the buffer at the end | |
1375 | * of the allocated dma buffer. The size of the buffer | |
1376 | * may be uneven so use unaligned functions instead | |
1377 | * of just rounding up. It makes sense to optimize for | |
1378 | * memory footprint over access speed since the amount | |
1379 | * of memory available for dma may be limited. | |
1380 | */ | |
1381 | put_unaligned((unsigned long)*vaddr_handle, | |
1382 | (unsigned long *)(vaddr + size)); | |
1383 | ||
1384 | if (dir == DMA_TO_DEVICE) | |
1385 | memcpy(vaddr, *vaddr_handle, size); | |
1386 | ||
1387 | *vaddr_handle = vaddr; | |
1388 | return 0; | |
1389 | } | |
1390 | ||
1391 | static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle, | |
1392 | void **vaddr_handle, size_t size, | |
1393 | enum dma_data_direction dir) | |
1394 | { | |
1395 | unsigned char *vaddr = *vaddr_handle; | |
1396 | ||
1397 | vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size)); | |
1398 | ||
1399 | if (dir == DMA_FROM_DEVICE) | |
1400 | memcpy(vaddr, *vaddr_handle, size); | |
1401 | ||
1402 | hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle); | |
1403 | ||
1404 | *vaddr_handle = vaddr; | |
1405 | *dma_handle = 0; | |
1406 | } | |
1407 | ||
c8cf203a | 1408 | void usb_hcd_unmap_urb_setup_for_dma(struct usb_hcd *hcd, struct urb *urb) |
ff9c895f | 1409 | { |
ff9c895f AS |
1410 | if (urb->transfer_flags & URB_SETUP_MAP_SINGLE) |
1411 | dma_unmap_single(hcd->self.controller, | |
1412 | urb->setup_dma, | |
1413 | sizeof(struct usb_ctrlrequest), | |
1414 | DMA_TO_DEVICE); | |
1415 | else if (urb->transfer_flags & URB_SETUP_MAP_LOCAL) | |
1416 | hcd_free_coherent(urb->dev->bus, | |
1417 | &urb->setup_dma, | |
1418 | (void **) &urb->setup_packet, | |
1419 | sizeof(struct usb_ctrlrequest), | |
1420 | DMA_TO_DEVICE); | |
1421 | ||
1dae423d MF |
1422 | /* Make it safe to call this routine more than once */ |
1423 | urb->transfer_flags &= ~(URB_SETUP_MAP_SINGLE | URB_SETUP_MAP_LOCAL); | |
1424 | } | |
c8cf203a | 1425 | EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_setup_for_dma); |
1dae423d | 1426 | |
2694a48d RM |
1427 | static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) |
1428 | { | |
1429 | if (hcd->driver->unmap_urb_for_dma) | |
1430 | hcd->driver->unmap_urb_for_dma(hcd, urb); | |
1431 | else | |
1432 | usb_hcd_unmap_urb_for_dma(hcd, urb); | |
1433 | } | |
1434 | ||
c8cf203a | 1435 | void usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) |
1dae423d MF |
1436 | { |
1437 | enum dma_data_direction dir; | |
1438 | ||
c8cf203a | 1439 | usb_hcd_unmap_urb_setup_for_dma(hcd, urb); |
1dae423d | 1440 | |
ff9c895f AS |
1441 | dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
1442 | if (urb->transfer_flags & URB_DMA_MAP_SG) | |
1443 | dma_unmap_sg(hcd->self.controller, | |
910f8d0c | 1444 | urb->sg, |
ff9c895f AS |
1445 | urb->num_sgs, |
1446 | dir); | |
1447 | else if (urb->transfer_flags & URB_DMA_MAP_PAGE) | |
1448 | dma_unmap_page(hcd->self.controller, | |
1449 | urb->transfer_dma, | |
1450 | urb->transfer_buffer_length, | |
1451 | dir); | |
1452 | else if (urb->transfer_flags & URB_DMA_MAP_SINGLE) | |
1453 | dma_unmap_single(hcd->self.controller, | |
1454 | urb->transfer_dma, | |
1455 | urb->transfer_buffer_length, | |
1456 | dir); | |
1457 | else if (urb->transfer_flags & URB_MAP_LOCAL) | |
1458 | hcd_free_coherent(urb->dev->bus, | |
1459 | &urb->transfer_dma, | |
1460 | &urb->transfer_buffer, | |
1461 | urb->transfer_buffer_length, | |
1462 | dir); | |
1463 | ||
1464 | /* Make it safe to call this routine more than once */ | |
1dae423d | 1465 | urb->transfer_flags &= ~(URB_DMA_MAP_SG | URB_DMA_MAP_PAGE | |
ff9c895f AS |
1466 | URB_DMA_MAP_SINGLE | URB_MAP_LOCAL); |
1467 | } | |
c8cf203a | 1468 | EXPORT_SYMBOL_GPL(usb_hcd_unmap_urb_for_dma); |
ff9c895f | 1469 | |
b3476675 MD |
1470 | static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, |
1471 | gfp_t mem_flags) | |
2694a48d RM |
1472 | { |
1473 | if (hcd->driver->map_urb_for_dma) | |
1474 | return hcd->driver->map_urb_for_dma(hcd, urb, mem_flags); | |
1475 | else | |
1476 | return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); | |
1477 | } | |
1478 | ||
1479 | int usb_hcd_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, | |
1480 | gfp_t mem_flags) | |
9a9bf406 | 1481 | { |
b3476675 MD |
1482 | enum dma_data_direction dir; |
1483 | int ret = 0; | |
1484 | ||
9a9bf406 AS |
1485 | /* Map the URB's buffers for DMA access. |
1486 | * Lower level HCD code should use *_dma exclusively, | |
e04748e3 SS |
1487 | * unless it uses pio or talks to another transport, |
1488 | * or uses the provided scatter gather list for bulk. | |
1da177e4 | 1489 | */ |
b3476675 | 1490 | |
85bcb5ee | 1491 | if (usb_endpoint_xfer_control(&urb->ep->desc)) { |
07a8cdd2 AG |
1492 | if (hcd->self.uses_pio_for_control) |
1493 | return ret; | |
85e034fd | 1494 | if (hcd->self.uses_dma) { |
b3476675 | 1495 | urb->setup_dma = dma_map_single( |
1da177e4 LT |
1496 | hcd->self.controller, |
1497 | urb->setup_packet, | |
b3476675 | 1498 | sizeof(struct usb_ctrlrequest), |
1da177e4 | 1499 | DMA_TO_DEVICE); |
85e034fd LF |
1500 | if (dma_mapping_error(hcd->self.controller, |
1501 | urb->setup_dma)) | |
1502 | return -EAGAIN; | |
ff9c895f | 1503 | urb->transfer_flags |= URB_SETUP_MAP_SINGLE; |
f537da68 | 1504 | } else if (hcd->driver->flags & HCD_LOCAL_MEM) { |
b3476675 MD |
1505 | ret = hcd_alloc_coherent( |
1506 | urb->dev->bus, mem_flags, | |
1507 | &urb->setup_dma, | |
1508 | (void **)&urb->setup_packet, | |
1509 | sizeof(struct usb_ctrlrequest), | |
1510 | DMA_TO_DEVICE); | |
ff9c895f AS |
1511 | if (ret) |
1512 | return ret; | |
1513 | urb->transfer_flags |= URB_SETUP_MAP_LOCAL; | |
f537da68 | 1514 | } |
b3476675 MD |
1515 | } |
1516 | ||
1517 | dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE; | |
ff9c895f | 1518 | if (urb->transfer_buffer_length != 0 |
b3476675 | 1519 | && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) { |
85e034fd | 1520 | if (hcd->self.uses_dma) { |
ff9c895f | 1521 | if (urb->num_sgs) { |
fe2072cc HG |
1522 | int n; |
1523 | ||
1524 | /* We don't support sg for isoc transfers ! */ | |
1525 | if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { | |
1526 | WARN_ON(1); | |
1527 | return -EINVAL; | |
1528 | } | |
1529 | ||
1530 | n = dma_map_sg( | |
ff9c895f | 1531 | hcd->self.controller, |
910f8d0c | 1532 | urb->sg, |
ff9c895f AS |
1533 | urb->num_sgs, |
1534 | dir); | |
1535 | if (n <= 0) | |
1536 | ret = -EAGAIN; | |
1537 | else | |
1538 | urb->transfer_flags |= URB_DMA_MAP_SG; | |
bc677d5b CL |
1539 | urb->num_mapped_sgs = n; |
1540 | if (n != urb->num_sgs) | |
ff9c895f AS |
1541 | urb->transfer_flags |= |
1542 | URB_DMA_SG_COMBINED; | |
ff9c895f | 1543 | } else if (urb->sg) { |
910f8d0c | 1544 | struct scatterlist *sg = urb->sg; |
ff9c895f AS |
1545 | urb->transfer_dma = dma_map_page( |
1546 | hcd->self.controller, | |
1547 | sg_page(sg), | |
1548 | sg->offset, | |
1549 | urb->transfer_buffer_length, | |
1550 | dir); | |
1551 | if (dma_mapping_error(hcd->self.controller, | |
85e034fd | 1552 | urb->transfer_dma)) |
ff9c895f AS |
1553 | ret = -EAGAIN; |
1554 | else | |
1555 | urb->transfer_flags |= URB_DMA_MAP_PAGE; | |
29d2fef8 DW |
1556 | } else if (is_vmalloc_addr(urb->transfer_buffer)) { |
1557 | WARN_ONCE(1, "transfer buffer not dma capable\n"); | |
1558 | ret = -EAGAIN; | |
ff9c895f AS |
1559 | } else { |
1560 | urb->transfer_dma = dma_map_single( | |
1561 | hcd->self.controller, | |
1562 | urb->transfer_buffer, | |
1563 | urb->transfer_buffer_length, | |
1564 | dir); | |
1565 | if (dma_mapping_error(hcd->self.controller, | |
1566 | urb->transfer_dma)) | |
1567 | ret = -EAGAIN; | |
1568 | else | |
1569 | urb->transfer_flags |= URB_DMA_MAP_SINGLE; | |
1570 | } | |
85e034fd | 1571 | } else if (hcd->driver->flags & HCD_LOCAL_MEM) { |
b3476675 MD |
1572 | ret = hcd_alloc_coherent( |
1573 | urb->dev->bus, mem_flags, | |
1574 | &urb->transfer_dma, | |
1575 | &urb->transfer_buffer, | |
1576 | urb->transfer_buffer_length, | |
1577 | dir); | |
ff9c895f AS |
1578 | if (ret == 0) |
1579 | urb->transfer_flags |= URB_MAP_LOCAL; | |
b3476675 | 1580 | } |
ff9c895f AS |
1581 | if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE | |
1582 | URB_SETUP_MAP_LOCAL))) | |
c8cf203a | 1583 | usb_hcd_unmap_urb_for_dma(hcd, urb); |
1da177e4 | 1584 | } |
b3476675 | 1585 | return ret; |
9a9bf406 | 1586 | } |
2694a48d | 1587 | EXPORT_SYMBOL_GPL(usb_hcd_map_urb_for_dma); |
1da177e4 | 1588 | |
9a9bf406 AS |
1589 | /*-------------------------------------------------------------------------*/ |
1590 | ||
1591 | /* may be called in any context with a valid urb->dev usecount | |
1592 | * caller surrenders "ownership" of urb | |
1593 | * expects usb_submit_urb() to have sanity checked and conditioned all | |
1594 | * inputs in the urb | |
1595 | */ | |
1596 | int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags) | |
1597 | { | |
1598 | int status; | |
1599 | struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); | |
1600 | ||
1601 | /* increment urb's reference count as part of giving it to the HCD | |
1602 | * (which will control it). HCD guarantees that it either returns | |
1603 | * an error or calls giveback(), but not both. | |
1604 | */ | |
1605 | usb_get_urb(urb); | |
1606 | atomic_inc(&urb->use_count); | |
4d59d8a1 | 1607 | atomic_inc(&urb->dev->urbnum); |
9a9bf406 AS |
1608 | usbmon_urb_submit(&hcd->self, urb); |
1609 | ||
1610 | /* NOTE requirements on root-hub callers (usbfs and the hub | |
1611 | * driver, for now): URBs' urb->transfer_buffer must be | |
1612 | * valid and usb_buffer_{sync,unmap}() not be needed, since | |
1613 | * they could clobber root hub response data. Also, control | |
1614 | * URBs must be submitted in process context with interrupts | |
1615 | * enabled. | |
1616 | */ | |
b3476675 | 1617 | |
ff9c895f | 1618 | if (is_root_hub(urb->dev)) { |
e9df41c5 | 1619 | status = rh_urb_enqueue(hcd, urb); |
ff9c895f AS |
1620 | } else { |
1621 | status = map_urb_for_dma(hcd, urb, mem_flags); | |
1622 | if (likely(status == 0)) { | |
1623 | status = hcd->driver->urb_enqueue(hcd, urb, mem_flags); | |
1624 | if (unlikely(status)) | |
2694a48d | 1625 | unmap_urb_for_dma(hcd, urb); |
ff9c895f AS |
1626 | } |
1627 | } | |
9a9bf406 AS |
1628 | |
1629 | if (unlikely(status)) { | |
1da177e4 | 1630 | usbmon_urb_submit_error(&hcd->self, urb, status); |
b0d9efba | 1631 | urb->hcpriv = NULL; |
9a9bf406 AS |
1632 | INIT_LIST_HEAD(&urb->urb_list); |
1633 | atomic_dec(&urb->use_count); | |
4d59d8a1 | 1634 | atomic_dec(&urb->dev->urbnum); |
49367d8f | 1635 | if (atomic_read(&urb->reject)) |
9a9bf406 AS |
1636 | wake_up(&usb_kill_urb_queue); |
1637 | usb_put_urb(urb); | |
1da177e4 LT |
1638 | } |
1639 | return status; | |
1640 | } | |
1641 | ||
1642 | /*-------------------------------------------------------------------------*/ | |
1643 | ||
1da177e4 LT |
1644 | /* this makes the hcd giveback() the urb more quickly, by kicking it |
1645 | * off hardware queues (which may take a while) and returning it as | |
1646 | * soon as practical. we've already set up the urb's return status, | |
1647 | * but we can't know if the callback completed already. | |
1648 | */ | |
e9df41c5 | 1649 | static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status) |
1da177e4 LT |
1650 | { |
1651 | int value; | |
1652 | ||
809a58b8 | 1653 | if (is_root_hub(urb->dev)) |
e9df41c5 | 1654 | value = usb_rh_urb_dequeue(hcd, urb, status); |
1da177e4 LT |
1655 | else { |
1656 | ||
1657 | /* The only reason an HCD might fail this call is if | |
1658 | * it has not yet fully queued the urb to begin with. | |
1659 | * Such failures should be harmless. */ | |
e9df41c5 | 1660 | value = hcd->driver->urb_dequeue(hcd, urb, status); |
1da177e4 | 1661 | } |
1da177e4 LT |
1662 | return value; |
1663 | } | |
1664 | ||
1665 | /* | |
1666 | * called in any context | |
1667 | * | |
1668 | * caller guarantees urb won't be recycled till both unlink() | |
1669 | * and the urb's completion function return | |
1670 | */ | |
a6d2bb9f | 1671 | int usb_hcd_unlink_urb (struct urb *urb, int status) |
1da177e4 | 1672 | { |
9a9bf406 | 1673 | struct usb_hcd *hcd; |
c9919790 | 1674 | struct usb_device *udev = urb->dev; |
cde217a5 AS |
1675 | int retval = -EIDRM; |
1676 | unsigned long flags; | |
1da177e4 | 1677 | |
cde217a5 AS |
1678 | /* Prevent the device and bus from going away while |
1679 | * the unlink is carried out. If they are already gone | |
1680 | * then urb->use_count must be 0, since disconnected | |
1681 | * devices can't have any active URBs. | |
1682 | */ | |
1683 | spin_lock_irqsave(&hcd_urb_unlink_lock, flags); | |
1684 | if (atomic_read(&urb->use_count) > 0) { | |
1685 | retval = 0; | |
c9919790 | 1686 | usb_get_dev(udev); |
cde217a5 AS |
1687 | } |
1688 | spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags); | |
1689 | if (retval == 0) { | |
1690 | hcd = bus_to_hcd(urb->dev->bus); | |
1691 | retval = unlink1(hcd, urb, status); | |
c9919790 AS |
1692 | if (retval == 0) |
1693 | retval = -EINPROGRESS; | |
1694 | else if (retval != -EIDRM && retval != -EBUSY) | |
1695 | dev_dbg(&udev->dev, "hcd_unlink_urb %p fail %d\n", | |
1696 | urb, retval); | |
1697 | usb_put_dev(udev); | |
cde217a5 | 1698 | } |
1da177e4 LT |
1699 | return retval; |
1700 | } | |
1701 | ||
1702 | /*-------------------------------------------------------------------------*/ | |
1703 | ||
94dfd7ed ML |
1704 | static void __usb_hcd_giveback_urb(struct urb *urb) |
1705 | { | |
1706 | struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus); | |
6ec4147e | 1707 | struct usb_anchor *anchor = urb->anchor; |
94dfd7ed ML |
1708 | int status = urb->unlinked; |
1709 | unsigned long flags; | |
1710 | ||
1711 | urb->hcpriv = NULL; | |
1712 | if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) && | |
1713 | urb->actual_length < urb->transfer_buffer_length && | |
1714 | !status)) | |
1715 | status = -EREMOTEIO; | |
1716 | ||
1717 | unmap_urb_for_dma(hcd, urb); | |
1718 | usbmon_urb_complete(&hcd->self, urb, status); | |
6ec4147e | 1719 | usb_anchor_suspend_wakeups(anchor); |
94dfd7ed | 1720 | usb_unanchor_urb(urb); |
0cfbd328 MS |
1721 | if (likely(status == 0)) |
1722 | usb_led_activity(USB_LED_EVENT_HOST); | |
94dfd7ed ML |
1723 | |
1724 | /* pass ownership to the completion handler */ | |
1725 | urb->status = status; | |
1726 | ||
1727 | /* | |
1728 | * We disable local IRQs here avoid possible deadlock because | |
1729 | * drivers may call spin_lock() to hold lock which might be | |
1730 | * acquired in one hard interrupt handler. | |
1731 | * | |
1732 | * The local_irq_save()/local_irq_restore() around complete() | |
1733 | * will be removed if current USB drivers have been cleaned up | |
1734 | * and no one may trigger the above deadlock situation when | |
1735 | * running complete() in tasklet. | |
1736 | */ | |
1737 | local_irq_save(flags); | |
1738 | urb->complete(urb); | |
1739 | local_irq_restore(flags); | |
1740 | ||
6ec4147e | 1741 | usb_anchor_resume_wakeups(anchor); |
94dfd7ed ML |
1742 | atomic_dec(&urb->use_count); |
1743 | if (unlikely(atomic_read(&urb->reject))) | |
1744 | wake_up(&usb_kill_urb_queue); | |
1745 | usb_put_urb(urb); | |
1746 | } | |
1747 | ||
1748 | static void usb_giveback_urb_bh(unsigned long param) | |
1749 | { | |
1750 | struct giveback_urb_bh *bh = (struct giveback_urb_bh *)param; | |
1751 | struct list_head local_list; | |
1752 | ||
1753 | spin_lock_irq(&bh->lock); | |
1754 | bh->running = true; | |
1755 | restart: | |
1756 | list_replace_init(&bh->head, &local_list); | |
1757 | spin_unlock_irq(&bh->lock); | |
1758 | ||
1759 | while (!list_empty(&local_list)) { | |
1760 | struct urb *urb; | |
1761 | ||
1762 | urb = list_entry(local_list.next, struct urb, urb_list); | |
1763 | list_del_init(&urb->urb_list); | |
c7ccde6e | 1764 | bh->completing_ep = urb->ep; |
94dfd7ed | 1765 | __usb_hcd_giveback_urb(urb); |
c7ccde6e | 1766 | bh->completing_ep = NULL; |
94dfd7ed ML |
1767 | } |
1768 | ||
1769 | /* check if there are new URBs to giveback */ | |
1770 | spin_lock_irq(&bh->lock); | |
1771 | if (!list_empty(&bh->head)) | |
1772 | goto restart; | |
1773 | bh->running = false; | |
1774 | spin_unlock_irq(&bh->lock); | |
1775 | } | |
1776 | ||
32aca560 AS |
1777 | /** |
1778 | * usb_hcd_giveback_urb - return URB from HCD to device driver | |
1779 | * @hcd: host controller returning the URB | |
1780 | * @urb: urb being returned to the USB device driver. | |
4a00027d | 1781 | * @status: completion status code for the URB. |
32aca560 AS |
1782 | * Context: in_interrupt() |
1783 | * | |
1784 | * This hands the URB from HCD to its USB device driver, using its | |
1785 | * completion function. The HCD has freed all per-urb resources | |
1786 | * (and is done using urb->hcpriv). It also released all HCD locks; | |
1787 | * the device driver won't cause problems if it frees, modifies, | |
1788 | * or resubmits this URB. | |
eb231054 | 1789 | * |
4a00027d | 1790 | * If @urb was unlinked, the value of @status will be overridden by |
eb231054 AS |
1791 | * @urb->unlinked. Erroneous short transfers are detected in case |
1792 | * the HCD hasn't checked for them. | |
32aca560 | 1793 | */ |
4a00027d | 1794 | void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status) |
32aca560 | 1795 | { |
94dfd7ed ML |
1796 | struct giveback_urb_bh *bh; |
1797 | bool running, high_prio_bh; | |
32aca560 | 1798 | |
94dfd7ed ML |
1799 | /* pass status to tasklet via unlinked */ |
1800 | if (likely(!urb->unlinked)) | |
1801 | urb->unlinked = status; | |
1f5a3d0f | 1802 | |
94dfd7ed ML |
1803 | if (!hcd_giveback_urb_in_bh(hcd) && !is_root_hub(urb->dev)) { |
1804 | __usb_hcd_giveback_urb(urb); | |
1805 | return; | |
1806 | } | |
1807 | ||
1808 | if (usb_pipeisoc(urb->pipe) || usb_pipeint(urb->pipe)) { | |
1809 | bh = &hcd->high_prio_bh; | |
1810 | high_prio_bh = true; | |
1811 | } else { | |
1812 | bh = &hcd->low_prio_bh; | |
1813 | high_prio_bh = false; | |
1814 | } | |
1815 | ||
1816 | spin_lock(&bh->lock); | |
1817 | list_add_tail(&urb->urb_list, &bh->head); | |
1818 | running = bh->running; | |
1819 | spin_unlock(&bh->lock); | |
1820 | ||
1821 | if (running) | |
1822 | ; | |
1823 | else if (high_prio_bh) | |
1824 | tasklet_hi_schedule(&bh->bh); | |
1825 | else | |
1826 | tasklet_schedule(&bh->bh); | |
32aca560 | 1827 | } |
782e70c6 | 1828 | EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb); |
32aca560 AS |
1829 | |
1830 | /*-------------------------------------------------------------------------*/ | |
1831 | ||
95cf82f9 AS |
1832 | /* Cancel all URBs pending on this endpoint and wait for the endpoint's |
1833 | * queue to drain completely. The caller must first insure that no more | |
1834 | * URBs can be submitted for this endpoint. | |
1da177e4 | 1835 | */ |
95cf82f9 | 1836 | void usb_hcd_flush_endpoint(struct usb_device *udev, |
a6d2bb9f | 1837 | struct usb_host_endpoint *ep) |
1da177e4 LT |
1838 | { |
1839 | struct usb_hcd *hcd; | |
1840 | struct urb *urb; | |
1841 | ||
95cf82f9 AS |
1842 | if (!ep) |
1843 | return; | |
9a9bf406 | 1844 | might_sleep(); |
17200583 | 1845 | hcd = bus_to_hcd(udev->bus); |
1da177e4 | 1846 | |
95cf82f9 | 1847 | /* No more submits can occur */ |
9a9bf406 | 1848 | spin_lock_irq(&hcd_urb_list_lock); |
ddc1fd6a | 1849 | rescan: |
1da177e4 | 1850 | list_for_each_entry (urb, &ep->urb_list, urb_list) { |
5e60a161 | 1851 | int is_in; |
1da177e4 | 1852 | |
eb231054 | 1853 | if (urb->unlinked) |
1da177e4 LT |
1854 | continue; |
1855 | usb_get_urb (urb); | |
5e60a161 | 1856 | is_in = usb_urb_dir_in(urb); |
809a58b8 | 1857 | spin_unlock(&hcd_urb_list_lock); |
1da177e4 | 1858 | |
e9df41c5 AS |
1859 | /* kick hcd */ |
1860 | unlink1(hcd, urb, -ESHUTDOWN); | |
1861 | dev_dbg (hcd->self.controller, | |
1862 | "shutdown urb %p ep%d%s%s\n", | |
1863 | urb, usb_endpoint_num(&ep->desc), | |
1864 | is_in ? "in" : "out", | |
1865 | ({ char *s; | |
1866 | ||
1867 | switch (usb_endpoint_type(&ep->desc)) { | |
1868 | case USB_ENDPOINT_XFER_CONTROL: | |
1869 | s = ""; break; | |
1870 | case USB_ENDPOINT_XFER_BULK: | |
1871 | s = "-bulk"; break; | |
1872 | case USB_ENDPOINT_XFER_INT: | |
1873 | s = "-intr"; break; | |
1874 | default: | |
14557359 | 1875 | s = "-iso"; break; |
e9df41c5 AS |
1876 | }; |
1877 | s; | |
1878 | })); | |
1da177e4 LT |
1879 | usb_put_urb (urb); |
1880 | ||
1881 | /* list contents may have changed */ | |
ddc1fd6a | 1882 | spin_lock(&hcd_urb_list_lock); |
1da177e4 LT |
1883 | goto rescan; |
1884 | } | |
9a9bf406 | 1885 | spin_unlock_irq(&hcd_urb_list_lock); |
1da177e4 | 1886 | |
95cf82f9 | 1887 | /* Wait until the endpoint queue is completely empty */ |
455b25fb | 1888 | while (!list_empty (&ep->urb_list)) { |
809a58b8 | 1889 | spin_lock_irq(&hcd_urb_list_lock); |
455b25fb AS |
1890 | |
1891 | /* The list may have changed while we acquired the spinlock */ | |
1892 | urb = NULL; | |
1893 | if (!list_empty (&ep->urb_list)) { | |
1894 | urb = list_entry (ep->urb_list.prev, struct urb, | |
1895 | urb_list); | |
1896 | usb_get_urb (urb); | |
1897 | } | |
809a58b8 | 1898 | spin_unlock_irq(&hcd_urb_list_lock); |
455b25fb AS |
1899 | |
1900 | if (urb) { | |
1901 | usb_kill_urb (urb); | |
1902 | usb_put_urb (urb); | |
1903 | } | |
1904 | } | |
1da177e4 LT |
1905 | } |
1906 | ||
3f0479e0 | 1907 | /** |
70445ae6 RD |
1908 | * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds |
1909 | * the bus bandwidth | |
1910 | * @udev: target &usb_device | |
3f0479e0 SS |
1911 | * @new_config: new configuration to install |
1912 | * @cur_alt: the current alternate interface setting | |
1913 | * @new_alt: alternate interface setting that is being installed | |
1914 | * | |
1915 | * To change configurations, pass in the new configuration in new_config, | |
1916 | * and pass NULL for cur_alt and new_alt. | |
1917 | * | |
1918 | * To reset a device's configuration (put the device in the ADDRESSED state), | |
1919 | * pass in NULL for new_config, cur_alt, and new_alt. | |
1920 | * | |
1921 | * To change alternate interface settings, pass in NULL for new_config, | |
1922 | * pass in the current alternate interface setting in cur_alt, | |
1923 | * and pass in the new alternate interface setting in new_alt. | |
1924 | * | |
626f090c | 1925 | * Return: An error if the requested bandwidth change exceeds the |
3f0479e0 | 1926 | * bus bandwidth or host controller internal resources. |
79abb1ab | 1927 | */ |
3f0479e0 | 1928 | int usb_hcd_alloc_bandwidth(struct usb_device *udev, |
79abb1ab | 1929 | struct usb_host_config *new_config, |
3f0479e0 SS |
1930 | struct usb_host_interface *cur_alt, |
1931 | struct usb_host_interface *new_alt) | |
79abb1ab SS |
1932 | { |
1933 | int num_intfs, i, j; | |
576a362a | 1934 | struct usb_host_interface *alt = NULL; |
79abb1ab SS |
1935 | int ret = 0; |
1936 | struct usb_hcd *hcd; | |
1937 | struct usb_host_endpoint *ep; | |
1938 | ||
1939 | hcd = bus_to_hcd(udev->bus); | |
1940 | if (!hcd->driver->check_bandwidth) | |
1941 | return 0; | |
1942 | ||
1943 | /* Configuration is being removed - set configuration 0 */ | |
3f0479e0 | 1944 | if (!new_config && !cur_alt) { |
79abb1ab SS |
1945 | for (i = 1; i < 16; ++i) { |
1946 | ep = udev->ep_out[i]; | |
1947 | if (ep) | |
1948 | hcd->driver->drop_endpoint(hcd, udev, ep); | |
1949 | ep = udev->ep_in[i]; | |
1950 | if (ep) | |
1951 | hcd->driver->drop_endpoint(hcd, udev, ep); | |
1952 | } | |
1953 | hcd->driver->check_bandwidth(hcd, udev); | |
1954 | return 0; | |
1955 | } | |
1956 | /* Check if the HCD says there's enough bandwidth. Enable all endpoints | |
1957 | * each interface's alt setting 0 and ask the HCD to check the bandwidth | |
1958 | * of the bus. There will always be bandwidth for endpoint 0, so it's | |
1959 | * ok to exclude it. | |
1960 | */ | |
1961 | if (new_config) { | |
1962 | num_intfs = new_config->desc.bNumInterfaces; | |
1963 | /* Remove endpoints (except endpoint 0, which is always on the | |
1964 | * schedule) from the old config from the schedule | |
1965 | */ | |
1966 | for (i = 1; i < 16; ++i) { | |
1967 | ep = udev->ep_out[i]; | |
1968 | if (ep) { | |
1969 | ret = hcd->driver->drop_endpoint(hcd, udev, ep); | |
1970 | if (ret < 0) | |
1971 | goto reset; | |
1972 | } | |
1973 | ep = udev->ep_in[i]; | |
1974 | if (ep) { | |
1975 | ret = hcd->driver->drop_endpoint(hcd, udev, ep); | |
1976 | if (ret < 0) | |
1977 | goto reset; | |
1978 | } | |
1979 | } | |
1980 | for (i = 0; i < num_intfs; ++i) { | |
d837e219 SS |
1981 | struct usb_host_interface *first_alt; |
1982 | int iface_num; | |
1983 | ||
1984 | first_alt = &new_config->intf_cache[i]->altsetting[0]; | |
1985 | iface_num = first_alt->desc.bInterfaceNumber; | |
91017f9c | 1986 | /* Set up endpoints for alternate interface setting 0 */ |
d837e219 | 1987 | alt = usb_find_alt_setting(new_config, iface_num, 0); |
3f0479e0 SS |
1988 | if (!alt) |
1989 | /* No alt setting 0? Pick the first setting. */ | |
d837e219 | 1990 | alt = first_alt; |
3f0479e0 | 1991 | |
79abb1ab SS |
1992 | for (j = 0; j < alt->desc.bNumEndpoints; j++) { |
1993 | ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]); | |
1994 | if (ret < 0) | |
1995 | goto reset; | |
1996 | } | |
1997 | } | |
1998 | } | |
3f0479e0 | 1999 | if (cur_alt && new_alt) { |
04a723ea SS |
2000 | struct usb_interface *iface = usb_ifnum_to_if(udev, |
2001 | cur_alt->desc.bInterfaceNumber); | |
2002 | ||
8a9af4fd SS |
2003 | if (!iface) |
2004 | return -EINVAL; | |
04a723ea SS |
2005 | if (iface->resetting_device) { |
2006 | /* | |
2007 | * The USB core just reset the device, so the xHCI host | |
2008 | * and the device will think alt setting 0 is installed. | |
2009 | * However, the USB core will pass in the alternate | |
2010 | * setting installed before the reset as cur_alt. Dig | |
2011 | * out the alternate setting 0 structure, or the first | |
2012 | * alternate setting if a broken device doesn't have alt | |
2013 | * setting 0. | |
2014 | */ | |
2015 | cur_alt = usb_altnum_to_altsetting(iface, 0); | |
2016 | if (!cur_alt) | |
2017 | cur_alt = &iface->altsetting[0]; | |
2018 | } | |
2019 | ||
3f0479e0 SS |
2020 | /* Drop all the endpoints in the current alt setting */ |
2021 | for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) { | |
2022 | ret = hcd->driver->drop_endpoint(hcd, udev, | |
2023 | &cur_alt->endpoint[i]); | |
2024 | if (ret < 0) | |
2025 | goto reset; | |
2026 | } | |
2027 | /* Add all the endpoints in the new alt setting */ | |
2028 | for (i = 0; i < new_alt->desc.bNumEndpoints; i++) { | |
2029 | ret = hcd->driver->add_endpoint(hcd, udev, | |
2030 | &new_alt->endpoint[i]); | |
2031 | if (ret < 0) | |
2032 | goto reset; | |
2033 | } | |
2034 | } | |
79abb1ab SS |
2035 | ret = hcd->driver->check_bandwidth(hcd, udev); |
2036 | reset: | |
2037 | if (ret < 0) | |
2038 | hcd->driver->reset_bandwidth(hcd, udev); | |
2039 | return ret; | |
2040 | } | |
2041 | ||
95cf82f9 AS |
2042 | /* Disables the endpoint: synchronizes with the hcd to make sure all |
2043 | * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must | |
2044 | * have been called previously. Use for set_configuration, set_interface, | |
2045 | * driver removal, physical disconnect. | |
2046 | * | |
2047 | * example: a qh stored in ep->hcpriv, holding state related to endpoint | |
2048 | * type, maxpacket size, toggle, halt status, and scheduling. | |
2049 | */ | |
2050 | void usb_hcd_disable_endpoint(struct usb_device *udev, | |
2051 | struct usb_host_endpoint *ep) | |
2052 | { | |
2053 | struct usb_hcd *hcd; | |
2054 | ||
2055 | might_sleep(); | |
2056 | hcd = bus_to_hcd(udev->bus); | |
2057 | if (hcd->driver->endpoint_disable) | |
2058 | hcd->driver->endpoint_disable(hcd, ep); | |
2059 | } | |
2060 | ||
3444b26a DV |
2061 | /** |
2062 | * usb_hcd_reset_endpoint - reset host endpoint state | |
2063 | * @udev: USB device. | |
2064 | * @ep: the endpoint to reset. | |
2065 | * | |
2066 | * Resets any host endpoint state such as the toggle bit, sequence | |
2067 | * number and current window. | |
2068 | */ | |
2069 | void usb_hcd_reset_endpoint(struct usb_device *udev, | |
2070 | struct usb_host_endpoint *ep) | |
2071 | { | |
2072 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); | |
2073 | ||
2074 | if (hcd->driver->endpoint_reset) | |
2075 | hcd->driver->endpoint_reset(hcd, ep); | |
2076 | else { | |
2077 | int epnum = usb_endpoint_num(&ep->desc); | |
2078 | int is_out = usb_endpoint_dir_out(&ep->desc); | |
2079 | int is_control = usb_endpoint_xfer_control(&ep->desc); | |
2080 | ||
2081 | usb_settoggle(udev, epnum, is_out, 0); | |
2082 | if (is_control) | |
2083 | usb_settoggle(udev, epnum, !is_out, 0); | |
2084 | } | |
2085 | } | |
2086 | ||
eab1cafc SS |
2087 | /** |
2088 | * usb_alloc_streams - allocate bulk endpoint stream IDs. | |
2089 | * @interface: alternate setting that includes all endpoints. | |
2090 | * @eps: array of endpoints that need streams. | |
2091 | * @num_eps: number of endpoints in the array. | |
2092 | * @num_streams: number of streams to allocate. | |
2093 | * @mem_flags: flags hcd should use to allocate memory. | |
2094 | * | |
626f090c | 2095 | * Sets up a group of bulk endpoints to have @num_streams stream IDs available. |
eab1cafc SS |
2096 | * Drivers may queue multiple transfers to different stream IDs, which may |
2097 | * complete in a different order than they were queued. | |
626f090c YB |
2098 | * |
2099 | * Return: On success, the number of allocated streams. On failure, a negative | |
2100 | * error code. | |
eab1cafc SS |
2101 | */ |
2102 | int usb_alloc_streams(struct usb_interface *interface, | |
2103 | struct usb_host_endpoint **eps, unsigned int num_eps, | |
2104 | unsigned int num_streams, gfp_t mem_flags) | |
2105 | { | |
2106 | struct usb_hcd *hcd; | |
2107 | struct usb_device *dev; | |
8d4f70b2 | 2108 | int i, ret; |
eab1cafc SS |
2109 | |
2110 | dev = interface_to_usbdev(interface); | |
2111 | hcd = bus_to_hcd(dev->bus); | |
2112 | if (!hcd->driver->alloc_streams || !hcd->driver->free_streams) | |
2113 | return -EINVAL; | |
2114 | if (dev->speed != USB_SPEED_SUPER) | |
2115 | return -EINVAL; | |
90a646c7 HG |
2116 | if (dev->state < USB_STATE_CONFIGURED) |
2117 | return -ENODEV; | |
eab1cafc | 2118 | |
8d4f70b2 HG |
2119 | for (i = 0; i < num_eps; i++) { |
2120 | /* Streams only apply to bulk endpoints. */ | |
eab1cafc SS |
2121 | if (!usb_endpoint_xfer_bulk(&eps[i]->desc)) |
2122 | return -EINVAL; | |
8d4f70b2 HG |
2123 | /* Re-alloc is not allowed */ |
2124 | if (eps[i]->streams) | |
2125 | return -EINVAL; | |
2126 | } | |
eab1cafc | 2127 | |
8d4f70b2 | 2128 | ret = hcd->driver->alloc_streams(hcd, dev, eps, num_eps, |
eab1cafc | 2129 | num_streams, mem_flags); |
8d4f70b2 HG |
2130 | if (ret < 0) |
2131 | return ret; | |
2132 | ||
2133 | for (i = 0; i < num_eps; i++) | |
2134 | eps[i]->streams = ret; | |
2135 | ||
2136 | return ret; | |
eab1cafc SS |
2137 | } |
2138 | EXPORT_SYMBOL_GPL(usb_alloc_streams); | |
2139 | ||
2140 | /** | |
2141 | * usb_free_streams - free bulk endpoint stream IDs. | |
2142 | * @interface: alternate setting that includes all endpoints. | |
2143 | * @eps: array of endpoints to remove streams from. | |
2144 | * @num_eps: number of endpoints in the array. | |
2145 | * @mem_flags: flags hcd should use to allocate memory. | |
2146 | * | |
2147 | * Reverts a group of bulk endpoints back to not using stream IDs. | |
2148 | * Can fail if we are given bad arguments, or HCD is broken. | |
6c74dada | 2149 | * |
12d4bbce | 2150 | * Return: 0 on success. On failure, a negative error code. |
eab1cafc | 2151 | */ |
6c74dada | 2152 | int usb_free_streams(struct usb_interface *interface, |
eab1cafc SS |
2153 | struct usb_host_endpoint **eps, unsigned int num_eps, |
2154 | gfp_t mem_flags) | |
2155 | { | |
2156 | struct usb_hcd *hcd; | |
2157 | struct usb_device *dev; | |
8d4f70b2 | 2158 | int i, ret; |
eab1cafc SS |
2159 | |
2160 | dev = interface_to_usbdev(interface); | |
2161 | hcd = bus_to_hcd(dev->bus); | |
2162 | if (dev->speed != USB_SPEED_SUPER) | |
6c74dada | 2163 | return -EINVAL; |
eab1cafc | 2164 | |
8d4f70b2 | 2165 | /* Double-free is not allowed */ |
eab1cafc | 2166 | for (i = 0; i < num_eps; i++) |
8d4f70b2 | 2167 | if (!eps[i] || !eps[i]->streams) |
6c74dada | 2168 | return -EINVAL; |
eab1cafc | 2169 | |
8d4f70b2 HG |
2170 | ret = hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags); |
2171 | if (ret < 0) | |
2172 | return ret; | |
2173 | ||
2174 | for (i = 0; i < num_eps; i++) | |
2175 | eps[i]->streams = 0; | |
2176 | ||
2177 | return ret; | |
eab1cafc SS |
2178 | } |
2179 | EXPORT_SYMBOL_GPL(usb_free_streams); | |
2180 | ||
cde217a5 AS |
2181 | /* Protect against drivers that try to unlink URBs after the device |
2182 | * is gone, by waiting until all unlinks for @udev are finished. | |
2183 | * Since we don't currently track URBs by device, simply wait until | |
2184 | * nothing is running in the locked region of usb_hcd_unlink_urb(). | |
2185 | */ | |
2186 | void usb_hcd_synchronize_unlinks(struct usb_device *udev) | |
2187 | { | |
2188 | spin_lock_irq(&hcd_urb_unlink_lock); | |
2189 | spin_unlock_irq(&hcd_urb_unlink_lock); | |
2190 | } | |
2191 | ||
1da177e4 LT |
2192 | /*-------------------------------------------------------------------------*/ |
2193 | ||
32aca560 AS |
2194 | /* called in any context */ |
2195 | int usb_hcd_get_frame_number (struct usb_device *udev) | |
2196 | { | |
2197 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); | |
2198 | ||
9b37596a | 2199 | if (!HCD_RH_RUNNING(hcd)) |
32aca560 AS |
2200 | return -ESHUTDOWN; |
2201 | return hcd->driver->get_frame_number (hcd); | |
2202 | } | |
2203 | ||
2204 | /*-------------------------------------------------------------------------*/ | |
2205 | ||
9293677a | 2206 | #ifdef CONFIG_PM |
1da177e4 | 2207 | |
65bfd296 | 2208 | int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg) |
1da177e4 | 2209 | { |
686314cf AS |
2210 | struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self); |
2211 | int status; | |
2212 | int old_state = hcd->state; | |
1da177e4 | 2213 | |
30b1a7a3 AS |
2214 | dev_dbg(&rhdev->dev, "bus %ssuspend, wakeup %d\n", |
2215 | (PMSG_IS_AUTO(msg) ? "auto-" : ""), | |
2216 | rhdev->do_remote_wakeup); | |
9b37596a AS |
2217 | if (HCD_DEAD(hcd)) { |
2218 | dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "suspend"); | |
2219 | return 0; | |
2220 | } | |
2221 | ||
686314cf AS |
2222 | if (!hcd->driver->bus_suspend) { |
2223 | status = -ENOENT; | |
2224 | } else { | |
9b37596a | 2225 | clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
686314cf AS |
2226 | hcd->state = HC_STATE_QUIESCING; |
2227 | status = hcd->driver->bus_suspend(hcd); | |
2228 | } | |
2229 | if (status == 0) { | |
2230 | usb_set_device_state(rhdev, USB_STATE_SUSPENDED); | |
9293677a | 2231 | hcd->state = HC_STATE_SUSPENDED; |
879d38e6 AS |
2232 | |
2233 | /* Did we race with a root-hub wakeup event? */ | |
2234 | if (rhdev->do_remote_wakeup) { | |
2235 | char buffer[6]; | |
2236 | ||
2237 | status = hcd->driver->hub_status_data(hcd, buffer); | |
2238 | if (status != 0) { | |
2239 | dev_dbg(&rhdev->dev, "suspend raced with wakeup event\n"); | |
2240 | hcd_bus_resume(rhdev, PMSG_AUTO_RESUME); | |
2241 | status = -EBUSY; | |
2242 | } | |
2243 | } | |
686314cf | 2244 | } else { |
9b37596a AS |
2245 | spin_lock_irq(&hcd_root_hub_lock); |
2246 | if (!HCD_DEAD(hcd)) { | |
2247 | set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); | |
2248 | hcd->state = old_state; | |
2249 | } | |
2250 | spin_unlock_irq(&hcd_root_hub_lock); | |
686314cf | 2251 | dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", |
9293677a | 2252 | "suspend", status); |
686314cf | 2253 | } |
9293677a | 2254 | return status; |
1da177e4 LT |
2255 | } |
2256 | ||
65bfd296 | 2257 | int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg) |
1da177e4 | 2258 | { |
686314cf AS |
2259 | struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self); |
2260 | int status; | |
cfa59dab | 2261 | int old_state = hcd->state; |
1da177e4 | 2262 | |
30b1a7a3 AS |
2263 | dev_dbg(&rhdev->dev, "usb %sresume\n", |
2264 | (PMSG_IS_AUTO(msg) ? "auto-" : "")); | |
9b37596a AS |
2265 | if (HCD_DEAD(hcd)) { |
2266 | dev_dbg(&rhdev->dev, "skipped %s of dead bus\n", "resume"); | |
2267 | return 0; | |
2268 | } | |
0c0382e3 | 2269 | if (!hcd->driver->bus_resume) |
9293677a | 2270 | return -ENOENT; |
9b37596a | 2271 | if (HCD_RH_RUNNING(hcd)) |
979d5199 | 2272 | return 0; |
686314cf | 2273 | |
9293677a | 2274 | hcd->state = HC_STATE_RESUMING; |
686314cf | 2275 | status = hcd->driver->bus_resume(hcd); |
bf3d7d40 | 2276 | clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags); |
686314cf | 2277 | if (status == 0) { |
bfd1e910 AS |
2278 | struct usb_device *udev; |
2279 | int port1; | |
2280 | ||
9b37596a AS |
2281 | spin_lock_irq(&hcd_root_hub_lock); |
2282 | if (!HCD_DEAD(hcd)) { | |
2283 | usb_set_device_state(rhdev, rhdev->actconfig | |
2284 | ? USB_STATE_CONFIGURED | |
2285 | : USB_STATE_ADDRESS); | |
2286 | set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); | |
2287 | hcd->state = HC_STATE_RUNNING; | |
2288 | } | |
2289 | spin_unlock_irq(&hcd_root_hub_lock); | |
bfd1e910 AS |
2290 | |
2291 | /* | |
2292 | * Check whether any of the enabled ports on the root hub are | |
2293 | * unsuspended. If they are then a TRSMRCY delay is needed | |
2294 | * (this is what the USB-2 spec calls a "global resume"). | |
2295 | * Otherwise we can skip the delay. | |
2296 | */ | |
2297 | usb_hub_for_each_child(rhdev, port1, udev) { | |
2298 | if (udev->state != USB_STATE_NOTATTACHED && | |
2299 | !udev->port_is_suspended) { | |
2300 | usleep_range(10000, 11000); /* TRSMRCY */ | |
2301 | break; | |
2302 | } | |
2303 | } | |
686314cf | 2304 | } else { |
cfa59dab | 2305 | hcd->state = old_state; |
686314cf | 2306 | dev_dbg(&rhdev->dev, "bus %s fail, err %d\n", |
9293677a | 2307 | "resume", status); |
cfa59dab AS |
2308 | if (status != -ESHUTDOWN) |
2309 | usb_hc_died(hcd); | |
9293677a DB |
2310 | } |
2311 | return status; | |
1da177e4 LT |
2312 | } |
2313 | ||
6b157c9b AS |
2314 | /* Workqueue routine for root-hub remote wakeup */ |
2315 | static void hcd_resume_work(struct work_struct *work) | |
2316 | { | |
2317 | struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work); | |
2318 | struct usb_device *udev = hcd->self.root_hub; | |
2319 | ||
0534d468 | 2320 | usb_remote_wakeup(udev); |
6b157c9b AS |
2321 | } |
2322 | ||
1da177e4 | 2323 | /** |
14557359 | 2324 | * usb_hcd_resume_root_hub - called by HCD to resume its root hub |
1da177e4 LT |
2325 | * @hcd: host controller for this root hub |
2326 | * | |
2327 | * The USB host controller calls this function when its root hub is | |
2328 | * suspended (with the remote wakeup feature enabled) and a remote | |
6b157c9b AS |
2329 | * wakeup request is received. The routine submits a workqueue request |
2330 | * to resume the root hub (that is, manage its downstream ports again). | |
1da177e4 LT |
2331 | */ |
2332 | void usb_hcd_resume_root_hub (struct usb_hcd *hcd) | |
2333 | { | |
2334 | unsigned long flags; | |
2335 | ||
2336 | spin_lock_irqsave (&hcd_root_hub_lock, flags); | |
ff2f0787 AS |
2337 | if (hcd->rh_registered) { |
2338 | set_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags); | |
9bbdf1e0 | 2339 | queue_work(pm_wq, &hcd->wakeup_work); |
ff2f0787 | 2340 | } |
1da177e4 LT |
2341 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
2342 | } | |
9293677a | 2343 | EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); |
1da177e4 | 2344 | |
ceb6c9c8 | 2345 | #endif /* CONFIG_PM */ |
1da177e4 LT |
2346 | |
2347 | /*-------------------------------------------------------------------------*/ | |
2348 | ||
2349 | #ifdef CONFIG_USB_OTG | |
2350 | ||
2351 | /** | |
2352 | * usb_bus_start_enum - start immediate enumeration (for OTG) | |
2353 | * @bus: the bus (must use hcd framework) | |
2354 | * @port_num: 1-based number of port; usually bus->otg_port | |
2355 | * Context: in_interrupt() | |
2356 | * | |
2357 | * Starts enumeration, with an immediate reset followed later by | |
37ebb549 | 2358 | * hub_wq identifying and possibly configuring the device. |
1da177e4 LT |
2359 | * This is needed by OTG controller drivers, where it helps meet |
2360 | * HNP protocol timing requirements for starting a port reset. | |
626f090c YB |
2361 | * |
2362 | * Return: 0 if successful. | |
1da177e4 LT |
2363 | */ |
2364 | int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num) | |
2365 | { | |
2366 | struct usb_hcd *hcd; | |
2367 | int status = -EOPNOTSUPP; | |
2368 | ||
2369 | /* NOTE: since HNP can't start by grabbing the bus's address0_sem, | |
2370 | * boards with root hubs hooked up to internal devices (instead of | |
2371 | * just the OTG port) may need more attention to resetting... | |
2372 | */ | |
2373 | hcd = container_of (bus, struct usb_hcd, self); | |
2374 | if (port_num && hcd->driver->start_port_reset) | |
2375 | status = hcd->driver->start_port_reset(hcd, port_num); | |
2376 | ||
37ebb549 | 2377 | /* allocate hub_wq shortly after (first) root port reset finishes; |
1da177e4 LT |
2378 | * it may issue others, until at least 50 msecs have passed. |
2379 | */ | |
2380 | if (status == 0) | |
2381 | mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10)); | |
2382 | return status; | |
2383 | } | |
782e70c6 | 2384 | EXPORT_SYMBOL_GPL(usb_bus_start_enum); |
1da177e4 LT |
2385 | |
2386 | #endif | |
2387 | ||
2388 | /*-------------------------------------------------------------------------*/ | |
2389 | ||
1da177e4 LT |
2390 | /** |
2391 | * usb_hcd_irq - hook IRQs to HCD framework (bus glue) | |
2392 | * @irq: the IRQ being raised | |
2393 | * @__hcd: pointer to the HCD whose IRQ is being signaled | |
1da177e4 LT |
2394 | * |
2395 | * If the controller isn't HALTed, calls the driver's irq handler. | |
2396 | * Checks whether the controller is now dead. | |
626f090c YB |
2397 | * |
2398 | * Return: %IRQ_HANDLED if the IRQ was handled. %IRQ_NONE otherwise. | |
1da177e4 | 2399 | */ |
7d12e780 | 2400 | irqreturn_t usb_hcd_irq (int irq, void *__hcd) |
1da177e4 LT |
2401 | { |
2402 | struct usb_hcd *hcd = __hcd; | |
de85422b | 2403 | irqreturn_t rc; |
1da177e4 | 2404 | |
968b822c | 2405 | if (unlikely(HCD_DEAD(hcd) || !HCD_HW_ACCESSIBLE(hcd))) |
de85422b | 2406 | rc = IRQ_NONE; |
968b822c | 2407 | else if (hcd->driver->irq(hcd) == IRQ_NONE) |
de85422b | 2408 | rc = IRQ_NONE; |
968b822c | 2409 | else |
de85422b | 2410 | rc = IRQ_HANDLED; |
de85422b | 2411 | |
de85422b | 2412 | return rc; |
1da177e4 | 2413 | } |
43b86af8 | 2414 | EXPORT_SYMBOL_GPL(usb_hcd_irq); |
1da177e4 LT |
2415 | |
2416 | /*-------------------------------------------------------------------------*/ | |
2417 | ||
2418 | /** | |
2419 | * usb_hc_died - report abnormal shutdown of a host controller (bus glue) | |
2420 | * @hcd: pointer to the HCD representing the controller | |
2421 | * | |
2422 | * This is called by bus glue to report a USB host controller that died | |
2423 | * while operations may still have been pending. It's called automatically | |
c5635437 SS |
2424 | * by the PCI glue, so only glue for non-PCI busses should need to call it. |
2425 | * | |
2426 | * Only call this function with the primary HCD. | |
1da177e4 LT |
2427 | */ |
2428 | void usb_hc_died (struct usb_hcd *hcd) | |
2429 | { | |
2430 | unsigned long flags; | |
2431 | ||
2432 | dev_err (hcd->self.controller, "HC died; cleaning up\n"); | |
2433 | ||
2434 | spin_lock_irqsave (&hcd_root_hub_lock, flags); | |
9b37596a AS |
2435 | clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
2436 | set_bit(HCD_FLAG_DEAD, &hcd->flags); | |
1da177e4 | 2437 | if (hcd->rh_registered) { |
541c7d43 | 2438 | clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
1da177e4 | 2439 | |
37ebb549 | 2440 | /* make hub_wq clean up old urbs and devices */ |
1da177e4 LT |
2441 | usb_set_device_state (hcd->self.root_hub, |
2442 | USB_STATE_NOTATTACHED); | |
59d48b3f | 2443 | usb_kick_hub_wq(hcd->self.root_hub); |
1da177e4 | 2444 | } |
c5635437 SS |
2445 | if (usb_hcd_is_primary_hcd(hcd) && hcd->shared_hcd) { |
2446 | hcd = hcd->shared_hcd; | |
2447 | if (hcd->rh_registered) { | |
2448 | clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); | |
2449 | ||
37ebb549 | 2450 | /* make hub_wq clean up old urbs and devices */ |
c5635437 SS |
2451 | usb_set_device_state(hcd->self.root_hub, |
2452 | USB_STATE_NOTATTACHED); | |
59d48b3f | 2453 | usb_kick_hub_wq(hcd->self.root_hub); |
c5635437 SS |
2454 | } |
2455 | } | |
1da177e4 | 2456 | spin_unlock_irqrestore (&hcd_root_hub_lock, flags); |
c5635437 | 2457 | /* Make sure that the other roothub is also deallocated. */ |
1da177e4 LT |
2458 | } |
2459 | EXPORT_SYMBOL_GPL (usb_hc_died); | |
2460 | ||
2461 | /*-------------------------------------------------------------------------*/ | |
2462 | ||
94dfd7ed ML |
2463 | static void init_giveback_urb_bh(struct giveback_urb_bh *bh) |
2464 | { | |
2465 | ||
2466 | spin_lock_init(&bh->lock); | |
2467 | INIT_LIST_HEAD(&bh->head); | |
2468 | tasklet_init(&bh->bh, usb_giveback_urb_bh, (unsigned long)bh); | |
2469 | } | |
2470 | ||
1da177e4 | 2471 | /** |
c5635437 | 2472 | * usb_create_shared_hcd - create and initialize an HCD structure |
1da177e4 LT |
2473 | * @driver: HC driver that will use this hcd |
2474 | * @dev: device for this HC, stored in hcd->self.controller | |
2475 | * @bus_name: value to store in hcd->self.bus_name | |
c5635437 SS |
2476 | * @primary_hcd: a pointer to the usb_hcd structure that is sharing the |
2477 | * PCI device. Only allocate certain resources for the primary HCD | |
1da177e4 LT |
2478 | * Context: !in_interrupt() |
2479 | * | |
2480 | * Allocate a struct usb_hcd, with extra space at the end for the | |
2481 | * HC driver's private data. Initialize the generic members of the | |
2482 | * hcd structure. | |
2483 | * | |
626f090c YB |
2484 | * Return: On success, a pointer to the created and initialized HCD structure. |
2485 | * On failure (e.g. if memory is unavailable), %NULL. | |
1da177e4 | 2486 | */ |
c5635437 SS |
2487 | struct usb_hcd *usb_create_shared_hcd(const struct hc_driver *driver, |
2488 | struct device *dev, const char *bus_name, | |
2489 | struct usb_hcd *primary_hcd) | |
1da177e4 LT |
2490 | { |
2491 | struct usb_hcd *hcd; | |
2492 | ||
7b842b6e | 2493 | hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL); |
1da177e4 LT |
2494 | if (!hcd) { |
2495 | dev_dbg (dev, "hcd alloc failed\n"); | |
2496 | return NULL; | |
2497 | } | |
c5635437 SS |
2498 | if (primary_hcd == NULL) { |
2499 | hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex), | |
2500 | GFP_KERNEL); | |
2501 | if (!hcd->bandwidth_mutex) { | |
2502 | kfree(hcd); | |
2503 | dev_dbg(dev, "hcd bandwidth mutex alloc failed\n"); | |
2504 | return NULL; | |
2505 | } | |
2506 | mutex_init(hcd->bandwidth_mutex); | |
2507 | dev_set_drvdata(dev, hcd); | |
2508 | } else { | |
d8521afe | 2509 | mutex_lock(&usb_port_peer_mutex); |
c5635437 SS |
2510 | hcd->bandwidth_mutex = primary_hcd->bandwidth_mutex; |
2511 | hcd->primary_hcd = primary_hcd; | |
2512 | primary_hcd->primary_hcd = primary_hcd; | |
2513 | hcd->shared_hcd = primary_hcd; | |
2514 | primary_hcd->shared_hcd = hcd; | |
d8521afe | 2515 | mutex_unlock(&usb_port_peer_mutex); |
d673bfcb | 2516 | } |
d673bfcb | 2517 | |
17200583 | 2518 | kref_init(&hcd->kref); |
1da177e4 LT |
2519 | |
2520 | usb_bus_init(&hcd->self); | |
1da177e4 LT |
2521 | hcd->self.controller = dev; |
2522 | hcd->self.bus_name = bus_name; | |
dd990f16 | 2523 | hcd->self.uses_dma = (dev->dma_mask != NULL); |
1da177e4 LT |
2524 | |
2525 | init_timer(&hcd->rh_timer); | |
d5926ae7 AS |
2526 | hcd->rh_timer.function = rh_timer_func; |
2527 | hcd->rh_timer.data = (unsigned long) hcd; | |
ceb6c9c8 | 2528 | #ifdef CONFIG_PM |
6b157c9b AS |
2529 | INIT_WORK(&hcd->wakeup_work, hcd_resume_work); |
2530 | #endif | |
1da177e4 LT |
2531 | |
2532 | hcd->driver = driver; | |
83de4b2b | 2533 | hcd->speed = driver->flags & HCD_MASK; |
1da177e4 LT |
2534 | hcd->product_desc = (driver->product_desc) ? driver->product_desc : |
2535 | "USB Host Controller"; | |
1da177e4 LT |
2536 | return hcd; |
2537 | } | |
c5635437 SS |
2538 | EXPORT_SYMBOL_GPL(usb_create_shared_hcd); |
2539 | ||
2540 | /** | |
2541 | * usb_create_hcd - create and initialize an HCD structure | |
2542 | * @driver: HC driver that will use this hcd | |
2543 | * @dev: device for this HC, stored in hcd->self.controller | |
2544 | * @bus_name: value to store in hcd->self.bus_name | |
2545 | * Context: !in_interrupt() | |
2546 | * | |
2547 | * Allocate a struct usb_hcd, with extra space at the end for the | |
2548 | * HC driver's private data. Initialize the generic members of the | |
2549 | * hcd structure. | |
2550 | * | |
626f090c YB |
2551 | * Return: On success, a pointer to the created and initialized HCD |
2552 | * structure. On failure (e.g. if memory is unavailable), %NULL. | |
c5635437 SS |
2553 | */ |
2554 | struct usb_hcd *usb_create_hcd(const struct hc_driver *driver, | |
2555 | struct device *dev, const char *bus_name) | |
2556 | { | |
2557 | return usb_create_shared_hcd(driver, dev, bus_name, NULL); | |
2558 | } | |
782e70c6 | 2559 | EXPORT_SYMBOL_GPL(usb_create_hcd); |
1da177e4 | 2560 | |
c5635437 SS |
2561 | /* |
2562 | * Roothubs that share one PCI device must also share the bandwidth mutex. | |
2563 | * Don't deallocate the bandwidth_mutex until the last shared usb_hcd is | |
2564 | * deallocated. | |
2565 | * | |
2566 | * Make sure to only deallocate the bandwidth_mutex when the primary HCD is | |
d8521afe DW |
2567 | * freed. When hcd_release() is called for either hcd in a peer set |
2568 | * invalidate the peer's ->shared_hcd and ->primary_hcd pointers to | |
2569 | * block new peering attempts | |
c5635437 | 2570 | */ |
d8521afe | 2571 | static void hcd_release(struct kref *kref) |
17200583 AS |
2572 | { |
2573 | struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref); | |
2574 | ||
d8521afe | 2575 | mutex_lock(&usb_port_peer_mutex); |
c5635437 SS |
2576 | if (usb_hcd_is_primary_hcd(hcd)) |
2577 | kfree(hcd->bandwidth_mutex); | |
d8521afe DW |
2578 | if (hcd->shared_hcd) { |
2579 | struct usb_hcd *peer = hcd->shared_hcd; | |
2580 | ||
2581 | peer->shared_hcd = NULL; | |
2582 | if (peer->primary_hcd == hcd) | |
2583 | peer->primary_hcd = NULL; | |
2584 | } | |
2585 | mutex_unlock(&usb_port_peer_mutex); | |
17200583 AS |
2586 | kfree(hcd); |
2587 | } | |
2588 | ||
2589 | struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd) | |
2590 | { | |
2591 | if (hcd) | |
2592 | kref_get (&hcd->kref); | |
2593 | return hcd; | |
2594 | } | |
782e70c6 | 2595 | EXPORT_SYMBOL_GPL(usb_get_hcd); |
17200583 | 2596 | |
1da177e4 LT |
2597 | void usb_put_hcd (struct usb_hcd *hcd) |
2598 | { | |
17200583 AS |
2599 | if (hcd) |
2600 | kref_put (&hcd->kref, hcd_release); | |
1da177e4 | 2601 | } |
782e70c6 | 2602 | EXPORT_SYMBOL_GPL(usb_put_hcd); |
1da177e4 | 2603 | |
c5635437 SS |
2604 | int usb_hcd_is_primary_hcd(struct usb_hcd *hcd) |
2605 | { | |
2606 | if (!hcd->primary_hcd) | |
2607 | return 1; | |
2608 | return hcd == hcd->primary_hcd; | |
2609 | } | |
2610 | EXPORT_SYMBOL_GPL(usb_hcd_is_primary_hcd); | |
2611 | ||
3f5eb141 LT |
2612 | int usb_hcd_find_raw_port_number(struct usb_hcd *hcd, int port1) |
2613 | { | |
2614 | if (!hcd->driver->find_raw_port_number) | |
2615 | return port1; | |
2616 | ||
2617 | return hcd->driver->find_raw_port_number(hcd, port1); | |
2618 | } | |
2619 | ||
23e0d106 SS |
2620 | static int usb_hcd_request_irqs(struct usb_hcd *hcd, |
2621 | unsigned int irqnum, unsigned long irqflags) | |
2622 | { | |
2623 | int retval; | |
2624 | ||
2625 | if (hcd->driver->irq) { | |
e592c5d0 | 2626 | |
23e0d106 SS |
2627 | snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d", |
2628 | hcd->driver->description, hcd->self.busnum); | |
2629 | retval = request_irq(irqnum, &usb_hcd_irq, irqflags, | |
2630 | hcd->irq_descr, hcd); | |
2631 | if (retval != 0) { | |
2632 | dev_err(hcd->self.controller, | |
2633 | "request interrupt %d failed\n", | |
2634 | irqnum); | |
2635 | return retval; | |
2636 | } | |
2637 | hcd->irq = irqnum; | |
2638 | dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum, | |
2639 | (hcd->driver->flags & HCD_MEMORY) ? | |
2640 | "io mem" : "io base", | |
2641 | (unsigned long long)hcd->rsrc_start); | |
2642 | } else { | |
cd70469d | 2643 | hcd->irq = 0; |
23e0d106 SS |
2644 | if (hcd->rsrc_start) |
2645 | dev_info(hcd->self.controller, "%s 0x%08llx\n", | |
2646 | (hcd->driver->flags & HCD_MEMORY) ? | |
2647 | "io mem" : "io base", | |
2648 | (unsigned long long)hcd->rsrc_start); | |
2649 | } | |
2650 | return 0; | |
2651 | } | |
2652 | ||
d8521afe DW |
2653 | /* |
2654 | * Before we free this root hub, flush in-flight peering attempts | |
2655 | * and disable peer lookups | |
2656 | */ | |
2657 | static void usb_put_invalidate_rhdev(struct usb_hcd *hcd) | |
2658 | { | |
2659 | struct usb_device *rhdev; | |
2660 | ||
2661 | mutex_lock(&usb_port_peer_mutex); | |
2662 | rhdev = hcd->self.root_hub; | |
2663 | hcd->self.root_hub = NULL; | |
2664 | mutex_unlock(&usb_port_peer_mutex); | |
2665 | usb_put_dev(rhdev); | |
2666 | } | |
2667 | ||
1da177e4 LT |
2668 | /** |
2669 | * usb_add_hcd - finish generic HCD structure initialization and register | |
2670 | * @hcd: the usb_hcd structure to initialize | |
2671 | * @irqnum: Interrupt line to allocate | |
2672 | * @irqflags: Interrupt type flags | |
2673 | * | |
2674 | * Finish the remaining parts of generic HCD initialization: allocate the | |
2675 | * buffers of consistent memory, register the bus, request the IRQ line, | |
2676 | * and call the driver's reset() and start() routines. | |
2677 | */ | |
2678 | int usb_add_hcd(struct usb_hcd *hcd, | |
2679 | unsigned int irqnum, unsigned long irqflags) | |
2680 | { | |
8ec8d20b AS |
2681 | int retval; |
2682 | struct usb_device *rhdev; | |
1da177e4 | 2683 | |
3d46e73d | 2684 | if (IS_ENABLED(CONFIG_USB_PHY) && !hcd->usb_phy) { |
1ae5799e VB |
2685 | struct usb_phy *phy = usb_get_phy_dev(hcd->self.controller, 0); |
2686 | ||
2687 | if (IS_ERR(phy)) { | |
2688 | retval = PTR_ERR(phy); | |
2689 | if (retval == -EPROBE_DEFER) | |
2690 | return retval; | |
2691 | } else { | |
2692 | retval = usb_phy_init(phy); | |
2693 | if (retval) { | |
2694 | usb_put_phy(phy); | |
2695 | return retval; | |
2696 | } | |
3d46e73d | 2697 | hcd->usb_phy = phy; |
1ae5799e VB |
2698 | hcd->remove_phy = 1; |
2699 | } | |
2700 | } | |
2701 | ||
ef44cb42 | 2702 | if (IS_ENABLED(CONFIG_GENERIC_PHY) && !hcd->phy) { |
00433254 SS |
2703 | struct phy *phy = phy_get(hcd->self.controller, "usb"); |
2704 | ||
2705 | if (IS_ERR(phy)) { | |
2706 | retval = PTR_ERR(phy); | |
2707 | if (retval == -EPROBE_DEFER) | |
2708 | goto err_phy; | |
2709 | } else { | |
2710 | retval = phy_init(phy); | |
2711 | if (retval) { | |
2712 | phy_put(phy); | |
2713 | goto err_phy; | |
2714 | } | |
2715 | retval = phy_power_on(phy); | |
2716 | if (retval) { | |
2717 | phy_exit(phy); | |
2718 | phy_put(phy); | |
2719 | goto err_phy; | |
2720 | } | |
2721 | hcd->phy = phy; | |
ef44cb42 | 2722 | hcd->remove_phy = 1; |
00433254 SS |
2723 | } |
2724 | } | |
2725 | ||
1da177e4 LT |
2726 | dev_info(hcd->self.controller, "%s\n", hcd->product_desc); |
2727 | ||
c4fc2342 | 2728 | /* Keep old behaviour if authorized_default is not in [0, 1]. */ |
ff8e2c56 SK |
2729 | if (authorized_default < 0 || authorized_default > 1) { |
2730 | if (hcd->wireless) | |
2731 | clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); | |
2732 | else | |
2733 | set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); | |
2734 | } else { | |
2735 | if (authorized_default) | |
2736 | set_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); | |
2737 | else | |
2738 | clear_bit(HCD_FLAG_DEV_AUTHORIZED, &hcd->flags); | |
2739 | } | |
8de98402 BH |
2740 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
2741 | ||
6b2bd3c8 SK |
2742 | /* per default all interfaces are authorized */ |
2743 | set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags); | |
2744 | ||
b1e8f0a6 | 2745 | /* HC is in reset state, but accessible. Now do the one-time init, |
37ebb549 | 2746 | * bottom up so that hcds can customize the root hubs before hub_wq |
b1e8f0a6 DB |
2747 | * starts talking to them. (Note, bus id is assigned early too.) |
2748 | */ | |
0faaad46 KB |
2749 | retval = hcd_buffer_create(hcd); |
2750 | if (retval != 0) { | |
1da177e4 | 2751 | dev_dbg(hcd->self.controller, "pool alloc failed\n"); |
00433254 | 2752 | goto err_create_buf; |
1da177e4 LT |
2753 | } |
2754 | ||
0faaad46 KB |
2755 | retval = usb_register_bus(&hcd->self); |
2756 | if (retval < 0) | |
8ec8d20b | 2757 | goto err_register_bus; |
1da177e4 | 2758 | |
c688d621 GKH |
2759 | rhdev = usb_alloc_dev(NULL, &hcd->self, 0); |
2760 | if (rhdev == NULL) { | |
b1e8f0a6 DB |
2761 | dev_err(hcd->self.controller, "unable to allocate root hub\n"); |
2762 | retval = -ENOMEM; | |
2763 | goto err_allocate_root_hub; | |
2764 | } | |
d8521afe | 2765 | mutex_lock(&usb_port_peer_mutex); |
6d88e679 | 2766 | hcd->self.root_hub = rhdev; |
d8521afe | 2767 | mutex_unlock(&usb_port_peer_mutex); |
6b403b02 | 2768 | |
83de4b2b | 2769 | switch (hcd->speed) { |
6b403b02 SS |
2770 | case HCD_USB11: |
2771 | rhdev->speed = USB_SPEED_FULL; | |
2772 | break; | |
2773 | case HCD_USB2: | |
2774 | rhdev->speed = USB_SPEED_HIGH; | |
2775 | break; | |
1a81f881 TP |
2776 | case HCD_USB25: |
2777 | rhdev->speed = USB_SPEED_WIRELESS; | |
2778 | break; | |
6b403b02 | 2779 | case HCD_USB3: |
71175225 | 2780 | case HCD_USB31: |
6b403b02 SS |
2781 | rhdev->speed = USB_SPEED_SUPER; |
2782 | break; | |
2783 | default: | |
1d15ee4c | 2784 | retval = -EINVAL; |
96e077ae | 2785 | goto err_set_rh_speed; |
6b403b02 | 2786 | } |
b1e8f0a6 | 2787 | |
db4cefaa DB |
2788 | /* wakeup flag init defaults to "everything works" for root hubs, |
2789 | * but drivers can override it in reset() if needed, along with | |
2790 | * recording the overall controller's system wakeup capability. | |
2791 | */ | |
a6eeeb9f | 2792 | device_set_wakeup_capable(&rhdev->dev, 1); |
db4cefaa | 2793 | |
9b37596a AS |
2794 | /* HCD_FLAG_RH_RUNNING doesn't matter until the root hub is |
2795 | * registered. But since the controller can die at any time, | |
2796 | * let's initialize the flag before touching the hardware. | |
2797 | */ | |
2798 | set_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); | |
2799 | ||
b1e8f0a6 DB |
2800 | /* "reset" is misnamed; its role is now one-time init. the controller |
2801 | * should already have been reset (and boot firmware kicked off etc). | |
2802 | */ | |
0faaad46 KB |
2803 | if (hcd->driver->reset) { |
2804 | retval = hcd->driver->reset(hcd); | |
2805 | if (retval < 0) { | |
2806 | dev_err(hcd->self.controller, "can't setup: %d\n", | |
2807 | retval); | |
2808 | goto err_hcd_driver_setup; | |
2809 | } | |
b1e8f0a6 | 2810 | } |
6d88e679 | 2811 | hcd->rh_pollable = 1; |
b1e8f0a6 | 2812 | |
fb669cc0 DB |
2813 | /* NOTE: root hub and controller capabilities may not be the same */ |
2814 | if (device_can_wakeup(hcd->self.controller) | |
2815 | && device_can_wakeup(&hcd->self.root_hub->dev)) | |
b1e8f0a6 | 2816 | dev_dbg(hcd->self.controller, "supports USB remote wakeup\n"); |
b1e8f0a6 | 2817 | |
94dfd7ed ML |
2818 | /* initialize tasklets */ |
2819 | init_giveback_urb_bh(&hcd->high_prio_bh); | |
2820 | init_giveback_urb_bh(&hcd->low_prio_bh); | |
2821 | ||
68d07f64 SS |
2822 | /* enable irqs just before we start the controller, |
2823 | * if the BIOS provides legacy PCI irqs. | |
2824 | */ | |
2825 | if (usb_hcd_is_primary_hcd(hcd) && irqnum) { | |
c5635437 SS |
2826 | retval = usb_hcd_request_irqs(hcd, irqnum, irqflags); |
2827 | if (retval) | |
2828 | goto err_request_irq; | |
2829 | } | |
1da177e4 | 2830 | |
4814030c | 2831 | hcd->state = HC_STATE_RUNNING; |
abc4f9b0 SS |
2832 | retval = hcd->driver->start(hcd); |
2833 | if (retval < 0) { | |
1da177e4 | 2834 | dev_err(hcd->self.controller, "startup error %d\n", retval); |
8ec8d20b | 2835 | goto err_hcd_driver_start; |
1da177e4 LT |
2836 | } |
2837 | ||
b1e8f0a6 | 2838 | /* starting here, usbcore will pay attention to this root hub */ |
0faaad46 KB |
2839 | retval = register_root_hub(hcd); |
2840 | if (retval != 0) | |
8ec8d20b AS |
2841 | goto err_register_root_hub; |
2842 | ||
5234ce1b IPG |
2843 | retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group); |
2844 | if (retval < 0) { | |
2845 | printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n", | |
2846 | retval); | |
2847 | goto error_create_attr_group; | |
2848 | } | |
541c7d43 | 2849 | if (hcd->uses_new_polling && HCD_POLL_RH(hcd)) |
d5926ae7 | 2850 | usb_hcd_poll_rh_status(hcd); |
a6eeeb9f | 2851 | |
1da177e4 LT |
2852 | return retval; |
2853 | ||
5234ce1b | 2854 | error_create_attr_group: |
9b37596a | 2855 | clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
96e077ae AS |
2856 | if (HC_IS_RUNNING(hcd->state)) |
2857 | hcd->state = HC_STATE_QUIESCING; | |
2858 | spin_lock_irq(&hcd_root_hub_lock); | |
2859 | hcd->rh_registered = 0; | |
2860 | spin_unlock_irq(&hcd_root_hub_lock); | |
2861 | ||
ceb6c9c8 | 2862 | #ifdef CONFIG_PM |
96e077ae AS |
2863 | cancel_work_sync(&hcd->wakeup_work); |
2864 | #endif | |
5234ce1b | 2865 | mutex_lock(&usb_bus_list_lock); |
6d88e679 | 2866 | usb_disconnect(&rhdev); /* Sets rhdev to NULL */ |
5234ce1b | 2867 | mutex_unlock(&usb_bus_list_lock); |
b1e8f0a6 | 2868 | err_register_root_hub: |
6d88e679 | 2869 | hcd->rh_pollable = 0; |
541c7d43 | 2870 | clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
6d88e679 | 2871 | del_timer_sync(&hcd->rh_timer); |
8ec8d20b | 2872 | hcd->driver->stop(hcd); |
96e077ae | 2873 | hcd->state = HC_STATE_HALT; |
541c7d43 | 2874 | clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
96e077ae | 2875 | del_timer_sync(&hcd->rh_timer); |
b1e8f0a6 | 2876 | err_hcd_driver_start: |
cd70469d | 2877 | if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0) |
1da177e4 | 2878 | free_irq(irqnum, hcd); |
b1e8f0a6 DB |
2879 | err_request_irq: |
2880 | err_hcd_driver_setup: | |
96e077ae | 2881 | err_set_rh_speed: |
d8521afe | 2882 | usb_put_invalidate_rhdev(hcd); |
b1e8f0a6 | 2883 | err_allocate_root_hub: |
1da177e4 | 2884 | usb_deregister_bus(&hcd->self); |
b1e8f0a6 | 2885 | err_register_bus: |
1da177e4 | 2886 | hcd_buffer_destroy(hcd); |
00433254 | 2887 | err_create_buf: |
ef44cb42 | 2888 | if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) { |
00433254 SS |
2889 | phy_power_off(hcd->phy); |
2890 | phy_exit(hcd->phy); | |
2891 | phy_put(hcd->phy); | |
2892 | hcd->phy = NULL; | |
2893 | } | |
2894 | err_phy: | |
3d46e73d AT |
2895 | if (hcd->remove_phy && hcd->usb_phy) { |
2896 | usb_phy_shutdown(hcd->usb_phy); | |
2897 | usb_put_phy(hcd->usb_phy); | |
2898 | hcd->usb_phy = NULL; | |
103e127d | 2899 | } |
1da177e4 | 2900 | return retval; |
14557359 | 2901 | } |
782e70c6 | 2902 | EXPORT_SYMBOL_GPL(usb_add_hcd); |
1da177e4 LT |
2903 | |
2904 | /** | |
2905 | * usb_remove_hcd - shutdown processing for generic HCDs | |
2906 | * @hcd: the usb_hcd structure to remove | |
2907 | * Context: !in_interrupt() | |
2908 | * | |
2909 | * Disconnects the root hub, then reverses the effects of usb_add_hcd(), | |
2910 | * invoking the HCD's stop() method. | |
2911 | */ | |
2912 | void usb_remove_hcd(struct usb_hcd *hcd) | |
2913 | { | |
6d88e679 AS |
2914 | struct usb_device *rhdev = hcd->self.root_hub; |
2915 | ||
1da177e4 LT |
2916 | dev_info(hcd->self.controller, "remove, state %x\n", hcd->state); |
2917 | ||
6d88e679 AS |
2918 | usb_get_dev(rhdev); |
2919 | sysfs_remove_group(&rhdev->dev.kobj, &usb_bus_attr_group); | |
96e077ae | 2920 | |
9b37596a | 2921 | clear_bit(HCD_FLAG_RH_RUNNING, &hcd->flags); |
1da177e4 LT |
2922 | if (HC_IS_RUNNING (hcd->state)) |
2923 | hcd->state = HC_STATE_QUIESCING; | |
2924 | ||
2925 | dev_dbg(hcd->self.controller, "roothub graceful disconnect\n"); | |
2926 | spin_lock_irq (&hcd_root_hub_lock); | |
2927 | hcd->rh_registered = 0; | |
2928 | spin_unlock_irq (&hcd_root_hub_lock); | |
9ad3d6cc | 2929 | |
ceb6c9c8 | 2930 | #ifdef CONFIG_PM |
d5d4db70 | 2931 | cancel_work_sync(&hcd->wakeup_work); |
6b157c9b AS |
2932 | #endif |
2933 | ||
4186ecf8 | 2934 | mutex_lock(&usb_bus_list_lock); |
6d88e679 | 2935 | usb_disconnect(&rhdev); /* Sets rhdev to NULL */ |
4186ecf8 | 2936 | mutex_unlock(&usb_bus_list_lock); |
1da177e4 | 2937 | |
94dfd7ed ML |
2938 | /* |
2939 | * tasklet_kill() isn't needed here because: | |
2940 | * - driver's disconnect() called from usb_disconnect() should | |
2941 | * make sure its URBs are completed during the disconnect() | |
2942 | * callback | |
2943 | * | |
2944 | * - it is too late to run complete() here since driver may have | |
2945 | * been removed already now | |
2946 | */ | |
2947 | ||
6d88e679 AS |
2948 | /* Prevent any more root-hub status calls from the timer. |
2949 | * The HCD might still restart the timer (if a port status change | |
2950 | * interrupt occurs), but usb_hcd_poll_rh_status() won't invoke | |
2951 | * the hub_status_data() callback. | |
2952 | */ | |
2953 | hcd->rh_pollable = 0; | |
541c7d43 | 2954 | clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
6d88e679 AS |
2955 | del_timer_sync(&hcd->rh_timer); |
2956 | ||
1da177e4 LT |
2957 | hcd->driver->stop(hcd); |
2958 | hcd->state = HC_STATE_HALT; | |
2959 | ||
6d88e679 | 2960 | /* In case the HCD restarted the timer, stop it again. */ |
541c7d43 | 2961 | clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
1b42ae6d AS |
2962 | del_timer_sync(&hcd->rh_timer); |
2963 | ||
c5635437 | 2964 | if (usb_hcd_is_primary_hcd(hcd)) { |
cd70469d | 2965 | if (hcd->irq > 0) |
c5635437 SS |
2966 | free_irq(hcd->irq, hcd); |
2967 | } | |
6d88e679 | 2968 | |
1da177e4 LT |
2969 | usb_deregister_bus(&hcd->self); |
2970 | hcd_buffer_destroy(hcd); | |
00433254 | 2971 | |
ef44cb42 | 2972 | if (IS_ENABLED(CONFIG_GENERIC_PHY) && hcd->remove_phy && hcd->phy) { |
00433254 SS |
2973 | phy_power_off(hcd->phy); |
2974 | phy_exit(hcd->phy); | |
2975 | phy_put(hcd->phy); | |
2976 | hcd->phy = NULL; | |
2977 | } | |
3d46e73d AT |
2978 | if (hcd->remove_phy && hcd->usb_phy) { |
2979 | usb_phy_shutdown(hcd->usb_phy); | |
2980 | usb_put_phy(hcd->usb_phy); | |
2981 | hcd->usb_phy = NULL; | |
103e127d | 2982 | } |
d8521afe DW |
2983 | |
2984 | usb_put_invalidate_rhdev(hcd); | |
1da177e4 | 2985 | } |
782e70c6 | 2986 | EXPORT_SYMBOL_GPL(usb_remove_hcd); |
1da177e4 | 2987 | |
64a21d02 | 2988 | void |
842c1960 | 2989 | usb_hcd_platform_shutdown(struct platform_device *dev) |
64a21d02 AG |
2990 | { |
2991 | struct usb_hcd *hcd = platform_get_drvdata(dev); | |
2992 | ||
2993 | if (hcd->driver->shutdown) | |
2994 | hcd->driver->shutdown(hcd); | |
2995 | } | |
782e70c6 | 2996 | EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown); |
64a21d02 | 2997 | |
1da177e4 LT |
2998 | /*-------------------------------------------------------------------------*/ |
2999 | ||
f150fa1a | 3000 | #if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE) |
1da177e4 LT |
3001 | |
3002 | struct usb_mon_operations *mon_ops; | |
3003 | ||
3004 | /* | |
3005 | * The registration is unlocked. | |
3006 | * We do it this way because we do not want to lock in hot paths. | |
3007 | * | |
3008 | * Notice that the code is minimally error-proof. Because usbmon needs | |
3009 | * symbols from usbcore, usbcore gets referenced and cannot be unloaded first. | |
3010 | */ | |
14557359 | 3011 | |
1da177e4 LT |
3012 | int usb_mon_register (struct usb_mon_operations *ops) |
3013 | { | |
3014 | ||
3015 | if (mon_ops) | |
3016 | return -EBUSY; | |
3017 | ||
3018 | mon_ops = ops; | |
3019 | mb(); | |
3020 | return 0; | |
3021 | } | |
3022 | EXPORT_SYMBOL_GPL (usb_mon_register); | |
3023 | ||
3024 | void usb_mon_deregister (void) | |
3025 | { | |
3026 | ||
3027 | if (mon_ops == NULL) { | |
3028 | printk(KERN_ERR "USB: monitor was not registered\n"); | |
3029 | return; | |
3030 | } | |
3031 | mon_ops = NULL; | |
3032 | mb(); | |
3033 | } | |
3034 | EXPORT_SYMBOL_GPL (usb_mon_deregister); | |
3035 | ||
f150fa1a | 3036 | #endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */ |