blob: 6eab440e1542e450f7733fb4cbbf35b1b1044313 [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/*
36 * Adds a new dynamic USBdevice ID to this driver,
37 * and cause the driver to probe for all devices again.
38 */
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010039ssize_t usb_store_new_id(struct usb_dynids *dynids,
40 struct device_driver *driver,
41 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080042{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080043 struct usb_dynid *dynid;
44 u32 idVendor = 0;
45 u32 idProduct = 0;
Josua Dietzeff231db2011-10-23 14:22:29 +020046 unsigned int bInterfaceClass = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080047 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070048 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080049
Josua Dietzeff231db2011-10-23 14:22:29 +020050 fields = sscanf(buf, "%x %x %x", &idVendor, &idProduct,
51 &bInterfaceClass);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080052 if (fields < 2)
53 return -EINVAL;
54
55 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
56 if (!dynid)
57 return -ENOMEM;
58
59 INIT_LIST_HEAD(&dynid->node);
60 dynid->id.idVendor = idVendor;
61 dynid->id.idProduct = idProduct;
62 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Josua Dietzeff231db2011-10-23 14:22:29 +020063 if (fields == 3) {
64 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
65 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
66 }
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080067
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010068 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020069 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010070 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080071
Alan Sterncef9bc52012-01-24 13:34:41 -050072 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080073
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070074 if (retval)
75 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080076 return count;
77}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010078EXPORT_SYMBOL_GPL(usb_store_new_id);
79
Bjørn Morkef206f32012-05-13 12:35:00 +020080ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
Bjørn Morke6bbcef2012-05-13 12:34:59 +020081{
82 struct usb_dynid *dynid;
Bjørn Morke6bbcef2012-05-13 12:34:59 +020083 size_t count = 0;
84
Bjørn Morkef206f32012-05-13 12:35:00 +020085 list_for_each_entry(dynid, &dynids->list, node)
Bjørn Morke6bbcef2012-05-13 12:34:59 +020086 if (dynid->id.bInterfaceClass != 0)
87 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
88 dynid->id.idVendor, dynid->id.idProduct,
89 dynid->id.bInterfaceClass);
90 else
91 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
92 dynid->id.idVendor, dynid->id.idProduct);
93 return count;
94}
Bjørn Morkef206f32012-05-13 12:35:00 +020095EXPORT_SYMBOL_GPL(usb_show_dynids);
96
97static ssize_t show_dynids(struct device_driver *driver, char *buf)
98{
99 struct usb_driver *usb_drv = to_usb_driver(driver);
100
101 return usb_show_dynids(&usb_drv->dynids, buf);
102}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200103
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100104static ssize_t store_new_id(struct device_driver *driver,
105 const char *buf, size_t count)
106{
107 struct usb_driver *usb_drv = to_usb_driver(driver);
108
109 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
110}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200111static DRIVER_ATTR(new_id, S_IRUGO | S_IWUSR, show_dynids, store_new_id);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800112
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800113/**
114 * store_remove_id - remove a USB device ID from this driver
115 * @driver: target device driver
116 * @buf: buffer for scanning device ID data
117 * @count: input size
118 *
119 * Removes a dynamic usb device ID from this driver.
120 */
121static ssize_t
122store_remove_id(struct device_driver *driver, const char *buf, size_t count)
123{
124 struct usb_dynid *dynid, *n;
125 struct usb_driver *usb_driver = to_usb_driver(driver);
Alan Coxac08de32012-09-17 11:55:23 +0100126 u32 idVendor;
127 u32 idProduct;
128 int fields;
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800129
130 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
131 if (fields < 2)
132 return -EINVAL;
133
134 spin_lock(&usb_driver->dynids.lock);
135 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
136 struct usb_device_id *id = &dynid->id;
137 if ((id->idVendor == idVendor) &&
138 (id->idProduct == idProduct)) {
139 list_del(&dynid->node);
140 kfree(dynid);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800141 break;
142 }
143 }
144 spin_unlock(&usb_driver->dynids.lock);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800145 return count;
146}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200147static DRIVER_ATTR(remove_id, S_IRUGO | S_IWUSR, show_dynids, store_remove_id);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800148
Alan Sterned283e92012-01-24 14:35:13 -0500149static int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800150{
151 int error = 0;
152
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800153 if (usb_drv->no_dynamic_id)
154 goto exit;
155
Alan Sterned283e92012-01-24 14:35:13 -0500156 if (usb_drv->probe != NULL) {
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800157 error = driver_create_file(&usb_drv->drvwrap.driver,
158 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500159 if (error == 0) {
160 error = driver_create_file(&usb_drv->drvwrap.driver,
161 &driver_attr_remove_id);
162 if (error)
163 driver_remove_file(&usb_drv->drvwrap.driver,
164 &driver_attr_new_id);
165 }
166 }
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800167exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800168 return error;
169}
170
Alan Sterned283e92012-01-24 14:35:13 -0500171static void usb_remove_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800172{
173 if (usb_drv->no_dynamic_id)
174 return;
175
Alan Sterned283e92012-01-24 14:35:13 -0500176 if (usb_drv->probe != NULL) {
177 driver_remove_file(&usb_drv->drvwrap.driver,
178 &driver_attr_remove_id);
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800179 driver_remove_file(&usb_drv->drvwrap.driver,
180 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500181 }
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800182}
183
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800184static void usb_free_dynids(struct usb_driver *usb_drv)
185{
186 struct usb_dynid *dynid, *n;
187
188 spin_lock(&usb_drv->dynids.lock);
189 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
190 list_del(&dynid->node);
191 kfree(dynid);
192 }
193 spin_unlock(&usb_drv->dynids.lock);
194}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800195
196static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
197 struct usb_driver *drv)
198{
199 struct usb_dynid *dynid;
200
201 spin_lock(&drv->dynids.lock);
202 list_for_each_entry(dynid, &drv->dynids.list, node) {
203 if (usb_match_one_id(intf, &dynid->id)) {
204 spin_unlock(&drv->dynids.lock);
205 return &dynid->id;
206 }
207 }
208 spin_unlock(&drv->dynids.lock);
209 return NULL;
210}
211
212
Alan Stern8bb54ab2006-07-01 22:08:49 -0400213/* called from driver core with dev locked */
214static int usb_probe_device(struct device *dev)
215{
216 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200217 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500218 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400219
Harvey Harrison441b62c2008-03-03 16:08:34 -0800220 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400221
Alan Stern8bb54ab2006-07-01 22:08:49 -0400222 /* TODO: Add real matching code */
223
Alan Stern645daaa2006-08-30 15:47:02 -0400224 /* The device should always appear to be in use
Masanari Iida02582e92012-08-22 19:11:26 +0900225 * unless the driver supports autosuspend.
Alan Stern645daaa2006-08-30 15:47:02 -0400226 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500227 if (!udriver->supports_autosuspend)
228 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400229
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500230 if (!error)
231 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400232 return error;
233}
234
235/* called from driver core with dev locked */
236static int usb_unbind_device(struct device *dev)
237{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500238 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400239 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
240
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500241 udriver->disconnect(udev);
242 if (!udriver->supports_autosuspend)
243 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400244 return 0;
245}
246
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800247/*
248 * Cancel any pending scheduled resets
249 *
250 * [see usb_queue_reset_device()]
251 *
252 * Called after unconfiguring / when releasing interfaces. See
253 * comments in __usb_queue_reset_device() regarding
254 * udev->reset_running.
255 */
256static void usb_cancel_queued_reset(struct usb_interface *iface)
257{
258 if (iface->reset_running == 0)
259 cancel_work_sync(&iface->reset_ws);
260}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400261
262/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800263static int usb_probe_interface(struct device *dev)
264{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400265 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200266 struct usb_interface *intf = to_usb_interface(dev);
267 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800268 const struct usb_device_id *id;
269 int error = -ENODEV;
Sarah Sharp83060952012-05-02 14:25:52 -0700270 int lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800271
Harvey Harrison441b62c2008-03-03 16:08:34 -0800272 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800273
Alan Stern78d9a482008-06-23 16:00:40 -0400274 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800275
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400276 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500277 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400278
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800279 if (udev->authorized == 0) {
280 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500281 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800282 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700283
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800284 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800285 if (!id)
286 id = usb_match_dynamic_id(intf, driver);
Alan Stern0f3dda92010-01-08 12:56:04 -0500287 if (!id)
288 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800289
Alan Stern0f3dda92010-01-08 12:56:04 -0500290 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400291
Alan Stern0f3dda92010-01-08 12:56:04 -0500292 error = usb_autoresume_device(udev);
293 if (error)
294 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400295
Alan Stern0f3dda92010-01-08 12:56:04 -0500296 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400297
Alan Stern571dc792010-04-09 16:03:43 -0400298 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500299 * runtime-PM-enabled only if the driver has autosuspend support.
300 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500301 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500302 pm_runtime_set_active(dev);
303 pm_suspend_ignore_children(dev, false);
304 if (driver->supports_autosuspend)
305 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200306
Sarah Sharp83060952012-05-02 14:25:52 -0700307 /* If the new driver doesn't allow hub-initiated LPM, and we can't
308 * disable hub-initiated LPM, then fail the probe.
309 *
310 * Otherwise, leaving LPM enabled should be harmless, because the
311 * endpoint intervals should remain the same, and the U1/U2 timeouts
312 * should remain the same.
313 *
314 * If we need to install alt setting 0 before probe, or another alt
315 * setting during probe, that should also be fine. usb_set_interface()
316 * will attempt to disable LPM, and fail if it can't disable it.
317 */
318 lpm_disable_error = usb_unlocked_disable_lpm(udev);
319 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
320 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
321 __func__, driver->name);
322 error = lpm_disable_error;
323 goto err;
324 }
325
Alan Stern0f3dda92010-01-08 12:56:04 -0500326 /* Carry out a deferred switch to altsetting 0 */
327 if (intf->needs_altsetting0) {
328 error = usb_set_interface(udev, intf->altsetting[0].
329 desc.bInterfaceNumber, 0);
330 if (error < 0)
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200331 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500332 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800333 }
334
Alan Stern0f3dda92010-01-08 12:56:04 -0500335 error = driver->probe(intf, id);
336 if (error)
337 goto err;
338
339 intf->condition = USB_INTERFACE_BOUND;
Sarah Sharp83060952012-05-02 14:25:52 -0700340
341 /* If the LPM disable succeeded, balance the ref counts. */
342 if (!lpm_disable_error)
343 usb_unlocked_enable_lpm(udev);
344
Alan Stern0f3dda92010-01-08 12:56:04 -0500345 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800346 return error;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200347
Alan Stern0f3dda92010-01-08 12:56:04 -0500348 err:
Hans de Goedee714fad2012-05-22 11:36:59 +0200349 usb_set_intfdata(intf, NULL);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200350 intf->needs_remote_wakeup = 0;
351 intf->condition = USB_INTERFACE_UNBOUND;
352 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500353
Sarah Sharpd01f87c2012-10-04 09:53:43 -0700354 /* If the LPM disable succeeded, balance the ref counts. */
355 if (!lpm_disable_error)
356 usb_unlocked_enable_lpm(udev);
357
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500358 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400359 if (driver->supports_autosuspend)
360 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500361 pm_runtime_set_suspended(dev);
362
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200363 usb_autosuspend_device(udev);
364 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800365}
366
Alan Stern8bb54ab2006-07-01 22:08:49 -0400367/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800368static int usb_unbind_interface(struct device *dev)
369{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400370 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800371 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400372 struct usb_device *udev;
Sarah Sharp83060952012-05-02 14:25:52 -0700373 int error, r, lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800374
375 intf->condition = USB_INTERFACE_UNBINDING;
376
Alan Stern645daaa2006-08-30 15:47:02 -0400377 /* Autoresume for set_interface call below */
378 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500379 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400380
Sarah Sharp83060952012-05-02 14:25:52 -0700381 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
382 * the driver is unbound. If LPM isn't disabled, that's fine because it
383 * wouldn't be enabled unless all the bound interfaces supported
384 * hub-initiated LPM.
385 */
386 lpm_disable_error = usb_unlocked_disable_lpm(udev);
387
Alan Stern9da82bd2008-05-08 11:54:37 -0400388 /* Terminate all URBs for this interface unless the driver
389 * supports "soft" unbinding.
390 */
391 if (!driver->soft_unbind)
Alan Sternddeac4e2009-01-15 17:03:33 -0500392 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800393
Alan Stern8bb54ab2006-07-01 22:08:49 -0400394 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800395 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800396
Alan Stern55151d72008-08-12 14:33:59 -0400397 /* Reset other interface state.
398 * We cannot do a Set-Interface if the device is suspended or
399 * if it is prepared for a system sleep (since installing a new
400 * altsetting means creating new endpoint device entries).
401 * When either of these happens, defer the Set-Interface.
402 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500403 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
404 /* Already in altsetting 0 so skip Set-Interface.
405 * Just re-enable it without affecting the endpoint toggles.
406 */
407 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200408 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200409 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400410 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200411 if (r < 0)
412 intf->needs_altsetting0 = 1;
413 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400414 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200415 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800416 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400417
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800418 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400419 intf->needs_remote_wakeup = 0;
420
Sarah Sharp83060952012-05-02 14:25:52 -0700421 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
422 if (!lpm_disable_error)
423 usb_unlocked_enable_lpm(udev);
424
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500425 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400426 if (driver->supports_autosuspend)
427 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500428 pm_runtime_set_suspended(dev);
429
430 /* Undo any residual pm_autopm_get_interface_* calls */
431 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
432 usb_autopm_put_interface_no_suspend(intf);
433 atomic_set(&intf->pm_usage_cnt, 0);
434
Alan Stern645daaa2006-08-30 15:47:02 -0400435 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500436 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800437
438 return 0;
439}
440
Alan Stern36e56a32006-07-01 22:08:06 -0400441/**
442 * usb_driver_claim_interface - bind a driver to an interface
443 * @driver: the driver to be bound
444 * @iface: the interface to which it will be bound; must be in the
445 * usb device's active configuration
446 * @priv: driver data associated with that interface
447 *
448 * This is used by usb device drivers that need to claim more than one
449 * interface on a device when probing (audio and acm are current examples).
450 * No device driver should directly modify internal usb_interface or
451 * usb_device structure members.
452 *
453 * Few drivers should need to use this routine, since the most natural
454 * way to bind to an interface is to return the private data from
455 * the driver's probe() method.
456 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400457 * Callers must own the device lock, so driver probe() entries don't need
458 * extra locking, but other call contexts may need to explicitly claim that
459 * lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400460 */
461int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800462 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400463{
464 struct device *dev = &iface->dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700465 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700466 int retval = 0;
Sarah Sharp83060952012-05-02 14:25:52 -0700467 int lpm_disable_error;
Alan Stern36e56a32006-07-01 22:08:06 -0400468
469 if (dev->driver)
470 return -EBUSY;
471
Sarah Sharp83060952012-05-02 14:25:52 -0700472 udev = interface_to_usbdev(iface);
473
Alan Stern8bb54ab2006-07-01 22:08:49 -0400474 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400475 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400476 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400477
Alan Stern36e56a32006-07-01 22:08:06 -0400478 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500479
Sarah Sharp83060952012-05-02 14:25:52 -0700480 /* Disable LPM until this driver is bound. */
481 lpm_disable_error = usb_unlocked_disable_lpm(udev);
482 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
483 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
484 __func__, driver->name);
485 return -ENOMEM;
486 }
487
Alan Stern89842ae2010-05-11 11:44:06 -0400488 /* Claimed interfaces are initially inactive (suspended) and
489 * runtime-PM-enabled, but only if the driver has autosuspend
490 * support. Otherwise they are marked active, to prevent the
491 * device from being autosuspended, but left disabled. In either
492 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500493 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500494 pm_suspend_ignore_children(dev, false);
495 if (driver->supports_autosuspend)
496 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400497 else
498 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400499
500 /* if interface was already added, bind now; else let
501 * the future device_add() bind it, bypassing probe()
502 */
503 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700504 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400505
Sarah Sharp83060952012-05-02 14:25:52 -0700506 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
507 if (!lpm_disable_error)
508 usb_unlocked_enable_lpm(udev);
509
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700510 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400511}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600512EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400513
514/**
515 * usb_driver_release_interface - unbind a driver from an interface
516 * @driver: the driver to be unbound
517 * @iface: the interface from which it will be unbound
518 *
519 * This can be used by drivers to release an interface without waiting
520 * for their disconnect() methods to be called. In typical cases this
521 * also causes the driver disconnect() method to be called.
522 *
523 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400524 * Callers must own the device lock, so driver disconnect() entries don't
525 * need extra locking, but other call contexts may need to explicitly claim
526 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400527 */
528void usb_driver_release_interface(struct usb_driver *driver,
529 struct usb_interface *iface)
530{
531 struct device *dev = &iface->dev;
532
533 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400534 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400535 return;
536
537 /* don't release from within disconnect() */
538 if (iface->condition != USB_INTERFACE_BOUND)
539 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400540 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400541
Alan Stern91f8d062009-04-16 15:35:09 -0400542 /* Release via the driver core only if the interface
543 * has already been registered
544 */
Alan Stern36e56a32006-07-01 22:08:06 -0400545 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400546 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800547 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800548 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400549 usb_unbind_interface(dev);
550 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800551 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400552 }
Alan Stern36e56a32006-07-01 22:08:06 -0400553}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600554EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400555
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800556/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100557int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800558{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800559 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
560 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
561 return 0;
562
563 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
564 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
565 return 0;
566
567 /* No need to test id->bcdDevice_lo != 0, since 0 is never
568 greater than any unsigned number. */
569 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
570 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
571 return 0;
572
573 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
574 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
575 return 0;
576
577 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
578 (id->bDeviceClass != dev->descriptor.bDeviceClass))
579 return 0;
580
581 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800582 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800583 return 0;
584
585 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
586 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
587 return 0;
588
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100589 return 1;
590}
591
592/* returns 0 if no match, 1 if match */
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200593int usb_match_one_id_intf(struct usb_device *dev,
594 struct usb_host_interface *intf,
595 const struct usb_device_id *id)
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100596{
Bjørn Mork81df2d52012-05-18 21:27:43 +0200597 /* The interface class, subclass, protocol and number should never be
Alan Stern93c8bf42006-10-18 16:41:51 -0400598 * checked for a match if the device class is Vendor Specific,
599 * unless the match record specifies the Vendor ID. */
600 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
601 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
602 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
603 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
Bjørn Mork81df2d52012-05-18 21:27:43 +0200604 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
605 USB_DEVICE_ID_MATCH_INT_NUMBER)))
Alan Stern93c8bf42006-10-18 16:41:51 -0400606 return 0;
607
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800608 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
609 (id->bInterfaceClass != intf->desc.bInterfaceClass))
610 return 0;
611
612 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
613 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
614 return 0;
615
616 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
617 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
618 return 0;
619
Bjørn Mork81df2d52012-05-18 21:27:43 +0200620 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
621 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
622 return 0;
623
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800624 return 1;
625}
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200626
627/* returns 0 if no match, 1 if match */
628int usb_match_one_id(struct usb_interface *interface,
629 const struct usb_device_id *id)
630{
631 struct usb_host_interface *intf;
632 struct usb_device *dev;
633
634 /* proc_connectinfo in devio.c may call us with id == NULL. */
635 if (id == NULL)
636 return 0;
637
638 intf = interface->cur_altsetting;
639 dev = interface_to_usbdev(interface);
640
641 if (!usb_match_device(dev, id))
642 return 0;
643
644 return usb_match_one_id_intf(dev, intf, id);
645}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100646EXPORT_SYMBOL_GPL(usb_match_one_id);
647
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800648/**
649 * usb_match_id - find first usb_device_id matching device or interface
650 * @interface: the interface of interest
651 * @id: array of usb_device_id structures, terminated by zero entry
652 *
653 * usb_match_id searches an array of usb_device_id's and returns
654 * the first one matching the device or interface, or null.
655 * This is used when binding (or rebinding) a driver to an interface.
656 * Most USB device drivers will use this indirectly, through the usb core,
657 * but some layered driver frameworks use it directly.
658 * These device tables are exported with MODULE_DEVICE_TABLE, through
659 * modutils, to support the driver loading functionality of USB hotplugging.
660 *
661 * What Matches:
662 *
663 * The "match_flags" element in a usb_device_id controls which
664 * members are used. If the corresponding bit is set, the
665 * value in the device_id must match its corresponding member
666 * in the device or interface descriptor, or else the device_id
667 * does not match.
668 *
669 * "driver_info" is normally used only by device drivers,
670 * but you can create a wildcard "matches anything" usb_device_id
671 * as a driver's "modules.usbmap" entry if you provide an id with
672 * only a nonzero "driver_info" field. If you do this, the USB device
673 * driver's probe() routine should use additional intelligence to
674 * decide whether to bind to the specified interface.
675 *
676 * What Makes Good usb_device_id Tables:
677 *
678 * The match algorithm is very simple, so that intelligence in
679 * driver selection must come from smart driver id records.
680 * Unless you have good reasons to use another selection policy,
681 * provide match elements only in related groups, and order match
682 * specifiers from specific to general. Use the macros provided
683 * for that purpose if you can.
684 *
685 * The most specific match specifiers use device descriptor
686 * data. These are commonly used with product-specific matches;
687 * the USB_DEVICE macro lets you provide vendor and product IDs,
688 * and you can also match against ranges of product revisions.
689 * These are widely used for devices with application or vendor
690 * specific bDeviceClass values.
691 *
692 * Matches based on device class/subclass/protocol specifications
693 * are slightly more general; use the USB_DEVICE_INFO macro, or
694 * its siblings. These are used with single-function devices
695 * where bDeviceClass doesn't specify that each interface has
696 * its own class.
697 *
698 * Matches based on interface class/subclass/protocol are the
699 * most general; they let drivers bind to any interface on a
700 * multiple-function device. Use the USB_INTERFACE_INFO
701 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400702 * devices (as recorded in bInterfaceClass).
703 *
704 * Note that an entry created by USB_INTERFACE_INFO won't match
705 * any interface if the device class is set to Vendor-Specific.
706 * This is deliberate; according to the USB spec the meanings of
707 * the interface class/subclass/protocol for these devices are also
708 * vendor-specific, and hence matching against a standard product
709 * class wouldn't work anyway. If you really want to use an
710 * interface-based match for such a device, create a match record
711 * that also specifies the vendor ID. (Unforunately there isn't a
712 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800713 *
714 * Within those groups, remember that not all combinations are
715 * meaningful. For example, don't give a product version range
716 * without vendor and product IDs; or specify a protocol without
717 * its associated class and subclass.
718 */
719const struct usb_device_id *usb_match_id(struct usb_interface *interface,
720 const struct usb_device_id *id)
721{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800722 /* proc_connectinfo in devio.c may call us with id == NULL. */
723 if (id == NULL)
724 return NULL;
725
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800726 /* It is important to check that id->driver_info is nonzero,
727 since an entry that is all zeroes except for a nonzero
728 id->driver_info is the way to create an entry that
729 indicates that the driver want to examine every
730 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800731 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
732 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800733 if (usb_match_one_id(interface, id))
734 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800735 }
736
737 return NULL;
738}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600739EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800740
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100741static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800742{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400743 /* devices and interfaces are handled separately */
744 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800745
Alan Stern8bb54ab2006-07-01 22:08:49 -0400746 /* interface drivers never match devices */
747 if (!is_usb_device_driver(drv))
748 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800749
Alan Stern8bb54ab2006-07-01 22:08:49 -0400750 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800751 return 1;
752
Kay Sievers55129662009-05-04 19:48:32 +0200753 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400754 struct usb_interface *intf;
755 struct usb_driver *usb_drv;
756 const struct usb_device_id *id;
757
758 /* device drivers never match interfaces */
759 if (is_usb_device_driver(drv))
760 return 0;
761
762 intf = to_usb_interface(dev);
763 usb_drv = to_usb_driver(drv);
764
765 id = usb_match_id(intf, usb_drv->id_table);
766 if (id)
767 return 1;
768
769 id = usb_match_dynamic_id(intf, usb_drv);
770 if (id)
771 return 1;
772 }
773
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800774 return 0;
775}
776
Kay Sievers7eff2e72007-08-14 15:15:12 +0200777static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400778{
Alan Stern36e56a32006-07-01 22:08:06 -0400779 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400780
Kay Sievers55129662009-05-04 19:48:32 +0200781 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400782 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200783 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100784 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200785
Alan Stern8bb54ab2006-07-01 22:08:49 -0400786 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200787 } else {
788 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400789 }
Alan Stern36e56a32006-07-01 22:08:06 -0400790
791 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500792 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200793 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400794 return -ENODEV;
795 }
796 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200797 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400798 return -ENODEV;
799 }
800
Alan Stern36e56a32006-07-01 22:08:06 -0400801 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200802 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400803 le16_to_cpu(usb_dev->descriptor.idVendor),
804 le16_to_cpu(usb_dev->descriptor.idProduct),
805 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
806 return -ENOMEM;
807
808 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200809 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400810 usb_dev->descriptor.bDeviceClass,
811 usb_dev->descriptor.bDeviceSubClass,
812 usb_dev->descriptor.bDeviceProtocol))
813 return -ENOMEM;
814
Alan Stern36e56a32006-07-01 22:08:06 -0400815 return 0;
816}
817
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800818/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400819 * usb_register_device_driver - register a USB device (not interface) driver
820 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800821 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800822 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400823 * Registers a USB device driver with the USB core. The list of
824 * unattached devices will be rescanned whenever a new driver is
825 * added, allowing the new driver to attach to any recognized devices.
826 * Returns a negative error code on failure and 0 on success.
827 */
828int usb_register_device_driver(struct usb_device_driver *new_udriver,
829 struct module *owner)
830{
831 int retval = 0;
832
833 if (usb_disabled())
834 return -ENODEV;
835
836 new_udriver->drvwrap.for_devices = 1;
837 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
838 new_udriver->drvwrap.driver.bus = &usb_bus_type;
839 new_udriver->drvwrap.driver.probe = usb_probe_device;
840 new_udriver->drvwrap.driver.remove = usb_unbind_device;
841 new_udriver->drvwrap.driver.owner = owner;
842
843 retval = driver_register(&new_udriver->drvwrap.driver);
844
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700845 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400846 pr_info("%s: registered new device driver %s\n",
847 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700848 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400849 printk(KERN_ERR "%s: error %d registering device "
850 " driver %s\n",
851 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400852
853 return retval;
854}
855EXPORT_SYMBOL_GPL(usb_register_device_driver);
856
857/**
858 * usb_deregister_device_driver - unregister a USB device (not interface) driver
859 * @udriver: USB operations of the device driver to unregister
860 * Context: must be able to sleep
861 *
862 * Unlinks the specified driver from the internal USB driver list.
863 */
864void usb_deregister_device_driver(struct usb_device_driver *udriver)
865{
866 pr_info("%s: deregistering device driver %s\n",
867 usbcore_name, udriver->name);
868
869 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400870}
871EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
872
873/**
874 * usb_register_driver - register a USB interface driver
875 * @new_driver: USB operations for the interface driver
876 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800877 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400878 *
879 * Registers a USB interface driver with the USB core. The list of
880 * unattached interfaces will be rescanned whenever a new driver is
881 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800882 * Returns a negative error code on failure and 0 on success.
883 *
884 * NOTE: if you want your driver to use the USB major number, you must call
885 * usb_register_dev() to enable that functionality. This function no longer
886 * takes care of that.
887 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800888int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
889 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800890{
891 int retval = 0;
892
893 if (usb_disabled())
894 return -ENODEV;
895
Alan Stern8bb54ab2006-07-01 22:08:49 -0400896 new_driver->drvwrap.for_devices = 0;
897 new_driver->drvwrap.driver.name = (char *) new_driver->name;
898 new_driver->drvwrap.driver.bus = &usb_bus_type;
899 new_driver->drvwrap.driver.probe = usb_probe_interface;
900 new_driver->drvwrap.driver.remove = usb_unbind_interface;
901 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800902 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800903 spin_lock_init(&new_driver->dynids.lock);
904 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800905
Alan Stern8bb54ab2006-07-01 22:08:49 -0400906 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800907 if (retval)
908 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800909
Alan Sterned283e92012-01-24 14:35:13 -0500910 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800911 if (retval)
912 goto out_newid;
913
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800914 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800915 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800916
917out:
918 return retval;
919
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800920out_newid:
921 driver_unregister(&new_driver->drvwrap.driver);
922
923 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400924 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800925 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800926 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800927}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600928EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800929
930/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400931 * usb_deregister - unregister a USB interface driver
932 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800933 * Context: must be able to sleep
934 *
935 * Unlinks the specified driver from the internal USB driver list.
936 *
937 * NOTE: If you called usb_register_dev(), you still need to call
938 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
939 * this * call will no longer do it for you.
940 */
941void usb_deregister(struct usb_driver *driver)
942{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400943 pr_info("%s: deregistering interface driver %s\n",
944 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800945
Alan Sterned283e92012-01-24 14:35:13 -0500946 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400947 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -0500948 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800949}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600950EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400951
Alan Stern78d9a482008-06-23 16:00:40 -0400952/* Forced unbinding of a USB interface driver, either because
953 * it doesn't support pre_reset/post_reset/reset_resume or
954 * because it doesn't support suspend/resume.
955 *
956 * The caller must hold @intf's device's lock, but not its pm_mutex
957 * and not @intf->dev.sem.
958 */
959void usb_forced_unbind_intf(struct usb_interface *intf)
960{
961 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
962
963 dev_dbg(&intf->dev, "forced unbind\n");
964 usb_driver_release_interface(driver, intf);
965
966 /* Mark the interface for later rebinding */
967 intf->needs_binding = 1;
968}
969
970/* Delayed forced unbinding of a USB interface driver and scan
971 * for rebinding.
972 *
973 * The caller must hold @intf's device's lock, but not its pm_mutex
974 * and not @intf->dev.sem.
975 *
Alan Stern5096aed2008-08-12 14:34:14 -0400976 * Note: Rebinds will be skipped if a system sleep transition is in
977 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -0400978 */
979void usb_rebind_intf(struct usb_interface *intf)
980{
981 int rc;
982
983 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +0100984 if (intf->dev.driver)
985 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -0400986
987 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +0200988 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -0400989 intf->needs_binding = 0;
990 rc = device_attach(&intf->dev);
991 if (rc < 0)
992 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
993 }
Alan Stern78d9a482008-06-23 16:00:40 -0400994}
995
Alan Stern9ff78432008-08-07 13:04:51 -0400996#ifdef CONFIG_PM
997
Oliver Neukum14931382012-01-05 15:39:57 +0100998/* Unbind drivers for @udev's interfaces that don't support suspend/resume
999 * There is no check for reset_resume here because it can be determined
1000 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001001 *
1002 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001003 */
Oliver Neukum14931382012-01-05 15:39:57 +01001004static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001005{
1006 struct usb_host_config *config;
1007 int i;
1008 struct usb_interface *intf;
1009 struct usb_driver *drv;
1010
1011 config = udev->actconfig;
1012 if (config) {
1013 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1014 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001015
1016 if (intf->dev.driver) {
1017 drv = to_usb_driver(intf->dev.driver);
1018 if (!drv->suspend || !drv->resume)
1019 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001020 }
1021 }
1022 }
1023}
1024
Oliver Neukum14931382012-01-05 15:39:57 +01001025/* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1026 * These interfaces have the needs_binding flag set by usb_resume_interface().
1027 *
1028 * The caller must hold @udev's device lock.
1029 */
1030static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
1031{
1032 struct usb_host_config *config;
1033 int i;
1034 struct usb_interface *intf;
1035
1036 config = udev->actconfig;
1037 if (config) {
1038 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1039 intf = config->interface[i];
1040 if (intf->dev.driver && intf->needs_binding)
1041 usb_forced_unbind_intf(intf);
1042 }
1043 }
1044}
1045
1046static void do_rebind_interfaces(struct usb_device *udev)
1047{
1048 struct usb_host_config *config;
1049 int i;
1050 struct usb_interface *intf;
1051
1052 config = udev->actconfig;
1053 if (config) {
1054 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1055 intf = config->interface[i];
1056 if (intf->needs_binding)
1057 usb_rebind_intf(intf);
1058 }
1059 }
1060}
1061
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001062static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001063{
Alan Stern782da722006-07-01 22:09:35 -04001064 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001065 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001066
Alan Stern114b3682006-07-01 22:13:04 -04001067 if (udev->state == USB_STATE_NOTATTACHED ||
1068 udev->state == USB_STATE_SUSPENDED)
1069 goto done;
1070
Alan Sternb6f64362007-05-04 11:51:54 -04001071 /* For devices that don't have a driver, we do a generic suspend. */
1072 if (udev->dev.driver)
1073 udriver = to_usb_device_driver(udev->dev.driver);
1074 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001075 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001076 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001077 }
Alan Stern2bf40862006-07-01 22:12:19 -04001078 status = udriver->suspend(udev, msg);
1079
Alan Stern20dfdad2007-05-22 11:50:17 -04001080 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001081 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001082 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001083}
Alan Stern36e56a32006-07-01 22:08:06 -04001084
Alan Stern65bfd292008-11-25 16:39:18 -05001085static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001086{
1087 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001088 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001089
Alan Stern0458d5b2007-05-04 11:52:20 -04001090 if (udev->state == USB_STATE_NOTATTACHED)
1091 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001092
Alan Stern1c5df7e2006-07-01 22:13:50 -04001093 /* Can't resume it if it doesn't have a driver. */
1094 if (udev->dev.driver == NULL) {
1095 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001096 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001097 }
1098
Alan Stern6d19c002010-02-12 12:21:11 +01001099 /* Non-root devices on a full/low-speed bus must wait for their
1100 * companion high-speed root hub, in case a handoff is needed.
1101 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001102 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001103 device_pm_wait_for_dev(&udev->dev,
1104 &udev->bus->hs_companion->root_hub->dev);
1105
Alan Stern6bc6cff2007-05-04 11:53:03 -04001106 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1107 udev->reset_resume = 1;
1108
Alan Stern1cc8a252006-07-01 22:10:15 -04001109 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001110 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001111
Alan Stern20dfdad2007-05-22 11:50:17 -04001112 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001113 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001114 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001115}
1116
Alan Stern65605ae2008-08-12 14:33:27 -04001117static int usb_suspend_interface(struct usb_device *udev,
1118 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001119{
1120 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001121 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001122
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001123 if (udev->state == USB_STATE_NOTATTACHED ||
1124 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001125 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001126 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001127
Oliver Neukume78832c2012-01-02 15:11:48 +01001128 /* at this time we know the driver supports suspend */
1129 status = driver->suspend(intf, msg);
1130 if (status && !PMSG_IS_AUTO(msg))
1131 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001132
Alan Stern20dfdad2007-05-22 11:50:17 -04001133 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001134 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001135 return status;
1136}
1137
Alan Stern65605ae2008-08-12 14:33:27 -04001138static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001139 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001140{
Alan Stern1cc8a252006-07-01 22:10:15 -04001141 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001142 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001143
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001144 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001145 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001146
Alan Stern645daaa2006-08-30 15:47:02 -04001147 /* Don't let autoresume interfere with unbinding */
1148 if (intf->condition == USB_INTERFACE_UNBINDING)
1149 goto done;
1150
Alan Stern1c5df7e2006-07-01 22:13:50 -04001151 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001152 if (intf->condition == USB_INTERFACE_UNBOUND) {
1153
1154 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001155 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001156 usb_set_interface(udev, intf->altsetting[0].
1157 desc.bInterfaceNumber, 0);
1158 intf->needs_altsetting0 = 0;
1159 }
Alan Stern2bf40862006-07-01 22:12:19 -04001160 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001161 }
Alan Stern78d9a482008-06-23 16:00:40 -04001162
1163 /* Don't resume if the interface is marked for rebinding */
1164 if (intf->needs_binding)
1165 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001166 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001167
Alan Sternf07600c2007-05-30 15:38:16 -04001168 if (reset_resume) {
1169 if (driver->reset_resume) {
1170 status = driver->reset_resume(intf);
1171 if (status)
1172 dev_err(&intf->dev, "%s error %d\n",
1173 "reset_resume", status);
1174 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001175 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001176 dev_warn(&intf->dev, "no %s for driver %s?\n",
1177 "reset_resume", driver->name);
1178 }
1179 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001180 status = driver->resume(intf);
1181 if (status)
1182 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001183 }
Alan Stern2bf40862006-07-01 22:12:19 -04001184
1185done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001186 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001187
Alan Stern78d9a482008-06-23 16:00:40 -04001188 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001189 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001190}
1191
Alan Stern645daaa2006-08-30 15:47:02 -04001192/**
1193 * usb_suspend_both - suspend a USB device and its interfaces
1194 * @udev: the usb_device to suspend
1195 * @msg: Power Management message describing this state transition
1196 *
1197 * This is the central routine for suspending USB devices. It calls the
1198 * suspend methods for all the interface drivers in @udev and then calls
Ming Lei303f0842013-03-15 12:08:53 +08001199 * the suspend method for @udev itself. When the routine is called in
1200 * autosuspend, if an error occurs at any stage, all the interfaces
1201 * which were suspended are resumed so that they remain in the same
1202 * state as the device, but when called from system sleep, all error
1203 * from suspend methods of interfaces and the non-root-hub device itself
1204 * are simply ignored, so all suspended interfaces are only resumed
1205 * to the device's state when @udev is root-hub and its suspend method
1206 * returns failure.
Alan Stern645daaa2006-08-30 15:47:02 -04001207 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001208 * Autosuspend requests originating from a child device or an interface
1209 * driver may be made without the protection of @udev's device lock, but
1210 * all other suspend calls will hold the lock. Usbcore will insure that
1211 * method calls do not arrive during bind, unbind, or reset operations.
1212 * However drivers must be prepared to handle suspend calls arriving at
1213 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001214 *
1215 * This routine can run only in process context.
1216 */
Alan Stern718efa62007-03-09 15:41:13 -05001217static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001218{
1219 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001220 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001221 struct usb_interface *intf;
1222
Alan Stern19410442007-03-27 13:33:59 -04001223 if (udev->state == USB_STATE_NOTATTACHED ||
1224 udev->state == USB_STATE_SUSPENDED)
1225 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001226
Alan Stern645daaa2006-08-30 15:47:02 -04001227 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001228 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001229 n = udev->actconfig->desc.bNumInterfaces;
1230 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001231 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001232 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001233
1234 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001235 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001236 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001237 if (status != 0)
1238 break;
1239 }
1240 }
Alan Stern0af212b2011-06-15 16:27:43 -04001241 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001242 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001243
Alan Sterncd4376e2012-03-28 15:56:17 -04001244 /*
1245 * Ignore errors from non-root-hub devices during
1246 * system sleep transitions. For the most part,
1247 * these devices should go to low power anyway when
1248 * the entire bus is suspended.
1249 */
1250 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001251 status = 0;
1252 }
1253
Alan Sterna8e7c562006-07-01 22:11:02 -04001254 /* If the suspend failed, resume interfaces that did get suspended */
1255 if (status != 0) {
Chen Gang505bdbc2013-04-01 13:04:08 +08001256 if (udev->actconfig) {
1257 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1258 while (++i < n) {
1259 intf = udev->actconfig->interface[i];
1260 usb_resume_interface(udev, intf, msg, 0);
1261 }
Alan Sterna8e7c562006-07-01 22:11:02 -04001262 }
Alan Stern645daaa2006-08-30 15:47:02 -04001263
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001264 /* If the suspend succeeded then prevent any more URB submissions
1265 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001266 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001267 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001268 udev->can_submit = 0;
1269 for (i = 0; i < 16; ++i) {
1270 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1271 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1272 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001273 }
Alan Stern645daaa2006-08-30 15:47:02 -04001274
Alan Stern19410442007-03-27 13:33:59 -04001275 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001276 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001277 return status;
1278}
1279
Alan Stern645daaa2006-08-30 15:47:02 -04001280/**
1281 * usb_resume_both - resume a USB device and its interfaces
1282 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001283 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001284 *
1285 * This is the central routine for resuming USB devices. It calls the
1286 * the resume method for @udev and then calls the resume methods for all
1287 * the interface drivers in @udev.
1288 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001289 * Autoresume requests originating from a child device or an interface
1290 * driver may be made without the protection of @udev's device lock, but
1291 * all other resume calls will hold the lock. Usbcore will insure that
1292 * method calls do not arrive during bind, unbind, or reset operations.
1293 * However drivers must be prepared to handle resume calls arriving at
1294 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001295 *
1296 * This routine can run only in process context.
1297 */
Alan Stern65bfd292008-11-25 16:39:18 -05001298static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001299{
Alan Stern645daaa2006-08-30 15:47:02 -04001300 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001301 int i;
1302 struct usb_interface *intf;
1303
Alan Stern19410442007-03-27 13:33:59 -04001304 if (udev->state == USB_STATE_NOTATTACHED) {
1305 status = -ENODEV;
1306 goto done;
1307 }
Alan Stern6840d252007-09-10 11:34:26 -04001308 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001309
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001310 /* Resume the device */
1311 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001312 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001313
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001314 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001315 if (status == 0 && udev->actconfig) {
1316 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1317 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001318 usb_resume_interface(udev, intf, msg,
1319 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001320 }
1321 }
Alan Sternc08512c2010-11-15 15:57:58 -05001322 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001323
Alan Stern19410442007-03-27 13:33:59 -04001324 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001325 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001326 if (!status)
1327 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001328 return status;
1329}
1330
Alan Stern5f677f12010-04-02 13:20:11 -04001331static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1332{
Alan Stern48826622010-06-22 16:14:48 -04001333 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001334
1335 /* Remote wakeup is needed only when we actually go to sleep.
1336 * For things like FREEZE and QUIESCE, if the device is already
1337 * autosuspended then its current wakeup setting is okay.
1338 */
1339 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1340 if (udev->state != USB_STATE_SUSPENDED)
1341 udev->do_remote_wakeup = 0;
1342 return;
1343 }
1344
Alan Stern48826622010-06-22 16:14:48 -04001345 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001346 * actually want it.
1347 */
Alan Stern48826622010-06-22 16:14:48 -04001348 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001349
1350 /* If the device is autosuspended with the wrong wakeup setting,
1351 * autoresume now so the setting can be changed.
1352 */
1353 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1354 pm_runtime_resume(&udev->dev);
1355 udev->do_remote_wakeup = w;
1356}
1357
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001358/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001359int usb_suspend(struct device *dev, pm_message_t msg)
1360{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001361 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001362
Oliver Neukum14931382012-01-05 15:39:57 +01001363 unbind_no_pm_drivers_interfaces(udev);
1364
1365 /* From now on we are sure all drivers support suspend/resume
1366 * but not necessarily reset_resume()
1367 * so we may still need to unbind and rebind upon resume
1368 */
Alan Stern5f677f12010-04-02 13:20:11 -04001369 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001370 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001371}
1372
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001373/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001374int usb_resume_complete(struct device *dev)
1375{
1376 struct usb_device *udev = to_usb_device(dev);
1377
1378 /* For PM complete calls, all we do is rebind interfaces
1379 * whose needs_binding flag is set
1380 */
1381 if (udev->state != USB_STATE_NOTATTACHED)
1382 do_rebind_interfaces(udev);
1383 return 0;
1384}
1385
1386/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001387int usb_resume(struct device *dev, pm_message_t msg)
1388{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001389 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001390 int status;
1391
Oliver Neukum98d9a822012-01-11 08:38:35 +01001392 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001393 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001394 * Unbind the interfaces that will need rebinding later,
1395 * because they fail to support reset_resume.
1396 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001397 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001398 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001399 status = usb_resume_both(udev, msg);
1400 if (status == 0) {
1401 pm_runtime_disable(dev);
1402 pm_runtime_set_active(dev);
1403 pm_runtime_enable(dev);
1404 unbind_no_reset_resume_drivers_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001405 }
Alan Stern0c590e22010-01-08 12:57:14 -05001406
1407 /* Avoid PM error messages for devices disconnected while suspended
1408 * as we'll display regular disconnect messages just a bit later.
1409 */
Peter Chen7491f132010-09-27 16:43:25 +08001410 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001411 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001412 return status;
1413}
1414
1415#endif /* CONFIG_PM */
1416
Alan Stern84ebc102013-03-27 16:14:46 -04001417#ifdef CONFIG_PM_RUNTIME
Alan Stern645daaa2006-08-30 15:47:02 -04001418
Alan Stern088f7fe2010-01-08 12:56:54 -05001419/**
1420 * usb_enable_autosuspend - allow a USB device to be autosuspended
1421 * @udev: the USB device which may be autosuspended
1422 *
1423 * This routine allows @udev to be autosuspended. An autosuspend won't
1424 * take place until the autosuspend_delay has elapsed and all the other
1425 * necessary conditions are satisfied.
1426 *
1427 * The caller must hold @udev's device lock.
1428 */
Alan Stern9e18c822010-04-02 13:22:09 -04001429void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001430{
Alan Stern9e18c822010-04-02 13:22:09 -04001431 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001432}
1433EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1434
1435/**
1436 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1437 * @udev: the USB device which may not be autosuspended
1438 *
1439 * This routine prevents @udev from being autosuspended and wakes it up
1440 * if it is already autosuspended.
1441 *
1442 * The caller must hold @udev's device lock.
1443 */
Alan Stern9e18c822010-04-02 13:22:09 -04001444void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001445{
Alan Stern9e18c822010-04-02 13:22:09 -04001446 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001447}
1448EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1449
Alan Stern645daaa2006-08-30 15:47:02 -04001450/**
1451 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001452 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001453 *
1454 * This routine should be called when a core subsystem is finished using
1455 * @udev and wants to allow it to autosuspend. Examples would be when
1456 * @udev's device file in usbfs is closed or after a configuration change.
1457 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001458 * @udev's usage counter is decremented; if it drops to 0 and all the
1459 * interfaces are inactive then a delayed autosuspend will be attempted.
1460 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001461 *
Alan Stern62e299e2010-01-08 12:56:19 -05001462 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001463 *
1464 * This routine can run only in process context.
1465 */
Alan Stern94fcda12006-11-20 11:38:46 -05001466void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001467{
Alan Stern94fcda12006-11-20 11:38:46 -05001468 int status;
1469
Ming Lei6ddf27c2010-11-15 15:57:30 -05001470 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001471 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001472 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1473 __func__, atomic_read(&udev->dev.power.usage_count),
1474 status);
Alan Stern19c26232007-02-20 15:03:32 -05001475}
1476
1477/**
Alan Stern645daaa2006-08-30 15:47:02 -04001478 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001479 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001480 *
1481 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001482 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001483 * occur until usb_autosuspend_device() is called. (Note that this will
1484 * not prevent suspend events originating in the PM core.) Examples would
1485 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001486 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001487 *
Alan Stern94fcda12006-11-20 11:38:46 -05001488 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1489 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001490 *
Alan Stern62e299e2010-01-08 12:56:19 -05001491 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001492 *
1493 * This routine can run only in process context.
1494 */
Alan Stern94fcda12006-11-20 11:38:46 -05001495int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001496{
1497 int status;
1498
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001499 status = pm_runtime_get_sync(&udev->dev);
1500 if (status < 0)
1501 pm_runtime_put_sync(&udev->dev);
1502 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1503 __func__, atomic_read(&udev->dev.power.usage_count),
1504 status);
1505 if (status > 0)
1506 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001507 return status;
1508}
1509
Alan Stern645daaa2006-08-30 15:47:02 -04001510/**
1511 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001512 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001513 *
1514 * This routine should be called by an interface driver when it is
1515 * finished using @intf and wants to allow it to autosuspend. A typical
1516 * example would be a character-device driver when its device file is
1517 * closed.
1518 *
1519 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001520 * 0, a delayed autosuspend request for @intf's device is attempted. The
1521 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001522 *
Alan Stern645daaa2006-08-30 15:47:02 -04001523 * This routine can run only in process context.
1524 */
1525void usb_autopm_put_interface(struct usb_interface *intf)
1526{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001527 struct usb_device *udev = interface_to_usbdev(intf);
1528 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001529
Ming Lei6ddf27c2010-11-15 15:57:30 -05001530 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001531 atomic_dec(&intf->pm_usage_cnt);
1532 status = pm_runtime_put_sync(&intf->dev);
1533 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1534 __func__, atomic_read(&intf->dev.power.usage_count),
1535 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001536}
1537EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1538
1539/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001540 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1541 * @intf: the usb_interface whose counter should be decremented
1542 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001543 * This routine does much the same thing as usb_autopm_put_interface():
1544 * It decrements @intf's usage counter and schedules a delayed
1545 * autosuspend request if the counter is <= 0. The difference is that it
1546 * does not perform any synchronization; callers should hold a private
1547 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001548 *
1549 * Typically a driver would call this routine during an URB's completion
1550 * handler, if no more URBs were pending.
1551 *
1552 * This routine can run in atomic context.
1553 */
1554void usb_autopm_put_interface_async(struct usb_interface *intf)
1555{
1556 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001557 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001558
Ming Lei6ddf27c2010-11-15 15:57:30 -05001559 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001560 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001561 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001562 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1563 __func__, atomic_read(&intf->dev.power.usage_count),
1564 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001565}
1566EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1567
1568/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001569 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1570 * @intf: the usb_interface whose counter should be decremented
1571 *
1572 * This routine decrements @intf's usage counter but does not carry out an
1573 * autosuspend.
1574 *
1575 * This routine can run in atomic context.
1576 */
1577void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1578{
1579 struct usb_device *udev = interface_to_usbdev(intf);
1580
Ming Lei6ddf27c2010-11-15 15:57:30 -05001581 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001582 atomic_dec(&intf->pm_usage_cnt);
1583 pm_runtime_put_noidle(&intf->dev);
1584}
1585EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1586
1587/**
Alan Stern645daaa2006-08-30 15:47:02 -04001588 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001589 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001590 *
1591 * This routine should be called by an interface driver when it wants to
1592 * use @intf and needs to guarantee that it is not suspended. In addition,
1593 * the routine prevents @intf from being autosuspended subsequently. (Note
1594 * that this will not prevent suspend events originating in the PM core.)
1595 * This prevention will persist until usb_autopm_put_interface() is called
1596 * or @intf is unbound. A typical example would be a character-device
1597 * driver when its device file is opened.
1598 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001599 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1600 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001601 *
1602 * This routine can run only in process context.
1603 */
1604int usb_autopm_get_interface(struct usb_interface *intf)
1605{
Alan Sternaf4f7602006-10-30 17:06:45 -05001606 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001607
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001608 status = pm_runtime_get_sync(&intf->dev);
1609 if (status < 0)
1610 pm_runtime_put_sync(&intf->dev);
1611 else
1612 atomic_inc(&intf->pm_usage_cnt);
1613 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1614 __func__, atomic_read(&intf->dev.power.usage_count),
1615 status);
1616 if (status > 0)
1617 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001618 return status;
1619}
1620EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1621
Alan Stern692a1862006-10-30 17:07:51 -05001622/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001623 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1624 * @intf: the usb_interface whose counter should be incremented
1625 *
1626 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001627 * usb_autopm_get_interface(): It increments @intf's usage counter and
1628 * queues an autoresume request if the device is suspended. The
1629 * differences are that it does not perform any synchronization (callers
1630 * should hold a private lock and handle all synchronization issues
1631 * themselves), and it does not autoresume the device directly (it only
1632 * queues a request). After a successful call, the device may not yet be
1633 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001634 *
1635 * This routine can run in atomic context.
1636 */
1637int usb_autopm_get_interface_async(struct usb_interface *intf)
1638{
Ming Lei63defa72010-11-15 15:56:54 -05001639 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001640
Ming Lei63defa72010-11-15 15:56:54 -05001641 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001642 if (status < 0 && status != -EINPROGRESS)
1643 pm_runtime_put_noidle(&intf->dev);
1644 else
Alan Sternccf5b802009-06-29 11:00:01 -04001645 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001646 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1647 __func__, atomic_read(&intf->dev.power.usage_count),
1648 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001649 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001650 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001651 return status;
1652}
1653EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1654
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001655/**
1656 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1657 * @intf: the usb_interface whose counter should be incremented
1658 *
1659 * This routine increments @intf's usage counter but does not carry out an
1660 * autoresume.
1661 *
1662 * This routine can run in atomic context.
1663 */
1664void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1665{
1666 struct usb_device *udev = interface_to_usbdev(intf);
1667
Ming Lei6ddf27c2010-11-15 15:57:30 -05001668 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001669 atomic_inc(&intf->pm_usage_cnt);
1670 pm_runtime_get_noresume(&intf->dev);
1671}
1672EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1673
1674/* Internal routine to check whether we may autosuspend a device. */
1675static int autosuspend_check(struct usb_device *udev)
1676{
Alan Stern7560d32e2010-04-02 13:18:50 -04001677 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001678 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001679
1680 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1681 * any interface drivers require remote wakeup but it isn't available.
1682 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001683 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001684 if (udev->actconfig) {
1685 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1686 intf = udev->actconfig->interface[i];
1687
1688 /* We don't need to check interfaces that are
1689 * disabled for runtime PM. Either they are unbound
1690 * or else their drivers don't support autosuspend
1691 * and so they are permanently active.
1692 */
1693 if (intf->dev.power.disable_depth)
1694 continue;
1695 if (atomic_read(&intf->dev.power.usage_count) > 0)
1696 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001697 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001698
1699 /* Don't allow autosuspend if the device will need
1700 * a reset-resume and any of its interface drivers
1701 * doesn't include support or needs remote wakeup.
1702 */
1703 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1704 struct usb_driver *driver;
1705
1706 driver = to_usb_driver(intf->dev.driver);
1707 if (!driver->reset_resume ||
1708 intf->needs_remote_wakeup)
1709 return -EOPNOTSUPP;
1710 }
1711 }
1712 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001713 if (w && !device_can_wakeup(&udev->dev)) {
1714 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1715 return -EOPNOTSUPP;
1716 }
1717 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001718 return 0;
1719}
1720
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001721int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001722{
Ming Lei63defa72010-11-15 15:56:54 -05001723 struct usb_device *udev = to_usb_device(dev);
1724 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001725
1726 /* A USB device can be suspended if it passes the various autosuspend
1727 * checks. Runtime suspend for a USB device means suspending all the
1728 * interfaces and then the device itself.
1729 */
Ming Lei63defa72010-11-15 15:56:54 -05001730 if (autosuspend_check(udev) != 0)
1731 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001732
Ming Lei63defa72010-11-15 15:56:54 -05001733 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001734
1735 /* Allow a retry if autosuspend failed temporarily */
1736 if (status == -EAGAIN || status == -EBUSY)
1737 usb_mark_last_busy(udev);
1738
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001739 /* The PM core reacts badly unless the return code is 0,
1740 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1741 */
1742 if (status != 0)
1743 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001744 return status;
1745}
1746
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001747int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001748{
Ming Lei63defa72010-11-15 15:56:54 -05001749 struct usb_device *udev = to_usb_device(dev);
1750 int status;
1751
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001752 /* Runtime resume for a USB device means resuming both the device
1753 * and all its interfaces.
1754 */
Ming Lei63defa72010-11-15 15:56:54 -05001755 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001756 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001757}
1758
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001759int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001760{
Ming Lei63defa72010-11-15 15:56:54 -05001761 struct usb_device *udev = to_usb_device(dev);
1762
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001763 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001764 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001765 */
Ming Lei63defa72010-11-15 15:56:54 -05001766 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001767 pm_runtime_autosuspend(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001768 return 0;
1769}
1770
Andiry Xu65580b432011-09-23 14:19:52 -07001771int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1772{
1773 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1774 int ret = -EPERM;
1775
1776 if (hcd->driver->set_usb2_hw_lpm) {
1777 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1778 if (!ret)
1779 udev->usb2_hw_lpm_enabled = enable;
1780 }
1781
1782 return ret;
1783}
1784
Alan Stern84ebc102013-03-27 16:14:46 -04001785#endif /* CONFIG_PM_RUNTIME */
Alan Stern645daaa2006-08-30 15:47:02 -04001786
Alan Stern36e56a32006-07-01 22:08:06 -04001787struct bus_type usb_bus_type = {
1788 .name = "usb",
1789 .match = usb_device_match,
1790 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001791};