blob: f8e2d6d52e5c4bf645c3679807456d8a4303ec9d [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
Alan Sterncef9bc52012-01-24 13:34:41 -050074 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080075
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070076 if (retval)
77 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080078 return count;
79}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010080EXPORT_SYMBOL_GPL(usb_store_new_id);
81
82static ssize_t store_new_id(struct device_driver *driver,
83 const char *buf, size_t count)
84{
85 struct usb_driver *usb_drv = to_usb_driver(driver);
86
87 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
88}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080089static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
90
CHENG Renquan0c7a2b72009-11-22 01:28:52 +080091/**
92 * store_remove_id - remove a USB device ID from this driver
93 * @driver: target device driver
94 * @buf: buffer for scanning device ID data
95 * @count: input size
96 *
97 * Removes a dynamic usb device ID from this driver.
98 */
99static ssize_t
100store_remove_id(struct device_driver *driver, const char *buf, size_t count)
101{
102 struct usb_dynid *dynid, *n;
103 struct usb_driver *usb_driver = to_usb_driver(driver);
104 u32 idVendor = 0;
105 u32 idProduct = 0;
106 int fields = 0;
107 int retval = 0;
108
109 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
110 if (fields < 2)
111 return -EINVAL;
112
113 spin_lock(&usb_driver->dynids.lock);
114 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
115 struct usb_device_id *id = &dynid->id;
116 if ((id->idVendor == idVendor) &&
117 (id->idProduct == idProduct)) {
118 list_del(&dynid->node);
119 kfree(dynid);
120 retval = 0;
121 break;
122 }
123 }
124 spin_unlock(&usb_driver->dynids.lock);
125
126 if (retval)
127 return retval;
128 return count;
129}
130static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
131
Alan Sterned283e92012-01-24 14:35:13 -0500132static int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800133{
134 int error = 0;
135
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800136 if (usb_drv->no_dynamic_id)
137 goto exit;
138
Alan Sterned283e92012-01-24 14:35:13 -0500139 if (usb_drv->probe != NULL) {
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800140 error = driver_create_file(&usb_drv->drvwrap.driver,
141 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500142 if (error == 0) {
143 error = driver_create_file(&usb_drv->drvwrap.driver,
144 &driver_attr_remove_id);
145 if (error)
146 driver_remove_file(&usb_drv->drvwrap.driver,
147 &driver_attr_new_id);
148 }
149 }
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800150exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800151 return error;
152}
153
Alan Sterned283e92012-01-24 14:35:13 -0500154static void usb_remove_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800155{
156 if (usb_drv->no_dynamic_id)
157 return;
158
Alan Sterned283e92012-01-24 14:35:13 -0500159 if (usb_drv->probe != NULL) {
160 driver_remove_file(&usb_drv->drvwrap.driver,
161 &driver_attr_remove_id);
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800162 driver_remove_file(&usb_drv->drvwrap.driver,
163 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500164 }
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800165}
166
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800167static void usb_free_dynids(struct usb_driver *usb_drv)
168{
169 struct usb_dynid *dynid, *n;
170
171 spin_lock(&usb_drv->dynids.lock);
172 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
173 list_del(&dynid->node);
174 kfree(dynid);
175 }
176 spin_unlock(&usb_drv->dynids.lock);
177}
178#else
Alan Sterned283e92012-01-24 14:35:13 -0500179static inline int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800180{
181 return 0;
182}
183
Alan Sterned283e92012-01-24 14:35:13 -0500184static void usb_remove_newid_files(struct usb_driver *usb_drv)
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800185{
186}
187
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800188static inline void usb_free_dynids(struct usb_driver *usb_drv)
189{
190}
191#endif
192
193static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
194 struct usb_driver *drv)
195{
196 struct usb_dynid *dynid;
197
198 spin_lock(&drv->dynids.lock);
199 list_for_each_entry(dynid, &drv->dynids.list, node) {
200 if (usb_match_one_id(intf, &dynid->id)) {
201 spin_unlock(&drv->dynids.lock);
202 return &dynid->id;
203 }
204 }
205 spin_unlock(&drv->dynids.lock);
206 return NULL;
207}
208
209
Alan Stern8bb54ab2006-07-01 22:08:49 -0400210/* called from driver core with dev locked */
211static int usb_probe_device(struct device *dev)
212{
213 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200214 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500215 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400216
Harvey Harrison441b62c2008-03-03 16:08:34 -0800217 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400218
Alan Stern8bb54ab2006-07-01 22:08:49 -0400219 /* TODO: Add real matching code */
220
Alan Stern645daaa2006-08-30 15:47:02 -0400221 /* The device should always appear to be in use
222 * unless the driver suports autosuspend.
223 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500224 if (!udriver->supports_autosuspend)
225 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400226
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500227 if (!error)
228 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400229 return error;
230}
231
232/* called from driver core with dev locked */
233static int usb_unbind_device(struct device *dev)
234{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500235 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400236 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
237
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500238 udriver->disconnect(udev);
239 if (!udriver->supports_autosuspend)
240 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400241 return 0;
242}
243
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800244/*
245 * Cancel any pending scheduled resets
246 *
247 * [see usb_queue_reset_device()]
248 *
249 * Called after unconfiguring / when releasing interfaces. See
250 * comments in __usb_queue_reset_device() regarding
251 * udev->reset_running.
252 */
253static void usb_cancel_queued_reset(struct usb_interface *iface)
254{
255 if (iface->reset_running == 0)
256 cancel_work_sync(&iface->reset_ws);
257}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400258
259/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800260static int usb_probe_interface(struct device *dev)
261{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400262 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200263 struct usb_interface *intf = to_usb_interface(dev);
264 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800265 const struct usb_device_id *id;
266 int error = -ENODEV;
267
Harvey Harrison441b62c2008-03-03 16:08:34 -0800268 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800269
Alan Stern78d9a482008-06-23 16:00:40 -0400270 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800271
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400272 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500273 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400274
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800275 if (udev->authorized == 0) {
276 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500277 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800278 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700279
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800280 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800281 if (!id)
282 id = usb_match_dynamic_id(intf, driver);
Alan Stern0f3dda92010-01-08 12:56:04 -0500283 if (!id)
284 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800285
Alan Stern0f3dda92010-01-08 12:56:04 -0500286 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400287
Alan Stern0f3dda92010-01-08 12:56:04 -0500288 error = usb_autoresume_device(udev);
289 if (error)
290 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400291
Alan Stern0f3dda92010-01-08 12:56:04 -0500292 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400293
Alan Stern571dc792010-04-09 16:03:43 -0400294 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500295 * runtime-PM-enabled only if the driver has autosuspend support.
296 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500297 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500298 pm_runtime_set_active(dev);
299 pm_suspend_ignore_children(dev, false);
300 if (driver->supports_autosuspend)
301 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200302
Alan Stern0f3dda92010-01-08 12:56:04 -0500303 /* Carry out a deferred switch to altsetting 0 */
304 if (intf->needs_altsetting0) {
305 error = usb_set_interface(udev, intf->altsetting[0].
306 desc.bInterfaceNumber, 0);
307 if (error < 0)
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200308 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500309 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800310 }
311
Alan Stern0f3dda92010-01-08 12:56:04 -0500312 error = driver->probe(intf, id);
313 if (error)
314 goto err;
315
316 intf->condition = USB_INTERFACE_BOUND;
317 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800318 return error;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200319
Alan Stern0f3dda92010-01-08 12:56:04 -0500320 err:
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200321 intf->needs_remote_wakeup = 0;
322 intf->condition = USB_INTERFACE_UNBOUND;
323 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500324
325 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400326 if (driver->supports_autosuspend)
327 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500328 pm_runtime_set_suspended(dev);
329
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200330 usb_autosuspend_device(udev);
331 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800332}
333
Alan Stern8bb54ab2006-07-01 22:08:49 -0400334/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800335static int usb_unbind_interface(struct device *dev)
336{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400337 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800338 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400339 struct usb_device *udev;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200340 int error, r;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800341
342 intf->condition = USB_INTERFACE_UNBINDING;
343
Alan Stern645daaa2006-08-30 15:47:02 -0400344 /* Autoresume for set_interface call below */
345 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500346 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400347
Alan Stern9da82bd2008-05-08 11:54:37 -0400348 /* Terminate all URBs for this interface unless the driver
349 * supports "soft" unbinding.
350 */
351 if (!driver->soft_unbind)
Alan Sternddeac4e72009-01-15 17:03:33 -0500352 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800353
Alan Stern8bb54ab2006-07-01 22:08:49 -0400354 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800355 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800356
Alan Stern55151d72008-08-12 14:33:59 -0400357 /* Reset other interface state.
358 * We cannot do a Set-Interface if the device is suspended or
359 * if it is prepared for a system sleep (since installing a new
360 * altsetting means creating new endpoint device entries).
361 * When either of these happens, defer the Set-Interface.
362 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500363 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
364 /* Already in altsetting 0 so skip Set-Interface.
365 * Just re-enable it without affecting the endpoint toggles.
366 */
367 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200368 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200369 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400370 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200371 if (r < 0)
372 intf->needs_altsetting0 = 1;
373 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400374 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200375 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800376 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400377
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800378 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400379 intf->needs_remote_wakeup = 0;
380
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500381 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400382 if (driver->supports_autosuspend)
383 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500384 pm_runtime_set_suspended(dev);
385
386 /* Undo any residual pm_autopm_get_interface_* calls */
387 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
388 usb_autopm_put_interface_no_suspend(intf);
389 atomic_set(&intf->pm_usage_cnt, 0);
390
Alan Stern645daaa2006-08-30 15:47:02 -0400391 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500392 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800393
394 return 0;
395}
396
Alan Stern36e56a32006-07-01 22:08:06 -0400397/**
398 * usb_driver_claim_interface - bind a driver to an interface
399 * @driver: the driver to be bound
400 * @iface: the interface to which it will be bound; must be in the
401 * usb device's active configuration
402 * @priv: driver data associated with that interface
403 *
404 * This is used by usb device drivers that need to claim more than one
405 * interface on a device when probing (audio and acm are current examples).
406 * No device driver should directly modify internal usb_interface or
407 * usb_device structure members.
408 *
409 * Few drivers should need to use this routine, since the most natural
410 * way to bind to an interface is to return the private data from
411 * the driver's probe() method.
412 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400413 * Callers must own the device lock, so driver probe() entries don't need
414 * extra locking, but other call contexts may need to explicitly claim that
415 * lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400416 */
417int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800418 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400419{
420 struct device *dev = &iface->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700421 int retval = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400422
423 if (dev->driver)
424 return -EBUSY;
425
Alan Stern8bb54ab2006-07-01 22:08:49 -0400426 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400427 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400428 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400429
Alan Stern36e56a32006-07-01 22:08:06 -0400430 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500431
Alan Stern89842ae2010-05-11 11:44:06 -0400432 /* Claimed interfaces are initially inactive (suspended) and
433 * runtime-PM-enabled, but only if the driver has autosuspend
434 * support. Otherwise they are marked active, to prevent the
435 * device from being autosuspended, but left disabled. In either
436 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500437 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500438 pm_suspend_ignore_children(dev, false);
439 if (driver->supports_autosuspend)
440 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400441 else
442 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400443
444 /* if interface was already added, bind now; else let
445 * the future device_add() bind it, bypassing probe()
446 */
447 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700448 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400449
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700450 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400451}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600452EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400453
454/**
455 * usb_driver_release_interface - unbind a driver from an interface
456 * @driver: the driver to be unbound
457 * @iface: the interface from which it will be unbound
458 *
459 * This can be used by drivers to release an interface without waiting
460 * for their disconnect() methods to be called. In typical cases this
461 * also causes the driver disconnect() method to be called.
462 *
463 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400464 * Callers must own the device lock, so driver disconnect() entries don't
465 * need extra locking, but other call contexts may need to explicitly claim
466 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400467 */
468void usb_driver_release_interface(struct usb_driver *driver,
469 struct usb_interface *iface)
470{
471 struct device *dev = &iface->dev;
472
473 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400474 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400475 return;
476
477 /* don't release from within disconnect() */
478 if (iface->condition != USB_INTERFACE_BOUND)
479 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400480 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400481
Alan Stern91f8d062009-04-16 15:35:09 -0400482 /* Release via the driver core only if the interface
483 * has already been registered
484 */
Alan Stern36e56a32006-07-01 22:08:06 -0400485 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400486 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800487 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800488 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400489 usb_unbind_interface(dev);
490 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800491 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400492 }
Alan Stern36e56a32006-07-01 22:08:06 -0400493}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600494EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400495
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800496/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100497int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800498{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800499 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
500 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
501 return 0;
502
503 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
504 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
505 return 0;
506
507 /* No need to test id->bcdDevice_lo != 0, since 0 is never
508 greater than any unsigned number. */
509 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
510 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
511 return 0;
512
513 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
514 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
515 return 0;
516
517 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
518 (id->bDeviceClass != dev->descriptor.bDeviceClass))
519 return 0;
520
521 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800522 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800523 return 0;
524
525 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
526 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
527 return 0;
528
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100529 return 1;
530}
531
532/* returns 0 if no match, 1 if match */
533int usb_match_one_id(struct usb_interface *interface,
534 const struct usb_device_id *id)
535{
536 struct usb_host_interface *intf;
537 struct usb_device *dev;
538
539 /* proc_connectinfo in devio.c may call us with id == NULL. */
540 if (id == NULL)
541 return 0;
542
543 intf = interface->cur_altsetting;
544 dev = interface_to_usbdev(interface);
545
546 if (!usb_match_device(dev, id))
547 return 0;
548
Alan Stern93c8bf42006-10-18 16:41:51 -0400549 /* The interface class, subclass, and protocol should never be
550 * checked for a match if the device class is Vendor Specific,
551 * unless the match record specifies the Vendor ID. */
552 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
553 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
554 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
555 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
556 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
557 return 0;
558
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800559 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
560 (id->bInterfaceClass != intf->desc.bInterfaceClass))
561 return 0;
562
563 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
564 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
565 return 0;
566
567 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
568 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
569 return 0;
570
571 return 1;
572}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100573EXPORT_SYMBOL_GPL(usb_match_one_id);
574
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800575/**
576 * usb_match_id - find first usb_device_id matching device or interface
577 * @interface: the interface of interest
578 * @id: array of usb_device_id structures, terminated by zero entry
579 *
580 * usb_match_id searches an array of usb_device_id's and returns
581 * the first one matching the device or interface, or null.
582 * This is used when binding (or rebinding) a driver to an interface.
583 * Most USB device drivers will use this indirectly, through the usb core,
584 * but some layered driver frameworks use it directly.
585 * These device tables are exported with MODULE_DEVICE_TABLE, through
586 * modutils, to support the driver loading functionality of USB hotplugging.
587 *
588 * What Matches:
589 *
590 * The "match_flags" element in a usb_device_id controls which
591 * members are used. If the corresponding bit is set, the
592 * value in the device_id must match its corresponding member
593 * in the device or interface descriptor, or else the device_id
594 * does not match.
595 *
596 * "driver_info" is normally used only by device drivers,
597 * but you can create a wildcard "matches anything" usb_device_id
598 * as a driver's "modules.usbmap" entry if you provide an id with
599 * only a nonzero "driver_info" field. If you do this, the USB device
600 * driver's probe() routine should use additional intelligence to
601 * decide whether to bind to the specified interface.
602 *
603 * What Makes Good usb_device_id Tables:
604 *
605 * The match algorithm is very simple, so that intelligence in
606 * driver selection must come from smart driver id records.
607 * Unless you have good reasons to use another selection policy,
608 * provide match elements only in related groups, and order match
609 * specifiers from specific to general. Use the macros provided
610 * for that purpose if you can.
611 *
612 * The most specific match specifiers use device descriptor
613 * data. These are commonly used with product-specific matches;
614 * the USB_DEVICE macro lets you provide vendor and product IDs,
615 * and you can also match against ranges of product revisions.
616 * These are widely used for devices with application or vendor
617 * specific bDeviceClass values.
618 *
619 * Matches based on device class/subclass/protocol specifications
620 * are slightly more general; use the USB_DEVICE_INFO macro, or
621 * its siblings. These are used with single-function devices
622 * where bDeviceClass doesn't specify that each interface has
623 * its own class.
624 *
625 * Matches based on interface class/subclass/protocol are the
626 * most general; they let drivers bind to any interface on a
627 * multiple-function device. Use the USB_INTERFACE_INFO
628 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400629 * devices (as recorded in bInterfaceClass).
630 *
631 * Note that an entry created by USB_INTERFACE_INFO won't match
632 * any interface if the device class is set to Vendor-Specific.
633 * This is deliberate; according to the USB spec the meanings of
634 * the interface class/subclass/protocol for these devices are also
635 * vendor-specific, and hence matching against a standard product
636 * class wouldn't work anyway. If you really want to use an
637 * interface-based match for such a device, create a match record
638 * that also specifies the vendor ID. (Unforunately there isn't a
639 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800640 *
641 * Within those groups, remember that not all combinations are
642 * meaningful. For example, don't give a product version range
643 * without vendor and product IDs; or specify a protocol without
644 * its associated class and subclass.
645 */
646const struct usb_device_id *usb_match_id(struct usb_interface *interface,
647 const struct usb_device_id *id)
648{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800649 /* proc_connectinfo in devio.c may call us with id == NULL. */
650 if (id == NULL)
651 return NULL;
652
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800653 /* It is important to check that id->driver_info is nonzero,
654 since an entry that is all zeroes except for a nonzero
655 id->driver_info is the way to create an entry that
656 indicates that the driver want to examine every
657 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800658 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
659 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800660 if (usb_match_one_id(interface, id))
661 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800662 }
663
664 return NULL;
665}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600666EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800667
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100668static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800669{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400670 /* devices and interfaces are handled separately */
671 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800672
Alan Stern8bb54ab2006-07-01 22:08:49 -0400673 /* interface drivers never match devices */
674 if (!is_usb_device_driver(drv))
675 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800676
Alan Stern8bb54ab2006-07-01 22:08:49 -0400677 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800678 return 1;
679
Kay Sievers55129662009-05-04 19:48:32 +0200680 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400681 struct usb_interface *intf;
682 struct usb_driver *usb_drv;
683 const struct usb_device_id *id;
684
685 /* device drivers never match interfaces */
686 if (is_usb_device_driver(drv))
687 return 0;
688
689 intf = to_usb_interface(dev);
690 usb_drv = to_usb_driver(drv);
691
692 id = usb_match_id(intf, usb_drv->id_table);
693 if (id)
694 return 1;
695
696 id = usb_match_dynamic_id(intf, usb_drv);
697 if (id)
698 return 1;
699 }
700
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800701 return 0;
702}
703
Alan Stern36e56a32006-07-01 22:08:06 -0400704#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200705static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400706{
Alan Stern36e56a32006-07-01 22:08:06 -0400707 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400708
Kay Sievers55129662009-05-04 19:48:32 +0200709 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400710 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200711 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100712 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200713
Alan Stern8bb54ab2006-07-01 22:08:49 -0400714 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200715 } else {
716 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400717 }
Alan Stern36e56a32006-07-01 22:08:06 -0400718
719 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500720 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200721 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400722 return -ENODEV;
723 }
724 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200725 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400726 return -ENODEV;
727 }
728
729#ifdef CONFIG_USB_DEVICEFS
730 /* If this is available, userspace programs can directly read
731 * all the device descriptors we don't tell them about. Or
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100732 * act as usermode drivers.
Alan Stern36e56a32006-07-01 22:08:06 -0400733 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200734 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
Alan Stern36e56a32006-07-01 22:08:06 -0400735 usb_dev->bus->busnum, usb_dev->devnum))
736 return -ENOMEM;
737#endif
738
739 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200740 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400741 le16_to_cpu(usb_dev->descriptor.idVendor),
742 le16_to_cpu(usb_dev->descriptor.idProduct),
743 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
744 return -ENOMEM;
745
746 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200747 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400748 usb_dev->descriptor.bDeviceClass,
749 usb_dev->descriptor.bDeviceSubClass,
750 usb_dev->descriptor.bDeviceProtocol))
751 return -ENOMEM;
752
Alan Stern36e56a32006-07-01 22:08:06 -0400753 return 0;
754}
755
756#else
757
Kay Sievers7eff2e72007-08-14 15:15:12 +0200758static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400759{
760 return -ENODEV;
761}
Alan Stern36e56a32006-07-01 22:08:06 -0400762#endif /* CONFIG_HOTPLUG */
763
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800764/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400765 * usb_register_device_driver - register a USB device (not interface) driver
766 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800767 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800768 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400769 * Registers a USB device driver with the USB core. The list of
770 * unattached devices will be rescanned whenever a new driver is
771 * added, allowing the new driver to attach to any recognized devices.
772 * Returns a negative error code on failure and 0 on success.
773 */
774int usb_register_device_driver(struct usb_device_driver *new_udriver,
775 struct module *owner)
776{
777 int retval = 0;
778
779 if (usb_disabled())
780 return -ENODEV;
781
782 new_udriver->drvwrap.for_devices = 1;
783 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
784 new_udriver->drvwrap.driver.bus = &usb_bus_type;
785 new_udriver->drvwrap.driver.probe = usb_probe_device;
786 new_udriver->drvwrap.driver.remove = usb_unbind_device;
787 new_udriver->drvwrap.driver.owner = owner;
788
789 retval = driver_register(&new_udriver->drvwrap.driver);
790
791 if (!retval) {
792 pr_info("%s: registered new device driver %s\n",
793 usbcore_name, new_udriver->name);
794 usbfs_update_special();
795 } else {
796 printk(KERN_ERR "%s: error %d registering device "
797 " driver %s\n",
798 usbcore_name, retval, new_udriver->name);
799 }
800
801 return retval;
802}
803EXPORT_SYMBOL_GPL(usb_register_device_driver);
804
805/**
806 * usb_deregister_device_driver - unregister a USB device (not interface) driver
807 * @udriver: USB operations of the device driver to unregister
808 * Context: must be able to sleep
809 *
810 * Unlinks the specified driver from the internal USB driver list.
811 */
812void usb_deregister_device_driver(struct usb_device_driver *udriver)
813{
814 pr_info("%s: deregistering device driver %s\n",
815 usbcore_name, udriver->name);
816
817 driver_unregister(&udriver->drvwrap.driver);
818 usbfs_update_special();
819}
820EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
821
822/**
823 * usb_register_driver - register a USB interface driver
824 * @new_driver: USB operations for the interface driver
825 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800826 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400827 *
828 * Registers a USB interface driver with the USB core. The list of
829 * unattached interfaces will be rescanned whenever a new driver is
830 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800831 * Returns a negative error code on failure and 0 on success.
832 *
833 * NOTE: if you want your driver to use the USB major number, you must call
834 * usb_register_dev() to enable that functionality. This function no longer
835 * takes care of that.
836 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800837int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
838 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800839{
840 int retval = 0;
841
842 if (usb_disabled())
843 return -ENODEV;
844
Alan Stern8bb54ab2006-07-01 22:08:49 -0400845 new_driver->drvwrap.for_devices = 0;
846 new_driver->drvwrap.driver.name = (char *) new_driver->name;
847 new_driver->drvwrap.driver.bus = &usb_bus_type;
848 new_driver->drvwrap.driver.probe = usb_probe_interface;
849 new_driver->drvwrap.driver.remove = usb_unbind_interface;
850 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800851 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800852 spin_lock_init(&new_driver->dynids.lock);
853 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800854
Alan Stern8bb54ab2006-07-01 22:08:49 -0400855 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800856 if (retval)
857 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800858
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800859 usbfs_update_special();
860
Alan Sterned283e92012-01-24 14:35:13 -0500861 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800862 if (retval)
863 goto out_newid;
864
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800865 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800866 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800867
868out:
869 return retval;
870
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800871out_newid:
872 driver_unregister(&new_driver->drvwrap.driver);
873
874 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400875 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800876 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800877 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800878}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600879EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800880
881/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400882 * usb_deregister - unregister a USB interface driver
883 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800884 * Context: must be able to sleep
885 *
886 * Unlinks the specified driver from the internal USB driver list.
887 *
888 * NOTE: If you called usb_register_dev(), you still need to call
889 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
890 * this * call will no longer do it for you.
891 */
892void usb_deregister(struct usb_driver *driver)
893{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400894 pr_info("%s: deregistering interface driver %s\n",
895 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800896
Alan Sterned283e92012-01-24 14:35:13 -0500897 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400898 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -0500899 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800900
901 usbfs_update_special();
902}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600903EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400904
Alan Stern78d9a482008-06-23 16:00:40 -0400905/* Forced unbinding of a USB interface driver, either because
906 * it doesn't support pre_reset/post_reset/reset_resume or
907 * because it doesn't support suspend/resume.
908 *
909 * The caller must hold @intf's device's lock, but not its pm_mutex
910 * and not @intf->dev.sem.
911 */
912void usb_forced_unbind_intf(struct usb_interface *intf)
913{
914 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
915
916 dev_dbg(&intf->dev, "forced unbind\n");
917 usb_driver_release_interface(driver, intf);
918
919 /* Mark the interface for later rebinding */
920 intf->needs_binding = 1;
921}
922
923/* Delayed forced unbinding of a USB interface driver and scan
924 * for rebinding.
925 *
926 * The caller must hold @intf's device's lock, but not its pm_mutex
927 * and not @intf->dev.sem.
928 *
Alan Stern5096aed2008-08-12 14:34:14 -0400929 * Note: Rebinds will be skipped if a system sleep transition is in
930 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -0400931 */
932void usb_rebind_intf(struct usb_interface *intf)
933{
934 int rc;
935
936 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +0100937 if (intf->dev.driver)
938 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -0400939
940 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +0200941 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -0400942 intf->needs_binding = 0;
943 rc = device_attach(&intf->dev);
944 if (rc < 0)
945 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
946 }
Alan Stern78d9a482008-06-23 16:00:40 -0400947}
948
Alan Stern9ff78432008-08-07 13:04:51 -0400949#ifdef CONFIG_PM
950
Oliver Neukum14931382012-01-05 15:39:57 +0100951/* Unbind drivers for @udev's interfaces that don't support suspend/resume
952 * There is no check for reset_resume here because it can be determined
953 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -0400954 *
955 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -0400956 */
Oliver Neukum14931382012-01-05 15:39:57 +0100957static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -0400958{
959 struct usb_host_config *config;
960 int i;
961 struct usb_interface *intf;
962 struct usb_driver *drv;
963
964 config = udev->actconfig;
965 if (config) {
966 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
967 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +0100968
969 if (intf->dev.driver) {
970 drv = to_usb_driver(intf->dev.driver);
971 if (!drv->suspend || !drv->resume)
972 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -0400973 }
974 }
975 }
976}
977
Oliver Neukum14931382012-01-05 15:39:57 +0100978/* Unbind drivers for @udev's interfaces that failed to support reset-resume.
979 * These interfaces have the needs_binding flag set by usb_resume_interface().
980 *
981 * The caller must hold @udev's device lock.
982 */
983static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
984{
985 struct usb_host_config *config;
986 int i;
987 struct usb_interface *intf;
988
989 config = udev->actconfig;
990 if (config) {
991 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
992 intf = config->interface[i];
993 if (intf->dev.driver && intf->needs_binding)
994 usb_forced_unbind_intf(intf);
995 }
996 }
997}
998
999static void do_rebind_interfaces(struct usb_device *udev)
1000{
1001 struct usb_host_config *config;
1002 int i;
1003 struct usb_interface *intf;
1004
1005 config = udev->actconfig;
1006 if (config) {
1007 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1008 intf = config->interface[i];
1009 if (intf->needs_binding)
1010 usb_rebind_intf(intf);
1011 }
1012 }
1013}
1014
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001015static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001016{
Alan Stern782da722006-07-01 22:09:35 -04001017 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001018 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001019
Alan Stern114b3682006-07-01 22:13:04 -04001020 if (udev->state == USB_STATE_NOTATTACHED ||
1021 udev->state == USB_STATE_SUSPENDED)
1022 goto done;
1023
Alan Sternb6f64362007-05-04 11:51:54 -04001024 /* For devices that don't have a driver, we do a generic suspend. */
1025 if (udev->dev.driver)
1026 udriver = to_usb_device_driver(udev->dev.driver);
1027 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001028 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001029 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001030 }
Alan Stern2bf40862006-07-01 22:12:19 -04001031 status = udriver->suspend(udev, msg);
1032
Alan Stern20dfdad2007-05-22 11:50:17 -04001033 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001034 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001035 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001036}
Alan Stern36e56a32006-07-01 22:08:06 -04001037
Alan Stern65bfd292008-11-25 16:39:18 -05001038static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001039{
1040 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001041 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001042
Alan Stern0458d5b2007-05-04 11:52:20 -04001043 if (udev->state == USB_STATE_NOTATTACHED)
1044 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001045
Alan Stern1c5df7e2006-07-01 22:13:50 -04001046 /* Can't resume it if it doesn't have a driver. */
1047 if (udev->dev.driver == NULL) {
1048 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001049 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001050 }
1051
Alan Stern6d19c002010-02-12 12:21:11 +01001052 /* Non-root devices on a full/low-speed bus must wait for their
1053 * companion high-speed root hub, in case a handoff is needed.
1054 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001055 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001056 device_pm_wait_for_dev(&udev->dev,
1057 &udev->bus->hs_companion->root_hub->dev);
1058
Alan Stern6bc6cff2007-05-04 11:53:03 -04001059 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1060 udev->reset_resume = 1;
1061
Alan Stern1cc8a252006-07-01 22:10:15 -04001062 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001063 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001064
Alan Stern20dfdad2007-05-22 11:50:17 -04001065 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001066 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001067 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001068}
1069
Alan Stern65605ae2008-08-12 14:33:27 -04001070static int usb_suspend_interface(struct usb_device *udev,
1071 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001072{
1073 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001074 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001075
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001076 if (udev->state == USB_STATE_NOTATTACHED ||
1077 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001078 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001079 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001080
Oliver Neukume78832c2012-01-02 15:11:48 +01001081 /* at this time we know the driver supports suspend */
1082 status = driver->suspend(intf, msg);
1083 if (status && !PMSG_IS_AUTO(msg))
1084 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001085
Alan Stern20dfdad2007-05-22 11:50:17 -04001086 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001087 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001088 return status;
1089}
1090
Alan Stern65605ae2008-08-12 14:33:27 -04001091static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001092 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001093{
Alan Stern1cc8a252006-07-01 22:10:15 -04001094 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001095 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001096
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001097 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001098 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001099
Alan Stern645daaa2006-08-30 15:47:02 -04001100 /* Don't let autoresume interfere with unbinding */
1101 if (intf->condition == USB_INTERFACE_UNBINDING)
1102 goto done;
1103
Alan Stern1c5df7e2006-07-01 22:13:50 -04001104 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001105 if (intf->condition == USB_INTERFACE_UNBOUND) {
1106
1107 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001108 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001109 usb_set_interface(udev, intf->altsetting[0].
1110 desc.bInterfaceNumber, 0);
1111 intf->needs_altsetting0 = 0;
1112 }
Alan Stern2bf40862006-07-01 22:12:19 -04001113 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001114 }
Alan Stern78d9a482008-06-23 16:00:40 -04001115
1116 /* Don't resume if the interface is marked for rebinding */
1117 if (intf->needs_binding)
1118 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001119 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001120
Alan Sternf07600c2007-05-30 15:38:16 -04001121 if (reset_resume) {
1122 if (driver->reset_resume) {
1123 status = driver->reset_resume(intf);
1124 if (status)
1125 dev_err(&intf->dev, "%s error %d\n",
1126 "reset_resume", status);
1127 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001128 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001129 dev_warn(&intf->dev, "no %s for driver %s?\n",
1130 "reset_resume", driver->name);
1131 }
1132 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001133 status = driver->resume(intf);
1134 if (status)
1135 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001136 }
Alan Stern2bf40862006-07-01 22:12:19 -04001137
1138done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001139 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001140
Alan Stern78d9a482008-06-23 16:00:40 -04001141 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001142 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001143}
1144
Alan Stern645daaa2006-08-30 15:47:02 -04001145/**
1146 * usb_suspend_both - suspend a USB device and its interfaces
1147 * @udev: the usb_device to suspend
1148 * @msg: Power Management message describing this state transition
1149 *
1150 * This is the central routine for suspending USB devices. It calls the
1151 * suspend methods for all the interface drivers in @udev and then calls
1152 * the suspend method for @udev itself. If an error occurs at any stage,
1153 * all the interfaces which were suspended are resumed so that they remain
1154 * in the same state as the device.
1155 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001156 * Autosuspend requests originating from a child device or an interface
1157 * driver may be made without the protection of @udev's device lock, but
1158 * all other suspend calls will hold the lock. Usbcore will insure that
1159 * method calls do not arrive during bind, unbind, or reset operations.
1160 * However drivers must be prepared to handle suspend calls arriving at
1161 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001162 *
1163 * This routine can run only in process context.
1164 */
Alan Stern718efa62007-03-09 15:41:13 -05001165static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001166{
1167 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001168 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001169 struct usb_interface *intf;
1170
Alan Stern19410442007-03-27 13:33:59 -04001171 if (udev->state == USB_STATE_NOTATTACHED ||
1172 udev->state == USB_STATE_SUSPENDED)
1173 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001174
Alan Stern645daaa2006-08-30 15:47:02 -04001175 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001176 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001177 n = udev->actconfig->desc.bNumInterfaces;
1178 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001179 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001180 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001181
1182 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001183 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001184 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001185 if (status != 0)
1186 break;
1187 }
1188 }
Alan Stern0af212b2011-06-15 16:27:43 -04001189 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001190 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001191
Alan Stern0af212b2011-06-15 16:27:43 -04001192 /* Again, ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001193 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001194 status = 0;
1195 }
1196
Alan Sterna8e7c562006-07-01 22:11:02 -04001197 /* If the suspend failed, resume interfaces that did get suspended */
1198 if (status != 0) {
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001199 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
Alan Stern571dc792010-04-09 16:03:43 -04001200 while (++i < n) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001201 intf = udev->actconfig->interface[i];
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001202 usb_resume_interface(udev, intf, msg, 0);
Alan Sterna8e7c562006-07-01 22:11:02 -04001203 }
Alan Stern645daaa2006-08-30 15:47:02 -04001204
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001205 /* If the suspend succeeded then prevent any more URB submissions
1206 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001207 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001208 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001209 udev->can_submit = 0;
1210 for (i = 0; i < 16; ++i) {
1211 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1212 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1213 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001214 }
Alan Stern645daaa2006-08-30 15:47:02 -04001215
Alan Stern19410442007-03-27 13:33:59 -04001216 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001217 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001218 return status;
1219}
1220
Alan Stern645daaa2006-08-30 15:47:02 -04001221/**
1222 * usb_resume_both - resume a USB device and its interfaces
1223 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001224 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001225 *
1226 * This is the central routine for resuming USB devices. It calls the
1227 * the resume method for @udev and then calls the resume methods for all
1228 * the interface drivers in @udev.
1229 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001230 * Autoresume requests originating from a child device or an interface
1231 * driver may be made without the protection of @udev's device lock, but
1232 * all other resume calls will hold the lock. Usbcore will insure that
1233 * method calls do not arrive during bind, unbind, or reset operations.
1234 * However drivers must be prepared to handle resume calls arriving at
1235 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001236 *
1237 * This routine can run only in process context.
1238 */
Alan Stern65bfd292008-11-25 16:39:18 -05001239static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001240{
Alan Stern645daaa2006-08-30 15:47:02 -04001241 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001242 int i;
1243 struct usb_interface *intf;
1244
Alan Stern19410442007-03-27 13:33:59 -04001245 if (udev->state == USB_STATE_NOTATTACHED) {
1246 status = -ENODEV;
1247 goto done;
1248 }
Alan Stern6840d252007-09-10 11:34:26 -04001249 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001250
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001251 /* Resume the device */
1252 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001253 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001254
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001255 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001256 if (status == 0 && udev->actconfig) {
1257 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1258 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001259 usb_resume_interface(udev, intf, msg,
1260 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001261 }
1262 }
Alan Sternc08512c2010-11-15 15:57:58 -05001263 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001264
Alan Stern19410442007-03-27 13:33:59 -04001265 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001266 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001267 if (!status)
1268 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001269 return status;
1270}
1271
Alan Stern5f677f12010-04-02 13:20:11 -04001272static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1273{
Alan Stern48826622010-06-22 16:14:48 -04001274 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001275
1276 /* Remote wakeup is needed only when we actually go to sleep.
1277 * For things like FREEZE and QUIESCE, if the device is already
1278 * autosuspended then its current wakeup setting is okay.
1279 */
1280 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1281 if (udev->state != USB_STATE_SUSPENDED)
1282 udev->do_remote_wakeup = 0;
1283 return;
1284 }
1285
Alan Stern48826622010-06-22 16:14:48 -04001286 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001287 * actually want it.
1288 */
Alan Stern48826622010-06-22 16:14:48 -04001289 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001290
1291 /* If the device is autosuspended with the wrong wakeup setting,
1292 * autoresume now so the setting can be changed.
1293 */
1294 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1295 pm_runtime_resume(&udev->dev);
1296 udev->do_remote_wakeup = w;
1297}
1298
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001299/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001300int usb_suspend(struct device *dev, pm_message_t msg)
1301{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001302 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001303
Oliver Neukum14931382012-01-05 15:39:57 +01001304 unbind_no_pm_drivers_interfaces(udev);
1305
1306 /* From now on we are sure all drivers support suspend/resume
1307 * but not necessarily reset_resume()
1308 * so we may still need to unbind and rebind upon resume
1309 */
Alan Stern5f677f12010-04-02 13:20:11 -04001310 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001311 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001312}
1313
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001314/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001315int usb_resume_complete(struct device *dev)
1316{
1317 struct usb_device *udev = to_usb_device(dev);
1318
1319 /* For PM complete calls, all we do is rebind interfaces
1320 * whose needs_binding flag is set
1321 */
1322 if (udev->state != USB_STATE_NOTATTACHED)
1323 do_rebind_interfaces(udev);
1324 return 0;
1325}
1326
1327/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001328int usb_resume(struct device *dev, pm_message_t msg)
1329{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001330 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001331 int status;
1332
Oliver Neukum98d9a822012-01-11 08:38:35 +01001333 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001334 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001335 * Unbind the interfaces that will need rebinding later,
1336 * because they fail to support reset_resume.
1337 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001338 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001339 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001340 status = usb_resume_both(udev, msg);
1341 if (status == 0) {
1342 pm_runtime_disable(dev);
1343 pm_runtime_set_active(dev);
1344 pm_runtime_enable(dev);
1345 unbind_no_reset_resume_drivers_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001346 }
Alan Stern0c590e22010-01-08 12:57:14 -05001347
1348 /* Avoid PM error messages for devices disconnected while suspended
1349 * as we'll display regular disconnect messages just a bit later.
1350 */
Peter Chen7491f132010-09-27 16:43:25 +08001351 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001352 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001353 return status;
1354}
1355
1356#endif /* CONFIG_PM */
1357
Alan Stern645daaa2006-08-30 15:47:02 -04001358#ifdef CONFIG_USB_SUSPEND
1359
Alan Stern088f7fe2010-01-08 12:56:54 -05001360/**
1361 * usb_enable_autosuspend - allow a USB device to be autosuspended
1362 * @udev: the USB device which may be autosuspended
1363 *
1364 * This routine allows @udev to be autosuspended. An autosuspend won't
1365 * take place until the autosuspend_delay has elapsed and all the other
1366 * necessary conditions are satisfied.
1367 *
1368 * The caller must hold @udev's device lock.
1369 */
Alan Stern9e18c822010-04-02 13:22:09 -04001370void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001371{
Alan Stern9e18c822010-04-02 13:22:09 -04001372 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001373}
1374EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1375
1376/**
1377 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1378 * @udev: the USB device which may not be autosuspended
1379 *
1380 * This routine prevents @udev from being autosuspended and wakes it up
1381 * if it is already autosuspended.
1382 *
1383 * The caller must hold @udev's device lock.
1384 */
Alan Stern9e18c822010-04-02 13:22:09 -04001385void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001386{
Alan Stern9e18c822010-04-02 13:22:09 -04001387 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001388}
1389EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1390
Alan Stern645daaa2006-08-30 15:47:02 -04001391/**
1392 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001393 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001394 *
1395 * This routine should be called when a core subsystem is finished using
1396 * @udev and wants to allow it to autosuspend. Examples would be when
1397 * @udev's device file in usbfs is closed or after a configuration change.
1398 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001399 * @udev's usage counter is decremented; if it drops to 0 and all the
1400 * interfaces are inactive then a delayed autosuspend will be attempted.
1401 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001402 *
Alan Stern62e299e2010-01-08 12:56:19 -05001403 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001404 *
1405 * This routine can run only in process context.
1406 */
Alan Stern94fcda12006-11-20 11:38:46 -05001407void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001408{
Alan Stern94fcda12006-11-20 11:38:46 -05001409 int status;
1410
Ming Lei6ddf27c2010-11-15 15:57:30 -05001411 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001412 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001413 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1414 __func__, atomic_read(&udev->dev.power.usage_count),
1415 status);
Alan Stern19c26232007-02-20 15:03:32 -05001416}
1417
1418/**
Alan Stern645daaa2006-08-30 15:47:02 -04001419 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001420 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001421 *
1422 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001423 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001424 * occur until usb_autosuspend_device() is called. (Note that this will
1425 * not prevent suspend events originating in the PM core.) Examples would
1426 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001427 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001428 *
Alan Stern94fcda12006-11-20 11:38:46 -05001429 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1430 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001431 *
Alan Stern62e299e2010-01-08 12:56:19 -05001432 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001433 *
1434 * This routine can run only in process context.
1435 */
Alan Stern94fcda12006-11-20 11:38:46 -05001436int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001437{
1438 int status;
1439
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001440 status = pm_runtime_get_sync(&udev->dev);
1441 if (status < 0)
1442 pm_runtime_put_sync(&udev->dev);
1443 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1444 __func__, atomic_read(&udev->dev.power.usage_count),
1445 status);
1446 if (status > 0)
1447 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001448 return status;
1449}
1450
Alan Stern645daaa2006-08-30 15:47:02 -04001451/**
1452 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001453 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001454 *
1455 * This routine should be called by an interface driver when it is
1456 * finished using @intf and wants to allow it to autosuspend. A typical
1457 * example would be a character-device driver when its device file is
1458 * closed.
1459 *
1460 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001461 * 0, a delayed autosuspend request for @intf's device is attempted. The
1462 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001463 *
Alan Stern645daaa2006-08-30 15:47:02 -04001464 * This routine can run only in process context.
1465 */
1466void usb_autopm_put_interface(struct usb_interface *intf)
1467{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001468 struct usb_device *udev = interface_to_usbdev(intf);
1469 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001470
Ming Lei6ddf27c2010-11-15 15:57:30 -05001471 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001472 atomic_dec(&intf->pm_usage_cnt);
1473 status = pm_runtime_put_sync(&intf->dev);
1474 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1475 __func__, atomic_read(&intf->dev.power.usage_count),
1476 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001477}
1478EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1479
1480/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001481 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1482 * @intf: the usb_interface whose counter should be decremented
1483 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001484 * This routine does much the same thing as usb_autopm_put_interface():
1485 * It decrements @intf's usage counter and schedules a delayed
1486 * autosuspend request if the counter is <= 0. The difference is that it
1487 * does not perform any synchronization; callers should hold a private
1488 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001489 *
1490 * Typically a driver would call this routine during an URB's completion
1491 * handler, if no more URBs were pending.
1492 *
1493 * This routine can run in atomic context.
1494 */
1495void usb_autopm_put_interface_async(struct usb_interface *intf)
1496{
1497 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001498 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001499
Ming Lei6ddf27c2010-11-15 15:57:30 -05001500 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001501 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001502 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001503 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1504 __func__, atomic_read(&intf->dev.power.usage_count),
1505 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001506}
1507EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1508
1509/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001510 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1511 * @intf: the usb_interface whose counter should be decremented
1512 *
1513 * This routine decrements @intf's usage counter but does not carry out an
1514 * autosuspend.
1515 *
1516 * This routine can run in atomic context.
1517 */
1518void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1519{
1520 struct usb_device *udev = interface_to_usbdev(intf);
1521
Ming Lei6ddf27c2010-11-15 15:57:30 -05001522 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001523 atomic_dec(&intf->pm_usage_cnt);
1524 pm_runtime_put_noidle(&intf->dev);
1525}
1526EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1527
1528/**
Alan Stern645daaa2006-08-30 15:47:02 -04001529 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001530 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001531 *
1532 * This routine should be called by an interface driver when it wants to
1533 * use @intf and needs to guarantee that it is not suspended. In addition,
1534 * the routine prevents @intf from being autosuspended subsequently. (Note
1535 * that this will not prevent suspend events originating in the PM core.)
1536 * This prevention will persist until usb_autopm_put_interface() is called
1537 * or @intf is unbound. A typical example would be a character-device
1538 * driver when its device file is opened.
1539 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001540 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1541 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001542 *
1543 * This routine can run only in process context.
1544 */
1545int usb_autopm_get_interface(struct usb_interface *intf)
1546{
Alan Sternaf4f7602006-10-30 17:06:45 -05001547 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001548
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001549 status = pm_runtime_get_sync(&intf->dev);
1550 if (status < 0)
1551 pm_runtime_put_sync(&intf->dev);
1552 else
1553 atomic_inc(&intf->pm_usage_cnt);
1554 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1555 __func__, atomic_read(&intf->dev.power.usage_count),
1556 status);
1557 if (status > 0)
1558 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001559 return status;
1560}
1561EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1562
Alan Stern692a1862006-10-30 17:07:51 -05001563/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001564 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1565 * @intf: the usb_interface whose counter should be incremented
1566 *
1567 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001568 * usb_autopm_get_interface(): It increments @intf's usage counter and
1569 * queues an autoresume request if the device is suspended. The
1570 * differences are that it does not perform any synchronization (callers
1571 * should hold a private lock and handle all synchronization issues
1572 * themselves), and it does not autoresume the device directly (it only
1573 * queues a request). After a successful call, the device may not yet be
1574 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001575 *
1576 * This routine can run in atomic context.
1577 */
1578int usb_autopm_get_interface_async(struct usb_interface *intf)
1579{
Ming Lei63defa72010-11-15 15:56:54 -05001580 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001581
Ming Lei63defa72010-11-15 15:56:54 -05001582 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001583 if (status < 0 && status != -EINPROGRESS)
1584 pm_runtime_put_noidle(&intf->dev);
1585 else
Alan Sternccf5b802009-06-29 11:00:01 -04001586 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001587 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1588 __func__, atomic_read(&intf->dev.power.usage_count),
1589 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001590 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001591 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001592 return status;
1593}
1594EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1595
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001596/**
1597 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1598 * @intf: the usb_interface whose counter should be incremented
1599 *
1600 * This routine increments @intf's usage counter but does not carry out an
1601 * autoresume.
1602 *
1603 * This routine can run in atomic context.
1604 */
1605void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1606{
1607 struct usb_device *udev = interface_to_usbdev(intf);
1608
Ming Lei6ddf27c2010-11-15 15:57:30 -05001609 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001610 atomic_inc(&intf->pm_usage_cnt);
1611 pm_runtime_get_noresume(&intf->dev);
1612}
1613EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1614
1615/* Internal routine to check whether we may autosuspend a device. */
1616static int autosuspend_check(struct usb_device *udev)
1617{
Alan Stern7560d32e2010-04-02 13:18:50 -04001618 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001619 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001620
1621 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1622 * any interface drivers require remote wakeup but it isn't available.
1623 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001624 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001625 if (udev->actconfig) {
1626 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1627 intf = udev->actconfig->interface[i];
1628
1629 /* We don't need to check interfaces that are
1630 * disabled for runtime PM. Either they are unbound
1631 * or else their drivers don't support autosuspend
1632 * and so they are permanently active.
1633 */
1634 if (intf->dev.power.disable_depth)
1635 continue;
1636 if (atomic_read(&intf->dev.power.usage_count) > 0)
1637 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001638 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001639
1640 /* Don't allow autosuspend if the device will need
1641 * a reset-resume and any of its interface drivers
1642 * doesn't include support or needs remote wakeup.
1643 */
1644 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1645 struct usb_driver *driver;
1646
1647 driver = to_usb_driver(intf->dev.driver);
1648 if (!driver->reset_resume ||
1649 intf->needs_remote_wakeup)
1650 return -EOPNOTSUPP;
1651 }
1652 }
1653 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001654 if (w && !device_can_wakeup(&udev->dev)) {
1655 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1656 return -EOPNOTSUPP;
1657 }
1658 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001659 return 0;
1660}
1661
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001662int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001663{
Ming Lei63defa72010-11-15 15:56:54 -05001664 struct usb_device *udev = to_usb_device(dev);
1665 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001666
1667 /* A USB device can be suspended if it passes the various autosuspend
1668 * checks. Runtime suspend for a USB device means suspending all the
1669 * interfaces and then the device itself.
1670 */
Ming Lei63defa72010-11-15 15:56:54 -05001671 if (autosuspend_check(udev) != 0)
1672 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001673
Ming Lei63defa72010-11-15 15:56:54 -05001674 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001675
1676 /* Allow a retry if autosuspend failed temporarily */
1677 if (status == -EAGAIN || status == -EBUSY)
1678 usb_mark_last_busy(udev);
1679
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001680 /* The PM core reacts badly unless the return code is 0,
1681 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1682 */
1683 if (status != 0)
1684 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001685 return status;
1686}
1687
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001688int usb_runtime_resume(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 int status;
1692
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001693 /* Runtime resume for a USB device means resuming both the device
1694 * and all its interfaces.
1695 */
Ming Lei63defa72010-11-15 15:56:54 -05001696 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001697 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001698}
1699
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001700int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001701{
Ming Lei63defa72010-11-15 15:56:54 -05001702 struct usb_device *udev = to_usb_device(dev);
1703
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001704 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001705 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001706 */
Ming Lei63defa72010-11-15 15:56:54 -05001707 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001708 pm_runtime_autosuspend(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001709 return 0;
1710}
1711
Andiry Xu65580b432011-09-23 14:19:52 -07001712int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1713{
1714 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1715 int ret = -EPERM;
1716
1717 if (hcd->driver->set_usb2_hw_lpm) {
1718 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1719 if (!ret)
1720 udev->usb2_hw_lpm_enabled = enable;
1721 }
1722
1723 return ret;
1724}
1725
Alan Stern645daaa2006-08-30 15:47:02 -04001726#endif /* CONFIG_USB_SUSPEND */
1727
Alan Stern36e56a32006-07-01 22:08:06 -04001728struct bus_type usb_bus_type = {
1729 .name = "usb",
1730 .match = usb_device_match,
1731 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001732};