blob: 56593a9a87268cc0f6d8181cb5f8021c252b457b [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,
Wolfram Sang2fc82c22014-01-10 19:36:42 +010040 const struct usb_device_id *id_table,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010041 struct device_driver *driver,
42 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080043{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080044 struct usb_dynid *dynid;
45 u32 idVendor = 0;
46 u32 idProduct = 0;
Josua Dietzeff231db2011-10-23 14:22:29 +020047 unsigned int bInterfaceClass = 0;
Wolfram Sang2fc82c22014-01-10 19:36:42 +010048 u32 refVendor, refProduct;
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
Wolfram Sang2fc82c22014-01-10 19:36:42 +010052 fields = sscanf(buf, "%x %x %x %x %x", &idVendor, &idProduct,
53 &bInterfaceClass, &refVendor, &refProduct);
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;
Wolfram Sangc63fe8f2014-01-10 19:36:41 +010065 if (fields > 2 && bInterfaceClass) {
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010066 if (bInterfaceClass > 255) {
67 retval = -EINVAL;
68 goto fail;
69 }
Wolfram Sangc63fe8f2014-01-10 19:36:41 +010070
Josua Dietzeff231db2011-10-23 14:22:29 +020071 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
72 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
73 }
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080074
Wolfram Sang2fc82c22014-01-10 19:36:42 +010075 if (fields > 4) {
76 const struct usb_device_id *id = id_table;
77
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010078 if (!id) {
79 retval = -ENODEV;
80 goto fail;
81 }
Wolfram Sang1b9fb312014-01-13 11:29:23 +010082
Wolfram Sang2fc82c22014-01-10 19:36:42 +010083 for (; id->match_flags; id++)
Wolfram Sang52a6966c2014-01-12 10:07:50 +010084 if (id->idVendor == refVendor && id->idProduct == refProduct)
Wolfram Sang2fc82c22014-01-10 19:36:42 +010085 break;
Wolfram Sang52a6966c2014-01-12 10:07:50 +010086
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010087 if (id->match_flags) {
Wolfram Sang52a6966c2014-01-12 10:07:50 +010088 dynid->id.driver_info = id->driver_info;
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010089 } else {
90 retval = -ENODEV;
91 goto fail;
92 }
Wolfram Sang2fc82c22014-01-10 19:36:42 +010093 }
94
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010095 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020096 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010097 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080098
Alan Sterncef9bc52012-01-24 13:34:41 -050099 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800100
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700101 if (retval)
102 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800103 return count;
Christian Engelmayer7f196ca2014-01-28 22:22:27 +0100104
105fail:
106 kfree(dynid);
107 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800108}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100109EXPORT_SYMBOL_GPL(usb_store_new_id);
110
Bjørn Morkef206f32012-05-13 12:35:00 +0200111ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200112{
113 struct usb_dynid *dynid;
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200114 size_t count = 0;
115
Bjørn Morkef206f32012-05-13 12:35:00 +0200116 list_for_each_entry(dynid, &dynids->list, node)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200117 if (dynid->id.bInterfaceClass != 0)
118 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
119 dynid->id.idVendor, dynid->id.idProduct,
120 dynid->id.bInterfaceClass);
121 else
122 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
123 dynid->id.idVendor, dynid->id.idProduct);
124 return count;
125}
Bjørn Morkef206f32012-05-13 12:35:00 +0200126EXPORT_SYMBOL_GPL(usb_show_dynids);
127
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700128static ssize_t new_id_show(struct device_driver *driver, char *buf)
Bjørn Morkef206f32012-05-13 12:35:00 +0200129{
130 struct usb_driver *usb_drv = to_usb_driver(driver);
131
132 return usb_show_dynids(&usb_drv->dynids, buf);
133}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200134
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700135static ssize_t new_id_store(struct device_driver *driver,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100136 const char *buf, size_t count)
137{
138 struct usb_driver *usb_drv = to_usb_driver(driver);
139
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100140 return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100141}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700142static DRIVER_ATTR_RW(new_id);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800143
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700144/*
145 * Remove a USB device ID from this driver
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800146 */
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700147static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
148 size_t count)
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800149{
150 struct usb_dynid *dynid, *n;
151 struct usb_driver *usb_driver = to_usb_driver(driver);
Alan Coxac08de32012-09-17 11:55:23 +0100152 u32 idVendor;
153 u32 idProduct;
154 int fields;
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800155
156 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
157 if (fields < 2)
158 return -EINVAL;
159
160 spin_lock(&usb_driver->dynids.lock);
161 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
162 struct usb_device_id *id = &dynid->id;
Kris Borer79a02742015-06-16 13:24:53 -0400163
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800164 if ((id->idVendor == idVendor) &&
165 (id->idProduct == idProduct)) {
166 list_del(&dynid->node);
167 kfree(dynid);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800168 break;
169 }
170 }
171 spin_unlock(&usb_driver->dynids.lock);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800172 return count;
173}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700174
175static ssize_t remove_id_show(struct device_driver *driver, char *buf)
176{
177 return new_id_show(driver, buf);
178}
179static DRIVER_ATTR_RW(remove_id);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800180
Alan Sterned283e92012-01-24 14:35:13 -0500181static int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800182{
183 int error = 0;
184
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800185 if (usb_drv->no_dynamic_id)
186 goto exit;
187
Alan Sterned283e92012-01-24 14:35:13 -0500188 if (usb_drv->probe != NULL) {
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800189 error = driver_create_file(&usb_drv->drvwrap.driver,
190 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500191 if (error == 0) {
192 error = driver_create_file(&usb_drv->drvwrap.driver,
193 &driver_attr_remove_id);
194 if (error)
195 driver_remove_file(&usb_drv->drvwrap.driver,
196 &driver_attr_new_id);
197 }
198 }
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800199exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800200 return error;
201}
202
Alan Sterned283e92012-01-24 14:35:13 -0500203static void usb_remove_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800204{
205 if (usb_drv->no_dynamic_id)
206 return;
207
Alan Sterned283e92012-01-24 14:35:13 -0500208 if (usb_drv->probe != NULL) {
209 driver_remove_file(&usb_drv->drvwrap.driver,
210 &driver_attr_remove_id);
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800211 driver_remove_file(&usb_drv->drvwrap.driver,
212 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500213 }
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800214}
215
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800216static void usb_free_dynids(struct usb_driver *usb_drv)
217{
218 struct usb_dynid *dynid, *n;
219
220 spin_lock(&usb_drv->dynids.lock);
221 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
222 list_del(&dynid->node);
223 kfree(dynid);
224 }
225 spin_unlock(&usb_drv->dynids.lock);
226}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800227
228static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
229 struct usb_driver *drv)
230{
231 struct usb_dynid *dynid;
232
233 spin_lock(&drv->dynids.lock);
234 list_for_each_entry(dynid, &drv->dynids.list, node) {
235 if (usb_match_one_id(intf, &dynid->id)) {
236 spin_unlock(&drv->dynids.lock);
237 return &dynid->id;
238 }
239 }
240 spin_unlock(&drv->dynids.lock);
241 return NULL;
242}
243
244
Alan Stern8bb54ab2006-07-01 22:08:49 -0400245/* called from driver core with dev locked */
246static int usb_probe_device(struct device *dev)
247{
248 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200249 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500250 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400251
Harvey Harrison441b62c2008-03-03 16:08:34 -0800252 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400253
Alan Stern8bb54ab2006-07-01 22:08:49 -0400254 /* TODO: Add real matching code */
255
Alan Stern645daaa2006-08-30 15:47:02 -0400256 /* The device should always appear to be in use
Masanari Iida02582e92012-08-22 19:11:26 +0900257 * unless the driver supports autosuspend.
Alan Stern645daaa2006-08-30 15:47:02 -0400258 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500259 if (!udriver->supports_autosuspend)
260 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400261
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500262 if (!error)
263 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400264 return error;
265}
266
267/* called from driver core with dev locked */
268static int usb_unbind_device(struct device *dev)
269{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500270 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400271 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
272
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500273 udriver->disconnect(udev);
274 if (!udriver->supports_autosuspend)
275 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400276 return 0;
277}
278
Alan Stern8bb54ab2006-07-01 22:08:49 -0400279/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800280static int usb_probe_interface(struct device *dev)
281{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400282 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200283 struct usb_interface *intf = to_usb_interface(dev);
284 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800285 const struct usb_device_id *id;
286 int error = -ENODEV;
Sarah Sharp83060952012-05-02 14:25:52 -0700287 int lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800288
Harvey Harrison441b62c2008-03-03 16:08:34 -0800289 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800290
Alan Stern78d9a482008-06-23 16:00:40 -0400291 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800292
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400293 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500294 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400295
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800296 if (udev->authorized == 0) {
297 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500298 return error;
Stefan Koch8d1f8572015-08-25 21:10:07 +0200299 } else if (intf->authorized == 0) {
300 dev_err(&intf->dev, "Interface %d is not authorized for usage\n",
301 intf->altsetting->desc.bInterfaceNumber);
302 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800303 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700304
Bjørn Mork31c6bf72014-01-11 02:04:00 +0100305 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800306 if (!id)
Bjørn Mork31c6bf72014-01-11 02:04:00 +0100307 id = usb_match_id(intf, driver->id_table);
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 Neukum1e5ea5e2009-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 Neukum1e5ea5e2009-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 Neukum1e5ea5e2009-08-27 16:46:56 +0200368
Alan Stern0f3dda92010-01-08 12:56:04 -0500369 err:
Hans de Goedee714fad2012-05-22 11:36:59 +0200370 usb_set_intfdata(intf, NULL);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200371 intf->needs_remote_wakeup = 0;
372 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500373
Sarah Sharpd01f87c2012-10-04 09:53:43 -0700374 /* If the LPM disable succeeded, balance the ref counts. */
375 if (!lpm_disable_error)
376 usb_unlocked_enable_lpm(udev);
377
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500378 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400379 if (driver->supports_autosuspend)
380 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500381 pm_runtime_set_suspended(dev);
382
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200383 usb_autosuspend_device(udev);
384 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800385}
386
Alan Stern8bb54ab2006-07-01 22:08:49 -0400387/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800388static int usb_unbind_interface(struct device *dev)
389{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400390 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800391 struct usb_interface *intf = to_usb_interface(dev);
Hans de Goede6343e8b2013-10-09 17:19:26 +0200392 struct usb_host_endpoint *ep, **eps = NULL;
Alan Stern645daaa2006-08-30 15:47:02 -0400393 struct usb_device *udev;
Hans de Goede6343e8b2013-10-09 17:19:26 +0200394 int i, j, error, r, lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800395
396 intf->condition = USB_INTERFACE_UNBINDING;
397
Alan Stern645daaa2006-08-30 15:47:02 -0400398 /* Autoresume for set_interface call below */
399 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500400 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400401
Sarah Sharp83060952012-05-02 14:25:52 -0700402 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
403 * the driver is unbound. If LPM isn't disabled, that's fine because it
404 * wouldn't be enabled unless all the bound interfaces supported
405 * hub-initiated LPM.
406 */
407 lpm_disable_error = usb_unlocked_disable_lpm(udev);
408
Alan Stern1299cff2014-07-17 15:40:57 -0400409 /*
410 * Terminate all URBs for this interface unless the driver
411 * supports "soft" unbinding and the device is still present.
Alan Stern9da82bd2008-05-08 11:54:37 -0400412 */
Alan Stern1299cff2014-07-17 15:40:57 -0400413 if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
Alan Sternddeac4e72009-01-15 17:03:33 -0500414 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800415
Alan Stern8bb54ab2006-07-01 22:08:49 -0400416 driver->disconnect(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800417
Hans de Goede6343e8b2013-10-09 17:19:26 +0200418 /* Free streams */
419 for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
420 ep = &intf->cur_altsetting->endpoint[i];
421 if (ep->streams == 0)
422 continue;
423 if (j == 0) {
Muhammad Falak R Wani9766f252015-09-07 21:30:25 +0530424 eps = kmalloc_array(USB_MAXENDPOINTS, sizeof(void *),
Hans de Goede6343e8b2013-10-09 17:19:26 +0200425 GFP_KERNEL);
Muhammad Falak R Wani9766f252015-09-07 21:30:25 +0530426 if (!eps)
Hans de Goede6343e8b2013-10-09 17:19:26 +0200427 break;
Hans de Goede6343e8b2013-10-09 17:19:26 +0200428 }
429 eps[j++] = ep;
430 }
431 if (j) {
432 usb_free_streams(intf, eps, j, GFP_KERNEL);
433 kfree(eps);
434 }
435
Alan Stern55151d72008-08-12 14:33:59 -0400436 /* Reset other interface state.
437 * We cannot do a Set-Interface if the device is suspended or
438 * if it is prepared for a system sleep (since installing a new
439 * altsetting means creating new endpoint device entries).
440 * When either of these happens, defer the Set-Interface.
441 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500442 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
443 /* Already in altsetting 0 so skip Set-Interface.
444 * Just re-enable it without affecting the endpoint toggles.
445 */
446 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200447 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200448 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400449 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200450 if (r < 0)
451 intf->needs_altsetting0 = 1;
452 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400453 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200454 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800455 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400456
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800457 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400458 intf->needs_remote_wakeup = 0;
459
Sarah Sharp83060952012-05-02 14:25:52 -0700460 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
461 if (!lpm_disable_error)
462 usb_unlocked_enable_lpm(udev);
463
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500464 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400465 if (driver->supports_autosuspend)
466 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500467 pm_runtime_set_suspended(dev);
468
469 /* Undo any residual pm_autopm_get_interface_* calls */
470 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
471 usb_autopm_put_interface_no_suspend(intf);
472 atomic_set(&intf->pm_usage_cnt, 0);
473
Alan Stern645daaa2006-08-30 15:47:02 -0400474 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500475 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800476
477 return 0;
478}
479
Alan Stern36e56a32006-07-01 22:08:06 -0400480/**
481 * usb_driver_claim_interface - bind a driver to an interface
482 * @driver: the driver to be bound
483 * @iface: the interface to which it will be bound; must be in the
484 * usb device's active configuration
485 * @priv: driver data associated with that interface
486 *
487 * This is used by usb device drivers that need to claim more than one
488 * interface on a device when probing (audio and acm are current examples).
489 * No device driver should directly modify internal usb_interface or
490 * usb_device structure members.
491 *
492 * Few drivers should need to use this routine, since the most natural
493 * way to bind to an interface is to return the private data from
494 * the driver's probe() method.
495 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400496 * Callers must own the device lock, so driver probe() entries don't need
497 * extra locking, but other call contexts may need to explicitly claim that
498 * lock.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200499 *
500 * Return: 0 on success.
Alan Stern36e56a32006-07-01 22:08:06 -0400501 */
502int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800503 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400504{
505 struct device *dev = &iface->dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700506 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700507 int retval = 0;
Sarah Sharp83060952012-05-02 14:25:52 -0700508 int lpm_disable_error;
Alan Stern36e56a32006-07-01 22:08:06 -0400509
510 if (dev->driver)
511 return -EBUSY;
512
Stefan Koch8d1f8572015-08-25 21:10:07 +0200513 /* reject claim if interface is not authorized */
514 if (!iface->authorized)
515 return -ENODEV;
516
Sarah Sharp83060952012-05-02 14:25:52 -0700517 udev = interface_to_usbdev(iface);
518
Alan Stern8bb54ab2006-07-01 22:08:49 -0400519 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400520 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400521 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400522
Alan Stern36e56a32006-07-01 22:08:06 -0400523 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500524
Sarah Sharp83060952012-05-02 14:25:52 -0700525 /* Disable LPM until this driver is bound. */
526 lpm_disable_error = usb_unlocked_disable_lpm(udev);
527 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
528 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
529 __func__, driver->name);
530 return -ENOMEM;
531 }
532
Alan Stern89842ae2010-05-11 11:44:06 -0400533 /* Claimed interfaces are initially inactive (suspended) and
534 * runtime-PM-enabled, but only if the driver has autosuspend
535 * support. Otherwise they are marked active, to prevent the
536 * device from being autosuspended, but left disabled. In either
537 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500538 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500539 pm_suspend_ignore_children(dev, false);
540 if (driver->supports_autosuspend)
541 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400542 else
543 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400544
545 /* if interface was already added, bind now; else let
546 * the future device_add() bind it, bypassing probe()
547 */
548 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700549 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400550
Sarah Sharp83060952012-05-02 14:25:52 -0700551 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
552 if (!lpm_disable_error)
553 usb_unlocked_enable_lpm(udev);
554
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700555 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400556}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600557EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400558
559/**
560 * usb_driver_release_interface - unbind a driver from an interface
561 * @driver: the driver to be unbound
562 * @iface: the interface from which it will be unbound
563 *
564 * This can be used by drivers to release an interface without waiting
565 * for their disconnect() methods to be called. In typical cases this
566 * also causes the driver disconnect() method to be called.
567 *
568 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400569 * Callers must own the device lock, so driver disconnect() entries don't
570 * need extra locking, but other call contexts may need to explicitly claim
571 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400572 */
573void usb_driver_release_interface(struct usb_driver *driver,
574 struct usb_interface *iface)
575{
576 struct device *dev = &iface->dev;
577
578 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400579 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400580 return;
581
582 /* don't release from within disconnect() */
583 if (iface->condition != USB_INTERFACE_BOUND)
584 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400585 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400586
Alan Stern91f8d062009-04-16 15:35:09 -0400587 /* Release via the driver core only if the interface
588 * has already been registered
589 */
Alan Stern36e56a32006-07-01 22:08:06 -0400590 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400591 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800592 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800593 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400594 usb_unbind_interface(dev);
595 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800596 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400597 }
Alan Stern36e56a32006-07-01 22:08:06 -0400598}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600599EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400600
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800601/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100602int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800603{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800604 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
605 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
606 return 0;
607
608 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
609 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
610 return 0;
611
612 /* No need to test id->bcdDevice_lo != 0, since 0 is never
613 greater than any unsigned number. */
614 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
615 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
616 return 0;
617
618 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
619 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
620 return 0;
621
622 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
623 (id->bDeviceClass != dev->descriptor.bDeviceClass))
624 return 0;
625
626 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800627 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800628 return 0;
629
630 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
631 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
632 return 0;
633
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100634 return 1;
635}
636
637/* returns 0 if no match, 1 if match */
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200638int usb_match_one_id_intf(struct usb_device *dev,
639 struct usb_host_interface *intf,
640 const struct usb_device_id *id)
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100641{
Bjørn Mork81df2d52012-05-18 21:27:43 +0200642 /* The interface class, subclass, protocol and number should never be
Alan Stern93c8bf42006-10-18 16:41:51 -0400643 * checked for a match if the device class is Vendor Specific,
644 * unless the match record specifies the Vendor ID. */
645 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
646 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
647 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
648 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
Bjørn Mork81df2d52012-05-18 21:27:43 +0200649 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
650 USB_DEVICE_ID_MATCH_INT_NUMBER)))
Alan Stern93c8bf42006-10-18 16:41:51 -0400651 return 0;
652
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800653 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
654 (id->bInterfaceClass != intf->desc.bInterfaceClass))
655 return 0;
656
657 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
658 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
659 return 0;
660
661 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
662 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
663 return 0;
664
Bjørn Mork81df2d52012-05-18 21:27:43 +0200665 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
666 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
667 return 0;
668
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800669 return 1;
670}
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200671
672/* returns 0 if no match, 1 if match */
673int usb_match_one_id(struct usb_interface *interface,
674 const struct usb_device_id *id)
675{
676 struct usb_host_interface *intf;
677 struct usb_device *dev;
678
679 /* proc_connectinfo in devio.c may call us with id == NULL. */
680 if (id == NULL)
681 return 0;
682
683 intf = interface->cur_altsetting;
684 dev = interface_to_usbdev(interface);
685
686 if (!usb_match_device(dev, id))
687 return 0;
688
689 return usb_match_one_id_intf(dev, intf, id);
690}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100691EXPORT_SYMBOL_GPL(usb_match_one_id);
692
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800693/**
694 * usb_match_id - find first usb_device_id matching device or interface
695 * @interface: the interface of interest
696 * @id: array of usb_device_id structures, terminated by zero entry
697 *
698 * usb_match_id searches an array of usb_device_id's and returns
699 * the first one matching the device or interface, or null.
700 * This is used when binding (or rebinding) a driver to an interface.
701 * Most USB device drivers will use this indirectly, through the usb core,
702 * but some layered driver frameworks use it directly.
703 * These device tables are exported with MODULE_DEVICE_TABLE, through
704 * modutils, to support the driver loading functionality of USB hotplugging.
705 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200706 * Return: The first matching usb_device_id, or %NULL.
707 *
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800708 * What Matches:
709 *
710 * The "match_flags" element in a usb_device_id controls which
711 * members are used. If the corresponding bit is set, the
712 * value in the device_id must match its corresponding member
713 * in the device or interface descriptor, or else the device_id
714 * does not match.
715 *
716 * "driver_info" is normally used only by device drivers,
717 * but you can create a wildcard "matches anything" usb_device_id
718 * as a driver's "modules.usbmap" entry if you provide an id with
719 * only a nonzero "driver_info" field. If you do this, the USB device
720 * driver's probe() routine should use additional intelligence to
721 * decide whether to bind to the specified interface.
722 *
723 * What Makes Good usb_device_id Tables:
724 *
725 * The match algorithm is very simple, so that intelligence in
726 * driver selection must come from smart driver id records.
727 * Unless you have good reasons to use another selection policy,
728 * provide match elements only in related groups, and order match
729 * specifiers from specific to general. Use the macros provided
730 * for that purpose if you can.
731 *
732 * The most specific match specifiers use device descriptor
733 * data. These are commonly used with product-specific matches;
734 * the USB_DEVICE macro lets you provide vendor and product IDs,
735 * and you can also match against ranges of product revisions.
736 * These are widely used for devices with application or vendor
737 * specific bDeviceClass values.
738 *
739 * Matches based on device class/subclass/protocol specifications
740 * are slightly more general; use the USB_DEVICE_INFO macro, or
741 * its siblings. These are used with single-function devices
742 * where bDeviceClass doesn't specify that each interface has
743 * its own class.
744 *
745 * Matches based on interface class/subclass/protocol are the
746 * most general; they let drivers bind to any interface on a
747 * multiple-function device. Use the USB_INTERFACE_INFO
748 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400749 * devices (as recorded in bInterfaceClass).
750 *
751 * Note that an entry created by USB_INTERFACE_INFO won't match
752 * any interface if the device class is set to Vendor-Specific.
753 * This is deliberate; according to the USB spec the meanings of
754 * the interface class/subclass/protocol for these devices are also
755 * vendor-specific, and hence matching against a standard product
756 * class wouldn't work anyway. If you really want to use an
757 * interface-based match for such a device, create a match record
758 * that also specifies the vendor ID. (Unforunately there isn't a
759 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800760 *
761 * Within those groups, remember that not all combinations are
762 * meaningful. For example, don't give a product version range
763 * without vendor and product IDs; or specify a protocol without
764 * its associated class and subclass.
765 */
766const struct usb_device_id *usb_match_id(struct usb_interface *interface,
767 const struct usb_device_id *id)
768{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800769 /* proc_connectinfo in devio.c may call us with id == NULL. */
770 if (id == NULL)
771 return NULL;
772
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800773 /* It is important to check that id->driver_info is nonzero,
774 since an entry that is all zeroes except for a nonzero
775 id->driver_info is the way to create an entry that
776 indicates that the driver want to examine every
777 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800778 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
779 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800780 if (usb_match_one_id(interface, id))
781 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800782 }
783
784 return NULL;
785}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600786EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800787
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100788static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800789{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400790 /* devices and interfaces are handled separately */
791 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800792
Alan Stern8bb54ab2006-07-01 22:08:49 -0400793 /* interface drivers never match devices */
794 if (!is_usb_device_driver(drv))
795 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800796
Alan Stern8bb54ab2006-07-01 22:08:49 -0400797 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800798 return 1;
799
Kay Sievers55129662009-05-04 19:48:32 +0200800 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400801 struct usb_interface *intf;
802 struct usb_driver *usb_drv;
803 const struct usb_device_id *id;
804
805 /* device drivers never match interfaces */
806 if (is_usb_device_driver(drv))
807 return 0;
808
809 intf = to_usb_interface(dev);
810 usb_drv = to_usb_driver(drv);
811
812 id = usb_match_id(intf, usb_drv->id_table);
813 if (id)
814 return 1;
815
816 id = usb_match_dynamic_id(intf, usb_drv);
817 if (id)
818 return 1;
819 }
820
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800821 return 0;
822}
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{
Alan Stern36e56a32006-07-01 22:08:06 -0400826 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400827
Kay Sievers55129662009-05-04 19:48:32 +0200828 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400829 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200830 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100831 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200832
Alan Stern8bb54ab2006-07-01 22:08:49 -0400833 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200834 } else {
835 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400836 }
Alan Stern36e56a32006-07-01 22:08:06 -0400837
838 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500839 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200840 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400841 return -ENODEV;
842 }
843 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200844 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400845 return -ENODEV;
846 }
847
Alan Stern36e56a32006-07-01 22:08:06 -0400848 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200849 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400850 le16_to_cpu(usb_dev->descriptor.idVendor),
851 le16_to_cpu(usb_dev->descriptor.idProduct),
852 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
853 return -ENOMEM;
854
855 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200856 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400857 usb_dev->descriptor.bDeviceClass,
858 usb_dev->descriptor.bDeviceSubClass,
859 usb_dev->descriptor.bDeviceProtocol))
860 return -ENOMEM;
861
Alan Stern36e56a32006-07-01 22:08:06 -0400862 return 0;
863}
864
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800865/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400866 * usb_register_device_driver - register a USB device (not interface) driver
867 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800868 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800869 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400870 * Registers a USB device driver with the USB core. The list of
871 * unattached devices will be rescanned whenever a new driver is
872 * added, allowing the new driver to attach to any recognized devices.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200873 *
874 * Return: A negative error code on failure and 0 on success.
Alan Stern8bb54ab2006-07-01 22:08:49 -0400875 */
876int usb_register_device_driver(struct usb_device_driver *new_udriver,
877 struct module *owner)
878{
879 int retval = 0;
880
881 if (usb_disabled())
882 return -ENODEV;
883
884 new_udriver->drvwrap.for_devices = 1;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100885 new_udriver->drvwrap.driver.name = new_udriver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400886 new_udriver->drvwrap.driver.bus = &usb_bus_type;
887 new_udriver->drvwrap.driver.probe = usb_probe_device;
888 new_udriver->drvwrap.driver.remove = usb_unbind_device;
889 new_udriver->drvwrap.driver.owner = owner;
890
891 retval = driver_register(&new_udriver->drvwrap.driver);
892
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700893 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400894 pr_info("%s: registered new device driver %s\n",
895 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700896 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400897 printk(KERN_ERR "%s: error %d registering device "
898 " driver %s\n",
899 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400900
901 return retval;
902}
903EXPORT_SYMBOL_GPL(usb_register_device_driver);
904
905/**
906 * usb_deregister_device_driver - unregister a USB device (not interface) driver
907 * @udriver: USB operations of the device driver to unregister
908 * Context: must be able to sleep
909 *
910 * Unlinks the specified driver from the internal USB driver list.
911 */
912void usb_deregister_device_driver(struct usb_device_driver *udriver)
913{
914 pr_info("%s: deregistering device driver %s\n",
915 usbcore_name, udriver->name);
916
917 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400918}
919EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
920
921/**
922 * usb_register_driver - register a USB interface driver
923 * @new_driver: USB operations for the interface driver
924 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800925 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400926 *
927 * Registers a USB interface driver with the USB core. The list of
928 * unattached interfaces will be rescanned whenever a new driver is
929 * added, allowing the new driver to attach to any recognized interfaces.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200930 *
931 * Return: A negative error code on failure and 0 on success.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800932 *
933 * NOTE: if you want your driver to use the USB major number, you must call
934 * usb_register_dev() to enable that functionality. This function no longer
935 * takes care of that.
936 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800937int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
938 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800939{
940 int retval = 0;
941
942 if (usb_disabled())
943 return -ENODEV;
944
Alan Stern8bb54ab2006-07-01 22:08:49 -0400945 new_driver->drvwrap.for_devices = 0;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100946 new_driver->drvwrap.driver.name = new_driver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400947 new_driver->drvwrap.driver.bus = &usb_bus_type;
948 new_driver->drvwrap.driver.probe = usb_probe_interface;
949 new_driver->drvwrap.driver.remove = usb_unbind_interface;
950 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800951 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800952 spin_lock_init(&new_driver->dynids.lock);
953 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800954
Alan Stern8bb54ab2006-07-01 22:08:49 -0400955 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800956 if (retval)
957 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800958
Alan Sterned283e92012-01-24 14:35:13 -0500959 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800960 if (retval)
961 goto out_newid;
962
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800963 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800964 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800965
966out:
967 return retval;
968
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800969out_newid:
970 driver_unregister(&new_driver->drvwrap.driver);
971
972 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400973 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800974 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800975 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800976}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600977EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800978
979/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400980 * usb_deregister - unregister a USB interface driver
981 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800982 * Context: must be able to sleep
983 *
984 * Unlinks the specified driver from the internal USB driver list.
985 *
986 * NOTE: If you called usb_register_dev(), you still need to call
987 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
988 * this * call will no longer do it for you.
989 */
990void usb_deregister(struct usb_driver *driver)
991{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400992 pr_info("%s: deregistering interface driver %s\n",
993 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800994
Alan Sterned283e92012-01-24 14:35:13 -0500995 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400996 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -0500997 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800998}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600999EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -04001000
Alan Stern78d9a482008-06-23 16:00:40 -04001001/* Forced unbinding of a USB interface driver, either because
1002 * it doesn't support pre_reset/post_reset/reset_resume or
1003 * because it doesn't support suspend/resume.
1004 *
Alan Stern6aec0442014-03-12 11:30:38 -04001005 * The caller must hold @intf's device's lock, but not @intf's lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001006 */
1007void usb_forced_unbind_intf(struct usb_interface *intf)
1008{
1009 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
1010
1011 dev_dbg(&intf->dev, "forced unbind\n");
1012 usb_driver_release_interface(driver, intf);
1013
1014 /* Mark the interface for later rebinding */
1015 intf->needs_binding = 1;
1016}
1017
Alan Stern6aec0442014-03-12 11:30:38 -04001018/*
1019 * Unbind drivers for @udev's marked interfaces. These interfaces have
1020 * the needs_binding flag set, for example by usb_resume_interface().
1021 *
1022 * The caller must hold @udev's device lock.
1023 */
1024static void unbind_marked_interfaces(struct usb_device *udev)
1025{
1026 struct usb_host_config *config;
1027 int i;
1028 struct usb_interface *intf;
1029
1030 config = udev->actconfig;
1031 if (config) {
1032 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1033 intf = config->interface[i];
1034 if (intf->dev.driver && intf->needs_binding)
1035 usb_forced_unbind_intf(intf);
1036 }
1037 }
1038}
1039
Alan Stern78d9a482008-06-23 16:00:40 -04001040/* Delayed forced unbinding of a USB interface driver and scan
1041 * for rebinding.
1042 *
Alan Stern6aec0442014-03-12 11:30:38 -04001043 * The caller must hold @intf's device's lock, but not @intf's lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001044 *
Alan Stern5096aed2008-08-12 14:34:14 -04001045 * Note: Rebinds will be skipped if a system sleep transition is in
1046 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -04001047 */
Alan Stern6aec0442014-03-12 11:30:38 -04001048static void usb_rebind_intf(struct usb_interface *intf)
Alan Stern78d9a482008-06-23 16:00:40 -04001049{
1050 int rc;
1051
1052 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +01001053 if (intf->dev.driver)
1054 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001055
1056 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +02001057 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -04001058 intf->needs_binding = 0;
1059 rc = device_attach(&intf->dev);
1060 if (rc < 0)
1061 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1062 }
Alan Stern78d9a482008-06-23 16:00:40 -04001063}
1064
Alan Stern6aec0442014-03-12 11:30:38 -04001065/*
1066 * Rebind drivers to @udev's marked interfaces. These interfaces have
1067 * the needs_binding flag set.
1068 *
1069 * The caller must hold @udev's device lock.
1070 */
1071static void rebind_marked_interfaces(struct usb_device *udev)
1072{
1073 struct usb_host_config *config;
1074 int i;
1075 struct usb_interface *intf;
1076
1077 config = udev->actconfig;
1078 if (config) {
1079 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1080 intf = config->interface[i];
1081 if (intf->needs_binding)
1082 usb_rebind_intf(intf);
1083 }
1084 }
1085}
1086
1087/*
1088 * Unbind all of @udev's marked interfaces and then rebind all of them.
1089 * This ordering is necessary because some drivers claim several interfaces
1090 * when they are first probed.
1091 *
1092 * The caller must hold @udev's device lock.
1093 */
1094void usb_unbind_and_rebind_marked_interfaces(struct usb_device *udev)
1095{
1096 unbind_marked_interfaces(udev);
1097 rebind_marked_interfaces(udev);
1098}
1099
Alan Stern9ff78432008-08-07 13:04:51 -04001100#ifdef CONFIG_PM
1101
Oliver Neukum14931382012-01-05 15:39:57 +01001102/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1103 * There is no check for reset_resume here because it can be determined
1104 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001105 *
1106 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001107 */
Oliver Neukum14931382012-01-05 15:39:57 +01001108static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001109{
1110 struct usb_host_config *config;
1111 int i;
1112 struct usb_interface *intf;
1113 struct usb_driver *drv;
1114
1115 config = udev->actconfig;
1116 if (config) {
1117 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1118 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001119
1120 if (intf->dev.driver) {
1121 drv = to_usb_driver(intf->dev.driver);
1122 if (!drv->suspend || !drv->resume)
1123 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001124 }
1125 }
1126 }
1127}
1128
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001129static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001130{
Alan Stern782da722006-07-01 22:09:35 -04001131 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001132 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001133
Alan Stern114b3682006-07-01 22:13:04 -04001134 if (udev->state == USB_STATE_NOTATTACHED ||
1135 udev->state == USB_STATE_SUSPENDED)
1136 goto done;
1137
Alan Sternb6f64362007-05-04 11:51:54 -04001138 /* For devices that don't have a driver, we do a generic suspend. */
1139 if (udev->dev.driver)
1140 udriver = to_usb_device_driver(udev->dev.driver);
1141 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001142 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001143 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001144 }
Alan Stern2bf40862006-07-01 22:12:19 -04001145 status = udriver->suspend(udev, msg);
1146
Alan Stern20dfdad2007-05-22 11:50:17 -04001147 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001148 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001149 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001150}
Alan Stern36e56a32006-07-01 22:08:06 -04001151
Alan Stern65bfd292008-11-25 16:39:18 -05001152static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001153{
1154 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001155 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001156
Alan Stern0458d5b2007-05-04 11:52:20 -04001157 if (udev->state == USB_STATE_NOTATTACHED)
1158 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001159
Alan Stern1c5df7e2006-07-01 22:13:50 -04001160 /* Can't resume it if it doesn't have a driver. */
1161 if (udev->dev.driver == NULL) {
1162 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001163 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001164 }
1165
Alan Stern6d19c002010-02-12 12:21:11 +01001166 /* Non-root devices on a full/low-speed bus must wait for their
1167 * companion high-speed root hub, in case a handoff is needed.
1168 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001169 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001170 device_pm_wait_for_dev(&udev->dev,
1171 &udev->bus->hs_companion->root_hub->dev);
1172
Alan Stern6bc6cff2007-05-04 11:53:03 -04001173 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1174 udev->reset_resume = 1;
1175
Alan Stern1cc8a252006-07-01 22:10:15 -04001176 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001177 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001178
Alan Stern20dfdad2007-05-22 11:50:17 -04001179 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001180 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001181 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001182}
1183
Alan Stern65605ae2008-08-12 14:33:27 -04001184static int usb_suspend_interface(struct usb_device *udev,
1185 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001186{
1187 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001188 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001189
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001190 if (udev->state == USB_STATE_NOTATTACHED ||
1191 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001192 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001193 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001194
Oliver Neukume78832c2012-01-02 15:11:48 +01001195 /* at this time we know the driver supports suspend */
1196 status = driver->suspend(intf, msg);
1197 if (status && !PMSG_IS_AUTO(msg))
1198 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001199
Alan Stern20dfdad2007-05-22 11:50:17 -04001200 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001201 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001202 return status;
1203}
1204
Alan Stern65605ae2008-08-12 14:33:27 -04001205static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001206 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001207{
Alan Stern1cc8a252006-07-01 22:10:15 -04001208 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001209 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001210
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001211 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001212 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001213
Alan Stern645daaa2006-08-30 15:47:02 -04001214 /* Don't let autoresume interfere with unbinding */
1215 if (intf->condition == USB_INTERFACE_UNBINDING)
1216 goto done;
1217
Alan Stern1c5df7e2006-07-01 22:13:50 -04001218 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001219 if (intf->condition == USB_INTERFACE_UNBOUND) {
1220
1221 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001222 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001223 usb_set_interface(udev, intf->altsetting[0].
1224 desc.bInterfaceNumber, 0);
1225 intf->needs_altsetting0 = 0;
1226 }
Alan Stern2bf40862006-07-01 22:12:19 -04001227 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001228 }
Alan Stern78d9a482008-06-23 16:00:40 -04001229
1230 /* Don't resume if the interface is marked for rebinding */
1231 if (intf->needs_binding)
1232 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001233 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001234
Alan Sternf07600c2007-05-30 15:38:16 -04001235 if (reset_resume) {
1236 if (driver->reset_resume) {
1237 status = driver->reset_resume(intf);
1238 if (status)
1239 dev_err(&intf->dev, "%s error %d\n",
1240 "reset_resume", status);
1241 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001242 intf->needs_binding = 1;
Alan Stern0a56b4f2013-10-18 11:17:21 -04001243 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1244 driver->name);
Alan Sternf07600c2007-05-30 15:38:16 -04001245 }
1246 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001247 status = driver->resume(intf);
1248 if (status)
1249 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001250 }
Alan Stern2bf40862006-07-01 22:12:19 -04001251
1252done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001253 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001254
Alan Stern78d9a482008-06-23 16:00:40 -04001255 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001256 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001257}
1258
Alan Stern645daaa2006-08-30 15:47:02 -04001259/**
1260 * usb_suspend_both - suspend a USB device and its interfaces
1261 * @udev: the usb_device to suspend
1262 * @msg: Power Management message describing this state transition
1263 *
1264 * This is the central routine for suspending USB devices. It calls the
1265 * suspend methods for all the interface drivers in @udev and then calls
Ming Lei303f0842013-03-15 12:08:53 +08001266 * the suspend method for @udev itself. When the routine is called in
1267 * autosuspend, if an error occurs at any stage, all the interfaces
1268 * which were suspended are resumed so that they remain in the same
1269 * state as the device, but when called from system sleep, all error
1270 * from suspend methods of interfaces and the non-root-hub device itself
1271 * are simply ignored, so all suspended interfaces are only resumed
1272 * to the device's state when @udev is root-hub and its suspend method
1273 * returns failure.
Alan Stern645daaa2006-08-30 15:47:02 -04001274 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001275 * Autosuspend requests originating from a child device or an interface
1276 * driver may be made without the protection of @udev's device lock, but
1277 * all other suspend calls will hold the lock. Usbcore will insure that
1278 * method calls do not arrive during bind, unbind, or reset operations.
1279 * However drivers must be prepared to handle suspend calls arriving at
1280 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001281 *
1282 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001283 *
1284 * Return: 0 if the suspend succeeded.
Alan Stern645daaa2006-08-30 15:47:02 -04001285 */
Alan Stern718efa62007-03-09 15:41:13 -05001286static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001287{
1288 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001289 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001290 struct usb_interface *intf;
1291
Alan Stern19410442007-03-27 13:33:59 -04001292 if (udev->state == USB_STATE_NOTATTACHED ||
1293 udev->state == USB_STATE_SUSPENDED)
1294 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001295
Alan Stern645daaa2006-08-30 15:47:02 -04001296 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001297 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001298 n = udev->actconfig->desc.bNumInterfaces;
1299 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001300 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001301 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001302
1303 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001304 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001305 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001306 if (status != 0)
1307 break;
1308 }
1309 }
Alan Stern0af212b2011-06-15 16:27:43 -04001310 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001311 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001312
Alan Sterncd4376e2012-03-28 15:56:17 -04001313 /*
1314 * Ignore errors from non-root-hub devices during
1315 * system sleep transitions. For the most part,
1316 * these devices should go to low power anyway when
1317 * the entire bus is suspended.
1318 */
1319 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001320 status = 0;
1321 }
1322
Alan Sterna8e7c562006-07-01 22:11:02 -04001323 /* If the suspend failed, resume interfaces that did get suspended */
1324 if (status != 0) {
Chen Gang505bdbc2013-04-01 13:04:08 +08001325 if (udev->actconfig) {
1326 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1327 while (++i < n) {
1328 intf = udev->actconfig->interface[i];
1329 usb_resume_interface(udev, intf, msg, 0);
1330 }
Alan Sterna8e7c562006-07-01 22:11:02 -04001331 }
Alan Stern645daaa2006-08-30 15:47:02 -04001332
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001333 /* If the suspend succeeded then prevent any more URB submissions
1334 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001335 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001336 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001337 udev->can_submit = 0;
1338 for (i = 0; i < 16; ++i) {
1339 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1340 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1341 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001342 }
Alan Stern645daaa2006-08-30 15:47:02 -04001343
Alan Stern19410442007-03-27 13:33:59 -04001344 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001345 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001346 return status;
1347}
1348
Alan Stern645daaa2006-08-30 15:47:02 -04001349/**
1350 * usb_resume_both - resume a USB device and its interfaces
1351 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001352 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001353 *
1354 * This is the central routine for resuming USB devices. It calls the
1355 * the resume method for @udev and then calls the resume methods for all
1356 * the interface drivers in @udev.
1357 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001358 * Autoresume requests originating from a child device or an interface
1359 * driver may be made without the protection of @udev's device lock, but
1360 * all other resume calls will hold the lock. Usbcore will insure that
1361 * method calls do not arrive during bind, unbind, or reset operations.
1362 * However drivers must be prepared to handle resume calls arriving at
1363 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001364 *
1365 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001366 *
1367 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001368 */
Alan Stern65bfd292008-11-25 16:39:18 -05001369static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001370{
Alan Stern645daaa2006-08-30 15:47:02 -04001371 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001372 int i;
1373 struct usb_interface *intf;
1374
Alan Stern19410442007-03-27 13:33:59 -04001375 if (udev->state == USB_STATE_NOTATTACHED) {
1376 status = -ENODEV;
1377 goto done;
1378 }
Alan Stern6840d252007-09-10 11:34:26 -04001379 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001380
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001381 /* Resume the device */
1382 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001383 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001384
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001385 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001386 if (status == 0 && udev->actconfig) {
1387 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1388 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001389 usb_resume_interface(udev, intf, msg,
1390 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001391 }
1392 }
Alan Sternc08512c2010-11-15 15:57:58 -05001393 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001394
Alan Stern19410442007-03-27 13:33:59 -04001395 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001396 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001397 if (!status)
1398 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001399 return status;
1400}
1401
Alan Stern5f677f12010-04-02 13:20:11 -04001402static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1403{
Alan Stern48826622010-06-22 16:14:48 -04001404 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001405
1406 /* Remote wakeup is needed only when we actually go to sleep.
1407 * For things like FREEZE and QUIESCE, if the device is already
1408 * autosuspended then its current wakeup setting is okay.
1409 */
1410 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1411 if (udev->state != USB_STATE_SUSPENDED)
1412 udev->do_remote_wakeup = 0;
1413 return;
1414 }
1415
Alan Stern48826622010-06-22 16:14:48 -04001416 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001417 * actually want it.
1418 */
Alan Stern48826622010-06-22 16:14:48 -04001419 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001420
1421 /* If the device is autosuspended with the wrong wakeup setting,
1422 * autoresume now so the setting can be changed.
1423 */
1424 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1425 pm_runtime_resume(&udev->dev);
1426 udev->do_remote_wakeup = w;
1427}
1428
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001429/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001430int usb_suspend(struct device *dev, pm_message_t msg)
1431{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001432 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001433
Oliver Neukum14931382012-01-05 15:39:57 +01001434 unbind_no_pm_drivers_interfaces(udev);
1435
1436 /* From now on we are sure all drivers support suspend/resume
1437 * but not necessarily reset_resume()
1438 * so we may still need to unbind and rebind upon resume
1439 */
Alan Stern5f677f12010-04-02 13:20:11 -04001440 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001441 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001442}
1443
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001444/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001445int usb_resume_complete(struct device *dev)
1446{
1447 struct usb_device *udev = to_usb_device(dev);
1448
1449 /* For PM complete calls, all we do is rebind interfaces
1450 * whose needs_binding flag is set
1451 */
1452 if (udev->state != USB_STATE_NOTATTACHED)
Alan Stern6aec0442014-03-12 11:30:38 -04001453 rebind_marked_interfaces(udev);
Oliver Neukum98d9a822012-01-11 08:38:35 +01001454 return 0;
1455}
1456
1457/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001458int usb_resume(struct device *dev, pm_message_t msg)
1459{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001460 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001461 int status;
1462
Oliver Neukum98d9a822012-01-11 08:38:35 +01001463 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001464 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001465 * Unbind the interfaces that will need rebinding later,
1466 * because they fail to support reset_resume.
1467 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001468 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001469 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001470 status = usb_resume_both(udev, msg);
1471 if (status == 0) {
1472 pm_runtime_disable(dev);
1473 pm_runtime_set_active(dev);
1474 pm_runtime_enable(dev);
Alan Stern6aec0442014-03-12 11:30:38 -04001475 unbind_marked_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001476 }
Alan Stern0c590e22010-01-08 12:57:14 -05001477
1478 /* Avoid PM error messages for devices disconnected while suspended
1479 * as we'll display regular disconnect messages just a bit later.
1480 */
Peter Chen7491f132010-09-27 16:43:25 +08001481 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001482 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001483 return status;
1484}
1485
Alan Stern088f7fe2010-01-08 12:56:54 -05001486/**
1487 * usb_enable_autosuspend - allow a USB device to be autosuspended
1488 * @udev: the USB device which may be autosuspended
1489 *
1490 * This routine allows @udev to be autosuspended. An autosuspend won't
1491 * take place until the autosuspend_delay has elapsed and all the other
1492 * necessary conditions are satisfied.
1493 *
1494 * The caller must hold @udev's device lock.
1495 */
Alan Stern9e18c822010-04-02 13:22:09 -04001496void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001497{
Alan Stern9e18c822010-04-02 13:22:09 -04001498 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001499}
1500EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1501
1502/**
1503 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1504 * @udev: the USB device which may not be autosuspended
1505 *
1506 * This routine prevents @udev from being autosuspended and wakes it up
1507 * if it is already autosuspended.
1508 *
1509 * The caller must hold @udev's device lock.
1510 */
Alan Stern9e18c822010-04-02 13:22:09 -04001511void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001512{
Alan Stern9e18c822010-04-02 13:22:09 -04001513 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001514}
1515EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1516
Alan Stern645daaa2006-08-30 15:47:02 -04001517/**
1518 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001519 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001520 *
1521 * This routine should be called when a core subsystem is finished using
1522 * @udev and wants to allow it to autosuspend. Examples would be when
1523 * @udev's device file in usbfs is closed or after a configuration change.
1524 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001525 * @udev's usage counter is decremented; if it drops to 0 and all the
1526 * interfaces are inactive then a delayed autosuspend will be attempted.
1527 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001528 *
Alan Stern62e299e2010-01-08 12:56:19 -05001529 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001530 *
1531 * This routine can run only in process context.
1532 */
Alan Stern94fcda12006-11-20 11:38:46 -05001533void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001534{
Alan Stern94fcda12006-11-20 11:38:46 -05001535 int status;
1536
Ming Lei6ddf27c2010-11-15 15:57:30 -05001537 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001538 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001539 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1540 __func__, atomic_read(&udev->dev.power.usage_count),
1541 status);
Alan Stern19c26232007-02-20 15:03:32 -05001542}
1543
1544/**
Alan Stern645daaa2006-08-30 15:47:02 -04001545 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001546 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001547 *
1548 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001549 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001550 * occur until usb_autosuspend_device() is called. (Note that this will
1551 * not prevent suspend events originating in the PM core.) Examples would
1552 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001553 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001554 *
Alan Stern94fcda12006-11-20 11:38:46 -05001555 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1556 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001557 *
Alan Stern62e299e2010-01-08 12:56:19 -05001558 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001559 *
1560 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001561 *
1562 * Return: 0 on success. A negative error code otherwise.
Alan Stern645daaa2006-08-30 15:47:02 -04001563 */
Alan Stern94fcda12006-11-20 11:38:46 -05001564int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001565{
1566 int status;
1567
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001568 status = pm_runtime_get_sync(&udev->dev);
1569 if (status < 0)
1570 pm_runtime_put_sync(&udev->dev);
1571 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1572 __func__, atomic_read(&udev->dev.power.usage_count),
1573 status);
1574 if (status > 0)
1575 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001576 return status;
1577}
1578
Alan Stern645daaa2006-08-30 15:47:02 -04001579/**
1580 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001581 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001582 *
1583 * This routine should be called by an interface driver when it is
1584 * finished using @intf and wants to allow it to autosuspend. A typical
1585 * example would be a character-device driver when its device file is
1586 * closed.
1587 *
1588 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001589 * 0, a delayed autosuspend request for @intf's device is attempted. The
1590 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001591 *
Alan Stern645daaa2006-08-30 15:47:02 -04001592 * This routine can run only in process context.
1593 */
1594void usb_autopm_put_interface(struct usb_interface *intf)
1595{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001596 struct usb_device *udev = interface_to_usbdev(intf);
1597 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001598
Ming Lei6ddf27c2010-11-15 15:57:30 -05001599 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001600 atomic_dec(&intf->pm_usage_cnt);
1601 status = pm_runtime_put_sync(&intf->dev);
1602 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1603 __func__, atomic_read(&intf->dev.power.usage_count),
1604 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001605}
1606EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1607
1608/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001609 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1610 * @intf: the usb_interface whose counter should be decremented
1611 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001612 * This routine does much the same thing as usb_autopm_put_interface():
1613 * It decrements @intf's usage counter and schedules a delayed
1614 * autosuspend request if the counter is <= 0. The difference is that it
1615 * does not perform any synchronization; callers should hold a private
1616 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001617 *
1618 * Typically a driver would call this routine during an URB's completion
1619 * handler, if no more URBs were pending.
1620 *
1621 * This routine can run in atomic context.
1622 */
1623void usb_autopm_put_interface_async(struct usb_interface *intf)
1624{
1625 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001626 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001627
Ming Lei6ddf27c2010-11-15 15:57:30 -05001628 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001629 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001630 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001631 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1632 __func__, atomic_read(&intf->dev.power.usage_count),
1633 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001634}
1635EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1636
1637/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001638 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1639 * @intf: the usb_interface whose counter should be decremented
1640 *
1641 * This routine decrements @intf's usage counter but does not carry out an
1642 * autosuspend.
1643 *
1644 * This routine can run in atomic context.
1645 */
1646void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1647{
1648 struct usb_device *udev = interface_to_usbdev(intf);
1649
Ming Lei6ddf27c2010-11-15 15:57:30 -05001650 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001651 atomic_dec(&intf->pm_usage_cnt);
1652 pm_runtime_put_noidle(&intf->dev);
1653}
1654EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1655
1656/**
Alan Stern645daaa2006-08-30 15:47:02 -04001657 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001658 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001659 *
1660 * This routine should be called by an interface driver when it wants to
1661 * use @intf and needs to guarantee that it is not suspended. In addition,
1662 * the routine prevents @intf from being autosuspended subsequently. (Note
1663 * that this will not prevent suspend events originating in the PM core.)
1664 * This prevention will persist until usb_autopm_put_interface() is called
1665 * or @intf is unbound. A typical example would be a character-device
1666 * driver when its device file is opened.
1667 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001668 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1669 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001670 *
1671 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001672 *
1673 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001674 */
1675int usb_autopm_get_interface(struct usb_interface *intf)
1676{
Alan Sternaf4f7602006-10-30 17:06:45 -05001677 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001678
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001679 status = pm_runtime_get_sync(&intf->dev);
1680 if (status < 0)
1681 pm_runtime_put_sync(&intf->dev);
1682 else
1683 atomic_inc(&intf->pm_usage_cnt);
1684 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1685 __func__, atomic_read(&intf->dev.power.usage_count),
1686 status);
1687 if (status > 0)
1688 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001689 return status;
1690}
1691EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1692
Alan Stern692a1862006-10-30 17:07:51 -05001693/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001694 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1695 * @intf: the usb_interface whose counter should be incremented
1696 *
1697 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001698 * usb_autopm_get_interface(): It increments @intf's usage counter and
1699 * queues an autoresume request if the device is suspended. The
1700 * differences are that it does not perform any synchronization (callers
1701 * should hold a private lock and handle all synchronization issues
1702 * themselves), and it does not autoresume the device directly (it only
1703 * queues a request). After a successful call, the device may not yet be
1704 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001705 *
1706 * This routine can run in atomic context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001707 *
1708 * Return: 0 on success. A negative error code otherwise.
Alan Stern9ac39f22008-11-12 16:19:49 -05001709 */
1710int usb_autopm_get_interface_async(struct usb_interface *intf)
1711{
Ming Lei63defa72010-11-15 15:56:54 -05001712 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001713
Ming Lei63defa72010-11-15 15:56:54 -05001714 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001715 if (status < 0 && status != -EINPROGRESS)
1716 pm_runtime_put_noidle(&intf->dev);
1717 else
Alan Sternccf5b802009-06-29 11:00:01 -04001718 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001719 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1720 __func__, atomic_read(&intf->dev.power.usage_count),
1721 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001722 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001723 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001724 return status;
1725}
1726EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1727
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001728/**
1729 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1730 * @intf: the usb_interface whose counter should be incremented
1731 *
1732 * This routine increments @intf's usage counter but does not carry out an
1733 * autoresume.
1734 *
1735 * This routine can run in atomic context.
1736 */
1737void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1738{
1739 struct usb_device *udev = interface_to_usbdev(intf);
1740
Ming Lei6ddf27c2010-11-15 15:57:30 -05001741 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001742 atomic_inc(&intf->pm_usage_cnt);
1743 pm_runtime_get_noresume(&intf->dev);
1744}
1745EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1746
1747/* Internal routine to check whether we may autosuspend a device. */
1748static int autosuspend_check(struct usb_device *udev)
1749{
Alan Stern7560d32e2010-04-02 13:18:50 -04001750 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001751 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001752
1753 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1754 * any interface drivers require remote wakeup but it isn't available.
1755 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001756 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001757 if (udev->actconfig) {
1758 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1759 intf = udev->actconfig->interface[i];
1760
1761 /* We don't need to check interfaces that are
1762 * disabled for runtime PM. Either they are unbound
1763 * or else their drivers don't support autosuspend
1764 * and so they are permanently active.
1765 */
1766 if (intf->dev.power.disable_depth)
1767 continue;
1768 if (atomic_read(&intf->dev.power.usage_count) > 0)
1769 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001770 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001771
1772 /* Don't allow autosuspend if the device will need
1773 * a reset-resume and any of its interface drivers
1774 * doesn't include support or needs remote wakeup.
1775 */
1776 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1777 struct usb_driver *driver;
1778
1779 driver = to_usb_driver(intf->dev.driver);
1780 if (!driver->reset_resume ||
1781 intf->needs_remote_wakeup)
1782 return -EOPNOTSUPP;
1783 }
1784 }
1785 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001786 if (w && !device_can_wakeup(&udev->dev)) {
1787 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1788 return -EOPNOTSUPP;
1789 }
Alan Stern074f9dd2015-01-29 15:05:04 -05001790
1791 /*
1792 * If the device is a direct child of the root hub and the HCD
1793 * doesn't handle wakeup requests, don't allow autosuspend when
1794 * wakeup is needed.
1795 */
1796 if (w && udev->parent == udev->bus->root_hub &&
1797 bus_to_hcd(udev->bus)->cant_recv_wakeups) {
1798 dev_dbg(&udev->dev, "HCD doesn't handle wakeup requests\n");
1799 return -EOPNOTSUPP;
1800 }
1801
Alan Stern7560d32e2010-04-02 13:18:50 -04001802 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001803 return 0;
1804}
1805
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001806int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001807{
Ming Lei63defa72010-11-15 15:56:54 -05001808 struct usb_device *udev = to_usb_device(dev);
1809 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001810
1811 /* A USB device can be suspended if it passes the various autosuspend
1812 * checks. Runtime suspend for a USB device means suspending all the
1813 * interfaces and then the device itself.
1814 */
Ming Lei63defa72010-11-15 15:56:54 -05001815 if (autosuspend_check(udev) != 0)
1816 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001817
Ming Lei63defa72010-11-15 15:56:54 -05001818 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001819
1820 /* Allow a retry if autosuspend failed temporarily */
1821 if (status == -EAGAIN || status == -EBUSY)
1822 usb_mark_last_busy(udev);
1823
Alan Stern8ef42dd2014-05-23 10:45:54 -04001824 /*
1825 * The PM core reacts badly unless the return code is 0,
1826 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
1827 * (except for root hubs, because they don't suspend through
1828 * an upstream port like other USB devices).
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001829 */
Alan Stern8ef42dd2014-05-23 10:45:54 -04001830 if (status != 0 && udev->parent)
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001831 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001832 return status;
1833}
1834
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001835int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001836{
Ming Lei63defa72010-11-15 15:56:54 -05001837 struct usb_device *udev = to_usb_device(dev);
1838 int status;
1839
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001840 /* Runtime resume for a USB device means resuming both the device
1841 * and all its interfaces.
1842 */
Ming Lei63defa72010-11-15 15:56:54 -05001843 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001844 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001845}
1846
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001847int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001848{
Ming Lei63defa72010-11-15 15:56:54 -05001849 struct usb_device *udev = to_usb_device(dev);
1850
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001851 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001852 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001853 */
Ming Lei63defa72010-11-15 15:56:54 -05001854 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001855 pm_runtime_autosuspend(dev);
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001856 /* Tell the core not to suspend it, though. */
1857 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001858}
1859
Andiry Xu65580b432011-09-23 14:19:52 -07001860int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1861{
1862 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1863 int ret = -EPERM;
1864
Sarah Sharpde68bab2013-09-30 17:26:28 +03001865 if (enable && !udev->usb2_hw_lpm_allowed)
1866 return 0;
1867
Andiry Xu65580b432011-09-23 14:19:52 -07001868 if (hcd->driver->set_usb2_hw_lpm) {
1869 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1870 if (!ret)
1871 udev->usb2_hw_lpm_enabled = enable;
1872 }
1873
1874 return ret;
1875}
1876
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01001877#endif /* CONFIG_PM */
Alan Stern645daaa2006-08-30 15:47:02 -04001878
Alan Stern36e56a32006-07-01 22:08:06 -04001879struct bus_type usb_bus_type = {
1880 .name = "usb",
1881 .match = usb_device_match,
1882 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001883};