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
->probe
!= NULL
)
116 error
= sysfs_create_file(&usb_drv
->driver
.kobj
,
117 &driver_attr_new_id
.attr
);
121 static void usb_free_dynids(struct usb_driver
*usb_drv
)
123 struct usb_dynid
*dynid
, *n
;
125 spin_lock(&usb_drv
->dynids
.lock
);
126 list_for_each_entry_safe(dynid
, n
, &usb_drv
->dynids
.list
, node
) {
127 list_del(&dynid
->node
);
130 spin_unlock(&usb_drv
->dynids
.lock
);
133 static inline int usb_create_newid_file(struct usb_driver
*usb_drv
)
138 static inline void usb_free_dynids(struct usb_driver
*usb_drv
)
143 static const struct usb_device_id
*usb_match_dynamic_id(struct usb_interface
*intf
,
144 struct usb_driver
*drv
)
146 struct usb_dynid
*dynid
;
148 spin_lock(&drv
->dynids
.lock
);
149 list_for_each_entry(dynid
, &drv
->dynids
.list
, node
) {
150 if (usb_match_one_id(intf
, &dynid
->id
)) {
151 spin_unlock(&drv
->dynids
.lock
);
155 spin_unlock(&drv
->dynids
.lock
);
160 /* called from driver core with usb_bus_type.subsys writelock */
161 static int usb_probe_interface(struct device
*dev
)
163 struct usb_interface
* intf
= to_usb_interface(dev
);
164 struct usb_driver
* driver
= to_usb_driver(dev
->driver
);
165 const struct usb_device_id
*id
;
168 dev_dbg(dev
, "%s\n", __FUNCTION__
);
172 /* FIXME we'd much prefer to just resume it ... */
173 if (interface_to_usbdev(intf
)->state
== USB_STATE_SUSPENDED
)
174 return -EHOSTUNREACH
;
176 id
= usb_match_id(intf
, driver
->id_table
);
178 id
= usb_match_dynamic_id(intf
, driver
);
180 dev_dbg(dev
, "%s - got id\n", __FUNCTION__
);
182 /* Interface "power state" doesn't correspond to any hardware
183 * state whatsoever. We use it to record when it's bound to
184 * a driver that may start I/0: it's not frozen/quiesced.
187 intf
->condition
= USB_INTERFACE_BINDING
;
188 error
= driver
->probe(intf
, id
);
191 intf
->condition
= USB_INTERFACE_UNBOUND
;
193 intf
->condition
= USB_INTERFACE_BOUND
;
199 /* called from driver core with usb_bus_type.subsys writelock */
200 static int usb_unbind_interface(struct device
*dev
)
202 struct usb_interface
*intf
= to_usb_interface(dev
);
203 struct usb_driver
*driver
= to_usb_driver(intf
->dev
.driver
);
205 intf
->condition
= USB_INTERFACE_UNBINDING
;
207 /* release all urbs for this interface */
208 usb_disable_interface(interface_to_usbdev(intf
), intf
);
210 if (driver
&& driver
->disconnect
)
211 driver
->disconnect(intf
);
213 /* reset other interface state */
214 usb_set_interface(interface_to_usbdev(intf
),
215 intf
->altsetting
[0].desc
.bInterfaceNumber
,
217 usb_set_intfdata(intf
, NULL
);
218 intf
->condition
= USB_INTERFACE_UNBOUND
;
224 /* returns 0 if no match, 1 if match */
225 static int usb_match_one_id(struct usb_interface
*interface
,
226 const struct usb_device_id
*id
)
228 struct usb_host_interface
*intf
;
229 struct usb_device
*dev
;
231 /* proc_connectinfo in devio.c may call us with id == NULL. */
235 intf
= interface
->cur_altsetting
;
236 dev
= interface_to_usbdev(interface
);
238 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
239 id
->idVendor
!= le16_to_cpu(dev
->descriptor
.idVendor
))
242 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
243 id
->idProduct
!= le16_to_cpu(dev
->descriptor
.idProduct
))
246 /* No need to test id->bcdDevice_lo != 0, since 0 is never
247 greater than any unsigned number. */
248 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
249 (id
->bcdDevice_lo
> le16_to_cpu(dev
->descriptor
.bcdDevice
)))
252 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
253 (id
->bcdDevice_hi
< le16_to_cpu(dev
->descriptor
.bcdDevice
)))
256 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
257 (id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
))
260 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
261 (id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
))
264 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
265 (id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
))
268 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
269 (id
->bInterfaceClass
!= intf
->desc
.bInterfaceClass
))
272 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
273 (id
->bInterfaceSubClass
!= intf
->desc
.bInterfaceSubClass
))
276 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
277 (id
->bInterfaceProtocol
!= intf
->desc
.bInterfaceProtocol
))
283 * usb_match_id - find first usb_device_id matching device or interface
284 * @interface: the interface of interest
285 * @id: array of usb_device_id structures, terminated by zero entry
287 * usb_match_id searches an array of usb_device_id's and returns
288 * the first one matching the device or interface, or null.
289 * This is used when binding (or rebinding) a driver to an interface.
290 * Most USB device drivers will use this indirectly, through the usb core,
291 * but some layered driver frameworks use it directly.
292 * These device tables are exported with MODULE_DEVICE_TABLE, through
293 * modutils, to support the driver loading functionality of USB hotplugging.
297 * The "match_flags" element in a usb_device_id controls which
298 * members are used. If the corresponding bit is set, the
299 * value in the device_id must match its corresponding member
300 * in the device or interface descriptor, or else the device_id
303 * "driver_info" is normally used only by device drivers,
304 * but you can create a wildcard "matches anything" usb_device_id
305 * as a driver's "modules.usbmap" entry if you provide an id with
306 * only a nonzero "driver_info" field. If you do this, the USB device
307 * driver's probe() routine should use additional intelligence to
308 * decide whether to bind to the specified interface.
310 * What Makes Good usb_device_id Tables:
312 * The match algorithm is very simple, so that intelligence in
313 * driver selection must come from smart driver id records.
314 * Unless you have good reasons to use another selection policy,
315 * provide match elements only in related groups, and order match
316 * specifiers from specific to general. Use the macros provided
317 * for that purpose if you can.
319 * The most specific match specifiers use device descriptor
320 * data. These are commonly used with product-specific matches;
321 * the USB_DEVICE macro lets you provide vendor and product IDs,
322 * and you can also match against ranges of product revisions.
323 * These are widely used for devices with application or vendor
324 * specific bDeviceClass values.
326 * Matches based on device class/subclass/protocol specifications
327 * are slightly more general; use the USB_DEVICE_INFO macro, or
328 * its siblings. These are used with single-function devices
329 * where bDeviceClass doesn't specify that each interface has
332 * Matches based on interface class/subclass/protocol are the
333 * most general; they let drivers bind to any interface on a
334 * multiple-function device. Use the USB_INTERFACE_INFO
335 * macro, or its siblings, to match class-per-interface style
336 * devices (as recorded in bDeviceClass).
338 * Within those groups, remember that not all combinations are
339 * meaningful. For example, don't give a product version range
340 * without vendor and product IDs; or specify a protocol without
341 * its associated class and subclass.
343 const struct usb_device_id
*usb_match_id(struct usb_interface
*interface
,
344 const struct usb_device_id
*id
)
346 /* proc_connectinfo in devio.c may call us with id == NULL. */
350 /* It is important to check that id->driver_info is nonzero,
351 since an entry that is all zeroes except for a nonzero
352 id->driver_info is the way to create an entry that
353 indicates that the driver want to examine every
354 device and interface. */
355 for (; id
->idVendor
|| id
->bDeviceClass
|| id
->bInterfaceClass
||
356 id
->driver_info
; id
++) {
357 if (usb_match_one_id(interface
, id
))
363 EXPORT_SYMBOL_GPL(usb_match_id
);
365 int usb_device_match(struct device
*dev
, struct device_driver
*drv
)
367 struct usb_interface
*intf
;
368 struct usb_driver
*usb_drv
;
369 const struct usb_device_id
*id
;
371 /* check for generic driver, which we don't match any device with */
372 if (drv
== &usb_generic_driver
)
375 intf
= to_usb_interface(dev
);
376 usb_drv
= to_usb_driver(drv
);
378 id
= usb_match_id(intf
, usb_drv
->id_table
);
382 id
= usb_match_dynamic_id(intf
, usb_drv
);
389 * usb_register - register a USB driver
390 * @new_driver: USB operations for the driver
392 * Registers a USB driver with the USB core. The list of unattached
393 * interfaces will be rescanned whenever a new driver is added, allowing
394 * the new driver to attach to any recognized devices.
395 * Returns a negative error code on failure and 0 on success.
397 * NOTE: if you want your driver to use the USB major number, you must call
398 * usb_register_dev() to enable that functionality. This function no longer
399 * takes care of that.
401 int usb_register(struct usb_driver
*new_driver
)
408 new_driver
->driver
.name
= (char *)new_driver
->name
;
409 new_driver
->driver
.bus
= &usb_bus_type
;
410 new_driver
->driver
.probe
= usb_probe_interface
;
411 new_driver
->driver
.remove
= usb_unbind_interface
;
412 new_driver
->driver
.owner
= new_driver
->owner
;
413 spin_lock_init(&new_driver
->dynids
.lock
);
414 INIT_LIST_HEAD(&new_driver
->dynids
.list
);
416 usb_lock_all_devices();
417 retval
= driver_register(&new_driver
->driver
);
418 usb_unlock_all_devices();
421 pr_info("%s: registered new driver %s\n",
422 usbcore_name
, new_driver
->name
);
423 usbfs_update_special();
424 usb_create_newid_file(new_driver
);
426 printk(KERN_ERR
"%s: error %d registering driver %s\n",
427 usbcore_name
, retval
, new_driver
->name
);
432 EXPORT_SYMBOL_GPL(usb_register
);
435 * usb_deregister - unregister a USB driver
436 * @driver: USB operations of the driver to unregister
437 * Context: must be able to sleep
439 * Unlinks the specified driver from the internal USB driver list.
441 * NOTE: If you called usb_register_dev(), you still need to call
442 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
443 * this * call will no longer do it for you.
445 void usb_deregister(struct usb_driver
*driver
)
447 pr_info("%s: deregistering driver %s\n", usbcore_name
, driver
->name
);
449 usb_lock_all_devices();
450 usb_free_dynids(driver
);
451 driver_unregister(&driver
->driver
);
452 usb_unlock_all_devices();
454 usbfs_update_special();
456 EXPORT_SYMBOL_GPL(usb_deregister
);