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 GKH |
25 | #include <linux/device.h> |
26 | #include <linux/usb.h> | |
27 | #include "hcd.h" | |
28 | #include "usb.h" | |
29 | ||
733260ff GKH |
30 | static int usb_match_one_id(struct usb_interface *interface, |
31 | const struct usb_device_id *id); | |
32 | ||
33 | struct usb_dynid { | |
34 | struct list_head node; | |
35 | struct usb_device_id id; | |
36 | }; | |
37 | ||
733260ff GKH |
38 | #ifdef CONFIG_HOTPLUG |
39 | ||
40 | /* | |
41 | * Adds a new dynamic USBdevice ID to this driver, | |
42 | * and cause the driver to probe for all devices again. | |
43 | */ | |
44 | static ssize_t store_new_id(struct device_driver *driver, | |
45 | const char *buf, size_t count) | |
46 | { | |
47 | struct usb_driver *usb_drv = to_usb_driver(driver); | |
48 | struct usb_dynid *dynid; | |
49 | u32 idVendor = 0; | |
50 | u32 idProduct = 0; | |
51 | int fields = 0; | |
1b21d5e1 | 52 | int retval = 0; |
733260ff GKH |
53 | |
54 | fields = sscanf(buf, "%x %x", &idVendor, &idProduct); | |
55 | if (fields < 2) | |
56 | return -EINVAL; | |
57 | ||
58 | dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); | |
59 | if (!dynid) | |
60 | return -ENOMEM; | |
61 | ||
62 | INIT_LIST_HEAD(&dynid->node); | |
63 | dynid->id.idVendor = idVendor; | |
64 | dynid->id.idProduct = idProduct; | |
65 | dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE; | |
66 | ||
67 | spin_lock(&usb_drv->dynids.lock); | |
68 | list_add_tail(&usb_drv->dynids.list, &dynid->node); | |
69 | spin_unlock(&usb_drv->dynids.lock); | |
70 | ||
71 | if (get_driver(driver)) { | |
1b21d5e1 | 72 | retval = driver_attach(driver); |
733260ff GKH |
73 | put_driver(driver); |
74 | } | |
75 | ||
1b21d5e1 GKH |
76 | if (retval) |
77 | return retval; | |
733260ff GKH |
78 | return count; |
79 | } | |
80 | static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id); | |
81 | ||
82 | static int usb_create_newid_file(struct usb_driver *usb_drv) | |
83 | { | |
84 | int error = 0; | |
85 | ||
ba9dc657 GKH |
86 | if (usb_drv->no_dynamic_id) |
87 | goto exit; | |
88 | ||
733260ff | 89 | if (usb_drv->probe != NULL) |
8bb54ab5 | 90 | error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj, |
733260ff | 91 | &driver_attr_new_id.attr); |
ba9dc657 | 92 | exit: |
733260ff GKH |
93 | return error; |
94 | } | |
95 | ||
ba9dc657 GKH |
96 | static void usb_remove_newid_file(struct usb_driver *usb_drv) |
97 | { | |
98 | if (usb_drv->no_dynamic_id) | |
99 | return; | |
100 | ||
101 | if (usb_drv->probe != NULL) | |
8bb54ab5 | 102 | sysfs_remove_file(&usb_drv->drvwrap.driver.kobj, |
ba9dc657 GKH |
103 | &driver_attr_new_id.attr); |
104 | } | |
105 | ||
733260ff GKH |
106 | static void usb_free_dynids(struct usb_driver *usb_drv) |
107 | { | |
108 | struct usb_dynid *dynid, *n; | |
109 | ||
110 | spin_lock(&usb_drv->dynids.lock); | |
111 | list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) { | |
112 | list_del(&dynid->node); | |
113 | kfree(dynid); | |
114 | } | |
115 | spin_unlock(&usb_drv->dynids.lock); | |
116 | } | |
117 | #else | |
118 | static inline int usb_create_newid_file(struct usb_driver *usb_drv) | |
119 | { | |
120 | return 0; | |
121 | } | |
122 | ||
ba9dc657 GKH |
123 | static void usb_remove_newid_file(struct usb_driver *usb_drv) |
124 | { | |
125 | } | |
126 | ||
733260ff GKH |
127 | static inline void usb_free_dynids(struct usb_driver *usb_drv) |
128 | { | |
129 | } | |
130 | #endif | |
131 | ||
132 | static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf, | |
133 | struct usb_driver *drv) | |
134 | { | |
135 | struct usb_dynid *dynid; | |
136 | ||
137 | spin_lock(&drv->dynids.lock); | |
138 | list_for_each_entry(dynid, &drv->dynids.list, node) { | |
139 | if (usb_match_one_id(intf, &dynid->id)) { | |
140 | spin_unlock(&drv->dynids.lock); | |
141 | return &dynid->id; | |
142 | } | |
143 | } | |
144 | spin_unlock(&drv->dynids.lock); | |
145 | return NULL; | |
146 | } | |
147 | ||
148 | ||
8bb54ab5 AS |
149 | /* called from driver core with dev locked */ |
150 | static int usb_probe_device(struct device *dev) | |
151 | { | |
152 | struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); | |
153 | struct usb_device *udev; | |
154 | int error = -ENODEV; | |
155 | ||
156 | dev_dbg(dev, "%s\n", __FUNCTION__); | |
157 | ||
158 | if (!is_usb_device(dev)) /* Sanity check */ | |
159 | return error; | |
160 | ||
161 | udev = to_usb_device(dev); | |
162 | ||
8bb54ab5 AS |
163 | /* TODO: Add real matching code */ |
164 | ||
645daaab AS |
165 | /* The device should always appear to be in use |
166 | * unless the driver suports autosuspend. | |
167 | */ | |
168 | udev->pm_usage_cnt = !(udriver->supports_autosuspend); | |
169 | ||
8bb54ab5 AS |
170 | error = udriver->probe(udev); |
171 | return error; | |
172 | } | |
173 | ||
174 | /* called from driver core with dev locked */ | |
175 | static int usb_unbind_device(struct device *dev) | |
176 | { | |
177 | struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); | |
178 | ||
179 | udriver->disconnect(to_usb_device(dev)); | |
180 | return 0; | |
181 | } | |
182 | ||
183 | ||
184 | /* called from driver core with dev locked */ | |
ddae41be GKH |
185 | static int usb_probe_interface(struct device *dev) |
186 | { | |
8bb54ab5 AS |
187 | struct usb_driver *driver = to_usb_driver(dev->driver); |
188 | struct usb_interface *intf; | |
645daaab | 189 | struct usb_device *udev; |
ddae41be GKH |
190 | const struct usb_device_id *id; |
191 | int error = -ENODEV; | |
192 | ||
193 | dev_dbg(dev, "%s\n", __FUNCTION__); | |
194 | ||
8bb54ab5 | 195 | if (is_usb_device(dev)) /* Sanity check */ |
ddae41be | 196 | return error; |
8bb54ab5 AS |
197 | |
198 | intf = to_usb_interface(dev); | |
645daaab | 199 | udev = interface_to_usbdev(intf); |
ddae41be GKH |
200 | |
201 | id = usb_match_id(intf, driver->id_table); | |
733260ff GKH |
202 | if (!id) |
203 | id = usb_match_dynamic_id(intf, driver); | |
ddae41be GKH |
204 | if (id) { |
205 | dev_dbg(dev, "%s - got id\n", __FUNCTION__); | |
206 | ||
645daaab AS |
207 | error = usb_autoresume_device(udev, 1); |
208 | if (error) | |
209 | return error; | |
210 | ||
ddae41be GKH |
211 | /* Interface "power state" doesn't correspond to any hardware |
212 | * state whatsoever. We use it to record when it's bound to | |
213 | * a driver that may start I/0: it's not frozen/quiesced. | |
214 | */ | |
215 | mark_active(intf); | |
216 | intf->condition = USB_INTERFACE_BINDING; | |
645daaab AS |
217 | |
218 | /* The interface should always appear to be in use | |
219 | * unless the driver suports autosuspend. | |
220 | */ | |
221 | intf->pm_usage_cnt = !(driver->supports_autosuspend); | |
222 | ||
ddae41be GKH |
223 | error = driver->probe(intf, id); |
224 | if (error) { | |
225 | mark_quiesced(intf); | |
645daaab | 226 | intf->needs_remote_wakeup = 0; |
ddae41be GKH |
227 | intf->condition = USB_INTERFACE_UNBOUND; |
228 | } else | |
229 | intf->condition = USB_INTERFACE_BOUND; | |
645daaab AS |
230 | |
231 | usb_autosuspend_device(udev, 1); | |
ddae41be GKH |
232 | } |
233 | ||
234 | return error; | |
235 | } | |
236 | ||
8bb54ab5 | 237 | /* called from driver core with dev locked */ |
ddae41be GKH |
238 | static int usb_unbind_interface(struct device *dev) |
239 | { | |
8bb54ab5 | 240 | struct usb_driver *driver = to_usb_driver(dev->driver); |
ddae41be | 241 | struct usb_interface *intf = to_usb_interface(dev); |
645daaab AS |
242 | struct usb_device *udev; |
243 | int error; | |
ddae41be GKH |
244 | |
245 | intf->condition = USB_INTERFACE_UNBINDING; | |
246 | ||
645daaab AS |
247 | /* Autoresume for set_interface call below */ |
248 | udev = interface_to_usbdev(intf); | |
249 | error = usb_autoresume_device(udev, 1); | |
250 | ||
ddae41be GKH |
251 | /* release all urbs for this interface */ |
252 | usb_disable_interface(interface_to_usbdev(intf), intf); | |
253 | ||
8bb54ab5 | 254 | driver->disconnect(intf); |
ddae41be GKH |
255 | |
256 | /* reset other interface state */ | |
257 | usb_set_interface(interface_to_usbdev(intf), | |
258 | intf->altsetting[0].desc.bInterfaceNumber, | |
259 | 0); | |
260 | usb_set_intfdata(intf, NULL); | |
645daaab | 261 | |
ddae41be GKH |
262 | intf->condition = USB_INTERFACE_UNBOUND; |
263 | mark_quiesced(intf); | |
645daaab AS |
264 | intf->needs_remote_wakeup = 0; |
265 | ||
266 | if (!error) | |
267 | usb_autosuspend_device(udev, 1); | |
ddae41be GKH |
268 | |
269 | return 0; | |
270 | } | |
271 | ||
36e56a34 AS |
272 | /** |
273 | * usb_driver_claim_interface - bind a driver to an interface | |
274 | * @driver: the driver to be bound | |
275 | * @iface: the interface to which it will be bound; must be in the | |
276 | * usb device's active configuration | |
277 | * @priv: driver data associated with that interface | |
278 | * | |
279 | * This is used by usb device drivers that need to claim more than one | |
280 | * interface on a device when probing (audio and acm are current examples). | |
281 | * No device driver should directly modify internal usb_interface or | |
282 | * usb_device structure members. | |
283 | * | |
284 | * Few drivers should need to use this routine, since the most natural | |
285 | * way to bind to an interface is to return the private data from | |
286 | * the driver's probe() method. | |
287 | * | |
288 | * Callers must own the device lock and the driver model's usb_bus_type.subsys | |
289 | * writelock. So driver probe() entries don't need extra locking, | |
290 | * but other call contexts may need to explicitly claim those locks. | |
291 | */ | |
292 | int usb_driver_claim_interface(struct usb_driver *driver, | |
293 | struct usb_interface *iface, void* priv) | |
294 | { | |
295 | struct device *dev = &iface->dev; | |
645daaab | 296 | struct usb_device *udev = interface_to_usbdev(iface); |
1b21d5e1 | 297 | int retval = 0; |
36e56a34 AS |
298 | |
299 | if (dev->driver) | |
300 | return -EBUSY; | |
301 | ||
8bb54ab5 | 302 | dev->driver = &driver->drvwrap.driver; |
36e56a34 | 303 | usb_set_intfdata(iface, priv); |
645daaab AS |
304 | |
305 | mutex_lock_nested(&udev->pm_mutex, udev->level); | |
36e56a34 AS |
306 | iface->condition = USB_INTERFACE_BOUND; |
307 | mark_active(iface); | |
645daaab AS |
308 | iface->pm_usage_cnt = !(driver->supports_autosuspend); |
309 | mutex_unlock(&udev->pm_mutex); | |
36e56a34 AS |
310 | |
311 | /* if interface was already added, bind now; else let | |
312 | * the future device_add() bind it, bypassing probe() | |
313 | */ | |
314 | if (device_is_registered(dev)) | |
1b21d5e1 | 315 | retval = device_bind_driver(dev); |
36e56a34 | 316 | |
1b21d5e1 | 317 | return retval; |
36e56a34 AS |
318 | } |
319 | EXPORT_SYMBOL(usb_driver_claim_interface); | |
320 | ||
321 | /** | |
322 | * usb_driver_release_interface - unbind a driver from an interface | |
323 | * @driver: the driver to be unbound | |
324 | * @iface: the interface from which it will be unbound | |
325 | * | |
326 | * This can be used by drivers to release an interface without waiting | |
327 | * for their disconnect() methods to be called. In typical cases this | |
328 | * also causes the driver disconnect() method to be called. | |
329 | * | |
330 | * This call is synchronous, and may not be used in an interrupt context. | |
331 | * Callers must own the device lock and the driver model's usb_bus_type.subsys | |
332 | * writelock. So driver disconnect() entries don't need extra locking, | |
333 | * but other call contexts may need to explicitly claim those locks. | |
334 | */ | |
335 | void usb_driver_release_interface(struct usb_driver *driver, | |
336 | struct usb_interface *iface) | |
337 | { | |
338 | struct device *dev = &iface->dev; | |
645daaab | 339 | struct usb_device *udev = interface_to_usbdev(iface); |
36e56a34 AS |
340 | |
341 | /* this should never happen, don't release something that's not ours */ | |
8bb54ab5 | 342 | if (!dev->driver || dev->driver != &driver->drvwrap.driver) |
36e56a34 AS |
343 | return; |
344 | ||
345 | /* don't release from within disconnect() */ | |
346 | if (iface->condition != USB_INTERFACE_BOUND) | |
347 | return; | |
348 | ||
349 | /* don't release if the interface hasn't been added yet */ | |
350 | if (device_is_registered(dev)) { | |
351 | iface->condition = USB_INTERFACE_UNBINDING; | |
352 | device_release_driver(dev); | |
353 | } | |
354 | ||
355 | dev->driver = NULL; | |
356 | usb_set_intfdata(iface, NULL); | |
645daaab AS |
357 | |
358 | mutex_lock_nested(&udev->pm_mutex, udev->level); | |
36e56a34 AS |
359 | iface->condition = USB_INTERFACE_UNBOUND; |
360 | mark_quiesced(iface); | |
645daaab AS |
361 | iface->needs_remote_wakeup = 0; |
362 | mutex_unlock(&udev->pm_mutex); | |
36e56a34 AS |
363 | } |
364 | EXPORT_SYMBOL(usb_driver_release_interface); | |
365 | ||
733260ff GKH |
366 | /* returns 0 if no match, 1 if match */ |
367 | static int usb_match_one_id(struct usb_interface *interface, | |
368 | const struct usb_device_id *id) | |
369 | { | |
370 | struct usb_host_interface *intf; | |
371 | struct usb_device *dev; | |
372 | ||
373 | /* proc_connectinfo in devio.c may call us with id == NULL. */ | |
374 | if (id == NULL) | |
375 | return 0; | |
376 | ||
377 | intf = interface->cur_altsetting; | |
378 | dev = interface_to_usbdev(interface); | |
379 | ||
380 | if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && | |
381 | id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) | |
382 | return 0; | |
383 | ||
384 | if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && | |
385 | id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) | |
386 | return 0; | |
387 | ||
388 | /* No need to test id->bcdDevice_lo != 0, since 0 is never | |
389 | greater than any unsigned number. */ | |
390 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && | |
391 | (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) | |
392 | return 0; | |
393 | ||
394 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && | |
395 | (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) | |
396 | return 0; | |
397 | ||
398 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && | |
399 | (id->bDeviceClass != dev->descriptor.bDeviceClass)) | |
400 | return 0; | |
401 | ||
402 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && | |
403 | (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass)) | |
404 | return 0; | |
405 | ||
406 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && | |
407 | (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) | |
408 | return 0; | |
409 | ||
410 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && | |
411 | (id->bInterfaceClass != intf->desc.bInterfaceClass)) | |
412 | return 0; | |
413 | ||
414 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && | |
415 | (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) | |
416 | return 0; | |
417 | ||
418 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && | |
419 | (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) | |
420 | return 0; | |
421 | ||
422 | return 1; | |
423 | } | |
ddae41be GKH |
424 | /** |
425 | * usb_match_id - find first usb_device_id matching device or interface | |
426 | * @interface: the interface of interest | |
427 | * @id: array of usb_device_id structures, terminated by zero entry | |
428 | * | |
429 | * usb_match_id searches an array of usb_device_id's and returns | |
430 | * the first one matching the device or interface, or null. | |
431 | * This is used when binding (or rebinding) a driver to an interface. | |
432 | * Most USB device drivers will use this indirectly, through the usb core, | |
433 | * but some layered driver frameworks use it directly. | |
434 | * These device tables are exported with MODULE_DEVICE_TABLE, through | |
435 | * modutils, to support the driver loading functionality of USB hotplugging. | |
436 | * | |
437 | * What Matches: | |
438 | * | |
439 | * The "match_flags" element in a usb_device_id controls which | |
440 | * members are used. If the corresponding bit is set, the | |
441 | * value in the device_id must match its corresponding member | |
442 | * in the device or interface descriptor, or else the device_id | |
443 | * does not match. | |
444 | * | |
445 | * "driver_info" is normally used only by device drivers, | |
446 | * but you can create a wildcard "matches anything" usb_device_id | |
447 | * as a driver's "modules.usbmap" entry if you provide an id with | |
448 | * only a nonzero "driver_info" field. If you do this, the USB device | |
449 | * driver's probe() routine should use additional intelligence to | |
450 | * decide whether to bind to the specified interface. | |
451 | * | |
452 | * What Makes Good usb_device_id Tables: | |
453 | * | |
454 | * The match algorithm is very simple, so that intelligence in | |
455 | * driver selection must come from smart driver id records. | |
456 | * Unless you have good reasons to use another selection policy, | |
457 | * provide match elements only in related groups, and order match | |
458 | * specifiers from specific to general. Use the macros provided | |
459 | * for that purpose if you can. | |
460 | * | |
461 | * The most specific match specifiers use device descriptor | |
462 | * data. These are commonly used with product-specific matches; | |
463 | * the USB_DEVICE macro lets you provide vendor and product IDs, | |
464 | * and you can also match against ranges of product revisions. | |
465 | * These are widely used for devices with application or vendor | |
466 | * specific bDeviceClass values. | |
467 | * | |
468 | * Matches based on device class/subclass/protocol specifications | |
469 | * are slightly more general; use the USB_DEVICE_INFO macro, or | |
470 | * its siblings. These are used with single-function devices | |
471 | * where bDeviceClass doesn't specify that each interface has | |
472 | * its own class. | |
473 | * | |
474 | * Matches based on interface class/subclass/protocol are the | |
475 | * most general; they let drivers bind to any interface on a | |
476 | * multiple-function device. Use the USB_INTERFACE_INFO | |
477 | * macro, or its siblings, to match class-per-interface style | |
478 | * devices (as recorded in bDeviceClass). | |
479 | * | |
480 | * Within those groups, remember that not all combinations are | |
481 | * meaningful. For example, don't give a product version range | |
482 | * without vendor and product IDs; or specify a protocol without | |
483 | * its associated class and subclass. | |
484 | */ | |
485 | const struct usb_device_id *usb_match_id(struct usb_interface *interface, | |
486 | const struct usb_device_id *id) | |
487 | { | |
ddae41be GKH |
488 | /* proc_connectinfo in devio.c may call us with id == NULL. */ |
489 | if (id == NULL) | |
490 | return NULL; | |
491 | ||
ddae41be GKH |
492 | /* It is important to check that id->driver_info is nonzero, |
493 | since an entry that is all zeroes except for a nonzero | |
494 | id->driver_info is the way to create an entry that | |
495 | indicates that the driver want to examine every | |
496 | device and interface. */ | |
497 | for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass || | |
498 | id->driver_info; id++) { | |
733260ff GKH |
499 | if (usb_match_one_id(interface, id)) |
500 | return id; | |
ddae41be GKH |
501 | } |
502 | ||
503 | return NULL; | |
504 | } | |
b87ba0a3 | 505 | EXPORT_SYMBOL_GPL_FUTURE(usb_match_id); |
ddae41be GKH |
506 | |
507 | int usb_device_match(struct device *dev, struct device_driver *drv) | |
508 | { | |
8bb54ab5 AS |
509 | /* devices and interfaces are handled separately */ |
510 | if (is_usb_device(dev)) { | |
ddae41be | 511 | |
8bb54ab5 AS |
512 | /* interface drivers never match devices */ |
513 | if (!is_usb_device_driver(drv)) | |
514 | return 0; | |
ddae41be | 515 | |
8bb54ab5 | 516 | /* TODO: Add real matching code */ |
ddae41be GKH |
517 | return 1; |
518 | ||
8bb54ab5 AS |
519 | } else { |
520 | struct usb_interface *intf; | |
521 | struct usb_driver *usb_drv; | |
522 | const struct usb_device_id *id; | |
523 | ||
524 | /* device drivers never match interfaces */ | |
525 | if (is_usb_device_driver(drv)) | |
526 | return 0; | |
527 | ||
528 | intf = to_usb_interface(dev); | |
529 | usb_drv = to_usb_driver(drv); | |
530 | ||
531 | id = usb_match_id(intf, usb_drv->id_table); | |
532 | if (id) | |
533 | return 1; | |
534 | ||
535 | id = usb_match_dynamic_id(intf, usb_drv); | |
536 | if (id) | |
537 | return 1; | |
538 | } | |
539 | ||
ddae41be GKH |
540 | return 0; |
541 | } | |
542 | ||
36e56a34 AS |
543 | #ifdef CONFIG_HOTPLUG |
544 | ||
545 | /* | |
546 | * This sends an uevent to userspace, typically helping to load driver | |
547 | * or other modules, configure the device, and more. Drivers can provide | |
548 | * a MODULE_DEVICE_TABLE to help with module loading subtasks. | |
549 | * | |
550 | * We're called either from khubd (the typical case) or from root hub | |
551 | * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle | |
552 | * delays in event delivery. Use sysfs (and DEVPATH) to make sure the | |
553 | * device (and this configuration!) are still present. | |
554 | */ | |
555 | static int usb_uevent(struct device *dev, char **envp, int num_envp, | |
556 | char *buffer, int buffer_size) | |
557 | { | |
558 | struct usb_interface *intf; | |
559 | struct usb_device *usb_dev; | |
560 | struct usb_host_interface *alt; | |
561 | int i = 0; | |
562 | int length = 0; | |
563 | ||
564 | if (!dev) | |
565 | return -ENODEV; | |
566 | ||
567 | /* driver is often null here; dev_dbg() would oops */ | |
568 | pr_debug ("usb %s: uevent\n", dev->bus_id); | |
569 | ||
782da727 AS |
570 | if (is_usb_device(dev)) { |
571 | usb_dev = to_usb_device(dev); | |
572 | alt = NULL; | |
573 | } else { | |
8bb54ab5 AS |
574 | intf = to_usb_interface(dev); |
575 | usb_dev = interface_to_usbdev(intf); | |
576 | alt = intf->cur_altsetting; | |
577 | } | |
36e56a34 AS |
578 | |
579 | if (usb_dev->devnum < 0) { | |
580 | pr_debug ("usb %s: already deleted?\n", dev->bus_id); | |
581 | return -ENODEV; | |
582 | } | |
583 | if (!usb_dev->bus) { | |
584 | pr_debug ("usb %s: bus removed?\n", dev->bus_id); | |
585 | return -ENODEV; | |
586 | } | |
587 | ||
588 | #ifdef CONFIG_USB_DEVICEFS | |
589 | /* If this is available, userspace programs can directly read | |
590 | * all the device descriptors we don't tell them about. Or | |
591 | * even act as usermode drivers. | |
592 | * | |
593 | * FIXME reduce hardwired intelligence here | |
594 | */ | |
595 | if (add_uevent_var(envp, num_envp, &i, | |
596 | buffer, buffer_size, &length, | |
597 | "DEVICE=/proc/bus/usb/%03d/%03d", | |
598 | usb_dev->bus->busnum, usb_dev->devnum)) | |
599 | return -ENOMEM; | |
600 | #endif | |
601 | ||
602 | /* per-device configurations are common */ | |
603 | if (add_uevent_var(envp, num_envp, &i, | |
604 | buffer, buffer_size, &length, | |
605 | "PRODUCT=%x/%x/%x", | |
606 | le16_to_cpu(usb_dev->descriptor.idVendor), | |
607 | le16_to_cpu(usb_dev->descriptor.idProduct), | |
608 | le16_to_cpu(usb_dev->descriptor.bcdDevice))) | |
609 | return -ENOMEM; | |
610 | ||
611 | /* class-based driver binding models */ | |
612 | if (add_uevent_var(envp, num_envp, &i, | |
613 | buffer, buffer_size, &length, | |
614 | "TYPE=%d/%d/%d", | |
615 | usb_dev->descriptor.bDeviceClass, | |
616 | usb_dev->descriptor.bDeviceSubClass, | |
617 | usb_dev->descriptor.bDeviceProtocol)) | |
618 | return -ENOMEM; | |
619 | ||
782da727 AS |
620 | if (!is_usb_device(dev)) { |
621 | ||
622 | if (add_uevent_var(envp, num_envp, &i, | |
36e56a34 AS |
623 | buffer, buffer_size, &length, |
624 | "INTERFACE=%d/%d/%d", | |
625 | alt->desc.bInterfaceClass, | |
626 | alt->desc.bInterfaceSubClass, | |
627 | alt->desc.bInterfaceProtocol)) | |
782da727 | 628 | return -ENOMEM; |
36e56a34 | 629 | |
782da727 | 630 | if (add_uevent_var(envp, num_envp, &i, |
36e56a34 AS |
631 | buffer, buffer_size, &length, |
632 | "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X", | |
633 | le16_to_cpu(usb_dev->descriptor.idVendor), | |
634 | le16_to_cpu(usb_dev->descriptor.idProduct), | |
635 | le16_to_cpu(usb_dev->descriptor.bcdDevice), | |
636 | usb_dev->descriptor.bDeviceClass, | |
637 | usb_dev->descriptor.bDeviceSubClass, | |
638 | usb_dev->descriptor.bDeviceProtocol, | |
639 | alt->desc.bInterfaceClass, | |
640 | alt->desc.bInterfaceSubClass, | |
641 | alt->desc.bInterfaceProtocol)) | |
782da727 AS |
642 | return -ENOMEM; |
643 | } | |
36e56a34 AS |
644 | |
645 | envp[i] = NULL; | |
646 | ||
647 | return 0; | |
648 | } | |
649 | ||
650 | #else | |
651 | ||
652 | static int usb_uevent(struct device *dev, char **envp, | |
653 | int num_envp, char *buffer, int buffer_size) | |
654 | { | |
655 | return -ENODEV; | |
656 | } | |
657 | ||
658 | #endif /* CONFIG_HOTPLUG */ | |
659 | ||
ddae41be | 660 | /** |
8bb54ab5 AS |
661 | * usb_register_device_driver - register a USB device (not interface) driver |
662 | * @new_udriver: USB operations for the device driver | |
2143acc6 | 663 | * @owner: module owner of this driver. |
ddae41be | 664 | * |
8bb54ab5 AS |
665 | * Registers a USB device driver with the USB core. The list of |
666 | * unattached devices will be rescanned whenever a new driver is | |
667 | * added, allowing the new driver to attach to any recognized devices. | |
668 | * Returns a negative error code on failure and 0 on success. | |
669 | */ | |
670 | int usb_register_device_driver(struct usb_device_driver *new_udriver, | |
671 | struct module *owner) | |
672 | { | |
673 | int retval = 0; | |
674 | ||
675 | if (usb_disabled()) | |
676 | return -ENODEV; | |
677 | ||
678 | new_udriver->drvwrap.for_devices = 1; | |
679 | new_udriver->drvwrap.driver.name = (char *) new_udriver->name; | |
680 | new_udriver->drvwrap.driver.bus = &usb_bus_type; | |
681 | new_udriver->drvwrap.driver.probe = usb_probe_device; | |
682 | new_udriver->drvwrap.driver.remove = usb_unbind_device; | |
683 | new_udriver->drvwrap.driver.owner = owner; | |
684 | ||
685 | retval = driver_register(&new_udriver->drvwrap.driver); | |
686 | ||
687 | if (!retval) { | |
688 | pr_info("%s: registered new device driver %s\n", | |
689 | usbcore_name, new_udriver->name); | |
690 | usbfs_update_special(); | |
691 | } else { | |
692 | printk(KERN_ERR "%s: error %d registering device " | |
693 | " driver %s\n", | |
694 | usbcore_name, retval, new_udriver->name); | |
695 | } | |
696 | ||
697 | return retval; | |
698 | } | |
699 | EXPORT_SYMBOL_GPL(usb_register_device_driver); | |
700 | ||
701 | /** | |
702 | * usb_deregister_device_driver - unregister a USB device (not interface) driver | |
703 | * @udriver: USB operations of the device driver to unregister | |
704 | * Context: must be able to sleep | |
705 | * | |
706 | * Unlinks the specified driver from the internal USB driver list. | |
707 | */ | |
708 | void usb_deregister_device_driver(struct usb_device_driver *udriver) | |
709 | { | |
710 | pr_info("%s: deregistering device driver %s\n", | |
711 | usbcore_name, udriver->name); | |
712 | ||
713 | driver_unregister(&udriver->drvwrap.driver); | |
714 | usbfs_update_special(); | |
715 | } | |
716 | EXPORT_SYMBOL_GPL(usb_deregister_device_driver); | |
717 | ||
718 | /** | |
719 | * usb_register_driver - register a USB interface driver | |
720 | * @new_driver: USB operations for the interface driver | |
721 | * @owner: module owner of this driver. | |
722 | * | |
723 | * Registers a USB interface driver with the USB core. The list of | |
724 | * unattached interfaces will be rescanned whenever a new driver is | |
725 | * added, allowing the new driver to attach to any recognized interfaces. | |
ddae41be GKH |
726 | * Returns a negative error code on failure and 0 on success. |
727 | * | |
728 | * NOTE: if you want your driver to use the USB major number, you must call | |
729 | * usb_register_dev() to enable that functionality. This function no longer | |
730 | * takes care of that. | |
731 | */ | |
2143acc6 | 732 | int usb_register_driver(struct usb_driver *new_driver, struct module *owner) |
ddae41be GKH |
733 | { |
734 | int retval = 0; | |
735 | ||
736 | if (usb_disabled()) | |
737 | return -ENODEV; | |
738 | ||
8bb54ab5 AS |
739 | new_driver->drvwrap.for_devices = 0; |
740 | new_driver->drvwrap.driver.name = (char *) new_driver->name; | |
741 | new_driver->drvwrap.driver.bus = &usb_bus_type; | |
742 | new_driver->drvwrap.driver.probe = usb_probe_interface; | |
743 | new_driver->drvwrap.driver.remove = usb_unbind_interface; | |
744 | new_driver->drvwrap.driver.owner = owner; | |
733260ff GKH |
745 | spin_lock_init(&new_driver->dynids.lock); |
746 | INIT_LIST_HEAD(&new_driver->dynids.list); | |
ddae41be | 747 | |
8bb54ab5 | 748 | retval = driver_register(&new_driver->drvwrap.driver); |
ddae41be GKH |
749 | |
750 | if (!retval) { | |
8bb54ab5 | 751 | pr_info("%s: registered new interface driver %s\n", |
ddae41be GKH |
752 | usbcore_name, new_driver->name); |
753 | usbfs_update_special(); | |
733260ff | 754 | usb_create_newid_file(new_driver); |
ddae41be | 755 | } else { |
8bb54ab5 AS |
756 | printk(KERN_ERR "%s: error %d registering interface " |
757 | " driver %s\n", | |
ddae41be GKH |
758 | usbcore_name, retval, new_driver->name); |
759 | } | |
760 | ||
761 | return retval; | |
762 | } | |
b87ba0a3 | 763 | EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver); |
ddae41be GKH |
764 | |
765 | /** | |
8bb54ab5 AS |
766 | * usb_deregister - unregister a USB interface driver |
767 | * @driver: USB operations of the interface driver to unregister | |
ddae41be GKH |
768 | * Context: must be able to sleep |
769 | * | |
770 | * Unlinks the specified driver from the internal USB driver list. | |
771 | * | |
772 | * NOTE: If you called usb_register_dev(), you still need to call | |
773 | * usb_deregister_dev() to clean up your driver's allocated minor numbers, | |
774 | * this * call will no longer do it for you. | |
775 | */ | |
776 | void usb_deregister(struct usb_driver *driver) | |
777 | { | |
8bb54ab5 AS |
778 | pr_info("%s: deregistering interface driver %s\n", |
779 | usbcore_name, driver->name); | |
ddae41be | 780 | |
ba9dc657 | 781 | usb_remove_newid_file(driver); |
733260ff | 782 | usb_free_dynids(driver); |
8bb54ab5 | 783 | driver_unregister(&driver->drvwrap.driver); |
ddae41be GKH |
784 | |
785 | usbfs_update_special(); | |
786 | } | |
b87ba0a3 | 787 | EXPORT_SYMBOL_GPL_FUTURE(usb_deregister); |
36e56a34 AS |
788 | |
789 | #ifdef CONFIG_PM | |
790 | ||
645daaab | 791 | /* Caller has locked udev->pm_mutex */ |
1cc8a25d | 792 | static int suspend_device(struct usb_device *udev, pm_message_t msg) |
36e56a34 | 793 | { |
782da727 | 794 | struct usb_device_driver *udriver; |
2bf4086d | 795 | int status = 0; |
36e56a34 | 796 | |
114b368c AS |
797 | if (udev->state == USB_STATE_NOTATTACHED || |
798 | udev->state == USB_STATE_SUSPENDED) | |
799 | goto done; | |
800 | ||
1c5df7e7 AS |
801 | /* For devices that don't have a driver, we do a standard suspend. */ |
802 | if (udev->dev.driver == NULL) { | |
645daaab | 803 | udev->do_remote_wakeup = 0; |
1c5df7e7 | 804 | status = usb_port_suspend(udev); |
2bf4086d | 805 | goto done; |
1c5df7e7 AS |
806 | } |
807 | ||
1cc8a25d | 808 | udriver = to_usb_device_driver(udev->dev.driver); |
2bf4086d AS |
809 | status = udriver->suspend(udev, msg); |
810 | ||
811 | done: | |
645daaab | 812 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); |
2bf4086d AS |
813 | if (status == 0) |
814 | udev->dev.power.power_state.event = msg.event; | |
815 | return status; | |
1cc8a25d AS |
816 | } |
817 | ||
645daaab | 818 | /* Caller has locked udev->pm_mutex */ |
1cc8a25d AS |
819 | static int resume_device(struct usb_device *udev) |
820 | { | |
821 | struct usb_device_driver *udriver; | |
2bf4086d | 822 | int status = 0; |
36e56a34 | 823 | |
114b368c AS |
824 | if (udev->state == USB_STATE_NOTATTACHED || |
825 | udev->state != USB_STATE_SUSPENDED) | |
2bf4086d | 826 | goto done; |
1cc8a25d | 827 | |
1c5df7e7 AS |
828 | /* Can't resume it if it doesn't have a driver. */ |
829 | if (udev->dev.driver == NULL) { | |
830 | status = -ENOTCONN; | |
2bf4086d | 831 | goto done; |
1c5df7e7 AS |
832 | } |
833 | ||
1cc8a25d | 834 | udriver = to_usb_device_driver(udev->dev.driver); |
2bf4086d AS |
835 | status = udriver->resume(udev); |
836 | ||
837 | done: | |
645daaab | 838 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); |
2bf4086d AS |
839 | if (status == 0) |
840 | udev->dev.power.power_state.event = PM_EVENT_ON; | |
841 | return status; | |
1cc8a25d AS |
842 | } |
843 | ||
645daaab | 844 | /* Caller has locked intf's usb_device's pm_mutex */ |
1cc8a25d AS |
845 | static int suspend_interface(struct usb_interface *intf, pm_message_t msg) |
846 | { | |
847 | struct usb_driver *driver; | |
2bf4086d | 848 | int status = 0; |
1cc8a25d | 849 | |
114b368c AS |
850 | /* with no hardware, USB interfaces only use FREEZE and ON states */ |
851 | if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED || | |
852 | !is_active(intf)) | |
2bf4086d | 853 | goto done; |
1cc8a25d | 854 | |
645daaab | 855 | if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */ |
2bf4086d | 856 | goto done; |
114b368c | 857 | driver = to_usb_driver(intf->dev.driver); |
36e56a34 AS |
858 | |
859 | if (driver->suspend && driver->resume) { | |
1cc8a25d | 860 | status = driver->suspend(intf, msg); |
645daaab AS |
861 | if (status == 0) |
862 | mark_quiesced(intf); | |
863 | else if (!interface_to_usbdev(intf)->auto_pm) | |
1cc8a25d AS |
864 | dev_err(&intf->dev, "%s error %d\n", |
865 | "suspend", status); | |
36e56a34 AS |
866 | } else { |
867 | // FIXME else if there's no suspend method, disconnect... | |
645daaab | 868 | // Not possible if auto_pm is set... |
1cc8a25d AS |
869 | dev_warn(&intf->dev, "no suspend for driver %s?\n", |
870 | driver->name); | |
36e56a34 | 871 | mark_quiesced(intf); |
36e56a34 | 872 | } |
2bf4086d AS |
873 | |
874 | done: | |
645daaab | 875 | // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status); |
2bf4086d AS |
876 | if (status == 0) |
877 | intf->dev.power.power_state.event = msg.event; | |
36e56a34 AS |
878 | return status; |
879 | } | |
880 | ||
645daaab | 881 | /* Caller has locked intf's usb_device's pm_mutex */ |
1cc8a25d | 882 | static int resume_interface(struct usb_interface *intf) |
36e56a34 | 883 | { |
1cc8a25d | 884 | struct usb_driver *driver; |
2bf4086d | 885 | int status = 0; |
36e56a34 | 886 | |
114b368c AS |
887 | if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED || |
888 | is_active(intf)) | |
2bf4086d | 889 | goto done; |
36e56a34 | 890 | |
645daaab AS |
891 | /* Don't let autoresume interfere with unbinding */ |
892 | if (intf->condition == USB_INTERFACE_UNBINDING) | |
893 | goto done; | |
894 | ||
1c5df7e7 | 895 | /* Can't resume it if it doesn't have a driver. */ |
645daaab | 896 | if (intf->condition == USB_INTERFACE_UNBOUND) { |
1c5df7e7 | 897 | status = -ENOTCONN; |
2bf4086d | 898 | goto done; |
1c5df7e7 | 899 | } |
1cc8a25d | 900 | driver = to_usb_driver(intf->dev.driver); |
36e56a34 | 901 | |
36e56a34 AS |
902 | if (driver->resume) { |
903 | status = driver->resume(intf); | |
2bf4086d | 904 | if (status) |
1cc8a25d AS |
905 | dev_err(&intf->dev, "%s error %d\n", |
906 | "resume", status); | |
2bf4086d AS |
907 | else |
908 | mark_active(intf); | |
909 | } else { | |
1cc8a25d AS |
910 | dev_warn(&intf->dev, "no resume for driver %s?\n", |
911 | driver->name); | |
2bf4086d AS |
912 | mark_active(intf); |
913 | } | |
914 | ||
915 | done: | |
645daaab | 916 | // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status); |
2bf4086d AS |
917 | if (status == 0) |
918 | intf->dev.power.power_state.event = PM_EVENT_ON; | |
919 | return status; | |
36e56a34 AS |
920 | } |
921 | ||
645daaab AS |
922 | /** |
923 | * usb_suspend_both - suspend a USB device and its interfaces | |
924 | * @udev: the usb_device to suspend | |
925 | * @msg: Power Management message describing this state transition | |
926 | * | |
927 | * This is the central routine for suspending USB devices. It calls the | |
928 | * suspend methods for all the interface drivers in @udev and then calls | |
929 | * the suspend method for @udev itself. If an error occurs at any stage, | |
930 | * all the interfaces which were suspended are resumed so that they remain | |
931 | * in the same state as the device. | |
932 | * | |
933 | * If an autosuspend is in progress (@udev->auto_pm is set), the routine | |
934 | * checks first to make sure that neither the device itself or any of its | |
935 | * active interfaces is in use (pm_usage_cnt is greater than 0). If they | |
936 | * are, the autosuspend fails. | |
937 | * | |
938 | * If the suspend succeeds, the routine recursively queues an autosuspend | |
939 | * request for @udev's parent device, thereby propagating the change up | |
940 | * the device tree. If all of the parent's children are now suspended, | |
941 | * the parent will autosuspend in turn. | |
942 | * | |
943 | * The suspend method calls are subject to mutual exclusion under control | |
944 | * of @udev's pm_mutex. Many of these calls are also under the protection | |
945 | * of @udev's device lock (including all requests originating outside the | |
946 | * USB subsystem), but autosuspend requests generated by a child device or | |
947 | * interface driver may not be. Usbcore will insure that the method calls | |
948 | * do not arrive during bind, unbind, or reset operations. However, drivers | |
949 | * must be prepared to handle suspend calls arriving at unpredictable times. | |
950 | * The only way to block such calls is to do an autoresume (preventing | |
951 | * autosuspends) while holding @udev's device lock (preventing outside | |
952 | * suspends). | |
953 | * | |
954 | * The caller must hold @udev->pm_mutex. | |
955 | * | |
956 | * This routine can run only in process context. | |
957 | */ | |
a8e7c565 AS |
958 | int usb_suspend_both(struct usb_device *udev, pm_message_t msg) |
959 | { | |
960 | int status = 0; | |
961 | int i = 0; | |
962 | struct usb_interface *intf; | |
645daaab AS |
963 | struct usb_device *parent = udev->parent; |
964 | ||
965 | cancel_delayed_work(&udev->autosuspend); | |
966 | if (udev->state == USB_STATE_NOTATTACHED) | |
967 | return 0; | |
968 | if (udev->state == USB_STATE_SUSPENDED) | |
969 | return 0; | |
a8e7c565 | 970 | |
645daaab AS |
971 | udev->do_remote_wakeup = device_may_wakeup(&udev->dev); |
972 | ||
973 | /* For autosuspend, fail fast if anything is in use. | |
974 | * Also fail if any interfaces require remote wakeup but it | |
975 | * isn't available. */ | |
976 | if (udev->auto_pm) { | |
977 | if (udev->pm_usage_cnt > 0) | |
978 | return -EBUSY; | |
979 | if (udev->actconfig) { | |
980 | for (; i < udev->actconfig->desc.bNumInterfaces; i++) { | |
981 | intf = udev->actconfig->interface[i]; | |
982 | if (!is_active(intf)) | |
983 | continue; | |
984 | if (intf->pm_usage_cnt > 0) | |
985 | return -EBUSY; | |
986 | if (intf->needs_remote_wakeup && | |
987 | !udev->do_remote_wakeup) { | |
988 | dev_dbg(&udev->dev, | |
989 | "remote wakeup needed for autosuspend\n"); | |
990 | return -EOPNOTSUPP; | |
991 | } | |
992 | } | |
993 | i = 0; | |
994 | } | |
995 | } | |
996 | ||
997 | /* Suspend all the interfaces and then udev itself */ | |
a8e7c565 AS |
998 | if (udev->actconfig) { |
999 | for (; i < udev->actconfig->desc.bNumInterfaces; i++) { | |
1000 | intf = udev->actconfig->interface[i]; | |
1001 | status = suspend_interface(intf, msg); | |
1002 | if (status != 0) | |
1003 | break; | |
1004 | } | |
1005 | } | |
1006 | if (status == 0) | |
1007 | status = suspend_device(udev, msg); | |
1008 | ||
1009 | /* If the suspend failed, resume interfaces that did get suspended */ | |
1010 | if (status != 0) { | |
1011 | while (--i >= 0) { | |
1012 | intf = udev->actconfig->interface[i]; | |
1013 | resume_interface(intf); | |
1014 | } | |
645daaab AS |
1015 | |
1016 | /* If the suspend succeeded, propagate it up the tree */ | |
1017 | } else if (parent) | |
1018 | usb_autosuspend_device(parent, 0); | |
1019 | ||
1020 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); | |
a8e7c565 AS |
1021 | return status; |
1022 | } | |
1023 | ||
645daaab AS |
1024 | /** |
1025 | * usb_resume_both - resume a USB device and its interfaces | |
1026 | * @udev: the usb_device to resume | |
1027 | * | |
1028 | * This is the central routine for resuming USB devices. It calls the | |
1029 | * the resume method for @udev and then calls the resume methods for all | |
1030 | * the interface drivers in @udev. | |
1031 | * | |
1032 | * Before starting the resume, the routine calls itself recursively for | |
1033 | * the parent device of @udev, thereby propagating the change up the device | |
1034 | * tree and assuring that @udev will be able to resume. If the parent is | |
1035 | * unable to resume successfully, the routine fails. | |
1036 | * | |
1037 | * The resume method calls are subject to mutual exclusion under control | |
1038 | * of @udev's pm_mutex. Many of these calls are also under the protection | |
1039 | * of @udev's device lock (including all requests originating outside the | |
1040 | * USB subsystem), but autoresume requests generated by a child device or | |
1041 | * interface driver may not be. Usbcore will insure that the method calls | |
1042 | * do not arrive during bind, unbind, or reset operations. However, drivers | |
1043 | * must be prepared to handle resume calls arriving at unpredictable times. | |
1044 | * The only way to block such calls is to do an autoresume (preventing | |
1045 | * other autoresumes) while holding @udev's device lock (preventing outside | |
1046 | * resumes). | |
1047 | * | |
1048 | * The caller must hold @udev->pm_mutex. | |
1049 | * | |
1050 | * This routine can run only in process context. | |
1051 | */ | |
a8e7c565 AS |
1052 | int usb_resume_both(struct usb_device *udev) |
1053 | { | |
645daaab | 1054 | int status = 0; |
a8e7c565 AS |
1055 | int i; |
1056 | struct usb_interface *intf; | |
645daaab AS |
1057 | struct usb_device *parent = udev->parent; |
1058 | ||
1059 | cancel_delayed_work(&udev->autosuspend); | |
1060 | if (udev->state == USB_STATE_NOTATTACHED) | |
1061 | return -ENODEV; | |
a8e7c565 | 1062 | |
645daaab AS |
1063 | /* Propagate the resume up the tree, if necessary */ |
1064 | if (udev->state == USB_STATE_SUSPENDED) { | |
1065 | if (parent) { | |
1066 | mutex_lock_nested(&parent->pm_mutex, parent->level); | |
1067 | parent->auto_pm = 1; | |
1068 | status = usb_resume_both(parent); | |
1069 | } else { | |
1070 | ||
1071 | /* We can't progagate beyond the USB subsystem, | |
1072 | * so if a root hub's controller is suspended | |
1073 | * then we're stuck. */ | |
1074 | if (udev->dev.parent->power.power_state.event != | |
1075 | PM_EVENT_ON) | |
1076 | status = -EHOSTUNREACH; | |
1077 | } | |
592fbbe4 | 1078 | if (status == 0) |
645daaab AS |
1079 | status = resume_device(udev); |
1080 | if (parent) | |
1081 | mutex_unlock(&parent->pm_mutex); | |
592fbbe4 AS |
1082 | } else { |
1083 | ||
1084 | /* Needed only for setting udev->dev.power.power_state.event | |
1085 | * and for possible debugging message. */ | |
1086 | status = resume_device(udev); | |
114b368c AS |
1087 | } |
1088 | ||
645daaab AS |
1089 | /* Now the parent won't suspend until we are finished */ |
1090 | ||
a8e7c565 AS |
1091 | if (status == 0 && udev->actconfig) { |
1092 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { | |
1093 | intf = udev->actconfig->interface[i]; | |
1094 | resume_interface(intf); | |
1095 | } | |
1096 | } | |
645daaab AS |
1097 | |
1098 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); | |
1099 | return status; | |
1100 | } | |
1101 | ||
1102 | #ifdef CONFIG_USB_SUSPEND | |
1103 | ||
1104 | /** | |
1105 | * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces | |
701f35af HK |
1106 | * @udev: the usb_device to autosuspend |
1107 | * @dec_usage_cnt: flag to decrement @udev's PM-usage counter | |
645daaab AS |
1108 | * |
1109 | * This routine should be called when a core subsystem is finished using | |
1110 | * @udev and wants to allow it to autosuspend. Examples would be when | |
1111 | * @udev's device file in usbfs is closed or after a configuration change. | |
1112 | * | |
1113 | * @dec_usage_cnt should be 1 if the subsystem previously incremented | |
1114 | * @udev's usage counter (such as by passing 1 to usb_autoresume_device); | |
1115 | * otherwise it should be 0. | |
1116 | * | |
1117 | * If the usage counter for @udev or any of its active interfaces is greater | |
1118 | * than 0, the autosuspend request will not be queued. (If an interface | |
1119 | * driver does not support autosuspend then its usage counter is permanently | |
1120 | * positive.) Likewise, if an interface driver requires remote-wakeup | |
1121 | * capability during autosuspend but remote wakeup is disabled, the | |
1122 | * autosuspend will fail. | |
1123 | * | |
1124 | * Often the caller will hold @udev's device lock, but this is not | |
1125 | * necessary. | |
1126 | * | |
1127 | * This routine can run only in process context. | |
1128 | */ | |
1129 | void usb_autosuspend_device(struct usb_device *udev, int dec_usage_cnt) | |
1130 | { | |
1131 | mutex_lock_nested(&udev->pm_mutex, udev->level); | |
1132 | udev->pm_usage_cnt -= dec_usage_cnt; | |
1133 | if (udev->pm_usage_cnt <= 0) | |
1134 | schedule_delayed_work(&udev->autosuspend, | |
1135 | USB_AUTOSUSPEND_DELAY); | |
1136 | mutex_unlock(&udev->pm_mutex); | |
1137 | // dev_dbg(&udev->dev, "%s: cnt %d\n", | |
1138 | // __FUNCTION__, udev->pm_usage_cnt); | |
1139 | } | |
1140 | ||
1141 | /** | |
1142 | * usb_autoresume_device - immediately autoresume a USB device and its interfaces | |
701f35af HK |
1143 | * @udev: the usb_device to autoresume |
1144 | * @inc_usage_cnt: flag to increment @udev's PM-usage counter | |
645daaab AS |
1145 | * |
1146 | * This routine should be called when a core subsystem wants to use @udev | |
1147 | * and needs to guarantee that it is not suspended. In addition, the | |
1148 | * caller can prevent @udev from being autosuspended subsequently. (Note | |
1149 | * that this will not prevent suspend events originating in the PM core.) | |
1150 | * Examples would be when @udev's device file in usbfs is opened (autosuspend | |
1151 | * should be prevented until the file is closed) or when a remote-wakeup | |
1152 | * request is received (later autosuspends should not be prevented). | |
1153 | * | |
1154 | * @inc_usage_cnt should be 1 to increment @udev's usage counter and prevent | |
1155 | * autosuspends. This prevention will persist until the usage counter is | |
1156 | * decremented again (such as by passing 1 to usb_autosuspend_device). | |
1157 | * Otherwise @inc_usage_cnt should be 0 to leave the usage counter unchanged. | |
1158 | * Regardless, if the autoresume fails then the usage counter is not | |
1159 | * incremented. | |
1160 | * | |
1161 | * Often the caller will hold @udev's device lock, but this is not | |
1162 | * necessary (and attempting it might cause deadlock). | |
1163 | * | |
1164 | * This routine can run only in process context. | |
1165 | */ | |
1166 | int usb_autoresume_device(struct usb_device *udev, int inc_usage_cnt) | |
1167 | { | |
1168 | int status; | |
1169 | ||
1170 | mutex_lock_nested(&udev->pm_mutex, udev->level); | |
1171 | udev->pm_usage_cnt += inc_usage_cnt; | |
1172 | udev->auto_pm = 1; | |
1173 | status = usb_resume_both(udev); | |
1174 | if (status != 0) | |
1175 | udev->pm_usage_cnt -= inc_usage_cnt; | |
1176 | mutex_unlock(&udev->pm_mutex); | |
1177 | // dev_dbg(&udev->dev, "%s: status %d cnt %d\n", | |
1178 | // __FUNCTION__, status, udev->pm_usage_cnt); | |
1179 | return status; | |
1180 | } | |
1181 | ||
1182 | /** | |
1183 | * usb_autopm_put_interface - decrement a USB interface's PM-usage counter | |
701f35af | 1184 | * @intf: the usb_interface whose counter should be decremented |
645daaab AS |
1185 | * |
1186 | * This routine should be called by an interface driver when it is | |
1187 | * finished using @intf and wants to allow it to autosuspend. A typical | |
1188 | * example would be a character-device driver when its device file is | |
1189 | * closed. | |
1190 | * | |
1191 | * The routine decrements @intf's usage counter. When the counter reaches | |
1192 | * 0, a delayed autosuspend request for @intf's device is queued. When | |
1193 | * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all | |
1194 | * the other usage counters for the sibling interfaces and @intf's | |
1195 | * usb_device, the device and all its interfaces will be autosuspended. | |
1196 | * | |
1197 | * Note that @intf->pm_usage_cnt is owned by the interface driver. The | |
1198 | * core will not change its value other than the increment and decrement | |
1199 | * in usb_autopm_get_interface and usb_autopm_put_interface. The driver | |
1200 | * may use this simple counter-oriented discipline or may set the value | |
1201 | * any way it likes. | |
1202 | * | |
1203 | * If the driver has set @intf->needs_remote_wakeup then autosuspend will | |
1204 | * take place only if the device's remote-wakeup facility is enabled. | |
1205 | * | |
1206 | * Suspend method calls queued by this routine can arrive at any time | |
1207 | * while @intf is resumed and its usage counter is equal to 0. They are | |
1208 | * not protected by the usb_device's lock but only by its pm_mutex. | |
1209 | * Drivers must provide their own synchronization. | |
1210 | * | |
1211 | * This routine can run only in process context. | |
1212 | */ | |
1213 | void usb_autopm_put_interface(struct usb_interface *intf) | |
1214 | { | |
1215 | struct usb_device *udev = interface_to_usbdev(intf); | |
1216 | ||
1217 | mutex_lock_nested(&udev->pm_mutex, udev->level); | |
1218 | if (intf->condition != USB_INTERFACE_UNBOUND) { | |
1219 | if (--intf->pm_usage_cnt <= 0) | |
1220 | schedule_delayed_work(&udev->autosuspend, | |
1221 | USB_AUTOSUSPEND_DELAY); | |
1222 | } | |
1223 | mutex_unlock(&udev->pm_mutex); | |
1224 | // dev_dbg(&intf->dev, "%s: cnt %d\n", | |
1225 | // __FUNCTION__, intf->pm_usage_cnt); | |
1226 | } | |
1227 | EXPORT_SYMBOL_GPL(usb_autopm_put_interface); | |
1228 | ||
1229 | /** | |
1230 | * usb_autopm_get_interface - increment a USB interface's PM-usage counter | |
701f35af | 1231 | * @intf: the usb_interface whose counter should be incremented |
645daaab AS |
1232 | * |
1233 | * This routine should be called by an interface driver when it wants to | |
1234 | * use @intf and needs to guarantee that it is not suspended. In addition, | |
1235 | * the routine prevents @intf from being autosuspended subsequently. (Note | |
1236 | * that this will not prevent suspend events originating in the PM core.) | |
1237 | * This prevention will persist until usb_autopm_put_interface() is called | |
1238 | * or @intf is unbound. A typical example would be a character-device | |
1239 | * driver when its device file is opened. | |
1240 | * | |
1241 | * The routine increments @intf's usage counter. So long as the counter | |
1242 | * is greater than 0, autosuspend will not be allowed for @intf or its | |
1243 | * usb_device. When the driver is finished using @intf it should call | |
1244 | * usb_autopm_put_interface() to decrement the usage counter and queue | |
1245 | * a delayed autosuspend request (if the counter is <= 0). | |
1246 | * | |
1247 | * Note that @intf->pm_usage_cnt is owned by the interface driver. The | |
1248 | * core will not change its value other than the increment and decrement | |
1249 | * in usb_autopm_get_interface and usb_autopm_put_interface. The driver | |
1250 | * may use this simple counter-oriented discipline or may set the value | |
1251 | * any way it likes. | |
1252 | * | |
1253 | * Resume method calls generated by this routine can arrive at any time | |
1254 | * while @intf is suspended. They are not protected by the usb_device's | |
1255 | * lock but only by its pm_mutex. Drivers must provide their own | |
1256 | * synchronization. | |
1257 | * | |
1258 | * This routine can run only in process context. | |
1259 | */ | |
1260 | int usb_autopm_get_interface(struct usb_interface *intf) | |
1261 | { | |
1262 | struct usb_device *udev = interface_to_usbdev(intf); | |
1263 | int status; | |
1264 | ||
1265 | mutex_lock_nested(&udev->pm_mutex, udev->level); | |
1266 | if (intf->condition == USB_INTERFACE_UNBOUND) | |
1267 | status = -ENODEV; | |
1268 | else { | |
1269 | ++intf->pm_usage_cnt; | |
1270 | udev->auto_pm = 1; | |
1271 | status = usb_resume_both(udev); | |
1272 | if (status != 0) | |
1273 | --intf->pm_usage_cnt; | |
1274 | } | |
1275 | mutex_unlock(&udev->pm_mutex); | |
1276 | // dev_dbg(&intf->dev, "%s: status %d cnt %d\n", | |
1277 | // __FUNCTION__, status, intf->pm_usage_cnt); | |
a8e7c565 AS |
1278 | return status; |
1279 | } | |
645daaab AS |
1280 | EXPORT_SYMBOL_GPL(usb_autopm_get_interface); |
1281 | ||
1282 | #endif /* CONFIG_USB_SUSPEND */ | |
a8e7c565 | 1283 | |
1cc8a25d AS |
1284 | static int usb_suspend(struct device *dev, pm_message_t message) |
1285 | { | |
1286 | int status; | |
1287 | ||
645daaab AS |
1288 | if (is_usb_device(dev)) { |
1289 | struct usb_device *udev = to_usb_device(dev); | |
1290 | ||
1291 | mutex_lock_nested(&udev->pm_mutex, udev->level); | |
1292 | udev->auto_pm = 0; | |
1293 | status = usb_suspend_both(udev, message); | |
1294 | mutex_unlock(&udev->pm_mutex); | |
1295 | } else | |
a8e7c565 | 1296 | status = 0; |
1cc8a25d AS |
1297 | return status; |
1298 | } | |
1299 | ||
1300 | static int usb_resume(struct device *dev) | |
1301 | { | |
1302 | int status; | |
1303 | ||
a8e7c565 | 1304 | if (is_usb_device(dev)) { |
645daaab AS |
1305 | struct usb_device *udev = to_usb_device(dev); |
1306 | ||
1307 | mutex_lock_nested(&udev->pm_mutex, udev->level); | |
1308 | udev->auto_pm = 0; | |
1309 | status = usb_resume_both(udev); | |
1310 | mutex_unlock(&udev->pm_mutex); | |
a8e7c565 AS |
1311 | |
1312 | /* Rebind drivers that had no suspend method? */ | |
1313 | } else | |
1314 | status = 0; | |
1cc8a25d AS |
1315 | return status; |
1316 | } | |
1317 | ||
36e56a34 AS |
1318 | #endif /* CONFIG_PM */ |
1319 | ||
1320 | struct bus_type usb_bus_type = { | |
1321 | .name = "usb", | |
1322 | .match = usb_device_match, | |
1323 | .uevent = usb_uevent, | |
1324 | #ifdef CONFIG_PM | |
782da727 AS |
1325 | .suspend = usb_suspend, |
1326 | .resume = usb_resume, | |
36e56a34 AS |
1327 | #endif |
1328 | }; |