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> |
| 26 | #include <linux/usb.h> |
Alan Stern | bd85928 | 2006-09-19 10:14:07 -0400 | [diff] [blame] | 27 | #include <linux/workqueue.h> |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 28 | #include "hcd.h" |
| 29 | #include "usb.h" |
| 30 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 31 | #ifdef CONFIG_HOTPLUG |
| 32 | |
| 33 | /* |
| 34 | * Adds a new dynamic USBdevice ID to this driver, |
| 35 | * and cause the driver to probe for all devices again. |
| 36 | */ |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 37 | ssize_t usb_store_new_id(struct usb_dynids *dynids, |
| 38 | struct device_driver *driver, |
| 39 | const char *buf, size_t count) |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 40 | { |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 41 | struct usb_dynid *dynid; |
| 42 | u32 idVendor = 0; |
| 43 | u32 idProduct = 0; |
| 44 | int fields = 0; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 45 | int retval = 0; |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 46 | |
| 47 | fields = sscanf(buf, "%x %x", &idVendor, &idProduct); |
| 48 | if (fields < 2) |
| 49 | return -EINVAL; |
| 50 | |
| 51 | dynid = kzalloc(sizeof(*dynid), GFP_KERNEL); |
| 52 | if (!dynid) |
| 53 | return -ENOMEM; |
| 54 | |
| 55 | INIT_LIST_HEAD(&dynid->node); |
| 56 | dynid->id.idVendor = idVendor; |
| 57 | dynid->id.idProduct = idProduct; |
| 58 | dynid->id.match_flags = USB_DEVICE_ID_MATCH_DEVICE; |
| 59 | |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 60 | spin_lock(&dynids->lock); |
| 61 | list_add_tail(&dynids->list, &dynid->node); |
| 62 | spin_unlock(&dynids->lock); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 63 | |
| 64 | if (get_driver(driver)) { |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 65 | retval = driver_attach(driver); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 66 | put_driver(driver); |
| 67 | } |
| 68 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 69 | if (retval) |
| 70 | return retval; |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 71 | return count; |
| 72 | } |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 73 | EXPORT_SYMBOL_GPL(usb_store_new_id); |
| 74 | |
| 75 | static ssize_t store_new_id(struct device_driver *driver, |
| 76 | const char *buf, size_t count) |
| 77 | { |
| 78 | struct usb_driver *usb_drv = to_usb_driver(driver); |
| 79 | |
| 80 | return usb_store_new_id(&usb_drv->dynids, driver, buf, count); |
| 81 | } |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 82 | static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id); |
| 83 | |
| 84 | static int usb_create_newid_file(struct usb_driver *usb_drv) |
| 85 | { |
| 86 | int error = 0; |
| 87 | |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 88 | if (usb_drv->no_dynamic_id) |
| 89 | goto exit; |
| 90 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 91 | if (usb_drv->probe != NULL) |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 92 | error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj, |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 93 | &driver_attr_new_id.attr); |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 94 | exit: |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 95 | return error; |
| 96 | } |
| 97 | |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 98 | static void usb_remove_newid_file(struct usb_driver *usb_drv) |
| 99 | { |
| 100 | if (usb_drv->no_dynamic_id) |
| 101 | return; |
| 102 | |
| 103 | if (usb_drv->probe != NULL) |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 104 | sysfs_remove_file(&usb_drv->drvwrap.driver.kobj, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 105 | &driver_attr_new_id.attr); |
| 106 | } |
| 107 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 108 | static void usb_free_dynids(struct usb_driver *usb_drv) |
| 109 | { |
| 110 | struct usb_dynid *dynid, *n; |
| 111 | |
| 112 | spin_lock(&usb_drv->dynids.lock); |
| 113 | list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) { |
| 114 | list_del(&dynid->node); |
| 115 | kfree(dynid); |
| 116 | } |
| 117 | spin_unlock(&usb_drv->dynids.lock); |
| 118 | } |
| 119 | #else |
| 120 | static inline int usb_create_newid_file(struct usb_driver *usb_drv) |
| 121 | { |
| 122 | return 0; |
| 123 | } |
| 124 | |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 125 | static void usb_remove_newid_file(struct usb_driver *usb_drv) |
| 126 | { |
| 127 | } |
| 128 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 129 | static inline void usb_free_dynids(struct usb_driver *usb_drv) |
| 130 | { |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf, |
| 135 | struct usb_driver *drv) |
| 136 | { |
| 137 | struct usb_dynid *dynid; |
| 138 | |
| 139 | spin_lock(&drv->dynids.lock); |
| 140 | list_for_each_entry(dynid, &drv->dynids.list, node) { |
| 141 | if (usb_match_one_id(intf, &dynid->id)) { |
| 142 | spin_unlock(&drv->dynids.lock); |
| 143 | return &dynid->id; |
| 144 | } |
| 145 | } |
| 146 | spin_unlock(&drv->dynids.lock); |
| 147 | return NULL; |
| 148 | } |
| 149 | |
| 150 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 151 | /* called from driver core with dev locked */ |
| 152 | static int usb_probe_device(struct device *dev) |
| 153 | { |
| 154 | struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); |
| 155 | struct usb_device *udev; |
| 156 | int error = -ENODEV; |
| 157 | |
| 158 | dev_dbg(dev, "%s\n", __FUNCTION__); |
| 159 | |
| 160 | if (!is_usb_device(dev)) /* Sanity check */ |
| 161 | return error; |
| 162 | |
| 163 | udev = to_usb_device(dev); |
| 164 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 165 | /* TODO: Add real matching code */ |
| 166 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 167 | /* The device should always appear to be in use |
| 168 | * unless the driver suports autosuspend. |
| 169 | */ |
| 170 | udev->pm_usage_cnt = !(udriver->supports_autosuspend); |
| 171 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 172 | error = udriver->probe(udev); |
| 173 | return error; |
| 174 | } |
| 175 | |
| 176 | /* called from driver core with dev locked */ |
| 177 | static int usb_unbind_device(struct device *dev) |
| 178 | { |
| 179 | struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); |
| 180 | |
| 181 | udriver->disconnect(to_usb_device(dev)); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /* called from driver core with dev locked */ |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 187 | static int usb_probe_interface(struct device *dev) |
| 188 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 189 | struct usb_driver *driver = to_usb_driver(dev->driver); |
| 190 | struct usb_interface *intf; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 191 | struct usb_device *udev; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 192 | const struct usb_device_id *id; |
| 193 | int error = -ENODEV; |
| 194 | |
| 195 | dev_dbg(dev, "%s\n", __FUNCTION__); |
| 196 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 197 | if (is_usb_device(dev)) /* Sanity check */ |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 198 | return error; |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 199 | |
| 200 | intf = to_usb_interface(dev); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 201 | udev = interface_to_usbdev(intf); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 202 | |
| 203 | id = usb_match_id(intf, driver->id_table); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 204 | if (!id) |
| 205 | id = usb_match_dynamic_id(intf, driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 206 | if (id) { |
| 207 | dev_dbg(dev, "%s - got id\n", __FUNCTION__); |
| 208 | |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 209 | error = usb_autoresume_device(udev); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 210 | if (error) |
| 211 | return error; |
| 212 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 213 | /* Interface "power state" doesn't correspond to any hardware |
| 214 | * state whatsoever. We use it to record when it's bound to |
| 215 | * a driver that may start I/0: it's not frozen/quiesced. |
| 216 | */ |
| 217 | mark_active(intf); |
| 218 | intf->condition = USB_INTERFACE_BINDING; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 219 | |
| 220 | /* The interface should always appear to be in use |
| 221 | * unless the driver suports autosuspend. |
| 222 | */ |
| 223 | intf->pm_usage_cnt = !(driver->supports_autosuspend); |
| 224 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 225 | error = driver->probe(intf, id); |
| 226 | if (error) { |
| 227 | mark_quiesced(intf); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 228 | intf->needs_remote_wakeup = 0; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 229 | intf->condition = USB_INTERFACE_UNBOUND; |
| 230 | } else |
| 231 | intf->condition = USB_INTERFACE_BOUND; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 232 | |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 233 | usb_autosuspend_device(udev); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | return error; |
| 237 | } |
| 238 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 239 | /* called from driver core with dev locked */ |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 240 | static int usb_unbind_interface(struct device *dev) |
| 241 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 242 | struct usb_driver *driver = to_usb_driver(dev->driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 243 | struct usb_interface *intf = to_usb_interface(dev); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 244 | struct usb_device *udev; |
| 245 | int error; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 246 | |
| 247 | intf->condition = USB_INTERFACE_UNBINDING; |
| 248 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 249 | /* Autoresume for set_interface call below */ |
| 250 | udev = interface_to_usbdev(intf); |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 251 | error = usb_autoresume_device(udev); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 252 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 253 | /* release all urbs for this interface */ |
| 254 | usb_disable_interface(interface_to_usbdev(intf), intf); |
| 255 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 256 | driver->disconnect(intf); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 257 | |
| 258 | /* reset other interface state */ |
| 259 | usb_set_interface(interface_to_usbdev(intf), |
| 260 | intf->altsetting[0].desc.bInterfaceNumber, |
| 261 | 0); |
| 262 | usb_set_intfdata(intf, NULL); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 263 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 264 | intf->condition = USB_INTERFACE_UNBOUND; |
| 265 | mark_quiesced(intf); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 266 | intf->needs_remote_wakeup = 0; |
| 267 | |
| 268 | if (!error) |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 269 | usb_autosuspend_device(udev); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 274 | /** |
| 275 | * usb_driver_claim_interface - bind a driver to an interface |
| 276 | * @driver: the driver to be bound |
| 277 | * @iface: the interface to which it will be bound; must be in the |
| 278 | * usb device's active configuration |
| 279 | * @priv: driver data associated with that interface |
| 280 | * |
| 281 | * This is used by usb device drivers that need to claim more than one |
| 282 | * interface on a device when probing (audio and acm are current examples). |
| 283 | * No device driver should directly modify internal usb_interface or |
| 284 | * usb_device structure members. |
| 285 | * |
| 286 | * Few drivers should need to use this routine, since the most natural |
| 287 | * way to bind to an interface is to return the private data from |
| 288 | * the driver's probe() method. |
| 289 | * |
Greg Kroah-Hartman | 341487a8 | 2007-04-09 11:52:31 -0400 | [diff] [blame] | 290 | * Callers must own the device lock, so driver probe() entries don't need |
| 291 | * extra locking, but other call contexts may need to explicitly claim that |
| 292 | * lock. |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 293 | */ |
| 294 | int usb_driver_claim_interface(struct usb_driver *driver, |
| 295 | struct usb_interface *iface, void* priv) |
| 296 | { |
| 297 | struct device *dev = &iface->dev; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 298 | struct usb_device *udev = interface_to_usbdev(iface); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 299 | int retval = 0; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 300 | |
| 301 | if (dev->driver) |
| 302 | return -EBUSY; |
| 303 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 304 | dev->driver = &driver->drvwrap.driver; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 305 | usb_set_intfdata(iface, priv); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 306 | |
Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 307 | usb_pm_lock(udev); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 308 | iface->condition = USB_INTERFACE_BOUND; |
| 309 | mark_active(iface); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 310 | iface->pm_usage_cnt = !(driver->supports_autosuspend); |
Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 311 | usb_pm_unlock(udev); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 312 | |
| 313 | /* if interface was already added, bind now; else let |
| 314 | * the future device_add() bind it, bypassing probe() |
| 315 | */ |
| 316 | if (device_is_registered(dev)) |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 317 | retval = device_bind_driver(dev); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 318 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 319 | return retval; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 320 | } |
| 321 | EXPORT_SYMBOL(usb_driver_claim_interface); |
| 322 | |
| 323 | /** |
| 324 | * usb_driver_release_interface - unbind a driver from an interface |
| 325 | * @driver: the driver to be unbound |
| 326 | * @iface: the interface from which it will be unbound |
| 327 | * |
| 328 | * This can be used by drivers to release an interface without waiting |
| 329 | * for their disconnect() methods to be called. In typical cases this |
| 330 | * also causes the driver disconnect() method to be called. |
| 331 | * |
| 332 | * 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] | 333 | * Callers must own the device lock, so driver disconnect() entries don't |
| 334 | * need extra locking, but other call contexts may need to explicitly claim |
| 335 | * that lock. |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 336 | */ |
| 337 | void usb_driver_release_interface(struct usb_driver *driver, |
| 338 | struct usb_interface *iface) |
| 339 | { |
| 340 | struct device *dev = &iface->dev; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 341 | struct usb_device *udev = interface_to_usbdev(iface); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 342 | |
| 343 | /* this should never happen, don't release something that's not ours */ |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 344 | if (!dev->driver || dev->driver != &driver->drvwrap.driver) |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 345 | return; |
| 346 | |
| 347 | /* don't release from within disconnect() */ |
| 348 | if (iface->condition != USB_INTERFACE_BOUND) |
| 349 | return; |
| 350 | |
| 351 | /* don't release if the interface hasn't been added yet */ |
| 352 | if (device_is_registered(dev)) { |
| 353 | iface->condition = USB_INTERFACE_UNBINDING; |
| 354 | device_release_driver(dev); |
| 355 | } |
| 356 | |
| 357 | dev->driver = NULL; |
| 358 | usb_set_intfdata(iface, NULL); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 359 | |
Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 360 | usb_pm_lock(udev); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 361 | iface->condition = USB_INTERFACE_UNBOUND; |
| 362 | mark_quiesced(iface); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 363 | iface->needs_remote_wakeup = 0; |
Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 364 | usb_pm_unlock(udev); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 365 | } |
| 366 | EXPORT_SYMBOL(usb_driver_release_interface); |
| 367 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 368 | /* returns 0 if no match, 1 if match */ |
Greg Kroah-Hartman | bb41702 | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 369 | 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] | 370 | { |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 371 | if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
| 372 | id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) |
| 373 | return 0; |
| 374 | |
| 375 | if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && |
| 376 | id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) |
| 377 | return 0; |
| 378 | |
| 379 | /* No need to test id->bcdDevice_lo != 0, since 0 is never |
| 380 | greater than any unsigned number. */ |
| 381 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && |
| 382 | (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) |
| 383 | return 0; |
| 384 | |
| 385 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && |
| 386 | (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) |
| 387 | return 0; |
| 388 | |
| 389 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && |
| 390 | (id->bDeviceClass != dev->descriptor.bDeviceClass)) |
| 391 | return 0; |
| 392 | |
| 393 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && |
| 394 | (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass)) |
| 395 | return 0; |
| 396 | |
| 397 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && |
| 398 | (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) |
| 399 | return 0; |
| 400 | |
Greg Kroah-Hartman | bb41702 | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 401 | return 1; |
| 402 | } |
| 403 | |
| 404 | /* returns 0 if no match, 1 if match */ |
| 405 | int usb_match_one_id(struct usb_interface *interface, |
| 406 | const struct usb_device_id *id) |
| 407 | { |
| 408 | struct usb_host_interface *intf; |
| 409 | struct usb_device *dev; |
| 410 | |
| 411 | /* proc_connectinfo in devio.c may call us with id == NULL. */ |
| 412 | if (id == NULL) |
| 413 | return 0; |
| 414 | |
| 415 | intf = interface->cur_altsetting; |
| 416 | dev = interface_to_usbdev(interface); |
| 417 | |
| 418 | if (!usb_match_device(dev, id)) |
| 419 | return 0; |
| 420 | |
Alan Stern | 93c8bf4 | 2006-10-18 16:41:51 -0400 | [diff] [blame] | 421 | /* The interface class, subclass, and protocol should never be |
| 422 | * checked for a match if the device class is Vendor Specific, |
| 423 | * unless the match record specifies the Vendor ID. */ |
| 424 | if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC && |
| 425 | !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
| 426 | (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | |
| 427 | USB_DEVICE_ID_MATCH_INT_SUBCLASS | |
| 428 | USB_DEVICE_ID_MATCH_INT_PROTOCOL))) |
| 429 | return 0; |
| 430 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 431 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && |
| 432 | (id->bInterfaceClass != intf->desc.bInterfaceClass)) |
| 433 | return 0; |
| 434 | |
| 435 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && |
| 436 | (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) |
| 437 | return 0; |
| 438 | |
| 439 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && |
| 440 | (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) |
| 441 | return 0; |
| 442 | |
| 443 | return 1; |
| 444 | } |
Greg Kroah-Hartman | 93bacef | 2006-12-17 21:50:23 +0100 | [diff] [blame] | 445 | EXPORT_SYMBOL_GPL(usb_match_one_id); |
| 446 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 447 | /** |
| 448 | * usb_match_id - find first usb_device_id matching device or interface |
| 449 | * @interface: the interface of interest |
| 450 | * @id: array of usb_device_id structures, terminated by zero entry |
| 451 | * |
| 452 | * usb_match_id searches an array of usb_device_id's and returns |
| 453 | * the first one matching the device or interface, or null. |
| 454 | * This is used when binding (or rebinding) a driver to an interface. |
| 455 | * Most USB device drivers will use this indirectly, through the usb core, |
| 456 | * but some layered driver frameworks use it directly. |
| 457 | * These device tables are exported with MODULE_DEVICE_TABLE, through |
| 458 | * modutils, to support the driver loading functionality of USB hotplugging. |
| 459 | * |
| 460 | * What Matches: |
| 461 | * |
| 462 | * The "match_flags" element in a usb_device_id controls which |
| 463 | * members are used. If the corresponding bit is set, the |
| 464 | * value in the device_id must match its corresponding member |
| 465 | * in the device or interface descriptor, or else the device_id |
| 466 | * does not match. |
| 467 | * |
| 468 | * "driver_info" is normally used only by device drivers, |
| 469 | * but you can create a wildcard "matches anything" usb_device_id |
| 470 | * as a driver's "modules.usbmap" entry if you provide an id with |
| 471 | * only a nonzero "driver_info" field. If you do this, the USB device |
| 472 | * driver's probe() routine should use additional intelligence to |
| 473 | * decide whether to bind to the specified interface. |
| 474 | * |
| 475 | * What Makes Good usb_device_id Tables: |
| 476 | * |
| 477 | * The match algorithm is very simple, so that intelligence in |
| 478 | * driver selection must come from smart driver id records. |
| 479 | * Unless you have good reasons to use another selection policy, |
| 480 | * provide match elements only in related groups, and order match |
| 481 | * specifiers from specific to general. Use the macros provided |
| 482 | * for that purpose if you can. |
| 483 | * |
| 484 | * The most specific match specifiers use device descriptor |
| 485 | * data. These are commonly used with product-specific matches; |
| 486 | * the USB_DEVICE macro lets you provide vendor and product IDs, |
| 487 | * and you can also match against ranges of product revisions. |
| 488 | * These are widely used for devices with application or vendor |
| 489 | * specific bDeviceClass values. |
| 490 | * |
| 491 | * Matches based on device class/subclass/protocol specifications |
| 492 | * are slightly more general; use the USB_DEVICE_INFO macro, or |
| 493 | * its siblings. These are used with single-function devices |
| 494 | * where bDeviceClass doesn't specify that each interface has |
| 495 | * its own class. |
| 496 | * |
| 497 | * Matches based on interface class/subclass/protocol are the |
| 498 | * most general; they let drivers bind to any interface on a |
| 499 | * multiple-function device. Use the USB_INTERFACE_INFO |
| 500 | * macro, or its siblings, to match class-per-interface style |
Alan Stern | 93c8bf4 | 2006-10-18 16:41:51 -0400 | [diff] [blame] | 501 | * devices (as recorded in bInterfaceClass). |
| 502 | * |
| 503 | * Note that an entry created by USB_INTERFACE_INFO won't match |
| 504 | * any interface if the device class is set to Vendor-Specific. |
| 505 | * This is deliberate; according to the USB spec the meanings of |
| 506 | * the interface class/subclass/protocol for these devices are also |
| 507 | * vendor-specific, and hence matching against a standard product |
| 508 | * class wouldn't work anyway. If you really want to use an |
| 509 | * interface-based match for such a device, create a match record |
| 510 | * that also specifies the vendor ID. (Unforunately there isn't a |
| 511 | * standard macro for creating records like this.) |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 512 | * |
| 513 | * Within those groups, remember that not all combinations are |
| 514 | * meaningful. For example, don't give a product version range |
| 515 | * without vendor and product IDs; or specify a protocol without |
| 516 | * its associated class and subclass. |
| 517 | */ |
| 518 | const struct usb_device_id *usb_match_id(struct usb_interface *interface, |
| 519 | const struct usb_device_id *id) |
| 520 | { |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 521 | /* proc_connectinfo in devio.c may call us with id == NULL. */ |
| 522 | if (id == NULL) |
| 523 | return NULL; |
| 524 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 525 | /* It is important to check that id->driver_info is nonzero, |
| 526 | since an entry that is all zeroes except for a nonzero |
| 527 | id->driver_info is the way to create an entry that |
| 528 | indicates that the driver want to examine every |
| 529 | device and interface. */ |
| 530 | for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass || |
| 531 | id->driver_info; id++) { |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 532 | if (usb_match_one_id(interface, id)) |
| 533 | return id; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | return NULL; |
| 537 | } |
Greg Kroah-Hartman | b87ba0a | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 538 | EXPORT_SYMBOL_GPL_FUTURE(usb_match_id); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 539 | |
Adrian Bunk | 8bb22d2 | 2006-11-21 22:02:54 +0100 | [diff] [blame] | 540 | 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] | 541 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 542 | /* devices and interfaces are handled separately */ |
| 543 | if (is_usb_device(dev)) { |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 544 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 545 | /* interface drivers never match devices */ |
| 546 | if (!is_usb_device_driver(drv)) |
| 547 | return 0; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 548 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 549 | /* TODO: Add real matching code */ |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 550 | return 1; |
| 551 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 552 | } else { |
| 553 | struct usb_interface *intf; |
| 554 | struct usb_driver *usb_drv; |
| 555 | const struct usb_device_id *id; |
| 556 | |
| 557 | /* device drivers never match interfaces */ |
| 558 | if (is_usb_device_driver(drv)) |
| 559 | return 0; |
| 560 | |
| 561 | intf = to_usb_interface(dev); |
| 562 | usb_drv = to_usb_driver(drv); |
| 563 | |
| 564 | id = usb_match_id(intf, usb_drv->id_table); |
| 565 | if (id) |
| 566 | return 1; |
| 567 | |
| 568 | id = usb_match_dynamic_id(intf, usb_drv); |
| 569 | if (id) |
| 570 | return 1; |
| 571 | } |
| 572 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 576 | #ifdef CONFIG_HOTPLUG |
| 577 | |
| 578 | /* |
| 579 | * This sends an uevent to userspace, typically helping to load driver |
| 580 | * or other modules, configure the device, and more. Drivers can provide |
| 581 | * a MODULE_DEVICE_TABLE to help with module loading subtasks. |
| 582 | * |
| 583 | * We're called either from khubd (the typical case) or from root hub |
| 584 | * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle |
| 585 | * delays in event delivery. Use sysfs (and DEVPATH) to make sure the |
| 586 | * device (and this configuration!) are still present. |
| 587 | */ |
| 588 | static int usb_uevent(struct device *dev, char **envp, int num_envp, |
| 589 | char *buffer, int buffer_size) |
| 590 | { |
| 591 | struct usb_interface *intf; |
| 592 | struct usb_device *usb_dev; |
| 593 | struct usb_host_interface *alt; |
| 594 | int i = 0; |
| 595 | int length = 0; |
| 596 | |
| 597 | if (!dev) |
| 598 | return -ENODEV; |
| 599 | |
| 600 | /* driver is often null here; dev_dbg() would oops */ |
| 601 | pr_debug ("usb %s: uevent\n", dev->bus_id); |
| 602 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 603 | if (is_usb_device(dev)) { |
| 604 | usb_dev = to_usb_device(dev); |
| 605 | alt = NULL; |
| 606 | } else { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 607 | intf = to_usb_interface(dev); |
| 608 | usb_dev = interface_to_usbdev(intf); |
| 609 | alt = intf->cur_altsetting; |
| 610 | } |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 611 | |
| 612 | if (usb_dev->devnum < 0) { |
| 613 | pr_debug ("usb %s: already deleted?\n", dev->bus_id); |
| 614 | return -ENODEV; |
| 615 | } |
| 616 | if (!usb_dev->bus) { |
| 617 | pr_debug ("usb %s: bus removed?\n", dev->bus_id); |
| 618 | return -ENODEV; |
| 619 | } |
| 620 | |
| 621 | #ifdef CONFIG_USB_DEVICEFS |
| 622 | /* If this is available, userspace programs can directly read |
| 623 | * all the device descriptors we don't tell them about. Or |
| 624 | * even act as usermode drivers. |
| 625 | * |
| 626 | * FIXME reduce hardwired intelligence here |
| 627 | */ |
| 628 | if (add_uevent_var(envp, num_envp, &i, |
| 629 | buffer, buffer_size, &length, |
| 630 | "DEVICE=/proc/bus/usb/%03d/%03d", |
| 631 | usb_dev->bus->busnum, usb_dev->devnum)) |
| 632 | return -ENOMEM; |
| 633 | #endif |
| 634 | |
| 635 | /* per-device configurations are common */ |
| 636 | if (add_uevent_var(envp, num_envp, &i, |
| 637 | buffer, buffer_size, &length, |
| 638 | "PRODUCT=%x/%x/%x", |
| 639 | le16_to_cpu(usb_dev->descriptor.idVendor), |
| 640 | le16_to_cpu(usb_dev->descriptor.idProduct), |
| 641 | le16_to_cpu(usb_dev->descriptor.bcdDevice))) |
| 642 | return -ENOMEM; |
| 643 | |
| 644 | /* class-based driver binding models */ |
| 645 | if (add_uevent_var(envp, num_envp, &i, |
| 646 | buffer, buffer_size, &length, |
| 647 | "TYPE=%d/%d/%d", |
| 648 | usb_dev->descriptor.bDeviceClass, |
| 649 | usb_dev->descriptor.bDeviceSubClass, |
| 650 | usb_dev->descriptor.bDeviceProtocol)) |
| 651 | return -ENOMEM; |
| 652 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 653 | if (!is_usb_device(dev)) { |
| 654 | |
| 655 | if (add_uevent_var(envp, num_envp, &i, |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 656 | buffer, buffer_size, &length, |
| 657 | "INTERFACE=%d/%d/%d", |
| 658 | alt->desc.bInterfaceClass, |
| 659 | alt->desc.bInterfaceSubClass, |
| 660 | alt->desc.bInterfaceProtocol)) |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 661 | return -ENOMEM; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 662 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 663 | if (add_uevent_var(envp, num_envp, &i, |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 664 | buffer, buffer_size, &length, |
| 665 | "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X", |
| 666 | le16_to_cpu(usb_dev->descriptor.idVendor), |
| 667 | le16_to_cpu(usb_dev->descriptor.idProduct), |
| 668 | le16_to_cpu(usb_dev->descriptor.bcdDevice), |
| 669 | usb_dev->descriptor.bDeviceClass, |
| 670 | usb_dev->descriptor.bDeviceSubClass, |
| 671 | usb_dev->descriptor.bDeviceProtocol, |
| 672 | alt->desc.bInterfaceClass, |
| 673 | alt->desc.bInterfaceSubClass, |
| 674 | alt->desc.bInterfaceProtocol)) |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 675 | return -ENOMEM; |
| 676 | } |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 677 | |
| 678 | envp[i] = NULL; |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | #else |
| 684 | |
| 685 | static int usb_uevent(struct device *dev, char **envp, |
| 686 | int num_envp, char *buffer, int buffer_size) |
| 687 | { |
| 688 | return -ENODEV; |
| 689 | } |
| 690 | |
| 691 | #endif /* CONFIG_HOTPLUG */ |
| 692 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 693 | /** |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 694 | * usb_register_device_driver - register a USB device (not interface) driver |
| 695 | * @new_udriver: USB operations for the device driver |
Greg Kroah-Hartman | 2143acc | 2005-11-21 14:53:03 -0800 | [diff] [blame] | 696 | * @owner: module owner of this driver. |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 697 | * |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 698 | * Registers a USB device driver with the USB core. The list of |
| 699 | * unattached devices will be rescanned whenever a new driver is |
| 700 | * added, allowing the new driver to attach to any recognized devices. |
| 701 | * Returns a negative error code on failure and 0 on success. |
| 702 | */ |
| 703 | int usb_register_device_driver(struct usb_device_driver *new_udriver, |
| 704 | struct module *owner) |
| 705 | { |
| 706 | int retval = 0; |
| 707 | |
| 708 | if (usb_disabled()) |
| 709 | return -ENODEV; |
| 710 | |
| 711 | new_udriver->drvwrap.for_devices = 1; |
| 712 | new_udriver->drvwrap.driver.name = (char *) new_udriver->name; |
| 713 | new_udriver->drvwrap.driver.bus = &usb_bus_type; |
| 714 | new_udriver->drvwrap.driver.probe = usb_probe_device; |
| 715 | new_udriver->drvwrap.driver.remove = usb_unbind_device; |
| 716 | new_udriver->drvwrap.driver.owner = owner; |
| 717 | |
| 718 | retval = driver_register(&new_udriver->drvwrap.driver); |
| 719 | |
| 720 | if (!retval) { |
| 721 | pr_info("%s: registered new device driver %s\n", |
| 722 | usbcore_name, new_udriver->name); |
| 723 | usbfs_update_special(); |
| 724 | } else { |
| 725 | printk(KERN_ERR "%s: error %d registering device " |
| 726 | " driver %s\n", |
| 727 | usbcore_name, retval, new_udriver->name); |
| 728 | } |
| 729 | |
| 730 | return retval; |
| 731 | } |
| 732 | EXPORT_SYMBOL_GPL(usb_register_device_driver); |
| 733 | |
| 734 | /** |
| 735 | * usb_deregister_device_driver - unregister a USB device (not interface) driver |
| 736 | * @udriver: USB operations of the device driver to unregister |
| 737 | * Context: must be able to sleep |
| 738 | * |
| 739 | * Unlinks the specified driver from the internal USB driver list. |
| 740 | */ |
| 741 | void usb_deregister_device_driver(struct usb_device_driver *udriver) |
| 742 | { |
| 743 | pr_info("%s: deregistering device driver %s\n", |
| 744 | usbcore_name, udriver->name); |
| 745 | |
| 746 | driver_unregister(&udriver->drvwrap.driver); |
| 747 | usbfs_update_special(); |
| 748 | } |
| 749 | EXPORT_SYMBOL_GPL(usb_deregister_device_driver); |
| 750 | |
| 751 | /** |
| 752 | * usb_register_driver - register a USB interface driver |
| 753 | * @new_driver: USB operations for the interface driver |
| 754 | * @owner: module owner of this driver. |
Randy Dunlap | 892705a | 2007-02-10 14:41:41 -0800 | [diff] [blame] | 755 | * @mod_name: module name string |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 756 | * |
| 757 | * Registers a USB interface driver with the USB core. The list of |
| 758 | * unattached interfaces will be rescanned whenever a new driver is |
| 759 | * added, allowing the new driver to attach to any recognized interfaces. |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 760 | * Returns a negative error code on failure and 0 on success. |
| 761 | * |
| 762 | * NOTE: if you want your driver to use the USB major number, you must call |
| 763 | * usb_register_dev() to enable that functionality. This function no longer |
| 764 | * takes care of that. |
| 765 | */ |
Greg Kroah-Hartman | 80f745f | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 766 | int usb_register_driver(struct usb_driver *new_driver, struct module *owner, |
| 767 | const char *mod_name) |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 768 | { |
| 769 | int retval = 0; |
| 770 | |
| 771 | if (usb_disabled()) |
| 772 | return -ENODEV; |
| 773 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 774 | new_driver->drvwrap.for_devices = 0; |
| 775 | new_driver->drvwrap.driver.name = (char *) new_driver->name; |
| 776 | new_driver->drvwrap.driver.bus = &usb_bus_type; |
| 777 | new_driver->drvwrap.driver.probe = usb_probe_interface; |
| 778 | new_driver->drvwrap.driver.remove = usb_unbind_interface; |
| 779 | new_driver->drvwrap.driver.owner = owner; |
Greg Kroah-Hartman | 80f745f | 2007-01-15 11:50:02 -0800 | [diff] [blame] | 780 | new_driver->drvwrap.driver.mod_name = mod_name; |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 781 | spin_lock_init(&new_driver->dynids.lock); |
| 782 | INIT_LIST_HEAD(&new_driver->dynids.list); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 783 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 784 | retval = driver_register(&new_driver->drvwrap.driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 785 | |
| 786 | if (!retval) { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 787 | pr_info("%s: registered new interface driver %s\n", |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 788 | usbcore_name, new_driver->name); |
| 789 | usbfs_update_special(); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 790 | usb_create_newid_file(new_driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 791 | } else { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 792 | printk(KERN_ERR "%s: error %d registering interface " |
| 793 | " driver %s\n", |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 794 | usbcore_name, retval, new_driver->name); |
| 795 | } |
| 796 | |
| 797 | return retval; |
| 798 | } |
Greg Kroah-Hartman | b87ba0a | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 799 | EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 800 | |
| 801 | /** |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 802 | * usb_deregister - unregister a USB interface driver |
| 803 | * @driver: USB operations of the interface driver to unregister |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 804 | * Context: must be able to sleep |
| 805 | * |
| 806 | * Unlinks the specified driver from the internal USB driver list. |
| 807 | * |
| 808 | * NOTE: If you called usb_register_dev(), you still need to call |
| 809 | * usb_deregister_dev() to clean up your driver's allocated minor numbers, |
| 810 | * this * call will no longer do it for you. |
| 811 | */ |
| 812 | void usb_deregister(struct usb_driver *driver) |
| 813 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 814 | pr_info("%s: deregistering interface driver %s\n", |
| 815 | usbcore_name, driver->name); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 816 | |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 817 | usb_remove_newid_file(driver); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 818 | usb_free_dynids(driver); |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 819 | driver_unregister(&driver->drvwrap.driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 820 | |
| 821 | usbfs_update_special(); |
| 822 | } |
Greg Kroah-Hartman | b87ba0a | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 823 | EXPORT_SYMBOL_GPL_FUTURE(usb_deregister); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 824 | |
| 825 | #ifdef CONFIG_PM |
| 826 | |
Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 827 | /* Caller has locked udev's pm_mutex */ |
Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 828 | 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] | 829 | { |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 830 | struct usb_device_driver *udriver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 831 | int status = 0; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 832 | |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 833 | if (udev->state == USB_STATE_NOTATTACHED || |
| 834 | udev->state == USB_STATE_SUSPENDED) |
| 835 | goto done; |
| 836 | |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 837 | /* For devices that don't have a driver, we do a standard suspend. */ |
| 838 | if (udev->dev.driver == NULL) { |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 839 | udev->do_remote_wakeup = 0; |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 840 | status = usb_port_suspend(udev); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 841 | goto done; |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 842 | } |
| 843 | |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 844 | udriver = to_usb_device_driver(udev->dev.driver); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 845 | status = udriver->suspend(udev, msg); |
| 846 | |
| 847 | done: |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 848 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 849 | if (status == 0) |
| 850 | udev->dev.power.power_state.event = msg.event; |
| 851 | return status; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 852 | } |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 853 | |
Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 854 | /* Caller has locked udev's pm_mutex */ |
Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 855 | static int usb_resume_device(struct usb_device *udev) |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 856 | { |
| 857 | struct usb_device_driver *udriver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 858 | int status = 0; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 859 | |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 860 | if (udev->state == USB_STATE_NOTATTACHED || |
| 861 | udev->state != USB_STATE_SUSPENDED) |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 862 | goto done; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 863 | |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 864 | /* Can't resume it if it doesn't have a driver. */ |
| 865 | if (udev->dev.driver == NULL) { |
| 866 | status = -ENOTCONN; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 867 | goto done; |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 868 | } |
| 869 | |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 870 | udriver = to_usb_device_driver(udev->dev.driver); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 871 | status = udriver->resume(udev); |
| 872 | |
| 873 | done: |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 874 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame^] | 875 | if (status == 0) { |
| 876 | udev->autoresume_disabled = 0; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 877 | udev->dev.power.power_state.event = PM_EVENT_ON; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame^] | 878 | } |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 879 | return status; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 880 | } |
| 881 | |
Alan Stern | e0318eb | 2006-09-26 14:50:20 -0400 | [diff] [blame] | 882 | /* Caller has locked intf's usb_device's pm mutex */ |
Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 883 | static int usb_suspend_interface(struct usb_interface *intf, pm_message_t msg) |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 884 | { |
| 885 | struct usb_driver *driver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 886 | int status = 0; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 887 | |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 888 | /* with no hardware, USB interfaces only use FREEZE and ON states */ |
| 889 | if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED || |
| 890 | !is_active(intf)) |
| 891 | goto done; |
| 892 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 893 | if (intf->condition == USB_INTERFACE_UNBOUND) /* This can't happen */ |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 894 | goto done; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 895 | driver = to_usb_driver(intf->dev.driver); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 896 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 897 | if (driver->suspend && driver->resume) { |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 898 | status = driver->suspend(intf, msg); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 899 | if (status == 0) |
| 900 | mark_quiesced(intf); |
| 901 | else if (!interface_to_usbdev(intf)->auto_pm) |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 902 | dev_err(&intf->dev, "%s error %d\n", |
| 903 | "suspend", status); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 904 | } else { |
| 905 | // FIXME else if there's no suspend method, disconnect... |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 906 | // Not possible if auto_pm is set... |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 907 | dev_warn(&intf->dev, "no suspend for driver %s?\n", |
| 908 | driver->name); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 909 | mark_quiesced(intf); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 910 | } |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 911 | |
| 912 | done: |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 913 | // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 914 | if (status == 0) |
| 915 | intf->dev.power.power_state.event = msg.event; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 916 | return status; |
| 917 | } |
| 918 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 919 | /* Caller has locked intf's usb_device's pm_mutex */ |
Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 920 | static int usb_resume_interface(struct usb_interface *intf) |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 921 | { |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 922 | struct usb_driver *driver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 923 | int status = 0; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 924 | |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 925 | if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED || |
| 926 | is_active(intf)) |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 927 | goto done; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 928 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 929 | /* Don't let autoresume interfere with unbinding */ |
| 930 | if (intf->condition == USB_INTERFACE_UNBINDING) |
| 931 | goto done; |
| 932 | |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 933 | /* Can't resume it if it doesn't have a driver. */ |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 934 | if (intf->condition == USB_INTERFACE_UNBOUND) { |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 935 | status = -ENOTCONN; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 936 | goto done; |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame] | 937 | } |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 938 | driver = to_usb_driver(intf->dev.driver); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 939 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 940 | if (driver->resume) { |
| 941 | status = driver->resume(intf); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 942 | if (status) |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 943 | dev_err(&intf->dev, "%s error %d\n", |
| 944 | "resume", status); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 945 | else |
| 946 | mark_active(intf); |
| 947 | } else { |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 948 | dev_warn(&intf->dev, "no resume for driver %s?\n", |
| 949 | driver->name); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 950 | mark_active(intf); |
| 951 | } |
| 952 | |
| 953 | done: |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 954 | // dev_dbg(&intf->dev, "%s: status %d\n", __FUNCTION__, status); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 955 | if (status == 0) |
| 956 | intf->dev.power.power_state.event = PM_EVENT_ON; |
| 957 | return status; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 958 | } |
| 959 | |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 960 | #ifdef CONFIG_USB_SUSPEND |
| 961 | |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 962 | /* Internal routine to check whether we may autosuspend a device. */ |
| 963 | static int autosuspend_check(struct usb_device *udev) |
| 964 | { |
| 965 | int i; |
| 966 | struct usb_interface *intf; |
| 967 | |
Alan Stern | b5e795f | 2007-02-20 15:00:53 -0500 | [diff] [blame] | 968 | /* For autosuspend, fail fast if anything is in use or autosuspend |
| 969 | * is disabled. Also fail if any interfaces require remote wakeup |
| 970 | * but it isn't available. |
| 971 | */ |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 972 | udev->do_remote_wakeup = device_may_wakeup(&udev->dev); |
| 973 | if (udev->pm_usage_cnt > 0) |
| 974 | return -EBUSY; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame^] | 975 | if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled) |
Alan Stern | b5e795f | 2007-02-20 15:00:53 -0500 | [diff] [blame] | 976 | return -EPERM; |
| 977 | |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 978 | if (udev->actconfig) { |
| 979 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { |
| 980 | intf = udev->actconfig->interface[i]; |
| 981 | if (!is_active(intf)) |
| 982 | continue; |
| 983 | if (intf->pm_usage_cnt > 0) |
| 984 | return -EBUSY; |
| 985 | if (intf->needs_remote_wakeup && |
| 986 | !udev->do_remote_wakeup) { |
| 987 | dev_dbg(&udev->dev, "remote wakeup needed " |
| 988 | "for autosuspend\n"); |
| 989 | return -EOPNOTSUPP; |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | return 0; |
| 994 | } |
| 995 | |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 996 | #else |
| 997 | |
| 998 | #define autosuspend_check(udev) 0 |
| 999 | |
Alan Stern | b5e795f | 2007-02-20 15:00:53 -0500 | [diff] [blame] | 1000 | #endif /* CONFIG_USB_SUSPEND */ |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1001 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1002 | /** |
| 1003 | * usb_suspend_both - suspend a USB device and its interfaces |
| 1004 | * @udev: the usb_device to suspend |
| 1005 | * @msg: Power Management message describing this state transition |
| 1006 | * |
| 1007 | * This is the central routine for suspending USB devices. It calls the |
| 1008 | * suspend methods for all the interface drivers in @udev and then calls |
| 1009 | * the suspend method for @udev itself. If an error occurs at any stage, |
| 1010 | * all the interfaces which were suspended are resumed so that they remain |
| 1011 | * in the same state as the device. |
| 1012 | * |
| 1013 | * If an autosuspend is in progress (@udev->auto_pm is set), the routine |
| 1014 | * checks first to make sure that neither the device itself or any of its |
| 1015 | * active interfaces is in use (pm_usage_cnt is greater than 0). If they |
| 1016 | * are, the autosuspend fails. |
| 1017 | * |
| 1018 | * If the suspend succeeds, the routine recursively queues an autosuspend |
| 1019 | * request for @udev's parent device, thereby propagating the change up |
| 1020 | * the device tree. If all of the parent's children are now suspended, |
| 1021 | * the parent will autosuspend in turn. |
| 1022 | * |
| 1023 | * The suspend method calls are subject to mutual exclusion under control |
| 1024 | * of @udev's pm_mutex. Many of these calls are also under the protection |
| 1025 | * of @udev's device lock (including all requests originating outside the |
| 1026 | * USB subsystem), but autosuspend requests generated by a child device or |
| 1027 | * interface driver may not be. Usbcore will insure that the method calls |
| 1028 | * do not arrive during bind, unbind, or reset operations. However, drivers |
| 1029 | * must be prepared to handle suspend calls arriving at unpredictable times. |
| 1030 | * The only way to block such calls is to do an autoresume (preventing |
| 1031 | * autosuspends) while holding @udev's device lock (preventing outside |
| 1032 | * suspends). |
| 1033 | * |
| 1034 | * The caller must hold @udev->pm_mutex. |
| 1035 | * |
| 1036 | * This routine can run only in process context. |
| 1037 | */ |
Alan Stern | 718efa6 | 2007-03-09 15:41:13 -0500 | [diff] [blame] | 1038 | 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] | 1039 | { |
| 1040 | int status = 0; |
| 1041 | int i = 0; |
| 1042 | struct usb_interface *intf; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1043 | struct usb_device *parent = udev->parent; |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1044 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1045 | cancel_delayed_work(&udev->autosuspend); |
| 1046 | if (udev->state == USB_STATE_NOTATTACHED) |
| 1047 | return 0; |
| 1048 | if (udev->state == USB_STATE_SUSPENDED) |
| 1049 | return 0; |
| 1050 | |
| 1051 | udev->do_remote_wakeup = device_may_wakeup(&udev->dev); |
| 1052 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1053 | if (udev->auto_pm) { |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1054 | status = autosuspend_check(udev); |
| 1055 | if (status < 0) |
| 1056 | return status; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /* Suspend all the interfaces and then udev itself */ |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1060 | if (udev->actconfig) { |
| 1061 | for (; i < udev->actconfig->desc.bNumInterfaces; i++) { |
| 1062 | intf = udev->actconfig->interface[i]; |
Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1063 | status = usb_suspend_interface(intf, msg); |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1064 | if (status != 0) |
| 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | if (status == 0) |
Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1069 | status = usb_suspend_device(udev, msg); |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1070 | |
| 1071 | /* If the suspend failed, resume interfaces that did get suspended */ |
| 1072 | if (status != 0) { |
| 1073 | while (--i >= 0) { |
| 1074 | intf = udev->actconfig->interface[i]; |
Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1075 | usb_resume_interface(intf); |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1076 | } |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1077 | |
| 1078 | /* If the suspend succeeded, propagate it up the tree */ |
| 1079 | } else if (parent) |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1080 | usb_autosuspend_device(parent); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1081 | |
| 1082 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1083 | return status; |
| 1084 | } |
| 1085 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1086 | /** |
| 1087 | * usb_resume_both - resume a USB device and its interfaces |
| 1088 | * @udev: the usb_device to resume |
| 1089 | * |
| 1090 | * This is the central routine for resuming USB devices. It calls the |
| 1091 | * the resume method for @udev and then calls the resume methods for all |
| 1092 | * the interface drivers in @udev. |
| 1093 | * |
| 1094 | * Before starting the resume, the routine calls itself recursively for |
| 1095 | * the parent device of @udev, thereby propagating the change up the device |
| 1096 | * tree and assuring that @udev will be able to resume. If the parent is |
| 1097 | * unable to resume successfully, the routine fails. |
| 1098 | * |
| 1099 | * The resume method calls are subject to mutual exclusion under control |
| 1100 | * of @udev's pm_mutex. Many of these calls are also under the protection |
| 1101 | * of @udev's device lock (including all requests originating outside the |
| 1102 | * USB subsystem), but autoresume requests generated by a child device or |
| 1103 | * interface driver may not be. Usbcore will insure that the method calls |
| 1104 | * do not arrive during bind, unbind, or reset operations. However, drivers |
| 1105 | * must be prepared to handle resume calls arriving at unpredictable times. |
| 1106 | * The only way to block such calls is to do an autoresume (preventing |
| 1107 | * other autoresumes) while holding @udev's device lock (preventing outside |
| 1108 | * resumes). |
| 1109 | * |
| 1110 | * The caller must hold @udev->pm_mutex. |
| 1111 | * |
| 1112 | * This routine can run only in process context. |
| 1113 | */ |
Alan Stern | 718efa6 | 2007-03-09 15:41:13 -0500 | [diff] [blame] | 1114 | static int usb_resume_both(struct usb_device *udev) |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1115 | { |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1116 | int status = 0; |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1117 | int i; |
| 1118 | struct usb_interface *intf; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1119 | struct usb_device *parent = udev->parent; |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1120 | |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame^] | 1121 | if (udev->auto_pm && udev->autoresume_disabled) |
| 1122 | return -EPERM; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1123 | cancel_delayed_work(&udev->autosuspend); |
| 1124 | if (udev->state == USB_STATE_NOTATTACHED) |
| 1125 | return -ENODEV; |
| 1126 | |
| 1127 | /* Propagate the resume up the tree, if necessary */ |
| 1128 | if (udev->state == USB_STATE_SUSPENDED) { |
| 1129 | if (parent) { |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1130 | status = usb_autoresume_device(parent); |
Alan Stern | ee49fb5 | 2006-11-22 16:55:54 -0500 | [diff] [blame] | 1131 | if (status == 0) { |
| 1132 | status = usb_resume_device(udev); |
| 1133 | if (status) { |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1134 | usb_autosuspend_device(parent); |
Alan Stern | ee49fb5 | 2006-11-22 16:55:54 -0500 | [diff] [blame] | 1135 | |
| 1136 | /* It's possible usb_resume_device() |
| 1137 | * failed after the port was |
| 1138 | * unsuspended, causing udev to be |
| 1139 | * logically disconnected. We don't |
| 1140 | * want usb_disconnect() to autosuspend |
| 1141 | * the parent again, so tell it that |
| 1142 | * udev disconnected while still |
| 1143 | * suspended. */ |
| 1144 | if (udev->state == |
| 1145 | USB_STATE_NOTATTACHED) |
| 1146 | udev->discon_suspended = 1; |
| 1147 | } |
| 1148 | } |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1149 | } else { |
| 1150 | |
| 1151 | /* We can't progagate beyond the USB subsystem, |
| 1152 | * so if a root hub's controller is suspended |
| 1153 | * then we're stuck. */ |
| 1154 | if (udev->dev.parent->power.power_state.event != |
| 1155 | PM_EVENT_ON) |
| 1156 | status = -EHOSTUNREACH; |
Alan Stern | ee49fb5 | 2006-11-22 16:55:54 -0500 | [diff] [blame] | 1157 | else |
| 1158 | status = usb_resume_device(udev); |
| 1159 | } |
Alan Stern | 592fbbe | 2006-09-19 10:08:43 -0400 | [diff] [blame] | 1160 | } else { |
| 1161 | |
| 1162 | /* Needed only for setting udev->dev.power.power_state.event |
| 1163 | * and for possible debugging message. */ |
Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1164 | status = usb_resume_device(udev); |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 1165 | } |
| 1166 | |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1167 | if (status == 0 && udev->actconfig) { |
| 1168 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { |
| 1169 | intf = udev->actconfig->interface[i]; |
Stephen Hemminger | d5ec168 | 2006-11-14 10:06:17 -0800 | [diff] [blame] | 1170 | usb_resume_interface(intf); |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1171 | } |
| 1172 | } |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1173 | |
| 1174 | // dev_dbg(&udev->dev, "%s: status %d\n", __FUNCTION__, status); |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 1175 | return status; |
| 1176 | } |
| 1177 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1178 | #ifdef CONFIG_USB_SUSPEND |
| 1179 | |
Alan Stern | 718efa6 | 2007-03-09 15:41:13 -0500 | [diff] [blame] | 1180 | /* usb_autosuspend_work - callback routine to autosuspend a USB device */ |
| 1181 | void usb_autosuspend_work(struct work_struct *work) |
| 1182 | { |
| 1183 | struct usb_device *udev = |
| 1184 | container_of(work, struct usb_device, autosuspend.work); |
| 1185 | |
| 1186 | usb_pm_lock(udev); |
| 1187 | udev->auto_pm = 1; |
| 1188 | usb_suspend_both(udev, PMSG_SUSPEND); |
| 1189 | usb_pm_unlock(udev); |
| 1190 | } |
| 1191 | |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1192 | /* Internal routine to adjust a device's usage counter and change |
| 1193 | * its autosuspend state. |
| 1194 | */ |
| 1195 | static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt) |
| 1196 | { |
| 1197 | int status = 0; |
| 1198 | |
| 1199 | usb_pm_lock(udev); |
| 1200 | udev->pm_usage_cnt += inc_usage_cnt; |
| 1201 | WARN_ON(udev->pm_usage_cnt < 0); |
| 1202 | if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) { |
| 1203 | udev->auto_pm = 1; |
| 1204 | status = usb_resume_both(udev); |
| 1205 | if (status != 0) |
| 1206 | udev->pm_usage_cnt -= inc_usage_cnt; |
| 1207 | } else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0) |
| 1208 | queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, |
Alan Stern | b5e795f | 2007-02-20 15:00:53 -0500 | [diff] [blame] | 1209 | udev->autosuspend_delay); |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1210 | usb_pm_unlock(udev); |
| 1211 | return status; |
| 1212 | } |
| 1213 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1214 | /** |
| 1215 | * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces |
Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1216 | * @udev: the usb_device to autosuspend |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1217 | * |
| 1218 | * This routine should be called when a core subsystem is finished using |
| 1219 | * @udev and wants to allow it to autosuspend. Examples would be when |
| 1220 | * @udev's device file in usbfs is closed or after a configuration change. |
| 1221 | * |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1222 | * @udev's usage counter is decremented. If it or any of the usage counters |
| 1223 | * for an active interface is greater than 0, no autosuspend request will be |
| 1224 | * queued. (If an interface driver does not support autosuspend then its |
| 1225 | * usage counter is permanently positive.) Furthermore, if an interface |
| 1226 | * driver requires remote-wakeup capability during autosuspend but remote |
| 1227 | * wakeup is disabled, the autosuspend will fail. |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1228 | * |
| 1229 | * Often the caller will hold @udev's device lock, but this is not |
| 1230 | * necessary. |
| 1231 | * |
| 1232 | * This routine can run only in process context. |
| 1233 | */ |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1234 | void usb_autosuspend_device(struct usb_device *udev) |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1235 | { |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1236 | int status; |
| 1237 | |
| 1238 | status = usb_autopm_do_device(udev, -1); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1239 | // dev_dbg(&udev->dev, "%s: cnt %d\n", |
| 1240 | // __FUNCTION__, udev->pm_usage_cnt); |
| 1241 | } |
| 1242 | |
| 1243 | /** |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 1244 | * usb_try_autosuspend_device - attempt an autosuspend of a USB device and its interfaces |
| 1245 | * @udev: the usb_device to autosuspend |
| 1246 | * |
| 1247 | * This routine should be called when a core subsystem thinks @udev may |
| 1248 | * be ready to autosuspend. |
| 1249 | * |
| 1250 | * @udev's usage counter left unchanged. If it or any of the usage counters |
| 1251 | * for an active interface is greater than 0, or autosuspend is not allowed |
| 1252 | * for any other reason, no autosuspend request will be queued. |
| 1253 | * |
| 1254 | * This routine can run only in process context. |
| 1255 | */ |
| 1256 | void usb_try_autosuspend_device(struct usb_device *udev) |
| 1257 | { |
| 1258 | usb_autopm_do_device(udev, 0); |
| 1259 | // dev_dbg(&udev->dev, "%s: cnt %d\n", |
| 1260 | // __FUNCTION__, udev->pm_usage_cnt); |
| 1261 | } |
| 1262 | |
| 1263 | /** |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1264 | * usb_autoresume_device - immediately autoresume a USB device and its interfaces |
Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1265 | * @udev: the usb_device to autoresume |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1266 | * |
| 1267 | * 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] | 1268 | * and needs to guarantee that it is not suspended. No autosuspend will |
| 1269 | * occur until usb_autosuspend_device is called. (Note that this will not |
| 1270 | * prevent suspend events originating in the PM core.) Examples would be |
| 1271 | * when @udev's device file in usbfs is opened or when a remote-wakeup |
| 1272 | * request is received. |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1273 | * |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1274 | * @udev's usage counter is incremented to prevent subsequent autosuspends. |
| 1275 | * However if the autoresume fails then the usage counter is re-decremented. |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1276 | * |
| 1277 | * Often the caller will hold @udev's device lock, but this is not |
| 1278 | * necessary (and attempting it might cause deadlock). |
| 1279 | * |
| 1280 | * This routine can run only in process context. |
| 1281 | */ |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1282 | int usb_autoresume_device(struct usb_device *udev) |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1283 | { |
| 1284 | int status; |
| 1285 | |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 1286 | status = usb_autopm_do_device(udev, 1); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1287 | // dev_dbg(&udev->dev, "%s: status %d cnt %d\n", |
| 1288 | // __FUNCTION__, status, udev->pm_usage_cnt); |
| 1289 | return status; |
| 1290 | } |
| 1291 | |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1292 | /* Internal routine to adjust an interface's usage counter and change |
| 1293 | * its device's autosuspend state. |
| 1294 | */ |
| 1295 | static int usb_autopm_do_interface(struct usb_interface *intf, |
| 1296 | int inc_usage_cnt) |
| 1297 | { |
| 1298 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1299 | int status = 0; |
| 1300 | |
| 1301 | usb_pm_lock(udev); |
| 1302 | if (intf->condition == USB_INTERFACE_UNBOUND) |
| 1303 | status = -ENODEV; |
| 1304 | else { |
| 1305 | intf->pm_usage_cnt += inc_usage_cnt; |
| 1306 | if (inc_usage_cnt >= 0 && intf->pm_usage_cnt > 0) { |
| 1307 | udev->auto_pm = 1; |
| 1308 | status = usb_resume_both(udev); |
| 1309 | if (status != 0) |
| 1310 | intf->pm_usage_cnt -= inc_usage_cnt; |
| 1311 | } else if (inc_usage_cnt <= 0 && autosuspend_check(udev) == 0) |
| 1312 | queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, |
Alan Stern | b5e795f | 2007-02-20 15:00:53 -0500 | [diff] [blame] | 1313 | udev->autosuspend_delay); |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1314 | } |
| 1315 | usb_pm_unlock(udev); |
| 1316 | return status; |
| 1317 | } |
| 1318 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1319 | /** |
| 1320 | * usb_autopm_put_interface - decrement a USB interface's PM-usage counter |
Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1321 | * @intf: the usb_interface whose counter should be decremented |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1322 | * |
| 1323 | * This routine should be called by an interface driver when it is |
| 1324 | * finished using @intf and wants to allow it to autosuspend. A typical |
| 1325 | * example would be a character-device driver when its device file is |
| 1326 | * closed. |
| 1327 | * |
| 1328 | * The routine decrements @intf's usage counter. When the counter reaches |
| 1329 | * 0, a delayed autosuspend request for @intf's device is queued. When |
| 1330 | * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all |
| 1331 | * the other usage counters for the sibling interfaces and @intf's |
| 1332 | * usb_device, the device and all its interfaces will be autosuspended. |
| 1333 | * |
| 1334 | * Note that @intf->pm_usage_cnt is owned by the interface driver. The |
| 1335 | * core will not change its value other than the increment and decrement |
| 1336 | * in usb_autopm_get_interface and usb_autopm_put_interface. The driver |
| 1337 | * may use this simple counter-oriented discipline or may set the value |
| 1338 | * any way it likes. |
| 1339 | * |
| 1340 | * If the driver has set @intf->needs_remote_wakeup then autosuspend will |
| 1341 | * take place only if the device's remote-wakeup facility is enabled. |
| 1342 | * |
| 1343 | * Suspend method calls queued by this routine can arrive at any time |
| 1344 | * while @intf is resumed and its usage counter is equal to 0. They are |
| 1345 | * not protected by the usb_device's lock but only by its pm_mutex. |
| 1346 | * Drivers must provide their own synchronization. |
| 1347 | * |
| 1348 | * This routine can run only in process context. |
| 1349 | */ |
| 1350 | void usb_autopm_put_interface(struct usb_interface *intf) |
| 1351 | { |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1352 | int status; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1353 | |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1354 | status = usb_autopm_do_interface(intf, -1); |
| 1355 | // dev_dbg(&intf->dev, "%s: status %d cnt %d\n", |
| 1356 | // __FUNCTION__, status, intf->pm_usage_cnt); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1357 | } |
| 1358 | EXPORT_SYMBOL_GPL(usb_autopm_put_interface); |
| 1359 | |
| 1360 | /** |
| 1361 | * usb_autopm_get_interface - increment a USB interface's PM-usage counter |
Henrik Kretzschmar | 701f35a | 2006-09-25 17:00:56 -0700 | [diff] [blame] | 1362 | * @intf: the usb_interface whose counter should be incremented |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1363 | * |
| 1364 | * This routine should be called by an interface driver when it wants to |
| 1365 | * use @intf and needs to guarantee that it is not suspended. In addition, |
| 1366 | * the routine prevents @intf from being autosuspended subsequently. (Note |
| 1367 | * that this will not prevent suspend events originating in the PM core.) |
| 1368 | * This prevention will persist until usb_autopm_put_interface() is called |
| 1369 | * or @intf is unbound. A typical example would be a character-device |
| 1370 | * driver when its device file is opened. |
| 1371 | * |
| 1372 | * The routine increments @intf's usage counter. So long as the counter |
| 1373 | * is greater than 0, autosuspend will not be allowed for @intf or its |
| 1374 | * usb_device. When the driver is finished using @intf it should call |
| 1375 | * usb_autopm_put_interface() to decrement the usage counter and queue |
| 1376 | * a delayed autosuspend request (if the counter is <= 0). |
| 1377 | * |
| 1378 | * Note that @intf->pm_usage_cnt is owned by the interface driver. The |
| 1379 | * core will not change its value other than the increment and decrement |
| 1380 | * in usb_autopm_get_interface and usb_autopm_put_interface. The driver |
| 1381 | * may use this simple counter-oriented discipline or may set the value |
| 1382 | * any way it likes. |
| 1383 | * |
| 1384 | * Resume method calls generated by this routine can arrive at any time |
| 1385 | * while @intf is suspended. They are not protected by the usb_device's |
| 1386 | * lock but only by its pm_mutex. Drivers must provide their own |
| 1387 | * synchronization. |
| 1388 | * |
| 1389 | * This routine can run only in process context. |
| 1390 | */ |
| 1391 | int usb_autopm_get_interface(struct usb_interface *intf) |
| 1392 | { |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1393 | int status; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1394 | |
Alan Stern | af4f760 | 2006-10-30 17:06:45 -0500 | [diff] [blame] | 1395 | status = usb_autopm_do_interface(intf, 1); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1396 | // dev_dbg(&intf->dev, "%s: status %d cnt %d\n", |
| 1397 | // __FUNCTION__, status, intf->pm_usage_cnt); |
| 1398 | return status; |
| 1399 | } |
| 1400 | EXPORT_SYMBOL_GPL(usb_autopm_get_interface); |
| 1401 | |
Alan Stern | 692a186 | 2006-10-30 17:07:51 -0500 | [diff] [blame] | 1402 | /** |
| 1403 | * usb_autopm_set_interface - set a USB interface's autosuspend state |
| 1404 | * @intf: the usb_interface whose state should be set |
| 1405 | * |
| 1406 | * This routine sets the autosuspend state of @intf's device according |
| 1407 | * to @intf's usage counter, which the caller must have set previously. |
| 1408 | * If the counter is <= 0, the device is autosuspended (if it isn't |
| 1409 | * already suspended and if nothing else prevents the autosuspend). If |
| 1410 | * the counter is > 0, the device is autoresumed (if it isn't already |
| 1411 | * awake). |
| 1412 | */ |
| 1413 | int usb_autopm_set_interface(struct usb_interface *intf) |
| 1414 | { |
| 1415 | int status; |
| 1416 | |
| 1417 | status = usb_autopm_do_interface(intf, 0); |
| 1418 | // dev_dbg(&intf->dev, "%s: status %d cnt %d\n", |
| 1419 | // __FUNCTION__, status, intf->pm_usage_cnt); |
| 1420 | return status; |
| 1421 | } |
| 1422 | EXPORT_SYMBOL_GPL(usb_autopm_set_interface); |
| 1423 | |
Alan Stern | 718efa6 | 2007-03-09 15:41:13 -0500 | [diff] [blame] | 1424 | #else |
| 1425 | |
| 1426 | void usb_autosuspend_work(struct work_struct *work) |
| 1427 | {} |
| 1428 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1429 | #endif /* CONFIG_USB_SUSPEND */ |
| 1430 | |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1431 | /** |
| 1432 | * usb_external_suspend_device - external suspend of a USB device and its interfaces |
| 1433 | * @udev: the usb_device to suspend |
| 1434 | * @msg: Power Management message describing this state transition |
| 1435 | * |
| 1436 | * This routine handles external suspend requests: ones not generated |
| 1437 | * internally by a USB driver (autosuspend) but rather coming from the user |
| 1438 | * (via sysfs) or the PM core (system sleep). The suspend will be carried |
| 1439 | * out regardless of @udev's usage counter or those of its interfaces, |
| 1440 | * and regardless of whether or not remote wakeup is enabled. Of course, |
| 1441 | * interface drivers still have the option of failing the suspend (if |
| 1442 | * there are unsuspended children, for example). |
| 1443 | * |
| 1444 | * The caller must hold @udev's device lock. |
| 1445 | */ |
| 1446 | int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg) |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1447 | { |
| 1448 | int status; |
| 1449 | |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1450 | usb_pm_lock(udev); |
| 1451 | udev->auto_pm = 0; |
| 1452 | status = usb_suspend_both(udev, msg); |
| 1453 | usb_pm_unlock(udev); |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1454 | return status; |
| 1455 | } |
| 1456 | |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1457 | /** |
| 1458 | * usb_external_resume_device - external resume of a USB device and its interfaces |
| 1459 | * @udev: the usb_device to resume |
| 1460 | * |
| 1461 | * This routine handles external resume requests: ones not generated |
| 1462 | * internally by a USB driver (autoresume) but rather coming from the user |
| 1463 | * (via sysfs), the PM core (system resume), or the device itself (remote |
| 1464 | * wakeup). @udev's usage counter is unaffected. |
| 1465 | * |
| 1466 | * The caller must hold @udev's device lock. |
| 1467 | */ |
| 1468 | int usb_external_resume_device(struct usb_device *udev) |
| 1469 | { |
| 1470 | int status; |
| 1471 | |
| 1472 | usb_pm_lock(udev); |
| 1473 | udev->auto_pm = 0; |
| 1474 | status = usb_resume_both(udev); |
| 1475 | usb_pm_unlock(udev); |
| 1476 | |
| 1477 | /* Now that the device is awake, we can start trying to autosuspend |
| 1478 | * it again. */ |
| 1479 | if (status == 0) |
| 1480 | usb_try_autosuspend_device(udev); |
| 1481 | return status; |
| 1482 | } |
| 1483 | |
| 1484 | static int usb_suspend(struct device *dev, pm_message_t message) |
| 1485 | { |
| 1486 | if (!is_usb_device(dev)) /* Ignore PM for interfaces */ |
| 1487 | return 0; |
| 1488 | return usb_external_suspend_device(to_usb_device(dev), message); |
| 1489 | } |
| 1490 | |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1491 | static int usb_resume(struct device *dev) |
| 1492 | { |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame^] | 1493 | struct usb_device *udev; |
| 1494 | |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1495 | if (!is_usb_device(dev)) /* Ignore PM for interfaces */ |
| 1496 | return 0; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame^] | 1497 | udev = to_usb_device(dev); |
| 1498 | if (udev->autoresume_disabled) |
| 1499 | return -EPERM; |
| 1500 | return usb_external_resume_device(udev); |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 1501 | } |
| 1502 | |
Alan Stern | 6b157c9 | 2007-03-13 16:37:30 -0400 | [diff] [blame] | 1503 | #else |
| 1504 | |
| 1505 | #define usb_suspend NULL |
| 1506 | #define usb_resume NULL |
| 1507 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1508 | #endif /* CONFIG_PM */ |
| 1509 | |
| 1510 | struct bus_type usb_bus_type = { |
| 1511 | .name = "usb", |
| 1512 | .match = usb_device_match, |
| 1513 | .uevent = usb_uevent, |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 1514 | .suspend = usb_suspend, |
| 1515 | .resume = usb_resume, |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 1516 | }; |