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 usb_match_one_id(struct usb_interface
*interface
,
31 const struct usb_device_id
*id
);
34 struct list_head node
;
35 struct usb_device_id id
;
39 static int generic_probe(struct device
*dev
)
43 static int generic_remove(struct device
*dev
)
45 struct usb_device
*udev
= to_usb_device(dev
);
47 /* if this is only an unbind, not a physical disconnect, then
48 * unconfigure the device */
49 if (udev
->state
== USB_STATE_CONFIGURED
)
50 usb_set_configuration(udev
, 0);
52 /* in case the call failed or the device was suspended */
53 if (udev
->state
>= USB_STATE_CONFIGURED
)
54 usb_disable_device(udev
, 0);
58 struct device_driver usb_generic_driver
= {
62 .probe
= generic_probe
,
63 .remove
= generic_remove
,
66 /* Fun hack to determine if the struct device is a
67 * usb device or a usb interface. */
68 int usb_generic_driver_data
;
73 * Adds a new dynamic USBdevice ID to this driver,
74 * and cause the driver to probe for all devices again.
76 static ssize_t
store_new_id(struct device_driver
*driver
,
77 const char *buf
, size_t count
)
79 struct usb_driver
*usb_drv
= to_usb_driver(driver
);
80 struct usb_dynid
*dynid
;
85 fields
= sscanf(buf
, "%x %x", &idVendor
, &idProduct
);
89 dynid
= kzalloc(sizeof(*dynid
), GFP_KERNEL
);
93 INIT_LIST_HEAD(&dynid
->node
);
94 dynid
->id
.idVendor
= idVendor
;
95 dynid
->id
.idProduct
= idProduct
;
96 dynid
->id
.match_flags
= USB_DEVICE_ID_MATCH_DEVICE
;
98 spin_lock(&usb_drv
->dynids
.lock
);
99 list_add_tail(&usb_drv
->dynids
.list
, &dynid
->node
);
100 spin_unlock(&usb_drv
->dynids
.lock
);
102 if (get_driver(driver
)) {
103 driver_attach(driver
);
109 static DRIVER_ATTR(new_id
, S_IWUSR
, NULL
, store_new_id
);
111 static int usb_create_newid_file(struct usb_driver
*usb_drv
)
115 if (usb_drv
->no_dynamic_id
)
118 if (usb_drv
->probe
!= NULL
)
119 error
= sysfs_create_file(&usb_drv
->driver
.kobj
,
120 &driver_attr_new_id
.attr
);
125 static void usb_remove_newid_file(struct usb_driver
*usb_drv
)
127 if (usb_drv
->no_dynamic_id
)
130 if (usb_drv
->probe
!= NULL
)
131 sysfs_remove_file(&usb_drv
->driver
.kobj
,
132 &driver_attr_new_id
.attr
);
135 static void usb_free_dynids(struct usb_driver
*usb_drv
)
137 struct usb_dynid
*dynid
, *n
;
139 spin_lock(&usb_drv
->dynids
.lock
);
140 list_for_each_entry_safe(dynid
, n
, &usb_drv
->dynids
.list
, node
) {
141 list_del(&dynid
->node
);
144 spin_unlock(&usb_drv
->dynids
.lock
);
147 static inline int usb_create_newid_file(struct usb_driver
*usb_drv
)
152 static void usb_remove_newid_file(struct usb_driver
*usb_drv
)
156 static inline void usb_free_dynids(struct usb_driver
*usb_drv
)
161 static const struct usb_device_id
*usb_match_dynamic_id(struct usb_interface
*intf
,
162 struct usb_driver
*drv
)
164 struct usb_dynid
*dynid
;
166 spin_lock(&drv
->dynids
.lock
);
167 list_for_each_entry(dynid
, &drv
->dynids
.list
, node
) {
168 if (usb_match_one_id(intf
, &dynid
->id
)) {
169 spin_unlock(&drv
->dynids
.lock
);
173 spin_unlock(&drv
->dynids
.lock
);
178 /* called from driver core with usb_bus_type.subsys writelock */
179 static int usb_probe_interface(struct device
*dev
)
181 struct usb_interface
* intf
= to_usb_interface(dev
);
182 struct usb_driver
* driver
= to_usb_driver(dev
->driver
);
183 const struct usb_device_id
*id
;
186 dev_dbg(dev
, "%s\n", __FUNCTION__
);
190 /* FIXME we'd much prefer to just resume it ... */
191 if (interface_to_usbdev(intf
)->state
== USB_STATE_SUSPENDED
)
192 return -EHOSTUNREACH
;
194 id
= usb_match_id(intf
, driver
->id_table
);
196 id
= usb_match_dynamic_id(intf
, driver
);
198 dev_dbg(dev
, "%s - got id\n", __FUNCTION__
);
200 /* Interface "power state" doesn't correspond to any hardware
201 * state whatsoever. We use it to record when it's bound to
202 * a driver that may start I/0: it's not frozen/quiesced.
205 intf
->condition
= USB_INTERFACE_BINDING
;
206 error
= driver
->probe(intf
, id
);
209 intf
->condition
= USB_INTERFACE_UNBOUND
;
211 intf
->condition
= USB_INTERFACE_BOUND
;
217 /* called from driver core with usb_bus_type.subsys writelock */
218 static int usb_unbind_interface(struct device
*dev
)
220 struct usb_interface
*intf
= to_usb_interface(dev
);
221 struct usb_driver
*driver
= to_usb_driver(intf
->dev
.driver
);
223 intf
->condition
= USB_INTERFACE_UNBINDING
;
225 /* release all urbs for this interface */
226 usb_disable_interface(interface_to_usbdev(intf
), intf
);
228 if (driver
&& driver
->disconnect
)
229 driver
->disconnect(intf
);
231 /* reset other interface state */
232 usb_set_interface(interface_to_usbdev(intf
),
233 intf
->altsetting
[0].desc
.bInterfaceNumber
,
235 usb_set_intfdata(intf
, NULL
);
236 intf
->condition
= USB_INTERFACE_UNBOUND
;
242 /* returns 0 if no match, 1 if match */
243 static int usb_match_one_id(struct usb_interface
*interface
,
244 const struct usb_device_id
*id
)
246 struct usb_host_interface
*intf
;
247 struct usb_device
*dev
;
249 /* proc_connectinfo in devio.c may call us with id == NULL. */
253 intf
= interface
->cur_altsetting
;
254 dev
= interface_to_usbdev(interface
);
256 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
257 id
->idVendor
!= le16_to_cpu(dev
->descriptor
.idVendor
))
260 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
261 id
->idProduct
!= le16_to_cpu(dev
->descriptor
.idProduct
))
264 /* No need to test id->bcdDevice_lo != 0, since 0 is never
265 greater than any unsigned number. */
266 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
267 (id
->bcdDevice_lo
> le16_to_cpu(dev
->descriptor
.bcdDevice
)))
270 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
271 (id
->bcdDevice_hi
< le16_to_cpu(dev
->descriptor
.bcdDevice
)))
274 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
275 (id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
))
278 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
279 (id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
))
282 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
283 (id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
))
286 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
287 (id
->bInterfaceClass
!= intf
->desc
.bInterfaceClass
))
290 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
291 (id
->bInterfaceSubClass
!= intf
->desc
.bInterfaceSubClass
))
294 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
295 (id
->bInterfaceProtocol
!= intf
->desc
.bInterfaceProtocol
))
301 * usb_match_id - find first usb_device_id matching device or interface
302 * @interface: the interface of interest
303 * @id: array of usb_device_id structures, terminated by zero entry
305 * usb_match_id searches an array of usb_device_id's and returns
306 * the first one matching the device or interface, or null.
307 * This is used when binding (or rebinding) a driver to an interface.
308 * Most USB device drivers will use this indirectly, through the usb core,
309 * but some layered driver frameworks use it directly.
310 * These device tables are exported with MODULE_DEVICE_TABLE, through
311 * modutils, to support the driver loading functionality of USB hotplugging.
315 * The "match_flags" element in a usb_device_id controls which
316 * members are used. If the corresponding bit is set, the
317 * value in the device_id must match its corresponding member
318 * in the device or interface descriptor, or else the device_id
321 * "driver_info" is normally used only by device drivers,
322 * but you can create a wildcard "matches anything" usb_device_id
323 * as a driver's "modules.usbmap" entry if you provide an id with
324 * only a nonzero "driver_info" field. If you do this, the USB device
325 * driver's probe() routine should use additional intelligence to
326 * decide whether to bind to the specified interface.
328 * What Makes Good usb_device_id Tables:
330 * The match algorithm is very simple, so that intelligence in
331 * driver selection must come from smart driver id records.
332 * Unless you have good reasons to use another selection policy,
333 * provide match elements only in related groups, and order match
334 * specifiers from specific to general. Use the macros provided
335 * for that purpose if you can.
337 * The most specific match specifiers use device descriptor
338 * data. These are commonly used with product-specific matches;
339 * the USB_DEVICE macro lets you provide vendor and product IDs,
340 * and you can also match against ranges of product revisions.
341 * These are widely used for devices with application or vendor
342 * specific bDeviceClass values.
344 * Matches based on device class/subclass/protocol specifications
345 * are slightly more general; use the USB_DEVICE_INFO macro, or
346 * its siblings. These are used with single-function devices
347 * where bDeviceClass doesn't specify that each interface has
350 * Matches based on interface class/subclass/protocol are the
351 * most general; they let drivers bind to any interface on a
352 * multiple-function device. Use the USB_INTERFACE_INFO
353 * macro, or its siblings, to match class-per-interface style
354 * devices (as recorded in bDeviceClass).
356 * Within those groups, remember that not all combinations are
357 * meaningful. For example, don't give a product version range
358 * without vendor and product IDs; or specify a protocol without
359 * its associated class and subclass.
361 const struct usb_device_id
*usb_match_id(struct usb_interface
*interface
,
362 const struct usb_device_id
*id
)
364 /* proc_connectinfo in devio.c may call us with id == NULL. */
368 /* It is important to check that id->driver_info is nonzero,
369 since an entry that is all zeroes except for a nonzero
370 id->driver_info is the way to create an entry that
371 indicates that the driver want to examine every
372 device and interface. */
373 for (; id
->idVendor
|| id
->bDeviceClass
|| id
->bInterfaceClass
||
374 id
->driver_info
; id
++) {
375 if (usb_match_one_id(interface
, id
))
381 EXPORT_SYMBOL_GPL_FUTURE(usb_match_id
);
383 int usb_device_match(struct device
*dev
, struct device_driver
*drv
)
385 struct usb_interface
*intf
;
386 struct usb_driver
*usb_drv
;
387 const struct usb_device_id
*id
;
389 /* check for generic driver, which we don't match any device with */
390 if (drv
== &usb_generic_driver
)
393 intf
= to_usb_interface(dev
);
394 usb_drv
= to_usb_driver(drv
);
396 id
= usb_match_id(intf
, usb_drv
->id_table
);
400 id
= usb_match_dynamic_id(intf
, usb_drv
);
407 * usb_register_driver - register a USB driver
408 * @new_driver: USB operations for the driver
409 * @owner: module owner of this driver.
411 * Registers a USB driver with the USB core. The list of unattached
412 * interfaces will be rescanned whenever a new driver is added, allowing
413 * the new driver to attach to any recognized devices.
414 * Returns a negative error code on failure and 0 on success.
416 * NOTE: if you want your driver to use the USB major number, you must call
417 * usb_register_dev() to enable that functionality. This function no longer
418 * takes care of that.
420 int usb_register_driver(struct usb_driver
*new_driver
, struct module
*owner
)
427 new_driver
->driver
.name
= (char *)new_driver
->name
;
428 new_driver
->driver
.bus
= &usb_bus_type
;
429 new_driver
->driver
.probe
= usb_probe_interface
;
430 new_driver
->driver
.remove
= usb_unbind_interface
;
431 new_driver
->driver
.owner
= owner
;
432 spin_lock_init(&new_driver
->dynids
.lock
);
433 INIT_LIST_HEAD(&new_driver
->dynids
.list
);
435 retval
= driver_register(&new_driver
->driver
);
438 pr_info("%s: registered new driver %s\n",
439 usbcore_name
, new_driver
->name
);
440 usbfs_update_special();
441 usb_create_newid_file(new_driver
);
443 printk(KERN_ERR
"%s: error %d registering driver %s\n",
444 usbcore_name
, retval
, new_driver
->name
);
449 EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver
);
452 * usb_deregister - unregister a USB driver
453 * @driver: USB operations of the driver to unregister
454 * Context: must be able to sleep
456 * Unlinks the specified driver from the internal USB driver list.
458 * NOTE: If you called usb_register_dev(), you still need to call
459 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
460 * this * call will no longer do it for you.
462 void usb_deregister(struct usb_driver
*driver
)
464 pr_info("%s: deregistering driver %s\n", usbcore_name
, driver
->name
);
466 usb_remove_newid_file(driver
);
467 usb_free_dynids(driver
);
468 driver_unregister(&driver
->driver
);
470 usbfs_update_special();
472 EXPORT_SYMBOL_GPL_FUTURE(usb_deregister
);