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