[PATCH] USB: add dynamic id functionality to USB core
[pandora-kernel.git] / drivers / usb / core / driver.c
1 /*
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
30 static int usb_match_one_id(struct usb_interface *interface,
31                             const struct usb_device_id *id);
32
33 struct usb_dynid {
34         struct list_head node;
35         struct usb_device_id id;
36 };
37
38
39 static int generic_probe(struct device *dev)
40 {
41         return 0;
42 }
43 static 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
58 struct 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. */
68 int usb_generic_driver_data;
69
70 #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  */
76 static 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 }
109 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
110
111 static int usb_create_newid_file(struct usb_driver *usb_drv)
112 {
113         int error = 0;
114
115         if (usb_drv->probe != NULL)
116                 error = sysfs_create_file(&usb_drv->driver.kobj,
117                                           &driver_attr_new_id.attr);
118         return error;
119 }
120
121 static void usb_free_dynids(struct usb_driver *usb_drv)
122 {
123         struct usb_dynid *dynid, *n;
124
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);
128                 kfree(dynid);
129         }
130         spin_unlock(&usb_drv->dynids.lock);
131 }
132 #else
133 static inline int usb_create_newid_file(struct usb_driver *usb_drv)
134 {
135         return 0;
136 }
137
138 static inline void usb_free_dynids(struct usb_driver *usb_drv)
139 {
140 }
141 #endif
142
143 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
144                                                         struct usb_driver *drv)
145 {
146         struct usb_dynid *dynid;
147
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);
152                         return &dynid->id;
153                 }
154         }
155         spin_unlock(&drv->dynids.lock);
156         return NULL;
157 }
158
159
160 /* called from driver core with usb_bus_type.subsys writelock */
161 static int usb_probe_interface(struct device *dev)
162 {
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;
166         int error = -ENODEV;
167
168         dev_dbg(dev, "%s\n", __FUNCTION__);
169
170         if (!driver->probe)
171                 return error;
172         /* FIXME we'd much prefer to just resume it ... */
173         if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED)
174                 return -EHOSTUNREACH;
175
176         id = usb_match_id(intf, driver->id_table);
177         if (!id)
178                 id = usb_match_dynamic_id(intf, driver);
179         if (id) {
180                 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
181
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.
185                  */
186                 mark_active(intf);
187                 intf->condition = USB_INTERFACE_BINDING;
188                 error = driver->probe(intf, id);
189                 if (error) {
190                         mark_quiesced(intf);
191                         intf->condition = USB_INTERFACE_UNBOUND;
192                 } else
193                         intf->condition = USB_INTERFACE_BOUND;
194         }
195
196         return error;
197 }
198
199 /* called from driver core with usb_bus_type.subsys writelock */
200 static int usb_unbind_interface(struct device *dev)
201 {
202         struct usb_interface *intf = to_usb_interface(dev);
203         struct usb_driver *driver = to_usb_driver(intf->dev.driver);
204
205         intf->condition = USB_INTERFACE_UNBINDING;
206
207         /* release all urbs for this interface */
208         usb_disable_interface(interface_to_usbdev(intf), intf);
209
210         if (driver && driver->disconnect)
211                 driver->disconnect(intf);
212
213         /* reset other interface state */
214         usb_set_interface(interface_to_usbdev(intf),
215                         intf->altsetting[0].desc.bInterfaceNumber,
216                         0);
217         usb_set_intfdata(intf, NULL);
218         intf->condition = USB_INTERFACE_UNBOUND;
219         mark_quiesced(intf);
220
221         return 0;
222 }
223
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)
227 {
228         struct usb_host_interface *intf;
229         struct usb_device *dev;
230
231         /* proc_connectinfo in devio.c may call us with id == NULL. */
232         if (id == NULL)
233                 return 0;
234
235         intf = interface->cur_altsetting;
236         dev = interface_to_usbdev(interface);
237
238         if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
239             id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
240                 return 0;
241
242         if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
243             id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
244                 return 0;
245
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)))
250                 return 0;
251
252         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
253             (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
254                 return 0;
255
256         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
257             (id->bDeviceClass != dev->descriptor.bDeviceClass))
258                 return 0;
259
260         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
261             (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
262                 return 0;
263
264         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
265             (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
266                 return 0;
267
268         if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
269             (id->bInterfaceClass != intf->desc.bInterfaceClass))
270                 return 0;
271
272         if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
273             (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
274                 return 0;
275
276         if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
277             (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
278                 return 0;
279
280         return 1;
281 }
282 /**
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
286  *
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.
294  *
295  * What Matches:
296  *
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
301  * does not match.
302  *
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.
309  *
310  * What Makes Good usb_device_id Tables:
311  *
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.
318  *
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.
325  *
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
330  * its own class.
331  *
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).
337  *
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.
342  */
343 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
344                                          const struct usb_device_id *id)
345 {
346         /* proc_connectinfo in devio.c may call us with id == NULL. */
347         if (id == NULL)
348                 return NULL;
349
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))
358                         return id;
359         }
360
361         return NULL;
362 }
363 EXPORT_SYMBOL_GPL(usb_match_id);
364
365 int usb_device_match(struct device *dev, struct device_driver *drv)
366 {
367         struct usb_interface *intf;
368         struct usb_driver *usb_drv;
369         const struct usb_device_id *id;
370
371         /* check for generic driver, which we don't match any device with */
372         if (drv == &usb_generic_driver)
373                 return 0;
374
375         intf = to_usb_interface(dev);
376         usb_drv = to_usb_driver(drv);
377
378         id = usb_match_id(intf, usb_drv->id_table);
379         if (id)
380                 return 1;
381
382         id = usb_match_dynamic_id(intf, usb_drv);
383         if (id)
384                 return 1;
385         return 0;
386 }
387
388 /**
389  * usb_register - register a USB driver
390  * @new_driver: USB operations for the driver
391  *
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.
396  *
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.
400  */
401 int usb_register(struct usb_driver *new_driver)
402 {
403         int retval = 0;
404
405         if (usb_disabled())
406                 return -ENODEV;
407
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);
415
416         usb_lock_all_devices();
417         retval = driver_register(&new_driver->driver);
418         usb_unlock_all_devices();
419
420         if (!retval) {
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);
425         } else {
426                 printk(KERN_ERR "%s: error %d registering driver %s\n",
427                         usbcore_name, retval, new_driver->name);
428         }
429
430         return retval;
431 }
432 EXPORT_SYMBOL_GPL(usb_register);
433
434 /**
435  * usb_deregister - unregister a USB driver
436  * @driver: USB operations of the driver to unregister
437  * Context: must be able to sleep
438  *
439  * Unlinks the specified driver from the internal USB driver list.
440  *
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.
444  */
445 void usb_deregister(struct usb_driver *driver)
446 {
447         pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
448
449         usb_lock_all_devices();
450         usb_free_dynids(driver);
451         driver_unregister(&driver->driver);
452         usb_unlock_all_devices();
453
454         usbfs_update_special();
455 }
456 EXPORT_SYMBOL_GPL(usb_deregister);