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 |
| 20 | * generic USB things that the real drivers can use.. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/config.h> |
| 25 | #include <linux/device.h> |
| 26 | #include <linux/usb.h> |
| 27 | #include "hcd.h" |
| 28 | #include "usb.h" |
| 29 | |
| 30 | static int generic_probe(struct device *dev) |
| 31 | { |
| 32 | return 0; |
| 33 | } |
| 34 | static int generic_remove(struct device *dev) |
| 35 | { |
| 36 | struct usb_device *udev = to_usb_device(dev); |
| 37 | |
| 38 | /* if this is only an unbind, not a physical disconnect, then |
| 39 | * unconfigure the device */ |
| 40 | if (udev->state == USB_STATE_CONFIGURED) |
| 41 | usb_set_configuration(udev, 0); |
| 42 | |
| 43 | /* in case the call failed or the device was suspended */ |
| 44 | if (udev->state >= USB_STATE_CONFIGURED) |
| 45 | usb_disable_device(udev, 0); |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | struct device_driver usb_generic_driver = { |
| 50 | .owner = THIS_MODULE, |
| 51 | .name = "usb", |
| 52 | .bus = &usb_bus_type, |
| 53 | .probe = generic_probe, |
| 54 | .remove = generic_remove, |
| 55 | }; |
| 56 | |
| 57 | /* Fun hack to determine if the struct device is a |
| 58 | * usb device or a usb interface. */ |
| 59 | int usb_generic_driver_data; |
| 60 | |
| 61 | /* called from driver core with usb_bus_type.subsys writelock */ |
| 62 | static int usb_probe_interface(struct device *dev) |
| 63 | { |
| 64 | struct usb_interface * intf = to_usb_interface(dev); |
| 65 | struct usb_driver * driver = to_usb_driver(dev->driver); |
| 66 | const struct usb_device_id *id; |
| 67 | int error = -ENODEV; |
| 68 | |
| 69 | dev_dbg(dev, "%s\n", __FUNCTION__); |
| 70 | |
| 71 | if (!driver->probe) |
| 72 | return error; |
| 73 | /* FIXME we'd much prefer to just resume it ... */ |
| 74 | if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED) |
| 75 | return -EHOSTUNREACH; |
| 76 | |
| 77 | id = usb_match_id(intf, driver->id_table); |
| 78 | if (id) { |
| 79 | dev_dbg(dev, "%s - got id\n", __FUNCTION__); |
| 80 | |
| 81 | /* Interface "power state" doesn't correspond to any hardware |
| 82 | * state whatsoever. We use it to record when it's bound to |
| 83 | * a driver that may start I/0: it's not frozen/quiesced. |
| 84 | */ |
| 85 | mark_active(intf); |
| 86 | intf->condition = USB_INTERFACE_BINDING; |
| 87 | error = driver->probe(intf, id); |
| 88 | if (error) { |
| 89 | mark_quiesced(intf); |
| 90 | intf->condition = USB_INTERFACE_UNBOUND; |
| 91 | } else |
| 92 | intf->condition = USB_INTERFACE_BOUND; |
| 93 | } |
| 94 | |
| 95 | return error; |
| 96 | } |
| 97 | |
| 98 | /* called from driver core with usb_bus_type.subsys writelock */ |
| 99 | static int usb_unbind_interface(struct device *dev) |
| 100 | { |
| 101 | struct usb_interface *intf = to_usb_interface(dev); |
| 102 | struct usb_driver *driver = to_usb_driver(intf->dev.driver); |
| 103 | |
| 104 | intf->condition = USB_INTERFACE_UNBINDING; |
| 105 | |
| 106 | /* release all urbs for this interface */ |
| 107 | usb_disable_interface(interface_to_usbdev(intf), intf); |
| 108 | |
| 109 | if (driver && driver->disconnect) |
| 110 | driver->disconnect(intf); |
| 111 | |
| 112 | /* reset other interface state */ |
| 113 | usb_set_interface(interface_to_usbdev(intf), |
| 114 | intf->altsetting[0].desc.bInterfaceNumber, |
| 115 | 0); |
| 116 | usb_set_intfdata(intf, NULL); |
| 117 | intf->condition = USB_INTERFACE_UNBOUND; |
| 118 | mark_quiesced(intf); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * usb_match_id - find first usb_device_id matching device or interface |
| 125 | * @interface: the interface of interest |
| 126 | * @id: array of usb_device_id structures, terminated by zero entry |
| 127 | * |
| 128 | * usb_match_id searches an array of usb_device_id's and returns |
| 129 | * the first one matching the device or interface, or null. |
| 130 | * This is used when binding (or rebinding) a driver to an interface. |
| 131 | * Most USB device drivers will use this indirectly, through the usb core, |
| 132 | * but some layered driver frameworks use it directly. |
| 133 | * These device tables are exported with MODULE_DEVICE_TABLE, through |
| 134 | * modutils, to support the driver loading functionality of USB hotplugging. |
| 135 | * |
| 136 | * What Matches: |
| 137 | * |
| 138 | * The "match_flags" element in a usb_device_id controls which |
| 139 | * members are used. If the corresponding bit is set, the |
| 140 | * value in the device_id must match its corresponding member |
| 141 | * in the device or interface descriptor, or else the device_id |
| 142 | * does not match. |
| 143 | * |
| 144 | * "driver_info" is normally used only by device drivers, |
| 145 | * but you can create a wildcard "matches anything" usb_device_id |
| 146 | * as a driver's "modules.usbmap" entry if you provide an id with |
| 147 | * only a nonzero "driver_info" field. If you do this, the USB device |
| 148 | * driver's probe() routine should use additional intelligence to |
| 149 | * decide whether to bind to the specified interface. |
| 150 | * |
| 151 | * What Makes Good usb_device_id Tables: |
| 152 | * |
| 153 | * The match algorithm is very simple, so that intelligence in |
| 154 | * driver selection must come from smart driver id records. |
| 155 | * Unless you have good reasons to use another selection policy, |
| 156 | * provide match elements only in related groups, and order match |
| 157 | * specifiers from specific to general. Use the macros provided |
| 158 | * for that purpose if you can. |
| 159 | * |
| 160 | * The most specific match specifiers use device descriptor |
| 161 | * data. These are commonly used with product-specific matches; |
| 162 | * the USB_DEVICE macro lets you provide vendor and product IDs, |
| 163 | * and you can also match against ranges of product revisions. |
| 164 | * These are widely used for devices with application or vendor |
| 165 | * specific bDeviceClass values. |
| 166 | * |
| 167 | * Matches based on device class/subclass/protocol specifications |
| 168 | * are slightly more general; use the USB_DEVICE_INFO macro, or |
| 169 | * its siblings. These are used with single-function devices |
| 170 | * where bDeviceClass doesn't specify that each interface has |
| 171 | * its own class. |
| 172 | * |
| 173 | * Matches based on interface class/subclass/protocol are the |
| 174 | * most general; they let drivers bind to any interface on a |
| 175 | * multiple-function device. Use the USB_INTERFACE_INFO |
| 176 | * macro, or its siblings, to match class-per-interface style |
| 177 | * devices (as recorded in bDeviceClass). |
| 178 | * |
| 179 | * Within those groups, remember that not all combinations are |
| 180 | * meaningful. For example, don't give a product version range |
| 181 | * without vendor and product IDs; or specify a protocol without |
| 182 | * its associated class and subclass. |
| 183 | */ |
| 184 | const struct usb_device_id *usb_match_id(struct usb_interface *interface, |
| 185 | const struct usb_device_id *id) |
| 186 | { |
| 187 | struct usb_host_interface *intf; |
| 188 | struct usb_device *dev; |
| 189 | |
| 190 | /* proc_connectinfo in devio.c may call us with id == NULL. */ |
| 191 | if (id == NULL) |
| 192 | return NULL; |
| 193 | |
| 194 | intf = interface->cur_altsetting; |
| 195 | dev = interface_to_usbdev(interface); |
| 196 | |
| 197 | /* It is important to check that id->driver_info is nonzero, |
| 198 | since an entry that is all zeroes except for a nonzero |
| 199 | id->driver_info is the way to create an entry that |
| 200 | indicates that the driver want to examine every |
| 201 | device and interface. */ |
| 202 | for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass || |
| 203 | id->driver_info; id++) { |
| 204 | |
| 205 | if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
| 206 | id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) |
| 207 | continue; |
| 208 | |
| 209 | if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && |
| 210 | id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) |
| 211 | continue; |
| 212 | |
| 213 | /* No need to test id->bcdDevice_lo != 0, since 0 is never |
| 214 | greater than any unsigned number. */ |
| 215 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && |
| 216 | (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) |
| 217 | continue; |
| 218 | |
| 219 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && |
| 220 | (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) |
| 221 | continue; |
| 222 | |
| 223 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && |
| 224 | (id->bDeviceClass != dev->descriptor.bDeviceClass)) |
| 225 | continue; |
| 226 | |
| 227 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && |
| 228 | (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass)) |
| 229 | continue; |
| 230 | |
| 231 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && |
| 232 | (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) |
| 233 | continue; |
| 234 | |
| 235 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && |
| 236 | (id->bInterfaceClass != intf->desc.bInterfaceClass)) |
| 237 | continue; |
| 238 | |
| 239 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && |
| 240 | (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) |
| 241 | continue; |
| 242 | |
| 243 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && |
| 244 | (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) |
| 245 | continue; |
| 246 | |
| 247 | return id; |
| 248 | } |
| 249 | |
| 250 | return NULL; |
| 251 | } |
| 252 | EXPORT_SYMBOL_GPL(usb_match_id); |
| 253 | |
| 254 | int usb_device_match(struct device *dev, struct device_driver *drv) |
| 255 | { |
| 256 | struct usb_interface *intf; |
| 257 | struct usb_driver *usb_drv; |
| 258 | const struct usb_device_id *id; |
| 259 | |
| 260 | /* check for generic driver, which we don't match any device with */ |
| 261 | if (drv == &usb_generic_driver) |
| 262 | return 0; |
| 263 | |
| 264 | intf = to_usb_interface(dev); |
| 265 | usb_drv = to_usb_driver(drv); |
| 266 | |
| 267 | id = usb_match_id(intf, usb_drv->id_table); |
| 268 | if (id) |
| 269 | return 1; |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * usb_register - register a USB driver |
| 276 | * @new_driver: USB operations for the driver |
| 277 | * |
| 278 | * Registers a USB driver with the USB core. The list of unattached |
| 279 | * interfaces will be rescanned whenever a new driver is added, allowing |
| 280 | * the new driver to attach to any recognized devices. |
| 281 | * Returns a negative error code on failure and 0 on success. |
| 282 | * |
| 283 | * NOTE: if you want your driver to use the USB major number, you must call |
| 284 | * usb_register_dev() to enable that functionality. This function no longer |
| 285 | * takes care of that. |
| 286 | */ |
| 287 | int usb_register(struct usb_driver *new_driver) |
| 288 | { |
| 289 | int retval = 0; |
| 290 | |
| 291 | if (usb_disabled()) |
| 292 | return -ENODEV; |
| 293 | |
| 294 | new_driver->driver.name = (char *)new_driver->name; |
| 295 | new_driver->driver.bus = &usb_bus_type; |
| 296 | new_driver->driver.probe = usb_probe_interface; |
| 297 | new_driver->driver.remove = usb_unbind_interface; |
| 298 | new_driver->driver.owner = new_driver->owner; |
| 299 | |
| 300 | usb_lock_all_devices(); |
| 301 | retval = driver_register(&new_driver->driver); |
| 302 | usb_unlock_all_devices(); |
| 303 | |
| 304 | if (!retval) { |
| 305 | pr_info("%s: registered new driver %s\n", |
| 306 | usbcore_name, new_driver->name); |
| 307 | usbfs_update_special(); |
| 308 | } else { |
| 309 | printk(KERN_ERR "%s: error %d registering driver %s\n", |
| 310 | usbcore_name, retval, new_driver->name); |
| 311 | } |
| 312 | |
| 313 | return retval; |
| 314 | } |
| 315 | EXPORT_SYMBOL_GPL(usb_register); |
| 316 | |
| 317 | /** |
| 318 | * usb_deregister - unregister a USB driver |
| 319 | * @driver: USB operations of the driver to unregister |
| 320 | * Context: must be able to sleep |
| 321 | * |
| 322 | * Unlinks the specified driver from the internal USB driver list. |
| 323 | * |
| 324 | * NOTE: If you called usb_register_dev(), you still need to call |
| 325 | * usb_deregister_dev() to clean up your driver's allocated minor numbers, |
| 326 | * this * call will no longer do it for you. |
| 327 | */ |
| 328 | void usb_deregister(struct usb_driver *driver) |
| 329 | { |
| 330 | pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name); |
| 331 | |
| 332 | usb_lock_all_devices(); |
| 333 | driver_unregister(&driver->driver); |
| 334 | usb_unlock_all_devices(); |
| 335 | |
| 336 | usbfs_update_special(); |
| 337 | } |
| 338 | EXPORT_SYMBOL_GPL(usb_deregister); |