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