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