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