blob: 3aaee2811f012109c6962095b844e94888c08d3a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/device.h>
34#include <linux/dma-mapping.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/irq.h>
37#include <asm/byteorder.h>
Magnus Dammb3476672008-01-23 15:58:35 +090038#include <asm/unaligned.h>
Aleksey Gorelov64a21d02006-08-08 17:24:08 -070039#include <linux/platform_device.h>
Alan Stern6b157c92007-03-13 16:37:30 -040040#include <linux/workqueue.h>
Alan Stern9bbdf1e2010-01-08 12:57:28 -050041#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020044#include <linux/usb/hcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include "usb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*-------------------------------------------------------------------------*/
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
Alan Stern9beeee62008-10-02 11:48:13 -040084/* Keep track of which host controller drivers are loaded */
85unsigned long usb_hcds_loaded;
86EXPORT_SYMBOL_GPL(usb_hcds_loaded);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* 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 */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100100DEFINE_MUTEX(usb_bus_list_lock); /* exported only for usbfs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101EXPORT_SYMBOL_GPL (usb_bus_list_lock);
102
103/* used for controlling access to virtual root hubs */
104static DEFINE_SPINLOCK(hcd_root_hub_lock);
105
Alan Stern809a58b2007-07-18 12:14:24 -0400106/* used when updating an endpoint's URB list */
107static DEFINE_SPINLOCK(hcd_urb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Alan Sterncde217a2008-10-21 15:28:46 -0400109/* used to protect against unlinking URBs after the device is gone */
110static DEFINE_SPINLOCK(hcd_urb_unlink_lock);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/* wait queue for synchronous unlinks */
113DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
114
Alan Stern809a58b2007-07-18 12:14:24 -0400115static inline int is_root_hub(struct usb_device *udev)
116{
117 return (udev->parent == NULL);
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120/*-------------------------------------------------------------------------*/
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
Sarah Sharpd2e9b4d2009-04-27 19:55:01 -0700131/* 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 */
Alan Sterncd780692010-02-25 13:19:37 -0500143 0x03, 0x00, /* __le16 idProduct; device 0x0003 */
Sarah Sharpd2e9b4d2009-04-27 19:55:01 -0700144 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* 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; */
Alan Stern7329e212008-04-03 18:02:56 -0400160 0x00, /* __u8 bDeviceProtocol; [ usb 2.0 no TT ] */
Alan Stern16f16d12005-10-24 15:41:19 -0400161 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Greg Kroah-Hartman667d6912008-01-28 09:50:12 -0800163 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
164 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 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 ] */
Alan Stern16f16d12005-10-24 15:41:19 -0400184 0x40, /* __u8 bMaxPacketSize0; 64 Bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Greg Kroah-Hartman667d6912008-01-28 09:50:12 -0800186 0x6b, 0x1d, /* __le16 idVendor; Linux Foundation */
187 0x01, 0x00, /* __le16 idProduct; device 0x0001 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 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 */
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -0700291 /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8)
292 * see hub.c:hub_configure() for details. */
293 (USB_MAXCHILDREN + 1 + 7) / 8, 0x00,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
295};
296
Sarah Sharpd2e9b4d2009-04-27 19:55:01 -0700297static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/*-------------------------------------------------------------------------*/
339
George Spelvin392ca682009-08-24 22:06:41 -0400340/**
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
George Spelvin392ca682009-08-24 22:06:41 -0400352static unsigned
353ascii2desc(char const *s, u8 *buf, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
George Spelvin392ca682009-08-24 22:06:41 -0400355 unsigned n, t = 2 + 2*strlen(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
George Spelvin392ca682009-08-24 22:06:41 -0400357 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++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
George Spelvin392ca682009-08-24 22:06:41 -0400372 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
George Spelvin392ca682009-08-24 22:06:41 -0400375/**
376 * rh_string() - provides string descriptors for root hub
377 * @id: the string ID number (0: langids, 1: serial #, 2: product, 3: vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * @hcd: the host controller for this root hub
George Spelvin392ca682009-08-24 22:06:41 -0400379 * @data: buffer for output packet
380 * @len: length of the provided buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 *
382 * Produces either a manufacturer, product or serial number string for the
383 * virtual root hub device.
George Spelvin392ca682009-08-24 22:06:41 -0400384 * Returns the number of bytes filled in: the length of the descriptor or
385 * of the provided buffer, whichever is less.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
George Spelvin392ca682009-08-24 22:06:41 -0400387static unsigned
388rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
Roel Kluin71d27182009-03-13 12:19:18 +0100389{
George Spelvin392ca682009-08-24 22:06:41 -0400390 char buf[100];
391 char const *s;
392 static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 // language ids
George Spelvin392ca682009-08-24 22:06:41 -0400395 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return len;
George Spelvin392ca682009-08-24 22:06:41 -0400403 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 */
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700413 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
414 init_utsname()->release, hcd->driver->description);
George Spelvin392ca682009-08-24 22:06:41 -0400415 s = buf;
416 break;
417 default:
418 /* Can't happen; caller guarantees it */
419 return 0;
Roel Kluin71d27182009-03-13 12:19:18 +0100420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
George Spelvin392ca682009-08-24 22:06:41 -0400422 return ascii2desc(s, data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
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;
Mikael Pettersson54bee6e2006-09-23 17:05:31 -0700432 u8 tbuf [sizeof (struct usb_hub_descriptor)]
433 __attribute__((aligned(4)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 const u8 *bufp = tbuf;
Roel Kluin71d27182009-03-13 12:19:18 +0100435 unsigned len = 0;
Alan Sterne9df41c2007-08-08 11:48:02 -0400436 int status;
Alan Stern7329e212008-04-03 18:02:56 -0400437 u8 patch_wakeup = 0;
438 u8 patch_protocol = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Alan Stern9439eb92007-08-02 15:05:45 -0400440 might_sleep();
441
Alan Sterne9df41c2007-08-08 11:48:02 -0400442 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;
Alan Sternb0d9efb2007-08-21 15:39:21 -0400447 urb->hcpriv = hcd; /* Indicate it's queued */
Alan Sterne9df41c2007-08-08 11:48:02 -0400448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 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
David Brownellfb669cc2006-01-24 08:40:27 -0800463 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 case DeviceRequest | USB_REQ_GET_STATUS:
David Brownellfb669cc2006-01-24 08:40:27 -0800480 tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev)
481 << USB_DEVICE_REMOTE_WAKEUP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 | (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)
David Brownellfb669cc2006-01-24 08:40:27 -0800488 device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 else
490 goto error;
491 break;
492 case DeviceOutRequest | USB_REQ_SET_FEATURE:
David Brownellfb669cc2006-01-24 08:40:27 -0800493 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 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:
Viral Mehta7dd19e62009-05-05 15:54:23 +0530508 switch (hcd->driver->flags & HCD_MASK) {
509 case HCD_USB3:
Sarah Sharpd2e9b4d2009-04-27 19:55:01 -0700510 bufp = usb3_rh_dev_descriptor;
Viral Mehta7dd19e62009-05-05 15:54:23 +0530511 break;
512 case HCD_USB2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 bufp = usb2_rh_dev_descriptor;
Viral Mehta7dd19e62009-05-05 15:54:23 +0530514 break;
515 case HCD_USB11:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 bufp = usb11_rh_dev_descriptor;
Viral Mehta7dd19e62009-05-05 15:54:23 +0530517 break;
518 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto error;
Viral Mehta7dd19e62009-05-05 15:54:23 +0530520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 len = 18;
Alan Stern7329e212008-04-03 18:02:56 -0400522 if (hcd->has_tt)
523 patch_protocol = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 break;
525 case USB_DT_CONFIG << 8:
Viral Mehta7dd19e62009-05-05 15:54:23 +0530526 switch (hcd->driver->flags & HCD_MASK) {
527 case HCD_USB3:
Sarah Sharpd2e9b4d2009-04-27 19:55:01 -0700528 bufp = ss_rh_config_descriptor;
529 len = sizeof ss_rh_config_descriptor;
Viral Mehta7dd19e62009-05-05 15:54:23 +0530530 break;
531 case HCD_USB2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 bufp = hs_rh_config_descriptor;
533 len = sizeof hs_rh_config_descriptor;
Viral Mehta7dd19e62009-05-05 15:54:23 +0530534 break;
535 case HCD_USB11:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 bufp = fs_rh_config_descriptor;
537 len = sizeof fs_rh_config_descriptor;
Viral Mehta7dd19e62009-05-05 15:54:23 +0530538 break;
539 default:
540 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
David Brownellfb669cc2006-01-24 08:40:27 -0800542 if (device_can_wakeup(&hcd->self.root_hub->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 patch_wakeup = 1;
544 break;
545 case USB_DT_STRING << 8:
Roel Kluin71d27182009-03-13 12:19:18 +0100546 if ((wValue & 0xff) < 4)
547 urb->actual_length = rh_string(wValue & 0xff,
548 hcd, ubuf, wLength);
549 else /* unsupported IDs --> "protocol stall" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 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 */
David Brownellb13296c2005-09-27 10:38:54 -0700587 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
David Brownellb13296c2005-09-27 10:38:54 -0700596 status = hcd->driver->hub_control (hcd,
597 typeReq, wValue, wIndex,
598 tbuf, wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 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,
David Brownellb13296c2005-09-27 10:38:54 -0700612 wLength, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
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;
Alan Stern7329e212008-04-03 18:02:56 -0400628
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636
637 /* any errors get returned through the urb completion */
Alan Stern9439eb92007-08-02 15:05:45 -0400638 spin_lock_irq(&hcd_root_hub_lock);
Alan Sterne9df41c2007-08-08 11:48:02 -0400639 usb_hcd_unlink_urb_from_ep(hcd, urb);
Alan Stern9439eb92007-08-02 15:05:45 -0400640
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);
Alan Stern4a000272007-08-24 15:42:24 -0400646 usb_hcd_giveback_urb(hcd, urb, status);
Alan Stern9439eb92007-08-02 15:05:45 -0400647 spin_lock(&hcd_root_hub_lock);
648
649 spin_unlock_irq(&hcd_root_hub_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return 0;
651}
652
653/*-------------------------------------------------------------------------*/
654
655/*
Alan Sternd5926ae72005-04-21 15:56:37 -0400656 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *
Alan Sternd5926ae72005-04-21 15:56:37 -0400660 * Completions are called in_interrupt(), but they may or may not
661 * be in_irq().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Alan Sternd5926ae72005-04-21 15:56:37 -0400663void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct urb *urb;
Alan Sternd5926ae72005-04-21 15:56:37 -0400666 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 unsigned long flags;
Joe Perchesad361c92009-07-06 13:05:40 -0700668 char buffer[6]; /* Any root hubs with > 31 ports? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Alan Stern1b42ae62007-03-13 11:10:52 -0400670 if (unlikely(!hcd->rh_registered))
671 return;
Alan Sternd5926ae72005-04-21 15:56:37 -0400672 if (!hcd->uses_new_polling && !hcd->status_urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Alan Sternd5926ae72005-04-21 15:56:37 -0400675 length = hcd->driver->hub_status_data(hcd, buffer);
676 if (length > 0) {
677
678 /* try to complete the status urb */
Alan Stern9439eb92007-08-02 15:05:45 -0400679 spin_lock_irqsave(&hcd_root_hub_lock, flags);
Alan Sternd5926ae72005-04-21 15:56:37 -0400680 urb = hcd->status_urb;
681 if (urb) {
Alan Sterne9df41c2007-08-08 11:48:02 -0400682 hcd->poll_pending = 0;
683 hcd->status_urb = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -0400684 urb->actual_length = length;
685 memcpy(urb->transfer_buffer, buffer, length);
Alan Stern9439eb92007-08-02 15:05:45 -0400686
Alan Sterne9df41c2007-08-08 11:48:02 -0400687 usb_hcd_unlink_urb_from_ep(hcd, urb);
Alan Stern9439eb92007-08-02 15:05:45 -0400688 spin_unlock(&hcd_root_hub_lock);
Alan Stern4a000272007-08-24 15:42:24 -0400689 usb_hcd_giveback_urb(hcd, urb, 0);
Alan Stern9439eb92007-08-02 15:05:45 -0400690 spin_lock(&hcd_root_hub_lock);
Alan Sterne9df41c2007-08-08 11:48:02 -0400691 } else {
Alan Sternd5926ae72005-04-21 15:56:37 -0400692 length = 0;
Alan Sternd5926ae72005-04-21 15:56:37 -0400693 hcd->poll_pending = 1;
Alan Sterne9df41c2007-08-08 11:48:02 -0400694 }
Alan Stern9439eb92007-08-02 15:05:45 -0400695 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
Alan Sternd5926ae72005-04-21 15:56:37 -0400696 }
697
698 /* The USB 2.0 spec says 256 ms. This is close enough and won't
Arjan van de Ven01cd0812007-05-22 12:42:56 -0700699 * 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 */
Alan Sternd5926ae72005-04-21 15:56:37 -0400702 if (hcd->uses_new_polling ? hcd->poll_rh :
703 (length == 0 && hcd->status_urb != NULL))
Arjan van de Ven01cd0812007-05-22 12:42:56 -0700704 mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
Alan Sternd5926ae72005-04-21 15:56:37 -0400705}
706EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
707
708/* timer callback */
709static void rh_timer_func (unsigned long _hcd)
710{
711 usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714/*-------------------------------------------------------------------------*/
715
Alan Sternd5926ae72005-04-21 15:56:37 -0400716static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
717{
718 int retval;
719 unsigned long flags;
Roel Kluin71d27182009-03-13 12:19:18 +0100720 unsigned len = 1 + (urb->dev->maxchild / 8);
Alan Sternd5926ae72005-04-21 15:56:37 -0400721
722 spin_lock_irqsave (&hcd_root_hub_lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -0400723 if (hcd->status_urb || urb->transfer_buffer_length < len) {
Alan Sternd5926ae72005-04-21 15:56:37 -0400724 dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
725 retval = -EINVAL;
Alan Sterne9df41c2007-08-08 11:48:02 -0400726 goto done;
Alan Sternd5926ae72005-04-21 15:56:37 -0400727 }
Alan Sterne9df41c2007-08-08 11:48:02 -0400728
729 retval = usb_hcd_link_urb_to_ep(hcd, urb);
730 if (retval)
731 goto done;
732
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:
Alan Sternd5926ae72005-04-21 15:56:37 -0400743 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
744 return retval;
745}
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
748{
Alan Stern5e60a162007-07-30 17:07:21 -0400749 if (usb_endpoint_xfer_int(&urb->ep->desc))
Alan Sternd5926ae72005-04-21 15:56:37 -0400750 return rh_queue_status (hcd, urb);
Alan Stern5e60a162007-07-30 17:07:21 -0400751 if (usb_endpoint_xfer_control(&urb->ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return rh_call_control (hcd, urb);
Alan Sternd5926ae72005-04-21 15:56:37 -0400753 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/*-------------------------------------------------------------------------*/
757
Alan Stern455b25f2006-08-11 16:01:45 -0400758/* Unlinks of root-hub control URBs are legal, but they don't do anything
759 * since these URBs always execute synchronously.
Alan Sternd5926ae72005-04-21 15:56:37 -0400760 */
Alan Sterne9df41c2007-08-08 11:48:02 -0400761static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Alan Stern455b25f2006-08-11 16:01:45 -0400763 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -0400764 int rc;
Alan Stern455b25f2006-08-11 16:01:45 -0400765
Alan Stern9439eb92007-08-02 15:05:45 -0400766 spin_lock_irqsave(&hcd_root_hub_lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -0400767 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
768 if (rc)
769 goto done;
770
Alan Stern5e60a162007-07-30 17:07:21 -0400771 if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */
Alan Stern455b25f2006-08-11 16:01:45 -0400772 ; /* Do nothing */
Alan Sternd5926ae72005-04-21 15:56:37 -0400773
774 } else { /* Status URB */
775 if (!hcd->uses_new_polling)
Alan Stern455b25f2006-08-11 16:01:45 -0400776 del_timer (&hcd->rh_timer);
Alan Sternd5926ae72005-04-21 15:56:37 -0400777 if (urb == hcd->status_urb) {
778 hcd->status_urb = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -0400779 usb_hcd_unlink_urb_from_ep(hcd, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Alan Stern9439eb92007-08-02 15:05:45 -0400781 spin_unlock(&hcd_root_hub_lock);
Alan Stern4a000272007-08-24 15:42:24 -0400782 usb_hcd_giveback_urb(hcd, urb, status);
Alan Stern9439eb92007-08-02 15:05:45 -0400783 spin_lock(&hcd_root_hub_lock);
784 }
785 }
Alan Sterne9df41c2007-08-08 11:48:02 -0400786 done:
Alan Stern9439eb92007-08-02 15:05:45 -0400787 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -0400788 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Inaky Perez-Gonzalez5234ce12007-07-31 20:33:58 -0700791
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/*-------------------------------------------------------------------------*/
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853/**
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*-------------------------------------------------------------------------*/
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{
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700887 int result = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 int busnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100890 mutex_lock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700892 if (busnum >= USB_MAXBUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700894 goto error_find_busnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700896 set_bit (busnum, busmap.busmap);
897 bus->busnum = busnum;
Tony Jones5a3201b2007-09-11 14:07:31 -0700898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* Add it to the local list of buses */
900 list_add (&bus->bus_list, &usb_bus_list);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100901 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -0700903 usb_notify_add_bus(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700905 dev_info (bus->controller, "new USB bus registered, assigned bus "
906 "number %d\n", bus->busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return 0;
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700908
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700909error_find_busnum:
910 mutex_unlock(&usb_bus_list_lock);
911 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
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 */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100931 mutex_lock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 list_del (&bus->bus_list);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100933 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -0700935 usb_notify_remove_bus(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 clear_bit (bus->busnum, busmap.busmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940/**
Alan Stern8ec8d202005-04-25 11:25:17 -0400941 * register_root_hub - called by usb_add_hcd() to register a root hub
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 * @hcd: host controller for this root hub
943 *
Alan Stern8ec8d202005-04-25 11:25:17 -0400944 * This function registers the root hub with the USB subsystem. It sets up
David Brownellb1e8f0a2006-01-23 15:25:40 -0800945 * 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).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
David Brownellb1e8f0a2006-01-23 15:25:40 -0800949static int register_root_hub(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
951 struct device *parent_dev = hcd->self.controller;
David Brownellb1e8f0a2006-01-23 15:25:40 -0800952 struct usb_device *usb_dev = hcd->self.root_hub;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 const int devnum = 1;
954 int retval;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 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
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100963 mutex_lock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Harvey Harrison551509d2009-02-11 14:11:36 -0800965 usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
967 if (retval != sizeof usb_dev->descriptor) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100968 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +0200970 dev_name(&usb_dev->dev), retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return (retval < 0) ? retval : -EMSGSIZE;
972 }
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 retval = usb_new_device (usb_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 dev_err (parent_dev, "can't register root hub for %s, %d\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +0200977 dev_name(&usb_dev->dev), retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100979 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
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)
Dan Streetman498f78e2005-07-29 12:18:28 -07001032 tmp = HS_NSECS_ISO (bytecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 else
Dan Streetman498f78e2005-07-29 12:18:28 -07001034 tmp = HS_NSECS (bytecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return tmp;
1036 default:
1037 pr_debug ("%s: bogus device speed!\n", usbcore_name);
1038 return -1;
1039 }
1040}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001041EXPORT_SYMBOL_GPL(usb_calc_bus_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044/*-------------------------------------------------------------------------*/
1045
1046/*
1047 * Generic HC operations.
1048 */
1049
1050/*-------------------------------------------------------------------------*/
1051
Alan Sterne9df41c2007-08-08 11:48:02 -04001052/**
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)
Alan Stern9a9bf402007-08-02 15:06:54 -04001068{
Alan Stern9a9bf402007-08-02 15:06:54 -04001069 int rc = 0;
1070
Alan Sterne9df41c2007-08-08 11:48:02 -04001071 spin_lock(&hcd_urb_list_lock);
Alan Stern9a9bf402007-08-02 15:06:54 -04001072
1073 /* Check that the URB isn't being killed */
Ming Lei49367d82008-12-12 21:38:45 +08001074 if (unlikely(atomic_read(&urb->reject))) {
Alan Stern9a9bf402007-08-02 15:06:54 -04001075 rc = -EPERM;
1076 goto done;
1077 }
1078
1079 if (unlikely(!urb->ep->enabled)) {
1080 rc = -ENOENT;
1081 goto done;
1082 }
1083
Alan Stern6840d252007-09-10 11:34:26 -04001084 if (unlikely(!urb->dev->can_submit)) {
1085 rc = -EHOSTUNREACH;
1086 goto done;
1087 }
1088
Alan Stern9a9bf402007-08-02 15:06:54 -04001089 /*
1090 * Check the host controller's state and add the URB to the
1091 * endpoint's queue.
1092 */
1093 switch (hcd->state) {
1094 case HC_STATE_RUNNING:
1095 case HC_STATE_RESUMING:
Alan Sterneb231052007-08-21 15:40:36 -04001096 urb->unlinked = 0;
Alan Stern9a9bf402007-08-02 15:06:54 -04001097 list_add_tail(&urb->urb_list, &urb->ep->urb_list);
1098 break;
1099 default:
1100 rc = -ESHUTDOWN;
1101 goto done;
1102 }
1103 done:
Alan Sterne9df41c2007-08-08 11:48:02 -04001104 spin_unlock(&hcd_urb_list_lock);
Alan Stern9a9bf402007-08-02 15:06:54 -04001105 return rc;
1106}
Alan Sterne9df41c2007-08-08 11:48:02 -04001107EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
Alan Stern9a9bf402007-08-02 15:06:54 -04001108
Alan Sterne9df41c2007-08-08 11:48:02 -04001109/**
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,
Alan Stern9a9bf402007-08-02 15:06:54 -04001129 int status)
1130{
Alan Stern9a9bf402007-08-02 15:06:54 -04001131 struct list_head *tmp;
Alan Stern9a9bf402007-08-02 15:06:54 -04001132
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 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001138 if (tmp != &urb->urb_list)
1139 return -EIDRM;
Alan Stern9a9bf402007-08-02 15:06:54 -04001140
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.
1143 */
Alan Sterneb231052007-08-21 15:40:36 -04001144 if (urb->unlinked)
Alan Sterne9df41c2007-08-08 11:48:02 -04001145 return -EBUSY;
Alan Sterneb231052007-08-21 15:40:36 -04001146 urb->unlinked = status;
Alan Stern9a9bf402007-08-02 15:06:54 -04001147
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
Alan Sterne9df41c2007-08-08 11:48:02 -04001160 return 0;
Alan Stern9a9bf402007-08-02 15:06:54 -04001161}
Alan Sterne9df41c2007-08-08 11:48:02 -04001162EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
Alan Stern9a9bf402007-08-02 15:06:54 -04001163
Alan Sterne9df41c2007-08-08 11:48:02 -04001164/**
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 /* clear all state linking urb to this dev (and hcd) */
Alan Sterne9df41c2007-08-08 11:48:02 -04001177 spin_lock(&hcd_urb_list_lock);
Alan Stern9a9bf402007-08-02 15:06:54 -04001178 list_del_init(&urb->urb_list);
Alan Sterne9df41c2007-08-08 11:48:02 -04001179 spin_unlock(&hcd_urb_list_lock);
Pete Zaitcev9f6a93f2007-06-08 13:37:49 -07001180}
Alan Sterne9df41c2007-08-08 11:48:02 -04001181EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Magnus Dammb3476672008-01-23 15:58:35 +09001183/*
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Magnus Dammb3476672008-01-23 15:58:35 +09001220 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
Alan Sternff9c8952010-04-02 13:27:28 -04001262static 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,
1281 urb->sg->sg,
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
Magnus Dammb3476672008-01-23 15:58:35 +09001307static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1308 gfp_t mem_flags)
1309{
1310 enum dma_data_direction dir;
1311 int ret = 0;
1312
Alan Stern9a9bf402007-08-02 15:06:54 -04001313 /* Map the URB's buffers for DMA access.
1314 * Lower level HCD code should use *_dma exclusively,
Sarah Sharpe04748e2009-04-27 19:59:01 -07001315 * unless it uses pio or talks to another transport,
1316 * or uses the provided scatter gather list for bulk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
Magnus Dammb3476672008-01-23 15:58:35 +09001318
1319 if (usb_endpoint_xfer_control(&urb->ep->desc)
1320 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
Larry Finger85e034f2009-11-05 10:37:03 -06001321 if (hcd->self.uses_dma) {
Magnus Dammb3476672008-01-23 15:58:35 +09001322 urb->setup_dma = dma_map_single(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 hcd->self.controller,
1324 urb->setup_packet,
Magnus Dammb3476672008-01-23 15:58:35 +09001325 sizeof(struct usb_ctrlrequest),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 DMA_TO_DEVICE);
Larry Finger85e034f2009-11-05 10:37:03 -06001327 if (dma_mapping_error(hcd->self.controller,
1328 urb->setup_dma))
1329 return -EAGAIN;
Alan Sternff9c8952010-04-02 13:27:28 -04001330 urb->transfer_flags |= URB_SETUP_MAP_SINGLE;
Larry Finger85e034f2009-11-05 10:37:03 -06001331 } else if (hcd->driver->flags & HCD_LOCAL_MEM)
Magnus Dammb3476672008-01-23 15:58:35 +09001332 ret = hcd_alloc_coherent(
1333 urb->dev->bus, mem_flags,
1334 &urb->setup_dma,
1335 (void **)&urb->setup_packet,
1336 sizeof(struct usb_ctrlrequest),
1337 DMA_TO_DEVICE);
Alan Sternff9c8952010-04-02 13:27:28 -04001338 if (ret)
1339 return ret;
1340 urb->transfer_flags |= URB_SETUP_MAP_LOCAL;
Magnus Dammb3476672008-01-23 15:58:35 +09001341 }
1342
1343 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
Alan Sternff9c8952010-04-02 13:27:28 -04001344 if (urb->transfer_buffer_length != 0
Magnus Dammb3476672008-01-23 15:58:35 +09001345 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
Larry Finger85e034f2009-11-05 10:37:03 -06001346 if (hcd->self.uses_dma) {
Alan Sternff9c8952010-04-02 13:27:28 -04001347 if (urb->num_sgs) {
1348 int n = dma_map_sg(
1349 hcd->self.controller,
1350 urb->sg->sg,
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) {
1363 struct scatterlist *sg;
1364
1365 sg = (struct scatterlist *) urb->sg;
1366 urb->transfer_dma = dma_map_page(
1367 hcd->self.controller,
1368 sg_page(sg),
1369 sg->offset,
1370 urb->transfer_buffer_length,
1371 dir);
1372 if (dma_mapping_error(hcd->self.controller,
Larry Finger85e034f2009-11-05 10:37:03 -06001373 urb->transfer_dma))
Alan Sternff9c8952010-04-02 13:27:28 -04001374 ret = -EAGAIN;
1375 else
1376 urb->transfer_flags |= URB_DMA_MAP_PAGE;
1377 } else {
1378 urb->transfer_dma = dma_map_single(
1379 hcd->self.controller,
1380 urb->transfer_buffer,
1381 urb->transfer_buffer_length,
1382 dir);
1383 if (dma_mapping_error(hcd->self.controller,
1384 urb->transfer_dma))
1385 ret = -EAGAIN;
1386 else
1387 urb->transfer_flags |= URB_DMA_MAP_SINGLE;
1388 }
Larry Finger85e034f2009-11-05 10:37:03 -06001389 } else if (hcd->driver->flags & HCD_LOCAL_MEM) {
Magnus Dammb3476672008-01-23 15:58:35 +09001390 ret = hcd_alloc_coherent(
1391 urb->dev->bus, mem_flags,
1392 &urb->transfer_dma,
1393 &urb->transfer_buffer,
1394 urb->transfer_buffer_length,
1395 dir);
Alan Sternff9c8952010-04-02 13:27:28 -04001396 if (ret == 0)
1397 urb->transfer_flags |= URB_MAP_LOCAL;
Magnus Dammb3476672008-01-23 15:58:35 +09001398 }
Alan Sternff9c8952010-04-02 13:27:28 -04001399 if (ret && (urb->transfer_flags & (URB_SETUP_MAP_SINGLE |
1400 URB_SETUP_MAP_LOCAL)))
1401 unmap_urb_for_dma(hcd, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
Magnus Dammb3476672008-01-23 15:58:35 +09001403 return ret;
Alan Stern9a9bf402007-08-02 15:06:54 -04001404}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Alan Stern9a9bf402007-08-02 15:06:54 -04001406/*-------------------------------------------------------------------------*/
1407
1408/* may be called in any context with a valid urb->dev usecount
1409 * caller surrenders "ownership" of urb
1410 * expects usb_submit_urb() to have sanity checked and conditioned all
1411 * inputs in the urb
1412 */
1413int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
1414{
1415 int status;
1416 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
1417
1418 /* increment urb's reference count as part of giving it to the HCD
1419 * (which will control it). HCD guarantees that it either returns
1420 * an error or calls giveback(), but not both.
1421 */
1422 usb_get_urb(urb);
1423 atomic_inc(&urb->use_count);
Sarah Sharp4d59d8a2007-10-03 14:56:03 -07001424 atomic_inc(&urb->dev->urbnum);
Alan Stern9a9bf402007-08-02 15:06:54 -04001425 usbmon_urb_submit(&hcd->self, urb);
1426
1427 /* NOTE requirements on root-hub callers (usbfs and the hub
1428 * driver, for now): URBs' urb->transfer_buffer must be
1429 * valid and usb_buffer_{sync,unmap}() not be needed, since
1430 * they could clobber root hub response data. Also, control
1431 * URBs must be submitted in process context with interrupts
1432 * enabled.
1433 */
Alan Sternff9c8952010-04-02 13:27:28 -04001434
1435 if (is_root_hub(urb->dev)) {
1436 status = rh_urb_enqueue(hcd, urb);
1437 } else {
1438 status = map_urb_for_dma(hcd, urb, mem_flags);
1439 if (likely(status == 0)) {
1440 status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
1441 if (unlikely(status))
1442 unmap_urb_for_dma(hcd, urb);
1443 }
Magnus Dammb3476672008-01-23 15:58:35 +09001444 }
1445
Alan Stern9a9bf402007-08-02 15:06:54 -04001446 if (unlikely(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 usbmon_urb_submit_error(&hcd->self, urb, status);
Alan Sternb0d9efb2007-08-21 15:39:21 -04001448 urb->hcpriv = NULL;
Alan Stern9a9bf402007-08-02 15:06:54 -04001449 INIT_LIST_HEAD(&urb->urb_list);
1450 atomic_dec(&urb->use_count);
Sarah Sharp4d59d8a2007-10-03 14:56:03 -07001451 atomic_dec(&urb->dev->urbnum);
Ming Lei49367d82008-12-12 21:38:45 +08001452 if (atomic_read(&urb->reject))
Alan Stern9a9bf402007-08-02 15:06:54 -04001453 wake_up(&usb_kill_urb_queue);
1454 usb_put_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456 return status;
1457}
1458
1459/*-------------------------------------------------------------------------*/
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461/* this makes the hcd giveback() the urb more quickly, by kicking it
1462 * off hardware queues (which may take a while) and returning it as
1463 * soon as practical. we've already set up the urb's return status,
1464 * but we can't know if the callback completed already.
1465 */
Alan Sterne9df41c2007-08-08 11:48:02 -04001466static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 int value;
1469
Alan Stern809a58b2007-07-18 12:14:24 -04001470 if (is_root_hub(urb->dev))
Alan Sterne9df41c2007-08-08 11:48:02 -04001471 value = usb_rh_urb_dequeue(hcd, urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 else {
1473
1474 /* The only reason an HCD might fail this call is if
1475 * it has not yet fully queued the urb to begin with.
1476 * Such failures should be harmless. */
Alan Sterne9df41c2007-08-08 11:48:02 -04001477 value = hcd->driver->urb_dequeue(hcd, urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return value;
1480}
1481
1482/*
1483 * called in any context
1484 *
1485 * caller guarantees urb won't be recycled till both unlink()
1486 * and the urb's completion function return
1487 */
Alan Sterna6d2bb92006-08-30 11:27:36 -04001488int usb_hcd_unlink_urb (struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
Alan Stern9a9bf402007-08-02 15:06:54 -04001490 struct usb_hcd *hcd;
Alan Sterncde217a2008-10-21 15:28:46 -04001491 int retval = -EIDRM;
1492 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Alan Sterncde217a2008-10-21 15:28:46 -04001494 /* Prevent the device and bus from going away while
1495 * the unlink is carried out. If they are already gone
1496 * then urb->use_count must be 0, since disconnected
1497 * devices can't have any active URBs.
1498 */
1499 spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
1500 if (atomic_read(&urb->use_count) > 0) {
1501 retval = 0;
1502 usb_get_dev(urb->dev);
1503 }
1504 spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
1505 if (retval == 0) {
1506 hcd = bus_to_hcd(urb->dev->bus);
1507 retval = unlink1(hcd, urb, status);
1508 usb_put_dev(urb->dev);
1509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (retval == 0)
1512 retval = -EINPROGRESS;
Alan Sterne9df41c2007-08-08 11:48:02 -04001513 else if (retval != -EIDRM && retval != -EBUSY)
Alan Stern9a9bf402007-08-02 15:06:54 -04001514 dev_dbg(&urb->dev->dev, "hcd_unlink_urb %p fail %d\n",
1515 urb, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return retval;
1517}
1518
1519/*-------------------------------------------------------------------------*/
1520
Alan Stern32aca562007-07-18 12:08:02 -04001521/**
1522 * usb_hcd_giveback_urb - return URB from HCD to device driver
1523 * @hcd: host controller returning the URB
1524 * @urb: urb being returned to the USB device driver.
Alan Stern4a000272007-08-24 15:42:24 -04001525 * @status: completion status code for the URB.
Alan Stern32aca562007-07-18 12:08:02 -04001526 * Context: in_interrupt()
1527 *
1528 * This hands the URB from HCD to its USB device driver, using its
1529 * completion function. The HCD has freed all per-urb resources
1530 * (and is done using urb->hcpriv). It also released all HCD locks;
1531 * the device driver won't cause problems if it frees, modifies,
1532 * or resubmits this URB.
Alan Sterneb231052007-08-21 15:40:36 -04001533 *
Alan Stern4a000272007-08-24 15:42:24 -04001534 * If @urb was unlinked, the value of @status will be overridden by
Alan Sterneb231052007-08-21 15:40:36 -04001535 * @urb->unlinked. Erroneous short transfers are detected in case
1536 * the HCD hasn't checked for them.
Alan Stern32aca562007-07-18 12:08:02 -04001537 */
Alan Stern4a000272007-08-24 15:42:24 -04001538void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
Alan Stern32aca562007-07-18 12:08:02 -04001539{
Alan Sternb0d9efb2007-08-21 15:39:21 -04001540 urb->hcpriv = NULL;
Alan Sterneb231052007-08-21 15:40:36 -04001541 if (unlikely(urb->unlinked))
Alan Stern4a000272007-08-24 15:42:24 -04001542 status = urb->unlinked;
Alan Sterneb231052007-08-21 15:40:36 -04001543 else if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
Alan Sternb0d9efb2007-08-21 15:39:21 -04001544 urb->actual_length < urb->transfer_buffer_length &&
Alan Stern4a000272007-08-24 15:42:24 -04001545 !status))
1546 status = -EREMOTEIO;
Alan Stern32aca562007-07-18 12:08:02 -04001547
Alan Stern1f5a3d02007-08-22 13:06:53 -04001548 unmap_urb_for_dma(hcd, urb);
Alan Stern4a000272007-08-24 15:42:24 -04001549 usbmon_urb_complete(&hcd->self, urb, status);
Alan Stern1f5a3d02007-08-22 13:06:53 -04001550 usb_unanchor_urb(urb);
1551
Alan Stern32aca562007-07-18 12:08:02 -04001552 /* pass ownership to the completion handler */
Alan Stern4a000272007-08-24 15:42:24 -04001553 urb->status = status;
Alan Stern32aca562007-07-18 12:08:02 -04001554 urb->complete (urb);
1555 atomic_dec (&urb->use_count);
Ming Lei49367d82008-12-12 21:38:45 +08001556 if (unlikely(atomic_read(&urb->reject)))
Alan Stern32aca562007-07-18 12:08:02 -04001557 wake_up (&usb_kill_urb_queue);
1558 usb_put_urb (urb);
1559}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001560EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
Alan Stern32aca562007-07-18 12:08:02 -04001561
1562/*-------------------------------------------------------------------------*/
1563
Alan Stern95cf82f2007-09-10 11:33:05 -04001564/* Cancel all URBs pending on this endpoint and wait for the endpoint's
1565 * queue to drain completely. The caller must first insure that no more
1566 * URBs can be submitted for this endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 */
Alan Stern95cf82f2007-09-10 11:33:05 -04001568void usb_hcd_flush_endpoint(struct usb_device *udev,
Alan Sterna6d2bb92006-08-30 11:27:36 -04001569 struct usb_host_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
1571 struct usb_hcd *hcd;
1572 struct urb *urb;
1573
Alan Stern95cf82f2007-09-10 11:33:05 -04001574 if (!ep)
1575 return;
Alan Stern9a9bf402007-08-02 15:06:54 -04001576 might_sleep();
Alan Stern17200582006-08-30 11:32:52 -04001577 hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Alan Stern95cf82f2007-09-10 11:33:05 -04001579 /* No more submits can occur */
Alan Stern9a9bf402007-08-02 15:06:54 -04001580 spin_lock_irq(&hcd_urb_list_lock);
Alan Sternddc1fd62007-11-21 15:13:10 -08001581rescan:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 list_for_each_entry (urb, &ep->urb_list, urb_list) {
Alan Stern5e60a162007-07-30 17:07:21 -04001583 int is_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Alan Sterneb231052007-08-21 15:40:36 -04001585 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 continue;
1587 usb_get_urb (urb);
Alan Stern5e60a162007-07-30 17:07:21 -04001588 is_in = usb_urb_dir_in(urb);
Alan Stern809a58b2007-07-18 12:14:24 -04001589 spin_unlock(&hcd_urb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Alan Sterne9df41c2007-08-08 11:48:02 -04001591 /* kick hcd */
1592 unlink1(hcd, urb, -ESHUTDOWN);
1593 dev_dbg (hcd->self.controller,
1594 "shutdown urb %p ep%d%s%s\n",
1595 urb, usb_endpoint_num(&ep->desc),
1596 is_in ? "in" : "out",
1597 ({ char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Alan Sterne9df41c2007-08-08 11:48:02 -04001599 switch (usb_endpoint_type(&ep->desc)) {
1600 case USB_ENDPOINT_XFER_CONTROL:
1601 s = ""; break;
1602 case USB_ENDPOINT_XFER_BULK:
1603 s = "-bulk"; break;
1604 case USB_ENDPOINT_XFER_INT:
1605 s = "-intr"; break;
1606 default:
1607 s = "-iso"; break;
1608 };
1609 s;
1610 }));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 usb_put_urb (urb);
1612
1613 /* list contents may have changed */
Alan Sternddc1fd62007-11-21 15:13:10 -08001614 spin_lock(&hcd_urb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 goto rescan;
1616 }
Alan Stern9a9bf402007-08-02 15:06:54 -04001617 spin_unlock_irq(&hcd_urb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Alan Stern95cf82f2007-09-10 11:33:05 -04001619 /* Wait until the endpoint queue is completely empty */
Alan Stern455b25f2006-08-11 16:01:45 -04001620 while (!list_empty (&ep->urb_list)) {
Alan Stern809a58b2007-07-18 12:14:24 -04001621 spin_lock_irq(&hcd_urb_list_lock);
Alan Stern455b25f2006-08-11 16:01:45 -04001622
1623 /* The list may have changed while we acquired the spinlock */
1624 urb = NULL;
1625 if (!list_empty (&ep->urb_list)) {
1626 urb = list_entry (ep->urb_list.prev, struct urb,
1627 urb_list);
1628 usb_get_urb (urb);
1629 }
Alan Stern809a58b2007-07-18 12:14:24 -04001630 spin_unlock_irq(&hcd_urb_list_lock);
Alan Stern455b25f2006-08-11 16:01:45 -04001631
1632 if (urb) {
1633 usb_kill_urb (urb);
1634 usb_put_urb (urb);
1635 }
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001639/**
Randy Dunlap70445ae2009-12-13 10:17:16 -08001640 * usb_hcd_alloc_bandwidth - check whether a new bandwidth setting exceeds
1641 * the bus bandwidth
1642 * @udev: target &usb_device
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001643 * @new_config: new configuration to install
1644 * @cur_alt: the current alternate interface setting
1645 * @new_alt: alternate interface setting that is being installed
1646 *
1647 * To change configurations, pass in the new configuration in new_config,
1648 * and pass NULL for cur_alt and new_alt.
1649 *
1650 * To reset a device's configuration (put the device in the ADDRESSED state),
1651 * pass in NULL for new_config, cur_alt, and new_alt.
1652 *
1653 * To change alternate interface settings, pass in NULL for new_config,
1654 * pass in the current alternate interface setting in cur_alt,
1655 * and pass in the new alternate interface setting in new_alt.
1656 *
1657 * Returns an error if the requested bandwidth change exceeds the
1658 * bus bandwidth or host controller internal resources.
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001659 */
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001660int usb_hcd_alloc_bandwidth(struct usb_device *udev,
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001661 struct usb_host_config *new_config,
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001662 struct usb_host_interface *cur_alt,
1663 struct usb_host_interface *new_alt)
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001664{
1665 int num_intfs, i, j;
H Hartley Sweeten576a3622009-11-17 14:53:41 -08001666 struct usb_host_interface *alt = NULL;
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001667 int ret = 0;
1668 struct usb_hcd *hcd;
1669 struct usb_host_endpoint *ep;
1670
1671 hcd = bus_to_hcd(udev->bus);
1672 if (!hcd->driver->check_bandwidth)
1673 return 0;
1674
1675 /* Configuration is being removed - set configuration 0 */
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001676 if (!new_config && !cur_alt) {
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001677 for (i = 1; i < 16; ++i) {
1678 ep = udev->ep_out[i];
1679 if (ep)
1680 hcd->driver->drop_endpoint(hcd, udev, ep);
1681 ep = udev->ep_in[i];
1682 if (ep)
1683 hcd->driver->drop_endpoint(hcd, udev, ep);
1684 }
1685 hcd->driver->check_bandwidth(hcd, udev);
1686 return 0;
1687 }
1688 /* Check if the HCD says there's enough bandwidth. Enable all endpoints
1689 * each interface's alt setting 0 and ask the HCD to check the bandwidth
1690 * of the bus. There will always be bandwidth for endpoint 0, so it's
1691 * ok to exclude it.
1692 */
1693 if (new_config) {
1694 num_intfs = new_config->desc.bNumInterfaces;
1695 /* Remove endpoints (except endpoint 0, which is always on the
1696 * schedule) from the old config from the schedule
1697 */
1698 for (i = 1; i < 16; ++i) {
1699 ep = udev->ep_out[i];
1700 if (ep) {
1701 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1702 if (ret < 0)
1703 goto reset;
1704 }
1705 ep = udev->ep_in[i];
1706 if (ep) {
1707 ret = hcd->driver->drop_endpoint(hcd, udev, ep);
1708 if (ret < 0)
1709 goto reset;
1710 }
1711 }
1712 for (i = 0; i < num_intfs; ++i) {
Sarah Sharpd837e212010-01-05 14:33:29 -08001713 struct usb_host_interface *first_alt;
1714 int iface_num;
1715
1716 first_alt = &new_config->intf_cache[i]->altsetting[0];
1717 iface_num = first_alt->desc.bInterfaceNumber;
Sarah Sharp91017f92009-12-03 09:44:34 -08001718 /* Set up endpoints for alternate interface setting 0 */
Sarah Sharpd837e212010-01-05 14:33:29 -08001719 alt = usb_find_alt_setting(new_config, iface_num, 0);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001720 if (!alt)
1721 /* No alt setting 0? Pick the first setting. */
Sarah Sharpd837e212010-01-05 14:33:29 -08001722 alt = first_alt;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001723
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001724 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
1725 ret = hcd->driver->add_endpoint(hcd, udev, &alt->endpoint[j]);
1726 if (ret < 0)
1727 goto reset;
1728 }
1729 }
1730 }
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001731 if (cur_alt && new_alt) {
Sarah Sharp04a723e2010-01-06 10:16:51 -08001732 struct usb_interface *iface = usb_ifnum_to_if(udev,
1733 cur_alt->desc.bInterfaceNumber);
1734
1735 if (iface->resetting_device) {
1736 /*
1737 * The USB core just reset the device, so the xHCI host
1738 * and the device will think alt setting 0 is installed.
1739 * However, the USB core will pass in the alternate
1740 * setting installed before the reset as cur_alt. Dig
1741 * out the alternate setting 0 structure, or the first
1742 * alternate setting if a broken device doesn't have alt
1743 * setting 0.
1744 */
1745 cur_alt = usb_altnum_to_altsetting(iface, 0);
1746 if (!cur_alt)
1747 cur_alt = &iface->altsetting[0];
1748 }
1749
Sarah Sharp3f0479e2009-12-03 09:44:36 -08001750 /* Drop all the endpoints in the current alt setting */
1751 for (i = 0; i < cur_alt->desc.bNumEndpoints; i++) {
1752 ret = hcd->driver->drop_endpoint(hcd, udev,
1753 &cur_alt->endpoint[i]);
1754 if (ret < 0)
1755 goto reset;
1756 }
1757 /* Add all the endpoints in the new alt setting */
1758 for (i = 0; i < new_alt->desc.bNumEndpoints; i++) {
1759 ret = hcd->driver->add_endpoint(hcd, udev,
1760 &new_alt->endpoint[i]);
1761 if (ret < 0)
1762 goto reset;
1763 }
1764 }
Sarah Sharp79abb1a2009-04-27 19:58:26 -07001765 ret = hcd->driver->check_bandwidth(hcd, udev);
1766reset:
1767 if (ret < 0)
1768 hcd->driver->reset_bandwidth(hcd, udev);
1769 return ret;
1770}
1771
Alan Stern95cf82f2007-09-10 11:33:05 -04001772/* Disables the endpoint: synchronizes with the hcd to make sure all
1773 * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
1774 * have been called previously. Use for set_configuration, set_interface,
1775 * driver removal, physical disconnect.
1776 *
1777 * example: a qh stored in ep->hcpriv, holding state related to endpoint
1778 * type, maxpacket size, toggle, halt status, and scheduling.
1779 */
1780void usb_hcd_disable_endpoint(struct usb_device *udev,
1781 struct usb_host_endpoint *ep)
1782{
1783 struct usb_hcd *hcd;
1784
1785 might_sleep();
1786 hcd = bus_to_hcd(udev->bus);
1787 if (hcd->driver->endpoint_disable)
1788 hcd->driver->endpoint_disable(hcd, ep);
1789}
1790
David Vrabel3444b262009-04-08 17:36:28 +00001791/**
1792 * usb_hcd_reset_endpoint - reset host endpoint state
1793 * @udev: USB device.
1794 * @ep: the endpoint to reset.
1795 *
1796 * Resets any host endpoint state such as the toggle bit, sequence
1797 * number and current window.
1798 */
1799void usb_hcd_reset_endpoint(struct usb_device *udev,
1800 struct usb_host_endpoint *ep)
1801{
1802 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1803
1804 if (hcd->driver->endpoint_reset)
1805 hcd->driver->endpoint_reset(hcd, ep);
1806 else {
1807 int epnum = usb_endpoint_num(&ep->desc);
1808 int is_out = usb_endpoint_dir_out(&ep->desc);
1809 int is_control = usb_endpoint_xfer_control(&ep->desc);
1810
1811 usb_settoggle(udev, epnum, is_out, 0);
1812 if (is_control)
1813 usb_settoggle(udev, epnum, !is_out, 0);
1814 }
1815}
1816
Sarah Sharpeab1caf2010-04-05 10:55:58 -07001817/**
1818 * usb_alloc_streams - allocate bulk endpoint stream IDs.
1819 * @interface: alternate setting that includes all endpoints.
1820 * @eps: array of endpoints that need streams.
1821 * @num_eps: number of endpoints in the array.
1822 * @num_streams: number of streams to allocate.
1823 * @mem_flags: flags hcd should use to allocate memory.
1824 *
1825 * Sets up a group of bulk endpoints to have num_streams stream IDs available.
1826 * Drivers may queue multiple transfers to different stream IDs, which may
1827 * complete in a different order than they were queued.
1828 */
1829int usb_alloc_streams(struct usb_interface *interface,
1830 struct usb_host_endpoint **eps, unsigned int num_eps,
1831 unsigned int num_streams, gfp_t mem_flags)
1832{
1833 struct usb_hcd *hcd;
1834 struct usb_device *dev;
1835 int i;
1836
1837 dev = interface_to_usbdev(interface);
1838 hcd = bus_to_hcd(dev->bus);
1839 if (!hcd->driver->alloc_streams || !hcd->driver->free_streams)
1840 return -EINVAL;
1841 if (dev->speed != USB_SPEED_SUPER)
1842 return -EINVAL;
1843
1844 /* Streams only apply to bulk endpoints. */
1845 for (i = 0; i < num_eps; i++)
1846 if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
1847 return -EINVAL;
1848
1849 return hcd->driver->alloc_streams(hcd, dev, eps, num_eps,
1850 num_streams, mem_flags);
1851}
1852EXPORT_SYMBOL_GPL(usb_alloc_streams);
1853
1854/**
1855 * usb_free_streams - free bulk endpoint stream IDs.
1856 * @interface: alternate setting that includes all endpoints.
1857 * @eps: array of endpoints to remove streams from.
1858 * @num_eps: number of endpoints in the array.
1859 * @mem_flags: flags hcd should use to allocate memory.
1860 *
1861 * Reverts a group of bulk endpoints back to not using stream IDs.
1862 * Can fail if we are given bad arguments, or HCD is broken.
1863 */
1864void usb_free_streams(struct usb_interface *interface,
1865 struct usb_host_endpoint **eps, unsigned int num_eps,
1866 gfp_t mem_flags)
1867{
1868 struct usb_hcd *hcd;
1869 struct usb_device *dev;
1870 int i;
1871
1872 dev = interface_to_usbdev(interface);
1873 hcd = bus_to_hcd(dev->bus);
1874 if (dev->speed != USB_SPEED_SUPER)
1875 return;
1876
1877 /* Streams only apply to bulk endpoints. */
1878 for (i = 0; i < num_eps; i++)
1879 if (!usb_endpoint_xfer_bulk(&eps[i]->desc))
1880 return;
1881
1882 hcd->driver->free_streams(hcd, dev, eps, num_eps, mem_flags);
1883}
1884EXPORT_SYMBOL_GPL(usb_free_streams);
1885
Alan Sterncde217a2008-10-21 15:28:46 -04001886/* Protect against drivers that try to unlink URBs after the device
1887 * is gone, by waiting until all unlinks for @udev are finished.
1888 * Since we don't currently track URBs by device, simply wait until
1889 * nothing is running in the locked region of usb_hcd_unlink_urb().
1890 */
1891void usb_hcd_synchronize_unlinks(struct usb_device *udev)
1892{
1893 spin_lock_irq(&hcd_urb_unlink_lock);
1894 spin_unlock_irq(&hcd_urb_unlink_lock);
1895}
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897/*-------------------------------------------------------------------------*/
1898
Alan Stern32aca562007-07-18 12:08:02 -04001899/* called in any context */
1900int usb_hcd_get_frame_number (struct usb_device *udev)
1901{
1902 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1903
1904 if (!HC_IS_RUNNING (hcd->state))
1905 return -ESHUTDOWN;
1906 return hcd->driver->get_frame_number (hcd);
1907}
1908
1909/*-------------------------------------------------------------------------*/
1910
David Brownell92936772005-09-22 22:32:11 -07001911#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Alan Stern65bfd292008-11-25 16:39:18 -05001913int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Alan Stern686314c2007-05-30 15:34:36 -04001915 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
1916 int status;
1917 int old_state = hcd->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Alan Stern686314c2007-05-30 15:34:36 -04001919 dev_dbg(&rhdev->dev, "bus %s%s\n",
Alan Stern65bfd292008-11-25 16:39:18 -05001920 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "suspend");
Alan Stern686314c2007-05-30 15:34:36 -04001921 if (!hcd->driver->bus_suspend) {
1922 status = -ENOENT;
1923 } else {
1924 hcd->state = HC_STATE_QUIESCING;
1925 status = hcd->driver->bus_suspend(hcd);
1926 }
1927 if (status == 0) {
1928 usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
David Brownell92936772005-09-22 22:32:11 -07001929 hcd->state = HC_STATE_SUSPENDED;
Alan Stern686314c2007-05-30 15:34:36 -04001930 } else {
1931 hcd->state = old_state;
1932 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
David Brownell92936772005-09-22 22:32:11 -07001933 "suspend", status);
Alan Stern686314c2007-05-30 15:34:36 -04001934 }
David Brownell92936772005-09-22 22:32:11 -07001935 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Alan Stern65bfd292008-11-25 16:39:18 -05001938int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Alan Stern686314c2007-05-30 15:34:36 -04001940 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
1941 int status;
Alan Sterncfa59da2007-06-21 16:25:35 -04001942 int old_state = hcd->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Alan Stern686314c2007-05-30 15:34:36 -04001944 dev_dbg(&rhdev->dev, "usb %s%s\n",
Alan Stern65bfd292008-11-25 16:39:18 -05001945 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "resume");
Alan Stern0c0382e2005-10-13 17:08:02 -04001946 if (!hcd->driver->bus_resume)
David Brownell92936772005-09-22 22:32:11 -07001947 return -ENOENT;
David Brownell979d5192005-09-22 22:32:24 -07001948 if (hcd->state == HC_STATE_RUNNING)
1949 return 0;
Alan Stern686314c2007-05-30 15:34:36 -04001950
David Brownell92936772005-09-22 22:32:11 -07001951 hcd->state = HC_STATE_RESUMING;
Alan Stern686314c2007-05-30 15:34:36 -04001952 status = hcd->driver->bus_resume(hcd);
1953 if (status == 0) {
1954 /* TRSMRCY = 10 msec */
1955 msleep(10);
1956 usb_set_device_state(rhdev, rhdev->actconfig
1957 ? USB_STATE_CONFIGURED
1958 : USB_STATE_ADDRESS);
David Brownell92936772005-09-22 22:32:11 -07001959 hcd->state = HC_STATE_RUNNING;
Alan Stern686314c2007-05-30 15:34:36 -04001960 } else {
Alan Sterncfa59da2007-06-21 16:25:35 -04001961 hcd->state = old_state;
Alan Stern686314c2007-05-30 15:34:36 -04001962 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
David Brownell92936772005-09-22 22:32:11 -07001963 "resume", status);
Alan Sterncfa59da2007-06-21 16:25:35 -04001964 if (status != -ESHUTDOWN)
1965 usb_hc_died(hcd);
David Brownell92936772005-09-22 22:32:11 -07001966 }
1967 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001970#endif /* CONFIG_PM */
1971
1972#ifdef CONFIG_USB_SUSPEND
1973
Alan Stern6b157c92007-03-13 16:37:30 -04001974/* Workqueue routine for root-hub remote wakeup */
1975static void hcd_resume_work(struct work_struct *work)
1976{
1977 struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
1978 struct usb_device *udev = hcd->self.root_hub;
1979
1980 usb_lock_device(udev);
Alan Stern0534d462010-01-08 12:56:30 -05001981 usb_remote_wakeup(udev);
Alan Stern6b157c92007-03-13 16:37:30 -04001982 usb_unlock_device(udev);
1983}
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985/**
1986 * usb_hcd_resume_root_hub - called by HCD to resume its root hub
1987 * @hcd: host controller for this root hub
1988 *
1989 * The USB host controller calls this function when its root hub is
1990 * suspended (with the remote wakeup feature enabled) and a remote
Alan Stern6b157c92007-03-13 16:37:30 -04001991 * wakeup request is received. The routine submits a workqueue request
1992 * to resume the root hub (that is, manage its downstream ports again).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 */
1994void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
1995{
1996 unsigned long flags;
1997
1998 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1999 if (hcd->rh_registered)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002000 queue_work(pm_wq, &hcd->wakeup_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
2002}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
2004
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002005#endif /* CONFIG_USB_SUSPEND */
David Brownell92936772005-09-22 22:32:11 -07002006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007/*-------------------------------------------------------------------------*/
2008
2009#ifdef CONFIG_USB_OTG
2010
2011/**
2012 * usb_bus_start_enum - start immediate enumeration (for OTG)
2013 * @bus: the bus (must use hcd framework)
2014 * @port_num: 1-based number of port; usually bus->otg_port
2015 * Context: in_interrupt()
2016 *
2017 * Starts enumeration, with an immediate reset followed later by
2018 * khubd identifying and possibly configuring the device.
2019 * This is needed by OTG controller drivers, where it helps meet
2020 * HNP protocol timing requirements for starting a port reset.
2021 */
2022int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
2023{
2024 struct usb_hcd *hcd;
2025 int status = -EOPNOTSUPP;
2026
2027 /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
2028 * boards with root hubs hooked up to internal devices (instead of
2029 * just the OTG port) may need more attention to resetting...
2030 */
2031 hcd = container_of (bus, struct usb_hcd, self);
2032 if (port_num && hcd->driver->start_port_reset)
2033 status = hcd->driver->start_port_reset(hcd, port_num);
2034
2035 /* run khubd shortly after (first) root port reset finishes;
2036 * it may issue others, until at least 50 msecs have passed.
2037 */
2038 if (status == 0)
2039 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
2040 return status;
2041}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06002042EXPORT_SYMBOL_GPL(usb_bus_start_enum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044#endif
2045
2046/*-------------------------------------------------------------------------*/
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
2050 * @irq: the IRQ being raised
2051 * @__hcd: pointer to the HCD whose IRQ is being signaled
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 *
2053 * If the controller isn't HALTed, calls the driver's irq handler.
2054 * Checks whether the controller is now dead.
2055 */
David Howells7d12e782006-10-05 14:55:46 +01002056irqreturn_t usb_hcd_irq (int irq, void *__hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 struct usb_hcd *hcd = __hcd;
Stefan Beckerde854222008-07-01 19:19:22 +03002059 unsigned long flags;
2060 irqreturn_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Stefan Beckerde854222008-07-01 19:19:22 +03002062 /* IRQF_DISABLED doesn't work correctly with shared IRQs
2063 * when the first handler doesn't use it. So let's just
2064 * assume it's never used.
2065 */
2066 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Stefan Beckerde854222008-07-01 19:19:22 +03002068 if (unlikely(hcd->state == HC_STATE_HALT ||
2069 !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
2070 rc = IRQ_NONE;
2071 } else if (hcd->driver->irq(hcd) == IRQ_NONE) {
2072 rc = IRQ_NONE;
2073 } else {
2074 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002075
Stefan Beckerde854222008-07-01 19:19:22 +03002076 if (unlikely(hcd->state == HC_STATE_HALT))
2077 usb_hc_died(hcd);
2078 rc = IRQ_HANDLED;
2079 }
2080
2081 local_irq_restore(flags);
2082 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
2085/*-------------------------------------------------------------------------*/
2086
2087/**
2088 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
2089 * @hcd: pointer to the HCD representing the controller
2090 *
2091 * This is called by bus glue to report a USB host controller that died
2092 * while operations may still have been pending. It's called automatically
2093 * by the PCI glue, so only glue for non-PCI busses should need to call it.
2094 */
2095void usb_hc_died (struct usb_hcd *hcd)
2096{
2097 unsigned long flags;
2098
2099 dev_err (hcd->self.controller, "HC died; cleaning up\n");
2100
2101 spin_lock_irqsave (&hcd_root_hub_lock, flags);
2102 if (hcd->rh_registered) {
Alan Sternd5926ae72005-04-21 15:56:37 -04002103 hcd->poll_rh = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /* make khubd clean up old urbs and devices */
2106 usb_set_device_state (hcd->self.root_hub,
2107 USB_STATE_NOTATTACHED);
2108 usb_kick_khubd (hcd->self.root_hub);
2109 }
2110 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
2111}
2112EXPORT_SYMBOL_GPL (usb_hc_died);
2113
2114/*-------------------------------------------------------------------------*/
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116/**
2117 * usb_create_hcd - create and initialize an HCD structure
2118 * @driver: HC driver that will use this hcd
2119 * @dev: device for this HC, stored in hcd->self.controller
2120 * @bus_name: value to store in hcd->self.bus_name
2121 * Context: !in_interrupt()
2122 *
2123 * Allocate a struct usb_hcd, with extra space at the end for the
2124 * HC driver's private data. Initialize the generic members of the
2125 * hcd structure.
2126 *
2127 * If memory is unavailable, returns NULL.
2128 */
2129struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
Greg Kroah-Hartman1b26da12008-07-02 12:46:22 -07002130 struct device *dev, const char *bus_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 struct usb_hcd *hcd;
2133
Pekka Enberg7b842b62005-09-06 15:18:34 -07002134 hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (!hcd) {
2136 dev_dbg (dev, "hcd alloc failed\n");
2137 return NULL;
2138 }
2139 dev_set_drvdata(dev, hcd);
Alan Stern17200582006-08-30 11:32:52 -04002140 kref_init(&hcd->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 usb_bus_init(&hcd->self);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 hcd->self.controller = dev;
2144 hcd->self.bus_name = bus_name;
Alan Sterndd990f12006-08-30 11:29:56 -04002145 hcd->self.uses_dma = (dev->dma_mask != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 init_timer(&hcd->rh_timer);
Alan Sternd5926ae72005-04-21 15:56:37 -04002148 hcd->rh_timer.function = rh_timer_func;
2149 hcd->rh_timer.data = (unsigned long) hcd;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002150#ifdef CONFIG_USB_SUSPEND
Alan Stern6b157c92007-03-13 16:37:30 -04002151 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
2152#endif
Sarah Sharp3f0479e2009-12-03 09:44:36 -08002153 mutex_init(&hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 hcd->driver = driver;
2156 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
2157 "USB Host Controller";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return hcd;
2159}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06002160EXPORT_SYMBOL_GPL(usb_create_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Alan Stern17200582006-08-30 11:32:52 -04002162static void hcd_release (struct kref *kref)
2163{
2164 struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
2165
2166 kfree(hcd);
2167}
2168
2169struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
2170{
2171 if (hcd)
2172 kref_get (&hcd->kref);
2173 return hcd;
2174}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06002175EXPORT_SYMBOL_GPL(usb_get_hcd);
Alan Stern17200582006-08-30 11:32:52 -04002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177void usb_put_hcd (struct usb_hcd *hcd)
2178{
Alan Stern17200582006-08-30 11:32:52 -04002179 if (hcd)
2180 kref_put (&hcd->kref, hcd_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06002182EXPORT_SYMBOL_GPL(usb_put_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184/**
2185 * usb_add_hcd - finish generic HCD structure initialization and register
2186 * @hcd: the usb_hcd structure to initialize
2187 * @irqnum: Interrupt line to allocate
2188 * @irqflags: Interrupt type flags
2189 *
2190 * Finish the remaining parts of generic HCD initialization: allocate the
2191 * buffers of consistent memory, register the bus, request the IRQ line,
2192 * and call the driver's reset() and start() routines.
2193 */
2194int usb_add_hcd(struct usb_hcd *hcd,
2195 unsigned int irqnum, unsigned long irqflags)
2196{
Alan Stern8ec8d202005-04-25 11:25:17 -04002197 int retval;
2198 struct usb_device *rhdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
2201
Inaky Perez-Gonzalez5234ce12007-07-31 20:33:58 -07002202 hcd->authorized_default = hcd->wireless? 0 : 1;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11002203 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
2204
David Brownellb1e8f0a2006-01-23 15:25:40 -08002205 /* HC is in reset state, but accessible. Now do the one-time init,
2206 * bottom up so that hcds can customize the root hubs before khubd
2207 * starts talking to them. (Note, bus id is assigned early too.)
2208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if ((retval = hcd_buffer_create(hcd)) != 0) {
2210 dev_dbg(hcd->self.controller, "pool alloc failed\n");
2211 return retval;
2212 }
2213
2214 if ((retval = usb_register_bus(&hcd->self)) < 0)
Alan Stern8ec8d202005-04-25 11:25:17 -04002215 goto err_register_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
David Brownellb1e8f0a2006-01-23 15:25:40 -08002217 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
2218 dev_err(hcd->self.controller, "unable to allocate root hub\n");
2219 retval = -ENOMEM;
2220 goto err_allocate_root_hub;
2221 }
Sarah Sharp6b403b02009-04-27 19:54:10 -07002222
2223 switch (hcd->driver->flags & HCD_MASK) {
2224 case HCD_USB11:
2225 rhdev->speed = USB_SPEED_FULL;
2226 break;
2227 case HCD_USB2:
2228 rhdev->speed = USB_SPEED_HIGH;
2229 break;
2230 case HCD_USB3:
2231 rhdev->speed = USB_SPEED_SUPER;
2232 break;
2233 default:
2234 goto err_allocate_root_hub;
2235 }
David Brownellb1e8f0a2006-01-23 15:25:40 -08002236 hcd->self.root_hub = rhdev;
2237
David Brownelldb4cefa2006-05-01 22:07:13 -07002238 /* wakeup flag init defaults to "everything works" for root hubs,
2239 * but drivers can override it in reset() if needed, along with
2240 * recording the overall controller's system wakeup capability.
2241 */
2242 device_init_wakeup(&rhdev->dev, 1);
2243
David Brownellb1e8f0a2006-01-23 15:25:40 -08002244 /* "reset" is misnamed; its role is now one-time init. the controller
2245 * should already have been reset (and boot firmware kicked off etc).
2246 */
2247 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
2248 dev_err(hcd->self.controller, "can't setup\n");
2249 goto err_hcd_driver_setup;
2250 }
2251
David Brownellfb669cc2006-01-24 08:40:27 -08002252 /* NOTE: root hub and controller capabilities may not be the same */
2253 if (device_can_wakeup(hcd->self.controller)
2254 && device_can_wakeup(&hcd->self.root_hub->dev))
David Brownellb1e8f0a2006-01-23 15:25:40 -08002255 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
David Brownellb1e8f0a2006-01-23 15:25:40 -08002256
2257 /* enable irqs just before we start the controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (hcd->driver->irq) {
Stefan Beckerde854222008-07-01 19:19:22 +03002259
2260 /* IRQF_DISABLED doesn't work as advertised when used together
2261 * with IRQF_SHARED. As usb_hcd_irq() will always disable
2262 * interrupts we can remove it here.
2263 */
Geoff Levand83a79822008-08-22 14:13:00 -07002264 if (irqflags & IRQF_SHARED)
2265 irqflags &= ~IRQF_DISABLED;
Stefan Beckerde854222008-07-01 19:19:22 +03002266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
2268 hcd->driver->description, hcd->self.busnum);
2269 if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
2270 hcd->irq_descr, hcd)) != 0) {
2271 dev_err(hcd->self.controller,
David S. Millerc6387a42006-06-20 01:21:29 -07002272 "request interrupt %d failed\n", irqnum);
Alan Stern8ec8d202005-04-25 11:25:17 -04002273 goto err_request_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 hcd->irq = irqnum;
David S. Millerc6387a42006-06-20 01:21:29 -07002276 dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 (hcd->driver->flags & HCD_MEMORY) ?
2278 "io mem" : "io base",
2279 (unsigned long long)hcd->rsrc_start);
2280 } else {
2281 hcd->irq = -1;
2282 if (hcd->rsrc_start)
2283 dev_info(hcd->self.controller, "%s 0x%08llx\n",
2284 (hcd->driver->flags & HCD_MEMORY) ?
2285 "io mem" : "io base",
2286 (unsigned long long)hcd->rsrc_start);
2287 }
2288
2289 if ((retval = hcd->driver->start(hcd)) < 0) {
2290 dev_err(hcd->self.controller, "startup error %d\n", retval);
Alan Stern8ec8d202005-04-25 11:25:17 -04002291 goto err_hcd_driver_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
2293
David Brownellb1e8f0a2006-01-23 15:25:40 -08002294 /* starting here, usbcore will pay attention to this root hub */
Alan Stern55c52712005-11-23 12:03:12 -05002295 rhdev->bus_mA = min(500u, hcd->power_budget);
David Brownellb1e8f0a2006-01-23 15:25:40 -08002296 if ((retval = register_root_hub(hcd)) != 0)
Alan Stern8ec8d202005-04-25 11:25:17 -04002297 goto err_register_root_hub;
2298
Inaky Perez-Gonzalez5234ce12007-07-31 20:33:58 -07002299 retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
2300 if (retval < 0) {
2301 printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
2302 retval);
2303 goto error_create_attr_group;
2304 }
Alan Sternd5926ae72005-04-21 15:56:37 -04002305 if (hcd->uses_new_polling && hcd->poll_rh)
2306 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return retval;
2308
Inaky Perez-Gonzalez5234ce12007-07-31 20:33:58 -07002309error_create_attr_group:
2310 mutex_lock(&usb_bus_list_lock);
2311 usb_disconnect(&hcd->self.root_hub);
2312 mutex_unlock(&usb_bus_list_lock);
David Brownellb1e8f0a2006-01-23 15:25:40 -08002313err_register_root_hub:
Alan Stern8ec8d202005-04-25 11:25:17 -04002314 hcd->driver->stop(hcd);
David Brownellb1e8f0a2006-01-23 15:25:40 -08002315err_hcd_driver_start:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (hcd->irq >= 0)
2317 free_irq(irqnum, hcd);
David Brownellb1e8f0a2006-01-23 15:25:40 -08002318err_request_irq:
2319err_hcd_driver_setup:
2320 hcd->self.root_hub = NULL;
2321 usb_put_dev(rhdev);
2322err_allocate_root_hub:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 usb_deregister_bus(&hcd->self);
David Brownellb1e8f0a2006-01-23 15:25:40 -08002324err_register_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 hcd_buffer_destroy(hcd);
2326 return retval;
2327}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06002328EXPORT_SYMBOL_GPL(usb_add_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
2330/**
2331 * usb_remove_hcd - shutdown processing for generic HCDs
2332 * @hcd: the usb_hcd structure to remove
2333 * Context: !in_interrupt()
2334 *
2335 * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
2336 * invoking the HCD's stop() method.
2337 */
2338void usb_remove_hcd(struct usb_hcd *hcd)
2339{
2340 dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
2341
2342 if (HC_IS_RUNNING (hcd->state))
2343 hcd->state = HC_STATE_QUIESCING;
2344
2345 dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
2346 spin_lock_irq (&hcd_root_hub_lock);
2347 hcd->rh_registered = 0;
2348 spin_unlock_irq (&hcd_root_hub_lock);
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002349
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002350#ifdef CONFIG_USB_SUSPEND
Alan Sternd5d4db72007-05-29 16:34:52 -04002351 cancel_work_sync(&hcd->wakeup_work);
Alan Stern6b157c92007-03-13 16:37:30 -04002352#endif
2353
Inaky Perez-Gonzalez5234ce12007-07-31 20:33:58 -07002354 sysfs_remove_group(&hcd->self.root_hub->dev.kobj, &usb_bus_attr_group);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002355 mutex_lock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 usb_disconnect(&hcd->self.root_hub);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002357 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359 hcd->driver->stop(hcd);
2360 hcd->state = HC_STATE_HALT;
2361
Alan Stern1b42ae62007-03-13 11:10:52 -04002362 hcd->poll_rh = 0;
2363 del_timer_sync(&hcd->rh_timer);
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (hcd->irq >= 0)
2366 free_irq(hcd->irq, hcd);
2367 usb_deregister_bus(&hcd->self);
2368 hcd_buffer_destroy(hcd);
2369}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06002370EXPORT_SYMBOL_GPL(usb_remove_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Aleksey Gorelov64a21d02006-08-08 17:24:08 -07002372void
2373usb_hcd_platform_shutdown(struct platform_device* dev)
2374{
2375 struct usb_hcd *hcd = platform_get_drvdata(dev);
2376
2377 if (hcd->driver->shutdown)
2378 hcd->driver->shutdown(hcd);
2379}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06002380EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
Aleksey Gorelov64a21d02006-08-08 17:24:08 -07002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382/*-------------------------------------------------------------------------*/
2383
Pete Zaitcevf150fa12008-11-13 21:31:21 -07002384#if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386struct usb_mon_operations *mon_ops;
2387
2388/*
2389 * The registration is unlocked.
2390 * We do it this way because we do not want to lock in hot paths.
2391 *
2392 * Notice that the code is minimally error-proof. Because usbmon needs
2393 * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
2394 */
2395
2396int usb_mon_register (struct usb_mon_operations *ops)
2397{
2398
2399 if (mon_ops)
2400 return -EBUSY;
2401
2402 mon_ops = ops;
2403 mb();
2404 return 0;
2405}
2406EXPORT_SYMBOL_GPL (usb_mon_register);
2407
2408void usb_mon_deregister (void)
2409{
2410
2411 if (mon_ops == NULL) {
2412 printk(KERN_ERR "USB: monitor was not registered\n");
2413 return;
2414 }
2415 mon_ops = NULL;
2416 mb();
2417}
2418EXPORT_SYMBOL_GPL (usb_mon_deregister);
2419
Pete Zaitcevf150fa12008-11-13 21:31:21 -07002420#endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */