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