blob: 823744d80cb9fd145f61a0bd543b9699da9b0be7 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <linux/usb.h>
43
44#include "usb.h"
45#include "hcd.h"
46#include "hub.h"
47
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 */
143 0x02, 0x00, /* __le16 idProduct; device 0x0002 */
144 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
145
146 0x03, /* __u8 iManufacturer; */
147 0x02, /* __u8 iProduct; */
148 0x01, /* __u8 iSerialNumber; */
149 0x01 /* __u8 bNumConfigurations; */
150};
151
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
340/*
341 * helper routine for returning string descriptors in UTF-16LE
342 * input can actually be ISO-8859-1; ASCII is its 7-bit subset
343 */
Roel Kluin71d27182009-03-13 12:19:18 +0100344static unsigned ascii2utf(char *s, u8 *utf, int utfmax)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Roel Kluin71d27182009-03-13 12:19:18 +0100346 unsigned retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
349 *utf++ = *s++;
350 *utf++ = 0;
351 }
352 if (utfmax > 0) {
353 *utf = *s;
354 ++retval;
355 }
356 return retval;
357}
358
359/*
360 * rh_string - provides manufacturer, product and serial strings for root hub
361 * @id: the string ID number (1: serial number, 2: product, 3: vendor)
362 * @hcd: the host controller for this root hub
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * @data: return packet in UTF-16 LE
364 * @len: length of the return packet
365 *
366 * Produces either a manufacturer, product or serial number string for the
367 * virtual root hub device.
368 */
Roel Kluin71d27182009-03-13 12:19:18 +0100369static unsigned rh_string(int id, struct usb_hcd *hcd, u8 *data, unsigned len)
370{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 char buf [100];
372
373 // language ids
374 if (id == 0) {
375 buf[0] = 4; buf[1] = 3; /* 4 bytes string data */
376 buf[2] = 0x09; buf[3] = 0x04; /* MSFT-speak for "en-us" */
Roel Kluin71d27182009-03-13 12:19:18 +0100377 len = min_t(unsigned, len, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 memcpy (data, buf, len);
379 return len;
380
381 // serial number
382 } else if (id == 1) {
383 strlcpy (buf, hcd->self.bus_name, sizeof buf);
384
385 // product description
386 } else if (id == 2) {
387 strlcpy (buf, hcd->product_desc, sizeof buf);
388
389 // id 3 == vendor description
390 } else if (id == 3) {
Serge E. Hallyn96b644b2006-10-02 02:18:13 -0700391 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
392 init_utsname()->release, hcd->driver->description);
Roel Kluin71d27182009-03-13 12:19:18 +0100393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 switch (len) { /* All cases fall through */
396 default:
397 len = 2 + ascii2utf (buf, data + 2, len - 2);
398 case 2:
399 data [1] = 3; /* type == string */
400 case 1:
401 data [0] = 2 * (strlen (buf) + 1);
402 case 0:
403 ; /* Compiler wants a statement here */
404 }
405 return len;
406}
407
408
409/* Root hub control transfers execute synchronously */
410static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
411{
412 struct usb_ctrlrequest *cmd;
413 u16 typeReq, wValue, wIndex, wLength;
414 u8 *ubuf = urb->transfer_buffer;
Mikael Pettersson54bee6e2006-09-23 17:05:31 -0700415 u8 tbuf [sizeof (struct usb_hub_descriptor)]
416 __attribute__((aligned(4)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 const u8 *bufp = tbuf;
Roel Kluin71d27182009-03-13 12:19:18 +0100418 unsigned len = 0;
Alan Sterne9df41c2007-08-08 11:48:02 -0400419 int status;
Alan Stern7329e212008-04-03 18:02:56 -0400420 u8 patch_wakeup = 0;
421 u8 patch_protocol = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Alan Stern9439eb92007-08-02 15:05:45 -0400423 might_sleep();
424
Alan Sterne9df41c2007-08-08 11:48:02 -0400425 spin_lock_irq(&hcd_root_hub_lock);
426 status = usb_hcd_link_urb_to_ep(hcd, urb);
427 spin_unlock_irq(&hcd_root_hub_lock);
428 if (status)
429 return status;
Alan Sternb0d9efb2007-08-21 15:39:21 -0400430 urb->hcpriv = hcd; /* Indicate it's queued */
Alan Sterne9df41c2007-08-08 11:48:02 -0400431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 cmd = (struct usb_ctrlrequest *) urb->setup_packet;
433 typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
434 wValue = le16_to_cpu (cmd->wValue);
435 wIndex = le16_to_cpu (cmd->wIndex);
436 wLength = le16_to_cpu (cmd->wLength);
437
438 if (wLength > urb->transfer_buffer_length)
439 goto error;
440
441 urb->actual_length = 0;
442 switch (typeReq) {
443
444 /* DEVICE REQUESTS */
445
David Brownellfb669cc2006-01-24 08:40:27 -0800446 /* The root hub's remote wakeup enable bit is implemented using
447 * driver model wakeup flags. If this system supports wakeup
448 * through USB, userspace may change the default "allow wakeup"
449 * policy through sysfs or these calls.
450 *
451 * Most root hubs support wakeup from downstream devices, for
452 * runtime power management (disabling USB clocks and reducing
453 * VBUS power usage). However, not all of them do so; silicon,
454 * board, and BIOS bugs here are not uncommon, so these can't
455 * be treated quite like external hubs.
456 *
457 * Likewise, not all root hubs will pass wakeup events upstream,
458 * to wake up the whole system. So don't assume root hub and
459 * controller capabilities are identical.
460 */
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 case DeviceRequest | USB_REQ_GET_STATUS:
David Brownellfb669cc2006-01-24 08:40:27 -0800463 tbuf [0] = (device_may_wakeup(&hcd->self.root_hub->dev)
464 << USB_DEVICE_REMOTE_WAKEUP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 | (1 << USB_DEVICE_SELF_POWERED);
466 tbuf [1] = 0;
467 len = 2;
468 break;
469 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
470 if (wValue == USB_DEVICE_REMOTE_WAKEUP)
David Brownellfb669cc2006-01-24 08:40:27 -0800471 device_set_wakeup_enable(&hcd->self.root_hub->dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 else
473 goto error;
474 break;
475 case DeviceOutRequest | USB_REQ_SET_FEATURE:
David Brownellfb669cc2006-01-24 08:40:27 -0800476 if (device_can_wakeup(&hcd->self.root_hub->dev)
477 && wValue == USB_DEVICE_REMOTE_WAKEUP)
478 device_set_wakeup_enable(&hcd->self.root_hub->dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 else
480 goto error;
481 break;
482 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
483 tbuf [0] = 1;
484 len = 1;
485 /* FALLTHROUGH */
486 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
487 break;
488 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
489 switch (wValue & 0xff00) {
490 case USB_DT_DEVICE << 8:
Sarah Sharpd2e9b4d2009-04-27 19:55:01 -0700491 if (hcd->driver->flags & HCD_USB3)
492 bufp = usb3_rh_dev_descriptor;
493 else if (hcd->driver->flags & HCD_USB2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 bufp = usb2_rh_dev_descriptor;
495 else if (hcd->driver->flags & HCD_USB11)
496 bufp = usb11_rh_dev_descriptor;
497 else
498 goto error;
499 len = 18;
Alan Stern7329e212008-04-03 18:02:56 -0400500 if (hcd->has_tt)
501 patch_protocol = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503 case USB_DT_CONFIG << 8:
Sarah Sharpd2e9b4d2009-04-27 19:55:01 -0700504 if (hcd->driver->flags & HCD_USB3) {
505 bufp = ss_rh_config_descriptor;
506 len = sizeof ss_rh_config_descriptor;
507 } else if (hcd->driver->flags & HCD_USB2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 bufp = hs_rh_config_descriptor;
509 len = sizeof hs_rh_config_descriptor;
510 } else {
511 bufp = fs_rh_config_descriptor;
512 len = sizeof fs_rh_config_descriptor;
513 }
David Brownellfb669cc2006-01-24 08:40:27 -0800514 if (device_can_wakeup(&hcd->self.root_hub->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 patch_wakeup = 1;
516 break;
517 case USB_DT_STRING << 8:
Roel Kluin71d27182009-03-13 12:19:18 +0100518 if ((wValue & 0xff) < 4)
519 urb->actual_length = rh_string(wValue & 0xff,
520 hcd, ubuf, wLength);
521 else /* unsupported IDs --> "protocol stall" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
524 default:
525 goto error;
526 }
527 break;
528 case DeviceRequest | USB_REQ_GET_INTERFACE:
529 tbuf [0] = 0;
530 len = 1;
531 /* FALLTHROUGH */
532 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
533 break;
534 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
535 // wValue == urb->dev->devaddr
536 dev_dbg (hcd->self.controller, "root hub device address %d\n",
537 wValue);
538 break;
539
540 /* INTERFACE REQUESTS (no defined feature/status flags) */
541
542 /* ENDPOINT REQUESTS */
543
544 case EndpointRequest | USB_REQ_GET_STATUS:
545 // ENDPOINT_HALT flag
546 tbuf [0] = 0;
547 tbuf [1] = 0;
548 len = 2;
549 /* FALLTHROUGH */
550 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
551 case EndpointOutRequest | USB_REQ_SET_FEATURE:
552 dev_dbg (hcd->self.controller, "no endpoint features yet\n");
553 break;
554
555 /* CLASS REQUESTS (and errors) */
556
557 default:
558 /* non-generic request */
David Brownellb13296c2005-09-27 10:38:54 -0700559 switch (typeReq) {
560 case GetHubStatus:
561 case GetPortStatus:
562 len = 4;
563 break;
564 case GetHubDescriptor:
565 len = sizeof (struct usb_hub_descriptor);
566 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
David Brownellb13296c2005-09-27 10:38:54 -0700568 status = hcd->driver->hub_control (hcd,
569 typeReq, wValue, wIndex,
570 tbuf, wLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 break;
572error:
573 /* "protocol stall" on error */
574 status = -EPIPE;
575 }
576
577 if (status) {
578 len = 0;
579 if (status != -EPIPE) {
580 dev_dbg (hcd->self.controller,
581 "CTRL: TypeReq=0x%x val=0x%x "
582 "idx=0x%x len=%d ==> %d\n",
583 typeReq, wValue, wIndex,
David Brownellb13296c2005-09-27 10:38:54 -0700584 wLength, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 }
587 if (len) {
588 if (urb->transfer_buffer_length < len)
589 len = urb->transfer_buffer_length;
590 urb->actual_length = len;
591 // always USB_DIR_IN, toward host
592 memcpy (ubuf, bufp, len);
593
594 /* report whether RH hardware supports remote wakeup */
595 if (patch_wakeup &&
596 len > offsetof (struct usb_config_descriptor,
597 bmAttributes))
598 ((struct usb_config_descriptor *)ubuf)->bmAttributes
599 |= USB_CONFIG_ATT_WAKEUP;
Alan Stern7329e212008-04-03 18:02:56 -0400600
601 /* report whether RH hardware has an integrated TT */
602 if (patch_protocol &&
603 len > offsetof(struct usb_device_descriptor,
604 bDeviceProtocol))
605 ((struct usb_device_descriptor *) ubuf)->
606 bDeviceProtocol = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 /* any errors get returned through the urb completion */
Alan Stern9439eb92007-08-02 15:05:45 -0400610 spin_lock_irq(&hcd_root_hub_lock);
Alan Sterne9df41c2007-08-08 11:48:02 -0400611 usb_hcd_unlink_urb_from_ep(hcd, urb);
Alan Stern9439eb92007-08-02 15:05:45 -0400612
613 /* This peculiar use of spinlocks echoes what real HC drivers do.
614 * Avoiding calls to local_irq_disable/enable makes the code
615 * RT-friendly.
616 */
617 spin_unlock(&hcd_root_hub_lock);
Alan Stern4a000272007-08-24 15:42:24 -0400618 usb_hcd_giveback_urb(hcd, urb, status);
Alan Stern9439eb92007-08-02 15:05:45 -0400619 spin_lock(&hcd_root_hub_lock);
620
621 spin_unlock_irq(&hcd_root_hub_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return 0;
623}
624
625/*-------------------------------------------------------------------------*/
626
627/*
Alan Sternd5926ae72005-04-21 15:56:37 -0400628 * Root Hub interrupt transfers are polled using a timer if the
629 * driver requests it; otherwise the driver is responsible for
630 * calling usb_hcd_poll_rh_status() when an event occurs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 *
Alan Sternd5926ae72005-04-21 15:56:37 -0400632 * Completions are called in_interrupt(), but they may or may not
633 * be in_irq().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
Alan Sternd5926ae72005-04-21 15:56:37 -0400635void usb_hcd_poll_rh_status(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct urb *urb;
Alan Sternd5926ae72005-04-21 15:56:37 -0400638 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 unsigned long flags;
Alan Sternd5926ae72005-04-21 15:56:37 -0400640 char buffer[4]; /* Any root hubs with > 31 ports? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Alan Stern1b42ae62007-03-13 11:10:52 -0400642 if (unlikely(!hcd->rh_registered))
643 return;
Alan Sternd5926ae72005-04-21 15:56:37 -0400644 if (!hcd->uses_new_polling && !hcd->status_urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Alan Sternd5926ae72005-04-21 15:56:37 -0400647 length = hcd->driver->hub_status_data(hcd, buffer);
648 if (length > 0) {
649
650 /* try to complete the status urb */
Alan Stern9439eb92007-08-02 15:05:45 -0400651 spin_lock_irqsave(&hcd_root_hub_lock, flags);
Alan Sternd5926ae72005-04-21 15:56:37 -0400652 urb = hcd->status_urb;
653 if (urb) {
Alan Sterne9df41c2007-08-08 11:48:02 -0400654 hcd->poll_pending = 0;
655 hcd->status_urb = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -0400656 urb->actual_length = length;
657 memcpy(urb->transfer_buffer, buffer, length);
Alan Stern9439eb92007-08-02 15:05:45 -0400658
Alan Sterne9df41c2007-08-08 11:48:02 -0400659 usb_hcd_unlink_urb_from_ep(hcd, urb);
Alan Stern9439eb92007-08-02 15:05:45 -0400660 spin_unlock(&hcd_root_hub_lock);
Alan Stern4a000272007-08-24 15:42:24 -0400661 usb_hcd_giveback_urb(hcd, urb, 0);
Alan Stern9439eb92007-08-02 15:05:45 -0400662 spin_lock(&hcd_root_hub_lock);
Alan Sterne9df41c2007-08-08 11:48:02 -0400663 } else {
Alan Sternd5926ae72005-04-21 15:56:37 -0400664 length = 0;
Alan Sternd5926ae72005-04-21 15:56:37 -0400665 hcd->poll_pending = 1;
Alan Sterne9df41c2007-08-08 11:48:02 -0400666 }
Alan Stern9439eb92007-08-02 15:05:45 -0400667 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
Alan Sternd5926ae72005-04-21 15:56:37 -0400668 }
669
670 /* The USB 2.0 spec says 256 ms. This is close enough and won't
Arjan van de Ven01cd0812007-05-22 12:42:56 -0700671 * exceed that limit if HZ is 100. The math is more clunky than
672 * maybe expected, this is to make sure that all timers for USB devices
673 * fire at the same time to give the CPU a break inbetween */
Alan Sternd5926ae72005-04-21 15:56:37 -0400674 if (hcd->uses_new_polling ? hcd->poll_rh :
675 (length == 0 && hcd->status_urb != NULL))
Arjan van de Ven01cd0812007-05-22 12:42:56 -0700676 mod_timer (&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
Alan Sternd5926ae72005-04-21 15:56:37 -0400677}
678EXPORT_SYMBOL_GPL(usb_hcd_poll_rh_status);
679
680/* timer callback */
681static void rh_timer_func (unsigned long _hcd)
682{
683 usb_hcd_poll_rh_status((struct usb_hcd *) _hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686/*-------------------------------------------------------------------------*/
687
Alan Sternd5926ae72005-04-21 15:56:37 -0400688static int rh_queue_status (struct usb_hcd *hcd, struct urb *urb)
689{
690 int retval;
691 unsigned long flags;
Roel Kluin71d27182009-03-13 12:19:18 +0100692 unsigned len = 1 + (urb->dev->maxchild / 8);
Alan Sternd5926ae72005-04-21 15:56:37 -0400693
694 spin_lock_irqsave (&hcd_root_hub_lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -0400695 if (hcd->status_urb || urb->transfer_buffer_length < len) {
Alan Sternd5926ae72005-04-21 15:56:37 -0400696 dev_dbg (hcd->self.controller, "not queuing rh status urb\n");
697 retval = -EINVAL;
Alan Sterne9df41c2007-08-08 11:48:02 -0400698 goto done;
Alan Sternd5926ae72005-04-21 15:56:37 -0400699 }
Alan Sterne9df41c2007-08-08 11:48:02 -0400700
701 retval = usb_hcd_link_urb_to_ep(hcd, urb);
702 if (retval)
703 goto done;
704
705 hcd->status_urb = urb;
706 urb->hcpriv = hcd; /* indicate it's queued */
707 if (!hcd->uses_new_polling)
708 mod_timer(&hcd->rh_timer, (jiffies/(HZ/4) + 1) * (HZ/4));
709
710 /* If a status change has already occurred, report it ASAP */
711 else if (hcd->poll_pending)
712 mod_timer(&hcd->rh_timer, jiffies);
713 retval = 0;
714 done:
Alan Sternd5926ae72005-04-21 15:56:37 -0400715 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
716 return retval;
717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
720{
Alan Stern5e60a162007-07-30 17:07:21 -0400721 if (usb_endpoint_xfer_int(&urb->ep->desc))
Alan Sternd5926ae72005-04-21 15:56:37 -0400722 return rh_queue_status (hcd, urb);
Alan Stern5e60a162007-07-30 17:07:21 -0400723 if (usb_endpoint_xfer_control(&urb->ep->desc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return rh_call_control (hcd, urb);
Alan Sternd5926ae72005-04-21 15:56:37 -0400725 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728/*-------------------------------------------------------------------------*/
729
Alan Stern455b25f2006-08-11 16:01:45 -0400730/* Unlinks of root-hub control URBs are legal, but they don't do anything
731 * since these URBs always execute synchronously.
Alan Sternd5926ae72005-04-21 15:56:37 -0400732 */
Alan Sterne9df41c2007-08-08 11:48:02 -0400733static int usb_rh_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Alan Stern455b25f2006-08-11 16:01:45 -0400735 unsigned long flags;
Alan Sterne9df41c2007-08-08 11:48:02 -0400736 int rc;
Alan Stern455b25f2006-08-11 16:01:45 -0400737
Alan Stern9439eb92007-08-02 15:05:45 -0400738 spin_lock_irqsave(&hcd_root_hub_lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -0400739 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
740 if (rc)
741 goto done;
742
Alan Stern5e60a162007-07-30 17:07:21 -0400743 if (usb_endpoint_num(&urb->ep->desc) == 0) { /* Control URB */
Alan Stern455b25f2006-08-11 16:01:45 -0400744 ; /* Do nothing */
Alan Sternd5926ae72005-04-21 15:56:37 -0400745
746 } else { /* Status URB */
747 if (!hcd->uses_new_polling)
Alan Stern455b25f2006-08-11 16:01:45 -0400748 del_timer (&hcd->rh_timer);
Alan Sternd5926ae72005-04-21 15:56:37 -0400749 if (urb == hcd->status_urb) {
750 hcd->status_urb = NULL;
Alan Sterne9df41c2007-08-08 11:48:02 -0400751 usb_hcd_unlink_urb_from_ep(hcd, urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Alan Stern9439eb92007-08-02 15:05:45 -0400753 spin_unlock(&hcd_root_hub_lock);
Alan Stern4a000272007-08-24 15:42:24 -0400754 usb_hcd_giveback_urb(hcd, urb, status);
Alan Stern9439eb92007-08-02 15:05:45 -0400755 spin_lock(&hcd_root_hub_lock);
756 }
757 }
Alan Sterne9df41c2007-08-08 11:48:02 -0400758 done:
Alan Stern9439eb92007-08-02 15:05:45 -0400759 spin_unlock_irqrestore(&hcd_root_hub_lock, flags);
Alan Sterne9df41c2007-08-08 11:48:02 -0400760 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Inaky Perez-Gonzalez5234ce12007-07-31 20:33:58 -0700763
764
765/*
766 * Show & store the current value of authorized_default
767 */
768static ssize_t usb_host_authorized_default_show(struct device *dev,
769 struct device_attribute *attr,
770 char *buf)
771{
772 struct usb_device *rh_usb_dev = to_usb_device(dev);
773 struct usb_bus *usb_bus = rh_usb_dev->bus;
774 struct usb_hcd *usb_hcd;
775
776 if (usb_bus == NULL) /* FIXME: not sure if this case is possible */
777 return -ENODEV;
778 usb_hcd = bus_to_hcd(usb_bus);
779 return snprintf(buf, PAGE_SIZE, "%u\n", usb_hcd->authorized_default);
780}
781
782static ssize_t usb_host_authorized_default_store(struct device *dev,
783 struct device_attribute *attr,
784 const char *buf, size_t size)
785{
786 ssize_t result;
787 unsigned val;
788 struct usb_device *rh_usb_dev = to_usb_device(dev);
789 struct usb_bus *usb_bus = rh_usb_dev->bus;
790 struct usb_hcd *usb_hcd;
791
792 if (usb_bus == NULL) /* FIXME: not sure if this case is possible */
793 return -ENODEV;
794 usb_hcd = bus_to_hcd(usb_bus);
795 result = sscanf(buf, "%u\n", &val);
796 if (result == 1) {
797 usb_hcd->authorized_default = val? 1 : 0;
798 result = size;
799 }
800 else
801 result = -EINVAL;
802 return result;
803}
804
805static DEVICE_ATTR(authorized_default, 0644,
806 usb_host_authorized_default_show,
807 usb_host_authorized_default_store);
808
809
810/* Group all the USB bus attributes */
811static struct attribute *usb_bus_attrs[] = {
812 &dev_attr_authorized_default.attr,
813 NULL,
814};
815
816static struct attribute_group usb_bus_attr_group = {
817 .name = NULL, /* we want them in the same directory */
818 .attrs = usb_bus_attrs,
819};
820
821
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823/*-------------------------------------------------------------------------*/
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/**
826 * usb_bus_init - shared initialization code
827 * @bus: the bus structure being initialized
828 *
829 * This code is used to initialize a usb_bus structure, memory for which is
830 * separately managed.
831 */
832static void usb_bus_init (struct usb_bus *bus)
833{
834 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
835
836 bus->devnum_next = 1;
837
838 bus->root_hub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 bus->busnum = -1;
840 bus->bandwidth_allocated = 0;
841 bus->bandwidth_int_reqs = 0;
842 bus->bandwidth_isoc_reqs = 0;
843
844 INIT_LIST_HEAD (&bus->bus_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/*-------------------------------------------------------------------------*/
848
849/**
850 * usb_register_bus - registers the USB host controller with the usb core
851 * @bus: pointer to the bus to register
852 * Context: !in_interrupt()
853 *
854 * Assigns a bus number, and links the controller into usbcore data
855 * structures so that it can be seen by scanning the bus list.
856 */
857static int usb_register_bus(struct usb_bus *bus)
858{
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700859 int result = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 int busnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100862 mutex_lock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700864 if (busnum >= USB_MAXBUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700866 goto error_find_busnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700868 set_bit (busnum, busmap.busmap);
869 bus->busnum = busnum;
Tony Jones5a3201b2007-09-11 14:07:31 -0700870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 /* Add it to the local list of buses */
872 list_add (&bus->bus_list, &usb_bus_list);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100873 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -0700875 usb_notify_add_bus(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700877 dev_info (bus->controller, "new USB bus registered, assigned bus "
878 "number %d\n", bus->busnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 0;
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700880
Inaky Perez-Gonzalezeb579f52007-07-31 20:33:59 -0700881error_find_busnum:
882 mutex_unlock(&usb_bus_list_lock);
883 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886/**
887 * usb_deregister_bus - deregisters the USB host controller
888 * @bus: pointer to the bus to deregister
889 * Context: !in_interrupt()
890 *
891 * Recycles the bus number, and unlinks the controller from usbcore data
892 * structures so that it won't be seen by scanning the bus list.
893 */
894static void usb_deregister_bus (struct usb_bus *bus)
895{
896 dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
897
898 /*
899 * NOTE: make sure that all the devices are removed by the
900 * controller code, as well as having it call this when cleaning
901 * itself up
902 */
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100903 mutex_lock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 list_del (&bus->bus_list);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100905 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -0700907 usb_notify_remove_bus(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 clear_bit (bus->busnum, busmap.busmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912/**
Alan Stern8ec8d202005-04-25 11:25:17 -0400913 * register_root_hub - called by usb_add_hcd() to register a root hub
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * @hcd: host controller for this root hub
915 *
Alan Stern8ec8d202005-04-25 11:25:17 -0400916 * This function registers the root hub with the USB subsystem. It sets up
David Brownellb1e8f0a2006-01-23 15:25:40 -0800917 * the device properly in the device tree and then calls usb_new_device()
918 * to register the usb device. It also assigns the root hub's USB address
919 * (always 1).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 */
David Brownellb1e8f0a2006-01-23 15:25:40 -0800921static int register_root_hub(struct usb_hcd *hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 struct device *parent_dev = hcd->self.controller;
David Brownellb1e8f0a2006-01-23 15:25:40 -0800924 struct usb_device *usb_dev = hcd->self.root_hub;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 const int devnum = 1;
926 int retval;
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 usb_dev->devnum = devnum;
929 usb_dev->bus->devnum_next = devnum + 1;
930 memset (&usb_dev->bus->devmap.devicemap, 0,
931 sizeof usb_dev->bus->devmap.devicemap);
932 set_bit (devnum, usb_dev->bus->devmap.devicemap);
933 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
934
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100935 mutex_lock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Harvey Harrison551509d2009-02-11 14:11:36 -0800937 usb_dev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
939 if (retval != sizeof usb_dev->descriptor) {
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100940 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +0200942 dev_name(&usb_dev->dev), retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return (retval < 0) ? retval : -EMSGSIZE;
944 }
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 retval = usb_new_device (usb_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 dev_err (parent_dev, "can't register root hub for %s, %d\n",
Kay Sievers7071a3c2008-05-02 06:02:41 +0200949 dev_name(&usb_dev->dev), retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +0100951 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 if (retval == 0) {
954 spin_lock_irq (&hcd_root_hub_lock);
955 hcd->rh_registered = 1;
956 spin_unlock_irq (&hcd_root_hub_lock);
957
958 /* Did the HC die before the root hub was registered? */
959 if (hcd->state == HC_STATE_HALT)
960 usb_hc_died (hcd); /* This time clean up */
961 }
962
963 return retval;
964}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966
967/*-------------------------------------------------------------------------*/
968
969/**
970 * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
971 * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
972 * @is_input: true iff the transaction sends data to the host
973 * @isoc: true for isochronous transactions, false for interrupt ones
974 * @bytecount: how many bytes in the transaction.
975 *
976 * Returns approximate bus time in nanoseconds for a periodic transaction.
977 * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
978 * scheduled in software, this function is only used for such scheduling.
979 */
980long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
981{
982 unsigned long tmp;
983
984 switch (speed) {
985 case USB_SPEED_LOW: /* INTR only */
986 if (is_input) {
987 tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
988 return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
989 } else {
990 tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
991 return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
992 }
993 case USB_SPEED_FULL: /* ISOC or INTR */
994 if (isoc) {
995 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
996 return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
997 } else {
998 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
999 return (9107L + BW_HOST_DELAY + tmp);
1000 }
1001 case USB_SPEED_HIGH: /* ISOC or INTR */
1002 // FIXME adjust for input vs output
1003 if (isoc)
Dan Streetman498f78e2005-07-29 12:18:28 -07001004 tmp = HS_NSECS_ISO (bytecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 else
Dan Streetman498f78e2005-07-29 12:18:28 -07001006 tmp = HS_NSECS (bytecount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return tmp;
1008 default:
1009 pr_debug ("%s: bogus device speed!\n", usbcore_name);
1010 return -1;
1011 }
1012}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001013EXPORT_SYMBOL_GPL(usb_calc_bus_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016/*-------------------------------------------------------------------------*/
1017
1018/*
1019 * Generic HC operations.
1020 */
1021
1022/*-------------------------------------------------------------------------*/
1023
Alan Sterne9df41c2007-08-08 11:48:02 -04001024/**
1025 * usb_hcd_link_urb_to_ep - add an URB to its endpoint queue
1026 * @hcd: host controller to which @urb was submitted
1027 * @urb: URB being submitted
1028 *
1029 * Host controller drivers should call this routine in their enqueue()
1030 * method. The HCD's private spinlock must be held and interrupts must
1031 * be disabled. The actions carried out here are required for URB
1032 * submission, as well as for endpoint shutdown and for usb_kill_urb.
1033 *
1034 * Returns 0 for no error, otherwise a negative error code (in which case
1035 * the enqueue() method must fail). If no error occurs but enqueue() fails
1036 * anyway, it must call usb_hcd_unlink_urb_from_ep() before releasing
1037 * the private spinlock and returning.
1038 */
1039int usb_hcd_link_urb_to_ep(struct usb_hcd *hcd, struct urb *urb)
Alan Stern9a9bf402007-08-02 15:06:54 -04001040{
Alan Stern9a9bf402007-08-02 15:06:54 -04001041 int rc = 0;
1042
Alan Sterne9df41c2007-08-08 11:48:02 -04001043 spin_lock(&hcd_urb_list_lock);
Alan Stern9a9bf402007-08-02 15:06:54 -04001044
1045 /* Check that the URB isn't being killed */
Ming Lei49367d82008-12-12 21:38:45 +08001046 if (unlikely(atomic_read(&urb->reject))) {
Alan Stern9a9bf402007-08-02 15:06:54 -04001047 rc = -EPERM;
1048 goto done;
1049 }
1050
1051 if (unlikely(!urb->ep->enabled)) {
1052 rc = -ENOENT;
1053 goto done;
1054 }
1055
Alan Stern6840d252007-09-10 11:34:26 -04001056 if (unlikely(!urb->dev->can_submit)) {
1057 rc = -EHOSTUNREACH;
1058 goto done;
1059 }
1060
Alan Stern9a9bf402007-08-02 15:06:54 -04001061 /*
1062 * Check the host controller's state and add the URB to the
1063 * endpoint's queue.
1064 */
1065 switch (hcd->state) {
1066 case HC_STATE_RUNNING:
1067 case HC_STATE_RESUMING:
Alan Sterneb231052007-08-21 15:40:36 -04001068 urb->unlinked = 0;
Alan Stern9a9bf402007-08-02 15:06:54 -04001069 list_add_tail(&urb->urb_list, &urb->ep->urb_list);
1070 break;
1071 default:
1072 rc = -ESHUTDOWN;
1073 goto done;
1074 }
1075 done:
Alan Sterne9df41c2007-08-08 11:48:02 -04001076 spin_unlock(&hcd_urb_list_lock);
Alan Stern9a9bf402007-08-02 15:06:54 -04001077 return rc;
1078}
Alan Sterne9df41c2007-08-08 11:48:02 -04001079EXPORT_SYMBOL_GPL(usb_hcd_link_urb_to_ep);
Alan Stern9a9bf402007-08-02 15:06:54 -04001080
Alan Sterne9df41c2007-08-08 11:48:02 -04001081/**
1082 * usb_hcd_check_unlink_urb - check whether an URB may be unlinked
1083 * @hcd: host controller to which @urb was submitted
1084 * @urb: URB being checked for unlinkability
1085 * @status: error code to store in @urb if the unlink succeeds
1086 *
1087 * Host controller drivers should call this routine in their dequeue()
1088 * method. The HCD's private spinlock must be held and interrupts must
1089 * be disabled. The actions carried out here are required for making
1090 * sure than an unlink is valid.
1091 *
1092 * Returns 0 for no error, otherwise a negative error code (in which case
1093 * the dequeue() method must fail). The possible error codes are:
1094 *
1095 * -EIDRM: @urb was not submitted or has already completed.
1096 * The completion function may not have been called yet.
1097 *
1098 * -EBUSY: @urb has already been unlinked.
1099 */
1100int usb_hcd_check_unlink_urb(struct usb_hcd *hcd, struct urb *urb,
Alan Stern9a9bf402007-08-02 15:06:54 -04001101 int status)
1102{
Alan Stern9a9bf402007-08-02 15:06:54 -04001103 struct list_head *tmp;
Alan Stern9a9bf402007-08-02 15:06:54 -04001104
1105 /* insist the urb is still queued */
1106 list_for_each(tmp, &urb->ep->urb_list) {
1107 if (tmp == &urb->urb_list)
1108 break;
1109 }
Alan Sterne9df41c2007-08-08 11:48:02 -04001110 if (tmp != &urb->urb_list)
1111 return -EIDRM;
Alan Stern9a9bf402007-08-02 15:06:54 -04001112
1113 /* Any status except -EINPROGRESS means something already started to
1114 * unlink this URB from the hardware. So there's no more work to do.
1115 */
Alan Sterneb231052007-08-21 15:40:36 -04001116 if (urb->unlinked)
Alan Sterne9df41c2007-08-08 11:48:02 -04001117 return -EBUSY;
Alan Sterneb231052007-08-21 15:40:36 -04001118 urb->unlinked = status;
Alan Stern9a9bf402007-08-02 15:06:54 -04001119
1120 /* IRQ setup can easily be broken so that USB controllers
1121 * never get completion IRQs ... maybe even the ones we need to
1122 * finish unlinking the initial failed usb_set_address()
1123 * or device descriptor fetch.
1124 */
1125 if (!test_bit(HCD_FLAG_SAW_IRQ, &hcd->flags) &&
1126 !is_root_hub(urb->dev)) {
1127 dev_warn(hcd->self.controller, "Unlink after no-IRQ? "
1128 "Controller is probably using the wrong IRQ.\n");
1129 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
1130 }
1131
Alan Sterne9df41c2007-08-08 11:48:02 -04001132 return 0;
Alan Stern9a9bf402007-08-02 15:06:54 -04001133}
Alan Sterne9df41c2007-08-08 11:48:02 -04001134EXPORT_SYMBOL_GPL(usb_hcd_check_unlink_urb);
Alan Stern9a9bf402007-08-02 15:06:54 -04001135
Alan Sterne9df41c2007-08-08 11:48:02 -04001136/**
1137 * usb_hcd_unlink_urb_from_ep - remove an URB from its endpoint queue
1138 * @hcd: host controller to which @urb was submitted
1139 * @urb: URB being unlinked
1140 *
1141 * Host controller drivers should call this routine before calling
1142 * usb_hcd_giveback_urb(). The HCD's private spinlock must be held and
1143 * interrupts must be disabled. The actions carried out here are required
1144 * for URB completion.
1145 */
1146void usb_hcd_unlink_urb_from_ep(struct usb_hcd *hcd, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* clear all state linking urb to this dev (and hcd) */
Alan Sterne9df41c2007-08-08 11:48:02 -04001149 spin_lock(&hcd_urb_list_lock);
Alan Stern9a9bf402007-08-02 15:06:54 -04001150 list_del_init(&urb->urb_list);
Alan Sterne9df41c2007-08-08 11:48:02 -04001151 spin_unlock(&hcd_urb_list_lock);
Pete Zaitcev9f6a93f2007-06-08 13:37:49 -07001152}
Alan Sterne9df41c2007-08-08 11:48:02 -04001153EXPORT_SYMBOL_GPL(usb_hcd_unlink_urb_from_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Magnus Dammb3476672008-01-23 15:58:35 +09001155/*
1156 * Some usb host controllers can only perform dma using a small SRAM area.
1157 * The usb core itself is however optimized for host controllers that can dma
1158 * using regular system memory - like pci devices doing bus mastering.
1159 *
1160 * To support host controllers with limited dma capabilites we provide dma
1161 * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
1162 * For this to work properly the host controller code must first use the
1163 * function dma_declare_coherent_memory() to point out which memory area
1164 * that should be used for dma allocations.
1165 *
1166 * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
1167 * dma using dma_alloc_coherent() which in turn allocates from the memory
1168 * area pointed out with dma_declare_coherent_memory().
1169 *
1170 * So, to summarize...
1171 *
1172 * - We need "local" memory, canonical example being
1173 * a small SRAM on a discrete controller being the
1174 * only memory that the controller can read ...
1175 * (a) "normal" kernel memory is no good, and
1176 * (b) there's not enough to share
1177 *
1178 * - The only *portable* hook for such stuff in the
1179 * DMA framework is dma_declare_coherent_memory()
1180 *
1181 * - So we use that, even though the primary requirement
1182 * is that the memory be "local" (hence addressible
1183 * by that device), not "coherent".
1184 *
1185 */
1186
1187static int hcd_alloc_coherent(struct usb_bus *bus,
1188 gfp_t mem_flags, dma_addr_t *dma_handle,
1189 void **vaddr_handle, size_t size,
1190 enum dma_data_direction dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Magnus Dammb3476672008-01-23 15:58:35 +09001192 unsigned char *vaddr;
1193
1194 vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
1195 mem_flags, dma_handle);
1196 if (!vaddr)
1197 return -ENOMEM;
1198
1199 /*
1200 * Store the virtual address of the buffer at the end
1201 * of the allocated dma buffer. The size of the buffer
1202 * may be uneven so use unaligned functions instead
1203 * of just rounding up. It makes sense to optimize for
1204 * memory footprint over access speed since the amount
1205 * of memory available for dma may be limited.
1206 */
1207 put_unaligned((unsigned long)*vaddr_handle,
1208 (unsigned long *)(vaddr + size));
1209
1210 if (dir == DMA_TO_DEVICE)
1211 memcpy(vaddr, *vaddr_handle, size);
1212
1213 *vaddr_handle = vaddr;
1214 return 0;
1215}
1216
1217static void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
1218 void **vaddr_handle, size_t size,
1219 enum dma_data_direction dir)
1220{
1221 unsigned char *vaddr = *vaddr_handle;
1222
1223 vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
1224
1225 if (dir == DMA_FROM_DEVICE)
1226 memcpy(vaddr, *vaddr_handle, size);
1227
1228 hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
1229
1230 *vaddr_handle = vaddr;
1231 *dma_handle = 0;
1232}
1233
1234static int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1235 gfp_t mem_flags)
1236{
1237 enum dma_data_direction dir;
1238 int ret = 0;
1239
Alan Stern9a9bf402007-08-02 15:06:54 -04001240 /* Map the URB's buffers for DMA access.
1241 * Lower level HCD code should use *_dma exclusively,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 * unless it uses pio or talks to another transport.
1243 */
Magnus Dammb3476672008-01-23 15:58:35 +09001244 if (is_root_hub(urb->dev))
1245 return 0;
1246
1247 if (usb_endpoint_xfer_control(&urb->ep->desc)
1248 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
1249 if (hcd->self.uses_dma)
1250 urb->setup_dma = dma_map_single(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 hcd->self.controller,
1252 urb->setup_packet,
Magnus Dammb3476672008-01-23 15:58:35 +09001253 sizeof(struct usb_ctrlrequest),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 DMA_TO_DEVICE);
Magnus Dammb3476672008-01-23 15:58:35 +09001255 else if (hcd->driver->flags & HCD_LOCAL_MEM)
1256 ret = hcd_alloc_coherent(
1257 urb->dev->bus, mem_flags,
1258 &urb->setup_dma,
1259 (void **)&urb->setup_packet,
1260 sizeof(struct usb_ctrlrequest),
1261 DMA_TO_DEVICE);
1262 }
1263
1264 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1265 if (ret == 0 && urb->transfer_buffer_length != 0
1266 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
1267 if (hcd->self.uses_dma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 urb->transfer_dma = dma_map_single (
1269 hcd->self.controller,
1270 urb->transfer_buffer,
1271 urb->transfer_buffer_length,
Magnus Dammb3476672008-01-23 15:58:35 +09001272 dir);
1273 else if (hcd->driver->flags & HCD_LOCAL_MEM) {
1274 ret = hcd_alloc_coherent(
1275 urb->dev->bus, mem_flags,
1276 &urb->transfer_dma,
1277 &urb->transfer_buffer,
1278 urb->transfer_buffer_length,
1279 dir);
1280
1281 if (ret && usb_endpoint_xfer_control(&urb->ep->desc)
1282 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
1283 hcd_free_coherent(urb->dev->bus,
1284 &urb->setup_dma,
1285 (void **)&urb->setup_packet,
1286 sizeof(struct usb_ctrlrequest),
1287 DMA_TO_DEVICE);
1288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
Magnus Dammb3476672008-01-23 15:58:35 +09001290 return ret;
Alan Stern9a9bf402007-08-02 15:06:54 -04001291}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Alan Stern9a9bf402007-08-02 15:06:54 -04001293static void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
1294{
Magnus Dammb3476672008-01-23 15:58:35 +09001295 enum dma_data_direction dir;
1296
1297 if (is_root_hub(urb->dev))
1298 return;
1299
1300 if (usb_endpoint_xfer_control(&urb->ep->desc)
1301 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP)) {
1302 if (hcd->self.uses_dma)
Alan Stern9a9bf402007-08-02 15:06:54 -04001303 dma_unmap_single(hcd->self.controller, urb->setup_dma,
1304 sizeof(struct usb_ctrlrequest),
1305 DMA_TO_DEVICE);
Magnus Dammb3476672008-01-23 15:58:35 +09001306 else if (hcd->driver->flags & HCD_LOCAL_MEM)
1307 hcd_free_coherent(urb->dev->bus, &urb->setup_dma,
1308 (void **)&urb->setup_packet,
1309 sizeof(struct usb_ctrlrequest),
1310 DMA_TO_DEVICE);
1311 }
1312
1313 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
1314 if (urb->transfer_buffer_length != 0
1315 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)) {
1316 if (hcd->self.uses_dma)
Alan Stern9a9bf402007-08-02 15:06:54 -04001317 dma_unmap_single(hcd->self.controller,
1318 urb->transfer_dma,
1319 urb->transfer_buffer_length,
Magnus Dammb3476672008-01-23 15:58:35 +09001320 dir);
1321 else if (hcd->driver->flags & HCD_LOCAL_MEM)
1322 hcd_free_coherent(urb->dev->bus, &urb->transfer_dma,
1323 &urb->transfer_buffer,
1324 urb->transfer_buffer_length,
1325 dir);
Alan Stern9a9bf402007-08-02 15:06:54 -04001326 }
1327}
1328
1329/*-------------------------------------------------------------------------*/
1330
1331/* may be called in any context with a valid urb->dev usecount
1332 * caller surrenders "ownership" of urb
1333 * expects usb_submit_urb() to have sanity checked and conditioned all
1334 * inputs in the urb
1335 */
1336int usb_hcd_submit_urb (struct urb *urb, gfp_t mem_flags)
1337{
1338 int status;
1339 struct usb_hcd *hcd = bus_to_hcd(urb->dev->bus);
1340
1341 /* increment urb's reference count as part of giving it to the HCD
1342 * (which will control it). HCD guarantees that it either returns
1343 * an error or calls giveback(), but not both.
1344 */
1345 usb_get_urb(urb);
1346 atomic_inc(&urb->use_count);
Sarah Sharp4d59d8a2007-10-03 14:56:03 -07001347 atomic_inc(&urb->dev->urbnum);
Alan Stern9a9bf402007-08-02 15:06:54 -04001348 usbmon_urb_submit(&hcd->self, urb);
1349
1350 /* NOTE requirements on root-hub callers (usbfs and the hub
1351 * driver, for now): URBs' urb->transfer_buffer must be
1352 * valid and usb_buffer_{sync,unmap}() not be needed, since
1353 * they could clobber root hub response data. Also, control
1354 * URBs must be submitted in process context with interrupts
1355 * enabled.
1356 */
Magnus Dammb3476672008-01-23 15:58:35 +09001357 status = map_urb_for_dma(hcd, urb, mem_flags);
1358 if (unlikely(status)) {
1359 usbmon_urb_submit_error(&hcd->self, urb, status);
1360 goto error;
1361 }
1362
Alan Sterne9df41c2007-08-08 11:48:02 -04001363 if (is_root_hub(urb->dev))
1364 status = rh_urb_enqueue(hcd, urb);
1365 else
1366 status = hcd->driver->urb_enqueue(hcd, urb, mem_flags);
Alan Stern9a9bf402007-08-02 15:06:54 -04001367
1368 if (unlikely(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 usbmon_urb_submit_error(&hcd->self, urb, status);
Alan Stern9a9bf402007-08-02 15:06:54 -04001370 unmap_urb_for_dma(hcd, urb);
Magnus Dammb3476672008-01-23 15:58:35 +09001371 error:
Alan Sternb0d9efb2007-08-21 15:39:21 -04001372 urb->hcpriv = NULL;
Alan Stern9a9bf402007-08-02 15:06:54 -04001373 INIT_LIST_HEAD(&urb->urb_list);
1374 atomic_dec(&urb->use_count);
Sarah Sharp4d59d8a2007-10-03 14:56:03 -07001375 atomic_dec(&urb->dev->urbnum);
Ming Lei49367d82008-12-12 21:38:45 +08001376 if (atomic_read(&urb->reject))
Alan Stern9a9bf402007-08-02 15:06:54 -04001377 wake_up(&usb_kill_urb_queue);
1378 usb_put_urb(urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 return status;
1381}
1382
1383/*-------------------------------------------------------------------------*/
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/* this makes the hcd giveback() the urb more quickly, by kicking it
1386 * off hardware queues (which may take a while) and returning it as
1387 * soon as practical. we've already set up the urb's return status,
1388 * but we can't know if the callback completed already.
1389 */
Alan Sterne9df41c2007-08-08 11:48:02 -04001390static int unlink1(struct usb_hcd *hcd, struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
1392 int value;
1393
Alan Stern809a58b2007-07-18 12:14:24 -04001394 if (is_root_hub(urb->dev))
Alan Sterne9df41c2007-08-08 11:48:02 -04001395 value = usb_rh_urb_dequeue(hcd, urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 else {
1397
1398 /* The only reason an HCD might fail this call is if
1399 * it has not yet fully queued the urb to begin with.
1400 * Such failures should be harmless. */
Alan Sterne9df41c2007-08-08 11:48:02 -04001401 value = hcd->driver->urb_dequeue(hcd, urb, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return value;
1404}
1405
1406/*
1407 * called in any context
1408 *
1409 * caller guarantees urb won't be recycled till both unlink()
1410 * and the urb's completion function return
1411 */
Alan Sterna6d2bb92006-08-30 11:27:36 -04001412int usb_hcd_unlink_urb (struct urb *urb, int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Alan Stern9a9bf402007-08-02 15:06:54 -04001414 struct usb_hcd *hcd;
Alan Sterncde217a2008-10-21 15:28:46 -04001415 int retval = -EIDRM;
1416 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Alan Sterncde217a2008-10-21 15:28:46 -04001418 /* Prevent the device and bus from going away while
1419 * the unlink is carried out. If they are already gone
1420 * then urb->use_count must be 0, since disconnected
1421 * devices can't have any active URBs.
1422 */
1423 spin_lock_irqsave(&hcd_urb_unlink_lock, flags);
1424 if (atomic_read(&urb->use_count) > 0) {
1425 retval = 0;
1426 usb_get_dev(urb->dev);
1427 }
1428 spin_unlock_irqrestore(&hcd_urb_unlink_lock, flags);
1429 if (retval == 0) {
1430 hcd = bus_to_hcd(urb->dev->bus);
1431 retval = unlink1(hcd, urb, status);
1432 usb_put_dev(urb->dev);
1433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (retval == 0)
1436 retval = -EINPROGRESS;
Alan Sterne9df41c2007-08-08 11:48:02 -04001437 else if (retval != -EIDRM && retval != -EBUSY)
Alan Stern9a9bf402007-08-02 15:06:54 -04001438 dev_dbg(&urb->dev->dev, "hcd_unlink_urb %p fail %d\n",
1439 urb, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return retval;
1441}
1442
1443/*-------------------------------------------------------------------------*/
1444
Alan Stern32aca562007-07-18 12:08:02 -04001445/**
1446 * usb_hcd_giveback_urb - return URB from HCD to device driver
1447 * @hcd: host controller returning the URB
1448 * @urb: urb being returned to the USB device driver.
Alan Stern4a000272007-08-24 15:42:24 -04001449 * @status: completion status code for the URB.
Alan Stern32aca562007-07-18 12:08:02 -04001450 * Context: in_interrupt()
1451 *
1452 * This hands the URB from HCD to its USB device driver, using its
1453 * completion function. The HCD has freed all per-urb resources
1454 * (and is done using urb->hcpriv). It also released all HCD locks;
1455 * the device driver won't cause problems if it frees, modifies,
1456 * or resubmits this URB.
Alan Sterneb231052007-08-21 15:40:36 -04001457 *
Alan Stern4a000272007-08-24 15:42:24 -04001458 * If @urb was unlinked, the value of @status will be overridden by
Alan Sterneb231052007-08-21 15:40:36 -04001459 * @urb->unlinked. Erroneous short transfers are detected in case
1460 * the HCD hasn't checked for them.
Alan Stern32aca562007-07-18 12:08:02 -04001461 */
Alan Stern4a000272007-08-24 15:42:24 -04001462void usb_hcd_giveback_urb(struct usb_hcd *hcd, struct urb *urb, int status)
Alan Stern32aca562007-07-18 12:08:02 -04001463{
Alan Sternb0d9efb2007-08-21 15:39:21 -04001464 urb->hcpriv = NULL;
Alan Sterneb231052007-08-21 15:40:36 -04001465 if (unlikely(urb->unlinked))
Alan Stern4a000272007-08-24 15:42:24 -04001466 status = urb->unlinked;
Alan Sterneb231052007-08-21 15:40:36 -04001467 else if (unlikely((urb->transfer_flags & URB_SHORT_NOT_OK) &&
Alan Sternb0d9efb2007-08-21 15:39:21 -04001468 urb->actual_length < urb->transfer_buffer_length &&
Alan Stern4a000272007-08-24 15:42:24 -04001469 !status))
1470 status = -EREMOTEIO;
Alan Stern32aca562007-07-18 12:08:02 -04001471
Alan Stern1f5a3d02007-08-22 13:06:53 -04001472 unmap_urb_for_dma(hcd, urb);
Alan Stern4a000272007-08-24 15:42:24 -04001473 usbmon_urb_complete(&hcd->self, urb, status);
Alan Stern1f5a3d02007-08-22 13:06:53 -04001474 usb_unanchor_urb(urb);
1475
Alan Stern32aca562007-07-18 12:08:02 -04001476 /* pass ownership to the completion handler */
Alan Stern4a000272007-08-24 15:42:24 -04001477 urb->status = status;
Alan Stern32aca562007-07-18 12:08:02 -04001478 urb->complete (urb);
1479 atomic_dec (&urb->use_count);
Ming Lei49367d82008-12-12 21:38:45 +08001480 if (unlikely(atomic_read(&urb->reject)))
Alan Stern32aca562007-07-18 12:08:02 -04001481 wake_up (&usb_kill_urb_queue);
1482 usb_put_urb (urb);
1483}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001484EXPORT_SYMBOL_GPL(usb_hcd_giveback_urb);
Alan Stern32aca562007-07-18 12:08:02 -04001485
1486/*-------------------------------------------------------------------------*/
1487
Alan Stern95cf82f2007-09-10 11:33:05 -04001488/* Cancel all URBs pending on this endpoint and wait for the endpoint's
1489 * queue to drain completely. The caller must first insure that no more
1490 * URBs can be submitted for this endpoint.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 */
Alan Stern95cf82f2007-09-10 11:33:05 -04001492void usb_hcd_flush_endpoint(struct usb_device *udev,
Alan Sterna6d2bb92006-08-30 11:27:36 -04001493 struct usb_host_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 struct usb_hcd *hcd;
1496 struct urb *urb;
1497
Alan Stern95cf82f2007-09-10 11:33:05 -04001498 if (!ep)
1499 return;
Alan Stern9a9bf402007-08-02 15:06:54 -04001500 might_sleep();
Alan Stern17200582006-08-30 11:32:52 -04001501 hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Alan Stern95cf82f2007-09-10 11:33:05 -04001503 /* No more submits can occur */
Alan Stern9a9bf402007-08-02 15:06:54 -04001504 spin_lock_irq(&hcd_urb_list_lock);
Alan Sternddc1fd62007-11-21 15:13:10 -08001505rescan:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 list_for_each_entry (urb, &ep->urb_list, urb_list) {
Alan Stern5e60a162007-07-30 17:07:21 -04001507 int is_in;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Alan Sterneb231052007-08-21 15:40:36 -04001509 if (urb->unlinked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 continue;
1511 usb_get_urb (urb);
Alan Stern5e60a162007-07-30 17:07:21 -04001512 is_in = usb_urb_dir_in(urb);
Alan Stern809a58b2007-07-18 12:14:24 -04001513 spin_unlock(&hcd_urb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Alan Sterne9df41c2007-08-08 11:48:02 -04001515 /* kick hcd */
1516 unlink1(hcd, urb, -ESHUTDOWN);
1517 dev_dbg (hcd->self.controller,
1518 "shutdown urb %p ep%d%s%s\n",
1519 urb, usb_endpoint_num(&ep->desc),
1520 is_in ? "in" : "out",
1521 ({ char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Alan Sterne9df41c2007-08-08 11:48:02 -04001523 switch (usb_endpoint_type(&ep->desc)) {
1524 case USB_ENDPOINT_XFER_CONTROL:
1525 s = ""; break;
1526 case USB_ENDPOINT_XFER_BULK:
1527 s = "-bulk"; break;
1528 case USB_ENDPOINT_XFER_INT:
1529 s = "-intr"; break;
1530 default:
1531 s = "-iso"; break;
1532 };
1533 s;
1534 }));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 usb_put_urb (urb);
1536
1537 /* list contents may have changed */
Alan Sternddc1fd62007-11-21 15:13:10 -08001538 spin_lock(&hcd_urb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 goto rescan;
1540 }
Alan Stern9a9bf402007-08-02 15:06:54 -04001541 spin_unlock_irq(&hcd_urb_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Alan Stern95cf82f2007-09-10 11:33:05 -04001543 /* Wait until the endpoint queue is completely empty */
Alan Stern455b25f2006-08-11 16:01:45 -04001544 while (!list_empty (&ep->urb_list)) {
Alan Stern809a58b2007-07-18 12:14:24 -04001545 spin_lock_irq(&hcd_urb_list_lock);
Alan Stern455b25f2006-08-11 16:01:45 -04001546
1547 /* The list may have changed while we acquired the spinlock */
1548 urb = NULL;
1549 if (!list_empty (&ep->urb_list)) {
1550 urb = list_entry (ep->urb_list.prev, struct urb,
1551 urb_list);
1552 usb_get_urb (urb);
1553 }
Alan Stern809a58b2007-07-18 12:14:24 -04001554 spin_unlock_irq(&hcd_urb_list_lock);
Alan Stern455b25f2006-08-11 16:01:45 -04001555
1556 if (urb) {
1557 usb_kill_urb (urb);
1558 usb_put_urb (urb);
1559 }
1560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
Alan Stern95cf82f2007-09-10 11:33:05 -04001563/* Disables the endpoint: synchronizes with the hcd to make sure all
1564 * endpoint state is gone from hardware. usb_hcd_flush_endpoint() must
1565 * have been called previously. Use for set_configuration, set_interface,
1566 * driver removal, physical disconnect.
1567 *
1568 * example: a qh stored in ep->hcpriv, holding state related to endpoint
1569 * type, maxpacket size, toggle, halt status, and scheduling.
1570 */
1571void usb_hcd_disable_endpoint(struct usb_device *udev,
1572 struct usb_host_endpoint *ep)
1573{
1574 struct usb_hcd *hcd;
1575
1576 might_sleep();
1577 hcd = bus_to_hcd(udev->bus);
1578 if (hcd->driver->endpoint_disable)
1579 hcd->driver->endpoint_disable(hcd, ep);
1580}
1581
David Vrabel3444b262009-04-08 17:36:28 +00001582/**
1583 * usb_hcd_reset_endpoint - reset host endpoint state
1584 * @udev: USB device.
1585 * @ep: the endpoint to reset.
1586 *
1587 * Resets any host endpoint state such as the toggle bit, sequence
1588 * number and current window.
1589 */
1590void usb_hcd_reset_endpoint(struct usb_device *udev,
1591 struct usb_host_endpoint *ep)
1592{
1593 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1594
1595 if (hcd->driver->endpoint_reset)
1596 hcd->driver->endpoint_reset(hcd, ep);
1597 else {
1598 int epnum = usb_endpoint_num(&ep->desc);
1599 int is_out = usb_endpoint_dir_out(&ep->desc);
1600 int is_control = usb_endpoint_xfer_control(&ep->desc);
1601
1602 usb_settoggle(udev, epnum, is_out, 0);
1603 if (is_control)
1604 usb_settoggle(udev, epnum, !is_out, 0);
1605 }
1606}
1607
Alan Sterncde217a2008-10-21 15:28:46 -04001608/* Protect against drivers that try to unlink URBs after the device
1609 * is gone, by waiting until all unlinks for @udev are finished.
1610 * Since we don't currently track URBs by device, simply wait until
1611 * nothing is running in the locked region of usb_hcd_unlink_urb().
1612 */
1613void usb_hcd_synchronize_unlinks(struct usb_device *udev)
1614{
1615 spin_lock_irq(&hcd_urb_unlink_lock);
1616 spin_unlock_irq(&hcd_urb_unlink_lock);
1617}
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619/*-------------------------------------------------------------------------*/
1620
Alan Stern32aca562007-07-18 12:08:02 -04001621/* called in any context */
1622int usb_hcd_get_frame_number (struct usb_device *udev)
1623{
1624 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1625
1626 if (!HC_IS_RUNNING (hcd->state))
1627 return -ESHUTDOWN;
1628 return hcd->driver->get_frame_number (hcd);
1629}
1630
1631/*-------------------------------------------------------------------------*/
1632
David Brownell92936772005-09-22 22:32:11 -07001633#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Alan Stern65bfd292008-11-25 16:39:18 -05001635int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Alan Stern686314c2007-05-30 15:34:36 -04001637 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
1638 int status;
1639 int old_state = hcd->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Alan Stern686314c2007-05-30 15:34:36 -04001641 dev_dbg(&rhdev->dev, "bus %s%s\n",
Alan Stern65bfd292008-11-25 16:39:18 -05001642 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "suspend");
Alan Stern686314c2007-05-30 15:34:36 -04001643 if (!hcd->driver->bus_suspend) {
1644 status = -ENOENT;
1645 } else {
1646 hcd->state = HC_STATE_QUIESCING;
1647 status = hcd->driver->bus_suspend(hcd);
1648 }
1649 if (status == 0) {
1650 usb_set_device_state(rhdev, USB_STATE_SUSPENDED);
David Brownell92936772005-09-22 22:32:11 -07001651 hcd->state = HC_STATE_SUSPENDED;
Alan Stern686314c2007-05-30 15:34:36 -04001652 } else {
1653 hcd->state = old_state;
1654 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
David Brownell92936772005-09-22 22:32:11 -07001655 "suspend", status);
Alan Stern686314c2007-05-30 15:34:36 -04001656 }
David Brownell92936772005-09-22 22:32:11 -07001657 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Alan Stern65bfd292008-11-25 16:39:18 -05001660int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Alan Stern686314c2007-05-30 15:34:36 -04001662 struct usb_hcd *hcd = container_of(rhdev->bus, struct usb_hcd, self);
1663 int status;
Alan Sterncfa59da2007-06-21 16:25:35 -04001664 int old_state = hcd->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Alan Stern686314c2007-05-30 15:34:36 -04001666 dev_dbg(&rhdev->dev, "usb %s%s\n",
Alan Stern65bfd292008-11-25 16:39:18 -05001667 (msg.event & PM_EVENT_AUTO ? "auto-" : ""), "resume");
Alan Stern0c0382e2005-10-13 17:08:02 -04001668 if (!hcd->driver->bus_resume)
David Brownell92936772005-09-22 22:32:11 -07001669 return -ENOENT;
David Brownell979d5192005-09-22 22:32:24 -07001670 if (hcd->state == HC_STATE_RUNNING)
1671 return 0;
Alan Stern686314c2007-05-30 15:34:36 -04001672
David Brownell92936772005-09-22 22:32:11 -07001673 hcd->state = HC_STATE_RESUMING;
Alan Stern686314c2007-05-30 15:34:36 -04001674 status = hcd->driver->bus_resume(hcd);
1675 if (status == 0) {
1676 /* TRSMRCY = 10 msec */
1677 msleep(10);
1678 usb_set_device_state(rhdev, rhdev->actconfig
1679 ? USB_STATE_CONFIGURED
1680 : USB_STATE_ADDRESS);
David Brownell92936772005-09-22 22:32:11 -07001681 hcd->state = HC_STATE_RUNNING;
Alan Stern686314c2007-05-30 15:34:36 -04001682 } else {
Alan Sterncfa59da2007-06-21 16:25:35 -04001683 hcd->state = old_state;
Alan Stern686314c2007-05-30 15:34:36 -04001684 dev_dbg(&rhdev->dev, "bus %s fail, err %d\n",
David Brownell92936772005-09-22 22:32:11 -07001685 "resume", status);
Alan Sterncfa59da2007-06-21 16:25:35 -04001686 if (status != -ESHUTDOWN)
1687 usb_hc_died(hcd);
David Brownell92936772005-09-22 22:32:11 -07001688 }
1689 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
Alan Stern6b157c92007-03-13 16:37:30 -04001692/* Workqueue routine for root-hub remote wakeup */
1693static void hcd_resume_work(struct work_struct *work)
1694{
1695 struct usb_hcd *hcd = container_of(work, struct usb_hcd, wakeup_work);
1696 struct usb_device *udev = hcd->self.root_hub;
1697
1698 usb_lock_device(udev);
Alan Stern19410442007-03-27 13:33:59 -04001699 usb_mark_last_busy(udev);
Alan Stern65bfd292008-11-25 16:39:18 -05001700 usb_external_resume_device(udev, PMSG_REMOTE_RESUME);
Alan Stern6b157c92007-03-13 16:37:30 -04001701 usb_unlock_device(udev);
1702}
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704/**
1705 * usb_hcd_resume_root_hub - called by HCD to resume its root hub
1706 * @hcd: host controller for this root hub
1707 *
1708 * The USB host controller calls this function when its root hub is
1709 * suspended (with the remote wakeup feature enabled) and a remote
Alan Stern6b157c92007-03-13 16:37:30 -04001710 * wakeup request is received. The routine submits a workqueue request
1711 * to resume the root hub (that is, manage its downstream ports again).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 */
1713void usb_hcd_resume_root_hub (struct usb_hcd *hcd)
1714{
1715 unsigned long flags;
1716
1717 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1718 if (hcd->rh_registered)
Alan Stern6b157c92007-03-13 16:37:30 -04001719 queue_work(ksuspend_usb_wq, &hcd->wakeup_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
1721}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub);
1723
David Brownell92936772005-09-22 22:32:11 -07001724#endif
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726/*-------------------------------------------------------------------------*/
1727
1728#ifdef CONFIG_USB_OTG
1729
1730/**
1731 * usb_bus_start_enum - start immediate enumeration (for OTG)
1732 * @bus: the bus (must use hcd framework)
1733 * @port_num: 1-based number of port; usually bus->otg_port
1734 * Context: in_interrupt()
1735 *
1736 * Starts enumeration, with an immediate reset followed later by
1737 * khubd identifying and possibly configuring the device.
1738 * This is needed by OTG controller drivers, where it helps meet
1739 * HNP protocol timing requirements for starting a port reset.
1740 */
1741int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
1742{
1743 struct usb_hcd *hcd;
1744 int status = -EOPNOTSUPP;
1745
1746 /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
1747 * boards with root hubs hooked up to internal devices (instead of
1748 * just the OTG port) may need more attention to resetting...
1749 */
1750 hcd = container_of (bus, struct usb_hcd, self);
1751 if (port_num && hcd->driver->start_port_reset)
1752 status = hcd->driver->start_port_reset(hcd, port_num);
1753
1754 /* run khubd shortly after (first) root port reset finishes;
1755 * it may issue others, until at least 50 msecs have passed.
1756 */
1757 if (status == 0)
1758 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
1759 return status;
1760}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001761EXPORT_SYMBOL_GPL(usb_bus_start_enum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763#endif
1764
1765/*-------------------------------------------------------------------------*/
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
1769 * @irq: the IRQ being raised
1770 * @__hcd: pointer to the HCD whose IRQ is being signaled
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 *
1772 * If the controller isn't HALTed, calls the driver's irq handler.
1773 * Checks whether the controller is now dead.
1774 */
David Howells7d12e782006-10-05 14:55:46 +01001775irqreturn_t usb_hcd_irq (int irq, void *__hcd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct usb_hcd *hcd = __hcd;
Stefan Beckerde854222008-07-01 19:19:22 +03001778 unsigned long flags;
1779 irqreturn_t rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Stefan Beckerde854222008-07-01 19:19:22 +03001781 /* IRQF_DISABLED doesn't work correctly with shared IRQs
1782 * when the first handler doesn't use it. So let's just
1783 * assume it's never used.
1784 */
1785 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Stefan Beckerde854222008-07-01 19:19:22 +03001787 if (unlikely(hcd->state == HC_STATE_HALT ||
1788 !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
1789 rc = IRQ_NONE;
1790 } else if (hcd->driver->irq(hcd) == IRQ_NONE) {
1791 rc = IRQ_NONE;
1792 } else {
1793 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001794
Stefan Beckerde854222008-07-01 19:19:22 +03001795 if (unlikely(hcd->state == HC_STATE_HALT))
1796 usb_hc_died(hcd);
1797 rc = IRQ_HANDLED;
1798 }
1799
1800 local_irq_restore(flags);
1801 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
1804/*-------------------------------------------------------------------------*/
1805
1806/**
1807 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
1808 * @hcd: pointer to the HCD representing the controller
1809 *
1810 * This is called by bus glue to report a USB host controller that died
1811 * while operations may still have been pending. It's called automatically
1812 * by the PCI glue, so only glue for non-PCI busses should need to call it.
1813 */
1814void usb_hc_died (struct usb_hcd *hcd)
1815{
1816 unsigned long flags;
1817
1818 dev_err (hcd->self.controller, "HC died; cleaning up\n");
1819
1820 spin_lock_irqsave (&hcd_root_hub_lock, flags);
1821 if (hcd->rh_registered) {
Alan Sternd5926ae72005-04-21 15:56:37 -04001822 hcd->poll_rh = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 /* make khubd clean up old urbs and devices */
1825 usb_set_device_state (hcd->self.root_hub,
1826 USB_STATE_NOTATTACHED);
1827 usb_kick_khubd (hcd->self.root_hub);
1828 }
1829 spin_unlock_irqrestore (&hcd_root_hub_lock, flags);
1830}
1831EXPORT_SYMBOL_GPL (usb_hc_died);
1832
1833/*-------------------------------------------------------------------------*/
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835/**
1836 * usb_create_hcd - create and initialize an HCD structure
1837 * @driver: HC driver that will use this hcd
1838 * @dev: device for this HC, stored in hcd->self.controller
1839 * @bus_name: value to store in hcd->self.bus_name
1840 * Context: !in_interrupt()
1841 *
1842 * Allocate a struct usb_hcd, with extra space at the end for the
1843 * HC driver's private data. Initialize the generic members of the
1844 * hcd structure.
1845 *
1846 * If memory is unavailable, returns NULL.
1847 */
1848struct usb_hcd *usb_create_hcd (const struct hc_driver *driver,
Greg Kroah-Hartman1b26da12008-07-02 12:46:22 -07001849 struct device *dev, const char *bus_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 struct usb_hcd *hcd;
1852
Pekka Enberg7b842b62005-09-06 15:18:34 -07001853 hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (!hcd) {
1855 dev_dbg (dev, "hcd alloc failed\n");
1856 return NULL;
1857 }
1858 dev_set_drvdata(dev, hcd);
Alan Stern17200582006-08-30 11:32:52 -04001859 kref_init(&hcd->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 usb_bus_init(&hcd->self);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 hcd->self.controller = dev;
1863 hcd->self.bus_name = bus_name;
Alan Sterndd990f12006-08-30 11:29:56 -04001864 hcd->self.uses_dma = (dev->dma_mask != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 init_timer(&hcd->rh_timer);
Alan Sternd5926ae72005-04-21 15:56:37 -04001867 hcd->rh_timer.function = rh_timer_func;
1868 hcd->rh_timer.data = (unsigned long) hcd;
Alan Stern6b157c92007-03-13 16:37:30 -04001869#ifdef CONFIG_PM
1870 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
1871#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 hcd->driver = driver;
1874 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
1875 "USB Host Controller";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 return hcd;
1877}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001878EXPORT_SYMBOL_GPL(usb_create_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Alan Stern17200582006-08-30 11:32:52 -04001880static void hcd_release (struct kref *kref)
1881{
1882 struct usb_hcd *hcd = container_of (kref, struct usb_hcd, kref);
1883
1884 kfree(hcd);
1885}
1886
1887struct usb_hcd *usb_get_hcd (struct usb_hcd *hcd)
1888{
1889 if (hcd)
1890 kref_get (&hcd->kref);
1891 return hcd;
1892}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001893EXPORT_SYMBOL_GPL(usb_get_hcd);
Alan Stern17200582006-08-30 11:32:52 -04001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895void usb_put_hcd (struct usb_hcd *hcd)
1896{
Alan Stern17200582006-08-30 11:32:52 -04001897 if (hcd)
1898 kref_put (&hcd->kref, hcd_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001900EXPORT_SYMBOL_GPL(usb_put_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902/**
1903 * usb_add_hcd - finish generic HCD structure initialization and register
1904 * @hcd: the usb_hcd structure to initialize
1905 * @irqnum: Interrupt line to allocate
1906 * @irqflags: Interrupt type flags
1907 *
1908 * Finish the remaining parts of generic HCD initialization: allocate the
1909 * buffers of consistent memory, register the bus, request the IRQ line,
1910 * and call the driver's reset() and start() routines.
1911 */
1912int usb_add_hcd(struct usb_hcd *hcd,
1913 unsigned int irqnum, unsigned long irqflags)
1914{
Alan Stern8ec8d202005-04-25 11:25:17 -04001915 int retval;
1916 struct usb_device *rhdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
1919
Inaky Perez-Gonzalez5234ce12007-07-31 20:33:58 -07001920 hcd->authorized_default = hcd->wireless? 0 : 1;
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +11001921 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1922
David Brownellb1e8f0a2006-01-23 15:25:40 -08001923 /* HC is in reset state, but accessible. Now do the one-time init,
1924 * bottom up so that hcds can customize the root hubs before khubd
1925 * starts talking to them. (Note, bus id is assigned early too.)
1926 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if ((retval = hcd_buffer_create(hcd)) != 0) {
1928 dev_dbg(hcd->self.controller, "pool alloc failed\n");
1929 return retval;
1930 }
1931
1932 if ((retval = usb_register_bus(&hcd->self)) < 0)
Alan Stern8ec8d202005-04-25 11:25:17 -04001933 goto err_register_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
David Brownellb1e8f0a2006-01-23 15:25:40 -08001935 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
1936 dev_err(hcd->self.controller, "unable to allocate root hub\n");
1937 retval = -ENOMEM;
1938 goto err_allocate_root_hub;
1939 }
Sarah Sharp6b403b02009-04-27 19:54:10 -07001940
1941 switch (hcd->driver->flags & HCD_MASK) {
1942 case HCD_USB11:
1943 rhdev->speed = USB_SPEED_FULL;
1944 break;
1945 case HCD_USB2:
1946 rhdev->speed = USB_SPEED_HIGH;
1947 break;
1948 case HCD_USB3:
1949 rhdev->speed = USB_SPEED_SUPER;
1950 break;
1951 default:
1952 goto err_allocate_root_hub;
1953 }
David Brownellb1e8f0a2006-01-23 15:25:40 -08001954 hcd->self.root_hub = rhdev;
1955
David Brownelldb4cefa2006-05-01 22:07:13 -07001956 /* wakeup flag init defaults to "everything works" for root hubs,
1957 * but drivers can override it in reset() if needed, along with
1958 * recording the overall controller's system wakeup capability.
1959 */
1960 device_init_wakeup(&rhdev->dev, 1);
1961
David Brownellb1e8f0a2006-01-23 15:25:40 -08001962 /* "reset" is misnamed; its role is now one-time init. the controller
1963 * should already have been reset (and boot firmware kicked off etc).
1964 */
1965 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
1966 dev_err(hcd->self.controller, "can't setup\n");
1967 goto err_hcd_driver_setup;
1968 }
1969
David Brownellfb669cc2006-01-24 08:40:27 -08001970 /* NOTE: root hub and controller capabilities may not be the same */
1971 if (device_can_wakeup(hcd->self.controller)
1972 && device_can_wakeup(&hcd->self.root_hub->dev))
David Brownellb1e8f0a2006-01-23 15:25:40 -08001973 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
David Brownellb1e8f0a2006-01-23 15:25:40 -08001974
1975 /* enable irqs just before we start the controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (hcd->driver->irq) {
Stefan Beckerde854222008-07-01 19:19:22 +03001977
1978 /* IRQF_DISABLED doesn't work as advertised when used together
1979 * with IRQF_SHARED. As usb_hcd_irq() will always disable
1980 * interrupts we can remove it here.
1981 */
Geoff Levand83a79822008-08-22 14:13:00 -07001982 if (irqflags & IRQF_SHARED)
1983 irqflags &= ~IRQF_DISABLED;
Stefan Beckerde854222008-07-01 19:19:22 +03001984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
1986 hcd->driver->description, hcd->self.busnum);
1987 if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
1988 hcd->irq_descr, hcd)) != 0) {
1989 dev_err(hcd->self.controller,
David S. Millerc6387a42006-06-20 01:21:29 -07001990 "request interrupt %d failed\n", irqnum);
Alan Stern8ec8d202005-04-25 11:25:17 -04001991 goto err_request_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993 hcd->irq = irqnum;
David S. Millerc6387a42006-06-20 01:21:29 -07001994 dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 (hcd->driver->flags & HCD_MEMORY) ?
1996 "io mem" : "io base",
1997 (unsigned long long)hcd->rsrc_start);
1998 } else {
1999 hcd->irq = -1;
2000 if (hcd->rsrc_start)
2001 dev_info(hcd->self.controller, "%s 0x%08llx\n",
2002 (hcd->driver->flags & HCD_MEMORY) ?
2003 "io mem" : "io base",
2004 (unsigned long long)hcd->rsrc_start);
2005 }
2006
2007 if ((retval = hcd->driver->start(hcd)) < 0) {
2008 dev_err(hcd->self.controller, "startup error %d\n", retval);
Alan Stern8ec8d202005-04-25 11:25:17 -04002009 goto err_hcd_driver_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011
David Brownellb1e8f0a2006-01-23 15:25:40 -08002012 /* starting here, usbcore will pay attention to this root hub */
Alan Stern55c52712005-11-23 12:03:12 -05002013 rhdev->bus_mA = min(500u, hcd->power_budget);
David Brownellb1e8f0a2006-01-23 15:25:40 -08002014 if ((retval = register_root_hub(hcd)) != 0)
Alan Stern8ec8d202005-04-25 11:25:17 -04002015 goto err_register_root_hub;
2016
Inaky Perez-Gonzalez5234ce12007-07-31 20:33:58 -07002017 retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
2018 if (retval < 0) {
2019 printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
2020 retval);
2021 goto error_create_attr_group;
2022 }
Alan Sternd5926ae72005-04-21 15:56:37 -04002023 if (hcd->uses_new_polling && hcd->poll_rh)
2024 usb_hcd_poll_rh_status(hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return retval;
2026
Inaky Perez-Gonzalez5234ce12007-07-31 20:33:58 -07002027error_create_attr_group:
2028 mutex_lock(&usb_bus_list_lock);
2029 usb_disconnect(&hcd->self.root_hub);
2030 mutex_unlock(&usb_bus_list_lock);
David Brownellb1e8f0a2006-01-23 15:25:40 -08002031err_register_root_hub:
Alan Stern8ec8d202005-04-25 11:25:17 -04002032 hcd->driver->stop(hcd);
David Brownellb1e8f0a2006-01-23 15:25:40 -08002033err_hcd_driver_start:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (hcd->irq >= 0)
2035 free_irq(irqnum, hcd);
David Brownellb1e8f0a2006-01-23 15:25:40 -08002036err_request_irq:
2037err_hcd_driver_setup:
2038 hcd->self.root_hub = NULL;
2039 usb_put_dev(rhdev);
2040err_allocate_root_hub:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 usb_deregister_bus(&hcd->self);
David Brownellb1e8f0a2006-01-23 15:25:40 -08002042err_register_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 hcd_buffer_destroy(hcd);
2044 return retval;
2045}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06002046EXPORT_SYMBOL_GPL(usb_add_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048/**
2049 * usb_remove_hcd - shutdown processing for generic HCDs
2050 * @hcd: the usb_hcd structure to remove
2051 * Context: !in_interrupt()
2052 *
2053 * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
2054 * invoking the HCD's stop() method.
2055 */
2056void usb_remove_hcd(struct usb_hcd *hcd)
2057{
2058 dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
2059
2060 if (HC_IS_RUNNING (hcd->state))
2061 hcd->state = HC_STATE_QUIESCING;
2062
2063 dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
2064 spin_lock_irq (&hcd_root_hub_lock);
2065 hcd->rh_registered = 0;
2066 spin_unlock_irq (&hcd_root_hub_lock);
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002067
Alan Stern6b157c92007-03-13 16:37:30 -04002068#ifdef CONFIG_PM
Alan Sternd5d4db72007-05-29 16:34:52 -04002069 cancel_work_sync(&hcd->wakeup_work);
Alan Stern6b157c92007-03-13 16:37:30 -04002070#endif
2071
Inaky Perez-Gonzalez5234ce12007-07-31 20:33:58 -07002072 sysfs_remove_group(&hcd->self.root_hub->dev.kobj, &usb_bus_attr_group);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002073 mutex_lock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 usb_disconnect(&hcd->self.root_hub);
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002075 mutex_unlock(&usb_bus_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 hcd->driver->stop(hcd);
2078 hcd->state = HC_STATE_HALT;
2079
Alan Stern1b42ae62007-03-13 11:10:52 -04002080 hcd->poll_rh = 0;
2081 del_timer_sync(&hcd->rh_timer);
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (hcd->irq >= 0)
2084 free_irq(hcd->irq, hcd);
2085 usb_deregister_bus(&hcd->self);
2086 hcd_buffer_destroy(hcd);
2087}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06002088EXPORT_SYMBOL_GPL(usb_remove_hcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Aleksey Gorelov64a21d02006-08-08 17:24:08 -07002090void
2091usb_hcd_platform_shutdown(struct platform_device* dev)
2092{
2093 struct usb_hcd *hcd = platform_get_drvdata(dev);
2094
2095 if (hcd->driver->shutdown)
2096 hcd->driver->shutdown(hcd);
2097}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06002098EXPORT_SYMBOL_GPL(usb_hcd_platform_shutdown);
Aleksey Gorelov64a21d02006-08-08 17:24:08 -07002099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100/*-------------------------------------------------------------------------*/
2101
Pete Zaitcevf150fa12008-11-13 21:31:21 -07002102#if defined(CONFIG_USB_MON) || defined(CONFIG_USB_MON_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104struct usb_mon_operations *mon_ops;
2105
2106/*
2107 * The registration is unlocked.
2108 * We do it this way because we do not want to lock in hot paths.
2109 *
2110 * Notice that the code is minimally error-proof. Because usbmon needs
2111 * symbols from usbcore, usbcore gets referenced and cannot be unloaded first.
2112 */
2113
2114int usb_mon_register (struct usb_mon_operations *ops)
2115{
2116
2117 if (mon_ops)
2118 return -EBUSY;
2119
2120 mon_ops = ops;
2121 mb();
2122 return 0;
2123}
2124EXPORT_SYMBOL_GPL (usb_mon_register);
2125
2126void usb_mon_deregister (void)
2127{
2128
2129 if (mon_ops == NULL) {
2130 printk(KERN_ERR "USB: monitor was not registered\n");
2131 return;
2132 }
2133 mon_ops = NULL;
2134 mb();
2135}
2136EXPORT_SYMBOL_GPL (usb_mon_deregister);
2137
Pete Zaitcevf150fa12008-11-13 21:31:21 -07002138#endif /* CONFIG_USB_MON || CONFIG_USB_MON_MODULE */