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