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