blob: f3c233806fa3415a5489842f3ccbf4030da55f8f [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>
26#include <linux/usb.h>
Alan Stern6bc6cff2007-05-04 11:53:03 -040027#include <linux/usb/quirks.h>
Alan Stern9bbdf1e2010-01-08 12:57:28 -050028#include <linux/pm_runtime.h>
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080029#include "hcd.h"
30#include "usb.h"
31
Alan Stern20dfdad2007-05-22 11:50:17 -040032
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080033#ifdef CONFIG_HOTPLUG
34
35/*
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;
46 int fields = 0;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070047 int retval = 0;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080048
49 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
50 if (fields < 2)
51 return -EINVAL;
52
53 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
54 if (!dynid)
55 return -ENOMEM;
56
57 INIT_LIST_HEAD(&dynid->node);
58 dynid->id.idVendor = idVendor;
59 dynid->id.idProduct = idProduct;
60 dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE;
61
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010062 spin_lock(&dynids->lock);
Nathael Pajanie5dd0112007-09-04 11:46:23 +020063 list_add_tail(&dynid->node, &dynids->list);
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010064 spin_unlock(&dynids->lock);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080065
66 if (get_driver(driver)) {
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070067 retval = driver_attach(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080068 put_driver(driver);
69 }
70
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -070071 if (retval)
72 return retval;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080073 return count;
74}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +010075EXPORT_SYMBOL_GPL(usb_store_new_id);
76
77static ssize_t store_new_id(struct device_driver *driver,
78 const char *buf, size_t count)
79{
80 struct usb_driver *usb_drv = to_usb_driver(driver);
81
82 return usb_store_new_id(&usb_drv->dynids, driver, buf, count);
83}
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080084static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
85
CHENG Renquan0c7a2b72009-11-22 01:28:52 +080086/**
87 * store_remove_id - remove a USB device ID from this driver
88 * @driver: target device driver
89 * @buf: buffer for scanning device ID data
90 * @count: input size
91 *
92 * Removes a dynamic usb device ID from this driver.
93 */
94static ssize_t
95store_remove_id(struct device_driver *driver, const char *buf, size_t count)
96{
97 struct usb_dynid *dynid, *n;
98 struct usb_driver *usb_driver = to_usb_driver(driver);
99 u32 idVendor = 0;
100 u32 idProduct = 0;
101 int fields = 0;
102 int retval = 0;
103
104 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
105 if (fields < 2)
106 return -EINVAL;
107
108 spin_lock(&usb_driver->dynids.lock);
109 list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) {
110 struct usb_device_id *id = &dynid->id;
111 if ((id->idVendor == idVendor) &&
112 (id->idProduct == idProduct)) {
113 list_del(&dynid->node);
114 kfree(dynid);
115 retval = 0;
116 break;
117 }
118 }
119 spin_unlock(&usb_driver->dynids.lock);
120
121 if (retval)
122 return retval;
123 return count;
124}
125static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
126
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800127static int usb_create_newid_file(struct usb_driver *usb_drv)
128{
129 int error = 0;
130
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800131 if (usb_drv->no_dynamic_id)
132 goto exit;
133
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800134 if (usb_drv->probe != NULL)
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800135 error = driver_create_file(&usb_drv->drvwrap.driver,
136 &driver_attr_new_id);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800137exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800138 return error;
139}
140
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800141static void usb_remove_newid_file(struct usb_driver *usb_drv)
142{
143 if (usb_drv->no_dynamic_id)
144 return;
145
146 if (usb_drv->probe != NULL)
Greg Kroah-Hartman15147ff2007-11-28 12:23:18 -0800147 driver_remove_file(&usb_drv->drvwrap.driver,
148 &driver_attr_new_id);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800149}
150
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800151static int
152usb_create_removeid_file(struct usb_driver *drv)
153{
154 int error = 0;
155 if (drv->probe != NULL)
156 error = driver_create_file(&drv->drvwrap.driver,
157 &driver_attr_remove_id);
158 return error;
159}
160
161static void usb_remove_removeid_file(struct usb_driver *drv)
162{
163 driver_remove_file(&drv->drvwrap.driver, &driver_attr_remove_id);
164}
165
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800166static void usb_free_dynids(struct usb_driver *usb_drv)
167{
168 struct usb_dynid *dynid, *n;
169
170 spin_lock(&usb_drv->dynids.lock);
171 list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) {
172 list_del(&dynid->node);
173 kfree(dynid);
174 }
175 spin_unlock(&usb_drv->dynids.lock);
176}
177#else
178static inline int usb_create_newid_file(struct usb_driver *usb_drv)
179{
180 return 0;
181}
182
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800183static void usb_remove_newid_file(struct usb_driver *usb_drv)
184{
185}
186
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800187static int
188usb_create_removeid_file(struct usb_driver *drv)
189{
190 return 0;
191}
192
193static void usb_remove_removeid_file(struct usb_driver *drv)
194{
195}
196
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800197static inline void usb_free_dynids(struct usb_driver *usb_drv)
198{
199}
200#endif
201
202static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
203 struct usb_driver *drv)
204{
205 struct usb_dynid *dynid;
206
207 spin_lock(&drv->dynids.lock);
208 list_for_each_entry(dynid, &drv->dynids.list, node) {
209 if (usb_match_one_id(intf, &dynid->id)) {
210 spin_unlock(&drv->dynids.lock);
211 return &dynid->id;
212 }
213 }
214 spin_unlock(&drv->dynids.lock);
215 return NULL;
216}
217
218
Alan Stern8bb54ab2006-07-01 22:08:49 -0400219/* called from driver core with dev locked */
220static int usb_probe_device(struct device *dev)
221{
222 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200223 struct usb_device *udev = to_usb_device(dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500224 int error = 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400225
Harvey Harrison441b62c2008-03-03 16:08:34 -0800226 dev_dbg(dev, "%s\n", __func__);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400227
Alan Stern8bb54ab2006-07-01 22:08:49 -0400228 /* TODO: Add real matching code */
229
Alan Stern645daaa2006-08-30 15:47:02 -0400230 /* The device should always appear to be in use
231 * unless the driver suports autosuspend.
232 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500233 if (!udriver->supports_autosuspend)
234 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400235
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500236 if (!error)
237 error = udriver->probe(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400238 return error;
239}
240
241/* called from driver core with dev locked */
242static int usb_unbind_device(struct device *dev)
243{
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500244 struct usb_device *udev = to_usb_device(dev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400245 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
246
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500247 udriver->disconnect(udev);
248 if (!udriver->supports_autosuspend)
249 usb_autosuspend_device(udev);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400250 return 0;
251}
252
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800253/*
254 * Cancel any pending scheduled resets
255 *
256 * [see usb_queue_reset_device()]
257 *
258 * Called after unconfiguring / when releasing interfaces. See
259 * comments in __usb_queue_reset_device() regarding
260 * udev->reset_running.
261 */
262static void usb_cancel_queued_reset(struct usb_interface *iface)
263{
264 if (iface->reset_running == 0)
265 cancel_work_sync(&iface->reset_ws);
266}
Alan Stern8bb54ab2006-07-01 22:08:49 -0400267
268/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800269static int usb_probe_interface(struct device *dev)
270{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400271 struct usb_driver *driver = to_usb_driver(dev->driver);
Kay Sievers55129662009-05-04 19:48:32 +0200272 struct usb_interface *intf = to_usb_interface(dev);
273 struct usb_device *udev = interface_to_usbdev(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800274 const struct usb_device_id *id;
275 int error = -ENODEV;
276
Harvey Harrison441b62c2008-03-03 16:08:34 -0800277 dev_dbg(dev, "%s\n", __func__);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800278
Alan Stern78d9a482008-06-23 16:00:40 -0400279 intf->needs_binding = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800280
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400281 if (usb_device_is_owned(udev))
Alan Stern0f3dda92010-01-08 12:56:04 -0500282 return error;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400283
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800284 if (udev->authorized == 0) {
285 dev_err(&intf->dev, "Device is not authorized for usage\n");
Alan Stern0f3dda92010-01-08 12:56:04 -0500286 return error;
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800287 }
Inaky Perez-Gonzalez72230ab2007-07-31 20:34:03 -0700288
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800289 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800290 if (!id)
291 id = usb_match_dynamic_id(intf, driver);
Alan Stern0f3dda92010-01-08 12:56:04 -0500292 if (!id)
293 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800294
Alan Stern0f3dda92010-01-08 12:56:04 -0500295 dev_dbg(dev, "%s - got id\n", __func__);
Alan Stern645daaa2006-08-30 15:47:02 -0400296
Alan Stern0f3dda92010-01-08 12:56:04 -0500297 error = usb_autoresume_device(udev);
298 if (error)
299 return error;
Alan Stern645daaa2006-08-30 15:47:02 -0400300
Alan Stern0f3dda92010-01-08 12:56:04 -0500301 intf->condition = USB_INTERFACE_BINDING;
Alan Stern645daaa2006-08-30 15:47:02 -0400302
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500303 /* Bound interfaces are initially active. They are
304 * runtime-PM-enabled only if the driver has autosuspend support.
305 * They are sensitive to their children's power states.
Alan Stern0f3dda92010-01-08 12:56:04 -0500306 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500307 pm_runtime_set_active(dev);
308 pm_suspend_ignore_children(dev, false);
309 if (driver->supports_autosuspend)
310 pm_runtime_enable(dev);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200311
Alan Stern0f3dda92010-01-08 12:56:04 -0500312 /* Carry out a deferred switch to altsetting 0 */
313 if (intf->needs_altsetting0) {
314 error = usb_set_interface(udev, intf->altsetting[0].
315 desc.bInterfaceNumber, 0);
316 if (error < 0)
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200317 goto err;
Alan Stern0f3dda92010-01-08 12:56:04 -0500318 intf->needs_altsetting0 = 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800319 }
320
Alan Stern0f3dda92010-01-08 12:56:04 -0500321 error = driver->probe(intf, id);
322 if (error)
323 goto err;
324
325 intf->condition = USB_INTERFACE_BOUND;
326 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800327 return error;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200328
Alan Stern0f3dda92010-01-08 12:56:04 -0500329 err:
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200330 intf->needs_remote_wakeup = 0;
331 intf->condition = USB_INTERFACE_UNBOUND;
332 usb_cancel_queued_reset(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500333
334 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
335 pm_runtime_disable(dev);
336 pm_runtime_set_suspended(dev);
337
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200338 usb_autosuspend_device(udev);
339 return error;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800340}
341
Alan Stern8bb54ab2006-07-01 22:08:49 -0400342/* called from driver core with dev locked */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800343static int usb_unbind_interface(struct device *dev)
344{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400345 struct usb_driver *driver = to_usb_driver(dev->driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800346 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern645daaa2006-08-30 15:47:02 -0400347 struct usb_device *udev;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200348 int error, r;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800349
350 intf->condition = USB_INTERFACE_UNBINDING;
351
Alan Stern645daaa2006-08-30 15:47:02 -0400352 /* Autoresume for set_interface call below */
353 udev = interface_to_usbdev(intf);
Alan Stern94fcda12006-11-20 11:38:46 -0500354 error = usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -0400355
Alan Stern9da82bd2008-05-08 11:54:37 -0400356 /* Terminate all URBs for this interface unless the driver
357 * supports "soft" unbinding.
358 */
359 if (!driver->soft_unbind)
Alan Sternddeac4e72009-01-15 17:03:33 -0500360 usb_disable_interface(udev, intf, false);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800361
Alan Stern8bb54ab2006-07-01 22:08:49 -0400362 driver->disconnect(intf);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800363 usb_cancel_queued_reset(intf);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800364
Alan Stern55151d72008-08-12 14:33:59 -0400365 /* Reset other interface state.
366 * We cannot do a Set-Interface if the device is suspended or
367 * if it is prepared for a system sleep (since installing a new
368 * altsetting means creating new endpoint device entries).
369 * When either of these happens, defer the Set-Interface.
370 */
Alan Stern2caf7fc2008-12-31 11:31:33 -0500371 if (intf->cur_altsetting->desc.bAlternateSetting == 0) {
372 /* Already in altsetting 0 so skip Set-Interface.
373 * Just re-enable it without affecting the endpoint toggles.
374 */
375 usb_enable_interface(udev, intf, false);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200376 } else if (!error && intf->dev.power.status == DPM_ON) {
377 r = usb_set_interface(udev, intf->altsetting[0].
Alan Stern55151d72008-08-12 14:33:59 -0400378 desc.bInterfaceNumber, 0);
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200379 if (r < 0)
380 intf->needs_altsetting0 = 1;
381 } else {
Alan Stern55151d72008-08-12 14:33:59 -0400382 intf->needs_altsetting0 = 1;
Oliver Neukum1e5ea5e2009-08-27 16:46:56 +0200383 }
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800384 usb_set_intfdata(intf, NULL);
Alan Stern645daaa2006-08-30 15:47:02 -0400385
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800386 intf->condition = USB_INTERFACE_UNBOUND;
Alan Stern645daaa2006-08-30 15:47:02 -0400387 intf->needs_remote_wakeup = 0;
388
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500389 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
390 pm_runtime_disable(dev);
391 pm_runtime_set_suspended(dev);
392
393 /* Undo any residual pm_autopm_get_interface_* calls */
394 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
395 usb_autopm_put_interface_no_suspend(intf);
396 atomic_set(&intf->pm_usage_cnt, 0);
397
Alan Stern645daaa2006-08-30 15:47:02 -0400398 if (!error)
Alan Stern94fcda12006-11-20 11:38:46 -0500399 usb_autosuspend_device(udev);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800400
401 return 0;
402}
403
Alan Stern36e56a32006-07-01 22:08:06 -0400404/**
405 * usb_driver_claim_interface - bind a driver to an interface
406 * @driver: the driver to be bound
407 * @iface: the interface to which it will be bound; must be in the
408 * usb device's active configuration
409 * @priv: driver data associated with that interface
410 *
411 * This is used by usb device drivers that need to claim more than one
412 * interface on a device when probing (audio and acm are current examples).
413 * No device driver should directly modify internal usb_interface or
414 * usb_device structure members.
415 *
416 * Few drivers should need to use this routine, since the most natural
417 * way to bind to an interface is to return the private data from
418 * the driver's probe() method.
419 *
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400420 * Callers must own the device lock, so driver probe() entries don't need
421 * extra locking, but other call contexts may need to explicitly claim that
422 * lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400423 */
424int usb_driver_claim_interface(struct usb_driver *driver,
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800425 struct usb_interface *iface, void *priv)
Alan Stern36e56a32006-07-01 22:08:06 -0400426{
427 struct device *dev = &iface->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700428 int retval = 0;
Alan Stern36e56a32006-07-01 22:08:06 -0400429
430 if (dev->driver)
431 return -EBUSY;
432
Alan Stern8bb54ab2006-07-01 22:08:49 -0400433 dev->driver = &driver->drvwrap.driver;
Alan Stern36e56a32006-07-01 22:08:06 -0400434 usb_set_intfdata(iface, priv);
Alan Stern78d9a482008-06-23 16:00:40 -0400435 iface->needs_binding = 0;
Alan Stern645daaa2006-08-30 15:47:02 -0400436
Alan Stern36e56a32006-07-01 22:08:06 -0400437 iface->condition = USB_INTERFACE_BOUND;
Alan Stern9bbdf1e2010-01-08 12:57:28 -0500438
439 /* Bound interfaces are initially active. They are
440 * runtime-PM-enabled only if the driver has autosuspend support.
441 * They are sensitive to their children's power states.
442 */
443 pm_runtime_set_active(dev);
444 pm_suspend_ignore_children(dev, false);
445 if (driver->supports_autosuspend)
446 pm_runtime_enable(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400447
448 /* if interface was already added, bind now; else let
449 * the future device_add() bind it, bypassing probe()
450 */
451 if (device_is_registered(dev))
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700452 retval = device_bind_driver(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400453
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700454 return retval;
Alan Stern36e56a32006-07-01 22:08:06 -0400455}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600456EXPORT_SYMBOL_GPL(usb_driver_claim_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400457
458/**
459 * usb_driver_release_interface - unbind a driver from an interface
460 * @driver: the driver to be unbound
461 * @iface: the interface from which it will be unbound
462 *
463 * This can be used by drivers to release an interface without waiting
464 * for their disconnect() methods to be called. In typical cases this
465 * also causes the driver disconnect() method to be called.
466 *
467 * This call is synchronous, and may not be used in an interrupt context.
Greg Kroah-Hartman341487a82007-04-09 11:52:31 -0400468 * Callers must own the device lock, so driver disconnect() entries don't
469 * need extra locking, but other call contexts may need to explicitly claim
470 * that lock.
Alan Stern36e56a32006-07-01 22:08:06 -0400471 */
472void usb_driver_release_interface(struct usb_driver *driver,
473 struct usb_interface *iface)
474{
475 struct device *dev = &iface->dev;
476
477 /* this should never happen, don't release something that's not ours */
Alan Stern8bb54ab2006-07-01 22:08:49 -0400478 if (!dev->driver || dev->driver != &driver->drvwrap.driver)
Alan Stern36e56a32006-07-01 22:08:06 -0400479 return;
480
481 /* don't release from within disconnect() */
482 if (iface->condition != USB_INTERFACE_BOUND)
483 return;
Alan Stern91f8d062009-04-16 15:35:09 -0400484 iface->condition = USB_INTERFACE_UNBINDING;
Alan Stern36e56a32006-07-01 22:08:06 -0400485
Alan Stern91f8d062009-04-16 15:35:09 -0400486 /* Release via the driver core only if the interface
487 * has already been registered
488 */
Alan Stern36e56a32006-07-01 22:08:06 -0400489 if (device_is_registered(dev)) {
Alan Stern36e56a32006-07-01 22:08:06 -0400490 device_release_driver(dev);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -0800491 } else {
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800492 device_lock(dev);
Alan Stern91f8d062009-04-16 15:35:09 -0400493 usb_unbind_interface(dev);
494 dev->driver = NULL;
Greg Kroah-Hartman8e9394c2010-02-17 10:57:05 -0800495 device_unlock(dev);
Alan Stern36e56a32006-07-01 22:08:06 -0400496 }
Alan Stern36e56a32006-07-01 22:08:06 -0400497}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600498EXPORT_SYMBOL_GPL(usb_driver_release_interface);
Alan Stern36e56a32006-07-01 22:08:06 -0400499
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800500/* returns 0 if no match, 1 if match */
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100501int usb_match_device(struct usb_device *dev, const struct usb_device_id *id)
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800502{
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800503 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
504 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
505 return 0;
506
507 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
508 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
509 return 0;
510
511 /* No need to test id->bcdDevice_lo != 0, since 0 is never
512 greater than any unsigned number. */
513 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
514 (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
515 return 0;
516
517 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
518 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
519 return 0;
520
521 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
522 (id->bDeviceClass != dev->descriptor.bDeviceClass))
523 return 0;
524
525 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800526 (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800527 return 0;
528
529 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
530 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
531 return 0;
532
Greg Kroah-Hartmanbb417022007-01-26 14:26:21 +0100533 return 1;
534}
535
536/* returns 0 if no match, 1 if match */
537int usb_match_one_id(struct usb_interface *interface,
538 const struct usb_device_id *id)
539{
540 struct usb_host_interface *intf;
541 struct usb_device *dev;
542
543 /* proc_connectinfo in devio.c may call us with id == NULL. */
544 if (id == NULL)
545 return 0;
546
547 intf = interface->cur_altsetting;
548 dev = interface_to_usbdev(interface);
549
550 if (!usb_match_device(dev, id))
551 return 0;
552
Alan Stern93c8bf42006-10-18 16:41:51 -0400553 /* The interface class, subclass, and protocol should never be
554 * checked for a match if the device class is Vendor Specific,
555 * unless the match record specifies the Vendor ID. */
556 if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
557 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
558 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
559 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
560 USB_DEVICE_ID_MATCH_INT_PROTOCOL)))
561 return 0;
562
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800563 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
564 (id->bInterfaceClass != intf->desc.bInterfaceClass))
565 return 0;
566
567 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
568 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
569 return 0;
570
571 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
572 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
573 return 0;
574
575 return 1;
576}
Greg Kroah-Hartman93bacef2006-12-17 21:50:23 +0100577EXPORT_SYMBOL_GPL(usb_match_one_id);
578
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800579/**
580 * usb_match_id - find first usb_device_id matching device or interface
581 * @interface: the interface of interest
582 * @id: array of usb_device_id structures, terminated by zero entry
583 *
584 * usb_match_id searches an array of usb_device_id's and returns
585 * the first one matching the device or interface, or null.
586 * This is used when binding (or rebinding) a driver to an interface.
587 * Most USB device drivers will use this indirectly, through the usb core,
588 * but some layered driver frameworks use it directly.
589 * These device tables are exported with MODULE_DEVICE_TABLE, through
590 * modutils, to support the driver loading functionality of USB hotplugging.
591 *
592 * What Matches:
593 *
594 * The "match_flags" element in a usb_device_id controls which
595 * members are used. If the corresponding bit is set, the
596 * value in the device_id must match its corresponding member
597 * in the device or interface descriptor, or else the device_id
598 * does not match.
599 *
600 * "driver_info" is normally used only by device drivers,
601 * but you can create a wildcard "matches anything" usb_device_id
602 * as a driver's "modules.usbmap" entry if you provide an id with
603 * only a nonzero "driver_info" field. If you do this, the USB device
604 * driver's probe() routine should use additional intelligence to
605 * decide whether to bind to the specified interface.
606 *
607 * What Makes Good usb_device_id Tables:
608 *
609 * The match algorithm is very simple, so that intelligence in
610 * driver selection must come from smart driver id records.
611 * Unless you have good reasons to use another selection policy,
612 * provide match elements only in related groups, and order match
613 * specifiers from specific to general. Use the macros provided
614 * for that purpose if you can.
615 *
616 * The most specific match specifiers use device descriptor
617 * data. These are commonly used with product-specific matches;
618 * the USB_DEVICE macro lets you provide vendor and product IDs,
619 * and you can also match against ranges of product revisions.
620 * These are widely used for devices with application or vendor
621 * specific bDeviceClass values.
622 *
623 * Matches based on device class/subclass/protocol specifications
624 * are slightly more general; use the USB_DEVICE_INFO macro, or
625 * its siblings. These are used with single-function devices
626 * where bDeviceClass doesn't specify that each interface has
627 * its own class.
628 *
629 * Matches based on interface class/subclass/protocol are the
630 * most general; they let drivers bind to any interface on a
631 * multiple-function device. Use the USB_INTERFACE_INFO
632 * macro, or its siblings, to match class-per-interface style
Alan Stern93c8bf42006-10-18 16:41:51 -0400633 * devices (as recorded in bInterfaceClass).
634 *
635 * Note that an entry created by USB_INTERFACE_INFO won't match
636 * any interface if the device class is set to Vendor-Specific.
637 * This is deliberate; according to the USB spec the meanings of
638 * the interface class/subclass/protocol for these devices are also
639 * vendor-specific, and hence matching against a standard product
640 * class wouldn't work anyway. If you really want to use an
641 * interface-based match for such a device, create a match record
642 * that also specifies the vendor ID. (Unforunately there isn't a
643 * standard macro for creating records like this.)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800644 *
645 * Within those groups, remember that not all combinations are
646 * meaningful. For example, don't give a product version range
647 * without vendor and product IDs; or specify a protocol without
648 * its associated class and subclass.
649 */
650const struct usb_device_id *usb_match_id(struct usb_interface *interface,
651 const struct usb_device_id *id)
652{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800653 /* proc_connectinfo in devio.c may call us with id == NULL. */
654 if (id == NULL)
655 return NULL;
656
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800657 /* It is important to check that id->driver_info is nonzero,
658 since an entry that is all zeroes except for a nonzero
659 id->driver_info is the way to create an entry that
660 indicates that the driver want to examine every
661 device and interface. */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800662 for (; id->idVendor || id->idProduct || id->bDeviceClass ||
663 id->bInterfaceClass || id->driver_info; id++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800664 if (usb_match_one_id(interface, id))
665 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800666 }
667
668 return NULL;
669}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600670EXPORT_SYMBOL_GPL(usb_match_id);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800671
Adrian Bunk8bb22d22006-11-21 22:02:54 +0100672static int usb_device_match(struct device *dev, struct device_driver *drv)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800673{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400674 /* devices and interfaces are handled separately */
675 if (is_usb_device(dev)) {
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800676
Alan Stern8bb54ab2006-07-01 22:08:49 -0400677 /* interface drivers never match devices */
678 if (!is_usb_device_driver(drv))
679 return 0;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800680
Alan Stern8bb54ab2006-07-01 22:08:49 -0400681 /* TODO: Add real matching code */
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800682 return 1;
683
Kay Sievers55129662009-05-04 19:48:32 +0200684 } else if (is_usb_interface(dev)) {
Alan Stern8bb54ab2006-07-01 22:08:49 -0400685 struct usb_interface *intf;
686 struct usb_driver *usb_drv;
687 const struct usb_device_id *id;
688
689 /* device drivers never match interfaces */
690 if (is_usb_device_driver(drv))
691 return 0;
692
693 intf = to_usb_interface(dev);
694 usb_drv = to_usb_driver(drv);
695
696 id = usb_match_id(intf, usb_drv->id_table);
697 if (id)
698 return 1;
699
700 id = usb_match_dynamic_id(intf, usb_drv);
701 if (id)
702 return 1;
703 }
704
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800705 return 0;
706}
707
Alan Stern36e56a32006-07-01 22:08:06 -0400708#ifdef CONFIG_HOTPLUG
Kay Sievers7eff2e72007-08-14 15:15:12 +0200709static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400710{
Alan Stern36e56a32006-07-01 22:08:06 -0400711 struct usb_device *usb_dev;
Alan Stern36e56a32006-07-01 22:08:06 -0400712
Kay Sievers55129662009-05-04 19:48:32 +0200713 if (is_usb_device(dev)) {
Alan Stern782da722006-07-01 22:09:35 -0400714 usb_dev = to_usb_device(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200715 } else if (is_usb_interface(dev)) {
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100716 struct usb_interface *intf = to_usb_interface(dev);
Kay Sievers55129662009-05-04 19:48:32 +0200717
Alan Stern8bb54ab2006-07-01 22:08:49 -0400718 usb_dev = interface_to_usbdev(intf);
Kay Sievers55129662009-05-04 19:48:32 +0200719 } else {
720 return 0;
Alan Stern8bb54ab2006-07-01 22:08:49 -0400721 }
Alan Stern36e56a32006-07-01 22:08:06 -0400722
723 if (usb_dev->devnum < 0) {
Alan Sterncceffe92010-02-08 09:45:12 -0500724 /* driver is often null here; dev_dbg() would oops */
Kay Sievers7071a3c2008-05-02 06:02:41 +0200725 pr_debug("usb %s: already deleted?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400726 return -ENODEV;
727 }
728 if (!usb_dev->bus) {
Kay Sievers7071a3c2008-05-02 06:02:41 +0200729 pr_debug("usb %s: bus removed?\n", dev_name(dev));
Alan Stern36e56a32006-07-01 22:08:06 -0400730 return -ENODEV;
731 }
732
733#ifdef CONFIG_USB_DEVICEFS
734 /* If this is available, userspace programs can directly read
735 * all the device descriptors we don't tell them about. Or
Kay Sievers9f8b17e2007-03-13 15:59:31 +0100736 * act as usermode drivers.
Alan Stern36e56a32006-07-01 22:08:06 -0400737 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200738 if (add_uevent_var(env, "DEVICE=/proc/bus/usb/%03d/%03d",
Alan Stern36e56a32006-07-01 22:08:06 -0400739 usb_dev->bus->busnum, usb_dev->devnum))
740 return -ENOMEM;
741#endif
742
743 /* per-device configurations are common */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200744 if (add_uevent_var(env, "PRODUCT=%x/%x/%x",
Alan Stern36e56a32006-07-01 22:08:06 -0400745 le16_to_cpu(usb_dev->descriptor.idVendor),
746 le16_to_cpu(usb_dev->descriptor.idProduct),
747 le16_to_cpu(usb_dev->descriptor.bcdDevice)))
748 return -ENOMEM;
749
750 /* class-based driver binding models */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200751 if (add_uevent_var(env, "TYPE=%d/%d/%d",
Alan Stern36e56a32006-07-01 22:08:06 -0400752 usb_dev->descriptor.bDeviceClass,
753 usb_dev->descriptor.bDeviceSubClass,
754 usb_dev->descriptor.bDeviceProtocol))
755 return -ENOMEM;
756
Alan Stern36e56a32006-07-01 22:08:06 -0400757 return 0;
758}
759
760#else
761
Kay Sievers7eff2e72007-08-14 15:15:12 +0200762static int usb_uevent(struct device *dev, struct kobj_uevent_env *env)
Alan Stern36e56a32006-07-01 22:08:06 -0400763{
764 return -ENODEV;
765}
Alan Stern36e56a32006-07-01 22:08:06 -0400766#endif /* CONFIG_HOTPLUG */
767
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800768/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400769 * usb_register_device_driver - register a USB device (not interface) driver
770 * @new_udriver: USB operations for the device driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800771 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800772 *
Alan Stern8bb54ab2006-07-01 22:08:49 -0400773 * Registers a USB device driver with the USB core. The list of
774 * unattached devices will be rescanned whenever a new driver is
775 * added, allowing the new driver to attach to any recognized devices.
776 * Returns a negative error code on failure and 0 on success.
777 */
778int usb_register_device_driver(struct usb_device_driver *new_udriver,
779 struct module *owner)
780{
781 int retval = 0;
782
783 if (usb_disabled())
784 return -ENODEV;
785
786 new_udriver->drvwrap.for_devices = 1;
787 new_udriver->drvwrap.driver.name = (char *) new_udriver->name;
788 new_udriver->drvwrap.driver.bus = &usb_bus_type;
789 new_udriver->drvwrap.driver.probe = usb_probe_device;
790 new_udriver->drvwrap.driver.remove = usb_unbind_device;
791 new_udriver->drvwrap.driver.owner = owner;
792
793 retval = driver_register(&new_udriver->drvwrap.driver);
794
795 if (!retval) {
796 pr_info("%s: registered new device driver %s\n",
797 usbcore_name, new_udriver->name);
798 usbfs_update_special();
799 } else {
800 printk(KERN_ERR "%s: error %d registering device "
801 " driver %s\n",
802 usbcore_name, retval, new_udriver->name);
803 }
804
805 return retval;
806}
807EXPORT_SYMBOL_GPL(usb_register_device_driver);
808
809/**
810 * usb_deregister_device_driver - unregister a USB device (not interface) driver
811 * @udriver: USB operations of the device driver to unregister
812 * Context: must be able to sleep
813 *
814 * Unlinks the specified driver from the internal USB driver list.
815 */
816void usb_deregister_device_driver(struct usb_device_driver *udriver)
817{
818 pr_info("%s: deregistering device driver %s\n",
819 usbcore_name, udriver->name);
820
821 driver_unregister(&udriver->drvwrap.driver);
822 usbfs_update_special();
823}
824EXPORT_SYMBOL_GPL(usb_deregister_device_driver);
825
826/**
827 * usb_register_driver - register a USB interface driver
828 * @new_driver: USB operations for the interface driver
829 * @owner: module owner of this driver.
Randy Dunlap892705a2007-02-10 14:41:41 -0800830 * @mod_name: module name string
Alan Stern8bb54ab2006-07-01 22:08:49 -0400831 *
832 * Registers a USB interface driver with the USB core. The list of
833 * unattached interfaces will be rescanned whenever a new driver is
834 * added, allowing the new driver to attach to any recognized interfaces.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800835 * Returns a negative error code on failure and 0 on success.
836 *
837 * NOTE: if you want your driver to use the USB major number, you must call
838 * usb_register_dev() to enable that functionality. This function no longer
839 * takes care of that.
840 */
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800841int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
842 const char *mod_name)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800843{
844 int retval = 0;
845
846 if (usb_disabled())
847 return -ENODEV;
848
Alan Stern8bb54ab2006-07-01 22:08:49 -0400849 new_driver->drvwrap.for_devices = 0;
850 new_driver->drvwrap.driver.name = (char *) new_driver->name;
851 new_driver->drvwrap.driver.bus = &usb_bus_type;
852 new_driver->drvwrap.driver.probe = usb_probe_interface;
853 new_driver->drvwrap.driver.remove = usb_unbind_interface;
854 new_driver->drvwrap.driver.owner = owner;
Greg Kroah-Hartman80f745f2007-01-15 11:50:02 -0800855 new_driver->drvwrap.driver.mod_name = mod_name;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800856 spin_lock_init(&new_driver->dynids.lock);
857 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800858
Alan Stern8bb54ab2006-07-01 22:08:49 -0400859 retval = driver_register(&new_driver->drvwrap.driver);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800860 if (retval)
861 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800862
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800863 usbfs_update_special();
864
865 retval = usb_create_newid_file(new_driver);
866 if (retval)
867 goto out_newid;
868
869 retval = usb_create_removeid_file(new_driver);
870 if (retval)
871 goto out_removeid;
872
873 pr_info("%s: registered new interface driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800874 usbcore_name, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800875
876out:
877 return retval;
878
879out_removeid:
880 usb_remove_newid_file(new_driver);
881out_newid:
882 driver_unregister(&new_driver->drvwrap.driver);
883
884 printk(KERN_ERR "%s: error %d registering interface "
Alan Stern8bb54ab2006-07-01 22:08:49 -0400885 " driver %s\n",
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800886 usbcore_name, retval, new_driver->name);
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800887 goto out;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800888}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600889EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800890
891/**
Alan Stern8bb54ab2006-07-01 22:08:49 -0400892 * usb_deregister - unregister a USB interface driver
893 * @driver: USB operations of the interface driver to unregister
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800894 * Context: must be able to sleep
895 *
896 * Unlinks the specified driver from the internal USB driver list.
897 *
898 * NOTE: If you called usb_register_dev(), you still need to call
899 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
900 * this * call will no longer do it for you.
901 */
902void usb_deregister(struct usb_driver *driver)
903{
Alan Stern8bb54ab2006-07-01 22:08:49 -0400904 pr_info("%s: deregistering interface driver %s\n",
905 usbcore_name, driver->name);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800906
CHENG Renquan0c7a2b72009-11-22 01:28:52 +0800907 usb_remove_removeid_file(driver);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800908 usb_remove_newid_file(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800909 usb_free_dynids(driver);
Alan Stern8bb54ab2006-07-01 22:08:49 -0400910 driver_unregister(&driver->drvwrap.driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800911
912 usbfs_update_special();
913}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600914EXPORT_SYMBOL_GPL(usb_deregister);
Alan Stern36e56a32006-07-01 22:08:06 -0400915
Alan Stern78d9a482008-06-23 16:00:40 -0400916/* Forced unbinding of a USB interface driver, either because
917 * it doesn't support pre_reset/post_reset/reset_resume or
918 * because it doesn't support suspend/resume.
919 *
920 * The caller must hold @intf's device's lock, but not its pm_mutex
921 * and not @intf->dev.sem.
922 */
923void usb_forced_unbind_intf(struct usb_interface *intf)
924{
925 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
926
927 dev_dbg(&intf->dev, "forced unbind\n");
928 usb_driver_release_interface(driver, intf);
929
930 /* Mark the interface for later rebinding */
931 intf->needs_binding = 1;
932}
933
934/* Delayed forced unbinding of a USB interface driver and scan
935 * for rebinding.
936 *
937 * The caller must hold @intf's device's lock, but not its pm_mutex
938 * and not @intf->dev.sem.
939 *
Alan Stern5096aed2008-08-12 14:34:14 -0400940 * Note: Rebinds will be skipped if a system sleep transition is in
941 * progress and the PM "complete" callback hasn't occurred yet.
Alan Stern78d9a482008-06-23 16:00:40 -0400942 */
943void usb_rebind_intf(struct usb_interface *intf)
944{
945 int rc;
946
947 /* Delayed unbind of an existing driver */
948 if (intf->dev.driver) {
949 struct usb_driver *driver =
950 to_usb_driver(intf->dev.driver);
951
952 dev_dbg(&intf->dev, "forced unbind\n");
953 usb_driver_release_interface(driver, intf);
954 }
955
956 /* Try to rebind the interface */
Alan Stern5096aed2008-08-12 14:34:14 -0400957 if (intf->dev.power.status == DPM_ON) {
958 intf->needs_binding = 0;
959 rc = device_attach(&intf->dev);
960 if (rc < 0)
961 dev_warn(&intf->dev, "rebind failed: %d\n", rc);
962 }
Alan Stern78d9a482008-06-23 16:00:40 -0400963}
964
Alan Stern9ff78432008-08-07 13:04:51 -0400965#ifdef CONFIG_PM
966
Alan Stern78d9a482008-06-23 16:00:40 -0400967#define DO_UNBIND 0
968#define DO_REBIND 1
969
970/* Unbind drivers for @udev's interfaces that don't support suspend/resume,
971 * or rebind interfaces that have been unbound, according to @action.
972 *
973 * The caller must hold @udev's device lock.
Alan Stern78d9a482008-06-23 16:00:40 -0400974 */
975static void do_unbind_rebind(struct usb_device *udev, int action)
976{
977 struct usb_host_config *config;
978 int i;
979 struct usb_interface *intf;
980 struct usb_driver *drv;
981
982 config = udev->actconfig;
983 if (config) {
984 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
985 intf = config->interface[i];
986 switch (action) {
987 case DO_UNBIND:
988 if (intf->dev.driver) {
989 drv = to_usb_driver(intf->dev.driver);
990 if (!drv->suspend || !drv->resume)
991 usb_forced_unbind_intf(intf);
992 }
993 break;
994 case DO_REBIND:
Alan Stern5096aed2008-08-12 14:34:14 -0400995 if (intf->needs_binding)
Alan Stern78d9a482008-06-23 16:00:40 -0400996 usb_rebind_intf(intf);
Alan Stern78d9a482008-06-23 16:00:40 -0400997 break;
998 }
999 }
1000 }
1001}
1002
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001003static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
Alan Stern36e56a32006-07-01 22:08:06 -04001004{
Alan Stern782da722006-07-01 22:09:35 -04001005 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001006 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001007
Alan Stern114b3682006-07-01 22:13:04 -04001008 if (udev->state == USB_STATE_NOTATTACHED ||
1009 udev->state == USB_STATE_SUSPENDED)
1010 goto done;
1011
Alan Sternb6f64362007-05-04 11:51:54 -04001012 /* For devices that don't have a driver, we do a generic suspend. */
1013 if (udev->dev.driver)
1014 udriver = to_usb_device_driver(udev->dev.driver);
1015 else {
Alan Stern645daaa2006-08-30 15:47:02 -04001016 udev->do_remote_wakeup = 0;
Alan Sternb6f64362007-05-04 11:51:54 -04001017 udriver = &usb_generic_driver;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001018 }
Alan Stern2bf40862006-07-01 22:12:19 -04001019 status = udriver->suspend(udev, msg);
1020
Alan Stern20dfdad2007-05-22 11:50:17 -04001021 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001022 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001023 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001024}
Alan Stern36e56a32006-07-01 22:08:06 -04001025
Alan Stern65bfd292008-11-25 16:39:18 -05001026static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001027{
1028 struct usb_device_driver *udriver;
Alan Stern2bf40862006-07-01 22:12:19 -04001029 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001030
Alan Stern0458d5b2007-05-04 11:52:20 -04001031 if (udev->state == USB_STATE_NOTATTACHED)
1032 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001033
Alan Stern1c5df7e2006-07-01 22:13:50 -04001034 /* Can't resume it if it doesn't have a driver. */
1035 if (udev->dev.driver == NULL) {
1036 status = -ENOTCONN;
Alan Stern2bf40862006-07-01 22:12:19 -04001037 goto done;
Alan Stern1c5df7e2006-07-01 22:13:50 -04001038 }
1039
Alan Stern6d19c002010-02-12 12:21:11 +01001040 /* Non-root devices on a full/low-speed bus must wait for their
1041 * companion high-speed root hub, in case a handoff is needed.
1042 */
1043 if (!(msg.event & PM_EVENT_AUTO) && udev->parent &&
1044 udev->bus->hs_companion)
1045 device_pm_wait_for_dev(&udev->dev,
1046 &udev->bus->hs_companion->root_hub->dev);
1047
Alan Stern6bc6cff2007-05-04 11:53:03 -04001048 if (udev->quirks & USB_QUIRK_RESET_RESUME)
1049 udev->reset_resume = 1;
1050
Alan Stern1cc8a252006-07-01 22:10:15 -04001051 udriver = to_usb_device_driver(udev->dev.driver);
Alan Stern65bfd292008-11-25 16:39:18 -05001052 status = udriver->resume(udev, msg);
Alan Stern2bf40862006-07-01 22:12:19 -04001053
Alan Stern20dfdad2007-05-22 11:50:17 -04001054 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001055 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern2bf40862006-07-01 22:12:19 -04001056 return status;
Alan Stern1cc8a252006-07-01 22:10:15 -04001057}
1058
Alan Stern65605ae2008-08-12 14:33:27 -04001059static int usb_suspend_interface(struct usb_device *udev,
1060 struct usb_interface *intf, pm_message_t msg)
Alan Stern1cc8a252006-07-01 22:10:15 -04001061{
1062 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001063 int status = 0;
Alan Stern1cc8a252006-07-01 22:10:15 -04001064
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001065 if (udev->state == USB_STATE_NOTATTACHED ||
1066 intf->condition == USB_INTERFACE_UNBOUND)
Alan Stern2bf40862006-07-01 22:12:19 -04001067 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001068 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001069
Alan Stern78d9a482008-06-23 16:00:40 -04001070 if (driver->suspend) {
Alan Stern1cc8a252006-07-01 22:10:15 -04001071 status = driver->suspend(intf, msg);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001072 if (status && !(msg.event & PM_EVENT_AUTO))
Alan Stern1cc8a252006-07-01 22:10:15 -04001073 dev_err(&intf->dev, "%s error %d\n",
1074 "suspend", status);
Alan Stern36e56a32006-07-01 22:08:06 -04001075 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001076 /* Later we will unbind the driver and reprobe */
1077 intf->needs_binding = 1;
1078 dev_warn(&intf->dev, "no %s for driver %s?\n",
1079 "suspend", driver->name);
Alan Stern36e56a32006-07-01 22:08:06 -04001080 }
Alan Stern2bf40862006-07-01 22:12:19 -04001081
Alan Stern20dfdad2007-05-22 11:50:17 -04001082 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001083 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Stern36e56a32006-07-01 22:08:06 -04001084 return status;
1085}
1086
Alan Stern65605ae2008-08-12 14:33:27 -04001087static int usb_resume_interface(struct usb_device *udev,
Alan Stern65bfd292008-11-25 16:39:18 -05001088 struct usb_interface *intf, pm_message_t msg, int reset_resume)
Alan Stern36e56a32006-07-01 22:08:06 -04001089{
Alan Stern1cc8a252006-07-01 22:10:15 -04001090 struct usb_driver *driver;
Alan Stern2bf40862006-07-01 22:12:19 -04001091 int status = 0;
Alan Stern36e56a32006-07-01 22:08:06 -04001092
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001093 if (udev->state == USB_STATE_NOTATTACHED)
Alan Stern2bf40862006-07-01 22:12:19 -04001094 goto done;
Alan Stern36e56a32006-07-01 22:08:06 -04001095
Alan Stern645daaa2006-08-30 15:47:02 -04001096 /* Don't let autoresume interfere with unbinding */
1097 if (intf->condition == USB_INTERFACE_UNBINDING)
1098 goto done;
1099
Alan Stern1c5df7e2006-07-01 22:13:50 -04001100 /* Can't resume it if it doesn't have a driver. */
Alan Stern55151d72008-08-12 14:33:59 -04001101 if (intf->condition == USB_INTERFACE_UNBOUND) {
1102
1103 /* Carry out a deferred switch to altsetting 0 */
1104 if (intf->needs_altsetting0 &&
1105 intf->dev.power.status == DPM_ON) {
1106 usb_set_interface(udev, intf->altsetting[0].
1107 desc.bInterfaceNumber, 0);
1108 intf->needs_altsetting0 = 0;
1109 }
Alan Stern2bf40862006-07-01 22:12:19 -04001110 goto done;
Alan Stern55151d72008-08-12 14:33:59 -04001111 }
Alan Stern78d9a482008-06-23 16:00:40 -04001112
1113 /* Don't resume if the interface is marked for rebinding */
1114 if (intf->needs_binding)
1115 goto done;
Alan Stern1cc8a252006-07-01 22:10:15 -04001116 driver = to_usb_driver(intf->dev.driver);
Alan Stern36e56a32006-07-01 22:08:06 -04001117
Alan Sternf07600c2007-05-30 15:38:16 -04001118 if (reset_resume) {
1119 if (driver->reset_resume) {
1120 status = driver->reset_resume(intf);
1121 if (status)
1122 dev_err(&intf->dev, "%s error %d\n",
1123 "reset_resume", status);
1124 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001125 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001126 dev_warn(&intf->dev, "no %s for driver %s?\n",
1127 "reset_resume", driver->name);
1128 }
1129 } else {
1130 if (driver->resume) {
1131 status = driver->resume(intf);
1132 if (status)
1133 dev_err(&intf->dev, "%s error %d\n",
1134 "resume", status);
1135 } else {
Alan Stern78d9a482008-06-23 16:00:40 -04001136 intf->needs_binding = 1;
Alan Sternf07600c2007-05-30 15:38:16 -04001137 dev_warn(&intf->dev, "no %s for driver %s?\n",
1138 "resume", driver->name);
1139 }
1140 }
Alan Stern2bf40862006-07-01 22:12:19 -04001141
1142done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001143 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
Alan Sternf07600c2007-05-30 15:38:16 -04001144
Alan Stern78d9a482008-06-23 16:00:40 -04001145 /* Later we will unbind the driver and/or reprobe, if necessary */
Alan Stern2bf40862006-07-01 22:12:19 -04001146 return status;
Alan Stern36e56a32006-07-01 22:08:06 -04001147}
1148
Alan Stern645daaa2006-08-30 15:47:02 -04001149/**
1150 * usb_suspend_both - suspend a USB device and its interfaces
1151 * @udev: the usb_device to suspend
1152 * @msg: Power Management message describing this state transition
1153 *
1154 * This is the central routine for suspending USB devices. It calls the
1155 * suspend methods for all the interface drivers in @udev and then calls
1156 * the suspend method for @udev itself. If an error occurs at any stage,
1157 * all the interfaces which were suspended are resumed so that they remain
1158 * in the same state as the device.
1159 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001160 * Autosuspend requests originating from a child device or an interface
1161 * driver may be made without the protection of @udev's device lock, but
1162 * all other suspend calls will hold the lock. Usbcore will insure that
1163 * method calls do not arrive during bind, unbind, or reset operations.
1164 * However drivers must be prepared to handle suspend calls arriving at
1165 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001166 *
1167 * This routine can run only in process context.
1168 */
Alan Stern718efa62007-03-09 15:41:13 -05001169static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001170{
1171 int status = 0;
1172 int i = 0;
1173 struct usb_interface *intf;
1174
Alan Stern19410442007-03-27 13:33:59 -04001175 if (udev->state == USB_STATE_NOTATTACHED ||
1176 udev->state == USB_STATE_SUSPENDED)
1177 goto done;
Alan Stern645daaa2006-08-30 15:47:02 -04001178
Alan Stern645daaa2006-08-30 15:47:02 -04001179 /* Suspend all the interfaces and then udev itself */
Alan Sterna8e7c562006-07-01 22:11:02 -04001180 if (udev->actconfig) {
1181 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
1182 intf = udev->actconfig->interface[i];
Alan Stern65605ae2008-08-12 14:33:27 -04001183 status = usb_suspend_interface(udev, intf, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001184 if (status != 0)
1185 break;
1186 }
1187 }
Alan Stern5ad4f712007-09-10 11:31:43 -04001188 if (status == 0)
Stephen Hemmingerd5ec1682006-11-14 10:06:17 -08001189 status = usb_suspend_device(udev, msg);
Alan Sterna8e7c562006-07-01 22:11:02 -04001190
1191 /* If the suspend failed, resume interfaces that did get suspended */
1192 if (status != 0) {
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001193 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
Alan Sterna8e7c562006-07-01 22:11:02 -04001194 while (--i >= 0) {
1195 intf = udev->actconfig->interface[i];
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001196 usb_resume_interface(udev, intf, msg, 0);
Alan Sterna8e7c562006-07-01 22:11:02 -04001197 }
Alan Stern645daaa2006-08-30 15:47:02 -04001198
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001199 /* If the suspend succeeded then prevent any more URB submissions
1200 * and flush any outstanding URBs.
Alan Stern6840d252007-09-10 11:34:26 -04001201 */
Alan Sternef7f6c72007-04-05 16:03:49 -04001202 } else {
Alan Stern6840d252007-09-10 11:34:26 -04001203 udev->can_submit = 0;
1204 for (i = 0; i < 16; ++i) {
1205 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1206 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1207 }
Alan Sternef7f6c72007-04-05 16:03:49 -04001208 }
Alan Stern645daaa2006-08-30 15:47:02 -04001209
Alan Stern19410442007-03-27 13:33:59 -04001210 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001211 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Sterna8e7c562006-07-01 22:11:02 -04001212 return status;
1213}
1214
Alan Stern645daaa2006-08-30 15:47:02 -04001215/**
1216 * usb_resume_both - resume a USB device and its interfaces
1217 * @udev: the usb_device to resume
Alan Stern65bfd292008-11-25 16:39:18 -05001218 * @msg: Power Management message describing this state transition
Alan Stern645daaa2006-08-30 15:47:02 -04001219 *
1220 * This is the central routine for resuming USB devices. It calls the
1221 * the resume method for @udev and then calls the resume methods for all
1222 * the interface drivers in @udev.
1223 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001224 * Autoresume requests originating from a child device or an interface
1225 * driver may be made without the protection of @udev's device lock, but
1226 * all other resume calls will hold the lock. Usbcore will insure that
1227 * method calls do not arrive during bind, unbind, or reset operations.
1228 * However drivers must be prepared to handle resume calls arriving at
1229 * unpredictable times.
Alan Stern645daaa2006-08-30 15:47:02 -04001230 *
1231 * This routine can run only in process context.
1232 */
Alan Stern65bfd292008-11-25 16:39:18 -05001233static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
Alan Sterna8e7c562006-07-01 22:11:02 -04001234{
Alan Stern645daaa2006-08-30 15:47:02 -04001235 int status = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001236 int i;
1237 struct usb_interface *intf;
1238
Alan Stern19410442007-03-27 13:33:59 -04001239 if (udev->state == USB_STATE_NOTATTACHED) {
1240 status = -ENODEV;
1241 goto done;
1242 }
Alan Stern6840d252007-09-10 11:34:26 -04001243 udev->can_submit = 1;
Alan Stern645daaa2006-08-30 15:47:02 -04001244
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001245 /* Resume the device */
1246 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
Alan Stern65bfd292008-11-25 16:39:18 -05001247 status = usb_resume_device(udev, msg);
Alan Stern114b3682006-07-01 22:13:04 -04001248
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001249 /* Resume the interfaces */
Alan Sterna8e7c562006-07-01 22:11:02 -04001250 if (status == 0 && udev->actconfig) {
1251 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1252 intf = udev->actconfig->interface[i];
Alan Stern65bfd292008-11-25 16:39:18 -05001253 usb_resume_interface(udev, intf, msg,
1254 udev->reset_resume);
Alan Sterna8e7c562006-07-01 22:11:02 -04001255 }
1256 }
Alan Stern645daaa2006-08-30 15:47:02 -04001257
Alan Stern19410442007-03-27 13:33:59 -04001258 done:
Harvey Harrison441b62c2008-03-03 16:08:34 -08001259 dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status);
Alan Stern70a1c9e2008-03-06 17:00:58 -05001260 if (!status)
1261 udev->reset_resume = 0;
Alan Sterna8e7c562006-07-01 22:11:02 -04001262 return status;
1263}
1264
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001265/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001266int usb_suspend(struct device *dev, pm_message_t msg)
1267{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001268 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001269
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001270 do_unbind_rebind(udev, DO_UNBIND);
1271 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1272 return usb_suspend_both(udev, msg);
Alan Stern0c590e22010-01-08 12:57:14 -05001273}
1274
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001275/* The device lock is held by the PM core */
Alan Stern0c590e22010-01-08 12:57:14 -05001276int usb_resume(struct device *dev, pm_message_t msg)
1277{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001278 struct usb_device *udev = to_usb_device(dev);
Alan Stern0c590e22010-01-08 12:57:14 -05001279 int status;
1280
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001281 /* For PM complete calls, all we do is rebind interfaces */
1282 if (msg.event == PM_EVENT_ON) {
1283 if (udev->state != USB_STATE_NOTATTACHED)
1284 do_unbind_rebind(udev, DO_REBIND);
1285 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001286
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001287 /* For all other calls, take the device back to full power and
1288 * tell the PM core in case it was autosuspended previously.
Alan Stern0c590e22010-01-08 12:57:14 -05001289 */
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001290 } else {
1291 status = usb_resume_both(udev, msg);
1292 if (status == 0) {
1293 pm_runtime_disable(dev);
1294 pm_runtime_set_active(dev);
1295 pm_runtime_enable(dev);
1296 udev->last_busy = jiffies;
1297 }
1298 }
Alan Stern0c590e22010-01-08 12:57:14 -05001299
1300 /* Avoid PM error messages for devices disconnected while suspended
1301 * as we'll display regular disconnect messages just a bit later.
1302 */
1303 if (status == -ENODEV)
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001304 status = 0;
Alan Stern0c590e22010-01-08 12:57:14 -05001305 return status;
1306}
1307
1308#endif /* CONFIG_PM */
1309
Alan Stern645daaa2006-08-30 15:47:02 -04001310#ifdef CONFIG_USB_SUSPEND
1311
Alan Stern088f7fe2010-01-08 12:56:54 -05001312/**
1313 * usb_enable_autosuspend - allow a USB device to be autosuspended
1314 * @udev: the USB device which may be autosuspended
1315 *
1316 * This routine allows @udev to be autosuspended. An autosuspend won't
1317 * take place until the autosuspend_delay has elapsed and all the other
1318 * necessary conditions are satisfied.
1319 *
1320 * The caller must hold @udev's device lock.
1321 */
1322int usb_enable_autosuspend(struct usb_device *udev)
1323{
1324 if (udev->autosuspend_disabled) {
1325 udev->autosuspend_disabled = 0;
1326 usb_autosuspend_device(udev);
1327 }
1328 return 0;
1329}
1330EXPORT_SYMBOL_GPL(usb_enable_autosuspend);
1331
1332/**
1333 * usb_disable_autosuspend - prevent a USB device from being autosuspended
1334 * @udev: the USB device which may not be autosuspended
1335 *
1336 * This routine prevents @udev from being autosuspended and wakes it up
1337 * if it is already autosuspended.
1338 *
1339 * The caller must hold @udev's device lock.
1340 */
1341int usb_disable_autosuspend(struct usb_device *udev)
1342{
1343 int rc = 0;
1344
1345 if (!udev->autosuspend_disabled) {
1346 rc = usb_autoresume_device(udev);
1347 if (rc == 0)
1348 udev->autosuspend_disabled = 1;
1349 }
1350 return rc;
1351}
1352EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1353
Alan Stern645daaa2006-08-30 15:47:02 -04001354/**
1355 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001356 * @udev: the usb_device to autosuspend
Alan Stern645daaa2006-08-30 15:47:02 -04001357 *
1358 * This routine should be called when a core subsystem is finished using
1359 * @udev and wants to allow it to autosuspend. Examples would be when
1360 * @udev's device file in usbfs is closed or after a configuration change.
1361 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001362 * @udev's usage counter is decremented; if it drops to 0 and all the
1363 * interfaces are inactive then a delayed autosuspend will be attempted.
1364 * The attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001365 *
Alan Stern62e299e2010-01-08 12:56:19 -05001366 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001367 *
1368 * This routine can run only in process context.
1369 */
Alan Stern94fcda12006-11-20 11:38:46 -05001370void usb_autosuspend_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001371{
Alan Stern94fcda12006-11-20 11:38:46 -05001372 int status;
1373
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001374 udev->last_busy = jiffies;
1375 status = pm_runtime_put_sync(&udev->dev);
1376 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1377 __func__, atomic_read(&udev->dev.power.usage_count),
1378 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001379}
1380
1381/**
Alan Stern19c26232007-02-20 15:03:32 -05001382 * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces
1383 * @udev: the usb_device to autosuspend
1384 *
1385 * This routine should be called when a core subsystem thinks @udev may
1386 * be ready to autosuspend.
1387 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001388 * @udev's usage counter left unchanged. If it is 0 and all the interfaces
1389 * are inactive then an autosuspend will be attempted. The attempt may
1390 * fail or be delayed.
Alan Stern19c26232007-02-20 15:03:32 -05001391 *
Alan Stern62e299e2010-01-08 12:56:19 -05001392 * The caller must hold @udev's device lock.
1393 *
Alan Stern19c26232007-02-20 15:03:32 -05001394 * This routine can run only in process context.
1395 */
1396void usb_try_autosuspend_device(struct usb_device *udev)
1397{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001398 int status;
1399
1400 status = pm_runtime_idle(&udev->dev);
1401 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1402 __func__, atomic_read(&udev->dev.power.usage_count),
1403 status);
Alan Stern19c26232007-02-20 15:03:32 -05001404}
1405
1406/**
Alan Stern645daaa2006-08-30 15:47:02 -04001407 * usb_autoresume_device - immediately autoresume a USB device and its interfaces
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001408 * @udev: the usb_device to autoresume
Alan Stern645daaa2006-08-30 15:47:02 -04001409 *
1410 * This routine should be called when a core subsystem wants to use @udev
Alan Stern94fcda12006-11-20 11:38:46 -05001411 * and needs to guarantee that it is not suspended. No autosuspend will
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001412 * occur until usb_autosuspend_device() is called. (Note that this will
1413 * not prevent suspend events originating in the PM core.) Examples would
1414 * be when @udev's device file in usbfs is opened or when a remote-wakeup
Alan Stern94fcda12006-11-20 11:38:46 -05001415 * request is received.
Alan Stern645daaa2006-08-30 15:47:02 -04001416 *
Alan Stern94fcda12006-11-20 11:38:46 -05001417 * @udev's usage counter is incremented to prevent subsequent autosuspends.
1418 * However if the autoresume fails then the usage counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001419 *
Alan Stern62e299e2010-01-08 12:56:19 -05001420 * The caller must hold @udev's device lock.
Alan Stern645daaa2006-08-30 15:47:02 -04001421 *
1422 * This routine can run only in process context.
1423 */
Alan Stern94fcda12006-11-20 11:38:46 -05001424int usb_autoresume_device(struct usb_device *udev)
Alan Stern645daaa2006-08-30 15:47:02 -04001425{
1426 int status;
1427
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001428 status = pm_runtime_get_sync(&udev->dev);
1429 if (status < 0)
1430 pm_runtime_put_sync(&udev->dev);
1431 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1432 __func__, atomic_read(&udev->dev.power.usage_count),
1433 status);
1434 if (status > 0)
1435 status = 0;
Alan Sternaf4f7602006-10-30 17:06:45 -05001436 return status;
1437}
1438
Alan Stern645daaa2006-08-30 15:47:02 -04001439/**
1440 * usb_autopm_put_interface - decrement a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001441 * @intf: the usb_interface whose counter should be decremented
Alan Stern645daaa2006-08-30 15:47:02 -04001442 *
1443 * This routine should be called by an interface driver when it is
1444 * finished using @intf and wants to allow it to autosuspend. A typical
1445 * example would be a character-device driver when its device file is
1446 * closed.
1447 *
1448 * The routine decrements @intf's usage counter. When the counter reaches
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001449 * 0, a delayed autosuspend request for @intf's device is attempted. The
1450 * attempt may fail (see autosuspend_check()).
Alan Stern645daaa2006-08-30 15:47:02 -04001451 *
1452 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1453 * take place only if the device's remote-wakeup facility is enabled.
1454 *
Alan Stern645daaa2006-08-30 15:47:02 -04001455 * This routine can run only in process context.
1456 */
1457void usb_autopm_put_interface(struct usb_interface *intf)
1458{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001459 struct usb_device *udev = interface_to_usbdev(intf);
1460 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001461
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001462 udev->last_busy = jiffies;
1463 atomic_dec(&intf->pm_usage_cnt);
1464 status = pm_runtime_put_sync(&intf->dev);
1465 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1466 __func__, atomic_read(&intf->dev.power.usage_count),
1467 status);
Alan Stern645daaa2006-08-30 15:47:02 -04001468}
1469EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1470
1471/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001472 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1473 * @intf: the usb_interface whose counter should be decremented
1474 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001475 * This routine does much the same thing as usb_autopm_put_interface():
1476 * It decrements @intf's usage counter and schedules a delayed
1477 * autosuspend request if the counter is <= 0. The difference is that it
1478 * does not perform any synchronization; callers should hold a private
1479 * lock and handle all synchronization issues themselves.
Alan Stern9ac39f22008-11-12 16:19:49 -05001480 *
1481 * Typically a driver would call this routine during an URB's completion
1482 * handler, if no more URBs were pending.
1483 *
1484 * This routine can run in atomic context.
1485 */
1486void usb_autopm_put_interface_async(struct usb_interface *intf)
1487{
1488 struct usb_device *udev = interface_to_usbdev(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001489 unsigned long last_busy;
Alan Stern9ac39f22008-11-12 16:19:49 -05001490 int status = 0;
1491
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001492 last_busy = udev->last_busy;
1493 udev->last_busy = jiffies;
1494 atomic_dec(&intf->pm_usage_cnt);
1495 pm_runtime_put_noidle(&intf->dev);
1496
1497 if (!udev->autosuspend_disabled) {
1498 /* Optimization: Don't schedule a delayed autosuspend if
1499 * the timer is already running and the expiration time
1500 * wouldn't change.
1501 *
1502 * We have to use the interface's timer. Attempts to
1503 * schedule a suspend for the device would fail because
1504 * the interface is still active.
1505 */
1506 if (intf->dev.power.timer_expires == 0 ||
1507 round_jiffies_up(last_busy) !=
1508 round_jiffies_up(jiffies)) {
1509 status = pm_schedule_suspend(&intf->dev,
1510 jiffies_to_msecs(
Alan Stern4ec06d62008-11-25 16:40:02 -05001511 round_jiffies_up_relative(
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001512 udev->autosuspend_delay)));
Alan Stern9ac39f22008-11-12 16:19:49 -05001513 }
1514 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001515 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1516 __func__, atomic_read(&intf->dev.power.usage_count),
1517 status);
Alan Stern9ac39f22008-11-12 16:19:49 -05001518}
1519EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1520
1521/**
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001522 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1523 * @intf: the usb_interface whose counter should be decremented
1524 *
1525 * This routine decrements @intf's usage counter but does not carry out an
1526 * autosuspend.
1527 *
1528 * This routine can run in atomic context.
1529 */
1530void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1531{
1532 struct usb_device *udev = interface_to_usbdev(intf);
1533
1534 udev->last_busy = jiffies;
1535 atomic_dec(&intf->pm_usage_cnt);
1536 pm_runtime_put_noidle(&intf->dev);
1537}
1538EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1539
1540/**
Alan Stern645daaa2006-08-30 15:47:02 -04001541 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
Henrik Kretzschmar701f35a2006-09-25 17:00:56 -07001542 * @intf: the usb_interface whose counter should be incremented
Alan Stern645daaa2006-08-30 15:47:02 -04001543 *
1544 * This routine should be called by an interface driver when it wants to
1545 * use @intf and needs to guarantee that it is not suspended. In addition,
1546 * the routine prevents @intf from being autosuspended subsequently. (Note
1547 * that this will not prevent suspend events originating in the PM core.)
1548 * This prevention will persist until usb_autopm_put_interface() is called
1549 * or @intf is unbound. A typical example would be a character-device
1550 * driver when its device file is opened.
1551 *
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001552 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1553 * However if the autoresume fails then the counter is re-decremented.
Alan Stern645daaa2006-08-30 15:47:02 -04001554 *
1555 * This routine can run only in process context.
1556 */
1557int usb_autopm_get_interface(struct usb_interface *intf)
1558{
Alan Sternaf4f7602006-10-30 17:06:45 -05001559 int status;
Alan Stern645daaa2006-08-30 15:47:02 -04001560
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001561 status = pm_runtime_get_sync(&intf->dev);
1562 if (status < 0)
1563 pm_runtime_put_sync(&intf->dev);
1564 else
1565 atomic_inc(&intf->pm_usage_cnt);
1566 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1567 __func__, atomic_read(&intf->dev.power.usage_count),
1568 status);
1569 if (status > 0)
1570 status = 0;
Alan Stern645daaa2006-08-30 15:47:02 -04001571 return status;
1572}
1573EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1574
Alan Stern692a1862006-10-30 17:07:51 -05001575/**
Alan Stern9ac39f22008-11-12 16:19:49 -05001576 * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter
1577 * @intf: the usb_interface whose counter should be incremented
1578 *
1579 * This routine does much the same thing as
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001580 * usb_autopm_get_interface(): It increments @intf's usage counter and
1581 * queues an autoresume request if the device is suspended. The
1582 * differences are that it does not perform any synchronization (callers
1583 * should hold a private lock and handle all synchronization issues
1584 * themselves), and it does not autoresume the device directly (it only
1585 * queues a request). After a successful call, the device may not yet be
1586 * resumed.
Alan Stern9ac39f22008-11-12 16:19:49 -05001587 *
1588 * This routine can run in atomic context.
1589 */
1590int usb_autopm_get_interface_async(struct usb_interface *intf)
1591{
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001592 int status = 0;
1593 enum rpm_status s;
Alan Stern9ac39f22008-11-12 16:19:49 -05001594
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001595 /* Don't request a resume unless the interface is already suspending
1596 * or suspended. Doing so would force a running suspend timer to be
1597 * cancelled.
1598 */
1599 pm_runtime_get_noresume(&intf->dev);
1600 s = ACCESS_ONCE(intf->dev.power.runtime_status);
1601 if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
1602 status = pm_request_resume(&intf->dev);
1603
1604 if (status < 0 && status != -EINPROGRESS)
1605 pm_runtime_put_noidle(&intf->dev);
1606 else
Alan Sternccf5b802009-06-29 11:00:01 -04001607 atomic_inc(&intf->pm_usage_cnt);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001608 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1609 __func__, atomic_read(&intf->dev.power.usage_count),
1610 status);
1611 if (status > 0)
1612 status = 0;
Alan Stern9ac39f22008-11-12 16:19:49 -05001613 return status;
1614}
1615EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1616
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001617/**
1618 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1619 * @intf: the usb_interface whose counter should be incremented
1620 *
1621 * This routine increments @intf's usage counter but does not carry out an
1622 * autoresume.
1623 *
1624 * This routine can run in atomic context.
1625 */
1626void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1627{
1628 struct usb_device *udev = interface_to_usbdev(intf);
1629
1630 udev->last_busy = jiffies;
1631 atomic_inc(&intf->pm_usage_cnt);
1632 pm_runtime_get_noresume(&intf->dev);
1633}
1634EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1635
1636/* Internal routine to check whether we may autosuspend a device. */
1637static int autosuspend_check(struct usb_device *udev)
1638{
1639 int i;
1640 struct usb_interface *intf;
1641 unsigned long suspend_time, j;
1642
1643 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1644 * any interface drivers require remote wakeup but it isn't available.
1645 */
1646 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1647 if (udev->actconfig) {
1648 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1649 intf = udev->actconfig->interface[i];
1650
1651 /* We don't need to check interfaces that are
1652 * disabled for runtime PM. Either they are unbound
1653 * or else their drivers don't support autosuspend
1654 * and so they are permanently active.
1655 */
1656 if (intf->dev.power.disable_depth)
1657 continue;
1658 if (atomic_read(&intf->dev.power.usage_count) > 0)
1659 return -EBUSY;
1660 if (intf->needs_remote_wakeup &&
1661 !udev->do_remote_wakeup) {
1662 dev_dbg(&udev->dev, "remote wakeup needed "
1663 "for autosuspend\n");
1664 return -EOPNOTSUPP;
1665 }
1666
1667 /* Don't allow autosuspend if the device will need
1668 * a reset-resume and any of its interface drivers
1669 * doesn't include support or needs remote wakeup.
1670 */
1671 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1672 struct usb_driver *driver;
1673
1674 driver = to_usb_driver(intf->dev.driver);
1675 if (!driver->reset_resume ||
1676 intf->needs_remote_wakeup)
1677 return -EOPNOTSUPP;
1678 }
1679 }
1680 }
1681
1682 /* If everything is okay but the device hasn't been idle for long
1683 * enough, queue a delayed autosuspend request.
1684 */
1685 j = ACCESS_ONCE(jiffies);
1686 suspend_time = udev->last_busy + udev->autosuspend_delay;
1687 if (time_before(j, suspend_time)) {
1688 pm_schedule_suspend(&udev->dev, jiffies_to_msecs(
1689 round_jiffies_up_relative(suspend_time - j)));
1690 return -EAGAIN;
1691 }
1692 return 0;
1693}
1694
1695static int usb_runtime_suspend(struct device *dev)
1696{
1697 int status = 0;
1698
1699 /* A USB device can be suspended if it passes the various autosuspend
1700 * checks. Runtime suspend for a USB device means suspending all the
1701 * interfaces and then the device itself.
1702 */
1703 if (is_usb_device(dev)) {
1704 struct usb_device *udev = to_usb_device(dev);
1705
1706 if (autosuspend_check(udev) != 0)
1707 return -EAGAIN;
1708
1709 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1710
1711 /* If an interface fails the suspend, adjust the last_busy
1712 * time so that we don't get another suspend attempt right
1713 * away.
1714 */
1715 if (status) {
1716 udev->last_busy = jiffies +
1717 (udev->autosuspend_delay == 0 ?
1718 HZ/2 : 0);
1719 }
1720
1721 /* Prevent the parent from suspending immediately after */
1722 else if (udev->parent) {
1723 udev->parent->last_busy = jiffies;
1724 }
1725 }
1726
1727 /* Runtime suspend for a USB interface doesn't mean anything. */
1728 return status;
1729}
1730
1731static int usb_runtime_resume(struct device *dev)
1732{
1733 /* Runtime resume for a USB device means resuming both the device
1734 * and all its interfaces.
1735 */
1736 if (is_usb_device(dev)) {
1737 struct usb_device *udev = to_usb_device(dev);
1738 int status;
1739
1740 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1741 udev->last_busy = jiffies;
1742 return status;
1743 }
1744
1745 /* Runtime resume for a USB interface doesn't mean anything. */
1746 return 0;
1747}
1748
1749static int usb_runtime_idle(struct device *dev)
1750{
1751 /* An idle USB device can be suspended if it passes the various
1752 * autosuspend checks. An idle interface can be suspended at
1753 * any time.
1754 */
1755 if (is_usb_device(dev)) {
1756 struct usb_device *udev = to_usb_device(dev);
1757
1758 if (autosuspend_check(udev) != 0)
1759 return 0;
1760 }
1761
1762 pm_runtime_suspend(dev);
1763 return 0;
1764}
1765
1766static struct dev_pm_ops usb_bus_pm_ops = {
1767 .runtime_suspend = usb_runtime_suspend,
1768 .runtime_resume = usb_runtime_resume,
1769 .runtime_idle = usb_runtime_idle,
1770};
1771
Alan Stern718efa62007-03-09 15:41:13 -05001772#else
1773
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001774#define usb_bus_pm_ops (*(struct dev_pm_ops *) NULL)
Alan Stern9ac39f22008-11-12 16:19:49 -05001775
Alan Stern645daaa2006-08-30 15:47:02 -04001776#endif /* CONFIG_USB_SUSPEND */
1777
Alan Stern36e56a32006-07-01 22:08:06 -04001778struct bus_type usb_bus_type = {
1779 .name = "usb",
1780 .match = usb_device_match,
1781 .uevent = usb_uevent,
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001782 .pm = &usb_bus_pm_ops,
Alan Stern36e56a32006-07-01 22:08:06 -04001783};