1 /* 2 * drivers/usb/driver.c - most of the driver model stuff for usb 3 * 4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de> 5 * 6 * based on drivers/usb/usb.c which had the following copyrights: 7 * (C) Copyright Linus Torvalds 1999 8 * (C) Copyright Johannes Erdfelt 1999-2001 9 * (C) Copyright Andreas Gal 1999 10 * (C) Copyright Gregory P. Smith 1999 11 * (C) Copyright Deti Fliegl 1999 (new USB architecture) 12 * (C) Copyright Randy Dunlap 2000 13 * (C) Copyright David Brownell 2000-2004 14 * (C) Copyright Yggdrasil Computing, Inc. 2000 15 * (usb_device_id matching changes by Adam J. Richter) 16 * (C) Copyright Greg Kroah-Hartman 2002-2003 17 * 18 * NOTE! This is not actually a driver at all, rather this is 19 * just a collection of helper routines that implement the 20 * matching, probing, releasing, suspending and resuming for 21 * real drivers. 22 * 23 */ 24 25 #include <linux/device.h> 26 #include <linux/slab.h> 27 #include <linux/usb.h> 28 #include <linux/usb/quirks.h> 29 #include <linux/pm_runtime.h> 30 #include "hcd.h" 31 #include "usb.h" 32 33 34 #ifdef CONFIG_HOTPLUG 35 36 /* 37 * Adds a new dynamic USBdevice ID to this driver, 38 * and cause the driver to probe for all devices again. 39 */ 40 ssize_t usb_store_new_id(struct usb_dynids *dynids, 41 struct device_driver *driver, 42 const char *buf, size_t count) 43 { 44 struct usb_dynid *dynid; 45 u32 idVendor = 0; 46 u32 idProduct = 0; 47 int fields = 0; 48 int retval = 0; 49 50 fields = sscanf(buf, "%x %x", &idVendor, &idProduct); 51 if (fields < 2) 52 return -EINVAL; 53 54 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); 55 if (!dynid) 56 return -ENOMEM; 57 58 INIT_LIST_HEAD(&dynid->node); 59 dynid->id.idVendor = idVendor; 60 dynid->id.idProduct = idProduct; 61 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE; 62 63 spin_lock(&dynids->lock); 64 list_add_tail(&dynid->node, &dynids->list); 65 spin_unlock(&dynids->lock); 66 67 if (get_driver(driver)) { 68 retval = driver_attach(driver); 69 put_driver(driver); 70 } 71 72 if (retval) 73 return retval; 74 return count; 75 } 76 EXPORT_SYMBOL_GPL(usb_store_new_id); 77 78 static ssize_t store_new_id(struct device_driver *driver, 79 const char *buf, size_t count) 80 { 81 struct usb_driver *usb_drv = to_usb_driver(driver); 82 83 return usb_store_new_id(&usb_drv->dynids, driver, buf, count); 84 } 85 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id); 86 87 /** 88 * store_remove_id - remove a USB device ID from this driver 89 * @driver: target device driver 90 * @buf: buffer for scanning device ID data 91 * @count: input size 92 * 93 * Removes a dynamic usb device ID from this driver. 94 */ 95 static ssize_t 96 store_remove_id(struct device_driver *driver, const char *buf, size_t count) 97 { 98 struct usb_dynid *dynid, *n; 99 struct usb_driver *usb_driver = to_usb_driver(driver); 100 u32 idVendor = 0; 101 u32 idProduct = 0; 102 int fields = 0; 103 int retval = 0; 104 105 fields = sscanf(buf, "%x %x", &idVendor, &idProduct); 106 if (fields < 2) 107 return -EINVAL; 108 109 spin_lock(&usb_driver->dynids.lock); 110 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) { 111 struct usb_device_id *id = &dynid->id; 112 if ((id->idVendor == idVendor) && 113 (id->idProduct == idProduct)) { 114 list_del(&dynid->node); 115 kfree(dynid); 116 retval = 0; 117 break; 118 } 119 } 120 spin_unlock(&usb_driver->dynids.lock); 121 122 if (retval) 123 return retval; 124 return count; 125 } 126 static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id); 127 128 static int usb_create_newid_file(struct usb_driver *usb_drv) 129 { 130 int error = 0; 131 132 if (usb_drv->no_dynamic_id) 133 goto exit; 134 135 if (usb_drv->probe != NULL) 136 error = driver_create_file(&usb_drv->drvwrap.driver, 137 &driver_attr_new_id); 138 exit: 139 return error; 140 } 141 142 static void usb_remove_newid_file(struct usb_driver *usb_drv) 143 { 144 if (usb_drv->no_dynamic_id) 145 return; 146 147 if (usb_drv->probe != NULL) 148 driver_remove_file(&usb_drv->drvwrap.driver, 149 &driver_attr_new_id); 150 } 151 152 static int 153 usb_create_removeid_file(struct usb_driver *drv) 154 { 155 int error = 0; 156 if (drv->probe != NULL) 157 error = driver_create_file(&drv->drvwrap.driver, 158 &driver_attr_remove_id); 159 return error; 160 } 161 162 static void usb_remove_removeid_file(struct usb_driver *drv) 163 { 164 driver_remove_file(&drv->drvwrap.driver, &driver_attr_remove_id); 165 } 166 167 static void usb_free_dynids(struct usb_driver *usb_drv) 168 { 169 struct usb_dynid *dynid, *n; 170 171 spin_lock(&usb_drv->dynids.lock); 172 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) { 173 list_del(&dynid->node); 174 kfree(dynid); 175 } 176 spin_unlock(&usb_drv->dynids.lock); 177 } 178 #else 179 static inline int usb_create_newid_file(struct usb_driver *usb_drv) 180 { 181 return 0; 182 } 183 184 static void usb_remove_newid_file(struct usb_driver *usb_drv) 185 { 186 } 187 188 static int 189 usb_create_removeid_file(struct usb_driver *drv) 190 { 191 return 0; 192 } 193 194 static void usb_remove_removeid_file(struct usb_driver *drv) 195 { 196 } 197 198 static inline void usb_free_dynids(struct usb_driver *usb_drv) 199 { 200 } 201 #endif 202 203 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf, 204 struct usb_driver *drv) 205 { 206 struct usb_dynid *dynid; 207 208 spin_lock(&drv->dynids.lock); 209 list_for_each_entry(dynid, &drv->dynids.list, node) { 210 if (usb_match_one_id(intf, &dynid->id)) { 211 spin_unlock(&drv->dynids.lock); 212 return &dynid->id; 213 } 214 } 215 spin_unlock(&drv->dynids.lock); 216 return NULL; 217 } 218 219 220 /* called from driver core with dev locked */ 221 static int usb_probe_device(struct device *dev) 222 { 223 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 224 struct usb_device *udev = to_usb_device(dev); 225 int error = 0; 226 227 dev_dbg(dev, "%s\n", __func__); 228 229 /* TODO: Add real matching code */ 230 231 /* The device should always appear to be in use 232 * unless the driver suports autosuspend. 233 */ 234 if (!udriver->supports_autosuspend) 235 error = usb_autoresume_device(udev); 236 237 if (!error) 238 error = udriver->probe(udev); 239 return error; 240 } 241 242 /* called from driver core with dev locked */ 243 static int usb_unbind_device(struct device *dev) 244 { 245 struct usb_device *udev = to_usb_device(dev); 246 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 247 248 udriver->disconnect(udev); 249 if (!udriver->supports_autosuspend) 250 usb_autosuspend_device(udev); 251 return 0; 252 } 253 254 /* 255 * Cancel any pending scheduled resets 256 * 257 * [see usb_queue_reset_device()] 258 * 259 * Called after unconfiguring / when releasing interfaces. See 260 * comments in __usb_queue_reset_device() regarding 261 * udev->reset_running. 262 */ 263 static void usb_cancel_queued_reset(struct usb_interface *iface) 264 { 265 if (iface->reset_running == 0) 266 cancel_work_sync(&iface->reset_ws); 267 } 268 269 /* called from driver core with dev locked */ 270 static int usb_probe_interface(struct device *dev) 271 { 272 struct usb_driver *driver = to_usb_driver(dev->driver); 273 struct usb_interface *intf = to_usb_interface(dev); 274 struct usb_device *udev = interface_to_usbdev(intf); 275 const struct usb_device_id *id; 276 int error = -ENODEV; 277 278 dev_dbg(dev, "%s\n", __func__); 279 280 intf->needs_binding = 0; 281 282 if (usb_device_is_owned(udev)) 283 return error; 284 285 if (udev->authorized == 0) { 286 dev_err(&intf->dev, "Device is not authorized for usage\n"); 287 return error; 288 } 289 290 id = usb_match_id(intf, driver->id_table); 291 if (!id) 292 id = usb_match_dynamic_id(intf, driver); 293 if (!id) 294 return error; 295 296 dev_dbg(dev, "%s - got id\n", __func__); 297 298 error = usb_autoresume_device(udev); 299 if (error) 300 return error; 301 302 intf->condition = USB_INTERFACE_BINDING; 303 304 /* Probed interfaces are initially active. They are 305 * runtime-PM-enabled only if the driver has autosuspend support. 306 * They are sensitive to their children's power states. 307 */ 308 pm_runtime_set_active(dev); 309 pm_suspend_ignore_children(dev, false); 310 if (driver->supports_autosuspend) 311 pm_runtime_enable(dev); 312 313 /* Carry out a deferred switch to altsetting 0 */ 314 if (intf->needs_altsetting0) { 315 error = usb_set_interface(udev, intf->altsetting[0]. 316 desc.bInterfaceNumber, 0); 317 if (error < 0) 318 goto err; 319 intf->needs_altsetting0 = 0; 320 } 321 322 error = driver->probe(intf, id); 323 if (error) 324 goto err; 325 326 intf->condition = USB_INTERFACE_BOUND; 327 usb_autosuspend_device(udev); 328 return error; 329 330 err: 331 intf->needs_remote_wakeup = 0; 332 intf->condition = USB_INTERFACE_UNBOUND; 333 usb_cancel_queued_reset(intf); 334 335 /* Unbound interfaces are always runtime-PM-disabled and -suspended */ 336 pm_runtime_disable(dev); 337 pm_runtime_set_suspended(dev); 338 339 usb_autosuspend_device(udev); 340 return error; 341 } 342 343 /* called from driver core with dev locked */ 344 static int usb_unbind_interface(struct device *dev) 345 { 346 struct usb_driver *driver = to_usb_driver(dev->driver); 347 struct usb_interface *intf = to_usb_interface(dev); 348 struct usb_device *udev; 349 int error, r; 350 351 intf->condition = USB_INTERFACE_UNBINDING; 352 353 /* Autoresume for set_interface call below */ 354 udev = interface_to_usbdev(intf); 355 error = usb_autoresume_device(udev); 356 357 /* Terminate all URBs for this interface unless the driver 358 * supports "soft" unbinding. 359 */ 360 if (!driver->soft_unbind) 361 usb_disable_interface(udev, intf, false); 362 363 driver->disconnect(intf); 364 usb_cancel_queued_reset(intf); 365 366 /* Reset other interface state. 367 * We cannot do a Set-Interface if the device is suspended or 368 * if it is prepared for a system sleep (since installing a new 369 * altsetting means creating new endpoint device entries). 370 * When either of these happens, defer the Set-Interface. 371 */ 372 if (intf->cur_altsetting->desc.bAlternateSetting == 0) { 373 /* Already in altsetting 0 so skip Set-Interface. 374 * Just re-enable it without affecting the endpoint toggles. 375 */ 376 usb_enable_interface(udev, intf, false); 377 } else if (!error && intf->dev.power.status == DPM_ON) { 378 r = usb_set_interface(udev, intf->altsetting[0]. 379 desc.bInterfaceNumber, 0); 380 if (r < 0) 381 intf->needs_altsetting0 = 1; 382 } else { 383 intf->needs_altsetting0 = 1; 384 } 385 usb_set_intfdata(intf, NULL); 386 387 intf->condition = USB_INTERFACE_UNBOUND; 388 intf->needs_remote_wakeup = 0; 389 390 /* Unbound interfaces are always runtime-PM-disabled and -suspended */ 391 pm_runtime_disable(dev); 392 pm_runtime_set_suspended(dev); 393 394 /* Undo any residual pm_autopm_get_interface_* calls */ 395 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r) 396 usb_autopm_put_interface_no_suspend(intf); 397 atomic_set(&intf->pm_usage_cnt, 0); 398 399 if (!error) 400 usb_autosuspend_device(udev); 401 402 return 0; 403 } 404 405 /** 406 * usb_driver_claim_interface - bind a driver to an interface 407 * @driver: the driver to be bound 408 * @iface: the interface to which it will be bound; must be in the 409 * usb device's active configuration 410 * @priv: driver data associated with that interface 411 * 412 * This is used by usb device drivers that need to claim more than one 413 * interface on a device when probing (audio and acm are current examples). 414 * No device driver should directly modify internal usb_interface or 415 * usb_device structure members. 416 * 417 * Few drivers should need to use this routine, since the most natural 418 * way to bind to an interface is to return the private data from 419 * the driver's probe() method. 420 * 421 * Callers must own the device lock, so driver probe() entries don't need 422 * extra locking, but other call contexts may need to explicitly claim that 423 * lock. 424 */ 425 int usb_driver_claim_interface(struct usb_driver *driver, 426 struct usb_interface *iface, void *priv) 427 { 428 struct device *dev = &iface->dev; 429 int retval = 0; 430 431 if (dev->driver) 432 return -EBUSY; 433 434 dev->driver = &driver->drvwrap.driver; 435 usb_set_intfdata(iface, priv); 436 iface->needs_binding = 0; 437 438 iface->condition = USB_INTERFACE_BOUND; 439 440 /* Claimed interfaces are initially inactive (suspended). They are 441 * runtime-PM-enabled only if the driver has autosuspend support. 442 * They are sensitive to their children's power states. 443 */ 444 pm_runtime_set_suspended(dev); 445 pm_suspend_ignore_children(dev, false); 446 if (driver->supports_autosuspend) 447 pm_runtime_enable(dev); 448 449 /* if interface was already added, bind now; else let 450 * the future device_add() bind it, bypassing probe() 451 */ 452 if (device_is_registered(dev)) 453 retval = device_bind_driver(dev); 454 455 return retval; 456 } 457 EXPORT_SYMBOL_GPL(usb_driver_claim_interface); 458 459 /** 460 * usb_driver_release_interface - unbind a driver from an interface 461 * @driver: the driver to be unbound 462 * @iface: the interface from which it will be unbound 463 * 464 * This can be used by drivers to release an interface without waiting 465 * for their disconnect() methods to be called. In typical cases this 466 * also causes the driver disconnect() method to be called. 467 * 468 * This call is synchronous, and may not be used in an interrupt context. 469 * Callers must own the device lock, so driver disconnect() entries don't 470 * need extra locking, but other call contexts may need to explicitly claim 471 * that lock. 472 */ 473 void usb_driver_release_interface(struct usb_driver *driver, 474 struct usb_interface *iface) 475 { 476 struct device *dev = &iface->dev; 477 478 /* this should never happen, don't release something that's not ours */ 479 if (!dev->driver || dev->driver != &driver->drvwrap.driver) 480 return; 481 482 /* don't release from within disconnect() */ 483 if (iface->condition != USB_INTERFACE_BOUND) 484 return; 485 iface->condition = USB_INTERFACE_UNBINDING; 486 487 /* Release via the driver core only if the interface 488 * has already been registered 489 */ 490 if (device_is_registered(dev)) { 491 device_release_driver(dev); 492 } else { 493 device_lock(dev); 494 usb_unbind_interface(dev); 495 dev->driver = NULL; 496 device_unlock(dev); 497 } 498 } 499 EXPORT_SYMBOL_GPL(usb_driver_release_interface); 500 501 /* returns 0 if no match, 1 if match */ 502 int usb_match_device(struct usb_device *dev, const struct usb_device_id *id) 503 { 504 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 505 id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) 506 return 0; 507 508 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && 509 id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) 510 return 0; 511 512 /* No need to test id->bcdDevice_lo != 0, since 0 is never 513 greater than any unsigned number. */ 514 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && 515 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) 516 return 0; 517 518 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && 519 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) 520 return 0; 521 522 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && 523 (id->bDeviceClass != dev->descriptor.bDeviceClass)) 524 return 0; 525 526 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && 527 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass)) 528 return 0; 529 530 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && 531 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) 532 return 0; 533 534 return 1; 535 } 536 537 /* returns 0 if no match, 1 if match */ 538 int usb_match_one_id(struct usb_interface *interface, 539 const struct usb_device_id *id) 540 { 541 struct usb_host_interface *intf; 542 struct usb_device *dev; 543 544 /* proc_connectinfo in devio.c may call us with id == NULL. */ 545 if (id == NULL) 546 return 0; 547 548 intf = interface->cur_altsetting; 549 dev = interface_to_usbdev(interface); 550 551 if (!usb_match_device(dev, id)) 552 return 0; 553 554 /* The interface class, subclass, and protocol should never be 555 * checked for a match if the device class is Vendor Specific, 556 * unless the match record specifies the Vendor ID. */ 557 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC && 558 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 559 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | 560 USB_DEVICE_ID_MATCH_INT_SUBCLASS | 561 USB_DEVICE_ID_MATCH_INT_PROTOCOL))) 562 return 0; 563 564 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && 565 (id->bInterfaceClass != intf->desc.bInterfaceClass)) 566 return 0; 567 568 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && 569 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) 570 return 0; 571 572 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && 573 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) 574 return 0; 575 576 return 1; 577 } 578 EXPORT_SYMBOL_GPL(usb_match_one_id); 579 580 /** 581 * usb_match_id - find first usb_device_id matching device or interface 582 * @interface: the interface of interest 583 * @id: array of usb_device_id structures, terminated by zero entry 584 * 585 * usb_match_id searches an array of usb_device_id's and returns 586 * the first one matching the device or interface, or null. 587 * This is used when binding (or rebinding) a driver to an interface. 588 * Most USB device drivers will use this indirectly, through the usb core, 589 * but some layered driver frameworks use it directly. 590 * These device tables are exported with MODULE_DEVICE_TABLE, through 591 * modutils, to support the driver loading functionality of USB hotplugging. 592 * 593 * What Matches: 594 * 595 * The "match_flags" element in a usb_device_id controls which 596 * members are used. If the corresponding bit is set, the 597 * value in the device_id must match its corresponding member 598 * in the device or interface descriptor, or else the device_id 599 * does not match. 600 * 601 * "driver_info" is normally used only by device drivers, 602 * but you can create a wildcard "matches anything" usb_device_id 603 * as a driver's "modules.usbmap" entry if you provide an id with 604 * only a nonzero "driver_info" field. If you do this, the USB device 605 * driver's probe() routine should use additional intelligence to 606 * decide whether to bind to the specified interface. 607 * 608 * What Makes Good usb_device_id Tables: 609 * 610 * The match algorithm is very simple, so that intelligence in 611 * driver selection must come from smart driver id records. 612 * Unless you have good reasons to use another selection policy, 613 * provide match elements only in related groups, and order match 614 * specifiers from specific to general. Use the macros provided 615 * for that purpose if you can. 616 * 617 * The most specific match specifiers use device descriptor 618 * data. These are commonly used with product-specific matches; 619 * the USB_DEVICE macro lets you provide vendor and product IDs, 620 * and you can also match against ranges of product revisions. 621 * These are widely used for devices with application or vendor 622 * specific bDeviceClass values. 623 * 624 * Matches based on device class/subclass/protocol specifications 625 * are slightly more general; use the USB_DEVICE_INFO macro, or 626 * its siblings. These are used with single-function devices 627 * where bDeviceClass doesn't specify that each interface has 628 * its own class. 629 * 630 * Matches based on interface class/subclass/protocol are the 631 * most general; they let drivers bind to any interface on a 632 * multiple-function device. Use the USB_INTERFACE_INFO 633 * macro, or its siblings, to match class-per-interface style 634 * devices (as recorded in bInterfaceClass). 635 * 636 * Note that an entry created by USB_INTERFACE_INFO won't match 637 * any interface if the device class is set to Vendor-Specific. 638 * This is deliberate; according to the USB spec the meanings of 639 * the interface class/subclass/protocol for these devices are also 640 * vendor-specific, and hence matching against a standard product 641 * class wouldn't work anyway. If you really want to use an 642 * interface-based match for such a device, create a match record 643 * that also specifies the vendor ID. (Unforunately there isn't a 644 * standard macro for creating records like this.) 645 * 646 * Within those groups, remember that not all combinations are 647 * meaningful. For example, don't give a product version range 648 * without vendor and product IDs; or specify a protocol without 649 * its associated class and subclass. 650 */ 651 const struct usb_device_id *usb_match_id(struct usb_interface *interface, 652 const struct usb_device_id *id) 653 { 654 /* proc_connectinfo in devio.c may call us with id == NULL. */ 655 if (id == NULL) 656 return NULL; 657 658 /* It is important to check that id->driver_info is nonzero, 659 since an entry that is all zeroes except for a nonzero 660 id->driver_info is the way to create an entry that 661 indicates that the driver want to examine every 662 device and interface. */ 663 for (; id->idVendor || id->idProduct || id->bDeviceClass || 664 id->bInterfaceClass || id->driver_info; id++) { 665 if (usb_match_one_id(interface, id)) 666 return id; 667 } 668 669 return NULL; 670 } 671 EXPORT_SYMBOL_GPL(usb_match_id); 672 673 static int usb_device_match(struct device *dev, struct device_driver *drv) 674 { 675 /* devices and interfaces are handled separately */ 676 if (is_usb_device(dev)) { 677 678 /* interface drivers never match devices */ 679 if (!is_usb_device_driver(drv)) 680 return 0; 681 682 /* TODO: Add real matching code */ 683 return 1; 684 685 } else if (is_usb_interface(dev)) { 686 struct usb_interface *intf; 687 struct usb_driver *usb_drv; 688 const struct usb_device_id *id; 689 690 /* device drivers never match interfaces */ 691 if (is_usb_device_driver(drv)) 692 return 0; 693 694 intf = to_usb_interface(dev); 695 usb_drv = to_usb_driver(drv); 696 697 id = usb_match_id(intf, usb_drv->id_table); 698 if (id) 699 return 1; 700 701 id = usb_match_dynamic_id(intf, usb_drv); 702 if (id) 703 return 1; 704 } 705 706 return 0; 707 } 708 709 #ifdef CONFIG_HOTPLUG 710 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) 711 { 712 struct usb_device *usb_dev; 713 714 if (is_usb_device(dev)) { 715 usb_dev = to_usb_device(dev); 716 } else if (is_usb_interface(dev)) { 717 struct usb_interface *intf = to_usb_interface(dev); 718 719 usb_dev = interface_to_usbdev(intf); 720 } else { 721 return 0; 722 } 723 724 if (usb_dev->devnum < 0) { 725 /* driver is often null here; dev_dbg() would oops */ 726 pr_debug("usb %s: already deleted?\n", dev_name(dev)); 727 return -ENODEV; 728 } 729 if (!usb_dev->bus) { 730 pr_debug("usb %s: bus removed?\n", dev_name(dev)); 731 return -ENODEV; 732 } 733 734 #ifdef CONFIG_USB_DEVICEFS 735 /* If this is available, userspace programs can directly read 736 * all the device descriptors we don't tell them about. Or 737 * act as usermode drivers. 738 */ 739 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d", 740 usb_dev->bus->busnum, usb_dev->devnum)) 741 return -ENOMEM; 742 #endif 743 744 /* per-device configurations are common */ 745 if (add_uevent_var(env, "PRODUCT=%x/%x/%x", 746 le16_to_cpu(usb_dev->descriptor.idVendor), 747 le16_to_cpu(usb_dev->descriptor.idProduct), 748 le16_to_cpu(usb_dev->descriptor.bcdDevice))) 749 return -ENOMEM; 750 751 /* class-based driver binding models */ 752 if (add_uevent_var(env, "TYPE=%d/%d/%d", 753 usb_dev->descriptor.bDeviceClass, 754 usb_dev->descriptor.bDeviceSubClass, 755 usb_dev->descriptor.bDeviceProtocol)) 756 return -ENOMEM; 757 758 return 0; 759 } 760 761 #else 762 763 static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) 764 { 765 return -ENODEV; 766 } 767 #endif /* CONFIG_HOTPLUG */ 768 769 /** 770 * usb_register_device_driver - register a USB device (not interface) driver 771 * @new_udriver: USB operations for the device driver 772 * @owner: module owner of this driver. 773 * 774 * Registers a USB device driver with the USB core. The list of 775 * unattached devices will be rescanned whenever a new driver is 776 * added, allowing the new driver to attach to any recognized devices. 777 * Returns a negative error code on failure and 0 on success. 778 */ 779 int usb_register_device_driver(struct usb_device_driver *new_udriver, 780 struct module *owner) 781 { 782 int retval = 0; 783 784 if (usb_disabled()) 785 return -ENODEV; 786 787 new_udriver->drvwrap.for_devices = 1; 788 new_udriver->drvwrap.driver.name = (char *) new_udriver->name; 789 new_udriver->drvwrap.driver.bus = &usb_bus_type; 790 new_udriver->drvwrap.driver.probe = usb_probe_device; 791 new_udriver->drvwrap.driver.remove = usb_unbind_device; 792 new_udriver->drvwrap.driver.owner = owner; 793 794 retval = driver_register(&new_udriver->drvwrap.driver); 795 796 if (!retval) { 797 pr_info("%s: registered new device driver %s\n", 798 usbcore_name, new_udriver->name); 799 usbfs_update_special(); 800 } else { 801 printk(KERN_ERR "%s: error %d registering device " 802 " driver %s\n", 803 usbcore_name, retval, new_udriver->name); 804 } 805 806 return retval; 807 } 808 EXPORT_SYMBOL_GPL(usb_register_device_driver); 809 810 /** 811 * usb_deregister_device_driver - unregister a USB device (not interface) driver 812 * @udriver: USB operations of the device driver to unregister 813 * Context: must be able to sleep 814 * 815 * Unlinks the specified driver from the internal USB driver list. 816 */ 817 void usb_deregister_device_driver(struct usb_device_driver *udriver) 818 { 819 pr_info("%s: deregistering device driver %s\n", 820 usbcore_name, udriver->name); 821 822 driver_unregister(&udriver->drvwrap.driver); 823 usbfs_update_special(); 824 } 825 EXPORT_SYMBOL_GPL(usb_deregister_device_driver); 826 827 /** 828 * usb_register_driver - register a USB interface driver 829 * @new_driver: USB operations for the interface driver 830 * @owner: module owner of this driver. 831 * @mod_name: module name string 832 * 833 * Registers a USB interface driver with the USB core. The list of 834 * unattached interfaces will be rescanned whenever a new driver is 835 * added, allowing the new driver to attach to any recognized interfaces. 836 * Returns a negative error code on failure and 0 on success. 837 * 838 * NOTE: if you want your driver to use the USB major number, you must call 839 * usb_register_dev() to enable that functionality. This function no longer 840 * takes care of that. 841 */ 842 int usb_register_driver(struct usb_driver *new_driver, struct module *owner, 843 const char *mod_name) 844 { 845 int retval = 0; 846 847 if (usb_disabled()) 848 return -ENODEV; 849 850 new_driver->drvwrap.for_devices = 0; 851 new_driver->drvwrap.driver.name = (char *) new_driver->name; 852 new_driver->drvwrap.driver.bus = &usb_bus_type; 853 new_driver->drvwrap.driver.probe = usb_probe_interface; 854 new_driver->drvwrap.driver.remove = usb_unbind_interface; 855 new_driver->drvwrap.driver.owner = owner; 856 new_driver->drvwrap.driver.mod_name = mod_name; 857 spin_lock_init(&new_driver->dynids.lock); 858 INIT_LIST_HEAD(&new_driver->dynids.list); 859 860 retval = driver_register(&new_driver->drvwrap.driver); 861 if (retval) 862 goto out; 863 864 usbfs_update_special(); 865 866 retval = usb_create_newid_file(new_driver); 867 if (retval) 868 goto out_newid; 869 870 retval = usb_create_removeid_file(new_driver); 871 if (retval) 872 goto out_removeid; 873 874 pr_info("%s: registered new interface driver %s\n", 875 usbcore_name, new_driver->name); 876 877 out: 878 return retval; 879 880 out_removeid: 881 usb_remove_newid_file(new_driver); 882 out_newid: 883 driver_unregister(&new_driver->drvwrap.driver); 884 885 printk(KERN_ERR "%s: error %d registering interface " 886 " driver %s\n", 887 usbcore_name, retval, new_driver->name); 888 goto out; 889 } 890 EXPORT_SYMBOL_GPL(usb_register_driver); 891 892 /** 893 * usb_deregister - unregister a USB interface driver 894 * @driver: USB operations of the interface driver to unregister 895 * Context: must be able to sleep 896 * 897 * Unlinks the specified driver from the internal USB driver list. 898 * 899 * NOTE: If you called usb_register_dev(), you still need to call 900 * usb_deregister_dev() to clean up your driver's allocated minor numbers, 901 * this * call will no longer do it for you. 902 */ 903 void usb_deregister(struct usb_driver *driver) 904 { 905 pr_info("%s: deregistering interface driver %s\n", 906 usbcore_name, driver->name); 907 908 usb_remove_removeid_file(driver); 909 usb_remove_newid_file(driver); 910 usb_free_dynids(driver); 911 driver_unregister(&driver->drvwrap.driver); 912 913 usbfs_update_special(); 914 } 915 EXPORT_SYMBOL_GPL(usb_deregister); 916 917 /* Forced unbinding of a USB interface driver, either because 918 * it doesn't support pre_reset/post_reset/reset_resume or 919 * because it doesn't support suspend/resume. 920 * 921 * The caller must hold @intf's device's lock, but not its pm_mutex 922 * and not @intf->dev.sem. 923 */ 924 void usb_forced_unbind_intf(struct usb_interface *intf) 925 { 926 struct usb_driver *driver = to_usb_driver(intf->dev.driver); 927 928 dev_dbg(&intf->dev, "forced unbind\n"); 929 usb_driver_release_interface(driver, intf); 930 931 /* Mark the interface for later rebinding */ 932 intf->needs_binding = 1; 933 } 934 935 /* Delayed forced unbinding of a USB interface driver and scan 936 * for rebinding. 937 * 938 * The caller must hold @intf's device's lock, but not its pm_mutex 939 * and not @intf->dev.sem. 940 * 941 * Note: Rebinds will be skipped if a system sleep transition is in 942 * progress and the PM "complete" callback hasn't occurred yet. 943 */ 944 void usb_rebind_intf(struct usb_interface *intf) 945 { 946 int rc; 947 948 /* Delayed unbind of an existing driver */ 949 if (intf->dev.driver) { 950 struct usb_driver *driver = 951 to_usb_driver(intf->dev.driver); 952 953 dev_dbg(&intf->dev, "forced unbind\n"); 954 usb_driver_release_interface(driver, intf); 955 } 956 957 /* Try to rebind the interface */ 958 if (intf->dev.power.status == DPM_ON) { 959 intf->needs_binding = 0; 960 rc = device_attach(&intf->dev); 961 if (rc < 0) 962 dev_warn(&intf->dev, "rebind failed: %d\n", rc); 963 } 964 } 965 966 #ifdef CONFIG_PM 967 968 #define DO_UNBIND 0 969 #define DO_REBIND 1 970 971 /* Unbind drivers for @udev's interfaces that don't support suspend/resume, 972 * or rebind interfaces that have been unbound, according to @action. 973 * 974 * The caller must hold @udev's device lock. 975 */ 976 static void do_unbind_rebind(struct usb_device *udev, int action) 977 { 978 struct usb_host_config *config; 979 int i; 980 struct usb_interface *intf; 981 struct usb_driver *drv; 982 983 config = udev->actconfig; 984 if (config) { 985 for (i = 0; i < config->desc.bNumInterfaces; ++i) { 986 intf = config->interface[i]; 987 switch (action) { 988 case DO_UNBIND: 989 if (intf->dev.driver) { 990 drv = to_usb_driver(intf->dev.driver); 991 if (!drv->suspend || !drv->resume) 992 usb_forced_unbind_intf(intf); 993 } 994 break; 995 case DO_REBIND: 996 if (intf->needs_binding) 997 usb_rebind_intf(intf); 998 break; 999 } 1000 } 1001 } 1002 } 1003 1004 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg) 1005 { 1006 struct usb_device_driver *udriver; 1007 int status = 0; 1008 1009 if (udev->state == USB_STATE_NOTATTACHED || 1010 udev->state == USB_STATE_SUSPENDED) 1011 goto done; 1012 1013 /* For devices that don't have a driver, we do a generic suspend. */ 1014 if (udev->dev.driver) 1015 udriver = to_usb_device_driver(udev->dev.driver); 1016 else { 1017 udev->do_remote_wakeup = 0; 1018 udriver = &usb_generic_driver; 1019 } 1020 status = udriver->suspend(udev, msg); 1021 1022 done: 1023 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); 1024 return status; 1025 } 1026 1027 static int usb_resume_device(struct usb_device *udev, pm_message_t msg) 1028 { 1029 struct usb_device_driver *udriver; 1030 int status = 0; 1031 1032 if (udev->state == USB_STATE_NOTATTACHED) 1033 goto done; 1034 1035 /* Can't resume it if it doesn't have a driver. */ 1036 if (udev->dev.driver == NULL) { 1037 status = -ENOTCONN; 1038 goto done; 1039 } 1040 1041 /* Non-root devices on a full/low-speed bus must wait for their 1042 * companion high-speed root hub, in case a handoff is needed. 1043 */ 1044 if (!(msg.event & PM_EVENT_AUTO) && udev->parent && 1045 udev->bus->hs_companion) 1046 device_pm_wait_for_dev(&udev->dev, 1047 &udev->bus->hs_companion->root_hub->dev); 1048 1049 if (udev->quirks & USB_QUIRK_RESET_RESUME) 1050 udev->reset_resume = 1; 1051 1052 udriver = to_usb_device_driver(udev->dev.driver); 1053 status = udriver->resume(udev, msg); 1054 1055 done: 1056 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); 1057 return status; 1058 } 1059 1060 static int usb_suspend_interface(struct usb_device *udev, 1061 struct usb_interface *intf, pm_message_t msg) 1062 { 1063 struct usb_driver *driver; 1064 int status = 0; 1065 1066 if (udev->state == USB_STATE_NOTATTACHED || 1067 intf->condition == USB_INTERFACE_UNBOUND) 1068 goto done; 1069 driver = to_usb_driver(intf->dev.driver); 1070 1071 if (driver->suspend) { 1072 status = driver->suspend(intf, msg); 1073 if (status && !(msg.event & PM_EVENT_AUTO)) 1074 dev_err(&intf->dev, "%s error %d\n", 1075 "suspend", status); 1076 } else { 1077 /* Later we will unbind the driver and reprobe */ 1078 intf->needs_binding = 1; 1079 dev_warn(&intf->dev, "no %s for driver %s?\n", 1080 "suspend", driver->name); 1081 } 1082 1083 done: 1084 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); 1085 return status; 1086 } 1087 1088 static int usb_resume_interface(struct usb_device *udev, 1089 struct usb_interface *intf, pm_message_t msg, int reset_resume) 1090 { 1091 struct usb_driver *driver; 1092 int status = 0; 1093 1094 if (udev->state == USB_STATE_NOTATTACHED) 1095 goto done; 1096 1097 /* Don't let autoresume interfere with unbinding */ 1098 if (intf->condition == USB_INTERFACE_UNBINDING) 1099 goto done; 1100 1101 /* Can't resume it if it doesn't have a driver. */ 1102 if (intf->condition == USB_INTERFACE_UNBOUND) { 1103 1104 /* Carry out a deferred switch to altsetting 0 */ 1105 if (intf->needs_altsetting0 && 1106 intf->dev.power.status == DPM_ON) { 1107 usb_set_interface(udev, intf->altsetting[0]. 1108 desc.bInterfaceNumber, 0); 1109 intf->needs_altsetting0 = 0; 1110 } 1111 goto done; 1112 } 1113 1114 /* Don't resume if the interface is marked for rebinding */ 1115 if (intf->needs_binding) 1116 goto done; 1117 driver = to_usb_driver(intf->dev.driver); 1118 1119 if (reset_resume) { 1120 if (driver->reset_resume) { 1121 status = driver->reset_resume(intf); 1122 if (status) 1123 dev_err(&intf->dev, "%s error %d\n", 1124 "reset_resume", status); 1125 } else { 1126 intf->needs_binding = 1; 1127 dev_warn(&intf->dev, "no %s for driver %s?\n", 1128 "reset_resume", driver->name); 1129 } 1130 } else { 1131 if (driver->resume) { 1132 status = driver->resume(intf); 1133 if (status) 1134 dev_err(&intf->dev, "%s error %d\n", 1135 "resume", status); 1136 } else { 1137 intf->needs_binding = 1; 1138 dev_warn(&intf->dev, "no %s for driver %s?\n", 1139 "resume", driver->name); 1140 } 1141 } 1142 1143 done: 1144 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); 1145 1146 /* Later we will unbind the driver and/or reprobe, if necessary */ 1147 return status; 1148 } 1149 1150 /** 1151 * usb_suspend_both - suspend a USB device and its interfaces 1152 * @udev: the usb_device to suspend 1153 * @msg: Power Management message describing this state transition 1154 * 1155 * This is the central routine for suspending USB devices. It calls the 1156 * suspend methods for all the interface drivers in @udev and then calls 1157 * the suspend method for @udev itself. If an error occurs at any stage, 1158 * all the interfaces which were suspended are resumed so that they remain 1159 * in the same state as the device. 1160 * 1161 * Autosuspend requests originating from a child device or an interface 1162 * driver may be made without the protection of @udev's device lock, but 1163 * all other suspend calls will hold the lock. Usbcore will insure that 1164 * method calls do not arrive during bind, unbind, or reset operations. 1165 * However drivers must be prepared to handle suspend calls arriving at 1166 * unpredictable times. 1167 * 1168 * This routine can run only in process context. 1169 */ 1170 static int usb_suspend_both(struct usb_device *udev, pm_message_t msg) 1171 { 1172 int status = 0; 1173 int i = 0, n = 0; 1174 struct usb_interface *intf; 1175 1176 if (udev->state == USB_STATE_NOTATTACHED || 1177 udev->state == USB_STATE_SUSPENDED) 1178 goto done; 1179 1180 /* Suspend all the interfaces and then udev itself */ 1181 if (udev->actconfig) { 1182 n = udev->actconfig->desc.bNumInterfaces; 1183 for (i = n - 1; i >= 0; --i) { 1184 intf = udev->actconfig->interface[i]; 1185 status = usb_suspend_interface(udev, intf, msg); 1186 if (status != 0) 1187 break; 1188 } 1189 } 1190 if (status == 0) 1191 status = usb_suspend_device(udev, msg); 1192 1193 /* If the suspend failed, resume interfaces that did get suspended */ 1194 if (status != 0) { 1195 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME); 1196 while (++i < n) { 1197 intf = udev->actconfig->interface[i]; 1198 usb_resume_interface(udev, intf, msg, 0); 1199 } 1200 1201 /* If the suspend succeeded then prevent any more URB submissions 1202 * and flush any outstanding URBs. 1203 */ 1204 } else { 1205 udev->can_submit = 0; 1206 for (i = 0; i < 16; ++i) { 1207 usb_hcd_flush_endpoint(udev, udev->ep_out[i]); 1208 usb_hcd_flush_endpoint(udev, udev->ep_in[i]); 1209 } 1210 } 1211 1212 done: 1213 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); 1214 return status; 1215 } 1216 1217 /** 1218 * usb_resume_both - resume a USB device and its interfaces 1219 * @udev: the usb_device to resume 1220 * @msg: Power Management message describing this state transition 1221 * 1222 * This is the central routine for resuming USB devices. It calls the 1223 * the resume method for @udev and then calls the resume methods for all 1224 * the interface drivers in @udev. 1225 * 1226 * Autoresume requests originating from a child device or an interface 1227 * driver may be made without the protection of @udev's device lock, but 1228 * all other resume calls will hold the lock. Usbcore will insure that 1229 * method calls do not arrive during bind, unbind, or reset operations. 1230 * However drivers must be prepared to handle resume calls arriving at 1231 * unpredictable times. 1232 * 1233 * This routine can run only in process context. 1234 */ 1235 static int usb_resume_both(struct usb_device *udev, pm_message_t msg) 1236 { 1237 int status = 0; 1238 int i; 1239 struct usb_interface *intf; 1240 1241 if (udev->state == USB_STATE_NOTATTACHED) { 1242 status = -ENODEV; 1243 goto done; 1244 } 1245 udev->can_submit = 1; 1246 1247 /* Resume the device */ 1248 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume) 1249 status = usb_resume_device(udev, msg); 1250 1251 /* Resume the interfaces */ 1252 if (status == 0 && udev->actconfig) { 1253 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1254 intf = udev->actconfig->interface[i]; 1255 usb_resume_interface(udev, intf, msg, 1256 udev->reset_resume); 1257 } 1258 } 1259 1260 done: 1261 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); 1262 if (!status) 1263 udev->reset_resume = 0; 1264 return status; 1265 } 1266 1267 static void choose_wakeup(struct usb_device *udev, pm_message_t msg) 1268 { 1269 int w, i; 1270 struct usb_interface *intf; 1271 1272 /* Remote wakeup is needed only when we actually go to sleep. 1273 * For things like FREEZE and QUIESCE, if the device is already 1274 * autosuspended then its current wakeup setting is okay. 1275 */ 1276 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) { 1277 if (udev->state != USB_STATE_SUSPENDED) 1278 udev->do_remote_wakeup = 0; 1279 return; 1280 } 1281 1282 /* If remote wakeup is permitted, see whether any interface drivers 1283 * actually want it. 1284 */ 1285 w = 0; 1286 if (device_may_wakeup(&udev->dev) && udev->actconfig) { 1287 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1288 intf = udev->actconfig->interface[i]; 1289 w |= intf->needs_remote_wakeup; 1290 } 1291 } 1292 1293 /* If the device is autosuspended with the wrong wakeup setting, 1294 * autoresume now so the setting can be changed. 1295 */ 1296 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup) 1297 pm_runtime_resume(&udev->dev); 1298 udev->do_remote_wakeup = w; 1299 } 1300 1301 /* The device lock is held by the PM core */ 1302 int usb_suspend(struct device *dev, pm_message_t msg) 1303 { 1304 struct usb_device *udev = to_usb_device(dev); 1305 1306 do_unbind_rebind(udev, DO_UNBIND); 1307 choose_wakeup(udev, msg); 1308 return usb_suspend_both(udev, msg); 1309 } 1310 1311 /* The device lock is held by the PM core */ 1312 int usb_resume(struct device *dev, pm_message_t msg) 1313 { 1314 struct usb_device *udev = to_usb_device(dev); 1315 int status; 1316 1317 /* For PM complete calls, all we do is rebind interfaces */ 1318 if (msg.event == PM_EVENT_ON) { 1319 if (udev->state != USB_STATE_NOTATTACHED) 1320 do_unbind_rebind(udev, DO_REBIND); 1321 status = 0; 1322 1323 /* For all other calls, take the device back to full power and 1324 * tell the PM core in case it was autosuspended previously. 1325 */ 1326 } else { 1327 status = usb_resume_both(udev, msg); 1328 if (status == 0) { 1329 pm_runtime_disable(dev); 1330 pm_runtime_set_active(dev); 1331 pm_runtime_enable(dev); 1332 udev->last_busy = jiffies; 1333 } 1334 } 1335 1336 /* Avoid PM error messages for devices disconnected while suspended 1337 * as we'll display regular disconnect messages just a bit later. 1338 */ 1339 if (status == -ENODEV) 1340 status = 0; 1341 return status; 1342 } 1343 1344 #endif /* CONFIG_PM */ 1345 1346 #ifdef CONFIG_USB_SUSPEND 1347 1348 /** 1349 * usb_enable_autosuspend - allow a USB device to be autosuspended 1350 * @udev: the USB device which may be autosuspended 1351 * 1352 * This routine allows @udev to be autosuspended. An autosuspend won't 1353 * take place until the autosuspend_delay has elapsed and all the other 1354 * necessary conditions are satisfied. 1355 * 1356 * The caller must hold @udev's device lock. 1357 */ 1358 int usb_enable_autosuspend(struct usb_device *udev) 1359 { 1360 if (udev->autosuspend_disabled) { 1361 udev->autosuspend_disabled = 0; 1362 usb_autosuspend_device(udev); 1363 } 1364 return 0; 1365 } 1366 EXPORT_SYMBOL_GPL(usb_enable_autosuspend); 1367 1368 /** 1369 * usb_disable_autosuspend - prevent a USB device from being autosuspended 1370 * @udev: the USB device which may not be autosuspended 1371 * 1372 * This routine prevents @udev from being autosuspended and wakes it up 1373 * if it is already autosuspended. 1374 * 1375 * The caller must hold @udev's device lock. 1376 */ 1377 int usb_disable_autosuspend(struct usb_device *udev) 1378 { 1379 int rc = 0; 1380 1381 if (!udev->autosuspend_disabled) { 1382 rc = usb_autoresume_device(udev); 1383 if (rc == 0) 1384 udev->autosuspend_disabled = 1; 1385 } 1386 return rc; 1387 } 1388 EXPORT_SYMBOL_GPL(usb_disable_autosuspend); 1389 1390 /** 1391 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces 1392 * @udev: the usb_device to autosuspend 1393 * 1394 * This routine should be called when a core subsystem is finished using 1395 * @udev and wants to allow it to autosuspend. Examples would be when 1396 * @udev's device file in usbfs is closed or after a configuration change. 1397 * 1398 * @udev's usage counter is decremented; if it drops to 0 and all the 1399 * interfaces are inactive then a delayed autosuspend will be attempted. 1400 * The attempt may fail (see autosuspend_check()). 1401 * 1402 * The caller must hold @udev's device lock. 1403 * 1404 * This routine can run only in process context. 1405 */ 1406 void usb_autosuspend_device(struct usb_device *udev) 1407 { 1408 int status; 1409 1410 udev->last_busy = jiffies; 1411 status = pm_runtime_put_sync(&udev->dev); 1412 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", 1413 __func__, atomic_read(&udev->dev.power.usage_count), 1414 status); 1415 } 1416 1417 /** 1418 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces 1419 * @udev: the usb_device to autosuspend 1420 * 1421 * This routine should be called when a core subsystem thinks @udev may 1422 * be ready to autosuspend. 1423 * 1424 * @udev's usage counter left unchanged. If it is 0 and all the interfaces 1425 * are inactive then an autosuspend will be attempted. The attempt may 1426 * fail or be delayed. 1427 * 1428 * The caller must hold @udev's device lock. 1429 * 1430 * This routine can run only in process context. 1431 */ 1432 void usb_try_autosuspend_device(struct usb_device *udev) 1433 { 1434 int status; 1435 1436 status = pm_runtime_idle(&udev->dev); 1437 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", 1438 __func__, atomic_read(&udev->dev.power.usage_count), 1439 status); 1440 } 1441 1442 /** 1443 * usb_autoresume_device - immediately autoresume a USB device and its interfaces 1444 * @udev: the usb_device to autoresume 1445 * 1446 * This routine should be called when a core subsystem wants to use @udev 1447 * and needs to guarantee that it is not suspended. No autosuspend will 1448 * occur until usb_autosuspend_device() is called. (Note that this will 1449 * not prevent suspend events originating in the PM core.) Examples would 1450 * be when @udev's device file in usbfs is opened or when a remote-wakeup 1451 * request is received. 1452 * 1453 * @udev's usage counter is incremented to prevent subsequent autosuspends. 1454 * However if the autoresume fails then the usage counter is re-decremented. 1455 * 1456 * The caller must hold @udev's device lock. 1457 * 1458 * This routine can run only in process context. 1459 */ 1460 int usb_autoresume_device(struct usb_device *udev) 1461 { 1462 int status; 1463 1464 status = pm_runtime_get_sync(&udev->dev); 1465 if (status < 0) 1466 pm_runtime_put_sync(&udev->dev); 1467 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", 1468 __func__, atomic_read(&udev->dev.power.usage_count), 1469 status); 1470 if (status > 0) 1471 status = 0; 1472 return status; 1473 } 1474 1475 /** 1476 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter 1477 * @intf: the usb_interface whose counter should be decremented 1478 * 1479 * This routine should be called by an interface driver when it is 1480 * finished using @intf and wants to allow it to autosuspend. A typical 1481 * example would be a character-device driver when its device file is 1482 * closed. 1483 * 1484 * The routine decrements @intf's usage counter. When the counter reaches 1485 * 0, a delayed autosuspend request for @intf's device is attempted. The 1486 * attempt may fail (see autosuspend_check()). 1487 * 1488 * If the driver has set @intf->needs_remote_wakeup then autosuspend will 1489 * take place only if the device's remote-wakeup facility is enabled. 1490 * 1491 * This routine can run only in process context. 1492 */ 1493 void usb_autopm_put_interface(struct usb_interface *intf) 1494 { 1495 struct usb_device *udev = interface_to_usbdev(intf); 1496 int status; 1497 1498 udev->last_busy = jiffies; 1499 atomic_dec(&intf->pm_usage_cnt); 1500 status = pm_runtime_put_sync(&intf->dev); 1501 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1502 __func__, atomic_read(&intf->dev.power.usage_count), 1503 status); 1504 } 1505 EXPORT_SYMBOL_GPL(usb_autopm_put_interface); 1506 1507 /** 1508 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter 1509 * @intf: the usb_interface whose counter should be decremented 1510 * 1511 * This routine does much the same thing as usb_autopm_put_interface(): 1512 * It decrements @intf's usage counter and schedules a delayed 1513 * autosuspend request if the counter is <= 0. The difference is that it 1514 * does not perform any synchronization; callers should hold a private 1515 * lock and handle all synchronization issues themselves. 1516 * 1517 * Typically a driver would call this routine during an URB's completion 1518 * handler, if no more URBs were pending. 1519 * 1520 * This routine can run in atomic context. 1521 */ 1522 void usb_autopm_put_interface_async(struct usb_interface *intf) 1523 { 1524 struct usb_device *udev = interface_to_usbdev(intf); 1525 unsigned long last_busy; 1526 int status = 0; 1527 1528 last_busy = udev->last_busy; 1529 udev->last_busy = jiffies; 1530 atomic_dec(&intf->pm_usage_cnt); 1531 pm_runtime_put_noidle(&intf->dev); 1532 1533 if (!udev->autosuspend_disabled) { 1534 /* Optimization: Don't schedule a delayed autosuspend if 1535 * the timer is already running and the expiration time 1536 * wouldn't change. 1537 * 1538 * We have to use the interface's timer. Attempts to 1539 * schedule a suspend for the device would fail because 1540 * the interface is still active. 1541 */ 1542 if (intf->dev.power.timer_expires == 0 || 1543 round_jiffies_up(last_busy) != 1544 round_jiffies_up(jiffies)) { 1545 status = pm_schedule_suspend(&intf->dev, 1546 jiffies_to_msecs( 1547 round_jiffies_up_relative( 1548 udev->autosuspend_delay))); 1549 } 1550 } 1551 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1552 __func__, atomic_read(&intf->dev.power.usage_count), 1553 status); 1554 } 1555 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async); 1556 1557 /** 1558 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter 1559 * @intf: the usb_interface whose counter should be decremented 1560 * 1561 * This routine decrements @intf's usage counter but does not carry out an 1562 * autosuspend. 1563 * 1564 * This routine can run in atomic context. 1565 */ 1566 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf) 1567 { 1568 struct usb_device *udev = interface_to_usbdev(intf); 1569 1570 udev->last_busy = jiffies; 1571 atomic_dec(&intf->pm_usage_cnt); 1572 pm_runtime_put_noidle(&intf->dev); 1573 } 1574 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend); 1575 1576 /** 1577 * usb_autopm_get_interface - increment a USB interface's PM-usage counter 1578 * @intf: the usb_interface whose counter should be incremented 1579 * 1580 * This routine should be called by an interface driver when it wants to 1581 * use @intf and needs to guarantee that it is not suspended. In addition, 1582 * the routine prevents @intf from being autosuspended subsequently. (Note 1583 * that this will not prevent suspend events originating in the PM core.) 1584 * This prevention will persist until usb_autopm_put_interface() is called 1585 * or @intf is unbound. A typical example would be a character-device 1586 * driver when its device file is opened. 1587 * 1588 * @intf's usage counter is incremented to prevent subsequent autosuspends. 1589 * However if the autoresume fails then the counter is re-decremented. 1590 * 1591 * This routine can run only in process context. 1592 */ 1593 int usb_autopm_get_interface(struct usb_interface *intf) 1594 { 1595 int status; 1596 1597 status = pm_runtime_get_sync(&intf->dev); 1598 if (status < 0) 1599 pm_runtime_put_sync(&intf->dev); 1600 else 1601 atomic_inc(&intf->pm_usage_cnt); 1602 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1603 __func__, atomic_read(&intf->dev.power.usage_count), 1604 status); 1605 if (status > 0) 1606 status = 0; 1607 return status; 1608 } 1609 EXPORT_SYMBOL_GPL(usb_autopm_get_interface); 1610 1611 /** 1612 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter 1613 * @intf: the usb_interface whose counter should be incremented 1614 * 1615 * This routine does much the same thing as 1616 * usb_autopm_get_interface(): It increments @intf's usage counter and 1617 * queues an autoresume request if the device is suspended. The 1618 * differences are that it does not perform any synchronization (callers 1619 * should hold a private lock and handle all synchronization issues 1620 * themselves), and it does not autoresume the device directly (it only 1621 * queues a request). After a successful call, the device may not yet be 1622 * resumed. 1623 * 1624 * This routine can run in atomic context. 1625 */ 1626 int usb_autopm_get_interface_async(struct usb_interface *intf) 1627 { 1628 int status = 0; 1629 enum rpm_status s; 1630 1631 /* Don't request a resume unless the interface is already suspending 1632 * or suspended. Doing so would force a running suspend timer to be 1633 * cancelled. 1634 */ 1635 pm_runtime_get_noresume(&intf->dev); 1636 s = ACCESS_ONCE(intf->dev.power.runtime_status); 1637 if (s == RPM_SUSPENDING || s == RPM_SUSPENDED) 1638 status = pm_request_resume(&intf->dev); 1639 1640 if (status < 0 && status != -EINPROGRESS) 1641 pm_runtime_put_noidle(&intf->dev); 1642 else 1643 atomic_inc(&intf->pm_usage_cnt); 1644 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1645 __func__, atomic_read(&intf->dev.power.usage_count), 1646 status); 1647 if (status > 0) 1648 status = 0; 1649 return status; 1650 } 1651 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async); 1652 1653 /** 1654 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter 1655 * @intf: the usb_interface whose counter should be incremented 1656 * 1657 * This routine increments @intf's usage counter but does not carry out an 1658 * autoresume. 1659 * 1660 * This routine can run in atomic context. 1661 */ 1662 void usb_autopm_get_interface_no_resume(struct usb_interface *intf) 1663 { 1664 struct usb_device *udev = interface_to_usbdev(intf); 1665 1666 udev->last_busy = jiffies; 1667 atomic_inc(&intf->pm_usage_cnt); 1668 pm_runtime_get_noresume(&intf->dev); 1669 } 1670 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume); 1671 1672 /* Internal routine to check whether we may autosuspend a device. */ 1673 static int autosuspend_check(struct usb_device *udev) 1674 { 1675 int i; 1676 struct usb_interface *intf; 1677 unsigned long suspend_time, j; 1678 1679 /* Fail if autosuspend is disabled, or any interfaces are in use, or 1680 * any interface drivers require remote wakeup but it isn't available. 1681 */ 1682 udev->do_remote_wakeup = device_may_wakeup(&udev->dev); 1683 if (udev->actconfig) { 1684 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1685 intf = udev->actconfig->interface[i]; 1686 1687 /* We don't need to check interfaces that are 1688 * disabled for runtime PM. Either they are unbound 1689 * or else their drivers don't support autosuspend 1690 * and so they are permanently active. 1691 */ 1692 if (intf->dev.power.disable_depth) 1693 continue; 1694 if (atomic_read(&intf->dev.power.usage_count) > 0) 1695 return -EBUSY; 1696 if (intf->needs_remote_wakeup && 1697 !udev->do_remote_wakeup) { 1698 dev_dbg(&udev->dev, "remote wakeup needed " 1699 "for autosuspend\n"); 1700 return -EOPNOTSUPP; 1701 } 1702 1703 /* Don't allow autosuspend if the device will need 1704 * a reset-resume and any of its interface drivers 1705 * doesn't include support or needs remote wakeup. 1706 */ 1707 if (udev->quirks & USB_QUIRK_RESET_RESUME) { 1708 struct usb_driver *driver; 1709 1710 driver = to_usb_driver(intf->dev.driver); 1711 if (!driver->reset_resume || 1712 intf->needs_remote_wakeup) 1713 return -EOPNOTSUPP; 1714 } 1715 } 1716 } 1717 1718 /* If everything is okay but the device hasn't been idle for long 1719 * enough, queue a delayed autosuspend request. 1720 */ 1721 j = ACCESS_ONCE(jiffies); 1722 suspend_time = udev->last_busy + udev->autosuspend_delay; 1723 if (time_before(j, suspend_time)) { 1724 pm_schedule_suspend(&udev->dev, jiffies_to_msecs( 1725 round_jiffies_up_relative(suspend_time - j))); 1726 return -EAGAIN; 1727 } 1728 return 0; 1729 } 1730 1731 static int usb_runtime_suspend(struct device *dev) 1732 { 1733 int status = 0; 1734 1735 /* A USB device can be suspended if it passes the various autosuspend 1736 * checks. Runtime suspend for a USB device means suspending all the 1737 * interfaces and then the device itself. 1738 */ 1739 if (is_usb_device(dev)) { 1740 struct usb_device *udev = to_usb_device(dev); 1741 1742 if (autosuspend_check(udev) != 0) 1743 return -EAGAIN; 1744 1745 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND); 1746 1747 /* If an interface fails the suspend, adjust the last_busy 1748 * time so that we don't get another suspend attempt right 1749 * away. 1750 */ 1751 if (status) { 1752 udev->last_busy = jiffies + 1753 (udev->autosuspend_delay == 0 ? 1754 HZ/2 : 0); 1755 } 1756 1757 /* Prevent the parent from suspending immediately after */ 1758 else if (udev->parent) { 1759 udev->parent->last_busy = jiffies; 1760 } 1761 } 1762 1763 /* Runtime suspend for a USB interface doesn't mean anything. */ 1764 return status; 1765 } 1766 1767 static int usb_runtime_resume(struct device *dev) 1768 { 1769 /* Runtime resume for a USB device means resuming both the device 1770 * and all its interfaces. 1771 */ 1772 if (is_usb_device(dev)) { 1773 struct usb_device *udev = to_usb_device(dev); 1774 int status; 1775 1776 status = usb_resume_both(udev, PMSG_AUTO_RESUME); 1777 udev->last_busy = jiffies; 1778 return status; 1779 } 1780 1781 /* Runtime resume for a USB interface doesn't mean anything. */ 1782 return 0; 1783 } 1784 1785 static int usb_runtime_idle(struct device *dev) 1786 { 1787 /* An idle USB device can be suspended if it passes the various 1788 * autosuspend checks. An idle interface can be suspended at 1789 * any time. 1790 */ 1791 if (is_usb_device(dev)) { 1792 struct usb_device *udev = to_usb_device(dev); 1793 1794 if (autosuspend_check(udev) != 0) 1795 return 0; 1796 } 1797 1798 pm_runtime_suspend(dev); 1799 return 0; 1800 } 1801 1802 static struct dev_pm_ops usb_bus_pm_ops = { 1803 .runtime_suspend = usb_runtime_suspend, 1804 .runtime_resume = usb_runtime_resume, 1805 .runtime_idle = usb_runtime_idle, 1806 }; 1807 1808 #else 1809 1810 #define usb_bus_pm_ops (*(struct dev_pm_ops *) NULL) 1811 1812 #endif /* CONFIG_USB_SUSPEND */ 1813 1814 struct bus_type usb_bus_type = { 1815 .name = "usb", 1816 .match = usb_device_match, 1817 .uevent = usb_uevent, 1818 .pm = &usb_bus_pm_ops, 1819 }; 1820