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