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> |
| 27 | #include "hcd.h" |
| 28 | #include "usb.h" |
| 29 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 30 | static int usb_match_one_id(struct usb_interface *interface, |
| 31 | const struct usb_device_id *id); |
| 32 | |
| 33 | struct usb_dynid { |
| 34 | struct list_head node; |
| 35 | struct usb_device_id id; |
| 36 | }; |
| 37 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 38 | #ifdef CONFIG_HOTPLUG |
| 39 | |
| 40 | /* |
| 41 | * Adds a new dynamic USBdevice ID to this driver, |
| 42 | * and cause the driver to probe for all devices again. |
| 43 | */ |
| 44 | static ssize_t store_new_id(struct device_driver *driver, |
| 45 | const char *buf, size_t count) |
| 46 | { |
| 47 | struct usb_driver *usb_drv = to_usb_driver(driver); |
| 48 | struct usb_dynid *dynid; |
| 49 | u32 idVendor = 0; |
| 50 | u32 idProduct = 0; |
| 51 | int fields = 0; |
| 52 | |
| 53 | fields = sscanf(buf, "%x %x", &idVendor, &idProduct); |
| 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 | |
| 66 | spin_lock(&usb_drv->dynids.lock); |
| 67 | list_add_tail(&usb_drv->dynids.list, &dynid->node); |
| 68 | spin_unlock(&usb_drv->dynids.lock); |
| 69 | |
| 70 | if (get_driver(driver)) { |
| 71 | driver_attach(driver); |
| 72 | put_driver(driver); |
| 73 | } |
| 74 | |
| 75 | return count; |
| 76 | } |
| 77 | static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id); |
| 78 | |
| 79 | static int usb_create_newid_file(struct usb_driver *usb_drv) |
| 80 | { |
| 81 | int error = 0; |
| 82 | |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 83 | if (usb_drv->no_dynamic_id) |
| 84 | goto exit; |
| 85 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 86 | if (usb_drv->probe != NULL) |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 87 | error = sysfs_create_file(&usb_drv->drvwrap.driver.kobj, |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 88 | &driver_attr_new_id.attr); |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 89 | exit: |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 90 | return error; |
| 91 | } |
| 92 | |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 93 | static void usb_remove_newid_file(struct usb_driver *usb_drv) |
| 94 | { |
| 95 | if (usb_drv->no_dynamic_id) |
| 96 | return; |
| 97 | |
| 98 | if (usb_drv->probe != NULL) |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 99 | sysfs_remove_file(&usb_drv->drvwrap.driver.kobj, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 100 | &driver_attr_new_id.attr); |
| 101 | } |
| 102 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 103 | static void usb_free_dynids(struct usb_driver *usb_drv) |
| 104 | { |
| 105 | struct usb_dynid *dynid, *n; |
| 106 | |
| 107 | spin_lock(&usb_drv->dynids.lock); |
| 108 | list_for_each_entry_safe(dynid, n, &usb_drv->dynids.list, node) { |
| 109 | list_del(&dynid->node); |
| 110 | kfree(dynid); |
| 111 | } |
| 112 | spin_unlock(&usb_drv->dynids.lock); |
| 113 | } |
| 114 | #else |
| 115 | static inline int usb_create_newid_file(struct usb_driver *usb_drv) |
| 116 | { |
| 117 | return 0; |
| 118 | } |
| 119 | |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 120 | static void usb_remove_newid_file(struct usb_driver *usb_drv) |
| 121 | { |
| 122 | } |
| 123 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 124 | static inline void usb_free_dynids(struct usb_driver *usb_drv) |
| 125 | { |
| 126 | } |
| 127 | #endif |
| 128 | |
| 129 | static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf, |
| 130 | struct usb_driver *drv) |
| 131 | { |
| 132 | struct usb_dynid *dynid; |
| 133 | |
| 134 | spin_lock(&drv->dynids.lock); |
| 135 | list_for_each_entry(dynid, &drv->dynids.list, node) { |
| 136 | if (usb_match_one_id(intf, &dynid->id)) { |
| 137 | spin_unlock(&drv->dynids.lock); |
| 138 | return &dynid->id; |
| 139 | } |
| 140 | } |
| 141 | spin_unlock(&drv->dynids.lock); |
| 142 | return NULL; |
| 143 | } |
| 144 | |
| 145 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 146 | /* called from driver core with dev locked */ |
| 147 | static int usb_probe_device(struct device *dev) |
| 148 | { |
| 149 | struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); |
| 150 | struct usb_device *udev; |
| 151 | int error = -ENODEV; |
| 152 | |
| 153 | dev_dbg(dev, "%s\n", __FUNCTION__); |
| 154 | |
| 155 | if (!is_usb_device(dev)) /* Sanity check */ |
| 156 | return error; |
| 157 | |
| 158 | udev = to_usb_device(dev); |
| 159 | |
| 160 | /* FIXME: resume a suspended device */ |
| 161 | if (udev->state == USB_STATE_SUSPENDED) |
| 162 | return -EHOSTUNREACH; |
| 163 | |
| 164 | /* TODO: Add real matching code */ |
| 165 | |
| 166 | error = udriver->probe(udev); |
| 167 | return error; |
| 168 | } |
| 169 | |
| 170 | /* called from driver core with dev locked */ |
| 171 | static int usb_unbind_device(struct device *dev) |
| 172 | { |
| 173 | struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); |
| 174 | |
| 175 | udriver->disconnect(to_usb_device(dev)); |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /* called from driver core with dev locked */ |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 181 | static int usb_probe_interface(struct device *dev) |
| 182 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 183 | struct usb_driver *driver = to_usb_driver(dev->driver); |
| 184 | struct usb_interface *intf; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 185 | const struct usb_device_id *id; |
| 186 | int error = -ENODEV; |
| 187 | |
| 188 | dev_dbg(dev, "%s\n", __FUNCTION__); |
| 189 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 190 | if (is_usb_device(dev)) /* Sanity check */ |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 191 | return error; |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 192 | |
| 193 | intf = to_usb_interface(dev); |
| 194 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 195 | /* FIXME we'd much prefer to just resume it ... */ |
| 196 | if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED) |
| 197 | return -EHOSTUNREACH; |
| 198 | |
| 199 | id = usb_match_id(intf, driver->id_table); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 200 | if (!id) |
| 201 | id = usb_match_dynamic_id(intf, driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 202 | if (id) { |
| 203 | dev_dbg(dev, "%s - got id\n", __FUNCTION__); |
| 204 | |
| 205 | /* Interface "power state" doesn't correspond to any hardware |
| 206 | * state whatsoever. We use it to record when it's bound to |
| 207 | * a driver that may start I/0: it's not frozen/quiesced. |
| 208 | */ |
| 209 | mark_active(intf); |
| 210 | intf->condition = USB_INTERFACE_BINDING; |
| 211 | error = driver->probe(intf, id); |
| 212 | if (error) { |
| 213 | mark_quiesced(intf); |
| 214 | intf->condition = USB_INTERFACE_UNBOUND; |
| 215 | } else |
| 216 | intf->condition = USB_INTERFACE_BOUND; |
| 217 | } |
| 218 | |
| 219 | return error; |
| 220 | } |
| 221 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 222 | /* called from driver core with dev locked */ |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 223 | static int usb_unbind_interface(struct device *dev) |
| 224 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 225 | struct usb_driver *driver = to_usb_driver(dev->driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 226 | struct usb_interface *intf = to_usb_interface(dev); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 227 | |
| 228 | intf->condition = USB_INTERFACE_UNBINDING; |
| 229 | |
| 230 | /* release all urbs for this interface */ |
| 231 | usb_disable_interface(interface_to_usbdev(intf), intf); |
| 232 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 233 | driver->disconnect(intf); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 234 | |
| 235 | /* reset other interface state */ |
| 236 | usb_set_interface(interface_to_usbdev(intf), |
| 237 | intf->altsetting[0].desc.bInterfaceNumber, |
| 238 | 0); |
| 239 | usb_set_intfdata(intf, NULL); |
| 240 | intf->condition = USB_INTERFACE_UNBOUND; |
| 241 | mark_quiesced(intf); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 246 | /** |
| 247 | * usb_driver_claim_interface - bind a driver to an interface |
| 248 | * @driver: the driver to be bound |
| 249 | * @iface: the interface to which it will be bound; must be in the |
| 250 | * usb device's active configuration |
| 251 | * @priv: driver data associated with that interface |
| 252 | * |
| 253 | * This is used by usb device drivers that need to claim more than one |
| 254 | * interface on a device when probing (audio and acm are current examples). |
| 255 | * No device driver should directly modify internal usb_interface or |
| 256 | * usb_device structure members. |
| 257 | * |
| 258 | * Few drivers should need to use this routine, since the most natural |
| 259 | * way to bind to an interface is to return the private data from |
| 260 | * the driver's probe() method. |
| 261 | * |
| 262 | * Callers must own the device lock and the driver model's usb_bus_type.subsys |
| 263 | * writelock. So driver probe() entries don't need extra locking, |
| 264 | * but other call contexts may need to explicitly claim those locks. |
| 265 | */ |
| 266 | int usb_driver_claim_interface(struct usb_driver *driver, |
| 267 | struct usb_interface *iface, void* priv) |
| 268 | { |
| 269 | struct device *dev = &iface->dev; |
| 270 | |
| 271 | if (dev->driver) |
| 272 | return -EBUSY; |
| 273 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 274 | dev->driver = &driver->drvwrap.driver; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 275 | usb_set_intfdata(iface, priv); |
| 276 | iface->condition = USB_INTERFACE_BOUND; |
| 277 | mark_active(iface); |
| 278 | |
| 279 | /* if interface was already added, bind now; else let |
| 280 | * the future device_add() bind it, bypassing probe() |
| 281 | */ |
| 282 | if (device_is_registered(dev)) |
| 283 | device_bind_driver(dev); |
| 284 | |
| 285 | return 0; |
| 286 | } |
| 287 | EXPORT_SYMBOL(usb_driver_claim_interface); |
| 288 | |
| 289 | /** |
| 290 | * usb_driver_release_interface - unbind a driver from an interface |
| 291 | * @driver: the driver to be unbound |
| 292 | * @iface: the interface from which it will be unbound |
| 293 | * |
| 294 | * This can be used by drivers to release an interface without waiting |
| 295 | * for their disconnect() methods to be called. In typical cases this |
| 296 | * also causes the driver disconnect() method to be called. |
| 297 | * |
| 298 | * This call is synchronous, and may not be used in an interrupt context. |
| 299 | * Callers must own the device lock and the driver model's usb_bus_type.subsys |
| 300 | * writelock. So driver disconnect() entries don't need extra locking, |
| 301 | * but other call contexts may need to explicitly claim those locks. |
| 302 | */ |
| 303 | void usb_driver_release_interface(struct usb_driver *driver, |
| 304 | struct usb_interface *iface) |
| 305 | { |
| 306 | struct device *dev = &iface->dev; |
| 307 | |
| 308 | /* this should never happen, don't release something that's not ours */ |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 309 | if (!dev->driver || dev->driver != &driver->drvwrap.driver) |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 310 | return; |
| 311 | |
| 312 | /* don't release from within disconnect() */ |
| 313 | if (iface->condition != USB_INTERFACE_BOUND) |
| 314 | return; |
| 315 | |
| 316 | /* don't release if the interface hasn't been added yet */ |
| 317 | if (device_is_registered(dev)) { |
| 318 | iface->condition = USB_INTERFACE_UNBINDING; |
| 319 | device_release_driver(dev); |
| 320 | } |
| 321 | |
| 322 | dev->driver = NULL; |
| 323 | usb_set_intfdata(iface, NULL); |
| 324 | iface->condition = USB_INTERFACE_UNBOUND; |
| 325 | mark_quiesced(iface); |
| 326 | } |
| 327 | EXPORT_SYMBOL(usb_driver_release_interface); |
| 328 | |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 329 | /* returns 0 if no match, 1 if match */ |
| 330 | static int usb_match_one_id(struct usb_interface *interface, |
| 331 | const struct usb_device_id *id) |
| 332 | { |
| 333 | struct usb_host_interface *intf; |
| 334 | struct usb_device *dev; |
| 335 | |
| 336 | /* proc_connectinfo in devio.c may call us with id == NULL. */ |
| 337 | if (id == NULL) |
| 338 | return 0; |
| 339 | |
| 340 | intf = interface->cur_altsetting; |
| 341 | dev = interface_to_usbdev(interface); |
| 342 | |
| 343 | if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
| 344 | id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) |
| 345 | return 0; |
| 346 | |
| 347 | if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && |
| 348 | id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) |
| 349 | return 0; |
| 350 | |
| 351 | /* No need to test id->bcdDevice_lo != 0, since 0 is never |
| 352 | greater than any unsigned number. */ |
| 353 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && |
| 354 | (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) |
| 355 | return 0; |
| 356 | |
| 357 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && |
| 358 | (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) |
| 359 | return 0; |
| 360 | |
| 361 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && |
| 362 | (id->bDeviceClass != dev->descriptor.bDeviceClass)) |
| 363 | return 0; |
| 364 | |
| 365 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && |
| 366 | (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass)) |
| 367 | return 0; |
| 368 | |
| 369 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && |
| 370 | (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) |
| 371 | return 0; |
| 372 | |
| 373 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && |
| 374 | (id->bInterfaceClass != intf->desc.bInterfaceClass)) |
| 375 | return 0; |
| 376 | |
| 377 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && |
| 378 | (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) |
| 379 | return 0; |
| 380 | |
| 381 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && |
| 382 | (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) |
| 383 | return 0; |
| 384 | |
| 385 | return 1; |
| 386 | } |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 387 | /** |
| 388 | * usb_match_id - find first usb_device_id matching device or interface |
| 389 | * @interface: the interface of interest |
| 390 | * @id: array of usb_device_id structures, terminated by zero entry |
| 391 | * |
| 392 | * usb_match_id searches an array of usb_device_id's and returns |
| 393 | * the first one matching the device or interface, or null. |
| 394 | * This is used when binding (or rebinding) a driver to an interface. |
| 395 | * Most USB device drivers will use this indirectly, through the usb core, |
| 396 | * but some layered driver frameworks use it directly. |
| 397 | * These device tables are exported with MODULE_DEVICE_TABLE, through |
| 398 | * modutils, to support the driver loading functionality of USB hotplugging. |
| 399 | * |
| 400 | * What Matches: |
| 401 | * |
| 402 | * The "match_flags" element in a usb_device_id controls which |
| 403 | * members are used. If the corresponding bit is set, the |
| 404 | * value in the device_id must match its corresponding member |
| 405 | * in the device or interface descriptor, or else the device_id |
| 406 | * does not match. |
| 407 | * |
| 408 | * "driver_info" is normally used only by device drivers, |
| 409 | * but you can create a wildcard "matches anything" usb_device_id |
| 410 | * as a driver's "modules.usbmap" entry if you provide an id with |
| 411 | * only a nonzero "driver_info" field. If you do this, the USB device |
| 412 | * driver's probe() routine should use additional intelligence to |
| 413 | * decide whether to bind to the specified interface. |
| 414 | * |
| 415 | * What Makes Good usb_device_id Tables: |
| 416 | * |
| 417 | * The match algorithm is very simple, so that intelligence in |
| 418 | * driver selection must come from smart driver id records. |
| 419 | * Unless you have good reasons to use another selection policy, |
| 420 | * provide match elements only in related groups, and order match |
| 421 | * specifiers from specific to general. Use the macros provided |
| 422 | * for that purpose if you can. |
| 423 | * |
| 424 | * The most specific match specifiers use device descriptor |
| 425 | * data. These are commonly used with product-specific matches; |
| 426 | * the USB_DEVICE macro lets you provide vendor and product IDs, |
| 427 | * and you can also match against ranges of product revisions. |
| 428 | * These are widely used for devices with application or vendor |
| 429 | * specific bDeviceClass values. |
| 430 | * |
| 431 | * Matches based on device class/subclass/protocol specifications |
| 432 | * are slightly more general; use the USB_DEVICE_INFO macro, or |
| 433 | * its siblings. These are used with single-function devices |
| 434 | * where bDeviceClass doesn't specify that each interface has |
| 435 | * its own class. |
| 436 | * |
| 437 | * Matches based on interface class/subclass/protocol are the |
| 438 | * most general; they let drivers bind to any interface on a |
| 439 | * multiple-function device. Use the USB_INTERFACE_INFO |
| 440 | * macro, or its siblings, to match class-per-interface style |
| 441 | * devices (as recorded in bDeviceClass). |
| 442 | * |
| 443 | * Within those groups, remember that not all combinations are |
| 444 | * meaningful. For example, don't give a product version range |
| 445 | * without vendor and product IDs; or specify a protocol without |
| 446 | * its associated class and subclass. |
| 447 | */ |
| 448 | const struct usb_device_id *usb_match_id(struct usb_interface *interface, |
| 449 | const struct usb_device_id *id) |
| 450 | { |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 451 | /* proc_connectinfo in devio.c may call us with id == NULL. */ |
| 452 | if (id == NULL) |
| 453 | return NULL; |
| 454 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 455 | /* It is important to check that id->driver_info is nonzero, |
| 456 | since an entry that is all zeroes except for a nonzero |
| 457 | id->driver_info is the way to create an entry that |
| 458 | indicates that the driver want to examine every |
| 459 | device and interface. */ |
| 460 | for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass || |
| 461 | id->driver_info; id++) { |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 462 | if (usb_match_one_id(interface, id)) |
| 463 | return id; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | return NULL; |
| 467 | } |
Greg Kroah-Hartman | b87ba0a | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 468 | EXPORT_SYMBOL_GPL_FUTURE(usb_match_id); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 469 | |
| 470 | int usb_device_match(struct device *dev, struct device_driver *drv) |
| 471 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 472 | /* devices and interfaces are handled separately */ |
| 473 | if (is_usb_device(dev)) { |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 474 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 475 | /* interface drivers never match devices */ |
| 476 | if (!is_usb_device_driver(drv)) |
| 477 | return 0; |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 478 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 479 | /* TODO: Add real matching code */ |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 480 | return 1; |
| 481 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 482 | } else { |
| 483 | struct usb_interface *intf; |
| 484 | struct usb_driver *usb_drv; |
| 485 | const struct usb_device_id *id; |
| 486 | |
| 487 | /* device drivers never match interfaces */ |
| 488 | if (is_usb_device_driver(drv)) |
| 489 | return 0; |
| 490 | |
| 491 | intf = to_usb_interface(dev); |
| 492 | usb_drv = to_usb_driver(drv); |
| 493 | |
| 494 | id = usb_match_id(intf, usb_drv->id_table); |
| 495 | if (id) |
| 496 | return 1; |
| 497 | |
| 498 | id = usb_match_dynamic_id(intf, usb_drv); |
| 499 | if (id) |
| 500 | return 1; |
| 501 | } |
| 502 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 503 | return 0; |
| 504 | } |
| 505 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 506 | #ifdef CONFIG_HOTPLUG |
| 507 | |
| 508 | /* |
| 509 | * This sends an uevent to userspace, typically helping to load driver |
| 510 | * or other modules, configure the device, and more. Drivers can provide |
| 511 | * a MODULE_DEVICE_TABLE to help with module loading subtasks. |
| 512 | * |
| 513 | * We're called either from khubd (the typical case) or from root hub |
| 514 | * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle |
| 515 | * delays in event delivery. Use sysfs (and DEVPATH) to make sure the |
| 516 | * device (and this configuration!) are still present. |
| 517 | */ |
| 518 | static int usb_uevent(struct device *dev, char **envp, int num_envp, |
| 519 | char *buffer, int buffer_size) |
| 520 | { |
| 521 | struct usb_interface *intf; |
| 522 | struct usb_device *usb_dev; |
| 523 | struct usb_host_interface *alt; |
| 524 | int i = 0; |
| 525 | int length = 0; |
| 526 | |
| 527 | if (!dev) |
| 528 | return -ENODEV; |
| 529 | |
| 530 | /* driver is often null here; dev_dbg() would oops */ |
| 531 | pr_debug ("usb %s: uevent\n", dev->bus_id); |
| 532 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 533 | if (is_usb_device(dev)) { |
| 534 | usb_dev = to_usb_device(dev); |
| 535 | alt = NULL; |
| 536 | } else { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 537 | intf = to_usb_interface(dev); |
| 538 | usb_dev = interface_to_usbdev(intf); |
| 539 | alt = intf->cur_altsetting; |
| 540 | } |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 541 | |
| 542 | if (usb_dev->devnum < 0) { |
| 543 | pr_debug ("usb %s: already deleted?\n", dev->bus_id); |
| 544 | return -ENODEV; |
| 545 | } |
| 546 | if (!usb_dev->bus) { |
| 547 | pr_debug ("usb %s: bus removed?\n", dev->bus_id); |
| 548 | return -ENODEV; |
| 549 | } |
| 550 | |
| 551 | #ifdef CONFIG_USB_DEVICEFS |
| 552 | /* If this is available, userspace programs can directly read |
| 553 | * all the device descriptors we don't tell them about. Or |
| 554 | * even act as usermode drivers. |
| 555 | * |
| 556 | * FIXME reduce hardwired intelligence here |
| 557 | */ |
| 558 | if (add_uevent_var(envp, num_envp, &i, |
| 559 | buffer, buffer_size, &length, |
| 560 | "DEVICE=/proc/bus/usb/%03d/%03d", |
| 561 | usb_dev->bus->busnum, usb_dev->devnum)) |
| 562 | return -ENOMEM; |
| 563 | #endif |
| 564 | |
| 565 | /* per-device configurations are common */ |
| 566 | if (add_uevent_var(envp, num_envp, &i, |
| 567 | buffer, buffer_size, &length, |
| 568 | "PRODUCT=%x/%x/%x", |
| 569 | le16_to_cpu(usb_dev->descriptor.idVendor), |
| 570 | le16_to_cpu(usb_dev->descriptor.idProduct), |
| 571 | le16_to_cpu(usb_dev->descriptor.bcdDevice))) |
| 572 | return -ENOMEM; |
| 573 | |
| 574 | /* class-based driver binding models */ |
| 575 | if (add_uevent_var(envp, num_envp, &i, |
| 576 | buffer, buffer_size, &length, |
| 577 | "TYPE=%d/%d/%d", |
| 578 | usb_dev->descriptor.bDeviceClass, |
| 579 | usb_dev->descriptor.bDeviceSubClass, |
| 580 | usb_dev->descriptor.bDeviceProtocol)) |
| 581 | return -ENOMEM; |
| 582 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 583 | if (!is_usb_device(dev)) { |
| 584 | |
| 585 | if (add_uevent_var(envp, num_envp, &i, |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 586 | buffer, buffer_size, &length, |
| 587 | "INTERFACE=%d/%d/%d", |
| 588 | alt->desc.bInterfaceClass, |
| 589 | alt->desc.bInterfaceSubClass, |
| 590 | alt->desc.bInterfaceProtocol)) |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 591 | return -ENOMEM; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 592 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 593 | if (add_uevent_var(envp, num_envp, &i, |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 594 | buffer, buffer_size, &length, |
| 595 | "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X", |
| 596 | le16_to_cpu(usb_dev->descriptor.idVendor), |
| 597 | le16_to_cpu(usb_dev->descriptor.idProduct), |
| 598 | le16_to_cpu(usb_dev->descriptor.bcdDevice), |
| 599 | usb_dev->descriptor.bDeviceClass, |
| 600 | usb_dev->descriptor.bDeviceSubClass, |
| 601 | usb_dev->descriptor.bDeviceProtocol, |
| 602 | alt->desc.bInterfaceClass, |
| 603 | alt->desc.bInterfaceSubClass, |
| 604 | alt->desc.bInterfaceProtocol)) |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 605 | return -ENOMEM; |
| 606 | } |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 607 | |
| 608 | envp[i] = NULL; |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | #else |
| 614 | |
| 615 | static int usb_uevent(struct device *dev, char **envp, |
| 616 | int num_envp, char *buffer, int buffer_size) |
| 617 | { |
| 618 | return -ENODEV; |
| 619 | } |
| 620 | |
| 621 | #endif /* CONFIG_HOTPLUG */ |
| 622 | |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 623 | /** |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 624 | * usb_register_device_driver - register a USB device (not interface) driver |
| 625 | * @new_udriver: USB operations for the device driver |
Greg Kroah-Hartman | 2143acc | 2005-11-21 14:53:03 -0800 | [diff] [blame] | 626 | * @owner: module owner of this driver. |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 627 | * |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 628 | * Registers a USB device driver with the USB core. The list of |
| 629 | * unattached devices will be rescanned whenever a new driver is |
| 630 | * added, allowing the new driver to attach to any recognized devices. |
| 631 | * Returns a negative error code on failure and 0 on success. |
| 632 | */ |
| 633 | int usb_register_device_driver(struct usb_device_driver *new_udriver, |
| 634 | struct module *owner) |
| 635 | { |
| 636 | int retval = 0; |
| 637 | |
| 638 | if (usb_disabled()) |
| 639 | return -ENODEV; |
| 640 | |
| 641 | new_udriver->drvwrap.for_devices = 1; |
| 642 | new_udriver->drvwrap.driver.name = (char *) new_udriver->name; |
| 643 | new_udriver->drvwrap.driver.bus = &usb_bus_type; |
| 644 | new_udriver->drvwrap.driver.probe = usb_probe_device; |
| 645 | new_udriver->drvwrap.driver.remove = usb_unbind_device; |
| 646 | new_udriver->drvwrap.driver.owner = owner; |
| 647 | |
| 648 | retval = driver_register(&new_udriver->drvwrap.driver); |
| 649 | |
| 650 | if (!retval) { |
| 651 | pr_info("%s: registered new device driver %s\n", |
| 652 | usbcore_name, new_udriver->name); |
| 653 | usbfs_update_special(); |
| 654 | } else { |
| 655 | printk(KERN_ERR "%s: error %d registering device " |
| 656 | " driver %s\n", |
| 657 | usbcore_name, retval, new_udriver->name); |
| 658 | } |
| 659 | |
| 660 | return retval; |
| 661 | } |
| 662 | EXPORT_SYMBOL_GPL(usb_register_device_driver); |
| 663 | |
| 664 | /** |
| 665 | * usb_deregister_device_driver - unregister a USB device (not interface) driver |
| 666 | * @udriver: USB operations of the device driver to unregister |
| 667 | * Context: must be able to sleep |
| 668 | * |
| 669 | * Unlinks the specified driver from the internal USB driver list. |
| 670 | */ |
| 671 | void usb_deregister_device_driver(struct usb_device_driver *udriver) |
| 672 | { |
| 673 | pr_info("%s: deregistering device driver %s\n", |
| 674 | usbcore_name, udriver->name); |
| 675 | |
| 676 | driver_unregister(&udriver->drvwrap.driver); |
| 677 | usbfs_update_special(); |
| 678 | } |
| 679 | EXPORT_SYMBOL_GPL(usb_deregister_device_driver); |
| 680 | |
| 681 | /** |
| 682 | * usb_register_driver - register a USB interface driver |
| 683 | * @new_driver: USB operations for the interface driver |
| 684 | * @owner: module owner of this driver. |
| 685 | * |
| 686 | * Registers a USB interface driver with the USB core. The list of |
| 687 | * unattached interfaces will be rescanned whenever a new driver is |
| 688 | * added, allowing the new driver to attach to any recognized interfaces. |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 689 | * Returns a negative error code on failure and 0 on success. |
| 690 | * |
| 691 | * NOTE: if you want your driver to use the USB major number, you must call |
| 692 | * usb_register_dev() to enable that functionality. This function no longer |
| 693 | * takes care of that. |
| 694 | */ |
Greg Kroah-Hartman | 2143acc | 2005-11-21 14:53:03 -0800 | [diff] [blame] | 695 | int usb_register_driver(struct usb_driver *new_driver, struct module *owner) |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 696 | { |
| 697 | int retval = 0; |
| 698 | |
| 699 | if (usb_disabled()) |
| 700 | return -ENODEV; |
| 701 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 702 | new_driver->drvwrap.for_devices = 0; |
| 703 | new_driver->drvwrap.driver.name = (char *) new_driver->name; |
| 704 | new_driver->drvwrap.driver.bus = &usb_bus_type; |
| 705 | new_driver->drvwrap.driver.probe = usb_probe_interface; |
| 706 | new_driver->drvwrap.driver.remove = usb_unbind_interface; |
| 707 | new_driver->drvwrap.driver.owner = owner; |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 708 | spin_lock_init(&new_driver->dynids.lock); |
| 709 | INIT_LIST_HEAD(&new_driver->dynids.list); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 710 | |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 711 | retval = driver_register(&new_driver->drvwrap.driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 712 | |
| 713 | if (!retval) { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 714 | pr_info("%s: registered new interface driver %s\n", |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 715 | usbcore_name, new_driver->name); |
| 716 | usbfs_update_special(); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 717 | usb_create_newid_file(new_driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 718 | } else { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 719 | printk(KERN_ERR "%s: error %d registering interface " |
| 720 | " driver %s\n", |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 721 | usbcore_name, retval, new_driver->name); |
| 722 | } |
| 723 | |
| 724 | return retval; |
| 725 | } |
Greg Kroah-Hartman | b87ba0a | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 726 | EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 727 | |
| 728 | /** |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 729 | * usb_deregister - unregister a USB interface driver |
| 730 | * @driver: USB operations of the interface driver to unregister |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 731 | * Context: must be able to sleep |
| 732 | * |
| 733 | * Unlinks the specified driver from the internal USB driver list. |
| 734 | * |
| 735 | * NOTE: If you called usb_register_dev(), you still need to call |
| 736 | * usb_deregister_dev() to clean up your driver's allocated minor numbers, |
| 737 | * this * call will no longer do it for you. |
| 738 | */ |
| 739 | void usb_deregister(struct usb_driver *driver) |
| 740 | { |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 741 | pr_info("%s: deregistering interface driver %s\n", |
| 742 | usbcore_name, driver->name); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 743 | |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 744 | usb_remove_newid_file(driver); |
Greg Kroah-Hartman | 733260f | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 745 | usb_free_dynids(driver); |
Alan Stern | 8bb54ab | 2006-07-01 22:08:49 -0400 | [diff] [blame] | 746 | driver_unregister(&driver->drvwrap.driver); |
Greg Kroah-Hartman | ddae41b | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 747 | |
| 748 | usbfs_update_special(); |
| 749 | } |
Greg Kroah-Hartman | b87ba0a | 2006-03-20 13:17:13 -0800 | [diff] [blame] | 750 | EXPORT_SYMBOL_GPL_FUTURE(usb_deregister); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 751 | |
| 752 | #ifdef CONFIG_PM |
| 753 | |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 754 | /* Caller has locked udev */ |
| 755 | static int suspend_device(struct usb_device *udev, pm_message_t msg) |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 756 | { |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 757 | struct usb_device_driver *udriver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 758 | int status = 0; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 759 | |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 760 | if (udev->state == USB_STATE_NOTATTACHED || |
| 761 | udev->state == USB_STATE_SUSPENDED) |
| 762 | goto done; |
| 763 | |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame^] | 764 | /* For devices that don't have a driver, we do a standard suspend. */ |
| 765 | if (udev->dev.driver == NULL) { |
| 766 | status = usb_port_suspend(udev); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 767 | goto done; |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame^] | 768 | } |
| 769 | |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 770 | udriver = to_usb_device_driver(udev->dev.driver); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 771 | status = udriver->suspend(udev, msg); |
| 772 | |
| 773 | done: |
| 774 | if (status == 0) |
| 775 | udev->dev.power.power_state.event = msg.event; |
| 776 | return status; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 777 | } |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 778 | |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 779 | /* Caller has locked udev */ |
| 780 | static int resume_device(struct usb_device *udev) |
| 781 | { |
| 782 | struct usb_device_driver *udriver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 783 | int status = 0; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 784 | |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 785 | if (udev->state == USB_STATE_NOTATTACHED || |
| 786 | udev->state != USB_STATE_SUSPENDED) |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 787 | goto done; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 788 | |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame^] | 789 | /* Can't resume it if it doesn't have a driver. */ |
| 790 | if (udev->dev.driver == NULL) { |
| 791 | status = -ENOTCONN; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 792 | goto done; |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame^] | 793 | } |
| 794 | |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 795 | udriver = to_usb_device_driver(udev->dev.driver); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 796 | status = udriver->resume(udev); |
| 797 | |
| 798 | done: |
| 799 | if (status == 0) |
| 800 | udev->dev.power.power_state.event = PM_EVENT_ON; |
| 801 | return status; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 802 | } |
| 803 | |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 804 | /* Caller has locked intf's usb_device */ |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 805 | static int suspend_interface(struct usb_interface *intf, pm_message_t msg) |
| 806 | { |
| 807 | struct usb_driver *driver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 808 | int status = 0; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 809 | |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 810 | /* with no hardware, USB interfaces only use FREEZE and ON states */ |
| 811 | if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED || |
| 812 | !is_active(intf)) |
| 813 | goto done; |
| 814 | |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame^] | 815 | if (intf->dev.driver == NULL) /* This can't happen */ |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 816 | goto done; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 817 | driver = to_usb_driver(intf->dev.driver); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 818 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 819 | if (driver->suspend && driver->resume) { |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 820 | status = driver->suspend(intf, msg); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 821 | if (status) |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 822 | dev_err(&intf->dev, "%s error %d\n", |
| 823 | "suspend", status); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 824 | else |
| 825 | mark_quiesced(intf); |
| 826 | } else { |
| 827 | // FIXME else if there's no suspend method, disconnect... |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 828 | dev_warn(&intf->dev, "no suspend for driver %s?\n", |
| 829 | driver->name); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 830 | mark_quiesced(intf); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 831 | } |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 832 | |
| 833 | done: |
| 834 | if (status == 0) |
| 835 | intf->dev.power.power_state.event = msg.event; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 836 | return status; |
| 837 | } |
| 838 | |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 839 | /* Caller has locked intf's usb_device */ |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 840 | static int resume_interface(struct usb_interface *intf) |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 841 | { |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 842 | struct usb_driver *driver; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 843 | int status = 0; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 844 | |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 845 | if (interface_to_usbdev(intf)->state == USB_STATE_NOTATTACHED || |
| 846 | is_active(intf)) |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 847 | goto done; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 848 | |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame^] | 849 | /* Can't resume it if it doesn't have a driver. */ |
| 850 | if (intf->dev.driver == NULL) { |
| 851 | status = -ENOTCONN; |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 852 | goto done; |
Alan Stern | 1c5df7e | 2006-07-01 22:13:50 -0400 | [diff] [blame^] | 853 | } |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 854 | driver = to_usb_driver(intf->dev.driver); |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 855 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 856 | if (driver->resume) { |
| 857 | status = driver->resume(intf); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 858 | if (status) |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 859 | dev_err(&intf->dev, "%s error %d\n", |
| 860 | "resume", status); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 861 | else |
| 862 | mark_active(intf); |
| 863 | } else { |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 864 | dev_warn(&intf->dev, "no resume for driver %s?\n", |
| 865 | driver->name); |
Alan Stern | 2bf4086 | 2006-07-01 22:12:19 -0400 | [diff] [blame] | 866 | mark_active(intf); |
| 867 | } |
| 868 | |
| 869 | done: |
| 870 | if (status == 0) |
| 871 | intf->dev.power.power_state.event = PM_EVENT_ON; |
| 872 | return status; |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 873 | } |
| 874 | |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 875 | /* Caller has locked udev */ |
| 876 | int usb_suspend_both(struct usb_device *udev, pm_message_t msg) |
| 877 | { |
| 878 | int status = 0; |
| 879 | int i = 0; |
| 880 | struct usb_interface *intf; |
| 881 | |
| 882 | if (udev->actconfig) { |
| 883 | for (; i < udev->actconfig->desc.bNumInterfaces; i++) { |
| 884 | intf = udev->actconfig->interface[i]; |
| 885 | status = suspend_interface(intf, msg); |
| 886 | if (status != 0) |
| 887 | break; |
| 888 | } |
| 889 | } |
| 890 | if (status == 0) |
| 891 | status = suspend_device(udev, msg); |
| 892 | |
| 893 | /* If the suspend failed, resume interfaces that did get suspended */ |
| 894 | if (status != 0) { |
| 895 | while (--i >= 0) { |
| 896 | intf = udev->actconfig->interface[i]; |
| 897 | resume_interface(intf); |
| 898 | } |
| 899 | } |
| 900 | return status; |
| 901 | } |
| 902 | |
| 903 | /* Caller has locked udev */ |
| 904 | int usb_resume_both(struct usb_device *udev) |
| 905 | { |
| 906 | int status; |
| 907 | int i; |
| 908 | struct usb_interface *intf; |
| 909 | |
Alan Stern | 114b368 | 2006-07-01 22:13:04 -0400 | [diff] [blame] | 910 | /* Can't resume if the parent is suspended */ |
| 911 | if (udev->parent && udev->parent->state == USB_STATE_SUSPENDED) { |
| 912 | dev_warn(&udev->dev, "can't resume; parent is suspended\n"); |
| 913 | return -EHOSTUNREACH; |
| 914 | } |
| 915 | |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 916 | status = resume_device(udev); |
| 917 | if (status == 0 && udev->actconfig) { |
| 918 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { |
| 919 | intf = udev->actconfig->interface[i]; |
| 920 | resume_interface(intf); |
| 921 | } |
| 922 | } |
| 923 | return status; |
| 924 | } |
| 925 | |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 926 | static int usb_suspend(struct device *dev, pm_message_t message) |
| 927 | { |
| 928 | int status; |
| 929 | |
| 930 | if (is_usb_device(dev)) |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 931 | status = usb_suspend_both(to_usb_device(dev), message); |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 932 | else |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 933 | status = 0; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 934 | return status; |
| 935 | } |
| 936 | |
| 937 | static int usb_resume(struct device *dev) |
| 938 | { |
| 939 | int status; |
| 940 | |
Alan Stern | a8e7c56 | 2006-07-01 22:11:02 -0400 | [diff] [blame] | 941 | if (is_usb_device(dev)) { |
| 942 | status = usb_resume_both(to_usb_device(dev)); |
| 943 | |
| 944 | /* Rebind drivers that had no suspend method? */ |
| 945 | } else |
| 946 | status = 0; |
Alan Stern | 1cc8a25 | 2006-07-01 22:10:15 -0400 | [diff] [blame] | 947 | return status; |
| 948 | } |
| 949 | |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 950 | #endif /* CONFIG_PM */ |
| 951 | |
| 952 | struct bus_type usb_bus_type = { |
| 953 | .name = "usb", |
| 954 | .match = usb_device_match, |
| 955 | .uevent = usb_uevent, |
| 956 | #ifdef CONFIG_PM |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 957 | .suspend = usb_suspend, |
| 958 | .resume = usb_resume, |
Alan Stern | 36e56a3 | 2006-07-01 22:08:06 -0400 | [diff] [blame] | 959 | #endif |
| 960 | }; |