blob: 263c5035eabfd942ab2d4907d8547a61e425695e [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#ifdef CONFIG_HOTPLUG
36
37/*
38 * Adds a new dynamic USBdevice ID to this driver,
39 * and cause the driver to probe for all devices again.
40 */
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010041ssize_t usb_store_new_id(struct usb_dynids *dynids,
42 struct device_driver *driver,
43 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080044{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080045 struct usb_dynid *dynid;
46 u32 idVendor = 0;
47 u32 idProduct = 0;
Josua Dietzeff231db2011-10-23 14:22:29 +020048 unsigned int bInterfaceClass = 0;
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
Josua Dietzeff231db2011-10-23 14:22:29 +020052 fields = sscanf(buf, "%x %x %x", &idVendor, &idProduct,
53 &bInterfaceClass);
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;
Josua Dietzeff231db2011-10-23 14:22:29 +020065 if (fields == 3) {
66 dynid->id.bInterfaceClass = (u8)bInterfaceClass;
67 dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
68 }
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080069
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010070 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020071 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010072 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080073
Alan Sterncef9bc52012-01-24 13:34:41 -050074 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080075
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070076 if (retval)
77 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080078 return count;
79}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010080EXPORT_SYMBOL_GPL(usb_store_new_id);
81
Bjørn Morkef206f32012-05-13 12:35:00 +020082ssize_t usb_show_dynids(struct usb_dynids *dynids, char *buf)
Bjørn Morke6bbcef2012-05-13 12:34:59 +020083{
84 struct usb_dynid *dynid;
Bjørn Morke6bbcef2012-05-13 12:34:59 +020085 size_t count = 0;
86
Bjørn Morkef206f32012-05-13 12:35:00 +020087 list_for_each_entry(dynid, &dynids->list, node)
Bjørn Morke6bbcef2012-05-13 12:34:59 +020088 if (dynid->id.bInterfaceClass != 0)
89 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n",
90 dynid->id.idVendor, dynid->id.idProduct,
91 dynid->id.bInterfaceClass);
92 else
93 count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n",
94 dynid->id.idVendor, dynid->id.idProduct);
95 return count;
96}
Bjørn Morkef206f32012-05-13 12:35:00 +020097EXPORT_SYMBOL_GPL(usb_show_dynids);
98
99static ssize_t show_dynids(struct device_driver *driver, char *buf)
100{
101 struct usb_driver *usb_drv = to_usb_driver(driver);
102
103 return usb_show_dynids(&usb_drv->dynids, buf);
104}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200105
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100106static ssize_t store_new_id(struct device_driver *driver,
107 const char *buf, size_t count)
108{
109 struct usb_driver *usb_drv = to_usb_driver(driver);
110
111 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
112}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200113static DRIVER_ATTR(new_id, S_IRUGO | S_IWUSR, show_dynids, store_new_id);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800114
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800115/**
116 * store_remove_id - remove a USB device ID from this driver
117 * @driver: target device driver
118 * @buf: buffer for scanning device ID data
119 * @count: input size
120 *
121 * Removes a dynamic usb device ID from this driver.
122 */
123static ssize_t
124store_remove_id(struct device_driver *driver, const char *buf, size_t count)
125{
126 struct usb_dynid *dynid, *n;
127 struct usb_driver *usb_driver = to_usb_driver(driver);
Alan Coxac08de32012-09-17 11:55:23 +0100128 u32 idVendor;
129 u32 idProduct;
130 int fields;
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800131
132 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
133 if (fields < 2)
134 return -EINVAL;
135
136 spin_lock(&usb_driver->dynids.lock);
137 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
138 struct usb_device_id *id = &dynid->id;
139 if ((id->idVendor == idVendor) &&
140 (id->idProduct == idProduct)) {
141 list_del(&dynid->node);
142 kfree(dynid);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800143 break;
144 }
145 }
146 spin_unlock(&usb_driver->dynids.lock);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800147 return count;
148}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200149static DRIVER_ATTR(remove_id, S_IRUGO | S_IWUSR, show_dynids, store_remove_id);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800150
Alan Sterned283e92012-01-24 14:35:13 -0500151static int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800152{
153 int error = 0;
154
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800155 if (usb_drv->no_dynamic_id)
156 goto exit;
157
Alan Sterned283e92012-01-24 14:35:13 -0500158 if (usb_drv->probe != NULL) {
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800159 error = driver_create_file(&usb_drv->drvwrap.driver,
160 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500161 if (error == 0) {
162 error = driver_create_file(&usb_drv->drvwrap.driver,
163 &driver_attr_remove_id);
164 if (error)
165 driver_remove_file(&usb_drv->drvwrap.driver,
166 &driver_attr_new_id);
167 }
168 }
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800169exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800170 return error;
171}
172
Alan Sterned283e92012-01-24 14:35:13 -0500173static void usb_remove_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800174{
175 if (usb_drv->no_dynamic_id)
176 return;
177
Alan Sterned283e92012-01-24 14:35:13 -0500178 if (usb_drv->probe != NULL) {
179 driver_remove_file(&usb_drv->drvwrap.driver,
180 &driver_attr_remove_id);
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800181 driver_remove_file(&usb_drv->drvwrap.driver,
182 &driver_attr_new_id);
Alan Sterned283e92012-01-24 14:35:13 -0500183 }
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800184}
185
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800186static void usb_free_dynids(struct usb_driver *usb_drv)
187{
188 struct usb_dynid *dynid, *n;
189
190 spin_lock(&usb_drv->dynids.lock);
191 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
192 list_del(&dynid->node);
193 kfree(dynid);
194 }
195 spin_unlock(&usb_drv->dynids.lock);
196}
197#else
Alan Sterned283e92012-01-24 14:35:13 -0500198static inline int usb_create_newid_files(struct usb_driver *usb_drv)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800199{
200 return 0;
201}
202
Alan Sterned283e92012-01-24 14:35:13 -0500203static void usb_remove_newid_files(struct usb_driver *usb_drv)
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800204{
205}
206
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800207static inline void usb_free_dynids(struct usb_driver *usb_drv)
208{
209}
210#endif
211
212static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
213 struct usb_driver *drv)
214{
215 struct usb_dynid *dynid;
216
217 spin_lock(&drv->dynids.lock);
218 list_for_each_entry(dynid, &drv->dynids.list, node) {
219 if (usb_match_one_id(intf, &dynid->id)) {
220 spin_unlock(&drv->dynids.lock);
221 return &dynid->id;
222 }
223 }
224 spin_unlock(&drv->dynids.lock);
225 return NULL;
226}
227
228
Alan Stern8bb54ab2006-07-01 22:08:49 -0400229/* called from driver core with dev locked */
230static int usb_probe_device(struct device *dev)
231{
232 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200233 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500234 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400235
Harvey Harrison441b62c2008-03-03 16:08:34 -0800236 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400237
Alan Stern8bb54ab2006-07-01 22:08:49 -0400238 /* TODO: Add real matching code */
239
Alan Stern645daaa2006-08-30 15:47:02 -0400240 /* The device should always appear to be in use
Masanari Iida02582e92012-08-22 19:11:26 +0900241 * unless the driver supports autosuspend.
Alan Stern645daaa2006-08-30 15:47:02 -0400242 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500243 if (!udriver->supports_autosuspend)
244 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400245
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500246 if (!error)
247 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400248 return error;
249}
250
251/* called from driver core with dev locked */
252static int usb_unbind_device(struct device *dev)
253{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500254 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400255 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
256
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500257 udriver->disconnect(udev);
258 if (!udriver->supports_autosuspend)
259 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400260 return 0;
261}
262
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800263/*
264 * Cancel any pending scheduled resets
265 *
266 * [see usb_queue_reset_device()]
267 *
268 * Called after unconfiguring / when releasing interfaces. See
269 * comments in __usb_queue_reset_device() regarding
270 * udev->reset_running.
271 */
272static void usb_cancel_queued_reset(struct usb_interface *iface)
273{
274 if (iface->reset_running == 0)
275 cancel_work_sync(&iface->reset_ws);
276}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400277
278/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800279static int usb_probe_interface(struct device *dev)
280{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400281 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200282 struct usb_interface *intf = to_usb_interface(dev);
283 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800284 const struct usb_device_id *id;
285 int error = -ENODEV;
Sarah Sharp83060952012-05-02 14:25:52 -0700286 int lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800287
Harvey Harrison441b62c2008-03-03 16:08:34 -0800288 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800289
Alan Stern78d9a482008-06-23 16:00:40 -0400290 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800291
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400292 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500293 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400294
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800295 if (udev->authorized == 0) {
296 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500297 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800298 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700299
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800300 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800301 if (!id)
302 id = usb_match_dynamic_id(intf, driver);
Alan Stern0f3dda92010-01-08 12:56:04 -0500303 if (!id)
304 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800305
Alan Stern0f3dda92010-01-08 12:56:04 -0500306 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400307
Alan Stern0f3dda92010-01-08 12:56:04 -0500308 error = usb_autoresume_device(udev);
309 if (error)
310 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400311
Alan Stern0f3dda92010-01-08 12:56:04 -0500312 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400313
Alan Stern571dc792010-04-09 16:03:43 -0400314 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500315 * runtime-PM-enabled only if the driver has autosuspend support.
316 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500317 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500318 pm_runtime_set_active(dev);
319 pm_suspend_ignore_children(dev, false);
320 if (driver->supports_autosuspend)
321 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200322
Sarah Sharp83060952012-05-02 14:25:52 -0700323 /* If the new driver doesn't allow hub-initiated LPM, and we can't
324 * disable hub-initiated LPM, then fail the probe.
325 *
326 * Otherwise, leaving LPM enabled should be harmless, because the
327 * endpoint intervals should remain the same, and the U1/U2 timeouts
328 * should remain the same.
329 *
330 * If we need to install alt setting 0 before probe, or another alt
331 * setting during probe, that should also be fine. usb_set_interface()
332 * will attempt to disable LPM, and fail if it can't disable it.
333 */
334 lpm_disable_error = usb_unlocked_disable_lpm(udev);
335 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
336 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
337 __func__, driver->name);
338 error = lpm_disable_error;
339 goto err;
340 }
341
Alan Stern0f3dda92010-01-08 12:56:04 -0500342 /* Carry out a deferred switch to altsetting 0 */
343 if (intf->needs_altsetting0) {
344 error = usb_set_interface(udev, intf->altsetting[0].
345 desc.bInterfaceNumber, 0);
346 if (error < 0)
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200347 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500348 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800349 }
350
Alan Stern0f3dda92010-01-08 12:56:04 -0500351 error = driver->probe(intf, id);
352 if (error)
353 goto err;
354
355 intf->condition = USB_INTERFACE_BOUND;
Sarah Sharp83060952012-05-02 14:25:52 -0700356
357 /* If the LPM disable succeeded, balance the ref counts. */
358 if (!lpm_disable_error)
359 usb_unlocked_enable_lpm(udev);
360
Alan Stern0f3dda92010-01-08 12:56:04 -0500361 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800362 return error;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200363
Alan Stern0f3dda92010-01-08 12:56:04 -0500364 err:
Hans de Goedee714fad2012-05-22 11:36:59 +0200365 usb_set_intfdata(intf, NULL);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200366 intf->needs_remote_wakeup = 0;
367 intf->condition = USB_INTERFACE_UNBOUND;
368 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500369
Sarah Sharpd01f87c2012-10-04 09:53:43 -0700370 /* If the LPM disable succeeded, balance the ref counts. */
371 if (!lpm_disable_error)
372 usb_unlocked_enable_lpm(udev);
373
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500374 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400375 if (driver->supports_autosuspend)
376 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500377 pm_runtime_set_suspended(dev);
378
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200379 usb_autosuspend_device(udev);
380 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800381}
382
Alan Stern8bb54ab2006-07-01 22:08:49 -0400383/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800384static int usb_unbind_interface(struct device *dev)
385{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400386 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800387 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400388 struct usb_device *udev;
Sarah Sharp83060952012-05-02 14:25:52 -0700389 int error, r, lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800390
391 intf->condition = USB_INTERFACE_UNBINDING;
392
Alan Stern645daaa2006-08-30 15:47:02 -0400393 /* Autoresume for set_interface call below */
394 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500395 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400396
Sarah Sharp83060952012-05-02 14:25:52 -0700397 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
398 * the driver is unbound. If LPM isn't disabled, that's fine because it
399 * wouldn't be enabled unless all the bound interfaces supported
400 * hub-initiated LPM.
401 */
402 lpm_disable_error = usb_unlocked_disable_lpm(udev);
403
Alan Stern9da82bd2008-05-08 11:54:37 -0400404 /* Terminate all URBs for this interface unless the driver
405 * supports "soft" unbinding.
406 */
407 if (!driver->soft_unbind)
Alan Sternddeac4e2009-01-15 17:03:33 -0500408 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800409
Alan Stern8bb54ab2006-07-01 22:08:49 -0400410 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800411 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800412
Alan Stern55151d72008-08-12 14:33:59 -0400413 /* Reset other interface state.
414 * We cannot do a Set-Interface if the device is suspended or
415 * if it is prepared for a system sleep (since installing a new
416 * altsetting means creating new endpoint device entries).
417 * When either of these happens, defer the Set-Interface.
418 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500419 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
420 /* Already in altsetting 0 so skip Set-Interface.
421 * Just re-enable it without affecting the endpoint toggles.
422 */
423 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200424 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200425 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400426 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200427 if (r < 0)
428 intf->needs_altsetting0 = 1;
429 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400430 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200431 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800432 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400433
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800434 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400435 intf->needs_remote_wakeup = 0;
436
Sarah Sharp83060952012-05-02 14:25:52 -0700437 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
438 if (!lpm_disable_error)
439 usb_unlocked_enable_lpm(udev);
440
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500441 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400442 if (driver->supports_autosuspend)
443 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500444 pm_runtime_set_suspended(dev);
445
446 /* Undo any residual pm_autopm_get_interface_* calls */
447 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
448 usb_autopm_put_interface_no_suspend(intf);
449 atomic_set(&intf->pm_usage_cnt, 0);
450
Alan Stern645daaa2006-08-30 15:47:02 -0400451 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500452 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800453
454 return 0;
455}
456
Alan Stern36e56a32006-07-01 22:08:06 -0400457/**
458 * usb_driver_claim_interface - bind a driver to an interface
459 * @driver: the driver to be bound
460 * @iface: the interface to which it will be bound; must be in the
461 * usb device's active configuration
462 * @priv: driver data associated with that interface
463 *
464 * This is used by usb device drivers that need to claim more than one
465 * interface on a device when probing (audio and acm are current examples).
466 * No device driver should directly modify internal usb_interface or
467 * usb_device structure members.
468 *
469 * Few drivers should need to use this routine, since the most natural
470 * way to bind to an interface is to return the private data from
471 * the driver's probe() method.
472 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400473 * Callers must own the device lock, so driver probe() entries don't need
474 * extra locking, but other call contexts may need to explicitly claim that
475 * lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400476 */
477int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800478 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400479{
480 struct device *dev = &iface->dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700481 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700482 int retval = 0;
Sarah Sharp83060952012-05-02 14:25:52 -0700483 int lpm_disable_error;
Alan Stern36e56a32006-07-01 22:08:06 -0400484
485 if (dev->driver)
486 return -EBUSY;
487
Sarah Sharp83060952012-05-02 14:25:52 -0700488 udev = interface_to_usbdev(iface);
489
Alan Stern8bb54ab2006-07-01 22:08:49 -0400490 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400491 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400492 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400493
Alan Stern36e56a32006-07-01 22:08:06 -0400494 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500495
Sarah Sharp83060952012-05-02 14:25:52 -0700496 /* Disable LPM until this driver is bound. */
497 lpm_disable_error = usb_unlocked_disable_lpm(udev);
498 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
499 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
500 __func__, driver->name);
501 return -ENOMEM;
502 }
503
Alan Stern89842ae2010-05-11 11:44:06 -0400504 /* Claimed interfaces are initially inactive (suspended) and
505 * runtime-PM-enabled, but only if the driver has autosuspend
506 * support. Otherwise they are marked active, to prevent the
507 * device from being autosuspended, but left disabled. In either
508 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500509 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500510 pm_suspend_ignore_children(dev, false);
511 if (driver->supports_autosuspend)
512 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400513 else
514 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400515
516 /* if interface was already added, bind now; else let
517 * the future device_add() bind it, bypassing probe()
518 */
519 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700520 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400521
Sarah Sharp83060952012-05-02 14:25:52 -0700522 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
523 if (!lpm_disable_error)
524 usb_unlocked_enable_lpm(udev);
525
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700526 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400527}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600528EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400529
530/**
531 * usb_driver_release_interface - unbind a driver from an interface
532 * @driver: the driver to be unbound
533 * @iface: the interface from which it will be unbound
534 *
535 * This can be used by drivers to release an interface without waiting
536 * for their disconnect() methods to be called. In typical cases this
537 * also causes the driver disconnect() method to be called.
538 *
539 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400540 * Callers must own the device lock, so driver disconnect() entries don't
541 * need extra locking, but other call contexts may need to explicitly claim
542 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400543 */
544void usb_driver_release_interface(struct usb_driver *driver,
545 struct usb_interface *iface)
546{
547 struct device *dev = &iface->dev;
548
549 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400550 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400551 return;
552
553 /* don't release from within disconnect() */
554 if (iface->condition != USB_INTERFACE_BOUND)
555 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400556 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400557
Alan Stern91f8d062009-04-16 15:35:09 -0400558 /* Release via the driver core only if the interface
559 * has already been registered
560 */
Alan Stern36e56a32006-07-01 22:08:06 -0400561 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400562 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800563 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800564 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400565 usb_unbind_interface(dev);
566 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800567 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400568 }
Alan Stern36e56a32006-07-01 22:08:06 -0400569}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600570EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400571
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800572/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100573int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800574{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800575 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
576 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
577 return 0;
578
579 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
580 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
581 return 0;
582
583 /* No need to test id->bcdDevice_lo != 0, since 0 is never
584 greater than any unsigned number. */
585 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
586 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
587 return 0;
588
589 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
590 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
591 return 0;
592
593 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
594 (id->bDeviceClass != dev->descriptor.bDeviceClass))
595 return 0;
596
597 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800598 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800599 return 0;
600
601 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
602 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
603 return 0;
604
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100605 return 1;
606}
607
608/* returns 0 if no match, 1 if match */
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200609int usb_match_one_id_intf(struct usb_device *dev,
610 struct usb_host_interface *intf,
611 const struct usb_device_id *id)
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100612{
Bjørn Mork81df2d52012-05-18 21:27:43 +0200613 /* The interface class, subclass, protocol and number should never be
Alan Stern93c8bf42006-10-18 16:41:51 -0400614 * checked for a match if the device class is Vendor Specific,
615 * unless the match record specifies the Vendor ID. */
616 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
617 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
618 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
619 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
Bjørn Mork81df2d52012-05-18 21:27:43 +0200620 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
621 USB_DEVICE_ID_MATCH_INT_NUMBER)))
Alan Stern93c8bf42006-10-18 16:41:51 -0400622 return 0;
623
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800624 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
625 (id->bInterfaceClass != intf->desc.bInterfaceClass))
626 return 0;
627
628 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
629 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
630 return 0;
631
632 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
633 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
634 return 0;
635
Bjørn Mork81df2d52012-05-18 21:27:43 +0200636 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
637 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
638 return 0;
639
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800640 return 1;
641}
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200642
643/* returns 0 if no match, 1 if match */
644int usb_match_one_id(struct usb_interface *interface,
645 const struct usb_device_id *id)
646{
647 struct usb_host_interface *intf;
648 struct usb_device *dev;
649
650 /* proc_connectinfo in devio.c may call us with id == NULL. */
651 if (id == NULL)
652 return 0;
653
654 intf = interface->cur_altsetting;
655 dev = interface_to_usbdev(interface);
656
657 if (!usb_match_device(dev, id))
658 return 0;
659
660 return usb_match_one_id_intf(dev, intf, id);
661}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100662EXPORT_SYMBOL_GPL(usb_match_one_id);
663
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800664/**
665 * usb_match_id - find first usb_device_id matching device or interface
666 * @interface: the interface of interest
667 * @id: array of usb_device_id structures, terminated by zero entry
668 *
669 * usb_match_id searches an array of usb_device_id's and returns
670 * the first one matching the device or interface, or null.
671 * This is used when binding (or rebinding) a driver to an interface.
672 * Most USB device drivers will use this indirectly, through the usb core,
673 * but some layered driver frameworks use it directly.
674 * These device tables are exported with MODULE_DEVICE_TABLE, through
675 * modutils, to support the driver loading functionality of USB hotplugging.
676 *
677 * What Matches:
678 *
679 * The "match_flags" element in a usb_device_id controls which
680 * members are used. If the corresponding bit is set, the
681 * value in the device_id must match its corresponding member
682 * in the device or interface descriptor, or else the device_id
683 * does not match.
684 *
685 * "driver_info" is normally used only by device drivers,
686 * but you can create a wildcard "matches anything" usb_device_id
687 * as a driver's "modules.usbmap" entry if you provide an id with
688 * only a nonzero "driver_info" field. If you do this, the USB device
689 * driver's probe() routine should use additional intelligence to
690 * decide whether to bind to the specified interface.
691 *
692 * What Makes Good usb_device_id Tables:
693 *
694 * The match algorithm is very simple, so that intelligence in
695 * driver selection must come from smart driver id records.
696 * Unless you have good reasons to use another selection policy,
697 * provide match elements only in related groups, and order match
698 * specifiers from specific to general. Use the macros provided
699 * for that purpose if you can.
700 *
701 * The most specific match specifiers use device descriptor
702 * data. These are commonly used with product-specific matches;
703 * the USB_DEVICE macro lets you provide vendor and product IDs,
704 * and you can also match against ranges of product revisions.
705 * These are widely used for devices with application or vendor
706 * specific bDeviceClass values.
707 *
708 * Matches based on device class/subclass/protocol specifications
709 * are slightly more general; use the USB_DEVICE_INFO macro, or
710 * its siblings. These are used with single-function devices
711 * where bDeviceClass doesn't specify that each interface has
712 * its own class.
713 *
714 * Matches based on interface class/subclass/protocol are the
715 * most general; they let drivers bind to any interface on a
716 * multiple-function device. Use the USB_INTERFACE_INFO
717 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400718 * devices (as recorded in bInterfaceClass).
719 *
720 * Note that an entry created by USB_INTERFACE_INFO won't match
721 * any interface if the device class is set to Vendor-Specific.
722 * This is deliberate; according to the USB spec the meanings of
723 * the interface class/subclass/protocol for these devices are also
724 * vendor-specific, and hence matching against a standard product
725 * class wouldn't work anyway. If you really want to use an
726 * interface-based match for such a device, create a match record
727 * that also specifies the vendor ID. (Unforunately there isn't a
728 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800729 *
730 * Within those groups, remember that not all combinations are
731 * meaningful. For example, don't give a product version range
732 * without vendor and product IDs; or specify a protocol without
733 * its associated class and subclass.
734 */
735const struct usb_device_id *usb_match_id(struct usb_interface *interface,
736 const struct usb_device_id *id)
737{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800738 /* proc_connectinfo in devio.c may call us with id == NULL. */
739 if (id == NULL)
740 return NULL;
741
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800742 /* It is important to check that id->driver_info is nonzero,
743 since an entry that is all zeroes except for a nonzero
744 id->driver_info is the way to create an entry that
745 indicates that the driver want to examine every
746 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800747 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
748 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800749 if (usb_match_one_id(interface, id))
750 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800751 }
752
753 return NULL;
754}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600755EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800756
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100757static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800758{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400759 /* devices and interfaces are handled separately */
760 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800761
Alan Stern8bb54ab2006-07-01 22:08:49 -0400762 /* interface drivers never match devices */
763 if (!is_usb_device_driver(drv))
764 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800765
Alan Stern8bb54ab2006-07-01 22:08:49 -0400766 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800767 return 1;
768
Kay Sievers55129662009-05-04 19:48:32 +0200769 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400770 struct usb_interface *intf;
771 struct usb_driver *usb_drv;
772 const struct usb_device_id *id;
773
774 /* device drivers never match interfaces */
775 if (is_usb_device_driver(drv))
776 return 0;
777
778 intf = to_usb_interface(dev);
779 usb_drv = to_usb_driver(drv);
780
781 id = usb_match_id(intf, usb_drv->id_table);
782 if (id)
783 return 1;
784
785 id = usb_match_dynamic_id(intf, usb_drv);
786 if (id)
787 return 1;
788 }
789
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800790 return 0;
791}
792
Alan Stern36e56a32006-07-01 22:08:06 -0400793#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200794static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400795{
Alan Stern36e56a32006-07-01 22:08:06 -0400796 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400797
Kay Sievers55129662009-05-04 19:48:32 +0200798 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400799 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200800 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100801 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200802
Alan Stern8bb54ab2006-07-01 22:08:49 -0400803 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200804 } else {
805 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400806 }
Alan Stern36e56a32006-07-01 22:08:06 -0400807
808 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500809 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200810 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400811 return -ENODEV;
812 }
813 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200814 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400815 return -ENODEV;
816 }
817
Alan Stern36e56a32006-07-01 22:08:06 -0400818 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200819 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400820 le16_to_cpu(usb_dev->descriptor.idVendor),
821 le16_to_cpu(usb_dev->descriptor.idProduct),
822 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
823 return -ENOMEM;
824
825 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200826 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400827 usb_dev->descriptor.bDeviceClass,
828 usb_dev->descriptor.bDeviceSubClass,
829 usb_dev->descriptor.bDeviceProtocol))
830 return -ENOMEM;
831
Alan Stern36e56a32006-07-01 22:08:06 -0400832 return 0;
833}
834
835#else
836
Kay Sievers7eff2e72007-08-14 15:15:12 +0200837static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400838{
839 return -ENODEV;
840}
Alan Stern36e56a32006-07-01 22:08:06 -0400841#endif /* CONFIG_HOTPLUG */
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.
851 * Returns a negative error code on failure and 0 on success.
852 */
853int usb_register_device_driver(struct usb_device_driver *new_udriver,
854 struct module *owner)
855{
856 int retval = 0;
857
858 if (usb_disabled())
859 return -ENODEV;
860
861 new_udriver->drvwrap.for_devices = 1;
862 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
863 new_udriver->drvwrap.driver.bus = &usb_bus_type;
864 new_udriver->drvwrap.driver.probe = usb_probe_device;
865 new_udriver->drvwrap.driver.remove = usb_unbind_device;
866 new_udriver->drvwrap.driver.owner = owner;
867
868 retval = driver_register(&new_udriver->drvwrap.driver);
869
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700870 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400871 pr_info("%s: registered new device driver %s\n",
872 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700873 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400874 printk(KERN_ERR "%s: error %d registering device "
875 " driver %s\n",
876 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400877
878 return retval;
879}
880EXPORT_SYMBOL_GPL(usb_register_device_driver);
881
882/**
883 * usb_deregister_device_driver - unregister a USB device (not interface) driver
884 * @udriver: USB operations of the device driver to unregister
885 * Context: must be able to sleep
886 *
887 * Unlinks the specified driver from the internal USB driver list.
888 */
889void usb_deregister_device_driver(struct usb_device_driver *udriver)
890{
891 pr_info("%s: deregistering device driver %s\n",
892 usbcore_name, udriver->name);
893
894 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400895}
896EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
897
898/**
899 * usb_register_driver - register a USB interface driver
900 * @new_driver: USB operations for the interface driver
901 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800902 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400903 *
904 * Registers a USB interface driver with the USB core. The list of
905 * unattached interfaces will be rescanned whenever a new driver is
906 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800907 * Returns a negative error code on failure and 0 on success.
908 *
909 * NOTE: if you want your driver to use the USB major number, you must call
910 * usb_register_dev() to enable that functionality. This function no longer
911 * takes care of that.
912 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800913int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
914 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800915{
916 int retval = 0;
917
918 if (usb_disabled())
919 return -ENODEV;
920
Alan Stern8bb54ab2006-07-01 22:08:49 -0400921 new_driver->drvwrap.for_devices = 0;
922 new_driver->drvwrap.driver.name = (char *) new_driver->name;
923 new_driver->drvwrap.driver.bus = &usb_bus_type;
924 new_driver->drvwrap.driver.probe = usb_probe_interface;
925 new_driver->drvwrap.driver.remove = usb_unbind_interface;
926 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800927 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800928 spin_lock_init(&new_driver->dynids.lock);
929 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800930
Alan Stern8bb54ab2006-07-01 22:08:49 -0400931 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800932 if (retval)
933 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800934
Alan Sterned283e92012-01-24 14:35:13 -0500935 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800936 if (retval)
937 goto out_newid;
938
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800939 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800940 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800941
942out:
943 return retval;
944
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800945out_newid:
946 driver_unregister(&new_driver->drvwrap.driver);
947
948 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400949 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800950 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800951 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800952}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600953EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800954
955/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400956 * usb_deregister - unregister a USB interface driver
957 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800958 * Context: must be able to sleep
959 *
960 * Unlinks the specified driver from the internal USB driver list.
961 *
962 * NOTE: If you called usb_register_dev(), you still need to call
963 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
964 * this * call will no longer do it for you.
965 */
966void usb_deregister(struct usb_driver *driver)
967{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400968 pr_info("%s: deregistering interface driver %s\n",
969 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800970
Alan Sterned283e92012-01-24 14:35:13 -0500971 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400972 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -0500973 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800974}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600975EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400976
Alan Stern78d9a482008-06-23 16:00:40 -0400977/* Forced unbinding of a USB interface driver, either because
978 * it doesn't support pre_reset/post_reset/reset_resume or
979 * because it doesn't support suspend/resume.
980 *
981 * The caller must hold @intf's device's lock, but not its pm_mutex
982 * and not @intf->dev.sem.
983 */
984void usb_forced_unbind_intf(struct usb_interface *intf)
985{
986 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
987
988 dev_dbg(&intf->dev, "forced unbind\n");
989 usb_driver_release_interface(driver, intf);
990
991 /* Mark the interface for later rebinding */
992 intf->needs_binding = 1;
993}
994
995/* Delayed forced unbinding of a USB interface driver and scan
996 * for rebinding.
997 *
998 * The caller must hold @intf's device's lock, but not its pm_mutex
999 * and not @intf->dev.sem.
1000 *
Alan Stern5096aed2008-08-12 14:34:14 -04001001 * Note: Rebinds will be skipped if a system sleep transition is in
1002 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -04001003 */
1004void usb_rebind_intf(struct usb_interface *intf)
1005{
1006 int rc;
1007
1008 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +01001009 if (intf->dev.driver)
1010 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001011
1012 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +02001013 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -04001014 intf->needs_binding = 0;
1015 rc = device_attach(&intf->dev);
1016 if (rc < 0)
1017 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1018 }
Alan Stern78d9a482008-06-23 16:00:40 -04001019}
1020
Alan Stern9ff78432008-08-07 13:04:51 -04001021#ifdef CONFIG_PM
1022
Oliver Neukum14931382012-01-05 15:39:57 +01001023/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1024 * There is no check for reset_resume here because it can be determined
1025 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001026 *
1027 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001028 */
Oliver Neukum14931382012-01-05 15:39:57 +01001029static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001030{
1031 struct usb_host_config *config;
1032 int i;
1033 struct usb_interface *intf;
1034 struct usb_driver *drv;
1035
1036 config = udev->actconfig;
1037 if (config) {
1038 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1039 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001040
1041 if (intf->dev.driver) {
1042 drv = to_usb_driver(intf->dev.driver);
1043 if (!drv->suspend || !drv->resume)
1044 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001045 }
1046 }
1047 }
1048}
1049
Oliver Neukum14931382012-01-05 15:39:57 +01001050/* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1051 * These interfaces have the needs_binding flag set by usb_resume_interface().
1052 *
1053 * The caller must hold @udev's device lock.
1054 */
1055static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
1056{
1057 struct usb_host_config *config;
1058 int i;
1059 struct usb_interface *intf;
1060
1061 config = udev->actconfig;
1062 if (config) {
1063 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1064 intf = config->interface[i];
1065 if (intf->dev.driver && intf->needs_binding)
1066 usb_forced_unbind_intf(intf);
1067 }
1068 }
1069}
1070
1071static void do_rebind_interfaces(struct usb_device *udev)
1072{
1073 struct usb_host_config *config;
1074 int i;
1075 struct usb_interface *intf;
1076
1077 config = udev->actconfig;
1078 if (config) {
1079 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1080 intf = config->interface[i];
1081 if (intf->needs_binding)
1082 usb_rebind_intf(intf);
1083 }
1084 }
1085}
1086
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001087static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001088{
Alan Stern782da722006-07-01 22:09:35 -04001089 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001090 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001091
Alan Stern114b3682006-07-01 22:13:04 -04001092 if (udev->state == USB_STATE_NOTATTACHED ||
1093 udev->state == USB_STATE_SUSPENDED)
1094 goto done;
1095
Alan Sternb6f64362007-05-04 11:51:54 -04001096 /* For devices that don't have a driver, we do a generic suspend. */
1097 if (udev->dev.driver)
1098 udriver = to_usb_device_driver(udev->dev.driver);
1099 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001100 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001101 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001102 }
Alan Stern2bf40862006-07-01 22:12:19 -04001103 status = udriver->suspend(udev, msg);
1104
Alan Stern20dfdad2007-05-22 11:50:17 -04001105 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001106 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001107 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001108}
Alan Stern36e56a32006-07-01 22:08:06 -04001109
Alan Stern65bfd292008-11-25 16:39:18 -05001110static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001111{
1112 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001113 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001114
Alan Stern0458d5b2007-05-04 11:52:20 -04001115 if (udev->state == USB_STATE_NOTATTACHED)
1116 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001117
Alan Stern1c5df7e2006-07-01 22:13:50 -04001118 /* Can't resume it if it doesn't have a driver. */
1119 if (udev->dev.driver == NULL) {
1120 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001121 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001122 }
1123
Alan Stern6d19c002010-02-12 12:21:11 +01001124 /* Non-root devices on a full/low-speed bus must wait for their
1125 * companion high-speed root hub, in case a handoff is needed.
1126 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001127 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001128 device_pm_wait_for_dev(&udev->dev,
1129 &udev->bus->hs_companion->root_hub->dev);
1130
Alan Stern6bc6cff2007-05-04 11:53:03 -04001131 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1132 udev->reset_resume = 1;
1133
Alan Stern1cc8a252006-07-01 22:10:15 -04001134 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001135 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001136
Alan Stern20dfdad2007-05-22 11:50:17 -04001137 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001138 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001139 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001140}
1141
Alan Stern65605ae2008-08-12 14:33:27 -04001142static int usb_suspend_interface(struct usb_device *udev,
1143 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001144{
1145 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001146 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001147
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001148 if (udev->state == USB_STATE_NOTATTACHED ||
1149 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001150 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001151 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001152
Oliver Neukume78832c2012-01-02 15:11:48 +01001153 /* at this time we know the driver supports suspend */
1154 status = driver->suspend(intf, msg);
1155 if (status && !PMSG_IS_AUTO(msg))
1156 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001157
Alan Stern20dfdad2007-05-22 11:50:17 -04001158 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001159 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001160 return status;
1161}
1162
Alan Stern65605ae2008-08-12 14:33:27 -04001163static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001164 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001165{
Alan Stern1cc8a252006-07-01 22:10:15 -04001166 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001167 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001168
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001169 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001170 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001171
Alan Stern645daaa2006-08-30 15:47:02 -04001172 /* Don't let autoresume interfere with unbinding */
1173 if (intf->condition == USB_INTERFACE_UNBINDING)
1174 goto done;
1175
Alan Stern1c5df7e2006-07-01 22:13:50 -04001176 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001177 if (intf->condition == USB_INTERFACE_UNBOUND) {
1178
1179 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001180 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001181 usb_set_interface(udev, intf->altsetting[0].
1182 desc.bInterfaceNumber, 0);
1183 intf->needs_altsetting0 = 0;
1184 }
Alan Stern2bf40862006-07-01 22:12:19 -04001185 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001186 }
Alan Stern78d9a482008-06-23 16:00:40 -04001187
1188 /* Don't resume if the interface is marked for rebinding */
1189 if (intf->needs_binding)
1190 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001191 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001192
Alan Sternf07600c2007-05-30 15:38:16 -04001193 if (reset_resume) {
1194 if (driver->reset_resume) {
1195 status = driver->reset_resume(intf);
1196 if (status)
1197 dev_err(&intf->dev, "%s error %d\n",
1198 "reset_resume", status);
1199 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001200 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001201 dev_warn(&intf->dev, "no %s for driver %s?\n",
1202 "reset_resume", driver->name);
1203 }
1204 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001205 status = driver->resume(intf);
1206 if (status)
1207 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001208 }
Alan Stern2bf40862006-07-01 22:12:19 -04001209
1210done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001211 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001212
Alan Stern78d9a482008-06-23 16:00:40 -04001213 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001214 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001215}
1216
Alan Stern645daaa2006-08-30 15:47:02 -04001217/**
1218 * usb_suspend_both - suspend a USB device and its interfaces
1219 * @udev: the usb_device to suspend
1220 * @msg: Power Management message describing this state transition
1221 *
1222 * This is the central routine for suspending USB devices. It calls the
1223 * suspend methods for all the interface drivers in @udev and then calls
1224 * the suspend method for @udev itself. If an error occurs at any stage,
1225 * all the interfaces which were suspended are resumed so that they remain
1226 * in the same state as the device.
1227 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001228 * Autosuspend requests originating from a child device or an interface
1229 * driver may be made without the protection of @udev's device lock, but
1230 * all other suspend calls will hold the lock. Usbcore will insure that
1231 * method calls do not arrive during bind, unbind, or reset operations.
1232 * However drivers must be prepared to handle suspend calls arriving at
1233 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001234 *
1235 * This routine can run only in process context.
1236 */
Alan Stern718efa62007-03-09 15:41:13 -05001237static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001238{
1239 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001240 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001241 struct usb_interface *intf;
1242
Alan Stern19410442007-03-27 13:33:59 -04001243 if (udev->state == USB_STATE_NOTATTACHED ||
1244 udev->state == USB_STATE_SUSPENDED)
1245 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001246
Alan Stern645daaa2006-08-30 15:47:02 -04001247 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001248 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001249 n = udev->actconfig->desc.bNumInterfaces;
1250 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001251 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001252 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001253
1254 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001255 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001256 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001257 if (status != 0)
1258 break;
1259 }
1260 }
Alan Stern0af212b2011-06-15 16:27:43 -04001261 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001262 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001263
Alan Sterncd4376e2012-03-28 15:56:17 -04001264 /*
1265 * Ignore errors from non-root-hub devices during
1266 * system sleep transitions. For the most part,
1267 * these devices should go to low power anyway when
1268 * the entire bus is suspended.
1269 */
1270 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001271 status = 0;
1272 }
1273
Alan Sterna8e7c562006-07-01 22:11:02 -04001274 /* If the suspend failed, resume interfaces that did get suspended */
1275 if (status != 0) {
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001276 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
Alan Stern571dc792010-04-09 16:03:43 -04001277 while (++i < n) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001278 intf = udev->actconfig->interface[i];
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001279 usb_resume_interface(udev, intf, msg, 0);
Alan Sterna8e7c562006-07-01 22:11:02 -04001280 }
Alan Stern645daaa2006-08-30 15:47:02 -04001281
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001282 /* If the suspend succeeded then prevent any more URB submissions
1283 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001284 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001285 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001286 udev->can_submit = 0;
1287 for (i = 0; i < 16; ++i) {
1288 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1289 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1290 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001291 }
Alan Stern645daaa2006-08-30 15:47:02 -04001292
Alan Stern19410442007-03-27 13:33:59 -04001293 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001294 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001295 return status;
1296}
1297
Alan Stern645daaa2006-08-30 15:47:02 -04001298/**
1299 * usb_resume_both - resume a USB device and its interfaces
1300 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001301 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001302 *
1303 * This is the central routine for resuming USB devices. It calls the
1304 * the resume method for @udev and then calls the resume methods for all
1305 * the interface drivers in @udev.
1306 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001307 * Autoresume requests originating from a child device or an interface
1308 * driver may be made without the protection of @udev's device lock, but
1309 * all other resume calls will hold the lock. Usbcore will insure that
1310 * method calls do not arrive during bind, unbind, or reset operations.
1311 * However drivers must be prepared to handle resume calls arriving at
1312 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001313 *
1314 * This routine can run only in process context.
1315 */
Alan Stern65bfd292008-11-25 16:39:18 -05001316static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001317{
Alan Stern645daaa2006-08-30 15:47:02 -04001318 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001319 int i;
1320 struct usb_interface *intf;
1321
Alan Stern19410442007-03-27 13:33:59 -04001322 if (udev->state == USB_STATE_NOTATTACHED) {
1323 status = -ENODEV;
1324 goto done;
1325 }
Alan Stern6840d252007-09-10 11:34:26 -04001326 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001327
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001328 /* Resume the device */
1329 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001330 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001331
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001332 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001333 if (status == 0 && udev->actconfig) {
1334 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1335 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001336 usb_resume_interface(udev, intf, msg,
1337 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001338 }
1339 }
Alan Sternc08512c2010-11-15 15:57:58 -05001340 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001341
Alan Stern19410442007-03-27 13:33:59 -04001342 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001343 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001344 if (!status)
1345 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001346 return status;
1347}
1348
Alan Stern5f677f12010-04-02 13:20:11 -04001349static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1350{
Alan Stern48826622010-06-22 16:14:48 -04001351 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001352
1353 /* Remote wakeup is needed only when we actually go to sleep.
1354 * For things like FREEZE and QUIESCE, if the device is already
1355 * autosuspended then its current wakeup setting is okay.
1356 */
1357 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1358 if (udev->state != USB_STATE_SUSPENDED)
1359 udev->do_remote_wakeup = 0;
1360 return;
1361 }
1362
Alan Stern48826622010-06-22 16:14:48 -04001363 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001364 * actually want it.
1365 */
Alan Stern48826622010-06-22 16:14:48 -04001366 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001367
1368 /* If the device is autosuspended with the wrong wakeup setting,
1369 * autoresume now so the setting can be changed.
1370 */
1371 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1372 pm_runtime_resume(&udev->dev);
1373 udev->do_remote_wakeup = w;
1374}
1375
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001376/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001377int usb_suspend(struct device *dev, pm_message_t msg)
1378{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001379 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001380
Oliver Neukum14931382012-01-05 15:39:57 +01001381 unbind_no_pm_drivers_interfaces(udev);
1382
1383 /* From now on we are sure all drivers support suspend/resume
1384 * but not necessarily reset_resume()
1385 * so we may still need to unbind and rebind upon resume
1386 */
Alan Stern5f677f12010-04-02 13:20:11 -04001387 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001388 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001389}
1390
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001391/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001392int usb_resume_complete(struct device *dev)
1393{
1394 struct usb_device *udev = to_usb_device(dev);
1395
1396 /* For PM complete calls, all we do is rebind interfaces
1397 * whose needs_binding flag is set
1398 */
1399 if (udev->state != USB_STATE_NOTATTACHED)
1400 do_rebind_interfaces(udev);
1401 return 0;
1402}
1403
1404/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001405int usb_resume(struct device *dev, pm_message_t msg)
1406{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001407 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001408 int status;
1409
Oliver Neukum98d9a822012-01-11 08:38:35 +01001410 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001411 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001412 * Unbind the interfaces that will need rebinding later,
1413 * because they fail to support reset_resume.
1414 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001415 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001416 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001417 status = usb_resume_both(udev, msg);
1418 if (status == 0) {
1419 pm_runtime_disable(dev);
1420 pm_runtime_set_active(dev);
1421 pm_runtime_enable(dev);
1422 unbind_no_reset_resume_drivers_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001423 }
Alan Stern0c590e22010-01-08 12:57:14 -05001424
1425 /* Avoid PM error messages for devices disconnected while suspended
1426 * as we'll display regular disconnect messages just a bit later.
1427 */
Peter Chen7491f132010-09-27 16:43:25 +08001428 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001429 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001430 return status;
1431}
1432
1433#endif /* CONFIG_PM */
1434
Alan Stern645daaa2006-08-30 15:47:02 -04001435#ifdef CONFIG_USB_SUSPEND
1436
Alan Stern088f7fe2010-01-08 12:56:54 -05001437/**
1438 * usb_enable_autosuspend - allow a USB device to be autosuspended
1439 * @udev: the USB device which may be autosuspended
1440 *
1441 * This routine allows @udev to be autosuspended. An autosuspend won't
1442 * take place until the autosuspend_delay has elapsed and all the other
1443 * necessary conditions are satisfied.
1444 *
1445 * The caller must hold @udev's device lock.
1446 */
Alan Stern9e18c822010-04-02 13:22:09 -04001447void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001448{
Alan Stern9e18c822010-04-02 13:22:09 -04001449 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001450}
1451EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1452
1453/**
1454 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1455 * @udev: the USB device which may not be autosuspended
1456 *
1457 * This routine prevents @udev from being autosuspended and wakes it up
1458 * if it is already autosuspended.
1459 *
1460 * The caller must hold @udev's device lock.
1461 */
Alan Stern9e18c822010-04-02 13:22:09 -04001462void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001463{
Alan Stern9e18c822010-04-02 13:22:09 -04001464 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001465}
1466EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1467
Alan Stern645daaa2006-08-30 15:47:02 -04001468/**
1469 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001470 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001471 *
1472 * This routine should be called when a core subsystem is finished using
1473 * @udev and wants to allow it to autosuspend. Examples would be when
1474 * @udev's device file in usbfs is closed or after a configuration change.
1475 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001476 * @udev's usage counter is decremented; if it drops to 0 and all the
1477 * interfaces are inactive then a delayed autosuspend will be attempted.
1478 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001479 *
Alan Stern62e299e2010-01-08 12:56:19 -05001480 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001481 *
1482 * This routine can run only in process context.
1483 */
Alan Stern94fcda12006-11-20 11:38:46 -05001484void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001485{
Alan Stern94fcda12006-11-20 11:38:46 -05001486 int status;
1487
Ming Lei6ddf27c2010-11-15 15:57:30 -05001488 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001489 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001490 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1491 __func__, atomic_read(&udev->dev.power.usage_count),
1492 status);
Alan Stern19c26232007-02-20 15:03:32 -05001493}
1494
1495/**
Alan Stern645daaa2006-08-30 15:47:02 -04001496 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001497 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001498 *
1499 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001500 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001501 * occur until usb_autosuspend_device() is called. (Note that this will
1502 * not prevent suspend events originating in the PM core.) Examples would
1503 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001504 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001505 *
Alan Stern94fcda12006-11-20 11:38:46 -05001506 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1507 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001508 *
Alan Stern62e299e2010-01-08 12:56:19 -05001509 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001510 *
1511 * This routine can run only in process context.
1512 */
Alan Stern94fcda12006-11-20 11:38:46 -05001513int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001514{
1515 int status;
1516
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001517 status = pm_runtime_get_sync(&udev->dev);
1518 if (status < 0)
1519 pm_runtime_put_sync(&udev->dev);
1520 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1521 __func__, atomic_read(&udev->dev.power.usage_count),
1522 status);
1523 if (status > 0)
1524 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001525 return status;
1526}
1527
Alan Stern645daaa2006-08-30 15:47:02 -04001528/**
1529 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001530 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001531 *
1532 * This routine should be called by an interface driver when it is
1533 * finished using @intf and wants to allow it to autosuspend. A typical
1534 * example would be a character-device driver when its device file is
1535 * closed.
1536 *
1537 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001538 * 0, a delayed autosuspend request for @intf's device is attempted. The
1539 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001540 *
Alan Stern645daaa2006-08-30 15:47:02 -04001541 * This routine can run only in process context.
1542 */
1543void usb_autopm_put_interface(struct usb_interface *intf)
1544{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001545 struct usb_device *udev = interface_to_usbdev(intf);
1546 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001547
Ming Lei6ddf27c2010-11-15 15:57:30 -05001548 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001549 atomic_dec(&intf->pm_usage_cnt);
1550 status = pm_runtime_put_sync(&intf->dev);
1551 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1552 __func__, atomic_read(&intf->dev.power.usage_count),
1553 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001554}
1555EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1556
1557/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001558 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1559 * @intf: the usb_interface whose counter should be decremented
1560 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001561 * This routine does much the same thing as usb_autopm_put_interface():
1562 * It decrements @intf's usage counter and schedules a delayed
1563 * autosuspend request if the counter is <= 0. The difference is that it
1564 * does not perform any synchronization; callers should hold a private
1565 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001566 *
1567 * Typically a driver would call this routine during an URB's completion
1568 * handler, if no more URBs were pending.
1569 *
1570 * This routine can run in atomic context.
1571 */
1572void usb_autopm_put_interface_async(struct usb_interface *intf)
1573{
1574 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001575 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001576
Ming Lei6ddf27c2010-11-15 15:57:30 -05001577 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001578 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001579 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001580 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1581 __func__, atomic_read(&intf->dev.power.usage_count),
1582 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001583}
1584EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1585
1586/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001587 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1588 * @intf: the usb_interface whose counter should be decremented
1589 *
1590 * This routine decrements @intf's usage counter but does not carry out an
1591 * autosuspend.
1592 *
1593 * This routine can run in atomic context.
1594 */
1595void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1596{
1597 struct usb_device *udev = interface_to_usbdev(intf);
1598
Ming Lei6ddf27c2010-11-15 15:57:30 -05001599 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001600 atomic_dec(&intf->pm_usage_cnt);
1601 pm_runtime_put_noidle(&intf->dev);
1602}
1603EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1604
1605/**
Alan Stern645daaa2006-08-30 15:47:02 -04001606 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001607 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001608 *
1609 * This routine should be called by an interface driver when it wants to
1610 * use @intf and needs to guarantee that it is not suspended. In addition,
1611 * the routine prevents @intf from being autosuspended subsequently. (Note
1612 * that this will not prevent suspend events originating in the PM core.)
1613 * This prevention will persist until usb_autopm_put_interface() is called
1614 * or @intf is unbound. A typical example would be a character-device
1615 * driver when its device file is opened.
1616 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001617 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1618 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001619 *
1620 * This routine can run only in process context.
1621 */
1622int usb_autopm_get_interface(struct usb_interface *intf)
1623{
Alan Sternaf4f7602006-10-30 17:06:45 -05001624 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001625
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001626 status = pm_runtime_get_sync(&intf->dev);
1627 if (status < 0)
1628 pm_runtime_put_sync(&intf->dev);
1629 else
1630 atomic_inc(&intf->pm_usage_cnt);
1631 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1632 __func__, atomic_read(&intf->dev.power.usage_count),
1633 status);
1634 if (status > 0)
1635 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001636 return status;
1637}
1638EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1639
Alan Stern692a1862006-10-30 17:07:51 -05001640/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001641 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1642 * @intf: the usb_interface whose counter should be incremented
1643 *
1644 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001645 * usb_autopm_get_interface(): It increments @intf's usage counter and
1646 * queues an autoresume request if the device is suspended. The
1647 * differences are that it does not perform any synchronization (callers
1648 * should hold a private lock and handle all synchronization issues
1649 * themselves), and it does not autoresume the device directly (it only
1650 * queues a request). After a successful call, the device may not yet be
1651 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001652 *
1653 * This routine can run in atomic context.
1654 */
1655int usb_autopm_get_interface_async(struct usb_interface *intf)
1656{
Ming Lei63defa72010-11-15 15:56:54 -05001657 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001658
Ming Lei63defa72010-11-15 15:56:54 -05001659 status = pm_runtime_get(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001660 if (status < 0 && status != -EINPROGRESS)
1661 pm_runtime_put_noidle(&intf->dev);
1662 else
Alan Sternccf5b802009-06-29 11:00:01 -04001663 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001664 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1665 __func__, atomic_read(&intf->dev.power.usage_count),
1666 status);
Jim Wylderc5a48592011-09-06 21:07:20 -05001667 if (status > 0 || status == -EINPROGRESS)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001668 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001669 return status;
1670}
1671EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1672
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001673/**
1674 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1675 * @intf: the usb_interface whose counter should be incremented
1676 *
1677 * This routine increments @intf's usage counter but does not carry out an
1678 * autoresume.
1679 *
1680 * This routine can run in atomic context.
1681 */
1682void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1683{
1684 struct usb_device *udev = interface_to_usbdev(intf);
1685
Ming Lei6ddf27c2010-11-15 15:57:30 -05001686 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001687 atomic_inc(&intf->pm_usage_cnt);
1688 pm_runtime_get_noresume(&intf->dev);
1689}
1690EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1691
1692/* Internal routine to check whether we may autosuspend a device. */
1693static int autosuspend_check(struct usb_device *udev)
1694{
Alan Stern7560d32e2010-04-02 13:18:50 -04001695 int w, i;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001696 struct usb_interface *intf;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001697
1698 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1699 * any interface drivers require remote wakeup but it isn't available.
1700 */
Alan Stern7560d32e2010-04-02 13:18:50 -04001701 w = 0;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001702 if (udev->actconfig) {
1703 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1704 intf = udev->actconfig->interface[i];
1705
1706 /* We don't need to check interfaces that are
1707 * disabled for runtime PM. Either they are unbound
1708 * or else their drivers don't support autosuspend
1709 * and so they are permanently active.
1710 */
1711 if (intf->dev.power.disable_depth)
1712 continue;
1713 if (atomic_read(&intf->dev.power.usage_count) > 0)
1714 return -EBUSY;
Alan Stern7560d32e2010-04-02 13:18:50 -04001715 w |= intf->needs_remote_wakeup;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001716
1717 /* Don't allow autosuspend if the device will need
1718 * a reset-resume and any of its interface drivers
1719 * doesn't include support or needs remote wakeup.
1720 */
1721 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1722 struct usb_driver *driver;
1723
1724 driver = to_usb_driver(intf->dev.driver);
1725 if (!driver->reset_resume ||
1726 intf->needs_remote_wakeup)
1727 return -EOPNOTSUPP;
1728 }
1729 }
1730 }
Alan Stern7560d32e2010-04-02 13:18:50 -04001731 if (w && !device_can_wakeup(&udev->dev)) {
1732 dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n");
1733 return -EOPNOTSUPP;
1734 }
1735 udev->do_remote_wakeup = w;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001736 return 0;
1737}
1738
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001739int usb_runtime_suspend(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001740{
Ming Lei63defa72010-11-15 15:56:54 -05001741 struct usb_device *udev = to_usb_device(dev);
1742 int status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001743
1744 /* A USB device can be suspended if it passes the various autosuspend
1745 * checks. Runtime suspend for a USB device means suspending all the
1746 * interfaces and then the device itself.
1747 */
Ming Lei63defa72010-11-15 15:56:54 -05001748 if (autosuspend_check(udev) != 0)
1749 return -EAGAIN;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001750
Ming Lei63defa72010-11-15 15:56:54 -05001751 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
Alan Sternb2c0a862011-11-04 00:52:46 +01001752
1753 /* Allow a retry if autosuspend failed temporarily */
1754 if (status == -EAGAIN || status == -EBUSY)
1755 usb_mark_last_busy(udev);
1756
Sarah Sharpdb7c7c02010-12-29 22:03:07 -08001757 /* The PM core reacts badly unless the return code is 0,
1758 * -EAGAIN, or -EBUSY, so always return -EBUSY on an error.
1759 */
1760 if (status != 0)
1761 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001762 return status;
1763}
1764
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001765int usb_runtime_resume(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001766{
Ming Lei63defa72010-11-15 15:56:54 -05001767 struct usb_device *udev = to_usb_device(dev);
1768 int status;
1769
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001770 /* Runtime resume for a USB device means resuming both the device
1771 * and all its interfaces.
1772 */
Ming Lei63defa72010-11-15 15:56:54 -05001773 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
Ming Lei63defa72010-11-15 15:56:54 -05001774 return status;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001775}
1776
Rafael J. Wysockie1620d52011-03-18 19:55:36 +01001777int usb_runtime_idle(struct device *dev)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001778{
Ming Lei63defa72010-11-15 15:56:54 -05001779 struct usb_device *udev = to_usb_device(dev);
1780
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001781 /* An idle USB device can be suspended if it passes the various
Ming Lei63defa72010-11-15 15:56:54 -05001782 * autosuspend checks.
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001783 */
Ming Lei63defa72010-11-15 15:56:54 -05001784 if (autosuspend_check(udev) == 0)
Alan Sternfcc4a012010-11-15 15:57:51 -05001785 pm_runtime_autosuspend(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001786 return 0;
1787}
1788
Andiry Xu65580b432011-09-23 14:19:52 -07001789int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1790{
1791 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1792 int ret = -EPERM;
1793
1794 if (hcd->driver->set_usb2_hw_lpm) {
1795 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1796 if (!ret)
1797 udev->usb2_hw_lpm_enabled = enable;
1798 }
1799
1800 return ret;
1801}
1802
Alan Stern645daaa2006-08-30 15:47:02 -04001803#endif /* CONFIG_USB_SUSPEND */
1804
Alan Stern36e56a32006-07-01 22:08:06 -04001805struct bus_type usb_bus_type = {
1806 .name = "usb",
1807 .match = usb_device_match,
1808 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001809};