blob: d40ff95688135ce82a1c4ccbfac6d129104cd874 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040027#include <linux/export.h>
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080028#include <linux/usb.h>
Alan Stern6bc6cff2007-05-04 11:53:03 -040029#include <linux/usb/quirks.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020030#include <linux/usb/hcd.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020031
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080032#include "usb.h"
33
Alan Stern20dfdad2007-05-22 11:50:17 -040034
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080035#ifdef CONFIG_HOTPLUG
36
37/*
38 * Adds a new dynamic USBdevice ID to this driver,
39 * and cause the driver to probe for all devices again.
40 */
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010041ssize_t usb_store_new_id(struct usb_dynids *dynids,
42 struct device_driver *driver,
43 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080044{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080045 struct usb_dynid *dynid;
46 u32 idVendor = 0;
47 u32 idProduct = 0;
Josua Dietzeff231db2011-10-23 14:22:29 +020048 unsigned int bInterfaceClass = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080049 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070050 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080051
Josua Dietzeff231db2011-10-23 14:22:29 +020052 fields = sscanf(buf, "%x %x %x", &idVendor, &idProduct,
53 &bInterfaceClass);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080054 if (fields < 2)
55 return -EINVAL;
56
57 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
58 if (!dynid)
59 return -ENOMEM;
60
61 INIT_LIST_HEAD(&dynid->node);
62 dynid->id.idVendor = idVendor;
63 dynid->id.idProduct = idProduct;
64 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Josua Dietzeff231db2011-10-23 14:22:29 +020065 if (fields == 3) {
66 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
67 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
68 }
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080069
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010070 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020071 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010072 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080073
74 if (get_driver(driver)) {
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070075 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080076 put_driver(driver);
77 }
78
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070079 if (retval)
80 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080081 return count;
82}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010083EXPORT_SYMBOL_GPL(usb_store_new_id);
84
85static ssize_t store_new_id(struct device_driver *driver,
86 const char *buf, size_t count)
87{
88 struct usb_driver *usb_drv = to_usb_driver(driver);
89
90 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
91}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080092static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
93
CHENG Renquan0c7a2b72009-11-22 01:28:52 +080094/**
95 * store_remove_id - remove a USB device ID from this driver
96 * @driver: target device driver
97 * @buf: buffer for scanning device ID data
98 * @count: input size
99 *
100 * Removes a dynamic usb device ID from this driver.
101 */
102static ssize_t
103store_remove_id(struct device_driver *driver, const char *buf, size_t count)
104{
105 struct usb_dynid *dynid, *n;
106 struct usb_driver *usb_driver = to_usb_driver(driver);
107 u32 idVendor = 0;
108 u32 idProduct = 0;
109 int fields = 0;
110 int retval = 0;
111
112 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
113 if (fields < 2)
114 return -EINVAL;
115
116 spin_lock(&usb_driver->dynids.lock);
117 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
118 struct usb_device_id *id = &dynid->id;
119 if ((id->idVendor == idVendor) &&
120 (id->idProduct == idProduct)) {
121 list_del(&dynid->node);
122 kfree(dynid);
123 retval = 0;
124 break;
125 }
126 }
127 spin_unlock(&usb_driver->dynids.lock);
128
129 if (retval)
130 return retval;
131 return count;
132}
133static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
134
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800135static int usb_create_newid_file(struct usb_driver *usb_drv)
136{
137 int error = 0;
138
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800139 if (usb_drv->no_dynamic_id)
140 goto exit;
141
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800142 if (usb_drv->probe != NULL)
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800143 error = driver_create_file(&usb_drv->drvwrap.driver,
144 &driver_attr_new_id);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800145exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800146 return error;
147}
148
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800149static void usb_remove_newid_file(struct usb_driver *usb_drv)
150{
151 if (usb_drv->no_dynamic_id)
152 return;
153
154 if (usb_drv->probe != NULL)
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800155 driver_remove_file(&usb_drv->drvwrap.driver,
156 &driver_attr_new_id);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800157}
158
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800159static int
160usb_create_removeid_file(struct usb_driver *drv)
161{
162 int error = 0;
163 if (drv->probe != NULL)
164 error = driver_create_file(&drv->drvwrap.driver,
165 &driver_attr_remove_id);
166 return error;
167}
168
169static void usb_remove_removeid_file(struct usb_driver *drv)
170{
171 driver_remove_file(&drv->drvwrap.driver, &driver_attr_remove_id);
172}
173
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800174static void usb_free_dynids(struct usb_driver *usb_drv)
175{
176 struct usb_dynid *dynid, *n;
177
178 spin_lock(&usb_drv->dynids.lock);
179 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
180 list_del(&dynid->node);
181 kfree(dynid);
182 }
183 spin_unlock(&usb_drv->dynids.lock);
184}
185#else
186static inline int usb_create_newid_file(struct usb_driver *usb_drv)
187{
188 return 0;
189}
190
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800191static void usb_remove_newid_file(struct usb_driver *usb_drv)
192{
193}
194
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800195static int
196usb_create_removeid_file(struct usb_driver *drv)
197{
198 return 0;
199}
200
201static void usb_remove_removeid_file(struct usb_driver *drv)
202{
203}
204
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800205static inline void usb_free_dynids(struct usb_driver *usb_drv)
206{
207}
208#endif
209
210static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
211 struct usb_driver *drv)
212{
213 struct usb_dynid *dynid;
214
215 spin_lock(&drv->dynids.lock);
216 list_for_each_entry(dynid, &drv->dynids.list, node) {
217 if (usb_match_one_id(intf, &dynid->id)) {
218 spin_unlock(&drv->dynids.lock);
219 return &dynid->id;
220 }
221 }
222 spin_unlock(&drv->dynids.lock);
223 return NULL;
224}
225
226
Alan Stern8bb54ab2006-07-01 22:08:49 -0400227/* called from driver core with dev locked */
228static int usb_probe_device(struct device *dev)
229{
230 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200231 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500232 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400233
Harvey Harrison441b62c2008-03-03 16:08:34 -0800234 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400235
Alan Stern8bb54ab2006-07-01 22:08:49 -0400236 /* TODO: Add real matching code */
237
Alan Stern645daaa2006-08-30 15:47:02 -0400238 /* The device should always appear to be in use
239 * unless the driver suports autosuspend.
240 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500241 if (!udriver->supports_autosuspend)
242 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400243
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500244 if (!error)
245 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400246 return error;
247}
248
249/* called from driver core with dev locked */
250static int usb_unbind_device(struct device *dev)
251{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500252 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400253 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
254
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500255 udriver->disconnect(udev);
256 if (!udriver->supports_autosuspend)
257 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400258 return 0;
259}
260
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800261/*
262 * Cancel any pending scheduled resets
263 *
264 * [see usb_queue_reset_device()]
265 *
266 * Called after unconfiguring / when releasing interfaces. See
267 * comments in __usb_queue_reset_device() regarding
268 * udev->reset_running.
269 */
270static void usb_cancel_queued_reset(struct usb_interface *iface)
271{
272 if (iface->reset_running == 0)
273 cancel_work_sync(&iface->reset_ws);
274}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400275
276/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800277static int usb_probe_interface(struct device *dev)
278{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400279 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200280 struct usb_interface *intf = to_usb_interface(dev);
281 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800282 const struct usb_device_id *id;
283 int error = -ENODEV;
284
Harvey Harrison441b62c2008-03-03 16:08:34 -0800285 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800286
Alan Stern78d9a482008-06-23 16:00:40 -0400287 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800288
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400289 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500290 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400291
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800292 if (udev->authorized == 0) {
293 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500294 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800295 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700296
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800297 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800298 if (!id)
299 id = usb_match_dynamic_id(intf, driver);
Alan Stern0f3dda92010-01-08 12:56:04 -0500300 if (!id)
301 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800302
Alan Stern0f3dda92010-01-08 12:56:04 -0500303 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400304
Alan Stern0f3dda92010-01-08 12:56:04 -0500305 error = usb_autoresume_device(udev);
306 if (error)
307 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400308
Alan Stern0f3dda92010-01-08 12:56:04 -0500309 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400310
Alan Stern571dc792010-04-09 16:03:43 -0400311 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500312 * runtime-PM-enabled only if the driver has autosuspend support.
313 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500314 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500315 pm_runtime_set_active(dev);
316 pm_suspend_ignore_children(dev, false);
317 if (driver->supports_autosuspend)
318 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200319
Alan Stern0f3dda92010-01-08 12:56:04 -0500320 /* Carry out a deferred switch to altsetting 0 */
321 if (intf->needs_altsetting0) {
322 error = usb_set_interface(udev, intf->altsetting[0].
323 desc.bInterfaceNumber, 0);
324 if (error < 0)
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200325 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500326 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800327 }
328
Alan Stern0f3dda92010-01-08 12:56:04 -0500329 error = driver->probe(intf, id);
330 if (error)
331 goto err;
332
333 intf->condition = USB_INTERFACE_BOUND;
334 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800335 return error;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200336
Alan Stern0f3dda92010-01-08 12:56:04 -0500337 err:
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200338 intf->needs_remote_wakeup = 0;
339 intf->condition = USB_INTERFACE_UNBOUND;
340 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500341
342 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400343 if (driver->supports_autosuspend)
344 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500345 pm_runtime_set_suspended(dev);
346
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200347 usb_autosuspend_device(udev);
348 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800349}
350
Alan Stern8bb54ab2006-07-01 22:08:49 -0400351/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800352static int usb_unbind_interface(struct device *dev)
353{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400354 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800355 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400356 struct usb_device *udev;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200357 int error, r;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800358
359 intf->condition = USB_INTERFACE_UNBINDING;
360
Alan Stern645daaa2006-08-30 15:47:02 -0400361 /* Autoresume for set_interface call below */
362 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500363 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400364
Alan Stern9da82bd2008-05-08 11:54:37 -0400365 /* Terminate all URBs for this interface unless the driver
366 * supports "soft" unbinding.
367 */
368 if (!driver->soft_unbind)
Alan Sternddeac4e72009-01-15 17:03:33 -0500369 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800370
Alan Stern8bb54ab2006-07-01 22:08:49 -0400371 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800372 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800373
Alan Stern55151d72008-08-12 14:33:59 -0400374 /* Reset other interface state.
375 * We cannot do a Set-Interface if the device is suspended or
376 * if it is prepared for a system sleep (since installing a new
377 * altsetting means creating new endpoint device entries).
378 * When either of these happens, defer the Set-Interface.
379 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500380 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
381 /* Already in altsetting 0 so skip Set-Interface.
382 * Just re-enable it without affecting the endpoint toggles.
383 */
384 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200385 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200386 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400387 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200388 if (r < 0)
389 intf->needs_altsetting0 = 1;
390 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400391 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200392 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800393 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400394
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800395 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400396 intf->needs_remote_wakeup = 0;
397
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500398 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400399 if (driver->supports_autosuspend)
400 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500401 pm_runtime_set_suspended(dev);
402
403 /* Undo any residual pm_autopm_get_interface_* calls */
404 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
405 usb_autopm_put_interface_no_suspend(intf);
406 atomic_set(&intf->pm_usage_cnt, 0);
407
Alan Stern645daaa2006-08-30 15:47:02 -0400408 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500409 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800410
411 return 0;
412}
413
Alan Stern36e56a32006-07-01 22:08:06 -0400414/**
415 * usb_driver_claim_interface - bind a driver to an interface
416 * @driver: the driver to be bound
417 * @iface: the interface to which it will be bound; must be in the
418 * usb device's active configuration
419 * @priv: driver data associated with that interface
420 *
421 * This is used by usb device drivers that need to claim more than one
422 * interface on a device when probing (audio and acm are current examples).
423 * No device driver should directly modify internal usb_interface or
424 * usb_device structure members.
425 *
426 * Few drivers should need to use this routine, since the most natural
427 * way to bind to an interface is to return the private data from
428 * the driver's probe() method.
429 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400430 * Callers must own the device lock, so driver probe() entries don't need
431 * extra locking, but other call contexts may need to explicitly claim that
432 * lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400433 */
434int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800435 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400436{
437 struct device *dev = &iface->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700438 int retval = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400439
440 if (dev->driver)
441 return -EBUSY;
442
Alan Stern8bb54ab2006-07-01 22:08:49 -0400443 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400444 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400445 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400446
Alan Stern36e56a32006-07-01 22:08:06 -0400447 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500448
Alan Stern89842ae2010-05-11 11:44:06 -0400449 /* Claimed interfaces are initially inactive (suspended) and
450 * runtime-PM-enabled, but only if the driver has autosuspend
451 * support. Otherwise they are marked active, to prevent the
452 * device from being autosuspended, but left disabled. In either
453 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500454 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500455 pm_suspend_ignore_children(dev, false);
456 if (driver->supports_autosuspend)
457 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400458 else
459 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400460
461 /* if interface was already added, bind now; else let
462 * the future device_add() bind it, bypassing probe()
463 */
464 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700465 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400466
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700467 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400468}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600469EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400470
471/**
472 * usb_driver_release_interface - unbind a driver from an interface
473 * @driver: the driver to be unbound
474 * @iface: the interface from which it will be unbound
475 *
476 * This can be used by drivers to release an interface without waiting
477 * for their disconnect() methods to be called. In typical cases this
478 * also causes the driver disconnect() method to be called.
479 *
480 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400481 * Callers must own the device lock, so driver disconnect() entries don't
482 * need extra locking, but other call contexts may need to explicitly claim
483 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400484 */
485void usb_driver_release_interface(struct usb_driver *driver,
486 struct usb_interface *iface)
487{
488 struct device *dev = &iface->dev;
489
490 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400491 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400492 return;
493
494 /* don't release from within disconnect() */
495 if (iface->condition != USB_INTERFACE_BOUND)
496 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400497 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400498
Alan Stern91f8d062009-04-16 15:35:09 -0400499 /* Release via the driver core only if the interface
500 * has already been registered
501 */
Alan Stern36e56a32006-07-01 22:08:06 -0400502 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400503 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800504 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800505 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400506 usb_unbind_interface(dev);
507 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800508 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400509 }
Alan Stern36e56a32006-07-01 22:08:06 -0400510}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600511EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400512
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800513/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100514int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800515{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800516 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
517 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
518 return 0;
519
520 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
521 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
522 return 0;
523
524 /* No need to test id->bcdDevice_lo != 0, since 0 is never
525 greater than any unsigned number. */
526 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
527 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
528 return 0;
529
530 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
531 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
532 return 0;
533
534 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
535 (id->bDeviceClass != dev->descriptor.bDeviceClass))
536 return 0;
537
538 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800539 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800540 return 0;
541
542 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
543 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
544 return 0;
545
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100546 return 1;
547}
548
549/* returns 0 if no match, 1 if match */
550int usb_match_one_id(struct usb_interface *interface,
551 const struct usb_device_id *id)
552{
553 struct usb_host_interface *intf;
554 struct usb_device *dev;
555
556 /* proc_connectinfo in devio.c may call us with id == NULL. */
557 if (id == NULL)
558 return 0;
559
560 intf = interface->cur_altsetting;
561 dev = interface_to_usbdev(interface);
562
563 if (!usb_match_device(dev, id))
564 return 0;
565
Alan Stern93c8bf42006-10-18 16:41:51 -0400566 /* The interface class, subclass, and protocol should never be
567 * checked for a match if the device class is Vendor Specific,
568 * unless the match record specifies the Vendor ID. */
569 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
570 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
571 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
572 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
573 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
574 return 0;
575
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800576 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
577 (id->bInterfaceClass != intf->desc.bInterfaceClass))
578 return 0;
579
580 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
581 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
582 return 0;
583
584 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
585 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
586 return 0;
587
588 return 1;
589}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100590EXPORT_SYMBOL_GPL(usb_match_one_id);
591
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800592/**
593 * usb_match_id - find first usb_device_id matching device or interface
594 * @interface: the interface of interest
595 * @id: array of usb_device_id structures, terminated by zero entry
596 *
597 * usb_match_id searches an array of usb_device_id's and returns
598 * the first one matching the device or interface, or null.
599 * This is used when binding (or rebinding) a driver to an interface.
600 * Most USB device drivers will use this indirectly, through the usb core,
601 * but some layered driver frameworks use it directly.
602 * These device tables are exported with MODULE_DEVICE_TABLE, through
603 * modutils, to support the driver loading functionality of USB hotplugging.
604 *
605 * What Matches:
606 *
607 * The "match_flags" element in a usb_device_id controls which
608 * members are used. If the corresponding bit is set, the
609 * value in the device_id must match its corresponding member
610 * in the device or interface descriptor, or else the device_id
611 * does not match.
612 *
613 * "driver_info" is normally used only by device drivers,
614 * but you can create a wildcard "matches anything" usb_device_id
615 * as a driver's "modules.usbmap" entry if you provide an id with
616 * only a nonzero "driver_info" field. If you do this, the USB device
617 * driver's probe() routine should use additional intelligence to
618 * decide whether to bind to the specified interface.
619 *
620 * What Makes Good usb_device_id Tables:
621 *
622 * The match algorithm is very simple, so that intelligence in
623 * driver selection must come from smart driver id records.
624 * Unless you have good reasons to use another selection policy,
625 * provide match elements only in related groups, and order match
626 * specifiers from specific to general. Use the macros provided
627 * for that purpose if you can.
628 *
629 * The most specific match specifiers use device descriptor
630 * data. These are commonly used with product-specific matches;
631 * the USB_DEVICE macro lets you provide vendor and product IDs,
632 * and you can also match against ranges of product revisions.
633 * These are widely used for devices with application or vendor
634 * specific bDeviceClass values.
635 *
636 * Matches based on device class/subclass/protocol specifications
637 * are slightly more general; use the USB_DEVICE_INFO macro, or
638 * its siblings. These are used with single-function devices
639 * where bDeviceClass doesn't specify that each interface has
640 * its own class.
641 *
642 * Matches based on interface class/subclass/protocol are the
643 * most general; they let drivers bind to any interface on a
644 * multiple-function device. Use the USB_INTERFACE_INFO
645 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400646 * devices (as recorded in bInterfaceClass).
647 *
648 * Note that an entry created by USB_INTERFACE_INFO won't match
649 * any interface if the device class is set to Vendor-Specific.
650 * This is deliberate; according to the USB spec the meanings of
651 * the interface class/subclass/protocol for these devices are also
652 * vendor-specific, and hence matching against a standard product
653 * class wouldn't work anyway. If you really want to use an
654 * interface-based match for such a device, create a match record
655 * that also specifies the vendor ID. (Unforunately there isn't a
656 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800657 *
658 * Within those groups, remember that not all combinations are
659 * meaningful. For example, don't give a product version range
660 * without vendor and product IDs; or specify a protocol without
661 * its associated class and subclass.
662 */
663const struct usb_device_id *usb_match_id(struct usb_interface *interface,
664 const struct usb_device_id *id)
665{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800666 /* proc_connectinfo in devio.c may call us with id == NULL. */
667 if (id == NULL)
668 return NULL;
669
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800670 /* It is important to check that id->driver_info is nonzero,
671 since an entry that is all zeroes except for a nonzero
672 id->driver_info is the way to create an entry that
673 indicates that the driver want to examine every
674 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800675 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
676 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800677 if (usb_match_one_id(interface, id))
678 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800679 }
680
681 return NULL;
682}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600683EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800684
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100685static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800686{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400687 /* devices and interfaces are handled separately */
688 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800689
Alan Stern8bb54ab2006-07-01 22:08:49 -0400690 /* interface drivers never match devices */
691 if (!is_usb_device_driver(drv))
692 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800693
Alan Stern8bb54ab2006-07-01 22:08:49 -0400694 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800695 return 1;
696
Kay Sievers55129662009-05-04 19:48:32 +0200697 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400698 struct usb_interface *intf;
699 struct usb_driver *usb_drv;
700 const struct usb_device_id *id;
701
702 /* device drivers never match interfaces */
703 if (is_usb_device_driver(drv))
704 return 0;
705
706 intf = to_usb_interface(dev);
707 usb_drv = to_usb_driver(drv);
708
709 id = usb_match_id(intf, usb_drv->id_table);
710 if (id)
711 return 1;
712
713 id = usb_match_dynamic_id(intf, usb_drv);
714 if (id)
715 return 1;
716 }
717
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800718 return 0;
719}
720
Alan Stern36e56a32006-07-01 22:08:06 -0400721#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200722static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400723{
Alan Stern36e56a32006-07-01 22:08:06 -0400724 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400725
Kay Sievers55129662009-05-04 19:48:32 +0200726 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400727 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200728 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100729 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200730
Alan Stern8bb54ab2006-07-01 22:08:49 -0400731 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200732 } else {
733 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400734 }
Alan Stern36e56a32006-07-01 22:08:06 -0400735
736 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500737 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200738 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400739 return -ENODEV;
740 }
741 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200742 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400743 return -ENODEV;
744 }
745
746#ifdef CONFIG_USB_DEVICEFS
747 /* If this is available, userspace programs can directly read
748 * all the device descriptors we don't tell them about. Or
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100749 * act as usermode drivers.
Alan Stern36e56a32006-07-01 22:08:06 -0400750 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200751 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
Alan Stern36e56a32006-07-01 22:08:06 -0400752 usb_dev->bus->busnum, usb_dev->devnum))
753 return -ENOMEM;
754#endif
755
756 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200757 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400758 le16_to_cpu(usb_dev->descriptor.idVendor),
759 le16_to_cpu(usb_dev->descriptor.idProduct),
760 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
761 return -ENOMEM;
762
763 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200764 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400765 usb_dev->descriptor.bDeviceClass,
766 usb_dev->descriptor.bDeviceSubClass,
767 usb_dev->descriptor.bDeviceProtocol))
768 return -ENOMEM;
769
Alan Stern36e56a32006-07-01 22:08:06 -0400770 return 0;
771}
772
773#else
774
Kay Sievers7eff2e72007-08-14 15:15:12 +0200775static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400776{
777 return -ENODEV;
778}
Alan Stern36e56a32006-07-01 22:08:06 -0400779#endif /* CONFIG_HOTPLUG */
780
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800781/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400782 * usb_register_device_driver - register a USB device (not interface) driver
783 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800784 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800785 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400786 * Registers a USB device driver with the USB core. The list of
787 * unattached devices will be rescanned whenever a new driver is
788 * added, allowing the new driver to attach to any recognized devices.
789 * Returns a negative error code on failure and 0 on success.
790 */
791int usb_register_device_driver(struct usb_device_driver *new_udriver,
792 struct module *owner)
793{
794 int retval = 0;
795
796 if (usb_disabled())
797 return -ENODEV;
798
799 new_udriver->drvwrap.for_devices = 1;
800 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
801 new_udriver->drvwrap.driver.bus = &usb_bus_type;
802 new_udriver->drvwrap.driver.probe = usb_probe_device;
803 new_udriver->drvwrap.driver.remove = usb_unbind_device;
804 new_udriver->drvwrap.driver.owner = owner;
805
806 retval = driver_register(&new_udriver->drvwrap.driver);
807
808 if (!retval) {
809 pr_info("%s: registered new device driver %s\n",
810 usbcore_name, new_udriver->name);
811 usbfs_update_special();
812 } else {
813 printk(KERN_ERR "%s: error %d registering device "
814 " driver %s\n",
815 usbcore_name, retval, new_udriver->name);
816 }
817
818 return retval;
819}
820EXPORT_SYMBOL_GPL(usb_register_device_driver);
821
822/**
823 * usb_deregister_device_driver - unregister a USB device (not interface) driver
824 * @udriver: USB operations of the device driver to unregister
825 * Context: must be able to sleep
826 *
827 * Unlinks the specified driver from the internal USB driver list.
828 */
829void usb_deregister_device_driver(struct usb_device_driver *udriver)
830{
831 pr_info("%s: deregistering device driver %s\n",
832 usbcore_name, udriver->name);
833
834 driver_unregister(&udriver->drvwrap.driver);
835 usbfs_update_special();
836}
837EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
838
839/**
840 * usb_register_driver - register a USB interface driver
841 * @new_driver: USB operations for the interface driver
842 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800843 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400844 *
845 * Registers a USB interface driver with the USB core. The list of
846 * unattached interfaces will be rescanned whenever a new driver is
847 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800848 * Returns a negative error code on failure and 0 on success.
849 *
850 * NOTE: if you want your driver to use the USB major number, you must call
851 * usb_register_dev() to enable that functionality. This function no longer
852 * takes care of that.
853 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800854int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
855 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800856{
857 int retval = 0;
858
859 if (usb_disabled())
860 return -ENODEV;
861
Alan Stern8bb54ab2006-07-01 22:08:49 -0400862 new_driver->drvwrap.for_devices = 0;
863 new_driver->drvwrap.driver.name = (char *) new_driver->name;
864 new_driver->drvwrap.driver.bus = &usb_bus_type;
865 new_driver->drvwrap.driver.probe = usb_probe_interface;
866 new_driver->drvwrap.driver.remove = usb_unbind_interface;
867 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800868 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800869 spin_lock_init(&new_driver->dynids.lock);
870 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800871
Alan Stern8bb54ab2006-07-01 22:08:49 -0400872 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800873 if (retval)
874 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800875
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800876 usbfs_update_special();
877
878 retval = usb_create_newid_file(new_driver);
879 if (retval)
880 goto out_newid;
881
882 retval = usb_create_removeid_file(new_driver);
883 if (retval)
884 goto out_removeid;
885
886 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800887 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800888
889out:
890 return retval;
891
892out_removeid:
893 usb_remove_newid_file(new_driver);
894out_newid:
895 driver_unregister(&new_driver->drvwrap.driver);
896
897 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400898 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800899 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800900 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800901}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600902EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800903
904/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400905 * usb_deregister - unregister a USB interface driver
906 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800907 * Context: must be able to sleep
908 *
909 * Unlinks the specified driver from the internal USB driver list.
910 *
911 * NOTE: If you called usb_register_dev(), you still need to call
912 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
913 * this * call will no longer do it for you.
914 */
915void usb_deregister(struct usb_driver *driver)
916{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400917 pr_info("%s: deregistering interface driver %s\n",
918 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800919
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800920 usb_remove_removeid_file(driver);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800921 usb_remove_newid_file(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800922 usb_free_dynids(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400923 driver_unregister(&driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800924
925 usbfs_update_special();
926}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600927EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400928
Alan Stern78d9a482008-06-23 16:00:40 -0400929/* Forced unbinding of a USB interface driver, either because
930 * it doesn't support pre_reset/post_reset/reset_resume or
931 * because it doesn't support suspend/resume.
932 *
933 * The caller must hold @intf's device's lock, but not its pm_mutex
934 * and not @intf->dev.sem.
935 */
936void usb_forced_unbind_intf(struct usb_interface *intf)
937{
938 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
939
940 dev_dbg(&intf->dev, "forced unbind\n");
941 usb_driver_release_interface(driver, intf);
942
943 /* Mark the interface for later rebinding */
944 intf->needs_binding = 1;
945}
946
947/* Delayed forced unbinding of a USB interface driver and scan
948 * for rebinding.
949 *
950 * The caller must hold @intf's device's lock, but not its pm_mutex
951 * and not @intf->dev.sem.
952 *
Alan Stern5096aed2008-08-12 14:34:14 -0400953 * Note: Rebinds will be skipped if a system sleep transition is in
954 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -0400955 */
956void usb_rebind_intf(struct usb_interface *intf)
957{
958 int rc;
959
960 /* Delayed unbind of an existing driver */
961 if (intf->dev.driver) {
962 struct usb_driver *driver =
963 to_usb_driver(intf->dev.driver);
964
965 dev_dbg(&intf->dev, "forced unbind\n");
966 usb_driver_release_interface(driver, intf);
967 }
968
969 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +0200970 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -0400971 intf->needs_binding = 0;
972 rc = device_attach(&intf->dev);
973 if (rc < 0)
974 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
975 }
Alan Stern78d9a482008-06-23 16:00:40 -0400976}
977
Alan Stern9ff78432008-08-07 13:04:51 -0400978#ifdef CONFIG_PM
979
Alan Stern78d9a482008-06-23 16:00:40 -0400980#define DO_UNBIND 0
981#define DO_REBIND 1
982
983/* Unbind drivers for @udev's interfaces that don't support suspend/resume,
984 * or rebind interfaces that have been unbound, according to @action.
985 *
986 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -0400987 */
988static void do_unbind_rebind(struct usb_device *udev, int action)
989{
990 struct usb_host_config *config;
991 int i;
992 struct usb_interface *intf;
993 struct usb_driver *drv;
994
995 config = udev->actconfig;
996 if (config) {
997 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
998 intf = config->interface[i];
999 switch (action) {
1000 case DO_UNBIND:
1001 if (intf->dev.driver) {
1002 drv = to_usb_driver(intf->dev.driver);
1003 if (!drv->suspend || !drv->resume)
1004 usb_forced_unbind_intf(intf);
1005 }
1006 break;
1007 case DO_REBIND:
Alan Stern5096aed2008-08-12 14:34:14 -04001008 if (intf->needs_binding)
Alan Stern78d9a482008-06-23 16:00:40 -04001009 usb_rebind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001010 break;
1011 }
1012 }
1013 }
1014}
1015
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001016static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001017{
Alan Stern782da722006-07-01 22:09:35 -04001018 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001019 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001020
Alan Stern114b3682006-07-01 22:13:04 -04001021 if (udev->state == USB_STATE_NOTATTACHED ||
1022 udev->state == USB_STATE_SUSPENDED)
1023 goto done;
1024
Alan Sternb6f64362007-05-04 11:51:54 -04001025 /* For devices that don't have a driver, we do a generic suspend. */
1026 if (udev->dev.driver)
1027 udriver = to_usb_device_driver(udev->dev.driver);
1028 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001029 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001030 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001031 }
Alan Stern2bf40862006-07-01 22:12:19 -04001032 status = udriver->suspend(udev, msg);
1033
Alan Stern20dfdad2007-05-22 11:50:17 -04001034 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001035 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001036 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001037}
Alan Stern36e56a32006-07-01 22:08:06 -04001038
Alan Stern65bfd292008-11-25 16:39:18 -05001039static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001040{
1041 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001042 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001043
Alan Stern0458d5b2007-05-04 11:52:20 -04001044 if (udev->state == USB_STATE_NOTATTACHED)
1045 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001046
Alan Stern1c5df7e2006-07-01 22:13:50 -04001047 /* Can't resume it if it doesn't have a driver. */
1048 if (udev->dev.driver == NULL) {
1049 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001050 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001051 }
1052
Alan Stern6d19c002010-02-12 12:21:11 +01001053 /* Non-root devices on a full/low-speed bus must wait for their
1054 * companion high-speed root hub, in case a handoff is needed.
1055 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001056 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001057 device_pm_wait_for_dev(&udev->dev,
1058 &udev->bus->hs_companion->root_hub->dev);
1059
Alan Stern6bc6cff2007-05-04 11:53:03 -04001060 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1061 udev->reset_resume = 1;
1062
Alan Stern1cc8a252006-07-01 22:10:15 -04001063 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001064 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001065
Alan Stern20dfdad2007-05-22 11:50:17 -04001066 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001067 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001068 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001069}
1070
Alan Stern65605ae2008-08-12 14:33:27 -04001071static int usb_suspend_interface(struct usb_device *udev,
1072 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001073{
1074 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001075 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001076
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001077 if (udev->state == USB_STATE_NOTATTACHED ||
1078 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001079 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001080 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001081
Oliver Neukume78832c2012-01-02 15:11:48 +01001082 /* at this time we know the driver supports suspend */
1083 status = driver->suspend(intf, msg);
1084 if (status && !PMSG_IS_AUTO(msg))
1085 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001086
Alan Stern20dfdad2007-05-22 11:50:17 -04001087 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001088 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001089 return status;
1090}
1091
Alan Stern65605ae2008-08-12 14:33:27 -04001092static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001093 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001094{
Alan Stern1cc8a252006-07-01 22:10:15 -04001095 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001096 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001097
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001098 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001099 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001100
Alan Stern645daaa2006-08-30 15:47:02 -04001101 /* Don't let autoresume interfere with unbinding */
1102 if (intf->condition == USB_INTERFACE_UNBINDING)
1103 goto done;
1104
Alan Stern1c5df7e2006-07-01 22:13:50 -04001105 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001106 if (intf->condition == USB_INTERFACE_UNBOUND) {
1107
1108 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001109 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001110 usb_set_interface(udev, intf->altsetting[0].
1111 desc.bInterfaceNumber, 0);
1112 intf->needs_altsetting0 = 0;
1113 }
Alan Stern2bf40862006-07-01 22:12:19 -04001114 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001115 }
Alan Stern78d9a482008-06-23 16:00:40 -04001116
1117 /* Don't resume if the interface is marked for rebinding */
1118 if (intf->needs_binding)
1119 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001120 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001121
Alan Sternf07600c2007-05-30 15:38:16 -04001122 if (reset_resume) {
1123 if (driver->reset_resume) {
1124 status = driver->reset_resume(intf);
1125 if (status)
1126 dev_err(&intf->dev, "%s error %d\n",
1127 "reset_resume", status);
1128 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001129 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001130 dev_warn(&intf->dev, "no %s for driver %s?\n",
1131 "reset_resume", driver->name);
1132 }
1133 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001134 status = driver->resume(intf);
1135 if (status)
1136 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001137 }
Alan Stern2bf40862006-07-01 22:12:19 -04001138
1139done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001140 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001141
Alan Stern78d9a482008-06-23 16:00:40 -04001142 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001143 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001144}
1145
Alan Stern645daaa2006-08-30 15:47:02 -04001146/**
1147 * usb_suspend_both - suspend a USB device and its interfaces
1148 * @udev: the usb_device to suspend
1149 * @msg: Power Management message describing this state transition
1150 *
1151 * This is the central routine for suspending USB devices. It calls the
1152 * suspend methods for all the interface drivers in @udev and then calls
1153 * the suspend method for @udev itself. If an error occurs at any stage,
1154 * all the interfaces which were suspended are resumed so that they remain
1155 * in the same state as the device.
1156 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001157 * Autosuspend requests originating from a child device or an interface
1158 * driver may be made without the protection of @udev's device lock, but
1159 * all other suspend calls will hold the lock. Usbcore will insure that
1160 * method calls do not arrive during bind, unbind, or reset operations.
1161 * However drivers must be prepared to handle suspend calls arriving at
1162 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001163 *
1164 * This routine can run only in process context.
1165 */
Alan Stern718efa62007-03-09 15:41:13 -05001166static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001167{
1168 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001169 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001170 struct usb_interface *intf;
1171
Alan Stern19410442007-03-27 13:33:59 -04001172 if (udev->state == USB_STATE_NOTATTACHED ||
1173 udev->state == USB_STATE_SUSPENDED)
1174 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001175
Alan Stern645daaa2006-08-30 15:47:02 -04001176 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001177 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001178 n = udev->actconfig->desc.bNumInterfaces;
1179 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001180 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001181 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001182
1183 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001184 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001185 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001186 if (status != 0)
1187 break;
1188 }
1189 }
Alan Stern0af212b2011-06-15 16:27:43 -04001190 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001191 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001192
Alan Stern0af212b2011-06-15 16:27:43 -04001193 /* Again, ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001194 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001195 status = 0;
1196 }
1197
Alan Sterna8e7c562006-07-01 22:11:02 -04001198 /* If the suspend failed, resume interfaces that did get suspended */
1199 if (status != 0) {
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001200 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
Alan Stern571dc792010-04-09 16:03:43 -04001201 while (++i < n) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001202 intf = udev->actconfig->interface[i];
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001203 usb_resume_interface(udev, intf, msg, 0);
Alan Sterna8e7c562006-07-01 22:11:02 -04001204 }
Alan Stern645daaa2006-08-30 15:47:02 -04001205
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001206 /* If the suspend succeeded then prevent any more URB submissions
1207 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001208 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001209 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001210 udev->can_submit = 0;
1211 for (i = 0; i < 16; ++i) {
1212 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1213 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1214 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001215 }
Alan Stern645daaa2006-08-30 15:47:02 -04001216
Alan Stern19410442007-03-27 13:33:59 -04001217 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001218 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001219 return status;
1220}
1221
Alan Stern645daaa2006-08-30 15:47:02 -04001222/**
1223 * usb_resume_both - resume a USB device and its interfaces
1224 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001225 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001226 *
1227 * This is the central routine for resuming USB devices. It calls the
1228 * the resume method for @udev and then calls the resume methods for all
1229 * the interface drivers in @udev.
1230 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001231 * Autoresume requests originating from a child device or an interface
1232 * driver may be made without the protection of @udev's device lock, but
1233 * all other resume calls will hold the lock. Usbcore will insure that
1234 * method calls do not arrive during bind, unbind, or reset operations.
1235 * However drivers must be prepared to handle resume calls arriving at
1236 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001237 *
1238 * This routine can run only in process context.
1239 */
Alan Stern65bfd292008-11-25 16:39:18 -05001240static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001241{
Alan Stern645daaa2006-08-30 15:47:02 -04001242 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001243 int i;
1244 struct usb_interface *intf;
1245
Alan Stern19410442007-03-27 13:33:59 -04001246 if (udev->state == USB_STATE_NOTATTACHED) {
1247 status = -ENODEV;
1248 goto done;
1249 }
Alan Stern6840d252007-09-10 11:34:26 -04001250 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001251
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001252 /* Resume the device */
1253 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001254 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001255
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001256 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001257 if (status == 0 && udev->actconfig) {
1258 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1259 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001260 usb_resume_interface(udev, intf, msg,
1261 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001262 }
1263 }
Alan Sternc08512c2010-11-15 15:57:58 -05001264 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001265
Alan Stern19410442007-03-27 13:33:59 -04001266 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001267 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001268 if (!status)
1269 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001270 return status;
1271}
1272
Alan Stern5f677f12010-04-02 13:20:11 -04001273static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1274{
Alan Stern48826622010-06-22 16:14:48 -04001275 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001276
1277 /* Remote wakeup is needed only when we actually go to sleep.
1278 * For things like FREEZE and QUIESCE, if the device is already
1279 * autosuspended then its current wakeup setting is okay.
1280 */
1281 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1282 if (udev->state != USB_STATE_SUSPENDED)
1283 udev->do_remote_wakeup = 0;
1284 return;
1285 }
1286
Alan Stern48826622010-06-22 16:14:48 -04001287 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001288 * actually want it.
1289 */
Alan Stern48826622010-06-22 16:14:48 -04001290 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001291
1292 /* If the device is autosuspended with the wrong wakeup setting,
1293 * autoresume now so the setting can be changed.
1294 */
1295 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1296 pm_runtime_resume(&udev->dev);
1297 udev->do_remote_wakeup = w;
1298}
1299
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001300/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001301int usb_suspend(struct device *dev, pm_message_t msg)
1302{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001303 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001304
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001305 do_unbind_rebind(udev, DO_UNBIND);
Alan Stern5f677f12010-04-02 13:20:11 -04001306 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001307 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001308}
1309
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001310/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001311int usb_resume(struct device *dev, pm_message_t msg)
1312{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001313 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001314 int status;
1315
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001316 /* For PM complete calls, all we do is rebind interfaces */
1317 if (msg.event == PM_EVENT_ON) {
1318 if (udev->state != USB_STATE_NOTATTACHED)
1319 do_unbind_rebind(udev, DO_REBIND);
1320 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001321
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001322 /* For all other calls, take the device back to full power and
1323 * tell the PM core in case it was autosuspended previously.
Alan Sternc043f122010-06-04 14:02:42 -04001324 * Unbind the interfaces that will need rebinding later.
Alan Stern0c590e22010-01-08 12:57:14 -05001325 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001326 } else {
1327 status = usb_resume_both(udev, msg);
1328 if (status == 0) {
1329 pm_runtime_disable(dev);
1330 pm_runtime_set_active(dev);
1331 pm_runtime_enable(dev);
Alan Sternc043f122010-06-04 14:02:42 -04001332 do_unbind_rebind(udev, DO_REBIND);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001333 }
1334 }
Alan Stern0c590e22010-01-08 12:57:14 -05001335
1336 /* Avoid PM error messages for devices disconnected while suspended
1337 * as we'll display regular disconnect messages just a bit later.
1338 */
Peter Chen7491f132010-09-27 16:43:25 +08001339 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001340 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001341 return status;
1342}
1343
1344#endif /* CONFIG_PM */
1345
Alan Stern645daaa2006-08-30 15:47:02 -04001346#ifdef CONFIG_USB_SUSPEND
1347
Alan Stern088f7fe2010-01-08 12:56:54 -05001348/**
1349 * usb_enable_autosuspend - allow a USB device to be autosuspended
1350 * @udev: the USB device which may be autosuspended
1351 *
1352 * This routine allows @udev to be autosuspended. An autosuspend won't
1353 * take place until the autosuspend_delay has elapsed and all the other
1354 * necessary conditions are satisfied.
1355 *
1356 * The caller must hold @udev's device lock.
1357 */
Alan Stern9e18c822010-04-02 13:22:09 -04001358void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001359{
Alan Stern9e18c822010-04-02 13:22:09 -04001360 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001361}
1362EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1363
1364/**
1365 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1366 * @udev: the USB device which may not be autosuspended
1367 *
1368 * This routine prevents @udev from being autosuspended and wakes it up
1369 * if it is already autosuspended.
1370 *
1371 * The caller must hold @udev's device lock.
1372 */
Alan Stern9e18c822010-04-02 13:22:09 -04001373void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001374{
Alan Stern9e18c822010-04-02 13:22:09 -04001375 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001376}
1377EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
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 Stern9bbdf1e2010-01-08 12:57:28 -05001387 * @udev's usage counter is decremented; if it drops to 0 and all the
1388 * interfaces are inactive then a delayed autosuspend will be attempted.
1389 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001390 *
Alan Stern62e299e2010-01-08 12:56:19 -05001391 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001392 *
1393 * This routine can run only in process context.
1394 */
Alan Stern94fcda12006-11-20 11:38:46 -05001395void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001396{
Alan Stern94fcda12006-11-20 11:38:46 -05001397 int status;
1398
Ming Lei6ddf27c2010-11-15 15:57:30 -05001399 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001400 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001401 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1402 __func__, atomic_read(&udev->dev.power.usage_count),
1403 status);
Alan Stern19c26232007-02-20 15:03:32 -05001404}
1405
1406/**
Alan Stern645daaa2006-08-30 15:47:02 -04001407 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001408 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001409 *
1410 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001411 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001412 * occur until usb_autosuspend_device() is called. (Note that this will
1413 * not prevent suspend events originating in the PM core.) Examples would
1414 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001415 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001416 *
Alan Stern94fcda12006-11-20 11:38:46 -05001417 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1418 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001419 *
Alan Stern62e299e2010-01-08 12:56:19 -05001420 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001421 *
1422 * This routine can run only in process context.
1423 */
Alan Stern94fcda12006-11-20 11:38:46 -05001424int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001425{
1426 int status;
1427
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001428 status = pm_runtime_get_sync(&udev->dev);
1429 if (status < 0)
1430 pm_runtime_put_sync(&udev->dev);
1431 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1432 __func__, atomic_read(&udev->dev.power.usage_count),
1433 status);
1434 if (status > 0)
1435 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001436 return status;
1437}
1438
Alan Stern645daaa2006-08-30 15:47:02 -04001439/**
1440 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001441 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001442 *
1443 * This routine should be called by an interface driver when it is
1444 * finished using @intf and wants to allow it to autosuspend. A typical
1445 * example would be a character-device driver when its device file is
1446 * closed.
1447 *
1448 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001449 * 0, a delayed autosuspend request for @intf's device is attempted. The
1450 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001451 *
Alan Stern645daaa2006-08-30 15:47:02 -04001452 * This routine can run only in process context.
1453 */
1454void usb_autopm_put_interface(struct usb_interface *intf)
1455{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001456 struct usb_device *udev = interface_to_usbdev(intf);
1457 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001458
Ming Lei6ddf27c2010-11-15 15:57:30 -05001459 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001460 atomic_dec(&intf->pm_usage_cnt);
1461 status = pm_runtime_put_sync(&intf->dev);
1462 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1463 __func__, atomic_read(&intf->dev.power.usage_count),
1464 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001465}
1466EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1467
1468/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001469 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1470 * @intf: the usb_interface whose counter should be decremented
1471 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001472 * This routine does much the same thing as usb_autopm_put_interface():
1473 * It decrements @intf's usage counter and schedules a delayed
1474 * autosuspend request if the counter is <= 0. The difference is that it
1475 * does not perform any synchronization; callers should hold a private
1476 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001477 *
1478 * Typically a driver would call this routine during an URB's completion
1479 * handler, if no more URBs were pending.
1480 *
1481 * This routine can run in atomic context.
1482 */
1483void usb_autopm_put_interface_async(struct usb_interface *intf)
1484{
1485 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001486 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001487
Ming Lei6ddf27c2010-11-15 15:57:30 -05001488 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001489 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001490 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001491 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1492 __func__, atomic_read(&intf->dev.power.usage_count),
1493 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001494}
1495EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1496
1497/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001498 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1499 * @intf: the usb_interface whose counter should be decremented
1500 *
1501 * This routine decrements @intf's usage counter but does not carry out an
1502 * autosuspend.
1503 *
1504 * This routine can run in atomic context.
1505 */
1506void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1507{
1508 struct usb_device *udev = interface_to_usbdev(intf);
1509
Ming Lei6ddf27c2010-11-15 15:57:30 -05001510 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001511 atomic_dec(&intf->pm_usage_cnt);
1512 pm_runtime_put_noidle(&intf->dev);
1513}
1514EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1515
1516/**
Alan Stern645daaa2006-08-30 15:47:02 -04001517 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001518 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001519 *
1520 * This routine should be called by an interface driver when it wants to
1521 * use @intf and needs to guarantee that it is not suspended. In addition,
1522 * the routine prevents @intf from being autosuspended subsequently. (Note
1523 * that this will not prevent suspend events originating in the PM core.)
1524 * This prevention will persist until usb_autopm_put_interface() is called
1525 * or @intf is unbound. A typical example would be a character-device
1526 * driver when its device file is opened.
1527 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001528 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1529 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001530 *
1531 * This routine can run only in process context.
1532 */
1533int usb_autopm_get_interface(struct usb_interface *intf)
1534{
Alan Sternaf4f7602006-10-30 17:06:45 -05001535 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001536
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001537 status = pm_runtime_get_sync(&intf->dev);
1538 if (status < 0)
1539 pm_runtime_put_sync(&intf->dev);
1540 else
1541 atomic_inc(&intf->pm_usage_cnt);
1542 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1543 __func__, atomic_read(&intf->dev.power.usage_count),
1544 status);
1545 if (status > 0)
1546 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001547 return status;
1548}
1549EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1550
Alan Stern692a1862006-10-30 17:07:51 -05001551/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001552 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1553 * @intf: the usb_interface whose counter should be incremented
1554 *
1555 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001556 * usb_autopm_get_interface(): It increments @intf's usage counter and
1557 * queues an autoresume request if the device is suspended. The
1558 * differences are that it does not perform any synchronization (callers
1559 * should hold a private lock and handle all synchronization issues
1560 * themselves), and it does not autoresume the device directly (it only
1561 * queues a request). After a successful call, the device may not yet be
1562 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001563 *
1564 * This routine can run in atomic context.
1565 */
1566int usb_autopm_get_interface_async(struct usb_interface *intf)
1567{
Ming Lei63defa72010-11-15 15:56:54 -05001568 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001569
Ming Lei63defa72010-11-15 15:56:54 -05001570 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001571 if (status < 0 && status != -EINPROGRESS)
1572 pm_runtime_put_noidle(&intf->dev);
1573 else
Alan Sternccf5b802009-06-29 11:00:01 -04001574 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001575 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1576 __func__, atomic_read(&intf->dev.power.usage_count),
1577 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001578 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001579 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001580 return status;
1581}
1582EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1583
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001584/**
1585 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1586 * @intf: the usb_interface whose counter should be incremented
1587 *
1588 * This routine increments @intf's usage counter but does not carry out an
1589 * autoresume.
1590 *
1591 * This routine can run in atomic context.
1592 */
1593void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1594{
1595 struct usb_device *udev = interface_to_usbdev(intf);
1596
Ming Lei6ddf27c2010-11-15 15:57:30 -05001597 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001598 atomic_inc(&intf->pm_usage_cnt);
1599 pm_runtime_get_noresume(&intf->dev);
1600}
1601EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1602
1603/* Internal routine to check whether we may autosuspend a device. */
1604static int autosuspend_check(struct usb_device *udev)
1605{
Alan Stern7560d32e2010-04-02 13:18:50 -04001606 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001607 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001608
1609 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1610 * any interface drivers require remote wakeup but it isn't available.
1611 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001612 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001613 if (udev->actconfig) {
1614 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1615 intf = udev->actconfig->interface[i];
1616
1617 /* We don't need to check interfaces that are
1618 * disabled for runtime PM. Either they are unbound
1619 * or else their drivers don't support autosuspend
1620 * and so they are permanently active.
1621 */
1622 if (intf->dev.power.disable_depth)
1623 continue;
1624 if (atomic_read(&intf->dev.power.usage_count) > 0)
1625 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001626 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001627
1628 /* Don't allow autosuspend if the device will need
1629 * a reset-resume and any of its interface drivers
1630 * doesn't include support or needs remote wakeup.
1631 */
1632 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1633 struct usb_driver *driver;
1634
1635 driver = to_usb_driver(intf->dev.driver);
1636 if (!driver->reset_resume ||
1637 intf->needs_remote_wakeup)
1638 return -EOPNOTSUPP;
1639 }
1640 }
1641 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001642 if (w && !device_can_wakeup(&udev->dev)) {
1643 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1644 return -EOPNOTSUPP;
1645 }
1646 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001647 return 0;
1648}
1649
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001650int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001651{
Ming Lei63defa72010-11-15 15:56:54 -05001652 struct usb_device *udev = to_usb_device(dev);
1653 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001654
1655 /* A USB device can be suspended if it passes the various autosuspend
1656 * checks. Runtime suspend for a USB device means suspending all the
1657 * interfaces and then the device itself.
1658 */
Ming Lei63defa72010-11-15 15:56:54 -05001659 if (autosuspend_check(udev) != 0)
1660 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001661
Ming Lei63defa72010-11-15 15:56:54 -05001662 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001663
1664 /* Allow a retry if autosuspend failed temporarily */
1665 if (status == -EAGAIN || status == -EBUSY)
1666 usb_mark_last_busy(udev);
1667
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001668 /* The PM core reacts badly unless the return code is 0,
1669 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1670 */
1671 if (status != 0)
1672 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001673 return status;
1674}
1675
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001676int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001677{
Ming Lei63defa72010-11-15 15:56:54 -05001678 struct usb_device *udev = to_usb_device(dev);
1679 int status;
1680
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001681 /* Runtime resume for a USB device means resuming both the device
1682 * and all its interfaces.
1683 */
Ming Lei63defa72010-11-15 15:56:54 -05001684 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001685 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001686}
1687
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001688int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001689{
Ming Lei63defa72010-11-15 15:56:54 -05001690 struct usb_device *udev = to_usb_device(dev);
1691
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001692 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001693 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001694 */
Ming Lei63defa72010-11-15 15:56:54 -05001695 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001696 pm_runtime_autosuspend(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001697 return 0;
1698}
1699
Andiry Xu65580b432011-09-23 14:19:52 -07001700int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1701{
1702 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1703 int ret = -EPERM;
1704
1705 if (hcd->driver->set_usb2_hw_lpm) {
1706 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1707 if (!ret)
1708 udev->usb2_hw_lpm_enabled = enable;
1709 }
1710
1711 return ret;
1712}
1713
Alan Stern645daaa2006-08-30 15:47:02 -04001714#endif /* CONFIG_USB_SUSPEND */
1715
Alan Stern36e56a32006-07-01 22:08:06 -04001716struct bus_type usb_bus_type = {
1717 .name = "usb",
1718 .match = usb_device_match,
1719 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001720};