]>
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 | 39 | ssize_t usb_store_new_id(struct usb_dynids *dynids, |
2fc82c2d | 40 | const struct usb_device_id *id_table, |
93bacefc GKH |
41 | struct device_driver *driver, |
42 | const char *buf, size_t count) | |
733260ff | 43 | { |
733260ff GKH |
44 | struct usb_dynid *dynid; |
45 | u32 idVendor = 0; | |
46 | u32 idProduct = 0; | |
ff231db8 | 47 | unsigned int bInterfaceClass = 0; |
2fc82c2d | 48 | u32 refVendor, refProduct; |
733260ff | 49 | int fields = 0; |
1b21d5e1 | 50 | int retval = 0; |
733260ff | 51 | |
2fc82c2d WS |
52 | fields = sscanf(buf, "%x %x %x %x %x", &idVendor, &idProduct, |
53 | &bInterfaceClass, &refVendor, &refProduct); | |
733260ff GKH |
54 | if (fields < 2) |
55 | return -EINVAL; | |
56 | ||
57 | dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); | |
58 | if (!dynid) | |
59 | return -ENOMEM; | |
60 | ||
61 | INIT_LIST_HEAD(&dynid->node); | |
62 | dynid->id.idVendor = idVendor; | |
63 | dynid->id.idProduct = idProduct; | |
64 | dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE; | |
c63fe8f6 | 65 | if (fields > 2 && bInterfaceClass) { |
7f196caf CE |
66 | if (bInterfaceClass > 255) { |
67 | retval = -EINVAL; | |
68 | goto fail; | |
69 | } | |
c63fe8f6 | 70 | |
ff231db8 JD |
71 | dynid->id.bInterfaceClass = (u8)bInterfaceClass; |
72 | dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS; | |
73 | } | |
733260ff | 74 | |
2fc82c2d WS |
75 | if (fields > 4) { |
76 | const struct usb_device_id *id = id_table; | |
77 | ||
7f196caf CE |
78 | if (!id) { |
79 | retval = -ENODEV; | |
80 | goto fail; | |
81 | } | |
1b9fb31f | 82 | |
2fc82c2d | 83 | for (; id->match_flags; id++) |
52a6966c | 84 | if (id->idVendor == refVendor && id->idProduct == refProduct) |
2fc82c2d | 85 | break; |
52a6966c | 86 | |
7f196caf | 87 | if (id->match_flags) { |
52a6966c | 88 | dynid->id.driver_info = id->driver_info; |
7f196caf CE |
89 | } else { |
90 | retval = -ENODEV; | |
91 | goto fail; | |
92 | } | |
2fc82c2d WS |
93 | } |
94 | ||
93bacefc | 95 | spin_lock(&dynids->lock); |
e5dd0115 | 96 | list_add_tail(&dynid->node, &dynids->list); |
93bacefc | 97 | spin_unlock(&dynids->lock); |
733260ff | 98 | |
cef9bc56 | 99 | retval = driver_attach(driver); |
733260ff | 100 | |
1b21d5e1 GKH |
101 | if (retval) |
102 | return retval; | |
733260ff | 103 | return count; |
7f196caf CE |
104 | |
105 | fail: | |
106 | kfree(dynid); | |
107 | return retval; | |
733260ff | 108 | } |
93bacefc GKH |
109 | EXPORT_SYMBOL_GPL(usb_store_new_id); |
110 | ||
ef206f3f | 111 | ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf) |
e6bbcef0 BM |
112 | { |
113 | struct usb_dynid *dynid; | |
e6bbcef0 BM |
114 | size_t count = 0; |
115 | ||
ef206f3f | 116 | list_for_each_entry(dynid, &dynids->list, node) |
e6bbcef0 BM |
117 | if (dynid->id.bInterfaceClass != 0) |
118 | count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n", | |
119 | dynid->id.idVendor, dynid->id.idProduct, | |
120 | dynid->id.bInterfaceClass); | |
121 | else | |
122 | count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n", | |
123 | dynid->id.idVendor, dynid->id.idProduct); | |
124 | return count; | |
125 | } | |
ef206f3f BM |
126 | EXPORT_SYMBOL_GPL(usb_show_dynids); |
127 | ||
598d0361 | 128 | static ssize_t new_id_show(struct device_driver *driver, char *buf) |
ef206f3f BM |
129 | { |
130 | struct usb_driver *usb_drv = to_usb_driver(driver); | |
131 | ||
132 | return usb_show_dynids(&usb_drv->dynids, buf); | |
133 | } | |
e6bbcef0 | 134 | |
598d0361 | 135 | static ssize_t new_id_store(struct device_driver *driver, |
93bacefc GKH |
136 | const char *buf, size_t count) |
137 | { | |
138 | struct usb_driver *usb_drv = to_usb_driver(driver); | |
139 | ||
2fc82c2d | 140 | return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count); |
93bacefc | 141 | } |
598d0361 | 142 | static DRIVER_ATTR_RW(new_id); |
733260ff | 143 | |
598d0361 GKH |
144 | /* |
145 | * Remove a USB device ID from this driver | |
0c7a2b72 | 146 | */ |
598d0361 GKH |
147 | static ssize_t remove_id_store(struct device_driver *driver, const char *buf, |
148 | size_t count) | |
0c7a2b72 CR |
149 | { |
150 | struct usb_dynid *dynid, *n; | |
151 | struct usb_driver *usb_driver = to_usb_driver(driver); | |
ac08de32 AC |
152 | u32 idVendor; |
153 | u32 idProduct; | |
154 | int fields; | |
0c7a2b72 CR |
155 | |
156 | fields = sscanf(buf, "%x %x", &idVendor, &idProduct); | |
157 | if (fields < 2) | |
158 | return -EINVAL; | |
159 | ||
160 | spin_lock(&usb_driver->dynids.lock); | |
161 | list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) { | |
162 | struct usb_device_id *id = &dynid->id; | |
163 | if ((id->idVendor == idVendor) && | |
164 | (id->idProduct == idProduct)) { | |
165 | list_del(&dynid->node); | |
166 | kfree(dynid); | |
0c7a2b72 CR |
167 | break; |
168 | } | |
169 | } | |
170 | spin_unlock(&usb_driver->dynids.lock); | |
0c7a2b72 CR |
171 | return count; |
172 | } | |
598d0361 GKH |
173 | |
174 | static ssize_t remove_id_show(struct device_driver *driver, char *buf) | |
175 | { | |
176 | return new_id_show(driver, buf); | |
177 | } | |
178 | static DRIVER_ATTR_RW(remove_id); | |
0c7a2b72 | 179 | |
ed283e9f | 180 | static int usb_create_newid_files(struct usb_driver *usb_drv) |
733260ff GKH |
181 | { |
182 | int error = 0; | |
183 | ||
ba9dc657 GKH |
184 | if (usb_drv->no_dynamic_id) |
185 | goto exit; | |
186 | ||
ed283e9f | 187 | if (usb_drv->probe != NULL) { |
15147ffd GKH |
188 | error = driver_create_file(&usb_drv->drvwrap.driver, |
189 | &driver_attr_new_id); | |
ed283e9f AS |
190 | if (error == 0) { |
191 | error = driver_create_file(&usb_drv->drvwrap.driver, | |
192 | &driver_attr_remove_id); | |
193 | if (error) | |
194 | driver_remove_file(&usb_drv->drvwrap.driver, | |
195 | &driver_attr_new_id); | |
196 | } | |
197 | } | |
ba9dc657 | 198 | exit: |
733260ff GKH |
199 | return error; |
200 | } | |
201 | ||
ed283e9f | 202 | static void usb_remove_newid_files(struct usb_driver *usb_drv) |
ba9dc657 GKH |
203 | { |
204 | if (usb_drv->no_dynamic_id) | |
205 | return; | |
206 | ||
ed283e9f | 207 | if (usb_drv->probe != NULL) { |
15147ffd | 208 | driver_remove_file(&usb_drv->drvwrap.driver, |
0c7a2b72 | 209 | &driver_attr_remove_id); |
ed283e9f AS |
210 | driver_remove_file(&usb_drv->drvwrap.driver, |
211 | &driver_attr_new_id); | |
212 | } | |
0c7a2b72 CR |
213 | } |
214 | ||
733260ff GKH |
215 | 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 | list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) { | |
221 | list_del(&dynid->node); | |
222 | kfree(dynid); | |
223 | } | |
224 | spin_unlock(&usb_drv->dynids.lock); | |
225 | } | |
733260ff GKH |
226 | |
227 | 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 | list_for_each_entry(dynid, &drv->dynids.list, node) { | |
234 | if (usb_match_one_id(intf, &dynid->id)) { | |
235 | spin_unlock(&drv->dynids.lock); | |
236 | return &dynid->id; | |
237 | } | |
238 | } | |
239 | spin_unlock(&drv->dynids.lock); | |
240 | return NULL; | |
241 | } | |
242 | ||
243 | ||
8bb54ab5 AS |
244 | /* called from driver core with dev locked */ |
245 | static int usb_probe_device(struct device *dev) | |
246 | { | |
247 | struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); | |
55129666 | 248 | struct usb_device *udev = to_usb_device(dev); |
9bbdf1e0 | 249 | int error = 0; |
8bb54ab5 | 250 | |
441b62c1 | 251 | dev_dbg(dev, "%s\n", __func__); |
8bb54ab5 | 252 | |
8bb54ab5 AS |
253 | /* TODO: Add real matching code */ |
254 | ||
645daaab | 255 | /* The device should always appear to be in use |
02582e9b | 256 | * unless the driver supports autosuspend. |
645daaab | 257 | */ |
9bbdf1e0 AS |
258 | if (!udriver->supports_autosuspend) |
259 | error = usb_autoresume_device(udev); | |
645daaab | 260 | |
9bbdf1e0 AS |
261 | if (!error) |
262 | error = udriver->probe(udev); | |
8bb54ab5 AS |
263 | return error; |
264 | } | |
265 | ||
266 | /* called from driver core with dev locked */ | |
267 | static int usb_unbind_device(struct device *dev) | |
268 | { | |
9bbdf1e0 | 269 | struct usb_device *udev = to_usb_device(dev); |
8bb54ab5 AS |
270 | struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); |
271 | ||
9bbdf1e0 AS |
272 | udriver->disconnect(udev); |
273 | if (!udriver->supports_autosuspend) | |
274 | usb_autosuspend_device(udev); | |
8bb54ab5 AS |
275 | return 0; |
276 | } | |
277 | ||
dc023dce IPG |
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 | if (iface->reset_running == 0) | |
290 | cancel_work_sync(&iface->reset_ws); | |
291 | } | |
8bb54ab5 AS |
292 | |
293 | /* called from driver core with dev locked */ | |
ddae41be GKH |
294 | static int usb_probe_interface(struct device *dev) |
295 | { | |
8bb54ab5 | 296 | struct usb_driver *driver = to_usb_driver(dev->driver); |
55129666 KS |
297 | struct usb_interface *intf = to_usb_interface(dev); |
298 | struct usb_device *udev = interface_to_usbdev(intf); | |
ddae41be GKH |
299 | const struct usb_device_id *id; |
300 | int error = -ENODEV; | |
8306095f | 301 | int lpm_disable_error; |
ddae41be | 302 | |
441b62c1 | 303 | dev_dbg(dev, "%s\n", __func__); |
ddae41be | 304 | |
78d9a487 | 305 | intf->needs_binding = 0; |
ddae41be | 306 | |
7cbe5dca | 307 | if (usb_device_is_owned(udev)) |
0f3dda9f | 308 | return error; |
7cbe5dca | 309 | |
2c044a48 GKH |
310 | if (udev->authorized == 0) { |
311 | dev_err(&intf->dev, "Device is not authorized for usage\n"); | |
0f3dda9f | 312 | return error; |
2c044a48 | 313 | } |
72230abb | 314 | |
ddae41be | 315 | id = usb_match_id(intf, driver->id_table); |
733260ff GKH |
316 | if (!id) |
317 | id = usb_match_dynamic_id(intf, driver); | |
0f3dda9f AS |
318 | if (!id) |
319 | return error; | |
55151d7d | 320 | |
0f3dda9f AS |
321 | dev_dbg(dev, "%s - got id\n", __func__); |
322 | ||
323 | error = usb_autoresume_device(udev); | |
324 | if (error) | |
325 | return error; | |
326 | ||
0f3dda9f | 327 | intf->condition = USB_INTERFACE_BINDING; |
645daaab | 328 | |
571dc79d | 329 | /* Probed interfaces are initially active. They are |
9bbdf1e0 AS |
330 | * runtime-PM-enabled only if the driver has autosuspend support. |
331 | * They are sensitive to their children's power states. | |
0f3dda9f | 332 | */ |
9bbdf1e0 AS |
333 | pm_runtime_set_active(dev); |
334 | pm_suspend_ignore_children(dev, false); | |
335 | if (driver->supports_autosuspend) | |
336 | pm_runtime_enable(dev); | |
0f3dda9f | 337 | |
8306095f SS |
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 | lpm_disable_error = usb_unlocked_disable_lpm(udev); | |
350 | if (lpm_disable_error && driver->disable_hub_initiated_lpm) { | |
351 | dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.", | |
352 | __func__, driver->name); | |
353 | error = lpm_disable_error; | |
354 | goto err; | |
355 | } | |
356 | ||
0f3dda9f AS |
357 | /* Carry out a deferred switch to altsetting 0 */ |
358 | if (intf->needs_altsetting0) { | |
359 | error = usb_set_interface(udev, intf->altsetting[0]. | |
360 | desc.bInterfaceNumber, 0); | |
361 | if (error < 0) | |
362 | goto err; | |
363 | intf->needs_altsetting0 = 0; | |
ddae41be GKH |
364 | } |
365 | ||
0f3dda9f AS |
366 | error = driver->probe(intf, id); |
367 | if (error) | |
368 | goto err; | |
369 | ||
370 | intf->condition = USB_INTERFACE_BOUND; | |
8306095f SS |
371 | |
372 | /* If the LPM disable succeeded, balance the ref counts. */ | |
373 | if (!lpm_disable_error) | |
374 | usb_unlocked_enable_lpm(udev); | |
375 | ||
0f3dda9f | 376 | usb_autosuspend_device(udev); |
ddae41be | 377 | return error; |
1e5ea5e3 | 378 | |
0f3dda9f | 379 | err: |
e714fad0 | 380 | usb_set_intfdata(intf, NULL); |
1e5ea5e3 ON |
381 | intf->needs_remote_wakeup = 0; |
382 | intf->condition = USB_INTERFACE_UNBOUND; | |
383 | usb_cancel_queued_reset(intf); | |
9bbdf1e0 | 384 | |
d01f87c0 SS |
385 | /* If the LPM disable succeeded, balance the ref counts. */ |
386 | if (!lpm_disable_error) | |
387 | usb_unlocked_enable_lpm(udev); | |
388 | ||
9bbdf1e0 | 389 | /* Unbound interfaces are always runtime-PM-disabled and -suspended */ |
89842ae6 AS |
390 | if (driver->supports_autosuspend) |
391 | pm_runtime_disable(dev); | |
9bbdf1e0 AS |
392 | pm_runtime_set_suspended(dev); |
393 | ||
1e5ea5e3 ON |
394 | usb_autosuspend_device(udev); |
395 | return error; | |
ddae41be GKH |
396 | } |
397 | ||
8bb54ab5 | 398 | /* called from driver core with dev locked */ |
ddae41be GKH |
399 | static int usb_unbind_interface(struct device *dev) |
400 | { | |
8bb54ab5 | 401 | struct usb_driver *driver = to_usb_driver(dev->driver); |
ddae41be | 402 | struct usb_interface *intf = to_usb_interface(dev); |
645daaab | 403 | struct usb_device *udev; |
8306095f | 404 | int error, r, lpm_disable_error; |
ddae41be GKH |
405 | |
406 | intf->condition = USB_INTERFACE_UNBINDING; | |
407 | ||
645daaab AS |
408 | /* Autoresume for set_interface call below */ |
409 | udev = interface_to_usbdev(intf); | |
94fcda1f | 410 | error = usb_autoresume_device(udev); |
645daaab | 411 | |
8306095f SS |
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 | lpm_disable_error = usb_unlocked_disable_lpm(udev); | |
418 | ||
9da82bd4 AS |
419 | /* Terminate all URBs for this interface unless the driver |
420 | * supports "soft" unbinding. | |
421 | */ | |
422 | if (!driver->soft_unbind) | |
ddeac4e7 | 423 | usb_disable_interface(udev, intf, false); |
ddae41be | 424 | |
8bb54ab5 | 425 | driver->disconnect(intf); |
dc023dce | 426 | usb_cancel_queued_reset(intf); |
ddae41be | 427 | |
55151d7d AS |
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 | */ | |
2caf7fcd AS |
434 | 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 | usb_enable_interface(udev, intf, false); | |
f76b168b | 439 | } else if (!error && !intf->dev.power.is_prepared) { |
1e5ea5e3 | 440 | r = usb_set_interface(udev, intf->altsetting[0]. |
55151d7d | 441 | desc.bInterfaceNumber, 0); |
1e5ea5e3 ON |
442 | if (r < 0) |
443 | intf->needs_altsetting0 = 1; | |
444 | } else { | |
55151d7d | 445 | intf->needs_altsetting0 = 1; |
1e5ea5e3 | 446 | } |
ddae41be | 447 | usb_set_intfdata(intf, NULL); |
645daaab | 448 | |
ddae41be | 449 | intf->condition = USB_INTERFACE_UNBOUND; |
645daaab AS |
450 | intf->needs_remote_wakeup = 0; |
451 | ||
8306095f SS |
452 | /* Attempt to re-enable USB3 LPM, if the disable succeeded. */ |
453 | if (!lpm_disable_error) | |
454 | usb_unlocked_enable_lpm(udev); | |
455 | ||
9bbdf1e0 | 456 | /* Unbound interfaces are always runtime-PM-disabled and -suspended */ |
89842ae6 AS |
457 | if (driver->supports_autosuspend) |
458 | pm_runtime_disable(dev); | |
9bbdf1e0 AS |
459 | pm_runtime_set_suspended(dev); |
460 | ||
461 | /* Undo any residual pm_autopm_get_interface_* calls */ | |
462 | for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r) | |
463 | usb_autopm_put_interface_no_suspend(intf); | |
464 | atomic_set(&intf->pm_usage_cnt, 0); | |
465 | ||
645daaab | 466 | if (!error) |
94fcda1f | 467 | usb_autosuspend_device(udev); |
ddae41be GKH |
468 | |
469 | return 0; | |
470 | } | |
471 | ||
36e56a34 AS |
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 | * | |
341487a8 GKH |
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. | |
626f090c YB |
491 | * |
492 | * Return: 0 on success. | |
36e56a34 AS |
493 | */ |
494 | int usb_driver_claim_interface(struct usb_driver *driver, | |
2c044a48 | 495 | struct usb_interface *iface, void *priv) |
36e56a34 AS |
496 | { |
497 | struct device *dev = &iface->dev; | |
8306095f | 498 | struct usb_device *udev; |
1b21d5e1 | 499 | int retval = 0; |
8306095f | 500 | int lpm_disable_error; |
36e56a34 AS |
501 | |
502 | if (dev->driver) | |
503 | return -EBUSY; | |
504 | ||
8306095f SS |
505 | udev = interface_to_usbdev(iface); |
506 | ||
8bb54ab5 | 507 | dev->driver = &driver->drvwrap.driver; |
36e56a34 | 508 | usb_set_intfdata(iface, priv); |
78d9a487 | 509 | iface->needs_binding = 0; |
645daaab | 510 | |
36e56a34 | 511 | iface->condition = USB_INTERFACE_BOUND; |
9bbdf1e0 | 512 | |
8306095f SS |
513 | /* Disable LPM until this driver is bound. */ |
514 | lpm_disable_error = usb_unlocked_disable_lpm(udev); | |
515 | if (lpm_disable_error && driver->disable_hub_initiated_lpm) { | |
516 | dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.", | |
517 | __func__, driver->name); | |
518 | return -ENOMEM; | |
519 | } | |
520 | ||
89842ae6 AS |
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. | |
9bbdf1e0 | 526 | */ |
9bbdf1e0 AS |
527 | pm_suspend_ignore_children(dev, false); |
528 | if (driver->supports_autosuspend) | |
529 | pm_runtime_enable(dev); | |
89842ae6 AS |
530 | else |
531 | pm_runtime_set_active(dev); | |
36e56a34 AS |
532 | |
533 | /* if interface was already added, bind now; else let | |
534 | * the future device_add() bind it, bypassing probe() | |
535 | */ | |
536 | if (device_is_registered(dev)) | |
1b21d5e1 | 537 | retval = device_bind_driver(dev); |
36e56a34 | 538 | |
8306095f SS |
539 | /* Attempt to re-enable USB3 LPM, if the disable was successful. */ |
540 | if (!lpm_disable_error) | |
541 | usb_unlocked_enable_lpm(udev); | |
542 | ||
1b21d5e1 | 543 | return retval; |
36e56a34 | 544 | } |
782e70c6 | 545 | EXPORT_SYMBOL_GPL(usb_driver_claim_interface); |
36e56a34 AS |
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. | |
341487a8 GKH |
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. | |
36e56a34 AS |
560 | */ |
561 | void usb_driver_release_interface(struct usb_driver *driver, | |
562 | struct usb_interface *iface) | |
563 | { | |
564 | struct device *dev = &iface->dev; | |
565 | ||
566 | /* this should never happen, don't release something that's not ours */ | |
8bb54ab5 | 567 | if (!dev->driver || dev->driver != &driver->drvwrap.driver) |
36e56a34 AS |
568 | return; |
569 | ||
570 | /* don't release from within disconnect() */ | |
571 | if (iface->condition != USB_INTERFACE_BOUND) | |
572 | return; | |
91f8d063 | 573 | iface->condition = USB_INTERFACE_UNBINDING; |
36e56a34 | 574 | |
91f8d063 AS |
575 | /* Release via the driver core only if the interface |
576 | * has already been registered | |
577 | */ | |
36e56a34 | 578 | if (device_is_registered(dev)) { |
36e56a34 | 579 | device_release_driver(dev); |
dc023dce | 580 | } else { |
8e9394ce | 581 | device_lock(dev); |
91f8d063 AS |
582 | usb_unbind_interface(dev); |
583 | dev->driver = NULL; | |
8e9394ce | 584 | device_unlock(dev); |
36e56a34 | 585 | } |
36e56a34 | 586 | } |
782e70c6 | 587 | EXPORT_SYMBOL_GPL(usb_driver_release_interface); |
36e56a34 | 588 | |
733260ff | 589 | /* returns 0 if no match, 1 if match */ |
bb417020 | 590 | int usb_match_device(struct usb_device *dev, const struct usb_device_id *id) |
733260ff | 591 | { |
733260ff GKH |
592 | if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
593 | id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) | |
594 | return 0; | |
595 | ||
596 | if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && | |
597 | 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 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && | |
603 | (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) | |
604 | return 0; | |
605 | ||
606 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && | |
607 | (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) | |
608 | return 0; | |
609 | ||
610 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && | |
611 | (id->bDeviceClass != dev->descriptor.bDeviceClass)) | |
612 | return 0; | |
613 | ||
614 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && | |
2c044a48 | 615 | (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass)) |
733260ff GKH |
616 | return 0; |
617 | ||
618 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && | |
619 | (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) | |
620 | return 0; | |
621 | ||
bb417020 GKH |
622 | return 1; |
623 | } | |
624 | ||
625 | /* returns 0 if no match, 1 if match */ | |
80da2e0d LP |
626 | int usb_match_one_id_intf(struct usb_device *dev, |
627 | struct usb_host_interface *intf, | |
628 | const struct usb_device_id *id) | |
bb417020 | 629 | { |
81df2d59 | 630 | /* The interface class, subclass, protocol and number should never be |
93c8bf45 AS |
631 | * checked for a match if the device class is Vendor Specific, |
632 | * unless the match record specifies the Vendor ID. */ | |
633 | if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC && | |
634 | !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && | |
635 | (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | | |
636 | USB_DEVICE_ID_MATCH_INT_SUBCLASS | | |
81df2d59 BM |
637 | USB_DEVICE_ID_MATCH_INT_PROTOCOL | |
638 | USB_DEVICE_ID_MATCH_INT_NUMBER))) | |
93c8bf45 AS |
639 | return 0; |
640 | ||
733260ff GKH |
641 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && |
642 | (id->bInterfaceClass != intf->desc.bInterfaceClass)) | |
643 | return 0; | |
644 | ||
645 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && | |
646 | (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) | |
647 | return 0; | |
648 | ||
649 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && | |
650 | (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) | |
651 | return 0; | |
652 | ||
81df2d59 BM |
653 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) && |
654 | (id->bInterfaceNumber != intf->desc.bInterfaceNumber)) | |
655 | return 0; | |
656 | ||
733260ff GKH |
657 | return 1; |
658 | } | |
80da2e0d LP |
659 | |
660 | /* returns 0 if no match, 1 if match */ | |
661 | 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 | if (id == NULL) | |
669 | return 0; | |
670 | ||
671 | intf = interface->cur_altsetting; | |
672 | dev = interface_to_usbdev(interface); | |
673 | ||
674 | if (!usb_match_device(dev, id)) | |
675 | return 0; | |
676 | ||
677 | return usb_match_one_id_intf(dev, intf, id); | |
678 | } | |
93bacefc GKH |
679 | EXPORT_SYMBOL_GPL(usb_match_one_id); |
680 | ||
ddae41be GKH |
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 | * | |
626f090c YB |
694 | * Return: The first matching usb_device_id, or %NULL. |
695 | * | |
ddae41be GKH |
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 | |
93c8bf45 AS |
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.) | |
ddae41be GKH |
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 | const struct usb_device_id *usb_match_id(struct usb_interface *interface, | |
755 | const struct usb_device_id *id) | |
756 | { | |
ddae41be GKH |
757 | /* proc_connectinfo in devio.c may call us with id == NULL. */ |
758 | if (id == NULL) | |
759 | return NULL; | |
760 | ||
ddae41be GKH |
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. */ | |
de6f92b9 GKH |
766 | for (; id->idVendor || id->idProduct || id->bDeviceClass || |
767 | id->bInterfaceClass || id->driver_info; id++) { | |
733260ff GKH |
768 | if (usb_match_one_id(interface, id)) |
769 | return id; | |
ddae41be GKH |
770 | } |
771 | ||
772 | return NULL; | |
773 | } | |
782e70c6 | 774 | EXPORT_SYMBOL_GPL(usb_match_id); |
ddae41be | 775 | |
8bb22d2b | 776 | static int usb_device_match(struct device *dev, struct device_driver *drv) |
ddae41be | 777 | { |
8bb54ab5 AS |
778 | /* devices and interfaces are handled separately */ |
779 | if (is_usb_device(dev)) { | |
ddae41be | 780 | |
8bb54ab5 AS |
781 | /* interface drivers never match devices */ |
782 | if (!is_usb_device_driver(drv)) | |
783 | return 0; | |
ddae41be | 784 | |
8bb54ab5 | 785 | /* TODO: Add real matching code */ |
ddae41be GKH |
786 | return 1; |
787 | ||
55129666 | 788 | } else if (is_usb_interface(dev)) { |
8bb54ab5 AS |
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 | if (is_usb_device_driver(drv)) | |
795 | return 0; | |
796 | ||
797 | intf = to_usb_interface(dev); | |
798 | usb_drv = to_usb_driver(drv); | |
799 | ||
800 | id = usb_match_id(intf, usb_drv->id_table); | |
801 | if (id) | |
802 | return 1; | |
803 | ||
804 | id = usb_match_dynamic_id(intf, usb_drv); | |
805 | if (id) | |
806 | return 1; | |
807 | } | |
808 | ||
ddae41be GKH |
809 | return 0; |
810 | } | |
811 | ||
7eff2e7a | 812 | static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) |
36e56a34 | 813 | { |
36e56a34 | 814 | struct usb_device *usb_dev; |
36e56a34 | 815 | |
55129666 | 816 | if (is_usb_device(dev)) { |
782da727 | 817 | usb_dev = to_usb_device(dev); |
55129666 | 818 | } else if (is_usb_interface(dev)) { |
9f8b17e6 | 819 | struct usb_interface *intf = to_usb_interface(dev); |
55129666 | 820 | |
8bb54ab5 | 821 | usb_dev = interface_to_usbdev(intf); |
55129666 KS |
822 | } else { |
823 | return 0; | |
8bb54ab5 | 824 | } |
36e56a34 AS |
825 | |
826 | if (usb_dev->devnum < 0) { | |
cceffe93 | 827 | /* driver is often null here; dev_dbg() would oops */ |
7071a3ce | 828 | pr_debug("usb %s: already deleted?\n", dev_name(dev)); |
36e56a34 AS |
829 | return -ENODEV; |
830 | } | |
831 | if (!usb_dev->bus) { | |
7071a3ce | 832 | pr_debug("usb %s: bus removed?\n", dev_name(dev)); |
36e56a34 AS |
833 | return -ENODEV; |
834 | } | |
835 | ||
36e56a34 | 836 | /* per-device configurations are common */ |
7eff2e7a | 837 | if (add_uevent_var(env, "PRODUCT=%x/%x/%x", |
36e56a34 AS |
838 | le16_to_cpu(usb_dev->descriptor.idVendor), |
839 | le16_to_cpu(usb_dev->descriptor.idProduct), | |
840 | le16_to_cpu(usb_dev->descriptor.bcdDevice))) | |
841 | return -ENOMEM; | |
842 | ||
843 | /* class-based driver binding models */ | |
7eff2e7a | 844 | if (add_uevent_var(env, "TYPE=%d/%d/%d", |
36e56a34 AS |
845 | usb_dev->descriptor.bDeviceClass, |
846 | usb_dev->descriptor.bDeviceSubClass, | |
847 | usb_dev->descriptor.bDeviceProtocol)) | |
848 | return -ENOMEM; | |
849 | ||
36e56a34 AS |
850 | return 0; |
851 | } | |
852 | ||
ddae41be | 853 | /** |
8bb54ab5 AS |
854 | * usb_register_device_driver - register a USB device (not interface) driver |
855 | * @new_udriver: USB operations for the device driver | |
2143acc6 | 856 | * @owner: module owner of this driver. |
ddae41be | 857 | * |
8bb54ab5 AS |
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. | |
626f090c YB |
861 | * |
862 | * Return: A negative error code on failure and 0 on success. | |
8bb54ab5 AS |
863 | */ |
864 | int usb_register_device_driver(struct usb_device_driver *new_udriver, | |
865 | struct module *owner) | |
866 | { | |
867 | int retval = 0; | |
868 | ||
869 | if (usb_disabled()) | |
870 | return -ENODEV; | |
871 | ||
872 | new_udriver->drvwrap.for_devices = 1; | |
9f9af82f | 873 | new_udriver->drvwrap.driver.name = new_udriver->name; |
8bb54ab5 AS |
874 | new_udriver->drvwrap.driver.bus = &usb_bus_type; |
875 | new_udriver->drvwrap.driver.probe = usb_probe_device; | |
876 | new_udriver->drvwrap.driver.remove = usb_unbind_device; | |
877 | new_udriver->drvwrap.driver.owner = owner; | |
878 | ||
879 | retval = driver_register(&new_udriver->drvwrap.driver); | |
880 | ||
fb28d58b | 881 | if (!retval) |
8bb54ab5 AS |
882 | pr_info("%s: registered new device driver %s\n", |
883 | usbcore_name, new_udriver->name); | |
fb28d58b | 884 | else |
8bb54ab5 AS |
885 | printk(KERN_ERR "%s: error %d registering device " |
886 | " driver %s\n", | |
887 | usbcore_name, retval, new_udriver->name); | |
8bb54ab5 AS |
888 | |
889 | 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 | void usb_deregister_device_driver(struct usb_device_driver *udriver) | |
901 | { | |
902 | pr_info("%s: deregistering device driver %s\n", | |
903 | usbcore_name, udriver->name); | |
904 | ||
905 | driver_unregister(&udriver->drvwrap.driver); | |
8bb54ab5 AS |
906 | } |
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. | |
892705a1 | 913 | * @mod_name: module name string |
8bb54ab5 AS |
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. | |
626f090c YB |
918 | * |
919 | * Return: A negative error code on failure and 0 on success. | |
ddae41be GKH |
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 | */ | |
80f745fb GKH |
925 | int usb_register_driver(struct usb_driver *new_driver, struct module *owner, |
926 | const char *mod_name) | |
ddae41be GKH |
927 | { |
928 | int retval = 0; | |
929 | ||
930 | if (usb_disabled()) | |
931 | return -ENODEV; | |
932 | ||
8bb54ab5 | 933 | new_driver->drvwrap.for_devices = 0; |
9f9af82f | 934 | new_driver->drvwrap.driver.name = new_driver->name; |
8bb54ab5 AS |
935 | new_driver->drvwrap.driver.bus = &usb_bus_type; |
936 | new_driver->drvwrap.driver.probe = usb_probe_interface; | |
937 | new_driver->drvwrap.driver.remove = usb_unbind_interface; | |
938 | new_driver->drvwrap.driver.owner = owner; | |
80f745fb | 939 | new_driver->drvwrap.driver.mod_name = mod_name; |
733260ff GKH |
940 | spin_lock_init(&new_driver->dynids.lock); |
941 | INIT_LIST_HEAD(&new_driver->dynids.list); | |
ddae41be | 942 | |
8bb54ab5 | 943 | retval = driver_register(&new_driver->drvwrap.driver); |
0c7a2b72 CR |
944 | if (retval) |
945 | goto out; | |
ddae41be | 946 | |
ed283e9f | 947 | retval = usb_create_newid_files(new_driver); |
0c7a2b72 CR |
948 | if (retval) |
949 | goto out_newid; | |
950 | ||
0c7a2b72 | 951 | pr_info("%s: registered new interface driver %s\n", |
ddae41be | 952 | usbcore_name, new_driver->name); |
ddae41be | 953 | |
0c7a2b72 | 954 | out: |
ddae41be | 955 | return retval; |
0c7a2b72 | 956 | |
0c7a2b72 CR |
957 | out_newid: |
958 | driver_unregister(&new_driver->drvwrap.driver); | |
959 | ||
960 | printk(KERN_ERR "%s: error %d registering interface " | |
961 | " driver %s\n", | |
962 | usbcore_name, retval, new_driver->name); | |
963 | goto out; | |
ddae41be | 964 | } |
782e70c6 | 965 | EXPORT_SYMBOL_GPL(usb_register_driver); |
ddae41be GKH |
966 | |
967 | /** | |
8bb54ab5 AS |
968 | * usb_deregister - unregister a USB interface driver |
969 | * @driver: USB operations of the interface driver to unregister | |
ddae41be GKH |
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 | void usb_deregister(struct usb_driver *driver) | |
979 | { | |
8bb54ab5 AS |
980 | pr_info("%s: deregistering interface driver %s\n", |
981 | usbcore_name, driver->name); | |
ddae41be | 982 | |
ed283e9f | 983 | usb_remove_newid_files(driver); |
8bb54ab5 | 984 | driver_unregister(&driver->drvwrap.driver); |
ed283e9f | 985 | usb_free_dynids(driver); |
ddae41be | 986 | } |
782e70c6 | 987 | EXPORT_SYMBOL_GPL(usb_deregister); |
36e56a34 | 988 | |
78d9a487 AS |
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 | void usb_forced_unbind_intf(struct usb_interface *intf) | |
997 | { | |
998 | struct usb_driver *driver = to_usb_driver(intf->dev.driver); | |
999 | ||
1000 | dev_dbg(&intf->dev, "forced unbind\n"); | |
1001 | usb_driver_release_interface(driver, intf); | |
1002 | ||
1003 | /* Mark the interface for later rebinding */ | |
1004 | intf->needs_binding = 1; | |
1005 | } | |
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 | * | |
5096aedc AS |
1013 | * Note: Rebinds will be skipped if a system sleep transition is in |
1014 | * progress and the PM "complete" callback hasn't occurred yet. | |
78d9a487 AS |
1015 | */ |
1016 | void usb_rebind_intf(struct usb_interface *intf) | |
1017 | { | |
1018 | int rc; | |
1019 | ||
1020 | /* Delayed unbind of an existing driver */ | |
1493138a ON |
1021 | if (intf->dev.driver) |
1022 | usb_forced_unbind_intf(intf); | |
78d9a487 AS |
1023 | |
1024 | /* Try to rebind the interface */ | |
f76b168b | 1025 | if (!intf->dev.power.is_prepared) { |
5096aedc AS |
1026 | intf->needs_binding = 0; |
1027 | rc = device_attach(&intf->dev); | |
1028 | if (rc < 0) | |
1029 | dev_warn(&intf->dev, "rebind failed: %d\n", rc); | |
1030 | } | |
78d9a487 AS |
1031 | } |
1032 | ||
9ff78433 AS |
1033 | #ifdef CONFIG_PM |
1034 | ||
1493138a ON |
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. | |
78d9a487 AS |
1038 | * |
1039 | * The caller must hold @udev's device lock. | |
78d9a487 | 1040 | */ |
1493138a | 1041 | static void unbind_no_pm_drivers_interfaces(struct usb_device *udev) |
78d9a487 AS |
1042 | { |
1043 | struct usb_host_config *config; | |
1044 | int i; | |
1045 | struct usb_interface *intf; | |
1046 | struct usb_driver *drv; | |
1047 | ||
1048 | config = udev->actconfig; | |
1049 | if (config) { | |
1050 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { | |
1051 | intf = config->interface[i]; | |
1493138a ON |
1052 | |
1053 | if (intf->dev.driver) { | |
1054 | drv = to_usb_driver(intf->dev.driver); | |
1055 | if (!drv->suspend || !drv->resume) | |
1056 | usb_forced_unbind_intf(intf); | |
78d9a487 AS |
1057 | } |
1058 | } | |
1059 | } | |
1060 | } | |
1061 | ||
1493138a ON |
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 | 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 | config = udev->actconfig; | |
1074 | if (config) { | |
1075 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { | |
1076 | intf = config->interface[i]; | |
1077 | if (intf->dev.driver && intf->needs_binding) | |
1078 | usb_forced_unbind_intf(intf); | |
1079 | } | |
1080 | } | |
1081 | } | |
1082 | ||
1083 | 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 | config = udev->actconfig; | |
1090 | if (config) { | |
1091 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { | |
1092 | intf = config->interface[i]; | |
1093 | if (intf->needs_binding) | |
1094 | usb_rebind_intf(intf); | |
1095 | } | |
1096 | } | |
1097 | } | |
1098 | ||
d5ec1686 | 1099 | static int usb_suspend_device(struct usb_device *udev, pm_message_t msg) |
36e56a34 | 1100 | { |
782da727 | 1101 | struct usb_device_driver *udriver; |
2bf4086d | 1102 | int status = 0; |
36e56a34 | 1103 | |
114b368c AS |
1104 | if (udev->state == USB_STATE_NOTATTACHED || |
1105 | udev->state == USB_STATE_SUSPENDED) | |
1106 | goto done; | |
1107 | ||
b6f6436d AS |
1108 | /* For devices that don't have a driver, we do a generic suspend. */ |
1109 | if (udev->dev.driver) | |
1110 | udriver = to_usb_device_driver(udev->dev.driver); | |
1111 | else { | |
645daaab | 1112 | udev->do_remote_wakeup = 0; |
b6f6436d | 1113 | udriver = &usb_generic_driver; |
1c5df7e7 | 1114 | } |
2bf4086d AS |
1115 | status = udriver->suspend(udev, msg); |
1116 | ||
20dfdad7 | 1117 | done: |
441b62c1 | 1118 | dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); |
2bf4086d | 1119 | return status; |
1cc8a25d AS |
1120 | } |
1121 | ||
65bfd296 | 1122 | static int usb_resume_device(struct usb_device *udev, pm_message_t msg) |
1cc8a25d AS |
1123 | { |
1124 | struct usb_device_driver *udriver; | |
2bf4086d | 1125 | int status = 0; |
36e56a34 | 1126 | |
0458d5b4 AS |
1127 | if (udev->state == USB_STATE_NOTATTACHED) |
1128 | goto done; | |
1cc8a25d | 1129 | |
1c5df7e7 AS |
1130 | /* Can't resume it if it doesn't have a driver. */ |
1131 | if (udev->dev.driver == NULL) { | |
1132 | status = -ENOTCONN; | |
2bf4086d | 1133 | goto done; |
1c5df7e7 AS |
1134 | } |
1135 | ||
6d19c009 AS |
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 | */ | |
5b1b0b81 | 1139 | if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion) |
6d19c009 AS |
1140 | device_pm_wait_for_dev(&udev->dev, |
1141 | &udev->bus->hs_companion->root_hub->dev); | |
1142 | ||
6bc6cff5 AS |
1143 | if (udev->quirks & USB_QUIRK_RESET_RESUME) |
1144 | udev->reset_resume = 1; | |
1145 | ||
1cc8a25d | 1146 | udriver = to_usb_device_driver(udev->dev.driver); |
65bfd296 | 1147 | status = udriver->resume(udev, msg); |
2bf4086d | 1148 | |
20dfdad7 | 1149 | done: |
441b62c1 | 1150 | dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); |
2bf4086d | 1151 | return status; |
1cc8a25d AS |
1152 | } |
1153 | ||
65605ae8 AS |
1154 | static int usb_suspend_interface(struct usb_device *udev, |
1155 | struct usb_interface *intf, pm_message_t msg) | |
1cc8a25d AS |
1156 | { |
1157 | struct usb_driver *driver; | |
2bf4086d | 1158 | int status = 0; |
1cc8a25d | 1159 | |
9bbdf1e0 AS |
1160 | if (udev->state == USB_STATE_NOTATTACHED || |
1161 | intf->condition == USB_INTERFACE_UNBOUND) | |
2bf4086d | 1162 | goto done; |
114b368c | 1163 | driver = to_usb_driver(intf->dev.driver); |
36e56a34 | 1164 | |
e78832cd ON |
1165 | /* at this time we know the driver supports suspend */ |
1166 | status = driver->suspend(intf, msg); | |
1167 | if (status && !PMSG_IS_AUTO(msg)) | |
1168 | dev_err(&intf->dev, "suspend error %d\n", status); | |
2bf4086d | 1169 | |
20dfdad7 | 1170 | done: |
441b62c1 | 1171 | dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); |
36e56a34 AS |
1172 | return status; |
1173 | } | |
1174 | ||
65605ae8 | 1175 | static int usb_resume_interface(struct usb_device *udev, |
65bfd296 | 1176 | struct usb_interface *intf, pm_message_t msg, int reset_resume) |
36e56a34 | 1177 | { |
1cc8a25d | 1178 | struct usb_driver *driver; |
2bf4086d | 1179 | int status = 0; |
36e56a34 | 1180 | |
9bbdf1e0 | 1181 | if (udev->state == USB_STATE_NOTATTACHED) |
2bf4086d | 1182 | goto done; |
36e56a34 | 1183 | |
645daaab AS |
1184 | /* Don't let autoresume interfere with unbinding */ |
1185 | if (intf->condition == USB_INTERFACE_UNBINDING) | |
1186 | goto done; | |
1187 | ||
1c5df7e7 | 1188 | /* Can't resume it if it doesn't have a driver. */ |
55151d7d AS |
1189 | if (intf->condition == USB_INTERFACE_UNBOUND) { |
1190 | ||
1191 | /* Carry out a deferred switch to altsetting 0 */ | |
f76b168b | 1192 | if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) { |
55151d7d AS |
1193 | usb_set_interface(udev, intf->altsetting[0]. |
1194 | desc.bInterfaceNumber, 0); | |
1195 | intf->needs_altsetting0 = 0; | |
1196 | } | |
78d9a487 | 1197 | goto done; |
55151d7d | 1198 | } |
78d9a487 AS |
1199 | |
1200 | /* Don't resume if the interface is marked for rebinding */ | |
1201 | if (intf->needs_binding) | |
2bf4086d | 1202 | goto done; |
1cc8a25d | 1203 | driver = to_usb_driver(intf->dev.driver); |
36e56a34 | 1204 | |
f07600cf AS |
1205 | if (reset_resume) { |
1206 | if (driver->reset_resume) { | |
1207 | status = driver->reset_resume(intf); | |
1208 | if (status) | |
1209 | dev_err(&intf->dev, "%s error %d\n", | |
1210 | "reset_resume", status); | |
1211 | } else { | |
78d9a487 | 1212 | intf->needs_binding = 1; |
0a56b4fa AS |
1213 | dev_dbg(&intf->dev, "no reset_resume for driver %s?\n", |
1214 | driver->name); | |
f07600cf AS |
1215 | } |
1216 | } else { | |
e78832cd ON |
1217 | status = driver->resume(intf); |
1218 | if (status) | |
1219 | dev_err(&intf->dev, "resume error %d\n", status); | |
f07600cf | 1220 | } |
2bf4086d AS |
1221 | |
1222 | done: | |
441b62c1 | 1223 | dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); |
f07600cf | 1224 | |
78d9a487 | 1225 | /* Later we will unbind the driver and/or reprobe, if necessary */ |
2bf4086d | 1226 | return status; |
36e56a34 AS |
1227 | } |
1228 | ||
645daaab AS |
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 | |
303f0847 ML |
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. | |
645daaab | 1244 | * |
9bbdf1e0 AS |
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. | |
645daaab AS |
1251 | * |
1252 | * This routine can run only in process context. | |
626f090c YB |
1253 | * |
1254 | * Return: 0 if the suspend succeeded. | |
645daaab | 1255 | */ |
718efa64 | 1256 | static int usb_suspend_both(struct usb_device *udev, pm_message_t msg) |
a8e7c565 AS |
1257 | { |
1258 | int status = 0; | |
571dc79d | 1259 | int i = 0, n = 0; |
a8e7c565 | 1260 | struct usb_interface *intf; |
645daaab | 1261 | |
1941044a AS |
1262 | if (udev->state == USB_STATE_NOTATTACHED || |
1263 | udev->state == USB_STATE_SUSPENDED) | |
1264 | goto done; | |
a8e7c565 | 1265 | |
645daaab | 1266 | /* Suspend all the interfaces and then udev itself */ |
a8e7c565 | 1267 | if (udev->actconfig) { |
571dc79d AS |
1268 | n = udev->actconfig->desc.bNumInterfaces; |
1269 | for (i = n - 1; i >= 0; --i) { | |
a8e7c565 | 1270 | intf = udev->actconfig->interface[i]; |
65605ae8 | 1271 | status = usb_suspend_interface(udev, intf, msg); |
0af212ba AS |
1272 | |
1273 | /* Ignore errors during system sleep transitions */ | |
5b1b0b81 | 1274 | if (!PMSG_IS_AUTO(msg)) |
0af212ba | 1275 | status = 0; |
a8e7c565 AS |
1276 | if (status != 0) |
1277 | break; | |
1278 | } | |
1279 | } | |
0af212ba | 1280 | if (status == 0) { |
d5ec1686 | 1281 | status = usb_suspend_device(udev, msg); |
a8e7c565 | 1282 | |
cd4376e2 AS |
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 | if (udev->parent && !PMSG_IS_AUTO(msg)) | |
0af212ba AS |
1290 | status = 0; |
1291 | } | |
1292 | ||
a8e7c565 AS |
1293 | /* If the suspend failed, resume interfaces that did get suspended */ |
1294 | if (status != 0) { | |
505bdbc7 CG |
1295 | if (udev->actconfig) { |
1296 | msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME); | |
1297 | while (++i < n) { | |
1298 | intf = udev->actconfig->interface[i]; | |
1299 | usb_resume_interface(udev, intf, msg, 0); | |
1300 | } | |
a8e7c565 | 1301 | } |
645daaab | 1302 | |
9bbdf1e0 AS |
1303 | /* If the suspend succeeded then prevent any more URB submissions |
1304 | * and flush any outstanding URBs. | |
6840d255 | 1305 | */ |
ef7f6c70 | 1306 | } else { |
6840d255 AS |
1307 | udev->can_submit = 0; |
1308 | for (i = 0; i < 16; ++i) { | |
1309 | usb_hcd_flush_endpoint(udev, udev->ep_out[i]); | |
1310 | usb_hcd_flush_endpoint(udev, udev->ep_in[i]); | |
1311 | } | |
ef7f6c70 | 1312 | } |
645daaab | 1313 | |
1941044a | 1314 | done: |
441b62c1 | 1315 | dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); |
a8e7c565 AS |
1316 | return status; |
1317 | } | |
1318 | ||
645daaab AS |
1319 | /** |
1320 | * usb_resume_both - resume a USB device and its interfaces | |
1321 | * @udev: the usb_device to resume | |
65bfd296 | 1322 | * @msg: Power Management message describing this state transition |
645daaab AS |
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 | * | |
9bbdf1e0 AS |
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. | |
645daaab AS |
1334 | * |
1335 | * This routine can run only in process context. | |
626f090c YB |
1336 | * |
1337 | * Return: 0 on success. | |
645daaab | 1338 | */ |
65bfd296 | 1339 | static int usb_resume_both(struct usb_device *udev, pm_message_t msg) |
a8e7c565 | 1340 | { |
645daaab | 1341 | int status = 0; |
a8e7c565 AS |
1342 | int i; |
1343 | struct usb_interface *intf; | |
645daaab | 1344 | |
1941044a AS |
1345 | if (udev->state == USB_STATE_NOTATTACHED) { |
1346 | status = -ENODEV; | |
1347 | goto done; | |
1348 | } | |
6840d255 | 1349 | udev->can_submit = 1; |
a8e7c565 | 1350 | |
9bbdf1e0 AS |
1351 | /* Resume the device */ |
1352 | if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume) | |
65bfd296 | 1353 | status = usb_resume_device(udev, msg); |
114b368c | 1354 | |
9bbdf1e0 | 1355 | /* Resume the interfaces */ |
a8e7c565 AS |
1356 | if (status == 0 && udev->actconfig) { |
1357 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { | |
1358 | intf = udev->actconfig->interface[i]; | |
65bfd296 AS |
1359 | usb_resume_interface(udev, intf, msg, |
1360 | udev->reset_resume); | |
a8e7c565 AS |
1361 | } |
1362 | } | |
c08512c7 | 1363 | usb_mark_last_busy(udev); |
645daaab | 1364 | |
1941044a | 1365 | done: |
441b62c1 | 1366 | dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); |
70a1c9e0 AS |
1367 | if (!status) |
1368 | udev->reset_resume = 0; | |
645daaab AS |
1369 | return status; |
1370 | } | |
1371 | ||
5f677f1d AS |
1372 | static void choose_wakeup(struct usb_device *udev, pm_message_t msg) |
1373 | { | |
48826626 | 1374 | int w; |
5f677f1d AS |
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 | if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) { | |
1381 | if (udev->state != USB_STATE_SUSPENDED) | |
1382 | udev->do_remote_wakeup = 0; | |
1383 | return; | |
1384 | } | |
1385 | ||
48826626 | 1386 | /* Enable remote wakeup if it is allowed, even if no interface drivers |
5f677f1d AS |
1387 | * actually want it. |
1388 | */ | |
48826626 | 1389 | w = device_may_wakeup(&udev->dev); |
5f677f1d AS |
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 | udev->do_remote_wakeup = w; | |
1397 | } | |
1398 | ||
9bbdf1e0 | 1399 | /* The device lock is held by the PM core */ |
0c590e23 AS |
1400 | int usb_suspend(struct device *dev, pm_message_t msg) |
1401 | { | |
9bbdf1e0 | 1402 | struct usb_device *udev = to_usb_device(dev); |
0c590e23 | 1403 | |
1493138a ON |
1404 | 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 | */ | |
5f677f1d | 1410 | choose_wakeup(udev, msg); |
9bbdf1e0 | 1411 | return usb_suspend_both(udev, msg); |
0c590e23 AS |
1412 | } |
1413 | ||
9bbdf1e0 | 1414 | /* The device lock is held by the PM core */ |
98d9a82e | 1415 | int usb_resume_complete(struct device *dev) |
0c590e23 | 1416 | { |
98d9a82e | 1417 | struct usb_device *udev = to_usb_device(dev); |
0c590e23 | 1418 | |
1493138a ON |
1419 | /* For PM complete calls, all we do is rebind interfaces |
1420 | * whose needs_binding flag is set | |
1421 | */ | |
98d9a82e ON |
1422 | if (udev->state != USB_STATE_NOTATTACHED) |
1423 | do_rebind_interfaces(udev); | |
1424 | return 0; | |
1425 | } | |
0c590e23 | 1426 | |
9bbdf1e0 | 1427 | /* The device lock is held by the PM core */ |
0c590e23 AS |
1428 | int usb_resume(struct device *dev, pm_message_t msg) |
1429 | { | |
9bbdf1e0 | 1430 | struct usb_device *udev = to_usb_device(dev); |
0c590e23 AS |
1431 | int status; |
1432 | ||
98d9a82e | 1433 | /* For all calls, take the device back to full power and |
9bbdf1e0 | 1434 | * tell the PM core in case it was autosuspended previously. |
1493138a ON |
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() | |
98d9a82e | 1438 | * above because it doesn't own the right set of locks.) |
0c590e23 | 1439 | */ |
98d9a82e ON |
1440 | status = usb_resume_both(udev, msg); |
1441 | if (status == 0) { | |
1442 | pm_runtime_disable(dev); | |
1443 | pm_runtime_set_active(dev); | |
1444 | pm_runtime_enable(dev); | |
1445 | unbind_no_reset_resume_drivers_interfaces(udev); | |
9bbdf1e0 | 1446 | } |
0c590e23 AS |
1447 | |
1448 | /* Avoid PM error messages for devices disconnected while suspended | |
1449 | * as we'll display regular disconnect messages just a bit later. | |
1450 | */ | |
7491f133 | 1451 | if (status == -ENODEV || status == -ESHUTDOWN) |
9bbdf1e0 | 1452 | status = 0; |
0c590e23 AS |
1453 | return status; |
1454 | } | |
1455 | ||
1456 | #endif /* CONFIG_PM */ | |
1457 | ||
84ebc102 | 1458 | #ifdef CONFIG_PM_RUNTIME |
645daaab | 1459 | |
088f7fec AS |
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 | */ | |
9e18c821 | 1470 | void usb_enable_autosuspend(struct usb_device *udev) |
088f7fec | 1471 | { |
9e18c821 | 1472 | pm_runtime_allow(&udev->dev); |
088f7fec AS |
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 | */ | |
9e18c821 | 1485 | void usb_disable_autosuspend(struct usb_device *udev) |
088f7fec | 1486 | { |
9e18c821 | 1487 | pm_runtime_forbid(&udev->dev); |
088f7fec AS |
1488 | } |
1489 | EXPORT_SYMBOL_GPL(usb_disable_autosuspend); | |
1490 | ||
645daaab AS |
1491 | /** |
1492 | * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces | |
701f35af | 1493 | * @udev: the usb_device to autosuspend |
645daaab AS |
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 | * | |
9bbdf1e0 AS |
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()). | |
645daaab | 1502 | * |
62e299e6 | 1503 | * The caller must hold @udev's device lock. |
645daaab AS |
1504 | * |
1505 | * This routine can run only in process context. | |
1506 | */ | |
94fcda1f | 1507 | void usb_autosuspend_device(struct usb_device *udev) |
645daaab | 1508 | { |
94fcda1f AS |
1509 | int status; |
1510 | ||
6ddf27cd | 1511 | usb_mark_last_busy(udev); |
fcc4a01e | 1512 | status = pm_runtime_put_sync_autosuspend(&udev->dev); |
9bbdf1e0 AS |
1513 | dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", |
1514 | __func__, atomic_read(&udev->dev.power.usage_count), | |
1515 | status); | |
645daaab AS |
1516 | } |
1517 | ||
1518 | /** | |
1519 | * usb_autoresume_device - immediately autoresume a USB device and its interfaces | |
701f35af | 1520 | * @udev: the usb_device to autoresume |
645daaab AS |
1521 | * |
1522 | * This routine should be called when a core subsystem wants to use @udev | |
94fcda1f | 1523 | * and needs to guarantee that it is not suspended. No autosuspend will |
9bbdf1e0 AS |
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 | |
94fcda1f | 1527 | * request is received. |
645daaab | 1528 | * |
94fcda1f AS |
1529 | * @udev's usage counter is incremented to prevent subsequent autosuspends. |
1530 | * However if the autoresume fails then the usage counter is re-decremented. | |
645daaab | 1531 | * |
62e299e6 | 1532 | * The caller must hold @udev's device lock. |
645daaab AS |
1533 | * |
1534 | * This routine can run only in process context. | |
626f090c YB |
1535 | * |
1536 | * Return: 0 on success. A negative error code otherwise. | |
645daaab | 1537 | */ |
94fcda1f | 1538 | int usb_autoresume_device(struct usb_device *udev) |
645daaab AS |
1539 | { |
1540 | int status; | |
1541 | ||
9bbdf1e0 AS |
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; | |
af4f7606 AS |
1550 | return status; |
1551 | } | |
1552 | ||
645daaab AS |
1553 | /** |
1554 | * usb_autopm_put_interface - decrement a USB interface's PM-usage counter | |
701f35af | 1555 | * @intf: the usb_interface whose counter should be decremented |
645daaab AS |
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 | |
9bbdf1e0 AS |
1563 | * 0, a delayed autosuspend request for @intf's device is attempted. The |
1564 | * attempt may fail (see autosuspend_check()). | |
645daaab | 1565 | * |
645daaab AS |
1566 | * This routine can run only in process context. |
1567 | */ | |
1568 | void usb_autopm_put_interface(struct usb_interface *intf) | |
1569 | { | |
9bbdf1e0 AS |
1570 | struct usb_device *udev = interface_to_usbdev(intf); |
1571 | int status; | |
645daaab | 1572 | |
6ddf27cd | 1573 | usb_mark_last_busy(udev); |
9bbdf1e0 AS |
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); | |
645daaab AS |
1579 | } |
1580 | EXPORT_SYMBOL_GPL(usb_autopm_put_interface); | |
1581 | ||
9ac39f28 AS |
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 | * | |
9bbdf1e0 AS |
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. | |
9ac39f28 AS |
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); | |
fcc4a01e | 1600 | int status; |
9ac39f28 | 1601 | |
6ddf27cd | 1602 | usb_mark_last_busy(udev); |
9bbdf1e0 | 1603 | atomic_dec(&intf->pm_usage_cnt); |
fcc4a01e | 1604 | status = pm_runtime_put(&intf->dev); |
9bbdf1e0 AS |
1605 | dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", |
1606 | __func__, atomic_read(&intf->dev.power.usage_count), | |
1607 | status); | |
9ac39f28 AS |
1608 | } |
1609 | EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async); | |
1610 | ||
9bbdf1e0 AS |
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 | ||
6ddf27cd | 1624 | usb_mark_last_busy(udev); |
9bbdf1e0 AS |
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 | ||
645daaab AS |
1630 | /** |
1631 | * usb_autopm_get_interface - increment a USB interface's PM-usage counter | |
701f35af | 1632 | * @intf: the usb_interface whose counter should be incremented |
645daaab AS |
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 | * | |
9bbdf1e0 AS |
1642 | * @intf's usage counter is incremented to prevent subsequent autosuspends. |
1643 | * However if the autoresume fails then the counter is re-decremented. | |
645daaab AS |
1644 | * |
1645 | * This routine can run only in process context. | |
626f090c YB |
1646 | * |
1647 | * Return: 0 on success. | |
645daaab AS |
1648 | */ |
1649 | int usb_autopm_get_interface(struct usb_interface *intf) | |
1650 | { | |
af4f7606 | 1651 | int status; |
645daaab | 1652 | |
9bbdf1e0 AS |
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; | |
a8e7c565 AS |
1663 | return status; |
1664 | } | |
645daaab AS |
1665 | EXPORT_SYMBOL_GPL(usb_autopm_get_interface); |
1666 | ||
9ac39f28 AS |
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 | |
9bbdf1e0 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. | |
9ac39f28 AS |
1679 | * |
1680 | * This routine can run in atomic context. | |
626f090c YB |
1681 | * |
1682 | * Return: 0 on success. A negative error code otherwise. | |
9ac39f28 AS |
1683 | */ |
1684 | int usb_autopm_get_interface_async(struct usb_interface *intf) | |
1685 | { | |
63defa73 | 1686 | int status; |
9bbdf1e0 | 1687 | |
63defa73 | 1688 | status = pm_runtime_get(&intf->dev); |
9bbdf1e0 AS |
1689 | if (status < 0 && status != -EINPROGRESS) |
1690 | pm_runtime_put_noidle(&intf->dev); | |
1691 | else | |
ccf5b801 | 1692 | atomic_inc(&intf->pm_usage_cnt); |
9bbdf1e0 AS |
1693 | dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", |
1694 | __func__, atomic_read(&intf->dev.power.usage_count), | |
1695 | status); | |
c5a48592 | 1696 | if (status > 0 || status == -EINPROGRESS) |
9bbdf1e0 | 1697 | status = 0; |
9ac39f28 AS |
1698 | return status; |
1699 | } | |
1700 | EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async); | |
1701 | ||
9bbdf1e0 AS |
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 | ||
6ddf27cd | 1715 | usb_mark_last_busy(udev); |
9bbdf1e0 AS |
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 | { | |
7560d32e | 1724 | int w, i; |
9bbdf1e0 | 1725 | struct usb_interface *intf; |
9bbdf1e0 AS |
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 | */ | |
7560d32e | 1730 | w = 0; |
9bbdf1e0 AS |
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; | |
7560d32e | 1744 | w |= intf->needs_remote_wakeup; |
9bbdf1e0 AS |
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 | } | |
7560d32e AS |
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; | |
9bbdf1e0 AS |
1765 | return 0; |
1766 | } | |
1767 | ||
e1620d59 | 1768 | int usb_runtime_suspend(struct device *dev) |
9bbdf1e0 | 1769 | { |
63defa73 ML |
1770 | struct usb_device *udev = to_usb_device(dev); |
1771 | int status; | |
718efa64 | 1772 | |
9bbdf1e0 AS |
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 | */ | |
63defa73 ML |
1777 | if (autosuspend_check(udev) != 0) |
1778 | return -EAGAIN; | |
9bbdf1e0 | 1779 | |
63defa73 | 1780 | status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND); |
b2c0a863 AS |
1781 | |
1782 | /* Allow a retry if autosuspend failed temporarily */ | |
1783 | if (status == -EAGAIN || status == -EBUSY) | |
1784 | usb_mark_last_busy(udev); | |
1785 | ||
db7c7c0a SS |
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; | |
9bbdf1e0 AS |
1791 | return status; |
1792 | } | |
1793 | ||
e1620d59 | 1794 | int usb_runtime_resume(struct device *dev) |
9bbdf1e0 | 1795 | { |
63defa73 ML |
1796 | struct usb_device *udev = to_usb_device(dev); |
1797 | int status; | |
1798 | ||
9bbdf1e0 AS |
1799 | /* Runtime resume for a USB device means resuming both the device |
1800 | * and all its interfaces. | |
1801 | */ | |
63defa73 | 1802 | status = usb_resume_both(udev, PMSG_AUTO_RESUME); |
63defa73 | 1803 | return status; |
9bbdf1e0 AS |
1804 | } |
1805 | ||
e1620d59 | 1806 | int usb_runtime_idle(struct device *dev) |
9bbdf1e0 | 1807 | { |
63defa73 ML |
1808 | struct usb_device *udev = to_usb_device(dev); |
1809 | ||
9bbdf1e0 | 1810 | /* An idle USB device can be suspended if it passes the various |
63defa73 | 1811 | * autosuspend checks. |
9bbdf1e0 | 1812 | */ |
63defa73 | 1813 | if (autosuspend_check(udev) == 0) |
fcc4a01e | 1814 | pm_runtime_autosuspend(dev); |
45f0a85c RW |
1815 | /* Tell the core not to suspend it, though. */ |
1816 | return -EBUSY; | |
9bbdf1e0 AS |
1817 | } |
1818 | ||
65580b43 AX |
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 | ||
de68bab4 SS |
1824 | if (enable && !udev->usb2_hw_lpm_allowed) |
1825 | return 0; | |
1826 | ||
65580b43 AX |
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 | ||
84ebc102 | 1836 | #endif /* CONFIG_PM_RUNTIME */ |
a8e7c565 | 1837 | |
36e56a34 AS |
1838 | struct bus_type usb_bus_type = { |
1839 | .name = "usb", | |
1840 | .match = usb_device_match, | |
1841 | .uevent = usb_uevent, | |
36e56a34 | 1842 | }; |