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