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