blob: 54c493b4226ba6c99dbf09a0ed7529ec90134efe [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
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800132static int usb_create_newid_file(struct usb_driver *usb_drv)
133{
134 int error = 0;
135
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800136 if (usb_drv->no_dynamic_id)
137 goto exit;
138
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800139 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);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800142exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800143 return error;
144}
145
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800146static void usb_remove_newid_file(struct usb_driver *usb_drv)
147{
148 if (usb_drv->no_dynamic_id)
149 return;
150
151 if (usb_drv->probe != NULL)
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800152 driver_remove_file(&usb_drv->drvwrap.driver,
153 &driver_attr_new_id);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800154}
155
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800156static int
157usb_create_removeid_file(struct usb_driver *drv)
158{
159 int error = 0;
160 if (drv->probe != NULL)
161 error = driver_create_file(&drv->drvwrap.driver,
162 &driver_attr_remove_id);
163 return error;
164}
165
166static void usb_remove_removeid_file(struct usb_driver *drv)
167{
168 driver_remove_file(&drv->drvwrap.driver, &driver_attr_remove_id);
169}
170
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800171static void usb_free_dynids(struct usb_driver *usb_drv)
172{
173 struct usb_dynid *dynid, *n;
174
175 spin_lock(&usb_drv->dynids.lock);
176 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
177 list_del(&dynid->node);
178 kfree(dynid);
179 }
180 spin_unlock(&usb_drv->dynids.lock);
181}
182#else
183static inline int usb_create_newid_file(struct usb_driver *usb_drv)
184{
185 return 0;
186}
187
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800188static void usb_remove_newid_file(struct usb_driver *usb_drv)
189{
190}
191
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800192static int
193usb_create_removeid_file(struct usb_driver *drv)
194{
195 return 0;
196}
197
198static void usb_remove_removeid_file(struct usb_driver *drv)
199{
200}
201
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800202static inline void usb_free_dynids(struct usb_driver *usb_drv)
203{
204}
205#endif
206
207static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
208 struct usb_driver *drv)
209{
210 struct usb_dynid *dynid;
211
212 spin_lock(&drv->dynids.lock);
213 list_for_each_entry(dynid, &drv->dynids.list, node) {
214 if (usb_match_one_id(intf, &dynid->id)) {
215 spin_unlock(&drv->dynids.lock);
216 return &dynid->id;
217 }
218 }
219 spin_unlock(&drv->dynids.lock);
220 return NULL;
221}
222
223
Alan Stern8bb54ab2006-07-01 22:08:49 -0400224/* called from driver core with dev locked */
225static int usb_probe_device(struct device *dev)
226{
227 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200228 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500229 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400230
Harvey Harrison441b62c2008-03-03 16:08:34 -0800231 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400232
Alan Stern8bb54ab2006-07-01 22:08:49 -0400233 /* TODO: Add real matching code */
234
Alan Stern645daaa2006-08-30 15:47:02 -0400235 /* The device should always appear to be in use
236 * unless the driver suports autosuspend.
237 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500238 if (!udriver->supports_autosuspend)
239 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400240
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500241 if (!error)
242 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400243 return error;
244}
245
246/* called from driver core with dev locked */
247static int usb_unbind_device(struct device *dev)
248{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500249 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400250 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
251
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500252 udriver->disconnect(udev);
253 if (!udriver->supports_autosuspend)
254 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400255 return 0;
256}
257
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800258/*
259 * Cancel any pending scheduled resets
260 *
261 * [see usb_queue_reset_device()]
262 *
263 * Called after unconfiguring / when releasing interfaces. See
264 * comments in __usb_queue_reset_device() regarding
265 * udev->reset_running.
266 */
267static void usb_cancel_queued_reset(struct usb_interface *iface)
268{
269 if (iface->reset_running == 0)
270 cancel_work_sync(&iface->reset_ws);
271}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400272
273/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800274static int usb_probe_interface(struct device *dev)
275{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400276 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200277 struct usb_interface *intf = to_usb_interface(dev);
278 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800279 const struct usb_device_id *id;
280 int error = -ENODEV;
281
Harvey Harrison441b62c2008-03-03 16:08:34 -0800282 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800283
Alan Stern78d9a482008-06-23 16:00:40 -0400284 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800285
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400286 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500287 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400288
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800289 if (udev->authorized == 0) {
290 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500291 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800292 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700293
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800294 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800295 if (!id)
296 id = usb_match_dynamic_id(intf, driver);
Alan Stern0f3dda92010-01-08 12:56:04 -0500297 if (!id)
298 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800299
Alan Stern0f3dda92010-01-08 12:56:04 -0500300 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400301
Alan Stern0f3dda92010-01-08 12:56:04 -0500302 error = usb_autoresume_device(udev);
303 if (error)
304 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400305
Alan Stern0f3dda92010-01-08 12:56:04 -0500306 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400307
Alan Stern571dc792010-04-09 16:03:43 -0400308 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500309 * runtime-PM-enabled only if the driver has autosuspend support.
310 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500311 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500312 pm_runtime_set_active(dev);
313 pm_suspend_ignore_children(dev, false);
314 if (driver->supports_autosuspend)
315 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200316
Alan Stern0f3dda92010-01-08 12:56:04 -0500317 /* Carry out a deferred switch to altsetting 0 */
318 if (intf->needs_altsetting0) {
319 error = usb_set_interface(udev, intf->altsetting[0].
320 desc.bInterfaceNumber, 0);
321 if (error < 0)
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200322 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500323 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800324 }
325
Alan Stern0f3dda92010-01-08 12:56:04 -0500326 error = driver->probe(intf, id);
327 if (error)
328 goto err;
329
330 intf->condition = USB_INTERFACE_BOUND;
331 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800332 return error;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200333
Alan Stern0f3dda92010-01-08 12:56:04 -0500334 err:
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200335 intf->needs_remote_wakeup = 0;
336 intf->condition = USB_INTERFACE_UNBOUND;
337 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500338
339 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400340 if (driver->supports_autosuspend)
341 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500342 pm_runtime_set_suspended(dev);
343
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200344 usb_autosuspend_device(udev);
345 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800346}
347
Alan Stern8bb54ab2006-07-01 22:08:49 -0400348/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800349static int usb_unbind_interface(struct device *dev)
350{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400351 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800352 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400353 struct usb_device *udev;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200354 int error, r;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800355
356 intf->condition = USB_INTERFACE_UNBINDING;
357
Alan Stern645daaa2006-08-30 15:47:02 -0400358 /* Autoresume for set_interface call below */
359 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500360 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400361
Alan Stern9da82bd2008-05-08 11:54:37 -0400362 /* Terminate all URBs for this interface unless the driver
363 * supports "soft" unbinding.
364 */
365 if (!driver->soft_unbind)
Alan Sternddeac4e2009-01-15 17:03:33 -0500366 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800367
Alan Stern8bb54ab2006-07-01 22:08:49 -0400368 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800369 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800370
Alan Stern55151d72008-08-12 14:33:59 -0400371 /* Reset other interface state.
372 * We cannot do a Set-Interface if the device is suspended or
373 * if it is prepared for a system sleep (since installing a new
374 * altsetting means creating new endpoint device entries).
375 * When either of these happens, defer the Set-Interface.
376 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500377 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
378 /* Already in altsetting 0 so skip Set-Interface.
379 * Just re-enable it without affecting the endpoint toggles.
380 */
381 usb_enable_interface(udev, intf, false);
Alan Sternf76b1682011-06-18 20:22:23 +0200382 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200383 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400384 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200385 if (r < 0)
386 intf->needs_altsetting0 = 1;
387 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400388 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200389 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800390 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400391
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800392 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400393 intf->needs_remote_wakeup = 0;
394
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500395 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400396 if (driver->supports_autosuspend)
397 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500398 pm_runtime_set_suspended(dev);
399
400 /* Undo any residual pm_autopm_get_interface_* calls */
401 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
402 usb_autopm_put_interface_no_suspend(intf);
403 atomic_set(&intf->pm_usage_cnt, 0);
404
Alan Stern645daaa2006-08-30 15:47:02 -0400405 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500406 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800407
408 return 0;
409}
410
Alan Stern36e56a32006-07-01 22:08:06 -0400411/**
412 * usb_driver_claim_interface - bind a driver to an interface
413 * @driver: the driver to be bound
414 * @iface: the interface to which it will be bound; must be in the
415 * usb device's active configuration
416 * @priv: driver data associated with that interface
417 *
418 * This is used by usb device drivers that need to claim more than one
419 * interface on a device when probing (audio and acm are current examples).
420 * No device driver should directly modify internal usb_interface or
421 * usb_device structure members.
422 *
423 * Few drivers should need to use this routine, since the most natural
424 * way to bind to an interface is to return the private data from
425 * the driver's probe() method.
426 *
Greg Kroah-Hartman341487a2007-04-09 11:52:31 -0400427 * Callers must own the device lock, so driver probe() entries don't need
428 * extra locking, but other call contexts may need to explicitly claim that
429 * lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400430 */
431int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800432 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400433{
434 struct device *dev = &iface->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700435 int retval = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400436
437 if (dev->driver)
438 return -EBUSY;
439
Alan Stern8bb54ab2006-07-01 22:08:49 -0400440 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400441 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400442 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400443
Alan Stern36e56a32006-07-01 22:08:06 -0400444 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500445
Alan Stern89842ae2010-05-11 11:44:06 -0400446 /* Claimed interfaces are initially inactive (suspended) and
447 * runtime-PM-enabled, but only if the driver has autosuspend
448 * support. Otherwise they are marked active, to prevent the
449 * device from being autosuspended, but left disabled. In either
450 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500451 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500452 pm_suspend_ignore_children(dev, false);
453 if (driver->supports_autosuspend)
454 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400455 else
456 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400457
458 /* if interface was already added, bind now; else let
459 * the future device_add() bind it, bypassing probe()
460 */
461 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700462 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400463
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700464 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400465}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600466EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400467
468/**
469 * usb_driver_release_interface - unbind a driver from an interface
470 * @driver: the driver to be unbound
471 * @iface: the interface from which it will be unbound
472 *
473 * This can be used by drivers to release an interface without waiting
474 * for their disconnect() methods to be called. In typical cases this
475 * also causes the driver disconnect() method to be called.
476 *
477 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a2007-04-09 11:52:31 -0400478 * Callers must own the device lock, so driver disconnect() entries don't
479 * need extra locking, but other call contexts may need to explicitly claim
480 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400481 */
482void usb_driver_release_interface(struct usb_driver *driver,
483 struct usb_interface *iface)
484{
485 struct device *dev = &iface->dev;
486
487 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400488 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400489 return;
490
491 /* don't release from within disconnect() */
492 if (iface->condition != USB_INTERFACE_BOUND)
493 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400494 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400495
Alan Stern91f8d062009-04-16 15:35:09 -0400496 /* Release via the driver core only if the interface
497 * has already been registered
498 */
Alan Stern36e56a32006-07-01 22:08:06 -0400499 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400500 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800501 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800502 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400503 usb_unbind_interface(dev);
504 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800505 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400506 }
Alan Stern36e56a32006-07-01 22:08:06 -0400507}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600508EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400509
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800510/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100511int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800512{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800513 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
514 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
515 return 0;
516
517 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
518 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
519 return 0;
520
521 /* No need to test id->bcdDevice_lo != 0, since 0 is never
522 greater than any unsigned number. */
523 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
524 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
525 return 0;
526
527 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
528 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
529 return 0;
530
531 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
532 (id->bDeviceClass != dev->descriptor.bDeviceClass))
533 return 0;
534
535 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800536 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800537 return 0;
538
539 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
540 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
541 return 0;
542
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100543 return 1;
544}
545
546/* returns 0 if no match, 1 if match */
547int usb_match_one_id(struct usb_interface *interface,
548 const struct usb_device_id *id)
549{
550 struct usb_host_interface *intf;
551 struct usb_device *dev;
552
553 /* proc_connectinfo in devio.c may call us with id == NULL. */
554 if (id == NULL)
555 return 0;
556
557 intf = interface->cur_altsetting;
558 dev = interface_to_usbdev(interface);
559
560 if (!usb_match_device(dev, id))
561 return 0;
562
Alan Stern93c8bf42006-10-18 16:41:51 -0400563 /* The interface class, subclass, and protocol should never be
564 * checked for a match if the device class is Vendor Specific,
565 * unless the match record specifies the Vendor ID. */
566 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
567 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
568 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
569 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
570 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
571 return 0;
572
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800573 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
574 (id->bInterfaceClass != intf->desc.bInterfaceClass))
575 return 0;
576
577 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
578 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
579 return 0;
580
581 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
582 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
583 return 0;
584
585 return 1;
586}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100587EXPORT_SYMBOL_GPL(usb_match_one_id);
588
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800589/**
590 * usb_match_id - find first usb_device_id matching device or interface
591 * @interface: the interface of interest
592 * @id: array of usb_device_id structures, terminated by zero entry
593 *
594 * usb_match_id searches an array of usb_device_id's and returns
595 * the first one matching the device or interface, or null.
596 * This is used when binding (or rebinding) a driver to an interface.
597 * Most USB device drivers will use this indirectly, through the usb core,
598 * but some layered driver frameworks use it directly.
599 * These device tables are exported with MODULE_DEVICE_TABLE, through
600 * modutils, to support the driver loading functionality of USB hotplugging.
601 *
602 * What Matches:
603 *
604 * The "match_flags" element in a usb_device_id controls which
605 * members are used. If the corresponding bit is set, the
606 * value in the device_id must match its corresponding member
607 * in the device or interface descriptor, or else the device_id
608 * does not match.
609 *
610 * "driver_info" is normally used only by device drivers,
611 * but you can create a wildcard "matches anything" usb_device_id
612 * as a driver's "modules.usbmap" entry if you provide an id with
613 * only a nonzero "driver_info" field. If you do this, the USB device
614 * driver's probe() routine should use additional intelligence to
615 * decide whether to bind to the specified interface.
616 *
617 * What Makes Good usb_device_id Tables:
618 *
619 * The match algorithm is very simple, so that intelligence in
620 * driver selection must come from smart driver id records.
621 * Unless you have good reasons to use another selection policy,
622 * provide match elements only in related groups, and order match
623 * specifiers from specific to general. Use the macros provided
624 * for that purpose if you can.
625 *
626 * The most specific match specifiers use device descriptor
627 * data. These are commonly used with product-specific matches;
628 * the USB_DEVICE macro lets you provide vendor and product IDs,
629 * and you can also match against ranges of product revisions.
630 * These are widely used for devices with application or vendor
631 * specific bDeviceClass values.
632 *
633 * Matches based on device class/subclass/protocol specifications
634 * are slightly more general; use the USB_DEVICE_INFO macro, or
635 * its siblings. These are used with single-function devices
636 * where bDeviceClass doesn't specify that each interface has
637 * its own class.
638 *
639 * Matches based on interface class/subclass/protocol are the
640 * most general; they let drivers bind to any interface on a
641 * multiple-function device. Use the USB_INTERFACE_INFO
642 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400643 * devices (as recorded in bInterfaceClass).
644 *
645 * Note that an entry created by USB_INTERFACE_INFO won't match
646 * any interface if the device class is set to Vendor-Specific.
647 * This is deliberate; according to the USB spec the meanings of
648 * the interface class/subclass/protocol for these devices are also
649 * vendor-specific, and hence matching against a standard product
650 * class wouldn't work anyway. If you really want to use an
651 * interface-based match for such a device, create a match record
652 * that also specifies the vendor ID. (Unforunately there isn't a
653 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800654 *
655 * Within those groups, remember that not all combinations are
656 * meaningful. For example, don't give a product version range
657 * without vendor and product IDs; or specify a protocol without
658 * its associated class and subclass.
659 */
660const struct usb_device_id *usb_match_id(struct usb_interface *interface,
661 const struct usb_device_id *id)
662{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800663 /* proc_connectinfo in devio.c may call us with id == NULL. */
664 if (id == NULL)
665 return NULL;
666
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800667 /* It is important to check that id->driver_info is nonzero,
668 since an entry that is all zeroes except for a nonzero
669 id->driver_info is the way to create an entry that
670 indicates that the driver want to examine every
671 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800672 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
673 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800674 if (usb_match_one_id(interface, id))
675 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800676 }
677
678 return NULL;
679}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600680EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800681
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100682static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800683{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400684 /* devices and interfaces are handled separately */
685 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800686
Alan Stern8bb54ab2006-07-01 22:08:49 -0400687 /* interface drivers never match devices */
688 if (!is_usb_device_driver(drv))
689 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800690
Alan Stern8bb54ab2006-07-01 22:08:49 -0400691 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800692 return 1;
693
Kay Sievers55129662009-05-04 19:48:32 +0200694 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400695 struct usb_interface *intf;
696 struct usb_driver *usb_drv;
697 const struct usb_device_id *id;
698
699 /* device drivers never match interfaces */
700 if (is_usb_device_driver(drv))
701 return 0;
702
703 intf = to_usb_interface(dev);
704 usb_drv = to_usb_driver(drv);
705
706 id = usb_match_id(intf, usb_drv->id_table);
707 if (id)
708 return 1;
709
710 id = usb_match_dynamic_id(intf, usb_drv);
711 if (id)
712 return 1;
713 }
714
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800715 return 0;
716}
717
Alan Stern36e56a32006-07-01 22:08:06 -0400718#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200719static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400720{
Alan Stern36e56a32006-07-01 22:08:06 -0400721 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400722
Kay Sievers55129662009-05-04 19:48:32 +0200723 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400724 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200725 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100726 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200727
Alan Stern8bb54ab2006-07-01 22:08:49 -0400728 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200729 } else {
730 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400731 }
Alan Stern36e56a32006-07-01 22:08:06 -0400732
733 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500734 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200735 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400736 return -ENODEV;
737 }
738 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200739 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400740 return -ENODEV;
741 }
742
743#ifdef CONFIG_USB_DEVICEFS
744 /* If this is available, userspace programs can directly read
745 * all the device descriptors we don't tell them about. Or
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100746 * act as usermode drivers.
Alan Stern36e56a32006-07-01 22:08:06 -0400747 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200748 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
Alan Stern36e56a32006-07-01 22:08:06 -0400749 usb_dev->bus->busnum, usb_dev->devnum))
750 return -ENOMEM;
751#endif
752
753 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200754 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400755 le16_to_cpu(usb_dev->descriptor.idVendor),
756 le16_to_cpu(usb_dev->descriptor.idProduct),
757 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
758 return -ENOMEM;
759
760 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200761 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400762 usb_dev->descriptor.bDeviceClass,
763 usb_dev->descriptor.bDeviceSubClass,
764 usb_dev->descriptor.bDeviceProtocol))
765 return -ENOMEM;
766
Alan Stern36e56a32006-07-01 22:08:06 -0400767 return 0;
768}
769
770#else
771
Kay Sievers7eff2e72007-08-14 15:15:12 +0200772static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400773{
774 return -ENODEV;
775}
Alan Stern36e56a32006-07-01 22:08:06 -0400776#endif /* CONFIG_HOTPLUG */
777
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800778/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400779 * usb_register_device_driver - register a USB device (not interface) driver
780 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800781 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800782 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400783 * Registers a USB device driver with the USB core. The list of
784 * unattached devices will be rescanned whenever a new driver is
785 * added, allowing the new driver to attach to any recognized devices.
786 * Returns a negative error code on failure and 0 on success.
787 */
788int usb_register_device_driver(struct usb_device_driver *new_udriver,
789 struct module *owner)
790{
791 int retval = 0;
792
793 if (usb_disabled())
794 return -ENODEV;
795
796 new_udriver->drvwrap.for_devices = 1;
797 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
798 new_udriver->drvwrap.driver.bus = &usb_bus_type;
799 new_udriver->drvwrap.driver.probe = usb_probe_device;
800 new_udriver->drvwrap.driver.remove = usb_unbind_device;
801 new_udriver->drvwrap.driver.owner = owner;
802
803 retval = driver_register(&new_udriver->drvwrap.driver);
804
805 if (!retval) {
806 pr_info("%s: registered new device driver %s\n",
807 usbcore_name, new_udriver->name);
808 usbfs_update_special();
809 } else {
810 printk(KERN_ERR "%s: error %d registering device "
811 " driver %s\n",
812 usbcore_name, retval, new_udriver->name);
813 }
814
815 return retval;
816}
817EXPORT_SYMBOL_GPL(usb_register_device_driver);
818
819/**
820 * usb_deregister_device_driver - unregister a USB device (not interface) driver
821 * @udriver: USB operations of the device driver to unregister
822 * Context: must be able to sleep
823 *
824 * Unlinks the specified driver from the internal USB driver list.
825 */
826void usb_deregister_device_driver(struct usb_device_driver *udriver)
827{
828 pr_info("%s: deregistering device driver %s\n",
829 usbcore_name, udriver->name);
830
831 driver_unregister(&udriver->drvwrap.driver);
832 usbfs_update_special();
833}
834EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
835
836/**
837 * usb_register_driver - register a USB interface driver
838 * @new_driver: USB operations for the interface driver
839 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800840 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400841 *
842 * Registers a USB interface driver with the USB core. The list of
843 * unattached interfaces will be rescanned whenever a new driver is
844 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800845 * Returns a negative error code on failure and 0 on success.
846 *
847 * NOTE: if you want your driver to use the USB major number, you must call
848 * usb_register_dev() to enable that functionality. This function no longer
849 * takes care of that.
850 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800851int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
852 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800853{
854 int retval = 0;
855
856 if (usb_disabled())
857 return -ENODEV;
858
Alan Stern8bb54ab2006-07-01 22:08:49 -0400859 new_driver->drvwrap.for_devices = 0;
860 new_driver->drvwrap.driver.name = (char *) new_driver->name;
861 new_driver->drvwrap.driver.bus = &usb_bus_type;
862 new_driver->drvwrap.driver.probe = usb_probe_interface;
863 new_driver->drvwrap.driver.remove = usb_unbind_interface;
864 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800865 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800866 spin_lock_init(&new_driver->dynids.lock);
867 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800868
Alan Stern8bb54ab2006-07-01 22:08:49 -0400869 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800870 if (retval)
871 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800872
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800873 usbfs_update_special();
874
875 retval = usb_create_newid_file(new_driver);
876 if (retval)
877 goto out_newid;
878
879 retval = usb_create_removeid_file(new_driver);
880 if (retval)
881 goto out_removeid;
882
883 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800884 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800885
886out:
887 return retval;
888
889out_removeid:
890 usb_remove_newid_file(new_driver);
891out_newid:
892 driver_unregister(&new_driver->drvwrap.driver);
893
894 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400895 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800896 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800897 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800898}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600899EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800900
901/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400902 * usb_deregister - unregister a USB interface driver
903 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800904 * Context: must be able to sleep
905 *
906 * Unlinks the specified driver from the internal USB driver list.
907 *
908 * NOTE: If you called usb_register_dev(), you still need to call
909 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
910 * this * call will no longer do it for you.
911 */
912void usb_deregister(struct usb_driver *driver)
913{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400914 pr_info("%s: deregistering interface driver %s\n",
915 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800916
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800917 usb_remove_removeid_file(driver);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800918 usb_remove_newid_file(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800919 usb_free_dynids(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400920 driver_unregister(&driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800921
922 usbfs_update_special();
923}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600924EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400925
Alan Stern78d9a482008-06-23 16:00:40 -0400926/* Forced unbinding of a USB interface driver, either because
927 * it doesn't support pre_reset/post_reset/reset_resume or
928 * because it doesn't support suspend/resume.
929 *
930 * The caller must hold @intf's device's lock, but not its pm_mutex
931 * and not @intf->dev.sem.
932 */
933void usb_forced_unbind_intf(struct usb_interface *intf)
934{
935 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
936
937 dev_dbg(&intf->dev, "forced unbind\n");
938 usb_driver_release_interface(driver, intf);
939
940 /* Mark the interface for later rebinding */
941 intf->needs_binding = 1;
942}
943
944/* Delayed forced unbinding of a USB interface driver and scan
945 * for rebinding.
946 *
947 * The caller must hold @intf's device's lock, but not its pm_mutex
948 * and not @intf->dev.sem.
949 *
Alan Stern5096aed2008-08-12 14:34:14 -0400950 * Note: Rebinds will be skipped if a system sleep transition is in
951 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -0400952 */
953void usb_rebind_intf(struct usb_interface *intf)
954{
955 int rc;
956
957 /* Delayed unbind of an existing driver */
958 if (intf->dev.driver) {
959 struct usb_driver *driver =
960 to_usb_driver(intf->dev.driver);
961
962 dev_dbg(&intf->dev, "forced unbind\n");
963 usb_driver_release_interface(driver, intf);
964 }
965
966 /* Try to rebind the interface */
Alan Sternf76b1682011-06-18 20:22:23 +0200967 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -0400968 intf->needs_binding = 0;
969 rc = device_attach(&intf->dev);
970 if (rc < 0)
971 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
972 }
Alan Stern78d9a482008-06-23 16:00:40 -0400973}
974
Alan Stern9ff78432008-08-07 13:04:51 -0400975#ifdef CONFIG_PM
976
Alan Stern78d9a482008-06-23 16:00:40 -0400977#define DO_UNBIND 0
978#define DO_REBIND 1
979
980/* Unbind drivers for @udev's interfaces that don't support suspend/resume,
981 * or rebind interfaces that have been unbound, according to @action.
982 *
983 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -0400984 */
985static void do_unbind_rebind(struct usb_device *udev, int action)
986{
987 struct usb_host_config *config;
988 int i;
989 struct usb_interface *intf;
990 struct usb_driver *drv;
991
992 config = udev->actconfig;
993 if (config) {
994 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
995 intf = config->interface[i];
996 switch (action) {
997 case DO_UNBIND:
998 if (intf->dev.driver) {
999 drv = to_usb_driver(intf->dev.driver);
1000 if (!drv->suspend || !drv->resume)
1001 usb_forced_unbind_intf(intf);
1002 }
1003 break;
1004 case DO_REBIND:
Alan Stern5096aed2008-08-12 14:34:14 -04001005 if (intf->needs_binding)
Alan Stern78d9a482008-06-23 16:00:40 -04001006 usb_rebind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001007 break;
1008 }
1009 }
1010 }
1011}
1012
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001013static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001014{
Alan Stern782da722006-07-01 22:09:35 -04001015 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001016 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001017
Alan Stern114b3682006-07-01 22:13:04 -04001018 if (udev->state == USB_STATE_NOTATTACHED ||
1019 udev->state == USB_STATE_SUSPENDED)
1020 goto done;
1021
Alan Sternb6f6436d2007-05-04 11:51:54 -04001022 /* For devices that don't have a driver, we do a generic suspend. */
1023 if (udev->dev.driver)
1024 udriver = to_usb_device_driver(udev->dev.driver);
1025 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001026 udev->do_remote_wakeup = 0;
Alan Sternb6f6436d2007-05-04 11:51:54 -04001027 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001028 }
Alan Stern2bf40862006-07-01 22:12:19 -04001029 status = udriver->suspend(udev, msg);
1030
Alan Stern20dfdad2007-05-22 11:50:17 -04001031 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001032 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001033 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001034}
Alan Stern36e56a32006-07-01 22:08:06 -04001035
Alan Stern65bfd292008-11-25 16:39:18 -05001036static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001037{
1038 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001039 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001040
Alan Stern0458d5b2007-05-04 11:52:20 -04001041 if (udev->state == USB_STATE_NOTATTACHED)
1042 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001043
Alan Stern1c5df7e2006-07-01 22:13:50 -04001044 /* Can't resume it if it doesn't have a driver. */
1045 if (udev->dev.driver == NULL) {
1046 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001047 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001048 }
1049
Alan Stern6d19c002010-02-12 12:21:11 +01001050 /* Non-root devices on a full/low-speed bus must wait for their
1051 * companion high-speed root hub, in case a handoff is needed.
1052 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001053 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001054 device_pm_wait_for_dev(&udev->dev,
1055 &udev->bus->hs_companion->root_hub->dev);
1056
Alan Stern6bc6cff2007-05-04 11:53:03 -04001057 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1058 udev->reset_resume = 1;
1059
Alan Stern1cc8a252006-07-01 22:10:15 -04001060 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001061 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001062
Alan Stern20dfdad2007-05-22 11:50:17 -04001063 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001064 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001065 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001066}
1067
Alan Stern65605ae2008-08-12 14:33:27 -04001068static int usb_suspend_interface(struct usb_device *udev,
1069 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001070{
1071 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001072 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001073
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001074 if (udev->state == USB_STATE_NOTATTACHED ||
1075 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001076 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001077 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001078
Oliver Neukume78832c2012-01-02 15:11:48 +01001079 /* at this time we know the driver supports suspend */
1080 status = driver->suspend(intf, msg);
1081 if (status && !PMSG_IS_AUTO(msg))
1082 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001083
Alan Stern20dfdad2007-05-22 11:50:17 -04001084 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001085 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001086 return status;
1087}
1088
Alan Stern65605ae2008-08-12 14:33:27 -04001089static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001090 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001091{
Alan Stern1cc8a252006-07-01 22:10:15 -04001092 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001093 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001094
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001095 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001096 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001097
Alan Stern645daaa2006-08-30 15:47:02 -04001098 /* Don't let autoresume interfere with unbinding */
1099 if (intf->condition == USB_INTERFACE_UNBINDING)
1100 goto done;
1101
Alan Stern1c5df7e2006-07-01 22:13:50 -04001102 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001103 if (intf->condition == USB_INTERFACE_UNBOUND) {
1104
1105 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b1682011-06-18 20:22:23 +02001106 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001107 usb_set_interface(udev, intf->altsetting[0].
1108 desc.bInterfaceNumber, 0);
1109 intf->needs_altsetting0 = 0;
1110 }
Alan Stern2bf40862006-07-01 22:12:19 -04001111 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001112 }
Alan Stern78d9a482008-06-23 16:00:40 -04001113
1114 /* Don't resume if the interface is marked for rebinding */
1115 if (intf->needs_binding)
1116 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001117 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001118
Alan Sternf07600c2007-05-30 15:38:16 -04001119 if (reset_resume) {
1120 if (driver->reset_resume) {
1121 status = driver->reset_resume(intf);
1122 if (status)
1123 dev_err(&intf->dev, "%s error %d\n",
1124 "reset_resume", status);
1125 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001126 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001127 dev_warn(&intf->dev, "no %s for driver %s?\n",
1128 "reset_resume", driver->name);
1129 }
1130 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001131 status = driver->resume(intf);
1132 if (status)
1133 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001134 }
Alan Stern2bf40862006-07-01 22:12:19 -04001135
1136done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001137 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001138
Alan Stern78d9a482008-06-23 16:00:40 -04001139 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001140 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001141}
1142
Alan Stern645daaa2006-08-30 15:47:02 -04001143/**
1144 * usb_suspend_both - suspend a USB device and its interfaces
1145 * @udev: the usb_device to suspend
1146 * @msg: Power Management message describing this state transition
1147 *
1148 * This is the central routine for suspending USB devices. It calls the
1149 * suspend methods for all the interface drivers in @udev and then calls
1150 * the suspend method for @udev itself. If an error occurs at any stage,
1151 * all the interfaces which were suspended are resumed so that they remain
1152 * in the same state as the device.
1153 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001154 * Autosuspend requests originating from a child device or an interface
1155 * driver may be made without the protection of @udev's device lock, but
1156 * all other suspend calls will hold the lock. Usbcore will insure that
1157 * method calls do not arrive during bind, unbind, or reset operations.
1158 * However drivers must be prepared to handle suspend calls arriving at
1159 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001160 *
1161 * This routine can run only in process context.
1162 */
Alan Stern718efa62007-03-09 15:41:13 -05001163static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001164{
1165 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001166 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001167 struct usb_interface *intf;
1168
Alan Stern19410442007-03-27 13:33:59 -04001169 if (udev->state == USB_STATE_NOTATTACHED ||
1170 udev->state == USB_STATE_SUSPENDED)
1171 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001172
Alan Stern645daaa2006-08-30 15:47:02 -04001173 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001174 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001175 n = udev->actconfig->desc.bNumInterfaces;
1176 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001177 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001178 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001179
1180 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001181 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001182 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001183 if (status != 0)
1184 break;
1185 }
1186 }
Alan Stern0af212b2011-06-15 16:27:43 -04001187 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001188 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001189
Alan Stern0af212b2011-06-15 16:27:43 -04001190 /* Again, ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001191 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001192 status = 0;
1193 }
1194
Alan Sterna8e7c562006-07-01 22:11:02 -04001195 /* If the suspend failed, resume interfaces that did get suspended */
1196 if (status != 0) {
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001197 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
Alan Stern571dc792010-04-09 16:03:43 -04001198 while (++i < n) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001199 intf = udev->actconfig->interface[i];
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001200 usb_resume_interface(udev, intf, msg, 0);
Alan Sterna8e7c562006-07-01 22:11:02 -04001201 }
Alan Stern645daaa2006-08-30 15:47:02 -04001202
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001203 /* If the suspend succeeded then prevent any more URB submissions
1204 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001205 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001206 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001207 udev->can_submit = 0;
1208 for (i = 0; i < 16; ++i) {
1209 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1210 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1211 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001212 }
Alan Stern645daaa2006-08-30 15:47:02 -04001213
Alan Stern19410442007-03-27 13:33:59 -04001214 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001215 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001216 return status;
1217}
1218
Alan Stern645daaa2006-08-30 15:47:02 -04001219/**
1220 * usb_resume_both - resume a USB device and its interfaces
1221 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001222 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001223 *
1224 * This is the central routine for resuming USB devices. It calls the
1225 * the resume method for @udev and then calls the resume methods for all
1226 * the interface drivers in @udev.
1227 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001228 * Autoresume requests originating from a child device or an interface
1229 * driver may be made without the protection of @udev's device lock, but
1230 * all other resume calls will hold the lock. Usbcore will insure that
1231 * method calls do not arrive during bind, unbind, or reset operations.
1232 * However drivers must be prepared to handle resume calls arriving at
1233 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001234 *
1235 * This routine can run only in process context.
1236 */
Alan Stern65bfd292008-11-25 16:39:18 -05001237static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001238{
Alan Stern645daaa2006-08-30 15:47:02 -04001239 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001240 int i;
1241 struct usb_interface *intf;
1242
Alan Stern19410442007-03-27 13:33:59 -04001243 if (udev->state == USB_STATE_NOTATTACHED) {
1244 status = -ENODEV;
1245 goto done;
1246 }
Alan Stern6840d252007-09-10 11:34:26 -04001247 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001248
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001249 /* Resume the device */
1250 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001251 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001252
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001253 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001254 if (status == 0 && udev->actconfig) {
1255 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1256 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001257 usb_resume_interface(udev, intf, msg,
1258 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001259 }
1260 }
Alan Sternc08512c2010-11-15 15:57:58 -05001261 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001262
Alan Stern19410442007-03-27 13:33:59 -04001263 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001264 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001265 if (!status)
1266 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001267 return status;
1268}
1269
Alan Stern5f677f12010-04-02 13:20:11 -04001270static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1271{
Alan Stern48826622010-06-22 16:14:48 -04001272 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001273
1274 /* Remote wakeup is needed only when we actually go to sleep.
1275 * For things like FREEZE and QUIESCE, if the device is already
1276 * autosuspended then its current wakeup setting is okay.
1277 */
1278 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1279 if (udev->state != USB_STATE_SUSPENDED)
1280 udev->do_remote_wakeup = 0;
1281 return;
1282 }
1283
Alan Stern48826622010-06-22 16:14:48 -04001284 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001285 * actually want it.
1286 */
Alan Stern48826622010-06-22 16:14:48 -04001287 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001288
1289 /* If the device is autosuspended with the wrong wakeup setting,
1290 * autoresume now so the setting can be changed.
1291 */
1292 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1293 pm_runtime_resume(&udev->dev);
1294 udev->do_remote_wakeup = w;
1295}
1296
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001297/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001298int usb_suspend(struct device *dev, pm_message_t msg)
1299{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001300 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001301
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001302 do_unbind_rebind(udev, DO_UNBIND);
Alan Stern5f677f12010-04-02 13:20:11 -04001303 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001304 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001305}
1306
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001307/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001308int usb_resume(struct device *dev, pm_message_t msg)
1309{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001310 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001311 int status;
1312
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001313 /* For PM complete calls, all we do is rebind interfaces */
1314 if (msg.event == PM_EVENT_ON) {
1315 if (udev->state != USB_STATE_NOTATTACHED)
1316 do_unbind_rebind(udev, DO_REBIND);
1317 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001318
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001319 /* For all other calls, take the device back to full power and
1320 * tell the PM core in case it was autosuspended previously.
Alan Sternc043f122010-06-04 14:02:42 -04001321 * Unbind the interfaces that will need rebinding later.
Alan Stern0c590e22010-01-08 12:57:14 -05001322 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001323 } else {
1324 status = usb_resume_both(udev, msg);
1325 if (status == 0) {
1326 pm_runtime_disable(dev);
1327 pm_runtime_set_active(dev);
1328 pm_runtime_enable(dev);
Alan Sternc043f122010-06-04 14:02:42 -04001329 do_unbind_rebind(udev, DO_REBIND);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001330 }
1331 }
Alan Stern0c590e22010-01-08 12:57:14 -05001332
1333 /* Avoid PM error messages for devices disconnected while suspended
1334 * as we'll display regular disconnect messages just a bit later.
1335 */
Peter Chen7491f132010-09-27 16:43:25 +08001336 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001337 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001338 return status;
1339}
1340
1341#endif /* CONFIG_PM */
1342
Alan Stern645daaa2006-08-30 15:47:02 -04001343#ifdef CONFIG_USB_SUSPEND
1344
Alan Stern088f7fe2010-01-08 12:56:54 -05001345/**
1346 * usb_enable_autosuspend - allow a USB device to be autosuspended
1347 * @udev: the USB device which may be autosuspended
1348 *
1349 * This routine allows @udev to be autosuspended. An autosuspend won't
1350 * take place until the autosuspend_delay has elapsed and all the other
1351 * necessary conditions are satisfied.
1352 *
1353 * The caller must hold @udev's device lock.
1354 */
Alan Stern9e18c822010-04-02 13:22:09 -04001355void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001356{
Alan Stern9e18c822010-04-02 13:22:09 -04001357 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001358}
1359EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1360
1361/**
1362 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1363 * @udev: the USB device which may not be autosuspended
1364 *
1365 * This routine prevents @udev from being autosuspended and wakes it up
1366 * if it is already autosuspended.
1367 *
1368 * The caller must hold @udev's device lock.
1369 */
Alan Stern9e18c822010-04-02 13:22:09 -04001370void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001371{
Alan Stern9e18c822010-04-02 13:22:09 -04001372 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001373}
1374EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1375
Alan Stern645daaa2006-08-30 15:47:02 -04001376/**
1377 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001378 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001379 *
1380 * This routine should be called when a core subsystem is finished using
1381 * @udev and wants to allow it to autosuspend. Examples would be when
1382 * @udev's device file in usbfs is closed or after a configuration change.
1383 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001384 * @udev's usage counter is decremented; if it drops to 0 and all the
1385 * interfaces are inactive then a delayed autosuspend will be attempted.
1386 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001387 *
Alan Stern62e299e2010-01-08 12:56:19 -05001388 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001389 *
1390 * This routine can run only in process context.
1391 */
Alan Stern94fcda12006-11-20 11:38:46 -05001392void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001393{
Alan Stern94fcda12006-11-20 11:38:46 -05001394 int status;
1395
Ming Lei6ddf27c2010-11-15 15:57:30 -05001396 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001397 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001398 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1399 __func__, atomic_read(&udev->dev.power.usage_count),
1400 status);
Alan Stern19c26232007-02-20 15:03:32 -05001401}
1402
1403/**
Alan Stern645daaa2006-08-30 15:47:02 -04001404 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001405 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001406 *
1407 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001408 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001409 * occur until usb_autosuspend_device() is called. (Note that this will
1410 * not prevent suspend events originating in the PM core.) Examples would
1411 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001412 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001413 *
Alan Stern94fcda12006-11-20 11:38:46 -05001414 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1415 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001416 *
Alan Stern62e299e2010-01-08 12:56:19 -05001417 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001418 *
1419 * This routine can run only in process context.
1420 */
Alan Stern94fcda12006-11-20 11:38:46 -05001421int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001422{
1423 int status;
1424
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001425 status = pm_runtime_get_sync(&udev->dev);
1426 if (status < 0)
1427 pm_runtime_put_sync(&udev->dev);
1428 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1429 __func__, atomic_read(&udev->dev.power.usage_count),
1430 status);
1431 if (status > 0)
1432 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001433 return status;
1434}
1435
Alan Stern645daaa2006-08-30 15:47:02 -04001436/**
1437 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001438 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001439 *
1440 * This routine should be called by an interface driver when it is
1441 * finished using @intf and wants to allow it to autosuspend. A typical
1442 * example would be a character-device driver when its device file is
1443 * closed.
1444 *
1445 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001446 * 0, a delayed autosuspend request for @intf's device is attempted. The
1447 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001448 *
Alan Stern645daaa2006-08-30 15:47:02 -04001449 * This routine can run only in process context.
1450 */
1451void usb_autopm_put_interface(struct usb_interface *intf)
1452{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001453 struct usb_device *udev = interface_to_usbdev(intf);
1454 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001455
Ming Lei6ddf27c2010-11-15 15:57:30 -05001456 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001457 atomic_dec(&intf->pm_usage_cnt);
1458 status = pm_runtime_put_sync(&intf->dev);
1459 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1460 __func__, atomic_read(&intf->dev.power.usage_count),
1461 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001462}
1463EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1464
1465/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001466 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1467 * @intf: the usb_interface whose counter should be decremented
1468 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001469 * This routine does much the same thing as usb_autopm_put_interface():
1470 * It decrements @intf's usage counter and schedules a delayed
1471 * autosuspend request if the counter is <= 0. The difference is that it
1472 * does not perform any synchronization; callers should hold a private
1473 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001474 *
1475 * Typically a driver would call this routine during an URB's completion
1476 * handler, if no more URBs were pending.
1477 *
1478 * This routine can run in atomic context.
1479 */
1480void usb_autopm_put_interface_async(struct usb_interface *intf)
1481{
1482 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001483 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001484
Ming Lei6ddf27c2010-11-15 15:57:30 -05001485 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001486 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001487 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001488 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1489 __func__, atomic_read(&intf->dev.power.usage_count),
1490 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001491}
1492EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1493
1494/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001495 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1496 * @intf: the usb_interface whose counter should be decremented
1497 *
1498 * This routine decrements @intf's usage counter but does not carry out an
1499 * autosuspend.
1500 *
1501 * This routine can run in atomic context.
1502 */
1503void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1504{
1505 struct usb_device *udev = interface_to_usbdev(intf);
1506
Ming Lei6ddf27c2010-11-15 15:57:30 -05001507 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001508 atomic_dec(&intf->pm_usage_cnt);
1509 pm_runtime_put_noidle(&intf->dev);
1510}
1511EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1512
1513/**
Alan Stern645daaa2006-08-30 15:47:02 -04001514 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001515 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001516 *
1517 * This routine should be called by an interface driver when it wants to
1518 * use @intf and needs to guarantee that it is not suspended. In addition,
1519 * the routine prevents @intf from being autosuspended subsequently. (Note
1520 * that this will not prevent suspend events originating in the PM core.)
1521 * This prevention will persist until usb_autopm_put_interface() is called
1522 * or @intf is unbound. A typical example would be a character-device
1523 * driver when its device file is opened.
1524 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001525 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1526 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001527 *
1528 * This routine can run only in process context.
1529 */
1530int usb_autopm_get_interface(struct usb_interface *intf)
1531{
Alan Sternaf4f7602006-10-30 17:06:45 -05001532 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001533
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001534 status = pm_runtime_get_sync(&intf->dev);
1535 if (status < 0)
1536 pm_runtime_put_sync(&intf->dev);
1537 else
1538 atomic_inc(&intf->pm_usage_cnt);
1539 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1540 __func__, atomic_read(&intf->dev.power.usage_count),
1541 status);
1542 if (status > 0)
1543 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001544 return status;
1545}
1546EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1547
Alan Stern692a1862006-10-30 17:07:51 -05001548/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001549 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1550 * @intf: the usb_interface whose counter should be incremented
1551 *
1552 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001553 * usb_autopm_get_interface(): It increments @intf's usage counter and
1554 * queues an autoresume request if the device is suspended. The
1555 * differences are that it does not perform any synchronization (callers
1556 * should hold a private lock and handle all synchronization issues
1557 * themselves), and it does not autoresume the device directly (it only
1558 * queues a request). After a successful call, the device may not yet be
1559 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001560 *
1561 * This routine can run in atomic context.
1562 */
1563int usb_autopm_get_interface_async(struct usb_interface *intf)
1564{
Ming Lei63defa72010-11-15 15:56:54 -05001565 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001566
Ming Lei63defa72010-11-15 15:56:54 -05001567 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001568 if (status < 0 && status != -EINPROGRESS)
1569 pm_runtime_put_noidle(&intf->dev);
1570 else
Alan Sternccf5b802009-06-29 11:00:01 -04001571 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001572 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1573 __func__, atomic_read(&intf->dev.power.usage_count),
1574 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001575 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001576 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001577 return status;
1578}
1579EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1580
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001581/**
1582 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1583 * @intf: the usb_interface whose counter should be incremented
1584 *
1585 * This routine increments @intf's usage counter but does not carry out an
1586 * autoresume.
1587 *
1588 * This routine can run in atomic context.
1589 */
1590void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1591{
1592 struct usb_device *udev = interface_to_usbdev(intf);
1593
Ming Lei6ddf27c2010-11-15 15:57:30 -05001594 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001595 atomic_inc(&intf->pm_usage_cnt);
1596 pm_runtime_get_noresume(&intf->dev);
1597}
1598EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1599
1600/* Internal routine to check whether we may autosuspend a device. */
1601static int autosuspend_check(struct usb_device *udev)
1602{
Alan Stern7560d322010-04-02 13:18:50 -04001603 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001604 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001605
1606 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1607 * any interface drivers require remote wakeup but it isn't available.
1608 */
Alan Stern7560d322010-04-02 13:18:50 -04001609 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001610 if (udev->actconfig) {
1611 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1612 intf = udev->actconfig->interface[i];
1613
1614 /* We don't need to check interfaces that are
1615 * disabled for runtime PM. Either they are unbound
1616 * or else their drivers don't support autosuspend
1617 * and so they are permanently active.
1618 */
1619 if (intf->dev.power.disable_depth)
1620 continue;
1621 if (atomic_read(&intf->dev.power.usage_count) > 0)
1622 return -EBUSY;
Alan Stern7560d322010-04-02 13:18:50 -04001623 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001624
1625 /* Don't allow autosuspend if the device will need
1626 * a reset-resume and any of its interface drivers
1627 * doesn't include support or needs remote wakeup.
1628 */
1629 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1630 struct usb_driver *driver;
1631
1632 driver = to_usb_driver(intf->dev.driver);
1633 if (!driver->reset_resume ||
1634 intf->needs_remote_wakeup)
1635 return -EOPNOTSUPP;
1636 }
1637 }
1638 }
Alan Stern7560d322010-04-02 13:18:50 -04001639 if (w && !device_can_wakeup(&udev->dev)) {
1640 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1641 return -EOPNOTSUPP;
1642 }
1643 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001644 return 0;
1645}
1646
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001647int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001648{
Ming Lei63defa72010-11-15 15:56:54 -05001649 struct usb_device *udev = to_usb_device(dev);
1650 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001651
1652 /* A USB device can be suspended if it passes the various autosuspend
1653 * checks. Runtime suspend for a USB device means suspending all the
1654 * interfaces and then the device itself.
1655 */
Ming Lei63defa72010-11-15 15:56:54 -05001656 if (autosuspend_check(udev) != 0)
1657 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001658
Ming Lei63defa72010-11-15 15:56:54 -05001659 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001660
1661 /* Allow a retry if autosuspend failed temporarily */
1662 if (status == -EAGAIN || status == -EBUSY)
1663 usb_mark_last_busy(udev);
1664
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001665 /* The PM core reacts badly unless the return code is 0,
1666 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1667 */
1668 if (status != 0)
1669 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001670 return status;
1671}
1672
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001673int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001674{
Ming Lei63defa72010-11-15 15:56:54 -05001675 struct usb_device *udev = to_usb_device(dev);
1676 int status;
1677
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001678 /* Runtime resume for a USB device means resuming both the device
1679 * and all its interfaces.
1680 */
Ming Lei63defa72010-11-15 15:56:54 -05001681 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001682 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001683}
1684
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001685int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001686{
Ming Lei63defa72010-11-15 15:56:54 -05001687 struct usb_device *udev = to_usb_device(dev);
1688
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001689 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001690 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001691 */
Ming Lei63defa72010-11-15 15:56:54 -05001692 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001693 pm_runtime_autosuspend(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001694 return 0;
1695}
1696
Andiry Xu65580b432011-09-23 14:19:52 -07001697int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1698{
1699 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1700 int ret = -EPERM;
1701
1702 if (hcd->driver->set_usb2_hw_lpm) {
1703 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1704 if (!ret)
1705 udev->usb2_hw_lpm_enabled = enable;
1706 }
1707
1708 return ret;
1709}
1710
Alan Stern645daaa2006-08-30 15:47:02 -04001711#endif /* CONFIG_USB_SUSPEND */
1712
Alan Stern36e56a32006-07-01 22:08:06 -04001713struct bus_type usb_bus_type = {
1714 .name = "usb",
1715 .match = usb_device_match,
1716 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001717};