blob: bb139f06bcd649b03692d0cf01a2c0fff10222a2 [file] [log] [blame]
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -08001/*
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
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080030static int usb_match_one_id(struct usb_interface *interface,
31 const struct usb_device_id *id);
32
33struct usb_dynid {
34 struct list_head node;
35 struct usb_device_id id;
36};
37
38
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -080039static int generic_probe(struct device *dev)
40{
41 return 0;
42}
43static int generic_remove(struct device *dev)
44{
45 struct usb_device *udev = to_usb_device(dev);
46
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);
51
52 /* in case the call failed or the device was suspended */
53 if (udev->state >= USB_STATE_CONFIGURED)
54 usb_disable_device(udev, 0);
55 return 0;
56}
57
58struct device_driver usb_generic_driver = {
59 .owner = THIS_MODULE,
60 .name = "usb",
61 .bus = &usb_bus_type,
62 .probe = generic_probe,
63 .remove = generic_remove,
64};
65
66/* Fun hack to determine if the struct device is a
67 * usb device or a usb interface. */
68int usb_generic_driver_data;
69
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -080070#ifdef CONFIG_HOTPLUG
71
72/*
73 * Adds a new dynamic USBdevice ID to this driver,
74 * and cause the driver to probe for all devices again.
75 */
76static ssize_t store_new_id(struct device_driver *driver,
77 const char *buf, size_t count)
78{
79 struct usb_driver *usb_drv = to_usb_driver(driver);
80 struct usb_dynid *dynid;
81 u32 idVendor = 0;
82 u32 idProduct = 0;
83 int fields = 0;
84
85 fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
86 if (fields < 2)
87 return -EINVAL;
88
89 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
90 if (!dynid)
91 return -ENOMEM;
92
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;
97
98 spin_lock(&usb_drv->dynids.lock);
99 list_add_tail(&usb_drv->dynids.list, &dynid->node);
100 spin_unlock(&usb_drv->dynids.lock);
101
102 if (get_driver(driver)) {
103 driver_attach(driver);
104 put_driver(driver);
105 }
106
107 return count;
108}
109static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
110
111static int usb_create_newid_file(struct usb_driver *usb_drv)
112{
113 int error = 0;
114
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800115 if (usb_drv->no_dynamic_id)
116 goto exit;
117
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800118 if (usb_drv->probe != NULL)
119 error = sysfs_create_file(&usb_drv->driver.kobj,
120 &driver_attr_new_id.attr);
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800121exit:
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800122 return error;
123}
124
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800125static void usb_remove_newid_file(struct usb_driver *usb_drv)
126{
127 if (usb_drv->no_dynamic_id)
128 return;
129
130 if (usb_drv->probe != NULL)
131 sysfs_remove_file(&usb_drv->driver.kobj,
132 &driver_attr_new_id.attr);
133}
134
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800135static void usb_free_dynids(struct usb_driver *usb_drv)
136{
137 struct usb_dynid *dynid, *n;
138
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);
142 kfree(dynid);
143 }
144 spin_unlock(&usb_drv->dynids.lock);
145}
146#else
147static inline int usb_create_newid_file(struct usb_driver *usb_drv)
148{
149 return 0;
150}
151
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800152static void usb_remove_newid_file(struct usb_driver *usb_drv)
153{
154}
155
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800156static inline void usb_free_dynids(struct usb_driver *usb_drv)
157{
158}
159#endif
160
161static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
162 struct usb_driver *drv)
163{
164 struct usb_dynid *dynid;
165
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);
170 return &dynid->id;
171 }
172 }
173 spin_unlock(&drv->dynids.lock);
174 return NULL;
175}
176
177
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800178/* called from driver core with usb_bus_type.subsys writelock */
179static int usb_probe_interface(struct device *dev)
180{
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;
184 int error = -ENODEV;
185
186 dev_dbg(dev, "%s\n", __FUNCTION__);
187
188 if (!driver->probe)
189 return error;
190 /* FIXME we'd much prefer to just resume it ... */
191 if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED)
192 return -EHOSTUNREACH;
193
194 id = usb_match_id(intf, driver->id_table);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800195 if (!id)
196 id = usb_match_dynamic_id(intf, driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800197 if (id) {
198 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
199
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.
203 */
204 mark_active(intf);
205 intf->condition = USB_INTERFACE_BINDING;
206 error = driver->probe(intf, id);
207 if (error) {
208 mark_quiesced(intf);
209 intf->condition = USB_INTERFACE_UNBOUND;
210 } else
211 intf->condition = USB_INTERFACE_BOUND;
212 }
213
214 return error;
215}
216
217/* called from driver core with usb_bus_type.subsys writelock */
218static int usb_unbind_interface(struct device *dev)
219{
220 struct usb_interface *intf = to_usb_interface(dev);
221 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
222
223 intf->condition = USB_INTERFACE_UNBINDING;
224
225 /* release all urbs for this interface */
226 usb_disable_interface(interface_to_usbdev(intf), intf);
227
228 if (driver && driver->disconnect)
229 driver->disconnect(intf);
230
231 /* reset other interface state */
232 usb_set_interface(interface_to_usbdev(intf),
233 intf->altsetting[0].desc.bInterfaceNumber,
234 0);
235 usb_set_intfdata(intf, NULL);
236 intf->condition = USB_INTERFACE_UNBOUND;
237 mark_quiesced(intf);
238
239 return 0;
240}
241
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800242/* returns 0 if no match, 1 if match */
243static int usb_match_one_id(struct usb_interface *interface,
244 const struct usb_device_id *id)
245{
246 struct usb_host_interface *intf;
247 struct usb_device *dev;
248
249 /* proc_connectinfo in devio.c may call us with id == NULL. */
250 if (id == NULL)
251 return 0;
252
253 intf = interface->cur_altsetting;
254 dev = interface_to_usbdev(interface);
255
256 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
257 id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
258 return 0;
259
260 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
261 id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
262 return 0;
263
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)))
268 return 0;
269
270 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
271 (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
272 return 0;
273
274 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
275 (id->bDeviceClass != dev->descriptor.bDeviceClass))
276 return 0;
277
278 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
279 (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
280 return 0;
281
282 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
283 (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
284 return 0;
285
286 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
287 (id->bInterfaceClass != intf->desc.bInterfaceClass))
288 return 0;
289
290 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
291 (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
292 return 0;
293
294 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
295 (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
296 return 0;
297
298 return 1;
299}
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800300/**
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
304 *
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.
312 *
313 * What Matches:
314 *
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
319 * does not match.
320 *
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.
327 *
328 * What Makes Good usb_device_id Tables:
329 *
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.
336 *
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.
343 *
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
348 * its own class.
349 *
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).
355 *
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.
360 */
361const struct usb_device_id *usb_match_id(struct usb_interface *interface,
362 const struct usb_device_id *id)
363{
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800364 /* proc_connectinfo in devio.c may call us with id == NULL. */
365 if (id == NULL)
366 return NULL;
367
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800368 /* 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++) {
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800375 if (usb_match_one_id(interface, id))
376 return id;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800377 }
378
379 return NULL;
380}
381EXPORT_SYMBOL_GPL(usb_match_id);
382
383int usb_device_match(struct device *dev, struct device_driver *drv)
384{
385 struct usb_interface *intf;
386 struct usb_driver *usb_drv;
387 const struct usb_device_id *id;
388
389 /* check for generic driver, which we don't match any device with */
390 if (drv == &usb_generic_driver)
391 return 0;
392
393 intf = to_usb_interface(dev);
394 usb_drv = to_usb_driver(drv);
395
396 id = usb_match_id(intf, usb_drv->id_table);
397 if (id)
398 return 1;
399
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800400 id = usb_match_dynamic_id(intf, usb_drv);
401 if (id)
402 return 1;
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800403 return 0;
404}
405
406/**
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800407 * usb_register_driver - register a USB driver
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800408 * @new_driver: USB operations for the driver
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800409 * @owner: module owner of this driver.
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800410 *
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.
415 *
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.
419 */
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800420int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800421{
422 int retval = 0;
423
424 if (usb_disabled())
425 return -ENODEV;
426
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;
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800431 new_driver->driver.owner = owner;
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800432 spin_lock_init(&new_driver->dynids.lock);
433 INIT_LIST_HEAD(&new_driver->dynids.list);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800434
435 usb_lock_all_devices();
436 retval = driver_register(&new_driver->driver);
437 usb_unlock_all_devices();
438
439 if (!retval) {
440 pr_info("%s: registered new driver %s\n",
441 usbcore_name, new_driver->name);
442 usbfs_update_special();
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800443 usb_create_newid_file(new_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800444 } else {
445 printk(KERN_ERR "%s: error %d registering driver %s\n",
446 usbcore_name, retval, new_driver->name);
447 }
448
449 return retval;
450}
Greg Kroah-Hartman2143acc2005-11-21 14:53:03 -0800451EXPORT_SYMBOL_GPL(usb_register_driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800452
453/**
454 * usb_deregister - unregister a USB driver
455 * @driver: USB operations of the driver to unregister
456 * Context: must be able to sleep
457 *
458 * Unlinks the specified driver from the internal USB driver list.
459 *
460 * NOTE: If you called usb_register_dev(), you still need to call
461 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
462 * this * call will no longer do it for you.
463 */
464void usb_deregister(struct usb_driver *driver)
465{
466 pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
467
468 usb_lock_all_devices();
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800469 usb_remove_newid_file(driver);
Greg Kroah-Hartman733260f2005-11-16 13:41:28 -0800470 usb_free_dynids(driver);
Greg Kroah-Hartmanddae41b2005-11-16 13:41:28 -0800471 driver_unregister(&driver->driver);
472 usb_unlock_all_devices();
473
474 usbfs_update_special();
475}
476EXPORT_SYMBOL_GPL(usb_deregister);