blob: 5d01558cef666233b47f29d569595a1f42dda8b2 [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) {
66 if (bInterfaceClass > 255)
67 return -EINVAL;
68
Josua Dietzeff231db2011-10-23 14:22:29 +020069 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
70 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
71 }
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080072
Wolfram Sang2fc82c22014-01-10 19:36:42 +010073 if (fields > 4) {
74 const struct usb_device_id *id = id_table;
75
Wolfram Sang1b9fb312014-01-13 11:29:23 +010076 if (!id)
77 return -ENODEV;
78
Wolfram Sang2fc82c22014-01-10 19:36:42 +010079 for (; id->match_flags; id++)
Wolfram Sang52a6966c2014-01-12 10:07:50 +010080 if (id->idVendor == refVendor && id->idProduct == refProduct)
Wolfram Sang2fc82c22014-01-10 19:36:42 +010081 break;
Wolfram Sang52a6966c2014-01-12 10:07:50 +010082
83 if (id->match_flags)
84 dynid->id.driver_info = id->driver_info;
85 else
86 return -ENODEV;
Wolfram Sang2fc82c22014-01-10 19:36:42 +010087 }
88
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010089 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020090 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010091 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080092
Alan Sterncef9bc52012-01-24 13:34:41 -050093 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080094
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070095 if (retval)
96 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080097 return count;
98}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010099EXPORT_SYMBOL_GPL(usb_store_new_id);
100
Bjørn Morkef206f32012-05-13 12:35:00 +0200101ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200102{
103 struct usb_dynid *dynid;
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200104 size_t count = 0;
105
Bjørn Morkef206f32012-05-13 12:35:00 +0200106 list_for_each_entry(dynid, &dynids->list, node)
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200107 if (dynid->id.bInterfaceClass != 0)
108 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
109 dynid->id.idVendor, dynid->id.idProduct,
110 dynid->id.bInterfaceClass);
111 else
112 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
113 dynid->id.idVendor, dynid->id.idProduct);
114 return count;
115}
Bjørn Morkef206f32012-05-13 12:35:00 +0200116EXPORT_SYMBOL_GPL(usb_show_dynids);
117
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700118static ssize_t new_id_show(struct device_driver *driver, char *buf)
Bjørn Morkef206f32012-05-13 12:35:00 +0200119{
120 struct usb_driver *usb_drv = to_usb_driver(driver);
121
122 return usb_show_dynids(&usb_drv->dynids, buf);
123}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200124
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700125static ssize_t new_id_store(struct device_driver *driver,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100126 const char *buf, size_t count)
127{
128 struct usb_driver *usb_drv = to_usb_driver(driver);
129
Wolfram Sang2fc82c22014-01-10 19:36:42 +0100130 return usb_store_new_id(&usb_drv->dynids, usb_drv->id_table, driver, buf, count);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100131}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700132static DRIVER_ATTR_RW(new_id);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800133
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700134/*
135 * Remove a USB device ID from this driver
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800136 */
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700137static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
138 size_t count)
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800139{
140 struct usb_dynid *dynid, *n;
141 struct usb_driver *usb_driver = to_usb_driver(driver);
Alan Coxac08de32012-09-17 11:55:23 +0100142 u32 idVendor;
143 u32 idProduct;
144 int fields;
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800145
146 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
147 if (fields < 2)
148 return -EINVAL;
149
150 spin_lock(&usb_driver->dynids.lock);
151 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
152 struct usb_device_id *id = &dynid->id;
153 if ((id->idVendor == idVendor) &&
154 (id->idProduct == idProduct)) {
155 list_del(&dynid->node);
156 kfree(dynid);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800157 break;
158 }
159 }
160 spin_unlock(&usb_driver->dynids.lock);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800161 return count;
162}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700163
164static ssize_t remove_id_show(struct device_driver *driver, char *buf)
165{
166 return new_id_show(driver, buf);
167}
168static DRIVER_ATTR_RW(remove_id);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800169
Alan Sterned283e92012-01-24 14:35:13 -0500170static int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800171{
172 int error = 0;
173
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800174 if (usb_drv->no_dynamic_id)
175 goto exit;
176
Alan Sterned283e92012-01-24 14:35:13 -0500177 if (usb_drv->probe != NULL) {
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800178 error = driver_create_file(&usb_drv->drvwrap.driver,
179 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500180 if (error == 0) {
181 error = driver_create_file(&usb_drv->drvwrap.driver,
182 &driver_attr_remove_id);
183 if (error)
184 driver_remove_file(&usb_drv->drvwrap.driver,
185 &driver_attr_new_id);
186 }
187 }
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800188exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800189 return error;
190}
191
Alan Sterned283e92012-01-24 14:35:13 -0500192static void usb_remove_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800193{
194 if (usb_drv->no_dynamic_id)
195 return;
196
Alan Sterned283e92012-01-24 14:35:13 -0500197 if (usb_drv->probe != NULL) {
198 driver_remove_file(&usb_drv->drvwrap.driver,
199 &driver_attr_remove_id);
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800200 driver_remove_file(&usb_drv->drvwrap.driver,
201 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500202 }
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800203}
204
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800205static void usb_free_dynids(struct usb_driver *usb_drv)
206{
207 struct usb_dynid *dynid, *n;
208
209 spin_lock(&usb_drv->dynids.lock);
210 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
211 list_del(&dynid->node);
212 kfree(dynid);
213 }
214 spin_unlock(&usb_drv->dynids.lock);
215}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800216
217static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
218 struct usb_driver *drv)
219{
220 struct usb_dynid *dynid;
221
222 spin_lock(&drv->dynids.lock);
223 list_for_each_entry(dynid, &drv->dynids.list, node) {
224 if (usb_match_one_id(intf, &dynid->id)) {
225 spin_unlock(&drv->dynids.lock);
226 return &dynid->id;
227 }
228 }
229 spin_unlock(&drv->dynids.lock);
230 return NULL;
231}
232
233
Alan Stern8bb54ab2006-07-01 22:08:49 -0400234/* called from driver core with dev locked */
235static int usb_probe_device(struct device *dev)
236{
237 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200238 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500239 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400240
Harvey Harrison441b62c2008-03-03 16:08:34 -0800241 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400242
Alan Stern8bb54ab2006-07-01 22:08:49 -0400243 /* TODO: Add real matching code */
244
Alan Stern645daaa2006-08-30 15:47:02 -0400245 /* The device should always appear to be in use
Masanari Iida02582e92012-08-22 19:11:26 +0900246 * unless the driver supports autosuspend.
Alan Stern645daaa2006-08-30 15:47:02 -0400247 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500248 if (!udriver->supports_autosuspend)
249 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400250
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500251 if (!error)
252 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400253 return error;
254}
255
256/* called from driver core with dev locked */
257static int usb_unbind_device(struct device *dev)
258{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500259 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400260 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
261
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500262 udriver->disconnect(udev);
263 if (!udriver->supports_autosuspend)
264 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400265 return 0;
266}
267
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800268/*
269 * Cancel any pending scheduled resets
270 *
271 * [see usb_queue_reset_device()]
272 *
273 * Called after unconfiguring / when releasing interfaces. See
274 * comments in __usb_queue_reset_device() regarding
275 * udev->reset_running.
276 */
277static void usb_cancel_queued_reset(struct usb_interface *iface)
278{
279 if (iface->reset_running == 0)
280 cancel_work_sync(&iface->reset_ws);
281}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400282
283/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800284static int usb_probe_interface(struct device *dev)
285{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400286 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200287 struct usb_interface *intf = to_usb_interface(dev);
288 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800289 const struct usb_device_id *id;
290 int error = -ENODEV;
Sarah Sharp83060952012-05-02 14:25:52 -0700291 int lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800292
Harvey Harrison441b62c2008-03-03 16:08:34 -0800293 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800294
Alan Stern78d9a482008-06-23 16:00:40 -0400295 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800296
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400297 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500298 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400299
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800300 if (udev->authorized == 0) {
301 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500302 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800303 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700304
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800305 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800306 if (!id)
307 id = usb_match_dynamic_id(intf, driver);
Alan Stern0f3dda92010-01-08 12:56:04 -0500308 if (!id)
309 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800310
Alan Stern0f3dda92010-01-08 12:56:04 -0500311 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400312
Alan Stern0f3dda92010-01-08 12:56:04 -0500313 error = usb_autoresume_device(udev);
314 if (error)
315 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400316
Alan Stern0f3dda92010-01-08 12:56:04 -0500317 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400318
Alan Stern571dc792010-04-09 16:03:43 -0400319 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500320 * runtime-PM-enabled only if the driver has autosuspend support.
321 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500322 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500323 pm_runtime_set_active(dev);
324 pm_suspend_ignore_children(dev, false);
325 if (driver->supports_autosuspend)
326 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200327
Sarah Sharp83060952012-05-02 14:25:52 -0700328 /* If the new driver doesn't allow hub-initiated LPM, and we can't
329 * disable hub-initiated LPM, then fail the probe.
330 *
331 * Otherwise, leaving LPM enabled should be harmless, because the
332 * endpoint intervals should remain the same, and the U1/U2 timeouts
333 * should remain the same.
334 *
335 * If we need to install alt setting 0 before probe, or another alt
336 * setting during probe, that should also be fine. usb_set_interface()
337 * will attempt to disable LPM, and fail if it can't disable it.
338 */
339 lpm_disable_error = usb_unlocked_disable_lpm(udev);
340 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
341 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
342 __func__, driver->name);
343 error = lpm_disable_error;
344 goto err;
345 }
346
Alan Stern0f3dda92010-01-08 12:56:04 -0500347 /* Carry out a deferred switch to altsetting 0 */
348 if (intf->needs_altsetting0) {
349 error = usb_set_interface(udev, intf->altsetting[0].
350 desc.bInterfaceNumber, 0);
351 if (error < 0)
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200352 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500353 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800354 }
355
Alan Stern0f3dda92010-01-08 12:56:04 -0500356 error = driver->probe(intf, id);
357 if (error)
358 goto err;
359
360 intf->condition = USB_INTERFACE_BOUND;
Sarah Sharp83060952012-05-02 14:25:52 -0700361
362 /* If the LPM disable succeeded, balance the ref counts. */
363 if (!lpm_disable_error)
364 usb_unlocked_enable_lpm(udev);
365
Alan Stern0f3dda92010-01-08 12:56:04 -0500366 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800367 return error;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200368
Alan Stern0f3dda92010-01-08 12:56:04 -0500369 err:
Hans de Goedee714fad2012-05-22 11:36:59 +0200370 usb_set_intfdata(intf, NULL);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200371 intf->needs_remote_wakeup = 0;
372 intf->condition = USB_INTERFACE_UNBOUND;
373 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500374
Sarah Sharpd01f87c2012-10-04 09:53:43 -0700375 /* If the LPM disable succeeded, balance the ref counts. */
376 if (!lpm_disable_error)
377 usb_unlocked_enable_lpm(udev);
378
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500379 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400380 if (driver->supports_autosuspend)
381 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500382 pm_runtime_set_suspended(dev);
383
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200384 usb_autosuspend_device(udev);
385 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800386}
387
Alan Stern8bb54ab2006-07-01 22:08:49 -0400388/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800389static int usb_unbind_interface(struct device *dev)
390{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400391 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800392 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400393 struct usb_device *udev;
Sarah Sharp83060952012-05-02 14:25:52 -0700394 int error, r, lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800395
396 intf->condition = USB_INTERFACE_UNBINDING;
397
Alan Stern645daaa2006-08-30 15:47:02 -0400398 /* Autoresume for set_interface call below */
399 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500400 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400401
Sarah Sharp83060952012-05-02 14:25:52 -0700402 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
403 * the driver is unbound. If LPM isn't disabled, that's fine because it
404 * wouldn't be enabled unless all the bound interfaces supported
405 * hub-initiated LPM.
406 */
407 lpm_disable_error = usb_unlocked_disable_lpm(udev);
408
Alan Stern9da82bd2008-05-08 11:54:37 -0400409 /* Terminate all URBs for this interface unless the driver
410 * supports "soft" unbinding.
411 */
412 if (!driver->soft_unbind)
Alan Sternddeac4e72009-01-15 17:03:33 -0500413 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800414
Alan Stern8bb54ab2006-07-01 22:08:49 -0400415 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800416 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800417
Alan Stern55151d72008-08-12 14:33:59 -0400418 /* Reset other interface state.
419 * We cannot do a Set-Interface if the device is suspended or
420 * if it is prepared for a system sleep (since installing a new
421 * altsetting means creating new endpoint device entries).
422 * When either of these happens, defer the Set-Interface.
423 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500424 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
425 /* Already in altsetting 0 so skip Set-Interface.
426 * Just re-enable it without affecting the endpoint toggles.
427 */
428 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200429 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200430 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400431 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200432 if (r < 0)
433 intf->needs_altsetting0 = 1;
434 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400435 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200436 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800437 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400438
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800439 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400440 intf->needs_remote_wakeup = 0;
441
Sarah Sharp83060952012-05-02 14:25:52 -0700442 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
443 if (!lpm_disable_error)
444 usb_unlocked_enable_lpm(udev);
445
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500446 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400447 if (driver->supports_autosuspend)
448 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500449 pm_runtime_set_suspended(dev);
450
451 /* Undo any residual pm_autopm_get_interface_* calls */
452 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
453 usb_autopm_put_interface_no_suspend(intf);
454 atomic_set(&intf->pm_usage_cnt, 0);
455
Alan Stern645daaa2006-08-30 15:47:02 -0400456 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500457 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800458
459 return 0;
460}
461
Alan Stern36e56a32006-07-01 22:08:06 -0400462/**
463 * usb_driver_claim_interface - bind a driver to an interface
464 * @driver: the driver to be bound
465 * @iface: the interface to which it will be bound; must be in the
466 * usb device's active configuration
467 * @priv: driver data associated with that interface
468 *
469 * This is used by usb device drivers that need to claim more than one
470 * interface on a device when probing (audio and acm are current examples).
471 * No device driver should directly modify internal usb_interface or
472 * usb_device structure members.
473 *
474 * Few drivers should need to use this routine, since the most natural
475 * way to bind to an interface is to return the private data from
476 * the driver's probe() method.
477 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400478 * Callers must own the device lock, so driver probe() entries don't need
479 * extra locking, but other call contexts may need to explicitly claim that
480 * lock.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200481 *
482 * Return: 0 on success.
Alan Stern36e56a32006-07-01 22:08:06 -0400483 */
484int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800485 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400486{
487 struct device *dev = &iface->dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700488 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700489 int retval = 0;
Sarah Sharp83060952012-05-02 14:25:52 -0700490 int lpm_disable_error;
Alan Stern36e56a32006-07-01 22:08:06 -0400491
492 if (dev->driver)
493 return -EBUSY;
494
Sarah Sharp83060952012-05-02 14:25:52 -0700495 udev = interface_to_usbdev(iface);
496
Alan Stern8bb54ab2006-07-01 22:08:49 -0400497 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400498 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400499 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400500
Alan Stern36e56a32006-07-01 22:08:06 -0400501 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500502
Sarah Sharp83060952012-05-02 14:25:52 -0700503 /* Disable LPM until this driver is bound. */
504 lpm_disable_error = usb_unlocked_disable_lpm(udev);
505 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
506 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
507 __func__, driver->name);
508 return -ENOMEM;
509 }
510
Alan Stern89842ae2010-05-11 11:44:06 -0400511 /* Claimed interfaces are initially inactive (suspended) and
512 * runtime-PM-enabled, but only if the driver has autosuspend
513 * support. Otherwise they are marked active, to prevent the
514 * device from being autosuspended, but left disabled. In either
515 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500516 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500517 pm_suspend_ignore_children(dev, false);
518 if (driver->supports_autosuspend)
519 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400520 else
521 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400522
523 /* if interface was already added, bind now; else let
524 * the future device_add() bind it, bypassing probe()
525 */
526 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700527 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400528
Sarah Sharp83060952012-05-02 14:25:52 -0700529 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
530 if (!lpm_disable_error)
531 usb_unlocked_enable_lpm(udev);
532
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700533 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400534}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600535EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400536
537/**
538 * usb_driver_release_interface - unbind a driver from an interface
539 * @driver: the driver to be unbound
540 * @iface: the interface from which it will be unbound
541 *
542 * This can be used by drivers to release an interface without waiting
543 * for their disconnect() methods to be called. In typical cases this
544 * also causes the driver disconnect() method to be called.
545 *
546 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400547 * Callers must own the device lock, so driver disconnect() entries don't
548 * need extra locking, but other call contexts may need to explicitly claim
549 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400550 */
551void usb_driver_release_interface(struct usb_driver *driver,
552 struct usb_interface *iface)
553{
554 struct device *dev = &iface->dev;
555
556 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400557 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400558 return;
559
560 /* don't release from within disconnect() */
561 if (iface->condition != USB_INTERFACE_BOUND)
562 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400563 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400564
Alan Stern91f8d062009-04-16 15:35:09 -0400565 /* Release via the driver core only if the interface
566 * has already been registered
567 */
Alan Stern36e56a32006-07-01 22:08:06 -0400568 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400569 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800570 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800571 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400572 usb_unbind_interface(dev);
573 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800574 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400575 }
Alan Stern36e56a32006-07-01 22:08:06 -0400576}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600577EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400578
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800579/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100580int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800581{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800582 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
583 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
584 return 0;
585
586 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
587 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
588 return 0;
589
590 /* No need to test id->bcdDevice_lo != 0, since 0 is never
591 greater than any unsigned number. */
592 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
593 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
594 return 0;
595
596 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
597 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
598 return 0;
599
600 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
601 (id->bDeviceClass != dev->descriptor.bDeviceClass))
602 return 0;
603
604 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800605 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800606 return 0;
607
608 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
609 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
610 return 0;
611
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100612 return 1;
613}
614
615/* returns 0 if no match, 1 if match */
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200616int usb_match_one_id_intf(struct usb_device *dev,
617 struct usb_host_interface *intf,
618 const struct usb_device_id *id)
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100619{
Bjørn Mork81df2d52012-05-18 21:27:43 +0200620 /* The interface class, subclass, protocol and number should never be
Alan Stern93c8bf42006-10-18 16:41:51 -0400621 * checked for a match if the device class is Vendor Specific,
622 * unless the match record specifies the Vendor ID. */
623 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
624 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
625 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
626 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
Bjørn Mork81df2d52012-05-18 21:27:43 +0200627 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
628 USB_DEVICE_ID_MATCH_INT_NUMBER)))
Alan Stern93c8bf42006-10-18 16:41:51 -0400629 return 0;
630
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800631 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
632 (id->bInterfaceClass != intf->desc.bInterfaceClass))
633 return 0;
634
635 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
636 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
637 return 0;
638
639 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
640 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
641 return 0;
642
Bjørn Mork81df2d52012-05-18 21:27:43 +0200643 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
644 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
645 return 0;
646
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800647 return 1;
648}
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200649
650/* returns 0 if no match, 1 if match */
651int usb_match_one_id(struct usb_interface *interface,
652 const struct usb_device_id *id)
653{
654 struct usb_host_interface *intf;
655 struct usb_device *dev;
656
657 /* proc_connectinfo in devio.c may call us with id == NULL. */
658 if (id == NULL)
659 return 0;
660
661 intf = interface->cur_altsetting;
662 dev = interface_to_usbdev(interface);
663
664 if (!usb_match_device(dev, id))
665 return 0;
666
667 return usb_match_one_id_intf(dev, intf, id);
668}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100669EXPORT_SYMBOL_GPL(usb_match_one_id);
670
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800671/**
672 * usb_match_id - find first usb_device_id matching device or interface
673 * @interface: the interface of interest
674 * @id: array of usb_device_id structures, terminated by zero entry
675 *
676 * usb_match_id searches an array of usb_device_id's and returns
677 * the first one matching the device or interface, or null.
678 * This is used when binding (or rebinding) a driver to an interface.
679 * Most USB device drivers will use this indirectly, through the usb core,
680 * but some layered driver frameworks use it directly.
681 * These device tables are exported with MODULE_DEVICE_TABLE, through
682 * modutils, to support the driver loading functionality of USB hotplugging.
683 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200684 * Return: The first matching usb_device_id, or %NULL.
685 *
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800686 * What Matches:
687 *
688 * The "match_flags" element in a usb_device_id controls which
689 * members are used. If the corresponding bit is set, the
690 * value in the device_id must match its corresponding member
691 * in the device or interface descriptor, or else the device_id
692 * does not match.
693 *
694 * "driver_info" is normally used only by device drivers,
695 * but you can create a wildcard "matches anything" usb_device_id
696 * as a driver's "modules.usbmap" entry if you provide an id with
697 * only a nonzero "driver_info" field. If you do this, the USB device
698 * driver's probe() routine should use additional intelligence to
699 * decide whether to bind to the specified interface.
700 *
701 * What Makes Good usb_device_id Tables:
702 *
703 * The match algorithm is very simple, so that intelligence in
704 * driver selection must come from smart driver id records.
705 * Unless you have good reasons to use another selection policy,
706 * provide match elements only in related groups, and order match
707 * specifiers from specific to general. Use the macros provided
708 * for that purpose if you can.
709 *
710 * The most specific match specifiers use device descriptor
711 * data. These are commonly used with product-specific matches;
712 * the USB_DEVICE macro lets you provide vendor and product IDs,
713 * and you can also match against ranges of product revisions.
714 * These are widely used for devices with application or vendor
715 * specific bDeviceClass values.
716 *
717 * Matches based on device class/subclass/protocol specifications
718 * are slightly more general; use the USB_DEVICE_INFO macro, or
719 * its siblings. These are used with single-function devices
720 * where bDeviceClass doesn't specify that each interface has
721 * its own class.
722 *
723 * Matches based on interface class/subclass/protocol are the
724 * most general; they let drivers bind to any interface on a
725 * multiple-function device. Use the USB_INTERFACE_INFO
726 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400727 * devices (as recorded in bInterfaceClass).
728 *
729 * Note that an entry created by USB_INTERFACE_INFO won't match
730 * any interface if the device class is set to Vendor-Specific.
731 * This is deliberate; according to the USB spec the meanings of
732 * the interface class/subclass/protocol for these devices are also
733 * vendor-specific, and hence matching against a standard product
734 * class wouldn't work anyway. If you really want to use an
735 * interface-based match for such a device, create a match record
736 * that also specifies the vendor ID. (Unforunately there isn't a
737 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800738 *
739 * Within those groups, remember that not all combinations are
740 * meaningful. For example, don't give a product version range
741 * without vendor and product IDs; or specify a protocol without
742 * its associated class and subclass.
743 */
744const struct usb_device_id *usb_match_id(struct usb_interface *interface,
745 const struct usb_device_id *id)
746{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800747 /* proc_connectinfo in devio.c may call us with id == NULL. */
748 if (id == NULL)
749 return NULL;
750
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800751 /* It is important to check that id->driver_info is nonzero,
752 since an entry that is all zeroes except for a nonzero
753 id->driver_info is the way to create an entry that
754 indicates that the driver want to examine every
755 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800756 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
757 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800758 if (usb_match_one_id(interface, id))
759 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800760 }
761
762 return NULL;
763}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600764EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800765
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100766static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800767{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400768 /* devices and interfaces are handled separately */
769 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800770
Alan Stern8bb54ab2006-07-01 22:08:49 -0400771 /* interface drivers never match devices */
772 if (!is_usb_device_driver(drv))
773 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800774
Alan Stern8bb54ab2006-07-01 22:08:49 -0400775 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800776 return 1;
777
Kay Sievers55129662009-05-04 19:48:32 +0200778 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400779 struct usb_interface *intf;
780 struct usb_driver *usb_drv;
781 const struct usb_device_id *id;
782
783 /* device drivers never match interfaces */
784 if (is_usb_device_driver(drv))
785 return 0;
786
787 intf = to_usb_interface(dev);
788 usb_drv = to_usb_driver(drv);
789
790 id = usb_match_id(intf, usb_drv->id_table);
791 if (id)
792 return 1;
793
794 id = usb_match_dynamic_id(intf, usb_drv);
795 if (id)
796 return 1;
797 }
798
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800799 return 0;
800}
801
Kay Sievers7eff2e72007-08-14 15:15:12 +0200802static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400803{
Alan Stern36e56a32006-07-01 22:08:06 -0400804 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400805
Kay Sievers55129662009-05-04 19:48:32 +0200806 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400807 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200808 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100809 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200810
Alan Stern8bb54ab2006-07-01 22:08:49 -0400811 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200812 } else {
813 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400814 }
Alan Stern36e56a32006-07-01 22:08:06 -0400815
816 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500817 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200818 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400819 return -ENODEV;
820 }
821 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200822 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400823 return -ENODEV;
824 }
825
Alan Stern36e56a32006-07-01 22:08:06 -0400826 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200827 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400828 le16_to_cpu(usb_dev->descriptor.idVendor),
829 le16_to_cpu(usb_dev->descriptor.idProduct),
830 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
831 return -ENOMEM;
832
833 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200834 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400835 usb_dev->descriptor.bDeviceClass,
836 usb_dev->descriptor.bDeviceSubClass,
837 usb_dev->descriptor.bDeviceProtocol))
838 return -ENOMEM;
839
Alan Stern36e56a32006-07-01 22:08:06 -0400840 return 0;
841}
842
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800843/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400844 * usb_register_device_driver - register a USB device (not interface) driver
845 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800846 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800847 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400848 * Registers a USB device driver with the USB core. The list of
849 * unattached devices will be rescanned whenever a new driver is
850 * added, allowing the new driver to attach to any recognized devices.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200851 *
852 * Return: A negative error code on failure and 0 on success.
Alan Stern8bb54ab2006-07-01 22:08:49 -0400853 */
854int usb_register_device_driver(struct usb_device_driver *new_udriver,
855 struct module *owner)
856{
857 int retval = 0;
858
859 if (usb_disabled())
860 return -ENODEV;
861
862 new_udriver->drvwrap.for_devices = 1;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100863 new_udriver->drvwrap.driver.name = new_udriver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400864 new_udriver->drvwrap.driver.bus = &usb_bus_type;
865 new_udriver->drvwrap.driver.probe = usb_probe_device;
866 new_udriver->drvwrap.driver.remove = usb_unbind_device;
867 new_udriver->drvwrap.driver.owner = owner;
868
869 retval = driver_register(&new_udriver->drvwrap.driver);
870
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700871 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400872 pr_info("%s: registered new device driver %s\n",
873 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700874 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400875 printk(KERN_ERR "%s: error %d registering device "
876 " driver %s\n",
877 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400878
879 return retval;
880}
881EXPORT_SYMBOL_GPL(usb_register_device_driver);
882
883/**
884 * usb_deregister_device_driver - unregister a USB device (not interface) driver
885 * @udriver: USB operations of the device driver to unregister
886 * Context: must be able to sleep
887 *
888 * Unlinks the specified driver from the internal USB driver list.
889 */
890void usb_deregister_device_driver(struct usb_device_driver *udriver)
891{
892 pr_info("%s: deregistering device driver %s\n",
893 usbcore_name, udriver->name);
894
895 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400896}
897EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
898
899/**
900 * usb_register_driver - register a USB interface driver
901 * @new_driver: USB operations for the interface driver
902 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800903 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400904 *
905 * Registers a USB interface driver with the USB core. The list of
906 * unattached interfaces will be rescanned whenever a new driver is
907 * added, allowing the new driver to attach to any recognized interfaces.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200908 *
909 * Return: A negative error code on failure and 0 on success.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800910 *
911 * NOTE: if you want your driver to use the USB major number, you must call
912 * usb_register_dev() to enable that functionality. This function no longer
913 * takes care of that.
914 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800915int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
916 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800917{
918 int retval = 0;
919
920 if (usb_disabled())
921 return -ENODEV;
922
Alan Stern8bb54ab2006-07-01 22:08:49 -0400923 new_driver->drvwrap.for_devices = 0;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100924 new_driver->drvwrap.driver.name = new_driver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400925 new_driver->drvwrap.driver.bus = &usb_bus_type;
926 new_driver->drvwrap.driver.probe = usb_probe_interface;
927 new_driver->drvwrap.driver.remove = usb_unbind_interface;
928 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800929 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800930 spin_lock_init(&new_driver->dynids.lock);
931 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800932
Alan Stern8bb54ab2006-07-01 22:08:49 -0400933 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800934 if (retval)
935 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800936
Alan Sterned283e92012-01-24 14:35:13 -0500937 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800938 if (retval)
939 goto out_newid;
940
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800941 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800942 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800943
944out:
945 return retval;
946
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800947out_newid:
948 driver_unregister(&new_driver->drvwrap.driver);
949
950 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400951 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800952 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800953 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800954}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600955EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800956
957/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400958 * usb_deregister - unregister a USB interface driver
959 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800960 * Context: must be able to sleep
961 *
962 * Unlinks the specified driver from the internal USB driver list.
963 *
964 * NOTE: If you called usb_register_dev(), you still need to call
965 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
966 * this * call will no longer do it for you.
967 */
968void usb_deregister(struct usb_driver *driver)
969{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400970 pr_info("%s: deregistering interface driver %s\n",
971 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800972
Alan Sterned283e92012-01-24 14:35:13 -0500973 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400974 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -0500975 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800976}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600977EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400978
Alan Stern78d9a482008-06-23 16:00:40 -0400979/* Forced unbinding of a USB interface driver, either because
980 * it doesn't support pre_reset/post_reset/reset_resume or
981 * because it doesn't support suspend/resume.
982 *
983 * The caller must hold @intf's device's lock, but not its pm_mutex
984 * and not @intf->dev.sem.
985 */
986void usb_forced_unbind_intf(struct usb_interface *intf)
987{
988 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
989
990 dev_dbg(&intf->dev, "forced unbind\n");
991 usb_driver_release_interface(driver, intf);
992
993 /* Mark the interface for later rebinding */
994 intf->needs_binding = 1;
995}
996
997/* Delayed forced unbinding of a USB interface driver and scan
998 * for rebinding.
999 *
1000 * The caller must hold @intf's device's lock, but not its pm_mutex
1001 * and not @intf->dev.sem.
1002 *
Alan Stern5096aed2008-08-12 14:34:14 -04001003 * Note: Rebinds will be skipped if a system sleep transition is in
1004 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -04001005 */
1006void usb_rebind_intf(struct usb_interface *intf)
1007{
1008 int rc;
1009
1010 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +01001011 if (intf->dev.driver)
1012 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001013
1014 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +02001015 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -04001016 intf->needs_binding = 0;
1017 rc = device_attach(&intf->dev);
1018 if (rc < 0)
1019 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1020 }
Alan Stern78d9a482008-06-23 16:00:40 -04001021}
1022
Alan Stern9ff78432008-08-07 13:04:51 -04001023#ifdef CONFIG_PM
1024
Oliver Neukum14931382012-01-05 15:39:57 +01001025/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1026 * There is no check for reset_resume here because it can be determined
1027 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001028 *
1029 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001030 */
Oliver Neukum14931382012-01-05 15:39:57 +01001031static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001032{
1033 struct usb_host_config *config;
1034 int i;
1035 struct usb_interface *intf;
1036 struct usb_driver *drv;
1037
1038 config = udev->actconfig;
1039 if (config) {
1040 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1041 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001042
1043 if (intf->dev.driver) {
1044 drv = to_usb_driver(intf->dev.driver);
1045 if (!drv->suspend || !drv->resume)
1046 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001047 }
1048 }
1049 }
1050}
1051
Oliver Neukum14931382012-01-05 15:39:57 +01001052/* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1053 * These interfaces have the needs_binding flag set by usb_resume_interface().
1054 *
1055 * The caller must hold @udev's device lock.
1056 */
1057static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
1058{
1059 struct usb_host_config *config;
1060 int i;
1061 struct usb_interface *intf;
1062
1063 config = udev->actconfig;
1064 if (config) {
1065 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1066 intf = config->interface[i];
1067 if (intf->dev.driver && intf->needs_binding)
1068 usb_forced_unbind_intf(intf);
1069 }
1070 }
1071}
1072
1073static void do_rebind_interfaces(struct usb_device *udev)
1074{
1075 struct usb_host_config *config;
1076 int i;
1077 struct usb_interface *intf;
1078
1079 config = udev->actconfig;
1080 if (config) {
1081 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1082 intf = config->interface[i];
1083 if (intf->needs_binding)
1084 usb_rebind_intf(intf);
1085 }
1086 }
1087}
1088
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001089static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001090{
Alan Stern782da722006-07-01 22:09:35 -04001091 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001092 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001093
Alan Stern114b3682006-07-01 22:13:04 -04001094 if (udev->state == USB_STATE_NOTATTACHED ||
1095 udev->state == USB_STATE_SUSPENDED)
1096 goto done;
1097
Alan Sternb6f64362007-05-04 11:51:54 -04001098 /* For devices that don't have a driver, we do a generic suspend. */
1099 if (udev->dev.driver)
1100 udriver = to_usb_device_driver(udev->dev.driver);
1101 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001102 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001103 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001104 }
Alan Stern2bf40862006-07-01 22:12:19 -04001105 status = udriver->suspend(udev, msg);
1106
Alan Stern20dfdad2007-05-22 11:50:17 -04001107 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001108 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001109 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001110}
Alan Stern36e56a32006-07-01 22:08:06 -04001111
Alan Stern65bfd292008-11-25 16:39:18 -05001112static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001113{
1114 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001115 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001116
Alan Stern0458d5b2007-05-04 11:52:20 -04001117 if (udev->state == USB_STATE_NOTATTACHED)
1118 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001119
Alan Stern1c5df7e2006-07-01 22:13:50 -04001120 /* Can't resume it if it doesn't have a driver. */
1121 if (udev->dev.driver == NULL) {
1122 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001123 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001124 }
1125
Alan Stern6d19c002010-02-12 12:21:11 +01001126 /* Non-root devices on a full/low-speed bus must wait for their
1127 * companion high-speed root hub, in case a handoff is needed.
1128 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001129 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001130 device_pm_wait_for_dev(&udev->dev,
1131 &udev->bus->hs_companion->root_hub->dev);
1132
Alan Stern6bc6cff2007-05-04 11:53:03 -04001133 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1134 udev->reset_resume = 1;
1135
Alan Stern1cc8a252006-07-01 22:10:15 -04001136 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001137 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001138
Alan Stern20dfdad2007-05-22 11:50:17 -04001139 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001140 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001141 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001142}
1143
Alan Stern65605ae2008-08-12 14:33:27 -04001144static int usb_suspend_interface(struct usb_device *udev,
1145 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001146{
1147 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001148 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001149
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001150 if (udev->state == USB_STATE_NOTATTACHED ||
1151 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001152 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001153 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001154
Oliver Neukume78832c2012-01-02 15:11:48 +01001155 /* at this time we know the driver supports suspend */
1156 status = driver->suspend(intf, msg);
1157 if (status && !PMSG_IS_AUTO(msg))
1158 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001159
Alan Stern20dfdad2007-05-22 11:50:17 -04001160 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001161 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001162 return status;
1163}
1164
Alan Stern65605ae2008-08-12 14:33:27 -04001165static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001166 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001167{
Alan Stern1cc8a252006-07-01 22:10:15 -04001168 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001169 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001170
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001171 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001172 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001173
Alan Stern645daaa2006-08-30 15:47:02 -04001174 /* Don't let autoresume interfere with unbinding */
1175 if (intf->condition == USB_INTERFACE_UNBINDING)
1176 goto done;
1177
Alan Stern1c5df7e2006-07-01 22:13:50 -04001178 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001179 if (intf->condition == USB_INTERFACE_UNBOUND) {
1180
1181 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001182 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001183 usb_set_interface(udev, intf->altsetting[0].
1184 desc.bInterfaceNumber, 0);
1185 intf->needs_altsetting0 = 0;
1186 }
Alan Stern2bf40862006-07-01 22:12:19 -04001187 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001188 }
Alan Stern78d9a482008-06-23 16:00:40 -04001189
1190 /* Don't resume if the interface is marked for rebinding */
1191 if (intf->needs_binding)
1192 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001193 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001194
Alan Sternf07600c2007-05-30 15:38:16 -04001195 if (reset_resume) {
1196 if (driver->reset_resume) {
1197 status = driver->reset_resume(intf);
1198 if (status)
1199 dev_err(&intf->dev, "%s error %d\n",
1200 "reset_resume", status);
1201 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001202 intf->needs_binding = 1;
Alan Stern0a56b4f2013-10-18 11:17:21 -04001203 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1204 driver->name);
Alan Sternf07600c2007-05-30 15:38:16 -04001205 }
1206 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001207 status = driver->resume(intf);
1208 if (status)
1209 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001210 }
Alan Stern2bf40862006-07-01 22:12:19 -04001211
1212done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001213 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001214
Alan Stern78d9a482008-06-23 16:00:40 -04001215 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001216 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001217}
1218
Alan Stern645daaa2006-08-30 15:47:02 -04001219/**
1220 * usb_suspend_both - suspend a USB device and its interfaces
1221 * @udev: the usb_device to suspend
1222 * @msg: Power Management message describing this state transition
1223 *
1224 * This is the central routine for suspending USB devices. It calls the
1225 * suspend methods for all the interface drivers in @udev and then calls
Ming Lei303f0842013-03-15 12:08:53 +08001226 * the suspend method for @udev itself. When the routine is called in
1227 * autosuspend, if an error occurs at any stage, all the interfaces
1228 * which were suspended are resumed so that they remain in the same
1229 * state as the device, but when called from system sleep, all error
1230 * from suspend methods of interfaces and the non-root-hub device itself
1231 * are simply ignored, so all suspended interfaces are only resumed
1232 * to the device's state when @udev is root-hub and its suspend method
1233 * returns failure.
Alan Stern645daaa2006-08-30 15:47:02 -04001234 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001235 * Autosuspend requests originating from a child device or an interface
1236 * driver may be made without the protection of @udev's device lock, but
1237 * all other suspend calls will hold the lock. Usbcore will insure that
1238 * method calls do not arrive during bind, unbind, or reset operations.
1239 * However drivers must be prepared to handle suspend calls arriving at
1240 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001241 *
1242 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001243 *
1244 * Return: 0 if the suspend succeeded.
Alan Stern645daaa2006-08-30 15:47:02 -04001245 */
Alan Stern718efa62007-03-09 15:41:13 -05001246static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001247{
1248 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001249 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001250 struct usb_interface *intf;
1251
Alan Stern19410442007-03-27 13:33:59 -04001252 if (udev->state == USB_STATE_NOTATTACHED ||
1253 udev->state == USB_STATE_SUSPENDED)
1254 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001255
Alan Stern645daaa2006-08-30 15:47:02 -04001256 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001257 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001258 n = udev->actconfig->desc.bNumInterfaces;
1259 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001260 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001261 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001262
1263 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001264 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001265 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001266 if (status != 0)
1267 break;
1268 }
1269 }
Alan Stern0af212b2011-06-15 16:27:43 -04001270 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001271 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001272
Alan Sterncd4376e2012-03-28 15:56:17 -04001273 /*
1274 * Ignore errors from non-root-hub devices during
1275 * system sleep transitions. For the most part,
1276 * these devices should go to low power anyway when
1277 * the entire bus is suspended.
1278 */
1279 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001280 status = 0;
1281 }
1282
Alan Sterna8e7c562006-07-01 22:11:02 -04001283 /* If the suspend failed, resume interfaces that did get suspended */
1284 if (status != 0) {
Chen Gang505bdbc2013-04-01 13:04:08 +08001285 if (udev->actconfig) {
1286 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1287 while (++i < n) {
1288 intf = udev->actconfig->interface[i];
1289 usb_resume_interface(udev, intf, msg, 0);
1290 }
Alan Sterna8e7c562006-07-01 22:11:02 -04001291 }
Alan Stern645daaa2006-08-30 15:47:02 -04001292
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001293 /* If the suspend succeeded then prevent any more URB submissions
1294 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001295 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001296 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001297 udev->can_submit = 0;
1298 for (i = 0; i < 16; ++i) {
1299 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1300 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1301 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001302 }
Alan Stern645daaa2006-08-30 15:47:02 -04001303
Alan Stern19410442007-03-27 13:33:59 -04001304 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001305 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001306 return status;
1307}
1308
Alan Stern645daaa2006-08-30 15:47:02 -04001309/**
1310 * usb_resume_both - resume a USB device and its interfaces
1311 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001312 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001313 *
1314 * This is the central routine for resuming USB devices. It calls the
1315 * the resume method for @udev and then calls the resume methods for all
1316 * the interface drivers in @udev.
1317 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001318 * Autoresume requests originating from a child device or an interface
1319 * driver may be made without the protection of @udev's device lock, but
1320 * all other resume calls will hold the lock. Usbcore will insure that
1321 * method calls do not arrive during bind, unbind, or reset operations.
1322 * However drivers must be prepared to handle resume calls arriving at
1323 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001324 *
1325 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001326 *
1327 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001328 */
Alan Stern65bfd292008-11-25 16:39:18 -05001329static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001330{
Alan Stern645daaa2006-08-30 15:47:02 -04001331 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001332 int i;
1333 struct usb_interface *intf;
1334
Alan Stern19410442007-03-27 13:33:59 -04001335 if (udev->state == USB_STATE_NOTATTACHED) {
1336 status = -ENODEV;
1337 goto done;
1338 }
Alan Stern6840d252007-09-10 11:34:26 -04001339 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001340
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001341 /* Resume the device */
1342 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001343 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001344
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001345 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001346 if (status == 0 && udev->actconfig) {
1347 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1348 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001349 usb_resume_interface(udev, intf, msg,
1350 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001351 }
1352 }
Alan Sternc08512c2010-11-15 15:57:58 -05001353 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001354
Alan Stern19410442007-03-27 13:33:59 -04001355 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001356 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001357 if (!status)
1358 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001359 return status;
1360}
1361
Alan Stern5f677f12010-04-02 13:20:11 -04001362static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1363{
Alan Stern48826622010-06-22 16:14:48 -04001364 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001365
1366 /* Remote wakeup is needed only when we actually go to sleep.
1367 * For things like FREEZE and QUIESCE, if the device is already
1368 * autosuspended then its current wakeup setting is okay.
1369 */
1370 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1371 if (udev->state != USB_STATE_SUSPENDED)
1372 udev->do_remote_wakeup = 0;
1373 return;
1374 }
1375
Alan Stern48826622010-06-22 16:14:48 -04001376 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001377 * actually want it.
1378 */
Alan Stern48826622010-06-22 16:14:48 -04001379 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001380
1381 /* If the device is autosuspended with the wrong wakeup setting,
1382 * autoresume now so the setting can be changed.
1383 */
1384 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1385 pm_runtime_resume(&udev->dev);
1386 udev->do_remote_wakeup = w;
1387}
1388
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001389/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001390int usb_suspend(struct device *dev, pm_message_t msg)
1391{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001392 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001393
Oliver Neukum14931382012-01-05 15:39:57 +01001394 unbind_no_pm_drivers_interfaces(udev);
1395
1396 /* From now on we are sure all drivers support suspend/resume
1397 * but not necessarily reset_resume()
1398 * so we may still need to unbind and rebind upon resume
1399 */
Alan Stern5f677f12010-04-02 13:20:11 -04001400 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001401 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001402}
1403
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001404/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001405int usb_resume_complete(struct device *dev)
1406{
1407 struct usb_device *udev = to_usb_device(dev);
1408
1409 /* For PM complete calls, all we do is rebind interfaces
1410 * whose needs_binding flag is set
1411 */
1412 if (udev->state != USB_STATE_NOTATTACHED)
1413 do_rebind_interfaces(udev);
1414 return 0;
1415}
1416
1417/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001418int usb_resume(struct device *dev, pm_message_t msg)
1419{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001420 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001421 int status;
1422
Oliver Neukum98d9a822012-01-11 08:38:35 +01001423 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001424 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001425 * Unbind the interfaces that will need rebinding later,
1426 * because they fail to support reset_resume.
1427 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001428 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001429 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001430 status = usb_resume_both(udev, msg);
1431 if (status == 0) {
1432 pm_runtime_disable(dev);
1433 pm_runtime_set_active(dev);
1434 pm_runtime_enable(dev);
1435 unbind_no_reset_resume_drivers_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001436 }
Alan Stern0c590e22010-01-08 12:57:14 -05001437
1438 /* Avoid PM error messages for devices disconnected while suspended
1439 * as we'll display regular disconnect messages just a bit later.
1440 */
Peter Chen7491f132010-09-27 16:43:25 +08001441 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001442 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001443 return status;
1444}
1445
1446#endif /* CONFIG_PM */
1447
Alan Stern84ebc102013-03-27 16:14:46 -04001448#ifdef CONFIG_PM_RUNTIME
Alan Stern645daaa2006-08-30 15:47:02 -04001449
Alan Stern088f7fe2010-01-08 12:56:54 -05001450/**
1451 * usb_enable_autosuspend - allow a USB device to be autosuspended
1452 * @udev: the USB device which may be autosuspended
1453 *
1454 * This routine allows @udev to be autosuspended. An autosuspend won't
1455 * take place until the autosuspend_delay has elapsed and all the other
1456 * necessary conditions are satisfied.
1457 *
1458 * The caller must hold @udev's device lock.
1459 */
Alan Stern9e18c822010-04-02 13:22:09 -04001460void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001461{
Alan Stern9e18c822010-04-02 13:22:09 -04001462 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001463}
1464EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1465
1466/**
1467 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1468 * @udev: the USB device which may not be autosuspended
1469 *
1470 * This routine prevents @udev from being autosuspended and wakes it up
1471 * if it is already autosuspended.
1472 *
1473 * The caller must hold @udev's device lock.
1474 */
Alan Stern9e18c822010-04-02 13:22:09 -04001475void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001476{
Alan Stern9e18c822010-04-02 13:22:09 -04001477 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001478}
1479EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1480
Alan Stern645daaa2006-08-30 15:47:02 -04001481/**
1482 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001483 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001484 *
1485 * This routine should be called when a core subsystem is finished using
1486 * @udev and wants to allow it to autosuspend. Examples would be when
1487 * @udev's device file in usbfs is closed or after a configuration change.
1488 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001489 * @udev's usage counter is decremented; if it drops to 0 and all the
1490 * interfaces are inactive then a delayed autosuspend will be attempted.
1491 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001492 *
Alan Stern62e299e2010-01-08 12:56:19 -05001493 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001494 *
1495 * This routine can run only in process context.
1496 */
Alan Stern94fcda12006-11-20 11:38:46 -05001497void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001498{
Alan Stern94fcda12006-11-20 11:38:46 -05001499 int status;
1500
Ming Lei6ddf27c2010-11-15 15:57:30 -05001501 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001502 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001503 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1504 __func__, atomic_read(&udev->dev.power.usage_count),
1505 status);
Alan Stern19c26232007-02-20 15:03:32 -05001506}
1507
1508/**
Alan Stern645daaa2006-08-30 15:47:02 -04001509 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001510 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001511 *
1512 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001513 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001514 * occur until usb_autosuspend_device() is called. (Note that this will
1515 * not prevent suspend events originating in the PM core.) Examples would
1516 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001517 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001518 *
Alan Stern94fcda12006-11-20 11:38:46 -05001519 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1520 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001521 *
Alan Stern62e299e2010-01-08 12:56:19 -05001522 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001523 *
1524 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001525 *
1526 * Return: 0 on success. A negative error code otherwise.
Alan Stern645daaa2006-08-30 15:47:02 -04001527 */
Alan Stern94fcda12006-11-20 11:38:46 -05001528int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001529{
1530 int status;
1531
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001532 status = pm_runtime_get_sync(&udev->dev);
1533 if (status < 0)
1534 pm_runtime_put_sync(&udev->dev);
1535 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1536 __func__, atomic_read(&udev->dev.power.usage_count),
1537 status);
1538 if (status > 0)
1539 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001540 return status;
1541}
1542
Alan Stern645daaa2006-08-30 15:47:02 -04001543/**
1544 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001545 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001546 *
1547 * This routine should be called by an interface driver when it is
1548 * finished using @intf and wants to allow it to autosuspend. A typical
1549 * example would be a character-device driver when its device file is
1550 * closed.
1551 *
1552 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001553 * 0, a delayed autosuspend request for @intf's device is attempted. The
1554 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001555 *
Alan Stern645daaa2006-08-30 15:47:02 -04001556 * This routine can run only in process context.
1557 */
1558void usb_autopm_put_interface(struct usb_interface *intf)
1559{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001560 struct usb_device *udev = interface_to_usbdev(intf);
1561 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001562
Ming Lei6ddf27c2010-11-15 15:57:30 -05001563 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001564 atomic_dec(&intf->pm_usage_cnt);
1565 status = pm_runtime_put_sync(&intf->dev);
1566 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1567 __func__, atomic_read(&intf->dev.power.usage_count),
1568 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001569}
1570EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1571
1572/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001573 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1574 * @intf: the usb_interface whose counter should be decremented
1575 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001576 * This routine does much the same thing as usb_autopm_put_interface():
1577 * It decrements @intf's usage counter and schedules a delayed
1578 * autosuspend request if the counter is <= 0. The difference is that it
1579 * does not perform any synchronization; callers should hold a private
1580 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001581 *
1582 * Typically a driver would call this routine during an URB's completion
1583 * handler, if no more URBs were pending.
1584 *
1585 * This routine can run in atomic context.
1586 */
1587void usb_autopm_put_interface_async(struct usb_interface *intf)
1588{
1589 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001590 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001591
Ming Lei6ddf27c2010-11-15 15:57:30 -05001592 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001593 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001594 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001595 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1596 __func__, atomic_read(&intf->dev.power.usage_count),
1597 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001598}
1599EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1600
1601/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001602 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1603 * @intf: the usb_interface whose counter should be decremented
1604 *
1605 * This routine decrements @intf's usage counter but does not carry out an
1606 * autosuspend.
1607 *
1608 * This routine can run in atomic context.
1609 */
1610void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1611{
1612 struct usb_device *udev = interface_to_usbdev(intf);
1613
Ming Lei6ddf27c2010-11-15 15:57:30 -05001614 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001615 atomic_dec(&intf->pm_usage_cnt);
1616 pm_runtime_put_noidle(&intf->dev);
1617}
1618EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1619
1620/**
Alan Stern645daaa2006-08-30 15:47:02 -04001621 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001622 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001623 *
1624 * This routine should be called by an interface driver when it wants to
1625 * use @intf and needs to guarantee that it is not suspended. In addition,
1626 * the routine prevents @intf from being autosuspended subsequently. (Note
1627 * that this will not prevent suspend events originating in the PM core.)
1628 * This prevention will persist until usb_autopm_put_interface() is called
1629 * or @intf is unbound. A typical example would be a character-device
1630 * driver when its device file is opened.
1631 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001632 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1633 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001634 *
1635 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001636 *
1637 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001638 */
1639int usb_autopm_get_interface(struct usb_interface *intf)
1640{
Alan Sternaf4f7602006-10-30 17:06:45 -05001641 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001642
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001643 status = pm_runtime_get_sync(&intf->dev);
1644 if (status < 0)
1645 pm_runtime_put_sync(&intf->dev);
1646 else
1647 atomic_inc(&intf->pm_usage_cnt);
1648 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1649 __func__, atomic_read(&intf->dev.power.usage_count),
1650 status);
1651 if (status > 0)
1652 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001653 return status;
1654}
1655EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1656
Alan Stern692a1862006-10-30 17:07:51 -05001657/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001658 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1659 * @intf: the usb_interface whose counter should be incremented
1660 *
1661 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001662 * usb_autopm_get_interface(): It increments @intf's usage counter and
1663 * queues an autoresume request if the device is suspended. The
1664 * differences are that it does not perform any synchronization (callers
1665 * should hold a private lock and handle all synchronization issues
1666 * themselves), and it does not autoresume the device directly (it only
1667 * queues a request). After a successful call, the device may not yet be
1668 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001669 *
1670 * This routine can run in atomic context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001671 *
1672 * Return: 0 on success. A negative error code otherwise.
Alan Stern9ac39f22008-11-12 16:19:49 -05001673 */
1674int usb_autopm_get_interface_async(struct usb_interface *intf)
1675{
Ming Lei63defa72010-11-15 15:56:54 -05001676 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001677
Ming Lei63defa72010-11-15 15:56:54 -05001678 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001679 if (status < 0 && status != -EINPROGRESS)
1680 pm_runtime_put_noidle(&intf->dev);
1681 else
Alan Sternccf5b802009-06-29 11:00:01 -04001682 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001683 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1684 __func__, atomic_read(&intf->dev.power.usage_count),
1685 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001686 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001687 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001688 return status;
1689}
1690EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1691
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001692/**
1693 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1694 * @intf: the usb_interface whose counter should be incremented
1695 *
1696 * This routine increments @intf's usage counter but does not carry out an
1697 * autoresume.
1698 *
1699 * This routine can run in atomic context.
1700 */
1701void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1702{
1703 struct usb_device *udev = interface_to_usbdev(intf);
1704
Ming Lei6ddf27c2010-11-15 15:57:30 -05001705 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001706 atomic_inc(&intf->pm_usage_cnt);
1707 pm_runtime_get_noresume(&intf->dev);
1708}
1709EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1710
1711/* Internal routine to check whether we may autosuspend a device. */
1712static int autosuspend_check(struct usb_device *udev)
1713{
Alan Stern7560d32e2010-04-02 13:18:50 -04001714 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001715 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001716
1717 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1718 * any interface drivers require remote wakeup but it isn't available.
1719 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001720 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001721 if (udev->actconfig) {
1722 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1723 intf = udev->actconfig->interface[i];
1724
1725 /* We don't need to check interfaces that are
1726 * disabled for runtime PM. Either they are unbound
1727 * or else their drivers don't support autosuspend
1728 * and so they are permanently active.
1729 */
1730 if (intf->dev.power.disable_depth)
1731 continue;
1732 if (atomic_read(&intf->dev.power.usage_count) > 0)
1733 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001734 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001735
1736 /* Don't allow autosuspend if the device will need
1737 * a reset-resume and any of its interface drivers
1738 * doesn't include support or needs remote wakeup.
1739 */
1740 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1741 struct usb_driver *driver;
1742
1743 driver = to_usb_driver(intf->dev.driver);
1744 if (!driver->reset_resume ||
1745 intf->needs_remote_wakeup)
1746 return -EOPNOTSUPP;
1747 }
1748 }
1749 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001750 if (w && !device_can_wakeup(&udev->dev)) {
1751 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1752 return -EOPNOTSUPP;
1753 }
1754 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001755 return 0;
1756}
1757
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001758int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001759{
Ming Lei63defa72010-11-15 15:56:54 -05001760 struct usb_device *udev = to_usb_device(dev);
1761 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001762
1763 /* A USB device can be suspended if it passes the various autosuspend
1764 * checks. Runtime suspend for a USB device means suspending all the
1765 * interfaces and then the device itself.
1766 */
Ming Lei63defa72010-11-15 15:56:54 -05001767 if (autosuspend_check(udev) != 0)
1768 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001769
Ming Lei63defa72010-11-15 15:56:54 -05001770 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001771
1772 /* Allow a retry if autosuspend failed temporarily */
1773 if (status == -EAGAIN || status == -EBUSY)
1774 usb_mark_last_busy(udev);
1775
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001776 /* The PM core reacts badly unless the return code is 0,
1777 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1778 */
1779 if (status != 0)
1780 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001781 return status;
1782}
1783
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001784int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001785{
Ming Lei63defa72010-11-15 15:56:54 -05001786 struct usb_device *udev = to_usb_device(dev);
1787 int status;
1788
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001789 /* Runtime resume for a USB device means resuming both the device
1790 * and all its interfaces.
1791 */
Ming Lei63defa72010-11-15 15:56:54 -05001792 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001793 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001794}
1795
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001796int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001797{
Ming Lei63defa72010-11-15 15:56:54 -05001798 struct usb_device *udev = to_usb_device(dev);
1799
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001800 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001801 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001802 */
Ming Lei63defa72010-11-15 15:56:54 -05001803 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001804 pm_runtime_autosuspend(dev);
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001805 /* Tell the core not to suspend it, though. */
1806 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001807}
1808
Andiry Xu65580b432011-09-23 14:19:52 -07001809int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1810{
1811 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1812 int ret = -EPERM;
1813
Sarah Sharpde68bab2013-09-30 17:26:28 +03001814 if (enable && !udev->usb2_hw_lpm_allowed)
1815 return 0;
1816
Andiry Xu65580b432011-09-23 14:19:52 -07001817 if (hcd->driver->set_usb2_hw_lpm) {
1818 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1819 if (!ret)
1820 udev->usb2_hw_lpm_enabled = enable;
1821 }
1822
1823 return ret;
1824}
1825
Alan Stern84ebc102013-03-27 16:14:46 -04001826#endif /* CONFIG_PM_RUNTIME */
Alan Stern645daaa2006-08-30 15:47:02 -04001827
Alan Stern36e56a32006-07-01 22:08:06 -04001828struct bus_type usb_bus_type = {
1829 .name = "usb",
1830 .match = usb_device_match,
1831 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001832};