blob: c115eed0fdc3d06348c0c7e8cb22db84b8a7ccd2 [file] [log] [blame]
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -08001/*
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
Alan Stern36e56a32006-07-01 22:08:06 -040020 * matching, probing, releasing, suspending and resuming for
21 * real drivers.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080022 *
23 */
24
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080025#include <linux/device.h>
26#include <linux/usb.h>
Alan Stern6bc6cff2007-05-04 11:53:03 -040027#include <linux/usb/quirks.h>
Alan Sternbd859282006-09-19 10:14:07 -040028#include <linux/workqueue.h>
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080029#include "hcd.h"
30#include "usb.h"
31
Alan Stern20dfdad2007-05-22 11:50:17 -040032
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080033#ifdef CONFIG_HOTPLUG
34
35/*
36 * Adds a new dynamic USBdevice ID to this driver,
37 * and cause the driver to probe for all devices again.
38 */
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010039ssize_t usb_store_new_id(struct usb_dynids *dynids,
40 struct device_driver *driver,
41 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080042{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080043 struct usb_dynid *dynid;
44 u32 idVendor = 0;
45 u32 idProduct = 0;
46 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070047 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080048
49 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
50 if (fields < 2)
51 return -EINVAL;
52
53 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
54 if (!dynid)
55 return -ENOMEM;
56
57 INIT_LIST_HEAD(&dynid->node);
58 dynid->id.idVendor = idVendor;
59 dynid->id.idProduct = idProduct;
60 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
61
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010062 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020063 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010064 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080065
66 if (get_driver(driver)) {
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070067 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080068 put_driver(driver);
69 }
70
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070071 if (retval)
72 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080073 return count;
74}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010075EXPORT_SYMBOL_GPL(usb_store_new_id);
76
77static ssize_t store_new_id(struct device_driver *driver,
78 const char *buf, size_t count)
79{
80 struct usb_driver *usb_drv = to_usb_driver(driver);
81
82 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
83}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080084static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
85
86static int usb_create_newid_file(struct usb_driver *usb_drv)
87{
88 int error = 0;
89
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080090 if (usb_drv->no_dynamic_id)
91 goto exit;
92
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080093 if (usb_drv->probe != NULL)
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -080094 error = driver_create_file(&usb_drv->drvwrap.driver,
95 &driver_attr_new_id);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080096exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080097 return error;
98}
99
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800100static void usb_remove_newid_file(struct usb_driver *usb_drv)
101{
102 if (usb_drv->no_dynamic_id)
103 return;
104
105 if (usb_drv->probe != NULL)
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800106 driver_remove_file(&usb_drv->drvwrap.driver,
107 &driver_attr_new_id);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800108}
109
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800110static void usb_free_dynids(struct usb_driver *usb_drv)
111{
112 struct usb_dynid *dynid, *n;
113
114 spin_lock(&usb_drv->dynids.lock);
115 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
116 list_del(&dynid->node);
117 kfree(dynid);
118 }
119 spin_unlock(&usb_drv->dynids.lock);
120}
121#else
122static inline int usb_create_newid_file(struct usb_driver *usb_drv)
123{
124 return 0;
125}
126
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800127static void usb_remove_newid_file(struct usb_driver *usb_drv)
128{
129}
130
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800131static inline void usb_free_dynids(struct usb_driver *usb_drv)
132{
133}
134#endif
135
136static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
137 struct usb_driver *drv)
138{
139 struct usb_dynid *dynid;
140
141 spin_lock(&drv->dynids.lock);
142 list_for_each_entry(dynid, &drv->dynids.list, node) {
143 if (usb_match_one_id(intf, &dynid->id)) {
144 spin_unlock(&drv->dynids.lock);
145 return &dynid->id;
146 }
147 }
148 spin_unlock(&drv->dynids.lock);
149 return NULL;
150}
151
152
Alan Stern8bb54ab2006-07-01 22:08:49 -0400153/* called from driver core with dev locked */
154static int usb_probe_device(struct device *dev)
155{
156 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
157 struct usb_device *udev;
158 int error = -ENODEV;
159
Harvey Harrison441b62c2008-03-03 16:08:34 -0800160 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400161
162 if (!is_usb_device(dev)) /* Sanity check */
163 return error;
164
165 udev = to_usb_device(dev);
166
Alan Stern8bb54ab2006-07-01 22:08:49 -0400167 /* TODO: Add real matching code */
168
Alan Stern645daaa2006-08-30 15:47:02 -0400169 /* The device should always appear to be in use
170 * unless the driver suports autosuspend.
171 */
172 udev->pm_usage_cnt = !(udriver->supports_autosuspend);
173
Alan Stern8bb54ab2006-07-01 22:08:49 -0400174 error = udriver->probe(udev);
175 return error;
176}
177
178/* called from driver core with dev locked */
179static int usb_unbind_device(struct device *dev)
180{
181 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
182
183 udriver->disconnect(to_usb_device(dev));
184 return 0;
185}
186
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800187/*
188 * Cancel any pending scheduled resets
189 *
190 * [see usb_queue_reset_device()]
191 *
192 * Called after unconfiguring / when releasing interfaces. See
193 * comments in __usb_queue_reset_device() regarding
194 * udev->reset_running.
195 */
196static void usb_cancel_queued_reset(struct usb_interface *iface)
197{
198 if (iface->reset_running == 0)
199 cancel_work_sync(&iface->reset_ws);
200}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400201
202/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800203static int usb_probe_interface(struct device *dev)
204{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400205 struct usb_driver *driver = to_usb_driver(dev->driver);
206 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -0400207 struct usb_device *udev;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800208 const struct usb_device_id *id;
209 int error = -ENODEV;
210
Harvey Harrison441b62c2008-03-03 16:08:34 -0800211 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800212
Alan Stern8bb54ab2006-07-01 22:08:49 -0400213 if (is_usb_device(dev)) /* Sanity check */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800214 return error;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400215
216 intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400217 udev = interface_to_usbdev(intf);
Alan Stern78d9a482008-06-23 16:00:40 -0400218 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800219
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800220 if (udev->authorized == 0) {
221 dev_err(&intf->dev, "Device is not authorized for usage\n");
222 return -ENODEV;
223 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700224
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800225 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800226 if (!id)
227 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800228 if (id) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800229 dev_dbg(dev, "%s - got id\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800230
Alan Stern94fcda12006-11-20 11:38:46 -0500231 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400232 if (error)
233 return error;
234
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800235 /* Interface "power state" doesn't correspond to any hardware
236 * state whatsoever. We use it to record when it's bound to
237 * a driver that may start I/0: it's not frozen/quiesced.
238 */
239 mark_active(intf);
240 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400241
242 /* The interface should always appear to be in use
243 * unless the driver suports autosuspend.
244 */
245 intf->pm_usage_cnt = !(driver->supports_autosuspend);
246
Alan Stern55151d72008-08-12 14:33:59 -0400247 /* Carry out a deferred switch to altsetting 0 */
248 if (intf->needs_altsetting0) {
249 usb_set_interface(udev, intf->altsetting[0].
250 desc.bInterfaceNumber, 0);
251 intf->needs_altsetting0 = 0;
252 }
253
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800254 error = driver->probe(intf, id);
255 if (error) {
256 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400257 intf->needs_remote_wakeup = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800258 intf->condition = USB_INTERFACE_UNBOUND;
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800259 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800260 } else
261 intf->condition = USB_INTERFACE_BOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400262
Alan Stern94fcda12006-11-20 11:38:46 -0500263 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800264 }
265
266 return error;
267}
268
Alan Stern8bb54ab2006-07-01 22:08:49 -0400269/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800270static int usb_unbind_interface(struct device *dev)
271{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400272 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800273 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400274 struct usb_device *udev;
275 int error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800276
277 intf->condition = USB_INTERFACE_UNBINDING;
278
Alan Stern645daaa2006-08-30 15:47:02 -0400279 /* Autoresume for set_interface call below */
280 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500281 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400282
Alan Stern9da82bd2008-05-08 11:54:37 -0400283 /* Terminate all URBs for this interface unless the driver
284 * supports "soft" unbinding.
285 */
286 if (!driver->soft_unbind)
Alan Sternddeac4e2009-01-15 17:03:33 -0500287 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800288
Alan Stern8bb54ab2006-07-01 22:08:49 -0400289 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800290 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800291
Alan Stern55151d72008-08-12 14:33:59 -0400292 /* Reset other interface state.
293 * We cannot do a Set-Interface if the device is suspended or
294 * if it is prepared for a system sleep (since installing a new
295 * altsetting means creating new endpoint device entries).
296 * When either of these happens, defer the Set-Interface.
297 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500298 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
299 /* Already in altsetting 0 so skip Set-Interface.
300 * Just re-enable it without affecting the endpoint toggles.
301 */
302 usb_enable_interface(udev, intf, false);
303 } else if (!error && intf->dev.power.status == DPM_ON)
Alan Stern55151d72008-08-12 14:33:59 -0400304 usb_set_interface(udev, intf->altsetting[0].
305 desc.bInterfaceNumber, 0);
306 else
307 intf->needs_altsetting0 = 1;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800308 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400309
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800310 intf->condition = USB_INTERFACE_UNBOUND;
311 mark_quiesced(intf);
Alan Stern645daaa2006-08-30 15:47:02 -0400312 intf->needs_remote_wakeup = 0;
313
314 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500315 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800316
317 return 0;
318}
319
Alan Stern36e56a32006-07-01 22:08:06 -0400320/**
321 * usb_driver_claim_interface - bind a driver to an interface
322 * @driver: the driver to be bound
323 * @iface: the interface to which it will be bound; must be in the
324 * usb device's active configuration
325 * @priv: driver data associated with that interface
326 *
327 * This is used by usb device drivers that need to claim more than one
328 * interface on a device when probing (audio and acm are current examples).
329 * No device driver should directly modify internal usb_interface or
330 * usb_device structure members.
331 *
332 * Few drivers should need to use this routine, since the most natural
333 * way to bind to an interface is to return the private data from
334 * the driver's probe() method.
335 *
Greg Kroah-Hartman341487a2007-04-09 11:52:31 -0400336 * Callers must own the device lock, so driver probe() entries don't need
337 * extra locking, but other call contexts may need to explicitly claim that
338 * lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400339 */
340int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800341 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400342{
343 struct device *dev = &iface->dev;
Alan Stern645daaa2006-08-30 15:47:02 -0400344 struct usb_device *udev = interface_to_usbdev(iface);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700345 int retval = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400346
347 if (dev->driver)
348 return -EBUSY;
349
Alan Stern8bb54ab2006-07-01 22:08:49 -0400350 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400351 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400352 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400353
Alan Sterne0318eb2006-09-26 14:50:20 -0400354 usb_pm_lock(udev);
Alan Stern36e56a32006-07-01 22:08:06 -0400355 iface->condition = USB_INTERFACE_BOUND;
356 mark_active(iface);
Alan Stern645daaa2006-08-30 15:47:02 -0400357 iface->pm_usage_cnt = !(driver->supports_autosuspend);
Alan Sterne0318eb2006-09-26 14:50:20 -0400358 usb_pm_unlock(udev);
Alan Stern36e56a32006-07-01 22:08:06 -0400359
360 /* if interface was already added, bind now; else let
361 * the future device_add() bind it, bypassing probe()
362 */
363 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700364 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400365
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700366 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400367}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600368EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400369
370/**
371 * usb_driver_release_interface - unbind a driver from an interface
372 * @driver: the driver to be unbound
373 * @iface: the interface from which it will be unbound
374 *
375 * This can be used by drivers to release an interface without waiting
376 * for their disconnect() methods to be called. In typical cases this
377 * also causes the driver disconnect() method to be called.
378 *
379 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a2007-04-09 11:52:31 -0400380 * Callers must own the device lock, so driver disconnect() entries don't
381 * need extra locking, but other call contexts may need to explicitly claim
382 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400383 */
384void usb_driver_release_interface(struct usb_driver *driver,
385 struct usb_interface *iface)
386{
387 struct device *dev = &iface->dev;
388
389 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400390 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400391 return;
392
393 /* don't release from within disconnect() */
394 if (iface->condition != USB_INTERFACE_BOUND)
395 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400396 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400397
Alan Stern91f8d062009-04-16 15:35:09 -0400398 /* Release via the driver core only if the interface
399 * has already been registered
400 */
Alan Stern36e56a32006-07-01 22:08:06 -0400401 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400402 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800403 } else {
Alan Stern91f8d062009-04-16 15:35:09 -0400404 down(&dev->sem);
405 usb_unbind_interface(dev);
406 dev->driver = NULL;
407 up(&dev->sem);
Alan Stern36e56a32006-07-01 22:08:06 -0400408 }
Alan Stern36e56a32006-07-01 22:08:06 -0400409}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600410EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400411
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800412/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100413int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800414{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800415 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
416 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
417 return 0;
418
419 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
420 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
421 return 0;
422
423 /* No need to test id->bcdDevice_lo != 0, since 0 is never
424 greater than any unsigned number. */
425 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
426 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
427 return 0;
428
429 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
430 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
431 return 0;
432
433 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
434 (id->bDeviceClass != dev->descriptor.bDeviceClass))
435 return 0;
436
437 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800438 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800439 return 0;
440
441 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
442 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
443 return 0;
444
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100445 return 1;
446}
447
448/* returns 0 if no match, 1 if match */
449int usb_match_one_id(struct usb_interface *interface,
450 const struct usb_device_id *id)
451{
452 struct usb_host_interface *intf;
453 struct usb_device *dev;
454
455 /* proc_connectinfo in devio.c may call us with id == NULL. */
456 if (id == NULL)
457 return 0;
458
459 intf = interface->cur_altsetting;
460 dev = interface_to_usbdev(interface);
461
462 if (!usb_match_device(dev, id))
463 return 0;
464
Alan Stern93c8bf42006-10-18 16:41:51 -0400465 /* The interface class, subclass, and protocol should never be
466 * checked for a match if the device class is Vendor Specific,
467 * unless the match record specifies the Vendor ID. */
468 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
469 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
470 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
471 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
472 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
473 return 0;
474
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800475 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
476 (id->bInterfaceClass != intf->desc.bInterfaceClass))
477 return 0;
478
479 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
480 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
481 return 0;
482
483 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
484 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
485 return 0;
486
487 return 1;
488}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100489EXPORT_SYMBOL_GPL(usb_match_one_id);
490
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800491/**
492 * usb_match_id - find first usb_device_id matching device or interface
493 * @interface: the interface of interest
494 * @id: array of usb_device_id structures, terminated by zero entry
495 *
496 * usb_match_id searches an array of usb_device_id's and returns
497 * the first one matching the device or interface, or null.
498 * This is used when binding (or rebinding) a driver to an interface.
499 * Most USB device drivers will use this indirectly, through the usb core,
500 * but some layered driver frameworks use it directly.
501 * These device tables are exported with MODULE_DEVICE_TABLE, through
502 * modutils, to support the driver loading functionality of USB hotplugging.
503 *
504 * What Matches:
505 *
506 * The "match_flags" element in a usb_device_id controls which
507 * members are used. If the corresponding bit is set, the
508 * value in the device_id must match its corresponding member
509 * in the device or interface descriptor, or else the device_id
510 * does not match.
511 *
512 * "driver_info" is normally used only by device drivers,
513 * but you can create a wildcard "matches anything" usb_device_id
514 * as a driver's "modules.usbmap" entry if you provide an id with
515 * only a nonzero "driver_info" field. If you do this, the USB device
516 * driver's probe() routine should use additional intelligence to
517 * decide whether to bind to the specified interface.
518 *
519 * What Makes Good usb_device_id Tables:
520 *
521 * The match algorithm is very simple, so that intelligence in
522 * driver selection must come from smart driver id records.
523 * Unless you have good reasons to use another selection policy,
524 * provide match elements only in related groups, and order match
525 * specifiers from specific to general. Use the macros provided
526 * for that purpose if you can.
527 *
528 * The most specific match specifiers use device descriptor
529 * data. These are commonly used with product-specific matches;
530 * the USB_DEVICE macro lets you provide vendor and product IDs,
531 * and you can also match against ranges of product revisions.
532 * These are widely used for devices with application or vendor
533 * specific bDeviceClass values.
534 *
535 * Matches based on device class/subclass/protocol specifications
536 * are slightly more general; use the USB_DEVICE_INFO macro, or
537 * its siblings. These are used with single-function devices
538 * where bDeviceClass doesn't specify that each interface has
539 * its own class.
540 *
541 * Matches based on interface class/subclass/protocol are the
542 * most general; they let drivers bind to any interface on a
543 * multiple-function device. Use the USB_INTERFACE_INFO
544 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400545 * devices (as recorded in bInterfaceClass).
546 *
547 * Note that an entry created by USB_INTERFACE_INFO won't match
548 * any interface if the device class is set to Vendor-Specific.
549 * This is deliberate; according to the USB spec the meanings of
550 * the interface class/subclass/protocol for these devices are also
551 * vendor-specific, and hence matching against a standard product
552 * class wouldn't work anyway. If you really want to use an
553 * interface-based match for such a device, create a match record
554 * that also specifies the vendor ID. (Unforunately there isn't a
555 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800556 *
557 * Within those groups, remember that not all combinations are
558 * meaningful. For example, don't give a product version range
559 * without vendor and product IDs; or specify a protocol without
560 * its associated class and subclass.
561 */
562const struct usb_device_id *usb_match_id(struct usb_interface *interface,
563 const struct usb_device_id *id)
564{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800565 /* proc_connectinfo in devio.c may call us with id == NULL. */
566 if (id == NULL)
567 return NULL;
568
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800569 /* It is important to check that id->driver_info is nonzero,
570 since an entry that is all zeroes except for a nonzero
571 id->driver_info is the way to create an entry that
572 indicates that the driver want to examine every
573 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800574 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
575 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800576 if (usb_match_one_id(interface, id))
577 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800578 }
579
580 return NULL;
581}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600582EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800583
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100584static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800585{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400586 /* devices and interfaces are handled separately */
587 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800588
Alan Stern8bb54ab2006-07-01 22:08:49 -0400589 /* interface drivers never match devices */
590 if (!is_usb_device_driver(drv))
591 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800592
Alan Stern8bb54ab2006-07-01 22:08:49 -0400593 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800594 return 1;
595
Alan Stern8bb54ab2006-07-01 22:08:49 -0400596 } else {
597 struct usb_interface *intf;
598 struct usb_driver *usb_drv;
599 const struct usb_device_id *id;
600
601 /* device drivers never match interfaces */
602 if (is_usb_device_driver(drv))
603 return 0;
604
605 intf = to_usb_interface(dev);
606 usb_drv = to_usb_driver(drv);
607
608 id = usb_match_id(intf, usb_drv->id_table);
609 if (id)
610 return 1;
611
612 id = usb_match_dynamic_id(intf, usb_drv);
613 if (id)
614 return 1;
615 }
616
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800617 return 0;
618}
619
Alan Stern36e56a32006-07-01 22:08:06 -0400620#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200621static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400622{
Alan Stern36e56a32006-07-01 22:08:06 -0400623 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400624
Alan Stern36e56a32006-07-01 22:08:06 -0400625 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200626 pr_debug("usb %s: uevent\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400627
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100628 if (is_usb_device(dev))
Alan Stern782da722006-07-01 22:09:35 -0400629 usb_dev = to_usb_device(dev);
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100630 else {
631 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400632 usb_dev = interface_to_usbdev(intf);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400633 }
Alan Stern36e56a32006-07-01 22:08:06 -0400634
635 if (usb_dev->devnum < 0) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200636 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400637 return -ENODEV;
638 }
639 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200640 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400641 return -ENODEV;
642 }
643
644#ifdef CONFIG_USB_DEVICEFS
645 /* If this is available, userspace programs can directly read
646 * all the device descriptors we don't tell them about. Or
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100647 * act as usermode drivers.
Alan Stern36e56a32006-07-01 22:08:06 -0400648 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200649 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
Alan Stern36e56a32006-07-01 22:08:06 -0400650 usb_dev->bus->busnum, usb_dev->devnum))
651 return -ENOMEM;
652#endif
653
654 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200655 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400656 le16_to_cpu(usb_dev->descriptor.idVendor),
657 le16_to_cpu(usb_dev->descriptor.idProduct),
658 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
659 return -ENOMEM;
660
661 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200662 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400663 usb_dev->descriptor.bDeviceClass,
664 usb_dev->descriptor.bDeviceSubClass,
665 usb_dev->descriptor.bDeviceProtocol))
666 return -ENOMEM;
667
Alan Stern36e56a32006-07-01 22:08:06 -0400668 return 0;
669}
670
671#else
672
Kay Sievers7eff2e72007-08-14 15:15:12 +0200673static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400674{
675 return -ENODEV;
676}
Alan Stern36e56a32006-07-01 22:08:06 -0400677#endif /* CONFIG_HOTPLUG */
678
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800679/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400680 * usb_register_device_driver - register a USB device (not interface) driver
681 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800682 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800683 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400684 * Registers a USB device driver with the USB core. The list of
685 * unattached devices will be rescanned whenever a new driver is
686 * added, allowing the new driver to attach to any recognized devices.
687 * Returns a negative error code on failure and 0 on success.
688 */
689int usb_register_device_driver(struct usb_device_driver *new_udriver,
690 struct module *owner)
691{
692 int retval = 0;
693
694 if (usb_disabled())
695 return -ENODEV;
696
697 new_udriver->drvwrap.for_devices = 1;
698 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
699 new_udriver->drvwrap.driver.bus = &usb_bus_type;
700 new_udriver->drvwrap.driver.probe = usb_probe_device;
701 new_udriver->drvwrap.driver.remove = usb_unbind_device;
702 new_udriver->drvwrap.driver.owner = owner;
703
704 retval = driver_register(&new_udriver->drvwrap.driver);
705
706 if (!retval) {
707 pr_info("%s: registered new device driver %s\n",
708 usbcore_name, new_udriver->name);
709 usbfs_update_special();
710 } else {
711 printk(KERN_ERR "%s: error %d registering device "
712 " driver %s\n",
713 usbcore_name, retval, new_udriver->name);
714 }
715
716 return retval;
717}
718EXPORT_SYMBOL_GPL(usb_register_device_driver);
719
720/**
721 * usb_deregister_device_driver - unregister a USB device (not interface) driver
722 * @udriver: USB operations of the device driver to unregister
723 * Context: must be able to sleep
724 *
725 * Unlinks the specified driver from the internal USB driver list.
726 */
727void usb_deregister_device_driver(struct usb_device_driver *udriver)
728{
729 pr_info("%s: deregistering device driver %s\n",
730 usbcore_name, udriver->name);
731
732 driver_unregister(&udriver->drvwrap.driver);
733 usbfs_update_special();
734}
735EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
736
737/**
738 * usb_register_driver - register a USB interface driver
739 * @new_driver: USB operations for the interface driver
740 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800741 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400742 *
743 * Registers a USB interface driver with the USB core. The list of
744 * unattached interfaces will be rescanned whenever a new driver is
745 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800746 * Returns a negative error code on failure and 0 on success.
747 *
748 * NOTE: if you want your driver to use the USB major number, you must call
749 * usb_register_dev() to enable that functionality. This function no longer
750 * takes care of that.
751 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800752int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
753 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800754{
755 int retval = 0;
756
757 if (usb_disabled())
758 return -ENODEV;
759
Alan Stern8bb54ab2006-07-01 22:08:49 -0400760 new_driver->drvwrap.for_devices = 0;
761 new_driver->drvwrap.driver.name = (char *) new_driver->name;
762 new_driver->drvwrap.driver.bus = &usb_bus_type;
763 new_driver->drvwrap.driver.probe = usb_probe_interface;
764 new_driver->drvwrap.driver.remove = usb_unbind_interface;
765 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800766 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800767 spin_lock_init(&new_driver->dynids.lock);
768 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800769
Alan Stern8bb54ab2006-07-01 22:08:49 -0400770 retval = driver_register(&new_driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800771
772 if (!retval) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400773 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800774 usbcore_name, new_driver->name);
775 usbfs_update_special();
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800776 usb_create_newid_file(new_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800777 } else {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400778 printk(KERN_ERR "%s: error %d registering interface "
779 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800780 usbcore_name, retval, new_driver->name);
781 }
782
783 return retval;
784}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600785EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800786
787/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400788 * usb_deregister - unregister a USB interface driver
789 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800790 * Context: must be able to sleep
791 *
792 * Unlinks the specified driver from the internal USB driver list.
793 *
794 * NOTE: If you called usb_register_dev(), you still need to call
795 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
796 * this * call will no longer do it for you.
797 */
798void usb_deregister(struct usb_driver *driver)
799{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400800 pr_info("%s: deregistering interface driver %s\n",
801 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800802
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800803 usb_remove_newid_file(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800804 usb_free_dynids(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400805 driver_unregister(&driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800806
807 usbfs_update_special();
808}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600809EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400810
Alan Stern78d9a482008-06-23 16:00:40 -0400811/* Forced unbinding of a USB interface driver, either because
812 * it doesn't support pre_reset/post_reset/reset_resume or
813 * because it doesn't support suspend/resume.
814 *
815 * The caller must hold @intf's device's lock, but not its pm_mutex
816 * and not @intf->dev.sem.
817 */
818void usb_forced_unbind_intf(struct usb_interface *intf)
819{
820 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
821
822 dev_dbg(&intf->dev, "forced unbind\n");
823 usb_driver_release_interface(driver, intf);
824
825 /* Mark the interface for later rebinding */
826 intf->needs_binding = 1;
827}
828
829/* Delayed forced unbinding of a USB interface driver and scan
830 * for rebinding.
831 *
832 * The caller must hold @intf's device's lock, but not its pm_mutex
833 * and not @intf->dev.sem.
834 *
Alan Stern5096aed2008-08-12 14:34:14 -0400835 * Note: Rebinds will be skipped if a system sleep transition is in
836 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -0400837 */
838void usb_rebind_intf(struct usb_interface *intf)
839{
840 int rc;
841
842 /* Delayed unbind of an existing driver */
843 if (intf->dev.driver) {
844 struct usb_driver *driver =
845 to_usb_driver(intf->dev.driver);
846
847 dev_dbg(&intf->dev, "forced unbind\n");
848 usb_driver_release_interface(driver, intf);
849 }
850
851 /* Try to rebind the interface */
Alan Stern5096aed2008-08-12 14:34:14 -0400852 if (intf->dev.power.status == DPM_ON) {
853 intf->needs_binding = 0;
854 rc = device_attach(&intf->dev);
855 if (rc < 0)
856 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
857 }
Alan Stern78d9a482008-06-23 16:00:40 -0400858}
859
Alan Stern9ff78432008-08-07 13:04:51 -0400860#ifdef CONFIG_PM
861
Alan Stern78d9a482008-06-23 16:00:40 -0400862#define DO_UNBIND 0
863#define DO_REBIND 1
864
865/* Unbind drivers for @udev's interfaces that don't support suspend/resume,
866 * or rebind interfaces that have been unbound, according to @action.
867 *
868 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -0400869 */
870static void do_unbind_rebind(struct usb_device *udev, int action)
871{
872 struct usb_host_config *config;
873 int i;
874 struct usb_interface *intf;
875 struct usb_driver *drv;
876
877 config = udev->actconfig;
878 if (config) {
879 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
880 intf = config->interface[i];
881 switch (action) {
882 case DO_UNBIND:
883 if (intf->dev.driver) {
884 drv = to_usb_driver(intf->dev.driver);
885 if (!drv->suspend || !drv->resume)
886 usb_forced_unbind_intf(intf);
887 }
888 break;
889 case DO_REBIND:
Alan Stern5096aed2008-08-12 14:34:14 -0400890 if (intf->needs_binding)
Alan Stern78d9a482008-06-23 16:00:40 -0400891 usb_rebind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -0400892 break;
893 }
894 }
895 }
896}
897
Alan Sterne0318eb2006-09-26 14:50:20 -0400898/* Caller has locked udev's pm_mutex */
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -0800899static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -0400900{
Alan Stern782da722006-07-01 22:09:35 -0400901 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -0400902 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400903
Alan Stern114b3682006-07-01 22:13:04 -0400904 if (udev->state == USB_STATE_NOTATTACHED ||
905 udev->state == USB_STATE_SUSPENDED)
906 goto done;
907
Alan Sternb6f6436d2007-05-04 11:51:54 -0400908 /* For devices that don't have a driver, we do a generic suspend. */
909 if (udev->dev.driver)
910 udriver = to_usb_device_driver(udev->dev.driver);
911 else {
Alan Stern645daaa2006-08-30 15:47:02 -0400912 udev->do_remote_wakeup = 0;
Alan Sternb6f6436d2007-05-04 11:51:54 -0400913 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400914 }
Alan Stern2bf40862006-07-01 22:12:19 -0400915 status = udriver->suspend(udev, msg);
916
Alan Stern20dfdad2007-05-22 11:50:17 -0400917 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800918 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -0400919 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -0400920}
Alan Stern36e56a32006-07-01 22:08:06 -0400921
Alan Sterne0318eb2006-09-26 14:50:20 -0400922/* Caller has locked udev's pm_mutex */
Alan Stern65bfd292008-11-25 16:39:18 -0500923static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -0400924{
925 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -0400926 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -0400927
Alan Stern0458d5b2007-05-04 11:52:20 -0400928 if (udev->state == USB_STATE_NOTATTACHED)
929 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400930
Alan Stern1c5df7e2006-07-01 22:13:50 -0400931 /* Can't resume it if it doesn't have a driver. */
932 if (udev->dev.driver == NULL) {
933 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -0400934 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -0400935 }
936
Alan Stern6bc6cff2007-05-04 11:53:03 -0400937 if (udev->quirks & USB_QUIRK_RESET_RESUME)
938 udev->reset_resume = 1;
939
Alan Stern1cc8a252006-07-01 22:10:15 -0400940 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -0500941 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -0400942
Alan Stern20dfdad2007-05-22 11:50:17 -0400943 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800944 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -0500945 if (status == 0)
Alan Stern2add5222007-03-20 14:59:39 -0400946 udev->autoresume_disabled = 0;
Alan Stern2bf40862006-07-01 22:12:19 -0400947 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -0400948}
949
Alan Sterne0318eb2006-09-26 14:50:20 -0400950/* Caller has locked intf's usb_device's pm mutex */
Alan Stern65605ae2008-08-12 14:33:27 -0400951static int usb_suspend_interface(struct usb_device *udev,
952 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -0400953{
954 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400955 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -0400956
Alan Stern114b3682006-07-01 22:13:04 -0400957 /* with no hardware, USB interfaces only use FREEZE and ON states */
Alan Stern65605ae2008-08-12 14:33:27 -0400958 if (udev->state == USB_STATE_NOTATTACHED || !is_active(intf))
Alan Stern114b3682006-07-01 22:13:04 -0400959 goto done;
960
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800961 /* This can happen; see usb_driver_release_interface() */
962 if (intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -0400963 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -0400964 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -0400965
Alan Stern78d9a482008-06-23 16:00:40 -0400966 if (driver->suspend) {
Alan Stern1cc8a252006-07-01 22:10:15 -0400967 status = driver->suspend(intf, msg);
Alan Stern645daaa2006-08-30 15:47:02 -0400968 if (status == 0)
969 mark_quiesced(intf);
Alan Stern65bfd292008-11-25 16:39:18 -0500970 else if (!(msg.event & PM_EVENT_AUTO))
Alan Stern1cc8a252006-07-01 22:10:15 -0400971 dev_err(&intf->dev, "%s error %d\n",
972 "suspend", status);
Alan Stern36e56a32006-07-01 22:08:06 -0400973 } else {
Alan Stern78d9a482008-06-23 16:00:40 -0400974 /* Later we will unbind the driver and reprobe */
975 intf->needs_binding = 1;
976 dev_warn(&intf->dev, "no %s for driver %s?\n",
977 "suspend", driver->name);
Alan Stern36e56a32006-07-01 22:08:06 -0400978 mark_quiesced(intf);
Alan Stern36e56a32006-07-01 22:08:06 -0400979 }
Alan Stern2bf40862006-07-01 22:12:19 -0400980
Alan Stern20dfdad2007-05-22 11:50:17 -0400981 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -0800982 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -0400983 return status;
984}
985
Alan Stern645daaa2006-08-30 15:47:02 -0400986/* Caller has locked intf's usb_device's pm_mutex */
Alan Stern65605ae2008-08-12 14:33:27 -0400987static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -0500988 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -0400989{
Alan Stern1cc8a252006-07-01 22:10:15 -0400990 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -0400991 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400992
Alan Stern65605ae2008-08-12 14:33:27 -0400993 if (udev->state == USB_STATE_NOTATTACHED || is_active(intf))
Alan Stern2bf40862006-07-01 22:12:19 -0400994 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -0400995
Alan Stern645daaa2006-08-30 15:47:02 -0400996 /* Don't let autoresume interfere with unbinding */
997 if (intf->condition == USB_INTERFACE_UNBINDING)
998 goto done;
999
Alan Stern1c5df7e2006-07-01 22:13:50 -04001000 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001001 if (intf->condition == USB_INTERFACE_UNBOUND) {
1002
1003 /* Carry out a deferred switch to altsetting 0 */
1004 if (intf->needs_altsetting0 &&
1005 intf->dev.power.status == DPM_ON) {
1006 usb_set_interface(udev, intf->altsetting[0].
1007 desc.bInterfaceNumber, 0);
1008 intf->needs_altsetting0 = 0;
1009 }
Alan Stern2bf40862006-07-01 22:12:19 -04001010 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001011 }
Alan Stern78d9a482008-06-23 16:00:40 -04001012
1013 /* Don't resume if the interface is marked for rebinding */
1014 if (intf->needs_binding)
1015 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001016 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001017
Alan Sternf07600c2007-05-30 15:38:16 -04001018 if (reset_resume) {
1019 if (driver->reset_resume) {
1020 status = driver->reset_resume(intf);
1021 if (status)
1022 dev_err(&intf->dev, "%s error %d\n",
1023 "reset_resume", status);
1024 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001025 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001026 dev_warn(&intf->dev, "no %s for driver %s?\n",
1027 "reset_resume", driver->name);
1028 }
1029 } else {
1030 if (driver->resume) {
1031 status = driver->resume(intf);
1032 if (status)
1033 dev_err(&intf->dev, "%s error %d\n",
1034 "resume", status);
1035 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001036 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001037 dev_warn(&intf->dev, "no %s for driver %s?\n",
1038 "resume", driver->name);
1039 }
1040 }
Alan Stern2bf40862006-07-01 22:12:19 -04001041
1042done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001043 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern78d9a482008-06-23 16:00:40 -04001044 if (status == 0 && intf->condition == USB_INTERFACE_BOUND)
Alan Stern0458d5b2007-05-04 11:52:20 -04001045 mark_active(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04001046
Alan Stern78d9a482008-06-23 16:00:40 -04001047 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001048 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001049}
1050
Alan Stern94fcda12006-11-20 11:38:46 -05001051#ifdef CONFIG_USB_SUSPEND
1052
Alan Sternaf4f7602006-10-30 17:06:45 -05001053/* Internal routine to check whether we may autosuspend a device. */
Alan Sternd1aa3e62007-10-11 16:47:36 -04001054static int autosuspend_check(struct usb_device *udev, int reschedule)
Alan Sternaf4f7602006-10-30 17:06:45 -05001055{
1056 int i;
1057 struct usb_interface *intf;
Alan Sternd1aa3e62007-10-11 16:47:36 -04001058 unsigned long suspend_time, j;
Alan Sternaf4f7602006-10-30 17:06:45 -05001059
Alan Sternb5e795f2007-02-20 15:00:53 -05001060 /* For autosuspend, fail fast if anything is in use or autosuspend
1061 * is disabled. Also fail if any interfaces require remote wakeup
1062 * but it isn't available.
1063 */
Alan Sternaf4f7602006-10-30 17:06:45 -05001064 if (udev->pm_usage_cnt > 0)
1065 return -EBUSY;
Alan Stern2add5222007-03-20 14:59:39 -04001066 if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
Alan Sternb5e795f2007-02-20 15:00:53 -05001067 return -EPERM;
1068
Alan Stern19410442007-03-27 13:33:59 -04001069 suspend_time = udev->last_busy + udev->autosuspend_delay;
Alan Sternaf4f7602006-10-30 17:06:45 -05001070 if (udev->actconfig) {
1071 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1072 intf = udev->actconfig->interface[i];
1073 if (!is_active(intf))
1074 continue;
1075 if (intf->pm_usage_cnt > 0)
1076 return -EBUSY;
1077 if (intf->needs_remote_wakeup &&
1078 !udev->do_remote_wakeup) {
1079 dev_dbg(&udev->dev, "remote wakeup needed "
1080 "for autosuspend\n");
1081 return -EOPNOTSUPP;
1082 }
Alan Sternf07600c2007-05-30 15:38:16 -04001083
1084 /* Don't allow autosuspend if the device will need
1085 * a reset-resume and any of its interface drivers
1086 * doesn't include support.
1087 */
1088 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1089 struct usb_driver *driver;
1090
1091 driver = to_usb_driver(intf->dev.driver);
Oliver Neukum399d31d2008-09-15 17:29:28 +02001092 if (!driver->reset_resume ||
1093 intf->needs_remote_wakeup)
Alan Sternf07600c2007-05-30 15:38:16 -04001094 return -EOPNOTSUPP;
1095 }
Alan Sternaf4f7602006-10-30 17:06:45 -05001096 }
1097 }
Alan Stern19410442007-03-27 13:33:59 -04001098
1099 /* If everything is okay but the device hasn't been idle for long
Alan Sternd1aa3e62007-10-11 16:47:36 -04001100 * enough, queue a delayed autosuspend request. If the device
1101 * _has_ been idle for long enough and the reschedule flag is set,
1102 * likewise queue a delayed (1 second) autosuspend request.
Alan Stern19410442007-03-27 13:33:59 -04001103 */
Alan Sternd1aa3e62007-10-11 16:47:36 -04001104 j = jiffies;
1105 if (time_before(j, suspend_time))
1106 reschedule = 1;
1107 else
1108 suspend_time = j + HZ;
1109 if (reschedule) {
Alan Stern8c9862e2007-04-11 12:06:16 -04001110 if (!timer_pending(&udev->autosuspend.timer)) {
Alan Stern19410442007-03-27 13:33:59 -04001111 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
Alan Stern4ec06d62008-11-25 16:40:02 -05001112 round_jiffies_up_relative(suspend_time - j));
Alan Sternd1aa3e62007-10-11 16:47:36 -04001113 }
Alan Stern19410442007-03-27 13:33:59 -04001114 return -EAGAIN;
1115 }
Alan Sternaf4f7602006-10-30 17:06:45 -05001116 return 0;
1117}
1118
Alan Stern94fcda12006-11-20 11:38:46 -05001119#else
1120
Alan Sternd1aa3e62007-10-11 16:47:36 -04001121static inline int autosuspend_check(struct usb_device *udev, int reschedule)
Alan Sternef7f6c72007-04-05 16:03:49 -04001122{
1123 return 0;
1124}
Alan Stern94fcda12006-11-20 11:38:46 -05001125
Alan Sternb5e795f2007-02-20 15:00:53 -05001126#endif /* CONFIG_USB_SUSPEND */
Alan Stern94fcda12006-11-20 11:38:46 -05001127
Alan Stern645daaa2006-08-30 15:47:02 -04001128/**
1129 * usb_suspend_both - suspend a USB device and its interfaces
1130 * @udev: the usb_device to suspend
1131 * @msg: Power Management message describing this state transition
1132 *
1133 * This is the central routine for suspending USB devices. It calls the
1134 * suspend methods for all the interface drivers in @udev and then calls
1135 * the suspend method for @udev itself. If an error occurs at any stage,
1136 * all the interfaces which were suspended are resumed so that they remain
1137 * in the same state as the device.
1138 *
Alan Stern65bfd292008-11-25 16:39:18 -05001139 * If an autosuspend is in progress the routine checks first to make sure
1140 * that neither the device itself or any of its active interfaces is in use
1141 * (pm_usage_cnt is greater than 0). If they are, the autosuspend fails.
Alan Stern645daaa2006-08-30 15:47:02 -04001142 *
1143 * If the suspend succeeds, the routine recursively queues an autosuspend
1144 * request for @udev's parent device, thereby propagating the change up
1145 * the device tree. If all of the parent's children are now suspended,
1146 * the parent will autosuspend in turn.
1147 *
1148 * The suspend method calls are subject to mutual exclusion under control
1149 * of @udev's pm_mutex. Many of these calls are also under the protection
1150 * of @udev's device lock (including all requests originating outside the
1151 * USB subsystem), but autosuspend requests generated by a child device or
1152 * interface driver may not be. Usbcore will insure that the method calls
1153 * do not arrive during bind, unbind, or reset operations. However, drivers
1154 * must be prepared to handle suspend calls arriving at unpredictable times.
1155 * The only way to block such calls is to do an autoresume (preventing
1156 * autosuspends) while holding @udev's device lock (preventing outside
1157 * suspends).
1158 *
1159 * The caller must hold @udev->pm_mutex.
1160 *
1161 * This routine can run only in process context.
1162 */
Alan Stern718efa62007-03-09 15:41:13 -05001163static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001164{
1165 int status = 0;
1166 int i = 0;
1167 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -04001168 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -04001169
Alan Stern19410442007-03-27 13:33:59 -04001170 if (udev->state == USB_STATE_NOTATTACHED ||
1171 udev->state == USB_STATE_SUSPENDED)
1172 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001173
1174 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1175
Alan Stern65bfd292008-11-25 16:39:18 -05001176 if (msg.event & PM_EVENT_AUTO) {
Alan Sternd1aa3e62007-10-11 16:47:36 -04001177 status = autosuspend_check(udev, 0);
Alan Sternaf4f7602006-10-30 17:06:45 -05001178 if (status < 0)
Alan Stern19410442007-03-27 13:33:59 -04001179 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001180 }
1181
1182 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001183 if (udev->actconfig) {
1184 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1185 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001186 status = usb_suspend_interface(udev, intf, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001187 if (status != 0)
1188 break;
1189 }
1190 }
Alan Stern5ad4f712007-09-10 11:31:43 -04001191 if (status == 0)
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001192 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001193
1194 /* If the suspend failed, resume interfaces that did get suspended */
1195 if (status != 0) {
Alan Stern65bfd292008-11-25 16:39:18 -05001196 pm_message_t msg2;
1197
1198 msg2.event = msg.event ^ (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
Alan Sterna8e7c562006-07-01 22:11:02 -04001199 while (--i >= 0) {
1200 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001201 usb_resume_interface(udev, intf, msg2, 0);
Alan Sterna8e7c562006-07-01 22:11:02 -04001202 }
Alan Stern645daaa2006-08-30 15:47:02 -04001203
Alan Sternef7f6c72007-04-05 16:03:49 -04001204 /* Try another autosuspend when the interfaces aren't busy */
Alan Stern65bfd292008-11-25 16:39:18 -05001205 if (msg.event & PM_EVENT_AUTO)
Alan Sternd1aa3e62007-10-11 16:47:36 -04001206 autosuspend_check(udev, status == -EBUSY);
Alan Sternef7f6c72007-04-05 16:03:49 -04001207
Alan Stern6840d252007-09-10 11:34:26 -04001208 /* If the suspend succeeded then prevent any more URB submissions,
1209 * flush any outstanding URBs, and propagate the suspend up the tree.
1210 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001211 } else {
1212 cancel_delayed_work(&udev->autosuspend);
Alan Stern6840d252007-09-10 11:34:26 -04001213 udev->can_submit = 0;
1214 for (i = 0; i < 16; ++i) {
1215 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1216 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1217 }
Alan Stern7108f282007-09-20 12:37:50 -04001218
1219 /* If this is just a FREEZE or a PRETHAW, udev might
1220 * not really be suspended. Only true suspends get
1221 * propagated up the device tree.
1222 */
1223 if (parent && udev->state == USB_STATE_SUSPENDED)
Alan Sternef7f6c72007-04-05 16:03:49 -04001224 usb_autosuspend_device(parent);
1225 }
Alan Stern645daaa2006-08-30 15:47:02 -04001226
Alan Stern19410442007-03-27 13:33:59 -04001227 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001228 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001229 return status;
1230}
1231
Alan Stern645daaa2006-08-30 15:47:02 -04001232/**
1233 * usb_resume_both - resume a USB device and its interfaces
1234 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001235 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001236 *
1237 * This is the central routine for resuming USB devices. It calls the
1238 * the resume method for @udev and then calls the resume methods for all
1239 * the interface drivers in @udev.
1240 *
1241 * Before starting the resume, the routine calls itself recursively for
1242 * the parent device of @udev, thereby propagating the change up the device
1243 * tree and assuring that @udev will be able to resume. If the parent is
1244 * unable to resume successfully, the routine fails.
1245 *
1246 * The resume method calls are subject to mutual exclusion under control
1247 * of @udev's pm_mutex. Many of these calls are also under the protection
1248 * of @udev's device lock (including all requests originating outside the
1249 * USB subsystem), but autoresume requests generated by a child device or
1250 * interface driver may not be. Usbcore will insure that the method calls
1251 * do not arrive during bind, unbind, or reset operations. However, drivers
1252 * must be prepared to handle resume calls arriving at unpredictable times.
1253 * The only way to block such calls is to do an autoresume (preventing
1254 * other autoresumes) while holding @udev's device lock (preventing outside
1255 * resumes).
1256 *
1257 * The caller must hold @udev->pm_mutex.
1258 *
1259 * This routine can run only in process context.
1260 */
Alan Stern65bfd292008-11-25 16:39:18 -05001261static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001262{
Alan Stern645daaa2006-08-30 15:47:02 -04001263 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001264 int i;
1265 struct usb_interface *intf;
Alan Stern645daaa2006-08-30 15:47:02 -04001266 struct usb_device *parent = udev->parent;
Alan Sterna8e7c562006-07-01 22:11:02 -04001267
Alan Stern645daaa2006-08-30 15:47:02 -04001268 cancel_delayed_work(&udev->autosuspend);
Alan Stern19410442007-03-27 13:33:59 -04001269 if (udev->state == USB_STATE_NOTATTACHED) {
1270 status = -ENODEV;
1271 goto done;
1272 }
Alan Stern6840d252007-09-10 11:34:26 -04001273 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001274
1275 /* Propagate the resume up the tree, if necessary */
1276 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern65bfd292008-11-25 16:39:18 -05001277 if ((msg.event & PM_EVENT_AUTO) &&
1278 udev->autoresume_disabled) {
Alan Stern19410442007-03-27 13:33:59 -04001279 status = -EPERM;
1280 goto done;
1281 }
Alan Stern645daaa2006-08-30 15:47:02 -04001282 if (parent) {
Alan Stern94fcda12006-11-20 11:38:46 -05001283 status = usb_autoresume_device(parent);
Alan Sternee49fb52006-11-22 16:55:54 -05001284 if (status == 0) {
Alan Stern65bfd292008-11-25 16:39:18 -05001285 status = usb_resume_device(udev, msg);
Alan Sternf07600c2007-05-30 15:38:16 -04001286 if (status || udev->state ==
1287 USB_STATE_NOTATTACHED) {
Alan Stern94fcda12006-11-20 11:38:46 -05001288 usb_autosuspend_device(parent);
Alan Sternee49fb52006-11-22 16:55:54 -05001289
1290 /* It's possible usb_resume_device()
1291 * failed after the port was
1292 * unsuspended, causing udev to be
1293 * logically disconnected. We don't
1294 * want usb_disconnect() to autosuspend
1295 * the parent again, so tell it that
1296 * udev disconnected while still
1297 * suspended. */
1298 if (udev->state ==
1299 USB_STATE_NOTATTACHED)
1300 udev->discon_suspended = 1;
1301 }
1302 }
Alan Stern645daaa2006-08-30 15:47:02 -04001303 } else {
1304
1305 /* We can't progagate beyond the USB subsystem,
1306 * so if a root hub's controller is suspended
1307 * then we're stuck. */
Alan Stern65bfd292008-11-25 16:39:18 -05001308 status = usb_resume_device(udev, msg);
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -08001309 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001310 } else if (udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001311 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001312
Alan Sterna8e7c562006-07-01 22:11:02 -04001313 if (status == 0 && udev->actconfig) {
1314 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1315 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001316 usb_resume_interface(udev, intf, msg,
1317 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001318 }
1319 }
Alan Stern645daaa2006-08-30 15:47:02 -04001320
Alan Stern19410442007-03-27 13:33:59 -04001321 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001322 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001323 if (!status)
1324 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001325 return status;
1326}
1327
Alan Stern645daaa2006-08-30 15:47:02 -04001328#ifdef CONFIG_USB_SUSPEND
1329
Alan Stern94fcda12006-11-20 11:38:46 -05001330/* Internal routine to adjust a device's usage counter and change
1331 * its autosuspend state.
1332 */
1333static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1334{
1335 int status = 0;
1336
1337 usb_pm_lock(udev);
Alan Stern19410442007-03-27 13:33:59 -04001338 udev->auto_pm = 1;
Alan Stern94fcda12006-11-20 11:38:46 -05001339 udev->pm_usage_cnt += inc_usage_cnt;
1340 WARN_ON(udev->pm_usage_cnt < 0);
Alan Stern013d27f2007-08-20 12:18:39 -04001341 if (inc_usage_cnt)
1342 udev->last_busy = jiffies;
Alan Stern94fcda12006-11-20 11:38:46 -05001343 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
Alan Stern19410442007-03-27 13:33:59 -04001344 if (udev->state == USB_STATE_SUSPENDED)
Alan Stern65bfd292008-11-25 16:39:18 -05001345 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Alan Stern94fcda12006-11-20 11:38:46 -05001346 if (status != 0)
1347 udev->pm_usage_cnt -= inc_usage_cnt;
Alan Stern19410442007-03-27 13:33:59 -04001348 else if (inc_usage_cnt)
1349 udev->last_busy = jiffies;
1350 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
Alan Stern65bfd292008-11-25 16:39:18 -05001351 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Stern19410442007-03-27 13:33:59 -04001352 }
Alan Stern94fcda12006-11-20 11:38:46 -05001353 usb_pm_unlock(udev);
1354 return status;
1355}
1356
Alan Stern19410442007-03-27 13:33:59 -04001357/* usb_autosuspend_work - callback routine to autosuspend a USB device */
1358void usb_autosuspend_work(struct work_struct *work)
1359{
1360 struct usb_device *udev =
1361 container_of(work, struct usb_device, autosuspend.work);
1362
1363 usb_autopm_do_device(udev, 0);
1364}
1365
Alan Stern9ac39f22008-11-12 16:19:49 -05001366/* usb_autoresume_work - callback routine to autoresume a USB device */
1367void usb_autoresume_work(struct work_struct *work)
1368{
1369 struct usb_device *udev =
1370 container_of(work, struct usb_device, autoresume);
1371
1372 /* Wake it up, let the drivers do their thing, and then put it
1373 * back to sleep.
1374 */
1375 if (usb_autopm_do_device(udev, 1) == 0)
1376 usb_autopm_do_device(udev, -1);
1377}
1378
Alan Stern645daaa2006-08-30 15:47:02 -04001379/**
1380 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001381 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001382 *
1383 * This routine should be called when a core subsystem is finished using
1384 * @udev and wants to allow it to autosuspend. Examples would be when
1385 * @udev's device file in usbfs is closed or after a configuration change.
1386 *
Alan Stern94fcda12006-11-20 11:38:46 -05001387 * @udev's usage counter is decremented. If it or any of the usage counters
1388 * for an active interface is greater than 0, no autosuspend request will be
1389 * queued. (If an interface driver does not support autosuspend then its
1390 * usage counter is permanently positive.) Furthermore, if an interface
1391 * driver requires remote-wakeup capability during autosuspend but remote
1392 * wakeup is disabled, the autosuspend will fail.
Alan Stern645daaa2006-08-30 15:47:02 -04001393 *
1394 * Often the caller will hold @udev's device lock, but this is not
1395 * necessary.
1396 *
1397 * This routine can run only in process context.
1398 */
Alan Stern94fcda12006-11-20 11:38:46 -05001399void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001400{
Alan Stern94fcda12006-11-20 11:38:46 -05001401 int status;
1402
1403 status = usb_autopm_do_device(udev, -1);
Alan Stern20dfdad2007-05-22 11:50:17 -04001404 dev_vdbg(&udev->dev, "%s: cnt %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001405 __func__, udev->pm_usage_cnt);
Alan Stern645daaa2006-08-30 15:47:02 -04001406}
1407
1408/**
Alan Stern19c26232007-02-20 15:03:32 -05001409 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1410 * @udev: the usb_device to autosuspend
1411 *
1412 * This routine should be called when a core subsystem thinks @udev may
1413 * be ready to autosuspend.
1414 *
1415 * @udev's usage counter left unchanged. If it or any of the usage counters
1416 * for an active interface is greater than 0, or autosuspend is not allowed
1417 * for any other reason, no autosuspend request will be queued.
1418 *
1419 * This routine can run only in process context.
1420 */
1421void usb_try_autosuspend_device(struct usb_device *udev)
1422{
1423 usb_autopm_do_device(udev, 0);
Alan Stern20dfdad2007-05-22 11:50:17 -04001424 dev_vdbg(&udev->dev, "%s: cnt %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001425 __func__, udev->pm_usage_cnt);
Alan Stern19c26232007-02-20 15:03:32 -05001426}
1427
1428/**
Alan Stern645daaa2006-08-30 15:47:02 -04001429 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001430 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001431 *
1432 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001433 * and needs to guarantee that it is not suspended. No autosuspend will
1434 * occur until usb_autosuspend_device is called. (Note that this will not
1435 * prevent suspend events originating in the PM core.) Examples would be
1436 * when @udev's device file in usbfs is opened or when a remote-wakeup
1437 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001438 *
Alan Stern94fcda12006-11-20 11:38:46 -05001439 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1440 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001441 *
1442 * Often the caller will hold @udev's device lock, but this is not
1443 * necessary (and attempting it might cause deadlock).
1444 *
1445 * This routine can run only in process context.
1446 */
Alan Stern94fcda12006-11-20 11:38:46 -05001447int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001448{
1449 int status;
1450
Alan Stern94fcda12006-11-20 11:38:46 -05001451 status = usb_autopm_do_device(udev, 1);
Alan Stern20dfdad2007-05-22 11:50:17 -04001452 dev_vdbg(&udev->dev, "%s: status %d cnt %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001453 __func__, status, udev->pm_usage_cnt);
Alan Stern645daaa2006-08-30 15:47:02 -04001454 return status;
1455}
1456
Alan Sternaf4f7602006-10-30 17:06:45 -05001457/* Internal routine to adjust an interface's usage counter and change
1458 * its device's autosuspend state.
1459 */
1460static int usb_autopm_do_interface(struct usb_interface *intf,
1461 int inc_usage_cnt)
1462{
1463 struct usb_device *udev = interface_to_usbdev(intf);
1464 int status = 0;
1465
1466 usb_pm_lock(udev);
1467 if (intf->condition == USB_INTERFACE_UNBOUND)
1468 status = -ENODEV;
1469 else {
Alan Stern19410442007-03-27 13:33:59 -04001470 udev->auto_pm = 1;
Alan Sternaf4f7602006-10-30 17:06:45 -05001471 intf->pm_usage_cnt += inc_usage_cnt;
Alan Stern013d27f2007-08-20 12:18:39 -04001472 udev->last_busy = jiffies;
Alan Sternaf4f7602006-10-30 17:06:45 -05001473 if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) {
Alan Stern19410442007-03-27 13:33:59 -04001474 if (udev->state == USB_STATE_SUSPENDED)
Alan Stern65bfd292008-11-25 16:39:18 -05001475 status = usb_resume_both(udev,
1476 PMSG_AUTO_RESUME);
Alan Sternaf4f7602006-10-30 17:06:45 -05001477 if (status != 0)
1478 intf->pm_usage_cnt -= inc_usage_cnt;
Alan Stern013d27f2007-08-20 12:18:39 -04001479 else
Alan Stern19410442007-03-27 13:33:59 -04001480 udev->last_busy = jiffies;
1481 } else if (inc_usage_cnt <= 0 && intf->pm_usage_cnt <= 0) {
Alan Stern65bfd292008-11-25 16:39:18 -05001482 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Stern19410442007-03-27 13:33:59 -04001483 }
Alan Sternaf4f7602006-10-30 17:06:45 -05001484 }
1485 usb_pm_unlock(udev);
1486 return status;
1487}
1488
Alan Stern645daaa2006-08-30 15:47:02 -04001489/**
1490 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001491 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001492 *
1493 * This routine should be called by an interface driver when it is
1494 * finished using @intf and wants to allow it to autosuspend. A typical
1495 * example would be a character-device driver when its device file is
1496 * closed.
1497 *
1498 * The routine decrements @intf's usage counter. When the counter reaches
1499 * 0, a delayed autosuspend request for @intf's device is queued. When
1500 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all
1501 * the other usage counters for the sibling interfaces and @intf's
1502 * usb_device, the device and all its interfaces will be autosuspended.
1503 *
1504 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1505 * core will not change its value other than the increment and decrement
1506 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1507 * may use this simple counter-oriented discipline or may set the value
1508 * any way it likes.
1509 *
1510 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1511 * take place only if the device's remote-wakeup facility is enabled.
1512 *
1513 * Suspend method calls queued by this routine can arrive at any time
1514 * while @intf is resumed and its usage counter is equal to 0. They are
1515 * not protected by the usb_device's lock but only by its pm_mutex.
1516 * Drivers must provide their own synchronization.
1517 *
1518 * This routine can run only in process context.
1519 */
1520void usb_autopm_put_interface(struct usb_interface *intf)
1521{
Alan Sternaf4f7602006-10-30 17:06:45 -05001522 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001523
Alan Sternaf4f7602006-10-30 17:06:45 -05001524 status = usb_autopm_do_interface(intf, -1);
Alan Stern20dfdad2007-05-22 11:50:17 -04001525 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001526 __func__, status, intf->pm_usage_cnt);
Alan Stern645daaa2006-08-30 15:47:02 -04001527}
1528EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1529
1530/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001531 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1532 * @intf: the usb_interface whose counter should be decremented
1533 *
1534 * This routine does essentially the same thing as
1535 * usb_autopm_put_interface(): it decrements @intf's usage counter and
1536 * queues a delayed autosuspend request if the counter is <= 0. The
1537 * difference is that it does not acquire the device's pm_mutex;
1538 * callers must handle all synchronization issues themselves.
1539 *
1540 * Typically a driver would call this routine during an URB's completion
1541 * handler, if no more URBs were pending.
1542 *
1543 * This routine can run in atomic context.
1544 */
1545void usb_autopm_put_interface_async(struct usb_interface *intf)
1546{
1547 struct usb_device *udev = interface_to_usbdev(intf);
1548 int status = 0;
1549
1550 if (intf->condition == USB_INTERFACE_UNBOUND) {
1551 status = -ENODEV;
1552 } else {
1553 udev->last_busy = jiffies;
1554 --intf->pm_usage_cnt;
1555 if (udev->autosuspend_disabled || udev->autosuspend_delay < 0)
1556 status = -EPERM;
1557 else if (intf->pm_usage_cnt <= 0 &&
1558 !timer_pending(&udev->autosuspend.timer)) {
1559 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
Alan Stern4ec06d62008-11-25 16:40:02 -05001560 round_jiffies_up_relative(
Alan Stern9ac39f22008-11-12 16:19:49 -05001561 udev->autosuspend_delay));
1562 }
1563 }
1564 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1565 __func__, status, intf->pm_usage_cnt);
1566}
1567EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1568
1569/**
Alan Stern645daaa2006-08-30 15:47:02 -04001570 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001571 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001572 *
1573 * This routine should be called by an interface driver when it wants to
1574 * use @intf and needs to guarantee that it is not suspended. In addition,
1575 * the routine prevents @intf from being autosuspended subsequently. (Note
1576 * that this will not prevent suspend events originating in the PM core.)
1577 * This prevention will persist until usb_autopm_put_interface() is called
1578 * or @intf is unbound. A typical example would be a character-device
1579 * driver when its device file is opened.
1580 *
Alan Stern19410442007-03-27 13:33:59 -04001581 *
1582 * The routine increments @intf's usage counter. (However if the
1583 * autoresume fails then the counter is re-decremented.) So long as the
1584 * counter is greater than 0, autosuspend will not be allowed for @intf
1585 * or its usb_device. When the driver is finished using @intf it should
1586 * call usb_autopm_put_interface() to decrement the usage counter and
1587 * queue a delayed autosuspend request (if the counter is <= 0).
1588 *
Alan Stern645daaa2006-08-30 15:47:02 -04001589 *
1590 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1591 * core will not change its value other than the increment and decrement
1592 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1593 * may use this simple counter-oriented discipline or may set the value
1594 * any way it likes.
1595 *
1596 * Resume method calls generated by this routine can arrive at any time
1597 * while @intf is suspended. They are not protected by the usb_device's
1598 * lock but only by its pm_mutex. Drivers must provide their own
1599 * synchronization.
1600 *
1601 * This routine can run only in process context.
1602 */
1603int usb_autopm_get_interface(struct usb_interface *intf)
1604{
Alan Sternaf4f7602006-10-30 17:06:45 -05001605 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001606
Alan Sternaf4f7602006-10-30 17:06:45 -05001607 status = usb_autopm_do_interface(intf, 1);
Alan Stern20dfdad2007-05-22 11:50:17 -04001608 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001609 __func__, status, intf->pm_usage_cnt);
Alan Stern645daaa2006-08-30 15:47:02 -04001610 return status;
1611}
1612EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1613
Alan Stern692a1862006-10-30 17:07:51 -05001614/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001615 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1616 * @intf: the usb_interface whose counter should be incremented
1617 *
1618 * This routine does much the same thing as
1619 * usb_autopm_get_interface(): it increments @intf's usage counter and
1620 * queues an autoresume request if the result is > 0. The differences
1621 * are that it does not acquire the device's pm_mutex (callers must
1622 * handle all synchronization issues themselves), and it does not
1623 * autoresume the device directly (it only queues a request). After a
1624 * successful call, the device will generally not yet be resumed.
1625 *
1626 * This routine can run in atomic context.
1627 */
1628int usb_autopm_get_interface_async(struct usb_interface *intf)
1629{
1630 struct usb_device *udev = interface_to_usbdev(intf);
1631 int status = 0;
1632
1633 if (intf->condition == USB_INTERFACE_UNBOUND)
1634 status = -ENODEV;
1635 else if (udev->autoresume_disabled)
1636 status = -EPERM;
1637 else if (++intf->pm_usage_cnt > 0 && udev->state == USB_STATE_SUSPENDED)
1638 queue_work(ksuspend_usb_wq, &udev->autoresume);
1639 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
1640 __func__, status, intf->pm_usage_cnt);
1641 return status;
1642}
1643EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1644
1645/**
Alan Stern692a1862006-10-30 17:07:51 -05001646 * usb_autopm_set_interface - set a USB interface's autosuspend state
1647 * @intf: the usb_interface whose state should be set
1648 *
1649 * This routine sets the autosuspend state of @intf's device according
1650 * to @intf's usage counter, which the caller must have set previously.
1651 * If the counter is <= 0, the device is autosuspended (if it isn't
1652 * already suspended and if nothing else prevents the autosuspend). If
1653 * the counter is > 0, the device is autoresumed (if it isn't already
1654 * awake).
1655 */
1656int usb_autopm_set_interface(struct usb_interface *intf)
1657{
1658 int status;
1659
1660 status = usb_autopm_do_interface(intf, 0);
Alan Stern20dfdad2007-05-22 11:50:17 -04001661 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n",
Harvey Harrison441b62c2008-03-03 16:08:34 -08001662 __func__, status, intf->pm_usage_cnt);
Alan Stern692a1862006-10-30 17:07:51 -05001663 return status;
1664}
1665EXPORT_SYMBOL_GPL(usb_autopm_set_interface);
1666
Alan Stern718efa62007-03-09 15:41:13 -05001667#else
1668
1669void usb_autosuspend_work(struct work_struct *work)
1670{}
1671
Alan Stern9ac39f22008-11-12 16:19:49 -05001672void usb_autoresume_work(struct work_struct *work)
1673{}
1674
Alan Stern645daaa2006-08-30 15:47:02 -04001675#endif /* CONFIG_USB_SUSPEND */
1676
Alan Stern6b157c92007-03-13 16:37:30 -04001677/**
1678 * usb_external_suspend_device - external suspend of a USB device and its interfaces
1679 * @udev: the usb_device to suspend
1680 * @msg: Power Management message describing this state transition
1681 *
1682 * This routine handles external suspend requests: ones not generated
1683 * internally by a USB driver (autosuspend) but rather coming from the user
1684 * (via sysfs) or the PM core (system sleep). The suspend will be carried
1685 * out regardless of @udev's usage counter or those of its interfaces,
1686 * and regardless of whether or not remote wakeup is enabled. Of course,
1687 * interface drivers still have the option of failing the suspend (if
1688 * there are unsuspended children, for example).
1689 *
1690 * The caller must hold @udev's device lock.
1691 */
1692int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001693{
1694 int status;
1695
Alan Stern78d9a482008-06-23 16:00:40 -04001696 do_unbind_rebind(udev, DO_UNBIND);
Alan Stern6b157c92007-03-13 16:37:30 -04001697 usb_pm_lock(udev);
1698 udev->auto_pm = 0;
1699 status = usb_suspend_both(udev, msg);
1700 usb_pm_unlock(udev);
Alan Stern1cc8a252006-07-01 22:10:15 -04001701 return status;
1702}
1703
Alan Stern6b157c92007-03-13 16:37:30 -04001704/**
1705 * usb_external_resume_device - external resume of a USB device and its interfaces
1706 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001707 * @msg: Power Management message describing this state transition
Alan Stern6b157c92007-03-13 16:37:30 -04001708 *
1709 * This routine handles external resume requests: ones not generated
1710 * internally by a USB driver (autoresume) but rather coming from the user
1711 * (via sysfs), the PM core (system resume), or the device itself (remote
1712 * wakeup). @udev's usage counter is unaffected.
1713 *
1714 * The caller must hold @udev's device lock.
1715 */
Alan Stern65bfd292008-11-25 16:39:18 -05001716int usb_external_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern6b157c92007-03-13 16:37:30 -04001717{
1718 int status;
1719
1720 usb_pm_lock(udev);
1721 udev->auto_pm = 0;
Alan Stern65bfd292008-11-25 16:39:18 -05001722 status = usb_resume_both(udev, msg);
Alan Sternef7f6c72007-04-05 16:03:49 -04001723 udev->last_busy = jiffies;
Alan Stern6b157c92007-03-13 16:37:30 -04001724 usb_pm_unlock(udev);
Alan Stern6c640942008-10-21 15:40:03 -04001725 if (status == 0)
1726 do_unbind_rebind(udev, DO_REBIND);
Alan Stern6b157c92007-03-13 16:37:30 -04001727
1728 /* Now that the device is awake, we can start trying to autosuspend
1729 * it again. */
1730 if (status == 0)
1731 usb_try_autosuspend_device(udev);
1732 return status;
1733}
1734
Alan Stern65bfd292008-11-25 16:39:18 -05001735int usb_suspend(struct device *dev, pm_message_t msg)
Alan Stern6b157c92007-03-13 16:37:30 -04001736{
Alan Stern271f9e62007-10-10 16:30:12 -04001737 struct usb_device *udev;
1738
Alan Stern271f9e62007-10-10 16:30:12 -04001739 udev = to_usb_device(dev);
1740
1741 /* If udev is already suspended, we can skip this suspend and
Alan Stern3bb1af52008-03-03 15:15:36 -05001742 * we should also skip the upcoming system resume. High-speed
1743 * root hubs are an exception; they need to resume whenever the
1744 * system wakes up in order for USB-PERSIST port handover to work
1745 * properly.
1746 */
Alan Stern271f9e62007-10-10 16:30:12 -04001747 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern3bb1af52008-03-03 15:15:36 -05001748 if (udev->parent || udev->speed != USB_SPEED_HIGH)
1749 udev->skip_sys_resume = 1;
Alan Stern271f9e62007-10-10 16:30:12 -04001750 return 0;
1751 }
1752
1753 udev->skip_sys_resume = 0;
Alan Stern65bfd292008-11-25 16:39:18 -05001754 return usb_external_suspend_device(udev, msg);
Alan Stern6b157c92007-03-13 16:37:30 -04001755}
1756
Alan Stern65bfd292008-11-25 16:39:18 -05001757int usb_resume(struct device *dev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001758{
Alan Stern2add5222007-03-20 14:59:39 -04001759 struct usb_device *udev;
1760
Alan Stern2add5222007-03-20 14:59:39 -04001761 udev = to_usb_device(dev);
Alan Stern0458d5b2007-05-04 11:52:20 -04001762
Alan Stern271f9e62007-10-10 16:30:12 -04001763 /* If udev->skip_sys_resume is set then udev was already suspended
Alan Stern8808f002008-04-28 11:06:55 -04001764 * when the system sleep started, so we don't want to resume it
1765 * during this system wakeup.
1766 */
1767 if (udev->skip_sys_resume)
1768 return 0;
Alan Stern65bfd292008-11-25 16:39:18 -05001769 return usb_external_resume_device(udev, msg);
Alan Stern1cc8a252006-07-01 22:10:15 -04001770}
1771
Alan Stern36e56a32006-07-01 22:08:06 -04001772#endif /* CONFIG_PM */
1773
1774struct bus_type usb_bus_type = {
1775 .name = "usb",
1776 .match = usb_device_match,
1777 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001778};