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