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