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/device.h>
25 #include <linux/usb.h>
29 static int usb_match_one_id(struct usb_interface
*interface
,
30 const struct usb_device_id
*id
);
33 struct list_head node
;
34 struct usb_device_id id
;
38 static int generic_probe(struct device
*dev
)
42 static int generic_remove(struct device
*dev
)
44 struct usb_device
*udev
= to_usb_device(dev
);
46 /* if this is only an unbind, not a physical disconnect, then
47 * unconfigure the device */
48 if (udev
->state
== USB_STATE_CONFIGURED
)
49 usb_set_configuration(udev
, 0);
51 /* in case the call failed or the device was suspended */
52 if (udev
->state
>= USB_STATE_CONFIGURED
)
53 usb_disable_device(udev
, 0);
57 struct device_driver usb_generic_driver
= {
61 .probe
= generic_probe
,
62 .remove
= generic_remove
,
65 /* Fun hack to determine if the struct device is a
66 * usb device or a usb interface. */
67 int usb_generic_driver_data
;
72 * Adds a new dynamic USBdevice ID to this driver,
73 * and cause the driver to probe for all devices again.
75 static ssize_t
store_new_id(struct device_driver
*driver
,
76 const char *buf
, size_t count
)
78 struct usb_driver
*usb_drv
= to_usb_driver(driver
);
79 struct usb_dynid
*dynid
;
84 fields
= sscanf(buf
, "%x %x", &idVendor
, &idProduct
);
88 dynid
= kzalloc(sizeof(*dynid
), GFP_KERNEL
);
92 INIT_LIST_HEAD(&dynid
->node
);
93 dynid
->id
.idVendor
= idVendor
;
94 dynid
->id
.idProduct
= idProduct
;
95 dynid
->id
.match_flags
= USB_DEVICE_ID_MATCH_DEVICE
;
97 spin_lock(&usb_drv
->dynids
.lock
);
98 list_add_tail(&usb_drv
->dynids
.list
, &dynid
->node
);
99 spin_unlock(&usb_drv
->dynids
.lock
);
101 if (get_driver(driver
)) {
102 driver_attach(driver
);
108 static DRIVER_ATTR(new_id
, S_IWUSR
, NULL
, store_new_id
);
110 static int usb_create_newid_file(struct usb_driver
*usb_drv
)
114 if (usb_drv
->no_dynamic_id
)
117 if (usb_drv
->probe
!= NULL
)
118 error
= sysfs_create_file(&usb_drv
->driver
.kobj
,
119 &driver_attr_new_id
.attr
);
124 static void usb_remove_newid_file(struct usb_driver
*usb_drv
)
126 if (usb_drv
->no_dynamic_id
)
129 if (usb_drv
->probe
!= NULL
)
130 sysfs_remove_file(&usb_drv
->driver
.kobj
,
131 &driver_attr_new_id
.attr
);
134 static void usb_free_dynids(struct usb_driver
*usb_drv
)
136 struct usb_dynid
*dynid
, *n
;
138 spin_lock(&usb_drv
->dynids
.lock
);
139 list_for_each_entry_safe(dynid
, n
, &usb_drv
->dynids
.list
, node
) {
140 list_del(&dynid
->node
);
143 spin_unlock(&usb_drv
->dynids
.lock
);
146 static inline int usb_create_newid_file(struct usb_driver
*usb_drv
)
151 static void usb_remove_newid_file(struct usb_driver
*usb_drv
)
155 static inline void usb_free_dynids(struct usb_driver
*usb_drv
)
160 static const struct usb_device_id
*usb_match_dynamic_id(struct usb_interface
*intf
,
161 struct usb_driver
*drv
)
163 struct usb_dynid
*dynid
;
165 spin_lock(&drv
->dynids
.lock
);
166 list_for_each_entry(dynid
, &drv
->dynids
.list
, node
) {
167 if (usb_match_one_id(intf
, &dynid
->id
)) {
168 spin_unlock(&drv
->dynids
.lock
);
172 spin_unlock(&drv
->dynids
.lock
);
177 /* called from driver core with usb_bus_type.subsys writelock */
178 static int usb_probe_interface(struct device
*dev
)
180 struct usb_interface
* intf
= to_usb_interface(dev
);
181 struct usb_driver
* driver
= to_usb_driver(dev
->driver
);
182 const struct usb_device_id
*id
;
185 dev_dbg(dev
, "%s\n", __FUNCTION__
);
189 /* FIXME we'd much prefer to just resume it ... */
190 if (interface_to_usbdev(intf
)->state
== USB_STATE_SUSPENDED
)
191 return -EHOSTUNREACH
;
193 id
= usb_match_id(intf
, driver
->id_table
);
195 id
= usb_match_dynamic_id(intf
, driver
);
197 dev_dbg(dev
, "%s - got id\n", __FUNCTION__
);
199 /* Interface "power state" doesn't correspond to any hardware
200 * state whatsoever. We use it to record when it's bound to
201 * a driver that may start I/0: it's not frozen/quiesced.
204 intf
->condition
= USB_INTERFACE_BINDING
;
205 error
= driver
->probe(intf
, id
);
208 intf
->condition
= USB_INTERFACE_UNBOUND
;
210 intf
->condition
= USB_INTERFACE_BOUND
;
216 /* called from driver core with usb_bus_type.subsys writelock */
217 static int usb_unbind_interface(struct device
*dev
)
219 struct usb_interface
*intf
= to_usb_interface(dev
);
220 struct usb_driver
*driver
= to_usb_driver(intf
->dev
.driver
);
222 intf
->condition
= USB_INTERFACE_UNBINDING
;
224 /* release all urbs for this interface */
225 usb_disable_interface(interface_to_usbdev(intf
), intf
);
227 if (driver
&& driver
->disconnect
)
228 driver
->disconnect(intf
);
230 /* reset other interface state */
231 usb_set_interface(interface_to_usbdev(intf
),
232 intf
->altsetting
[0].desc
.bInterfaceNumber
,
234 usb_set_intfdata(intf
, NULL
);
235 intf
->condition
= USB_INTERFACE_UNBOUND
;
241 /* returns 0 if no match, 1 if match */
242 static int usb_match_one_id(struct usb_interface
*interface
,
243 const struct usb_device_id
*id
)
245 struct usb_host_interface
*intf
;
246 struct usb_device
*dev
;
248 /* proc_connectinfo in devio.c may call us with id == NULL. */
252 intf
= interface
->cur_altsetting
;
253 dev
= interface_to_usbdev(interface
);
255 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
256 id
->idVendor
!= le16_to_cpu(dev
->descriptor
.idVendor
))
259 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
260 id
->idProduct
!= le16_to_cpu(dev
->descriptor
.idProduct
))
263 /* No need to test id->bcdDevice_lo != 0, since 0 is never
264 greater than any unsigned number. */
265 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
266 (id
->bcdDevice_lo
> le16_to_cpu(dev
->descriptor
.bcdDevice
)))
269 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
270 (id
->bcdDevice_hi
< le16_to_cpu(dev
->descriptor
.bcdDevice
)))
273 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
274 (id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
))
277 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
278 (id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
))
281 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
282 (id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
))
285 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
286 (id
->bInterfaceClass
!= intf
->desc
.bInterfaceClass
))
289 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
290 (id
->bInterfaceSubClass
!= intf
->desc
.bInterfaceSubClass
))
293 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
294 (id
->bInterfaceProtocol
!= intf
->desc
.bInterfaceProtocol
))
300 * usb_match_id - find first usb_device_id matching device or interface
301 * @interface: the interface of interest
302 * @id: array of usb_device_id structures, terminated by zero entry
304 * usb_match_id searches an array of usb_device_id's and returns
305 * the first one matching the device or interface, or null.
306 * This is used when binding (or rebinding) a driver to an interface.
307 * Most USB device drivers will use this indirectly, through the usb core,
308 * but some layered driver frameworks use it directly.
309 * These device tables are exported with MODULE_DEVICE_TABLE, through
310 * modutils, to support the driver loading functionality of USB hotplugging.
314 * The "match_flags" element in a usb_device_id controls which
315 * members are used. If the corresponding bit is set, the
316 * value in the device_id must match its corresponding member
317 * in the device or interface descriptor, or else the device_id
320 * "driver_info" is normally used only by device drivers,
321 * but you can create a wildcard "matches anything" usb_device_id
322 * as a driver's "modules.usbmap" entry if you provide an id with
323 * only a nonzero "driver_info" field. If you do this, the USB device
324 * driver's probe() routine should use additional intelligence to
325 * decide whether to bind to the specified interface.
327 * What Makes Good usb_device_id Tables:
329 * The match algorithm is very simple, so that intelligence in
330 * driver selection must come from smart driver id records.
331 * Unless you have good reasons to use another selection policy,
332 * provide match elements only in related groups, and order match
333 * specifiers from specific to general. Use the macros provided
334 * for that purpose if you can.
336 * The most specific match specifiers use device descriptor
337 * data. These are commonly used with product-specific matches;
338 * the USB_DEVICE macro lets you provide vendor and product IDs,
339 * and you can also match against ranges of product revisions.
340 * These are widely used for devices with application or vendor
341 * specific bDeviceClass values.
343 * Matches based on device class/subclass/protocol specifications
344 * are slightly more general; use the USB_DEVICE_INFO macro, or
345 * its siblings. These are used with single-function devices
346 * where bDeviceClass doesn't specify that each interface has
349 * Matches based on interface class/subclass/protocol are the
350 * most general; they let drivers bind to any interface on a
351 * multiple-function device. Use the USB_INTERFACE_INFO
352 * macro, or its siblings, to match class-per-interface style
353 * devices (as recorded in bDeviceClass).
355 * Within those groups, remember that not all combinations are
356 * meaningful. For example, don't give a product version range
357 * without vendor and product IDs; or specify a protocol without
358 * its associated class and subclass.
360 const struct usb_device_id
*usb_match_id(struct usb_interface
*interface
,
361 const struct usb_device_id
*id
)
363 /* proc_connectinfo in devio.c may call us with id == NULL. */
367 /* It is important to check that id->driver_info is nonzero,
368 since an entry that is all zeroes except for a nonzero
369 id->driver_info is the way to create an entry that
370 indicates that the driver want to examine every
371 device and interface. */
372 for (; id
->idVendor
|| id
->bDeviceClass
|| id
->bInterfaceClass
||
373 id
->driver_info
; id
++) {
374 if (usb_match_one_id(interface
, id
))
380 EXPORT_SYMBOL_GPL_FUTURE(usb_match_id
);
382 int usb_device_match(struct device
*dev
, struct device_driver
*drv
)
384 struct usb_interface
*intf
;
385 struct usb_driver
*usb_drv
;
386 const struct usb_device_id
*id
;
388 /* check for generic driver, which we don't match any device with */
389 if (drv
== &usb_generic_driver
)
392 intf
= to_usb_interface(dev
);
393 usb_drv
= to_usb_driver(drv
);
395 id
= usb_match_id(intf
, usb_drv
->id_table
);
399 id
= usb_match_dynamic_id(intf
, usb_drv
);
406 * usb_register_driver - register a USB driver
407 * @new_driver: USB operations for the driver
408 * @owner: module owner of this driver.
410 * Registers a USB driver with the USB core. The list of unattached
411 * interfaces will be rescanned whenever a new driver is added, allowing
412 * the new driver to attach to any recognized devices.
413 * Returns a negative error code on failure and 0 on success.
415 * NOTE: if you want your driver to use the USB major number, you must call
416 * usb_register_dev() to enable that functionality. This function no longer
417 * takes care of that.
419 int usb_register_driver(struct usb_driver
*new_driver
, struct module
*owner
)
426 new_driver
->driver
.name
= (char *)new_driver
->name
;
427 new_driver
->driver
.bus
= &usb_bus_type
;
428 new_driver
->driver
.probe
= usb_probe_interface
;
429 new_driver
->driver
.remove
= usb_unbind_interface
;
430 new_driver
->driver
.owner
= owner
;
431 spin_lock_init(&new_driver
->dynids
.lock
);
432 INIT_LIST_HEAD(&new_driver
->dynids
.list
);
434 retval
= driver_register(&new_driver
->driver
);
437 pr_info("%s: registered new driver %s\n",
438 usbcore_name
, new_driver
->name
);
439 usbfs_update_special();
440 usb_create_newid_file(new_driver
);
442 printk(KERN_ERR
"%s: error %d registering driver %s\n",
443 usbcore_name
, retval
, new_driver
->name
);
448 EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver
);
451 * usb_deregister - unregister a USB driver
452 * @driver: USB operations of the driver to unregister
453 * Context: must be able to sleep
455 * Unlinks the specified driver from the internal USB driver list.
457 * NOTE: If you called usb_register_dev(), you still need to call
458 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
459 * this * call will no longer do it for you.
461 void usb_deregister(struct usb_driver
*driver
)
463 pr_info("%s: deregistering driver %s\n", usbcore_name
, driver
->name
);
465 usb_remove_newid_file(driver
);
466 usb_free_dynids(driver
);
467 driver_unregister(&driver
->driver
);
469 usbfs_update_special();
471 EXPORT_SYMBOL_GPL_FUTURE(usb_deregister
);