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