blob: 08283d40616c462af6ca4663592239a722dc8f6b [file] [log] [blame]
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -08001/*
2 * drivers/usb/driver.c - most of the driver model stuff for usb
3 *
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
5 *
6 * based on drivers/usb/usb.c which had the following copyrights:
7 * (C) Copyright Linus Torvalds 1999
8 * (C) Copyright Johannes Erdfelt 1999-2001
9 * (C) Copyright Andreas Gal 1999
10 * (C) Copyright Gregory P. Smith 1999
11 * (C) Copyright Deti Fliegl 1999 (new USB architecture)
12 * (C) Copyright Randy Dunlap 2000
13 * (C) Copyright David Brownell 2000-2004
14 * (C) Copyright Yggdrasil Computing, Inc. 2000
15 * (usb_device_id matching changes by Adam J. Richter)
16 * (C) Copyright Greg Kroah-Hartman 2002-2003
17 *
18 * NOTE! This is not actually a driver at all, rather this is
19 * just a collection of helper routines that implement the
Alan Stern36e56a32006-07-01 22:08:06 -040020 * matching, probing, releasing, suspending and resuming for
21 * real drivers.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080022 *
23 */
24
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080025#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Paul Gortmakerf940fcd2011-05-27 09:56:31 -040027#include <linux/export.h>
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080028#include <linux/usb.h>
Alan Stern6bc6cff2007-05-04 11:53:03 -040029#include <linux/usb/quirks.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020030#include <linux/usb/hcd.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020031
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080032#include "usb.h"
33
Alan Stern20dfdad2007-05-22 11:50:17 -040034
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080035/*
36 * Adds a new dynamic USBdevice ID to this driver,
37 * and cause the driver to probe for all devices again.
38 */
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010039ssize_t usb_store_new_id(struct usb_dynids *dynids,
Wolfram Sang2fc82c22014-01-10 19:36:42 +010040 const struct usb_device_id *id_table,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010041 struct device_driver *driver,
42 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080043{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080044 struct usb_dynid *dynid;
45 u32 idVendor = 0;
46 u32 idProduct = 0;
Josua Dietzeff231db2011-10-23 14:22:29 +020047 unsigned int bInterfaceClass = 0;
Wolfram Sang2fc82c22014-01-10 19:36:42 +010048 u32 refVendor, refProduct;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080049 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070050 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080051
Wolfram Sang2fc82c22014-01-10 19:36:42 +010052 fields = sscanf(buf, "%x %x %x %x %x", &idVendor, &idProduct,
53 &bInterfaceClass, &refVendor, &refProduct);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080054 if (fields < 2)
55 return -EINVAL;
56
57 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
58 if (!dynid)
59 return -ENOMEM;
60
61 INIT_LIST_HEAD(&dynid->node);
62 dynid->id.idVendor = idVendor;
63 dynid->id.idProduct = idProduct;
64 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Wolfram Sangc63fe8f2014-01-10 19:36:41 +010065 if (fields > 2 && bInterfaceClass) {
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010066 if (bInterfaceClass > 255) {
67 retval = -EINVAL;
68 goto fail;
69 }
Wolfram Sangc63fe8f2014-01-10 19:36:41 +010070
Josua Dietzeff231db2011-10-23 14:22:29 +020071 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
72 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
73 }
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080074
Wolfram Sang2fc82c22014-01-10 19:36:42 +010075 if (fields > 4) {
76 const struct usb_device_id *id = id_table;
77
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010078 if (!id) {
79 retval = -ENODEV;
80 goto fail;
81 }
Wolfram Sang1b9fb312014-01-13 11:29:23 +010082
Wolfram Sang2fc82c22014-01-10 19:36:42 +010083 for (; id->match_flags; id++)
Wolfram Sang52a6966c2014-01-12 10:07:50 +010084 if (id->idVendor == refVendor && id->idProduct == refProduct)
Wolfram Sang2fc82c22014-01-10 19:36:42 +010085 break;
Wolfram Sang52a6966c2014-01-12 10:07:50 +010086
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010087 if (id->match_flags) {
Wolfram Sang52a6966c2014-01-12 10:07:50 +010088 dynid->id.driver_info = id->driver_info;
Christian Engelmayer7f196ca2014-01-28 22:22:27 +010089 } else {
90 retval = -ENODEV;
91 goto fail;
92 }
Wolfram Sang2fc82c22014-01-10 19:36:42 +010093 }
94
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010095 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020096 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010097 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080098
Alan Sterncef9bc52012-01-24 13:34:41 -050099 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800100
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700101 if (retval)
102 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800103 return count;
Christian Engelmayer7f196ca2014-01-28 22:22:27 +0100104
105fail:
106 kfree(dynid);
107 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800108}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100109EXPORT_SYMBOL_GPL(usb_store_new_id);
110
Bjørn Morkef206f32012-05-13 12:35:00 +0200111ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200112{
113 struct usb_dynid *dynid;
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200114 size_t count = 0;
115
Bjørn Morkef206f32012-05-13 12:35:00 +0200116 list_for_each_entry(dynid, &dynids->list, node)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200117 if (dynid->id.bInterfaceClass != 0)
118 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
119 dynid->id.idVendor, dynid->id.idProduct,
120 dynid->id.bInterfaceClass);
121 else
122 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
123 dynid->id.idVendor, dynid->id.idProduct);
124 return count;
125}
Bjørn Morkef206f32012-05-13 12:35:00 +0200126EXPORT_SYMBOL_GPL(usb_show_dynids);
127
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700128static ssize_t new_id_show(struct device_driver *driver, char *buf)
Bjørn Morkef206f32012-05-13 12:35:00 +0200129{
130 struct usb_driver *usb_drv = to_usb_driver(driver);
131
132 return usb_show_dynids(&usb_drv->dynids, buf);
133}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200134
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700135static ssize_t new_id_store(struct device_driver *driver,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100136 const char *buf, size_t count)
137{
138 struct usb_driver *usb_drv = to_usb_driver(driver);
139
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100140 return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100141}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700142static DRIVER_ATTR_RW(new_id);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800143
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700144/*
145 * Remove a USB device ID from this driver
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800146 */
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700147static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
148 size_t count)
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800149{
150 struct usb_dynid *dynid, *n;
151 struct usb_driver *usb_driver = to_usb_driver(driver);
Alan Coxac08de32012-09-17 11:55:23 +0100152 u32 idVendor;
153 u32 idProduct;
154 int fields;
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800155
156 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
157 if (fields < 2)
158 return -EINVAL;
159
160 spin_lock(&usb_driver->dynids.lock);
161 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
162 struct usb_device_id *id = &dynid->id;
163 if ((id->idVendor == idVendor) &&
164 (id->idProduct == idProduct)) {
165 list_del(&dynid->node);
166 kfree(dynid);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800167 break;
168 }
169 }
170 spin_unlock(&usb_driver->dynids.lock);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800171 return count;
172}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700173
174static ssize_t remove_id_show(struct device_driver *driver, char *buf)
175{
176 return new_id_show(driver, buf);
177}
178static DRIVER_ATTR_RW(remove_id);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800179
Alan Sterned283e92012-01-24 14:35:13 -0500180static int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800181{
182 int error = 0;
183
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800184 if (usb_drv->no_dynamic_id)
185 goto exit;
186
Alan Sterned283e92012-01-24 14:35:13 -0500187 if (usb_drv->probe != NULL) {
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800188 error = driver_create_file(&usb_drv->drvwrap.driver,
189 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500190 if (error == 0) {
191 error = driver_create_file(&usb_drv->drvwrap.driver,
192 &driver_attr_remove_id);
193 if (error)
194 driver_remove_file(&usb_drv->drvwrap.driver,
195 &driver_attr_new_id);
196 }
197 }
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800198exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800199 return error;
200}
201
Alan Sterned283e92012-01-24 14:35:13 -0500202static void usb_remove_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800203{
204 if (usb_drv->no_dynamic_id)
205 return;
206
Alan Sterned283e92012-01-24 14:35:13 -0500207 if (usb_drv->probe != NULL) {
208 driver_remove_file(&usb_drv->drvwrap.driver,
209 &driver_attr_remove_id);
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800210 driver_remove_file(&usb_drv->drvwrap.driver,
211 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500212 }
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800213}
214
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800215static void usb_free_dynids(struct usb_driver *usb_drv)
216{
217 struct usb_dynid *dynid, *n;
218
219 spin_lock(&usb_drv->dynids.lock);
220 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
221 list_del(&dynid->node);
222 kfree(dynid);
223 }
224 spin_unlock(&usb_drv->dynids.lock);
225}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800226
227static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
228 struct usb_driver *drv)
229{
230 struct usb_dynid *dynid;
231
232 spin_lock(&drv->dynids.lock);
233 list_for_each_entry(dynid, &drv->dynids.list, node) {
234 if (usb_match_one_id(intf, &dynid->id)) {
235 spin_unlock(&drv->dynids.lock);
236 return &dynid->id;
237 }
238 }
239 spin_unlock(&drv->dynids.lock);
240 return NULL;
241}
242
243
Alan Stern8bb54ab2006-07-01 22:08:49 -0400244/* called from driver core with dev locked */
245static int usb_probe_device(struct device *dev)
246{
247 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200248 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500249 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400250
Harvey Harrison441b62c2008-03-03 16:08:34 -0800251 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400252
Alan Stern8bb54ab2006-07-01 22:08:49 -0400253 /* TODO: Add real matching code */
254
Alan Stern645daaa2006-08-30 15:47:02 -0400255 /* The device should always appear to be in use
Masanari Iida02582e92012-08-22 19:11:26 +0900256 * unless the driver supports autosuspend.
Alan Stern645daaa2006-08-30 15:47:02 -0400257 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500258 if (!udriver->supports_autosuspend)
259 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400260
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500261 if (!error)
262 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400263 return error;
264}
265
266/* called from driver core with dev locked */
267static int usb_unbind_device(struct device *dev)
268{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500269 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400270 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
271
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500272 udriver->disconnect(udev);
273 if (!udriver->supports_autosuspend)
274 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400275 return 0;
276}
277
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800278/*
279 * Cancel any pending scheduled resets
280 *
281 * [see usb_queue_reset_device()]
282 *
283 * Called after unconfiguring / when releasing interfaces. See
284 * comments in __usb_queue_reset_device() regarding
285 * udev->reset_running.
286 */
287static void usb_cancel_queued_reset(struct usb_interface *iface)
288{
289 if (iface->reset_running == 0)
290 cancel_work_sync(&iface->reset_ws);
291}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400292
293/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800294static int usb_probe_interface(struct device *dev)
295{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400296 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200297 struct usb_interface *intf = to_usb_interface(dev);
298 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800299 const struct usb_device_id *id;
300 int error = -ENODEV;
Sarah Sharp83060952012-05-02 14:25:52 -0700301 int lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800302
Harvey Harrison441b62c2008-03-03 16:08:34 -0800303 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800304
Alan Stern78d9a482008-06-23 16:00:40 -0400305 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800306
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400307 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500308 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400309
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800310 if (udev->authorized == 0) {
311 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500312 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800313 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700314
Bjørn Mork31c6bf72014-01-11 02:04:00 +0100315 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800316 if (!id)
Bjørn Mork31c6bf72014-01-11 02:04:00 +0100317 id = usb_match_id(intf, driver->id_table);
Alan Stern0f3dda92010-01-08 12:56:04 -0500318 if (!id)
319 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800320
Alan Stern0f3dda92010-01-08 12:56:04 -0500321 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400322
Alan Stern0f3dda92010-01-08 12:56:04 -0500323 error = usb_autoresume_device(udev);
324 if (error)
325 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400326
Alan Stern0f3dda92010-01-08 12:56:04 -0500327 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400328
Alan Stern571dc792010-04-09 16:03:43 -0400329 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500330 * runtime-PM-enabled only if the driver has autosuspend support.
331 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500332 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500333 pm_runtime_set_active(dev);
334 pm_suspend_ignore_children(dev, false);
335 if (driver->supports_autosuspend)
336 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200337
Sarah Sharp83060952012-05-02 14:25:52 -0700338 /* If the new driver doesn't allow hub-initiated LPM, and we can't
339 * disable hub-initiated LPM, then fail the probe.
340 *
341 * Otherwise, leaving LPM enabled should be harmless, because the
342 * endpoint intervals should remain the same, and the U1/U2 timeouts
343 * should remain the same.
344 *
345 * If we need to install alt setting 0 before probe, or another alt
346 * setting during probe, that should also be fine. usb_set_interface()
347 * will attempt to disable LPM, and fail if it can't disable it.
348 */
349 lpm_disable_error = usb_unlocked_disable_lpm(udev);
350 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
351 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
352 __func__, driver->name);
353 error = lpm_disable_error;
354 goto err;
355 }
356
Alan Stern0f3dda92010-01-08 12:56:04 -0500357 /* Carry out a deferred switch to altsetting 0 */
358 if (intf->needs_altsetting0) {
359 error = usb_set_interface(udev, intf->altsetting[0].
360 desc.bInterfaceNumber, 0);
361 if (error < 0)
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200362 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500363 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800364 }
365
Alan Stern0f3dda92010-01-08 12:56:04 -0500366 error = driver->probe(intf, id);
367 if (error)
368 goto err;
369
370 intf->condition = USB_INTERFACE_BOUND;
Sarah Sharp83060952012-05-02 14:25:52 -0700371
372 /* If the LPM disable succeeded, balance the ref counts. */
373 if (!lpm_disable_error)
374 usb_unlocked_enable_lpm(udev);
375
Alan Stern0f3dda92010-01-08 12:56:04 -0500376 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800377 return error;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200378
Alan Stern0f3dda92010-01-08 12:56:04 -0500379 err:
Hans de Goedee714fad2012-05-22 11:36:59 +0200380 usb_set_intfdata(intf, NULL);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200381 intf->needs_remote_wakeup = 0;
382 intf->condition = USB_INTERFACE_UNBOUND;
383 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500384
Sarah Sharpd01f87c2012-10-04 09:53:43 -0700385 /* If the LPM disable succeeded, balance the ref counts. */
386 if (!lpm_disable_error)
387 usb_unlocked_enable_lpm(udev);
388
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500389 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400390 if (driver->supports_autosuspend)
391 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500392 pm_runtime_set_suspended(dev);
393
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200394 usb_autosuspend_device(udev);
395 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800396}
397
Alan Stern8bb54ab2006-07-01 22:08:49 -0400398/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800399static int usb_unbind_interface(struct device *dev)
400{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400401 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800402 struct usb_interface *intf = to_usb_interface(dev);
Hans de Goede6343e8b2013-10-09 17:19:26 +0200403 struct usb_host_endpoint *ep, **eps = NULL;
Alan Stern645daaa2006-08-30 15:47:02 -0400404 struct usb_device *udev;
Hans de Goede6343e8b2013-10-09 17:19:26 +0200405 int i, j, error, r, lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800406
407 intf->condition = USB_INTERFACE_UNBINDING;
408
Alan Stern645daaa2006-08-30 15:47:02 -0400409 /* Autoresume for set_interface call below */
410 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500411 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400412
Sarah Sharp83060952012-05-02 14:25:52 -0700413 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
414 * the driver is unbound. If LPM isn't disabled, that's fine because it
415 * wouldn't be enabled unless all the bound interfaces supported
416 * hub-initiated LPM.
417 */
418 lpm_disable_error = usb_unlocked_disable_lpm(udev);
419
Alan Stern9da82bd2008-05-08 11:54:37 -0400420 /* Terminate all URBs for this interface unless the driver
421 * supports "soft" unbinding.
422 */
423 if (!driver->soft_unbind)
Alan Sternddeac4e2009-01-15 17:03:33 -0500424 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800425
Alan Stern8bb54ab2006-07-01 22:08:49 -0400426 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800427 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800428
Hans de Goede6343e8b2013-10-09 17:19:26 +0200429 /* Free streams */
430 for (i = 0, j = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
431 ep = &intf->cur_altsetting->endpoint[i];
432 if (ep->streams == 0)
433 continue;
434 if (j == 0) {
435 eps = kmalloc(USB_MAXENDPOINTS * sizeof(void *),
436 GFP_KERNEL);
437 if (!eps) {
438 dev_warn(dev, "oom, leaking streams\n");
439 break;
440 }
441 }
442 eps[j++] = ep;
443 }
444 if (j) {
445 usb_free_streams(intf, eps, j, GFP_KERNEL);
446 kfree(eps);
447 }
448
Alan Stern55151d72008-08-12 14:33:59 -0400449 /* Reset other interface state.
450 * We cannot do a Set-Interface if the device is suspended or
451 * if it is prepared for a system sleep (since installing a new
452 * altsetting means creating new endpoint device entries).
453 * When either of these happens, defer the Set-Interface.
454 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500455 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
456 /* Already in altsetting 0 so skip Set-Interface.
457 * Just re-enable it without affecting the endpoint toggles.
458 */
459 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200460 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200461 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400462 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200463 if (r < 0)
464 intf->needs_altsetting0 = 1;
465 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400466 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200467 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800468 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400469
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800470 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400471 intf->needs_remote_wakeup = 0;
472
Sarah Sharp83060952012-05-02 14:25:52 -0700473 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
474 if (!lpm_disable_error)
475 usb_unlocked_enable_lpm(udev);
476
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500477 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400478 if (driver->supports_autosuspend)
479 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500480 pm_runtime_set_suspended(dev);
481
482 /* Undo any residual pm_autopm_get_interface_* calls */
483 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
484 usb_autopm_put_interface_no_suspend(intf);
485 atomic_set(&intf->pm_usage_cnt, 0);
486
Alan Stern645daaa2006-08-30 15:47:02 -0400487 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500488 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800489
490 return 0;
491}
492
Alan Stern36e56a32006-07-01 22:08:06 -0400493/**
494 * usb_driver_claim_interface - bind a driver to an interface
495 * @driver: the driver to be bound
496 * @iface: the interface to which it will be bound; must be in the
497 * usb device's active configuration
498 * @priv: driver data associated with that interface
499 *
500 * This is used by usb device drivers that need to claim more than one
501 * interface on a device when probing (audio and acm are current examples).
502 * No device driver should directly modify internal usb_interface or
503 * usb_device structure members.
504 *
505 * Few drivers should need to use this routine, since the most natural
506 * way to bind to an interface is to return the private data from
507 * the driver's probe() method.
508 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400509 * Callers must own the device lock, so driver probe() entries don't need
510 * extra locking, but other call contexts may need to explicitly claim that
511 * lock.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200512 *
513 * Return: 0 on success.
Alan Stern36e56a32006-07-01 22:08:06 -0400514 */
515int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800516 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400517{
518 struct device *dev = &iface->dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700519 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700520 int retval = 0;
Sarah Sharp83060952012-05-02 14:25:52 -0700521 int lpm_disable_error;
Alan Stern36e56a32006-07-01 22:08:06 -0400522
523 if (dev->driver)
524 return -EBUSY;
525
Sarah Sharp83060952012-05-02 14:25:52 -0700526 udev = interface_to_usbdev(iface);
527
Alan Stern8bb54ab2006-07-01 22:08:49 -0400528 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400529 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400530 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400531
Alan Stern36e56a32006-07-01 22:08:06 -0400532 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500533
Sarah Sharp83060952012-05-02 14:25:52 -0700534 /* Disable LPM until this driver is bound. */
535 lpm_disable_error = usb_unlocked_disable_lpm(udev);
536 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
537 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
538 __func__, driver->name);
539 return -ENOMEM;
540 }
541
Alan Stern89842ae2010-05-11 11:44:06 -0400542 /* Claimed interfaces are initially inactive (suspended) and
543 * runtime-PM-enabled, but only if the driver has autosuspend
544 * support. Otherwise they are marked active, to prevent the
545 * device from being autosuspended, but left disabled. In either
546 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500547 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500548 pm_suspend_ignore_children(dev, false);
549 if (driver->supports_autosuspend)
550 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400551 else
552 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400553
554 /* if interface was already added, bind now; else let
555 * the future device_add() bind it, bypassing probe()
556 */
557 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700558 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400559
Sarah Sharp83060952012-05-02 14:25:52 -0700560 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
561 if (!lpm_disable_error)
562 usb_unlocked_enable_lpm(udev);
563
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700564 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400565}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600566EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400567
568/**
569 * usb_driver_release_interface - unbind a driver from an interface
570 * @driver: the driver to be unbound
571 * @iface: the interface from which it will be unbound
572 *
573 * This can be used by drivers to release an interface without waiting
574 * for their disconnect() methods to be called. In typical cases this
575 * also causes the driver disconnect() method to be called.
576 *
577 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400578 * Callers must own the device lock, so driver disconnect() entries don't
579 * need extra locking, but other call contexts may need to explicitly claim
580 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400581 */
582void usb_driver_release_interface(struct usb_driver *driver,
583 struct usb_interface *iface)
584{
585 struct device *dev = &iface->dev;
586
587 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400588 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400589 return;
590
591 /* don't release from within disconnect() */
592 if (iface->condition != USB_INTERFACE_BOUND)
593 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400594 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400595
Alan Stern91f8d062009-04-16 15:35:09 -0400596 /* Release via the driver core only if the interface
597 * has already been registered
598 */
Alan Stern36e56a32006-07-01 22:08:06 -0400599 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400600 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800601 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800602 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400603 usb_unbind_interface(dev);
604 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800605 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400606 }
Alan Stern36e56a32006-07-01 22:08:06 -0400607}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600608EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400609
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800610/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100611int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800612{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800613 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
614 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
615 return 0;
616
617 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
618 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
619 return 0;
620
621 /* No need to test id->bcdDevice_lo != 0, since 0 is never
622 greater than any unsigned number. */
623 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
624 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
625 return 0;
626
627 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
628 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
629 return 0;
630
631 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
632 (id->bDeviceClass != dev->descriptor.bDeviceClass))
633 return 0;
634
635 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800636 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800637 return 0;
638
639 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
640 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
641 return 0;
642
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100643 return 1;
644}
645
646/* returns 0 if no match, 1 if match */
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200647int usb_match_one_id_intf(struct usb_device *dev,
648 struct usb_host_interface *intf,
649 const struct usb_device_id *id)
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100650{
Bjørn Mork81df2d52012-05-18 21:27:43 +0200651 /* The interface class, subclass, protocol and number should never be
Alan Stern93c8bf42006-10-18 16:41:51 -0400652 * checked for a match if the device class is Vendor Specific,
653 * unless the match record specifies the Vendor ID. */
654 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
655 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
656 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
657 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
Bjørn Mork81df2d52012-05-18 21:27:43 +0200658 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
659 USB_DEVICE_ID_MATCH_INT_NUMBER)))
Alan Stern93c8bf42006-10-18 16:41:51 -0400660 return 0;
661
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800662 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
663 (id->bInterfaceClass != intf->desc.bInterfaceClass))
664 return 0;
665
666 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
667 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
668 return 0;
669
670 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
671 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
672 return 0;
673
Bjørn Mork81df2d52012-05-18 21:27:43 +0200674 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
675 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
676 return 0;
677
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800678 return 1;
679}
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200680
681/* returns 0 if no match, 1 if match */
682int usb_match_one_id(struct usb_interface *interface,
683 const struct usb_device_id *id)
684{
685 struct usb_host_interface *intf;
686 struct usb_device *dev;
687
688 /* proc_connectinfo in devio.c may call us with id == NULL. */
689 if (id == NULL)
690 return 0;
691
692 intf = interface->cur_altsetting;
693 dev = interface_to_usbdev(interface);
694
695 if (!usb_match_device(dev, id))
696 return 0;
697
698 return usb_match_one_id_intf(dev, intf, id);
699}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100700EXPORT_SYMBOL_GPL(usb_match_one_id);
701
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800702/**
703 * usb_match_id - find first usb_device_id matching device or interface
704 * @interface: the interface of interest
705 * @id: array of usb_device_id structures, terminated by zero entry
706 *
707 * usb_match_id searches an array of usb_device_id's and returns
708 * the first one matching the device or interface, or null.
709 * This is used when binding (or rebinding) a driver to an interface.
710 * Most USB device drivers will use this indirectly, through the usb core,
711 * but some layered driver frameworks use it directly.
712 * These device tables are exported with MODULE_DEVICE_TABLE, through
713 * modutils, to support the driver loading functionality of USB hotplugging.
714 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200715 * Return: The first matching usb_device_id, or %NULL.
716 *
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800717 * What Matches:
718 *
719 * The "match_flags" element in a usb_device_id controls which
720 * members are used. If the corresponding bit is set, the
721 * value in the device_id must match its corresponding member
722 * in the device or interface descriptor, or else the device_id
723 * does not match.
724 *
725 * "driver_info" is normally used only by device drivers,
726 * but you can create a wildcard "matches anything" usb_device_id
727 * as a driver's "modules.usbmap" entry if you provide an id with
728 * only a nonzero "driver_info" field. If you do this, the USB device
729 * driver's probe() routine should use additional intelligence to
730 * decide whether to bind to the specified interface.
731 *
732 * What Makes Good usb_device_id Tables:
733 *
734 * The match algorithm is very simple, so that intelligence in
735 * driver selection must come from smart driver id records.
736 * Unless you have good reasons to use another selection policy,
737 * provide match elements only in related groups, and order match
738 * specifiers from specific to general. Use the macros provided
739 * for that purpose if you can.
740 *
741 * The most specific match specifiers use device descriptor
742 * data. These are commonly used with product-specific matches;
743 * the USB_DEVICE macro lets you provide vendor and product IDs,
744 * and you can also match against ranges of product revisions.
745 * These are widely used for devices with application or vendor
746 * specific bDeviceClass values.
747 *
748 * Matches based on device class/subclass/protocol specifications
749 * are slightly more general; use the USB_DEVICE_INFO macro, or
750 * its siblings. These are used with single-function devices
751 * where bDeviceClass doesn't specify that each interface has
752 * its own class.
753 *
754 * Matches based on interface class/subclass/protocol are the
755 * most general; they let drivers bind to any interface on a
756 * multiple-function device. Use the USB_INTERFACE_INFO
757 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400758 * devices (as recorded in bInterfaceClass).
759 *
760 * Note that an entry created by USB_INTERFACE_INFO won't match
761 * any interface if the device class is set to Vendor-Specific.
762 * This is deliberate; according to the USB spec the meanings of
763 * the interface class/subclass/protocol for these devices are also
764 * vendor-specific, and hence matching against a standard product
765 * class wouldn't work anyway. If you really want to use an
766 * interface-based match for such a device, create a match record
767 * that also specifies the vendor ID. (Unforunately there isn't a
768 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800769 *
770 * Within those groups, remember that not all combinations are
771 * meaningful. For example, don't give a product version range
772 * without vendor and product IDs; or specify a protocol without
773 * its associated class and subclass.
774 */
775const struct usb_device_id *usb_match_id(struct usb_interface *interface,
776 const struct usb_device_id *id)
777{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800778 /* proc_connectinfo in devio.c may call us with id == NULL. */
779 if (id == NULL)
780 return NULL;
781
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800782 /* It is important to check that id->driver_info is nonzero,
783 since an entry that is all zeroes except for a nonzero
784 id->driver_info is the way to create an entry that
785 indicates that the driver want to examine every
786 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800787 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
788 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800789 if (usb_match_one_id(interface, id))
790 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800791 }
792
793 return NULL;
794}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600795EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800796
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100797static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800798{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400799 /* devices and interfaces are handled separately */
800 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800801
Alan Stern8bb54ab2006-07-01 22:08:49 -0400802 /* interface drivers never match devices */
803 if (!is_usb_device_driver(drv))
804 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800805
Alan Stern8bb54ab2006-07-01 22:08:49 -0400806 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800807 return 1;
808
Kay Sievers55129662009-05-04 19:48:32 +0200809 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400810 struct usb_interface *intf;
811 struct usb_driver *usb_drv;
812 const struct usb_device_id *id;
813
814 /* device drivers never match interfaces */
815 if (is_usb_device_driver(drv))
816 return 0;
817
818 intf = to_usb_interface(dev);
819 usb_drv = to_usb_driver(drv);
820
821 id = usb_match_id(intf, usb_drv->id_table);
822 if (id)
823 return 1;
824
825 id = usb_match_dynamic_id(intf, usb_drv);
826 if (id)
827 return 1;
828 }
829
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800830 return 0;
831}
832
Kay Sievers7eff2e72007-08-14 15:15:12 +0200833static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400834{
Alan Stern36e56a32006-07-01 22:08:06 -0400835 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400836
Kay Sievers55129662009-05-04 19:48:32 +0200837 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400838 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200839 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100840 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200841
Alan Stern8bb54ab2006-07-01 22:08:49 -0400842 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200843 } else {
844 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400845 }
Alan Stern36e56a32006-07-01 22:08:06 -0400846
847 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500848 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200849 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400850 return -ENODEV;
851 }
852 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200853 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400854 return -ENODEV;
855 }
856
Alan Stern36e56a32006-07-01 22:08:06 -0400857 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200858 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400859 le16_to_cpu(usb_dev->descriptor.idVendor),
860 le16_to_cpu(usb_dev->descriptor.idProduct),
861 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
862 return -ENOMEM;
863
864 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200865 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400866 usb_dev->descriptor.bDeviceClass,
867 usb_dev->descriptor.bDeviceSubClass,
868 usb_dev->descriptor.bDeviceProtocol))
869 return -ENOMEM;
870
Alan Stern36e56a32006-07-01 22:08:06 -0400871 return 0;
872}
873
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800874/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400875 * usb_register_device_driver - register a USB device (not interface) driver
876 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800877 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800878 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400879 * Registers a USB device driver with the USB core. The list of
880 * unattached devices will be rescanned whenever a new driver is
881 * added, allowing the new driver to attach to any recognized devices.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200882 *
883 * Return: A negative error code on failure and 0 on success.
Alan Stern8bb54ab2006-07-01 22:08:49 -0400884 */
885int usb_register_device_driver(struct usb_device_driver *new_udriver,
886 struct module *owner)
887{
888 int retval = 0;
889
890 if (usb_disabled())
891 return -ENODEV;
892
893 new_udriver->drvwrap.for_devices = 1;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100894 new_udriver->drvwrap.driver.name = new_udriver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400895 new_udriver->drvwrap.driver.bus = &usb_bus_type;
896 new_udriver->drvwrap.driver.probe = usb_probe_device;
897 new_udriver->drvwrap.driver.remove = usb_unbind_device;
898 new_udriver->drvwrap.driver.owner = owner;
899
900 retval = driver_register(&new_udriver->drvwrap.driver);
901
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700902 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400903 pr_info("%s: registered new device driver %s\n",
904 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700905 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400906 printk(KERN_ERR "%s: error %d registering device "
907 " driver %s\n",
908 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400909
910 return retval;
911}
912EXPORT_SYMBOL_GPL(usb_register_device_driver);
913
914/**
915 * usb_deregister_device_driver - unregister a USB device (not interface) driver
916 * @udriver: USB operations of the device driver to unregister
917 * Context: must be able to sleep
918 *
919 * Unlinks the specified driver from the internal USB driver list.
920 */
921void usb_deregister_device_driver(struct usb_device_driver *udriver)
922{
923 pr_info("%s: deregistering device driver %s\n",
924 usbcore_name, udriver->name);
925
926 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400927}
928EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
929
930/**
931 * usb_register_driver - register a USB interface driver
932 * @new_driver: USB operations for the interface driver
933 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800934 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400935 *
936 * Registers a USB interface driver with the USB core. The list of
937 * unattached interfaces will be rescanned whenever a new driver is
938 * added, allowing the new driver to attach to any recognized interfaces.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200939 *
940 * Return: A negative error code on failure and 0 on success.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800941 *
942 * NOTE: if you want your driver to use the USB major number, you must call
943 * usb_register_dev() to enable that functionality. This function no longer
944 * takes care of that.
945 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800946int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
947 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800948{
949 int retval = 0;
950
951 if (usb_disabled())
952 return -ENODEV;
953
Alan Stern8bb54ab2006-07-01 22:08:49 -0400954 new_driver->drvwrap.for_devices = 0;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100955 new_driver->drvwrap.driver.name = new_driver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400956 new_driver->drvwrap.driver.bus = &usb_bus_type;
957 new_driver->drvwrap.driver.probe = usb_probe_interface;
958 new_driver->drvwrap.driver.remove = usb_unbind_interface;
959 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800960 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800961 spin_lock_init(&new_driver->dynids.lock);
962 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800963
Alan Stern8bb54ab2006-07-01 22:08:49 -0400964 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800965 if (retval)
966 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800967
Alan Sterned283e92012-01-24 14:35:13 -0500968 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800969 if (retval)
970 goto out_newid;
971
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800972 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800973 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800974
975out:
976 return retval;
977
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800978out_newid:
979 driver_unregister(&new_driver->drvwrap.driver);
980
981 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400982 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800983 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800984 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800985}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600986EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800987
988/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400989 * usb_deregister - unregister a USB interface driver
990 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800991 * Context: must be able to sleep
992 *
993 * Unlinks the specified driver from the internal USB driver list.
994 *
995 * NOTE: If you called usb_register_dev(), you still need to call
996 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
997 * this * call will no longer do it for you.
998 */
999void usb_deregister(struct usb_driver *driver)
1000{
Alan Stern8bb54ab2006-07-01 22:08:49 -04001001 pr_info("%s: deregistering interface driver %s\n",
1002 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -08001003
Alan Sterned283e92012-01-24 14:35:13 -05001004 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -04001005 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -05001006 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -08001007}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06001008EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -04001009
Alan Stern78d9a482008-06-23 16:00:40 -04001010/* Forced unbinding of a USB interface driver, either because
1011 * it doesn't support pre_reset/post_reset/reset_resume or
1012 * because it doesn't support suspend/resume.
1013 *
1014 * The caller must hold @intf's device's lock, but not its pm_mutex
1015 * and not @intf->dev.sem.
1016 */
1017void usb_forced_unbind_intf(struct usb_interface *intf)
1018{
1019 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
1020
1021 dev_dbg(&intf->dev, "forced unbind\n");
1022 usb_driver_release_interface(driver, intf);
1023
1024 /* Mark the interface for later rebinding */
1025 intf->needs_binding = 1;
1026}
1027
1028/* Delayed forced unbinding of a USB interface driver and scan
1029 * for rebinding.
1030 *
1031 * The caller must hold @intf's device's lock, but not its pm_mutex
1032 * and not @intf->dev.sem.
1033 *
Alan Stern5096aed2008-08-12 14:34:14 -04001034 * Note: Rebinds will be skipped if a system sleep transition is in
1035 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -04001036 */
1037void usb_rebind_intf(struct usb_interface *intf)
1038{
1039 int rc;
1040
1041 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +01001042 if (intf->dev.driver)
1043 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001044
1045 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +02001046 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -04001047 intf->needs_binding = 0;
1048 rc = device_attach(&intf->dev);
1049 if (rc < 0)
1050 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1051 }
Alan Stern78d9a482008-06-23 16:00:40 -04001052}
1053
Alan Stern9ff78432008-08-07 13:04:51 -04001054#ifdef CONFIG_PM
1055
Oliver Neukum14931382012-01-05 15:39:57 +01001056/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1057 * There is no check for reset_resume here because it can be determined
1058 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001059 *
1060 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001061 */
Oliver Neukum14931382012-01-05 15:39:57 +01001062static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001063{
1064 struct usb_host_config *config;
1065 int i;
1066 struct usb_interface *intf;
1067 struct usb_driver *drv;
1068
1069 config = udev->actconfig;
1070 if (config) {
1071 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1072 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001073
1074 if (intf->dev.driver) {
1075 drv = to_usb_driver(intf->dev.driver);
1076 if (!drv->suspend || !drv->resume)
1077 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001078 }
1079 }
1080 }
1081}
1082
Oliver Neukum14931382012-01-05 15:39:57 +01001083/* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1084 * These interfaces have the needs_binding flag set by usb_resume_interface().
1085 *
1086 * The caller must hold @udev's device lock.
1087 */
1088static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
1089{
1090 struct usb_host_config *config;
1091 int i;
1092 struct usb_interface *intf;
1093
1094 config = udev->actconfig;
1095 if (config) {
1096 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1097 intf = config->interface[i];
1098 if (intf->dev.driver && intf->needs_binding)
1099 usb_forced_unbind_intf(intf);
1100 }
1101 }
1102}
1103
1104static void do_rebind_interfaces(struct usb_device *udev)
1105{
1106 struct usb_host_config *config;
1107 int i;
1108 struct usb_interface *intf;
1109
1110 config = udev->actconfig;
1111 if (config) {
1112 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1113 intf = config->interface[i];
1114 if (intf->needs_binding)
1115 usb_rebind_intf(intf);
1116 }
1117 }
1118}
1119
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001120static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001121{
Alan Stern782da722006-07-01 22:09:35 -04001122 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001123 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001124
Alan Stern114b3682006-07-01 22:13:04 -04001125 if (udev->state == USB_STATE_NOTATTACHED ||
1126 udev->state == USB_STATE_SUSPENDED)
1127 goto done;
1128
Alan Sternb6f64362007-05-04 11:51:54 -04001129 /* For devices that don't have a driver, we do a generic suspend. */
1130 if (udev->dev.driver)
1131 udriver = to_usb_device_driver(udev->dev.driver);
1132 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001133 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001134 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001135 }
Alan Stern2bf40862006-07-01 22:12:19 -04001136 status = udriver->suspend(udev, msg);
1137
Alan Stern20dfdad2007-05-22 11:50:17 -04001138 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001139 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001140 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001141}
Alan Stern36e56a32006-07-01 22:08:06 -04001142
Alan Stern65bfd292008-11-25 16:39:18 -05001143static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001144{
1145 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001146 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001147
Alan Stern0458d5b2007-05-04 11:52:20 -04001148 if (udev->state == USB_STATE_NOTATTACHED)
1149 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001150
Alan Stern1c5df7e2006-07-01 22:13:50 -04001151 /* Can't resume it if it doesn't have a driver. */
1152 if (udev->dev.driver == NULL) {
1153 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001154 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001155 }
1156
Alan Stern6d19c002010-02-12 12:21:11 +01001157 /* Non-root devices on a full/low-speed bus must wait for their
1158 * companion high-speed root hub, in case a handoff is needed.
1159 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001160 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001161 device_pm_wait_for_dev(&udev->dev,
1162 &udev->bus->hs_companion->root_hub->dev);
1163
Alan Stern6bc6cff2007-05-04 11:53:03 -04001164 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1165 udev->reset_resume = 1;
1166
Alan Stern1cc8a252006-07-01 22:10:15 -04001167 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001168 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001169
Alan Stern20dfdad2007-05-22 11:50:17 -04001170 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001171 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001172 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001173}
1174
Alan Stern65605ae2008-08-12 14:33:27 -04001175static int usb_suspend_interface(struct usb_device *udev,
1176 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001177{
1178 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001179 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001180
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001181 if (udev->state == USB_STATE_NOTATTACHED ||
1182 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001183 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001184 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001185
Oliver Neukume78832c2012-01-02 15:11:48 +01001186 /* at this time we know the driver supports suspend */
1187 status = driver->suspend(intf, msg);
1188 if (status && !PMSG_IS_AUTO(msg))
1189 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001190
Alan Stern20dfdad2007-05-22 11:50:17 -04001191 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001192 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001193 return status;
1194}
1195
Alan Stern65605ae2008-08-12 14:33:27 -04001196static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001197 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001198{
Alan Stern1cc8a252006-07-01 22:10:15 -04001199 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001200 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001201
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001202 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001203 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001204
Alan Stern645daaa2006-08-30 15:47:02 -04001205 /* Don't let autoresume interfere with unbinding */
1206 if (intf->condition == USB_INTERFACE_UNBINDING)
1207 goto done;
1208
Alan Stern1c5df7e2006-07-01 22:13:50 -04001209 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001210 if (intf->condition == USB_INTERFACE_UNBOUND) {
1211
1212 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001213 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001214 usb_set_interface(udev, intf->altsetting[0].
1215 desc.bInterfaceNumber, 0);
1216 intf->needs_altsetting0 = 0;
1217 }
Alan Stern2bf40862006-07-01 22:12:19 -04001218 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001219 }
Alan Stern78d9a482008-06-23 16:00:40 -04001220
1221 /* Don't resume if the interface is marked for rebinding */
1222 if (intf->needs_binding)
1223 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001224 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001225
Alan Sternf07600c2007-05-30 15:38:16 -04001226 if (reset_resume) {
1227 if (driver->reset_resume) {
1228 status = driver->reset_resume(intf);
1229 if (status)
1230 dev_err(&intf->dev, "%s error %d\n",
1231 "reset_resume", status);
1232 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001233 intf->needs_binding = 1;
Alan Stern0a56b4f2013-10-18 11:17:21 -04001234 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1235 driver->name);
Alan Sternf07600c2007-05-30 15:38:16 -04001236 }
1237 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001238 status = driver->resume(intf);
1239 if (status)
1240 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001241 }
Alan Stern2bf40862006-07-01 22:12:19 -04001242
1243done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001244 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001245
Alan Stern78d9a482008-06-23 16:00:40 -04001246 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001247 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001248}
1249
Alan Stern645daaa2006-08-30 15:47:02 -04001250/**
1251 * usb_suspend_both - suspend a USB device and its interfaces
1252 * @udev: the usb_device to suspend
1253 * @msg: Power Management message describing this state transition
1254 *
1255 * This is the central routine for suspending USB devices. It calls the
1256 * suspend methods for all the interface drivers in @udev and then calls
Ming Lei303f0842013-03-15 12:08:53 +08001257 * the suspend method for @udev itself. When the routine is called in
1258 * autosuspend, if an error occurs at any stage, all the interfaces
1259 * which were suspended are resumed so that they remain in the same
1260 * state as the device, but when called from system sleep, all error
1261 * from suspend methods of interfaces and the non-root-hub device itself
1262 * are simply ignored, so all suspended interfaces are only resumed
1263 * to the device's state when @udev is root-hub and its suspend method
1264 * returns failure.
Alan Stern645daaa2006-08-30 15:47:02 -04001265 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001266 * Autosuspend requests originating from a child device or an interface
1267 * driver may be made without the protection of @udev's device lock, but
1268 * all other suspend calls will hold the lock. Usbcore will insure that
1269 * method calls do not arrive during bind, unbind, or reset operations.
1270 * However drivers must be prepared to handle suspend calls arriving at
1271 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001272 *
1273 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001274 *
1275 * Return: 0 if the suspend succeeded.
Alan Stern645daaa2006-08-30 15:47:02 -04001276 */
Alan Stern718efa62007-03-09 15:41:13 -05001277static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001278{
1279 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001280 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001281 struct usb_interface *intf;
1282
Alan Stern19410442007-03-27 13:33:59 -04001283 if (udev->state == USB_STATE_NOTATTACHED ||
1284 udev->state == USB_STATE_SUSPENDED)
1285 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001286
Alan Stern645daaa2006-08-30 15:47:02 -04001287 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001288 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001289 n = udev->actconfig->desc.bNumInterfaces;
1290 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001291 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001292 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001293
1294 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001295 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001296 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001297 if (status != 0)
1298 break;
1299 }
1300 }
Alan Stern0af212b2011-06-15 16:27:43 -04001301 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001302 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001303
Alan Sterncd4376e2012-03-28 15:56:17 -04001304 /*
1305 * Ignore errors from non-root-hub devices during
1306 * system sleep transitions. For the most part,
1307 * these devices should go to low power anyway when
1308 * the entire bus is suspended.
1309 */
1310 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001311 status = 0;
1312 }
1313
Alan Sterna8e7c562006-07-01 22:11:02 -04001314 /* If the suspend failed, resume interfaces that did get suspended */
1315 if (status != 0) {
Chen Gang505bdbc2013-04-01 13:04:08 +08001316 if (udev->actconfig) {
1317 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1318 while (++i < n) {
1319 intf = udev->actconfig->interface[i];
1320 usb_resume_interface(udev, intf, msg, 0);
1321 }
Alan Sterna8e7c562006-07-01 22:11:02 -04001322 }
Alan Stern645daaa2006-08-30 15:47:02 -04001323
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001324 /* If the suspend succeeded then prevent any more URB submissions
1325 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001326 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001327 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001328 udev->can_submit = 0;
1329 for (i = 0; i < 16; ++i) {
1330 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1331 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1332 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001333 }
Alan Stern645daaa2006-08-30 15:47:02 -04001334
Alan Stern19410442007-03-27 13:33:59 -04001335 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001336 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001337 return status;
1338}
1339
Alan Stern645daaa2006-08-30 15:47:02 -04001340/**
1341 * usb_resume_both - resume a USB device and its interfaces
1342 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001343 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001344 *
1345 * This is the central routine for resuming USB devices. It calls the
1346 * the resume method for @udev and then calls the resume methods for all
1347 * the interface drivers in @udev.
1348 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001349 * Autoresume requests originating from a child device or an interface
1350 * driver may be made without the protection of @udev's device lock, but
1351 * all other resume calls will hold the lock. Usbcore will insure that
1352 * method calls do not arrive during bind, unbind, or reset operations.
1353 * However drivers must be prepared to handle resume calls arriving at
1354 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001355 *
1356 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001357 *
1358 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001359 */
Alan Stern65bfd292008-11-25 16:39:18 -05001360static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001361{
Alan Stern645daaa2006-08-30 15:47:02 -04001362 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001363 int i;
1364 struct usb_interface *intf;
1365
Alan Stern19410442007-03-27 13:33:59 -04001366 if (udev->state == USB_STATE_NOTATTACHED) {
1367 status = -ENODEV;
1368 goto done;
1369 }
Alan Stern6840d252007-09-10 11:34:26 -04001370 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001371
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001372 /* Resume the device */
1373 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001374 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001375
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001376 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001377 if (status == 0 && udev->actconfig) {
1378 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1379 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001380 usb_resume_interface(udev, intf, msg,
1381 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001382 }
1383 }
Alan Sternc08512c2010-11-15 15:57:58 -05001384 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001385
Alan Stern19410442007-03-27 13:33:59 -04001386 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001387 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001388 if (!status)
1389 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001390 return status;
1391}
1392
Alan Stern5f677f12010-04-02 13:20:11 -04001393static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1394{
Alan Stern48826622010-06-22 16:14:48 -04001395 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001396
1397 /* Remote wakeup is needed only when we actually go to sleep.
1398 * For things like FREEZE and QUIESCE, if the device is already
1399 * autosuspended then its current wakeup setting is okay.
1400 */
1401 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1402 if (udev->state != USB_STATE_SUSPENDED)
1403 udev->do_remote_wakeup = 0;
1404 return;
1405 }
1406
Alan Stern48826622010-06-22 16:14:48 -04001407 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001408 * actually want it.
1409 */
Alan Stern48826622010-06-22 16:14:48 -04001410 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001411
1412 /* If the device is autosuspended with the wrong wakeup setting,
1413 * autoresume now so the setting can be changed.
1414 */
1415 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1416 pm_runtime_resume(&udev->dev);
1417 udev->do_remote_wakeup = w;
1418}
1419
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001420/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001421int usb_suspend(struct device *dev, pm_message_t msg)
1422{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001423 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001424
Oliver Neukum14931382012-01-05 15:39:57 +01001425 unbind_no_pm_drivers_interfaces(udev);
1426
1427 /* From now on we are sure all drivers support suspend/resume
1428 * but not necessarily reset_resume()
1429 * so we may still need to unbind and rebind upon resume
1430 */
Alan Stern5f677f12010-04-02 13:20:11 -04001431 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001432 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001433}
1434
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001435/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001436int usb_resume_complete(struct device *dev)
1437{
1438 struct usb_device *udev = to_usb_device(dev);
1439
1440 /* For PM complete calls, all we do is rebind interfaces
1441 * whose needs_binding flag is set
1442 */
1443 if (udev->state != USB_STATE_NOTATTACHED)
1444 do_rebind_interfaces(udev);
1445 return 0;
1446}
1447
1448/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001449int usb_resume(struct device *dev, pm_message_t msg)
1450{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001451 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001452 int status;
1453
Oliver Neukum98d9a822012-01-11 08:38:35 +01001454 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001455 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001456 * Unbind the interfaces that will need rebinding later,
1457 * because they fail to support reset_resume.
1458 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001459 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001460 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001461 status = usb_resume_both(udev, msg);
1462 if (status == 0) {
1463 pm_runtime_disable(dev);
1464 pm_runtime_set_active(dev);
1465 pm_runtime_enable(dev);
1466 unbind_no_reset_resume_drivers_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001467 }
Alan Stern0c590e22010-01-08 12:57:14 -05001468
1469 /* Avoid PM error messages for devices disconnected while suspended
1470 * as we'll display regular disconnect messages just a bit later.
1471 */
Peter Chen7491f132010-09-27 16:43:25 +08001472 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001473 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001474 return status;
1475}
1476
1477#endif /* CONFIG_PM */
1478
Alan Stern84ebc102013-03-27 16:14:46 -04001479#ifdef CONFIG_PM_RUNTIME
Alan Stern645daaa2006-08-30 15:47:02 -04001480
Alan Stern088f7fe2010-01-08 12:56:54 -05001481/**
1482 * usb_enable_autosuspend - allow a USB device to be autosuspended
1483 * @udev: the USB device which may be autosuspended
1484 *
1485 * This routine allows @udev to be autosuspended. An autosuspend won't
1486 * take place until the autosuspend_delay has elapsed and all the other
1487 * necessary conditions are satisfied.
1488 *
1489 * The caller must hold @udev's device lock.
1490 */
Alan Stern9e18c822010-04-02 13:22:09 -04001491void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001492{
Alan Stern9e18c822010-04-02 13:22:09 -04001493 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001494}
1495EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1496
1497/**
1498 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1499 * @udev: the USB device which may not be autosuspended
1500 *
1501 * This routine prevents @udev from being autosuspended and wakes it up
1502 * if it is already autosuspended.
1503 *
1504 * The caller must hold @udev's device lock.
1505 */
Alan Stern9e18c822010-04-02 13:22:09 -04001506void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001507{
Alan Stern9e18c822010-04-02 13:22:09 -04001508 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001509}
1510EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1511
Alan Stern645daaa2006-08-30 15:47:02 -04001512/**
1513 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001514 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001515 *
1516 * This routine should be called when a core subsystem is finished using
1517 * @udev and wants to allow it to autosuspend. Examples would be when
1518 * @udev's device file in usbfs is closed or after a configuration change.
1519 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001520 * @udev's usage counter is decremented; if it drops to 0 and all the
1521 * interfaces are inactive then a delayed autosuspend will be attempted.
1522 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001523 *
Alan Stern62e299e2010-01-08 12:56:19 -05001524 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001525 *
1526 * This routine can run only in process context.
1527 */
Alan Stern94fcda12006-11-20 11:38:46 -05001528void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001529{
Alan Stern94fcda12006-11-20 11:38:46 -05001530 int status;
1531
Ming Lei6ddf27c2010-11-15 15:57:30 -05001532 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001533 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001534 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1535 __func__, atomic_read(&udev->dev.power.usage_count),
1536 status);
Alan Stern19c26232007-02-20 15:03:32 -05001537}
1538
1539/**
Alan Stern645daaa2006-08-30 15:47:02 -04001540 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001541 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001542 *
1543 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001544 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001545 * occur until usb_autosuspend_device() is called. (Note that this will
1546 * not prevent suspend events originating in the PM core.) Examples would
1547 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001548 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001549 *
Alan Stern94fcda12006-11-20 11:38:46 -05001550 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1551 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001552 *
Alan Stern62e299e2010-01-08 12:56:19 -05001553 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001554 *
1555 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001556 *
1557 * Return: 0 on success. A negative error code otherwise.
Alan Stern645daaa2006-08-30 15:47:02 -04001558 */
Alan Stern94fcda12006-11-20 11:38:46 -05001559int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001560{
1561 int status;
1562
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001563 status = pm_runtime_get_sync(&udev->dev);
1564 if (status < 0)
1565 pm_runtime_put_sync(&udev->dev);
1566 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1567 __func__, atomic_read(&udev->dev.power.usage_count),
1568 status);
1569 if (status > 0)
1570 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001571 return status;
1572}
1573
Alan Stern645daaa2006-08-30 15:47:02 -04001574/**
1575 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001576 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001577 *
1578 * This routine should be called by an interface driver when it is
1579 * finished using @intf and wants to allow it to autosuspend. A typical
1580 * example would be a character-device driver when its device file is
1581 * closed.
1582 *
1583 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001584 * 0, a delayed autosuspend request for @intf's device is attempted. The
1585 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001586 *
Alan Stern645daaa2006-08-30 15:47:02 -04001587 * This routine can run only in process context.
1588 */
1589void usb_autopm_put_interface(struct usb_interface *intf)
1590{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001591 struct usb_device *udev = interface_to_usbdev(intf);
1592 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001593
Ming Lei6ddf27c2010-11-15 15:57:30 -05001594 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001595 atomic_dec(&intf->pm_usage_cnt);
1596 status = pm_runtime_put_sync(&intf->dev);
1597 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1598 __func__, atomic_read(&intf->dev.power.usage_count),
1599 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001600}
1601EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1602
1603/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001604 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1605 * @intf: the usb_interface whose counter should be decremented
1606 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001607 * This routine does much the same thing as usb_autopm_put_interface():
1608 * It decrements @intf's usage counter and schedules a delayed
1609 * autosuspend request if the counter is <= 0. The difference is that it
1610 * does not perform any synchronization; callers should hold a private
1611 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001612 *
1613 * Typically a driver would call this routine during an URB's completion
1614 * handler, if no more URBs were pending.
1615 *
1616 * This routine can run in atomic context.
1617 */
1618void usb_autopm_put_interface_async(struct usb_interface *intf)
1619{
1620 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001621 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001622
Ming Lei6ddf27c2010-11-15 15:57:30 -05001623 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001624 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001625 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001626 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1627 __func__, atomic_read(&intf->dev.power.usage_count),
1628 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001629}
1630EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1631
1632/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001633 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1634 * @intf: the usb_interface whose counter should be decremented
1635 *
1636 * This routine decrements @intf's usage counter but does not carry out an
1637 * autosuspend.
1638 *
1639 * This routine can run in atomic context.
1640 */
1641void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1642{
1643 struct usb_device *udev = interface_to_usbdev(intf);
1644
Ming Lei6ddf27c2010-11-15 15:57:30 -05001645 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001646 atomic_dec(&intf->pm_usage_cnt);
1647 pm_runtime_put_noidle(&intf->dev);
1648}
1649EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1650
1651/**
Alan Stern645daaa2006-08-30 15:47:02 -04001652 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001653 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001654 *
1655 * This routine should be called by an interface driver when it wants to
1656 * use @intf and needs to guarantee that it is not suspended. In addition,
1657 * the routine prevents @intf from being autosuspended subsequently. (Note
1658 * that this will not prevent suspend events originating in the PM core.)
1659 * This prevention will persist until usb_autopm_put_interface() is called
1660 * or @intf is unbound. A typical example would be a character-device
1661 * driver when its device file is opened.
1662 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001663 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1664 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001665 *
1666 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001667 *
1668 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001669 */
1670int usb_autopm_get_interface(struct usb_interface *intf)
1671{
Alan Sternaf4f7602006-10-30 17:06:45 -05001672 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001673
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001674 status = pm_runtime_get_sync(&intf->dev);
1675 if (status < 0)
1676 pm_runtime_put_sync(&intf->dev);
1677 else
1678 atomic_inc(&intf->pm_usage_cnt);
1679 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1680 __func__, atomic_read(&intf->dev.power.usage_count),
1681 status);
1682 if (status > 0)
1683 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001684 return status;
1685}
1686EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1687
Alan Stern692a1862006-10-30 17:07:51 -05001688/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001689 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1690 * @intf: the usb_interface whose counter should be incremented
1691 *
1692 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001693 * usb_autopm_get_interface(): It increments @intf's usage counter and
1694 * queues an autoresume request if the device is suspended. The
1695 * differences are that it does not perform any synchronization (callers
1696 * should hold a private lock and handle all synchronization issues
1697 * themselves), and it does not autoresume the device directly (it only
1698 * queues a request). After a successful call, the device may not yet be
1699 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001700 *
1701 * This routine can run in atomic context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001702 *
1703 * Return: 0 on success. A negative error code otherwise.
Alan Stern9ac39f22008-11-12 16:19:49 -05001704 */
1705int usb_autopm_get_interface_async(struct usb_interface *intf)
1706{
Ming Lei63defa72010-11-15 15:56:54 -05001707 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001708
Ming Lei63defa72010-11-15 15:56:54 -05001709 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001710 if (status < 0 && status != -EINPROGRESS)
1711 pm_runtime_put_noidle(&intf->dev);
1712 else
Alan Sternccf5b802009-06-29 11:00:01 -04001713 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001714 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1715 __func__, atomic_read(&intf->dev.power.usage_count),
1716 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001717 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001718 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001719 return status;
1720}
1721EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1722
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001723/**
1724 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1725 * @intf: the usb_interface whose counter should be incremented
1726 *
1727 * This routine increments @intf's usage counter but does not carry out an
1728 * autoresume.
1729 *
1730 * This routine can run in atomic context.
1731 */
1732void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1733{
1734 struct usb_device *udev = interface_to_usbdev(intf);
1735
Ming Lei6ddf27c2010-11-15 15:57:30 -05001736 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001737 atomic_inc(&intf->pm_usage_cnt);
1738 pm_runtime_get_noresume(&intf->dev);
1739}
1740EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1741
1742/* Internal routine to check whether we may autosuspend a device. */
1743static int autosuspend_check(struct usb_device *udev)
1744{
Alan Stern7560d32e2010-04-02 13:18:50 -04001745 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001746 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001747
1748 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1749 * any interface drivers require remote wakeup but it isn't available.
1750 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001751 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001752 if (udev->actconfig) {
1753 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1754 intf = udev->actconfig->interface[i];
1755
1756 /* We don't need to check interfaces that are
1757 * disabled for runtime PM. Either they are unbound
1758 * or else their drivers don't support autosuspend
1759 * and so they are permanently active.
1760 */
1761 if (intf->dev.power.disable_depth)
1762 continue;
1763 if (atomic_read(&intf->dev.power.usage_count) > 0)
1764 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001765 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001766
1767 /* Don't allow autosuspend if the device will need
1768 * a reset-resume and any of its interface drivers
1769 * doesn't include support or needs remote wakeup.
1770 */
1771 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1772 struct usb_driver *driver;
1773
1774 driver = to_usb_driver(intf->dev.driver);
1775 if (!driver->reset_resume ||
1776 intf->needs_remote_wakeup)
1777 return -EOPNOTSUPP;
1778 }
1779 }
1780 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001781 if (w && !device_can_wakeup(&udev->dev)) {
1782 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1783 return -EOPNOTSUPP;
1784 }
1785 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001786 return 0;
1787}
1788
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001789int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001790{
Ming Lei63defa72010-11-15 15:56:54 -05001791 struct usb_device *udev = to_usb_device(dev);
1792 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001793
1794 /* A USB device can be suspended if it passes the various autosuspend
1795 * checks. Runtime suspend for a USB device means suspending all the
1796 * interfaces and then the device itself.
1797 */
Ming Lei63defa72010-11-15 15:56:54 -05001798 if (autosuspend_check(udev) != 0)
1799 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001800
Ming Lei63defa72010-11-15 15:56:54 -05001801 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001802
1803 /* Allow a retry if autosuspend failed temporarily */
1804 if (status == -EAGAIN || status == -EBUSY)
1805 usb_mark_last_busy(udev);
1806
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001807 /* The PM core reacts badly unless the return code is 0,
1808 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1809 */
1810 if (status != 0)
1811 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001812 return status;
1813}
1814
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001815int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001816{
Ming Lei63defa72010-11-15 15:56:54 -05001817 struct usb_device *udev = to_usb_device(dev);
1818 int status;
1819
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001820 /* Runtime resume for a USB device means resuming both the device
1821 * and all its interfaces.
1822 */
Ming Lei63defa72010-11-15 15:56:54 -05001823 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001824 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001825}
1826
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001827int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001828{
Ming Lei63defa72010-11-15 15:56:54 -05001829 struct usb_device *udev = to_usb_device(dev);
1830
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001831 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001832 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001833 */
Ming Lei63defa72010-11-15 15:56:54 -05001834 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001835 pm_runtime_autosuspend(dev);
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001836 /* Tell the core not to suspend it, though. */
1837 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001838}
1839
Andiry Xu65580b432011-09-23 14:19:52 -07001840int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1841{
1842 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1843 int ret = -EPERM;
1844
Sarah Sharpde68bab2013-09-30 17:26:28 +03001845 if (enable && !udev->usb2_hw_lpm_allowed)
1846 return 0;
1847
Andiry Xu65580b432011-09-23 14:19:52 -07001848 if (hcd->driver->set_usb2_hw_lpm) {
1849 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1850 if (!ret)
1851 udev->usb2_hw_lpm_enabled = enable;
1852 }
1853
1854 return ret;
1855}
1856
Alan Stern84ebc102013-03-27 16:14:46 -04001857#endif /* CONFIG_PM_RUNTIME */
Alan Stern645daaa2006-08-30 15:47:02 -04001858
Alan Stern36e56a32006-07-01 22:08:06 -04001859struct bus_type usb_bus_type = {
1860 .name = "usb",
1861 .match = usb_device_match,
1862 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001863};