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