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