blob: 6b5063e7943fac720a266478d79b04a352d51e41 [file] [log] [blame]
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -08001/*
2 * drivers/usb/driver.c - most of the driver model stuff for usb
3 *
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
5 *
6 * based on drivers/usb/usb.c which had the following copyrights:
7 * (C) Copyright Linus Torvalds 1999
8 * (C) Copyright Johannes Erdfelt 1999-2001
9 * (C) Copyright Andreas Gal 1999
10 * (C) Copyright Gregory P. Smith 1999
11 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
12 * (C) Copyright Randy Dunlap 2000
13 * (C) Copyright David Brownell 2000-2004
14 * (C) Copyright Yggdrasil Computing, Inc. 2000
15 * (usb_device_id matching changes by Adam J. Richter)
16 * (C) Copyright Greg Kroah-Hartman 2002-2003
17 *
18 * NOTE! This is not actually a driver at all, rather this is
19 * just a collection of helper routines that implement the
Alan Stern36e56a32006-07-01 22:08:06 -040020 * matching, probing, releasing, suspending and resuming for
21 * real drivers.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080022 *
23 */
24
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080025#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040027#include <linux/export.h>
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080028#include <linux/usb.h>
Alan Stern6bc6cff2007-05-04 11:53:03 -040029#include <linux/usb/quirks.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020030#include <linux/usb/hcd.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020031
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080032#include "usb.h"
33
Alan Stern20dfdad2007-05-22 11:50:17 -040034
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080035/*
36 * Adds a new dynamic USBdevice ID to this driver,
37 * and cause the driver to probe for all devices again.
38 */
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010039ssize_t usb_store_new_id(struct usb_dynids *dynids,
Wolfram Sang2fc82c22014-01-10 19:36:42 +010040 const struct usb_device_id *id_table,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010041 struct device_driver *driver,
42 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080043{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080044 struct usb_dynid *dynid;
45 u32 idVendor = 0;
46 u32 idProduct = 0;
Josua Dietzeff231db2011-10-23 14:22:29 +020047 unsigned int bInterfaceClass = 0;
Wolfram Sang2fc82c22014-01-10 19:36:42 +010048 u32 refVendor, refProduct;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080049 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070050 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080051
Wolfram Sang2fc82c22014-01-10 19:36:42 +010052 fields = sscanf(buf, "%x %x %x %x %x", &idVendor, &idProduct,
53 &bInterfaceClass, &refVendor, &refProduct);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080054 if (fields < 2)
55 return -EINVAL;
56
57 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
58 if (!dynid)
59 return -ENOMEM;
60
61 INIT_LIST_HEAD(&dynid->node);
62 dynid->id.idVendor = idVendor;
63 dynid->id.idProduct = idProduct;
64 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Wolfram Sangc63fe8f2014-01-10 19:36:41 +010065 if (fields > 2 && bInterfaceClass) {
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010066 if (bInterfaceClass > 255) {
67 retval = -EINVAL;
68 goto fail;
69 }
Wolfram Sangc63fe8f2014-01-10 19:36:41 +010070
Josua Dietzeff231db2011-10-23 14:22:29 +020071 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
72 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
73 }
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080074
Wolfram Sang2fc82c22014-01-10 19:36:42 +010075 if (fields > 4) {
76 const struct usb_device_id *id = id_table;
77
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010078 if (!id) {
79 retval = -ENODEV;
80 goto fail;
81 }
Wolfram Sang1b9fb312014-01-13 11:29:23 +010082
Wolfram Sang2fc82c22014-01-10 19:36:42 +010083 for (; id->match_flags; id++)
Wolfram Sang52a6966c2014-01-12 10:07:50 +010084 if (id->idVendor == refVendor && id->idProduct == refProduct)
Wolfram Sang2fc82c22014-01-10 19:36:42 +010085 break;
Wolfram Sang52a6966c2014-01-12 10:07:50 +010086
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010087 if (id->match_flags) {
Wolfram Sang52a6966c2014-01-12 10:07:50 +010088 dynid->id.driver_info = id->driver_info;
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010089 } else {
90 retval = -ENODEV;
91 goto fail;
92 }
Wolfram Sang2fc82c22014-01-10 19:36:42 +010093 }
94
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010095 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020096 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010097 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080098
Alan Sterncef9bc52012-01-24 13:34:41 -050099 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800100
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700101 if (retval)
102 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800103 return count;
Christian Engelmayer7f196ca2014-01-28 22:22:27 +0100104
105fail:
106 kfree(dynid);
107 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800108}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100109EXPORT_SYMBOL_GPL(usb_store_new_id);
110
Bjørn Morkef206f32012-05-13 12:35:00 +0200111ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200112{
113 struct usb_dynid *dynid;
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200114 size_t count = 0;
115
Bjørn Morkef206f32012-05-13 12:35:00 +0200116 list_for_each_entry(dynid, &dynids->list, node)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200117 if (dynid->id.bInterfaceClass != 0)
118 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
119 dynid->id.idVendor, dynid->id.idProduct,
120 dynid->id.bInterfaceClass);
121 else
122 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
123 dynid->id.idVendor, dynid->id.idProduct);
124 return count;
125}
Bjørn Morkef206f32012-05-13 12:35:00 +0200126EXPORT_SYMBOL_GPL(usb_show_dynids);
127
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700128static ssize_t new_id_show(struct device_driver *driver, char *buf)
Bjørn Morkef206f32012-05-13 12:35:00 +0200129{
130 struct usb_driver *usb_drv = to_usb_driver(driver);
131
132 return usb_show_dynids(&usb_drv->dynids, buf);
133}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200134
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700135static ssize_t new_id_store(struct device_driver *driver,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100136 const char *buf, size_t count)
137{
138 struct usb_driver *usb_drv = to_usb_driver(driver);
139
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100140 return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100141}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700142static DRIVER_ATTR_RW(new_id);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800143
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700144/*
145 * Remove a USB device ID from this driver
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800146 */
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700147static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
148 size_t count)
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800149{
150 struct usb_dynid *dynid, *n;
151 struct usb_driver *usb_driver = to_usb_driver(driver);
Alan Coxac08de32012-09-17 11:55:23 +0100152 u32 idVendor;
153 u32 idProduct;
154 int fields;
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800155
156 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
157 if (fields < 2)
158 return -EINVAL;
159
160 spin_lock(&usb_driver->dynids.lock);
161 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
162 struct usb_device_id *id = &dynid->id;
Kris Borer79a02742015-06-16 13:24:53 -0400163
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800164 if ((id->idVendor == idVendor) &&
165 (id->idProduct == idProduct)) {
166 list_del(&dynid->node);
167 kfree(dynid);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800168 break;
169 }
170 }
171 spin_unlock(&usb_driver->dynids.lock);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800172 return count;
173}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700174
175static ssize_t remove_id_show(struct device_driver *driver, char *buf)
176{
177 return new_id_show(driver, buf);
178}
179static DRIVER_ATTR_RW(remove_id);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800180
Alan Sterned283e92012-01-24 14:35:13 -0500181static int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800182{
183 int error = 0;
184
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800185 if (usb_drv->no_dynamic_id)
186 goto exit;
187
Alan Sterned283e92012-01-24 14:35:13 -0500188 if (usb_drv->probe != NULL) {
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800189 error = driver_create_file(&usb_drv->drvwrap.driver,
190 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500191 if (error == 0) {
192 error = driver_create_file(&usb_drv->drvwrap.driver,
193 &driver_attr_remove_id);
194 if (error)
195 driver_remove_file(&usb_drv->drvwrap.driver,
196 &driver_attr_new_id);
197 }
198 }
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800199exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800200 return error;
201}
202
Alan Sterned283e92012-01-24 14:35:13 -0500203static void usb_remove_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800204{
205 if (usb_drv->no_dynamic_id)
206 return;
207
Alan Sterned283e92012-01-24 14:35:13 -0500208 if (usb_drv->probe != NULL) {
209 driver_remove_file(&usb_drv->drvwrap.driver,
210 &driver_attr_remove_id);
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800211 driver_remove_file(&usb_drv->drvwrap.driver,
212 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500213 }
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800214}
215
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800216static void usb_free_dynids(struct usb_driver *usb_drv)
217{
218 struct usb_dynid *dynid, *n;
219
220 spin_lock(&usb_drv->dynids.lock);
221 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
222 list_del(&dynid->node);
223 kfree(dynid);
224 }
225 spin_unlock(&usb_drv->dynids.lock);
226}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800227
228static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
229 struct usb_driver *drv)
230{
231 struct usb_dynid *dynid;
232
233 spin_lock(&drv->dynids.lock);
234 list_for_each_entry(dynid, &drv->dynids.list, node) {
235 if (usb_match_one_id(intf, &dynid->id)) {
236 spin_unlock(&drv->dynids.lock);
237 return &dynid->id;
238 }
239 }
240 spin_unlock(&drv->dynids.lock);
241 return NULL;
242}
243
244
Alan Stern8bb54ab2006-07-01 22:08:49 -0400245/* called from driver core with dev locked */
246static int usb_probe_device(struct device *dev)
247{
248 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200249 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500250 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400251
Harvey Harrison441b62c2008-03-03 16:08:34 -0800252 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400253
Alan Stern8bb54ab2006-07-01 22:08:49 -0400254 /* TODO: Add real matching code */
255
Alan Stern645daaa2006-08-30 15:47:02 -0400256 /* The device should always appear to be in use
Masanari Iida02582e92012-08-22 19:11:26 +0900257 * unless the driver supports autosuspend.
Alan Stern645daaa2006-08-30 15:47:02 -0400258 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500259 if (!udriver->supports_autosuspend)
260 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400261
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500262 if (!error)
263 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400264 return error;
265}
266
267/* called from driver core with dev locked */
268static int usb_unbind_device(struct device *dev)
269{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500270 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400271 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
272
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500273 udriver->disconnect(udev);
274 if (!udriver->supports_autosuspend)
275 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400276 return 0;
277}
278
Alan Stern8bb54ab2006-07-01 22:08:49 -0400279/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800280static int usb_probe_interface(struct device *dev)
281{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400282 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200283 struct usb_interface *intf = to_usb_interface(dev);
284 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800285 const struct usb_device_id *id;
286 int error = -ENODEV;
Sarah Sharp83060952012-05-02 14:25:52 -0700287 int lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800288
Harvey Harrison441b62c2008-03-03 16:08:34 -0800289 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800290
Alan Stern78d9a482008-06-23 16:00:40 -0400291 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800292
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400293 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500294 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400295
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800296 if (udev->authorized == 0) {
297 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500298 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800299 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700300
Bjørn Mork31c6bf72014-01-11 02:04:00 +0100301 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800302 if (!id)
Bjørn Mork31c6bf72014-01-11 02:04:00 +0100303 id = usb_match_id(intf, driver->id_table);
Alan Stern0f3dda92010-01-08 12:56:04 -0500304 if (!id)
305 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800306
Alan Stern0f3dda92010-01-08 12:56:04 -0500307 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400308
Alan Stern0f3dda92010-01-08 12:56:04 -0500309 error = usb_autoresume_device(udev);
310 if (error)
311 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400312
Alan Stern0f3dda92010-01-08 12:56:04 -0500313 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400314
Alan Stern571dc792010-04-09 16:03:43 -0400315 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500316 * runtime-PM-enabled only if the driver has autosuspend support.
317 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500318 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500319 pm_runtime_set_active(dev);
320 pm_suspend_ignore_children(dev, false);
321 if (driver->supports_autosuspend)
322 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200323
Sarah Sharp83060952012-05-02 14:25:52 -0700324 /* If the new driver doesn't allow hub-initiated LPM, and we can't
325 * disable hub-initiated LPM, then fail the probe.
326 *
327 * Otherwise, leaving LPM enabled should be harmless, because the
328 * endpoint intervals should remain the same, and the U1/U2 timeouts
329 * should remain the same.
330 *
331 * If we need to install alt setting 0 before probe, or another alt
332 * setting during probe, that should also be fine. usb_set_interface()
333 * will attempt to disable LPM, and fail if it can't disable it.
334 */
335 lpm_disable_error = usb_unlocked_disable_lpm(udev);
336 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
337 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
338 __func__, driver->name);
339 error = lpm_disable_error;
340 goto err;
341 }
342
Alan Stern0f3dda92010-01-08 12:56:04 -0500343 /* Carry out a deferred switch to altsetting 0 */
344 if (intf->needs_altsetting0) {
345 error = usb_set_interface(udev, intf->altsetting[0].
346 desc.bInterfaceNumber, 0);
347 if (error < 0)
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200348 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500349 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800350 }
351
Alan Stern0f3dda92010-01-08 12:56:04 -0500352 error = driver->probe(intf, id);
353 if (error)
354 goto err;
355
356 intf->condition = USB_INTERFACE_BOUND;
Sarah Sharp83060952012-05-02 14:25:52 -0700357
358 /* If the LPM disable succeeded, balance the ref counts. */
359 if (!lpm_disable_error)
360 usb_unlocked_enable_lpm(udev);
361
Alan Stern0f3dda92010-01-08 12:56:04 -0500362 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800363 return error;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200364
Alan Stern0f3dda92010-01-08 12:56:04 -0500365 err:
Hans de Goedee714fad2012-05-22 11:36:59 +0200366 usb_set_intfdata(intf, NULL);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200367 intf->needs_remote_wakeup = 0;
368 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500369
Sarah Sharpd01f87c2012-10-04 09:53:43 -0700370 /* If the LPM disable succeeded, balance the ref counts. */
371 if (!lpm_disable_error)
372 usb_unlocked_enable_lpm(udev);
373
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500374 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400375 if (driver->supports_autosuspend)
376 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500377 pm_runtime_set_suspended(dev);
378
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200379 usb_autosuspend_device(udev);
380 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800381}
382
Alan Stern8bb54ab2006-07-01 22:08:49 -0400383/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800384static int usb_unbind_interface(struct device *dev)
385{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400386 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800387 struct usb_interface *intf = to_usb_interface(dev);
Hans de Goede6343e8b2013-10-09 17:19:26 +0200388 struct usb_host_endpoint *ep, **eps = NULL;
Alan Stern645daaa2006-08-30 15:47:02 -0400389 struct usb_device *udev;
Hans de Goede6343e8b2013-10-09 17:19:26 +0200390 int i, j, error, r, lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800391
392 intf->condition = USB_INTERFACE_UNBINDING;
393
Alan Stern645daaa2006-08-30 15:47:02 -0400394 /* Autoresume for set_interface call below */
395 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500396 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400397
Sarah Sharp83060952012-05-02 14:25:52 -0700398 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
399 * the driver is unbound. If LPM isn't disabled, that's fine because it
400 * wouldn't be enabled unless all the bound interfaces supported
401 * hub-initiated LPM.
402 */
403 lpm_disable_error = usb_unlocked_disable_lpm(udev);
404
Alan Stern1299cff2014-07-17 15:40:57 -0400405 /*
406 * Terminate all URBs for this interface unless the driver
407 * supports "soft" unbinding and the device is still present.
Alan Stern9da82bd2008-05-08 11:54:37 -0400408 */
Alan Stern1299cff2014-07-17 15:40:57 -0400409 if (!driver->soft_unbind || udev->state == USB_STATE_NOTATTACHED)
Alan Sternddeac4e2009-01-15 17:03:33 -0500410 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800411
Alan Stern8bb54ab2006-07-01 22:08:49 -0400412 driver->disconnect(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800413
Hans de Goede6343e8b2013-10-09 17:19:26 +0200414 /* Free streams */
415 for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
416 ep = &intf->cur_altsetting->endpoint[i];
417 if (ep->streams == 0)
418 continue;
419 if (j == 0) {
420 eps = kmalloc(USB_MAXENDPOINTS * sizeof(void *),
421 GFP_KERNEL);
422 if (!eps) {
423 dev_warn(dev, "oom, leaking streams\n");
424 break;
425 }
426 }
427 eps[j++] = ep;
428 }
429 if (j) {
430 usb_free_streams(intf, eps, j, GFP_KERNEL);
431 kfree(eps);
432 }
433
Alan Stern55151d72008-08-12 14:33:59 -0400434 /* Reset other interface state.
435 * We cannot do a Set-Interface if the device is suspended or
436 * if it is prepared for a system sleep (since installing a new
437 * altsetting means creating new endpoint device entries).
438 * When either of these happens, defer the Set-Interface.
439 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500440 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
441 /* Already in altsetting 0 so skip Set-Interface.
442 * Just re-enable it without affecting the endpoint toggles.
443 */
444 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200445 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200446 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400447 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200448 if (r < 0)
449 intf->needs_altsetting0 = 1;
450 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400451 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200452 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800453 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400454
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800455 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400456 intf->needs_remote_wakeup = 0;
457
Sarah Sharp83060952012-05-02 14:25:52 -0700458 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
459 if (!lpm_disable_error)
460 usb_unlocked_enable_lpm(udev);
461
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500462 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400463 if (driver->supports_autosuspend)
464 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500465 pm_runtime_set_suspended(dev);
466
467 /* Undo any residual pm_autopm_get_interface_* calls */
468 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
469 usb_autopm_put_interface_no_suspend(intf);
470 atomic_set(&intf->pm_usage_cnt, 0);
471
Alan Stern645daaa2006-08-30 15:47:02 -0400472 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500473 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800474
475 return 0;
476}
477
Alan Stern36e56a32006-07-01 22:08:06 -0400478/**
479 * usb_driver_claim_interface - bind a driver to an interface
480 * @driver: the driver to be bound
481 * @iface: the interface to which it will be bound; must be in the
482 * usb device's active configuration
483 * @priv: driver data associated with that interface
484 *
485 * This is used by usb device drivers that need to claim more than one
486 * interface on a device when probing (audio and acm are current examples).
487 * No device driver should directly modify internal usb_interface or
488 * usb_device structure members.
489 *
490 * Few drivers should need to use this routine, since the most natural
491 * way to bind to an interface is to return the private data from
492 * the driver's probe() method.
493 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400494 * Callers must own the device lock, so driver probe() entries don't need
495 * extra locking, but other call contexts may need to explicitly claim that
496 * lock.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200497 *
498 * Return: 0 on success.
Alan Stern36e56a32006-07-01 22:08:06 -0400499 */
500int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800501 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400502{
503 struct device *dev = &iface->dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700504 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700505 int retval = 0;
Sarah Sharp83060952012-05-02 14:25:52 -0700506 int lpm_disable_error;
Alan Stern36e56a32006-07-01 22:08:06 -0400507
508 if (dev->driver)
509 return -EBUSY;
510
Sarah Sharp83060952012-05-02 14:25:52 -0700511 udev = interface_to_usbdev(iface);
512
Alan Stern8bb54ab2006-07-01 22:08:49 -0400513 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400514 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400515 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400516
Alan Stern36e56a32006-07-01 22:08:06 -0400517 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500518
Sarah Sharp83060952012-05-02 14:25:52 -0700519 /* Disable LPM until this driver is bound. */
520 lpm_disable_error = usb_unlocked_disable_lpm(udev);
521 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
522 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
523 __func__, driver->name);
524 return -ENOMEM;
525 }
526
Alan Stern89842ae2010-05-11 11:44:06 -0400527 /* Claimed interfaces are initially inactive (suspended) and
528 * runtime-PM-enabled, but only if the driver has autosuspend
529 * support. Otherwise they are marked active, to prevent the
530 * device from being autosuspended, but left disabled. In either
531 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500532 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500533 pm_suspend_ignore_children(dev, false);
534 if (driver->supports_autosuspend)
535 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400536 else
537 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400538
539 /* if interface was already added, bind now; else let
540 * the future device_add() bind it, bypassing probe()
541 */
542 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700543 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400544
Sarah Sharp83060952012-05-02 14:25:52 -0700545 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
546 if (!lpm_disable_error)
547 usb_unlocked_enable_lpm(udev);
548
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700549 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400550}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600551EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400552
553/**
554 * usb_driver_release_interface - unbind a driver from an interface
555 * @driver: the driver to be unbound
556 * @iface: the interface from which it will be unbound
557 *
558 * This can be used by drivers to release an interface without waiting
559 * for their disconnect() methods to be called. In typical cases this
560 * also causes the driver disconnect() method to be called.
561 *
562 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400563 * Callers must own the device lock, so driver disconnect() entries don't
564 * need extra locking, but other call contexts may need to explicitly claim
565 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400566 */
567void usb_driver_release_interface(struct usb_driver *driver,
568 struct usb_interface *iface)
569{
570 struct device *dev = &iface->dev;
571
572 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400573 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400574 return;
575
576 /* don't release from within disconnect() */
577 if (iface->condition != USB_INTERFACE_BOUND)
578 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400579 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400580
Alan Stern91f8d062009-04-16 15:35:09 -0400581 /* Release via the driver core only if the interface
582 * has already been registered
583 */
Alan Stern36e56a32006-07-01 22:08:06 -0400584 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400585 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800586 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800587 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400588 usb_unbind_interface(dev);
589 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800590 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400591 }
Alan Stern36e56a32006-07-01 22:08:06 -0400592}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600593EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400594
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800595/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100596int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800597{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800598 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
599 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
600 return 0;
601
602 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
603 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
604 return 0;
605
606 /* No need to test id->bcdDevice_lo != 0, since 0 is never
607 greater than any unsigned number. */
608 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
609 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
610 return 0;
611
612 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
613 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
614 return 0;
615
616 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
617 (id->bDeviceClass != dev->descriptor.bDeviceClass))
618 return 0;
619
620 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800621 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800622 return 0;
623
624 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
625 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
626 return 0;
627
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100628 return 1;
629}
630
631/* returns 0 if no match, 1 if match */
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200632int usb_match_one_id_intf(struct usb_device *dev,
633 struct usb_host_interface *intf,
634 const struct usb_device_id *id)
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100635{
Bjørn Mork81df2d52012-05-18 21:27:43 +0200636 /* The interface class, subclass, protocol and number should never be
Alan Stern93c8bf42006-10-18 16:41:51 -0400637 * checked for a match if the device class is Vendor Specific,
638 * unless the match record specifies the Vendor ID. */
639 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
640 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
641 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
642 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
Bjørn Mork81df2d52012-05-18 21:27:43 +0200643 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
644 USB_DEVICE_ID_MATCH_INT_NUMBER)))
Alan Stern93c8bf42006-10-18 16:41:51 -0400645 return 0;
646
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800647 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
648 (id->bInterfaceClass != intf->desc.bInterfaceClass))
649 return 0;
650
651 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
652 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
653 return 0;
654
655 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
656 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
657 return 0;
658
Bjørn Mork81df2d52012-05-18 21:27:43 +0200659 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
660 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
661 return 0;
662
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800663 return 1;
664}
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200665
666/* returns 0 if no match, 1 if match */
667int usb_match_one_id(struct usb_interface *interface,
668 const struct usb_device_id *id)
669{
670 struct usb_host_interface *intf;
671 struct usb_device *dev;
672
673 /* proc_connectinfo in devio.c may call us with id == NULL. */
674 if (id == NULL)
675 return 0;
676
677 intf = interface->cur_altsetting;
678 dev = interface_to_usbdev(interface);
679
680 if (!usb_match_device(dev, id))
681 return 0;
682
683 return usb_match_one_id_intf(dev, intf, id);
684}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100685EXPORT_SYMBOL_GPL(usb_match_one_id);
686
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800687/**
688 * usb_match_id - find first usb_device_id matching device or interface
689 * @interface: the interface of interest
690 * @id: array of usb_device_id structures, terminated by zero entry
691 *
692 * usb_match_id searches an array of usb_device_id's and returns
693 * the first one matching the device or interface, or null.
694 * This is used when binding (or rebinding) a driver to an interface.
695 * Most USB device drivers will use this indirectly, through the usb core,
696 * but some layered driver frameworks use it directly.
697 * These device tables are exported with MODULE_DEVICE_TABLE, through
698 * modutils, to support the driver loading functionality of USB hotplugging.
699 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200700 * Return: The first matching usb_device_id, or %NULL.
701 *
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800702 * What Matches:
703 *
704 * The "match_flags" element in a usb_device_id controls which
705 * members are used. If the corresponding bit is set, the
706 * value in the device_id must match its corresponding member
707 * in the device or interface descriptor, or else the device_id
708 * does not match.
709 *
710 * "driver_info" is normally used only by device drivers,
711 * but you can create a wildcard "matches anything" usb_device_id
712 * as a driver's "modules.usbmap" entry if you provide an id with
713 * only a nonzero "driver_info" field. If you do this, the USB device
714 * driver's probe() routine should use additional intelligence to
715 * decide whether to bind to the specified interface.
716 *
717 * What Makes Good usb_device_id Tables:
718 *
719 * The match algorithm is very simple, so that intelligence in
720 * driver selection must come from smart driver id records.
721 * Unless you have good reasons to use another selection policy,
722 * provide match elements only in related groups, and order match
723 * specifiers from specific to general. Use the macros provided
724 * for that purpose if you can.
725 *
726 * The most specific match specifiers use device descriptor
727 * data. These are commonly used with product-specific matches;
728 * the USB_DEVICE macro lets you provide vendor and product IDs,
729 * and you can also match against ranges of product revisions.
730 * These are widely used for devices with application or vendor
731 * specific bDeviceClass values.
732 *
733 * Matches based on device class/subclass/protocol specifications
734 * are slightly more general; use the USB_DEVICE_INFO macro, or
735 * its siblings. These are used with single-function devices
736 * where bDeviceClass doesn't specify that each interface has
737 * its own class.
738 *
739 * Matches based on interface class/subclass/protocol are the
740 * most general; they let drivers bind to any interface on a
741 * multiple-function device. Use the USB_INTERFACE_INFO
742 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400743 * devices (as recorded in bInterfaceClass).
744 *
745 * Note that an entry created by USB_INTERFACE_INFO won't match
746 * any interface if the device class is set to Vendor-Specific.
747 * This is deliberate; according to the USB spec the meanings of
748 * the interface class/subclass/protocol for these devices are also
749 * vendor-specific, and hence matching against a standard product
750 * class wouldn't work anyway. If you really want to use an
751 * interface-based match for such a device, create a match record
752 * that also specifies the vendor ID. (Unforunately there isn't a
753 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800754 *
755 * Within those groups, remember that not all combinations are
756 * meaningful. For example, don't give a product version range
757 * without vendor and product IDs; or specify a protocol without
758 * its associated class and subclass.
759 */
760const struct usb_device_id *usb_match_id(struct usb_interface *interface,
761 const struct usb_device_id *id)
762{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800763 /* proc_connectinfo in devio.c may call us with id == NULL. */
764 if (id == NULL)
765 return NULL;
766
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800767 /* It is important to check that id->driver_info is nonzero,
768 since an entry that is all zeroes except for a nonzero
769 id->driver_info is the way to create an entry that
770 indicates that the driver want to examine every
771 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800772 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
773 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800774 if (usb_match_one_id(interface, id))
775 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800776 }
777
778 return NULL;
779}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600780EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800781
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100782static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800783{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400784 /* devices and interfaces are handled separately */
785 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800786
Alan Stern8bb54ab2006-07-01 22:08:49 -0400787 /* interface drivers never match devices */
788 if (!is_usb_device_driver(drv))
789 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800790
Alan Stern8bb54ab2006-07-01 22:08:49 -0400791 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800792 return 1;
793
Kay Sievers55129662009-05-04 19:48:32 +0200794 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400795 struct usb_interface *intf;
796 struct usb_driver *usb_drv;
797 const struct usb_device_id *id;
798
799 /* device drivers never match interfaces */
800 if (is_usb_device_driver(drv))
801 return 0;
802
803 intf = to_usb_interface(dev);
804 usb_drv = to_usb_driver(drv);
805
806 id = usb_match_id(intf, usb_drv->id_table);
807 if (id)
808 return 1;
809
810 id = usb_match_dynamic_id(intf, usb_drv);
811 if (id)
812 return 1;
813 }
814
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800815 return 0;
816}
817
Kay Sievers7eff2e72007-08-14 15:15:12 +0200818static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400819{
Alan Stern36e56a32006-07-01 22:08:06 -0400820 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400821
Kay Sievers55129662009-05-04 19:48:32 +0200822 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400823 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200824 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100825 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200826
Alan Stern8bb54ab2006-07-01 22:08:49 -0400827 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200828 } else {
829 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400830 }
Alan Stern36e56a32006-07-01 22:08:06 -0400831
832 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500833 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200834 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400835 return -ENODEV;
836 }
837 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200838 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400839 return -ENODEV;
840 }
841
Alan Stern36e56a32006-07-01 22:08:06 -0400842 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200843 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400844 le16_to_cpu(usb_dev->descriptor.idVendor),
845 le16_to_cpu(usb_dev->descriptor.idProduct),
846 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
847 return -ENOMEM;
848
849 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200850 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400851 usb_dev->descriptor.bDeviceClass,
852 usb_dev->descriptor.bDeviceSubClass,
853 usb_dev->descriptor.bDeviceProtocol))
854 return -ENOMEM;
855
Alan Stern36e56a32006-07-01 22:08:06 -0400856 return 0;
857}
858
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800859/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400860 * usb_register_device_driver - register a USB device (not interface) driver
861 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800862 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800863 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400864 * Registers a USB device driver with the USB core. The list of
865 * unattached devices will be rescanned whenever a new driver is
866 * added, allowing the new driver to attach to any recognized devices.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200867 *
868 * Return: A negative error code on failure and 0 on success.
Alan Stern8bb54ab2006-07-01 22:08:49 -0400869 */
870int usb_register_device_driver(struct usb_device_driver *new_udriver,
871 struct module *owner)
872{
873 int retval = 0;
874
875 if (usb_disabled())
876 return -ENODEV;
877
878 new_udriver->drvwrap.for_devices = 1;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100879 new_udriver->drvwrap.driver.name = new_udriver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400880 new_udriver->drvwrap.driver.bus = &usb_bus_type;
881 new_udriver->drvwrap.driver.probe = usb_probe_device;
882 new_udriver->drvwrap.driver.remove = usb_unbind_device;
883 new_udriver->drvwrap.driver.owner = owner;
884
885 retval = driver_register(&new_udriver->drvwrap.driver);
886
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700887 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400888 pr_info("%s: registered new device driver %s\n",
889 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700890 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400891 printk(KERN_ERR "%s: error %d registering device "
892 " driver %s\n",
893 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400894
895 return retval;
896}
897EXPORT_SYMBOL_GPL(usb_register_device_driver);
898
899/**
900 * usb_deregister_device_driver - unregister a USB device (not interface) driver
901 * @udriver: USB operations of the device driver to unregister
902 * Context: must be able to sleep
903 *
904 * Unlinks the specified driver from the internal USB driver list.
905 */
906void usb_deregister_device_driver(struct usb_device_driver *udriver)
907{
908 pr_info("%s: deregistering device driver %s\n",
909 usbcore_name, udriver->name);
910
911 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400912}
913EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
914
915/**
916 * usb_register_driver - register a USB interface driver
917 * @new_driver: USB operations for the interface driver
918 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800919 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400920 *
921 * Registers a USB interface driver with the USB core. The list of
922 * unattached interfaces will be rescanned whenever a new driver is
923 * added, allowing the new driver to attach to any recognized interfaces.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200924 *
925 * Return: A negative error code on failure and 0 on success.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800926 *
927 * NOTE: if you want your driver to use the USB major number, you must call
928 * usb_register_dev() to enable that functionality. This function no longer
929 * takes care of that.
930 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800931int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
932 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800933{
934 int retval = 0;
935
936 if (usb_disabled())
937 return -ENODEV;
938
Alan Stern8bb54ab2006-07-01 22:08:49 -0400939 new_driver->drvwrap.for_devices = 0;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100940 new_driver->drvwrap.driver.name = new_driver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400941 new_driver->drvwrap.driver.bus = &usb_bus_type;
942 new_driver->drvwrap.driver.probe = usb_probe_interface;
943 new_driver->drvwrap.driver.remove = usb_unbind_interface;
944 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800945 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800946 spin_lock_init(&new_driver->dynids.lock);
947 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800948
Alan Stern8bb54ab2006-07-01 22:08:49 -0400949 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800950 if (retval)
951 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800952
Alan Sterned283e92012-01-24 14:35:13 -0500953 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800954 if (retval)
955 goto out_newid;
956
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800957 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800958 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800959
960out:
961 return retval;
962
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800963out_newid:
964 driver_unregister(&new_driver->drvwrap.driver);
965
966 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400967 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800968 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800969 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800970}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600971EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800972
973/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400974 * usb_deregister - unregister a USB interface driver
975 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800976 * Context: must be able to sleep
977 *
978 * Unlinks the specified driver from the internal USB driver list.
979 *
980 * NOTE: If you called usb_register_dev(), you still need to call
981 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
982 * this * call will no longer do it for you.
983 */
984void usb_deregister(struct usb_driver *driver)
985{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400986 pr_info("%s: deregistering interface driver %s\n",
987 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800988
Alan Sterned283e92012-01-24 14:35:13 -0500989 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400990 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -0500991 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800992}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600993EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400994
Alan Stern78d9a482008-06-23 16:00:40 -0400995/* Forced unbinding of a USB interface driver, either because
996 * it doesn't support pre_reset/post_reset/reset_resume or
997 * because it doesn't support suspend/resume.
998 *
Alan Stern6aec0442014-03-12 11:30:38 -0400999 * The caller must hold @intf's device's lock, but not @intf's lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001000 */
1001void usb_forced_unbind_intf(struct usb_interface *intf)
1002{
1003 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
1004
1005 dev_dbg(&intf->dev, "forced unbind\n");
1006 usb_driver_release_interface(driver, intf);
1007
1008 /* Mark the interface for later rebinding */
1009 intf->needs_binding = 1;
1010}
1011
Alan Stern6aec0442014-03-12 11:30:38 -04001012/*
1013 * Unbind drivers for @udev's marked interfaces. These interfaces have
1014 * the needs_binding flag set, for example by usb_resume_interface().
1015 *
1016 * The caller must hold @udev's device lock.
1017 */
1018static void unbind_marked_interfaces(struct usb_device *udev)
1019{
1020 struct usb_host_config *config;
1021 int i;
1022 struct usb_interface *intf;
1023
1024 config = udev->actconfig;
1025 if (config) {
1026 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1027 intf = config->interface[i];
1028 if (intf->dev.driver && intf->needs_binding)
1029 usb_forced_unbind_intf(intf);
1030 }
1031 }
1032}
1033
Alan Stern78d9a482008-06-23 16:00:40 -04001034/* Delayed forced unbinding of a USB interface driver and scan
1035 * for rebinding.
1036 *
Alan Stern6aec0442014-03-12 11:30:38 -04001037 * The caller must hold @intf's device's lock, but not @intf's lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001038 *
Alan Stern5096aed2008-08-12 14:34:14 -04001039 * Note: Rebinds will be skipped if a system sleep transition is in
1040 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -04001041 */
Alan Stern6aec0442014-03-12 11:30:38 -04001042static void usb_rebind_intf(struct usb_interface *intf)
Alan Stern78d9a482008-06-23 16:00:40 -04001043{
1044 int rc;
1045
1046 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +01001047 if (intf->dev.driver)
1048 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001049
1050 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +02001051 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -04001052 intf->needs_binding = 0;
1053 rc = device_attach(&intf->dev);
1054 if (rc < 0)
1055 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1056 }
Alan Stern78d9a482008-06-23 16:00:40 -04001057}
1058
Alan Stern6aec0442014-03-12 11:30:38 -04001059/*
1060 * Rebind drivers to @udev's marked interfaces. These interfaces have
1061 * the needs_binding flag set.
1062 *
1063 * The caller must hold @udev's device lock.
1064 */
1065static void rebind_marked_interfaces(struct usb_device *udev)
1066{
1067 struct usb_host_config *config;
1068 int i;
1069 struct usb_interface *intf;
1070
1071 config = udev->actconfig;
1072 if (config) {
1073 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1074 intf = config->interface[i];
1075 if (intf->needs_binding)
1076 usb_rebind_intf(intf);
1077 }
1078 }
1079}
1080
1081/*
1082 * Unbind all of @udev's marked interfaces and then rebind all of them.
1083 * This ordering is necessary because some drivers claim several interfaces
1084 * when they are first probed.
1085 *
1086 * The caller must hold @udev's device lock.
1087 */
1088void usb_unbind_and_rebind_marked_interfaces(struct usb_device *udev)
1089{
1090 unbind_marked_interfaces(udev);
1091 rebind_marked_interfaces(udev);
1092}
1093
Alan Stern9ff784332008-08-07 13:04:51 -04001094#ifdef CONFIG_PM
1095
Oliver Neukum14931382012-01-05 15:39:57 +01001096/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1097 * There is no check for reset_resume here because it can be determined
1098 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001099 *
1100 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001101 */
Oliver Neukum14931382012-01-05 15:39:57 +01001102static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001103{
1104 struct usb_host_config *config;
1105 int i;
1106 struct usb_interface *intf;
1107 struct usb_driver *drv;
1108
1109 config = udev->actconfig;
1110 if (config) {
1111 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1112 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001113
1114 if (intf->dev.driver) {
1115 drv = to_usb_driver(intf->dev.driver);
1116 if (!drv->suspend || !drv->resume)
1117 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001118 }
1119 }
1120 }
1121}
1122
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001123static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001124{
Alan Stern782da722006-07-01 22:09:35 -04001125 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001126 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001127
Alan Stern114b3682006-07-01 22:13:04 -04001128 if (udev->state == USB_STATE_NOTATTACHED ||
1129 udev->state == USB_STATE_SUSPENDED)
1130 goto done;
1131
Alan Sternb6f64362007-05-04 11:51:54 -04001132 /* For devices that don't have a driver, we do a generic suspend. */
1133 if (udev->dev.driver)
1134 udriver = to_usb_device_driver(udev->dev.driver);
1135 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001136 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001137 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001138 }
Alan Stern2bf40862006-07-01 22:12:19 -04001139 status = udriver->suspend(udev, msg);
1140
Alan Stern20dfdad2007-05-22 11:50:17 -04001141 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001142 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001143 return status;
Alan Stern1cc8a25d2006-07-01 22:10:15 -04001144}
Alan Stern36e56a32006-07-01 22:08:06 -04001145
Alan Stern65bfd292008-11-25 16:39:18 -05001146static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a25d2006-07-01 22:10:15 -04001147{
1148 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001149 int status = 0;
Alan Stern1cc8a25d2006-07-01 22:10:15 -04001150
Alan Stern0458d5b2007-05-04 11:52:20 -04001151 if (udev->state == USB_STATE_NOTATTACHED)
1152 goto done;
Alan Stern1cc8a25d2006-07-01 22:10:15 -04001153
Alan Stern1c5df7e2006-07-01 22:13:50 -04001154 /* Can't resume it if it doesn't have a driver. */
1155 if (udev->dev.driver == NULL) {
1156 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001157 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001158 }
1159
Alan Stern6d19c002010-02-12 12:21:11 +01001160 /* Non-root devices on a full/low-speed bus must wait for their
1161 * companion high-speed root hub, in case a handoff is needed.
1162 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001163 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001164 device_pm_wait_for_dev(&udev->dev,
1165 &udev->bus->hs_companion->root_hub->dev);
1166
Alan Stern6bc6cff2007-05-04 11:53:03 -04001167 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1168 udev->reset_resume = 1;
1169
Alan Stern1cc8a25d2006-07-01 22:10:15 -04001170 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001171 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001172
Alan Stern20dfdad2007-05-22 11:50:17 -04001173 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001174 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001175 return status;
Alan Stern1cc8a25d2006-07-01 22:10:15 -04001176}
1177
Alan Stern65605ae2008-08-12 14:33:27 -04001178static int usb_suspend_interface(struct usb_device *udev,
1179 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a25d2006-07-01 22:10:15 -04001180{
1181 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001182 int status = 0;
Alan Stern1cc8a25d2006-07-01 22:10:15 -04001183
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001184 if (udev->state == USB_STATE_NOTATTACHED ||
1185 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001186 goto done;
Alan Stern1cc8a25d2006-07-01 22:10:15 -04001187 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001188
Oliver Neukume78832c2012-01-02 15:11:48 +01001189 /* at this time we know the driver supports suspend */
1190 status = driver->suspend(intf, msg);
1191 if (status && !PMSG_IS_AUTO(msg))
1192 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001193
Alan Stern20dfdad2007-05-22 11:50:17 -04001194 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001195 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001196 return status;
1197}
1198
Alan Stern65605ae2008-08-12 14:33:27 -04001199static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001200 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001201{
Alan Stern1cc8a25d2006-07-01 22:10:15 -04001202 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001203 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001204
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001205 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001206 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001207
Alan Stern645daaa2006-08-30 15:47:02 -04001208 /* Don't let autoresume interfere with unbinding */
1209 if (intf->condition == USB_INTERFACE_UNBINDING)
1210 goto done;
1211
Alan Stern1c5df7e2006-07-01 22:13:50 -04001212 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001213 if (intf->condition == USB_INTERFACE_UNBOUND) {
1214
1215 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001216 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001217 usb_set_interface(udev, intf->altsetting[0].
1218 desc.bInterfaceNumber, 0);
1219 intf->needs_altsetting0 = 0;
1220 }
Alan Stern2bf40862006-07-01 22:12:19 -04001221 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001222 }
Alan Stern78d9a482008-06-23 16:00:40 -04001223
1224 /* Don't resume if the interface is marked for rebinding */
1225 if (intf->needs_binding)
1226 goto done;
Alan Stern1cc8a25d2006-07-01 22:10:15 -04001227 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001228
Alan Sternf07600c2007-05-30 15:38:16 -04001229 if (reset_resume) {
1230 if (driver->reset_resume) {
1231 status = driver->reset_resume(intf);
1232 if (status)
1233 dev_err(&intf->dev, "%s error %d\n",
1234 "reset_resume", status);
1235 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001236 intf->needs_binding = 1;
Alan Stern0a56b4f2013-10-18 11:17:21 -04001237 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1238 driver->name);
Alan Sternf07600c2007-05-30 15:38:16 -04001239 }
1240 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001241 status = driver->resume(intf);
1242 if (status)
1243 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001244 }
Alan Stern2bf40862006-07-01 22:12:19 -04001245
1246done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001247 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001248
Alan Stern78d9a482008-06-23 16:00:40 -04001249 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001250 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001251}
1252
Alan Stern645daaa2006-08-30 15:47:02 -04001253/**
1254 * usb_suspend_both - suspend a USB device and its interfaces
1255 * @udev: the usb_device to suspend
1256 * @msg: Power Management message describing this state transition
1257 *
1258 * This is the central routine for suspending USB devices. It calls the
1259 * suspend methods for all the interface drivers in @udev and then calls
Ming Lei303f0842013-03-15 12:08:53 +08001260 * the suspend method for @udev itself. When the routine is called in
1261 * autosuspend, if an error occurs at any stage, all the interfaces
1262 * which were suspended are resumed so that they remain in the same
1263 * state as the device, but when called from system sleep, all error
1264 * from suspend methods of interfaces and the non-root-hub device itself
1265 * are simply ignored, so all suspended interfaces are only resumed
1266 * to the device's state when @udev is root-hub and its suspend method
1267 * returns failure.
Alan Stern645daaa2006-08-30 15:47:02 -04001268 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001269 * Autosuspend requests originating from a child device or an interface
1270 * driver may be made without the protection of @udev's device lock, but
1271 * all other suspend calls will hold the lock. Usbcore will insure that
1272 * method calls do not arrive during bind, unbind, or reset operations.
1273 * However drivers must be prepared to handle suspend calls arriving at
1274 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001275 *
1276 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001277 *
1278 * Return: 0 if the suspend succeeded.
Alan Stern645daaa2006-08-30 15:47:02 -04001279 */
Alan Stern718efa62007-03-09 15:41:13 -05001280static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001281{
1282 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001283 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001284 struct usb_interface *intf;
1285
Alan Stern19410442007-03-27 13:33:59 -04001286 if (udev->state == USB_STATE_NOTATTACHED ||
1287 udev->state == USB_STATE_SUSPENDED)
1288 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001289
Alan Stern645daaa2006-08-30 15:47:02 -04001290 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001291 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001292 n = udev->actconfig->desc.bNumInterfaces;
1293 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001294 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001295 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001296
1297 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001298 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001299 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001300 if (status != 0)
1301 break;
1302 }
1303 }
Alan Stern0af212b2011-06-15 16:27:43 -04001304 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001305 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001306
Alan Sterncd4376e2012-03-28 15:56:17 -04001307 /*
1308 * Ignore errors from non-root-hub devices during
1309 * system sleep transitions. For the most part,
1310 * these devices should go to low power anyway when
1311 * the entire bus is suspended.
1312 */
1313 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001314 status = 0;
1315 }
1316
Alan Sterna8e7c562006-07-01 22:11:02 -04001317 /* If the suspend failed, resume interfaces that did get suspended */
1318 if (status != 0) {
Chen Gang505bdbc2013-04-01 13:04:08 +08001319 if (udev->actconfig) {
1320 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1321 while (++i < n) {
1322 intf = udev->actconfig->interface[i];
1323 usb_resume_interface(udev, intf, msg, 0);
1324 }
Alan Sterna8e7c562006-07-01 22:11:02 -04001325 }
Alan Stern645daaa2006-08-30 15:47:02 -04001326
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001327 /* If the suspend succeeded then prevent any more URB submissions
1328 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001329 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001330 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001331 udev->can_submit = 0;
1332 for (i = 0; i < 16; ++i) {
1333 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1334 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1335 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001336 }
Alan Stern645daaa2006-08-30 15:47:02 -04001337
Alan Stern19410442007-03-27 13:33:59 -04001338 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001339 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001340 return status;
1341}
1342
Alan Stern645daaa2006-08-30 15:47:02 -04001343/**
1344 * usb_resume_both - resume a USB device and its interfaces
1345 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001346 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001347 *
1348 * This is the central routine for resuming USB devices. It calls the
1349 * the resume method for @udev and then calls the resume methods for all
1350 * the interface drivers in @udev.
1351 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001352 * Autoresume requests originating from a child device or an interface
1353 * driver may be made without the protection of @udev's device lock, but
1354 * all other resume calls will hold the lock. Usbcore will insure that
1355 * method calls do not arrive during bind, unbind, or reset operations.
1356 * However drivers must be prepared to handle resume calls arriving at
1357 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001358 *
1359 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001360 *
1361 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001362 */
Alan Stern65bfd292008-11-25 16:39:18 -05001363static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001364{
Alan Stern645daaa2006-08-30 15:47:02 -04001365 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001366 int i;
1367 struct usb_interface *intf;
1368
Alan Stern19410442007-03-27 13:33:59 -04001369 if (udev->state == USB_STATE_NOTATTACHED) {
1370 status = -ENODEV;
1371 goto done;
1372 }
Alan Stern6840d252007-09-10 11:34:26 -04001373 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001374
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001375 /* Resume the device */
1376 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001377 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001378
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001379 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001380 if (status == 0 && udev->actconfig) {
1381 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1382 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001383 usb_resume_interface(udev, intf, msg,
1384 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001385 }
1386 }
Alan Sternc08512c2010-11-15 15:57:58 -05001387 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001388
Alan Stern19410442007-03-27 13:33:59 -04001389 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001390 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001391 if (!status)
1392 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001393 return status;
1394}
1395
Alan Stern5f677f12010-04-02 13:20:11 -04001396static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1397{
Alan Stern48826622010-06-22 16:14:48 -04001398 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001399
1400 /* Remote wakeup is needed only when we actually go to sleep.
1401 * For things like FREEZE and QUIESCE, if the device is already
1402 * autosuspended then its current wakeup setting is okay.
1403 */
1404 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1405 if (udev->state != USB_STATE_SUSPENDED)
1406 udev->do_remote_wakeup = 0;
1407 return;
1408 }
1409
Alan Stern48826622010-06-22 16:14:48 -04001410 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001411 * actually want it.
1412 */
Alan Stern48826622010-06-22 16:14:48 -04001413 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001414
1415 /* If the device is autosuspended with the wrong wakeup setting,
1416 * autoresume now so the setting can be changed.
1417 */
1418 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1419 pm_runtime_resume(&udev->dev);
1420 udev->do_remote_wakeup = w;
1421}
1422
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001423/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001424int usb_suspend(struct device *dev, pm_message_t msg)
1425{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001426 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001427
Oliver Neukum14931382012-01-05 15:39:57 +01001428 unbind_no_pm_drivers_interfaces(udev);
1429
1430 /* From now on we are sure all drivers support suspend/resume
1431 * but not necessarily reset_resume()
1432 * so we may still need to unbind and rebind upon resume
1433 */
Alan Stern5f677f12010-04-02 13:20:11 -04001434 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001435 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001436}
1437
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001438/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001439int usb_resume_complete(struct device *dev)
1440{
1441 struct usb_device *udev = to_usb_device(dev);
1442
1443 /* For PM complete calls, all we do is rebind interfaces
1444 * whose needs_binding flag is set
1445 */
1446 if (udev->state != USB_STATE_NOTATTACHED)
Alan Stern6aec0442014-03-12 11:30:38 -04001447 rebind_marked_interfaces(udev);
Oliver Neukum98d9a822012-01-11 08:38:35 +01001448 return 0;
1449}
1450
1451/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001452int usb_resume(struct device *dev, pm_message_t msg)
1453{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001454 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001455 int status;
1456
Oliver Neukum98d9a822012-01-11 08:38:35 +01001457 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001458 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001459 * Unbind the interfaces that will need rebinding later,
1460 * because they fail to support reset_resume.
1461 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001462 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001463 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001464 status = usb_resume_both(udev, msg);
1465 if (status == 0) {
1466 pm_runtime_disable(dev);
1467 pm_runtime_set_active(dev);
1468 pm_runtime_enable(dev);
Alan Stern6aec0442014-03-12 11:30:38 -04001469 unbind_marked_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001470 }
Alan Stern0c590e22010-01-08 12:57:14 -05001471
1472 /* Avoid PM error messages for devices disconnected while suspended
1473 * as we'll display regular disconnect messages just a bit later.
1474 */
Peter Chen7491f132010-09-27 16:43:25 +08001475 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001476 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001477 return status;
1478}
1479
Alan Stern088f7fe2010-01-08 12:56:54 -05001480/**
1481 * usb_enable_autosuspend - allow a USB device to be autosuspended
1482 * @udev: the USB device which may be autosuspended
1483 *
1484 * This routine allows @udev to be autosuspended. An autosuspend won't
1485 * take place until the autosuspend_delay has elapsed and all the other
1486 * necessary conditions are satisfied.
1487 *
1488 * The caller must hold @udev's device lock.
1489 */
Alan Stern9e18c822010-04-02 13:22:09 -04001490void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001491{
Alan Stern9e18c822010-04-02 13:22:09 -04001492 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001493}
1494EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1495
1496/**
1497 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1498 * @udev: the USB device which may not be autosuspended
1499 *
1500 * This routine prevents @udev from being autosuspended and wakes it up
1501 * if it is already autosuspended.
1502 *
1503 * The caller must hold @udev's device lock.
1504 */
Alan Stern9e18c822010-04-02 13:22:09 -04001505void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001506{
Alan Stern9e18c822010-04-02 13:22:09 -04001507 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001508}
1509EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1510
Alan Stern645daaa2006-08-30 15:47:02 -04001511/**
1512 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001513 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001514 *
1515 * This routine should be called when a core subsystem is finished using
1516 * @udev and wants to allow it to autosuspend. Examples would be when
1517 * @udev's device file in usbfs is closed or after a configuration change.
1518 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001519 * @udev's usage counter is decremented; if it drops to 0 and all the
1520 * interfaces are inactive then a delayed autosuspend will be attempted.
1521 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001522 *
Alan Stern62e299e2010-01-08 12:56:19 -05001523 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001524 *
1525 * This routine can run only in process context.
1526 */
Alan Stern94fcda12006-11-20 11:38:46 -05001527void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001528{
Alan Stern94fcda12006-11-20 11:38:46 -05001529 int status;
1530
Ming Lei6ddf27c2010-11-15 15:57:30 -05001531 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001532 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001533 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1534 __func__, atomic_read(&udev->dev.power.usage_count),
1535 status);
Alan Stern19c26232007-02-20 15:03:32 -05001536}
1537
1538/**
Alan Stern645daaa2006-08-30 15:47:02 -04001539 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001540 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001541 *
1542 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001543 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001544 * occur until usb_autosuspend_device() is called. (Note that this will
1545 * not prevent suspend events originating in the PM core.) Examples would
1546 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001547 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001548 *
Alan Stern94fcda12006-11-20 11:38:46 -05001549 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1550 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001551 *
Alan Stern62e299e2010-01-08 12:56:19 -05001552 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001553 *
1554 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001555 *
1556 * Return: 0 on success. A negative error code otherwise.
Alan Stern645daaa2006-08-30 15:47:02 -04001557 */
Alan Stern94fcda12006-11-20 11:38:46 -05001558int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001559{
1560 int status;
1561
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001562 status = pm_runtime_get_sync(&udev->dev);
1563 if (status < 0)
1564 pm_runtime_put_sync(&udev->dev);
1565 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1566 __func__, atomic_read(&udev->dev.power.usage_count),
1567 status);
1568 if (status > 0)
1569 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001570 return status;
1571}
1572
Alan Stern645daaa2006-08-30 15:47:02 -04001573/**
1574 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001575 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001576 *
1577 * This routine should be called by an interface driver when it is
1578 * finished using @intf and wants to allow it to autosuspend. A typical
1579 * example would be a character-device driver when its device file is
1580 * closed.
1581 *
1582 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001583 * 0, a delayed autosuspend request for @intf's device is attempted. The
1584 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001585 *
Alan Stern645daaa2006-08-30 15:47:02 -04001586 * This routine can run only in process context.
1587 */
1588void usb_autopm_put_interface(struct usb_interface *intf)
1589{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001590 struct usb_device *udev = interface_to_usbdev(intf);
1591 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001592
Ming Lei6ddf27c2010-11-15 15:57:30 -05001593 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001594 atomic_dec(&intf->pm_usage_cnt);
1595 status = pm_runtime_put_sync(&intf->dev);
1596 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1597 __func__, atomic_read(&intf->dev.power.usage_count),
1598 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001599}
1600EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1601
1602/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001603 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1604 * @intf: the usb_interface whose counter should be decremented
1605 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001606 * This routine does much the same thing as usb_autopm_put_interface():
1607 * It decrements @intf's usage counter and schedules a delayed
1608 * autosuspend request if the counter is <= 0. The difference is that it
1609 * does not perform any synchronization; callers should hold a private
1610 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001611 *
1612 * Typically a driver would call this routine during an URB's completion
1613 * handler, if no more URBs were pending.
1614 *
1615 * This routine can run in atomic context.
1616 */
1617void usb_autopm_put_interface_async(struct usb_interface *intf)
1618{
1619 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001620 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001621
Ming Lei6ddf27c2010-11-15 15:57:30 -05001622 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001623 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001624 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001625 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1626 __func__, atomic_read(&intf->dev.power.usage_count),
1627 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001628}
1629EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1630
1631/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001632 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1633 * @intf: the usb_interface whose counter should be decremented
1634 *
1635 * This routine decrements @intf's usage counter but does not carry out an
1636 * autosuspend.
1637 *
1638 * This routine can run in atomic context.
1639 */
1640void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1641{
1642 struct usb_device *udev = interface_to_usbdev(intf);
1643
Ming Lei6ddf27c2010-11-15 15:57:30 -05001644 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001645 atomic_dec(&intf->pm_usage_cnt);
1646 pm_runtime_put_noidle(&intf->dev);
1647}
1648EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1649
1650/**
Alan Stern645daaa2006-08-30 15:47:02 -04001651 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001652 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001653 *
1654 * This routine should be called by an interface driver when it wants to
1655 * use @intf and needs to guarantee that it is not suspended. In addition,
1656 * the routine prevents @intf from being autosuspended subsequently. (Note
1657 * that this will not prevent suspend events originating in the PM core.)
1658 * This prevention will persist until usb_autopm_put_interface() is called
1659 * or @intf is unbound. A typical example would be a character-device
1660 * driver when its device file is opened.
1661 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001662 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1663 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001664 *
1665 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001666 *
1667 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001668 */
1669int usb_autopm_get_interface(struct usb_interface *intf)
1670{
Alan Sternaf4f7602006-10-30 17:06:45 -05001671 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001672
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001673 status = pm_runtime_get_sync(&intf->dev);
1674 if (status < 0)
1675 pm_runtime_put_sync(&intf->dev);
1676 else
1677 atomic_inc(&intf->pm_usage_cnt);
1678 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1679 __func__, atomic_read(&intf->dev.power.usage_count),
1680 status);
1681 if (status > 0)
1682 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001683 return status;
1684}
1685EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1686
Alan Stern692a1862006-10-30 17:07:51 -05001687/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001688 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1689 * @intf: the usb_interface whose counter should be incremented
1690 *
1691 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001692 * usb_autopm_get_interface(): It increments @intf's usage counter and
1693 * queues an autoresume request if the device is suspended. The
1694 * differences are that it does not perform any synchronization (callers
1695 * should hold a private lock and handle all synchronization issues
1696 * themselves), and it does not autoresume the device directly (it only
1697 * queues a request). After a successful call, the device may not yet be
1698 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001699 *
1700 * This routine can run in atomic context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001701 *
1702 * Return: 0 on success. A negative error code otherwise.
Alan Stern9ac39f22008-11-12 16:19:49 -05001703 */
1704int usb_autopm_get_interface_async(struct usb_interface *intf)
1705{
Ming Lei63defa72010-11-15 15:56:54 -05001706 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001707
Ming Lei63defa72010-11-15 15:56:54 -05001708 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001709 if (status < 0 && status != -EINPROGRESS)
1710 pm_runtime_put_noidle(&intf->dev);
1711 else
Alan Sternccf5b802009-06-29 11:00:01 -04001712 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001713 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1714 __func__, atomic_read(&intf->dev.power.usage_count),
1715 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001716 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001717 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001718 return status;
1719}
1720EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1721
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001722/**
1723 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1724 * @intf: the usb_interface whose counter should be incremented
1725 *
1726 * This routine increments @intf's usage counter but does not carry out an
1727 * autoresume.
1728 *
1729 * This routine can run in atomic context.
1730 */
1731void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1732{
1733 struct usb_device *udev = interface_to_usbdev(intf);
1734
Ming Lei6ddf27c2010-11-15 15:57:30 -05001735 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001736 atomic_inc(&intf->pm_usage_cnt);
1737 pm_runtime_get_noresume(&intf->dev);
1738}
1739EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1740
1741/* Internal routine to check whether we may autosuspend a device. */
1742static int autosuspend_check(struct usb_device *udev)
1743{
Alan Stern7560d32e2010-04-02 13:18:50 -04001744 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001745 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001746
1747 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1748 * any interface drivers require remote wakeup but it isn't available.
1749 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001750 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001751 if (udev->actconfig) {
1752 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1753 intf = udev->actconfig->interface[i];
1754
1755 /* We don't need to check interfaces that are
1756 * disabled for runtime PM. Either they are unbound
1757 * or else their drivers don't support autosuspend
1758 * and so they are permanently active.
1759 */
1760 if (intf->dev.power.disable_depth)
1761 continue;
1762 if (atomic_read(&intf->dev.power.usage_count) > 0)
1763 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001764 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001765
1766 /* Don't allow autosuspend if the device will need
1767 * a reset-resume and any of its interface drivers
1768 * doesn't include support or needs remote wakeup.
1769 */
1770 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1771 struct usb_driver *driver;
1772
1773 driver = to_usb_driver(intf->dev.driver);
1774 if (!driver->reset_resume ||
1775 intf->needs_remote_wakeup)
1776 return -EOPNOTSUPP;
1777 }
1778 }
1779 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001780 if (w && !device_can_wakeup(&udev->dev)) {
1781 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1782 return -EOPNOTSUPP;
1783 }
Alan Stern074f9dd2015-01-29 15:05:04 -05001784
1785 /*
1786 * If the device is a direct child of the root hub and the HCD
1787 * doesn't handle wakeup requests, don't allow autosuspend when
1788 * wakeup is needed.
1789 */
1790 if (w && udev->parent == udev->bus->root_hub &&
1791 bus_to_hcd(udev->bus)->cant_recv_wakeups) {
1792 dev_dbg(&udev->dev, "HCD doesn't handle wakeup requests\n");
1793 return -EOPNOTSUPP;
1794 }
1795
Alan Stern7560d32e2010-04-02 13:18:50 -04001796 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001797 return 0;
1798}
1799
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001800int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001801{
Ming Lei63defa72010-11-15 15:56:54 -05001802 struct usb_device *udev = to_usb_device(dev);
1803 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001804
1805 /* A USB device can be suspended if it passes the various autosuspend
1806 * checks. Runtime suspend for a USB device means suspending all the
1807 * interfaces and then the device itself.
1808 */
Ming Lei63defa72010-11-15 15:56:54 -05001809 if (autosuspend_check(udev) != 0)
1810 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001811
Ming Lei63defa72010-11-15 15:56:54 -05001812 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001813
1814 /* Allow a retry if autosuspend failed temporarily */
1815 if (status == -EAGAIN || status == -EBUSY)
1816 usb_mark_last_busy(udev);
1817
Alan Stern8ef42dd2014-05-23 10:45:54 -04001818 /*
1819 * The PM core reacts badly unless the return code is 0,
1820 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error
1821 * (except for root hubs, because they don't suspend through
1822 * an upstream port like other USB devices).
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001823 */
Alan Stern8ef42dd2014-05-23 10:45:54 -04001824 if (status != 0 && udev->parent)
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001825 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001826 return status;
1827}
1828
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001829int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001830{
Ming Lei63defa72010-11-15 15:56:54 -05001831 struct usb_device *udev = to_usb_device(dev);
1832 int status;
1833
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001834 /* Runtime resume for a USB device means resuming both the device
1835 * and all its interfaces.
1836 */
Ming Lei63defa72010-11-15 15:56:54 -05001837 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001838 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001839}
1840
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001841int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001842{
Ming Lei63defa72010-11-15 15:56:54 -05001843 struct usb_device *udev = to_usb_device(dev);
1844
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001845 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001846 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001847 */
Ming Lei63defa72010-11-15 15:56:54 -05001848 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001849 pm_runtime_autosuspend(dev);
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001850 /* Tell the core not to suspend it, though. */
1851 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001852}
1853
Andiry Xu65580b432011-09-23 14:19:52 -07001854int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1855{
1856 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1857 int ret = -EPERM;
1858
Sarah Sharpde68bab2013-09-30 17:26:28 +03001859 if (enable && !udev->usb2_hw_lpm_allowed)
1860 return 0;
1861
Andiry Xu65580b432011-09-23 14:19:52 -07001862 if (hcd->driver->set_usb2_hw_lpm) {
1863 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1864 if (!ret)
1865 udev->usb2_hw_lpm_enabled = enable;
1866 }
1867
1868 return ret;
1869}
1870
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01001871#endif /* CONFIG_PM */
Alan Stern645daaa2006-08-30 15:47:02 -04001872
Alan Stern36e56a32006-07-01 22:08:06 -04001873struct bus_type usb_bus_type = {
1874 .name = "usb",
1875 .match = usb_device_match,
1876 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001877};