[PATCH] fix spinlock-debugging smp_processor_id() usage
[linux-2.6/verdex.git] / drivers / usb / core / usb.c
blobe197ce9353de0f4591ad38861413b7ceb1b66593
1 /*
2 * drivers/usb/usb.c
4 * (C) Copyright Linus Torvalds 1999
5 * (C) Copyright Johannes Erdfelt 1999-2001
6 * (C) Copyright Andreas Gal 1999
7 * (C) Copyright Gregory P. Smith 1999
8 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
9 * (C) Copyright Randy Dunlap 2000
10 * (C) Copyright David Brownell 2000-2004
11 * (C) Copyright Yggdrasil Computing, Inc. 2000
12 * (usb_device_id matching changes by Adam J. Richter)
13 * (C) Copyright Greg Kroah-Hartman 2002-2003
15 * NOTE! This is not actually a driver at all, rather this is
16 * just a collection of helper routines that implement the
17 * generic USB things that the real drivers can use..
19 * Think of this as a "USB library" rather than anything else.
20 * It should be considered a slave, with no callbacks. Callbacks
21 * are evil.
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/string.h>
27 #include <linux/bitops.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h> /* for in_interrupt() */
30 #include <linux/kmod.h>
31 #include <linux/init.h>
32 #include <linux/spinlock.h>
33 #include <linux/errno.h>
34 #include <linux/smp_lock.h>
35 #include <linux/rwsem.h>
36 #include <linux/usb.h>
38 #include <asm/io.h>
39 #include <asm/scatterlist.h>
40 #include <linux/mm.h>
41 #include <linux/dma-mapping.h>
43 #include "hcd.h"
44 #include "usb.h"
47 const char *usbcore_name = "usbcore";
49 static int nousb; /* Disable USB when built into kernel image */
50 /* Not honored on modular build */
52 static DECLARE_RWSEM(usb_all_devices_rwsem);
55 static int generic_probe (struct device *dev)
57 return 0;
59 static int generic_remove (struct device *dev)
61 struct usb_device *udev = to_usb_device(dev);
63 /* if this is only an unbind, not a physical disconnect, then
64 * unconfigure the device */
65 if (udev->state == USB_STATE_CONFIGURED)
66 usb_set_configuration(udev, 0);
68 /* in case the call failed or the device was suspended */
69 if (udev->state >= USB_STATE_CONFIGURED)
70 usb_disable_device(udev, 0);
71 return 0;
74 static struct device_driver usb_generic_driver = {
75 .owner = THIS_MODULE,
76 .name = "usb",
77 .bus = &usb_bus_type,
78 .probe = generic_probe,
79 .remove = generic_remove,
82 static int usb_generic_driver_data;
84 /* called from driver core with usb_bus_type.subsys writelock */
85 static int usb_probe_interface(struct device *dev)
87 struct usb_interface * intf = to_usb_interface(dev);
88 struct usb_driver * driver = to_usb_driver(dev->driver);
89 const struct usb_device_id *id;
90 int error = -ENODEV;
92 dev_dbg(dev, "%s\n", __FUNCTION__);
94 if (!driver->probe)
95 return error;
96 /* FIXME we'd much prefer to just resume it ... */
97 if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED)
98 return -EHOSTUNREACH;
100 id = usb_match_id (intf, driver->id_table);
101 if (id) {
102 dev_dbg (dev, "%s - got id\n", __FUNCTION__);
104 /* Interface "power state" doesn't correspond to any hardware
105 * state whatsoever. We use it to record when it's bound to
106 * a driver that may start I/0: it's not frozen/quiesced.
108 mark_active(intf);
109 intf->condition = USB_INTERFACE_BINDING;
110 error = driver->probe (intf, id);
111 if (error) {
112 mark_quiesced(intf);
113 intf->condition = USB_INTERFACE_UNBOUND;
114 } else
115 intf->condition = USB_INTERFACE_BOUND;
118 return error;
121 /* called from driver core with usb_bus_type.subsys writelock */
122 static int usb_unbind_interface(struct device *dev)
124 struct usb_interface *intf = to_usb_interface(dev);
125 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
127 intf->condition = USB_INTERFACE_UNBINDING;
129 /* release all urbs for this interface */
130 usb_disable_interface(interface_to_usbdev(intf), intf);
132 if (driver && driver->disconnect)
133 driver->disconnect(intf);
135 /* reset other interface state */
136 usb_set_interface(interface_to_usbdev(intf),
137 intf->altsetting[0].desc.bInterfaceNumber,
139 usb_set_intfdata(intf, NULL);
140 intf->condition = USB_INTERFACE_UNBOUND;
141 mark_quiesced(intf);
143 return 0;
147 * usb_register - register a USB driver
148 * @new_driver: USB operations for the driver
150 * Registers a USB driver with the USB core. The list of unattached
151 * interfaces will be rescanned whenever a new driver is added, allowing
152 * the new driver to attach to any recognized devices.
153 * Returns a negative error code on failure and 0 on success.
155 * NOTE: if you want your driver to use the USB major number, you must call
156 * usb_register_dev() to enable that functionality. This function no longer
157 * takes care of that.
159 int usb_register(struct usb_driver *new_driver)
161 int retval = 0;
163 if (nousb)
164 return -ENODEV;
166 new_driver->driver.name = (char *)new_driver->name;
167 new_driver->driver.bus = &usb_bus_type;
168 new_driver->driver.probe = usb_probe_interface;
169 new_driver->driver.remove = usb_unbind_interface;
170 new_driver->driver.owner = new_driver->owner;
172 usb_lock_all_devices();
173 retval = driver_register(&new_driver->driver);
174 usb_unlock_all_devices();
176 if (!retval) {
177 pr_info("%s: registered new driver %s\n",
178 usbcore_name, new_driver->name);
179 usbfs_update_special();
180 } else {
181 printk(KERN_ERR "%s: error %d registering driver %s\n",
182 usbcore_name, retval, new_driver->name);
185 return retval;
189 * usb_deregister - unregister a USB driver
190 * @driver: USB operations of the driver to unregister
191 * Context: must be able to sleep
193 * Unlinks the specified driver from the internal USB driver list.
195 * NOTE: If you called usb_register_dev(), you still need to call
196 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
197 * this * call will no longer do it for you.
199 void usb_deregister(struct usb_driver *driver)
201 pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
203 usb_lock_all_devices();
204 driver_unregister (&driver->driver);
205 usb_unlock_all_devices();
207 usbfs_update_special();
211 * usb_ifnum_to_if - get the interface object with a given interface number
212 * @dev: the device whose current configuration is considered
213 * @ifnum: the desired interface
215 * This walks the device descriptor for the currently active configuration
216 * and returns a pointer to the interface with that particular interface
217 * number, or null.
219 * Note that configuration descriptors are not required to assign interface
220 * numbers sequentially, so that it would be incorrect to assume that
221 * the first interface in that descriptor corresponds to interface zero.
222 * This routine helps device drivers avoid such mistakes.
223 * However, you should make sure that you do the right thing with any
224 * alternate settings available for this interfaces.
226 * Don't call this function unless you are bound to one of the interfaces
227 * on this device or you have locked the device!
229 struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum)
231 struct usb_host_config *config = dev->actconfig;
232 int i;
234 if (!config)
235 return NULL;
236 for (i = 0; i < config->desc.bNumInterfaces; i++)
237 if (config->interface[i]->altsetting[0]
238 .desc.bInterfaceNumber == ifnum)
239 return config->interface[i];
241 return NULL;
245 * usb_altnum_to_altsetting - get the altsetting structure with a given
246 * alternate setting number.
247 * @intf: the interface containing the altsetting in question
248 * @altnum: the desired alternate setting number
250 * This searches the altsetting array of the specified interface for
251 * an entry with the correct bAlternateSetting value and returns a pointer
252 * to that entry, or null.
254 * Note that altsettings need not be stored sequentially by number, so
255 * it would be incorrect to assume that the first altsetting entry in
256 * the array corresponds to altsetting zero. This routine helps device
257 * drivers avoid such mistakes.
259 * Don't call this function unless you are bound to the intf interface
260 * or you have locked the device!
262 struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf,
263 unsigned int altnum)
265 int i;
267 for (i = 0; i < intf->num_altsetting; i++) {
268 if (intf->altsetting[i].desc.bAlternateSetting == altnum)
269 return &intf->altsetting[i];
271 return NULL;
275 * usb_driver_claim_interface - bind a driver to an interface
276 * @driver: the driver to be bound
277 * @iface: the interface to which it will be bound; must be in the
278 * usb device's active configuration
279 * @priv: driver data associated with that interface
281 * This is used by usb device drivers that need to claim more than one
282 * interface on a device when probing (audio and acm are current examples).
283 * No device driver should directly modify internal usb_interface or
284 * usb_device structure members.
286 * Few drivers should need to use this routine, since the most natural
287 * way to bind to an interface is to return the private data from
288 * the driver's probe() method.
290 * Callers must own the device lock and the driver model's usb_bus_type.subsys
291 * writelock. So driver probe() entries don't need extra locking,
292 * but other call contexts may need to explicitly claim those locks.
294 int usb_driver_claim_interface(struct usb_driver *driver,
295 struct usb_interface *iface, void* priv)
297 struct device *dev = &iface->dev;
299 if (dev->driver)
300 return -EBUSY;
302 dev->driver = &driver->driver;
303 usb_set_intfdata(iface, priv);
304 iface->condition = USB_INTERFACE_BOUND;
305 mark_active(iface);
307 /* if interface was already added, bind now; else let
308 * the future device_add() bind it, bypassing probe()
310 if (device_is_registered(dev))
311 device_bind_driver(dev);
313 return 0;
317 * usb_driver_release_interface - unbind a driver from an interface
318 * @driver: the driver to be unbound
319 * @iface: the interface from which it will be unbound
321 * This can be used by drivers to release an interface without waiting
322 * for their disconnect() methods to be called. In typical cases this
323 * also causes the driver disconnect() method to be called.
325 * This call is synchronous, and may not be used in an interrupt context.
326 * Callers must own the device lock and the driver model's usb_bus_type.subsys
327 * writelock. So driver disconnect() entries don't need extra locking,
328 * but other call contexts may need to explicitly claim those locks.
330 void usb_driver_release_interface(struct usb_driver *driver,
331 struct usb_interface *iface)
333 struct device *dev = &iface->dev;
335 /* this should never happen, don't release something that's not ours */
336 if (!dev->driver || dev->driver != &driver->driver)
337 return;
339 /* don't release from within disconnect() */
340 if (iface->condition != USB_INTERFACE_BOUND)
341 return;
343 /* don't release if the interface hasn't been added yet */
344 if (device_is_registered(dev)) {
345 iface->condition = USB_INTERFACE_UNBINDING;
346 device_release_driver(dev);
349 dev->driver = NULL;
350 usb_set_intfdata(iface, NULL);
351 iface->condition = USB_INTERFACE_UNBOUND;
352 mark_quiesced(iface);
356 * usb_match_id - find first usb_device_id matching device or interface
357 * @interface: the interface of interest
358 * @id: array of usb_device_id structures, terminated by zero entry
360 * usb_match_id searches an array of usb_device_id's and returns
361 * the first one matching the device or interface, or null.
362 * This is used when binding (or rebinding) a driver to an interface.
363 * Most USB device drivers will use this indirectly, through the usb core,
364 * but some layered driver frameworks use it directly.
365 * These device tables are exported with MODULE_DEVICE_TABLE, through
366 * modutils and "modules.usbmap", to support the driver loading
367 * functionality of USB hotplugging.
369 * What Matches:
371 * The "match_flags" element in a usb_device_id controls which
372 * members are used. If the corresponding bit is set, the
373 * value in the device_id must match its corresponding member
374 * in the device or interface descriptor, or else the device_id
375 * does not match.
377 * "driver_info" is normally used only by device drivers,
378 * but you can create a wildcard "matches anything" usb_device_id
379 * as a driver's "modules.usbmap" entry if you provide an id with
380 * only a nonzero "driver_info" field. If you do this, the USB device
381 * driver's probe() routine should use additional intelligence to
382 * decide whether to bind to the specified interface.
384 * What Makes Good usb_device_id Tables:
386 * The match algorithm is very simple, so that intelligence in
387 * driver selection must come from smart driver id records.
388 * Unless you have good reasons to use another selection policy,
389 * provide match elements only in related groups, and order match
390 * specifiers from specific to general. Use the macros provided
391 * for that purpose if you can.
393 * The most specific match specifiers use device descriptor
394 * data. These are commonly used with product-specific matches;
395 * the USB_DEVICE macro lets you provide vendor and product IDs,
396 * and you can also match against ranges of product revisions.
397 * These are widely used for devices with application or vendor
398 * specific bDeviceClass values.
400 * Matches based on device class/subclass/protocol specifications
401 * are slightly more general; use the USB_DEVICE_INFO macro, or
402 * its siblings. These are used with single-function devices
403 * where bDeviceClass doesn't specify that each interface has
404 * its own class.
406 * Matches based on interface class/subclass/protocol are the
407 * most general; they let drivers bind to any interface on a
408 * multiple-function device. Use the USB_INTERFACE_INFO
409 * macro, or its siblings, to match class-per-interface style
410 * devices (as recorded in bDeviceClass).
412 * Within those groups, remember that not all combinations are
413 * meaningful. For example, don't give a product version range
414 * without vendor and product IDs; or specify a protocol without
415 * its associated class and subclass.
417 const struct usb_device_id *
418 usb_match_id(struct usb_interface *interface, const struct usb_device_id *id)
420 struct usb_host_interface *intf;
421 struct usb_device *dev;
423 /* proc_connectinfo in devio.c may call us with id == NULL. */
424 if (id == NULL)
425 return NULL;
427 intf = interface->cur_altsetting;
428 dev = interface_to_usbdev(interface);
430 /* It is important to check that id->driver_info is nonzero,
431 since an entry that is all zeroes except for a nonzero
432 id->driver_info is the way to create an entry that
433 indicates that the driver want to examine every
434 device and interface. */
435 for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass ||
436 id->driver_info; id++) {
438 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
439 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
440 continue;
442 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
443 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
444 continue;
446 /* No need to test id->bcdDevice_lo != 0, since 0 is never
447 greater than any unsigned number. */
448 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
449 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
450 continue;
452 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
453 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
454 continue;
456 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
457 (id->bDeviceClass != dev->descriptor.bDeviceClass))
458 continue;
460 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
461 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
462 continue;
464 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
465 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
466 continue;
468 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
469 (id->bInterfaceClass != intf->desc.bInterfaceClass))
470 continue;
472 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
473 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
474 continue;
476 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
477 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
478 continue;
480 return id;
483 return NULL;
487 static int __find_interface(struct device * dev, void * data)
489 struct usb_interface ** ret = (struct usb_interface **)data;
490 struct usb_interface * intf = *ret;
491 int *minor = (int *)data;
493 /* can't look at usb devices, only interfaces */
494 if (dev->driver == &usb_generic_driver)
495 return 0;
497 intf = to_usb_interface(dev);
498 if (intf->minor != -1 && intf->minor == *minor) {
499 *ret = intf;
500 return 1;
502 return 0;
506 * usb_find_interface - find usb_interface pointer for driver and device
507 * @drv: the driver whose current configuration is considered
508 * @minor: the minor number of the desired device
510 * This walks the driver device list and returns a pointer to the interface
511 * with the matching minor. Note, this only works for devices that share the
512 * USB major number.
514 struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor)
516 struct usb_interface *intf = (struct usb_interface *)(long)minor;
517 int ret;
519 ret = driver_for_each_device(&drv->driver, NULL, &intf, __find_interface);
521 return ret ? intf : NULL;
524 static int usb_device_match (struct device *dev, struct device_driver *drv)
526 struct usb_interface *intf;
527 struct usb_driver *usb_drv;
528 const struct usb_device_id *id;
530 /* check for generic driver, which we don't match any device with */
531 if (drv == &usb_generic_driver)
532 return 0;
534 intf = to_usb_interface(dev);
535 usb_drv = to_usb_driver(drv);
537 id = usb_match_id (intf, usb_drv->id_table);
538 if (id)
539 return 1;
541 return 0;
545 #ifdef CONFIG_HOTPLUG
548 * USB hotplugging invokes what /proc/sys/kernel/hotplug says
549 * (normally /sbin/hotplug) when USB devices get added or removed.
551 * This invokes a user mode policy agent, typically helping to load driver
552 * or other modules, configure the device, and more. Drivers can provide
553 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
555 * We're called either from khubd (the typical case) or from root hub
556 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
557 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
558 * device (and this configuration!) are still present.
560 static int usb_hotplug (struct device *dev, char **envp, int num_envp,
561 char *buffer, int buffer_size)
563 struct usb_interface *intf;
564 struct usb_device *usb_dev;
565 struct usb_host_interface *alt;
566 int i = 0;
567 int length = 0;
569 if (!dev)
570 return -ENODEV;
572 /* driver is often null here; dev_dbg() would oops */
573 pr_debug ("usb %s: hotplug\n", dev->bus_id);
575 /* Must check driver_data here, as on remove driver is always NULL */
576 if ((dev->driver == &usb_generic_driver) ||
577 (dev->driver_data == &usb_generic_driver_data))
578 return 0;
580 intf = to_usb_interface(dev);
581 usb_dev = interface_to_usbdev (intf);
582 alt = intf->cur_altsetting;
584 if (usb_dev->devnum < 0) {
585 pr_debug ("usb %s: already deleted?\n", dev->bus_id);
586 return -ENODEV;
588 if (!usb_dev->bus) {
589 pr_debug ("usb %s: bus removed?\n", dev->bus_id);
590 return -ENODEV;
593 #ifdef CONFIG_USB_DEVICEFS
594 /* If this is available, userspace programs can directly read
595 * all the device descriptors we don't tell them about. Or
596 * even act as usermode drivers.
598 * FIXME reduce hardwired intelligence here
600 if (add_hotplug_env_var(envp, num_envp, &i,
601 buffer, buffer_size, &length,
602 "DEVICE=/proc/bus/usb/%03d/%03d",
603 usb_dev->bus->busnum, usb_dev->devnum))
604 return -ENOMEM;
605 #endif
607 /* per-device configurations are common */
608 if (add_hotplug_env_var(envp, num_envp, &i,
609 buffer, buffer_size, &length,
610 "PRODUCT=%x/%x/%x",
611 le16_to_cpu(usb_dev->descriptor.idVendor),
612 le16_to_cpu(usb_dev->descriptor.idProduct),
613 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
614 return -ENOMEM;
616 /* class-based driver binding models */
617 if (add_hotplug_env_var(envp, num_envp, &i,
618 buffer, buffer_size, &length,
619 "TYPE=%d/%d/%d",
620 usb_dev->descriptor.bDeviceClass,
621 usb_dev->descriptor.bDeviceSubClass,
622 usb_dev->descriptor.bDeviceProtocol))
623 return -ENOMEM;
625 if (add_hotplug_env_var(envp, num_envp, &i,
626 buffer, buffer_size, &length,
627 "INTERFACE=%d/%d/%d",
628 alt->desc.bInterfaceClass,
629 alt->desc.bInterfaceSubClass,
630 alt->desc.bInterfaceProtocol))
631 return -ENOMEM;
633 if (add_hotplug_env_var(envp, num_envp, &i,
634 buffer, buffer_size, &length,
635 "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",
636 le16_to_cpu(usb_dev->descriptor.idVendor),
637 le16_to_cpu(usb_dev->descriptor.idProduct),
638 le16_to_cpu(usb_dev->descriptor.bcdDevice),
639 usb_dev->descriptor.bDeviceClass,
640 usb_dev->descriptor.bDeviceSubClass,
641 usb_dev->descriptor.bDeviceProtocol,
642 alt->desc.bInterfaceClass,
643 alt->desc.bInterfaceSubClass,
644 alt->desc.bInterfaceProtocol))
645 return -ENOMEM;
647 envp[i] = NULL;
649 return 0;
652 #else
654 static int usb_hotplug (struct device *dev, char **envp,
655 int num_envp, char *buffer, int buffer_size)
657 return -ENODEV;
660 #endif /* CONFIG_HOTPLUG */
663 * usb_release_dev - free a usb device structure when all users of it are finished.
664 * @dev: device that's been disconnected
666 * Will be called only by the device core when all users of this usb device are
667 * done.
669 static void usb_release_dev(struct device *dev)
671 struct usb_device *udev;
673 udev = to_usb_device(dev);
675 usb_destroy_configuration(udev);
676 usb_bus_put(udev->bus);
677 kfree(udev->product);
678 kfree(udev->manufacturer);
679 kfree(udev->serial);
680 kfree(udev);
684 * usb_alloc_dev - usb device constructor (usbcore-internal)
685 * @parent: hub to which device is connected; null to allocate a root hub
686 * @bus: bus used to access the device
687 * @port1: one-based index of port; ignored for root hubs
688 * Context: !in_interrupt ()
690 * Only hub drivers (including virtual root hub drivers for host
691 * controllers) should ever call this.
693 * This call may not be used in a non-sleeping context.
695 struct usb_device *
696 usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1)
698 struct usb_device *dev;
700 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
701 if (!dev)
702 return NULL;
704 bus = usb_bus_get(bus);
705 if (!bus) {
706 kfree(dev);
707 return NULL;
710 device_initialize(&dev->dev);
711 dev->dev.bus = &usb_bus_type;
712 dev->dev.dma_mask = bus->controller->dma_mask;
713 dev->dev.driver_data = &usb_generic_driver_data;
714 dev->dev.driver = &usb_generic_driver;
715 dev->dev.release = usb_release_dev;
716 dev->state = USB_STATE_ATTACHED;
718 INIT_LIST_HEAD(&dev->ep0.urb_list);
719 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
720 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
721 /* ep0 maxpacket comes later, from device descriptor */
722 dev->ep_in[0] = dev->ep_out[0] = &dev->ep0;
724 /* Save readable and stable topology id, distinguishing devices
725 * by location for diagnostics, tools, driver model, etc. The
726 * string is a path along hub ports, from the root. Each device's
727 * dev->devpath will be stable until USB is re-cabled, and hubs
728 * are often labeled with these port numbers. The bus_id isn't
729 * as stable: bus->busnum changes easily from modprobe order,
730 * cardbus or pci hotplugging, and so on.
732 if (unlikely (!parent)) {
733 dev->devpath [0] = '0';
735 dev->dev.parent = bus->controller;
736 sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);
737 } else {
738 /* match any labeling on the hubs; it's one-based */
739 if (parent->devpath [0] == '0')
740 snprintf (dev->devpath, sizeof dev->devpath,
741 "%d", port1);
742 else
743 snprintf (dev->devpath, sizeof dev->devpath,
744 "%s.%d", parent->devpath, port1);
746 dev->dev.parent = &parent->dev;
747 sprintf (&dev->dev.bus_id[0], "%d-%s",
748 bus->busnum, dev->devpath);
750 /* hub driver sets up TT records */
753 dev->bus = bus;
754 dev->parent = parent;
755 INIT_LIST_HEAD(&dev->filelist);
757 init_MUTEX(&dev->serialize);
759 return dev;
763 * usb_get_dev - increments the reference count of the usb device structure
764 * @dev: the device being referenced
766 * Each live reference to a device should be refcounted.
768 * Drivers for USB interfaces should normally record such references in
769 * their probe() methods, when they bind to an interface, and release
770 * them by calling usb_put_dev(), in their disconnect() methods.
772 * A pointer to the device with the incremented reference counter is returned.
774 struct usb_device *usb_get_dev(struct usb_device *dev)
776 if (dev)
777 get_device(&dev->dev);
778 return dev;
782 * usb_put_dev - release a use of the usb device structure
783 * @dev: device that's been disconnected
785 * Must be called when a user of a device is finished with it. When the last
786 * user of the device calls this function, the memory of the device is freed.
788 void usb_put_dev(struct usb_device *dev)
790 if (dev)
791 put_device(&dev->dev);
795 * usb_get_intf - increments the reference count of the usb interface structure
796 * @intf: the interface being referenced
798 * Each live reference to a interface must be refcounted.
800 * Drivers for USB interfaces should normally record such references in
801 * their probe() methods, when they bind to an interface, and release
802 * them by calling usb_put_intf(), in their disconnect() methods.
804 * A pointer to the interface with the incremented reference counter is
805 * returned.
807 struct usb_interface *usb_get_intf(struct usb_interface *intf)
809 if (intf)
810 get_device(&intf->dev);
811 return intf;
815 * usb_put_intf - release a use of the usb interface structure
816 * @intf: interface that's been decremented
818 * Must be called when a user of an interface is finished with it. When the
819 * last user of the interface calls this function, the memory of the interface
820 * is freed.
822 void usb_put_intf(struct usb_interface *intf)
824 if (intf)
825 put_device(&intf->dev);
829 /* USB device locking
831 * Although locking USB devices should be straightforward, it is
832 * complicated by the way the driver-model core works. When a new USB
833 * driver is registered or unregistered, the core will automatically
834 * probe or disconnect all matching interfaces on all USB devices while
835 * holding the USB subsystem writelock. There's no good way for us to
836 * tell which devices will be used or to lock them beforehand; our only
837 * option is to effectively lock all the USB devices.
839 * We do that by using a private rw-semaphore, usb_all_devices_rwsem.
840 * When locking an individual device you must first acquire the rwsem's
841 * readlock. When a driver is registered or unregistered the writelock
842 * must be held. These actions are encapsulated in the subroutines
843 * below, so all a driver needs to do is call usb_lock_device() and
844 * usb_unlock_device().
846 * Complications arise when several devices are to be locked at the same
847 * time. Only hub-aware drivers that are part of usbcore ever have to
848 * do this; nobody else needs to worry about it. The problem is that
849 * usb_lock_device() must not be called to lock a second device since it
850 * would acquire the rwsem's readlock reentrantly, leading to deadlock if
851 * another thread was waiting for the writelock. The solution is simple:
853 * When locking more than one device, call usb_lock_device()
854 * to lock the first one. Lock the others by calling
855 * down(&udev->serialize) directly.
857 * When unlocking multiple devices, use up(&udev->serialize)
858 * to unlock all but the last one. Unlock the last one by
859 * calling usb_unlock_device().
861 * When locking both a device and its parent, always lock the
862 * the parent first.
866 * usb_lock_device - acquire the lock for a usb device structure
867 * @udev: device that's being locked
869 * Use this routine when you don't hold any other device locks;
870 * to acquire nested inner locks call down(&udev->serialize) directly.
871 * This is necessary for proper interaction with usb_lock_all_devices().
873 void usb_lock_device(struct usb_device *udev)
875 down_read(&usb_all_devices_rwsem);
876 down(&udev->serialize);
880 * usb_trylock_device - attempt to acquire the lock for a usb device structure
881 * @udev: device that's being locked
883 * Don't use this routine if you already hold a device lock;
884 * use down_trylock(&udev->serialize) instead.
885 * This is necessary for proper interaction with usb_lock_all_devices().
887 * Returns 1 if successful, 0 if contention.
889 int usb_trylock_device(struct usb_device *udev)
891 if (!down_read_trylock(&usb_all_devices_rwsem))
892 return 0;
893 if (down_trylock(&udev->serialize)) {
894 up_read(&usb_all_devices_rwsem);
895 return 0;
897 return 1;
901 * usb_lock_device_for_reset - cautiously acquire the lock for a
902 * usb device structure
903 * @udev: device that's being locked
904 * @iface: interface bound to the driver making the request (optional)
906 * Attempts to acquire the device lock, but fails if the device is
907 * NOTATTACHED or SUSPENDED, or if iface is specified and the interface
908 * is neither BINDING nor BOUND. Rather than sleeping to wait for the
909 * lock, the routine polls repeatedly. This is to prevent deadlock with
910 * disconnect; in some drivers (such as usb-storage) the disconnect()
911 * or suspend() method will block waiting for a device reset to complete.
913 * Returns a negative error code for failure, otherwise 1 or 0 to indicate
914 * that the device will or will not have to be unlocked. (0 can be
915 * returned when an interface is given and is BINDING, because in that
916 * case the driver already owns the device lock.)
918 int usb_lock_device_for_reset(struct usb_device *udev,
919 struct usb_interface *iface)
921 unsigned long jiffies_expire = jiffies + HZ;
923 if (udev->state == USB_STATE_NOTATTACHED)
924 return -ENODEV;
925 if (udev->state == USB_STATE_SUSPENDED)
926 return -EHOSTUNREACH;
927 if (iface) {
928 switch (iface->condition) {
929 case USB_INTERFACE_BINDING:
930 return 0;
931 case USB_INTERFACE_BOUND:
932 break;
933 default:
934 return -EINTR;
938 while (!usb_trylock_device(udev)) {
940 /* If we can't acquire the lock after waiting one second,
941 * we're probably deadlocked */
942 if (time_after(jiffies, jiffies_expire))
943 return -EBUSY;
945 msleep(15);
946 if (udev->state == USB_STATE_NOTATTACHED)
947 return -ENODEV;
948 if (udev->state == USB_STATE_SUSPENDED)
949 return -EHOSTUNREACH;
950 if (iface && iface->condition != USB_INTERFACE_BOUND)
951 return -EINTR;
953 return 1;
957 * usb_unlock_device - release the lock for a usb device structure
958 * @udev: device that's being unlocked
960 * Use this routine when releasing the only device lock you hold;
961 * to release inner nested locks call up(&udev->serialize) directly.
962 * This is necessary for proper interaction with usb_lock_all_devices().
964 void usb_unlock_device(struct usb_device *udev)
966 up(&udev->serialize);
967 up_read(&usb_all_devices_rwsem);
971 * usb_lock_all_devices - acquire the lock for all usb device structures
973 * This is necessary when registering a new driver or probing a bus,
974 * since the driver-model core may try to use any usb_device.
976 void usb_lock_all_devices(void)
978 down_write(&usb_all_devices_rwsem);
982 * usb_unlock_all_devices - release the lock for all usb device structures
984 void usb_unlock_all_devices(void)
986 up_write(&usb_all_devices_rwsem);
990 static struct usb_device *match_device(struct usb_device *dev,
991 u16 vendor_id, u16 product_id)
993 struct usb_device *ret_dev = NULL;
994 int child;
996 dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",
997 le16_to_cpu(dev->descriptor.idVendor),
998 le16_to_cpu(dev->descriptor.idProduct));
1000 /* see if this device matches */
1001 if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&
1002 (product_id == le16_to_cpu(dev->descriptor.idProduct))) {
1003 dev_dbg (&dev->dev, "matched this device!\n");
1004 ret_dev = usb_get_dev(dev);
1005 goto exit;
1008 /* look through all of the children of this device */
1009 for (child = 0; child < dev->maxchild; ++child) {
1010 if (dev->children[child]) {
1011 down(&dev->children[child]->serialize);
1012 ret_dev = match_device(dev->children[child],
1013 vendor_id, product_id);
1014 up(&dev->children[child]->serialize);
1015 if (ret_dev)
1016 goto exit;
1019 exit:
1020 return ret_dev;
1024 * usb_find_device - find a specific usb device in the system
1025 * @vendor_id: the vendor id of the device to find
1026 * @product_id: the product id of the device to find
1028 * Returns a pointer to a struct usb_device if such a specified usb
1029 * device is present in the system currently. The usage count of the
1030 * device will be incremented if a device is found. Make sure to call
1031 * usb_put_dev() when the caller is finished with the device.
1033 * If a device with the specified vendor and product id is not found,
1034 * NULL is returned.
1036 struct usb_device *usb_find_device(u16 vendor_id, u16 product_id)
1038 struct list_head *buslist;
1039 struct usb_bus *bus;
1040 struct usb_device *dev = NULL;
1042 down(&usb_bus_list_lock);
1043 for (buslist = usb_bus_list.next;
1044 buslist != &usb_bus_list;
1045 buslist = buslist->next) {
1046 bus = container_of(buslist, struct usb_bus, bus_list);
1047 if (!bus->root_hub)
1048 continue;
1049 usb_lock_device(bus->root_hub);
1050 dev = match_device(bus->root_hub, vendor_id, product_id);
1051 usb_unlock_device(bus->root_hub);
1052 if (dev)
1053 goto exit;
1055 exit:
1056 up(&usb_bus_list_lock);
1057 return dev;
1061 * usb_get_current_frame_number - return current bus frame number
1062 * @dev: the device whose bus is being queried
1064 * Returns the current frame number for the USB host controller
1065 * used with the given USB device. This can be used when scheduling
1066 * isochronous requests.
1068 * Note that different kinds of host controller have different
1069 * "scheduling horizons". While one type might support scheduling only
1070 * 32 frames into the future, others could support scheduling up to
1071 * 1024 frames into the future.
1073 int usb_get_current_frame_number(struct usb_device *dev)
1075 return dev->bus->op->get_frame_number (dev);
1078 /*-------------------------------------------------------------------*/
1080 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
1081 * extra field of the interface and endpoint descriptor structs.
1084 int __usb_get_extra_descriptor(char *buffer, unsigned size,
1085 unsigned char type, void **ptr)
1087 struct usb_descriptor_header *header;
1089 while (size >= sizeof(struct usb_descriptor_header)) {
1090 header = (struct usb_descriptor_header *)buffer;
1092 if (header->bLength < 2) {
1093 printk(KERN_ERR
1094 "%s: bogus descriptor, type %d length %d\n",
1095 usbcore_name,
1096 header->bDescriptorType,
1097 header->bLength);
1098 return -1;
1101 if (header->bDescriptorType == type) {
1102 *ptr = header;
1103 return 0;
1106 buffer += header->bLength;
1107 size -= header->bLength;
1109 return -1;
1113 * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
1114 * @dev: device the buffer will be used with
1115 * @size: requested buffer size
1116 * @mem_flags: affect whether allocation may block
1117 * @dma: used to return DMA address of buffer
1119 * Return value is either null (indicating no buffer could be allocated), or
1120 * the cpu-space pointer to a buffer that may be used to perform DMA to the
1121 * specified device. Such cpu-space buffers are returned along with the DMA
1122 * address (through the pointer provided).
1124 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
1125 * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
1126 * mapping hardware for long idle periods. The implementation varies between
1127 * platforms, depending on details of how DMA will work to this device.
1128 * Using these buffers also helps prevent cacheline sharing problems on
1129 * architectures where CPU caches are not DMA-coherent.
1131 * When the buffer is no longer used, free it with usb_buffer_free().
1133 void *usb_buffer_alloc (
1134 struct usb_device *dev,
1135 size_t size,
1136 gfp_t mem_flags,
1137 dma_addr_t *dma
1140 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)
1141 return NULL;
1142 return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);
1146 * usb_buffer_free - free memory allocated with usb_buffer_alloc()
1147 * @dev: device the buffer was used with
1148 * @size: requested buffer size
1149 * @addr: CPU address of buffer
1150 * @dma: DMA address of buffer
1152 * This reclaims an I/O buffer, letting it be reused. The memory must have
1153 * been allocated using usb_buffer_alloc(), and the parameters must match
1154 * those provided in that allocation request.
1156 void usb_buffer_free (
1157 struct usb_device *dev,
1158 size_t size,
1159 void *addr,
1160 dma_addr_t dma
1163 if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)
1164 return;
1165 dev->bus->op->buffer_free (dev->bus, size, addr, dma);
1169 * usb_buffer_map - create DMA mapping(s) for an urb
1170 * @urb: urb whose transfer_buffer/setup_packet will be mapped
1172 * Return value is either null (indicating no buffer could be mapped), or
1173 * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
1174 * added to urb->transfer_flags if the operation succeeds. If the device
1175 * is connected to this system through a non-DMA controller, this operation
1176 * always succeeds.
1178 * This call would normally be used for an urb which is reused, perhaps
1179 * as the target of a large periodic transfer, with usb_buffer_dmasync()
1180 * calls to synchronize memory and dma state.
1182 * Reverse the effect of this call with usb_buffer_unmap().
1184 #if 0
1185 struct urb *usb_buffer_map (struct urb *urb)
1187 struct usb_bus *bus;
1188 struct device *controller;
1190 if (!urb
1191 || !urb->dev
1192 || !(bus = urb->dev->bus)
1193 || !(controller = bus->controller))
1194 return NULL;
1196 if (controller->dma_mask) {
1197 urb->transfer_dma = dma_map_single (controller,
1198 urb->transfer_buffer, urb->transfer_buffer_length,
1199 usb_pipein (urb->pipe)
1200 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1201 if (usb_pipecontrol (urb->pipe))
1202 urb->setup_dma = dma_map_single (controller,
1203 urb->setup_packet,
1204 sizeof (struct usb_ctrlrequest),
1205 DMA_TO_DEVICE);
1206 // FIXME generic api broken like pci, can't report errors
1207 // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
1208 } else
1209 urb->transfer_dma = ~0;
1210 urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
1211 | URB_NO_SETUP_DMA_MAP);
1212 return urb;
1214 #endif /* 0 */
1216 /* XXX DISABLED, no users currently. If you wish to re-enable this
1217 * XXX please determine whether the sync is to transfer ownership of
1218 * XXX the buffer from device to cpu or vice verse, and thusly use the
1219 * XXX appropriate _for_{cpu,device}() method. -DaveM
1221 #if 0
1224 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
1225 * @urb: urb whose transfer_buffer/setup_packet will be synchronized
1227 void usb_buffer_dmasync (struct urb *urb)
1229 struct usb_bus *bus;
1230 struct device *controller;
1232 if (!urb
1233 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1234 || !urb->dev
1235 || !(bus = urb->dev->bus)
1236 || !(controller = bus->controller))
1237 return;
1239 if (controller->dma_mask) {
1240 dma_sync_single (controller,
1241 urb->transfer_dma, urb->transfer_buffer_length,
1242 usb_pipein (urb->pipe)
1243 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1244 if (usb_pipecontrol (urb->pipe))
1245 dma_sync_single (controller,
1246 urb->setup_dma,
1247 sizeof (struct usb_ctrlrequest),
1248 DMA_TO_DEVICE);
1251 #endif
1254 * usb_buffer_unmap - free DMA mapping(s) for an urb
1255 * @urb: urb whose transfer_buffer will be unmapped
1257 * Reverses the effect of usb_buffer_map().
1259 #if 0
1260 void usb_buffer_unmap (struct urb *urb)
1262 struct usb_bus *bus;
1263 struct device *controller;
1265 if (!urb
1266 || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)
1267 || !urb->dev
1268 || !(bus = urb->dev->bus)
1269 || !(controller = bus->controller))
1270 return;
1272 if (controller->dma_mask) {
1273 dma_unmap_single (controller,
1274 urb->transfer_dma, urb->transfer_buffer_length,
1275 usb_pipein (urb->pipe)
1276 ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1277 if (usb_pipecontrol (urb->pipe))
1278 dma_unmap_single (controller,
1279 urb->setup_dma,
1280 sizeof (struct usb_ctrlrequest),
1281 DMA_TO_DEVICE);
1283 urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP
1284 | URB_NO_SETUP_DMA_MAP);
1286 #endif /* 0 */
1289 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
1290 * @dev: device to which the scatterlist will be mapped
1291 * @pipe: endpoint defining the mapping direction
1292 * @sg: the scatterlist to map
1293 * @nents: the number of entries in the scatterlist
1295 * Return value is either < 0 (indicating no buffers could be mapped), or
1296 * the number of DMA mapping array entries in the scatterlist.
1298 * The caller is responsible for placing the resulting DMA addresses from
1299 * the scatterlist into URB transfer buffer pointers, and for setting the
1300 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
1302 * Top I/O rates come from queuing URBs, instead of waiting for each one
1303 * to complete before starting the next I/O. This is particularly easy
1304 * to do with scatterlists. Just allocate and submit one URB for each DMA
1305 * mapping entry returned, stopping on the first error or when all succeed.
1306 * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
1308 * This call would normally be used when translating scatterlist requests,
1309 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
1310 * may be able to coalesce mappings for improved I/O efficiency.
1312 * Reverse the effect of this call with usb_buffer_unmap_sg().
1314 int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe,
1315 struct scatterlist *sg, int nents)
1317 struct usb_bus *bus;
1318 struct device *controller;
1320 if (!dev
1321 || usb_pipecontrol (pipe)
1322 || !(bus = dev->bus)
1323 || !(controller = bus->controller)
1324 || !controller->dma_mask)
1325 return -1;
1327 // FIXME generic api broken like pci, can't report errors
1328 return dma_map_sg (controller, sg, nents,
1329 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1332 /* XXX DISABLED, no users currently. If you wish to re-enable this
1333 * XXX please determine whether the sync is to transfer ownership of
1334 * XXX the buffer from device to cpu or vice verse, and thusly use the
1335 * XXX appropriate _for_{cpu,device}() method. -DaveM
1337 #if 0
1340 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
1341 * @dev: device to which the scatterlist will be mapped
1342 * @pipe: endpoint defining the mapping direction
1343 * @sg: the scatterlist to synchronize
1344 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1346 * Use this when you are re-using a scatterlist's data buffers for
1347 * another USB request.
1349 void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe,
1350 struct scatterlist *sg, int n_hw_ents)
1352 struct usb_bus *bus;
1353 struct device *controller;
1355 if (!dev
1356 || !(bus = dev->bus)
1357 || !(controller = bus->controller)
1358 || !controller->dma_mask)
1359 return;
1361 dma_sync_sg (controller, sg, n_hw_ents,
1362 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1364 #endif
1367 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
1368 * @dev: device to which the scatterlist will be mapped
1369 * @pipe: endpoint defining the mapping direction
1370 * @sg: the scatterlist to unmap
1371 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1373 * Reverses the effect of usb_buffer_map_sg().
1375 void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe,
1376 struct scatterlist *sg, int n_hw_ents)
1378 struct usb_bus *bus;
1379 struct device *controller;
1381 if (!dev
1382 || !(bus = dev->bus)
1383 || !(controller = bus->controller)
1384 || !controller->dma_mask)
1385 return;
1387 dma_unmap_sg (controller, sg, n_hw_ents,
1388 usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
1391 static int verify_suspended(struct device *dev, void *unused)
1393 return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0;
1396 static int usb_generic_suspend(struct device *dev, pm_message_t message)
1398 struct usb_interface *intf;
1399 struct usb_driver *driver;
1400 int status;
1402 /* USB devices enter SUSPEND state through their hubs, but can be
1403 * marked for FREEZE as soon as their children are already idled.
1404 * But those semantics are useless, so we equate the two (sigh).
1406 if (dev->driver == &usb_generic_driver) {
1407 if (dev->power.power_state.event == message.event)
1408 return 0;
1409 /* we need to rule out bogus requests through sysfs */
1410 status = device_for_each_child(dev, NULL, verify_suspended);
1411 if (status)
1412 return status;
1413 return usb_suspend_device (to_usb_device(dev));
1416 if ((dev->driver == NULL) ||
1417 (dev->driver_data == &usb_generic_driver_data))
1418 return 0;
1420 intf = to_usb_interface(dev);
1421 driver = to_usb_driver(dev->driver);
1423 /* with no hardware, USB interfaces only use FREEZE and ON states */
1424 if (!is_active(intf))
1425 return 0;
1427 if (driver->suspend && driver->resume) {
1428 status = driver->suspend(intf, message);
1429 if (status)
1430 dev_err(dev, "%s error %d\n", "suspend", status);
1431 else
1432 mark_quiesced(intf);
1433 } else {
1434 // FIXME else if there's no suspend method, disconnect...
1435 dev_warn(dev, "no %s?\n", "suspend");
1436 status = 0;
1438 return status;
1441 static int usb_generic_resume(struct device *dev)
1443 struct usb_interface *intf;
1444 struct usb_driver *driver;
1445 struct usb_device *udev;
1446 int status;
1448 if (dev->power.power_state.event == PM_EVENT_ON)
1449 return 0;
1451 /* mark things as "on" immediately, no matter what errors crop up */
1452 dev->power.power_state.event = PM_EVENT_ON;
1454 /* devices resume through their hubs */
1455 if (dev->driver == &usb_generic_driver) {
1456 udev = to_usb_device(dev);
1457 if (udev->state == USB_STATE_NOTATTACHED)
1458 return 0;
1459 return usb_resume_device (to_usb_device(dev));
1462 if ((dev->driver == NULL) ||
1463 (dev->driver_data == &usb_generic_driver_data))
1464 return 0;
1466 intf = to_usb_interface(dev);
1467 driver = to_usb_driver(dev->driver);
1469 udev = interface_to_usbdev(intf);
1470 if (udev->state == USB_STATE_NOTATTACHED)
1471 return 0;
1473 /* if driver was suspended, it has a resume method;
1474 * however, sysfs can wrongly mark things as suspended
1475 * (on the "no suspend method" FIXME path above)
1477 if (driver->resume) {
1478 status = driver->resume(intf);
1479 if (status) {
1480 dev_err(dev, "%s error %d\n", "resume", status);
1481 mark_quiesced(intf);
1483 } else
1484 dev_warn(dev, "no %s?\n", "resume");
1485 return 0;
1488 struct bus_type usb_bus_type = {
1489 .name = "usb",
1490 .match = usb_device_match,
1491 .hotplug = usb_hotplug,
1492 .suspend = usb_generic_suspend,
1493 .resume = usb_generic_resume,
1496 #ifndef MODULE
1498 static int __init usb_setup_disable(char *str)
1500 nousb = 1;
1501 return 1;
1504 /* format to disable USB on kernel command line is: nousb */
1505 __setup("nousb", usb_setup_disable);
1507 #endif
1510 * for external read access to <nousb>
1512 int usb_disabled(void)
1514 return nousb;
1518 * Init
1520 static int __init usb_init(void)
1522 int retval;
1523 if (nousb) {
1524 pr_info ("%s: USB support disabled\n", usbcore_name);
1525 return 0;
1528 retval = bus_register(&usb_bus_type);
1529 if (retval)
1530 goto out;
1531 retval = usb_host_init();
1532 if (retval)
1533 goto host_init_failed;
1534 retval = usb_major_init();
1535 if (retval)
1536 goto major_init_failed;
1537 retval = usb_register(&usbfs_driver);
1538 if (retval)
1539 goto driver_register_failed;
1540 retval = usbdev_init();
1541 if (retval)
1542 goto usbdevice_init_failed;
1543 retval = usbfs_init();
1544 if (retval)
1545 goto fs_init_failed;
1546 retval = usb_hub_init();
1547 if (retval)
1548 goto hub_init_failed;
1549 retval = driver_register(&usb_generic_driver);
1550 if (!retval)
1551 goto out;
1553 usb_hub_cleanup();
1554 hub_init_failed:
1555 usbfs_cleanup();
1556 fs_init_failed:
1557 usbdev_cleanup();
1558 usbdevice_init_failed:
1559 usb_deregister(&usbfs_driver);
1560 driver_register_failed:
1561 usb_major_cleanup();
1562 major_init_failed:
1563 usb_host_cleanup();
1564 host_init_failed:
1565 bus_unregister(&usb_bus_type);
1566 out:
1567 return retval;
1571 * Cleanup
1573 static void __exit usb_exit(void)
1575 /* This will matter if shutdown/reboot does exitcalls. */
1576 if (nousb)
1577 return;
1579 driver_unregister(&usb_generic_driver);
1580 usb_major_cleanup();
1581 usbfs_cleanup();
1582 usb_deregister(&usbfs_driver);
1583 usbdev_cleanup();
1584 usb_hub_cleanup();
1585 usb_host_cleanup();
1586 bus_unregister(&usb_bus_type);
1589 subsys_initcall(usb_init);
1590 module_exit(usb_exit);
1593 * USB may be built into the kernel or be built as modules.
1594 * These symbols are exported for device (or host controller)
1595 * driver modules to use.
1598 EXPORT_SYMBOL(usb_register);
1599 EXPORT_SYMBOL(usb_deregister);
1600 EXPORT_SYMBOL(usb_disabled);
1602 EXPORT_SYMBOL_GPL(usb_get_intf);
1603 EXPORT_SYMBOL_GPL(usb_put_intf);
1605 EXPORT_SYMBOL(usb_alloc_dev);
1606 EXPORT_SYMBOL(usb_put_dev);
1607 EXPORT_SYMBOL(usb_get_dev);
1608 EXPORT_SYMBOL(usb_hub_tt_clear_buffer);
1610 EXPORT_SYMBOL(usb_lock_device);
1611 EXPORT_SYMBOL(usb_trylock_device);
1612 EXPORT_SYMBOL(usb_lock_device_for_reset);
1613 EXPORT_SYMBOL(usb_unlock_device);
1615 EXPORT_SYMBOL(usb_driver_claim_interface);
1616 EXPORT_SYMBOL(usb_driver_release_interface);
1617 EXPORT_SYMBOL(usb_match_id);
1618 EXPORT_SYMBOL(usb_find_interface);
1619 EXPORT_SYMBOL(usb_ifnum_to_if);
1620 EXPORT_SYMBOL(usb_altnum_to_altsetting);
1622 EXPORT_SYMBOL(usb_reset_device);
1623 EXPORT_SYMBOL(usb_disconnect);
1625 EXPORT_SYMBOL(__usb_get_extra_descriptor);
1627 EXPORT_SYMBOL(usb_find_device);
1628 EXPORT_SYMBOL(usb_get_current_frame_number);
1630 EXPORT_SYMBOL (usb_buffer_alloc);
1631 EXPORT_SYMBOL (usb_buffer_free);
1633 #if 0
1634 EXPORT_SYMBOL (usb_buffer_map);
1635 EXPORT_SYMBOL (usb_buffer_dmasync);
1636 EXPORT_SYMBOL (usb_buffer_unmap);
1637 #endif
1639 EXPORT_SYMBOL (usb_buffer_map_sg);
1640 #if 0
1641 EXPORT_SYMBOL (usb_buffer_dmasync_sg);
1642 #endif
1643 EXPORT_SYMBOL (usb_buffer_unmap_sg);
1645 MODULE_LICENSE("GPL");