blob: 8d989b1d3dc5f2e62b79afe7edf0b61fe9d4d3aa [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,
40 struct device_driver *driver,
41 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080042{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080043 struct usb_dynid *dynid;
44 u32 idVendor = 0;
45 u32 idProduct = 0;
Josua Dietzeff231db2011-10-23 14:22:29 +020046 unsigned int bInterfaceClass = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080047 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070048 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080049
Josua Dietzeff231db2011-10-23 14:22:29 +020050 fields = sscanf(buf, "%x %x %x", &idVendor, &idProduct,
51 &bInterfaceClass);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080052 if (fields < 2)
53 return -EINVAL;
54
55 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
56 if (!dynid)
57 return -ENOMEM;
58
59 INIT_LIST_HEAD(&dynid->node);
60 dynid->id.idVendor = idVendor;
61 dynid->id.idProduct = idProduct;
62 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
Josua Dietzeff231db2011-10-23 14:22:29 +020063 if (fields == 3) {
64 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
65 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
66 }
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080067
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010068 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020069 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010070 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080071
Alan Sterncef9bc52012-01-24 13:34:41 -050072 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080073
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070074 if (retval)
75 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080076 return count;
77}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010078EXPORT_SYMBOL_GPL(usb_store_new_id);
79
Bjørn Morkef206f32012-05-13 12:35:00 +020080ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
Bjørn Morke6bbcef2012-05-13 12:34:59 +020081{
82 struct usb_dynid *dynid;
Bjørn Morke6bbcef2012-05-13 12:34:59 +020083 size_t count = 0;
84
Bjørn Morkef206f32012-05-13 12:35:00 +020085 list_for_each_entry(dynid, &dynids->list, node)
Bjørn Morke6bbcef2012-05-13 12:34:59 +020086 if (dynid->id.bInterfaceClass != 0)
87 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
88 dynid->id.idVendor, dynid->id.idProduct,
89 dynid->id.bInterfaceClass);
90 else
91 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
92 dynid->id.idVendor, dynid->id.idProduct);
93 return count;
94}
Bjørn Morkef206f32012-05-13 12:35:00 +020095EXPORT_SYMBOL_GPL(usb_show_dynids);
96
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -070097static ssize_t new_id_show(struct device_driver *driver, char *buf)
Bjørn Morkef206f32012-05-13 12:35:00 +020098{
99 struct usb_driver *usb_drv = to_usb_driver(driver);
100
101 return usb_show_dynids(&usb_drv->dynids, buf);
102}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200103
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700104static ssize_t new_id_store(struct device_driver *driver,
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100105 const char *buf, size_t count)
106{
107 struct usb_driver *usb_drv = to_usb_driver(driver);
108
109 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
110}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700111static DRIVER_ATTR_RW(new_id);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800112
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700113/*
114 * Remove a USB device ID from this driver
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800115 */
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700116static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
117 size_t count)
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800118{
119 struct usb_dynid *dynid, *n;
120 struct usb_driver *usb_driver = to_usb_driver(driver);
Alan Coxac08de32012-09-17 11:55:23 +0100121 u32 idVendor;
122 u32 idProduct;
123 int fields;
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800124
125 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
126 if (fields < 2)
127 return -EINVAL;
128
129 spin_lock(&usb_driver->dynids.lock);
130 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
131 struct usb_device_id *id = &dynid->id;
132 if ((id->idVendor == idVendor) &&
133 (id->idProduct == idProduct)) {
134 list_del(&dynid->node);
135 kfree(dynid);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800136 break;
137 }
138 }
139 spin_unlock(&usb_driver->dynids.lock);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800140 return count;
141}
Greg Kroah-Hartman598d0362013-08-23 15:12:14 -0700142
143static ssize_t remove_id_show(struct device_driver *driver, char *buf)
144{
145 return new_id_show(driver, buf);
146}
147static DRIVER_ATTR_RW(remove_id);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800148
Alan Sterned283e92012-01-24 14:35:13 -0500149static int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800150{
151 int error = 0;
152
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800153 if (usb_drv->no_dynamic_id)
154 goto exit;
155
Alan Sterned283e92012-01-24 14:35:13 -0500156 if (usb_drv->probe != NULL) {
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800157 error = driver_create_file(&usb_drv->drvwrap.driver,
158 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500159 if (error == 0) {
160 error = driver_create_file(&usb_drv->drvwrap.driver,
161 &driver_attr_remove_id);
162 if (error)
163 driver_remove_file(&usb_drv->drvwrap.driver,
164 &driver_attr_new_id);
165 }
166 }
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800167exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800168 return error;
169}
170
Alan Sterned283e92012-01-24 14:35:13 -0500171static void usb_remove_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800172{
173 if (usb_drv->no_dynamic_id)
174 return;
175
Alan Sterned283e92012-01-24 14:35:13 -0500176 if (usb_drv->probe != NULL) {
177 driver_remove_file(&usb_drv->drvwrap.driver,
178 &driver_attr_remove_id);
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800179 driver_remove_file(&usb_drv->drvwrap.driver,
180 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500181 }
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800182}
183
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800184static void usb_free_dynids(struct usb_driver *usb_drv)
185{
186 struct usb_dynid *dynid, *n;
187
188 spin_lock(&usb_drv->dynids.lock);
189 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
190 list_del(&dynid->node);
191 kfree(dynid);
192 }
193 spin_unlock(&usb_drv->dynids.lock);
194}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800195
196static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
197 struct usb_driver *drv)
198{
199 struct usb_dynid *dynid;
200
201 spin_lock(&drv->dynids.lock);
202 list_for_each_entry(dynid, &drv->dynids.list, node) {
203 if (usb_match_one_id(intf, &dynid->id)) {
204 spin_unlock(&drv->dynids.lock);
205 return &dynid->id;
206 }
207 }
208 spin_unlock(&drv->dynids.lock);
209 return NULL;
210}
211
212
Alan Stern8bb54ab2006-07-01 22:08:49 -0400213/* called from driver core with dev locked */
214static int usb_probe_device(struct device *dev)
215{
216 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200217 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500218 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400219
Harvey Harrison441b62c2008-03-03 16:08:34 -0800220 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400221
Alan Stern8bb54ab2006-07-01 22:08:49 -0400222 /* TODO: Add real matching code */
223
Alan Stern645daaa2006-08-30 15:47:02 -0400224 /* The device should always appear to be in use
Masanari Iida02582e92012-08-22 19:11:26 +0900225 * unless the driver supports autosuspend.
Alan Stern645daaa2006-08-30 15:47:02 -0400226 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500227 if (!udriver->supports_autosuspend)
228 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400229
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500230 if (!error)
231 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400232 return error;
233}
234
235/* called from driver core with dev locked */
236static int usb_unbind_device(struct device *dev)
237{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500238 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400239 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
240
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500241 udriver->disconnect(udev);
242 if (!udriver->supports_autosuspend)
243 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400244 return 0;
245}
246
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800247/*
248 * Cancel any pending scheduled resets
249 *
250 * [see usb_queue_reset_device()]
251 *
252 * Called after unconfiguring / when releasing interfaces. See
253 * comments in __usb_queue_reset_device() regarding
254 * udev->reset_running.
255 */
256static void usb_cancel_queued_reset(struct usb_interface *iface)
257{
258 if (iface->reset_running == 0)
259 cancel_work_sync(&iface->reset_ws);
260}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400261
262/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800263static int usb_probe_interface(struct device *dev)
264{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400265 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200266 struct usb_interface *intf = to_usb_interface(dev);
267 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800268 const struct usb_device_id *id;
269 int error = -ENODEV;
Sarah Sharp83060952012-05-02 14:25:52 -0700270 int lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800271
Harvey Harrison441b62c2008-03-03 16:08:34 -0800272 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800273
Alan Stern78d9a482008-06-23 16:00:40 -0400274 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800275
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400276 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500277 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400278
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800279 if (udev->authorized == 0) {
280 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500281 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800282 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700283
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800284 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800285 if (!id)
286 id = usb_match_dynamic_id(intf, driver);
Alan Stern0f3dda92010-01-08 12:56:04 -0500287 if (!id)
288 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800289
Alan Stern0f3dda92010-01-08 12:56:04 -0500290 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400291
Alan Stern0f3dda92010-01-08 12:56:04 -0500292 error = usb_autoresume_device(udev);
293 if (error)
294 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400295
Alan Stern0f3dda92010-01-08 12:56:04 -0500296 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400297
Alan Stern571dc792010-04-09 16:03:43 -0400298 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500299 * runtime-PM-enabled only if the driver has autosuspend support.
300 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500301 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500302 pm_runtime_set_active(dev);
303 pm_suspend_ignore_children(dev, false);
304 if (driver->supports_autosuspend)
305 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200306
Sarah Sharp83060952012-05-02 14:25:52 -0700307 /* If the new driver doesn't allow hub-initiated LPM, and we can't
308 * disable hub-initiated LPM, then fail the probe.
309 *
310 * Otherwise, leaving LPM enabled should be harmless, because the
311 * endpoint intervals should remain the same, and the U1/U2 timeouts
312 * should remain the same.
313 *
314 * If we need to install alt setting 0 before probe, or another alt
315 * setting during probe, that should also be fine. usb_set_interface()
316 * will attempt to disable LPM, and fail if it can't disable it.
317 */
318 lpm_disable_error = usb_unlocked_disable_lpm(udev);
319 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
320 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
321 __func__, driver->name);
322 error = lpm_disable_error;
323 goto err;
324 }
325
Alan Stern0f3dda92010-01-08 12:56:04 -0500326 /* Carry out a deferred switch to altsetting 0 */
327 if (intf->needs_altsetting0) {
328 error = usb_set_interface(udev, intf->altsetting[0].
329 desc.bInterfaceNumber, 0);
330 if (error < 0)
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200331 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500332 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800333 }
334
Alan Stern0f3dda92010-01-08 12:56:04 -0500335 error = driver->probe(intf, id);
336 if (error)
337 goto err;
338
339 intf->condition = USB_INTERFACE_BOUND;
Sarah Sharp83060952012-05-02 14:25:52 -0700340
341 /* If the LPM disable succeeded, balance the ref counts. */
342 if (!lpm_disable_error)
343 usb_unlocked_enable_lpm(udev);
344
Alan Stern0f3dda92010-01-08 12:56:04 -0500345 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800346 return error;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200347
Alan Stern0f3dda92010-01-08 12:56:04 -0500348 err:
Hans de Goedee714fad2012-05-22 11:36:59 +0200349 usb_set_intfdata(intf, NULL);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200350 intf->needs_remote_wakeup = 0;
351 intf->condition = USB_INTERFACE_UNBOUND;
352 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500353
Sarah Sharpd01f87c2012-10-04 09:53:43 -0700354 /* If the LPM disable succeeded, balance the ref counts. */
355 if (!lpm_disable_error)
356 usb_unlocked_enable_lpm(udev);
357
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500358 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400359 if (driver->supports_autosuspend)
360 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500361 pm_runtime_set_suspended(dev);
362
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200363 usb_autosuspend_device(udev);
364 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800365}
366
Alan Stern8bb54ab2006-07-01 22:08:49 -0400367/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800368static int usb_unbind_interface(struct device *dev)
369{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400370 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800371 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400372 struct usb_device *udev;
Sarah Sharp83060952012-05-02 14:25:52 -0700373 int error, r, lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800374
375 intf->condition = USB_INTERFACE_UNBINDING;
376
Alan Stern645daaa2006-08-30 15:47:02 -0400377 /* Autoresume for set_interface call below */
378 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500379 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400380
Sarah Sharp83060952012-05-02 14:25:52 -0700381 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
382 * the driver is unbound. If LPM isn't disabled, that's fine because it
383 * wouldn't be enabled unless all the bound interfaces supported
384 * hub-initiated LPM.
385 */
386 lpm_disable_error = usb_unlocked_disable_lpm(udev);
387
Alan Stern9da82bd2008-05-08 11:54:37 -0400388 /* Terminate all URBs for this interface unless the driver
389 * supports "soft" unbinding.
390 */
391 if (!driver->soft_unbind)
Alan Sternddeac4e72009-01-15 17:03:33 -0500392 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800393
Alan Stern8bb54ab2006-07-01 22:08:49 -0400394 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800395 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800396
Alan Stern55151d72008-08-12 14:33:59 -0400397 /* Reset other interface state.
398 * We cannot do a Set-Interface if the device is suspended or
399 * if it is prepared for a system sleep (since installing a new
400 * altsetting means creating new endpoint device entries).
401 * When either of these happens, defer the Set-Interface.
402 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500403 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
404 /* Already in altsetting 0 so skip Set-Interface.
405 * Just re-enable it without affecting the endpoint toggles.
406 */
407 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200408 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200409 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400410 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200411 if (r < 0)
412 intf->needs_altsetting0 = 1;
413 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400414 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200415 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800416 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400417
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800418 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400419 intf->needs_remote_wakeup = 0;
420
Sarah Sharp83060952012-05-02 14:25:52 -0700421 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
422 if (!lpm_disable_error)
423 usb_unlocked_enable_lpm(udev);
424
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500425 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400426 if (driver->supports_autosuspend)
427 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500428 pm_runtime_set_suspended(dev);
429
430 /* Undo any residual pm_autopm_get_interface_* calls */
431 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
432 usb_autopm_put_interface_no_suspend(intf);
433 atomic_set(&intf->pm_usage_cnt, 0);
434
Alan Stern645daaa2006-08-30 15:47:02 -0400435 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500436 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800437
438 return 0;
439}
440
Alan Stern36e56a32006-07-01 22:08:06 -0400441/**
442 * usb_driver_claim_interface - bind a driver to an interface
443 * @driver: the driver to be bound
444 * @iface: the interface to which it will be bound; must be in the
445 * usb device's active configuration
446 * @priv: driver data associated with that interface
447 *
448 * This is used by usb device drivers that need to claim more than one
449 * interface on a device when probing (audio and acm are current examples).
450 * No device driver should directly modify internal usb_interface or
451 * usb_device structure members.
452 *
453 * Few drivers should need to use this routine, since the most natural
454 * way to bind to an interface is to return the private data from
455 * the driver's probe() method.
456 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400457 * Callers must own the device lock, so driver probe() entries don't need
458 * extra locking, but other call contexts may need to explicitly claim that
459 * lock.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200460 *
461 * Return: 0 on success.
Alan Stern36e56a32006-07-01 22:08:06 -0400462 */
463int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800464 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400465{
466 struct device *dev = &iface->dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700467 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700468 int retval = 0;
Sarah Sharp83060952012-05-02 14:25:52 -0700469 int lpm_disable_error;
Alan Stern36e56a32006-07-01 22:08:06 -0400470
471 if (dev->driver)
472 return -EBUSY;
473
Sarah Sharp83060952012-05-02 14:25:52 -0700474 udev = interface_to_usbdev(iface);
475
Alan Stern8bb54ab2006-07-01 22:08:49 -0400476 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400477 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400478 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400479
Alan Stern36e56a32006-07-01 22:08:06 -0400480 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500481
Sarah Sharp83060952012-05-02 14:25:52 -0700482 /* Disable LPM until this driver is bound. */
483 lpm_disable_error = usb_unlocked_disable_lpm(udev);
484 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
485 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
486 __func__, driver->name);
487 return -ENOMEM;
488 }
489
Alan Stern89842ae2010-05-11 11:44:06 -0400490 /* Claimed interfaces are initially inactive (suspended) and
491 * runtime-PM-enabled, but only if the driver has autosuspend
492 * support. Otherwise they are marked active, to prevent the
493 * device from being autosuspended, but left disabled. In either
494 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500495 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500496 pm_suspend_ignore_children(dev, false);
497 if (driver->supports_autosuspend)
498 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400499 else
500 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400501
502 /* if interface was already added, bind now; else let
503 * the future device_add() bind it, bypassing probe()
504 */
505 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700506 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400507
Sarah Sharp83060952012-05-02 14:25:52 -0700508 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
509 if (!lpm_disable_error)
510 usb_unlocked_enable_lpm(udev);
511
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700512 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400513}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600514EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400515
516/**
517 * usb_driver_release_interface - unbind a driver from an interface
518 * @driver: the driver to be unbound
519 * @iface: the interface from which it will be unbound
520 *
521 * This can be used by drivers to release an interface without waiting
522 * for their disconnect() methods to be called. In typical cases this
523 * also causes the driver disconnect() method to be called.
524 *
525 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400526 * Callers must own the device lock, so driver disconnect() entries don't
527 * need extra locking, but other call contexts may need to explicitly claim
528 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400529 */
530void usb_driver_release_interface(struct usb_driver *driver,
531 struct usb_interface *iface)
532{
533 struct device *dev = &iface->dev;
534
535 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400536 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400537 return;
538
539 /* don't release from within disconnect() */
540 if (iface->condition != USB_INTERFACE_BOUND)
541 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400542 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400543
Alan Stern91f8d062009-04-16 15:35:09 -0400544 /* Release via the driver core only if the interface
545 * has already been registered
546 */
Alan Stern36e56a32006-07-01 22:08:06 -0400547 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400548 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800549 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800550 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400551 usb_unbind_interface(dev);
552 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800553 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400554 }
Alan Stern36e56a32006-07-01 22:08:06 -0400555}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600556EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400557
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800558/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100559int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800560{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800561 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
562 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
563 return 0;
564
565 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
566 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
567 return 0;
568
569 /* No need to test id->bcdDevice_lo != 0, since 0 is never
570 greater than any unsigned number. */
571 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
572 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
573 return 0;
574
575 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
576 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
577 return 0;
578
579 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
580 (id->bDeviceClass != dev->descriptor.bDeviceClass))
581 return 0;
582
583 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800584 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800585 return 0;
586
587 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
588 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
589 return 0;
590
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100591 return 1;
592}
593
594/* returns 0 if no match, 1 if match */
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200595int usb_match_one_id_intf(struct usb_device *dev,
596 struct usb_host_interface *intf,
597 const struct usb_device_id *id)
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100598{
Bjørn Mork81df2d52012-05-18 21:27:43 +0200599 /* The interface class, subclass, protocol and number should never be
Alan Stern93c8bf42006-10-18 16:41:51 -0400600 * checked for a match if the device class is Vendor Specific,
601 * unless the match record specifies the Vendor ID. */
602 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
603 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
604 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
605 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
Bjørn Mork81df2d52012-05-18 21:27:43 +0200606 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
607 USB_DEVICE_ID_MATCH_INT_NUMBER)))
Alan Stern93c8bf42006-10-18 16:41:51 -0400608 return 0;
609
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800610 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
611 (id->bInterfaceClass != intf->desc.bInterfaceClass))
612 return 0;
613
614 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
615 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
616 return 0;
617
618 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
619 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
620 return 0;
621
Bjørn Mork81df2d52012-05-18 21:27:43 +0200622 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
623 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
624 return 0;
625
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800626 return 1;
627}
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200628
629/* returns 0 if no match, 1 if match */
630int usb_match_one_id(struct usb_interface *interface,
631 const struct usb_device_id *id)
632{
633 struct usb_host_interface *intf;
634 struct usb_device *dev;
635
636 /* proc_connectinfo in devio.c may call us with id == NULL. */
637 if (id == NULL)
638 return 0;
639
640 intf = interface->cur_altsetting;
641 dev = interface_to_usbdev(interface);
642
643 if (!usb_match_device(dev, id))
644 return 0;
645
646 return usb_match_one_id_intf(dev, intf, id);
647}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100648EXPORT_SYMBOL_GPL(usb_match_one_id);
649
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800650/**
651 * usb_match_id - find first usb_device_id matching device or interface
652 * @interface: the interface of interest
653 * @id: array of usb_device_id structures, terminated by zero entry
654 *
655 * usb_match_id searches an array of usb_device_id's and returns
656 * the first one matching the device or interface, or null.
657 * This is used when binding (or rebinding) a driver to an interface.
658 * Most USB device drivers will use this indirectly, through the usb core,
659 * but some layered driver frameworks use it directly.
660 * These device tables are exported with MODULE_DEVICE_TABLE, through
661 * modutils, to support the driver loading functionality of USB hotplugging.
662 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200663 * Return: The first matching usb_device_id, or %NULL.
664 *
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800665 * What Matches:
666 *
667 * The "match_flags" element in a usb_device_id controls which
668 * members are used. If the corresponding bit is set, the
669 * value in the device_id must match its corresponding member
670 * in the device or interface descriptor, or else the device_id
671 * does not match.
672 *
673 * "driver_info" is normally used only by device drivers,
674 * but you can create a wildcard "matches anything" usb_device_id
675 * as a driver's "modules.usbmap" entry if you provide an id with
676 * only a nonzero "driver_info" field. If you do this, the USB device
677 * driver's probe() routine should use additional intelligence to
678 * decide whether to bind to the specified interface.
679 *
680 * What Makes Good usb_device_id Tables:
681 *
682 * The match algorithm is very simple, so that intelligence in
683 * driver selection must come from smart driver id records.
684 * Unless you have good reasons to use another selection policy,
685 * provide match elements only in related groups, and order match
686 * specifiers from specific to general. Use the macros provided
687 * for that purpose if you can.
688 *
689 * The most specific match specifiers use device descriptor
690 * data. These are commonly used with product-specific matches;
691 * the USB_DEVICE macro lets you provide vendor and product IDs,
692 * and you can also match against ranges of product revisions.
693 * These are widely used for devices with application or vendor
694 * specific bDeviceClass values.
695 *
696 * Matches based on device class/subclass/protocol specifications
697 * are slightly more general; use the USB_DEVICE_INFO macro, or
698 * its siblings. These are used with single-function devices
699 * where bDeviceClass doesn't specify that each interface has
700 * its own class.
701 *
702 * Matches based on interface class/subclass/protocol are the
703 * most general; they let drivers bind to any interface on a
704 * multiple-function device. Use the USB_INTERFACE_INFO
705 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400706 * devices (as recorded in bInterfaceClass).
707 *
708 * Note that an entry created by USB_INTERFACE_INFO won't match
709 * any interface if the device class is set to Vendor-Specific.
710 * This is deliberate; according to the USB spec the meanings of
711 * the interface class/subclass/protocol for these devices are also
712 * vendor-specific, and hence matching against a standard product
713 * class wouldn't work anyway. If you really want to use an
714 * interface-based match for such a device, create a match record
715 * that also specifies the vendor ID. (Unforunately there isn't a
716 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800717 *
718 * Within those groups, remember that not all combinations are
719 * meaningful. For example, don't give a product version range
720 * without vendor and product IDs; or specify a protocol without
721 * its associated class and subclass.
722 */
723const struct usb_device_id *usb_match_id(struct usb_interface *interface,
724 const struct usb_device_id *id)
725{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800726 /* proc_connectinfo in devio.c may call us with id == NULL. */
727 if (id == NULL)
728 return NULL;
729
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800730 /* It is important to check that id->driver_info is nonzero,
731 since an entry that is all zeroes except for a nonzero
732 id->driver_info is the way to create an entry that
733 indicates that the driver want to examine every
734 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800735 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
736 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800737 if (usb_match_one_id(interface, id))
738 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800739 }
740
741 return NULL;
742}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600743EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800744
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100745static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800746{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400747 /* devices and interfaces are handled separately */
748 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800749
Alan Stern8bb54ab2006-07-01 22:08:49 -0400750 /* interface drivers never match devices */
751 if (!is_usb_device_driver(drv))
752 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800753
Alan Stern8bb54ab2006-07-01 22:08:49 -0400754 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800755 return 1;
756
Kay Sievers55129662009-05-04 19:48:32 +0200757 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400758 struct usb_interface *intf;
759 struct usb_driver *usb_drv;
760 const struct usb_device_id *id;
761
762 /* device drivers never match interfaces */
763 if (is_usb_device_driver(drv))
764 return 0;
765
766 intf = to_usb_interface(dev);
767 usb_drv = to_usb_driver(drv);
768
769 id = usb_match_id(intf, usb_drv->id_table);
770 if (id)
771 return 1;
772
773 id = usb_match_dynamic_id(intf, usb_drv);
774 if (id)
775 return 1;
776 }
777
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800778 return 0;
779}
780
Kay Sievers7eff2e72007-08-14 15:15:12 +0200781static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400782{
Alan Stern36e56a32006-07-01 22:08:06 -0400783 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400784
Kay Sievers55129662009-05-04 19:48:32 +0200785 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400786 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200787 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100788 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200789
Alan Stern8bb54ab2006-07-01 22:08:49 -0400790 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200791 } else {
792 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400793 }
Alan Stern36e56a32006-07-01 22:08:06 -0400794
795 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500796 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200797 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400798 return -ENODEV;
799 }
800 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200801 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400802 return -ENODEV;
803 }
804
Alan Stern36e56a32006-07-01 22:08:06 -0400805 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200806 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400807 le16_to_cpu(usb_dev->descriptor.idVendor),
808 le16_to_cpu(usb_dev->descriptor.idProduct),
809 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
810 return -ENOMEM;
811
812 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200813 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400814 usb_dev->descriptor.bDeviceClass,
815 usb_dev->descriptor.bDeviceSubClass,
816 usb_dev->descriptor.bDeviceProtocol))
817 return -ENOMEM;
818
Alan Stern36e56a32006-07-01 22:08:06 -0400819 return 0;
820}
821
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800822/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400823 * usb_register_device_driver - register a USB device (not interface) driver
824 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800825 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800826 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400827 * Registers a USB device driver with the USB core. The list of
828 * unattached devices will be rescanned whenever a new driver is
829 * added, allowing the new driver to attach to any recognized devices.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200830 *
831 * Return: A negative error code on failure and 0 on success.
Alan Stern8bb54ab2006-07-01 22:08:49 -0400832 */
833int usb_register_device_driver(struct usb_device_driver *new_udriver,
834 struct module *owner)
835{
836 int retval = 0;
837
838 if (usb_disabled())
839 return -ENODEV;
840
841 new_udriver->drvwrap.for_devices = 1;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100842 new_udriver->drvwrap.driver.name = new_udriver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400843 new_udriver->drvwrap.driver.bus = &usb_bus_type;
844 new_udriver->drvwrap.driver.probe = usb_probe_device;
845 new_udriver->drvwrap.driver.remove = usb_unbind_device;
846 new_udriver->drvwrap.driver.owner = owner;
847
848 retval = driver_register(&new_udriver->drvwrap.driver);
849
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700850 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400851 pr_info("%s: registered new device driver %s\n",
852 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700853 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400854 printk(KERN_ERR "%s: error %d registering device "
855 " driver %s\n",
856 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400857
858 return retval;
859}
860EXPORT_SYMBOL_GPL(usb_register_device_driver);
861
862/**
863 * usb_deregister_device_driver - unregister a USB device (not interface) driver
864 * @udriver: USB operations of the device driver to unregister
865 * Context: must be able to sleep
866 *
867 * Unlinks the specified driver from the internal USB driver list.
868 */
869void usb_deregister_device_driver(struct usb_device_driver *udriver)
870{
871 pr_info("%s: deregistering device driver %s\n",
872 usbcore_name, udriver->name);
873
874 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400875}
876EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
877
878/**
879 * usb_register_driver - register a USB interface driver
880 * @new_driver: USB operations for the interface driver
881 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800882 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400883 *
884 * Registers a USB interface driver with the USB core. The list of
885 * unattached interfaces will be rescanned whenever a new driver is
886 * added, allowing the new driver to attach to any recognized interfaces.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200887 *
888 * Return: A negative error code on failure and 0 on success.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800889 *
890 * NOTE: if you want your driver to use the USB major number, you must call
891 * usb_register_dev() to enable that functionality. This function no longer
892 * takes care of that.
893 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800894int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
895 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800896{
897 int retval = 0;
898
899 if (usb_disabled())
900 return -ENODEV;
901
Alan Stern8bb54ab2006-07-01 22:08:49 -0400902 new_driver->drvwrap.for_devices = 0;
Geert Uytterhoeven9f9af822013-11-12 20:07:22 +0100903 new_driver->drvwrap.driver.name = new_driver->name;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400904 new_driver->drvwrap.driver.bus = &usb_bus_type;
905 new_driver->drvwrap.driver.probe = usb_probe_interface;
906 new_driver->drvwrap.driver.remove = usb_unbind_interface;
907 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800908 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800909 spin_lock_init(&new_driver->dynids.lock);
910 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800911
Alan Stern8bb54ab2006-07-01 22:08:49 -0400912 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800913 if (retval)
914 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800915
Alan Sterned283e92012-01-24 14:35:13 -0500916 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800917 if (retval)
918 goto out_newid;
919
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800920 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800921 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800922
923out:
924 return retval;
925
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800926out_newid:
927 driver_unregister(&new_driver->drvwrap.driver);
928
929 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400930 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800931 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800932 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800933}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600934EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800935
936/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400937 * usb_deregister - unregister a USB interface driver
938 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800939 * Context: must be able to sleep
940 *
941 * Unlinks the specified driver from the internal USB driver list.
942 *
943 * NOTE: If you called usb_register_dev(), you still need to call
944 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
945 * this * call will no longer do it for you.
946 */
947void usb_deregister(struct usb_driver *driver)
948{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400949 pr_info("%s: deregistering interface driver %s\n",
950 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800951
Alan Sterned283e92012-01-24 14:35:13 -0500952 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400953 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -0500954 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800955}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600956EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400957
Alan Stern78d9a482008-06-23 16:00:40 -0400958/* Forced unbinding of a USB interface driver, either because
959 * it doesn't support pre_reset/post_reset/reset_resume or
960 * because it doesn't support suspend/resume.
961 *
962 * The caller must hold @intf's device's lock, but not its pm_mutex
963 * and not @intf->dev.sem.
964 */
965void usb_forced_unbind_intf(struct usb_interface *intf)
966{
967 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
968
969 dev_dbg(&intf->dev, "forced unbind\n");
970 usb_driver_release_interface(driver, intf);
971
972 /* Mark the interface for later rebinding */
973 intf->needs_binding = 1;
974}
975
976/* Delayed forced unbinding of a USB interface driver and scan
977 * for rebinding.
978 *
979 * The caller must hold @intf's device's lock, but not its pm_mutex
980 * and not @intf->dev.sem.
981 *
Alan Stern5096aed2008-08-12 14:34:14 -0400982 * Note: Rebinds will be skipped if a system sleep transition is in
983 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -0400984 */
985void usb_rebind_intf(struct usb_interface *intf)
986{
987 int rc;
988
989 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +0100990 if (intf->dev.driver)
991 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -0400992
993 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +0200994 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -0400995 intf->needs_binding = 0;
996 rc = device_attach(&intf->dev);
997 if (rc < 0)
998 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
999 }
Alan Stern78d9a482008-06-23 16:00:40 -04001000}
1001
Alan Stern9ff78432008-08-07 13:04:51 -04001002#ifdef CONFIG_PM
1003
Oliver Neukum14931382012-01-05 15:39:57 +01001004/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1005 * There is no check for reset_resume here because it can be determined
1006 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001007 *
1008 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001009 */
Oliver Neukum14931382012-01-05 15:39:57 +01001010static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001011{
1012 struct usb_host_config *config;
1013 int i;
1014 struct usb_interface *intf;
1015 struct usb_driver *drv;
1016
1017 config = udev->actconfig;
1018 if (config) {
1019 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1020 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001021
1022 if (intf->dev.driver) {
1023 drv = to_usb_driver(intf->dev.driver);
1024 if (!drv->suspend || !drv->resume)
1025 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001026 }
1027 }
1028 }
1029}
1030
Oliver Neukum14931382012-01-05 15:39:57 +01001031/* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1032 * These interfaces have the needs_binding flag set by usb_resume_interface().
1033 *
1034 * The caller must hold @udev's device lock.
1035 */
1036static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
1037{
1038 struct usb_host_config *config;
1039 int i;
1040 struct usb_interface *intf;
1041
1042 config = udev->actconfig;
1043 if (config) {
1044 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1045 intf = config->interface[i];
1046 if (intf->dev.driver && intf->needs_binding)
1047 usb_forced_unbind_intf(intf);
1048 }
1049 }
1050}
1051
1052static void do_rebind_interfaces(struct usb_device *udev)
1053{
1054 struct usb_host_config *config;
1055 int i;
1056 struct usb_interface *intf;
1057
1058 config = udev->actconfig;
1059 if (config) {
1060 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1061 intf = config->interface[i];
1062 if (intf->needs_binding)
1063 usb_rebind_intf(intf);
1064 }
1065 }
1066}
1067
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001068static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001069{
Alan Stern782da722006-07-01 22:09:35 -04001070 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001071 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001072
Alan Stern114b3682006-07-01 22:13:04 -04001073 if (udev->state == USB_STATE_NOTATTACHED ||
1074 udev->state == USB_STATE_SUSPENDED)
1075 goto done;
1076
Alan Sternb6f64362007-05-04 11:51:54 -04001077 /* For devices that don't have a driver, we do a generic suspend. */
1078 if (udev->dev.driver)
1079 udriver = to_usb_device_driver(udev->dev.driver);
1080 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001081 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001082 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001083 }
Alan Stern2bf40862006-07-01 22:12:19 -04001084 status = udriver->suspend(udev, msg);
1085
Alan Stern20dfdad2007-05-22 11:50:17 -04001086 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001087 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001088 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001089}
Alan Stern36e56a32006-07-01 22:08:06 -04001090
Alan Stern65bfd292008-11-25 16:39:18 -05001091static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001092{
1093 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001094 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001095
Alan Stern0458d5b2007-05-04 11:52:20 -04001096 if (udev->state == USB_STATE_NOTATTACHED)
1097 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001098
Alan Stern1c5df7e2006-07-01 22:13:50 -04001099 /* Can't resume it if it doesn't have a driver. */
1100 if (udev->dev.driver == NULL) {
1101 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001102 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001103 }
1104
Alan Stern6d19c002010-02-12 12:21:11 +01001105 /* Non-root devices on a full/low-speed bus must wait for their
1106 * companion high-speed root hub, in case a handoff is needed.
1107 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001108 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001109 device_pm_wait_for_dev(&udev->dev,
1110 &udev->bus->hs_companion->root_hub->dev);
1111
Alan Stern6bc6cff2007-05-04 11:53:03 -04001112 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1113 udev->reset_resume = 1;
1114
Alan Stern1cc8a252006-07-01 22:10:15 -04001115 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001116 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001117
Alan Stern20dfdad2007-05-22 11:50:17 -04001118 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001119 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001120 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001121}
1122
Alan Stern65605ae2008-08-12 14:33:27 -04001123static int usb_suspend_interface(struct usb_device *udev,
1124 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001125{
1126 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001127 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001128
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001129 if (udev->state == USB_STATE_NOTATTACHED ||
1130 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001131 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001132 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001133
Oliver Neukume78832c2012-01-02 15:11:48 +01001134 /* at this time we know the driver supports suspend */
1135 status = driver->suspend(intf, msg);
1136 if (status && !PMSG_IS_AUTO(msg))
1137 dev_err(&intf->dev, "suspend error %d\n", status);
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(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001141 return status;
1142}
1143
Alan Stern65605ae2008-08-12 14:33:27 -04001144static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001145 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001146{
Alan Stern1cc8a252006-07-01 22:10:15 -04001147 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001148 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001149
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001150 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001151 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001152
Alan Stern645daaa2006-08-30 15:47:02 -04001153 /* Don't let autoresume interfere with unbinding */
1154 if (intf->condition == USB_INTERFACE_UNBINDING)
1155 goto done;
1156
Alan Stern1c5df7e2006-07-01 22:13:50 -04001157 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001158 if (intf->condition == USB_INTERFACE_UNBOUND) {
1159
1160 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001161 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001162 usb_set_interface(udev, intf->altsetting[0].
1163 desc.bInterfaceNumber, 0);
1164 intf->needs_altsetting0 = 0;
1165 }
Alan Stern2bf40862006-07-01 22:12:19 -04001166 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001167 }
Alan Stern78d9a482008-06-23 16:00:40 -04001168
1169 /* Don't resume if the interface is marked for rebinding */
1170 if (intf->needs_binding)
1171 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001172 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001173
Alan Sternf07600c2007-05-30 15:38:16 -04001174 if (reset_resume) {
1175 if (driver->reset_resume) {
1176 status = driver->reset_resume(intf);
1177 if (status)
1178 dev_err(&intf->dev, "%s error %d\n",
1179 "reset_resume", status);
1180 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001181 intf->needs_binding = 1;
Alan Stern0a56b4f2013-10-18 11:17:21 -04001182 dev_dbg(&intf->dev, "no reset_resume for driver %s?\n",
1183 driver->name);
Alan Sternf07600c2007-05-30 15:38:16 -04001184 }
1185 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001186 status = driver->resume(intf);
1187 if (status)
1188 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001189 }
Alan Stern2bf40862006-07-01 22:12:19 -04001190
1191done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001192 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001193
Alan Stern78d9a482008-06-23 16:00:40 -04001194 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001195 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001196}
1197
Alan Stern645daaa2006-08-30 15:47:02 -04001198/**
1199 * usb_suspend_both - suspend a USB device and its interfaces
1200 * @udev: the usb_device to suspend
1201 * @msg: Power Management message describing this state transition
1202 *
1203 * This is the central routine for suspending USB devices. It calls the
1204 * suspend methods for all the interface drivers in @udev and then calls
Ming Lei303f0842013-03-15 12:08:53 +08001205 * the suspend method for @udev itself. When the routine is called in
1206 * autosuspend, if an error occurs at any stage, all the interfaces
1207 * which were suspended are resumed so that they remain in the same
1208 * state as the device, but when called from system sleep, all error
1209 * from suspend methods of interfaces and the non-root-hub device itself
1210 * are simply ignored, so all suspended interfaces are only resumed
1211 * to the device's state when @udev is root-hub and its suspend method
1212 * returns failure.
Alan Stern645daaa2006-08-30 15:47:02 -04001213 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001214 * Autosuspend requests originating from a child device or an interface
1215 * driver may be made without the protection of @udev's device lock, but
1216 * all other suspend calls will hold the lock. Usbcore will insure that
1217 * method calls do not arrive during bind, unbind, or reset operations.
1218 * However drivers must be prepared to handle suspend calls arriving at
1219 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001220 *
1221 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001222 *
1223 * Return: 0 if the suspend succeeded.
Alan Stern645daaa2006-08-30 15:47:02 -04001224 */
Alan Stern718efa62007-03-09 15:41:13 -05001225static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001226{
1227 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001228 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001229 struct usb_interface *intf;
1230
Alan Stern19410442007-03-27 13:33:59 -04001231 if (udev->state == USB_STATE_NOTATTACHED ||
1232 udev->state == USB_STATE_SUSPENDED)
1233 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001234
Alan Stern645daaa2006-08-30 15:47:02 -04001235 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001236 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001237 n = udev->actconfig->desc.bNumInterfaces;
1238 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001239 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001240 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001241
1242 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001243 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001244 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001245 if (status != 0)
1246 break;
1247 }
1248 }
Alan Stern0af212b2011-06-15 16:27:43 -04001249 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001250 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001251
Alan Sterncd4376e2012-03-28 15:56:17 -04001252 /*
1253 * Ignore errors from non-root-hub devices during
1254 * system sleep transitions. For the most part,
1255 * these devices should go to low power anyway when
1256 * the entire bus is suspended.
1257 */
1258 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001259 status = 0;
1260 }
1261
Alan Sterna8e7c562006-07-01 22:11:02 -04001262 /* If the suspend failed, resume interfaces that did get suspended */
1263 if (status != 0) {
Chen Gang505bdbc2013-04-01 13:04:08 +08001264 if (udev->actconfig) {
1265 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1266 while (++i < n) {
1267 intf = udev->actconfig->interface[i];
1268 usb_resume_interface(udev, intf, msg, 0);
1269 }
Alan Sterna8e7c562006-07-01 22:11:02 -04001270 }
Alan Stern645daaa2006-08-30 15:47:02 -04001271
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001272 /* If the suspend succeeded then prevent any more URB submissions
1273 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001274 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001275 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001276 udev->can_submit = 0;
1277 for (i = 0; i < 16; ++i) {
1278 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1279 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1280 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001281 }
Alan Stern645daaa2006-08-30 15:47:02 -04001282
Alan Stern19410442007-03-27 13:33:59 -04001283 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001284 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001285 return status;
1286}
1287
Alan Stern645daaa2006-08-30 15:47:02 -04001288/**
1289 * usb_resume_both - resume a USB device and its interfaces
1290 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001291 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001292 *
1293 * This is the central routine for resuming USB devices. It calls the
1294 * the resume method for @udev and then calls the resume methods for all
1295 * the interface drivers in @udev.
1296 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001297 * Autoresume requests originating from a child device or an interface
1298 * driver may be made without the protection of @udev's device lock, but
1299 * all other resume calls will hold the lock. Usbcore will insure that
1300 * method calls do not arrive during bind, unbind, or reset operations.
1301 * However drivers must be prepared to handle resume calls arriving at
1302 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001303 *
1304 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001305 *
1306 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001307 */
Alan Stern65bfd292008-11-25 16:39:18 -05001308static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001309{
Alan Stern645daaa2006-08-30 15:47:02 -04001310 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001311 int i;
1312 struct usb_interface *intf;
1313
Alan Stern19410442007-03-27 13:33:59 -04001314 if (udev->state == USB_STATE_NOTATTACHED) {
1315 status = -ENODEV;
1316 goto done;
1317 }
Alan Stern6840d252007-09-10 11:34:26 -04001318 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001319
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001320 /* Resume the device */
1321 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001322 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001323
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001324 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001325 if (status == 0 && udev->actconfig) {
1326 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1327 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001328 usb_resume_interface(udev, intf, msg,
1329 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001330 }
1331 }
Alan Sternc08512c2010-11-15 15:57:58 -05001332 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001333
Alan Stern19410442007-03-27 13:33:59 -04001334 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001335 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001336 if (!status)
1337 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001338 return status;
1339}
1340
Alan Stern5f677f12010-04-02 13:20:11 -04001341static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1342{
Alan Stern48826622010-06-22 16:14:48 -04001343 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001344
1345 /* Remote wakeup is needed only when we actually go to sleep.
1346 * For things like FREEZE and QUIESCE, if the device is already
1347 * autosuspended then its current wakeup setting is okay.
1348 */
1349 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1350 if (udev->state != USB_STATE_SUSPENDED)
1351 udev->do_remote_wakeup = 0;
1352 return;
1353 }
1354
Alan Stern48826622010-06-22 16:14:48 -04001355 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001356 * actually want it.
1357 */
Alan Stern48826622010-06-22 16:14:48 -04001358 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001359
1360 /* If the device is autosuspended with the wrong wakeup setting,
1361 * autoresume now so the setting can be changed.
1362 */
1363 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1364 pm_runtime_resume(&udev->dev);
1365 udev->do_remote_wakeup = w;
1366}
1367
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001368/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001369int usb_suspend(struct device *dev, pm_message_t msg)
1370{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001371 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001372
Oliver Neukum14931382012-01-05 15:39:57 +01001373 unbind_no_pm_drivers_interfaces(udev);
1374
1375 /* From now on we are sure all drivers support suspend/resume
1376 * but not necessarily reset_resume()
1377 * so we may still need to unbind and rebind upon resume
1378 */
Alan Stern5f677f12010-04-02 13:20:11 -04001379 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001380 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001381}
1382
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001383/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001384int usb_resume_complete(struct device *dev)
1385{
1386 struct usb_device *udev = to_usb_device(dev);
1387
1388 /* For PM complete calls, all we do is rebind interfaces
1389 * whose needs_binding flag is set
1390 */
1391 if (udev->state != USB_STATE_NOTATTACHED)
1392 do_rebind_interfaces(udev);
1393 return 0;
1394}
1395
1396/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001397int usb_resume(struct device *dev, pm_message_t msg)
1398{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001399 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001400 int status;
1401
Oliver Neukum98d9a822012-01-11 08:38:35 +01001402 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001403 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001404 * Unbind the interfaces that will need rebinding later,
1405 * because they fail to support reset_resume.
1406 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001407 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001408 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001409 status = usb_resume_both(udev, msg);
1410 if (status == 0) {
1411 pm_runtime_disable(dev);
1412 pm_runtime_set_active(dev);
1413 pm_runtime_enable(dev);
1414 unbind_no_reset_resume_drivers_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001415 }
Alan Stern0c590e22010-01-08 12:57:14 -05001416
1417 /* Avoid PM error messages for devices disconnected while suspended
1418 * as we'll display regular disconnect messages just a bit later.
1419 */
Peter Chen7491f132010-09-27 16:43:25 +08001420 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001421 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001422 return status;
1423}
1424
1425#endif /* CONFIG_PM */
1426
Alan Stern84ebc102013-03-27 16:14:46 -04001427#ifdef CONFIG_PM_RUNTIME
Alan Stern645daaa2006-08-30 15:47:02 -04001428
Alan Stern088f7fe2010-01-08 12:56:54 -05001429/**
1430 * usb_enable_autosuspend - allow a USB device to be autosuspended
1431 * @udev: the USB device which may be autosuspended
1432 *
1433 * This routine allows @udev to be autosuspended. An autosuspend won't
1434 * take place until the autosuspend_delay has elapsed and all the other
1435 * necessary conditions are satisfied.
1436 *
1437 * The caller must hold @udev's device lock.
1438 */
Alan Stern9e18c822010-04-02 13:22:09 -04001439void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001440{
Alan Stern9e18c822010-04-02 13:22:09 -04001441 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001442}
1443EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1444
1445/**
1446 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1447 * @udev: the USB device which may not be autosuspended
1448 *
1449 * This routine prevents @udev from being autosuspended and wakes it up
1450 * if it is already autosuspended.
1451 *
1452 * The caller must hold @udev's device lock.
1453 */
Alan Stern9e18c822010-04-02 13:22:09 -04001454void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001455{
Alan Stern9e18c822010-04-02 13:22:09 -04001456 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001457}
1458EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1459
Alan Stern645daaa2006-08-30 15:47:02 -04001460/**
1461 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001462 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001463 *
1464 * This routine should be called when a core subsystem is finished using
1465 * @udev and wants to allow it to autosuspend. Examples would be when
1466 * @udev's device file in usbfs is closed or after a configuration change.
1467 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001468 * @udev's usage counter is decremented; if it drops to 0 and all the
1469 * interfaces are inactive then a delayed autosuspend will be attempted.
1470 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001471 *
Alan Stern62e299e2010-01-08 12:56:19 -05001472 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001473 *
1474 * This routine can run only in process context.
1475 */
Alan Stern94fcda12006-11-20 11:38:46 -05001476void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001477{
Alan Stern94fcda12006-11-20 11:38:46 -05001478 int status;
1479
Ming Lei6ddf27c2010-11-15 15:57:30 -05001480 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001481 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001482 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1483 __func__, atomic_read(&udev->dev.power.usage_count),
1484 status);
Alan Stern19c26232007-02-20 15:03:32 -05001485}
1486
1487/**
Alan Stern645daaa2006-08-30 15:47:02 -04001488 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001489 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001490 *
1491 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001492 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001493 * occur until usb_autosuspend_device() is called. (Note that this will
1494 * not prevent suspend events originating in the PM core.) Examples would
1495 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001496 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001497 *
Alan Stern94fcda12006-11-20 11:38:46 -05001498 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1499 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001500 *
Alan Stern62e299e2010-01-08 12:56:19 -05001501 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001502 *
1503 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001504 *
1505 * Return: 0 on success. A negative error code otherwise.
Alan Stern645daaa2006-08-30 15:47:02 -04001506 */
Alan Stern94fcda12006-11-20 11:38:46 -05001507int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001508{
1509 int status;
1510
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001511 status = pm_runtime_get_sync(&udev->dev);
1512 if (status < 0)
1513 pm_runtime_put_sync(&udev->dev);
1514 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1515 __func__, atomic_read(&udev->dev.power.usage_count),
1516 status);
1517 if (status > 0)
1518 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001519 return status;
1520}
1521
Alan Stern645daaa2006-08-30 15:47:02 -04001522/**
1523 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001524 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001525 *
1526 * This routine should be called by an interface driver when it is
1527 * finished using @intf and wants to allow it to autosuspend. A typical
1528 * example would be a character-device driver when its device file is
1529 * closed.
1530 *
1531 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001532 * 0, a delayed autosuspend request for @intf's device is attempted. The
1533 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001534 *
Alan Stern645daaa2006-08-30 15:47:02 -04001535 * This routine can run only in process context.
1536 */
1537void usb_autopm_put_interface(struct usb_interface *intf)
1538{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001539 struct usb_device *udev = interface_to_usbdev(intf);
1540 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001541
Ming Lei6ddf27c2010-11-15 15:57:30 -05001542 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001543 atomic_dec(&intf->pm_usage_cnt);
1544 status = pm_runtime_put_sync(&intf->dev);
1545 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1546 __func__, atomic_read(&intf->dev.power.usage_count),
1547 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001548}
1549EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1550
1551/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001552 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1553 * @intf: the usb_interface whose counter should be decremented
1554 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001555 * This routine does much the same thing as usb_autopm_put_interface():
1556 * It decrements @intf's usage counter and schedules a delayed
1557 * autosuspend request if the counter is <= 0. The difference is that it
1558 * does not perform any synchronization; callers should hold a private
1559 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001560 *
1561 * Typically a driver would call this routine during an URB's completion
1562 * handler, if no more URBs were pending.
1563 *
1564 * This routine can run in atomic context.
1565 */
1566void usb_autopm_put_interface_async(struct usb_interface *intf)
1567{
1568 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001569 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001570
Ming Lei6ddf27c2010-11-15 15:57:30 -05001571 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001572 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001573 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001574 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1575 __func__, atomic_read(&intf->dev.power.usage_count),
1576 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001577}
1578EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1579
1580/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001581 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1582 * @intf: the usb_interface whose counter should be decremented
1583 *
1584 * This routine decrements @intf's usage counter but does not carry out an
1585 * autosuspend.
1586 *
1587 * This routine can run in atomic context.
1588 */
1589void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1590{
1591 struct usb_device *udev = interface_to_usbdev(intf);
1592
Ming Lei6ddf27c2010-11-15 15:57:30 -05001593 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001594 atomic_dec(&intf->pm_usage_cnt);
1595 pm_runtime_put_noidle(&intf->dev);
1596}
1597EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1598
1599/**
Alan Stern645daaa2006-08-30 15:47:02 -04001600 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001601 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001602 *
1603 * This routine should be called by an interface driver when it wants to
1604 * use @intf and needs to guarantee that it is not suspended. In addition,
1605 * the routine prevents @intf from being autosuspended subsequently. (Note
1606 * that this will not prevent suspend events originating in the PM core.)
1607 * This prevention will persist until usb_autopm_put_interface() is called
1608 * or @intf is unbound. A typical example would be a character-device
1609 * driver when its device file is opened.
1610 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001611 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1612 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001613 *
1614 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001615 *
1616 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001617 */
1618int usb_autopm_get_interface(struct usb_interface *intf)
1619{
Alan Sternaf4f7602006-10-30 17:06:45 -05001620 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001621
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001622 status = pm_runtime_get_sync(&intf->dev);
1623 if (status < 0)
1624 pm_runtime_put_sync(&intf->dev);
1625 else
1626 atomic_inc(&intf->pm_usage_cnt);
1627 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1628 __func__, atomic_read(&intf->dev.power.usage_count),
1629 status);
1630 if (status > 0)
1631 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001632 return status;
1633}
1634EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1635
Alan Stern692a1862006-10-30 17:07:51 -05001636/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001637 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1638 * @intf: the usb_interface whose counter should be incremented
1639 *
1640 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001641 * usb_autopm_get_interface(): It increments @intf's usage counter and
1642 * queues an autoresume request if the device is suspended. The
1643 * differences are that it does not perform any synchronization (callers
1644 * should hold a private lock and handle all synchronization issues
1645 * themselves), and it does not autoresume the device directly (it only
1646 * queues a request). After a successful call, the device may not yet be
1647 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001648 *
1649 * This routine can run in atomic context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001650 *
1651 * Return: 0 on success. A negative error code otherwise.
Alan Stern9ac39f22008-11-12 16:19:49 -05001652 */
1653int usb_autopm_get_interface_async(struct usb_interface *intf)
1654{
Ming Lei63defa72010-11-15 15:56:54 -05001655 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001656
Ming Lei63defa72010-11-15 15:56:54 -05001657 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001658 if (status < 0 && status != -EINPROGRESS)
1659 pm_runtime_put_noidle(&intf->dev);
1660 else
Alan Sternccf5b802009-06-29 11:00:01 -04001661 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001662 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1663 __func__, atomic_read(&intf->dev.power.usage_count),
1664 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001665 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001666 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001667 return status;
1668}
1669EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1670
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001671/**
1672 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1673 * @intf: the usb_interface whose counter should be incremented
1674 *
1675 * This routine increments @intf's usage counter but does not carry out an
1676 * autoresume.
1677 *
1678 * This routine can run in atomic context.
1679 */
1680void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1681{
1682 struct usb_device *udev = interface_to_usbdev(intf);
1683
Ming Lei6ddf27c2010-11-15 15:57:30 -05001684 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001685 atomic_inc(&intf->pm_usage_cnt);
1686 pm_runtime_get_noresume(&intf->dev);
1687}
1688EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1689
1690/* Internal routine to check whether we may autosuspend a device. */
1691static int autosuspend_check(struct usb_device *udev)
1692{
Alan Stern7560d32e2010-04-02 13:18:50 -04001693 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001694 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001695
1696 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1697 * any interface drivers require remote wakeup but it isn't available.
1698 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001699 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001700 if (udev->actconfig) {
1701 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1702 intf = udev->actconfig->interface[i];
1703
1704 /* We don't need to check interfaces that are
1705 * disabled for runtime PM. Either they are unbound
1706 * or else their drivers don't support autosuspend
1707 * and so they are permanently active.
1708 */
1709 if (intf->dev.power.disable_depth)
1710 continue;
1711 if (atomic_read(&intf->dev.power.usage_count) > 0)
1712 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001713 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001714
1715 /* Don't allow autosuspend if the device will need
1716 * a reset-resume and any of its interface drivers
1717 * doesn't include support or needs remote wakeup.
1718 */
1719 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1720 struct usb_driver *driver;
1721
1722 driver = to_usb_driver(intf->dev.driver);
1723 if (!driver->reset_resume ||
1724 intf->needs_remote_wakeup)
1725 return -EOPNOTSUPP;
1726 }
1727 }
1728 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001729 if (w && !device_can_wakeup(&udev->dev)) {
1730 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1731 return -EOPNOTSUPP;
1732 }
1733 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001734 return 0;
1735}
1736
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001737int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001738{
Ming Lei63defa72010-11-15 15:56:54 -05001739 struct usb_device *udev = to_usb_device(dev);
1740 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001741
1742 /* A USB device can be suspended if it passes the various autosuspend
1743 * checks. Runtime suspend for a USB device means suspending all the
1744 * interfaces and then the device itself.
1745 */
Ming Lei63defa72010-11-15 15:56:54 -05001746 if (autosuspend_check(udev) != 0)
1747 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001748
Ming Lei63defa72010-11-15 15:56:54 -05001749 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001750
1751 /* Allow a retry if autosuspend failed temporarily */
1752 if (status == -EAGAIN || status == -EBUSY)
1753 usb_mark_last_busy(udev);
1754
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001755 /* The PM core reacts badly unless the return code is 0,
1756 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1757 */
1758 if (status != 0)
1759 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001760 return status;
1761}
1762
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001763int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001764{
Ming Lei63defa72010-11-15 15:56:54 -05001765 struct usb_device *udev = to_usb_device(dev);
1766 int status;
1767
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001768 /* Runtime resume for a USB device means resuming both the device
1769 * and all its interfaces.
1770 */
Ming Lei63defa72010-11-15 15:56:54 -05001771 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001772 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001773}
1774
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001775int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001776{
Ming Lei63defa72010-11-15 15:56:54 -05001777 struct usb_device *udev = to_usb_device(dev);
1778
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001779 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001780 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001781 */
Ming Lei63defa72010-11-15 15:56:54 -05001782 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001783 pm_runtime_autosuspend(dev);
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001784 /* Tell the core not to suspend it, though. */
1785 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001786}
1787
Andiry Xu65580b432011-09-23 14:19:52 -07001788int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1789{
1790 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1791 int ret = -EPERM;
1792
Sarah Sharpde68bab2013-09-30 17:26:28 +03001793 if (enable && !udev->usb2_hw_lpm_allowed)
1794 return 0;
1795
Andiry Xu65580b432011-09-23 14:19:52 -07001796 if (hcd->driver->set_usb2_hw_lpm) {
1797 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1798 if (!ret)
1799 udev->usb2_hw_lpm_enabled = enable;
1800 }
1801
1802 return ret;
1803}
1804
Alan Stern84ebc102013-03-27 16:14:46 -04001805#endif /* CONFIG_PM_RUNTIME */
Alan Stern645daaa2006-08-30 15:47:02 -04001806
Alan Stern36e56a32006-07-01 22:08:06 -04001807struct bus_type usb_bus_type = {
1808 .name = "usb",
1809 .match = usb_device_match,
1810 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001811};