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