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