Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/usb/usb.c |
| 3 | * |
| 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 |
| 14 | * |
| 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.. |
| 18 | * |
| 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. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/config.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
| 37 | |
| 38 | #include <asm/io.h> |
| 39 | #include <asm/scatterlist.h> |
| 40 | #include <linux/mm.h> |
| 41 | #include <linux/dma-mapping.h> |
| 42 | |
| 43 | #include "hcd.h" |
| 44 | #include "usb.h" |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | const char *usbcore_name = "usbcore"; |
| 48 | |
| 49 | static int nousb; /* Disable USB when built into kernel image */ |
| 50 | /* Not honored on modular build */ |
| 51 | |
| 52 | static DECLARE_RWSEM(usb_all_devices_rwsem); |
| 53 | |
| 54 | |
| 55 | static int generic_probe (struct device *dev) |
| 56 | { |
| 57 | return 0; |
| 58 | } |
| 59 | static int generic_remove (struct device *dev) |
| 60 | { |
Alan Stern | 3b4d7f7 | 2005-08-11 15:50:32 -0400 | [diff] [blame] | 61 | struct usb_device *udev = to_usb_device(dev); |
| 62 | |
| 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); |
| 67 | |
| 68 | /* in case the call failed or the device was suspended */ |
| 69 | if (udev->state >= USB_STATE_CONFIGURED) |
| 70 | usb_disable_device(udev, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
| 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, |
| 80 | }; |
| 81 | |
| 82 | static int usb_generic_driver_data; |
| 83 | |
| 84 | /* called from driver core with usb_bus_type.subsys writelock */ |
| 85 | static int usb_probe_interface(struct device *dev) |
| 86 | { |
| 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; |
| 91 | |
| 92 | dev_dbg(dev, "%s\n", __FUNCTION__); |
| 93 | |
| 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; |
| 99 | |
| 100 | id = usb_match_id (intf, driver->id_table); |
| 101 | if (id) { |
| 102 | dev_dbg (dev, "%s - got id\n", __FUNCTION__); |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 103 | |
| 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. |
| 107 | */ |
| 108 | mark_active(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | intf->condition = USB_INTERFACE_BINDING; |
| 110 | error = driver->probe (intf, id); |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 111 | if (error) { |
| 112 | mark_quiesced(intf); |
| 113 | intf->condition = USB_INTERFACE_UNBOUND; |
| 114 | } else |
| 115 | intf->condition = USB_INTERFACE_BOUND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return error; |
| 119 | } |
| 120 | |
| 121 | /* called from driver core with usb_bus_type.subsys writelock */ |
| 122 | static int usb_unbind_interface(struct device *dev) |
| 123 | { |
| 124 | struct usb_interface *intf = to_usb_interface(dev); |
| 125 | struct usb_driver *driver = to_usb_driver(intf->dev.driver); |
| 126 | |
| 127 | intf->condition = USB_INTERFACE_UNBINDING; |
| 128 | |
| 129 | /* release all urbs for this interface */ |
| 130 | usb_disable_interface(interface_to_usbdev(intf), intf); |
| 131 | |
| 132 | if (driver && driver->disconnect) |
| 133 | driver->disconnect(intf); |
| 134 | |
| 135 | /* reset other interface state */ |
| 136 | usb_set_interface(interface_to_usbdev(intf), |
| 137 | intf->altsetting[0].desc.bInterfaceNumber, |
| 138 | 0); |
| 139 | usb_set_intfdata(intf, NULL); |
| 140 | intf->condition = USB_INTERFACE_UNBOUND; |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 141 | mark_quiesced(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * usb_register - register a USB driver |
| 148 | * @new_driver: USB operations for the driver |
| 149 | * |
| 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. |
| 154 | * |
| 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. |
| 158 | */ |
| 159 | int usb_register(struct usb_driver *new_driver) |
| 160 | { |
| 161 | int retval = 0; |
| 162 | |
| 163 | if (nousb) |
| 164 | return -ENODEV; |
| 165 | |
| 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; |
| 171 | |
| 172 | usb_lock_all_devices(); |
| 173 | retval = driver_register(&new_driver->driver); |
| 174 | usb_unlock_all_devices(); |
| 175 | |
| 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); |
| 183 | } |
| 184 | |
| 185 | return retval; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * usb_deregister - unregister a USB driver |
| 190 | * @driver: USB operations of the driver to unregister |
| 191 | * Context: must be able to sleep |
| 192 | * |
| 193 | * Unlinks the specified driver from the internal USB driver list. |
| 194 | * |
| 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. |
| 198 | */ |
| 199 | void usb_deregister(struct usb_driver *driver) |
| 200 | { |
| 201 | pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name); |
| 202 | |
| 203 | usb_lock_all_devices(); |
| 204 | driver_unregister (&driver->driver); |
| 205 | usb_unlock_all_devices(); |
| 206 | |
| 207 | usbfs_update_special(); |
| 208 | } |
| 209 | |
| 210 | /** |
| 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 |
| 214 | * |
| 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. |
| 218 | * |
| 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. |
| 225 | * |
| 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! |
| 228 | */ |
| 229 | struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum) |
| 230 | { |
| 231 | struct usb_host_config *config = dev->actconfig; |
| 232 | int i; |
| 233 | |
| 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]; |
| 240 | |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | /** |
| 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 |
| 249 | * |
| 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. |
| 253 | * |
| 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. |
| 258 | * |
| 259 | * Don't call this function unless you are bound to the intf interface |
| 260 | * or you have locked the device! |
| 261 | */ |
| 262 | struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf, |
| 263 | unsigned int altnum) |
| 264 | { |
| 265 | int i; |
| 266 | |
| 267 | for (i = 0; i < intf->num_altsetting; i++) { |
| 268 | if (intf->altsetting[i].desc.bAlternateSetting == altnum) |
| 269 | return &intf->altsetting[i]; |
| 270 | } |
| 271 | return NULL; |
| 272 | } |
| 273 | |
| 274 | /** |
| 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 |
| 280 | * |
| 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. |
| 285 | * |
| 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. |
| 289 | * |
| 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. |
| 293 | */ |
| 294 | int usb_driver_claim_interface(struct usb_driver *driver, |
| 295 | struct usb_interface *iface, void* priv) |
| 296 | { |
| 297 | struct device *dev = &iface->dev; |
| 298 | |
| 299 | if (dev->driver) |
| 300 | return -EBUSY; |
| 301 | |
| 302 | dev->driver = &driver->driver; |
| 303 | usb_set_intfdata(iface, priv); |
| 304 | iface->condition = USB_INTERFACE_BOUND; |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 305 | mark_active(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
| 307 | /* if interface was already added, bind now; else let |
| 308 | * the future device_add() bind it, bypassing probe() |
| 309 | */ |
Daniel Ritz | d305ef5 | 2005-09-22 00:47:24 -0700 | [diff] [blame] | 310 | if (device_is_registered(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | device_bind_driver(dev); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /** |
| 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 |
| 320 | * |
| 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. |
| 324 | * |
| 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. |
| 329 | */ |
| 330 | void usb_driver_release_interface(struct usb_driver *driver, |
| 331 | struct usb_interface *iface) |
| 332 | { |
| 333 | struct device *dev = &iface->dev; |
| 334 | |
| 335 | /* this should never happen, don't release something that's not ours */ |
| 336 | if (!dev->driver || dev->driver != &driver->driver) |
| 337 | return; |
| 338 | |
Alan Stern | f409661 | 2005-05-06 15:41:08 -0400 | [diff] [blame] | 339 | /* don't release from within disconnect() */ |
| 340 | if (iface->condition != USB_INTERFACE_BOUND) |
| 341 | return; |
| 342 | |
Daniel Ritz | d305ef5 | 2005-09-22 00:47:24 -0700 | [diff] [blame] | 343 | /* don't release if the interface hasn't been added yet */ |
| 344 | if (device_is_registered(dev)) { |
Alan Stern | f409661 | 2005-05-06 15:41:08 -0400 | [diff] [blame] | 345 | iface->condition = USB_INTERFACE_UNBINDING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | device_release_driver(dev); |
Alan Stern | f409661 | 2005-05-06 15:41:08 -0400 | [diff] [blame] | 347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
| 349 | dev->driver = NULL; |
| 350 | usb_set_intfdata(iface, NULL); |
| 351 | iface->condition = USB_INTERFACE_UNBOUND; |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 352 | mark_quiesced(iface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /** |
| 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 |
| 359 | * |
| 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. |
| 368 | * |
| 369 | * What Matches: |
| 370 | * |
| 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. |
| 376 | * |
| 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. |
| 383 | * |
| 384 | * What Makes Good usb_device_id Tables: |
| 385 | * |
| 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. |
| 392 | * |
| 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. |
| 399 | * |
| 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. |
| 405 | * |
| 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). |
| 411 | * |
| 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. |
| 416 | */ |
| 417 | const struct usb_device_id * |
| 418 | usb_match_id(struct usb_interface *interface, const struct usb_device_id *id) |
| 419 | { |
| 420 | struct usb_host_interface *intf; |
| 421 | struct usb_device *dev; |
| 422 | |
| 423 | /* proc_connectinfo in devio.c may call us with id == NULL. */ |
| 424 | if (id == NULL) |
| 425 | return NULL; |
| 426 | |
| 427 | intf = interface->cur_altsetting; |
| 428 | dev = interface_to_usbdev(interface); |
| 429 | |
| 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++) { |
| 437 | |
| 438 | if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
| 439 | id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) |
| 440 | continue; |
| 441 | |
| 442 | if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && |
| 443 | id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) |
| 444 | continue; |
| 445 | |
| 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; |
| 451 | |
| 452 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && |
| 453 | (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) |
| 454 | continue; |
| 455 | |
| 456 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && |
| 457 | (id->bDeviceClass != dev->descriptor.bDeviceClass)) |
| 458 | continue; |
| 459 | |
| 460 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && |
| 461 | (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass)) |
| 462 | continue; |
| 463 | |
| 464 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && |
| 465 | (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) |
| 466 | continue; |
| 467 | |
| 468 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && |
| 469 | (id->bInterfaceClass != intf->desc.bInterfaceClass)) |
| 470 | continue; |
| 471 | |
| 472 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && |
| 473 | (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) |
| 474 | continue; |
| 475 | |
| 476 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && |
| 477 | (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) |
| 478 | continue; |
| 479 | |
| 480 | return id; |
| 481 | } |
| 482 | |
| 483 | return NULL; |
| 484 | } |
| 485 | |
mochel@digitalimplant.org | 6034a08 | 2005-03-21 11:09:40 -0800 | [diff] [blame] | 486 | |
| 487 | static int __find_interface(struct device * dev, void * data) |
| 488 | { |
| 489 | struct usb_interface ** ret = (struct usb_interface **)data; |
| 490 | struct usb_interface * intf = *ret; |
| 491 | int *minor = (int *)data; |
| 492 | |
| 493 | /* can't look at usb devices, only interfaces */ |
| 494 | if (dev->driver == &usb_generic_driver) |
| 495 | return 0; |
| 496 | |
| 497 | intf = to_usb_interface(dev); |
| 498 | if (intf->minor != -1 && intf->minor == *minor) { |
| 499 | *ret = intf; |
| 500 | return 1; |
| 501 | } |
| 502 | return 0; |
| 503 | } |
| 504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | /** |
| 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 |
| 509 | * |
| 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. |
| 513 | */ |
| 514 | struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor) |
| 515 | { |
gregkh@suse.de | ff71071 | 2005-03-24 00:44:28 -0800 | [diff] [blame] | 516 | struct usb_interface *intf = (struct usb_interface *)(long)minor; |
mochel@digitalimplant.org | 6034a08 | 2005-03-21 11:09:40 -0800 | [diff] [blame] | 517 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
mochel@digitalimplant.org | 6034a08 | 2005-03-21 11:09:40 -0800 | [diff] [blame] | 519 | ret = driver_for_each_device(&drv->driver, NULL, &intf, __find_interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
mochel@digitalimplant.org | 6034a08 | 2005-03-21 11:09:40 -0800 | [diff] [blame] | 521 | return ret ? intf : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | static int usb_device_match (struct device *dev, struct device_driver *drv) |
| 525 | { |
| 526 | struct usb_interface *intf; |
| 527 | struct usb_driver *usb_drv; |
| 528 | const struct usb_device_id *id; |
| 529 | |
| 530 | /* check for generic driver, which we don't match any device with */ |
| 531 | if (drv == &usb_generic_driver) |
| 532 | return 0; |
| 533 | |
| 534 | intf = to_usb_interface(dev); |
| 535 | usb_drv = to_usb_driver(drv); |
| 536 | |
| 537 | id = usb_match_id (intf, usb_drv->id_table); |
| 538 | if (id) |
| 539 | return 1; |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | |
| 545 | #ifdef CONFIG_HOTPLUG |
| 546 | |
| 547 | /* |
| 548 | * USB hotplugging invokes what /proc/sys/kernel/hotplug says |
| 549 | * (normally /sbin/hotplug) when USB devices get added or removed. |
| 550 | * |
| 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. |
| 554 | * |
| 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. |
| 559 | */ |
| 560 | static int usb_hotplug (struct device *dev, char **envp, int num_envp, |
| 561 | char *buffer, int buffer_size) |
| 562 | { |
| 563 | struct usb_interface *intf; |
| 564 | struct usb_device *usb_dev; |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 565 | struct usb_host_interface *alt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | int i = 0; |
| 567 | int length = 0; |
| 568 | |
| 569 | if (!dev) |
| 570 | return -ENODEV; |
| 571 | |
| 572 | /* driver is often null here; dev_dbg() would oops */ |
| 573 | pr_debug ("usb %s: hotplug\n", dev->bus_id); |
| 574 | |
| 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; |
| 579 | |
| 580 | intf = to_usb_interface(dev); |
| 581 | usb_dev = interface_to_usbdev (intf); |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 582 | alt = intf->cur_altsetting; |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | if (usb_dev->devnum < 0) { |
| 585 | pr_debug ("usb %s: already deleted?\n", dev->bus_id); |
| 586 | return -ENODEV; |
| 587 | } |
| 588 | if (!usb_dev->bus) { |
| 589 | pr_debug ("usb %s: bus removed?\n", dev->bus_id); |
| 590 | return -ENODEV; |
| 591 | } |
| 592 | |
| 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. |
| 597 | * |
| 598 | * FIXME reduce hardwired intelligence here |
| 599 | */ |
| 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 |
| 606 | |
| 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; |
| 615 | |
| 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; |
| 624 | |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
| 647 | envp[i] = NULL; |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | #else |
| 653 | |
| 654 | static int usb_hotplug (struct device *dev, char **envp, |
| 655 | int num_envp, char *buffer, int buffer_size) |
| 656 | { |
| 657 | return -ENODEV; |
| 658 | } |
| 659 | |
| 660 | #endif /* CONFIG_HOTPLUG */ |
| 661 | |
| 662 | /** |
| 663 | * usb_release_dev - free a usb device structure when all users of it are finished. |
| 664 | * @dev: device that's been disconnected |
| 665 | * |
| 666 | * Will be called only by the device core when all users of this usb device are |
| 667 | * done. |
| 668 | */ |
| 669 | static void usb_release_dev(struct device *dev) |
| 670 | { |
| 671 | struct usb_device *udev; |
| 672 | |
| 673 | udev = to_usb_device(dev); |
| 674 | |
| 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); |
| 681 | } |
| 682 | |
| 683 | /** |
| 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 () |
| 689 | * |
| 690 | * Only hub drivers (including virtual root hub drivers for host |
| 691 | * controllers) should ever call this. |
| 692 | * |
| 693 | * This call may not be used in a non-sleeping context. |
| 694 | */ |
| 695 | struct usb_device * |
| 696 | usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1) |
| 697 | { |
| 698 | struct usb_device *dev; |
| 699 | |
Alan Stern | 0a1ef3b | 2005-10-24 15:38:24 -0400 | [diff] [blame] | 700 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | if (!dev) |
| 702 | return NULL; |
| 703 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | bus = usb_bus_get(bus); |
| 705 | if (!bus) { |
| 706 | kfree(dev); |
| 707 | return NULL; |
| 708 | } |
| 709 | |
| 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; |
| 717 | |
| 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; |
| 723 | |
| 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. |
| 731 | */ |
| 732 | if (unlikely (!parent)) { |
| 733 | dev->devpath [0] = '0'; |
| 734 | |
| 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); |
| 745 | |
| 746 | dev->dev.parent = &parent->dev; |
| 747 | sprintf (&dev->dev.bus_id[0], "%d-%s", |
| 748 | bus->busnum, dev->devpath); |
| 749 | |
| 750 | /* hub driver sets up TT records */ |
| 751 | } |
| 752 | |
| 753 | dev->bus = bus; |
| 754 | dev->parent = parent; |
| 755 | INIT_LIST_HEAD(&dev->filelist); |
| 756 | |
| 757 | init_MUTEX(&dev->serialize); |
| 758 | |
| 759 | return dev; |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * usb_get_dev - increments the reference count of the usb device structure |
| 764 | * @dev: the device being referenced |
| 765 | * |
| 766 | * Each live reference to a device should be refcounted. |
| 767 | * |
| 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. |
| 771 | * |
| 772 | * A pointer to the device with the incremented reference counter is returned. |
| 773 | */ |
| 774 | struct usb_device *usb_get_dev(struct usb_device *dev) |
| 775 | { |
| 776 | if (dev) |
| 777 | get_device(&dev->dev); |
| 778 | return dev; |
| 779 | } |
| 780 | |
| 781 | /** |
| 782 | * usb_put_dev - release a use of the usb device structure |
| 783 | * @dev: device that's been disconnected |
| 784 | * |
| 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. |
| 787 | */ |
| 788 | void usb_put_dev(struct usb_device *dev) |
| 789 | { |
| 790 | if (dev) |
| 791 | put_device(&dev->dev); |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * usb_get_intf - increments the reference count of the usb interface structure |
| 796 | * @intf: the interface being referenced |
| 797 | * |
| 798 | * Each live reference to a interface must be refcounted. |
| 799 | * |
| 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. |
| 803 | * |
| 804 | * A pointer to the interface with the incremented reference counter is |
| 805 | * returned. |
| 806 | */ |
| 807 | struct usb_interface *usb_get_intf(struct usb_interface *intf) |
| 808 | { |
| 809 | if (intf) |
| 810 | get_device(&intf->dev); |
| 811 | return intf; |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * usb_put_intf - release a use of the usb interface structure |
| 816 | * @intf: interface that's been decremented |
| 817 | * |
| 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. |
| 821 | */ |
| 822 | void usb_put_intf(struct usb_interface *intf) |
| 823 | { |
| 824 | if (intf) |
| 825 | put_device(&intf->dev); |
| 826 | } |
| 827 | |
| 828 | |
| 829 | /* USB device locking |
| 830 | * |
| 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. |
| 838 | * |
| 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(). |
| 845 | * |
| 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: |
| 852 | * |
| 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. |
| 856 | * |
| 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(). |
| 860 | * |
| 861 | * When locking both a device and its parent, always lock the |
| 862 | * the parent first. |
| 863 | */ |
| 864 | |
| 865 | /** |
| 866 | * usb_lock_device - acquire the lock for a usb device structure |
| 867 | * @udev: device that's being locked |
| 868 | * |
| 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(). |
| 872 | */ |
| 873 | void usb_lock_device(struct usb_device *udev) |
| 874 | { |
| 875 | down_read(&usb_all_devices_rwsem); |
| 876 | down(&udev->serialize); |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * usb_trylock_device - attempt to acquire the lock for a usb device structure |
| 881 | * @udev: device that's being locked |
| 882 | * |
| 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(). |
| 886 | * |
| 887 | * Returns 1 if successful, 0 if contention. |
| 888 | */ |
| 889 | int usb_trylock_device(struct usb_device *udev) |
| 890 | { |
| 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; |
| 896 | } |
| 897 | return 1; |
| 898 | } |
| 899 | |
| 900 | /** |
| 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) |
| 905 | * |
| 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() |
Alan Stern | 3ea1596 | 2005-08-11 10:15:39 -0400 | [diff] [blame] | 911 | * or suspend() method will block waiting for a device reset to complete. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | * |
| 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.) |
| 917 | */ |
| 918 | int usb_lock_device_for_reset(struct usb_device *udev, |
| 919 | struct usb_interface *iface) |
| 920 | { |
Alan Stern | 3ea1596 | 2005-08-11 10:15:39 -0400 | [diff] [blame] | 921 | unsigned long jiffies_expire = jiffies + HZ; |
| 922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | while (!usb_trylock_device(udev)) { |
Alan Stern | 3ea1596 | 2005-08-11 10:15:39 -0400 | [diff] [blame] | 939 | |
| 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; |
| 944 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 952 | } |
| 953 | return 1; |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * usb_unlock_device - release the lock for a usb device structure |
| 958 | * @udev: device that's being unlocked |
| 959 | * |
| 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(). |
| 963 | */ |
| 964 | void usb_unlock_device(struct usb_device *udev) |
| 965 | { |
| 966 | up(&udev->serialize); |
| 967 | up_read(&usb_all_devices_rwsem); |
| 968 | } |
| 969 | |
| 970 | /** |
| 971 | * usb_lock_all_devices - acquire the lock for all usb device structures |
| 972 | * |
| 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. |
| 975 | */ |
| 976 | void usb_lock_all_devices(void) |
| 977 | { |
| 978 | down_write(&usb_all_devices_rwsem); |
| 979 | } |
| 980 | |
| 981 | /** |
| 982 | * usb_unlock_all_devices - release the lock for all usb device structures |
| 983 | */ |
| 984 | void usb_unlock_all_devices(void) |
| 985 | { |
| 986 | up_write(&usb_all_devices_rwsem); |
| 987 | } |
| 988 | |
| 989 | |
| 990 | static struct usb_device *match_device(struct usb_device *dev, |
| 991 | u16 vendor_id, u16 product_id) |
| 992 | { |
| 993 | struct usb_device *ret_dev = NULL; |
| 994 | int child; |
| 995 | |
| 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)); |
| 999 | |
| 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; |
| 1006 | } |
| 1007 | |
| 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; |
| 1017 | } |
| 1018 | } |
| 1019 | exit: |
| 1020 | return ret_dev; |
| 1021 | } |
| 1022 | |
| 1023 | /** |
| 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 |
| 1027 | * |
| 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. |
| 1032 | * |
| 1033 | * If a device with the specified vendor and product id is not found, |
| 1034 | * NULL is returned. |
| 1035 | */ |
| 1036 | struct usb_device *usb_find_device(u16 vendor_id, u16 product_id) |
| 1037 | { |
| 1038 | struct list_head *buslist; |
| 1039 | struct usb_bus *bus; |
| 1040 | struct usb_device *dev = NULL; |
| 1041 | |
| 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; |
| 1054 | } |
| 1055 | exit: |
| 1056 | up(&usb_bus_list_lock); |
| 1057 | return dev; |
| 1058 | } |
| 1059 | |
| 1060 | /** |
| 1061 | * usb_get_current_frame_number - return current bus frame number |
| 1062 | * @dev: the device whose bus is being queried |
| 1063 | * |
| 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. |
| 1067 | * |
| 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. |
| 1072 | */ |
| 1073 | int usb_get_current_frame_number(struct usb_device *dev) |
| 1074 | { |
| 1075 | return dev->bus->op->get_frame_number (dev); |
| 1076 | } |
| 1077 | |
| 1078 | /*-------------------------------------------------------------------*/ |
| 1079 | /* |
| 1080 | * __usb_get_extra_descriptor() finds a descriptor of specific type in the |
| 1081 | * extra field of the interface and endpoint descriptor structs. |
| 1082 | */ |
| 1083 | |
| 1084 | int __usb_get_extra_descriptor(char *buffer, unsigned size, |
| 1085 | unsigned char type, void **ptr) |
| 1086 | { |
| 1087 | struct usb_descriptor_header *header; |
| 1088 | |
| 1089 | while (size >= sizeof(struct usb_descriptor_header)) { |
| 1090 | header = (struct usb_descriptor_header *)buffer; |
| 1091 | |
| 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; |
| 1099 | } |
| 1100 | |
| 1101 | if (header->bDescriptorType == type) { |
| 1102 | *ptr = header; |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | buffer += header->bLength; |
| 1107 | size -= header->bLength; |
| 1108 | } |
| 1109 | return -1; |
| 1110 | } |
| 1111 | |
| 1112 | /** |
| 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 |
| 1118 | * |
| 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). |
| 1123 | * |
| 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. |
| 1130 | * |
| 1131 | * When the buffer is no longer used, free it with usb_buffer_free(). |
| 1132 | */ |
| 1133 | void *usb_buffer_alloc ( |
| 1134 | struct usb_device *dev, |
| 1135 | size_t size, |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 1136 | gfp_t mem_flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | dma_addr_t *dma |
| 1138 | ) |
| 1139 | { |
| 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); |
| 1143 | } |
| 1144 | |
| 1145 | /** |
| 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 |
| 1151 | * |
| 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. |
| 1155 | */ |
| 1156 | void usb_buffer_free ( |
| 1157 | struct usb_device *dev, |
| 1158 | size_t size, |
| 1159 | void *addr, |
| 1160 | dma_addr_t dma |
| 1161 | ) |
| 1162 | { |
| 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); |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * usb_buffer_map - create DMA mapping(s) for an urb |
| 1170 | * @urb: urb whose transfer_buffer/setup_packet will be mapped |
| 1171 | * |
| 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. |
| 1177 | * |
| 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. |
| 1181 | * |
| 1182 | * Reverse the effect of this call with usb_buffer_unmap(). |
| 1183 | */ |
| 1184 | #if 0 |
| 1185 | struct urb *usb_buffer_map (struct urb *urb) |
| 1186 | { |
| 1187 | struct usb_bus *bus; |
| 1188 | struct device *controller; |
| 1189 | |
| 1190 | if (!urb |
| 1191 | || !urb->dev |
| 1192 | || !(bus = urb->dev->bus) |
| 1193 | || !(controller = bus->controller)) |
| 1194 | return NULL; |
| 1195 | |
| 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; |
| 1213 | } |
| 1214 | #endif /* 0 */ |
| 1215 | |
| 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 |
| 1220 | */ |
| 1221 | #if 0 |
| 1222 | |
| 1223 | /** |
| 1224 | * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s) |
| 1225 | * @urb: urb whose transfer_buffer/setup_packet will be synchronized |
| 1226 | */ |
| 1227 | void usb_buffer_dmasync (struct urb *urb) |
| 1228 | { |
| 1229 | struct usb_bus *bus; |
| 1230 | struct device *controller; |
| 1231 | |
| 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; |
| 1238 | |
| 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); |
| 1249 | } |
| 1250 | } |
| 1251 | #endif |
| 1252 | |
| 1253 | /** |
| 1254 | * usb_buffer_unmap - free DMA mapping(s) for an urb |
| 1255 | * @urb: urb whose transfer_buffer will be unmapped |
| 1256 | * |
| 1257 | * Reverses the effect of usb_buffer_map(). |
| 1258 | */ |
| 1259 | #if 0 |
| 1260 | void usb_buffer_unmap (struct urb *urb) |
| 1261 | { |
| 1262 | struct usb_bus *bus; |
| 1263 | struct device *controller; |
| 1264 | |
| 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; |
| 1271 | |
| 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); |
| 1282 | } |
| 1283 | urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP |
| 1284 | | URB_NO_SETUP_DMA_MAP); |
| 1285 | } |
| 1286 | #endif /* 0 */ |
| 1287 | |
| 1288 | /** |
| 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 |
| 1294 | * |
| 1295 | * Return value is either < 0 (indicating no buffers could be mapped), or |
| 1296 | * the number of DMA mapping array entries in the scatterlist. |
| 1297 | * |
| 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. |
| 1301 | * |
| 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. |
| 1307 | * |
| 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. |
| 1311 | * |
| 1312 | * Reverse the effect of this call with usb_buffer_unmap_sg(). |
| 1313 | */ |
| 1314 | int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe, |
| 1315 | struct scatterlist *sg, int nents) |
| 1316 | { |
| 1317 | struct usb_bus *bus; |
| 1318 | struct device *controller; |
| 1319 | |
| 1320 | if (!dev |
| 1321 | || usb_pipecontrol (pipe) |
| 1322 | || !(bus = dev->bus) |
| 1323 | || !(controller = bus->controller) |
| 1324 | || !controller->dma_mask) |
| 1325 | return -1; |
| 1326 | |
| 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); |
| 1330 | } |
| 1331 | |
| 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 |
| 1336 | */ |
| 1337 | #if 0 |
| 1338 | |
| 1339 | /** |
| 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 |
| 1345 | * |
| 1346 | * Use this when you are re-using a scatterlist's data buffers for |
| 1347 | * another USB request. |
| 1348 | */ |
| 1349 | void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe, |
| 1350 | struct scatterlist *sg, int n_hw_ents) |
| 1351 | { |
| 1352 | struct usb_bus *bus; |
| 1353 | struct device *controller; |
| 1354 | |
| 1355 | if (!dev |
| 1356 | || !(bus = dev->bus) |
| 1357 | || !(controller = bus->controller) |
| 1358 | || !controller->dma_mask) |
| 1359 | return; |
| 1360 | |
| 1361 | dma_sync_sg (controller, sg, n_hw_ents, |
| 1362 | usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 1363 | } |
| 1364 | #endif |
| 1365 | |
| 1366 | /** |
| 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 |
| 1372 | * |
| 1373 | * Reverses the effect of usb_buffer_map_sg(). |
| 1374 | */ |
| 1375 | void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe, |
| 1376 | struct scatterlist *sg, int n_hw_ents) |
| 1377 | { |
| 1378 | struct usb_bus *bus; |
| 1379 | struct device *controller; |
| 1380 | |
| 1381 | if (!dev |
| 1382 | || !(bus = dev->bus) |
| 1383 | || !(controller = bus->controller) |
| 1384 | || !controller->dma_mask) |
| 1385 | return; |
| 1386 | |
| 1387 | dma_unmap_sg (controller, sg, n_hw_ents, |
| 1388 | usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 1389 | } |
| 1390 | |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 1391 | static int verify_suspended(struct device *dev, void *unused) |
| 1392 | { |
| 1393 | return (dev->power.power_state.event == PM_EVENT_ON) ? -EBUSY : 0; |
| 1394 | } |
| 1395 | |
David Brownell | 27d72e8 | 2005-04-18 17:39:22 -0700 | [diff] [blame] | 1396 | static int usb_generic_suspend(struct device *dev, pm_message_t message) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | { |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1398 | struct usb_interface *intf; |
| 1399 | struct usb_driver *driver; |
| 1400 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 1402 | /* USB devices enter SUSPEND state through their hubs, but can be |
| 1403 | * marked for FREEZE as soon as their children are already idled. |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1404 | * But those semantics are useless, so we equate the two (sigh). |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 1405 | */ |
| 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; |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 1413 | return usb_suspend_device (to_usb_device(dev)); |
| 1414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | |
| 1416 | if ((dev->driver == NULL) || |
| 1417 | (dev->driver_data == &usb_generic_driver_data)) |
| 1418 | return 0; |
| 1419 | |
| 1420 | intf = to_usb_interface(dev); |
| 1421 | driver = to_usb_driver(dev->driver); |
| 1422 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1423 | /* with no hardware, USB interfaces only use FREEZE and ON states */ |
| 1424 | if (!is_active(intf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | return 0; |
| 1426 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 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; |
| 1437 | } |
| 1438 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | static int usb_generic_resume(struct device *dev) |
| 1442 | { |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1443 | struct usb_interface *intf; |
| 1444 | struct usb_driver *driver; |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1445 | struct usb_device *udev; |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1446 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1448 | if (dev->power.power_state.event == PM_EVENT_ON) |
| 1449 | return 0; |
| 1450 | |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1451 | /* mark things as "on" immediately, no matter what errors crop up */ |
| 1452 | dev->power.power_state.event = PM_EVENT_ON; |
| 1453 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 1454 | /* devices resume through their hubs */ |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1455 | if (dev->driver == &usb_generic_driver) { |
| 1456 | udev = to_usb_device(dev); |
| 1457 | if (udev->state == USB_STATE_NOTATTACHED) |
| 1458 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | return usb_resume_device (to_usb_device(dev)); |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1460 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | |
| 1462 | if ((dev->driver == NULL) || |
| 1463 | (dev->driver_data == &usb_generic_driver_data)) |
| 1464 | return 0; |
| 1465 | |
| 1466 | intf = to_usb_interface(dev); |
| 1467 | driver = to_usb_driver(dev->driver); |
| 1468 | |
David Brownell | 979d519 | 2005-09-22 22:32:24 -0700 | [diff] [blame] | 1469 | udev = interface_to_usbdev(intf); |
| 1470 | if (udev->state == USB_STATE_NOTATTACHED) |
| 1471 | return 0; |
| 1472 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 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) |
| 1476 | */ |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 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); |
| 1482 | } |
| 1483 | } else |
| 1484 | dev_warn(dev, "no %s?\n", "resume"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 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, |
| 1494 | }; |
| 1495 | |
| 1496 | #ifndef MODULE |
| 1497 | |
| 1498 | static int __init usb_setup_disable(char *str) |
| 1499 | { |
| 1500 | nousb = 1; |
| 1501 | return 1; |
| 1502 | } |
| 1503 | |
| 1504 | /* format to disable USB on kernel command line is: nousb */ |
| 1505 | __setup("nousb", usb_setup_disable); |
| 1506 | |
| 1507 | #endif |
| 1508 | |
| 1509 | /* |
| 1510 | * for external read access to <nousb> |
| 1511 | */ |
| 1512 | int usb_disabled(void) |
| 1513 | { |
| 1514 | return nousb; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * Init |
| 1519 | */ |
| 1520 | static int __init usb_init(void) |
| 1521 | { |
| 1522 | int retval; |
| 1523 | if (nousb) { |
| 1524 | pr_info ("%s: USB support disabled\n", usbcore_name); |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
| 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; |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | retval = driver_register(&usb_generic_driver); |
| 1550 | if (!retval) |
| 1551 | goto out; |
| 1552 | |
| 1553 | usb_hub_cleanup(); |
| 1554 | hub_init_failed: |
| 1555 | usbfs_cleanup(); |
| 1556 | fs_init_failed: |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1557 | usbdev_cleanup(); |
| 1558 | usbdevice_init_failed: |
| 1559 | usb_deregister(&usbfs_driver); |
| 1560 | driver_register_failed: |
| 1561 | usb_major_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | major_init_failed: |
| 1563 | usb_host_cleanup(); |
| 1564 | host_init_failed: |
| 1565 | bus_unregister(&usb_bus_type); |
| 1566 | out: |
| 1567 | return retval; |
| 1568 | } |
| 1569 | |
| 1570 | /* |
| 1571 | * Cleanup |
| 1572 | */ |
| 1573 | static void __exit usb_exit(void) |
| 1574 | { |
| 1575 | /* This will matter if shutdown/reboot does exitcalls. */ |
| 1576 | if (nousb) |
| 1577 | return; |
| 1578 | |
| 1579 | driver_unregister(&usb_generic_driver); |
| 1580 | usb_major_cleanup(); |
| 1581 | usbfs_cleanup(); |
Kay Sievers | fbf82fd | 2005-07-31 01:05:53 +0200 | [diff] [blame] | 1582 | usb_deregister(&usbfs_driver); |
| 1583 | usbdev_cleanup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | usb_hub_cleanup(); |
| 1585 | usb_host_cleanup(); |
| 1586 | bus_unregister(&usb_bus_type); |
| 1587 | } |
| 1588 | |
| 1589 | subsys_initcall(usb_init); |
| 1590 | module_exit(usb_exit); |
| 1591 | |
| 1592 | /* |
| 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. |
| 1596 | */ |
| 1597 | |
| 1598 | EXPORT_SYMBOL(usb_register); |
| 1599 | EXPORT_SYMBOL(usb_deregister); |
| 1600 | EXPORT_SYMBOL(usb_disabled); |
| 1601 | |
brian@murphy.dk | a3fdf4e | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 1602 | EXPORT_SYMBOL_GPL(usb_get_intf); |
| 1603 | EXPORT_SYMBOL_GPL(usb_put_intf); |
| 1604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 1609 | |
| 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); |
| 1614 | |
| 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); |
| 1621 | |
| 1622 | EXPORT_SYMBOL(usb_reset_device); |
| 1623 | EXPORT_SYMBOL(usb_disconnect); |
| 1624 | |
| 1625 | EXPORT_SYMBOL(__usb_get_extra_descriptor); |
| 1626 | |
| 1627 | EXPORT_SYMBOL(usb_find_device); |
| 1628 | EXPORT_SYMBOL(usb_get_current_frame_number); |
| 1629 | |
| 1630 | EXPORT_SYMBOL (usb_buffer_alloc); |
| 1631 | EXPORT_SYMBOL (usb_buffer_free); |
| 1632 | |
| 1633 | #if 0 |
| 1634 | EXPORT_SYMBOL (usb_buffer_map); |
| 1635 | EXPORT_SYMBOL (usb_buffer_dmasync); |
| 1636 | EXPORT_SYMBOL (usb_buffer_unmap); |
| 1637 | #endif |
| 1638 | |
| 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); |
| 1644 | |
| 1645 | MODULE_LICENSE("GPL"); |