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