blob: f536aebc958e71d459f71ee5b9ae19f717a851c6 [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
Bjørn Morkef206f32012-05-13 12:35:00 +020082ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
Bjørn Morke6bbcef2012-05-13 12:34:59 +020083{
84 struct usb_dynid *dynid;
Bjørn Morke6bbcef2012-05-13 12:34:59 +020085 size_t count = 0;
86
Bjørn Morkef206f32012-05-13 12:35:00 +020087 list_for_each_entry(dynid, &dynids->list, node)
Bjørn Morke6bbcef2012-05-13 12:34:59 +020088 if (dynid->id.bInterfaceClass != 0)
89 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
90 dynid->id.idVendor, dynid->id.idProduct,
91 dynid->id.bInterfaceClass);
92 else
93 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
94 dynid->id.idVendor, dynid->id.idProduct);
95 return count;
96}
Bjørn Morkef206f32012-05-13 12:35:00 +020097EXPORT_SYMBOL_GPL(usb_show_dynids);
98
99static ssize_t show_dynids(struct device_driver *driver, char *buf)
100{
101 struct usb_driver *usb_drv = to_usb_driver(driver);
102
103 return usb_show_dynids(&usb_drv->dynids, buf);
104}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200105
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100106static ssize_t store_new_id(struct device_driver *driver,
107 const char *buf, size_t count)
108{
109 struct usb_driver *usb_drv = to_usb_driver(driver);
110
111 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
112}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200113static DRIVER_ATTR(new_id, S_IRUGO | S_IWUSR, show_dynids, store_new_id);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800114
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800115/**
116 * store_remove_id - remove a USB device ID from this driver
117 * @driver: target device driver
118 * @buf: buffer for scanning device ID data
119 * @count: input size
120 *
121 * Removes a dynamic usb device ID from this driver.
122 */
123static ssize_t
124store_remove_id(struct device_driver *driver, const char *buf, size_t count)
125{
126 struct usb_dynid *dynid, *n;
127 struct usb_driver *usb_driver = to_usb_driver(driver);
128 u32 idVendor = 0;
129 u32 idProduct = 0;
130 int fields = 0;
131 int retval = 0;
132
133 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
134 if (fields < 2)
135 return -EINVAL;
136
137 spin_lock(&usb_driver->dynids.lock);
138 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
139 struct usb_device_id *id = &dynid->id;
140 if ((id->idVendor == idVendor) &&
141 (id->idProduct == idProduct)) {
142 list_del(&dynid->node);
143 kfree(dynid);
144 retval = 0;
145 break;
146 }
147 }
148 spin_unlock(&usb_driver->dynids.lock);
149
150 if (retval)
151 return retval;
152 return count;
153}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200154static DRIVER_ATTR(remove_id, S_IRUGO | S_IWUSR, show_dynids, store_remove_id);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800155
Alan Sterned283e92012-01-24 14:35:13 -0500156static int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800157{
158 int error = 0;
159
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800160 if (usb_drv->no_dynamic_id)
161 goto exit;
162
Alan Sterned283e92012-01-24 14:35:13 -0500163 if (usb_drv->probe != NULL) {
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800164 error = driver_create_file(&usb_drv->drvwrap.driver,
165 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500166 if (error == 0) {
167 error = driver_create_file(&usb_drv->drvwrap.driver,
168 &driver_attr_remove_id);
169 if (error)
170 driver_remove_file(&usb_drv->drvwrap.driver,
171 &driver_attr_new_id);
172 }
173 }
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800174exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800175 return error;
176}
177
Alan Sterned283e92012-01-24 14:35:13 -0500178static void usb_remove_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800179{
180 if (usb_drv->no_dynamic_id)
181 return;
182
Alan Sterned283e92012-01-24 14:35:13 -0500183 if (usb_drv->probe != NULL) {
184 driver_remove_file(&usb_drv->drvwrap.driver,
185 &driver_attr_remove_id);
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800186 driver_remove_file(&usb_drv->drvwrap.driver,
187 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500188 }
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800189}
190
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800191static void usb_free_dynids(struct usb_driver *usb_drv)
192{
193 struct usb_dynid *dynid, *n;
194
195 spin_lock(&usb_drv->dynids.lock);
196 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
197 list_del(&dynid->node);
198 kfree(dynid);
199 }
200 spin_unlock(&usb_drv->dynids.lock);
201}
202#else
Alan Sterned283e92012-01-24 14:35:13 -0500203static inline int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800204{
205 return 0;
206}
207
Alan Sterned283e92012-01-24 14:35:13 -0500208static void usb_remove_newid_files(struct usb_driver *usb_drv)
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800209{
210}
211
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800212static inline void usb_free_dynids(struct usb_driver *usb_drv)
213{
214}
215#endif
216
217static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
218 struct usb_driver *drv)
219{
220 struct usb_dynid *dynid;
221
222 spin_lock(&drv->dynids.lock);
223 list_for_each_entry(dynid, &drv->dynids.list, node) {
224 if (usb_match_one_id(intf, &dynid->id)) {
225 spin_unlock(&drv->dynids.lock);
226 return &dynid->id;
227 }
228 }
229 spin_unlock(&drv->dynids.lock);
230 return NULL;
231}
232
233
Alan Stern8bb54ab2006-07-01 22:08:49 -0400234/* called from driver core with dev locked */
235static int usb_probe_device(struct device *dev)
236{
237 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200238 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500239 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400240
Harvey Harrison441b62c2008-03-03 16:08:34 -0800241 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400242
Alan Stern8bb54ab2006-07-01 22:08:49 -0400243 /* TODO: Add real matching code */
244
Alan Stern645daaa2006-08-30 15:47:02 -0400245 /* The device should always appear to be in use
246 * unless the driver suports autosuspend.
247 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500248 if (!udriver->supports_autosuspend)
249 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400250
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500251 if (!error)
252 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400253 return error;
254}
255
256/* called from driver core with dev locked */
257static int usb_unbind_device(struct device *dev)
258{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500259 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400260 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
261
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500262 udriver->disconnect(udev);
263 if (!udriver->supports_autosuspend)
264 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400265 return 0;
266}
267
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800268/*
269 * Cancel any pending scheduled resets
270 *
271 * [see usb_queue_reset_device()]
272 *
273 * Called after unconfiguring / when releasing interfaces. See
274 * comments in __usb_queue_reset_device() regarding
275 * udev->reset_running.
276 */
277static void usb_cancel_queued_reset(struct usb_interface *iface)
278{
279 if (iface->reset_running == 0)
280 cancel_work_sync(&iface->reset_ws);
281}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400282
283/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800284static int usb_probe_interface(struct device *dev)
285{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400286 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200287 struct usb_interface *intf = to_usb_interface(dev);
288 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800289 const struct usb_device_id *id;
290 int error = -ENODEV;
Sarah Sharp83060952012-05-02 14:25:52 -0700291 int lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800292
Harvey Harrison441b62c2008-03-03 16:08:34 -0800293 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800294
Alan Stern78d9a482008-06-23 16:00:40 -0400295 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800296
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400297 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500298 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400299
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800300 if (udev->authorized == 0) {
301 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500302 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800303 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700304
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800305 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800306 if (!id)
307 id = usb_match_dynamic_id(intf, driver);
Alan Stern0f3dda92010-01-08 12:56:04 -0500308 if (!id)
309 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800310
Alan Stern0f3dda92010-01-08 12:56:04 -0500311 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400312
Alan Stern0f3dda92010-01-08 12:56:04 -0500313 error = usb_autoresume_device(udev);
314 if (error)
315 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400316
Alan Stern0f3dda92010-01-08 12:56:04 -0500317 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400318
Alan Stern571dc792010-04-09 16:03:43 -0400319 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500320 * runtime-PM-enabled only if the driver has autosuspend support.
321 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500322 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500323 pm_runtime_set_active(dev);
324 pm_suspend_ignore_children(dev, false);
325 if (driver->supports_autosuspend)
326 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200327
Sarah Sharp83060952012-05-02 14:25:52 -0700328 /* If the new driver doesn't allow hub-initiated LPM, and we can't
329 * disable hub-initiated LPM, then fail the probe.
330 *
331 * Otherwise, leaving LPM enabled should be harmless, because the
332 * endpoint intervals should remain the same, and the U1/U2 timeouts
333 * should remain the same.
334 *
335 * If we need to install alt setting 0 before probe, or another alt
336 * setting during probe, that should also be fine. usb_set_interface()
337 * will attempt to disable LPM, and fail if it can't disable it.
338 */
339 lpm_disable_error = usb_unlocked_disable_lpm(udev);
340 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
341 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
342 __func__, driver->name);
343 error = lpm_disable_error;
344 goto err;
345 }
346
Alan Stern0f3dda92010-01-08 12:56:04 -0500347 /* Carry out a deferred switch to altsetting 0 */
348 if (intf->needs_altsetting0) {
349 error = usb_set_interface(udev, intf->altsetting[0].
350 desc.bInterfaceNumber, 0);
351 if (error < 0)
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200352 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500353 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800354 }
355
Alan Stern0f3dda92010-01-08 12:56:04 -0500356 error = driver->probe(intf, id);
357 if (error)
358 goto err;
359
360 intf->condition = USB_INTERFACE_BOUND;
Sarah Sharp83060952012-05-02 14:25:52 -0700361
362 /* If the LPM disable succeeded, balance the ref counts. */
363 if (!lpm_disable_error)
364 usb_unlocked_enable_lpm(udev);
365
Alan Stern0f3dda92010-01-08 12:56:04 -0500366 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800367 return error;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200368
Alan Stern0f3dda92010-01-08 12:56:04 -0500369 err:
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200370 intf->needs_remote_wakeup = 0;
371 intf->condition = USB_INTERFACE_UNBOUND;
372 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500373
374 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400375 if (driver->supports_autosuspend)
376 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500377 pm_runtime_set_suspended(dev);
378
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200379 usb_autosuspend_device(udev);
380 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800381}
382
Alan Stern8bb54ab2006-07-01 22:08:49 -0400383/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800384static int usb_unbind_interface(struct device *dev)
385{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400386 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800387 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400388 struct usb_device *udev;
Sarah Sharp83060952012-05-02 14:25:52 -0700389 int error, r, lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800390
391 intf->condition = USB_INTERFACE_UNBINDING;
392
Alan Stern645daaa2006-08-30 15:47:02 -0400393 /* Autoresume for set_interface call below */
394 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500395 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400396
Sarah Sharp83060952012-05-02 14:25:52 -0700397 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
398 * the driver is unbound. If LPM isn't disabled, that's fine because it
399 * wouldn't be enabled unless all the bound interfaces supported
400 * hub-initiated LPM.
401 */
402 lpm_disable_error = usb_unlocked_disable_lpm(udev);
403
Alan Stern9da82bd2008-05-08 11:54:37 -0400404 /* Terminate all URBs for this interface unless the driver
405 * supports "soft" unbinding.
406 */
407 if (!driver->soft_unbind)
Alan Sternddeac4e2009-01-15 17:03:33 -0500408 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800409
Alan Stern8bb54ab2006-07-01 22:08:49 -0400410 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800411 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800412
Alan Stern55151d72008-08-12 14:33:59 -0400413 /* Reset other interface state.
414 * We cannot do a Set-Interface if the device is suspended or
415 * if it is prepared for a system sleep (since installing a new
416 * altsetting means creating new endpoint device entries).
417 * When either of these happens, defer the Set-Interface.
418 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500419 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
420 /* Already in altsetting 0 so skip Set-Interface.
421 * Just re-enable it without affecting the endpoint toggles.
422 */
423 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200424 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200425 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400426 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200427 if (r < 0)
428 intf->needs_altsetting0 = 1;
429 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400430 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200431 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800432 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400433
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800434 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400435 intf->needs_remote_wakeup = 0;
436
Sarah Sharp83060952012-05-02 14:25:52 -0700437 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
438 if (!lpm_disable_error)
439 usb_unlocked_enable_lpm(udev);
440
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500441 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400442 if (driver->supports_autosuspend)
443 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500444 pm_runtime_set_suspended(dev);
445
446 /* Undo any residual pm_autopm_get_interface_* calls */
447 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
448 usb_autopm_put_interface_no_suspend(intf);
449 atomic_set(&intf->pm_usage_cnt, 0);
450
Alan Stern645daaa2006-08-30 15:47:02 -0400451 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500452 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800453
454 return 0;
455}
456
Alan Stern36e56a32006-07-01 22:08:06 -0400457/**
458 * usb_driver_claim_interface - bind a driver to an interface
459 * @driver: the driver to be bound
460 * @iface: the interface to which it will be bound; must be in the
461 * usb device's active configuration
462 * @priv: driver data associated with that interface
463 *
464 * This is used by usb device drivers that need to claim more than one
465 * interface on a device when probing (audio and acm are current examples).
466 * No device driver should directly modify internal usb_interface or
467 * usb_device structure members.
468 *
469 * Few drivers should need to use this routine, since the most natural
470 * way to bind to an interface is to return the private data from
471 * the driver's probe() method.
472 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400473 * Callers must own the device lock, so driver probe() entries don't need
474 * extra locking, but other call contexts may need to explicitly claim that
475 * lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400476 */
477int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800478 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400479{
480 struct device *dev = &iface->dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700481 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700482 int retval = 0;
Sarah Sharp83060952012-05-02 14:25:52 -0700483 int lpm_disable_error;
Alan Stern36e56a32006-07-01 22:08:06 -0400484
485 if (dev->driver)
486 return -EBUSY;
487
Sarah Sharp83060952012-05-02 14:25:52 -0700488 udev = interface_to_usbdev(iface);
489
Alan Stern8bb54ab2006-07-01 22:08:49 -0400490 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400491 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400492 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400493
Alan Stern36e56a32006-07-01 22:08:06 -0400494 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500495
Sarah Sharp83060952012-05-02 14:25:52 -0700496 /* Disable LPM until this driver is bound. */
497 lpm_disable_error = usb_unlocked_disable_lpm(udev);
498 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
499 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
500 __func__, driver->name);
501 return -ENOMEM;
502 }
503
Alan Stern89842ae2010-05-11 11:44:06 -0400504 /* Claimed interfaces are initially inactive (suspended) and
505 * runtime-PM-enabled, but only if the driver has autosuspend
506 * support. Otherwise they are marked active, to prevent the
507 * device from being autosuspended, but left disabled. In either
508 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500509 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500510 pm_suspend_ignore_children(dev, false);
511 if (driver->supports_autosuspend)
512 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400513 else
514 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400515
516 /* if interface was already added, bind now; else let
517 * the future device_add() bind it, bypassing probe()
518 */
519 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700520 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400521
Sarah Sharp83060952012-05-02 14:25:52 -0700522 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
523 if (!lpm_disable_error)
524 usb_unlocked_enable_lpm(udev);
525
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700526 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400527}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600528EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400529
530/**
531 * usb_driver_release_interface - unbind a driver from an interface
532 * @driver: the driver to be unbound
533 * @iface: the interface from which it will be unbound
534 *
535 * This can be used by drivers to release an interface without waiting
536 * for their disconnect() methods to be called. In typical cases this
537 * also causes the driver disconnect() method to be called.
538 *
539 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400540 * Callers must own the device lock, so driver disconnect() entries don't
541 * need extra locking, but other call contexts may need to explicitly claim
542 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400543 */
544void usb_driver_release_interface(struct usb_driver *driver,
545 struct usb_interface *iface)
546{
547 struct device *dev = &iface->dev;
548
549 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400550 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400551 return;
552
553 /* don't release from within disconnect() */
554 if (iface->condition != USB_INTERFACE_BOUND)
555 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400556 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400557
Alan Stern91f8d062009-04-16 15:35:09 -0400558 /* Release via the driver core only if the interface
559 * has already been registered
560 */
Alan Stern36e56a32006-07-01 22:08:06 -0400561 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400562 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800563 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800564 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400565 usb_unbind_interface(dev);
566 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800567 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400568 }
Alan Stern36e56a32006-07-01 22:08:06 -0400569}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600570EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400571
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800572/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100573int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800574{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800575 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
576 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
577 return 0;
578
579 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
580 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
581 return 0;
582
583 /* No need to test id->bcdDevice_lo != 0, since 0 is never
584 greater than any unsigned number. */
585 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
586 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
587 return 0;
588
589 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
590 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
591 return 0;
592
593 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
594 (id->bDeviceClass != dev->descriptor.bDeviceClass))
595 return 0;
596
597 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800598 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800599 return 0;
600
601 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
602 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
603 return 0;
604
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100605 return 1;
606}
607
608/* returns 0 if no match, 1 if match */
609int usb_match_one_id(struct usb_interface *interface,
610 const struct usb_device_id *id)
611{
612 struct usb_host_interface *intf;
613 struct usb_device *dev;
614
615 /* proc_connectinfo in devio.c may call us with id == NULL. */
616 if (id == NULL)
617 return 0;
618
619 intf = interface->cur_altsetting;
620 dev = interface_to_usbdev(interface);
621
622 if (!usb_match_device(dev, id))
623 return 0;
624
Alan Stern93c8bf42006-10-18 16:41:51 -0400625 /* The interface class, subclass, and protocol should never be
626 * checked for a match if the device class is Vendor Specific,
627 * unless the match record specifies the Vendor ID. */
628 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
629 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
630 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
631 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
632 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
633 return 0;
634
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800635 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
636 (id->bInterfaceClass != intf->desc.bInterfaceClass))
637 return 0;
638
639 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
640 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
641 return 0;
642
643 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
644 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
645 return 0;
646
647 return 1;
648}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100649EXPORT_SYMBOL_GPL(usb_match_one_id);
650
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800651/**
652 * usb_match_id - find first usb_device_id matching device or interface
653 * @interface: the interface of interest
654 * @id: array of usb_device_id structures, terminated by zero entry
655 *
656 * usb_match_id searches an array of usb_device_id's and returns
657 * the first one matching the device or interface, or null.
658 * This is used when binding (or rebinding) a driver to an interface.
659 * Most USB device drivers will use this indirectly, through the usb core,
660 * but some layered driver frameworks use it directly.
661 * These device tables are exported with MODULE_DEVICE_TABLE, through
662 * modutils, to support the driver loading functionality of USB hotplugging.
663 *
664 * What Matches:
665 *
666 * The "match_flags" element in a usb_device_id controls which
667 * members are used. If the corresponding bit is set, the
668 * value in the device_id must match its corresponding member
669 * in the device or interface descriptor, or else the device_id
670 * does not match.
671 *
672 * "driver_info" is normally used only by device drivers,
673 * but you can create a wildcard "matches anything" usb_device_id
674 * as a driver's "modules.usbmap" entry if you provide an id with
675 * only a nonzero "driver_info" field. If you do this, the USB device
676 * driver's probe() routine should use additional intelligence to
677 * decide whether to bind to the specified interface.
678 *
679 * What Makes Good usb_device_id Tables:
680 *
681 * The match algorithm is very simple, so that intelligence in
682 * driver selection must come from smart driver id records.
683 * Unless you have good reasons to use another selection policy,
684 * provide match elements only in related groups, and order match
685 * specifiers from specific to general. Use the macros provided
686 * for that purpose if you can.
687 *
688 * The most specific match specifiers use device descriptor
689 * data. These are commonly used with product-specific matches;
690 * the USB_DEVICE macro lets you provide vendor and product IDs,
691 * and you can also match against ranges of product revisions.
692 * These are widely used for devices with application or vendor
693 * specific bDeviceClass values.
694 *
695 * Matches based on device class/subclass/protocol specifications
696 * are slightly more general; use the USB_DEVICE_INFO macro, or
697 * its siblings. These are used with single-function devices
698 * where bDeviceClass doesn't specify that each interface has
699 * its own class.
700 *
701 * Matches based on interface class/subclass/protocol are the
702 * most general; they let drivers bind to any interface on a
703 * multiple-function device. Use the USB_INTERFACE_INFO
704 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400705 * devices (as recorded in bInterfaceClass).
706 *
707 * Note that an entry created by USB_INTERFACE_INFO won't match
708 * any interface if the device class is set to Vendor-Specific.
709 * This is deliberate; according to the USB spec the meanings of
710 * the interface class/subclass/protocol for these devices are also
711 * vendor-specific, and hence matching against a standard product
712 * class wouldn't work anyway. If you really want to use an
713 * interface-based match for such a device, create a match record
714 * that also specifies the vendor ID. (Unforunately there isn't a
715 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800716 *
717 * Within those groups, remember that not all combinations are
718 * meaningful. For example, don't give a product version range
719 * without vendor and product IDs; or specify a protocol without
720 * its associated class and subclass.
721 */
722const struct usb_device_id *usb_match_id(struct usb_interface *interface,
723 const struct usb_device_id *id)
724{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800725 /* proc_connectinfo in devio.c may call us with id == NULL. */
726 if (id == NULL)
727 return NULL;
728
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800729 /* It is important to check that id->driver_info is nonzero,
730 since an entry that is all zeroes except for a nonzero
731 id->driver_info is the way to create an entry that
732 indicates that the driver want to examine every
733 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800734 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
735 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800736 if (usb_match_one_id(interface, id))
737 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800738 }
739
740 return NULL;
741}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600742EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800743
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100744static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800745{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400746 /* devices and interfaces are handled separately */
747 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800748
Alan Stern8bb54ab2006-07-01 22:08:49 -0400749 /* interface drivers never match devices */
750 if (!is_usb_device_driver(drv))
751 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800752
Alan Stern8bb54ab2006-07-01 22:08:49 -0400753 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800754 return 1;
755
Kay Sievers55129662009-05-04 19:48:32 +0200756 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400757 struct usb_interface *intf;
758 struct usb_driver *usb_drv;
759 const struct usb_device_id *id;
760
761 /* device drivers never match interfaces */
762 if (is_usb_device_driver(drv))
763 return 0;
764
765 intf = to_usb_interface(dev);
766 usb_drv = to_usb_driver(drv);
767
768 id = usb_match_id(intf, usb_drv->id_table);
769 if (id)
770 return 1;
771
772 id = usb_match_dynamic_id(intf, usb_drv);
773 if (id)
774 return 1;
775 }
776
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800777 return 0;
778}
779
Alan Stern36e56a32006-07-01 22:08:06 -0400780#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200781static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400782{
Alan Stern36e56a32006-07-01 22:08:06 -0400783 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400784
Kay Sievers55129662009-05-04 19:48:32 +0200785 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400786 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200787 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100788 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200789
Alan Stern8bb54ab2006-07-01 22:08:49 -0400790 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200791 } else {
792 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400793 }
Alan Stern36e56a32006-07-01 22:08:06 -0400794
795 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500796 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200797 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400798 return -ENODEV;
799 }
800 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200801 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400802 return -ENODEV;
803 }
804
Alan Stern36e56a32006-07-01 22:08:06 -0400805 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200806 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400807 le16_to_cpu(usb_dev->descriptor.idVendor),
808 le16_to_cpu(usb_dev->descriptor.idProduct),
809 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
810 return -ENOMEM;
811
812 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200813 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400814 usb_dev->descriptor.bDeviceClass,
815 usb_dev->descriptor.bDeviceSubClass,
816 usb_dev->descriptor.bDeviceProtocol))
817 return -ENOMEM;
818
Alan Stern36e56a32006-07-01 22:08:06 -0400819 return 0;
820}
821
822#else
823
Kay Sievers7eff2e72007-08-14 15:15:12 +0200824static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400825{
826 return -ENODEV;
827}
Alan Stern36e56a32006-07-01 22:08:06 -0400828#endif /* CONFIG_HOTPLUG */
829
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800830/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400831 * usb_register_device_driver - register a USB device (not interface) driver
832 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800833 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800834 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400835 * Registers a USB device driver with the USB core. The list of
836 * unattached devices will be rescanned whenever a new driver is
837 * added, allowing the new driver to attach to any recognized devices.
838 * Returns a negative error code on failure and 0 on success.
839 */
840int usb_register_device_driver(struct usb_device_driver *new_udriver,
841 struct module *owner)
842{
843 int retval = 0;
844
845 if (usb_disabled())
846 return -ENODEV;
847
848 new_udriver->drvwrap.for_devices = 1;
849 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
850 new_udriver->drvwrap.driver.bus = &usb_bus_type;
851 new_udriver->drvwrap.driver.probe = usb_probe_device;
852 new_udriver->drvwrap.driver.remove = usb_unbind_device;
853 new_udriver->drvwrap.driver.owner = owner;
854
855 retval = driver_register(&new_udriver->drvwrap.driver);
856
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700857 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400858 pr_info("%s: registered new device driver %s\n",
859 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700860 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400861 printk(KERN_ERR "%s: error %d registering device "
862 " driver %s\n",
863 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400864
865 return retval;
866}
867EXPORT_SYMBOL_GPL(usb_register_device_driver);
868
869/**
870 * usb_deregister_device_driver - unregister a USB device (not interface) driver
871 * @udriver: USB operations of the device driver to unregister
872 * Context: must be able to sleep
873 *
874 * Unlinks the specified driver from the internal USB driver list.
875 */
876void usb_deregister_device_driver(struct usb_device_driver *udriver)
877{
878 pr_info("%s: deregistering device driver %s\n",
879 usbcore_name, udriver->name);
880
881 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400882}
883EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
884
885/**
886 * usb_register_driver - register a USB interface driver
887 * @new_driver: USB operations for the interface driver
888 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800889 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400890 *
891 * Registers a USB interface driver with the USB core. The list of
892 * unattached interfaces will be rescanned whenever a new driver is
893 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800894 * Returns a negative error code on failure and 0 on success.
895 *
896 * NOTE: if you want your driver to use the USB major number, you must call
897 * usb_register_dev() to enable that functionality. This function no longer
898 * takes care of that.
899 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800900int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
901 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800902{
903 int retval = 0;
904
905 if (usb_disabled())
906 return -ENODEV;
907
Alan Stern8bb54ab2006-07-01 22:08:49 -0400908 new_driver->drvwrap.for_devices = 0;
909 new_driver->drvwrap.driver.name = (char *) new_driver->name;
910 new_driver->drvwrap.driver.bus = &usb_bus_type;
911 new_driver->drvwrap.driver.probe = usb_probe_interface;
912 new_driver->drvwrap.driver.remove = usb_unbind_interface;
913 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800914 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800915 spin_lock_init(&new_driver->dynids.lock);
916 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800917
Alan Stern8bb54ab2006-07-01 22:08:49 -0400918 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800919 if (retval)
920 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800921
Alan Sterned283e92012-01-24 14:35:13 -0500922 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800923 if (retval)
924 goto out_newid;
925
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800926 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800927 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800928
929out:
930 return retval;
931
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800932out_newid:
933 driver_unregister(&new_driver->drvwrap.driver);
934
935 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400936 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800937 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800938 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800939}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600940EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800941
942/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400943 * usb_deregister - unregister a USB interface driver
944 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800945 * Context: must be able to sleep
946 *
947 * Unlinks the specified driver from the internal USB driver list.
948 *
949 * NOTE: If you called usb_register_dev(), you still need to call
950 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
951 * this * call will no longer do it for you.
952 */
953void usb_deregister(struct usb_driver *driver)
954{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400955 pr_info("%s: deregistering interface driver %s\n",
956 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800957
Alan Sterned283e92012-01-24 14:35:13 -0500958 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400959 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -0500960 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800961}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600962EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400963
Alan Stern78d9a482008-06-23 16:00:40 -0400964/* Forced unbinding of a USB interface driver, either because
965 * it doesn't support pre_reset/post_reset/reset_resume or
966 * because it doesn't support suspend/resume.
967 *
968 * The caller must hold @intf's device's lock, but not its pm_mutex
969 * and not @intf->dev.sem.
970 */
971void usb_forced_unbind_intf(struct usb_interface *intf)
972{
973 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
974
975 dev_dbg(&intf->dev, "forced unbind\n");
976 usb_driver_release_interface(driver, intf);
977
978 /* Mark the interface for later rebinding */
979 intf->needs_binding = 1;
980}
981
982/* Delayed forced unbinding of a USB interface driver and scan
983 * for rebinding.
984 *
985 * The caller must hold @intf's device's lock, but not its pm_mutex
986 * and not @intf->dev.sem.
987 *
Alan Stern5096aed2008-08-12 14:34:14 -0400988 * Note: Rebinds will be skipped if a system sleep transition is in
989 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -0400990 */
991void usb_rebind_intf(struct usb_interface *intf)
992{
993 int rc;
994
995 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +0100996 if (intf->dev.driver)
997 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -0400998
999 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +02001000 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -04001001 intf->needs_binding = 0;
1002 rc = device_attach(&intf->dev);
1003 if (rc < 0)
1004 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1005 }
Alan Stern78d9a482008-06-23 16:00:40 -04001006}
1007
Alan Stern9ff78432008-08-07 13:04:51 -04001008#ifdef CONFIG_PM
1009
Oliver Neukum14931382012-01-05 15:39:57 +01001010/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1011 * There is no check for reset_resume here because it can be determined
1012 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001013 *
1014 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001015 */
Oliver Neukum14931382012-01-05 15:39:57 +01001016static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001017{
1018 struct usb_host_config *config;
1019 int i;
1020 struct usb_interface *intf;
1021 struct usb_driver *drv;
1022
1023 config = udev->actconfig;
1024 if (config) {
1025 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1026 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001027
1028 if (intf->dev.driver) {
1029 drv = to_usb_driver(intf->dev.driver);
1030 if (!drv->suspend || !drv->resume)
1031 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001032 }
1033 }
1034 }
1035}
1036
Oliver Neukum14931382012-01-05 15:39:57 +01001037/* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1038 * These interfaces have the needs_binding flag set by usb_resume_interface().
1039 *
1040 * The caller must hold @udev's device lock.
1041 */
1042static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
1043{
1044 struct usb_host_config *config;
1045 int i;
1046 struct usb_interface *intf;
1047
1048 config = udev->actconfig;
1049 if (config) {
1050 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1051 intf = config->interface[i];
1052 if (intf->dev.driver && intf->needs_binding)
1053 usb_forced_unbind_intf(intf);
1054 }
1055 }
1056}
1057
1058static void do_rebind_interfaces(struct usb_device *udev)
1059{
1060 struct usb_host_config *config;
1061 int i;
1062 struct usb_interface *intf;
1063
1064 config = udev->actconfig;
1065 if (config) {
1066 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1067 intf = config->interface[i];
1068 if (intf->needs_binding)
1069 usb_rebind_intf(intf);
1070 }
1071 }
1072}
1073
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001074static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001075{
Alan Stern782da722006-07-01 22:09:35 -04001076 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001077 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001078
Alan Stern114b3682006-07-01 22:13:04 -04001079 if (udev->state == USB_STATE_NOTATTACHED ||
1080 udev->state == USB_STATE_SUSPENDED)
1081 goto done;
1082
Alan Sternb6f64362007-05-04 11:51:54 -04001083 /* For devices that don't have a driver, we do a generic suspend. */
1084 if (udev->dev.driver)
1085 udriver = to_usb_device_driver(udev->dev.driver);
1086 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001087 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001088 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001089 }
Alan Stern2bf40862006-07-01 22:12:19 -04001090 status = udriver->suspend(udev, msg);
1091
Alan Stern20dfdad2007-05-22 11:50:17 -04001092 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001093 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001094 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001095}
Alan Stern36e56a32006-07-01 22:08:06 -04001096
Alan Stern65bfd292008-11-25 16:39:18 -05001097static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001098{
1099 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001100 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001101
Alan Stern0458d5b2007-05-04 11:52:20 -04001102 if (udev->state == USB_STATE_NOTATTACHED)
1103 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001104
Alan Stern1c5df7e2006-07-01 22:13:50 -04001105 /* Can't resume it if it doesn't have a driver. */
1106 if (udev->dev.driver == NULL) {
1107 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001108 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001109 }
1110
Alan Stern6d19c002010-02-12 12:21:11 +01001111 /* Non-root devices on a full/low-speed bus must wait for their
1112 * companion high-speed root hub, in case a handoff is needed.
1113 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001114 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001115 device_pm_wait_for_dev(&udev->dev,
1116 &udev->bus->hs_companion->root_hub->dev);
1117
Alan Stern6bc6cff2007-05-04 11:53:03 -04001118 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1119 udev->reset_resume = 1;
1120
Alan Stern1cc8a252006-07-01 22:10:15 -04001121 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001122 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001123
Alan Stern20dfdad2007-05-22 11:50:17 -04001124 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001125 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001126 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001127}
1128
Alan Stern65605ae2008-08-12 14:33:27 -04001129static int usb_suspend_interface(struct usb_device *udev,
1130 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001131{
1132 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001133 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001134
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001135 if (udev->state == USB_STATE_NOTATTACHED ||
1136 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001137 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001138 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001139
Oliver Neukume78832c2012-01-02 15:11:48 +01001140 /* at this time we know the driver supports suspend */
1141 status = driver->suspend(intf, msg);
1142 if (status && !PMSG_IS_AUTO(msg))
1143 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001144
Alan Stern20dfdad2007-05-22 11:50:17 -04001145 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001146 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001147 return status;
1148}
1149
Alan Stern65605ae2008-08-12 14:33:27 -04001150static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001151 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001152{
Alan Stern1cc8a252006-07-01 22:10:15 -04001153 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001154 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001155
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001156 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001157 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001158
Alan Stern645daaa2006-08-30 15:47:02 -04001159 /* Don't let autoresume interfere with unbinding */
1160 if (intf->condition == USB_INTERFACE_UNBINDING)
1161 goto done;
1162
Alan Stern1c5df7e2006-07-01 22:13:50 -04001163 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001164 if (intf->condition == USB_INTERFACE_UNBOUND) {
1165
1166 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001167 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001168 usb_set_interface(udev, intf->altsetting[0].
1169 desc.bInterfaceNumber, 0);
1170 intf->needs_altsetting0 = 0;
1171 }
Alan Stern2bf40862006-07-01 22:12:19 -04001172 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001173 }
Alan Stern78d9a482008-06-23 16:00:40 -04001174
1175 /* Don't resume if the interface is marked for rebinding */
1176 if (intf->needs_binding)
1177 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001178 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001179
Alan Sternf07600c2007-05-30 15:38:16 -04001180 if (reset_resume) {
1181 if (driver->reset_resume) {
1182 status = driver->reset_resume(intf);
1183 if (status)
1184 dev_err(&intf->dev, "%s error %d\n",
1185 "reset_resume", status);
1186 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001187 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001188 dev_warn(&intf->dev, "no %s for driver %s?\n",
1189 "reset_resume", driver->name);
1190 }
1191 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001192 status = driver->resume(intf);
1193 if (status)
1194 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001195 }
Alan Stern2bf40862006-07-01 22:12:19 -04001196
1197done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001198 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001199
Alan Stern78d9a482008-06-23 16:00:40 -04001200 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001201 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001202}
1203
Alan Stern645daaa2006-08-30 15:47:02 -04001204/**
1205 * usb_suspend_both - suspend a USB device and its interfaces
1206 * @udev: the usb_device to suspend
1207 * @msg: Power Management message describing this state transition
1208 *
1209 * This is the central routine for suspending USB devices. It calls the
1210 * suspend methods for all the interface drivers in @udev and then calls
1211 * the suspend method for @udev itself. If an error occurs at any stage,
1212 * all the interfaces which were suspended are resumed so that they remain
1213 * in the same state as the device.
1214 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001215 * Autosuspend requests originating from a child device or an interface
1216 * driver may be made without the protection of @udev's device lock, but
1217 * all other suspend calls will hold the lock. Usbcore will insure that
1218 * method calls do not arrive during bind, unbind, or reset operations.
1219 * However drivers must be prepared to handle suspend calls arriving at
1220 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001221 *
1222 * This routine can run only in process context.
1223 */
Alan Stern718efa62007-03-09 15:41:13 -05001224static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001225{
1226 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001227 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001228 struct usb_interface *intf;
1229
Alan Stern19410442007-03-27 13:33:59 -04001230 if (udev->state == USB_STATE_NOTATTACHED ||
1231 udev->state == USB_STATE_SUSPENDED)
1232 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001233
Alan Stern645daaa2006-08-30 15:47:02 -04001234 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001235 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001236 n = udev->actconfig->desc.bNumInterfaces;
1237 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001238 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001239 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001240
1241 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001242 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001243 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001244 if (status != 0)
1245 break;
1246 }
1247 }
Alan Stern0af212b2011-06-15 16:27:43 -04001248 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001249 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001250
Alan Sterncd4376e2012-03-28 15:56:17 -04001251 /*
1252 * Ignore errors from non-root-hub devices during
1253 * system sleep transitions. For the most part,
1254 * these devices should go to low power anyway when
1255 * the entire bus is suspended.
1256 */
1257 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001258 status = 0;
1259 }
1260
Alan Sterna8e7c562006-07-01 22:11:02 -04001261 /* If the suspend failed, resume interfaces that did get suspended */
1262 if (status != 0) {
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001263 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
Alan Stern571dc792010-04-09 16:03:43 -04001264 while (++i < n) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001265 intf = udev->actconfig->interface[i];
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001266 usb_resume_interface(udev, intf, msg, 0);
Alan Sterna8e7c562006-07-01 22:11:02 -04001267 }
Alan Stern645daaa2006-08-30 15:47:02 -04001268
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001269 /* If the suspend succeeded then prevent any more URB submissions
1270 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001271 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001272 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001273 udev->can_submit = 0;
1274 for (i = 0; i < 16; ++i) {
1275 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1276 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1277 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001278 }
Alan Stern645daaa2006-08-30 15:47:02 -04001279
Alan Stern19410442007-03-27 13:33:59 -04001280 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001281 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001282 return status;
1283}
1284
Alan Stern645daaa2006-08-30 15:47:02 -04001285/**
1286 * usb_resume_both - resume a USB device and its interfaces
1287 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001288 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001289 *
1290 * This is the central routine for resuming USB devices. It calls the
1291 * the resume method for @udev and then calls the resume methods for all
1292 * the interface drivers in @udev.
1293 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001294 * Autoresume requests originating from a child device or an interface
1295 * driver may be made without the protection of @udev's device lock, but
1296 * all other resume calls will hold the lock. Usbcore will insure that
1297 * method calls do not arrive during bind, unbind, or reset operations.
1298 * However drivers must be prepared to handle resume calls arriving at
1299 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001300 *
1301 * This routine can run only in process context.
1302 */
Alan Stern65bfd292008-11-25 16:39:18 -05001303static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001304{
Alan Stern645daaa2006-08-30 15:47:02 -04001305 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001306 int i;
1307 struct usb_interface *intf;
1308
Alan Stern19410442007-03-27 13:33:59 -04001309 if (udev->state == USB_STATE_NOTATTACHED) {
1310 status = -ENODEV;
1311 goto done;
1312 }
Alan Stern6840d252007-09-10 11:34:26 -04001313 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001314
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001315 /* Resume the device */
1316 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001317 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001318
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001319 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001320 if (status == 0 && udev->actconfig) {
1321 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1322 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001323 usb_resume_interface(udev, intf, msg,
1324 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001325 }
1326 }
Alan Sternc08512c2010-11-15 15:57:58 -05001327 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001328
Alan Stern19410442007-03-27 13:33:59 -04001329 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001330 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001331 if (!status)
1332 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001333 return status;
1334}
1335
Alan Stern5f677f12010-04-02 13:20:11 -04001336static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1337{
Alan Stern48826622010-06-22 16:14:48 -04001338 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001339
1340 /* Remote wakeup is needed only when we actually go to sleep.
1341 * For things like FREEZE and QUIESCE, if the device is already
1342 * autosuspended then its current wakeup setting is okay.
1343 */
1344 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1345 if (udev->state != USB_STATE_SUSPENDED)
1346 udev->do_remote_wakeup = 0;
1347 return;
1348 }
1349
Alan Stern48826622010-06-22 16:14:48 -04001350 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001351 * actually want it.
1352 */
Alan Stern48826622010-06-22 16:14:48 -04001353 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001354
1355 /* If the device is autosuspended with the wrong wakeup setting,
1356 * autoresume now so the setting can be changed.
1357 */
1358 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1359 pm_runtime_resume(&udev->dev);
1360 udev->do_remote_wakeup = w;
1361}
1362
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001363/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001364int usb_suspend(struct device *dev, pm_message_t msg)
1365{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001366 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001367
Oliver Neukum14931382012-01-05 15:39:57 +01001368 unbind_no_pm_drivers_interfaces(udev);
1369
1370 /* From now on we are sure all drivers support suspend/resume
1371 * but not necessarily reset_resume()
1372 * so we may still need to unbind and rebind upon resume
1373 */
Alan Stern5f677f12010-04-02 13:20:11 -04001374 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001375 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001376}
1377
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001378/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001379int usb_resume_complete(struct device *dev)
1380{
1381 struct usb_device *udev = to_usb_device(dev);
1382
1383 /* For PM complete calls, all we do is rebind interfaces
1384 * whose needs_binding flag is set
1385 */
1386 if (udev->state != USB_STATE_NOTATTACHED)
1387 do_rebind_interfaces(udev);
1388 return 0;
1389}
1390
1391/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001392int usb_resume(struct device *dev, pm_message_t msg)
1393{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001394 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001395 int status;
1396
Oliver Neukum98d9a822012-01-11 08:38:35 +01001397 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001398 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001399 * Unbind the interfaces that will need rebinding later,
1400 * because they fail to support reset_resume.
1401 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001402 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001403 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001404 status = usb_resume_both(udev, msg);
1405 if (status == 0) {
1406 pm_runtime_disable(dev);
1407 pm_runtime_set_active(dev);
1408 pm_runtime_enable(dev);
1409 unbind_no_reset_resume_drivers_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001410 }
Alan Stern0c590e22010-01-08 12:57:14 -05001411
1412 /* Avoid PM error messages for devices disconnected while suspended
1413 * as we'll display regular disconnect messages just a bit later.
1414 */
Peter Chen7491f132010-09-27 16:43:25 +08001415 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001416 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001417 return status;
1418}
1419
1420#endif /* CONFIG_PM */
1421
Alan Stern645daaa2006-08-30 15:47:02 -04001422#ifdef CONFIG_USB_SUSPEND
1423
Alan Stern088f7fe2010-01-08 12:56:54 -05001424/**
1425 * usb_enable_autosuspend - allow a USB device to be autosuspended
1426 * @udev: the USB device which may be autosuspended
1427 *
1428 * This routine allows @udev to be autosuspended. An autosuspend won't
1429 * take place until the autosuspend_delay has elapsed and all the other
1430 * necessary conditions are satisfied.
1431 *
1432 * The caller must hold @udev's device lock.
1433 */
Alan Stern9e18c822010-04-02 13:22:09 -04001434void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001435{
Alan Stern9e18c822010-04-02 13:22:09 -04001436 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001437}
1438EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1439
1440/**
1441 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1442 * @udev: the USB device which may not be autosuspended
1443 *
1444 * This routine prevents @udev from being autosuspended and wakes it up
1445 * if it is already autosuspended.
1446 *
1447 * The caller must hold @udev's device lock.
1448 */
Alan Stern9e18c822010-04-02 13:22:09 -04001449void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001450{
Alan Stern9e18c822010-04-02 13:22:09 -04001451 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001452}
1453EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1454
Alan Stern645daaa2006-08-30 15:47:02 -04001455/**
1456 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001457 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001458 *
1459 * This routine should be called when a core subsystem is finished using
1460 * @udev and wants to allow it to autosuspend. Examples would be when
1461 * @udev's device file in usbfs is closed or after a configuration change.
1462 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001463 * @udev's usage counter is decremented; if it drops to 0 and all the
1464 * interfaces are inactive then a delayed autosuspend will be attempted.
1465 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001466 *
Alan Stern62e299e2010-01-08 12:56:19 -05001467 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001468 *
1469 * This routine can run only in process context.
1470 */
Alan Stern94fcda12006-11-20 11:38:46 -05001471void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001472{
Alan Stern94fcda12006-11-20 11:38:46 -05001473 int status;
1474
Ming Lei6ddf27c2010-11-15 15:57:30 -05001475 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001476 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001477 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1478 __func__, atomic_read(&udev->dev.power.usage_count),
1479 status);
Alan Stern19c26232007-02-20 15:03:32 -05001480}
1481
1482/**
Alan Stern645daaa2006-08-30 15:47:02 -04001483 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001484 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001485 *
1486 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001487 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001488 * occur until usb_autosuspend_device() is called. (Note that this will
1489 * not prevent suspend events originating in the PM core.) Examples would
1490 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001491 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001492 *
Alan Stern94fcda12006-11-20 11:38:46 -05001493 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1494 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001495 *
Alan Stern62e299e2010-01-08 12:56:19 -05001496 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001497 *
1498 * This routine can run only in process context.
1499 */
Alan Stern94fcda12006-11-20 11:38:46 -05001500int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001501{
1502 int status;
1503
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001504 status = pm_runtime_get_sync(&udev->dev);
1505 if (status < 0)
1506 pm_runtime_put_sync(&udev->dev);
1507 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1508 __func__, atomic_read(&udev->dev.power.usage_count),
1509 status);
1510 if (status > 0)
1511 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001512 return status;
1513}
1514
Alan Stern645daaa2006-08-30 15:47:02 -04001515/**
1516 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001517 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001518 *
1519 * This routine should be called by an interface driver when it is
1520 * finished using @intf and wants to allow it to autosuspend. A typical
1521 * example would be a character-device driver when its device file is
1522 * closed.
1523 *
1524 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001525 * 0, a delayed autosuspend request for @intf's device is attempted. The
1526 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001527 *
Alan Stern645daaa2006-08-30 15:47:02 -04001528 * This routine can run only in process context.
1529 */
1530void usb_autopm_put_interface(struct usb_interface *intf)
1531{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001532 struct usb_device *udev = interface_to_usbdev(intf);
1533 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001534
Ming Lei6ddf27c2010-11-15 15:57:30 -05001535 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001536 atomic_dec(&intf->pm_usage_cnt);
1537 status = pm_runtime_put_sync(&intf->dev);
1538 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1539 __func__, atomic_read(&intf->dev.power.usage_count),
1540 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001541}
1542EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1543
1544/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001545 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1546 * @intf: the usb_interface whose counter should be decremented
1547 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001548 * This routine does much the same thing as usb_autopm_put_interface():
1549 * It decrements @intf's usage counter and schedules a delayed
1550 * autosuspend request if the counter is <= 0. The difference is that it
1551 * does not perform any synchronization; callers should hold a private
1552 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001553 *
1554 * Typically a driver would call this routine during an URB's completion
1555 * handler, if no more URBs were pending.
1556 *
1557 * This routine can run in atomic context.
1558 */
1559void usb_autopm_put_interface_async(struct usb_interface *intf)
1560{
1561 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001562 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001563
Ming Lei6ddf27c2010-11-15 15:57:30 -05001564 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001565 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001566 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001567 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1568 __func__, atomic_read(&intf->dev.power.usage_count),
1569 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001570}
1571EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1572
1573/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001574 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1575 * @intf: the usb_interface whose counter should be decremented
1576 *
1577 * This routine decrements @intf's usage counter but does not carry out an
1578 * autosuspend.
1579 *
1580 * This routine can run in atomic context.
1581 */
1582void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1583{
1584 struct usb_device *udev = interface_to_usbdev(intf);
1585
Ming Lei6ddf27c2010-11-15 15:57:30 -05001586 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001587 atomic_dec(&intf->pm_usage_cnt);
1588 pm_runtime_put_noidle(&intf->dev);
1589}
1590EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1591
1592/**
Alan Stern645daaa2006-08-30 15:47:02 -04001593 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001594 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001595 *
1596 * This routine should be called by an interface driver when it wants to
1597 * use @intf and needs to guarantee that it is not suspended. In addition,
1598 * the routine prevents @intf from being autosuspended subsequently. (Note
1599 * that this will not prevent suspend events originating in the PM core.)
1600 * This prevention will persist until usb_autopm_put_interface() is called
1601 * or @intf is unbound. A typical example would be a character-device
1602 * driver when its device file is opened.
1603 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001604 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1605 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001606 *
1607 * This routine can run only in process context.
1608 */
1609int usb_autopm_get_interface(struct usb_interface *intf)
1610{
Alan Sternaf4f7602006-10-30 17:06:45 -05001611 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001612
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001613 status = pm_runtime_get_sync(&intf->dev);
1614 if (status < 0)
1615 pm_runtime_put_sync(&intf->dev);
1616 else
1617 atomic_inc(&intf->pm_usage_cnt);
1618 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1619 __func__, atomic_read(&intf->dev.power.usage_count),
1620 status);
1621 if (status > 0)
1622 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001623 return status;
1624}
1625EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1626
Alan Stern692a1862006-10-30 17:07:51 -05001627/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001628 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1629 * @intf: the usb_interface whose counter should be incremented
1630 *
1631 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001632 * usb_autopm_get_interface(): It increments @intf's usage counter and
1633 * queues an autoresume request if the device is suspended. The
1634 * differences are that it does not perform any synchronization (callers
1635 * should hold a private lock and handle all synchronization issues
1636 * themselves), and it does not autoresume the device directly (it only
1637 * queues a request). After a successful call, the device may not yet be
1638 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001639 *
1640 * This routine can run in atomic context.
1641 */
1642int usb_autopm_get_interface_async(struct usb_interface *intf)
1643{
Ming Lei63defa72010-11-15 15:56:54 -05001644 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001645
Ming Lei63defa72010-11-15 15:56:54 -05001646 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001647 if (status < 0 && status != -EINPROGRESS)
1648 pm_runtime_put_noidle(&intf->dev);
1649 else
Alan Sternccf5b802009-06-29 11:00:01 -04001650 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001651 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1652 __func__, atomic_read(&intf->dev.power.usage_count),
1653 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001654 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001655 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001656 return status;
1657}
1658EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1659
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001660/**
1661 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1662 * @intf: the usb_interface whose counter should be incremented
1663 *
1664 * This routine increments @intf's usage counter but does not carry out an
1665 * autoresume.
1666 *
1667 * This routine can run in atomic context.
1668 */
1669void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1670{
1671 struct usb_device *udev = interface_to_usbdev(intf);
1672
Ming Lei6ddf27c2010-11-15 15:57:30 -05001673 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001674 atomic_inc(&intf->pm_usage_cnt);
1675 pm_runtime_get_noresume(&intf->dev);
1676}
1677EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1678
1679/* Internal routine to check whether we may autosuspend a device. */
1680static int autosuspend_check(struct usb_device *udev)
1681{
Alan Stern7560d32e2010-04-02 13:18:50 -04001682 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001683 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001684
1685 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1686 * any interface drivers require remote wakeup but it isn't available.
1687 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001688 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001689 if (udev->actconfig) {
1690 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1691 intf = udev->actconfig->interface[i];
1692
1693 /* We don't need to check interfaces that are
1694 * disabled for runtime PM. Either they are unbound
1695 * or else their drivers don't support autosuspend
1696 * and so they are permanently active.
1697 */
1698 if (intf->dev.power.disable_depth)
1699 continue;
1700 if (atomic_read(&intf->dev.power.usage_count) > 0)
1701 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001702 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001703
1704 /* Don't allow autosuspend if the device will need
1705 * a reset-resume and any of its interface drivers
1706 * doesn't include support or needs remote wakeup.
1707 */
1708 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1709 struct usb_driver *driver;
1710
1711 driver = to_usb_driver(intf->dev.driver);
1712 if (!driver->reset_resume ||
1713 intf->needs_remote_wakeup)
1714 return -EOPNOTSUPP;
1715 }
1716 }
1717 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001718 if (w && !device_can_wakeup(&udev->dev)) {
1719 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1720 return -EOPNOTSUPP;
1721 }
1722 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001723 return 0;
1724}
1725
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001726int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001727{
Ming Lei63defa72010-11-15 15:56:54 -05001728 struct usb_device *udev = to_usb_device(dev);
1729 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001730
1731 /* A USB device can be suspended if it passes the various autosuspend
1732 * checks. Runtime suspend for a USB device means suspending all the
1733 * interfaces and then the device itself.
1734 */
Ming Lei63defa72010-11-15 15:56:54 -05001735 if (autosuspend_check(udev) != 0)
1736 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001737
Ming Lei63defa72010-11-15 15:56:54 -05001738 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001739
1740 /* Allow a retry if autosuspend failed temporarily */
1741 if (status == -EAGAIN || status == -EBUSY)
1742 usb_mark_last_busy(udev);
1743
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001744 /* The PM core reacts badly unless the return code is 0,
1745 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1746 */
1747 if (status != 0)
1748 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001749 return status;
1750}
1751
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001752int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001753{
Ming Lei63defa72010-11-15 15:56:54 -05001754 struct usb_device *udev = to_usb_device(dev);
1755 int status;
1756
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001757 /* Runtime resume for a USB device means resuming both the device
1758 * and all its interfaces.
1759 */
Ming Lei63defa72010-11-15 15:56:54 -05001760 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001761 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001762}
1763
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001764int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001765{
Ming Lei63defa72010-11-15 15:56:54 -05001766 struct usb_device *udev = to_usb_device(dev);
1767
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001768 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001769 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001770 */
Ming Lei63defa72010-11-15 15:56:54 -05001771 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001772 pm_runtime_autosuspend(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001773 return 0;
1774}
1775
Andiry Xu65580b432011-09-23 14:19:52 -07001776int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1777{
1778 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1779 int ret = -EPERM;
1780
1781 if (hcd->driver->set_usb2_hw_lpm) {
1782 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1783 if (!ret)
1784 udev->usb2_hw_lpm_enabled = enable;
1785 }
1786
1787 return ret;
1788}
1789
Alan Stern645daaa2006-08-30 15:47:02 -04001790#endif /* CONFIG_USB_SUSPEND */
1791
Alan Stern36e56a32006-07-01 22:08:06 -04001792struct bus_type usb_bus_type = {
1793 .name = "usb",
1794 .match = usb_device_match,
1795 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001796};