blob: 124bcc50b0b7cba21f2429cd414729f9dcec08f6 [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
97static ssize_t show_dynids(struct device_driver *driver, char *buf)
98{
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-Hartman93bacef2006-12-17 21:50:23 +0100104static ssize_t store_new_id(struct device_driver *driver,
105 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}
Bjørn Morke6bbcef2012-05-13 12:34:59 +0200111static DRIVER_ATTR(new_id, S_IRUGO | S_IWUSR, show_dynids, store_new_id);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800112
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800113/**
114 * store_remove_id - remove a USB device ID from this driver
115 * @driver: target device driver
116 * @buf: buffer for scanning device ID data
117 * @count: input size
118 *
119 * Removes a dynamic usb device ID from this driver.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200120 *
121 * Return: @count on success. A negative error code otherwise.
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800122 */
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}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800197
198static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
199 struct usb_driver *drv)
200{
201 struct usb_dynid *dynid;
202
203 spin_lock(&drv->dynids.lock);
204 list_for_each_entry(dynid, &drv->dynids.list, node) {
205 if (usb_match_one_id(intf, &dynid->id)) {
206 spin_unlock(&drv->dynids.lock);
207 return &dynid->id;
208 }
209 }
210 spin_unlock(&drv->dynids.lock);
211 return NULL;
212}
213
214
Alan Stern8bb54ab2006-07-01 22:08:49 -0400215/* called from driver core with dev locked */
216static int usb_probe_device(struct device *dev)
217{
218 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200219 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500220 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400221
Harvey Harrison441b62c2008-03-03 16:08:34 -0800222 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400223
Alan Stern8bb54ab2006-07-01 22:08:49 -0400224 /* TODO: Add real matching code */
225
Alan Stern645daaa2006-08-30 15:47:02 -0400226 /* The device should always appear to be in use
Masanari Iida02582e92012-08-22 19:11:26 +0900227 * unless the driver supports autosuspend.
Alan Stern645daaa2006-08-30 15:47:02 -0400228 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500229 if (!udriver->supports_autosuspend)
230 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400231
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500232 if (!error)
233 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400234 return error;
235}
236
237/* called from driver core with dev locked */
238static int usb_unbind_device(struct device *dev)
239{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500240 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400241 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
242
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500243 udriver->disconnect(udev);
244 if (!udriver->supports_autosuspend)
245 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400246 return 0;
247}
248
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800249/*
250 * Cancel any pending scheduled resets
251 *
252 * [see usb_queue_reset_device()]
253 *
254 * Called after unconfiguring / when releasing interfaces. See
255 * comments in __usb_queue_reset_device() regarding
256 * udev->reset_running.
257 */
258static void usb_cancel_queued_reset(struct usb_interface *iface)
259{
260 if (iface->reset_running == 0)
261 cancel_work_sync(&iface->reset_ws);
262}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400263
264/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800265static int usb_probe_interface(struct device *dev)
266{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400267 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200268 struct usb_interface *intf = to_usb_interface(dev);
269 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800270 const struct usb_device_id *id;
271 int error = -ENODEV;
Sarah Sharp83060952012-05-02 14:25:52 -0700272 int lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800273
Harvey Harrison441b62c2008-03-03 16:08:34 -0800274 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800275
Alan Stern78d9a482008-06-23 16:00:40 -0400276 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800277
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400278 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500279 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400280
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800281 if (udev->authorized == 0) {
282 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500283 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800284 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700285
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800286 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800287 if (!id)
288 id = usb_match_dynamic_id(intf, driver);
Alan Stern0f3dda92010-01-08 12:56:04 -0500289 if (!id)
290 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800291
Alan Stern0f3dda92010-01-08 12:56:04 -0500292 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400293
Alan Stern0f3dda92010-01-08 12:56:04 -0500294 error = usb_autoresume_device(udev);
295 if (error)
296 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400297
Alan Stern0f3dda92010-01-08 12:56:04 -0500298 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400299
Alan Stern571dc792010-04-09 16:03:43 -0400300 /* Probed interfaces are initially active. They are
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500301 * runtime-PM-enabled only if the driver has autosuspend support.
302 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500303 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500304 pm_runtime_set_active(dev);
305 pm_suspend_ignore_children(dev, false);
306 if (driver->supports_autosuspend)
307 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200308
Sarah Sharp83060952012-05-02 14:25:52 -0700309 /* If the new driver doesn't allow hub-initiated LPM, and we can't
310 * disable hub-initiated LPM, then fail the probe.
311 *
312 * Otherwise, leaving LPM enabled should be harmless, because the
313 * endpoint intervals should remain the same, and the U1/U2 timeouts
314 * should remain the same.
315 *
316 * If we need to install alt setting 0 before probe, or another alt
317 * setting during probe, that should also be fine. usb_set_interface()
318 * will attempt to disable LPM, and fail if it can't disable it.
319 */
320 lpm_disable_error = usb_unlocked_disable_lpm(udev);
321 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
322 dev_err(&intf->dev, "%s Failed to disable LPM for driver %s\n.",
323 __func__, driver->name);
324 error = lpm_disable_error;
325 goto err;
326 }
327
Alan Stern0f3dda92010-01-08 12:56:04 -0500328 /* Carry out a deferred switch to altsetting 0 */
329 if (intf->needs_altsetting0) {
330 error = usb_set_interface(udev, intf->altsetting[0].
331 desc.bInterfaceNumber, 0);
332 if (error < 0)
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200333 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500334 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800335 }
336
Alan Stern0f3dda92010-01-08 12:56:04 -0500337 error = driver->probe(intf, id);
338 if (error)
339 goto err;
340
341 intf->condition = USB_INTERFACE_BOUND;
Sarah Sharp83060952012-05-02 14:25:52 -0700342
343 /* If the LPM disable succeeded, balance the ref counts. */
344 if (!lpm_disable_error)
345 usb_unlocked_enable_lpm(udev);
346
Alan Stern0f3dda92010-01-08 12:56:04 -0500347 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800348 return error;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200349
Alan Stern0f3dda92010-01-08 12:56:04 -0500350 err:
Hans de Goedee714fad2012-05-22 11:36:59 +0200351 usb_set_intfdata(intf, NULL);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200352 intf->needs_remote_wakeup = 0;
353 intf->condition = USB_INTERFACE_UNBOUND;
354 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500355
Sarah Sharpd01f87c2012-10-04 09:53:43 -0700356 /* If the LPM disable succeeded, balance the ref counts. */
357 if (!lpm_disable_error)
358 usb_unlocked_enable_lpm(udev);
359
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500360 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400361 if (driver->supports_autosuspend)
362 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500363 pm_runtime_set_suspended(dev);
364
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200365 usb_autosuspend_device(udev);
366 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800367}
368
Alan Stern8bb54ab2006-07-01 22:08:49 -0400369/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800370static int usb_unbind_interface(struct device *dev)
371{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400372 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800373 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400374 struct usb_device *udev;
Sarah Sharp83060952012-05-02 14:25:52 -0700375 int error, r, lpm_disable_error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800376
377 intf->condition = USB_INTERFACE_UNBINDING;
378
Alan Stern645daaa2006-08-30 15:47:02 -0400379 /* Autoresume for set_interface call below */
380 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500381 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400382
Sarah Sharp83060952012-05-02 14:25:52 -0700383 /* Hub-initiated LPM policy may change, so attempt to disable LPM until
384 * the driver is unbound. If LPM isn't disabled, that's fine because it
385 * wouldn't be enabled unless all the bound interfaces supported
386 * hub-initiated LPM.
387 */
388 lpm_disable_error = usb_unlocked_disable_lpm(udev);
389
Alan Stern9da82bd2008-05-08 11:54:37 -0400390 /* Terminate all URBs for this interface unless the driver
391 * supports "soft" unbinding.
392 */
393 if (!driver->soft_unbind)
Alan Sternddeac4e2009-01-15 17:03:33 -0500394 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800395
Alan Stern8bb54ab2006-07-01 22:08:49 -0400396 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800397 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800398
Alan Stern55151d72008-08-12 14:33:59 -0400399 /* Reset other interface state.
400 * We cannot do a Set-Interface if the device is suspended or
401 * if it is prepared for a system sleep (since installing a new
402 * altsetting means creating new endpoint device entries).
403 * When either of these happens, defer the Set-Interface.
404 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500405 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
406 /* Already in altsetting 0 so skip Set-Interface.
407 * Just re-enable it without affecting the endpoint toggles.
408 */
409 usb_enable_interface(udev, intf, false);
Alan Sternf76b168b2011-06-18 20:22:23 +0200410 } else if (!error && !intf->dev.power.is_prepared) {
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200411 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400412 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200413 if (r < 0)
414 intf->needs_altsetting0 = 1;
415 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400416 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e32009-08-27 16:46:56 +0200417 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800418 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400419
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800420 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400421 intf->needs_remote_wakeup = 0;
422
Sarah Sharp83060952012-05-02 14:25:52 -0700423 /* Attempt to re-enable USB3 LPM, if the disable succeeded. */
424 if (!lpm_disable_error)
425 usb_unlocked_enable_lpm(udev);
426
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500427 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
Alan Stern89842ae2010-05-11 11:44:06 -0400428 if (driver->supports_autosuspend)
429 pm_runtime_disable(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500430 pm_runtime_set_suspended(dev);
431
432 /* Undo any residual pm_autopm_get_interface_* calls */
433 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
434 usb_autopm_put_interface_no_suspend(intf);
435 atomic_set(&intf->pm_usage_cnt, 0);
436
Alan Stern645daaa2006-08-30 15:47:02 -0400437 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500438 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800439
440 return 0;
441}
442
Alan Stern36e56a32006-07-01 22:08:06 -0400443/**
444 * usb_driver_claim_interface - bind a driver to an interface
445 * @driver: the driver to be bound
446 * @iface: the interface to which it will be bound; must be in the
447 * usb device's active configuration
448 * @priv: driver data associated with that interface
449 *
450 * This is used by usb device drivers that need to claim more than one
451 * interface on a device when probing (audio and acm are current examples).
452 * No device driver should directly modify internal usb_interface or
453 * usb_device structure members.
454 *
455 * Few drivers should need to use this routine, since the most natural
456 * way to bind to an interface is to return the private data from
457 * the driver's probe() method.
458 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400459 * Callers must own the device lock, so driver probe() entries don't need
460 * extra locking, but other call contexts may need to explicitly claim that
461 * lock.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200462 *
463 * Return: 0 on success.
Alan Stern36e56a32006-07-01 22:08:06 -0400464 */
465int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800466 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400467{
468 struct device *dev = &iface->dev;
Sarah Sharp83060952012-05-02 14:25:52 -0700469 struct usb_device *udev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700470 int retval = 0;
Sarah Sharp83060952012-05-02 14:25:52 -0700471 int lpm_disable_error;
Alan Stern36e56a32006-07-01 22:08:06 -0400472
473 if (dev->driver)
474 return -EBUSY;
475
Sarah Sharp83060952012-05-02 14:25:52 -0700476 udev = interface_to_usbdev(iface);
477
Alan Stern8bb54ab2006-07-01 22:08:49 -0400478 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400479 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400480 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400481
Alan Stern36e56a32006-07-01 22:08:06 -0400482 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500483
Sarah Sharp83060952012-05-02 14:25:52 -0700484 /* Disable LPM until this driver is bound. */
485 lpm_disable_error = usb_unlocked_disable_lpm(udev);
486 if (lpm_disable_error && driver->disable_hub_initiated_lpm) {
487 dev_err(&iface->dev, "%s Failed to disable LPM for driver %s\n.",
488 __func__, driver->name);
489 return -ENOMEM;
490 }
491
Alan Stern89842ae2010-05-11 11:44:06 -0400492 /* Claimed interfaces are initially inactive (suspended) and
493 * runtime-PM-enabled, but only if the driver has autosuspend
494 * support. Otherwise they are marked active, to prevent the
495 * device from being autosuspended, but left disabled. In either
496 * case they are sensitive to their children's power states.
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500497 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500498 pm_suspend_ignore_children(dev, false);
499 if (driver->supports_autosuspend)
500 pm_runtime_enable(dev);
Alan Stern89842ae2010-05-11 11:44:06 -0400501 else
502 pm_runtime_set_active(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400503
504 /* if interface was already added, bind now; else let
505 * the future device_add() bind it, bypassing probe()
506 */
507 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700508 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400509
Sarah Sharp83060952012-05-02 14:25:52 -0700510 /* Attempt to re-enable USB3 LPM, if the disable was successful. */
511 if (!lpm_disable_error)
512 usb_unlocked_enable_lpm(udev);
513
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700514 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400515}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600516EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400517
518/**
519 * usb_driver_release_interface - unbind a driver from an interface
520 * @driver: the driver to be unbound
521 * @iface: the interface from which it will be unbound
522 *
523 * This can be used by drivers to release an interface without waiting
524 * for their disconnect() methods to be called. In typical cases this
525 * also causes the driver disconnect() method to be called.
526 *
527 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400528 * Callers must own the device lock, so driver disconnect() entries don't
529 * need extra locking, but other call contexts may need to explicitly claim
530 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400531 */
532void usb_driver_release_interface(struct usb_driver *driver,
533 struct usb_interface *iface)
534{
535 struct device *dev = &iface->dev;
536
537 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400538 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400539 return;
540
541 /* don't release from within disconnect() */
542 if (iface->condition != USB_INTERFACE_BOUND)
543 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400544 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400545
Alan Stern91f8d062009-04-16 15:35:09 -0400546 /* Release via the driver core only if the interface
547 * has already been registered
548 */
Alan Stern36e56a32006-07-01 22:08:06 -0400549 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400550 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800551 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800552 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400553 usb_unbind_interface(dev);
554 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800555 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400556 }
Alan Stern36e56a32006-07-01 22:08:06 -0400557}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600558EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400559
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800560/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100561int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800562{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800563 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
564 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
565 return 0;
566
567 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
568 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
569 return 0;
570
571 /* No need to test id->bcdDevice_lo != 0, since 0 is never
572 greater than any unsigned number. */
573 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
574 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
575 return 0;
576
577 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
578 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
579 return 0;
580
581 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
582 (id->bDeviceClass != dev->descriptor.bDeviceClass))
583 return 0;
584
585 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800586 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800587 return 0;
588
589 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
590 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
591 return 0;
592
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100593 return 1;
594}
595
596/* returns 0 if no match, 1 if match */
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200597int usb_match_one_id_intf(struct usb_device *dev,
598 struct usb_host_interface *intf,
599 const struct usb_device_id *id)
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100600{
Bjørn Mork81df2d52012-05-18 21:27:43 +0200601 /* The interface class, subclass, protocol and number should never be
Alan Stern93c8bf42006-10-18 16:41:51 -0400602 * checked for a match if the device class is Vendor Specific,
603 * unless the match record specifies the Vendor ID. */
604 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
605 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
606 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
607 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
Bjørn Mork81df2d52012-05-18 21:27:43 +0200608 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
609 USB_DEVICE_ID_MATCH_INT_NUMBER)))
Alan Stern93c8bf42006-10-18 16:41:51 -0400610 return 0;
611
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800612 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
613 (id->bInterfaceClass != intf->desc.bInterfaceClass))
614 return 0;
615
616 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
617 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
618 return 0;
619
620 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
621 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
622 return 0;
623
Bjørn Mork81df2d52012-05-18 21:27:43 +0200624 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
625 (id->bInterfaceNumber != intf->desc.bInterfaceNumber))
626 return 0;
627
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800628 return 1;
629}
Laurent Pinchart80da2e02012-07-19 12:39:13 +0200630
631/* returns 0 if no match, 1 if match */
632int usb_match_one_id(struct usb_interface *interface,
633 const struct usb_device_id *id)
634{
635 struct usb_host_interface *intf;
636 struct usb_device *dev;
637
638 /* proc_connectinfo in devio.c may call us with id == NULL. */
639 if (id == NULL)
640 return 0;
641
642 intf = interface->cur_altsetting;
643 dev = interface_to_usbdev(interface);
644
645 if (!usb_match_device(dev, id))
646 return 0;
647
648 return usb_match_one_id_intf(dev, intf, id);
649}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100650EXPORT_SYMBOL_GPL(usb_match_one_id);
651
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800652/**
653 * usb_match_id - find first usb_device_id matching device or interface
654 * @interface: the interface of interest
655 * @id: array of usb_device_id structures, terminated by zero entry
656 *
657 * usb_match_id searches an array of usb_device_id's and returns
658 * the first one matching the device or interface, or null.
659 * This is used when binding (or rebinding) a driver to an interface.
660 * Most USB device drivers will use this indirectly, through the usb core,
661 * but some layered driver frameworks use it directly.
662 * These device tables are exported with MODULE_DEVICE_TABLE, through
663 * modutils, to support the driver loading functionality of USB hotplugging.
664 *
Yacine Belkadi626f0902013-08-02 20:10:04 +0200665 * Return: The first matching usb_device_id, or %NULL.
666 *
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800667 * What Matches:
668 *
669 * The "match_flags" element in a usb_device_id controls which
670 * members are used. If the corresponding bit is set, the
671 * value in the device_id must match its corresponding member
672 * in the device or interface descriptor, or else the device_id
673 * does not match.
674 *
675 * "driver_info" is normally used only by device drivers,
676 * but you can create a wildcard "matches anything" usb_device_id
677 * as a driver's "modules.usbmap" entry if you provide an id with
678 * only a nonzero "driver_info" field. If you do this, the USB device
679 * driver's probe() routine should use additional intelligence to
680 * decide whether to bind to the specified interface.
681 *
682 * What Makes Good usb_device_id Tables:
683 *
684 * The match algorithm is very simple, so that intelligence in
685 * driver selection must come from smart driver id records.
686 * Unless you have good reasons to use another selection policy,
687 * provide match elements only in related groups, and order match
688 * specifiers from specific to general. Use the macros provided
689 * for that purpose if you can.
690 *
691 * The most specific match specifiers use device descriptor
692 * data. These are commonly used with product-specific matches;
693 * the USB_DEVICE macro lets you provide vendor and product IDs,
694 * and you can also match against ranges of product revisions.
695 * These are widely used for devices with application or vendor
696 * specific bDeviceClass values.
697 *
698 * Matches based on device class/subclass/protocol specifications
699 * are slightly more general; use the USB_DEVICE_INFO macro, or
700 * its siblings. These are used with single-function devices
701 * where bDeviceClass doesn't specify that each interface has
702 * its own class.
703 *
704 * Matches based on interface class/subclass/protocol are the
705 * most general; they let drivers bind to any interface on a
706 * multiple-function device. Use the USB_INTERFACE_INFO
707 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400708 * devices (as recorded in bInterfaceClass).
709 *
710 * Note that an entry created by USB_INTERFACE_INFO won't match
711 * any interface if the device class is set to Vendor-Specific.
712 * This is deliberate; according to the USB spec the meanings of
713 * the interface class/subclass/protocol for these devices are also
714 * vendor-specific, and hence matching against a standard product
715 * class wouldn't work anyway. If you really want to use an
716 * interface-based match for such a device, create a match record
717 * that also specifies the vendor ID. (Unforunately there isn't a
718 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800719 *
720 * Within those groups, remember that not all combinations are
721 * meaningful. For example, don't give a product version range
722 * without vendor and product IDs; or specify a protocol without
723 * its associated class and subclass.
724 */
725const struct usb_device_id *usb_match_id(struct usb_interface *interface,
726 const struct usb_device_id *id)
727{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800728 /* proc_connectinfo in devio.c may call us with id == NULL. */
729 if (id == NULL)
730 return NULL;
731
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800732 /* It is important to check that id->driver_info is nonzero,
733 since an entry that is all zeroes except for a nonzero
734 id->driver_info is the way to create an entry that
735 indicates that the driver want to examine every
736 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800737 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
738 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800739 if (usb_match_one_id(interface, id))
740 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800741 }
742
743 return NULL;
744}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600745EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800746
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100747static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800748{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400749 /* devices and interfaces are handled separately */
750 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800751
Alan Stern8bb54ab2006-07-01 22:08:49 -0400752 /* interface drivers never match devices */
753 if (!is_usb_device_driver(drv))
754 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800755
Alan Stern8bb54ab2006-07-01 22:08:49 -0400756 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800757 return 1;
758
Kay Sievers55129662009-05-04 19:48:32 +0200759 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400760 struct usb_interface *intf;
761 struct usb_driver *usb_drv;
762 const struct usb_device_id *id;
763
764 /* device drivers never match interfaces */
765 if (is_usb_device_driver(drv))
766 return 0;
767
768 intf = to_usb_interface(dev);
769 usb_drv = to_usb_driver(drv);
770
771 id = usb_match_id(intf, usb_drv->id_table);
772 if (id)
773 return 1;
774
775 id = usb_match_dynamic_id(intf, usb_drv);
776 if (id)
777 return 1;
778 }
779
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800780 return 0;
781}
782
Kay Sievers7eff2e72007-08-14 15:15:12 +0200783static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400784{
Alan Stern36e56a32006-07-01 22:08:06 -0400785 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400786
Kay Sievers55129662009-05-04 19:48:32 +0200787 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400788 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200789 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100790 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200791
Alan Stern8bb54ab2006-07-01 22:08:49 -0400792 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200793 } else {
794 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400795 }
Alan Stern36e56a32006-07-01 22:08:06 -0400796
797 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500798 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200799 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400800 return -ENODEV;
801 }
802 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200803 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400804 return -ENODEV;
805 }
806
Alan Stern36e56a32006-07-01 22:08:06 -0400807 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200808 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400809 le16_to_cpu(usb_dev->descriptor.idVendor),
810 le16_to_cpu(usb_dev->descriptor.idProduct),
811 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
812 return -ENOMEM;
813
814 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200815 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400816 usb_dev->descriptor.bDeviceClass,
817 usb_dev->descriptor.bDeviceSubClass,
818 usb_dev->descriptor.bDeviceProtocol))
819 return -ENOMEM;
820
Alan Stern36e56a32006-07-01 22:08:06 -0400821 return 0;
822}
823
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800824/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400825 * usb_register_device_driver - register a USB device (not interface) driver
826 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800827 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800828 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400829 * Registers a USB device driver with the USB core. The list of
830 * unattached devices will be rescanned whenever a new driver is
831 * added, allowing the new driver to attach to any recognized devices.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200832 *
833 * Return: A negative error code on failure and 0 on success.
Alan Stern8bb54ab2006-07-01 22:08:49 -0400834 */
835int usb_register_device_driver(struct usb_device_driver *new_udriver,
836 struct module *owner)
837{
838 int retval = 0;
839
840 if (usb_disabled())
841 return -ENODEV;
842
843 new_udriver->drvwrap.for_devices = 1;
844 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
845 new_udriver->drvwrap.driver.bus = &usb_bus_type;
846 new_udriver->drvwrap.driver.probe = usb_probe_device;
847 new_udriver->drvwrap.driver.remove = usb_unbind_device;
848 new_udriver->drvwrap.driver.owner = owner;
849
850 retval = driver_register(&new_udriver->drvwrap.driver);
851
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700852 if (!retval)
Alan Stern8bb54ab2006-07-01 22:08:49 -0400853 pr_info("%s: registered new device driver %s\n",
854 usbcore_name, new_udriver->name);
Greg Kroah-Hartmanfb28d582012-04-25 17:15:29 -0700855 else
Alan Stern8bb54ab2006-07-01 22:08:49 -0400856 printk(KERN_ERR "%s: error %d registering device "
857 " driver %s\n",
858 usbcore_name, retval, new_udriver->name);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400859
860 return retval;
861}
862EXPORT_SYMBOL_GPL(usb_register_device_driver);
863
864/**
865 * usb_deregister_device_driver - unregister a USB device (not interface) driver
866 * @udriver: USB operations of the device driver to unregister
867 * Context: must be able to sleep
868 *
869 * Unlinks the specified driver from the internal USB driver list.
870 */
871void usb_deregister_device_driver(struct usb_device_driver *udriver)
872{
873 pr_info("%s: deregistering device driver %s\n",
874 usbcore_name, udriver->name);
875
876 driver_unregister(&udriver->drvwrap.driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400877}
878EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
879
880/**
881 * usb_register_driver - register a USB interface driver
882 * @new_driver: USB operations for the interface driver
883 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800884 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400885 *
886 * Registers a USB interface driver with the USB core. The list of
887 * unattached interfaces will be rescanned whenever a new driver is
888 * added, allowing the new driver to attach to any recognized interfaces.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200889 *
890 * Return: A negative error code on failure and 0 on success.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800891 *
892 * NOTE: if you want your driver to use the USB major number, you must call
893 * usb_register_dev() to enable that functionality. This function no longer
894 * takes care of that.
895 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800896int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
897 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800898{
899 int retval = 0;
900
901 if (usb_disabled())
902 return -ENODEV;
903
Alan Stern8bb54ab2006-07-01 22:08:49 -0400904 new_driver->drvwrap.for_devices = 0;
905 new_driver->drvwrap.driver.name = (char *) new_driver->name;
906 new_driver->drvwrap.driver.bus = &usb_bus_type;
907 new_driver->drvwrap.driver.probe = usb_probe_interface;
908 new_driver->drvwrap.driver.remove = usb_unbind_interface;
909 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800910 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800911 spin_lock_init(&new_driver->dynids.lock);
912 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800913
Alan Stern8bb54ab2006-07-01 22:08:49 -0400914 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800915 if (retval)
916 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800917
Alan Sterned283e92012-01-24 14:35:13 -0500918 retval = usb_create_newid_files(new_driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800919 if (retval)
920 goto out_newid;
921
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800922 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800923 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800924
925out:
926 return retval;
927
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800928out_newid:
929 driver_unregister(&new_driver->drvwrap.driver);
930
931 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400932 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800933 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800934 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800935}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600936EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800937
938/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400939 * usb_deregister - unregister a USB interface driver
940 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800941 * Context: must be able to sleep
942 *
943 * Unlinks the specified driver from the internal USB driver list.
944 *
945 * NOTE: If you called usb_register_dev(), you still need to call
946 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
947 * this * call will no longer do it for you.
948 */
949void usb_deregister(struct usb_driver *driver)
950{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400951 pr_info("%s: deregistering interface driver %s\n",
952 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800953
Alan Sterned283e92012-01-24 14:35:13 -0500954 usb_remove_newid_files(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400955 driver_unregister(&driver->drvwrap.driver);
Alan Sterned283e92012-01-24 14:35:13 -0500956 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800957}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600958EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400959
Alan Stern78d9a482008-06-23 16:00:40 -0400960/* Forced unbinding of a USB interface driver, either because
961 * it doesn't support pre_reset/post_reset/reset_resume or
962 * because it doesn't support suspend/resume.
963 *
964 * The caller must hold @intf's device's lock, but not its pm_mutex
965 * and not @intf->dev.sem.
966 */
967void usb_forced_unbind_intf(struct usb_interface *intf)
968{
969 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
970
971 dev_dbg(&intf->dev, "forced unbind\n");
972 usb_driver_release_interface(driver, intf);
973
974 /* Mark the interface for later rebinding */
975 intf->needs_binding = 1;
976}
977
978/* Delayed forced unbinding of a USB interface driver and scan
979 * for rebinding.
980 *
981 * The caller must hold @intf's device's lock, but not its pm_mutex
982 * and not @intf->dev.sem.
983 *
Alan Stern5096aed2008-08-12 14:34:14 -0400984 * Note: Rebinds will be skipped if a system sleep transition is in
985 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -0400986 */
987void usb_rebind_intf(struct usb_interface *intf)
988{
989 int rc;
990
991 /* Delayed unbind of an existing driver */
Oliver Neukum14931382012-01-05 15:39:57 +0100992 if (intf->dev.driver)
993 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -0400994
995 /* Try to rebind the interface */
Alan Sternf76b168b2011-06-18 20:22:23 +0200996 if (!intf->dev.power.is_prepared) {
Alan Stern5096aed2008-08-12 14:34:14 -0400997 intf->needs_binding = 0;
998 rc = device_attach(&intf->dev);
999 if (rc < 0)
1000 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
1001 }
Alan Stern78d9a482008-06-23 16:00:40 -04001002}
1003
Alan Stern9ff78432008-08-07 13:04:51 -04001004#ifdef CONFIG_PM
1005
Oliver Neukum14931382012-01-05 15:39:57 +01001006/* Unbind drivers for @udev's interfaces that don't support suspend/resume
1007 * There is no check for reset_resume here because it can be determined
1008 * only during resume whether reset_resume is needed.
Alan Stern78d9a482008-06-23 16:00:40 -04001009 *
1010 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -04001011 */
Oliver Neukum14931382012-01-05 15:39:57 +01001012static void unbind_no_pm_drivers_interfaces(struct usb_device *udev)
Alan Stern78d9a482008-06-23 16:00:40 -04001013{
1014 struct usb_host_config *config;
1015 int i;
1016 struct usb_interface *intf;
1017 struct usb_driver *drv;
1018
1019 config = udev->actconfig;
1020 if (config) {
1021 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1022 intf = config->interface[i];
Oliver Neukum14931382012-01-05 15:39:57 +01001023
1024 if (intf->dev.driver) {
1025 drv = to_usb_driver(intf->dev.driver);
1026 if (!drv->suspend || !drv->resume)
1027 usb_forced_unbind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -04001028 }
1029 }
1030 }
1031}
1032
Oliver Neukum14931382012-01-05 15:39:57 +01001033/* Unbind drivers for @udev's interfaces that failed to support reset-resume.
1034 * These interfaces have the needs_binding flag set by usb_resume_interface().
1035 *
1036 * The caller must hold @udev's device lock.
1037 */
1038static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev)
1039{
1040 struct usb_host_config *config;
1041 int i;
1042 struct usb_interface *intf;
1043
1044 config = udev->actconfig;
1045 if (config) {
1046 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1047 intf = config->interface[i];
1048 if (intf->dev.driver && intf->needs_binding)
1049 usb_forced_unbind_intf(intf);
1050 }
1051 }
1052}
1053
1054static void do_rebind_interfaces(struct usb_device *udev)
1055{
1056 struct usb_host_config *config;
1057 int i;
1058 struct usb_interface *intf;
1059
1060 config = udev->actconfig;
1061 if (config) {
1062 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
1063 intf = config->interface[i];
1064 if (intf->needs_binding)
1065 usb_rebind_intf(intf);
1066 }
1067 }
1068}
1069
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001070static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001071{
Alan Stern782da722006-07-01 22:09:35 -04001072 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001073 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001074
Alan Stern114b3682006-07-01 22:13:04 -04001075 if (udev->state == USB_STATE_NOTATTACHED ||
1076 udev->state == USB_STATE_SUSPENDED)
1077 goto done;
1078
Alan Sternb6f64362007-05-04 11:51:54 -04001079 /* For devices that don't have a driver, we do a generic suspend. */
1080 if (udev->dev.driver)
1081 udriver = to_usb_device_driver(udev->dev.driver);
1082 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001083 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001084 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001085 }
Alan Stern2bf40862006-07-01 22:12:19 -04001086 status = udriver->suspend(udev, msg);
1087
Alan Stern20dfdad2007-05-22 11:50:17 -04001088 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001089 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001090 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001091}
Alan Stern36e56a32006-07-01 22:08:06 -04001092
Alan Stern65bfd292008-11-25 16:39:18 -05001093static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001094{
1095 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001096 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001097
Alan Stern0458d5b2007-05-04 11:52:20 -04001098 if (udev->state == USB_STATE_NOTATTACHED)
1099 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001100
Alan Stern1c5df7e2006-07-01 22:13:50 -04001101 /* Can't resume it if it doesn't have a driver. */
1102 if (udev->dev.driver == NULL) {
1103 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001104 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001105 }
1106
Alan Stern6d19c002010-02-12 12:21:11 +01001107 /* Non-root devices on a full/low-speed bus must wait for their
1108 * companion high-speed root hub, in case a handoff is needed.
1109 */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001110 if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion)
Alan Stern6d19c002010-02-12 12:21:11 +01001111 device_pm_wait_for_dev(&udev->dev,
1112 &udev->bus->hs_companion->root_hub->dev);
1113
Alan Stern6bc6cff2007-05-04 11:53:03 -04001114 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1115 udev->reset_resume = 1;
1116
Alan Stern1cc8a252006-07-01 22:10:15 -04001117 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001118 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001119
Alan Stern20dfdad2007-05-22 11:50:17 -04001120 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001121 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001122 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001123}
1124
Alan Stern65605ae2008-08-12 14:33:27 -04001125static int usb_suspend_interface(struct usb_device *udev,
1126 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001127{
1128 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001129 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001130
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001131 if (udev->state == USB_STATE_NOTATTACHED ||
1132 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001133 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001134 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001135
Oliver Neukume78832c2012-01-02 15:11:48 +01001136 /* at this time we know the driver supports suspend */
1137 status = driver->suspend(intf, msg);
1138 if (status && !PMSG_IS_AUTO(msg))
1139 dev_err(&intf->dev, "suspend error %d\n", status);
Alan Stern2bf40862006-07-01 22:12:19 -04001140
Alan Stern20dfdad2007-05-22 11:50:17 -04001141 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001142 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001143 return status;
1144}
1145
Alan Stern65605ae2008-08-12 14:33:27 -04001146static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001147 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001148{
Alan Stern1cc8a252006-07-01 22:10:15 -04001149 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001150 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001151
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001152 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001153 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001154
Alan Stern645daaa2006-08-30 15:47:02 -04001155 /* Don't let autoresume interfere with unbinding */
1156 if (intf->condition == USB_INTERFACE_UNBINDING)
1157 goto done;
1158
Alan Stern1c5df7e2006-07-01 22:13:50 -04001159 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001160 if (intf->condition == USB_INTERFACE_UNBOUND) {
1161
1162 /* Carry out a deferred switch to altsetting 0 */
Alan Sternf76b168b2011-06-18 20:22:23 +02001163 if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) {
Alan Stern55151d72008-08-12 14:33:59 -04001164 usb_set_interface(udev, intf->altsetting[0].
1165 desc.bInterfaceNumber, 0);
1166 intf->needs_altsetting0 = 0;
1167 }
Alan Stern2bf40862006-07-01 22:12:19 -04001168 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001169 }
Alan Stern78d9a482008-06-23 16:00:40 -04001170
1171 /* Don't resume if the interface is marked for rebinding */
1172 if (intf->needs_binding)
1173 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001174 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001175
Alan Sternf07600c2007-05-30 15:38:16 -04001176 if (reset_resume) {
1177 if (driver->reset_resume) {
1178 status = driver->reset_resume(intf);
1179 if (status)
1180 dev_err(&intf->dev, "%s error %d\n",
1181 "reset_resume", status);
1182 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001183 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001184 dev_warn(&intf->dev, "no %s for driver %s?\n",
1185 "reset_resume", driver->name);
1186 }
1187 } else {
Oliver Neukume78832c2012-01-02 15:11:48 +01001188 status = driver->resume(intf);
1189 if (status)
1190 dev_err(&intf->dev, "resume error %d\n", status);
Alan Sternf07600c2007-05-30 15:38:16 -04001191 }
Alan Stern2bf40862006-07-01 22:12:19 -04001192
1193done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001194 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001195
Alan Stern78d9a482008-06-23 16:00:40 -04001196 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001197 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001198}
1199
Alan Stern645daaa2006-08-30 15:47:02 -04001200/**
1201 * usb_suspend_both - suspend a USB device and its interfaces
1202 * @udev: the usb_device to suspend
1203 * @msg: Power Management message describing this state transition
1204 *
1205 * This is the central routine for suspending USB devices. It calls the
1206 * suspend methods for all the interface drivers in @udev and then calls
Ming Lei303f0842013-03-15 12:08:53 +08001207 * the suspend method for @udev itself. When the routine is called in
1208 * autosuspend, if an error occurs at any stage, all the interfaces
1209 * which were suspended are resumed so that they remain in the same
1210 * state as the device, but when called from system sleep, all error
1211 * from suspend methods of interfaces and the non-root-hub device itself
1212 * are simply ignored, so all suspended interfaces are only resumed
1213 * to the device's state when @udev is root-hub and its suspend method
1214 * returns failure.
Alan Stern645daaa2006-08-30 15:47:02 -04001215 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001216 * Autosuspend requests originating from a child device or an interface
1217 * driver may be made without the protection of @udev's device lock, but
1218 * all other suspend calls will hold the lock. Usbcore will insure that
1219 * method calls do not arrive during bind, unbind, or reset operations.
1220 * However drivers must be prepared to handle suspend calls arriving at
1221 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001222 *
1223 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001224 *
1225 * Return: 0 if the suspend succeeded.
Alan Stern645daaa2006-08-30 15:47:02 -04001226 */
Alan Stern718efa62007-03-09 15:41:13 -05001227static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001228{
1229 int status = 0;
Alan Stern571dc792010-04-09 16:03:43 -04001230 int i = 0, n = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001231 struct usb_interface *intf;
1232
Alan Stern19410442007-03-27 13:33:59 -04001233 if (udev->state == USB_STATE_NOTATTACHED ||
1234 udev->state == USB_STATE_SUSPENDED)
1235 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001236
Alan Stern645daaa2006-08-30 15:47:02 -04001237 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001238 if (udev->actconfig) {
Alan Stern571dc792010-04-09 16:03:43 -04001239 n = udev->actconfig->desc.bNumInterfaces;
1240 for (i = n - 1; i >= 0; --i) {
Alan Sterna8e7c562006-07-01 22:11:02 -04001241 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001242 status = usb_suspend_interface(udev, intf, msg);
Alan Stern0af212b2011-06-15 16:27:43 -04001243
1244 /* Ignore errors during system sleep transitions */
Alan Stern5b1b0b82011-08-19 23:49:48 +02001245 if (!PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001246 status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001247 if (status != 0)
1248 break;
1249 }
1250 }
Alan Stern0af212b2011-06-15 16:27:43 -04001251 if (status == 0) {
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001252 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001253
Alan Sterncd4376e2012-03-28 15:56:17 -04001254 /*
1255 * Ignore errors from non-root-hub devices during
1256 * system sleep transitions. For the most part,
1257 * these devices should go to low power anyway when
1258 * the entire bus is suspended.
1259 */
1260 if (udev->parent && !PMSG_IS_AUTO(msg))
Alan Stern0af212b2011-06-15 16:27:43 -04001261 status = 0;
1262 }
1263
Alan Sterna8e7c562006-07-01 22:11:02 -04001264 /* If the suspend failed, resume interfaces that did get suspended */
1265 if (status != 0) {
Chen Gang505bdbc2013-04-01 13:04:08 +08001266 if (udev->actconfig) {
1267 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1268 while (++i < n) {
1269 intf = udev->actconfig->interface[i];
1270 usb_resume_interface(udev, intf, msg, 0);
1271 }
Alan Sterna8e7c562006-07-01 22:11:02 -04001272 }
Alan Stern645daaa2006-08-30 15:47:02 -04001273
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001274 /* If the suspend succeeded then prevent any more URB submissions
1275 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001276 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001277 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001278 udev->can_submit = 0;
1279 for (i = 0; i < 16; ++i) {
1280 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1281 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1282 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001283 }
Alan Stern645daaa2006-08-30 15:47:02 -04001284
Alan Stern19410442007-03-27 13:33:59 -04001285 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001286 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001287 return status;
1288}
1289
Alan Stern645daaa2006-08-30 15:47:02 -04001290/**
1291 * usb_resume_both - resume a USB device and its interfaces
1292 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001293 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001294 *
1295 * This is the central routine for resuming USB devices. It calls the
1296 * the resume method for @udev and then calls the resume methods for all
1297 * the interface drivers in @udev.
1298 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001299 * Autoresume requests originating from a child device or an interface
1300 * driver may be made without the protection of @udev's device lock, but
1301 * all other resume calls will hold the lock. Usbcore will insure that
1302 * method calls do not arrive during bind, unbind, or reset operations.
1303 * However drivers must be prepared to handle resume calls arriving at
1304 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001305 *
1306 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001307 *
1308 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001309 */
Alan Stern65bfd292008-11-25 16:39:18 -05001310static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001311{
Alan Stern645daaa2006-08-30 15:47:02 -04001312 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001313 int i;
1314 struct usb_interface *intf;
1315
Alan Stern19410442007-03-27 13:33:59 -04001316 if (udev->state == USB_STATE_NOTATTACHED) {
1317 status = -ENODEV;
1318 goto done;
1319 }
Alan Stern6840d252007-09-10 11:34:26 -04001320 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001321
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001322 /* Resume the device */
1323 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001324 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001325
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001326 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001327 if (status == 0 && udev->actconfig) {
1328 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1329 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001330 usb_resume_interface(udev, intf, msg,
1331 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001332 }
1333 }
Alan Sternc08512c2010-11-15 15:57:58 -05001334 usb_mark_last_busy(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04001335
Alan Stern19410442007-03-27 13:33:59 -04001336 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001337 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001338 if (!status)
1339 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001340 return status;
1341}
1342
Alan Stern5f677f12010-04-02 13:20:11 -04001343static void choose_wakeup(struct usb_device *udev, pm_message_t msg)
1344{
Alan Stern48826622010-06-22 16:14:48 -04001345 int w;
Alan Stern5f677f12010-04-02 13:20:11 -04001346
1347 /* Remote wakeup is needed only when we actually go to sleep.
1348 * For things like FREEZE and QUIESCE, if the device is already
1349 * autosuspended then its current wakeup setting is okay.
1350 */
1351 if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) {
1352 if (udev->state != USB_STATE_SUSPENDED)
1353 udev->do_remote_wakeup = 0;
1354 return;
1355 }
1356
Alan Stern48826622010-06-22 16:14:48 -04001357 /* Enable remote wakeup if it is allowed, even if no interface drivers
Alan Stern5f677f12010-04-02 13:20:11 -04001358 * actually want it.
1359 */
Alan Stern48826622010-06-22 16:14:48 -04001360 w = device_may_wakeup(&udev->dev);
Alan Stern5f677f12010-04-02 13:20:11 -04001361
1362 /* If the device is autosuspended with the wrong wakeup setting,
1363 * autoresume now so the setting can be changed.
1364 */
1365 if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup)
1366 pm_runtime_resume(&udev->dev);
1367 udev->do_remote_wakeup = w;
1368}
1369
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001370/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001371int usb_suspend(struct device *dev, pm_message_t msg)
1372{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001373 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001374
Oliver Neukum14931382012-01-05 15:39:57 +01001375 unbind_no_pm_drivers_interfaces(udev);
1376
1377 /* From now on we are sure all drivers support suspend/resume
1378 * but not necessarily reset_resume()
1379 * so we may still need to unbind and rebind upon resume
1380 */
Alan Stern5f677f12010-04-02 13:20:11 -04001381 choose_wakeup(udev, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001382 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001383}
1384
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001385/* The device lock is held by the PM core */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001386int usb_resume_complete(struct device *dev)
1387{
1388 struct usb_device *udev = to_usb_device(dev);
1389
1390 /* For PM complete calls, all we do is rebind interfaces
1391 * whose needs_binding flag is set
1392 */
1393 if (udev->state != USB_STATE_NOTATTACHED)
1394 do_rebind_interfaces(udev);
1395 return 0;
1396}
1397
1398/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001399int usb_resume(struct device *dev, pm_message_t msg)
1400{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001401 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001402 int status;
1403
Oliver Neukum98d9a822012-01-11 08:38:35 +01001404 /* For all calls, take the device back to full power and
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001405 * tell the PM core in case it was autosuspended previously.
Oliver Neukum14931382012-01-05 15:39:57 +01001406 * Unbind the interfaces that will need rebinding later,
1407 * because they fail to support reset_resume.
1408 * (This can't be done in usb_resume_interface()
Oliver Neukum98d9a822012-01-11 08:38:35 +01001409 * above because it doesn't own the right set of locks.)
Alan Stern0c590e22010-01-08 12:57:14 -05001410 */
Oliver Neukum98d9a822012-01-11 08:38:35 +01001411 status = usb_resume_both(udev, msg);
1412 if (status == 0) {
1413 pm_runtime_disable(dev);
1414 pm_runtime_set_active(dev);
1415 pm_runtime_enable(dev);
1416 unbind_no_reset_resume_drivers_interfaces(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001417 }
Alan Stern0c590e22010-01-08 12:57:14 -05001418
1419 /* Avoid PM error messages for devices disconnected while suspended
1420 * as we'll display regular disconnect messages just a bit later.
1421 */
Peter Chen7491f132010-09-27 16:43:25 +08001422 if (status == -ENODEV || status == -ESHUTDOWN)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001423 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001424 return status;
1425}
1426
1427#endif /* CONFIG_PM */
1428
Alan Stern84ebc102013-03-27 16:14:46 -04001429#ifdef CONFIG_PM_RUNTIME
Alan Stern645daaa2006-08-30 15:47:02 -04001430
Alan Stern088f7fe2010-01-08 12:56:54 -05001431/**
1432 * usb_enable_autosuspend - allow a USB device to be autosuspended
1433 * @udev: the USB device which may be autosuspended
1434 *
1435 * This routine allows @udev to be autosuspended. An autosuspend won't
1436 * take place until the autosuspend_delay has elapsed and all the other
1437 * necessary conditions are satisfied.
1438 *
1439 * The caller must hold @udev's device lock.
1440 */
Alan Stern9e18c822010-04-02 13:22:09 -04001441void usb_enable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001442{
Alan Stern9e18c822010-04-02 13:22:09 -04001443 pm_runtime_allow(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001444}
1445EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1446
1447/**
1448 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1449 * @udev: the USB device which may not be autosuspended
1450 *
1451 * This routine prevents @udev from being autosuspended and wakes it up
1452 * if it is already autosuspended.
1453 *
1454 * The caller must hold @udev's device lock.
1455 */
Alan Stern9e18c822010-04-02 13:22:09 -04001456void usb_disable_autosuspend(struct usb_device *udev)
Alan Stern088f7fe2010-01-08 12:56:54 -05001457{
Alan Stern9e18c822010-04-02 13:22:09 -04001458 pm_runtime_forbid(&udev->dev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001459}
1460EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1461
Alan Stern645daaa2006-08-30 15:47:02 -04001462/**
1463 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001464 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001465 *
1466 * This routine should be called when a core subsystem is finished using
1467 * @udev and wants to allow it to autosuspend. Examples would be when
1468 * @udev's device file in usbfs is closed or after a configuration change.
1469 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001470 * @udev's usage counter is decremented; if it drops to 0 and all the
1471 * interfaces are inactive then a delayed autosuspend will be attempted.
1472 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001473 *
Alan Stern62e299e2010-01-08 12:56:19 -05001474 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001475 *
1476 * This routine can run only in process context.
1477 */
Alan Stern94fcda12006-11-20 11:38:46 -05001478void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001479{
Alan Stern94fcda12006-11-20 11:38:46 -05001480 int status;
1481
Ming Lei6ddf27c2010-11-15 15:57:30 -05001482 usb_mark_last_busy(udev);
Alan Sternfcc4a012010-11-15 15:57:51 -05001483 status = pm_runtime_put_sync_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001484 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1485 __func__, atomic_read(&udev->dev.power.usage_count),
1486 status);
Alan Stern19c26232007-02-20 15:03:32 -05001487}
1488
1489/**
Alan Stern645daaa2006-08-30 15:47:02 -04001490 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001491 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001492 *
1493 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001494 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001495 * occur until usb_autosuspend_device() is called. (Note that this will
1496 * not prevent suspend events originating in the PM core.) Examples would
1497 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001498 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001499 *
Alan Stern94fcda12006-11-20 11:38:46 -05001500 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1501 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001502 *
Alan Stern62e299e2010-01-08 12:56:19 -05001503 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001504 *
1505 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001506 *
1507 * Return: 0 on success. A negative error code otherwise.
Alan Stern645daaa2006-08-30 15:47:02 -04001508 */
Alan Stern94fcda12006-11-20 11:38:46 -05001509int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001510{
1511 int status;
1512
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001513 status = pm_runtime_get_sync(&udev->dev);
1514 if (status < 0)
1515 pm_runtime_put_sync(&udev->dev);
1516 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1517 __func__, atomic_read(&udev->dev.power.usage_count),
1518 status);
1519 if (status > 0)
1520 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001521 return status;
1522}
1523
Alan Stern645daaa2006-08-30 15:47:02 -04001524/**
1525 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001526 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001527 *
1528 * This routine should be called by an interface driver when it is
1529 * finished using @intf and wants to allow it to autosuspend. A typical
1530 * example would be a character-device driver when its device file is
1531 * closed.
1532 *
1533 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001534 * 0, a delayed autosuspend request for @intf's device is attempted. The
1535 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001536 *
Alan Stern645daaa2006-08-30 15:47:02 -04001537 * This routine can run only in process context.
1538 */
1539void usb_autopm_put_interface(struct usb_interface *intf)
1540{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001541 struct usb_device *udev = interface_to_usbdev(intf);
1542 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001543
Ming Lei6ddf27c2010-11-15 15:57:30 -05001544 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001545 atomic_dec(&intf->pm_usage_cnt);
1546 status = pm_runtime_put_sync(&intf->dev);
1547 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1548 __func__, atomic_read(&intf->dev.power.usage_count),
1549 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001550}
1551EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1552
1553/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001554 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1555 * @intf: the usb_interface whose counter should be decremented
1556 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001557 * This routine does much the same thing as usb_autopm_put_interface():
1558 * It decrements @intf's usage counter and schedules a delayed
1559 * autosuspend request if the counter is <= 0. The difference is that it
1560 * does not perform any synchronization; callers should hold a private
1561 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001562 *
1563 * Typically a driver would call this routine during an URB's completion
1564 * handler, if no more URBs were pending.
1565 *
1566 * This routine can run in atomic context.
1567 */
1568void usb_autopm_put_interface_async(struct usb_interface *intf)
1569{
1570 struct usb_device *udev = interface_to_usbdev(intf);
Alan Sternfcc4a012010-11-15 15:57:51 -05001571 int status;
Alan Stern9ac39f22008-11-12 16:19:49 -05001572
Ming Lei6ddf27c2010-11-15 15:57:30 -05001573 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001574 atomic_dec(&intf->pm_usage_cnt);
Alan Sternfcc4a012010-11-15 15:57:51 -05001575 status = pm_runtime_put(&intf->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001576 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1577 __func__, atomic_read(&intf->dev.power.usage_count),
1578 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001579}
1580EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1581
1582/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001583 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1584 * @intf: the usb_interface whose counter should be decremented
1585 *
1586 * This routine decrements @intf's usage counter but does not carry out an
1587 * autosuspend.
1588 *
1589 * This routine can run in atomic context.
1590 */
1591void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1592{
1593 struct usb_device *udev = interface_to_usbdev(intf);
1594
Ming Lei6ddf27c2010-11-15 15:57:30 -05001595 usb_mark_last_busy(udev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001596 atomic_dec(&intf->pm_usage_cnt);
1597 pm_runtime_put_noidle(&intf->dev);
1598}
1599EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1600
1601/**
Alan Stern645daaa2006-08-30 15:47:02 -04001602 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001603 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001604 *
1605 * This routine should be called by an interface driver when it wants to
1606 * use @intf and needs to guarantee that it is not suspended. In addition,
1607 * the routine prevents @intf from being autosuspended subsequently. (Note
1608 * that this will not prevent suspend events originating in the PM core.)
1609 * This prevention will persist until usb_autopm_put_interface() is called
1610 * or @intf is unbound. A typical example would be a character-device
1611 * driver when its device file is opened.
1612 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001613 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1614 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001615 *
1616 * This routine can run only in process context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001617 *
1618 * Return: 0 on success.
Alan Stern645daaa2006-08-30 15:47:02 -04001619 */
1620int usb_autopm_get_interface(struct usb_interface *intf)
1621{
Alan Sternaf4f7602006-10-30 17:06:45 -05001622 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001623
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001624 status = pm_runtime_get_sync(&intf->dev);
1625 if (status < 0)
1626 pm_runtime_put_sync(&intf->dev);
1627 else
1628 atomic_inc(&intf->pm_usage_cnt);
1629 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1630 __func__, atomic_read(&intf->dev.power.usage_count),
1631 status);
1632 if (status > 0)
1633 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001634 return status;
1635}
1636EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1637
Alan Stern692a1862006-10-30 17:07:51 -05001638/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001639 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1640 * @intf: the usb_interface whose counter should be incremented
1641 *
1642 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001643 * usb_autopm_get_interface(): It increments @intf's usage counter and
1644 * queues an autoresume request if the device is suspended. The
1645 * differences are that it does not perform any synchronization (callers
1646 * should hold a private lock and handle all synchronization issues
1647 * themselves), and it does not autoresume the device directly (it only
1648 * queues a request). After a successful call, the device may not yet be
1649 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001650 *
1651 * This routine can run in atomic context.
Yacine Belkadi626f0902013-08-02 20:10:04 +02001652 *
1653 * Return: 0 on success. A negative error code otherwise.
Alan Stern9ac39f22008-11-12 16:19:49 -05001654 */
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);
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001786 /* Tell the core not to suspend it, though. */
1787 return -EBUSY;
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001788}
1789
Andiry Xu65580b432011-09-23 14:19:52 -07001790int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable)
1791{
1792 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1793 int ret = -EPERM;
1794
1795 if (hcd->driver->set_usb2_hw_lpm) {
1796 ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable);
1797 if (!ret)
1798 udev->usb2_hw_lpm_enabled = enable;
1799 }
1800
1801 return ret;
1802}
1803
Alan Stern84ebc102013-03-27 16:14:46 -04001804#endif /* CONFIG_PM_RUNTIME */
Alan Stern645daaa2006-08-30 15:47:02 -04001805
Alan Stern36e56a32006-07-01 22:08:06 -04001806struct bus_type usb_bus_type = {
1807 .name = "usb",
1808 .match = usb_device_match,
1809 .uevent = usb_uevent,
Alan Stern36e56a32006-07-01 22:08:06 -04001810};