Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 1 | /* |
| 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 Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 20 | * matching, probing, releasing, suspending and resuming for |
| 21 | * real drivers. |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 22 | * |
| 23 | */ |
| 24 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 25 | #include <linux/device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Paul Gortmaker | f940fcd | 2011-05-27 09:56:31 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 28 | #include <linux/usb.h> |
Alan Stern | 6bc6cff | 2007-05-04 11:53:03 -0400 | [diff] [blame] | 29 | #include <linux/usb/quirks.h> |
Eric Lescouet | 27729aa | 2010-04-24 23:21:52 +0200 | [diff] [blame] | 30 | #include <linux/usb/hcd.h> |
Eric Lescouet | 27729aa | 2010-04-24 23:21:52 +0200 | [diff] [blame] | 31 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 32 | #include "usb.h" |
| 33 | |
Alan Stern | 20dfdad | 2007-05-22 11:50:17 -0400 | [diff] [blame] | 34 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 35 | #ifdef CONFIG_HOTPLUG |
| 36 | |
| 37 | /* |
| 38 | * Adds a new dynamic USBdevice ID to this driver, |
| 39 | * and cause the driver to probe for all devices again. |
| 40 | */ |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 41 | ssize_t usb_store_new_id(struct usb_dynids *dynids, |
| 42 | struct device_driver *driver, |
| 43 | const char *buf, size_t count) |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 44 | { |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 45 | struct usb_dynid *dynid; |
| 46 | u32 idVendor = 0; |
| 47 | u32 idProduct = 0; |
Josua Dietze | ff231db | 2011-10-23 14:22:29 +0200 | [diff] [blame] | 48 | unsigned int bInterfaceClass = 0; |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 49 | int fields = 0; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 50 | int retval = 0; |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 51 | |
Josua Dietze | ff231db | 2011-10-23 14:22:29 +0200 | [diff] [blame] | 52 | fields = sscanf(buf, "%x %x %x", &idVendor, &idProduct, |
| 53 | &bInterfaceClass); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 54 | if (fields < 2) |
| 55 | return -EINVAL; |
| 56 | |
| 57 | dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); |
| 58 | if (!dynid) |
| 59 | return -ENOMEM; |
| 60 | |
| 61 | INIT_LIST_HEAD(&dynid->node); |
| 62 | dynid->id.idVendor = idVendor; |
| 63 | dynid->id.idProduct = idProduct; |
| 64 | dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE; |
Josua Dietze | ff231db | 2011-10-23 14:22:29 +0200 | [diff] [blame] | 65 | if (fields == 3) { |
| 66 | dynid->id.bInterfaceClass = (u8)bInterfaceClass; |
| 67 | dynid->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS; |
| 68 | } |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 69 | |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 70 | spin_lock(&dynids->lock); |
Nathael Pajani | e5dd011 | 2007-09-04 11:46:23 +0200 | [diff] [blame] | 71 | list_add_tail(&dynid->node, &dynids->list); |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 72 | spin_unlock(&dynids->lock); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 73 | |
Alan Stern | cef9bc5 | 2012-01-24 13:34:41 -0500 | [diff] [blame] | 74 | retval = driver_attach(driver); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 75 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 76 | if (retval) |
| 77 | return retval; |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 78 | return count; |
| 79 | } |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 80 | EXPORT_SYMBOL_GPL(usb_store_new_id); |
| 81 | |
Bjørn Mork | e6bbcef | 2012-05-13 12:34:59 +0200 | [diff] [blame^] | 82 | static ssize_t show_dynids(struct device_driver *driver, char *buf) |
| 83 | { |
| 84 | struct usb_dynid *dynid; |
| 85 | struct usb_driver *usb_drv = to_usb_driver(driver); |
| 86 | size_t count = 0; |
| 87 | |
| 88 | list_for_each_entry(dynid, &usb_drv->dynids.list, node) |
| 89 | if (dynid->id.bInterfaceClass != 0) |
| 90 | count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x %02x\n", |
| 91 | dynid->id.idVendor, dynid->id.idProduct, |
| 92 | dynid->id.bInterfaceClass); |
| 93 | else |
| 94 | count += scnprintf(&buf[count], PAGE_SIZE - count, "%04x %04x\n", |
| 95 | dynid->id.idVendor, dynid->id.idProduct); |
| 96 | return count; |
| 97 | } |
| 98 | |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 99 | static ssize_t store_new_id(struct device_driver *driver, |
| 100 | const char *buf, size_t count) |
| 101 | { |
| 102 | struct usb_driver *usb_drv = to_usb_driver(driver); |
| 103 | |
| 104 | return usb_store_new_id(&usb_drv->dynids, driver, buf, count); |
| 105 | } |
Bjørn Mork | e6bbcef | 2012-05-13 12:34:59 +0200 | [diff] [blame^] | 106 | static DRIVER_ATTR(new_id, S_IRUGO | S_IWUSR, show_dynids, store_new_id); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 107 | |
CHENG Renquan | 0c7a2b7 | 2009-11-22 01:28:52 +0800 | [diff] [blame] | 108 | /** |
| 109 | * store_remove_id - remove a USB device ID from this driver |
| 110 | * @driver: target device driver |
| 111 | * @buf: buffer for scanning device ID data |
| 112 | * @count: input size |
| 113 | * |
| 114 | * Removes a dynamic usb device ID from this driver. |
| 115 | */ |
| 116 | static ssize_t |
| 117 | store_remove_id(struct device_driver *driver, const char *buf, size_t count) |
| 118 | { |
| 119 | struct usb_dynid *dynid, *n; |
| 120 | struct usb_driver *usb_driver = to_usb_driver(driver); |
| 121 | u32 idVendor = 0; |
| 122 | u32 idProduct = 0; |
| 123 | int fields = 0; |
| 124 | int retval = 0; |
| 125 | |
| 126 | fields = sscanf(buf, "%x %x", &idVendor, &idProduct); |
| 127 | if (fields < 2) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | spin_lock(&usb_driver->dynids.lock); |
| 131 | list_for_each_entry_safe(dynid, n, &usb_driver->dynids.list, node) { |
| 132 | struct usb_device_id *id = &dynid->id; |
| 133 | if ((id->idVendor == idVendor) && |
| 134 | (id->idProduct == idProduct)) { |
| 135 | list_del(&dynid->node); |
| 136 | kfree(dynid); |
| 137 | retval = 0; |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | spin_unlock(&usb_driver->dynids.lock); |
| 142 | |
| 143 | if (retval) |
| 144 | return retval; |
| 145 | return count; |
| 146 | } |
Bjørn Mork | e6bbcef | 2012-05-13 12:34:59 +0200 | [diff] [blame^] | 147 | static DRIVER_ATTR(remove_id, S_IRUGO | S_IWUSR, show_dynids, store_remove_id); |
CHENG Renquan | 0c7a2b7 | 2009-11-22 01:28:52 +0800 | [diff] [blame] | 148 | |
Alan Stern | ed283e9 | 2012-01-24 14:35:13 -0500 | [diff] [blame] | 149 | static int usb_create_newid_files(struct usb_driver *usb_drv) |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 150 | { |
| 151 | int error = 0; |
| 152 | |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 153 | if (usb_drv->no_dynamic_id) |
| 154 | goto exit; |
| 155 | |
Alan Stern | ed283e9 | 2012-01-24 14:35:13 -0500 | [diff] [blame] | 156 | if (usb_drv->probe != NULL) { |
Greg Kroah-Hartman | 15147ff | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 157 | error = driver_create_file(&usb_drv->drvwrap.driver, |
| 158 | &driver_attr_new_id); |
Alan Stern | ed283e9 | 2012-01-24 14:35:13 -0500 | [diff] [blame] | 159 | if (error == 0) { |
| 160 | error = driver_create_file(&usb_drv->drvwrap.driver, |
| 161 | &driver_attr_remove_id); |
| 162 | if (error) |
| 163 | driver_remove_file(&usb_drv->drvwrap.driver, |
| 164 | &driver_attr_new_id); |
| 165 | } |
| 166 | } |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 167 | exit: |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 168 | return error; |
| 169 | } |
| 170 | |
Alan Stern | ed283e9 | 2012-01-24 14:35:13 -0500 | [diff] [blame] | 171 | static void usb_remove_newid_files(struct usb_driver *usb_drv) |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 172 | { |
| 173 | if (usb_drv->no_dynamic_id) |
| 174 | return; |
| 175 | |
Alan Stern | ed283e9 | 2012-01-24 14:35:13 -0500 | [diff] [blame] | 176 | if (usb_drv->probe != NULL) { |
| 177 | driver_remove_file(&usb_drv->drvwrap.driver, |
| 178 | &driver_attr_remove_id); |
Greg Kroah-Hartman | 15147ff | 2007-11-28 12:23:18 -0800 | [diff] [blame] | 179 | driver_remove_file(&usb_drv->drvwrap.driver, |
| 180 | &driver_attr_new_id); |
Alan Stern | ed283e9 | 2012-01-24 14:35:13 -0500 | [diff] [blame] | 181 | } |
CHENG Renquan | 0c7a2b7 | 2009-11-22 01:28:52 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 184 | static void usb_free_dynids(struct usb_driver *usb_drv) |
| 185 | { |
| 186 | struct usb_dynid *dynid, *n; |
| 187 | |
| 188 | spin_lock(&usb_drv->dynids.lock); |
| 189 | list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) { |
| 190 | list_del(&dynid->node); |
| 191 | kfree(dynid); |
| 192 | } |
| 193 | spin_unlock(&usb_drv->dynids.lock); |
| 194 | } |
| 195 | #else |
Alan Stern | ed283e9 | 2012-01-24 14:35:13 -0500 | [diff] [blame] | 196 | static inline int usb_create_newid_files(struct usb_driver *usb_drv) |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 197 | { |
| 198 | return 0; |
| 199 | } |
| 200 | |
Alan Stern | ed283e9 | 2012-01-24 14:35:13 -0500 | [diff] [blame] | 201 | static void usb_remove_newid_files(struct usb_driver *usb_drv) |
CHENG Renquan | 0c7a2b7 | 2009-11-22 01:28:52 +0800 | [diff] [blame] | 202 | { |
| 203 | } |
| 204 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 205 | static inline void usb_free_dynids(struct usb_driver *usb_drv) |
| 206 | { |
| 207 | } |
| 208 | #endif |
| 209 | |
| 210 | static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf, |
| 211 | struct usb_driver *drv) |
| 212 | { |
| 213 | struct usb_dynid *dynid; |
| 214 | |
| 215 | spin_lock(&drv->dynids.lock); |
| 216 | list_for_each_entry(dynid, &drv->dynids.list, node) { |
| 217 | if (usb_match_one_id(intf, &dynid->id)) { |
| 218 | spin_unlock(&drv->dynids.lock); |
| 219 | return &dynid->id; |
| 220 | } |
| 221 | } |
| 222 | spin_unlock(&drv->dynids.lock); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 227 | /* called from driver core with dev locked */ |
| 228 | static int usb_probe_device(struct device *dev) |
| 229 | { |
| 230 | struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); |
Kay Sievers | 5512966 | 2009-05-04 19:48:32 +0200 | [diff] [blame] | 231 | struct usb_device *udev = to_usb_device(dev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 232 | int error = 0; |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 233 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 234 | dev_dbg(dev, "%s\n", __func__); |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 235 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 236 | /* TODO: Add real matching code */ |
| 237 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 238 | /* The device should always appear to be in use |
| 239 | * unless the driver suports autosuspend. |
| 240 | */ |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 241 | if (!udriver->supports_autosuspend) |
| 242 | error = usb_autoresume_device(udev); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 243 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 244 | if (!error) |
| 245 | error = udriver->probe(udev); |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 246 | return error; |
| 247 | } |
| 248 | |
| 249 | /* called from driver core with dev locked */ |
| 250 | static int usb_unbind_device(struct device *dev) |
| 251 | { |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 252 | struct usb_device *udev = to_usb_device(dev); |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 253 | struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); |
| 254 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 255 | udriver->disconnect(udev); |
| 256 | if (!udriver->supports_autosuspend) |
| 257 | usb_autosuspend_device(udev); |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 258 | return 0; |
| 259 | } |
| 260 | |
Inaky Perez-Gonzalez | dc023dc | 2008-11-13 10:31:35 -0800 | [diff] [blame] | 261 | /* |
| 262 | * Cancel any pending scheduled resets |
| 263 | * |
| 264 | * [see usb_queue_reset_device()] |
| 265 | * |
| 266 | * Called after unconfiguring / when releasing interfaces. See |
| 267 | * comments in __usb_queue_reset_device() regarding |
| 268 | * udev->reset_running. |
| 269 | */ |
| 270 | static void usb_cancel_queued_reset(struct usb_interface *iface) |
| 271 | { |
| 272 | if (iface->reset_running == 0) |
| 273 | cancel_work_sync(&iface->reset_ws); |
| 274 | } |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 275 | |
| 276 | /* called from driver core with dev locked */ |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 277 | static int usb_probe_interface(struct device *dev) |
| 278 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 279 | struct usb_driver *driver = to_usb_driver(dev->driver); |
Kay Sievers | 5512966 | 2009-05-04 19:48:32 +0200 | [diff] [blame] | 280 | struct usb_interface *intf = to_usb_interface(dev); |
| 281 | struct usb_device *udev = interface_to_usbdev(intf); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 282 | const struct usb_device_id *id; |
| 283 | int error = -ENODEV; |
| 284 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 285 | dev_dbg(dev, "%s\n", __func__); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 286 | |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 287 | intf->needs_binding = 0; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 288 | |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 289 | if (usb_device_is_owned(udev)) |
Alan Stern | 0f3dda9 | 2010-01-08 12:56:04 -0500 | [diff] [blame] | 290 | return error; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 291 | |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 292 | if (udev->authorized == 0) { |
| 293 | dev_err(&intf->dev, "Device is not authorized for usage\n"); |
Alan Stern | 0f3dda9 | 2010-01-08 12:56:04 -0500 | [diff] [blame] | 294 | return error; |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 295 | } |
Inaky Perez-Gonzalez | 72230ab | 2007-07-31 20:34:03 -0700 | [diff] [blame] | 296 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 297 | id = usb_match_id(intf, driver->id_table); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 298 | if (!id) |
| 299 | id = usb_match_dynamic_id(intf, driver); |
Alan Stern | 0f3dda9 | 2010-01-08 12:56:04 -0500 | [diff] [blame] | 300 | if (!id) |
| 301 | return error; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 302 | |
Alan Stern | 0f3dda9 | 2010-01-08 12:56:04 -0500 | [diff] [blame] | 303 | dev_dbg(dev, "%s - got id\n", __func__); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 304 | |
Alan Stern | 0f3dda9 | 2010-01-08 12:56:04 -0500 | [diff] [blame] | 305 | error = usb_autoresume_device(udev); |
| 306 | if (error) |
| 307 | return error; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 308 | |
Alan Stern | 0f3dda9 | 2010-01-08 12:56:04 -0500 | [diff] [blame] | 309 | intf->condition = USB_INTERFACE_BINDING; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 310 | |
Alan Stern | 571dc79 | 2010-04-09 16:03:43 -0400 | [diff] [blame] | 311 | /* Probed interfaces are initially active. They are |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 312 | * runtime-PM-enabled only if the driver has autosuspend support. |
| 313 | * They are sensitive to their children's power states. |
Alan Stern | 0f3dda9 | 2010-01-08 12:56:04 -0500 | [diff] [blame] | 314 | */ |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 315 | pm_runtime_set_active(dev); |
| 316 | pm_suspend_ignore_children(dev, false); |
| 317 | if (driver->supports_autosuspend) |
| 318 | pm_runtime_enable(dev); |
Oliver Neukum | 1e5ea5e3 | 2009-08-27 16:46:56 +0200 | [diff] [blame] | 319 | |
Alan Stern | 0f3dda9 | 2010-01-08 12:56:04 -0500 | [diff] [blame] | 320 | /* Carry out a deferred switch to altsetting 0 */ |
| 321 | if (intf->needs_altsetting0) { |
| 322 | error = usb_set_interface(udev, intf->altsetting[0]. |
| 323 | desc.bInterfaceNumber, 0); |
| 324 | if (error < 0) |
Oliver Neukum | 1e5ea5e3 | 2009-08-27 16:46:56 +0200 | [diff] [blame] | 325 | goto err; |
Alan Stern | 0f3dda9 | 2010-01-08 12:56:04 -0500 | [diff] [blame] | 326 | intf->needs_altsetting0 = 0; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Alan Stern | 0f3dda9 | 2010-01-08 12:56:04 -0500 | [diff] [blame] | 329 | error = driver->probe(intf, id); |
| 330 | if (error) |
| 331 | goto err; |
| 332 | |
| 333 | intf->condition = USB_INTERFACE_BOUND; |
| 334 | usb_autosuspend_device(udev); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 335 | return error; |
Oliver Neukum | 1e5ea5e3 | 2009-08-27 16:46:56 +0200 | [diff] [blame] | 336 | |
Alan Stern | 0f3dda9 | 2010-01-08 12:56:04 -0500 | [diff] [blame] | 337 | err: |
Oliver Neukum | 1e5ea5e3 | 2009-08-27 16:46:56 +0200 | [diff] [blame] | 338 | intf->needs_remote_wakeup = 0; |
| 339 | intf->condition = USB_INTERFACE_UNBOUND; |
| 340 | usb_cancel_queued_reset(intf); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 341 | |
| 342 | /* Unbound interfaces are always runtime-PM-disabled and -suspended */ |
Alan Stern | 89842ae | 2010-05-11 11:44:06 -0400 | [diff] [blame] | 343 | if (driver->supports_autosuspend) |
| 344 | pm_runtime_disable(dev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 345 | pm_runtime_set_suspended(dev); |
| 346 | |
Oliver Neukum | 1e5ea5e3 | 2009-08-27 16:46:56 +0200 | [diff] [blame] | 347 | usb_autosuspend_device(udev); |
| 348 | return error; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 351 | /* called from driver core with dev locked */ |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 352 | static int usb_unbind_interface(struct device *dev) |
| 353 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 354 | struct usb_driver *driver = to_usb_driver(dev->driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 355 | struct usb_interface *intf = to_usb_interface(dev); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 356 | struct usb_device *udev; |
Oliver Neukum | 1e5ea5e3 | 2009-08-27 16:46:56 +0200 | [diff] [blame] | 357 | int error, r; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 358 | |
| 359 | intf->condition = USB_INTERFACE_UNBINDING; |
| 360 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 361 | /* Autoresume for set_interface call below */ |
| 362 | udev = interface_to_usbdev(intf); |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 363 | error = usb_autoresume_device(udev); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 364 | |
Alan Stern | 9da82bd | 2008-05-08 11:54:37 -0400 | [diff] [blame] | 365 | /* Terminate all URBs for this interface unless the driver |
| 366 | * supports "soft" unbinding. |
| 367 | */ |
| 368 | if (!driver->soft_unbind) |
Alan Stern | ddeac4e | 2009-01-15 17:03:33 -0500 | [diff] [blame] | 369 | usb_disable_interface(udev, intf, false); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 370 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 371 | driver->disconnect(intf); |
Inaky Perez-Gonzalez | dc023dc | 2008-11-13 10:31:35 -0800 | [diff] [blame] | 372 | usb_cancel_queued_reset(intf); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 373 | |
Alan Stern | 55151d7 | 2008-08-12 14:33:59 -0400 | [diff] [blame] | 374 | /* Reset other interface state. |
| 375 | * We cannot do a Set-Interface if the device is suspended or |
| 376 | * if it is prepared for a system sleep (since installing a new |
| 377 | * altsetting means creating new endpoint device entries). |
| 378 | * When either of these happens, defer the Set-Interface. |
| 379 | */ |
Alan Stern | 2caf7fc | 2008-12-31 11:31:33 -0500 | [diff] [blame] | 380 | if (intf->cur_altsetting->desc.bAlternateSetting == 0) { |
| 381 | /* Already in altsetting 0 so skip Set-Interface. |
| 382 | * Just re-enable it without affecting the endpoint toggles. |
| 383 | */ |
| 384 | usb_enable_interface(udev, intf, false); |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 385 | } else if (!error && !intf->dev.power.is_prepared) { |
Oliver Neukum | 1e5ea5e3 | 2009-08-27 16:46:56 +0200 | [diff] [blame] | 386 | r = usb_set_interface(udev, intf->altsetting[0]. |
Alan Stern | 55151d7 | 2008-08-12 14:33:59 -0400 | [diff] [blame] | 387 | desc.bInterfaceNumber, 0); |
Oliver Neukum | 1e5ea5e3 | 2009-08-27 16:46:56 +0200 | [diff] [blame] | 388 | if (r < 0) |
| 389 | intf->needs_altsetting0 = 1; |
| 390 | } else { |
Alan Stern | 55151d7 | 2008-08-12 14:33:59 -0400 | [diff] [blame] | 391 | intf->needs_altsetting0 = 1; |
Oliver Neukum | 1e5ea5e3 | 2009-08-27 16:46:56 +0200 | [diff] [blame] | 392 | } |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 393 | usb_set_intfdata(intf, NULL); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 394 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 395 | intf->condition = USB_INTERFACE_UNBOUND; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 396 | intf->needs_remote_wakeup = 0; |
| 397 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 398 | /* Unbound interfaces are always runtime-PM-disabled and -suspended */ |
Alan Stern | 89842ae | 2010-05-11 11:44:06 -0400 | [diff] [blame] | 399 | if (driver->supports_autosuspend) |
| 400 | pm_runtime_disable(dev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 401 | pm_runtime_set_suspended(dev); |
| 402 | |
| 403 | /* Undo any residual pm_autopm_get_interface_* calls */ |
| 404 | for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r) |
| 405 | usb_autopm_put_interface_no_suspend(intf); |
| 406 | atomic_set(&intf->pm_usage_cnt, 0); |
| 407 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 408 | if (!error) |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 409 | usb_autosuspend_device(udev); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 414 | /** |
| 415 | * usb_driver_claim_interface - bind a driver to an interface |
| 416 | * @driver: the driver to be bound |
| 417 | * @iface: the interface to which it will be bound; must be in the |
| 418 | * usb device's active configuration |
| 419 | * @priv: driver data associated with that interface |
| 420 | * |
| 421 | * This is used by usb device drivers that need to claim more than one |
| 422 | * interface on a device when probing (audio and acm are current examples). |
| 423 | * No device driver should directly modify internal usb_interface or |
| 424 | * usb_device structure members. |
| 425 | * |
| 426 | * Few drivers should need to use this routine, since the most natural |
| 427 | * way to bind to an interface is to return the private data from |
| 428 | * the driver's probe() method. |
| 429 | * |
Greg Kroah-Hartman | 341487a8 | 2007-04-09 11:52:31 -0400 | [diff] [blame] | 430 | * Callers must own the device lock, so driver probe() entries don't need |
| 431 | * extra locking, but other call contexts may need to explicitly claim that |
| 432 | * lock. |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 433 | */ |
| 434 | int usb_driver_claim_interface(struct usb_driver *driver, |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 435 | struct usb_interface *iface, void *priv) |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 436 | { |
| 437 | struct device *dev = &iface->dev; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 438 | int retval = 0; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 439 | |
| 440 | if (dev->driver) |
| 441 | return -EBUSY; |
| 442 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 443 | dev->driver = &driver->drvwrap.driver; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 444 | usb_set_intfdata(iface, priv); |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 445 | iface->needs_binding = 0; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 446 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 447 | iface->condition = USB_INTERFACE_BOUND; |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 448 | |
Alan Stern | 89842ae | 2010-05-11 11:44:06 -0400 | [diff] [blame] | 449 | /* Claimed interfaces are initially inactive (suspended) and |
| 450 | * runtime-PM-enabled, but only if the driver has autosuspend |
| 451 | * support. Otherwise they are marked active, to prevent the |
| 452 | * device from being autosuspended, but left disabled. In either |
| 453 | * case they are sensitive to their children's power states. |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 454 | */ |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 455 | pm_suspend_ignore_children(dev, false); |
| 456 | if (driver->supports_autosuspend) |
| 457 | pm_runtime_enable(dev); |
Alan Stern | 89842ae | 2010-05-11 11:44:06 -0400 | [diff] [blame] | 458 | else |
| 459 | pm_runtime_set_active(dev); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 460 | |
| 461 | /* if interface was already added, bind now; else let |
| 462 | * the future device_add() bind it, bypassing probe() |
| 463 | */ |
| 464 | if (device_is_registered(dev)) |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 465 | retval = device_bind_driver(dev); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 466 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 467 | return retval; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 468 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 469 | EXPORT_SYMBOL_GPL(usb_driver_claim_interface); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 470 | |
| 471 | /** |
| 472 | * usb_driver_release_interface - unbind a driver from an interface |
| 473 | * @driver: the driver to be unbound |
| 474 | * @iface: the interface from which it will be unbound |
| 475 | * |
| 476 | * This can be used by drivers to release an interface without waiting |
| 477 | * for their disconnect() methods to be called. In typical cases this |
| 478 | * also causes the driver disconnect() method to be called. |
| 479 | * |
| 480 | * This call is synchronous, and may not be used in an interrupt context. |
Greg Kroah-Hartman | 341487a8 | 2007-04-09 11:52:31 -0400 | [diff] [blame] | 481 | * Callers must own the device lock, so driver disconnect() entries don't |
| 482 | * need extra locking, but other call contexts may need to explicitly claim |
| 483 | * that lock. |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 484 | */ |
| 485 | void usb_driver_release_interface(struct usb_driver *driver, |
| 486 | struct usb_interface *iface) |
| 487 | { |
| 488 | struct device *dev = &iface->dev; |
| 489 | |
| 490 | /* this should never happen, don't release something that's not ours */ |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 491 | if (!dev->driver || dev->driver != &driver->drvwrap.driver) |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 492 | return; |
| 493 | |
| 494 | /* don't release from within disconnect() */ |
| 495 | if (iface->condition != USB_INTERFACE_BOUND) |
| 496 | return; |
Alan Stern | 91f8d06 | 2009-04-16 15:35:09 -0400 | [diff] [blame] | 497 | iface->condition = USB_INTERFACE_UNBINDING; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 498 | |
Alan Stern | 91f8d06 | 2009-04-16 15:35:09 -0400 | [diff] [blame] | 499 | /* Release via the driver core only if the interface |
| 500 | * has already been registered |
| 501 | */ |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 502 | if (device_is_registered(dev)) { |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 503 | device_release_driver(dev); |
Inaky Perez-Gonzalez | dc023dc | 2008-11-13 10:31:35 -0800 | [diff] [blame] | 504 | } else { |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 505 | device_lock(dev); |
Alan Stern | 91f8d06 | 2009-04-16 15:35:09 -0400 | [diff] [blame] | 506 | usb_unbind_interface(dev); |
| 507 | dev->driver = NULL; |
Greg Kroah-Hartman | 8e9394c | 2010-02-17 10:57:05 -0800 | [diff] [blame] | 508 | device_unlock(dev); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 509 | } |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 510 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 511 | EXPORT_SYMBOL_GPL(usb_driver_release_interface); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 512 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 513 | /* returns 0 if no match, 1 if match */ |
Greg Kroah-Hartman | bb41702 | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 514 | int usb_match_device(struct usb_device *dev, const struct usb_device_id *id) |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 515 | { |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 516 | if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
| 517 | id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) |
| 518 | return 0; |
| 519 | |
| 520 | if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && |
| 521 | id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) |
| 522 | return 0; |
| 523 | |
| 524 | /* No need to test id->bcdDevice_lo != 0, since 0 is never |
| 525 | greater than any unsigned number. */ |
| 526 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && |
| 527 | (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) |
| 528 | return 0; |
| 529 | |
| 530 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && |
| 531 | (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) |
| 532 | return 0; |
| 533 | |
| 534 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && |
| 535 | (id->bDeviceClass != dev->descriptor.bDeviceClass)) |
| 536 | return 0; |
| 537 | |
| 538 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 539 | (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass)) |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 540 | return 0; |
| 541 | |
| 542 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && |
| 543 | (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) |
| 544 | return 0; |
| 545 | |
Greg Kroah-Hartman | bb41702 | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 546 | return 1; |
| 547 | } |
| 548 | |
| 549 | /* returns 0 if no match, 1 if match */ |
| 550 | int usb_match_one_id(struct usb_interface *interface, |
| 551 | const struct usb_device_id *id) |
| 552 | { |
| 553 | struct usb_host_interface *intf; |
| 554 | struct usb_device *dev; |
| 555 | |
| 556 | /* proc_connectinfo in devio.c may call us with id == NULL. */ |
| 557 | if (id == NULL) |
| 558 | return 0; |
| 559 | |
| 560 | intf = interface->cur_altsetting; |
| 561 | dev = interface_to_usbdev(interface); |
| 562 | |
| 563 | if (!usb_match_device(dev, id)) |
| 564 | return 0; |
| 565 | |
Alan Stern | 93c8bf4 | 2006-10-18 16:41:51 -0400 | [diff] [blame] | 566 | /* The interface class, subclass, and protocol should never be |
| 567 | * checked for a match if the device class is Vendor Specific, |
| 568 | * unless the match record specifies the Vendor ID. */ |
| 569 | if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC && |
| 570 | !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
| 571 | (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | |
| 572 | USB_DEVICE_ID_MATCH_INT_SUBCLASS | |
| 573 | USB_DEVICE_ID_MATCH_INT_PROTOCOL))) |
| 574 | return 0; |
| 575 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 576 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && |
| 577 | (id->bInterfaceClass != intf->desc.bInterfaceClass)) |
| 578 | return 0; |
| 579 | |
| 580 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && |
| 581 | (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) |
| 582 | return 0; |
| 583 | |
| 584 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && |
| 585 | (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) |
| 586 | return 0; |
| 587 | |
| 588 | return 1; |
| 589 | } |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 590 | EXPORT_SYMBOL_GPL(usb_match_one_id); |
| 591 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 592 | /** |
| 593 | * usb_match_id - find first usb_device_id matching device or interface |
| 594 | * @interface: the interface of interest |
| 595 | * @id: array of usb_device_id structures, terminated by zero entry |
| 596 | * |
| 597 | * usb_match_id searches an array of usb_device_id's and returns |
| 598 | * the first one matching the device or interface, or null. |
| 599 | * This is used when binding (or rebinding) a driver to an interface. |
| 600 | * Most USB device drivers will use this indirectly, through the usb core, |
| 601 | * but some layered driver frameworks use it directly. |
| 602 | * These device tables are exported with MODULE_DEVICE_TABLE, through |
| 603 | * modutils, to support the driver loading functionality of USB hotplugging. |
| 604 | * |
| 605 | * What Matches: |
| 606 | * |
| 607 | * The "match_flags" element in a usb_device_id controls which |
| 608 | * members are used. If the corresponding bit is set, the |
| 609 | * value in the device_id must match its corresponding member |
| 610 | * in the device or interface descriptor, or else the device_id |
| 611 | * does not match. |
| 612 | * |
| 613 | * "driver_info" is normally used only by device drivers, |
| 614 | * but you can create a wildcard "matches anything" usb_device_id |
| 615 | * as a driver's "modules.usbmap" entry if you provide an id with |
| 616 | * only a nonzero "driver_info" field. If you do this, the USB device |
| 617 | * driver's probe() routine should use additional intelligence to |
| 618 | * decide whether to bind to the specified interface. |
| 619 | * |
| 620 | * What Makes Good usb_device_id Tables: |
| 621 | * |
| 622 | * The match algorithm is very simple, so that intelligence in |
| 623 | * driver selection must come from smart driver id records. |
| 624 | * Unless you have good reasons to use another selection policy, |
| 625 | * provide match elements only in related groups, and order match |
| 626 | * specifiers from specific to general. Use the macros provided |
| 627 | * for that purpose if you can. |
| 628 | * |
| 629 | * The most specific match specifiers use device descriptor |
| 630 | * data. These are commonly used with product-specific matches; |
| 631 | * the USB_DEVICE macro lets you provide vendor and product IDs, |
| 632 | * and you can also match against ranges of product revisions. |
| 633 | * These are widely used for devices with application or vendor |
| 634 | * specific bDeviceClass values. |
| 635 | * |
| 636 | * Matches based on device class/subclass/protocol specifications |
| 637 | * are slightly more general; use the USB_DEVICE_INFO macro, or |
| 638 | * its siblings. These are used with single-function devices |
| 639 | * where bDeviceClass doesn't specify that each interface has |
| 640 | * its own class. |
| 641 | * |
| 642 | * Matches based on interface class/subclass/protocol are the |
| 643 | * most general; they let drivers bind to any interface on a |
| 644 | * multiple-function device. Use the USB_INTERFACE_INFO |
| 645 | * macro, or its siblings, to match class-per-interface style |
Alan Stern | 93c8bf4 | 2006-10-18 16:41:51 -0400 | [diff] [blame] | 646 | * devices (as recorded in bInterfaceClass). |
| 647 | * |
| 648 | * Note that an entry created by USB_INTERFACE_INFO won't match |
| 649 | * any interface if the device class is set to Vendor-Specific. |
| 650 | * This is deliberate; according to the USB spec the meanings of |
| 651 | * the interface class/subclass/protocol for these devices are also |
| 652 | * vendor-specific, and hence matching against a standard product |
| 653 | * class wouldn't work anyway. If you really want to use an |
| 654 | * interface-based match for such a device, create a match record |
| 655 | * that also specifies the vendor ID. (Unforunately there isn't a |
| 656 | * standard macro for creating records like this.) |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 657 | * |
| 658 | * Within those groups, remember that not all combinations are |
| 659 | * meaningful. For example, don't give a product version range |
| 660 | * without vendor and product IDs; or specify a protocol without |
| 661 | * its associated class and subclass. |
| 662 | */ |
| 663 | const struct usb_device_id *usb_match_id(struct usb_interface *interface, |
| 664 | const struct usb_device_id *id) |
| 665 | { |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 666 | /* proc_connectinfo in devio.c may call us with id == NULL. */ |
| 667 | if (id == NULL) |
| 668 | return NULL; |
| 669 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 670 | /* It is important to check that id->driver_info is nonzero, |
| 671 | since an entry that is all zeroes except for a nonzero |
| 672 | id->driver_info is the way to create an entry that |
| 673 | indicates that the driver want to examine every |
| 674 | device and interface. */ |
Greg Kroah-Hartman | de6f92b | 2008-01-28 09:50:12 -0800 | [diff] [blame] | 675 | for (; id->idVendor || id->idProduct || id->bDeviceClass || |
| 676 | id->bInterfaceClass || id->driver_info; id++) { |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 677 | if (usb_match_one_id(interface, id)) |
| 678 | return id; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | return NULL; |
| 682 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 683 | EXPORT_SYMBOL_GPL(usb_match_id); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 684 | |
Adrian Bunk | 8bb22d2 | 2006-11-21 22:02:54 +0100 | [diff] [blame] | 685 | static int usb_device_match(struct device *dev, struct device_driver *drv) |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 686 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 687 | /* devices and interfaces are handled separately */ |
| 688 | if (is_usb_device(dev)) { |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 689 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 690 | /* interface drivers never match devices */ |
| 691 | if (!is_usb_device_driver(drv)) |
| 692 | return 0; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 693 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 694 | /* TODO: Add real matching code */ |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 695 | return 1; |
| 696 | |
Kay Sievers | 5512966 | 2009-05-04 19:48:32 +0200 | [diff] [blame] | 697 | } else if (is_usb_interface(dev)) { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 698 | struct usb_interface *intf; |
| 699 | struct usb_driver *usb_drv; |
| 700 | const struct usb_device_id *id; |
| 701 | |
| 702 | /* device drivers never match interfaces */ |
| 703 | if (is_usb_device_driver(drv)) |
| 704 | return 0; |
| 705 | |
| 706 | intf = to_usb_interface(dev); |
| 707 | usb_drv = to_usb_driver(drv); |
| 708 | |
| 709 | id = usb_match_id(intf, usb_drv->id_table); |
| 710 | if (id) |
| 711 | return 1; |
| 712 | |
| 713 | id = usb_match_dynamic_id(intf, usb_drv); |
| 714 | if (id) |
| 715 | return 1; |
| 716 | } |
| 717 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 718 | return 0; |
| 719 | } |
| 720 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 721 | #ifdef CONFIG_HOTPLUG |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 722 | static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 723 | { |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 724 | struct usb_device *usb_dev; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 725 | |
Kay Sievers | 5512966 | 2009-05-04 19:48:32 +0200 | [diff] [blame] | 726 | if (is_usb_device(dev)) { |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 727 | usb_dev = to_usb_device(dev); |
Kay Sievers | 5512966 | 2009-05-04 19:48:32 +0200 | [diff] [blame] | 728 | } else if (is_usb_interface(dev)) { |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 729 | struct usb_interface *intf = to_usb_interface(dev); |
Kay Sievers | 5512966 | 2009-05-04 19:48:32 +0200 | [diff] [blame] | 730 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 731 | usb_dev = interface_to_usbdev(intf); |
Kay Sievers | 5512966 | 2009-05-04 19:48:32 +0200 | [diff] [blame] | 732 | } else { |
| 733 | return 0; |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 734 | } |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 735 | |
| 736 | if (usb_dev->devnum < 0) { |
Alan Stern | cceffe9 | 2010-02-08 09:45:12 -0500 | [diff] [blame] | 737 | /* driver is often null here; dev_dbg() would oops */ |
Kay Sievers | 7071a3c | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 738 | pr_debug("usb %s: already deleted?\n", dev_name(dev)); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 739 | return -ENODEV; |
| 740 | } |
| 741 | if (!usb_dev->bus) { |
Kay Sievers | 7071a3c | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 742 | pr_debug("usb %s: bus removed?\n", dev_name(dev)); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 743 | return -ENODEV; |
| 744 | } |
| 745 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 746 | /* per-device configurations are common */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 747 | if (add_uevent_var(env, "PRODUCT=%x/%x/%x", |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 748 | le16_to_cpu(usb_dev->descriptor.idVendor), |
| 749 | le16_to_cpu(usb_dev->descriptor.idProduct), |
| 750 | le16_to_cpu(usb_dev->descriptor.bcdDevice))) |
| 751 | return -ENOMEM; |
| 752 | |
| 753 | /* class-based driver binding models */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 754 | if (add_uevent_var(env, "TYPE=%d/%d/%d", |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 755 | usb_dev->descriptor.bDeviceClass, |
| 756 | usb_dev->descriptor.bDeviceSubClass, |
| 757 | usb_dev->descriptor.bDeviceProtocol)) |
| 758 | return -ENOMEM; |
| 759 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | #else |
| 764 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 765 | static int usb_uevent(struct device *dev, struct kobj_uevent_env *env) |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 766 | { |
| 767 | return -ENODEV; |
| 768 | } |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 769 | #endif /* CONFIG_HOTPLUG */ |
| 770 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 771 | /** |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 772 | * usb_register_device_driver - register a USB device (not interface) driver |
| 773 | * @new_udriver: USB operations for the device driver |
Greg Kroah-Hartman | 2143acc | 2005-11-21 14:53:03 -0800 | [diff] [blame] | 774 | * @owner: module owner of this driver. |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 775 | * |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 776 | * Registers a USB device driver with the USB core. The list of |
| 777 | * unattached devices will be rescanned whenever a new driver is |
| 778 | * added, allowing the new driver to attach to any recognized devices. |
| 779 | * Returns a negative error code on failure and 0 on success. |
| 780 | */ |
| 781 | int usb_register_device_driver(struct usb_device_driver *new_udriver, |
| 782 | struct module *owner) |
| 783 | { |
| 784 | int retval = 0; |
| 785 | |
| 786 | if (usb_disabled()) |
| 787 | return -ENODEV; |
| 788 | |
| 789 | new_udriver->drvwrap.for_devices = 1; |
| 790 | new_udriver->drvwrap.driver.name = (char *) new_udriver->name; |
| 791 | new_udriver->drvwrap.driver.bus = &usb_bus_type; |
| 792 | new_udriver->drvwrap.driver.probe = usb_probe_device; |
| 793 | new_udriver->drvwrap.driver.remove = usb_unbind_device; |
| 794 | new_udriver->drvwrap.driver.owner = owner; |
| 795 | |
| 796 | retval = driver_register(&new_udriver->drvwrap.driver); |
| 797 | |
Greg Kroah-Hartman | fb28d58 | 2012-04-25 17:15:29 -0700 | [diff] [blame] | 798 | if (!retval) |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 799 | pr_info("%s: registered new device driver %s\n", |
| 800 | usbcore_name, new_udriver->name); |
Greg Kroah-Hartman | fb28d58 | 2012-04-25 17:15:29 -0700 | [diff] [blame] | 801 | else |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 802 | printk(KERN_ERR "%s: error %d registering device " |
| 803 | " driver %s\n", |
| 804 | usbcore_name, retval, new_udriver->name); |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 805 | |
| 806 | return retval; |
| 807 | } |
| 808 | EXPORT_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 | */ |
| 817 | void 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); |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 823 | } |
| 824 | EXPORT_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 Dunlap | 892705a | 2007-02-10 14:41:41 -0800 | [diff] [blame] | 830 | * @mod_name: module name string |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 831 | * |
| 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-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 835 | * 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-Hartman | 80f745f | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 841 | int usb_register_driver(struct usb_driver *new_driver, struct module *owner, |
| 842 | const char *mod_name) |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 843 | { |
| 844 | int retval = 0; |
| 845 | |
| 846 | if (usb_disabled()) |
| 847 | return -ENODEV; |
| 848 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 849 | 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-Hartman | 80f745f | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 855 | new_driver->drvwrap.driver.mod_name = mod_name; |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 856 | spin_lock_init(&new_driver->dynids.lock); |
| 857 | INIT_LIST_HEAD(&new_driver->dynids.list); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 858 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 859 | retval = driver_register(&new_driver->drvwrap.driver); |
CHENG Renquan | 0c7a2b7 | 2009-11-22 01:28:52 +0800 | [diff] [blame] | 860 | if (retval) |
| 861 | goto out; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 862 | |
Alan Stern | ed283e9 | 2012-01-24 14:35:13 -0500 | [diff] [blame] | 863 | retval = usb_create_newid_files(new_driver); |
CHENG Renquan | 0c7a2b7 | 2009-11-22 01:28:52 +0800 | [diff] [blame] | 864 | if (retval) |
| 865 | goto out_newid; |
| 866 | |
CHENG Renquan | 0c7a2b7 | 2009-11-22 01:28:52 +0800 | [diff] [blame] | 867 | pr_info("%s: registered new interface driver %s\n", |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 868 | usbcore_name, new_driver->name); |
CHENG Renquan | 0c7a2b7 | 2009-11-22 01:28:52 +0800 | [diff] [blame] | 869 | |
| 870 | out: |
| 871 | return retval; |
| 872 | |
CHENG Renquan | 0c7a2b7 | 2009-11-22 01:28:52 +0800 | [diff] [blame] | 873 | out_newid: |
| 874 | driver_unregister(&new_driver->drvwrap.driver); |
| 875 | |
| 876 | printk(KERN_ERR "%s: error %d registering interface " |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 877 | " driver %s\n", |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 878 | usbcore_name, retval, new_driver->name); |
CHENG Renquan | 0c7a2b7 | 2009-11-22 01:28:52 +0800 | [diff] [blame] | 879 | goto out; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 880 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 881 | EXPORT_SYMBOL_GPL(usb_register_driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 882 | |
| 883 | /** |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 884 | * usb_deregister - unregister a USB interface driver |
| 885 | * @driver: USB operations of the interface driver to unregister |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 886 | * Context: must be able to sleep |
| 887 | * |
| 888 | * Unlinks the specified driver from the internal USB driver list. |
| 889 | * |
| 890 | * NOTE: If you called usb_register_dev(), you still need to call |
| 891 | * usb_deregister_dev() to clean up your driver's allocated minor numbers, |
| 892 | * this * call will no longer do it for you. |
| 893 | */ |
| 894 | void usb_deregister(struct usb_driver *driver) |
| 895 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 896 | pr_info("%s: deregistering interface driver %s\n", |
| 897 | usbcore_name, driver->name); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 898 | |
Alan Stern | ed283e9 | 2012-01-24 14:35:13 -0500 | [diff] [blame] | 899 | usb_remove_newid_files(driver); |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 900 | driver_unregister(&driver->drvwrap.driver); |
Alan Stern | ed283e9 | 2012-01-24 14:35:13 -0500 | [diff] [blame] | 901 | usb_free_dynids(driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 902 | } |
Greg Kroah-Hartman | 782e70c | 2008-01-25 11:12:21 -0600 | [diff] [blame] | 903 | EXPORT_SYMBOL_GPL(usb_deregister); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 904 | |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 905 | /* Forced unbinding of a USB interface driver, either because |
| 906 | * it doesn't support pre_reset/post_reset/reset_resume or |
| 907 | * because it doesn't support suspend/resume. |
| 908 | * |
| 909 | * The caller must hold @intf's device's lock, but not its pm_mutex |
| 910 | * and not @intf->dev.sem. |
| 911 | */ |
| 912 | void usb_forced_unbind_intf(struct usb_interface *intf) |
| 913 | { |
| 914 | struct usb_driver *driver = to_usb_driver(intf->dev.driver); |
| 915 | |
| 916 | dev_dbg(&intf->dev, "forced unbind\n"); |
| 917 | usb_driver_release_interface(driver, intf); |
| 918 | |
| 919 | /* Mark the interface for later rebinding */ |
| 920 | intf->needs_binding = 1; |
| 921 | } |
| 922 | |
| 923 | /* Delayed forced unbinding of a USB interface driver and scan |
| 924 | * for rebinding. |
| 925 | * |
| 926 | * The caller must hold @intf's device's lock, but not its pm_mutex |
| 927 | * and not @intf->dev.sem. |
| 928 | * |
Alan Stern | 5096aed | 2008-08-12 14:34:14 -0400 | [diff] [blame] | 929 | * Note: Rebinds will be skipped if a system sleep transition is in |
| 930 | * progress and the PM "complete" callback hasn't occurred yet. |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 931 | */ |
| 932 | void usb_rebind_intf(struct usb_interface *intf) |
| 933 | { |
| 934 | int rc; |
| 935 | |
| 936 | /* Delayed unbind of an existing driver */ |
Oliver Neukum | 1493138 | 2012-01-05 15:39:57 +0100 | [diff] [blame] | 937 | if (intf->dev.driver) |
| 938 | usb_forced_unbind_intf(intf); |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 939 | |
| 940 | /* Try to rebind the interface */ |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 941 | if (!intf->dev.power.is_prepared) { |
Alan Stern | 5096aed | 2008-08-12 14:34:14 -0400 | [diff] [blame] | 942 | intf->needs_binding = 0; |
| 943 | rc = device_attach(&intf->dev); |
| 944 | if (rc < 0) |
| 945 | dev_warn(&intf->dev, "rebind failed: %d\n", rc); |
| 946 | } |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 947 | } |
| 948 | |
Alan Stern | 9ff7843 | 2008-08-07 13:04:51 -0400 | [diff] [blame] | 949 | #ifdef CONFIG_PM |
| 950 | |
Oliver Neukum | 1493138 | 2012-01-05 15:39:57 +0100 | [diff] [blame] | 951 | /* Unbind drivers for @udev's interfaces that don't support suspend/resume |
| 952 | * There is no check for reset_resume here because it can be determined |
| 953 | * only during resume whether reset_resume is needed. |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 954 | * |
| 955 | * The caller must hold @udev's device lock. |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 956 | */ |
Oliver Neukum | 1493138 | 2012-01-05 15:39:57 +0100 | [diff] [blame] | 957 | static void unbind_no_pm_drivers_interfaces(struct usb_device *udev) |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 958 | { |
| 959 | struct usb_host_config *config; |
| 960 | int i; |
| 961 | struct usb_interface *intf; |
| 962 | struct usb_driver *drv; |
| 963 | |
| 964 | config = udev->actconfig; |
| 965 | if (config) { |
| 966 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { |
| 967 | intf = config->interface[i]; |
Oliver Neukum | 1493138 | 2012-01-05 15:39:57 +0100 | [diff] [blame] | 968 | |
| 969 | if (intf->dev.driver) { |
| 970 | drv = to_usb_driver(intf->dev.driver); |
| 971 | if (!drv->suspend || !drv->resume) |
| 972 | usb_forced_unbind_intf(intf); |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
Oliver Neukum | 1493138 | 2012-01-05 15:39:57 +0100 | [diff] [blame] | 978 | /* Unbind drivers for @udev's interfaces that failed to support reset-resume. |
| 979 | * These interfaces have the needs_binding flag set by usb_resume_interface(). |
| 980 | * |
| 981 | * The caller must hold @udev's device lock. |
| 982 | */ |
| 983 | static void unbind_no_reset_resume_drivers_interfaces(struct usb_device *udev) |
| 984 | { |
| 985 | struct usb_host_config *config; |
| 986 | int i; |
| 987 | struct usb_interface *intf; |
| 988 | |
| 989 | config = udev->actconfig; |
| 990 | if (config) { |
| 991 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { |
| 992 | intf = config->interface[i]; |
| 993 | if (intf->dev.driver && intf->needs_binding) |
| 994 | usb_forced_unbind_intf(intf); |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | static void do_rebind_interfaces(struct usb_device *udev) |
| 1000 | { |
| 1001 | struct usb_host_config *config; |
| 1002 | int i; |
| 1003 | struct usb_interface *intf; |
| 1004 | |
| 1005 | config = udev->actconfig; |
| 1006 | if (config) { |
| 1007 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { |
| 1008 | intf = config->interface[i]; |
| 1009 | if (intf->needs_binding) |
| 1010 | usb_rebind_intf(intf); |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | |
Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1015 | static int usb_suspend_device(struct usb_device *udev, pm_message_t msg) |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1016 | { |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 1017 | struct usb_device_driver *udriver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1018 | int status = 0; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1019 | |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 1020 | if (udev->state == USB_STATE_NOTATTACHED || |
| 1021 | udev->state == USB_STATE_SUSPENDED) |
| 1022 | goto done; |
| 1023 | |
Alan Stern | b6f6436 | 2007-05-04 11:51:54 -0400 | [diff] [blame] | 1024 | /* For devices that don't have a driver, we do a generic suspend. */ |
| 1025 | if (udev->dev.driver) |
| 1026 | udriver = to_usb_device_driver(udev->dev.driver); |
| 1027 | else { |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1028 | udev->do_remote_wakeup = 0; |
Alan Stern | b6f6436 | 2007-05-04 11:51:54 -0400 | [diff] [blame] | 1029 | udriver = &usb_generic_driver; |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 1030 | } |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1031 | status = udriver->suspend(udev, msg); |
| 1032 | |
Alan Stern | 20dfdad | 2007-05-22 11:50:17 -0400 | [diff] [blame] | 1033 | done: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1034 | dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1035 | return status; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1036 | } |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1037 | |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1038 | static int usb_resume_device(struct usb_device *udev, pm_message_t msg) |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1039 | { |
| 1040 | struct usb_device_driver *udriver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1041 | int status = 0; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1042 | |
Alan Stern | 0458d5b | 2007-05-04 11:52:20 -0400 | [diff] [blame] | 1043 | if (udev->state == USB_STATE_NOTATTACHED) |
| 1044 | goto done; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1045 | |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 1046 | /* Can't resume it if it doesn't have a driver. */ |
| 1047 | if (udev->dev.driver == NULL) { |
| 1048 | status = -ENOTCONN; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1049 | goto done; |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 1050 | } |
| 1051 | |
Alan Stern | 6d19c00 | 2010-02-12 12:21:11 +0100 | [diff] [blame] | 1052 | /* Non-root devices on a full/low-speed bus must wait for their |
| 1053 | * companion high-speed root hub, in case a handoff is needed. |
| 1054 | */ |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 1055 | if (!PMSG_IS_AUTO(msg) && udev->parent && udev->bus->hs_companion) |
Alan Stern | 6d19c00 | 2010-02-12 12:21:11 +0100 | [diff] [blame] | 1056 | device_pm_wait_for_dev(&udev->dev, |
| 1057 | &udev->bus->hs_companion->root_hub->dev); |
| 1058 | |
Alan Stern | 6bc6cff | 2007-05-04 11:53:03 -0400 | [diff] [blame] | 1059 | if (udev->quirks & USB_QUIRK_RESET_RESUME) |
| 1060 | udev->reset_resume = 1; |
| 1061 | |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1062 | udriver = to_usb_device_driver(udev->dev.driver); |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1063 | status = udriver->resume(udev, msg); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1064 | |
Alan Stern | 20dfdad | 2007-05-22 11:50:17 -0400 | [diff] [blame] | 1065 | done: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1066 | dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1067 | return status; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1068 | } |
| 1069 | |
Alan Stern | 65605ae | 2008-08-12 14:33:27 -0400 | [diff] [blame] | 1070 | static int usb_suspend_interface(struct usb_device *udev, |
| 1071 | struct usb_interface *intf, pm_message_t msg) |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1072 | { |
| 1073 | struct usb_driver *driver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1074 | int status = 0; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1075 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1076 | if (udev->state == USB_STATE_NOTATTACHED || |
| 1077 | intf->condition == USB_INTERFACE_UNBOUND) |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1078 | goto done; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1079 | driver = to_usb_driver(intf->dev.driver); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1080 | |
Oliver Neukum | e78832c | 2012-01-02 15:11:48 +0100 | [diff] [blame] | 1081 | /* at this time we know the driver supports suspend */ |
| 1082 | status = driver->suspend(intf, msg); |
| 1083 | if (status && !PMSG_IS_AUTO(msg)) |
| 1084 | dev_err(&intf->dev, "suspend error %d\n", status); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1085 | |
Alan Stern | 20dfdad | 2007-05-22 11:50:17 -0400 | [diff] [blame] | 1086 | done: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1087 | dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1088 | return status; |
| 1089 | } |
| 1090 | |
Alan Stern | 65605ae | 2008-08-12 14:33:27 -0400 | [diff] [blame] | 1091 | static int usb_resume_interface(struct usb_device *udev, |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1092 | struct usb_interface *intf, pm_message_t msg, int reset_resume) |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1093 | { |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1094 | struct usb_driver *driver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1095 | int status = 0; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1096 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1097 | if (udev->state == USB_STATE_NOTATTACHED) |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1098 | goto done; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1099 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1100 | /* Don't let autoresume interfere with unbinding */ |
| 1101 | if (intf->condition == USB_INTERFACE_UNBINDING) |
| 1102 | goto done; |
| 1103 | |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 1104 | /* Can't resume it if it doesn't have a driver. */ |
Alan Stern | 55151d7 | 2008-08-12 14:33:59 -0400 | [diff] [blame] | 1105 | if (intf->condition == USB_INTERFACE_UNBOUND) { |
| 1106 | |
| 1107 | /* Carry out a deferred switch to altsetting 0 */ |
Alan Stern | f76b168b | 2011-06-18 20:22:23 +0200 | [diff] [blame] | 1108 | if (intf->needs_altsetting0 && !intf->dev.power.is_prepared) { |
Alan Stern | 55151d7 | 2008-08-12 14:33:59 -0400 | [diff] [blame] | 1109 | usb_set_interface(udev, intf->altsetting[0]. |
| 1110 | desc.bInterfaceNumber, 0); |
| 1111 | intf->needs_altsetting0 = 0; |
| 1112 | } |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1113 | goto done; |
Alan Stern | 55151d7 | 2008-08-12 14:33:59 -0400 | [diff] [blame] | 1114 | } |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 1115 | |
| 1116 | /* Don't resume if the interface is marked for rebinding */ |
| 1117 | if (intf->needs_binding) |
| 1118 | goto done; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1119 | driver = to_usb_driver(intf->dev.driver); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1120 | |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 1121 | if (reset_resume) { |
| 1122 | if (driver->reset_resume) { |
| 1123 | status = driver->reset_resume(intf); |
| 1124 | if (status) |
| 1125 | dev_err(&intf->dev, "%s error %d\n", |
| 1126 | "reset_resume", status); |
| 1127 | } else { |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 1128 | intf->needs_binding = 1; |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 1129 | dev_warn(&intf->dev, "no %s for driver %s?\n", |
| 1130 | "reset_resume", driver->name); |
| 1131 | } |
| 1132 | } else { |
Oliver Neukum | e78832c | 2012-01-02 15:11:48 +0100 | [diff] [blame] | 1133 | status = driver->resume(intf); |
| 1134 | if (status) |
| 1135 | dev_err(&intf->dev, "resume error %d\n", status); |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 1136 | } |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1137 | |
| 1138 | done: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1139 | dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 1140 | |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 1141 | /* Later we will unbind the driver and/or reprobe, if necessary */ |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 1142 | return status; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1143 | } |
| 1144 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1145 | /** |
| 1146 | * usb_suspend_both - suspend a USB device and its interfaces |
| 1147 | * @udev: the usb_device to suspend |
| 1148 | * @msg: Power Management message describing this state transition |
| 1149 | * |
| 1150 | * This is the central routine for suspending USB devices. It calls the |
| 1151 | * suspend methods for all the interface drivers in @udev and then calls |
| 1152 | * the suspend method for @udev itself. If an error occurs at any stage, |
| 1153 | * all the interfaces which were suspended are resumed so that they remain |
| 1154 | * in the same state as the device. |
| 1155 | * |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1156 | * Autosuspend requests originating from a child device or an interface |
| 1157 | * driver may be made without the protection of @udev's device lock, but |
| 1158 | * all other suspend calls will hold the lock. Usbcore will insure that |
| 1159 | * method calls do not arrive during bind, unbind, or reset operations. |
| 1160 | * However drivers must be prepared to handle suspend calls arriving at |
| 1161 | * unpredictable times. |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1162 | * |
| 1163 | * This routine can run only in process context. |
| 1164 | */ |
Alan Stern | 718efa6 | 2007-03-09 15:41:13 -0500 | [diff] [blame] | 1165 | static int usb_suspend_both(struct usb_device *udev, pm_message_t msg) |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1166 | { |
| 1167 | int status = 0; |
Alan Stern | 571dc79 | 2010-04-09 16:03:43 -0400 | [diff] [blame] | 1168 | int i = 0, n = 0; |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1169 | struct usb_interface *intf; |
| 1170 | |
Alan Stern | 1941044 | 2007-03-27 13:33:59 -0400 | [diff] [blame] | 1171 | if (udev->state == USB_STATE_NOTATTACHED || |
| 1172 | udev->state == USB_STATE_SUSPENDED) |
| 1173 | goto done; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1174 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1175 | /* Suspend all the interfaces and then udev itself */ |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1176 | if (udev->actconfig) { |
Alan Stern | 571dc79 | 2010-04-09 16:03:43 -0400 | [diff] [blame] | 1177 | n = udev->actconfig->desc.bNumInterfaces; |
| 1178 | for (i = n - 1; i >= 0; --i) { |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1179 | intf = udev->actconfig->interface[i]; |
Alan Stern | 65605ae | 2008-08-12 14:33:27 -0400 | [diff] [blame] | 1180 | status = usb_suspend_interface(udev, intf, msg); |
Alan Stern | 0af212b | 2011-06-15 16:27:43 -0400 | [diff] [blame] | 1181 | |
| 1182 | /* Ignore errors during system sleep transitions */ |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 1183 | if (!PMSG_IS_AUTO(msg)) |
Alan Stern | 0af212b | 2011-06-15 16:27:43 -0400 | [diff] [blame] | 1184 | status = 0; |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1185 | if (status != 0) |
| 1186 | break; |
| 1187 | } |
| 1188 | } |
Alan Stern | 0af212b | 2011-06-15 16:27:43 -0400 | [diff] [blame] | 1189 | if (status == 0) { |
Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1190 | status = usb_suspend_device(udev, msg); |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1191 | |
Alan Stern | cd4376e | 2012-03-28 15:56:17 -0400 | [diff] [blame] | 1192 | /* |
| 1193 | * Ignore errors from non-root-hub devices during |
| 1194 | * system sleep transitions. For the most part, |
| 1195 | * these devices should go to low power anyway when |
| 1196 | * the entire bus is suspended. |
| 1197 | */ |
| 1198 | if (udev->parent && !PMSG_IS_AUTO(msg)) |
Alan Stern | 0af212b | 2011-06-15 16:27:43 -0400 | [diff] [blame] | 1199 | status = 0; |
| 1200 | } |
| 1201 | |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1202 | /* If the suspend failed, resume interfaces that did get suspended */ |
| 1203 | if (status != 0) { |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1204 | msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME); |
Alan Stern | 571dc79 | 2010-04-09 16:03:43 -0400 | [diff] [blame] | 1205 | while (++i < n) { |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1206 | intf = udev->actconfig->interface[i]; |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1207 | usb_resume_interface(udev, intf, msg, 0); |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1208 | } |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1209 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1210 | /* If the suspend succeeded then prevent any more URB submissions |
| 1211 | * and flush any outstanding URBs. |
Alan Stern | 6840d25 | 2007-09-10 11:34:26 -0400 | [diff] [blame] | 1212 | */ |
Alan Stern | ef7f6c7 | 2007-04-05 16:03:49 -0400 | [diff] [blame] | 1213 | } else { |
Alan Stern | 6840d25 | 2007-09-10 11:34:26 -0400 | [diff] [blame] | 1214 | udev->can_submit = 0; |
| 1215 | for (i = 0; i < 16; ++i) { |
| 1216 | usb_hcd_flush_endpoint(udev, udev->ep_out[i]); |
| 1217 | usb_hcd_flush_endpoint(udev, udev->ep_in[i]); |
| 1218 | } |
Alan Stern | ef7f6c7 | 2007-04-05 16:03:49 -0400 | [diff] [blame] | 1219 | } |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1220 | |
Alan Stern | 1941044 | 2007-03-27 13:33:59 -0400 | [diff] [blame] | 1221 | done: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1222 | dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1223 | return status; |
| 1224 | } |
| 1225 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1226 | /** |
| 1227 | * usb_resume_both - resume a USB device and its interfaces |
| 1228 | * @udev: the usb_device to resume |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1229 | * @msg: Power Management message describing this state transition |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1230 | * |
| 1231 | * This is the central routine for resuming USB devices. It calls the |
| 1232 | * the resume method for @udev and then calls the resume methods for all |
| 1233 | * the interface drivers in @udev. |
| 1234 | * |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1235 | * Autoresume requests originating from a child device or an interface |
| 1236 | * driver may be made without the protection of @udev's device lock, but |
| 1237 | * all other resume calls will hold the lock. Usbcore will insure that |
| 1238 | * method calls do not arrive during bind, unbind, or reset operations. |
| 1239 | * However drivers must be prepared to handle resume calls arriving at |
| 1240 | * unpredictable times. |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1241 | * |
| 1242 | * This routine can run only in process context. |
| 1243 | */ |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1244 | static int usb_resume_both(struct usb_device *udev, pm_message_t msg) |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1245 | { |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1246 | int status = 0; |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1247 | int i; |
| 1248 | struct usb_interface *intf; |
| 1249 | |
Alan Stern | 1941044 | 2007-03-27 13:33:59 -0400 | [diff] [blame] | 1250 | if (udev->state == USB_STATE_NOTATTACHED) { |
| 1251 | status = -ENODEV; |
| 1252 | goto done; |
| 1253 | } |
Alan Stern | 6840d25 | 2007-09-10 11:34:26 -0400 | [diff] [blame] | 1254 | udev->can_submit = 1; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1255 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1256 | /* Resume the device */ |
| 1257 | if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume) |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1258 | status = usb_resume_device(udev, msg); |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 1259 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1260 | /* Resume the interfaces */ |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1261 | if (status == 0 && udev->actconfig) { |
| 1262 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { |
| 1263 | intf = udev->actconfig->interface[i]; |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 1264 | usb_resume_interface(udev, intf, msg, |
| 1265 | udev->reset_resume); |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1266 | } |
| 1267 | } |
Alan Stern | c08512c | 2010-11-15 15:57:58 -0500 | [diff] [blame] | 1268 | usb_mark_last_busy(udev); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1269 | |
Alan Stern | 1941044 | 2007-03-27 13:33:59 -0400 | [diff] [blame] | 1270 | done: |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 1271 | dev_vdbg(&udev->dev, "%s: status %d\n", __func__, status); |
Alan Stern | 70a1c9e | 2008-03-06 17:00:58 -0500 | [diff] [blame] | 1272 | if (!status) |
| 1273 | udev->reset_resume = 0; |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1274 | return status; |
| 1275 | } |
| 1276 | |
Alan Stern | 5f677f1 | 2010-04-02 13:20:11 -0400 | [diff] [blame] | 1277 | static void choose_wakeup(struct usb_device *udev, pm_message_t msg) |
| 1278 | { |
Alan Stern | 4882662 | 2010-06-22 16:14:48 -0400 | [diff] [blame] | 1279 | int w; |
Alan Stern | 5f677f1 | 2010-04-02 13:20:11 -0400 | [diff] [blame] | 1280 | |
| 1281 | /* Remote wakeup is needed only when we actually go to sleep. |
| 1282 | * For things like FREEZE and QUIESCE, if the device is already |
| 1283 | * autosuspended then its current wakeup setting is okay. |
| 1284 | */ |
| 1285 | if (msg.event == PM_EVENT_FREEZE || msg.event == PM_EVENT_QUIESCE) { |
| 1286 | if (udev->state != USB_STATE_SUSPENDED) |
| 1287 | udev->do_remote_wakeup = 0; |
| 1288 | return; |
| 1289 | } |
| 1290 | |
Alan Stern | 4882662 | 2010-06-22 16:14:48 -0400 | [diff] [blame] | 1291 | /* Enable remote wakeup if it is allowed, even if no interface drivers |
Alan Stern | 5f677f1 | 2010-04-02 13:20:11 -0400 | [diff] [blame] | 1292 | * actually want it. |
| 1293 | */ |
Alan Stern | 4882662 | 2010-06-22 16:14:48 -0400 | [diff] [blame] | 1294 | w = device_may_wakeup(&udev->dev); |
Alan Stern | 5f677f1 | 2010-04-02 13:20:11 -0400 | [diff] [blame] | 1295 | |
| 1296 | /* If the device is autosuspended with the wrong wakeup setting, |
| 1297 | * autoresume now so the setting can be changed. |
| 1298 | */ |
| 1299 | if (udev->state == USB_STATE_SUSPENDED && w != udev->do_remote_wakeup) |
| 1300 | pm_runtime_resume(&udev->dev); |
| 1301 | udev->do_remote_wakeup = w; |
| 1302 | } |
| 1303 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1304 | /* The device lock is held by the PM core */ |
Alan Stern | 0c590e2 | 2010-01-08 12:57:14 -0500 | [diff] [blame] | 1305 | int usb_suspend(struct device *dev, pm_message_t msg) |
| 1306 | { |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1307 | struct usb_device *udev = to_usb_device(dev); |
Alan Stern | 0c590e2 | 2010-01-08 12:57:14 -0500 | [diff] [blame] | 1308 | |
Oliver Neukum | 1493138 | 2012-01-05 15:39:57 +0100 | [diff] [blame] | 1309 | unbind_no_pm_drivers_interfaces(udev); |
| 1310 | |
| 1311 | /* From now on we are sure all drivers support suspend/resume |
| 1312 | * but not necessarily reset_resume() |
| 1313 | * so we may still need to unbind and rebind upon resume |
| 1314 | */ |
Alan Stern | 5f677f1 | 2010-04-02 13:20:11 -0400 | [diff] [blame] | 1315 | choose_wakeup(udev, msg); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1316 | return usb_suspend_both(udev, msg); |
Alan Stern | 0c590e2 | 2010-01-08 12:57:14 -0500 | [diff] [blame] | 1317 | } |
| 1318 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1319 | /* The device lock is held by the PM core */ |
Oliver Neukum | 98d9a82 | 2012-01-11 08:38:35 +0100 | [diff] [blame] | 1320 | int usb_resume_complete(struct device *dev) |
| 1321 | { |
| 1322 | struct usb_device *udev = to_usb_device(dev); |
| 1323 | |
| 1324 | /* For PM complete calls, all we do is rebind interfaces |
| 1325 | * whose needs_binding flag is set |
| 1326 | */ |
| 1327 | if (udev->state != USB_STATE_NOTATTACHED) |
| 1328 | do_rebind_interfaces(udev); |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | /* The device lock is held by the PM core */ |
Alan Stern | 0c590e2 | 2010-01-08 12:57:14 -0500 | [diff] [blame] | 1333 | int usb_resume(struct device *dev, pm_message_t msg) |
| 1334 | { |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1335 | struct usb_device *udev = to_usb_device(dev); |
Alan Stern | 0c590e2 | 2010-01-08 12:57:14 -0500 | [diff] [blame] | 1336 | int status; |
| 1337 | |
Oliver Neukum | 98d9a82 | 2012-01-11 08:38:35 +0100 | [diff] [blame] | 1338 | /* For all calls, take the device back to full power and |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1339 | * tell the PM core in case it was autosuspended previously. |
Oliver Neukum | 1493138 | 2012-01-05 15:39:57 +0100 | [diff] [blame] | 1340 | * Unbind the interfaces that will need rebinding later, |
| 1341 | * because they fail to support reset_resume. |
| 1342 | * (This can't be done in usb_resume_interface() |
Oliver Neukum | 98d9a82 | 2012-01-11 08:38:35 +0100 | [diff] [blame] | 1343 | * above because it doesn't own the right set of locks.) |
Alan Stern | 0c590e2 | 2010-01-08 12:57:14 -0500 | [diff] [blame] | 1344 | */ |
Oliver Neukum | 98d9a82 | 2012-01-11 08:38:35 +0100 | [diff] [blame] | 1345 | status = usb_resume_both(udev, msg); |
| 1346 | if (status == 0) { |
| 1347 | pm_runtime_disable(dev); |
| 1348 | pm_runtime_set_active(dev); |
| 1349 | pm_runtime_enable(dev); |
| 1350 | unbind_no_reset_resume_drivers_interfaces(udev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1351 | } |
Alan Stern | 0c590e2 | 2010-01-08 12:57:14 -0500 | [diff] [blame] | 1352 | |
| 1353 | /* Avoid PM error messages for devices disconnected while suspended |
| 1354 | * as we'll display regular disconnect messages just a bit later. |
| 1355 | */ |
Peter Chen | 7491f13 | 2010-09-27 16:43:25 +0800 | [diff] [blame] | 1356 | if (status == -ENODEV || status == -ESHUTDOWN) |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1357 | status = 0; |
Alan Stern | 0c590e2 | 2010-01-08 12:57:14 -0500 | [diff] [blame] | 1358 | return status; |
| 1359 | } |
| 1360 | |
| 1361 | #endif /* CONFIG_PM */ |
| 1362 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1363 | #ifdef CONFIG_USB_SUSPEND |
| 1364 | |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 1365 | /** |
| 1366 | * usb_enable_autosuspend - allow a USB device to be autosuspended |
| 1367 | * @udev: the USB device which may be autosuspended |
| 1368 | * |
| 1369 | * This routine allows @udev to be autosuspended. An autosuspend won't |
| 1370 | * take place until the autosuspend_delay has elapsed and all the other |
| 1371 | * necessary conditions are satisfied. |
| 1372 | * |
| 1373 | * The caller must hold @udev's device lock. |
| 1374 | */ |
Alan Stern | 9e18c82 | 2010-04-02 13:22:09 -0400 | [diff] [blame] | 1375 | void usb_enable_autosuspend(struct usb_device *udev) |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 1376 | { |
Alan Stern | 9e18c82 | 2010-04-02 13:22:09 -0400 | [diff] [blame] | 1377 | pm_runtime_allow(&udev->dev); |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 1378 | } |
| 1379 | EXPORT_SYMBOL_GPL(usb_enable_autosuspend); |
| 1380 | |
| 1381 | /** |
| 1382 | * usb_disable_autosuspend - prevent a USB device from being autosuspended |
| 1383 | * @udev: the USB device which may not be autosuspended |
| 1384 | * |
| 1385 | * This routine prevents @udev from being autosuspended and wakes it up |
| 1386 | * if it is already autosuspended. |
| 1387 | * |
| 1388 | * The caller must hold @udev's device lock. |
| 1389 | */ |
Alan Stern | 9e18c82 | 2010-04-02 13:22:09 -0400 | [diff] [blame] | 1390 | void usb_disable_autosuspend(struct usb_device *udev) |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 1391 | { |
Alan Stern | 9e18c82 | 2010-04-02 13:22:09 -0400 | [diff] [blame] | 1392 | pm_runtime_forbid(&udev->dev); |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 1393 | } |
| 1394 | EXPORT_SYMBOL_GPL(usb_disable_autosuspend); |
| 1395 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1396 | /** |
| 1397 | * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces |
Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1398 | * @udev: the usb_device to autosuspend |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1399 | * |
| 1400 | * This routine should be called when a core subsystem is finished using |
| 1401 | * @udev and wants to allow it to autosuspend. Examples would be when |
| 1402 | * @udev's device file in usbfs is closed or after a configuration change. |
| 1403 | * |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1404 | * @udev's usage counter is decremented; if it drops to 0 and all the |
| 1405 | * interfaces are inactive then a delayed autosuspend will be attempted. |
| 1406 | * The attempt may fail (see autosuspend_check()). |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1407 | * |
Alan Stern | 62e299e | 2010-01-08 12:56:19 -0500 | [diff] [blame] | 1408 | * The caller must hold @udev's device lock. |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1409 | * |
| 1410 | * This routine can run only in process context. |
| 1411 | */ |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1412 | void usb_autosuspend_device(struct usb_device *udev) |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1413 | { |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1414 | int status; |
| 1415 | |
Ming Lei | 6ddf27c | 2010-11-15 15:57:30 -0500 | [diff] [blame] | 1416 | usb_mark_last_busy(udev); |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 1417 | status = pm_runtime_put_sync_autosuspend(&udev->dev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1418 | dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", |
| 1419 | __func__, atomic_read(&udev->dev.power.usage_count), |
| 1420 | status); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /** |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1424 | * usb_autoresume_device - immediately autoresume a USB device and its interfaces |
Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1425 | * @udev: the usb_device to autoresume |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1426 | * |
| 1427 | * This routine should be called when a core subsystem wants to use @udev |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1428 | * and needs to guarantee that it is not suspended. No autosuspend will |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1429 | * occur until usb_autosuspend_device() is called. (Note that this will |
| 1430 | * not prevent suspend events originating in the PM core.) Examples would |
| 1431 | * be when @udev's device file in usbfs is opened or when a remote-wakeup |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1432 | * request is received. |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1433 | * |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1434 | * @udev's usage counter is incremented to prevent subsequent autosuspends. |
| 1435 | * However if the autoresume fails then the usage counter is re-decremented. |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1436 | * |
Alan Stern | 62e299e | 2010-01-08 12:56:19 -0500 | [diff] [blame] | 1437 | * The caller must hold @udev's device lock. |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1438 | * |
| 1439 | * This routine can run only in process context. |
| 1440 | */ |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1441 | int usb_autoresume_device(struct usb_device *udev) |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1442 | { |
| 1443 | int status; |
| 1444 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1445 | status = pm_runtime_get_sync(&udev->dev); |
| 1446 | if (status < 0) |
| 1447 | pm_runtime_put_sync(&udev->dev); |
| 1448 | dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", |
| 1449 | __func__, atomic_read(&udev->dev.power.usage_count), |
| 1450 | status); |
| 1451 | if (status > 0) |
| 1452 | status = 0; |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1453 | return status; |
| 1454 | } |
| 1455 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1456 | /** |
| 1457 | * usb_autopm_put_interface - decrement a USB interface's PM-usage counter |
Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1458 | * @intf: the usb_interface whose counter should be decremented |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1459 | * |
| 1460 | * This routine should be called by an interface driver when it is |
| 1461 | * finished using @intf and wants to allow it to autosuspend. A typical |
| 1462 | * example would be a character-device driver when its device file is |
| 1463 | * closed. |
| 1464 | * |
| 1465 | * The routine decrements @intf's usage counter. When the counter reaches |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1466 | * 0, a delayed autosuspend request for @intf's device is attempted. The |
| 1467 | * attempt may fail (see autosuspend_check()). |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1468 | * |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1469 | * This routine can run only in process context. |
| 1470 | */ |
| 1471 | void usb_autopm_put_interface(struct usb_interface *intf) |
| 1472 | { |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1473 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1474 | int status; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1475 | |
Ming Lei | 6ddf27c | 2010-11-15 15:57:30 -0500 | [diff] [blame] | 1476 | usb_mark_last_busy(udev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1477 | atomic_dec(&intf->pm_usage_cnt); |
| 1478 | status = pm_runtime_put_sync(&intf->dev); |
| 1479 | dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", |
| 1480 | __func__, atomic_read(&intf->dev.power.usage_count), |
| 1481 | status); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1482 | } |
| 1483 | EXPORT_SYMBOL_GPL(usb_autopm_put_interface); |
| 1484 | |
| 1485 | /** |
Alan Stern | 9ac39f2 | 2008-11-12 16:19:49 -0500 | [diff] [blame] | 1486 | * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter |
| 1487 | * @intf: the usb_interface whose counter should be decremented |
| 1488 | * |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1489 | * This routine does much the same thing as usb_autopm_put_interface(): |
| 1490 | * It decrements @intf's usage counter and schedules a delayed |
| 1491 | * autosuspend request if the counter is <= 0. The difference is that it |
| 1492 | * does not perform any synchronization; callers should hold a private |
| 1493 | * lock and handle all synchronization issues themselves. |
Alan Stern | 9ac39f2 | 2008-11-12 16:19:49 -0500 | [diff] [blame] | 1494 | * |
| 1495 | * Typically a driver would call this routine during an URB's completion |
| 1496 | * handler, if no more URBs were pending. |
| 1497 | * |
| 1498 | * This routine can run in atomic context. |
| 1499 | */ |
| 1500 | void usb_autopm_put_interface_async(struct usb_interface *intf) |
| 1501 | { |
| 1502 | struct usb_device *udev = interface_to_usbdev(intf); |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 1503 | int status; |
Alan Stern | 9ac39f2 | 2008-11-12 16:19:49 -0500 | [diff] [blame] | 1504 | |
Ming Lei | 6ddf27c | 2010-11-15 15:57:30 -0500 | [diff] [blame] | 1505 | usb_mark_last_busy(udev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1506 | atomic_dec(&intf->pm_usage_cnt); |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 1507 | status = pm_runtime_put(&intf->dev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1508 | dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", |
| 1509 | __func__, atomic_read(&intf->dev.power.usage_count), |
| 1510 | status); |
Alan Stern | 9ac39f2 | 2008-11-12 16:19:49 -0500 | [diff] [blame] | 1511 | } |
| 1512 | EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async); |
| 1513 | |
| 1514 | /** |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1515 | * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter |
| 1516 | * @intf: the usb_interface whose counter should be decremented |
| 1517 | * |
| 1518 | * This routine decrements @intf's usage counter but does not carry out an |
| 1519 | * autosuspend. |
| 1520 | * |
| 1521 | * This routine can run in atomic context. |
| 1522 | */ |
| 1523 | void usb_autopm_put_interface_no_suspend(struct usb_interface *intf) |
| 1524 | { |
| 1525 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1526 | |
Ming Lei | 6ddf27c | 2010-11-15 15:57:30 -0500 | [diff] [blame] | 1527 | usb_mark_last_busy(udev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1528 | atomic_dec(&intf->pm_usage_cnt); |
| 1529 | pm_runtime_put_noidle(&intf->dev); |
| 1530 | } |
| 1531 | EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend); |
| 1532 | |
| 1533 | /** |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1534 | * usb_autopm_get_interface - increment a USB interface's PM-usage counter |
Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1535 | * @intf: the usb_interface whose counter should be incremented |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1536 | * |
| 1537 | * This routine should be called by an interface driver when it wants to |
| 1538 | * use @intf and needs to guarantee that it is not suspended. In addition, |
| 1539 | * the routine prevents @intf from being autosuspended subsequently. (Note |
| 1540 | * that this will not prevent suspend events originating in the PM core.) |
| 1541 | * This prevention will persist until usb_autopm_put_interface() is called |
| 1542 | * or @intf is unbound. A typical example would be a character-device |
| 1543 | * driver when its device file is opened. |
| 1544 | * |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1545 | * @intf's usage counter is incremented to prevent subsequent autosuspends. |
| 1546 | * However if the autoresume fails then the counter is re-decremented. |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1547 | * |
| 1548 | * This routine can run only in process context. |
| 1549 | */ |
| 1550 | int usb_autopm_get_interface(struct usb_interface *intf) |
| 1551 | { |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1552 | int status; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1553 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1554 | status = pm_runtime_get_sync(&intf->dev); |
| 1555 | if (status < 0) |
| 1556 | pm_runtime_put_sync(&intf->dev); |
| 1557 | else |
| 1558 | atomic_inc(&intf->pm_usage_cnt); |
| 1559 | dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", |
| 1560 | __func__, atomic_read(&intf->dev.power.usage_count), |
| 1561 | status); |
| 1562 | if (status > 0) |
| 1563 | status = 0; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1564 | return status; |
| 1565 | } |
| 1566 | EXPORT_SYMBOL_GPL(usb_autopm_get_interface); |
| 1567 | |
Alan Stern | 692a186 | 2006-10-30 17:07:51 -0500 | [diff] [blame] | 1568 | /** |
Alan Stern | 9ac39f2 | 2008-11-12 16:19:49 -0500 | [diff] [blame] | 1569 | * usb_autopm_get_interface_async - increment a USB interface's PM-usage counter |
| 1570 | * @intf: the usb_interface whose counter should be incremented |
| 1571 | * |
| 1572 | * This routine does much the same thing as |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1573 | * usb_autopm_get_interface(): It increments @intf's usage counter and |
| 1574 | * queues an autoresume request if the device is suspended. The |
| 1575 | * differences are that it does not perform any synchronization (callers |
| 1576 | * should hold a private lock and handle all synchronization issues |
| 1577 | * themselves), and it does not autoresume the device directly (it only |
| 1578 | * queues a request). After a successful call, the device may not yet be |
| 1579 | * resumed. |
Alan Stern | 9ac39f2 | 2008-11-12 16:19:49 -0500 | [diff] [blame] | 1580 | * |
| 1581 | * This routine can run in atomic context. |
| 1582 | */ |
| 1583 | int usb_autopm_get_interface_async(struct usb_interface *intf) |
| 1584 | { |
Ming Lei | 63defa7 | 2010-11-15 15:56:54 -0500 | [diff] [blame] | 1585 | int status; |
Alan Stern | 9ac39f2 | 2008-11-12 16:19:49 -0500 | [diff] [blame] | 1586 | |
Ming Lei | 63defa7 | 2010-11-15 15:56:54 -0500 | [diff] [blame] | 1587 | status = pm_runtime_get(&intf->dev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1588 | if (status < 0 && status != -EINPROGRESS) |
| 1589 | pm_runtime_put_noidle(&intf->dev); |
| 1590 | else |
Alan Stern | ccf5b80 | 2009-06-29 11:00:01 -0400 | [diff] [blame] | 1591 | atomic_inc(&intf->pm_usage_cnt); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1592 | dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", |
| 1593 | __func__, atomic_read(&intf->dev.power.usage_count), |
| 1594 | status); |
Jim Wylder | c5a4859 | 2011-09-06 21:07:20 -0500 | [diff] [blame] | 1595 | if (status > 0 || status == -EINPROGRESS) |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1596 | status = 0; |
Alan Stern | 9ac39f2 | 2008-11-12 16:19:49 -0500 | [diff] [blame] | 1597 | return status; |
| 1598 | } |
| 1599 | EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async); |
| 1600 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1601 | /** |
| 1602 | * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter |
| 1603 | * @intf: the usb_interface whose counter should be incremented |
| 1604 | * |
| 1605 | * This routine increments @intf's usage counter but does not carry out an |
| 1606 | * autoresume. |
| 1607 | * |
| 1608 | * This routine can run in atomic context. |
| 1609 | */ |
| 1610 | void usb_autopm_get_interface_no_resume(struct usb_interface *intf) |
| 1611 | { |
| 1612 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1613 | |
Ming Lei | 6ddf27c | 2010-11-15 15:57:30 -0500 | [diff] [blame] | 1614 | usb_mark_last_busy(udev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1615 | atomic_inc(&intf->pm_usage_cnt); |
| 1616 | pm_runtime_get_noresume(&intf->dev); |
| 1617 | } |
| 1618 | EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume); |
| 1619 | |
| 1620 | /* Internal routine to check whether we may autosuspend a device. */ |
| 1621 | static int autosuspend_check(struct usb_device *udev) |
| 1622 | { |
Alan Stern | 7560d32e | 2010-04-02 13:18:50 -0400 | [diff] [blame] | 1623 | int w, i; |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1624 | struct usb_interface *intf; |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1625 | |
| 1626 | /* Fail if autosuspend is disabled, or any interfaces are in use, or |
| 1627 | * any interface drivers require remote wakeup but it isn't available. |
| 1628 | */ |
Alan Stern | 7560d32e | 2010-04-02 13:18:50 -0400 | [diff] [blame] | 1629 | w = 0; |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1630 | if (udev->actconfig) { |
| 1631 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { |
| 1632 | intf = udev->actconfig->interface[i]; |
| 1633 | |
| 1634 | /* We don't need to check interfaces that are |
| 1635 | * disabled for runtime PM. Either they are unbound |
| 1636 | * or else their drivers don't support autosuspend |
| 1637 | * and so they are permanently active. |
| 1638 | */ |
| 1639 | if (intf->dev.power.disable_depth) |
| 1640 | continue; |
| 1641 | if (atomic_read(&intf->dev.power.usage_count) > 0) |
| 1642 | return -EBUSY; |
Alan Stern | 7560d32e | 2010-04-02 13:18:50 -0400 | [diff] [blame] | 1643 | w |= intf->needs_remote_wakeup; |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1644 | |
| 1645 | /* Don't allow autosuspend if the device will need |
| 1646 | * a reset-resume and any of its interface drivers |
| 1647 | * doesn't include support or needs remote wakeup. |
| 1648 | */ |
| 1649 | if (udev->quirks & USB_QUIRK_RESET_RESUME) { |
| 1650 | struct usb_driver *driver; |
| 1651 | |
| 1652 | driver = to_usb_driver(intf->dev.driver); |
| 1653 | if (!driver->reset_resume || |
| 1654 | intf->needs_remote_wakeup) |
| 1655 | return -EOPNOTSUPP; |
| 1656 | } |
| 1657 | } |
| 1658 | } |
Alan Stern | 7560d32e | 2010-04-02 13:18:50 -0400 | [diff] [blame] | 1659 | if (w && !device_can_wakeup(&udev->dev)) { |
| 1660 | dev_dbg(&udev->dev, "remote wakeup needed for autosuspend\n"); |
| 1661 | return -EOPNOTSUPP; |
| 1662 | } |
| 1663 | udev->do_remote_wakeup = w; |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1664 | return 0; |
| 1665 | } |
| 1666 | |
Rafael J. Wysocki | e1620d5 | 2011-03-18 19:55:36 +0100 | [diff] [blame] | 1667 | int usb_runtime_suspend(struct device *dev) |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1668 | { |
Ming Lei | 63defa7 | 2010-11-15 15:56:54 -0500 | [diff] [blame] | 1669 | struct usb_device *udev = to_usb_device(dev); |
| 1670 | int status; |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1671 | |
| 1672 | /* A USB device can be suspended if it passes the various autosuspend |
| 1673 | * checks. Runtime suspend for a USB device means suspending all the |
| 1674 | * interfaces and then the device itself. |
| 1675 | */ |
Ming Lei | 63defa7 | 2010-11-15 15:56:54 -0500 | [diff] [blame] | 1676 | if (autosuspend_check(udev) != 0) |
| 1677 | return -EAGAIN; |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1678 | |
Ming Lei | 63defa7 | 2010-11-15 15:56:54 -0500 | [diff] [blame] | 1679 | status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND); |
Alan Stern | b2c0a86 | 2011-11-04 00:52:46 +0100 | [diff] [blame] | 1680 | |
| 1681 | /* Allow a retry if autosuspend failed temporarily */ |
| 1682 | if (status == -EAGAIN || status == -EBUSY) |
| 1683 | usb_mark_last_busy(udev); |
| 1684 | |
Sarah Sharp | db7c7c0 | 2010-12-29 22:03:07 -0800 | [diff] [blame] | 1685 | /* The PM core reacts badly unless the return code is 0, |
| 1686 | * -EAGAIN, or -EBUSY, so always return -EBUSY on an error. |
| 1687 | */ |
| 1688 | if (status != 0) |
| 1689 | return -EBUSY; |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1690 | return status; |
| 1691 | } |
| 1692 | |
Rafael J. Wysocki | e1620d5 | 2011-03-18 19:55:36 +0100 | [diff] [blame] | 1693 | int usb_runtime_resume(struct device *dev) |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1694 | { |
Ming Lei | 63defa7 | 2010-11-15 15:56:54 -0500 | [diff] [blame] | 1695 | struct usb_device *udev = to_usb_device(dev); |
| 1696 | int status; |
| 1697 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1698 | /* Runtime resume for a USB device means resuming both the device |
| 1699 | * and all its interfaces. |
| 1700 | */ |
Ming Lei | 63defa7 | 2010-11-15 15:56:54 -0500 | [diff] [blame] | 1701 | status = usb_resume_both(udev, PMSG_AUTO_RESUME); |
Ming Lei | 63defa7 | 2010-11-15 15:56:54 -0500 | [diff] [blame] | 1702 | return status; |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1703 | } |
| 1704 | |
Rafael J. Wysocki | e1620d5 | 2011-03-18 19:55:36 +0100 | [diff] [blame] | 1705 | int usb_runtime_idle(struct device *dev) |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1706 | { |
Ming Lei | 63defa7 | 2010-11-15 15:56:54 -0500 | [diff] [blame] | 1707 | struct usb_device *udev = to_usb_device(dev); |
| 1708 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1709 | /* An idle USB device can be suspended if it passes the various |
Ming Lei | 63defa7 | 2010-11-15 15:56:54 -0500 | [diff] [blame] | 1710 | * autosuspend checks. |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1711 | */ |
Ming Lei | 63defa7 | 2010-11-15 15:56:54 -0500 | [diff] [blame] | 1712 | if (autosuspend_check(udev) == 0) |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 1713 | pm_runtime_autosuspend(dev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1714 | return 0; |
| 1715 | } |
| 1716 | |
Andiry Xu | 65580b43 | 2011-09-23 14:19:52 -0700 | [diff] [blame] | 1717 | int usb_set_usb2_hardware_lpm(struct usb_device *udev, int enable) |
| 1718 | { |
| 1719 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 1720 | int ret = -EPERM; |
| 1721 | |
| 1722 | if (hcd->driver->set_usb2_hw_lpm) { |
| 1723 | ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, enable); |
| 1724 | if (!ret) |
| 1725 | udev->usb2_hw_lpm_enabled = enable; |
| 1726 | } |
| 1727 | |
| 1728 | return ret; |
| 1729 | } |
| 1730 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1731 | #endif /* CONFIG_USB_SUSPEND */ |
| 1732 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1733 | struct bus_type usb_bus_type = { |
| 1734 | .name = "usb", |
| 1735 | .match = usb_device_match, |
| 1736 | .uevent = usb_uevent, |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1737 | }; |