blob: ab90a01568283c3c0d2a0a3df9b653bfba65a812 [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
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800315 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800316 if (!id)
317 id = usb_match_dynamic_id(intf, driver);
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 Neukum1e5ea5e2009-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 Neukum1e5ea5e2009-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 Neukum1e5ea5e2009-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 Neukum1e5ea5e2009-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 Neukum1e5ea5e2009-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);
Alan Stern645daaa2006-08-30 15:47:02 -0400403 struct usb_device *udev;
Sarah Sharp83060952012-05-02 14:25:52 -0700404 int error, r, lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800405
406 intf->condition = USB_INTERFACE_UNBINDING;
407
Alan Stern645daaa2006-08-30 15:47:02 -0400408 /* Autoresume for set_interface call below */
409 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500410 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400411
Sarah Sharp83060952012-05-02 14:25:52 -0700412 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
413 * the driver is unbound. If LPM isn't disabled, that's fine because it
414 * wouldn't be enabled unless all the bound interfaces supported
415 * hub-initiated LPM.
416 */
417 lpm_disable_error = usb_unlocked_disable_lpm(udev);
418
Alan Stern9da82bd2008-05-08 11:54:37 -0400419 /* Terminate all URBs for this interface unless the driver
420 * supports "soft" unbinding.
421 */
422 if (!driver->soft_unbind)
Alan Sternddeac4e72009-01-15 17:03:33 -0500423 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800424
Alan Stern8bb54ab2006-07-01 22:08:49 -0400425 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800426 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800427
Alan Stern55151d72008-08-12 14:33:59 -0400428 /* Reset other interface state.
429 * We cannot do a Set-Interface if the device is suspended or
430 * if it is prepared for a system sleep (since installing a new
431 * altsetting means creating new endpoint device entries).
432 * When either of these happens, defer the Set-Interface.
433 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500434 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
435 /* Already in altsetting 0 so skip Set-Interface.
436 * Just re-enable it without affecting the endpoint toggles.
437 */
438 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200439 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200440 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400441 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200442 if (r < 0)
443 intf->needs_altsetting0 = 1;
444 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400445 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200446 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800447 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400448
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800449 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400450 intf->needs_remote_wakeup = 0;
451
Sarah Sharp83060952012-05-02 14:25:52 -0700452 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
453 if (!lpm_disable_error)
454 usb_unlocked_enable_lpm(udev);
455
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500456 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400457 if (driver->supports_autosuspend)
458 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500459 pm_runtime_set_suspended(dev);
460
461 /* Undo any residual pm_autopm_get_interface_* calls */
462 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
463 usb_autopm_put_interface_no_suspend(intf);
464 atomic_set(&intf->pm_usage_cnt, 0);
465
Alan Stern645daaa2006-08-30 15:47:02 -0400466 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500467 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800468
469 return 0;
470}
471
Alan Stern36e56a32006-07-01 22:08:06 -0400472/**
473 * usb_driver_claim_interface - bind a driver to an interface
474 * @driver: the driver to be bound
475 * @iface: the interface to which it will be bound; must be in the
476 * usb device's active configuration
477 * @priv: driver data associated with that interface
478 *
479 * This is used by usb device drivers that need to claim more than one
480 * interface on a device when probing (audio and acm are current examples).
481 * No device driver should directly modify internal usb_interface or
482 * usb_device structure members.
483 *
484 * Few drivers should need to use this routine, since the most natural
485 * way to bind to an interface is to return the private data from
486 * the driver's probe() method.
487 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400488 * Callers must own the device lock, so driver probe() entries don't need
489 * extra locking, but other call contexts may need to explicitly claim that
490 * lock.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200491 *
492 * Return: 0 on success.
Alan Stern36e56a32006-07-01 22:08:06 -0400493 */
494int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800495 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400496{
497 struct device *dev = &iface->dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700498 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700499 int retval = 0;
Sarah Sharp83060952012-05-02 14:25:52 -0700500 int lpm_disable_error;
Alan Stern36e56a32006-07-01 22:08:06 -0400501
502 if (dev->driver)
503 return -EBUSY;
504
Sarah Sharp83060952012-05-02 14:25:52 -0700505 udev = interface_to_usbdev(iface);
506
Alan Stern8bb54ab2006-07-01 22:08:49 -0400507 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400508 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400509 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400510
Alan Stern36e56a32006-07-01 22:08:06 -0400511 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500512
Sarah Sharp83060952012-05-02 14:25:52 -0700513 /* Disable LPM until this driver is bound. */
514 lpm_disable_error = usb_unlocked_disable_lpm(udev);
515 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
516 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
517 __func__, driver->name);
518 return -ENOMEM;
519 }
520
Alan Stern89842ae2010-05-11 11:44:06 -0400521 /* Claimed interfaces are initially inactive (suspended) and
522 * runtime-PM-enabled, but only if the driver has autosuspend
523 * support. Otherwise they are marked active, to prevent the
524 * device from being autosuspended, but left disabled. In either
525 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500526 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500527 pm_suspend_ignore_children(dev, false);
528 if (driver->supports_autosuspend)
529 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400530 else
531 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400532
533 /* if interface was already added, bind now; else let
534 * the future device_add() bind it, bypassing probe()
535 */
536 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700537 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400538
Sarah Sharp83060952012-05-02 14:25:52 -0700539 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
540 if (!lpm_disable_error)
541 usb_unlocked_enable_lpm(udev);
542
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700543 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400544}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600545EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400546
547/**
548 * usb_driver_release_interface - unbind a driver from an interface
549 * @driver: the driver to be unbound
550 * @iface: the interface from which it will be unbound
551 *
552 * This can be used by drivers to release an interface without waiting
553 * for their disconnect() methods to be called. In typical cases this
554 * also causes the driver disconnect() method to be called.
555 *
556 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400557 * Callers must own the device lock, so driver disconnect() entries don't
558 * need extra locking, but other call contexts may need to explicitly claim
559 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400560 */
561void usb_driver_release_interface(struct usb_driver *driver,
562 struct usb_interface *iface)
563{
564 struct device *dev = &iface->dev;
565
566 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400567 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400568 return;
569
570 /* don't release from within disconnect() */
571 if (iface->condition != USB_INTERFACE_BOUND)
572 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400573 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400574
Alan Stern91f8d062009-04-16 15:35:09 -0400575 /* Release via the driver core only if the interface
576 * has already been registered
577 */
Alan Stern36e56a32006-07-01 22:08:06 -0400578 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400579 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800580 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800581 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400582 usb_unbind_interface(dev);
583 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800584 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400585 }
Alan Stern36e56a32006-07-01 22:08:06 -0400586}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600587EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400588
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800589/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100590int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800591{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800592 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
593 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
594 return 0;
595
596 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
597 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
598 return 0;
599
600 /* No need to test id->bcdDevice_lo != 0, since 0 is never
601 greater than any unsigned number. */
602 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
603 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
604 return 0;
605
606 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
607 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
608 return 0;
609
610 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
611 (id->bDeviceClass != dev->descriptor.bDeviceClass))
612 return 0;
613
614 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800615 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800616 return 0;
617
618 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
619 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
620 return 0;
621
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100622 return 1;
623}
624
625/* returns 0 if no match, 1 if match */
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200626int usb_match_one_id_intf(struct usb_device *dev,
627 struct usb_host_interface *intf,
628 const struct usb_device_id *id)
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100629{
Bjørn Mork81df2d52012-05-18 21:27:43 +0200630 /* The interface class, subclass, protocol and number should never be
Alan Stern93c8bf42006-10-18 16:41:51 -0400631 * checked for a match if the device class is Vendor Specific,
632 * unless the match record specifies the Vendor ID. */
633 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
634 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
635 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
636 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
Bjørn Mork81df2d52012-05-18 21:27:43 +0200637 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
638 USB_DEVICE_ID_MATCH_INT_NUMBER)))
Alan Stern93c8bf42006-10-18 16:41:51 -0400639 return 0;
640
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800641 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
642 (id->bInterfaceClass != intf->desc.bInterfaceClass))
643 return 0;
644
645 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
646 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
647 return 0;
648
649 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
650 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
651 return 0;
652
Bjørn Mork81df2d52012-05-18 21:27:43 +0200653 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
654 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
655 return 0;
656
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800657 return 1;
658}
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200659
660/* returns 0 if no match, 1 if match */
661int usb_match_one_id(struct usb_interface *interface,
662 const struct usb_device_id *id)
663{
664 struct usb_host_interface *intf;
665 struct usb_device *dev;
666
667 /* proc_connectinfo in devio.c may call us with id == NULL. */
668 if (id == NULL)
669 return 0;
670
671 intf = interface->cur_altsetting;
672 dev = interface_to_usbdev(interface);
673
674 if (!usb_match_device(dev, id))
675 return 0;
676
677 return usb_match_one_id_intf(dev, intf, id);
678}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100679EXPORT_SYMBOL_GPL(usb_match_one_id);
680
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800681/**
682 * usb_match_id - find first usb_device_id matching device or interface
683 * @interface: the interface of interest
684 * @id: array of usb_device_id structures, terminated by zero entry
685 *
686 * usb_match_id searches an array of usb_device_id's and returns
687 * the first one matching the device or interface, or null.
688 * This is used when binding (or rebinding) a driver to an interface.
689 * Most USB device drivers will use this indirectly, through the usb core,
690 * but some layered driver frameworks use it directly.
691 * These device tables are exported with MODULE_DEVICE_TABLE, through
692 * modutils, to support the driver loading functionality of USB hotplugging.
693 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200694 * Return: The first matching usb_device_id, or %NULL.
695 *
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800696 * What Matches:
697 *
698 * The "match_flags" element in a usb_device_id controls which
699 * members are used. If the corresponding bit is set, the
700 * value in the device_id must match its corresponding member
701 * in the device or interface descriptor, or else the device_id
702 * does not match.
703 *
704 * "driver_info" is normally used only by device drivers,
705 * but you can create a wildcard "matches anything" usb_device_id
706 * as a driver's "modules.usbmap" entry if you provide an id with
707 * only a nonzero "driver_info" field. If you do this, the USB device
708 * driver's probe() routine should use additional intelligence to
709 * decide whether to bind to the specified interface.
710 *
711 * What Makes Good usb_device_id Tables:
712 *
713 * The match algorithm is very simple, so that intelligence in
714 * driver selection must come from smart driver id records.
715 * Unless you have good reasons to use another selection policy,
716 * provide match elements only in related groups, and order match
717 * specifiers from specific to general. Use the macros provided
718 * for that purpose if you can.
719 *
720 * The most specific match specifiers use device descriptor
721 * data. These are commonly used with product-specific matches;
722 * the USB_DEVICE macro lets you provide vendor and product IDs,
723 * and you can also match against ranges of product revisions.
724 * These are widely used for devices with application or vendor
725 * specific bDeviceClass values.
726 *
727 * Matches based on device class/subclass/protocol specifications
728 * are slightly more general; use the USB_DEVICE_INFO macro, or
729 * its siblings. These are used with single-function devices
730 * where bDeviceClass doesn't specify that each interface has
731 * its own class.
732 *
733 * Matches based on interface class/subclass/protocol are the
734 * most general; they let drivers bind to any interface on a
735 * multiple-function device. Use the USB_INTERFACE_INFO
736 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400737 * devices (as recorded in bInterfaceClass).
738 *
739 * Note that an entry created by USB_INTERFACE_INFO won't match
740 * any interface if the device class is set to Vendor-Specific.
741 * This is deliberate; according to the USB spec the meanings of
742 * the interface class/subclass/protocol for these devices are also
743 * vendor-specific, and hence matching against a standard product
744 * class wouldn't work anyway. If you really want to use an
745 * interface-based match for such a device, create a match record
746 * that also specifies the vendor ID. (Unforunately there isn't a
747 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800748 *
749 * Within those groups, remember that not all combinations are
750 * meaningful. For example, don't give a product version range
751 * without vendor and product IDs; or specify a protocol without
752 * its associated class and subclass.
753 */
754const struct usb_device_id *usb_match_id(struct usb_interface *interface,
755 const struct usb_device_id *id)
756{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800757 /* proc_connectinfo in devio.c may call us with id == NULL. */
758 if (id == NULL)
759 return NULL;
760
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800761 /* It is important to check that id->driver_info is nonzero,
762 since an entry that is all zeroes except for a nonzero
763 id->driver_info is the way to create an entry that
764 indicates that the driver want to examine every
765 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800766 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
767 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800768 if (usb_match_one_id(interface, id))
769 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800770 }
771
772 return NULL;
773}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600774EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800775
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100776static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800777{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400778 /* devices and interfaces are handled separately */
779 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800780
Alan Stern8bb54ab2006-07-01 22:08:49 -0400781 /* interface drivers never match devices */
782 if (!is_usb_device_driver(drv))
783 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800784
Alan Stern8bb54ab2006-07-01 22:08:49 -0400785 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800786 return 1;
787
Kay Sievers55129662009-05-04 19:48:32 +0200788 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400789 struct usb_interface *intf;
790 struct usb_driver *usb_drv;
791 const struct usb_device_id *id;
792
793 /* device drivers never match interfaces */
794 if (is_usb_device_driver(drv))
795 return 0;
796
797 intf = to_usb_interface(dev);
798 usb_drv = to_usb_driver(drv);
799
800 id = usb_match_id(intf, usb_drv->id_table);
801 if (id)
802 return 1;
803
804 id = usb_match_dynamic_id(intf, usb_drv);
805 if (id)
806 return 1;
807 }
808
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800809 return 0;
810}
811
Kay Sievers7eff2e72007-08-14 15:15:12 +0200812static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400813{
Alan Stern36e56a32006-07-01 22:08:06 -0400814 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400815
Kay Sievers55129662009-05-04 19:48:32 +0200816 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400817 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200818 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100819 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200820
Alan Stern8bb54ab2006-07-01 22:08:49 -0400821 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200822 } else {
823 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400824 }
Alan Stern36e56a32006-07-01 22:08:06 -0400825
826 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500827 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200828 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400829 return -ENODEV;
830 }
831 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200832 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400833 return -ENODEV;
834 }
835
Alan Stern36e56a32006-07-01 22:08:06 -0400836 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200837 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400838 le16_to_cpu(usb_dev->descriptor.idVendor),
839 le16_to_cpu(usb_dev->descriptor.idProduct),
840 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
841 return -ENOMEM;
842
843 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200844 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400845 usb_dev->descriptor.bDeviceClass,
846 usb_dev->descriptor.bDeviceSubClass,
847 usb_dev->descriptor.bDeviceProtocol))
848 return -ENOMEM;
849
Alan Stern36e56a32006-07-01 22:08:06 -0400850 return 0;
851}
852
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800853/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400854 * usb_register_device_driver - register a USB device (not interface) driver
855 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800856 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800857 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400858 * Registers a USB device driver with the USB core. The list of
859 * unattached devices will be rescanned whenever a new driver is
860 * added, allowing the new driver to attach to any recognized devices.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200861 *
862 * Return: A negative error code on failure and 0 on success.
Alan Stern8bb54ab2006-07-01 22:08:49 -0400863 */
864int usb_register_device_driver(struct usb_device_driver *new_udriver,
865 struct module *owner)
866{
867 int retval = 0;
868
869 if (usb_disabled())
870 return -ENODEV;
871
872 new_udriver->drvwrap.for_devices = 1;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100873 new_udriver->drvwrap.driver.name = new_udriver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400874 new_udriver->drvwrap.driver.bus = &usb_bus_type;
875 new_udriver->drvwrap.driver.probe = usb_probe_device;
876 new_udriver->drvwrap.driver.remove = usb_unbind_device;
877 new_udriver->drvwrap.driver.owner = owner;
878
879 retval = driver_register(&new_udriver->drvwrap.driver);
880
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700881 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400882 pr_info("%s: registered new device driver %s\n",
883 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700884 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400885 printk(KERN_ERR "%s: error %d registering device "
886 " driver %s\n",
887 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400888
889 return retval;
890}
891EXPORT_SYMBOL_GPL(usb_register_device_driver);
892
893/**
894 * usb_deregister_device_driver - unregister a USB device (not interface) driver
895 * @udriver: USB operations of the device driver to unregister
896 * Context: must be able to sleep
897 *
898 * Unlinks the specified driver from the internal USB driver list.
899 */
900void usb_deregister_device_driver(struct usb_device_driver *udriver)
901{
902 pr_info("%s: deregistering device driver %s\n",
903 usbcore_name, udriver->name);
904
905 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400906}
907EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
908
909/**
910 * usb_register_driver - register a USB interface driver
911 * @new_driver: USB operations for the interface driver
912 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800913 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400914 *
915 * Registers a USB interface driver with the USB core. The list of
916 * unattached interfaces will be rescanned whenever a new driver is
917 * added, allowing the new driver to attach to any recognized interfaces.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200918 *
919 * Return: A negative error code on failure and 0 on success.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800920 *
921 * NOTE: if you want your driver to use the USB major number, you must call
922 * usb_register_dev() to enable that functionality. This function no longer
923 * takes care of that.
924 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800925int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
926 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800927{
928 int retval = 0;
929
930 if (usb_disabled())
931 return -ENODEV;
932
Alan Stern8bb54ab2006-07-01 22:08:49 -0400933 new_driver->drvwrap.for_devices = 0;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100934 new_driver->drvwrap.driver.name = new_driver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400935 new_driver->drvwrap.driver.bus = &usb_bus_type;
936 new_driver->drvwrap.driver.probe = usb_probe_interface;
937 new_driver->drvwrap.driver.remove = usb_unbind_interface;
938 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800939 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800940 spin_lock_init(&new_driver->dynids.lock);
941 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800942
Alan Stern8bb54ab2006-07-01 22:08:49 -0400943 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800944 if (retval)
945 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800946
Alan Sterned283e92012-01-24 14:35:13 -0500947 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800948 if (retval)
949 goto out_newid;
950
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800951 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800952 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800953
954out:
955 return retval;
956
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800957out_newid:
958 driver_unregister(&new_driver->drvwrap.driver);
959
960 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400961 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800962 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800963 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800964}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600965EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800966
967/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400968 * usb_deregister - unregister a USB interface driver
969 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800970 * Context: must be able to sleep
971 *
972 * Unlinks the specified driver from the internal USB driver list.
973 *
974 * NOTE: If you called usb_register_dev(), you still need to call
975 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
976 * this * call will no longer do it for you.
977 */
978void usb_deregister(struct usb_driver *driver)
979{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400980 pr_info("%s: deregistering interface driver %s\n",
981 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800982
Alan Sterned283e92012-01-24 14:35:13 -0500983 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400984 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -0500985 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800986}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600987EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400988
Alan Stern78d9a482008-06-23 16:00:40 -0400989/* Forced unbinding of a USB interface driver, either because
990 * it doesn't support pre_reset/post_reset/reset_resume or
991 * because it doesn't support suspend/resume.
992 *
993 * The caller must hold @intf's device's lock, but not its pm_mutex
994 * and not @intf->dev.sem.
995 */
996void usb_forced_unbind_intf(struct usb_interface *intf)
997{
998 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
999
1000 dev_dbg(&intf->dev, "forced unbind\n");
1001 usb_driver_release_interface(driver, intf);
1002
1003 /* Mark the interface for later rebinding */
1004 intf->needs_binding = 1;
1005}
1006
1007/* Delayed forced unbinding of a USB interface driver and scan
1008 * for rebinding.
1009 *
1010 * The caller must hold @intf's device's lock, but not its pm_mutex
1011 * and not @intf->dev.sem.
1012 *
Alan Stern5096aed2008-08-12 14:34:14 -04001013 * Note: Rebinds will be skipped if a system sleep transition is in
1014 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -04001015 */
1016void usb_rebind_intf(struct usb_interface *intf)
1017{
1018 int rc;
1019
1020 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +01001021 if (intf->dev.driver)
1022 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001023
1024 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +02001025 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -04001026 intf->needs_binding = 0;
1027 rc = device_attach(&intf->dev);
1028 if (rc < 0)
1029 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1030 }
Alan Stern78d9a482008-06-23 16:00:40 -04001031}
1032
Alan Stern9ff78432008-08-07 13:04:51 -04001033#ifdef CONFIG_PM
1034
Oliver Neukum14931382012-01-05 15:39:57 +01001035/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1036 * There is no check for reset_resume here because it can be determined
1037 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001038 *
1039 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001040 */
Oliver Neukum14931382012-01-05 15:39:57 +01001041static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001042{
1043 struct usb_host_config *config;
1044 int i;
1045 struct usb_interface *intf;
1046 struct usb_driver *drv;
1047
1048 config = udev->actconfig;
1049 if (config) {
1050 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1051 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001052
1053 if (intf->dev.driver) {
1054 drv = to_usb_driver(intf->dev.driver);
1055 if (!drv->suspend || !drv->resume)
1056 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001057 }
1058 }
1059 }
1060}
1061
Oliver Neukum14931382012-01-05 15:39:57 +01001062/* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1063 * These interfaces have the needs_binding flag set by usb_resume_interface().
1064 *
1065 * The caller must hold @udev's device lock.
1066 */
1067static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
1068{
1069 struct usb_host_config *config;
1070 int i;
1071 struct usb_interface *intf;
1072
1073 config = udev->actconfig;
1074 if (config) {
1075 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1076 intf = config->interface[i];
1077 if (intf->dev.driver && intf->needs_binding)
1078 usb_forced_unbind_intf(intf);
1079 }
1080 }
1081}
1082
1083static void do_rebind_interfaces(struct usb_device *udev)
1084{
1085 struct usb_host_config *config;
1086 int i;
1087 struct usb_interface *intf;
1088
1089 config = udev->actconfig;
1090 if (config) {
1091 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1092 intf = config->interface[i];
1093 if (intf->needs_binding)
1094 usb_rebind_intf(intf);
1095 }
1096 }
1097}
1098
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001099static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001100{
Alan Stern782da722006-07-01 22:09:35 -04001101 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001102 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001103
Alan Stern114b3682006-07-01 22:13:04 -04001104 if (udev->state == USB_STATE_NOTATTACHED ||
1105 udev->state == USB_STATE_SUSPENDED)
1106 goto done;
1107
Alan Sternb6f64362007-05-04 11:51:54 -04001108 /* For devices that don't have a driver, we do a generic suspend. */
1109 if (udev->dev.driver)
1110 udriver = to_usb_device_driver(udev->dev.driver);
1111 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001112 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001113 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001114 }
Alan Stern2bf40862006-07-01 22:12:19 -04001115 status = udriver->suspend(udev, msg);
1116
Alan Stern20dfdad2007-05-22 11:50:17 -04001117 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001118 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001119 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001120}
Alan Stern36e56a32006-07-01 22:08:06 -04001121
Alan Stern65bfd292008-11-25 16:39:18 -05001122static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001123{
1124 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001125 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001126
Alan Stern0458d5b2007-05-04 11:52:20 -04001127 if (udev->state == USB_STATE_NOTATTACHED)
1128 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001129
Alan Stern1c5df7e2006-07-01 22:13:50 -04001130 /* Can't resume it if it doesn't have a driver. */
1131 if (udev->dev.driver == NULL) {
1132 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001133 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001134 }
1135
Alan Stern6d19c002010-02-12 12:21:11 +01001136 /* Non-root devices on a full/low-speed bus must wait for their
1137 * companion high-speed root hub, in case a handoff is needed.
1138 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001139 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001140 device_pm_wait_for_dev(&udev->dev,
1141 &udev->bus->hs_companion->root_hub->dev);
1142
Alan Stern6bc6cff2007-05-04 11:53:03 -04001143 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1144 udev->reset_resume = 1;
1145
Alan Stern1cc8a252006-07-01 22:10:15 -04001146 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001147 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001148
Alan Stern20dfdad2007-05-22 11:50:17 -04001149 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001150 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001151 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001152}
1153
Alan Stern65605ae2008-08-12 14:33:27 -04001154static int usb_suspend_interface(struct usb_device *udev,
1155 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001156{
1157 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001158 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001159
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001160 if (udev->state == USB_STATE_NOTATTACHED ||
1161 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001162 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001163 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001164
Oliver Neukume78832c2012-01-02 15:11:48 +01001165 /* at this time we know the driver supports suspend */
1166 status = driver->suspend(intf, msg);
1167 if (status && !PMSG_IS_AUTO(msg))
1168 dev_err(&intf->dev, "suspend error %d\n", status);
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(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001172 return status;
1173}
1174
Alan Stern65605ae2008-08-12 14:33:27 -04001175static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001176 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001177{
Alan Stern1cc8a252006-07-01 22:10:15 -04001178 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001179 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001180
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001181 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001182 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001183
Alan Stern645daaa2006-08-30 15:47:02 -04001184 /* Don't let autoresume interfere with unbinding */
1185 if (intf->condition == USB_INTERFACE_UNBINDING)
1186 goto done;
1187
Alan Stern1c5df7e2006-07-01 22:13:50 -04001188 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001189 if (intf->condition == USB_INTERFACE_UNBOUND) {
1190
1191 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001192 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001193 usb_set_interface(udev, intf->altsetting[0].
1194 desc.bInterfaceNumber, 0);
1195 intf->needs_altsetting0 = 0;
1196 }
Alan Stern2bf40862006-07-01 22:12:19 -04001197 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001198 }
Alan Stern78d9a482008-06-23 16:00:40 -04001199
1200 /* Don't resume if the interface is marked for rebinding */
1201 if (intf->needs_binding)
1202 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001203 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001204
Alan Sternf07600c2007-05-30 15:38:16 -04001205 if (reset_resume) {
1206 if (driver->reset_resume) {
1207 status = driver->reset_resume(intf);
1208 if (status)
1209 dev_err(&intf->dev, "%s error %d\n",
1210 "reset_resume", status);
1211 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001212 intf->needs_binding = 1;
Alan Stern0a56b4f2013-10-18 11:17:21 -04001213 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1214 driver->name);
Alan Sternf07600c2007-05-30 15:38:16 -04001215 }
1216 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001217 status = driver->resume(intf);
1218 if (status)
1219 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001220 }
Alan Stern2bf40862006-07-01 22:12:19 -04001221
1222done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001223 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001224
Alan Stern78d9a482008-06-23 16:00:40 -04001225 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001226 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001227}
1228
Alan Stern645daaa2006-08-30 15:47:02 -04001229/**
1230 * usb_suspend_both - suspend a USB device and its interfaces
1231 * @udev: the usb_device to suspend
1232 * @msg: Power Management message describing this state transition
1233 *
1234 * This is the central routine for suspending USB devices. It calls the
1235 * suspend methods for all the interface drivers in @udev and then calls
Ming Lei303f0842013-03-15 12:08:53 +08001236 * the suspend method for @udev itself. When the routine is called in
1237 * autosuspend, if an error occurs at any stage, all the interfaces
1238 * which were suspended are resumed so that they remain in the same
1239 * state as the device, but when called from system sleep, all error
1240 * from suspend methods of interfaces and the non-root-hub device itself
1241 * are simply ignored, so all suspended interfaces are only resumed
1242 * to the device's state when @udev is root-hub and its suspend method
1243 * returns failure.
Alan Stern645daaa2006-08-30 15:47:02 -04001244 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001245 * Autosuspend requests originating from a child device or an interface
1246 * driver may be made without the protection of @udev's device lock, but
1247 * all other suspend calls will hold the lock. Usbcore will insure that
1248 * method calls do not arrive during bind, unbind, or reset operations.
1249 * However drivers must be prepared to handle suspend calls arriving at
1250 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001251 *
1252 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001253 *
1254 * Return: 0 if the suspend succeeded.
Alan Stern645daaa2006-08-30 15:47:02 -04001255 */
Alan Stern718efa62007-03-09 15:41:13 -05001256static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001257{
1258 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001259 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001260 struct usb_interface *intf;
1261
Alan Stern19410442007-03-27 13:33:59 -04001262 if (udev->state == USB_STATE_NOTATTACHED ||
1263 udev->state == USB_STATE_SUSPENDED)
1264 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001265
Alan Stern645daaa2006-08-30 15:47:02 -04001266 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001267 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001268 n = udev->actconfig->desc.bNumInterfaces;
1269 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001270 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001271 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001272
1273 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001274 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001275 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001276 if (status != 0)
1277 break;
1278 }
1279 }
Alan Stern0af212b2011-06-15 16:27:43 -04001280 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001281 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001282
Alan Sterncd4376e2012-03-28 15:56:17 -04001283 /*
1284 * Ignore errors from non-root-hub devices during
1285 * system sleep transitions. For the most part,
1286 * these devices should go to low power anyway when
1287 * the entire bus is suspended.
1288 */
1289 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001290 status = 0;
1291 }
1292
Alan Sterna8e7c562006-07-01 22:11:02 -04001293 /* If the suspend failed, resume interfaces that did get suspended */
1294 if (status != 0) {
Chen Gang505bdbc2013-04-01 13:04:08 +08001295 if (udev->actconfig) {
1296 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1297 while (++i < n) {
1298 intf = udev->actconfig->interface[i];
1299 usb_resume_interface(udev, intf, msg, 0);
1300 }
Alan Sterna8e7c562006-07-01 22:11:02 -04001301 }
Alan Stern645daaa2006-08-30 15:47:02 -04001302
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001303 /* If the suspend succeeded then prevent any more URB submissions
1304 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001305 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001306 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001307 udev->can_submit = 0;
1308 for (i = 0; i < 16; ++i) {
1309 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1310 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1311 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001312 }
Alan Stern645daaa2006-08-30 15:47:02 -04001313
Alan Stern19410442007-03-27 13:33:59 -04001314 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001315 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001316 return status;
1317}
1318
Alan Stern645daaa2006-08-30 15:47:02 -04001319/**
1320 * usb_resume_both - resume a USB device and its interfaces
1321 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001322 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001323 *
1324 * This is the central routine for resuming USB devices. It calls the
1325 * the resume method for @udev and then calls the resume methods for all
1326 * the interface drivers in @udev.
1327 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001328 * Autoresume requests originating from a child device or an interface
1329 * driver may be made without the protection of @udev's device lock, but
1330 * all other resume calls will hold the lock. Usbcore will insure that
1331 * method calls do not arrive during bind, unbind, or reset operations.
1332 * However drivers must be prepared to handle resume calls arriving at
1333 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001334 *
1335 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001336 *
1337 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001338 */
Alan Stern65bfd292008-11-25 16:39:18 -05001339static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001340{
Alan Stern645daaa2006-08-30 15:47:02 -04001341 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001342 int i;
1343 struct usb_interface *intf;
1344
Alan Stern19410442007-03-27 13:33:59 -04001345 if (udev->state == USB_STATE_NOTATTACHED) {
1346 status = -ENODEV;
1347 goto done;
1348 }
Alan Stern6840d252007-09-10 11:34:26 -04001349 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001350
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001351 /* Resume the device */
1352 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001353 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001354
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001355 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001356 if (status == 0 && udev->actconfig) {
1357 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1358 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001359 usb_resume_interface(udev, intf, msg,
1360 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001361 }
1362 }
Alan Sternc08512c2010-11-15 15:57:58 -05001363 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001364
Alan Stern19410442007-03-27 13:33:59 -04001365 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001366 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001367 if (!status)
1368 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001369 return status;
1370}
1371
Alan Stern5f677f12010-04-02 13:20:11 -04001372static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1373{
Alan Stern48826622010-06-22 16:14:48 -04001374 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001375
1376 /* Remote wakeup is needed only when we actually go to sleep.
1377 * For things like FREEZE and QUIESCE, if the device is already
1378 * autosuspended then its current wakeup setting is okay.
1379 */
1380 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1381 if (udev->state != USB_STATE_SUSPENDED)
1382 udev->do_remote_wakeup = 0;
1383 return;
1384 }
1385
Alan Stern48826622010-06-22 16:14:48 -04001386 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001387 * actually want it.
1388 */
Alan Stern48826622010-06-22 16:14:48 -04001389 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001390
1391 /* If the device is autosuspended with the wrong wakeup setting,
1392 * autoresume now so the setting can be changed.
1393 */
1394 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1395 pm_runtime_resume(&udev->dev);
1396 udev->do_remote_wakeup = w;
1397}
1398
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001399/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001400int usb_suspend(struct device *dev, pm_message_t msg)
1401{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001402 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001403
Oliver Neukum14931382012-01-05 15:39:57 +01001404 unbind_no_pm_drivers_interfaces(udev);
1405
1406 /* From now on we are sure all drivers support suspend/resume
1407 * but not necessarily reset_resume()
1408 * so we may still need to unbind and rebind upon resume
1409 */
Alan Stern5f677f12010-04-02 13:20:11 -04001410 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001411 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001412}
1413
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001414/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001415int usb_resume_complete(struct device *dev)
1416{
1417 struct usb_device *udev = to_usb_device(dev);
1418
1419 /* For PM complete calls, all we do is rebind interfaces
1420 * whose needs_binding flag is set
1421 */
1422 if (udev->state != USB_STATE_NOTATTACHED)
1423 do_rebind_interfaces(udev);
1424 return 0;
1425}
1426
1427/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001428int usb_resume(struct device *dev, pm_message_t msg)
1429{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001430 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001431 int status;
1432
Oliver Neukum98d9a822012-01-11 08:38:35 +01001433 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001434 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001435 * Unbind the interfaces that will need rebinding later,
1436 * because they fail to support reset_resume.
1437 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001438 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001439 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001440 status = usb_resume_both(udev, msg);
1441 if (status == 0) {
1442 pm_runtime_disable(dev);
1443 pm_runtime_set_active(dev);
1444 pm_runtime_enable(dev);
1445 unbind_no_reset_resume_drivers_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001446 }
Alan Stern0c590e22010-01-08 12:57:14 -05001447
1448 /* Avoid PM error messages for devices disconnected while suspended
1449 * as we'll display regular disconnect messages just a bit later.
1450 */
Peter Chen7491f132010-09-27 16:43:25 +08001451 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001452 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001453 return status;
1454}
1455
1456#endif /* CONFIG_PM */
1457
Alan Stern84ebc102013-03-27 16:14:46 -04001458#ifdef CONFIG_PM_RUNTIME
Alan Stern645daaa2006-08-30 15:47:02 -04001459
Alan Stern088f7fe2010-01-08 12:56:54 -05001460/**
1461 * usb_enable_autosuspend - allow a USB device to be autosuspended
1462 * @udev: the USB device which may be autosuspended
1463 *
1464 * This routine allows @udev to be autosuspended. An autosuspend won't
1465 * take place until the autosuspend_delay has elapsed and all the other
1466 * necessary conditions are satisfied.
1467 *
1468 * The caller must hold @udev's device lock.
1469 */
Alan Stern9e18c822010-04-02 13:22:09 -04001470void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001471{
Alan Stern9e18c822010-04-02 13:22:09 -04001472 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001473}
1474EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1475
1476/**
1477 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1478 * @udev: the USB device which may not be autosuspended
1479 *
1480 * This routine prevents @udev from being autosuspended and wakes it up
1481 * if it is already autosuspended.
1482 *
1483 * The caller must hold @udev's device lock.
1484 */
Alan Stern9e18c822010-04-02 13:22:09 -04001485void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001486{
Alan Stern9e18c822010-04-02 13:22:09 -04001487 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001488}
1489EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1490
Alan Stern645daaa2006-08-30 15:47:02 -04001491/**
1492 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001493 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001494 *
1495 * This routine should be called when a core subsystem is finished using
1496 * @udev and wants to allow it to autosuspend. Examples would be when
1497 * @udev's device file in usbfs is closed or after a configuration change.
1498 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001499 * @udev's usage counter is decremented; if it drops to 0 and all the
1500 * interfaces are inactive then a delayed autosuspend will be attempted.
1501 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001502 *
Alan Stern62e299e2010-01-08 12:56:19 -05001503 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001504 *
1505 * This routine can run only in process context.
1506 */
Alan Stern94fcda12006-11-20 11:38:46 -05001507void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001508{
Alan Stern94fcda12006-11-20 11:38:46 -05001509 int status;
1510
Ming Lei6ddf27c2010-11-15 15:57:30 -05001511 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001512 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001513 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1514 __func__, atomic_read(&udev->dev.power.usage_count),
1515 status);
Alan Stern19c26232007-02-20 15:03:32 -05001516}
1517
1518/**
Alan Stern645daaa2006-08-30 15:47:02 -04001519 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001520 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001521 *
1522 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001523 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001524 * occur until usb_autosuspend_device() is called. (Note that this will
1525 * not prevent suspend events originating in the PM core.) Examples would
1526 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001527 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001528 *
Alan Stern94fcda12006-11-20 11:38:46 -05001529 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1530 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001531 *
Alan Stern62e299e2010-01-08 12:56:19 -05001532 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001533 *
1534 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001535 *
1536 * Return: 0 on success. A negative error code otherwise.
Alan Stern645daaa2006-08-30 15:47:02 -04001537 */
Alan Stern94fcda12006-11-20 11:38:46 -05001538int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001539{
1540 int status;
1541
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001542 status = pm_runtime_get_sync(&udev->dev);
1543 if (status < 0)
1544 pm_runtime_put_sync(&udev->dev);
1545 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1546 __func__, atomic_read(&udev->dev.power.usage_count),
1547 status);
1548 if (status > 0)
1549 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001550 return status;
1551}
1552
Alan Stern645daaa2006-08-30 15:47:02 -04001553/**
1554 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001555 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001556 *
1557 * This routine should be called by an interface driver when it is
1558 * finished using @intf and wants to allow it to autosuspend. A typical
1559 * example would be a character-device driver when its device file is
1560 * closed.
1561 *
1562 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001563 * 0, a delayed autosuspend request for @intf's device is attempted. The
1564 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001565 *
Alan Stern645daaa2006-08-30 15:47:02 -04001566 * This routine can run only in process context.
1567 */
1568void usb_autopm_put_interface(struct usb_interface *intf)
1569{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001570 struct usb_device *udev = interface_to_usbdev(intf);
1571 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001572
Ming Lei6ddf27c2010-11-15 15:57:30 -05001573 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001574 atomic_dec(&intf->pm_usage_cnt);
1575 status = pm_runtime_put_sync(&intf->dev);
1576 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1577 __func__, atomic_read(&intf->dev.power.usage_count),
1578 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001579}
1580EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1581
1582/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001583 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1584 * @intf: the usb_interface whose counter should be decremented
1585 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001586 * This routine does much the same thing as usb_autopm_put_interface():
1587 * It decrements @intf's usage counter and schedules a delayed
1588 * autosuspend request if the counter is <= 0. The difference is that it
1589 * does not perform any synchronization; callers should hold a private
1590 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001591 *
1592 * Typically a driver would call this routine during an URB's completion
1593 * handler, if no more URBs were pending.
1594 *
1595 * This routine can run in atomic context.
1596 */
1597void usb_autopm_put_interface_async(struct usb_interface *intf)
1598{
1599 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001600 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001601
Ming Lei6ddf27c2010-11-15 15:57:30 -05001602 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001603 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001604 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001605 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1606 __func__, atomic_read(&intf->dev.power.usage_count),
1607 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001608}
1609EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1610
1611/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001612 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1613 * @intf: the usb_interface whose counter should be decremented
1614 *
1615 * This routine decrements @intf's usage counter but does not carry out an
1616 * autosuspend.
1617 *
1618 * This routine can run in atomic context.
1619 */
1620void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1621{
1622 struct usb_device *udev = interface_to_usbdev(intf);
1623
Ming Lei6ddf27c2010-11-15 15:57:30 -05001624 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001625 atomic_dec(&intf->pm_usage_cnt);
1626 pm_runtime_put_noidle(&intf->dev);
1627}
1628EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1629
1630/**
Alan Stern645daaa2006-08-30 15:47:02 -04001631 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001632 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001633 *
1634 * This routine should be called by an interface driver when it wants to
1635 * use @intf and needs to guarantee that it is not suspended. In addition,
1636 * the routine prevents @intf from being autosuspended subsequently. (Note
1637 * that this will not prevent suspend events originating in the PM core.)
1638 * This prevention will persist until usb_autopm_put_interface() is called
1639 * or @intf is unbound. A typical example would be a character-device
1640 * driver when its device file is opened.
1641 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001642 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1643 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001644 *
1645 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001646 *
1647 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001648 */
1649int usb_autopm_get_interface(struct usb_interface *intf)
1650{
Alan Sternaf4f7602006-10-30 17:06:45 -05001651 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001652
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001653 status = pm_runtime_get_sync(&intf->dev);
1654 if (status < 0)
1655 pm_runtime_put_sync(&intf->dev);
1656 else
1657 atomic_inc(&intf->pm_usage_cnt);
1658 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1659 __func__, atomic_read(&intf->dev.power.usage_count),
1660 status);
1661 if (status > 0)
1662 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001663 return status;
1664}
1665EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1666
Alan Stern692a1862006-10-30 17:07:51 -05001667/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001668 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1669 * @intf: the usb_interface whose counter should be incremented
1670 *
1671 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001672 * usb_autopm_get_interface(): It increments @intf's usage counter and
1673 * queues an autoresume request if the device is suspended. The
1674 * differences are that it does not perform any synchronization (callers
1675 * should hold a private lock and handle all synchronization issues
1676 * themselves), and it does not autoresume the device directly (it only
1677 * queues a request). After a successful call, the device may not yet be
1678 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001679 *
1680 * This routine can run in atomic context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001681 *
1682 * Return: 0 on success. A negative error code otherwise.
Alan Stern9ac39f22008-11-12 16:19:49 -05001683 */
1684int usb_autopm_get_interface_async(struct usb_interface *intf)
1685{
Ming Lei63defa72010-11-15 15:56:54 -05001686 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001687
Ming Lei63defa72010-11-15 15:56:54 -05001688 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001689 if (status < 0 && status != -EINPROGRESS)
1690 pm_runtime_put_noidle(&intf->dev);
1691 else
Alan Sternccf5b802009-06-29 11:00:01 -04001692 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001693 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1694 __func__, atomic_read(&intf->dev.power.usage_count),
1695 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001696 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001697 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001698 return status;
1699}
1700EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1701
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001702/**
1703 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1704 * @intf: the usb_interface whose counter should be incremented
1705 *
1706 * This routine increments @intf's usage counter but does not carry out an
1707 * autoresume.
1708 *
1709 * This routine can run in atomic context.
1710 */
1711void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1712{
1713 struct usb_device *udev = interface_to_usbdev(intf);
1714
Ming Lei6ddf27c2010-11-15 15:57:30 -05001715 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001716 atomic_inc(&intf->pm_usage_cnt);
1717 pm_runtime_get_noresume(&intf->dev);
1718}
1719EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1720
1721/* Internal routine to check whether we may autosuspend a device. */
1722static int autosuspend_check(struct usb_device *udev)
1723{
Alan Stern7560d32e2010-04-02 13:18:50 -04001724 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001725 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001726
1727 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1728 * any interface drivers require remote wakeup but it isn't available.
1729 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001730 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001731 if (udev->actconfig) {
1732 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1733 intf = udev->actconfig->interface[i];
1734
1735 /* We don't need to check interfaces that are
1736 * disabled for runtime PM. Either they are unbound
1737 * or else their drivers don't support autosuspend
1738 * and so they are permanently active.
1739 */
1740 if (intf->dev.power.disable_depth)
1741 continue;
1742 if (atomic_read(&intf->dev.power.usage_count) > 0)
1743 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001744 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001745
1746 /* Don't allow autosuspend if the device will need
1747 * a reset-resume and any of its interface drivers
1748 * doesn't include support or needs remote wakeup.
1749 */
1750 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1751 struct usb_driver *driver;
1752
1753 driver = to_usb_driver(intf->dev.driver);
1754 if (!driver->reset_resume ||
1755 intf->needs_remote_wakeup)
1756 return -EOPNOTSUPP;
1757 }
1758 }
1759 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001760 if (w && !device_can_wakeup(&udev->dev)) {
1761 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1762 return -EOPNOTSUPP;
1763 }
1764 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001765 return 0;
1766}
1767
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001768int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001769{
Ming Lei63defa72010-11-15 15:56:54 -05001770 struct usb_device *udev = to_usb_device(dev);
1771 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001772
1773 /* A USB device can be suspended if it passes the various autosuspend
1774 * checks. Runtime suspend for a USB device means suspending all the
1775 * interfaces and then the device itself.
1776 */
Ming Lei63defa72010-11-15 15:56:54 -05001777 if (autosuspend_check(udev) != 0)
1778 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001779
Ming Lei63defa72010-11-15 15:56:54 -05001780 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001781
1782 /* Allow a retry if autosuspend failed temporarily */
1783 if (status == -EAGAIN || status == -EBUSY)
1784 usb_mark_last_busy(udev);
1785
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001786 /* The PM core reacts badly unless the return code is 0,
1787 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1788 */
1789 if (status != 0)
1790 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001791 return status;
1792}
1793
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001794int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001795{
Ming Lei63defa72010-11-15 15:56:54 -05001796 struct usb_device *udev = to_usb_device(dev);
1797 int status;
1798
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001799 /* Runtime resume for a USB device means resuming both the device
1800 * and all its interfaces.
1801 */
Ming Lei63defa72010-11-15 15:56:54 -05001802 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001803 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001804}
1805
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001806int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001807{
Ming Lei63defa72010-11-15 15:56:54 -05001808 struct usb_device *udev = to_usb_device(dev);
1809
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001810 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001811 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001812 */
Ming Lei63defa72010-11-15 15:56:54 -05001813 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001814 pm_runtime_autosuspend(dev);
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001815 /* Tell the core not to suspend it, though. */
1816 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001817}
1818
Andiry Xu65580b432011-09-23 14:19:52 -07001819int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1820{
1821 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1822 int ret = -EPERM;
1823
Sarah Sharpde68bab2013-09-30 17:26:28 +03001824 if (enable && !udev->usb2_hw_lpm_allowed)
1825 return 0;
1826
Andiry Xu65580b432011-09-23 14:19:52 -07001827 if (hcd->driver->set_usb2_hw_lpm) {
1828 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1829 if (!ret)
1830 udev->usb2_hw_lpm_enabled = enable;
1831 }
1832
1833 return ret;
1834}
1835
Alan Stern84ebc102013-03-27 16:14:46 -04001836#endif /* CONFIG_PM_RUNTIME */
Alan Stern645daaa2006-08-30 15:47:02 -04001837
Alan Stern36e56a32006-07-01 22:08:06 -04001838struct bus_type usb_bus_type = {
1839 .name = "usb",
1840 .match = usb_device_match,
1841 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001842};