blob: 6a3b5cae3a6e7d3b4b16fe0229caf59f3ad315f1 [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>
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080027#include <linux/usb.h>
Alan Stern6bc6cff2007-05-04 11:53:03 -040028#include <linux/usb/quirks.h>
Alan Stern9bbdf1e2010-01-08 12:57:28 -050029#include <linux/pm_runtime.h>
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080030#include "hcd.h"
31#include "usb.h"
32
Alan Stern20dfdad2007-05-22 11:50:17 -040033
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080034#ifdef CONFIG_HOTPLUG
35
36/*
37 * Adds a new dynamic USBdevice ID to this driver,
38 * and cause the driver to probe for all devices again.
39 */
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010040ssize_t usb_store_new_id(struct usb_dynids *dynids,
41 struct device_driver *driver,
42 const char *buf, size_t count)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080043{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080044 struct usb_dynid *dynid;
45 u32 idVendor = 0;
46 u32 idProduct = 0;
47 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070048 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080049
50 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
51 if (fields < 2)
52 return -EINVAL;
53
54 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
55 if (!dynid)
56 return -ENOMEM;
57
58 INIT_LIST_HEAD(&dynid->node);
59 dynid->id.idVendor = idVendor;
60 dynid->id.idProduct = idProduct;
61 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
62
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010063 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020064 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010065 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080066
67 if (get_driver(driver)) {
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070068 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080069 put_driver(driver);
70 }
71
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070072 if (retval)
73 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080074 return count;
75}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010076EXPORT_SYMBOL_GPL(usb_store_new_id);
77
78static ssize_t store_new_id(struct device_driver *driver,
79 const char *buf, size_t count)
80{
81 struct usb_driver *usb_drv = to_usb_driver(driver);
82
83 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
84}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080085static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
86
CHENG Renquan0c7a2b72009-11-22 01:28:52 +080087/**
88 * store_remove_id - remove a USB device ID from this driver
89 * @driver: target device driver
90 * @buf: buffer for scanning device ID data
91 * @count: input size
92 *
93 * Removes a dynamic usb device ID from this driver.
94 */
95static ssize_t
96store_remove_id(struct device_driver *driver, const char *buf, size_t count)
97{
98 struct usb_dynid *dynid, *n;
99 struct usb_driver *usb_driver = to_usb_driver(driver);
100 u32 idVendor = 0;
101 u32 idProduct = 0;
102 int fields = 0;
103 int retval = 0;
104
105 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
106 if (fields < 2)
107 return -EINVAL;
108
109 spin_lock(&usb_driver->dynids.lock);
110 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
111 struct usb_device_id *id = &dynid->id;
112 if ((id->idVendor == idVendor) &&
113 (id->idProduct == idProduct)) {
114 list_del(&dynid->node);
115 kfree(dynid);
116 retval = 0;
117 break;
118 }
119 }
120 spin_unlock(&usb_driver->dynids.lock);
121
122 if (retval)
123 return retval;
124 return count;
125}
126static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
127
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800128static int usb_create_newid_file(struct usb_driver *usb_drv)
129{
130 int error = 0;
131
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800132 if (usb_drv->no_dynamic_id)
133 goto exit;
134
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800135 if (usb_drv->probe != NULL)
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800136 error = driver_create_file(&usb_drv->drvwrap.driver,
137 &driver_attr_new_id);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800138exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800139 return error;
140}
141
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800142static void usb_remove_newid_file(struct usb_driver *usb_drv)
143{
144 if (usb_drv->no_dynamic_id)
145 return;
146
147 if (usb_drv->probe != NULL)
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800148 driver_remove_file(&usb_drv->drvwrap.driver,
149 &driver_attr_new_id);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800150}
151
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800152static int
153usb_create_removeid_file(struct usb_driver *drv)
154{
155 int error = 0;
156 if (drv->probe != NULL)
157 error = driver_create_file(&drv->drvwrap.driver,
158 &driver_attr_remove_id);
159 return error;
160}
161
162static void usb_remove_removeid_file(struct usb_driver *drv)
163{
164 driver_remove_file(&drv->drvwrap.driver, &driver_attr_remove_id);
165}
166
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800167static void usb_free_dynids(struct usb_driver *usb_drv)
168{
169 struct usb_dynid *dynid, *n;
170
171 spin_lock(&usb_drv->dynids.lock);
172 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
173 list_del(&dynid->node);
174 kfree(dynid);
175 }
176 spin_unlock(&usb_drv->dynids.lock);
177}
178#else
179static inline int usb_create_newid_file(struct usb_driver *usb_drv)
180{
181 return 0;
182}
183
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800184static void usb_remove_newid_file(struct usb_driver *usb_drv)
185{
186}
187
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800188static int
189usb_create_removeid_file(struct usb_driver *drv)
190{
191 return 0;
192}
193
194static void usb_remove_removeid_file(struct usb_driver *drv)
195{
196}
197
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800198static inline void usb_free_dynids(struct usb_driver *usb_drv)
199{
200}
201#endif
202
203static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
204 struct usb_driver *drv)
205{
206 struct usb_dynid *dynid;
207
208 spin_lock(&drv->dynids.lock);
209 list_for_each_entry(dynid, &drv->dynids.list, node) {
210 if (usb_match_one_id(intf, &dynid->id)) {
211 spin_unlock(&drv->dynids.lock);
212 return &dynid->id;
213 }
214 }
215 spin_unlock(&drv->dynids.lock);
216 return NULL;
217}
218
219
Alan Stern8bb54ab2006-07-01 22:08:49 -0400220/* called from driver core with dev locked */
221static int usb_probe_device(struct device *dev)
222{
223 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200224 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500225 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400226
Harvey Harrison441b62c2008-03-03 16:08:34 -0800227 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400228
Alan Stern8bb54ab2006-07-01 22:08:49 -0400229 /* TODO: Add real matching code */
230
Alan Stern645daaa2006-08-30 15:47:02 -0400231 /* The device should always appear to be in use
232 * unless the driver suports autosuspend.
233 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500234 if (!udriver->supports_autosuspend)
235 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400236
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500237 if (!error)
238 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400239 return error;
240}
241
242/* called from driver core with dev locked */
243static int usb_unbind_device(struct device *dev)
244{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500245 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400246 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
247
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500248 udriver->disconnect(udev);
249 if (!udriver->supports_autosuspend)
250 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400251 return 0;
252}
253
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800254/*
255 * Cancel any pending scheduled resets
256 *
257 * [see usb_queue_reset_device()]
258 *
259 * Called after unconfiguring / when releasing interfaces. See
260 * comments in __usb_queue_reset_device() regarding
261 * udev->reset_running.
262 */
263static void usb_cancel_queued_reset(struct usb_interface *iface)
264{
265 if (iface->reset_running == 0)
266 cancel_work_sync(&iface->reset_ws);
267}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400268
269/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800270static int usb_probe_interface(struct device *dev)
271{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400272 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200273 struct usb_interface *intf = to_usb_interface(dev);
274 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800275 const struct usb_device_id *id;
276 int error = -ENODEV;
277
Harvey Harrison441b62c2008-03-03 16:08:34 -0800278 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800279
Alan Stern78d9a482008-06-23 16:00:40 -0400280 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800281
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400282 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500283 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400284
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800285 if (udev->authorized == 0) {
286 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500287 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800288 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700289
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800290 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800291 if (!id)
292 id = usb_match_dynamic_id(intf, driver);
Alan Stern0f3dda92010-01-08 12:56:04 -0500293 if (!id)
294 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800295
Alan Stern0f3dda92010-01-08 12:56:04 -0500296 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400297
Alan Stern0f3dda92010-01-08 12:56:04 -0500298 error = usb_autoresume_device(udev);
299 if (error)
300 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400301
Alan Stern0f3dda92010-01-08 12:56:04 -0500302 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400303
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500304 /* Bound interfaces are initially active. They are
305 * runtime-PM-enabled only if the driver has autosuspend support.
306 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500307 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500308 pm_runtime_set_active(dev);
309 pm_suspend_ignore_children(dev, false);
310 if (driver->supports_autosuspend)
311 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200312
Alan Stern0f3dda92010-01-08 12:56:04 -0500313 /* Carry out a deferred switch to altsetting 0 */
314 if (intf->needs_altsetting0) {
315 error = usb_set_interface(udev, intf->altsetting[0].
316 desc.bInterfaceNumber, 0);
317 if (error < 0)
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200318 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500319 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800320 }
321
Alan Stern0f3dda92010-01-08 12:56:04 -0500322 error = driver->probe(intf, id);
323 if (error)
324 goto err;
325
326 intf->condition = USB_INTERFACE_BOUND;
327 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800328 return error;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200329
Alan Stern0f3dda92010-01-08 12:56:04 -0500330 err:
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200331 intf->needs_remote_wakeup = 0;
332 intf->condition = USB_INTERFACE_UNBOUND;
333 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500334
335 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
336 pm_runtime_disable(dev);
337 pm_runtime_set_suspended(dev);
338
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200339 usb_autosuspend_device(udev);
340 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800341}
342
Alan Stern8bb54ab2006-07-01 22:08:49 -0400343/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800344static int usb_unbind_interface(struct device *dev)
345{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400346 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800347 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400348 struct usb_device *udev;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200349 int error, r;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800350
351 intf->condition = USB_INTERFACE_UNBINDING;
352
Alan Stern645daaa2006-08-30 15:47:02 -0400353 /* Autoresume for set_interface call below */
354 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500355 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400356
Alan Stern9da82bd2008-05-08 11:54:37 -0400357 /* Terminate all URBs for this interface unless the driver
358 * supports "soft" unbinding.
359 */
360 if (!driver->soft_unbind)
Alan Sternddeac4e72009-01-15 17:03:33 -0500361 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800362
Alan Stern8bb54ab2006-07-01 22:08:49 -0400363 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800364 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800365
Alan Stern55151d72008-08-12 14:33:59 -0400366 /* Reset other interface state.
367 * We cannot do a Set-Interface if the device is suspended or
368 * if it is prepared for a system sleep (since installing a new
369 * altsetting means creating new endpoint device entries).
370 * When either of these happens, defer the Set-Interface.
371 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500372 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
373 /* Already in altsetting 0 so skip Set-Interface.
374 * Just re-enable it without affecting the endpoint toggles.
375 */
376 usb_enable_interface(udev, intf, false);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200377 } else if (!error && intf->dev.power.status == DPM_ON) {
378 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400379 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200380 if (r < 0)
381 intf->needs_altsetting0 = 1;
382 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400383 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200384 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800385 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400386
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800387 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400388 intf->needs_remote_wakeup = 0;
389
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500390 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
391 pm_runtime_disable(dev);
392 pm_runtime_set_suspended(dev);
393
394 /* Undo any residual pm_autopm_get_interface_* calls */
395 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
396 usb_autopm_put_interface_no_suspend(intf);
397 atomic_set(&intf->pm_usage_cnt, 0);
398
Alan Stern645daaa2006-08-30 15:47:02 -0400399 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500400 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800401
402 return 0;
403}
404
Alan Stern36e56a32006-07-01 22:08:06 -0400405/**
406 * usb_driver_claim_interface - bind a driver to an interface
407 * @driver: the driver to be bound
408 * @iface: the interface to which it will be bound; must be in the
409 * usb device's active configuration
410 * @priv: driver data associated with that interface
411 *
412 * This is used by usb device drivers that need to claim more than one
413 * interface on a device when probing (audio and acm are current examples).
414 * No device driver should directly modify internal usb_interface or
415 * usb_device structure members.
416 *
417 * Few drivers should need to use this routine, since the most natural
418 * way to bind to an interface is to return the private data from
419 * the driver's probe() method.
420 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400421 * Callers must own the device lock, so driver probe() entries don't need
422 * extra locking, but other call contexts may need to explicitly claim that
423 * lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400424 */
425int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800426 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400427{
428 struct device *dev = &iface->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700429 int retval = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400430
431 if (dev->driver)
432 return -EBUSY;
433
Alan Stern8bb54ab2006-07-01 22:08:49 -0400434 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400435 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400436 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400437
Alan Stern36e56a32006-07-01 22:08:06 -0400438 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500439
440 /* Bound interfaces are initially active. They are
441 * runtime-PM-enabled only if the driver has autosuspend support.
442 * They are sensitive to their children's power states.
443 */
444 pm_runtime_set_active(dev);
445 pm_suspend_ignore_children(dev, false);
446 if (driver->supports_autosuspend)
447 pm_runtime_enable(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400448
449 /* if interface was already added, bind now; else let
450 * the future device_add() bind it, bypassing probe()
451 */
452 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700453 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400454
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700455 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400456}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600457EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400458
459/**
460 * usb_driver_release_interface - unbind a driver from an interface
461 * @driver: the driver to be unbound
462 * @iface: the interface from which it will be unbound
463 *
464 * This can be used by drivers to release an interface without waiting
465 * for their disconnect() methods to be called. In typical cases this
466 * also causes the driver disconnect() method to be called.
467 *
468 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400469 * Callers must own the device lock, so driver disconnect() entries don't
470 * need extra locking, but other call contexts may need to explicitly claim
471 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400472 */
473void usb_driver_release_interface(struct usb_driver *driver,
474 struct usb_interface *iface)
475{
476 struct device *dev = &iface->dev;
477
478 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400479 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400480 return;
481
482 /* don't release from within disconnect() */
483 if (iface->condition != USB_INTERFACE_BOUND)
484 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400485 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400486
Alan Stern91f8d062009-04-16 15:35:09 -0400487 /* Release via the driver core only if the interface
488 * has already been registered
489 */
Alan Stern36e56a32006-07-01 22:08:06 -0400490 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400491 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800492 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800493 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400494 usb_unbind_interface(dev);
495 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800496 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400497 }
Alan Stern36e56a32006-07-01 22:08:06 -0400498}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600499EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400500
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800501/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100502int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800503{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800504 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
505 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
506 return 0;
507
508 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
509 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
510 return 0;
511
512 /* No need to test id->bcdDevice_lo != 0, since 0 is never
513 greater than any unsigned number. */
514 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
515 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
516 return 0;
517
518 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
519 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
520 return 0;
521
522 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
523 (id->bDeviceClass != dev->descriptor.bDeviceClass))
524 return 0;
525
526 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800527 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800528 return 0;
529
530 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
531 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
532 return 0;
533
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100534 return 1;
535}
536
537/* returns 0 if no match, 1 if match */
538int usb_match_one_id(struct usb_interface *interface,
539 const struct usb_device_id *id)
540{
541 struct usb_host_interface *intf;
542 struct usb_device *dev;
543
544 /* proc_connectinfo in devio.c may call us with id == NULL. */
545 if (id == NULL)
546 return 0;
547
548 intf = interface->cur_altsetting;
549 dev = interface_to_usbdev(interface);
550
551 if (!usb_match_device(dev, id))
552 return 0;
553
Alan Stern93c8bf42006-10-18 16:41:51 -0400554 /* The interface class, subclass, and protocol should never be
555 * checked for a match if the device class is Vendor Specific,
556 * unless the match record specifies the Vendor ID. */
557 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
558 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
559 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
560 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
561 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
562 return 0;
563
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800564 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
565 (id->bInterfaceClass != intf->desc.bInterfaceClass))
566 return 0;
567
568 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
569 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
570 return 0;
571
572 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
573 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
574 return 0;
575
576 return 1;
577}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100578EXPORT_SYMBOL_GPL(usb_match_one_id);
579
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800580/**
581 * usb_match_id - find first usb_device_id matching device or interface
582 * @interface: the interface of interest
583 * @id: array of usb_device_id structures, terminated by zero entry
584 *
585 * usb_match_id searches an array of usb_device_id's and returns
586 * the first one matching the device or interface, or null.
587 * This is used when binding (or rebinding) a driver to an interface.
588 * Most USB device drivers will use this indirectly, through the usb core,
589 * but some layered driver frameworks use it directly.
590 * These device tables are exported with MODULE_DEVICE_TABLE, through
591 * modutils, to support the driver loading functionality of USB hotplugging.
592 *
593 * What Matches:
594 *
595 * The "match_flags" element in a usb_device_id controls which
596 * members are used. If the corresponding bit is set, the
597 * value in the device_id must match its corresponding member
598 * in the device or interface descriptor, or else the device_id
599 * does not match.
600 *
601 * "driver_info" is normally used only by device drivers,
602 * but you can create a wildcard "matches anything" usb_device_id
603 * as a driver's "modules.usbmap" entry if you provide an id with
604 * only a nonzero "driver_info" field. If you do this, the USB device
605 * driver's probe() routine should use additional intelligence to
606 * decide whether to bind to the specified interface.
607 *
608 * What Makes Good usb_device_id Tables:
609 *
610 * The match algorithm is very simple, so that intelligence in
611 * driver selection must come from smart driver id records.
612 * Unless you have good reasons to use another selection policy,
613 * provide match elements only in related groups, and order match
614 * specifiers from specific to general. Use the macros provided
615 * for that purpose if you can.
616 *
617 * The most specific match specifiers use device descriptor
618 * data. These are commonly used with product-specific matches;
619 * the USB_DEVICE macro lets you provide vendor and product IDs,
620 * and you can also match against ranges of product revisions.
621 * These are widely used for devices with application or vendor
622 * specific bDeviceClass values.
623 *
624 * Matches based on device class/subclass/protocol specifications
625 * are slightly more general; use the USB_DEVICE_INFO macro, or
626 * its siblings. These are used with single-function devices
627 * where bDeviceClass doesn't specify that each interface has
628 * its own class.
629 *
630 * Matches based on interface class/subclass/protocol are the
631 * most general; they let drivers bind to any interface on a
632 * multiple-function device. Use the USB_INTERFACE_INFO
633 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400634 * devices (as recorded in bInterfaceClass).
635 *
636 * Note that an entry created by USB_INTERFACE_INFO won't match
637 * any interface if the device class is set to Vendor-Specific.
638 * This is deliberate; according to the USB spec the meanings of
639 * the interface class/subclass/protocol for these devices are also
640 * vendor-specific, and hence matching against a standard product
641 * class wouldn't work anyway. If you really want to use an
642 * interface-based match for such a device, create a match record
643 * that also specifies the vendor ID. (Unforunately there isn't a
644 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800645 *
646 * Within those groups, remember that not all combinations are
647 * meaningful. For example, don't give a product version range
648 * without vendor and product IDs; or specify a protocol without
649 * its associated class and subclass.
650 */
651const struct usb_device_id *usb_match_id(struct usb_interface *interface,
652 const struct usb_device_id *id)
653{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800654 /* proc_connectinfo in devio.c may call us with id == NULL. */
655 if (id == NULL)
656 return NULL;
657
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800658 /* It is important to check that id->driver_info is nonzero,
659 since an entry that is all zeroes except for a nonzero
660 id->driver_info is the way to create an entry that
661 indicates that the driver want to examine every
662 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800663 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
664 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800665 if (usb_match_one_id(interface, id))
666 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800667 }
668
669 return NULL;
670}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600671EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800672
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100673static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800674{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400675 /* devices and interfaces are handled separately */
676 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800677
Alan Stern8bb54ab2006-07-01 22:08:49 -0400678 /* interface drivers never match devices */
679 if (!is_usb_device_driver(drv))
680 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800681
Alan Stern8bb54ab2006-07-01 22:08:49 -0400682 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800683 return 1;
684
Kay Sievers55129662009-05-04 19:48:32 +0200685 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400686 struct usb_interface *intf;
687 struct usb_driver *usb_drv;
688 const struct usb_device_id *id;
689
690 /* device drivers never match interfaces */
691 if (is_usb_device_driver(drv))
692 return 0;
693
694 intf = to_usb_interface(dev);
695 usb_drv = to_usb_driver(drv);
696
697 id = usb_match_id(intf, usb_drv->id_table);
698 if (id)
699 return 1;
700
701 id = usb_match_dynamic_id(intf, usb_drv);
702 if (id)
703 return 1;
704 }
705
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800706 return 0;
707}
708
Alan Stern36e56a32006-07-01 22:08:06 -0400709#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200710static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400711{
Alan Stern36e56a32006-07-01 22:08:06 -0400712 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400713
Kay Sievers55129662009-05-04 19:48:32 +0200714 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400715 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200716 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100717 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200718
Alan Stern8bb54ab2006-07-01 22:08:49 -0400719 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200720 } else {
721 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400722 }
Alan Stern36e56a32006-07-01 22:08:06 -0400723
724 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500725 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200726 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400727 return -ENODEV;
728 }
729 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200730 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400731 return -ENODEV;
732 }
733
734#ifdef CONFIG_USB_DEVICEFS
735 /* If this is available, userspace programs can directly read
736 * all the device descriptors we don't tell them about. Or
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100737 * act as usermode drivers.
Alan Stern36e56a32006-07-01 22:08:06 -0400738 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200739 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
Alan Stern36e56a32006-07-01 22:08:06 -0400740 usb_dev->bus->busnum, usb_dev->devnum))
741 return -ENOMEM;
742#endif
743
744 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200745 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400746 le16_to_cpu(usb_dev->descriptor.idVendor),
747 le16_to_cpu(usb_dev->descriptor.idProduct),
748 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
749 return -ENOMEM;
750
751 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200752 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400753 usb_dev->descriptor.bDeviceClass,
754 usb_dev->descriptor.bDeviceSubClass,
755 usb_dev->descriptor.bDeviceProtocol))
756 return -ENOMEM;
757
Alan Stern36e56a32006-07-01 22:08:06 -0400758 return 0;
759}
760
761#else
762
Kay Sievers7eff2e72007-08-14 15:15:12 +0200763static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400764{
765 return -ENODEV;
766}
Alan Stern36e56a32006-07-01 22:08:06 -0400767#endif /* CONFIG_HOTPLUG */
768
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800769/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400770 * usb_register_device_driver - register a USB device (not interface) driver
771 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800772 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800773 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400774 * Registers a USB device driver with the USB core. The list of
775 * unattached devices will be rescanned whenever a new driver is
776 * added, allowing the new driver to attach to any recognized devices.
777 * Returns a negative error code on failure and 0 on success.
778 */
779int usb_register_device_driver(struct usb_device_driver *new_udriver,
780 struct module *owner)
781{
782 int retval = 0;
783
784 if (usb_disabled())
785 return -ENODEV;
786
787 new_udriver->drvwrap.for_devices = 1;
788 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
789 new_udriver->drvwrap.driver.bus = &usb_bus_type;
790 new_udriver->drvwrap.driver.probe = usb_probe_device;
791 new_udriver->drvwrap.driver.remove = usb_unbind_device;
792 new_udriver->drvwrap.driver.owner = owner;
793
794 retval = driver_register(&new_udriver->drvwrap.driver);
795
796 if (!retval) {
797 pr_info("%s: registered new device driver %s\n",
798 usbcore_name, new_udriver->name);
799 usbfs_update_special();
800 } else {
801 printk(KERN_ERR "%s: error %d registering device "
802 " driver %s\n",
803 usbcore_name, retval, new_udriver->name);
804 }
805
806 return retval;
807}
808EXPORT_SYMBOL_GPL(usb_register_device_driver);
809
810/**
811 * usb_deregister_device_driver - unregister a USB device (not interface) driver
812 * @udriver: USB operations of the device driver to unregister
813 * Context: must be able to sleep
814 *
815 * Unlinks the specified driver from the internal USB driver list.
816 */
817void usb_deregister_device_driver(struct usb_device_driver *udriver)
818{
819 pr_info("%s: deregistering device driver %s\n",
820 usbcore_name, udriver->name);
821
822 driver_unregister(&udriver->drvwrap.driver);
823 usbfs_update_special();
824}
825EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
826
827/**
828 * usb_register_driver - register a USB interface driver
829 * @new_driver: USB operations for the interface driver
830 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800831 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400832 *
833 * Registers a USB interface driver with the USB core. The list of
834 * unattached interfaces will be rescanned whenever a new driver is
835 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800836 * Returns a negative error code on failure and 0 on success.
837 *
838 * NOTE: if you want your driver to use the USB major number, you must call
839 * usb_register_dev() to enable that functionality. This function no longer
840 * takes care of that.
841 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800842int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
843 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800844{
845 int retval = 0;
846
847 if (usb_disabled())
848 return -ENODEV;
849
Alan Stern8bb54ab2006-07-01 22:08:49 -0400850 new_driver->drvwrap.for_devices = 0;
851 new_driver->drvwrap.driver.name = (char *) new_driver->name;
852 new_driver->drvwrap.driver.bus = &usb_bus_type;
853 new_driver->drvwrap.driver.probe = usb_probe_interface;
854 new_driver->drvwrap.driver.remove = usb_unbind_interface;
855 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800856 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800857 spin_lock_init(&new_driver->dynids.lock);
858 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800859
Alan Stern8bb54ab2006-07-01 22:08:49 -0400860 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800861 if (retval)
862 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800863
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800864 usbfs_update_special();
865
866 retval = usb_create_newid_file(new_driver);
867 if (retval)
868 goto out_newid;
869
870 retval = usb_create_removeid_file(new_driver);
871 if (retval)
872 goto out_removeid;
873
874 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800875 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800876
877out:
878 return retval;
879
880out_removeid:
881 usb_remove_newid_file(new_driver);
882out_newid:
883 driver_unregister(&new_driver->drvwrap.driver);
884
885 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400886 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800887 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800888 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800889}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600890EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800891
892/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400893 * usb_deregister - unregister a USB interface driver
894 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800895 * Context: must be able to sleep
896 *
897 * Unlinks the specified driver from the internal USB driver list.
898 *
899 * NOTE: If you called usb_register_dev(), you still need to call
900 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
901 * this * call will no longer do it for you.
902 */
903void usb_deregister(struct usb_driver *driver)
904{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400905 pr_info("%s: deregistering interface driver %s\n",
906 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800907
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800908 usb_remove_removeid_file(driver);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800909 usb_remove_newid_file(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800910 usb_free_dynids(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400911 driver_unregister(&driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800912
913 usbfs_update_special();
914}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600915EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400916
Alan Stern78d9a482008-06-23 16:00:40 -0400917/* Forced unbinding of a USB interface driver, either because
918 * it doesn't support pre_reset/post_reset/reset_resume or
919 * because it doesn't support suspend/resume.
920 *
921 * The caller must hold @intf's device's lock, but not its pm_mutex
922 * and not @intf->dev.sem.
923 */
924void usb_forced_unbind_intf(struct usb_interface *intf)
925{
926 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
927
928 dev_dbg(&intf->dev, "forced unbind\n");
929 usb_driver_release_interface(driver, intf);
930
931 /* Mark the interface for later rebinding */
932 intf->needs_binding = 1;
933}
934
935/* Delayed forced unbinding of a USB interface driver and scan
936 * for rebinding.
937 *
938 * The caller must hold @intf's device's lock, but not its pm_mutex
939 * and not @intf->dev.sem.
940 *
Alan Stern5096aed2008-08-12 14:34:14 -0400941 * Note: Rebinds will be skipped if a system sleep transition is in
942 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -0400943 */
944void usb_rebind_intf(struct usb_interface *intf)
945{
946 int rc;
947
948 /* Delayed unbind of an existing driver */
949 if (intf->dev.driver) {
950 struct usb_driver *driver =
951 to_usb_driver(intf->dev.driver);
952
953 dev_dbg(&intf->dev, "forced unbind\n");
954 usb_driver_release_interface(driver, intf);
955 }
956
957 /* Try to rebind the interface */
Alan Stern5096aed2008-08-12 14:34:14 -0400958 if (intf->dev.power.status == DPM_ON) {
959 intf->needs_binding = 0;
960 rc = device_attach(&intf->dev);
961 if (rc < 0)
962 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
963 }
Alan Stern78d9a482008-06-23 16:00:40 -0400964}
965
Alan Stern9ff78432008-08-07 13:04:51 -0400966#ifdef CONFIG_PM
967
Alan Stern78d9a482008-06-23 16:00:40 -0400968#define DO_UNBIND 0
969#define DO_REBIND 1
970
971/* Unbind drivers for @udev's interfaces that don't support suspend/resume,
972 * or rebind interfaces that have been unbound, according to @action.
973 *
974 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -0400975 */
976static void do_unbind_rebind(struct usb_device *udev, int action)
977{
978 struct usb_host_config *config;
979 int i;
980 struct usb_interface *intf;
981 struct usb_driver *drv;
982
983 config = udev->actconfig;
984 if (config) {
985 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
986 intf = config->interface[i];
987 switch (action) {
988 case DO_UNBIND:
989 if (intf->dev.driver) {
990 drv = to_usb_driver(intf->dev.driver);
991 if (!drv->suspend || !drv->resume)
992 usb_forced_unbind_intf(intf);
993 }
994 break;
995 case DO_REBIND:
Alan Stern5096aed2008-08-12 14:34:14 -0400996 if (intf->needs_binding)
Alan Stern78d9a482008-06-23 16:00:40 -0400997 usb_rebind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -0400998 break;
999 }
1000 }
1001 }
1002}
1003
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001004static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001005{
Alan Stern782da722006-07-01 22:09:35 -04001006 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001007 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001008
Alan Stern114b3682006-07-01 22:13:04 -04001009 if (udev->state == USB_STATE_NOTATTACHED ||
1010 udev->state == USB_STATE_SUSPENDED)
1011 goto done;
1012
Alan Sternb6f64362007-05-04 11:51:54 -04001013 /* For devices that don't have a driver, we do a generic suspend. */
1014 if (udev->dev.driver)
1015 udriver = to_usb_device_driver(udev->dev.driver);
1016 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001017 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001018 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001019 }
Alan Stern2bf40862006-07-01 22:12:19 -04001020 status = udriver->suspend(udev, msg);
1021
Alan Stern20dfdad2007-05-22 11:50:17 -04001022 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001023 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001024 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001025}
Alan Stern36e56a32006-07-01 22:08:06 -04001026
Alan Stern65bfd292008-11-25 16:39:18 -05001027static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001028{
1029 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001030 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001031
Alan Stern0458d5b2007-05-04 11:52:20 -04001032 if (udev->state == USB_STATE_NOTATTACHED)
1033 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001034
Alan Stern1c5df7e2006-07-01 22:13:50 -04001035 /* Can't resume it if it doesn't have a driver. */
1036 if (udev->dev.driver == NULL) {
1037 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001038 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001039 }
1040
Alan Stern6d19c002010-02-12 12:21:11 +01001041 /* Non-root devices on a full/low-speed bus must wait for their
1042 * companion high-speed root hub, in case a handoff is needed.
1043 */
1044 if (!(msg.event & PM_EVENT_AUTO) && udev->parent &&
1045 udev->bus->hs_companion)
1046 device_pm_wait_for_dev(&udev->dev,
1047 &udev->bus->hs_companion->root_hub->dev);
1048
Alan Stern6bc6cff2007-05-04 11:53:03 -04001049 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1050 udev->reset_resume = 1;
1051
Alan Stern1cc8a252006-07-01 22:10:15 -04001052 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001053 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001054
Alan Stern20dfdad2007-05-22 11:50:17 -04001055 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001056 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001057 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001058}
1059
Alan Stern65605ae2008-08-12 14:33:27 -04001060static int usb_suspend_interface(struct usb_device *udev,
1061 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001062{
1063 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001064 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001065
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001066 if (udev->state == USB_STATE_NOTATTACHED ||
1067 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001068 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001069 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001070
Alan Stern78d9a482008-06-23 16:00:40 -04001071 if (driver->suspend) {
Alan Stern1cc8a252006-07-01 22:10:15 -04001072 status = driver->suspend(intf, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001073 if (status && !(msg.event & PM_EVENT_AUTO))
Alan Stern1cc8a252006-07-01 22:10:15 -04001074 dev_err(&intf->dev, "%s error %d\n",
1075 "suspend", status);
Alan Stern36e56a32006-07-01 22:08:06 -04001076 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001077 /* Later we will unbind the driver and reprobe */
1078 intf->needs_binding = 1;
1079 dev_warn(&intf->dev, "no %s for driver %s?\n",
1080 "suspend", driver->name);
Alan Stern36e56a32006-07-01 22:08:06 -04001081 }
Alan Stern2bf40862006-07-01 22:12:19 -04001082
Alan Stern20dfdad2007-05-22 11:50:17 -04001083 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001084 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001085 return status;
1086}
1087
Alan Stern65605ae2008-08-12 14:33:27 -04001088static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001089 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001090{
Alan Stern1cc8a252006-07-01 22:10:15 -04001091 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001092 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001093
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001094 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001095 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001096
Alan Stern645daaa2006-08-30 15:47:02 -04001097 /* Don't let autoresume interfere with unbinding */
1098 if (intf->condition == USB_INTERFACE_UNBINDING)
1099 goto done;
1100
Alan Stern1c5df7e2006-07-01 22:13:50 -04001101 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001102 if (intf->condition == USB_INTERFACE_UNBOUND) {
1103
1104 /* Carry out a deferred switch to altsetting 0 */
1105 if (intf->needs_altsetting0 &&
1106 intf->dev.power.status == DPM_ON) {
1107 usb_set_interface(udev, intf->altsetting[0].
1108 desc.bInterfaceNumber, 0);
1109 intf->needs_altsetting0 = 0;
1110 }
Alan Stern2bf40862006-07-01 22:12:19 -04001111 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001112 }
Alan Stern78d9a482008-06-23 16:00:40 -04001113
1114 /* Don't resume if the interface is marked for rebinding */
1115 if (intf->needs_binding)
1116 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001117 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001118
Alan Sternf07600c2007-05-30 15:38:16 -04001119 if (reset_resume) {
1120 if (driver->reset_resume) {
1121 status = driver->reset_resume(intf);
1122 if (status)
1123 dev_err(&intf->dev, "%s error %d\n",
1124 "reset_resume", status);
1125 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001126 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001127 dev_warn(&intf->dev, "no %s for driver %s?\n",
1128 "reset_resume", driver->name);
1129 }
1130 } else {
1131 if (driver->resume) {
1132 status = driver->resume(intf);
1133 if (status)
1134 dev_err(&intf->dev, "%s error %d\n",
1135 "resume", status);
1136 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001137 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001138 dev_warn(&intf->dev, "no %s for driver %s?\n",
1139 "resume", driver->name);
1140 }
1141 }
Alan Stern2bf40862006-07-01 22:12:19 -04001142
1143done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001144 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001145
Alan Stern78d9a482008-06-23 16:00:40 -04001146 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001147 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001148}
1149
Alan Stern645daaa2006-08-30 15:47:02 -04001150/**
1151 * usb_suspend_both - suspend a USB device and its interfaces
1152 * @udev: the usb_device to suspend
1153 * @msg: Power Management message describing this state transition
1154 *
1155 * This is the central routine for suspending USB devices. It calls the
1156 * suspend methods for all the interface drivers in @udev and then calls
1157 * the suspend method for @udev itself. If an error occurs at any stage,
1158 * all the interfaces which were suspended are resumed so that they remain
1159 * in the same state as the device.
1160 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001161 * Autosuspend requests originating from a child device or an interface
1162 * driver may be made without the protection of @udev's device lock, but
1163 * all other suspend calls will hold the lock. Usbcore will insure that
1164 * method calls do not arrive during bind, unbind, or reset operations.
1165 * However drivers must be prepared to handle suspend calls arriving at
1166 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001167 *
1168 * This routine can run only in process context.
1169 */
Alan Stern718efa62007-03-09 15:41:13 -05001170static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001171{
1172 int status = 0;
1173 int i = 0;
1174 struct usb_interface *intf;
1175
Alan Stern19410442007-03-27 13:33:59 -04001176 if (udev->state == USB_STATE_NOTATTACHED ||
1177 udev->state == USB_STATE_SUSPENDED)
1178 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001179
Alan Stern645daaa2006-08-30 15:47:02 -04001180 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001181 if (udev->actconfig) {
1182 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1183 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001184 status = usb_suspend_interface(udev, intf, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001185 if (status != 0)
1186 break;
1187 }
1188 }
Alan Stern5ad4f712007-09-10 11:31:43 -04001189 if (status == 0)
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001190 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001191
1192 /* If the suspend failed, resume interfaces that did get suspended */
1193 if (status != 0) {
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001194 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
Alan Sterna8e7c562006-07-01 22:11:02 -04001195 while (--i >= 0) {
1196 intf = udev->actconfig->interface[i];
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001197 usb_resume_interface(udev, intf, msg, 0);
Alan Sterna8e7c562006-07-01 22:11:02 -04001198 }
Alan Stern645daaa2006-08-30 15:47:02 -04001199
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001200 /* If the suspend succeeded then prevent any more URB submissions
1201 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001202 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001203 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001204 udev->can_submit = 0;
1205 for (i = 0; i < 16; ++i) {
1206 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1207 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1208 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001209 }
Alan Stern645daaa2006-08-30 15:47:02 -04001210
Alan Stern19410442007-03-27 13:33:59 -04001211 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001212 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001213 return status;
1214}
1215
Alan Stern645daaa2006-08-30 15:47:02 -04001216/**
1217 * usb_resume_both - resume a USB device and its interfaces
1218 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001219 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001220 *
1221 * This is the central routine for resuming USB devices. It calls the
1222 * the resume method for @udev and then calls the resume methods for all
1223 * the interface drivers in @udev.
1224 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001225 * Autoresume requests originating from a child device or an interface
1226 * driver may be made without the protection of @udev's device lock, but
1227 * all other resume calls will hold the lock. Usbcore will insure that
1228 * method calls do not arrive during bind, unbind, or reset operations.
1229 * However drivers must be prepared to handle resume calls arriving at
1230 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001231 *
1232 * This routine can run only in process context.
1233 */
Alan Stern65bfd292008-11-25 16:39:18 -05001234static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001235{
Alan Stern645daaa2006-08-30 15:47:02 -04001236 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001237 int i;
1238 struct usb_interface *intf;
1239
Alan Stern19410442007-03-27 13:33:59 -04001240 if (udev->state == USB_STATE_NOTATTACHED) {
1241 status = -ENODEV;
1242 goto done;
1243 }
Alan Stern6840d252007-09-10 11:34:26 -04001244 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001245
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001246 /* Resume the device */
1247 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001248 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001249
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001250 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001251 if (status == 0 && udev->actconfig) {
1252 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1253 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001254 usb_resume_interface(udev, intf, msg,
1255 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001256 }
1257 }
Alan Stern645daaa2006-08-30 15:47:02 -04001258
Alan Stern19410442007-03-27 13:33:59 -04001259 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001260 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001261 if (!status)
1262 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001263 return status;
1264}
1265
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001266/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001267int usb_suspend(struct device *dev, pm_message_t msg)
1268{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001269 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001270
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001271 do_unbind_rebind(udev, DO_UNBIND);
1272 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1273 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001274}
1275
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001276/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001277int usb_resume(struct device *dev, pm_message_t msg)
1278{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001279 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001280 int status;
1281
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001282 /* For PM complete calls, all we do is rebind interfaces */
1283 if (msg.event == PM_EVENT_ON) {
1284 if (udev->state != USB_STATE_NOTATTACHED)
1285 do_unbind_rebind(udev, DO_REBIND);
1286 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001287
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001288 /* For all other calls, take the device back to full power and
1289 * tell the PM core in case it was autosuspended previously.
Alan Stern0c590e22010-01-08 12:57:14 -05001290 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001291 } else {
1292 status = usb_resume_both(udev, msg);
1293 if (status == 0) {
1294 pm_runtime_disable(dev);
1295 pm_runtime_set_active(dev);
1296 pm_runtime_enable(dev);
1297 udev->last_busy = jiffies;
1298 }
1299 }
Alan Stern0c590e22010-01-08 12:57:14 -05001300
1301 /* Avoid PM error messages for devices disconnected while suspended
1302 * as we'll display regular disconnect messages just a bit later.
1303 */
1304 if (status == -ENODEV)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001305 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001306 return status;
1307}
1308
1309#endif /* CONFIG_PM */
1310
Alan Stern645daaa2006-08-30 15:47:02 -04001311#ifdef CONFIG_USB_SUSPEND
1312
Alan Stern088f7fe2010-01-08 12:56:54 -05001313/**
1314 * usb_enable_autosuspend - allow a USB device to be autosuspended
1315 * @udev: the USB device which may be autosuspended
1316 *
1317 * This routine allows @udev to be autosuspended. An autosuspend won't
1318 * take place until the autosuspend_delay has elapsed and all the other
1319 * necessary conditions are satisfied.
1320 *
1321 * The caller must hold @udev's device lock.
1322 */
1323int usb_enable_autosuspend(struct usb_device *udev)
1324{
1325 if (udev->autosuspend_disabled) {
1326 udev->autosuspend_disabled = 0;
1327 usb_autosuspend_device(udev);
1328 }
1329 return 0;
1330}
1331EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1332
1333/**
1334 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1335 * @udev: the USB device which may not be autosuspended
1336 *
1337 * This routine prevents @udev from being autosuspended and wakes it up
1338 * if it is already autosuspended.
1339 *
1340 * The caller must hold @udev's device lock.
1341 */
1342int usb_disable_autosuspend(struct usb_device *udev)
1343{
1344 int rc = 0;
1345
1346 if (!udev->autosuspend_disabled) {
1347 rc = usb_autoresume_device(udev);
1348 if (rc == 0)
1349 udev->autosuspend_disabled = 1;
1350 }
1351 return rc;
1352}
1353EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1354
Alan Stern645daaa2006-08-30 15:47:02 -04001355/**
1356 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001357 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001358 *
1359 * This routine should be called when a core subsystem is finished using
1360 * @udev and wants to allow it to autosuspend. Examples would be when
1361 * @udev's device file in usbfs is closed or after a configuration change.
1362 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001363 * @udev's usage counter is decremented; if it drops to 0 and all the
1364 * interfaces are inactive then a delayed autosuspend will be attempted.
1365 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001366 *
Alan Stern62e299e2010-01-08 12:56:19 -05001367 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001368 *
1369 * This routine can run only in process context.
1370 */
Alan Stern94fcda12006-11-20 11:38:46 -05001371void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001372{
Alan Stern94fcda12006-11-20 11:38:46 -05001373 int status;
1374
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001375 udev->last_busy = jiffies;
1376 status = pm_runtime_put_sync(&udev->dev);
1377 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1378 __func__, atomic_read(&udev->dev.power.usage_count),
1379 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001380}
1381
1382/**
Alan Stern19c26232007-02-20 15:03:32 -05001383 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1384 * @udev: the usb_device to autosuspend
1385 *
1386 * This routine should be called when a core subsystem thinks @udev may
1387 * be ready to autosuspend.
1388 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001389 * @udev's usage counter left unchanged. If it is 0 and all the interfaces
1390 * are inactive then an autosuspend will be attempted. The attempt may
1391 * fail or be delayed.
Alan Stern19c26232007-02-20 15:03:32 -05001392 *
Alan Stern62e299e2010-01-08 12:56:19 -05001393 * The caller must hold @udev's device lock.
1394 *
Alan Stern19c26232007-02-20 15:03:32 -05001395 * This routine can run only in process context.
1396 */
1397void usb_try_autosuspend_device(struct usb_device *udev)
1398{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001399 int status;
1400
1401 status = pm_runtime_idle(&udev->dev);
1402 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1403 __func__, atomic_read(&udev->dev.power.usage_count),
1404 status);
Alan Stern19c26232007-02-20 15:03:32 -05001405}
1406
1407/**
Alan Stern645daaa2006-08-30 15:47:02 -04001408 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001409 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001410 *
1411 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001412 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001413 * occur until usb_autosuspend_device() is called. (Note that this will
1414 * not prevent suspend events originating in the PM core.) Examples would
1415 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001416 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001417 *
Alan Stern94fcda12006-11-20 11:38:46 -05001418 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1419 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001420 *
Alan Stern62e299e2010-01-08 12:56:19 -05001421 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001422 *
1423 * This routine can run only in process context.
1424 */
Alan Stern94fcda12006-11-20 11:38:46 -05001425int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001426{
1427 int status;
1428
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001429 status = pm_runtime_get_sync(&udev->dev);
1430 if (status < 0)
1431 pm_runtime_put_sync(&udev->dev);
1432 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1433 __func__, atomic_read(&udev->dev.power.usage_count),
1434 status);
1435 if (status > 0)
1436 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001437 return status;
1438}
1439
Alan Stern645daaa2006-08-30 15:47:02 -04001440/**
1441 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001442 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001443 *
1444 * This routine should be called by an interface driver when it is
1445 * finished using @intf and wants to allow it to autosuspend. A typical
1446 * example would be a character-device driver when its device file is
1447 * closed.
1448 *
1449 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001450 * 0, a delayed autosuspend request for @intf's device is attempted. The
1451 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001452 *
1453 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1454 * take place only if the device's remote-wakeup facility is enabled.
1455 *
Alan Stern645daaa2006-08-30 15:47:02 -04001456 * This routine can run only in process context.
1457 */
1458void usb_autopm_put_interface(struct usb_interface *intf)
1459{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001460 struct usb_device *udev = interface_to_usbdev(intf);
1461 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001462
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001463 udev->last_busy = jiffies;
1464 atomic_dec(&intf->pm_usage_cnt);
1465 status = pm_runtime_put_sync(&intf->dev);
1466 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1467 __func__, atomic_read(&intf->dev.power.usage_count),
1468 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001469}
1470EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1471
1472/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001473 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1474 * @intf: the usb_interface whose counter should be decremented
1475 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001476 * This routine does much the same thing as usb_autopm_put_interface():
1477 * It decrements @intf's usage counter and schedules a delayed
1478 * autosuspend request if the counter is <= 0. The difference is that it
1479 * does not perform any synchronization; callers should hold a private
1480 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001481 *
1482 * Typically a driver would call this routine during an URB's completion
1483 * handler, if no more URBs were pending.
1484 *
1485 * This routine can run in atomic context.
1486 */
1487void usb_autopm_put_interface_async(struct usb_interface *intf)
1488{
1489 struct usb_device *udev = interface_to_usbdev(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001490 unsigned long last_busy;
Alan Stern9ac39f22008-11-12 16:19:49 -05001491 int status = 0;
1492
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001493 last_busy = udev->last_busy;
1494 udev->last_busy = jiffies;
1495 atomic_dec(&intf->pm_usage_cnt);
1496 pm_runtime_put_noidle(&intf->dev);
1497
1498 if (!udev->autosuspend_disabled) {
1499 /* Optimization: Don't schedule a delayed autosuspend if
1500 * the timer is already running and the expiration time
1501 * wouldn't change.
1502 *
1503 * We have to use the interface's timer. Attempts to
1504 * schedule a suspend for the device would fail because
1505 * the interface is still active.
1506 */
1507 if (intf->dev.power.timer_expires == 0 ||
1508 round_jiffies_up(last_busy) !=
1509 round_jiffies_up(jiffies)) {
1510 status = pm_schedule_suspend(&intf->dev,
1511 jiffies_to_msecs(
Alan Stern4ec06d62008-11-25 16:40:02 -05001512 round_jiffies_up_relative(
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001513 udev->autosuspend_delay)));
Alan Stern9ac39f22008-11-12 16:19:49 -05001514 }
1515 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001516 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1517 __func__, atomic_read(&intf->dev.power.usage_count),
1518 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001519}
1520EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1521
1522/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001523 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1524 * @intf: the usb_interface whose counter should be decremented
1525 *
1526 * This routine decrements @intf's usage counter but does not carry out an
1527 * autosuspend.
1528 *
1529 * This routine can run in atomic context.
1530 */
1531void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1532{
1533 struct usb_device *udev = interface_to_usbdev(intf);
1534
1535 udev->last_busy = jiffies;
1536 atomic_dec(&intf->pm_usage_cnt);
1537 pm_runtime_put_noidle(&intf->dev);
1538}
1539EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1540
1541/**
Alan Stern645daaa2006-08-30 15:47:02 -04001542 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001543 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001544 *
1545 * This routine should be called by an interface driver when it wants to
1546 * use @intf and needs to guarantee that it is not suspended. In addition,
1547 * the routine prevents @intf from being autosuspended subsequently. (Note
1548 * that this will not prevent suspend events originating in the PM core.)
1549 * This prevention will persist until usb_autopm_put_interface() is called
1550 * or @intf is unbound. A typical example would be a character-device
1551 * driver when its device file is opened.
1552 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001553 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1554 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001555 *
1556 * This routine can run only in process context.
1557 */
1558int usb_autopm_get_interface(struct usb_interface *intf)
1559{
Alan Sternaf4f7602006-10-30 17:06:45 -05001560 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001561
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001562 status = pm_runtime_get_sync(&intf->dev);
1563 if (status < 0)
1564 pm_runtime_put_sync(&intf->dev);
1565 else
1566 atomic_inc(&intf->pm_usage_cnt);
1567 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1568 __func__, atomic_read(&intf->dev.power.usage_count),
1569 status);
1570 if (status > 0)
1571 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001572 return status;
1573}
1574EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1575
Alan Stern692a1862006-10-30 17:07:51 -05001576/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001577 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1578 * @intf: the usb_interface whose counter should be incremented
1579 *
1580 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001581 * usb_autopm_get_interface(): It increments @intf's usage counter and
1582 * queues an autoresume request if the device is suspended. The
1583 * differences are that it does not perform any synchronization (callers
1584 * should hold a private lock and handle all synchronization issues
1585 * themselves), and it does not autoresume the device directly (it only
1586 * queues a request). After a successful call, the device may not yet be
1587 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001588 *
1589 * This routine can run in atomic context.
1590 */
1591int usb_autopm_get_interface_async(struct usb_interface *intf)
1592{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001593 int status = 0;
1594 enum rpm_status s;
Alan Stern9ac39f22008-11-12 16:19:49 -05001595
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001596 /* Don't request a resume unless the interface is already suspending
1597 * or suspended. Doing so would force a running suspend timer to be
1598 * cancelled.
1599 */
1600 pm_runtime_get_noresume(&intf->dev);
1601 s = ACCESS_ONCE(intf->dev.power.runtime_status);
1602 if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
1603 status = pm_request_resume(&intf->dev);
1604
1605 if (status < 0 && status != -EINPROGRESS)
1606 pm_runtime_put_noidle(&intf->dev);
1607 else
Alan Sternccf5b802009-06-29 11:00:01 -04001608 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001609 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1610 __func__, atomic_read(&intf->dev.power.usage_count),
1611 status);
1612 if (status > 0)
1613 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001614 return status;
1615}
1616EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1617
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001618/**
1619 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1620 * @intf: the usb_interface whose counter should be incremented
1621 *
1622 * This routine increments @intf's usage counter but does not carry out an
1623 * autoresume.
1624 *
1625 * This routine can run in atomic context.
1626 */
1627void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1628{
1629 struct usb_device *udev = interface_to_usbdev(intf);
1630
1631 udev->last_busy = jiffies;
1632 atomic_inc(&intf->pm_usage_cnt);
1633 pm_runtime_get_noresume(&intf->dev);
1634}
1635EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1636
1637/* Internal routine to check whether we may autosuspend a device. */
1638static int autosuspend_check(struct usb_device *udev)
1639{
1640 int i;
1641 struct usb_interface *intf;
1642 unsigned long suspend_time, j;
1643
1644 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1645 * any interface drivers require remote wakeup but it isn't available.
1646 */
1647 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1648 if (udev->actconfig) {
1649 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1650 intf = udev->actconfig->interface[i];
1651
1652 /* We don't need to check interfaces that are
1653 * disabled for runtime PM. Either they are unbound
1654 * or else their drivers don't support autosuspend
1655 * and so they are permanently active.
1656 */
1657 if (intf->dev.power.disable_depth)
1658 continue;
1659 if (atomic_read(&intf->dev.power.usage_count) > 0)
1660 return -EBUSY;
1661 if (intf->needs_remote_wakeup &&
1662 !udev->do_remote_wakeup) {
1663 dev_dbg(&udev->dev, "remote wakeup needed "
1664 "for autosuspend\n");
1665 return -EOPNOTSUPP;
1666 }
1667
1668 /* Don't allow autosuspend if the device will need
1669 * a reset-resume and any of its interface drivers
1670 * doesn't include support or needs remote wakeup.
1671 */
1672 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1673 struct usb_driver *driver;
1674
1675 driver = to_usb_driver(intf->dev.driver);
1676 if (!driver->reset_resume ||
1677 intf->needs_remote_wakeup)
1678 return -EOPNOTSUPP;
1679 }
1680 }
1681 }
1682
1683 /* If everything is okay but the device hasn't been idle for long
1684 * enough, queue a delayed autosuspend request.
1685 */
1686 j = ACCESS_ONCE(jiffies);
1687 suspend_time = udev->last_busy + udev->autosuspend_delay;
1688 if (time_before(j, suspend_time)) {
1689 pm_schedule_suspend(&udev->dev, jiffies_to_msecs(
1690 round_jiffies_up_relative(suspend_time - j)));
1691 return -EAGAIN;
1692 }
1693 return 0;
1694}
1695
1696static int usb_runtime_suspend(struct device *dev)
1697{
1698 int status = 0;
1699
1700 /* A USB device can be suspended if it passes the various autosuspend
1701 * checks. Runtime suspend for a USB device means suspending all the
1702 * interfaces and then the device itself.
1703 */
1704 if (is_usb_device(dev)) {
1705 struct usb_device *udev = to_usb_device(dev);
1706
1707 if (autosuspend_check(udev) != 0)
1708 return -EAGAIN;
1709
1710 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1711
1712 /* If an interface fails the suspend, adjust the last_busy
1713 * time so that we don't get another suspend attempt right
1714 * away.
1715 */
1716 if (status) {
1717 udev->last_busy = jiffies +
1718 (udev->autosuspend_delay == 0 ?
1719 HZ/2 : 0);
1720 }
1721
1722 /* Prevent the parent from suspending immediately after */
1723 else if (udev->parent) {
1724 udev->parent->last_busy = jiffies;
1725 }
1726 }
1727
1728 /* Runtime suspend for a USB interface doesn't mean anything. */
1729 return status;
1730}
1731
1732static int usb_runtime_resume(struct device *dev)
1733{
1734 /* Runtime resume for a USB device means resuming both the device
1735 * and all its interfaces.
1736 */
1737 if (is_usb_device(dev)) {
1738 struct usb_device *udev = to_usb_device(dev);
1739 int status;
1740
1741 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1742 udev->last_busy = jiffies;
1743 return status;
1744 }
1745
1746 /* Runtime resume for a USB interface doesn't mean anything. */
1747 return 0;
1748}
1749
1750static int usb_runtime_idle(struct device *dev)
1751{
1752 /* An idle USB device can be suspended if it passes the various
1753 * autosuspend checks. An idle interface can be suspended at
1754 * any time.
1755 */
1756 if (is_usb_device(dev)) {
1757 struct usb_device *udev = to_usb_device(dev);
1758
1759 if (autosuspend_check(udev) != 0)
1760 return 0;
1761 }
1762
1763 pm_runtime_suspend(dev);
1764 return 0;
1765}
1766
1767static struct dev_pm_ops usb_bus_pm_ops = {
1768 .runtime_suspend = usb_runtime_suspend,
1769 .runtime_resume = usb_runtime_resume,
1770 .runtime_idle = usb_runtime_idle,
1771};
1772
Alan Stern718efa62007-03-09 15:41:13 -05001773#else
1774
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001775#define usb_bus_pm_ops (*(struct dev_pm_ops *) NULL)
Alan Stern9ac39f22008-11-12 16:19:49 -05001776
Alan Stern645daaa2006-08-30 15:47:02 -04001777#endif /* CONFIG_USB_SUSPEND */
1778
Alan Stern36e56a32006-07-01 22:08:06 -04001779struct bus_type usb_bus_type = {
1780 .name = "usb",
1781 .match = usb_device_match,
1782 .uevent = usb_uevent,
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001783 .pm = &usb_bus_pm_ops,
Alan Stern36e56a32006-07-01 22:08:06 -04001784};