blob: cdee5130638b109e5be899c41b8a5f432620c4e3 [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 *
Greg Kroah-Hartmanb65fba32016-10-28 17:16:36 -040018 * Released under the GPLv2 only.
19 * SPDX-License-Identifier: GPL-2.0
20 *
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080021 * NOTE! This is not actually a driver at all, rather this is
22 * just a collection of helper routines that implement the
Alan Stern36e56a32006-07-01 22:08:06 -040023 * matching, probing, releasing, suspending and resuming for
24 * real drivers.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080025 *
26 */
27
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080028#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040030#include <linux/export.h>
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080031#include <linux/usb.h>
Alan Stern6bc6cff2007-05-04 11:53:03 -040032#include <linux/usb/quirks.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020033#include <linux/usb/hcd.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020034
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080035#include "usb.h"
36
Alan Stern20dfdad2007-05-22 11:50:17 -040037
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080038/*
39 * Adds a new dynamic USBdevice ID to this driver,
40 * and cause the driver to probe for all devices again.
41 */
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010042ssize_t usb_store_new_id(struct usb_dynids *dynids,
Wolfram Sang2fc82c22014-01-10 19:36:42 +010043 const struct usb_device_id *id_table,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010044 struct device_driver *driver,
45 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080046{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080047 struct usb_dynid *dynid;
48 u32 idVendor = 0;
49 u32 idProduct = 0;
Josua Dietzeff231db2011-10-23 14:22:29 +020050 unsigned int bInterfaceClass = 0;
Wolfram Sang2fc82c22014-01-10 19:36:42 +010051 u32 refVendor, refProduct;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080052 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070053 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080054
Wolfram Sang2fc82c22014-01-10 19:36:42 +010055 fields = sscanf(buf, "%x %x %x %x %x", &idVendor, &idProduct,
56 &bInterfaceClass, &refVendor, &refProduct);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080057 if (fields < 2)
58 return -EINVAL;
59
60 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
61 if (!dynid)
62 return -ENOMEM;
63
64 INIT_LIST_HEAD(&dynid->node);
65 dynid->id.idVendor = idVendor;
66 dynid->id.idProduct = idProduct;
67 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Wolfram Sangc63fe8f2014-01-10 19:36:41 +010068 if (fields > 2 && bInterfaceClass) {
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010069 if (bInterfaceClass > 255) {
70 retval = -EINVAL;
71 goto fail;
72 }
Wolfram Sangc63fe8f2014-01-10 19:36:41 +010073
Josua Dietzeff231db2011-10-23 14:22:29 +020074 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
75 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
76 }
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080077
Wolfram Sang2fc82c22014-01-10 19:36:42 +010078 if (fields > 4) {
79 const struct usb_device_id *id = id_table;
80
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010081 if (!id) {
82 retval = -ENODEV;
83 goto fail;
84 }
Wolfram Sang1b9fb312014-01-13 11:29:23 +010085
Wolfram Sang2fc82c22014-01-10 19:36:42 +010086 for (; id->match_flags; id++)
Wolfram Sang52a6966c2014-01-12 10:07:50 +010087 if (id->idVendor == refVendor && id->idProduct == refProduct)
Wolfram Sang2fc82c22014-01-10 19:36:42 +010088 break;
Wolfram Sang52a6966c2014-01-12 10:07:50 +010089
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010090 if (id->match_flags) {
Wolfram Sang52a6966c2014-01-12 10:07:50 +010091 dynid->id.driver_info = id->driver_info;
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010092 } else {
93 retval = -ENODEV;
94 goto fail;
95 }
Wolfram Sang2fc82c22014-01-10 19:36:42 +010096 }
97
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010098 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020099 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100100 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800101
Alan Sterncef9bc52012-01-24 13:34:41 -0500102 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800103
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700104 if (retval)
105 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800106 return count;
Christian Engelmayer7f196ca2014-01-28 22:22:27 +0100107
108fail:
109 kfree(dynid);
110 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800111}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100112EXPORT_SYMBOL_GPL(usb_store_new_id);
113
Bjørn Morkef206f32012-05-13 12:35:00 +0200114ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200115{
116 struct usb_dynid *dynid;
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200117 size_t count = 0;
118
Bjørn Morkef206f32012-05-13 12:35:00 +0200119 list_for_each_entry(dynid, &dynids->list, node)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200120 if (dynid->id.bInterfaceClass != 0)
121 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
122 dynid->id.idVendor, dynid->id.idProduct,
123 dynid->id.bInterfaceClass);
124 else
125 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
126 dynid->id.idVendor, dynid->id.idProduct);
127 return count;
128}
Bjørn Morkef206f32012-05-13 12:35:00 +0200129EXPORT_SYMBOL_GPL(usb_show_dynids);
130
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700131static ssize_t new_id_show(struct device_driver *driver, char *buf)
Bjørn Morkef206f32012-05-13 12:35:00 +0200132{
133 struct usb_driver *usb_drv = to_usb_driver(driver);
134
135 return usb_show_dynids(&usb_drv->dynids, buf);
136}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200137
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700138static ssize_t new_id_store(struct device_driver *driver,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100139 const char *buf, size_t count)
140{
141 struct usb_driver *usb_drv = to_usb_driver(driver);
142
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100143 return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100144}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700145static DRIVER_ATTR_RW(new_id);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800146
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700147/*
148 * Remove a USB device ID from this driver
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800149 */
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700150static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
151 size_t count)
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800152{
153 struct usb_dynid *dynid, *n;
154 struct usb_driver *usb_driver = to_usb_driver(driver);
Alan Coxac08de32012-09-17 11:55:23 +0100155 u32 idVendor;
156 u32 idProduct;
157 int fields;
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800158
159 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
160 if (fields < 2)
161 return -EINVAL;
162
163 spin_lock(&usb_driver->dynids.lock);
164 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
165 struct usb_device_id *id = &dynid->id;
Kris Borer79a02742015-06-16 13:24:53 -0400166
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800167 if ((id->idVendor == idVendor) &&
168 (id->idProduct == idProduct)) {
169 list_del(&dynid->node);
170 kfree(dynid);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800171 break;
172 }
173 }
174 spin_unlock(&usb_driver->dynids.lock);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800175 return count;
176}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700177
178static ssize_t remove_id_show(struct device_driver *driver, char *buf)
179{
180 return new_id_show(driver, buf);
181}
182static DRIVER_ATTR_RW(remove_id);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800183
Alan Sterned283e92012-01-24 14:35:13 -0500184static int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800185{
186 int error = 0;
187
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800188 if (usb_drv->no_dynamic_id)
189 goto exit;
190
Alan Sterned283e92012-01-24 14:35:13 -0500191 if (usb_drv->probe != NULL) {
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800192 error = driver_create_file(&usb_drv->drvwrap.driver,
193 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500194 if (error == 0) {
195 error = driver_create_file(&usb_drv->drvwrap.driver,
196 &driver_attr_remove_id);
197 if (error)
198 driver_remove_file(&usb_drv->drvwrap.driver,
199 &driver_attr_new_id);
200 }
201 }
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800202exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800203 return error;
204}
205
Alan Sterned283e92012-01-24 14:35:13 -0500206static void usb_remove_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800207{
208 if (usb_drv->no_dynamic_id)
209 return;
210
Alan Sterned283e92012-01-24 14:35:13 -0500211 if (usb_drv->probe != NULL) {
212 driver_remove_file(&usb_drv->drvwrap.driver,
213 &driver_attr_remove_id);
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800214 driver_remove_file(&usb_drv->drvwrap.driver,
215 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500216 }
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800217}
218
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800219static void usb_free_dynids(struct usb_driver *usb_drv)
220{
221 struct usb_dynid *dynid, *n;
222
223 spin_lock(&usb_drv->dynids.lock);
224 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
225 list_del(&dynid->node);
226 kfree(dynid);
227 }
228 spin_unlock(&usb_drv->dynids.lock);
229}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800230
231static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
232 struct usb_driver *drv)
233{
234 struct usb_dynid *dynid;
235
236 spin_lock(&drv->dynids.lock);
237 list_for_each_entry(dynid, &drv->dynids.list, node) {
238 if (usb_match_one_id(intf, &dynid->id)) {
239 spin_unlock(&drv->dynids.lock);
240 return &dynid->id;
241 }
242 }
243 spin_unlock(&drv->dynids.lock);
244 return NULL;
245}
246
247
Alan Stern8bb54ab2006-07-01 22:08:49 -0400248/* called from driver core with dev locked */
249static int usb_probe_device(struct device *dev)
250{
251 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200252 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500253 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400254
Harvey Harrison441b62c2008-03-03 16:08:34 -0800255 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400256
Alan Stern8bb54ab2006-07-01 22:08:49 -0400257 /* TODO: Add real matching code */
258
Alan Stern645daaa2006-08-30 15:47:02 -0400259 /* The device should always appear to be in use
Masanari Iida02582e92012-08-22 19:11:26 +0900260 * unless the driver supports autosuspend.
Alan Stern645daaa2006-08-30 15:47:02 -0400261 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500262 if (!udriver->supports_autosuspend)
263 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400264
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500265 if (!error)
266 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400267 return error;
268}
269
270/* called from driver core with dev locked */
271static int usb_unbind_device(struct device *dev)
272{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500273 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400274 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
275
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500276 udriver->disconnect(udev);
277 if (!udriver->supports_autosuspend)
278 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400279 return 0;
280}
281
Alan Stern8bb54ab2006-07-01 22:08:49 -0400282/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800283static int usb_probe_interface(struct device *dev)
284{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400285 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200286 struct usb_interface *intf = to_usb_interface(dev);
287 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800288 const struct usb_device_id *id;
289 int error = -ENODEV;
Alan Stern6fb650d2016-04-29 15:25:17 -0400290 int lpm_disable_error = -ENODEV;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800291
Harvey Harrison441b62c2008-03-03 16:08:34 -0800292 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800293
Alan Stern78d9a482008-06-23 16:00:40 -0400294 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800295
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400296 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500297 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400298
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800299 if (udev->authorized == 0) {
300 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500301 return error;
Stefan Koch8d1f8572015-08-25 21:10:07 +0200302 } else if (intf->authorized == 0) {
303 dev_err(&intf->dev, "Interface %d is not authorized for usage\n",
304 intf->altsetting->desc.bInterfaceNumber);
305 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800306 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700307
Bjørn Mork31c6bf72014-01-11 02:04:00 +0100308 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800309 if (!id)
Bjørn Mork31c6bf72014-01-11 02:04:00 +0100310 id = usb_match_id(intf, driver->id_table);
Alan Stern0f3dda92010-01-08 12:56:04 -0500311 if (!id)
312 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800313
Alan Stern0f3dda92010-01-08 12:56:04 -0500314 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400315
Alan Stern0f3dda92010-01-08 12:56:04 -0500316 error = usb_autoresume_device(udev);
317 if (error)
318 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400319
Alan Stern0f3dda92010-01-08 12:56:04 -0500320 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400321
Alan Stern571dc792010-04-09 16:03:43 -0400322 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500323 * runtime-PM-enabled only if the driver has autosuspend support.
324 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500325 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500326 pm_runtime_set_active(dev);
327 pm_suspend_ignore_children(dev, false);
328 if (driver->supports_autosuspend)
329 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200330
Sarah Sharp83060952012-05-02 14:25:52 -0700331 /* If the new driver doesn't allow hub-initiated LPM, and we can't
332 * disable hub-initiated LPM, then fail the probe.
333 *
334 * Otherwise, leaving LPM enabled should be harmless, because the
335 * endpoint intervals should remain the same, and the U1/U2 timeouts
336 * should remain the same.
337 *
338 * If we need to install alt setting 0 before probe, or another alt
339 * setting during probe, that should also be fine. usb_set_interface()
340 * will attempt to disable LPM, and fail if it can't disable it.
341 */
Alan Stern6fb650d2016-04-29 15:25:17 -0400342 if (driver->disable_hub_initiated_lpm) {
343 lpm_disable_error = usb_unlocked_disable_lpm(udev);
344 if (lpm_disable_error) {
345 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
346 __func__, driver->name);
347 error = lpm_disable_error;
348 goto err;
349 }
Sarah Sharp83060952012-05-02 14:25:52 -0700350 }
351
Alan Stern0f3dda92010-01-08 12:56:04 -0500352 /* Carry out a deferred switch to altsetting 0 */
353 if (intf->needs_altsetting0) {
354 error = usb_set_interface(udev, intf->altsetting[0].
355 desc.bInterfaceNumber, 0);
356 if (error < 0)
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200357 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500358 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800359 }
360
Alan Stern0f3dda92010-01-08 12:56:04 -0500361 error = driver->probe(intf, id);
362 if (error)
363 goto err;
364
365 intf->condition = USB_INTERFACE_BOUND;
Sarah Sharp83060952012-05-02 14:25:52 -0700366
367 /* If the LPM disable succeeded, balance the ref counts. */
368 if (!lpm_disable_error)
369 usb_unlocked_enable_lpm(udev);
370
Alan Stern0f3dda92010-01-08 12:56:04 -0500371 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800372 return error;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200373
Alan Stern0f3dda92010-01-08 12:56:04 -0500374 err:
Hans de Goedee714fad2012-05-22 11:36:59 +0200375 usb_set_intfdata(intf, NULL);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200376 intf->needs_remote_wakeup = 0;
377 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500378
Sarah Sharpd01f87c2012-10-04 09:53:43 -0700379 /* If the LPM disable succeeded, balance the ref counts. */
380 if (!lpm_disable_error)
381 usb_unlocked_enable_lpm(udev);
382
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500383 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400384 if (driver->supports_autosuspend)
385 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500386 pm_runtime_set_suspended(dev);
387
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200388 usb_autosuspend_device(udev);
389 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800390}
391
Alan Stern8bb54ab2006-07-01 22:08:49 -0400392/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800393static int usb_unbind_interface(struct device *dev)
394{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400395 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800396 struct usb_interface *intf = to_usb_interface(dev);
Hans de Goede6343e8b2013-10-09 17:19:26 +0200397 struct usb_host_endpoint *ep, **eps = NULL;
Alan Stern645daaa2006-08-30 15:47:02 -0400398 struct usb_device *udev;
Alan Stern6fb650d2016-04-29 15:25:17 -0400399 int i, j, error, r;
400 int lpm_disable_error = -ENODEV;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800401
402 intf->condition = USB_INTERFACE_UNBINDING;
403
Alan Stern645daaa2006-08-30 15:47:02 -0400404 /* Autoresume for set_interface call below */
405 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500406 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400407
Alan Stern6fb650d2016-04-29 15:25:17 -0400408 /* If hub-initiated LPM policy may change, attempt to disable LPM until
Sarah Sharp83060952012-05-02 14:25:52 -0700409 * the driver is unbound. If LPM isn't disabled, that's fine because it
410 * wouldn't be enabled unless all the bound interfaces supported
411 * hub-initiated LPM.
412 */
Alan Stern6fb650d2016-04-29 15:25:17 -0400413 if (driver->disable_hub_initiated_lpm)
414 lpm_disable_error = usb_unlocked_disable_lpm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -0700415
Alan Stern1299cff2014-07-17 15:40:57 -0400416 /*
417 * Terminate all URBs for this interface unless the driver
418 * supports "soft" unbinding and the device is still present.
Alan Stern9da82bd2008-05-08 11:54:37 -0400419 */
Alan Stern1299cff2014-07-17 15:40:57 -0400420 if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
Alan Sternddeac4e2009-01-15 17:03:33 -0500421 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800422
Alan Stern8bb54ab2006-07-01 22:08:49 -0400423 driver->disconnect(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800424
Hans de Goede6343e8b2013-10-09 17:19:26 +0200425 /* Free streams */
426 for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
427 ep = &intf->cur_altsetting->endpoint[i];
428 if (ep->streams == 0)
429 continue;
430 if (j == 0) {
Muhammad Falak R Wani9766f252015-09-07 21:30:25 +0530431 eps = kmalloc_array(USB_MAXENDPOINTS, sizeof(void *),
Hans de Goede6343e8b2013-10-09 17:19:26 +0200432 GFP_KERNEL);
Muhammad Falak R Wani9766f252015-09-07 21:30:25 +0530433 if (!eps)
Hans de Goede6343e8b2013-10-09 17:19:26 +0200434 break;
Hans de Goede6343e8b2013-10-09 17:19:26 +0200435 }
436 eps[j++] = ep;
437 }
438 if (j) {
439 usb_free_streams(intf, eps, j, GFP_KERNEL);
440 kfree(eps);
441 }
442
Alan Stern55151d72008-08-12 14:33:59 -0400443 /* Reset other interface state.
444 * We cannot do a Set-Interface if the device is suspended or
445 * if it is prepared for a system sleep (since installing a new
446 * altsetting means creating new endpoint device entries).
447 * When either of these happens, defer the Set-Interface.
448 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500449 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
450 /* Already in altsetting 0 so skip Set-Interface.
451 * Just re-enable it without affecting the endpoint toggles.
452 */
453 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200454 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200455 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400456 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200457 if (r < 0)
458 intf->needs_altsetting0 = 1;
459 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400460 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200461 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800462 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400463
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800464 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400465 intf->needs_remote_wakeup = 0;
466
Sarah Sharp83060952012-05-02 14:25:52 -0700467 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
468 if (!lpm_disable_error)
469 usb_unlocked_enable_lpm(udev);
470
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500471 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400472 if (driver->supports_autosuspend)
473 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500474 pm_runtime_set_suspended(dev);
475
476 /* Undo any residual pm_autopm_get_interface_* calls */
477 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
478 usb_autopm_put_interface_no_suspend(intf);
479 atomic_set(&intf->pm_usage_cnt, 0);
480
Alan Stern645daaa2006-08-30 15:47:02 -0400481 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500482 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800483
484 return 0;
485}
486
Alan Stern36e56a32006-07-01 22:08:06 -0400487/**
488 * usb_driver_claim_interface - bind a driver to an interface
489 * @driver: the driver to be bound
490 * @iface: the interface to which it will be bound; must be in the
491 * usb device's active configuration
492 * @priv: driver data associated with that interface
493 *
494 * This is used by usb device drivers that need to claim more than one
495 * interface on a device when probing (audio and acm are current examples).
496 * No device driver should directly modify internal usb_interface or
497 * usb_device structure members.
498 *
499 * Few drivers should need to use this routine, since the most natural
500 * way to bind to an interface is to return the private data from
501 * the driver's probe() method.
502 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400503 * Callers must own the device lock, so driver probe() entries don't need
504 * extra locking, but other call contexts may need to explicitly claim that
505 * lock.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200506 *
507 * Return: 0 on success.
Alan Stern36e56a32006-07-01 22:08:06 -0400508 */
509int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800510 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400511{
Oliver Neukum0b818e32016-03-16 13:26:17 +0100512 struct device *dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700513 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700514 int retval = 0;
Alan Stern6fb650d2016-04-29 15:25:17 -0400515 int lpm_disable_error = -ENODEV;
Alan Stern36e56a32006-07-01 22:08:06 -0400516
Oliver Neukum0b818e32016-03-16 13:26:17 +0100517 if (!iface)
518 return -ENODEV;
519
520 dev = &iface->dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400521 if (dev->driver)
522 return -EBUSY;
523
Stefan Koch8d1f8572015-08-25 21:10:07 +0200524 /* reject claim if interface is not authorized */
525 if (!iface->authorized)
526 return -ENODEV;
527
Sarah Sharp83060952012-05-02 14:25:52 -0700528 udev = interface_to_usbdev(iface);
529
Alan Stern8bb54ab2006-07-01 22:08:49 -0400530 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400531 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400532 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400533
Alan Stern36e56a32006-07-01 22:08:06 -0400534 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500535
Alan Stern6fb650d2016-04-29 15:25:17 -0400536 /* See the comment about disabling LPM in usb_probe_interface(). */
537 if (driver->disable_hub_initiated_lpm) {
538 lpm_disable_error = usb_unlocked_disable_lpm(udev);
539 if (lpm_disable_error) {
540 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
541 __func__, driver->name);
542 return -ENOMEM;
543 }
Sarah Sharp83060952012-05-02 14:25:52 -0700544 }
545
Alan Stern89842ae2010-05-11 11:44:06 -0400546 /* Claimed interfaces are initially inactive (suspended) and
547 * runtime-PM-enabled, but only if the driver has autosuspend
548 * support. Otherwise they are marked active, to prevent the
549 * device from being autosuspended, but left disabled. In either
550 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500551 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500552 pm_suspend_ignore_children(dev, false);
553 if (driver->supports_autosuspend)
554 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400555 else
556 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400557
558 /* if interface was already added, bind now; else let
559 * the future device_add() bind it, bypassing probe()
560 */
561 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700562 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400563
Sarah Sharp83060952012-05-02 14:25:52 -0700564 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
565 if (!lpm_disable_error)
566 usb_unlocked_enable_lpm(udev);
567
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700568 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400569}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600570EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400571
572/**
573 * usb_driver_release_interface - unbind a driver from an interface
574 * @driver: the driver to be unbound
575 * @iface: the interface from which it will be unbound
576 *
577 * This can be used by drivers to release an interface without waiting
578 * for their disconnect() methods to be called. In typical cases this
579 * also causes the driver disconnect() method to be called.
580 *
581 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400582 * Callers must own the device lock, so driver disconnect() entries don't
583 * need extra locking, but other call contexts may need to explicitly claim
584 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400585 */
586void usb_driver_release_interface(struct usb_driver *driver,
587 struct usb_interface *iface)
588{
589 struct device *dev = &iface->dev;
590
591 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400592 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400593 return;
594
595 /* don't release from within disconnect() */
596 if (iface->condition != USB_INTERFACE_BOUND)
597 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400598 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400599
Alan Stern91f8d062009-04-16 15:35:09 -0400600 /* Release via the driver core only if the interface
601 * has already been registered
602 */
Alan Stern36e56a32006-07-01 22:08:06 -0400603 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400604 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800605 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800606 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400607 usb_unbind_interface(dev);
608 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800609 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400610 }
Alan Stern36e56a32006-07-01 22:08:06 -0400611}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600612EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400613
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800614/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100615int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800616{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800617 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
618 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
619 return 0;
620
621 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
622 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
623 return 0;
624
625 /* No need to test id->bcdDevice_lo != 0, since 0 is never
626 greater than any unsigned number. */
627 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
628 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
629 return 0;
630
631 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
632 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
633 return 0;
634
635 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
636 (id->bDeviceClass != dev->descriptor.bDeviceClass))
637 return 0;
638
639 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800640 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800641 return 0;
642
643 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
644 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
645 return 0;
646
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100647 return 1;
648}
649
650/* returns 0 if no match, 1 if match */
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200651int usb_match_one_id_intf(struct usb_device *dev,
652 struct usb_host_interface *intf,
653 const struct usb_device_id *id)
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100654{
Bjørn Mork81df2d52012-05-18 21:27:43 +0200655 /* The interface class, subclass, protocol and number should never be
Alan Stern93c8bf42006-10-18 16:41:51 -0400656 * checked for a match if the device class is Vendor Specific,
657 * unless the match record specifies the Vendor ID. */
658 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
659 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
660 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
661 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
Bjørn Mork81df2d52012-05-18 21:27:43 +0200662 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
663 USB_DEVICE_ID_MATCH_INT_NUMBER)))
Alan Stern93c8bf42006-10-18 16:41:51 -0400664 return 0;
665
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800666 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
667 (id->bInterfaceClass != intf->desc.bInterfaceClass))
668 return 0;
669
670 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
671 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
672 return 0;
673
674 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
675 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
676 return 0;
677
Bjørn Mork81df2d52012-05-18 21:27:43 +0200678 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
679 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
680 return 0;
681
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800682 return 1;
683}
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200684
685/* returns 0 if no match, 1 if match */
686int usb_match_one_id(struct usb_interface *interface,
687 const struct usb_device_id *id)
688{
689 struct usb_host_interface *intf;
690 struct usb_device *dev;
691
692 /* proc_connectinfo in devio.c may call us with id == NULL. */
693 if (id == NULL)
694 return 0;
695
696 intf = interface->cur_altsetting;
697 dev = interface_to_usbdev(interface);
698
699 if (!usb_match_device(dev, id))
700 return 0;
701
702 return usb_match_one_id_intf(dev, intf, id);
703}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100704EXPORT_SYMBOL_GPL(usb_match_one_id);
705
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800706/**
707 * usb_match_id - find first usb_device_id matching device or interface
708 * @interface: the interface of interest
709 * @id: array of usb_device_id structures, terminated by zero entry
710 *
711 * usb_match_id searches an array of usb_device_id's and returns
712 * the first one matching the device or interface, or null.
713 * This is used when binding (or rebinding) a driver to an interface.
714 * Most USB device drivers will use this indirectly, through the usb core,
715 * but some layered driver frameworks use it directly.
716 * These device tables are exported with MODULE_DEVICE_TABLE, through
717 * modutils, to support the driver loading functionality of USB hotplugging.
718 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200719 * Return: The first matching usb_device_id, or %NULL.
720 *
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800721 * What Matches:
722 *
723 * The "match_flags" element in a usb_device_id controls which
724 * members are used. If the corresponding bit is set, the
725 * value in the device_id must match its corresponding member
726 * in the device or interface descriptor, or else the device_id
727 * does not match.
728 *
729 * "driver_info" is normally used only by device drivers,
730 * but you can create a wildcard "matches anything" usb_device_id
731 * as a driver's "modules.usbmap" entry if you provide an id with
732 * only a nonzero "driver_info" field. If you do this, the USB device
733 * driver's probe() routine should use additional intelligence to
734 * decide whether to bind to the specified interface.
735 *
736 * What Makes Good usb_device_id Tables:
737 *
738 * The match algorithm is very simple, so that intelligence in
739 * driver selection must come from smart driver id records.
740 * Unless you have good reasons to use another selection policy,
741 * provide match elements only in related groups, and order match
742 * specifiers from specific to general. Use the macros provided
743 * for that purpose if you can.
744 *
745 * The most specific match specifiers use device descriptor
746 * data. These are commonly used with product-specific matches;
747 * the USB_DEVICE macro lets you provide vendor and product IDs,
748 * and you can also match against ranges of product revisions.
749 * These are widely used for devices with application or vendor
750 * specific bDeviceClass values.
751 *
752 * Matches based on device class/subclass/protocol specifications
753 * are slightly more general; use the USB_DEVICE_INFO macro, or
754 * its siblings. These are used with single-function devices
755 * where bDeviceClass doesn't specify that each interface has
756 * its own class.
757 *
758 * Matches based on interface class/subclass/protocol are the
759 * most general; they let drivers bind to any interface on a
760 * multiple-function device. Use the USB_INTERFACE_INFO
761 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400762 * devices (as recorded in bInterfaceClass).
763 *
764 * Note that an entry created by USB_INTERFACE_INFO won't match
765 * any interface if the device class is set to Vendor-Specific.
766 * This is deliberate; according to the USB spec the meanings of
767 * the interface class/subclass/protocol for these devices are also
768 * vendor-specific, and hence matching against a standard product
769 * class wouldn't work anyway. If you really want to use an
770 * interface-based match for such a device, create a match record
771 * that also specifies the vendor ID. (Unforunately there isn't a
772 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800773 *
774 * Within those groups, remember that not all combinations are
775 * meaningful. For example, don't give a product version range
776 * without vendor and product IDs; or specify a protocol without
777 * its associated class and subclass.
778 */
779const struct usb_device_id *usb_match_id(struct usb_interface *interface,
780 const struct usb_device_id *id)
781{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800782 /* proc_connectinfo in devio.c may call us with id == NULL. */
783 if (id == NULL)
784 return NULL;
785
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800786 /* It is important to check that id->driver_info is nonzero,
787 since an entry that is all zeroes except for a nonzero
788 id->driver_info is the way to create an entry that
789 indicates that the driver want to examine every
790 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800791 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
792 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800793 if (usb_match_one_id(interface, id))
794 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800795 }
796
797 return NULL;
798}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600799EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800800
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100801static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800802{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400803 /* devices and interfaces are handled separately */
804 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800805
Alan Stern8bb54ab2006-07-01 22:08:49 -0400806 /* interface drivers never match devices */
807 if (!is_usb_device_driver(drv))
808 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800809
Alan Stern8bb54ab2006-07-01 22:08:49 -0400810 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800811 return 1;
812
Kay Sievers55129662009-05-04 19:48:32 +0200813 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400814 struct usb_interface *intf;
815 struct usb_driver *usb_drv;
816 const struct usb_device_id *id;
817
818 /* device drivers never match interfaces */
819 if (is_usb_device_driver(drv))
820 return 0;
821
822 intf = to_usb_interface(dev);
823 usb_drv = to_usb_driver(drv);
824
825 id = usb_match_id(intf, usb_drv->id_table);
826 if (id)
827 return 1;
828
829 id = usb_match_dynamic_id(intf, usb_drv);
830 if (id)
831 return 1;
832 }
833
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800834 return 0;
835}
836
Kay Sievers7eff2e72007-08-14 15:15:12 +0200837static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400838{
Alan Stern36e56a32006-07-01 22:08:06 -0400839 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400840
Kay Sievers55129662009-05-04 19:48:32 +0200841 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400842 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200843 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100844 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200845
Alan Stern8bb54ab2006-07-01 22:08:49 -0400846 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200847 } else {
848 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400849 }
Alan Stern36e56a32006-07-01 22:08:06 -0400850
851 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500852 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200853 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400854 return -ENODEV;
855 }
856 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200857 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400858 return -ENODEV;
859 }
860
Alan Stern36e56a32006-07-01 22:08:06 -0400861 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200862 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400863 le16_to_cpu(usb_dev->descriptor.idVendor),
864 le16_to_cpu(usb_dev->descriptor.idProduct),
865 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
866 return -ENOMEM;
867
868 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200869 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400870 usb_dev->descriptor.bDeviceClass,
871 usb_dev->descriptor.bDeviceSubClass,
872 usb_dev->descriptor.bDeviceProtocol))
873 return -ENOMEM;
874
Alan Stern36e56a32006-07-01 22:08:06 -0400875 return 0;
876}
877
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800878/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400879 * usb_register_device_driver - register a USB device (not interface) driver
880 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800881 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800882 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400883 * Registers a USB device driver with the USB core. The list of
884 * unattached devices will be rescanned whenever a new driver is
885 * added, allowing the new driver to attach to any recognized devices.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200886 *
887 * Return: A negative error code on failure and 0 on success.
Alan Stern8bb54ab2006-07-01 22:08:49 -0400888 */
889int usb_register_device_driver(struct usb_device_driver *new_udriver,
890 struct module *owner)
891{
892 int retval = 0;
893
894 if (usb_disabled())
895 return -ENODEV;
896
897 new_udriver->drvwrap.for_devices = 1;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100898 new_udriver->drvwrap.driver.name = new_udriver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400899 new_udriver->drvwrap.driver.bus = &usb_bus_type;
900 new_udriver->drvwrap.driver.probe = usb_probe_device;
901 new_udriver->drvwrap.driver.remove = usb_unbind_device;
902 new_udriver->drvwrap.driver.owner = owner;
903
904 retval = driver_register(&new_udriver->drvwrap.driver);
905
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700906 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400907 pr_info("%s: registered new device driver %s\n",
908 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700909 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400910 printk(KERN_ERR "%s: error %d registering device "
911 " driver %s\n",
912 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400913
914 return retval;
915}
916EXPORT_SYMBOL_GPL(usb_register_device_driver);
917
918/**
919 * usb_deregister_device_driver - unregister a USB device (not interface) driver
920 * @udriver: USB operations of the device driver to unregister
921 * Context: must be able to sleep
922 *
923 * Unlinks the specified driver from the internal USB driver list.
924 */
925void usb_deregister_device_driver(struct usb_device_driver *udriver)
926{
927 pr_info("%s: deregistering device driver %s\n",
928 usbcore_name, udriver->name);
929
930 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400931}
932EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
933
934/**
935 * usb_register_driver - register a USB interface driver
936 * @new_driver: USB operations for the interface driver
937 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800938 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400939 *
940 * Registers a USB interface driver with the USB core. The list of
941 * unattached interfaces will be rescanned whenever a new driver is
942 * added, allowing the new driver to attach to any recognized interfaces.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200943 *
944 * Return: A negative error code on failure and 0 on success.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800945 *
946 * NOTE: if you want your driver to use the USB major number, you must call
947 * usb_register_dev() to enable that functionality. This function no longer
948 * takes care of that.
949 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800950int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
951 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800952{
953 int retval = 0;
954
955 if (usb_disabled())
956 return -ENODEV;
957
Alan Stern8bb54ab2006-07-01 22:08:49 -0400958 new_driver->drvwrap.for_devices = 0;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100959 new_driver->drvwrap.driver.name = new_driver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400960 new_driver->drvwrap.driver.bus = &usb_bus_type;
961 new_driver->drvwrap.driver.probe = usb_probe_interface;
962 new_driver->drvwrap.driver.remove = usb_unbind_interface;
963 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800964 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800965 spin_lock_init(&new_driver->dynids.lock);
966 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800967
Alan Stern8bb54ab2006-07-01 22:08:49 -0400968 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800969 if (retval)
970 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800971
Alan Sterned283e92012-01-24 14:35:13 -0500972 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800973 if (retval)
974 goto out_newid;
975
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800976 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800977 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800978
979out:
980 return retval;
981
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800982out_newid:
983 driver_unregister(&new_driver->drvwrap.driver);
984
985 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400986 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800987 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800988 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800989}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600990EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800991
992/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400993 * usb_deregister - unregister a USB interface driver
994 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800995 * Context: must be able to sleep
996 *
997 * Unlinks the specified driver from the internal USB driver list.
998 *
999 * NOTE: If you called usb_register_dev(), you still need to call
1000 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
1001 * this * call will no longer do it for you.
1002 */
1003void usb_deregister(struct usb_driver *driver)
1004{
Alan Stern8bb54ab2006-07-01 22:08:49 -04001005 pr_info("%s: deregistering interface driver %s\n",
1006 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -08001007
Alan Sterned283e92012-01-24 14:35:13 -05001008 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -04001009 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -05001010 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -08001011}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001012EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -04001013
Alan Stern78d9a482008-06-23 16:00:40 -04001014/* Forced unbinding of a USB interface driver, either because
1015 * it doesn't support pre_reset/post_reset/reset_resume or
1016 * because it doesn't support suspend/resume.
1017 *
Alan Stern6aec0442014-03-12 11:30:38 -04001018 * The caller must hold @intf's device's lock, but not @intf's lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001019 */
1020void usb_forced_unbind_intf(struct usb_interface *intf)
1021{
1022 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
1023
1024 dev_dbg(&intf->dev, "forced unbind\n");
1025 usb_driver_release_interface(driver, intf);
1026
1027 /* Mark the interface for later rebinding */
1028 intf->needs_binding = 1;
1029}
1030
Alan Stern6aec0442014-03-12 11:30:38 -04001031/*
1032 * Unbind drivers for @udev's marked interfaces. These interfaces have
1033 * the needs_binding flag set, for example by usb_resume_interface().
1034 *
1035 * The caller must hold @udev's device lock.
1036 */
1037static void unbind_marked_interfaces(struct usb_device *udev)
1038{
1039 struct usb_host_config *config;
1040 int i;
1041 struct usb_interface *intf;
1042
1043 config = udev->actconfig;
1044 if (config) {
1045 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1046 intf = config->interface[i];
1047 if (intf->dev.driver && intf->needs_binding)
1048 usb_forced_unbind_intf(intf);
1049 }
1050 }
1051}
1052
Alan Stern78d9a482008-06-23 16:00:40 -04001053/* Delayed forced unbinding of a USB interface driver and scan
1054 * for rebinding.
1055 *
Alan Stern6aec0442014-03-12 11:30:38 -04001056 * The caller must hold @intf's device's lock, but not @intf's lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001057 *
Alan Stern5096aed2008-08-12 14:34:14 -04001058 * Note: Rebinds will be skipped if a system sleep transition is in
1059 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -04001060 */
Alan Stern6aec0442014-03-12 11:30:38 -04001061static void usb_rebind_intf(struct usb_interface *intf)
Alan Stern78d9a482008-06-23 16:00:40 -04001062{
1063 int rc;
1064
1065 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +01001066 if (intf->dev.driver)
1067 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001068
1069 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +02001070 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -04001071 intf->needs_binding = 0;
1072 rc = device_attach(&intf->dev);
1073 if (rc < 0)
1074 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1075 }
Alan Stern78d9a482008-06-23 16:00:40 -04001076}
1077
Alan Stern6aec0442014-03-12 11:30:38 -04001078/*
1079 * Rebind drivers to @udev's marked interfaces. These interfaces have
1080 * the needs_binding flag set.
1081 *
1082 * The caller must hold @udev's device lock.
1083 */
1084static void rebind_marked_interfaces(struct usb_device *udev)
1085{
1086 struct usb_host_config *config;
1087 int i;
1088 struct usb_interface *intf;
1089
1090 config = udev->actconfig;
1091 if (config) {
1092 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1093 intf = config->interface[i];
1094 if (intf->needs_binding)
1095 usb_rebind_intf(intf);
1096 }
1097 }
1098}
1099
1100/*
1101 * Unbind all of @udev's marked interfaces and then rebind all of them.
1102 * This ordering is necessary because some drivers claim several interfaces
1103 * when they are first probed.
1104 *
1105 * The caller must hold @udev's device lock.
1106 */
1107void usb_unbind_and_rebind_marked_interfaces(struct usb_device *udev)
1108{
1109 unbind_marked_interfaces(udev);
1110 rebind_marked_interfaces(udev);
1111}
1112
Alan Stern9ff78432008-08-07 13:04:51 -04001113#ifdef CONFIG_PM
1114
Oliver Neukum14931382012-01-05 15:39:57 +01001115/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1116 * There is no check for reset_resume here because it can be determined
1117 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001118 *
1119 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001120 */
Oliver Neukum14931382012-01-05 15:39:57 +01001121static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001122{
1123 struct usb_host_config *config;
1124 int i;
1125 struct usb_interface *intf;
1126 struct usb_driver *drv;
1127
1128 config = udev->actconfig;
1129 if (config) {
1130 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1131 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001132
1133 if (intf->dev.driver) {
1134 drv = to_usb_driver(intf->dev.driver);
1135 if (!drv->suspend || !drv->resume)
1136 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001137 }
1138 }
1139 }
1140}
1141
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001142static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001143{
Alan Stern782da722006-07-01 22:09:35 -04001144 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001145 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001146
Alan Stern114b3682006-07-01 22:13:04 -04001147 if (udev->state == USB_STATE_NOTATTACHED ||
1148 udev->state == USB_STATE_SUSPENDED)
1149 goto done;
1150
Alan Sternb6f64362007-05-04 11:51:54 -04001151 /* For devices that don't have a driver, we do a generic suspend. */
1152 if (udev->dev.driver)
1153 udriver = to_usb_device_driver(udev->dev.driver);
1154 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001155 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001156 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001157 }
Alan Stern2bf40862006-07-01 22:12:19 -04001158 status = udriver->suspend(udev, msg);
1159
Alan Stern20dfdad2007-05-22 11:50:17 -04001160 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001161 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001162 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001163}
Alan Stern36e56a32006-07-01 22:08:06 -04001164
Alan Stern65bfd292008-11-25 16:39:18 -05001165static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001166{
1167 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001168 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001169
Alan Stern0458d5b2007-05-04 11:52:20 -04001170 if (udev->state == USB_STATE_NOTATTACHED)
1171 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001172
Alan Stern1c5df7e2006-07-01 22:13:50 -04001173 /* Can't resume it if it doesn't have a driver. */
1174 if (udev->dev.driver == NULL) {
1175 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001176 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001177 }
1178
Alan Stern6d19c002010-02-12 12:21:11 +01001179 /* Non-root devices on a full/low-speed bus must wait for their
1180 * companion high-speed root hub, in case a handoff is needed.
1181 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001182 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001183 device_pm_wait_for_dev(&udev->dev,
1184 &udev->bus->hs_companion->root_hub->dev);
1185
Alan Stern6bc6cff2007-05-04 11:53:03 -04001186 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1187 udev->reset_resume = 1;
1188
Alan Stern1cc8a252006-07-01 22:10:15 -04001189 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001190 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001191
Alan Stern20dfdad2007-05-22 11:50:17 -04001192 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001193 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001194 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001195}
1196
Alan Stern65605ae2008-08-12 14:33:27 -04001197static int usb_suspend_interface(struct usb_device *udev,
1198 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001199{
1200 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001201 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001202
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001203 if (udev->state == USB_STATE_NOTATTACHED ||
1204 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001205 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001206 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001207
Oliver Neukume78832c2012-01-02 15:11:48 +01001208 /* at this time we know the driver supports suspend */
1209 status = driver->suspend(intf, msg);
1210 if (status && !PMSG_IS_AUTO(msg))
1211 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001212
Alan Stern20dfdad2007-05-22 11:50:17 -04001213 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001214 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001215 return status;
1216}
1217
Alan Stern65605ae2008-08-12 14:33:27 -04001218static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001219 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001220{
Alan Stern1cc8a252006-07-01 22:10:15 -04001221 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001222 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001223
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001224 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001225 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001226
Alan Stern645daaa2006-08-30 15:47:02 -04001227 /* Don't let autoresume interfere with unbinding */
1228 if (intf->condition == USB_INTERFACE_UNBINDING)
1229 goto done;
1230
Alan Stern1c5df7e2006-07-01 22:13:50 -04001231 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001232 if (intf->condition == USB_INTERFACE_UNBOUND) {
1233
1234 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001235 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001236 usb_set_interface(udev, intf->altsetting[0].
1237 desc.bInterfaceNumber, 0);
1238 intf->needs_altsetting0 = 0;
1239 }
Alan Stern2bf40862006-07-01 22:12:19 -04001240 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001241 }
Alan Stern78d9a482008-06-23 16:00:40 -04001242
1243 /* Don't resume if the interface is marked for rebinding */
1244 if (intf->needs_binding)
1245 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001246 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001247
Alan Sternf07600c2007-05-30 15:38:16 -04001248 if (reset_resume) {
1249 if (driver->reset_resume) {
1250 status = driver->reset_resume(intf);
1251 if (status)
1252 dev_err(&intf->dev, "%s error %d\n",
1253 "reset_resume", status);
1254 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001255 intf->needs_binding = 1;
Alan Stern0a56b4f2013-10-18 11:17:21 -04001256 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1257 driver->name);
Alan Sternf07600c2007-05-30 15:38:16 -04001258 }
1259 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001260 status = driver->resume(intf);
1261 if (status)
1262 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001263 }
Alan Stern2bf40862006-07-01 22:12:19 -04001264
1265done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001266 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001267
Alan Stern78d9a482008-06-23 16:00:40 -04001268 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001269 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001270}
1271
Alan Stern645daaa2006-08-30 15:47:02 -04001272/**
1273 * usb_suspend_both - suspend a USB device and its interfaces
1274 * @udev: the usb_device to suspend
1275 * @msg: Power Management message describing this state transition
1276 *
1277 * This is the central routine for suspending USB devices. It calls the
1278 * suspend methods for all the interface drivers in @udev and then calls
Ming Lei303f0842013-03-15 12:08:53 +08001279 * the suspend method for @udev itself. When the routine is called in
1280 * autosuspend, if an error occurs at any stage, all the interfaces
1281 * which were suspended are resumed so that they remain in the same
1282 * state as the device, but when called from system sleep, all error
1283 * from suspend methods of interfaces and the non-root-hub device itself
1284 * are simply ignored, so all suspended interfaces are only resumed
1285 * to the device's state when @udev is root-hub and its suspend method
1286 * returns failure.
Alan Stern645daaa2006-08-30 15:47:02 -04001287 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001288 * Autosuspend requests originating from a child device or an interface
1289 * driver may be made without the protection of @udev's device lock, but
1290 * all other suspend calls will hold the lock. Usbcore will insure that
1291 * method calls do not arrive during bind, unbind, or reset operations.
1292 * However drivers must be prepared to handle suspend calls arriving at
1293 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001294 *
1295 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001296 *
1297 * Return: 0 if the suspend succeeded.
Alan Stern645daaa2006-08-30 15:47:02 -04001298 */
Alan Stern718efa62007-03-09 15:41:13 -05001299static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001300{
1301 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001302 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001303 struct usb_interface *intf;
1304
Alan Stern19410442007-03-27 13:33:59 -04001305 if (udev->state == USB_STATE_NOTATTACHED ||
1306 udev->state == USB_STATE_SUSPENDED)
1307 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001308
Alan Stern645daaa2006-08-30 15:47:02 -04001309 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001310 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001311 n = udev->actconfig->desc.bNumInterfaces;
1312 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001313 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001314 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001315
1316 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001317 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001318 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001319 if (status != 0)
1320 break;
1321 }
1322 }
Alan Stern0af212b2011-06-15 16:27:43 -04001323 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001324 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001325
Alan Sterncd4376e2012-03-28 15:56:17 -04001326 /*
1327 * Ignore errors from non-root-hub devices during
1328 * system sleep transitions. For the most part,
1329 * these devices should go to low power anyway when
1330 * the entire bus is suspended.
1331 */
1332 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001333 status = 0;
1334 }
1335
Alan Sterna8e7c562006-07-01 22:11:02 -04001336 /* If the suspend failed, resume interfaces that did get suspended */
1337 if (status != 0) {
Chen Gang505bdbc2013-04-01 13:04:08 +08001338 if (udev->actconfig) {
1339 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1340 while (++i < n) {
1341 intf = udev->actconfig->interface[i];
1342 usb_resume_interface(udev, intf, msg, 0);
1343 }
Alan Sterna8e7c562006-07-01 22:11:02 -04001344 }
Alan Stern645daaa2006-08-30 15:47:02 -04001345
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001346 /* If the suspend succeeded then prevent any more URB submissions
1347 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001348 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001349 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001350 udev->can_submit = 0;
1351 for (i = 0; i < 16; ++i) {
1352 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1353 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1354 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001355 }
Alan Stern645daaa2006-08-30 15:47:02 -04001356
Alan Stern19410442007-03-27 13:33:59 -04001357 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001358 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001359 return status;
1360}
1361
Alan Stern645daaa2006-08-30 15:47:02 -04001362/**
1363 * usb_resume_both - resume a USB device and its interfaces
1364 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001365 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001366 *
1367 * This is the central routine for resuming USB devices. It calls the
1368 * the resume method for @udev and then calls the resume methods for all
1369 * the interface drivers in @udev.
1370 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001371 * Autoresume requests originating from a child device or an interface
1372 * driver may be made without the protection of @udev's device lock, but
1373 * all other resume calls will hold the lock. Usbcore will insure that
1374 * method calls do not arrive during bind, unbind, or reset operations.
1375 * However drivers must be prepared to handle resume calls arriving at
1376 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001377 *
1378 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001379 *
1380 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001381 */
Alan Stern65bfd292008-11-25 16:39:18 -05001382static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001383{
Alan Stern645daaa2006-08-30 15:47:02 -04001384 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001385 int i;
1386 struct usb_interface *intf;
1387
Alan Stern19410442007-03-27 13:33:59 -04001388 if (udev->state == USB_STATE_NOTATTACHED) {
1389 status = -ENODEV;
1390 goto done;
1391 }
Alan Stern6840d252007-09-10 11:34:26 -04001392 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001393
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001394 /* Resume the device */
1395 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001396 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001397
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001398 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001399 if (status == 0 && udev->actconfig) {
1400 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1401 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001402 usb_resume_interface(udev, intf, msg,
1403 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001404 }
1405 }
Alan Sternc08512c2010-11-15 15:57:58 -05001406 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001407
Alan Stern19410442007-03-27 13:33:59 -04001408 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001409 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001410 if (!status)
1411 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001412 return status;
1413}
1414
Alan Stern5f677f12010-04-02 13:20:11 -04001415static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1416{
Alan Stern48826622010-06-22 16:14:48 -04001417 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001418
1419 /* Remote wakeup is needed only when we actually go to sleep.
1420 * For things like FREEZE and QUIESCE, if the device is already
1421 * autosuspended then its current wakeup setting is okay.
1422 */
1423 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1424 if (udev->state != USB_STATE_SUSPENDED)
1425 udev->do_remote_wakeup = 0;
1426 return;
1427 }
1428
Alan Stern48826622010-06-22 16:14:48 -04001429 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001430 * actually want it.
1431 */
Alan Stern48826622010-06-22 16:14:48 -04001432 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001433
1434 /* If the device is autosuspended with the wrong wakeup setting,
1435 * autoresume now so the setting can be changed.
1436 */
1437 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1438 pm_runtime_resume(&udev->dev);
1439 udev->do_remote_wakeup = w;
1440}
1441
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001442/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001443int usb_suspend(struct device *dev, pm_message_t msg)
1444{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001445 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001446
Oliver Neukum14931382012-01-05 15:39:57 +01001447 unbind_no_pm_drivers_interfaces(udev);
1448
1449 /* From now on we are sure all drivers support suspend/resume
1450 * but not necessarily reset_resume()
1451 * so we may still need to unbind and rebind upon resume
1452 */
Alan Stern5f677f12010-04-02 13:20:11 -04001453 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001454 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001455}
1456
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001457/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001458int usb_resume_complete(struct device *dev)
1459{
1460 struct usb_device *udev = to_usb_device(dev);
1461
1462 /* For PM complete calls, all we do is rebind interfaces
1463 * whose needs_binding flag is set
1464 */
1465 if (udev->state != USB_STATE_NOTATTACHED)
Alan Stern6aec0442014-03-12 11:30:38 -04001466 rebind_marked_interfaces(udev);
Oliver Neukum98d9a822012-01-11 08:38:35 +01001467 return 0;
1468}
1469
1470/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001471int usb_resume(struct device *dev, pm_message_t msg)
1472{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001473 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001474 int status;
1475
Oliver Neukum98d9a822012-01-11 08:38:35 +01001476 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001477 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001478 * Unbind the interfaces that will need rebinding later,
1479 * because they fail to support reset_resume.
1480 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001481 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001482 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001483 status = usb_resume_both(udev, msg);
1484 if (status == 0) {
1485 pm_runtime_disable(dev);
1486 pm_runtime_set_active(dev);
1487 pm_runtime_enable(dev);
Alan Stern6aec0442014-03-12 11:30:38 -04001488 unbind_marked_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001489 }
Alan Stern0c590e22010-01-08 12:57:14 -05001490
1491 /* Avoid PM error messages for devices disconnected while suspended
1492 * as we'll display regular disconnect messages just a bit later.
1493 */
Peter Chen7491f132010-09-27 16:43:25 +08001494 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001495 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001496 return status;
1497}
1498
Alan Stern088f7fe2010-01-08 12:56:54 -05001499/**
1500 * usb_enable_autosuspend - allow a USB device to be autosuspended
1501 * @udev: the USB device which may be autosuspended
1502 *
1503 * This routine allows @udev to be autosuspended. An autosuspend won't
1504 * take place until the autosuspend_delay has elapsed and all the other
1505 * necessary conditions are satisfied.
1506 *
1507 * The caller must hold @udev's device lock.
1508 */
Alan Stern9e18c822010-04-02 13:22:09 -04001509void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001510{
Alan Stern9e18c822010-04-02 13:22:09 -04001511 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001512}
1513EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1514
1515/**
1516 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1517 * @udev: the USB device which may not be autosuspended
1518 *
1519 * This routine prevents @udev from being autosuspended and wakes it up
1520 * if it is already autosuspended.
1521 *
1522 * The caller must hold @udev's device lock.
1523 */
Alan Stern9e18c822010-04-02 13:22:09 -04001524void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001525{
Alan Stern9e18c822010-04-02 13:22:09 -04001526 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001527}
1528EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1529
Alan Stern645daaa2006-08-30 15:47:02 -04001530/**
1531 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001532 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001533 *
1534 * This routine should be called when a core subsystem is finished using
1535 * @udev and wants to allow it to autosuspend. Examples would be when
1536 * @udev's device file in usbfs is closed or after a configuration change.
1537 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001538 * @udev's usage counter is decremented; if it drops to 0 and all the
1539 * interfaces are inactive then a delayed autosuspend will be attempted.
1540 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001541 *
Alan Stern62e299e2010-01-08 12:56:19 -05001542 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001543 *
1544 * This routine can run only in process context.
1545 */
Alan Stern94fcda12006-11-20 11:38:46 -05001546void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001547{
Alan Stern94fcda12006-11-20 11:38:46 -05001548 int status;
1549
Ming Lei6ddf27c2010-11-15 15:57:30 -05001550 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001551 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001552 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1553 __func__, atomic_read(&udev->dev.power.usage_count),
1554 status);
Alan Stern19c26232007-02-20 15:03:32 -05001555}
1556
1557/**
Alan Stern645daaa2006-08-30 15:47:02 -04001558 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001559 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001560 *
1561 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001562 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001563 * occur until usb_autosuspend_device() is called. (Note that this will
1564 * not prevent suspend events originating in the PM core.) Examples would
1565 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001566 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001567 *
Alan Stern94fcda12006-11-20 11:38:46 -05001568 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1569 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001570 *
Alan Stern62e299e2010-01-08 12:56:19 -05001571 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001572 *
1573 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001574 *
1575 * Return: 0 on success. A negative error code otherwise.
Alan Stern645daaa2006-08-30 15:47:02 -04001576 */
Alan Stern94fcda12006-11-20 11:38:46 -05001577int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001578{
1579 int status;
1580
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001581 status = pm_runtime_get_sync(&udev->dev);
1582 if (status < 0)
1583 pm_runtime_put_sync(&udev->dev);
1584 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1585 __func__, atomic_read(&udev->dev.power.usage_count),
1586 status);
1587 if (status > 0)
1588 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001589 return status;
1590}
1591
Alan Stern645daaa2006-08-30 15:47:02 -04001592/**
1593 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001594 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001595 *
1596 * This routine should be called by an interface driver when it is
1597 * finished using @intf and wants to allow it to autosuspend. A typical
1598 * example would be a character-device driver when its device file is
1599 * closed.
1600 *
1601 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001602 * 0, a delayed autosuspend request for @intf's device is attempted. The
1603 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001604 *
Alan Stern645daaa2006-08-30 15:47:02 -04001605 * This routine can run only in process context.
1606 */
1607void usb_autopm_put_interface(struct usb_interface *intf)
1608{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001609 struct usb_device *udev = interface_to_usbdev(intf);
1610 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001611
Ming Lei6ddf27c2010-11-15 15:57:30 -05001612 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001613 atomic_dec(&intf->pm_usage_cnt);
1614 status = pm_runtime_put_sync(&intf->dev);
1615 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1616 __func__, atomic_read(&intf->dev.power.usage_count),
1617 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001618}
1619EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1620
1621/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001622 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1623 * @intf: the usb_interface whose counter should be decremented
1624 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001625 * This routine does much the same thing as usb_autopm_put_interface():
1626 * It decrements @intf's usage counter and schedules a delayed
1627 * autosuspend request if the counter is <= 0. The difference is that it
1628 * does not perform any synchronization; callers should hold a private
1629 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001630 *
1631 * Typically a driver would call this routine during an URB's completion
1632 * handler, if no more URBs were pending.
1633 *
1634 * This routine can run in atomic context.
1635 */
1636void usb_autopm_put_interface_async(struct usb_interface *intf)
1637{
1638 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001639 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001640
Ming Lei6ddf27c2010-11-15 15:57:30 -05001641 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001642 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001643 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001644 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1645 __func__, atomic_read(&intf->dev.power.usage_count),
1646 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001647}
1648EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1649
1650/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001651 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1652 * @intf: the usb_interface whose counter should be decremented
1653 *
1654 * This routine decrements @intf's usage counter but does not carry out an
1655 * autosuspend.
1656 *
1657 * This routine can run in atomic context.
1658 */
1659void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1660{
1661 struct usb_device *udev = interface_to_usbdev(intf);
1662
Ming Lei6ddf27c2010-11-15 15:57:30 -05001663 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001664 atomic_dec(&intf->pm_usage_cnt);
1665 pm_runtime_put_noidle(&intf->dev);
1666}
1667EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1668
1669/**
Alan Stern645daaa2006-08-30 15:47:02 -04001670 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001671 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001672 *
1673 * This routine should be called by an interface driver when it wants to
1674 * use @intf and needs to guarantee that it is not suspended. In addition,
1675 * the routine prevents @intf from being autosuspended subsequently. (Note
1676 * that this will not prevent suspend events originating in the PM core.)
1677 * This prevention will persist until usb_autopm_put_interface() is called
1678 * or @intf is unbound. A typical example would be a character-device
1679 * driver when its device file is opened.
1680 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001681 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1682 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001683 *
1684 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001685 *
1686 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001687 */
1688int usb_autopm_get_interface(struct usb_interface *intf)
1689{
Alan Sternaf4f7602006-10-30 17:06:45 -05001690 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001691
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001692 status = pm_runtime_get_sync(&intf->dev);
1693 if (status < 0)
1694 pm_runtime_put_sync(&intf->dev);
1695 else
1696 atomic_inc(&intf->pm_usage_cnt);
1697 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1698 __func__, atomic_read(&intf->dev.power.usage_count),
1699 status);
1700 if (status > 0)
1701 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001702 return status;
1703}
1704EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1705
Alan Stern692a1862006-10-30 17:07:51 -05001706/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001707 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1708 * @intf: the usb_interface whose counter should be incremented
1709 *
1710 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001711 * usb_autopm_get_interface(): It increments @intf's usage counter and
1712 * queues an autoresume request if the device is suspended. The
1713 * differences are that it does not perform any synchronization (callers
1714 * should hold a private lock and handle all synchronization issues
1715 * themselves), and it does not autoresume the device directly (it only
1716 * queues a request). After a successful call, the device may not yet be
1717 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001718 *
1719 * This routine can run in atomic context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001720 *
1721 * Return: 0 on success. A negative error code otherwise.
Alan Stern9ac39f22008-11-12 16:19:49 -05001722 */
1723int usb_autopm_get_interface_async(struct usb_interface *intf)
1724{
Ming Lei63defa72010-11-15 15:56:54 -05001725 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001726
Ming Lei63defa72010-11-15 15:56:54 -05001727 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001728 if (status < 0 && status != -EINPROGRESS)
1729 pm_runtime_put_noidle(&intf->dev);
1730 else
Alan Sternccf5b802009-06-29 11:00:01 -04001731 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001732 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1733 __func__, atomic_read(&intf->dev.power.usage_count),
1734 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001735 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001736 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001737 return status;
1738}
1739EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1740
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001741/**
1742 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1743 * @intf: the usb_interface whose counter should be incremented
1744 *
1745 * This routine increments @intf's usage counter but does not carry out an
1746 * autoresume.
1747 *
1748 * This routine can run in atomic context.
1749 */
1750void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1751{
1752 struct usb_device *udev = interface_to_usbdev(intf);
1753
Ming Lei6ddf27c2010-11-15 15:57:30 -05001754 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001755 atomic_inc(&intf->pm_usage_cnt);
1756 pm_runtime_get_noresume(&intf->dev);
1757}
1758EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1759
1760/* Internal routine to check whether we may autosuspend a device. */
1761static int autosuspend_check(struct usb_device *udev)
1762{
Alan Stern7560d32e2010-04-02 13:18:50 -04001763 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001764 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001765
1766 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1767 * any interface drivers require remote wakeup but it isn't available.
1768 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001769 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001770 if (udev->actconfig) {
1771 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1772 intf = udev->actconfig->interface[i];
1773
1774 /* We don't need to check interfaces that are
1775 * disabled for runtime PM. Either they are unbound
1776 * or else their drivers don't support autosuspend
1777 * and so they are permanently active.
1778 */
1779 if (intf->dev.power.disable_depth)
1780 continue;
1781 if (atomic_read(&intf->dev.power.usage_count) > 0)
1782 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001783 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001784
1785 /* Don't allow autosuspend if the device will need
1786 * a reset-resume and any of its interface drivers
1787 * doesn't include support or needs remote wakeup.
1788 */
1789 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1790 struct usb_driver *driver;
1791
1792 driver = to_usb_driver(intf->dev.driver);
1793 if (!driver->reset_resume ||
1794 intf->needs_remote_wakeup)
1795 return -EOPNOTSUPP;
1796 }
1797 }
1798 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001799 if (w && !device_can_wakeup(&udev->dev)) {
1800 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1801 return -EOPNOTSUPP;
1802 }
Alan Stern074f9dd2015-01-29 15:05:04 -05001803
1804 /*
1805 * If the device is a direct child of the root hub and the HCD
1806 * doesn't handle wakeup requests, don't allow autosuspend when
1807 * wakeup is needed.
1808 */
1809 if (w && udev->parent == udev->bus->root_hub &&
1810 bus_to_hcd(udev->bus)->cant_recv_wakeups) {
1811 dev_dbg(&udev->dev, "HCD doesn't handle wakeup requests\n");
1812 return -EOPNOTSUPP;
1813 }
1814
Alan Stern7560d32e2010-04-02 13:18:50 -04001815 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001816 return 0;
1817}
1818
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001819int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001820{
Ming Lei63defa72010-11-15 15:56:54 -05001821 struct usb_device *udev = to_usb_device(dev);
1822 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001823
1824 /* A USB device can be suspended if it passes the various autosuspend
1825 * checks. Runtime suspend for a USB device means suspending all the
1826 * interfaces and then the device itself.
1827 */
Ming Lei63defa72010-11-15 15:56:54 -05001828 if (autosuspend_check(udev) != 0)
1829 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001830
Ming Lei63defa72010-11-15 15:56:54 -05001831 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001832
1833 /* Allow a retry if autosuspend failed temporarily */
1834 if (status == -EAGAIN || status == -EBUSY)
1835 usb_mark_last_busy(udev);
1836
Alan Stern8ef42dd2014-05-23 10:45:54 -04001837 /*
1838 * The PM core reacts badly unless the return code is 0,
1839 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
1840 * (except for root hubs, because they don't suspend through
1841 * an upstream port like other USB devices).
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001842 */
Alan Stern8ef42dd2014-05-23 10:45:54 -04001843 if (status != 0 && udev->parent)
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001844 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001845 return status;
1846}
1847
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001848int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001849{
Ming Lei63defa72010-11-15 15:56:54 -05001850 struct usb_device *udev = to_usb_device(dev);
1851 int status;
1852
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001853 /* Runtime resume for a USB device means resuming both the device
1854 * and all its interfaces.
1855 */
Ming Lei63defa72010-11-15 15:56:54 -05001856 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001857 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001858}
1859
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001860int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001861{
Ming Lei63defa72010-11-15 15:56:54 -05001862 struct usb_device *udev = to_usb_device(dev);
1863
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001864 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001865 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001866 */
Ming Lei63defa72010-11-15 15:56:54 -05001867 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001868 pm_runtime_autosuspend(dev);
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001869 /* Tell the core not to suspend it, though. */
1870 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001871}
1872
Andiry Xu65580b432011-09-23 14:19:52 -07001873int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1874{
1875 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1876 int ret = -EPERM;
1877
Sarah Sharpde68bab2013-09-30 17:26:28 +03001878 if (enable && !udev->usb2_hw_lpm_allowed)
1879 return 0;
1880
Andiry Xu65580b432011-09-23 14:19:52 -07001881 if (hcd->driver->set_usb2_hw_lpm) {
1882 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1883 if (!ret)
1884 udev->usb2_hw_lpm_enabled = enable;
1885 }
1886
1887 return ret;
1888}
1889
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01001890#endif /* CONFIG_PM */
Alan Stern645daaa2006-08-30 15:47:02 -04001891
Alan Stern36e56a32006-07-01 22:08:06 -04001892struct bus_type usb_bus_type = {
1893 .name = "usb",
1894 .match = usb_device_match,
1895 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001896};