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