blob: c76ec9758ce301d0b72925aa3cc92b1d7e377225 [file] [log] [blame]
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -08001/*
2 * drivers/usb/driver.c - most of the driver model stuff for usb
3 *
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
5 *
6 * based on drivers/usb/usb.c which had the following copyrights:
7 * (C) Copyright Linus Torvalds 1999
8 * (C) Copyright Johannes Erdfelt 1999-2001
9 * (C) Copyright Andreas Gal 1999
10 * (C) Copyright Gregory P. Smith 1999
11 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
12 * (C) Copyright Randy Dunlap 2000
13 * (C) Copyright David Brownell 2000-2004
14 * (C) Copyright Yggdrasil Computing, Inc. 2000
15 * (usb_device_id matching changes by Adam J. Richter)
16 * (C) Copyright Greg Kroah-Hartman 2002-2003
17 *
18 * NOTE! This is not actually a driver at all, rather this is
19 * just a collection of helper routines that implement the
Alan Stern36e56a32006-07-01 22:08:06 -040020 * matching, probing, releasing, suspending and resuming for
21 * real drivers.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080022 *
23 */
24
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080025#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040027#include <linux/export.h>
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080028#include <linux/usb.h>
Alan Stern6bc6cff2007-05-04 11:53:03 -040029#include <linux/usb/quirks.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020030#include <linux/usb/hcd.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020031
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080032#include "usb.h"
33
Alan Stern20dfdad2007-05-22 11:50:17 -040034
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080035/*
36 * Adds a new dynamic USBdevice ID to this driver,
37 * and cause the driver to probe for all devices again.
38 */
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010039ssize_t usb_store_new_id(struct usb_dynids *dynids,
Wolfram Sang2fc82c22014-01-10 19:36:42 +010040 const struct usb_device_id *id_table,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010041 struct device_driver *driver,
42 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080043{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080044 struct usb_dynid *dynid;
45 u32 idVendor = 0;
46 u32 idProduct = 0;
Josua Dietzeff231db2011-10-23 14:22:29 +020047 unsigned int bInterfaceClass = 0;
Wolfram Sang2fc82c22014-01-10 19:36:42 +010048 u32 refVendor, refProduct;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080049 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070050 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080051
Wolfram Sang2fc82c22014-01-10 19:36:42 +010052 fields = sscanf(buf, "%x %x %x %x %x", &idVendor, &idProduct,
53 &bInterfaceClass, &refVendor, &refProduct);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080054 if (fields < 2)
55 return -EINVAL;
56
57 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
58 if (!dynid)
59 return -ENOMEM;
60
61 INIT_LIST_HEAD(&dynid->node);
62 dynid->id.idVendor = idVendor;
63 dynid->id.idProduct = idProduct;
64 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Wolfram Sangc63fe8f2014-01-10 19:36:41 +010065 if (fields > 2 && bInterfaceClass) {
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010066 if (bInterfaceClass > 255) {
67 retval = -EINVAL;
68 goto fail;
69 }
Wolfram Sangc63fe8f2014-01-10 19:36:41 +010070
Josua Dietzeff231db2011-10-23 14:22:29 +020071 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
72 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
73 }
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080074
Wolfram Sang2fc82c22014-01-10 19:36:42 +010075 if (fields > 4) {
76 const struct usb_device_id *id = id_table;
77
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010078 if (!id) {
79 retval = -ENODEV;
80 goto fail;
81 }
Wolfram Sang1b9fb312014-01-13 11:29:23 +010082
Wolfram Sang2fc82c22014-01-10 19:36:42 +010083 for (; id->match_flags; id++)
Wolfram Sang52a6966c2014-01-12 10:07:50 +010084 if (id->idVendor == refVendor && id->idProduct == refProduct)
Wolfram Sang2fc82c22014-01-10 19:36:42 +010085 break;
Wolfram Sang52a6966c2014-01-12 10:07:50 +010086
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010087 if (id->match_flags) {
Wolfram Sang52a6966c2014-01-12 10:07:50 +010088 dynid->id.driver_info = id->driver_info;
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010089 } else {
90 retval = -ENODEV;
91 goto fail;
92 }
Wolfram Sang2fc82c22014-01-10 19:36:42 +010093 }
94
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010095 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020096 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010097 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080098
Alan Sterncef9bc52012-01-24 13:34:41 -050099 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800100
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700101 if (retval)
102 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800103 return count;
Christian Engelmayer7f196ca2014-01-28 22:22:27 +0100104
105fail:
106 kfree(dynid);
107 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800108}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100109EXPORT_SYMBOL_GPL(usb_store_new_id);
110
Bjørn Morkef206f32012-05-13 12:35:00 +0200111ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200112{
113 struct usb_dynid *dynid;
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200114 size_t count = 0;
115
Bjørn Morkef206f32012-05-13 12:35:00 +0200116 list_for_each_entry(dynid, &dynids->list, node)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200117 if (dynid->id.bInterfaceClass != 0)
118 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
119 dynid->id.idVendor, dynid->id.idProduct,
120 dynid->id.bInterfaceClass);
121 else
122 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
123 dynid->id.idVendor, dynid->id.idProduct);
124 return count;
125}
Bjørn Morkef206f32012-05-13 12:35:00 +0200126EXPORT_SYMBOL_GPL(usb_show_dynids);
127
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700128static ssize_t new_id_show(struct device_driver *driver, char *buf)
Bjørn Morkef206f32012-05-13 12:35:00 +0200129{
130 struct usb_driver *usb_drv = to_usb_driver(driver);
131
132 return usb_show_dynids(&usb_drv->dynids, buf);
133}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200134
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700135static ssize_t new_id_store(struct device_driver *driver,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100136 const char *buf, size_t count)
137{
138 struct usb_driver *usb_drv = to_usb_driver(driver);
139
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100140 return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100141}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700142static DRIVER_ATTR_RW(new_id);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800143
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700144/*
145 * Remove a USB device ID from this driver
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800146 */
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700147static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
148 size_t count)
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800149{
150 struct usb_dynid *dynid, *n;
151 struct usb_driver *usb_driver = to_usb_driver(driver);
Alan Coxac08de32012-09-17 11:55:23 +0100152 u32 idVendor;
153 u32 idProduct;
154 int fields;
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800155
156 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
157 if (fields < 2)
158 return -EINVAL;
159
160 spin_lock(&usb_driver->dynids.lock);
161 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
162 struct usb_device_id *id = &dynid->id;
163 if ((id->idVendor == idVendor) &&
164 (id->idProduct == idProduct)) {
165 list_del(&dynid->node);
166 kfree(dynid);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800167 break;
168 }
169 }
170 spin_unlock(&usb_driver->dynids.lock);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800171 return count;
172}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700173
174static ssize_t remove_id_show(struct device_driver *driver, char *buf)
175{
176 return new_id_show(driver, buf);
177}
178static DRIVER_ATTR_RW(remove_id);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800179
Alan Sterned283e92012-01-24 14:35:13 -0500180static int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800181{
182 int error = 0;
183
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800184 if (usb_drv->no_dynamic_id)
185 goto exit;
186
Alan Sterned283e92012-01-24 14:35:13 -0500187 if (usb_drv->probe != NULL) {
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800188 error = driver_create_file(&usb_drv->drvwrap.driver,
189 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500190 if (error == 0) {
191 error = driver_create_file(&usb_drv->drvwrap.driver,
192 &driver_attr_remove_id);
193 if (error)
194 driver_remove_file(&usb_drv->drvwrap.driver,
195 &driver_attr_new_id);
196 }
197 }
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800198exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800199 return error;
200}
201
Alan Sterned283e92012-01-24 14:35:13 -0500202static void usb_remove_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800203{
204 if (usb_drv->no_dynamic_id)
205 return;
206
Alan Sterned283e92012-01-24 14:35:13 -0500207 if (usb_drv->probe != NULL) {
208 driver_remove_file(&usb_drv->drvwrap.driver,
209 &driver_attr_remove_id);
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800210 driver_remove_file(&usb_drv->drvwrap.driver,
211 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500212 }
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800213}
214
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800215static void usb_free_dynids(struct usb_driver *usb_drv)
216{
217 struct usb_dynid *dynid, *n;
218
219 spin_lock(&usb_drv->dynids.lock);
220 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
221 list_del(&dynid->node);
222 kfree(dynid);
223 }
224 spin_unlock(&usb_drv->dynids.lock);
225}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800226
227static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
228 struct usb_driver *drv)
229{
230 struct usb_dynid *dynid;
231
232 spin_lock(&drv->dynids.lock);
233 list_for_each_entry(dynid, &drv->dynids.list, node) {
234 if (usb_match_one_id(intf, &dynid->id)) {
235 spin_unlock(&drv->dynids.lock);
236 return &dynid->id;
237 }
238 }
239 spin_unlock(&drv->dynids.lock);
240 return NULL;
241}
242
243
Alan Stern8bb54ab2006-07-01 22:08:49 -0400244/* called from driver core with dev locked */
245static int usb_probe_device(struct device *dev)
246{
247 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200248 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500249 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400250
Harvey Harrison441b62c2008-03-03 16:08:34 -0800251 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400252
Alan Stern8bb54ab2006-07-01 22:08:49 -0400253 /* TODO: Add real matching code */
254
Alan Stern645daaa2006-08-30 15:47:02 -0400255 /* The device should always appear to be in use
Masanari Iida02582e92012-08-22 19:11:26 +0900256 * unless the driver supports autosuspend.
Alan Stern645daaa2006-08-30 15:47:02 -0400257 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500258 if (!udriver->supports_autosuspend)
259 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400260
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500261 if (!error)
262 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400263 return error;
264}
265
266/* called from driver core with dev locked */
267static int usb_unbind_device(struct device *dev)
268{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500269 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400270 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
271
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500272 udriver->disconnect(udev);
273 if (!udriver->supports_autosuspend)
274 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400275 return 0;
276}
277
Alan Stern8bb54ab2006-07-01 22:08:49 -0400278/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800279static int usb_probe_interface(struct device *dev)
280{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400281 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200282 struct usb_interface *intf = to_usb_interface(dev);
283 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800284 const struct usb_device_id *id;
285 int error = -ENODEV;
Sarah Sharp83060952012-05-02 14:25:52 -0700286 int lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800287
Harvey Harrison441b62c2008-03-03 16:08:34 -0800288 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800289
Alan Stern78d9a482008-06-23 16:00:40 -0400290 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800291
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400292 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500293 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400294
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800295 if (udev->authorized == 0) {
296 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500297 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800298 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700299
Bjørn Mork31c6bf72014-01-11 02:04:00 +0100300 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800301 if (!id)
Bjørn Mork31c6bf72014-01-11 02:04:00 +0100302 id = usb_match_id(intf, driver->id_table);
Alan Stern0f3dda92010-01-08 12:56:04 -0500303 if (!id)
304 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800305
Alan Stern0f3dda92010-01-08 12:56:04 -0500306 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400307
Alan Stern0f3dda92010-01-08 12:56:04 -0500308 error = usb_autoresume_device(udev);
309 if (error)
310 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400311
Alan Stern0f3dda92010-01-08 12:56:04 -0500312 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400313
Alan Stern571dc792010-04-09 16:03:43 -0400314 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500315 * runtime-PM-enabled only if the driver has autosuspend support.
316 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500317 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500318 pm_runtime_set_active(dev);
319 pm_suspend_ignore_children(dev, false);
320 if (driver->supports_autosuspend)
321 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200322
Sarah Sharp83060952012-05-02 14:25:52 -0700323 /* If the new driver doesn't allow hub-initiated LPM, and we can't
324 * disable hub-initiated LPM, then fail the probe.
325 *
326 * Otherwise, leaving LPM enabled should be harmless, because the
327 * endpoint intervals should remain the same, and the U1/U2 timeouts
328 * should remain the same.
329 *
330 * If we need to install alt setting 0 before probe, or another alt
331 * setting during probe, that should also be fine. usb_set_interface()
332 * will attempt to disable LPM, and fail if it can't disable it.
333 */
334 lpm_disable_error = usb_unlocked_disable_lpm(udev);
335 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
336 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
337 __func__, driver->name);
338 error = lpm_disable_error;
339 goto err;
340 }
341
Alan Stern0f3dda92010-01-08 12:56:04 -0500342 /* Carry out a deferred switch to altsetting 0 */
343 if (intf->needs_altsetting0) {
344 error = usb_set_interface(udev, intf->altsetting[0].
345 desc.bInterfaceNumber, 0);
346 if (error < 0)
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200347 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500348 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800349 }
350
Alan Stern0f3dda92010-01-08 12:56:04 -0500351 error = driver->probe(intf, id);
352 if (error)
353 goto err;
354
355 intf->condition = USB_INTERFACE_BOUND;
Sarah Sharp83060952012-05-02 14:25:52 -0700356
357 /* If the LPM disable succeeded, balance the ref counts. */
358 if (!lpm_disable_error)
359 usb_unlocked_enable_lpm(udev);
360
Alan Stern0f3dda92010-01-08 12:56:04 -0500361 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800362 return error;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200363
Alan Stern0f3dda92010-01-08 12:56:04 -0500364 err:
Hans de Goedee714fad2012-05-22 11:36:59 +0200365 usb_set_intfdata(intf, NULL);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200366 intf->needs_remote_wakeup = 0;
367 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500368
Sarah Sharpd01f87c2012-10-04 09:53:43 -0700369 /* If the LPM disable succeeded, balance the ref counts. */
370 if (!lpm_disable_error)
371 usb_unlocked_enable_lpm(udev);
372
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500373 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400374 if (driver->supports_autosuspend)
375 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500376 pm_runtime_set_suspended(dev);
377
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200378 usb_autosuspend_device(udev);
379 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800380}
381
Alan Stern8bb54ab2006-07-01 22:08:49 -0400382/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800383static int usb_unbind_interface(struct device *dev)
384{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400385 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800386 struct usb_interface *intf = to_usb_interface(dev);
Hans de Goede6343e8b2013-10-09 17:19:26 +0200387 struct usb_host_endpoint *ep, **eps = NULL;
Alan Stern645daaa2006-08-30 15:47:02 -0400388 struct usb_device *udev;
Hans de Goede6343e8b2013-10-09 17:19:26 +0200389 int i, j, error, r, lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800390
391 intf->condition = USB_INTERFACE_UNBINDING;
392
Alan Stern645daaa2006-08-30 15:47:02 -0400393 /* Autoresume for set_interface call below */
394 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500395 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400396
Sarah Sharp83060952012-05-02 14:25:52 -0700397 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
398 * the driver is unbound. If LPM isn't disabled, that's fine because it
399 * wouldn't be enabled unless all the bound interfaces supported
400 * hub-initiated LPM.
401 */
402 lpm_disable_error = usb_unlocked_disable_lpm(udev);
403
Alan Stern1299cff2014-07-17 15:40:57 -0400404 /*
405 * Terminate all URBs for this interface unless the driver
406 * supports "soft" unbinding and the device is still present.
Alan Stern9da82bd2008-05-08 11:54:37 -0400407 */
Alan Stern1299cff2014-07-17 15:40:57 -0400408 if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
Alan Sternddeac4e72009-01-15 17:03:33 -0500409 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800410
Alan Stern8bb54ab2006-07-01 22:08:49 -0400411 driver->disconnect(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800412
Hans de Goede6343e8b2013-10-09 17:19:26 +0200413 /* Free streams */
414 for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
415 ep = &intf->cur_altsetting->endpoint[i];
416 if (ep->streams == 0)
417 continue;
418 if (j == 0) {
419 eps = kmalloc(USB_MAXENDPOINTS * sizeof(void *),
420 GFP_KERNEL);
421 if (!eps) {
422 dev_warn(dev, "oom, leaking streams\n");
423 break;
424 }
425 }
426 eps[j++] = ep;
427 }
428 if (j) {
429 usb_free_streams(intf, eps, j, GFP_KERNEL);
430 kfree(eps);
431 }
432
Alan Stern55151d72008-08-12 14:33:59 -0400433 /* Reset other interface state.
434 * We cannot do a Set-Interface if the device is suspended or
435 * if it is prepared for a system sleep (since installing a new
436 * altsetting means creating new endpoint device entries).
437 * When either of these happens, defer the Set-Interface.
438 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500439 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
440 /* Already in altsetting 0 so skip Set-Interface.
441 * Just re-enable it without affecting the endpoint toggles.
442 */
443 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200444 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200445 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400446 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200447 if (r < 0)
448 intf->needs_altsetting0 = 1;
449 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400450 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200451 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800452 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400453
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800454 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400455 intf->needs_remote_wakeup = 0;
456
Sarah Sharp83060952012-05-02 14:25:52 -0700457 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
458 if (!lpm_disable_error)
459 usb_unlocked_enable_lpm(udev);
460
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500461 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400462 if (driver->supports_autosuspend)
463 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500464 pm_runtime_set_suspended(dev);
465
466 /* Undo any residual pm_autopm_get_interface_* calls */
467 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
468 usb_autopm_put_interface_no_suspend(intf);
469 atomic_set(&intf->pm_usage_cnt, 0);
470
Alan Stern645daaa2006-08-30 15:47:02 -0400471 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500472 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800473
474 return 0;
475}
476
Alan Stern36e56a32006-07-01 22:08:06 -0400477/**
478 * usb_driver_claim_interface - bind a driver to an interface
479 * @driver: the driver to be bound
480 * @iface: the interface to which it will be bound; must be in the
481 * usb device's active configuration
482 * @priv: driver data associated with that interface
483 *
484 * This is used by usb device drivers that need to claim more than one
485 * interface on a device when probing (audio and acm are current examples).
486 * No device driver should directly modify internal usb_interface or
487 * usb_device structure members.
488 *
489 * Few drivers should need to use this routine, since the most natural
490 * way to bind to an interface is to return the private data from
491 * the driver's probe() method.
492 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400493 * Callers must own the device lock, so driver probe() entries don't need
494 * extra locking, but other call contexts may need to explicitly claim that
495 * lock.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200496 *
497 * Return: 0 on success.
Alan Stern36e56a32006-07-01 22:08:06 -0400498 */
499int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800500 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400501{
502 struct device *dev = &iface->dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700503 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700504 int retval = 0;
Sarah Sharp83060952012-05-02 14:25:52 -0700505 int lpm_disable_error;
Alan Stern36e56a32006-07-01 22:08:06 -0400506
507 if (dev->driver)
508 return -EBUSY;
509
Sarah Sharp83060952012-05-02 14:25:52 -0700510 udev = interface_to_usbdev(iface);
511
Alan Stern8bb54ab2006-07-01 22:08:49 -0400512 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400513 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400514 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400515
Alan Stern36e56a32006-07-01 22:08:06 -0400516 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500517
Sarah Sharp83060952012-05-02 14:25:52 -0700518 /* Disable LPM until this driver is bound. */
519 lpm_disable_error = usb_unlocked_disable_lpm(udev);
520 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
521 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
522 __func__, driver->name);
523 return -ENOMEM;
524 }
525
Alan Stern89842ae2010-05-11 11:44:06 -0400526 /* Claimed interfaces are initially inactive (suspended) and
527 * runtime-PM-enabled, but only if the driver has autosuspend
528 * support. Otherwise they are marked active, to prevent the
529 * device from being autosuspended, but left disabled. In either
530 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500531 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500532 pm_suspend_ignore_children(dev, false);
533 if (driver->supports_autosuspend)
534 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400535 else
536 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400537
538 /* if interface was already added, bind now; else let
539 * the future device_add() bind it, bypassing probe()
540 */
541 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700542 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400543
Sarah Sharp83060952012-05-02 14:25:52 -0700544 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
545 if (!lpm_disable_error)
546 usb_unlocked_enable_lpm(udev);
547
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700548 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400549}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600550EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400551
552/**
553 * usb_driver_release_interface - unbind a driver from an interface
554 * @driver: the driver to be unbound
555 * @iface: the interface from which it will be unbound
556 *
557 * This can be used by drivers to release an interface without waiting
558 * for their disconnect() methods to be called. In typical cases this
559 * also causes the driver disconnect() method to be called.
560 *
561 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400562 * Callers must own the device lock, so driver disconnect() entries don't
563 * need extra locking, but other call contexts may need to explicitly claim
564 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400565 */
566void usb_driver_release_interface(struct usb_driver *driver,
567 struct usb_interface *iface)
568{
569 struct device *dev = &iface->dev;
570
571 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400572 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400573 return;
574
575 /* don't release from within disconnect() */
576 if (iface->condition != USB_INTERFACE_BOUND)
577 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400578 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400579
Alan Stern91f8d062009-04-16 15:35:09 -0400580 /* Release via the driver core only if the interface
581 * has already been registered
582 */
Alan Stern36e56a32006-07-01 22:08:06 -0400583 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400584 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800585 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800586 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400587 usb_unbind_interface(dev);
588 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800589 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400590 }
Alan Stern36e56a32006-07-01 22:08:06 -0400591}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600592EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400593
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800594/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100595int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800596{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800597 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
598 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
599 return 0;
600
601 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
602 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
603 return 0;
604
605 /* No need to test id->bcdDevice_lo != 0, since 0 is never
606 greater than any unsigned number. */
607 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
608 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
609 return 0;
610
611 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
612 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
613 return 0;
614
615 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
616 (id->bDeviceClass != dev->descriptor.bDeviceClass))
617 return 0;
618
619 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800620 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800621 return 0;
622
623 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
624 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
625 return 0;
626
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100627 return 1;
628}
629
630/* returns 0 if no match, 1 if match */
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200631int usb_match_one_id_intf(struct usb_device *dev,
632 struct usb_host_interface *intf,
633 const struct usb_device_id *id)
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100634{
Bjørn Mork81df2d52012-05-18 21:27:43 +0200635 /* The interface class, subclass, protocol and number should never be
Alan Stern93c8bf42006-10-18 16:41:51 -0400636 * checked for a match if the device class is Vendor Specific,
637 * unless the match record specifies the Vendor ID. */
638 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
639 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
640 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
641 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
Bjørn Mork81df2d52012-05-18 21:27:43 +0200642 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
643 USB_DEVICE_ID_MATCH_INT_NUMBER)))
Alan Stern93c8bf42006-10-18 16:41:51 -0400644 return 0;
645
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800646 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
647 (id->bInterfaceClass != intf->desc.bInterfaceClass))
648 return 0;
649
650 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
651 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
652 return 0;
653
654 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
655 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
656 return 0;
657
Bjørn Mork81df2d52012-05-18 21:27:43 +0200658 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
659 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
660 return 0;
661
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800662 return 1;
663}
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200664
665/* returns 0 if no match, 1 if match */
666int usb_match_one_id(struct usb_interface *interface,
667 const struct usb_device_id *id)
668{
669 struct usb_host_interface *intf;
670 struct usb_device *dev;
671
672 /* proc_connectinfo in devio.c may call us with id == NULL. */
673 if (id == NULL)
674 return 0;
675
676 intf = interface->cur_altsetting;
677 dev = interface_to_usbdev(interface);
678
679 if (!usb_match_device(dev, id))
680 return 0;
681
682 return usb_match_one_id_intf(dev, intf, id);
683}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100684EXPORT_SYMBOL_GPL(usb_match_one_id);
685
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800686/**
687 * usb_match_id - find first usb_device_id matching device or interface
688 * @interface: the interface of interest
689 * @id: array of usb_device_id structures, terminated by zero entry
690 *
691 * usb_match_id searches an array of usb_device_id's and returns
692 * the first one matching the device or interface, or null.
693 * This is used when binding (or rebinding) a driver to an interface.
694 * Most USB device drivers will use this indirectly, through the usb core,
695 * but some layered driver frameworks use it directly.
696 * These device tables are exported with MODULE_DEVICE_TABLE, through
697 * modutils, to support the driver loading functionality of USB hotplugging.
698 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200699 * Return: The first matching usb_device_id, or %NULL.
700 *
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800701 * What Matches:
702 *
703 * The "match_flags" element in a usb_device_id controls which
704 * members are used. If the corresponding bit is set, the
705 * value in the device_id must match its corresponding member
706 * in the device or interface descriptor, or else the device_id
707 * does not match.
708 *
709 * "driver_info" is normally used only by device drivers,
710 * but you can create a wildcard "matches anything" usb_device_id
711 * as a driver's "modules.usbmap" entry if you provide an id with
712 * only a nonzero "driver_info" field. If you do this, the USB device
713 * driver's probe() routine should use additional intelligence to
714 * decide whether to bind to the specified interface.
715 *
716 * What Makes Good usb_device_id Tables:
717 *
718 * The match algorithm is very simple, so that intelligence in
719 * driver selection must come from smart driver id records.
720 * Unless you have good reasons to use another selection policy,
721 * provide match elements only in related groups, and order match
722 * specifiers from specific to general. Use the macros provided
723 * for that purpose if you can.
724 *
725 * The most specific match specifiers use device descriptor
726 * data. These are commonly used with product-specific matches;
727 * the USB_DEVICE macro lets you provide vendor and product IDs,
728 * and you can also match against ranges of product revisions.
729 * These are widely used for devices with application or vendor
730 * specific bDeviceClass values.
731 *
732 * Matches based on device class/subclass/protocol specifications
733 * are slightly more general; use the USB_DEVICE_INFO macro, or
734 * its siblings. These are used with single-function devices
735 * where bDeviceClass doesn't specify that each interface has
736 * its own class.
737 *
738 * Matches based on interface class/subclass/protocol are the
739 * most general; they let drivers bind to any interface on a
740 * multiple-function device. Use the USB_INTERFACE_INFO
741 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400742 * devices (as recorded in bInterfaceClass).
743 *
744 * Note that an entry created by USB_INTERFACE_INFO won't match
745 * any interface if the device class is set to Vendor-Specific.
746 * This is deliberate; according to the USB spec the meanings of
747 * the interface class/subclass/protocol for these devices are also
748 * vendor-specific, and hence matching against a standard product
749 * class wouldn't work anyway. If you really want to use an
750 * interface-based match for such a device, create a match record
751 * that also specifies the vendor ID. (Unforunately there isn't a
752 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800753 *
754 * Within those groups, remember that not all combinations are
755 * meaningful. For example, don't give a product version range
756 * without vendor and product IDs; or specify a protocol without
757 * its associated class and subclass.
758 */
759const struct usb_device_id *usb_match_id(struct usb_interface *interface,
760 const struct usb_device_id *id)
761{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800762 /* proc_connectinfo in devio.c may call us with id == NULL. */
763 if (id == NULL)
764 return NULL;
765
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800766 /* It is important to check that id->driver_info is nonzero,
767 since an entry that is all zeroes except for a nonzero
768 id->driver_info is the way to create an entry that
769 indicates that the driver want to examine every
770 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800771 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
772 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800773 if (usb_match_one_id(interface, id))
774 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800775 }
776
777 return NULL;
778}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600779EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800780
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100781static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800782{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400783 /* devices and interfaces are handled separately */
784 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800785
Alan Stern8bb54ab2006-07-01 22:08:49 -0400786 /* interface drivers never match devices */
787 if (!is_usb_device_driver(drv))
788 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800789
Alan Stern8bb54ab2006-07-01 22:08:49 -0400790 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800791 return 1;
792
Kay Sievers55129662009-05-04 19:48:32 +0200793 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400794 struct usb_interface *intf;
795 struct usb_driver *usb_drv;
796 const struct usb_device_id *id;
797
798 /* device drivers never match interfaces */
799 if (is_usb_device_driver(drv))
800 return 0;
801
802 intf = to_usb_interface(dev);
803 usb_drv = to_usb_driver(drv);
804
805 id = usb_match_id(intf, usb_drv->id_table);
806 if (id)
807 return 1;
808
809 id = usb_match_dynamic_id(intf, usb_drv);
810 if (id)
811 return 1;
812 }
813
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800814 return 0;
815}
816
Kay Sievers7eff2e72007-08-14 15:15:12 +0200817static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400818{
Alan Stern36e56a32006-07-01 22:08:06 -0400819 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400820
Kay Sievers55129662009-05-04 19:48:32 +0200821 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400822 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200823 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100824 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200825
Alan Stern8bb54ab2006-07-01 22:08:49 -0400826 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200827 } else {
828 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400829 }
Alan Stern36e56a32006-07-01 22:08:06 -0400830
831 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500832 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200833 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400834 return -ENODEV;
835 }
836 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200837 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400838 return -ENODEV;
839 }
840
Alan Stern36e56a32006-07-01 22:08:06 -0400841 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200842 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400843 le16_to_cpu(usb_dev->descriptor.idVendor),
844 le16_to_cpu(usb_dev->descriptor.idProduct),
845 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
846 return -ENOMEM;
847
848 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200849 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400850 usb_dev->descriptor.bDeviceClass,
851 usb_dev->descriptor.bDeviceSubClass,
852 usb_dev->descriptor.bDeviceProtocol))
853 return -ENOMEM;
854
Alan Stern36e56a32006-07-01 22:08:06 -0400855 return 0;
856}
857
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800858/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400859 * usb_register_device_driver - register a USB device (not interface) driver
860 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800861 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800862 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400863 * Registers a USB device driver with the USB core. The list of
864 * unattached devices will be rescanned whenever a new driver is
865 * added, allowing the new driver to attach to any recognized devices.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200866 *
867 * Return: A negative error code on failure and 0 on success.
Alan Stern8bb54ab2006-07-01 22:08:49 -0400868 */
869int usb_register_device_driver(struct usb_device_driver *new_udriver,
870 struct module *owner)
871{
872 int retval = 0;
873
874 if (usb_disabled())
875 return -ENODEV;
876
877 new_udriver->drvwrap.for_devices = 1;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100878 new_udriver->drvwrap.driver.name = new_udriver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400879 new_udriver->drvwrap.driver.bus = &usb_bus_type;
880 new_udriver->drvwrap.driver.probe = usb_probe_device;
881 new_udriver->drvwrap.driver.remove = usb_unbind_device;
882 new_udriver->drvwrap.driver.owner = owner;
883
884 retval = driver_register(&new_udriver->drvwrap.driver);
885
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700886 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400887 pr_info("%s: registered new device driver %s\n",
888 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700889 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400890 printk(KERN_ERR "%s: error %d registering device "
891 " driver %s\n",
892 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400893
894 return retval;
895}
896EXPORT_SYMBOL_GPL(usb_register_device_driver);
897
898/**
899 * usb_deregister_device_driver - unregister a USB device (not interface) driver
900 * @udriver: USB operations of the device driver to unregister
901 * Context: must be able to sleep
902 *
903 * Unlinks the specified driver from the internal USB driver list.
904 */
905void usb_deregister_device_driver(struct usb_device_driver *udriver)
906{
907 pr_info("%s: deregistering device driver %s\n",
908 usbcore_name, udriver->name);
909
910 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400911}
912EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
913
914/**
915 * usb_register_driver - register a USB interface driver
916 * @new_driver: USB operations for the interface driver
917 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800918 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400919 *
920 * Registers a USB interface driver with the USB core. The list of
921 * unattached interfaces will be rescanned whenever a new driver is
922 * added, allowing the new driver to attach to any recognized interfaces.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200923 *
924 * Return: A negative error code on failure and 0 on success.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800925 *
926 * NOTE: if you want your driver to use the USB major number, you must call
927 * usb_register_dev() to enable that functionality. This function no longer
928 * takes care of that.
929 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800930int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
931 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800932{
933 int retval = 0;
934
935 if (usb_disabled())
936 return -ENODEV;
937
Alan Stern8bb54ab2006-07-01 22:08:49 -0400938 new_driver->drvwrap.for_devices = 0;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100939 new_driver->drvwrap.driver.name = new_driver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400940 new_driver->drvwrap.driver.bus = &usb_bus_type;
941 new_driver->drvwrap.driver.probe = usb_probe_interface;
942 new_driver->drvwrap.driver.remove = usb_unbind_interface;
943 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800944 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800945 spin_lock_init(&new_driver->dynids.lock);
946 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800947
Alan Stern8bb54ab2006-07-01 22:08:49 -0400948 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800949 if (retval)
950 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800951
Alan Sterned283e92012-01-24 14:35:13 -0500952 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800953 if (retval)
954 goto out_newid;
955
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800956 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800957 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800958
959out:
960 return retval;
961
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800962out_newid:
963 driver_unregister(&new_driver->drvwrap.driver);
964
965 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400966 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800967 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800968 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800969}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600970EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800971
972/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400973 * usb_deregister - unregister a USB interface driver
974 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800975 * Context: must be able to sleep
976 *
977 * Unlinks the specified driver from the internal USB driver list.
978 *
979 * NOTE: If you called usb_register_dev(), you still need to call
980 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
981 * this * call will no longer do it for you.
982 */
983void usb_deregister(struct usb_driver *driver)
984{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400985 pr_info("%s: deregistering interface driver %s\n",
986 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800987
Alan Sterned283e92012-01-24 14:35:13 -0500988 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400989 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -0500990 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800991}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600992EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400993
Alan Stern78d9a482008-06-23 16:00:40 -0400994/* Forced unbinding of a USB interface driver, either because
995 * it doesn't support pre_reset/post_reset/reset_resume or
996 * because it doesn't support suspend/resume.
997 *
Alan Stern6aec0442014-03-12 11:30:38 -0400998 * The caller must hold @intf's device's lock, but not @intf's lock.
Alan Stern78d9a482008-06-23 16:00:40 -0400999 */
1000void usb_forced_unbind_intf(struct usb_interface *intf)
1001{
1002 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
1003
1004 dev_dbg(&intf->dev, "forced unbind\n");
1005 usb_driver_release_interface(driver, intf);
1006
1007 /* Mark the interface for later rebinding */
1008 intf->needs_binding = 1;
1009}
1010
Alan Stern6aec0442014-03-12 11:30:38 -04001011/*
1012 * Unbind drivers for @udev's marked interfaces. These interfaces have
1013 * the needs_binding flag set, for example by usb_resume_interface().
1014 *
1015 * The caller must hold @udev's device lock.
1016 */
1017static void unbind_marked_interfaces(struct usb_device *udev)
1018{
1019 struct usb_host_config *config;
1020 int i;
1021 struct usb_interface *intf;
1022
1023 config = udev->actconfig;
1024 if (config) {
1025 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1026 intf = config->interface[i];
1027 if (intf->dev.driver && intf->needs_binding)
1028 usb_forced_unbind_intf(intf);
1029 }
1030 }
1031}
1032
Alan Stern78d9a482008-06-23 16:00:40 -04001033/* Delayed forced unbinding of a USB interface driver and scan
1034 * for rebinding.
1035 *
Alan Stern6aec0442014-03-12 11:30:38 -04001036 * The caller must hold @intf's device's lock, but not @intf's lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001037 *
Alan Stern5096aed2008-08-12 14:34:14 -04001038 * Note: Rebinds will be skipped if a system sleep transition is in
1039 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -04001040 */
Alan Stern6aec0442014-03-12 11:30:38 -04001041static void usb_rebind_intf(struct usb_interface *intf)
Alan Stern78d9a482008-06-23 16:00:40 -04001042{
1043 int rc;
1044
1045 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +01001046 if (intf->dev.driver)
1047 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001048
1049 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +02001050 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -04001051 intf->needs_binding = 0;
1052 rc = device_attach(&intf->dev);
1053 if (rc < 0)
1054 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1055 }
Alan Stern78d9a482008-06-23 16:00:40 -04001056}
1057
Alan Stern6aec0442014-03-12 11:30:38 -04001058/*
1059 * Rebind drivers to @udev's marked interfaces. These interfaces have
1060 * the needs_binding flag set.
1061 *
1062 * The caller must hold @udev's device lock.
1063 */
1064static void rebind_marked_interfaces(struct usb_device *udev)
1065{
1066 struct usb_host_config *config;
1067 int i;
1068 struct usb_interface *intf;
1069
1070 config = udev->actconfig;
1071 if (config) {
1072 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1073 intf = config->interface[i];
1074 if (intf->needs_binding)
1075 usb_rebind_intf(intf);
1076 }
1077 }
1078}
1079
1080/*
1081 * Unbind all of @udev's marked interfaces and then rebind all of them.
1082 * This ordering is necessary because some drivers claim several interfaces
1083 * when they are first probed.
1084 *
1085 * The caller must hold @udev's device lock.
1086 */
1087void usb_unbind_and_rebind_marked_interfaces(struct usb_device *udev)
1088{
1089 unbind_marked_interfaces(udev);
1090 rebind_marked_interfaces(udev);
1091}
1092
Alan Stern9ff78432008-08-07 13:04:51 -04001093#ifdef CONFIG_PM
1094
Oliver Neukum14931382012-01-05 15:39:57 +01001095/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1096 * There is no check for reset_resume here because it can be determined
1097 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001098 *
1099 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001100 */
Oliver Neukum14931382012-01-05 15:39:57 +01001101static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001102{
1103 struct usb_host_config *config;
1104 int i;
1105 struct usb_interface *intf;
1106 struct usb_driver *drv;
1107
1108 config = udev->actconfig;
1109 if (config) {
1110 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1111 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001112
1113 if (intf->dev.driver) {
1114 drv = to_usb_driver(intf->dev.driver);
1115 if (!drv->suspend || !drv->resume)
1116 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001117 }
1118 }
1119 }
1120}
1121
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001122static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001123{
Alan Stern782da722006-07-01 22:09:35 -04001124 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001125 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001126
Alan Stern114b3682006-07-01 22:13:04 -04001127 if (udev->state == USB_STATE_NOTATTACHED ||
1128 udev->state == USB_STATE_SUSPENDED)
1129 goto done;
1130
Alan Sternb6f64362007-05-04 11:51:54 -04001131 /* For devices that don't have a driver, we do a generic suspend. */
1132 if (udev->dev.driver)
1133 udriver = to_usb_device_driver(udev->dev.driver);
1134 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001135 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001136 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001137 }
Alan Stern2bf40862006-07-01 22:12:19 -04001138 status = udriver->suspend(udev, msg);
1139
Alan Stern20dfdad2007-05-22 11:50:17 -04001140 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001141 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001142 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001143}
Alan Stern36e56a32006-07-01 22:08:06 -04001144
Alan Stern65bfd292008-11-25 16:39:18 -05001145static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001146{
1147 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001148 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001149
Alan Stern0458d5b2007-05-04 11:52:20 -04001150 if (udev->state == USB_STATE_NOTATTACHED)
1151 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001152
Alan Stern1c5df7e2006-07-01 22:13:50 -04001153 /* Can't resume it if it doesn't have a driver. */
1154 if (udev->dev.driver == NULL) {
1155 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001156 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001157 }
1158
Alan Stern6d19c002010-02-12 12:21:11 +01001159 /* Non-root devices on a full/low-speed bus must wait for their
1160 * companion high-speed root hub, in case a handoff is needed.
1161 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001162 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001163 device_pm_wait_for_dev(&udev->dev,
1164 &udev->bus->hs_companion->root_hub->dev);
1165
Alan Stern6bc6cff2007-05-04 11:53:03 -04001166 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1167 udev->reset_resume = 1;
1168
Alan Stern1cc8a252006-07-01 22:10:15 -04001169 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001170 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001171
Alan Stern20dfdad2007-05-22 11:50:17 -04001172 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001173 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001174 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001175}
1176
Alan Stern65605ae2008-08-12 14:33:27 -04001177static int usb_suspend_interface(struct usb_device *udev,
1178 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001179{
1180 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001181 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001182
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001183 if (udev->state == USB_STATE_NOTATTACHED ||
1184 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001185 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001186 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001187
Oliver Neukume78832c2012-01-02 15:11:48 +01001188 /* at this time we know the driver supports suspend */
1189 status = driver->suspend(intf, msg);
1190 if (status && !PMSG_IS_AUTO(msg))
1191 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001192
Alan Stern20dfdad2007-05-22 11:50:17 -04001193 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001194 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001195 return status;
1196}
1197
Alan Stern65605ae2008-08-12 14:33:27 -04001198static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001199 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001200{
Alan Stern1cc8a252006-07-01 22:10:15 -04001201 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001202 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001203
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001204 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001205 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001206
Alan Stern645daaa2006-08-30 15:47:02 -04001207 /* Don't let autoresume interfere with unbinding */
1208 if (intf->condition == USB_INTERFACE_UNBINDING)
1209 goto done;
1210
Alan Stern1c5df7e2006-07-01 22:13:50 -04001211 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001212 if (intf->condition == USB_INTERFACE_UNBOUND) {
1213
1214 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001215 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001216 usb_set_interface(udev, intf->altsetting[0].
1217 desc.bInterfaceNumber, 0);
1218 intf->needs_altsetting0 = 0;
1219 }
Alan Stern2bf40862006-07-01 22:12:19 -04001220 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001221 }
Alan Stern78d9a482008-06-23 16:00:40 -04001222
1223 /* Don't resume if the interface is marked for rebinding */
1224 if (intf->needs_binding)
1225 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001226 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001227
Alan Sternf07600c2007-05-30 15:38:16 -04001228 if (reset_resume) {
1229 if (driver->reset_resume) {
1230 status = driver->reset_resume(intf);
1231 if (status)
1232 dev_err(&intf->dev, "%s error %d\n",
1233 "reset_resume", status);
1234 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001235 intf->needs_binding = 1;
Alan Stern0a56b4f2013-10-18 11:17:21 -04001236 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1237 driver->name);
Alan Sternf07600c2007-05-30 15:38:16 -04001238 }
1239 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001240 status = driver->resume(intf);
1241 if (status)
1242 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001243 }
Alan Stern2bf40862006-07-01 22:12:19 -04001244
1245done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001246 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001247
Alan Stern78d9a482008-06-23 16:00:40 -04001248 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001249 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001250}
1251
Alan Stern645daaa2006-08-30 15:47:02 -04001252/**
1253 * usb_suspend_both - suspend a USB device and its interfaces
1254 * @udev: the usb_device to suspend
1255 * @msg: Power Management message describing this state transition
1256 *
1257 * This is the central routine for suspending USB devices. It calls the
1258 * suspend methods for all the interface drivers in @udev and then calls
Ming Lei303f0842013-03-15 12:08:53 +08001259 * the suspend method for @udev itself. When the routine is called in
1260 * autosuspend, if an error occurs at any stage, all the interfaces
1261 * which were suspended are resumed so that they remain in the same
1262 * state as the device, but when called from system sleep, all error
1263 * from suspend methods of interfaces and the non-root-hub device itself
1264 * are simply ignored, so all suspended interfaces are only resumed
1265 * to the device's state when @udev is root-hub and its suspend method
1266 * returns failure.
Alan Stern645daaa2006-08-30 15:47:02 -04001267 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001268 * Autosuspend requests originating from a child device or an interface
1269 * driver may be made without the protection of @udev's device lock, but
1270 * all other suspend calls will hold the lock. Usbcore will insure that
1271 * method calls do not arrive during bind, unbind, or reset operations.
1272 * However drivers must be prepared to handle suspend calls arriving at
1273 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001274 *
1275 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001276 *
1277 * Return: 0 if the suspend succeeded.
Alan Stern645daaa2006-08-30 15:47:02 -04001278 */
Alan Stern718efa62007-03-09 15:41:13 -05001279static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001280{
1281 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001282 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001283 struct usb_interface *intf;
1284
Alan Stern19410442007-03-27 13:33:59 -04001285 if (udev->state == USB_STATE_NOTATTACHED ||
1286 udev->state == USB_STATE_SUSPENDED)
1287 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001288
Alan Stern645daaa2006-08-30 15:47:02 -04001289 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001290 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001291 n = udev->actconfig->desc.bNumInterfaces;
1292 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001293 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001294 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001295
1296 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001297 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001298 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001299 if (status != 0)
1300 break;
1301 }
1302 }
Alan Stern0af212b2011-06-15 16:27:43 -04001303 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001304 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001305
Alan Sterncd4376e2012-03-28 15:56:17 -04001306 /*
1307 * Ignore errors from non-root-hub devices during
1308 * system sleep transitions. For the most part,
1309 * these devices should go to low power anyway when
1310 * the entire bus is suspended.
1311 */
1312 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001313 status = 0;
1314 }
1315
Alan Sterna8e7c562006-07-01 22:11:02 -04001316 /* If the suspend failed, resume interfaces that did get suspended */
1317 if (status != 0) {
Chen Gang505bdbc2013-04-01 13:04:08 +08001318 if (udev->actconfig) {
1319 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1320 while (++i < n) {
1321 intf = udev->actconfig->interface[i];
1322 usb_resume_interface(udev, intf, msg, 0);
1323 }
Alan Sterna8e7c562006-07-01 22:11:02 -04001324 }
Alan Stern645daaa2006-08-30 15:47:02 -04001325
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001326 /* If the suspend succeeded then prevent any more URB submissions
1327 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001328 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001329 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001330 udev->can_submit = 0;
1331 for (i = 0; i < 16; ++i) {
1332 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1333 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1334 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001335 }
Alan Stern645daaa2006-08-30 15:47:02 -04001336
Alan Stern19410442007-03-27 13:33:59 -04001337 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001338 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001339 return status;
1340}
1341
Alan Stern645daaa2006-08-30 15:47:02 -04001342/**
1343 * usb_resume_both - resume a USB device and its interfaces
1344 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001345 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001346 *
1347 * This is the central routine for resuming USB devices. It calls the
1348 * the resume method for @udev and then calls the resume methods for all
1349 * the interface drivers in @udev.
1350 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001351 * Autoresume requests originating from a child device or an interface
1352 * driver may be made without the protection of @udev's device lock, but
1353 * all other resume calls will hold the lock. Usbcore will insure that
1354 * method calls do not arrive during bind, unbind, or reset operations.
1355 * However drivers must be prepared to handle resume calls arriving at
1356 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001357 *
1358 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001359 *
1360 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001361 */
Alan Stern65bfd292008-11-25 16:39:18 -05001362static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001363{
Alan Stern645daaa2006-08-30 15:47:02 -04001364 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001365 int i;
1366 struct usb_interface *intf;
1367
Alan Stern19410442007-03-27 13:33:59 -04001368 if (udev->state == USB_STATE_NOTATTACHED) {
1369 status = -ENODEV;
1370 goto done;
1371 }
Alan Stern6840d252007-09-10 11:34:26 -04001372 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001373
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001374 /* Resume the device */
1375 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001376 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001377
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001378 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001379 if (status == 0 && udev->actconfig) {
1380 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1381 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001382 usb_resume_interface(udev, intf, msg,
1383 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001384 }
1385 }
Alan Sternc08512c2010-11-15 15:57:58 -05001386 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001387
Alan Stern19410442007-03-27 13:33:59 -04001388 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001389 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001390 if (!status)
1391 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001392 return status;
1393}
1394
Alan Stern5f677f12010-04-02 13:20:11 -04001395static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1396{
Alan Stern48826622010-06-22 16:14:48 -04001397 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001398
1399 /* Remote wakeup is needed only when we actually go to sleep.
1400 * For things like FREEZE and QUIESCE, if the device is already
1401 * autosuspended then its current wakeup setting is okay.
1402 */
1403 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1404 if (udev->state != USB_STATE_SUSPENDED)
1405 udev->do_remote_wakeup = 0;
1406 return;
1407 }
1408
Alan Stern48826622010-06-22 16:14:48 -04001409 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001410 * actually want it.
1411 */
Alan Stern48826622010-06-22 16:14:48 -04001412 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001413
1414 /* If the device is autosuspended with the wrong wakeup setting,
1415 * autoresume now so the setting can be changed.
1416 */
1417 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1418 pm_runtime_resume(&udev->dev);
1419 udev->do_remote_wakeup = w;
1420}
1421
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001422/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001423int usb_suspend(struct device *dev, pm_message_t msg)
1424{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001425 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001426
Oliver Neukum14931382012-01-05 15:39:57 +01001427 unbind_no_pm_drivers_interfaces(udev);
1428
1429 /* From now on we are sure all drivers support suspend/resume
1430 * but not necessarily reset_resume()
1431 * so we may still need to unbind and rebind upon resume
1432 */
Alan Stern5f677f12010-04-02 13:20:11 -04001433 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001434 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001435}
1436
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001437/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001438int usb_resume_complete(struct device *dev)
1439{
1440 struct usb_device *udev = to_usb_device(dev);
1441
1442 /* For PM complete calls, all we do is rebind interfaces
1443 * whose needs_binding flag is set
1444 */
1445 if (udev->state != USB_STATE_NOTATTACHED)
Alan Stern6aec0442014-03-12 11:30:38 -04001446 rebind_marked_interfaces(udev);
Oliver Neukum98d9a822012-01-11 08:38:35 +01001447 return 0;
1448}
1449
1450/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001451int usb_resume(struct device *dev, pm_message_t msg)
1452{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001453 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001454 int status;
1455
Oliver Neukum98d9a822012-01-11 08:38:35 +01001456 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001457 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001458 * Unbind the interfaces that will need rebinding later,
1459 * because they fail to support reset_resume.
1460 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001461 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001462 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001463 status = usb_resume_both(udev, msg);
1464 if (status == 0) {
1465 pm_runtime_disable(dev);
1466 pm_runtime_set_active(dev);
1467 pm_runtime_enable(dev);
Alan Stern6aec0442014-03-12 11:30:38 -04001468 unbind_marked_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001469 }
Alan Stern0c590e22010-01-08 12:57:14 -05001470
1471 /* Avoid PM error messages for devices disconnected while suspended
1472 * as we'll display regular disconnect messages just a bit later.
1473 */
Peter Chen7491f132010-09-27 16:43:25 +08001474 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001475 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001476 return status;
1477}
1478
Alan Stern088f7fe2010-01-08 12:56:54 -05001479/**
1480 * usb_enable_autosuspend - allow a USB device to be autosuspended
1481 * @udev: the USB device which may be autosuspended
1482 *
1483 * This routine allows @udev to be autosuspended. An autosuspend won't
1484 * take place until the autosuspend_delay has elapsed and all the other
1485 * necessary conditions are satisfied.
1486 *
1487 * The caller must hold @udev's device lock.
1488 */
Alan Stern9e18c822010-04-02 13:22:09 -04001489void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001490{
Alan Stern9e18c822010-04-02 13:22:09 -04001491 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001492}
1493EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1494
1495/**
1496 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1497 * @udev: the USB device which may not be autosuspended
1498 *
1499 * This routine prevents @udev from being autosuspended and wakes it up
1500 * if it is already autosuspended.
1501 *
1502 * The caller must hold @udev's device lock.
1503 */
Alan Stern9e18c822010-04-02 13:22:09 -04001504void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001505{
Alan Stern9e18c822010-04-02 13:22:09 -04001506 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001507}
1508EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1509
Alan Stern645daaa2006-08-30 15:47:02 -04001510/**
1511 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001512 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001513 *
1514 * This routine should be called when a core subsystem is finished using
1515 * @udev and wants to allow it to autosuspend. Examples would be when
1516 * @udev's device file in usbfs is closed or after a configuration change.
1517 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001518 * @udev's usage counter is decremented; if it drops to 0 and all the
1519 * interfaces are inactive then a delayed autosuspend will be attempted.
1520 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001521 *
Alan Stern62e299e2010-01-08 12:56:19 -05001522 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001523 *
1524 * This routine can run only in process context.
1525 */
Alan Stern94fcda12006-11-20 11:38:46 -05001526void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001527{
Alan Stern94fcda12006-11-20 11:38:46 -05001528 int status;
1529
Ming Lei6ddf27c2010-11-15 15:57:30 -05001530 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001531 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001532 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1533 __func__, atomic_read(&udev->dev.power.usage_count),
1534 status);
Alan Stern19c26232007-02-20 15:03:32 -05001535}
1536
1537/**
Alan Stern645daaa2006-08-30 15:47:02 -04001538 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001539 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001540 *
1541 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001542 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001543 * occur until usb_autosuspend_device() is called. (Note that this will
1544 * not prevent suspend events originating in the PM core.) Examples would
1545 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001546 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001547 *
Alan Stern94fcda12006-11-20 11:38:46 -05001548 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1549 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001550 *
Alan Stern62e299e2010-01-08 12:56:19 -05001551 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001552 *
1553 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001554 *
1555 * Return: 0 on success. A negative error code otherwise.
Alan Stern645daaa2006-08-30 15:47:02 -04001556 */
Alan Stern94fcda12006-11-20 11:38:46 -05001557int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001558{
1559 int status;
1560
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001561 status = pm_runtime_get_sync(&udev->dev);
1562 if (status < 0)
1563 pm_runtime_put_sync(&udev->dev);
1564 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1565 __func__, atomic_read(&udev->dev.power.usage_count),
1566 status);
1567 if (status > 0)
1568 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001569 return status;
1570}
1571
Alan Stern645daaa2006-08-30 15:47:02 -04001572/**
1573 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001574 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001575 *
1576 * This routine should be called by an interface driver when it is
1577 * finished using @intf and wants to allow it to autosuspend. A typical
1578 * example would be a character-device driver when its device file is
1579 * closed.
1580 *
1581 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001582 * 0, a delayed autosuspend request for @intf's device is attempted. The
1583 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001584 *
Alan Stern645daaa2006-08-30 15:47:02 -04001585 * This routine can run only in process context.
1586 */
1587void usb_autopm_put_interface(struct usb_interface *intf)
1588{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001589 struct usb_device *udev = interface_to_usbdev(intf);
1590 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001591
Ming Lei6ddf27c2010-11-15 15:57:30 -05001592 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001593 atomic_dec(&intf->pm_usage_cnt);
1594 status = pm_runtime_put_sync(&intf->dev);
1595 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1596 __func__, atomic_read(&intf->dev.power.usage_count),
1597 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001598}
1599EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1600
1601/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001602 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1603 * @intf: the usb_interface whose counter should be decremented
1604 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001605 * This routine does much the same thing as usb_autopm_put_interface():
1606 * It decrements @intf's usage counter and schedules a delayed
1607 * autosuspend request if the counter is <= 0. The difference is that it
1608 * does not perform any synchronization; callers should hold a private
1609 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001610 *
1611 * Typically a driver would call this routine during an URB's completion
1612 * handler, if no more URBs were pending.
1613 *
1614 * This routine can run in atomic context.
1615 */
1616void usb_autopm_put_interface_async(struct usb_interface *intf)
1617{
1618 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001619 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001620
Ming Lei6ddf27c2010-11-15 15:57:30 -05001621 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001622 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001623 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001624 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1625 __func__, atomic_read(&intf->dev.power.usage_count),
1626 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001627}
1628EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1629
1630/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001631 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1632 * @intf: the usb_interface whose counter should be decremented
1633 *
1634 * This routine decrements @intf's usage counter but does not carry out an
1635 * autosuspend.
1636 *
1637 * This routine can run in atomic context.
1638 */
1639void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1640{
1641 struct usb_device *udev = interface_to_usbdev(intf);
1642
Ming Lei6ddf27c2010-11-15 15:57:30 -05001643 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001644 atomic_dec(&intf->pm_usage_cnt);
1645 pm_runtime_put_noidle(&intf->dev);
1646}
1647EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1648
1649/**
Alan Stern645daaa2006-08-30 15:47:02 -04001650 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001651 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001652 *
1653 * This routine should be called by an interface driver when it wants to
1654 * use @intf and needs to guarantee that it is not suspended. In addition,
1655 * the routine prevents @intf from being autosuspended subsequently. (Note
1656 * that this will not prevent suspend events originating in the PM core.)
1657 * This prevention will persist until usb_autopm_put_interface() is called
1658 * or @intf is unbound. A typical example would be a character-device
1659 * driver when its device file is opened.
1660 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001661 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1662 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001663 *
1664 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001665 *
1666 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001667 */
1668int usb_autopm_get_interface(struct usb_interface *intf)
1669{
Alan Sternaf4f7602006-10-30 17:06:45 -05001670 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001671
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001672 status = pm_runtime_get_sync(&intf->dev);
1673 if (status < 0)
1674 pm_runtime_put_sync(&intf->dev);
1675 else
1676 atomic_inc(&intf->pm_usage_cnt);
1677 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1678 __func__, atomic_read(&intf->dev.power.usage_count),
1679 status);
1680 if (status > 0)
1681 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001682 return status;
1683}
1684EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1685
Alan Stern692a1862006-10-30 17:07:51 -05001686/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001687 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1688 * @intf: the usb_interface whose counter should be incremented
1689 *
1690 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001691 * usb_autopm_get_interface(): It increments @intf's usage counter and
1692 * queues an autoresume request if the device is suspended. The
1693 * differences are that it does not perform any synchronization (callers
1694 * should hold a private lock and handle all synchronization issues
1695 * themselves), and it does not autoresume the device directly (it only
1696 * queues a request). After a successful call, the device may not yet be
1697 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001698 *
1699 * This routine can run in atomic context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001700 *
1701 * Return: 0 on success. A negative error code otherwise.
Alan Stern9ac39f22008-11-12 16:19:49 -05001702 */
1703int usb_autopm_get_interface_async(struct usb_interface *intf)
1704{
Ming Lei63defa72010-11-15 15:56:54 -05001705 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001706
Ming Lei63defa72010-11-15 15:56:54 -05001707 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001708 if (status < 0 && status != -EINPROGRESS)
1709 pm_runtime_put_noidle(&intf->dev);
1710 else
Alan Sternccf5b802009-06-29 11:00:01 -04001711 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001712 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1713 __func__, atomic_read(&intf->dev.power.usage_count),
1714 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001715 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001716 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001717 return status;
1718}
1719EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1720
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001721/**
1722 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1723 * @intf: the usb_interface whose counter should be incremented
1724 *
1725 * This routine increments @intf's usage counter but does not carry out an
1726 * autoresume.
1727 *
1728 * This routine can run in atomic context.
1729 */
1730void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1731{
1732 struct usb_device *udev = interface_to_usbdev(intf);
1733
Ming Lei6ddf27c2010-11-15 15:57:30 -05001734 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001735 atomic_inc(&intf->pm_usage_cnt);
1736 pm_runtime_get_noresume(&intf->dev);
1737}
1738EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1739
1740/* Internal routine to check whether we may autosuspend a device. */
1741static int autosuspend_check(struct usb_device *udev)
1742{
Alan Stern7560d32e2010-04-02 13:18:50 -04001743 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001744 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001745
1746 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1747 * any interface drivers require remote wakeup but it isn't available.
1748 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001749 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001750 if (udev->actconfig) {
1751 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1752 intf = udev->actconfig->interface[i];
1753
1754 /* We don't need to check interfaces that are
1755 * disabled for runtime PM. Either they are unbound
1756 * or else their drivers don't support autosuspend
1757 * and so they are permanently active.
1758 */
1759 if (intf->dev.power.disable_depth)
1760 continue;
1761 if (atomic_read(&intf->dev.power.usage_count) > 0)
1762 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001763 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001764
1765 /* Don't allow autosuspend if the device will need
1766 * a reset-resume and any of its interface drivers
1767 * doesn't include support or needs remote wakeup.
1768 */
1769 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1770 struct usb_driver *driver;
1771
1772 driver = to_usb_driver(intf->dev.driver);
1773 if (!driver->reset_resume ||
1774 intf->needs_remote_wakeup)
1775 return -EOPNOTSUPP;
1776 }
1777 }
1778 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001779 if (w && !device_can_wakeup(&udev->dev)) {
1780 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1781 return -EOPNOTSUPP;
1782 }
1783 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001784 return 0;
1785}
1786
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001787int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001788{
Ming Lei63defa72010-11-15 15:56:54 -05001789 struct usb_device *udev = to_usb_device(dev);
1790 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001791
1792 /* A USB device can be suspended if it passes the various autosuspend
1793 * checks. Runtime suspend for a USB device means suspending all the
1794 * interfaces and then the device itself.
1795 */
Ming Lei63defa72010-11-15 15:56:54 -05001796 if (autosuspend_check(udev) != 0)
1797 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001798
Ming Lei63defa72010-11-15 15:56:54 -05001799 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001800
1801 /* Allow a retry if autosuspend failed temporarily */
1802 if (status == -EAGAIN || status == -EBUSY)
1803 usb_mark_last_busy(udev);
1804
Alan Stern8ef42dd2014-05-23 10:45:54 -04001805 /*
1806 * The PM core reacts badly unless the return code is 0,
1807 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
1808 * (except for root hubs, because they don't suspend through
1809 * an upstream port like other USB devices).
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001810 */
Alan Stern8ef42dd2014-05-23 10:45:54 -04001811 if (status != 0 && udev->parent)
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001812 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001813 return status;
1814}
1815
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001816int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001817{
Ming Lei63defa72010-11-15 15:56:54 -05001818 struct usb_device *udev = to_usb_device(dev);
1819 int status;
1820
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001821 /* Runtime resume for a USB device means resuming both the device
1822 * and all its interfaces.
1823 */
Ming Lei63defa72010-11-15 15:56:54 -05001824 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001825 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001826}
1827
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001828int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001829{
Ming Lei63defa72010-11-15 15:56:54 -05001830 struct usb_device *udev = to_usb_device(dev);
1831
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001832 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001833 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001834 */
Ming Lei63defa72010-11-15 15:56:54 -05001835 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001836 pm_runtime_autosuspend(dev);
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001837 /* Tell the core not to suspend it, though. */
1838 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001839}
1840
Andiry Xu65580b432011-09-23 14:19:52 -07001841int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1842{
1843 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1844 int ret = -EPERM;
1845
Sarah Sharpde68bab2013-09-30 17:26:28 +03001846 if (enable && !udev->usb2_hw_lpm_allowed)
1847 return 0;
1848
Andiry Xu65580b432011-09-23 14:19:52 -07001849 if (hcd->driver->set_usb2_hw_lpm) {
1850 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1851 if (!ret)
1852 udev->usb2_hw_lpm_enabled = enable;
1853 }
1854
1855 return ret;
1856}
1857
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01001858#endif /* CONFIG_PM */
Alan Stern645daaa2006-08-30 15:47:02 -04001859
Alan Stern36e56a32006-07-01 22:08:06 -04001860struct bus_type usb_bus_type = {
1861 .name = "usb",
1862 .match = usb_device_match,
1863 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001864};