2 * drivers/usb/driver.c - most of the driver model stuff for usb
4 * (C) Copyright 2005 Greg Kroah-Hartman <gregkh@suse.de>
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
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..
24 #include <linux/config.h>
25 #include <linux/device.h>
26 #include <linux/usb.h>
30 static int generic_probe(struct device *dev)
34 static int generic_remove(struct device *dev)
36 struct usb_device *udev = to_usb_device(dev);
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);
43 /* in case the call failed or the device was suspended */
44 if (udev->state >= USB_STATE_CONFIGURED)
45 usb_disable_device(udev, 0);
49 struct device_driver usb_generic_driver = {
53 .probe = generic_probe,
54 .remove = generic_remove,
57 /* Fun hack to determine if the struct device is a
58 * usb device or a usb interface. */
59 int usb_generic_driver_data;
61 /* called from driver core with usb_bus_type.subsys writelock */
62 static int usb_probe_interface(struct device *dev)
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;
69 dev_dbg(dev, "%s\n", __FUNCTION__);
73 /* FIXME we'd much prefer to just resume it ... */
74 if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED)
77 id = usb_match_id(intf, driver->id_table);
79 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
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.
86 intf->condition = USB_INTERFACE_BINDING;
87 error = driver->probe(intf, id);
90 intf->condition = USB_INTERFACE_UNBOUND;
92 intf->condition = USB_INTERFACE_BOUND;
98 /* called from driver core with usb_bus_type.subsys writelock */
99 static int usb_unbind_interface(struct device *dev)
101 struct usb_interface *intf = to_usb_interface(dev);
102 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
104 intf->condition = USB_INTERFACE_UNBINDING;
106 /* release all urbs for this interface */
107 usb_disable_interface(interface_to_usbdev(intf), intf);
109 if (driver && driver->disconnect)
110 driver->disconnect(intf);
112 /* reset other interface state */
113 usb_set_interface(interface_to_usbdev(intf),
114 intf->altsetting[0].desc.bInterfaceNumber,
116 usb_set_intfdata(intf, NULL);
117 intf->condition = USB_INTERFACE_UNBOUND;
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
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.
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
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.
151 * What Makes Good usb_device_id Tables:
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.
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.
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
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).
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.
184 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
185 const struct usb_device_id *id)
187 struct usb_host_interface *intf;
188 struct usb_device *dev;
190 /* proc_connectinfo in devio.c may call us with id == NULL. */
194 intf = interface->cur_altsetting;
195 dev = interface_to_usbdev(interface);
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++) {
205 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
206 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
209 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
210 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
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)))
219 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
220 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
223 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
224 (id->bDeviceClass != dev->descriptor.bDeviceClass))
227 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
228 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
231 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
232 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
235 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
236 (id->bInterfaceClass != intf->desc.bInterfaceClass))
239 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
240 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
243 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
244 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
252 EXPORT_SYMBOL_GPL(usb_match_id);
254 int usb_device_match(struct device *dev, struct device_driver *drv)
256 struct usb_interface *intf;
257 struct usb_driver *usb_drv;
258 const struct usb_device_id *id;
260 /* check for generic driver, which we don't match any device with */
261 if (drv == &usb_generic_driver)
264 intf = to_usb_interface(dev);
265 usb_drv = to_usb_driver(drv);
267 id = usb_match_id(intf, usb_drv->id_table);
275 * usb_register - register a USB driver
276 * @new_driver: USB operations for the driver
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.
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.
287 int usb_register(struct usb_driver *new_driver)
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;
300 usb_lock_all_devices();
301 retval = driver_register(&new_driver->driver);
302 usb_unlock_all_devices();
305 pr_info("%s: registered new driver %s\n",
306 usbcore_name, new_driver->name);
307 usbfs_update_special();
309 printk(KERN_ERR "%s: error %d registering driver %s\n",
310 usbcore_name, retval, new_driver->name);
315 EXPORT_SYMBOL_GPL(usb_register);
318 * usb_deregister - unregister a USB driver
319 * @driver: USB operations of the driver to unregister
320 * Context: must be able to sleep
322 * Unlinks the specified driver from the internal USB driver list.
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.
328 void usb_deregister(struct usb_driver *driver)
330 pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
332 usb_lock_all_devices();
333 driver_unregister(&driver->driver);
334 usb_unlock_all_devices();
336 usbfs_update_special();
338 EXPORT_SYMBOL_GPL(usb_deregister);