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