Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.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/device.h>
25 #include <linux/usb.h>
26 #include "hcd.h"
27 #include "usb.h"
28
29 static int usb_match_one_id(struct usb_interface *interface,
30                             const struct usb_device_id *id);
31
32 struct usb_dynid {
33         struct list_head node;
34         struct usb_device_id id;
35 };
36
37
38 static int generic_probe(struct device *dev)
39 {
40         return 0;
41 }
42 static int generic_remove(struct device *dev)
43 {
44         struct usb_device *udev = to_usb_device(dev);
45
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);
50
51         /* in case the call failed or the device was suspended */
52         if (udev->state >= USB_STATE_CONFIGURED)
53                 usb_disable_device(udev, 0);
54         return 0;
55 }
56
57 struct device_driver usb_generic_driver = {
58         .owner = THIS_MODULE,
59         .name = "usb",
60         .bus = &usb_bus_type,
61         .probe = generic_probe,
62         .remove = generic_remove,
63 };
64
65 /* Fun hack to determine if the struct device is a
66  * usb device or a usb interface. */
67 int usb_generic_driver_data;
68
69 #ifdef CONFIG_HOTPLUG
70
71 /*
72  * Adds a new dynamic USBdevice ID to this driver,
73  * and cause the driver to probe for all devices again.
74  */
75 static ssize_t store_new_id(struct device_driver *driver,
76                             const char *buf, size_t count)
77 {
78         struct usb_driver *usb_drv = to_usb_driver(driver);
79         struct usb_dynid *dynid;
80         u32 idVendor = 0;
81         u32 idProduct = 0;
82         int fields = 0;
83
84         fields = sscanf(buf, "%x %x", &idVendor, &idProduct);
85         if (fields < 2)
86                 return -EINVAL;
87
88         dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
89         if (!dynid)
90                 return -ENOMEM;
91
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;
96
97         spin_lock(&usb_drv->dynids.lock);
98         list_add_tail(&usb_drv->dynids.list, &dynid->node);
99         spin_unlock(&usb_drv->dynids.lock);
100
101         if (get_driver(driver)) {
102                 driver_attach(driver);
103                 put_driver(driver);
104         }
105
106         return count;
107 }
108 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
109
110 static int usb_create_newid_file(struct usb_driver *usb_drv)
111 {
112         int error = 0;
113
114         if (usb_drv->no_dynamic_id)
115                 goto exit;
116
117         if (usb_drv->probe != NULL)
118                 error = sysfs_create_file(&usb_drv->driver.kobj,
119                                           &driver_attr_new_id.attr);
120 exit:
121         return error;
122 }
123
124 static void usb_remove_newid_file(struct usb_driver *usb_drv)
125 {
126         if (usb_drv->no_dynamic_id)
127                 return;
128
129         if (usb_drv->probe != NULL)
130                 sysfs_remove_file(&usb_drv->driver.kobj,
131                                   &driver_attr_new_id.attr);
132 }
133
134 static void usb_free_dynids(struct usb_driver *usb_drv)
135 {
136         struct usb_dynid *dynid, *n;
137
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);
141                 kfree(dynid);
142         }
143         spin_unlock(&usb_drv->dynids.lock);
144 }
145 #else
146 static inline int usb_create_newid_file(struct usb_driver *usb_drv)
147 {
148         return 0;
149 }
150
151 static void usb_remove_newid_file(struct usb_driver *usb_drv)
152 {
153 }
154
155 static inline void usb_free_dynids(struct usb_driver *usb_drv)
156 {
157 }
158 #endif
159
160 static const struct usb_device_id *usb_match_dynamic_id(struct usb_interface *intf,
161                                                         struct usb_driver *drv)
162 {
163         struct usb_dynid *dynid;
164
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);
169                         return &dynid->id;
170                 }
171         }
172         spin_unlock(&drv->dynids.lock);
173         return NULL;
174 }
175
176
177 /* called from driver core with usb_bus_type.subsys writelock */
178 static int usb_probe_interface(struct device *dev)
179 {
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;
183         int error = -ENODEV;
184
185         dev_dbg(dev, "%s\n", __FUNCTION__);
186
187         if (!driver->probe)
188                 return error;
189         /* FIXME we'd much prefer to just resume it ... */
190         if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED)
191                 return -EHOSTUNREACH;
192
193         id = usb_match_id(intf, driver->id_table);
194         if (!id)
195                 id = usb_match_dynamic_id(intf, driver);
196         if (id) {
197                 dev_dbg(dev, "%s - got id\n", __FUNCTION__);
198
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.
202                  */
203                 mark_active(intf);
204                 intf->condition = USB_INTERFACE_BINDING;
205                 error = driver->probe(intf, id);
206                 if (error) {
207                         mark_quiesced(intf);
208                         intf->condition = USB_INTERFACE_UNBOUND;
209                 } else
210                         intf->condition = USB_INTERFACE_BOUND;
211         }
212
213         return error;
214 }
215
216 /* called from driver core with usb_bus_type.subsys writelock */
217 static int usb_unbind_interface(struct device *dev)
218 {
219         struct usb_interface *intf = to_usb_interface(dev);
220         struct usb_driver *driver = to_usb_driver(intf->dev.driver);
221
222         intf->condition = USB_INTERFACE_UNBINDING;
223
224         /* release all urbs for this interface */
225         usb_disable_interface(interface_to_usbdev(intf), intf);
226
227         if (driver && driver->disconnect)
228                 driver->disconnect(intf);
229
230         /* reset other interface state */
231         usb_set_interface(interface_to_usbdev(intf),
232                         intf->altsetting[0].desc.bInterfaceNumber,
233                         0);
234         usb_set_intfdata(intf, NULL);
235         intf->condition = USB_INTERFACE_UNBOUND;
236         mark_quiesced(intf);
237
238         return 0;
239 }
240
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)
244 {
245         struct usb_host_interface *intf;
246         struct usb_device *dev;
247
248         /* proc_connectinfo in devio.c may call us with id == NULL. */
249         if (id == NULL)
250                 return 0;
251
252         intf = interface->cur_altsetting;
253         dev = interface_to_usbdev(interface);
254
255         if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
256             id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
257                 return 0;
258
259         if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
260             id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
261                 return 0;
262
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)))
267                 return 0;
268
269         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
270             (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
271                 return 0;
272
273         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
274             (id->bDeviceClass != dev->descriptor.bDeviceClass))
275                 return 0;
276
277         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
278             (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass))
279                 return 0;
280
281         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
282             (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
283                 return 0;
284
285         if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
286             (id->bInterfaceClass != intf->desc.bInterfaceClass))
287                 return 0;
288
289         if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
290             (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass))
291                 return 0;
292
293         if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
294             (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol))
295                 return 0;
296
297         return 1;
298 }
299 /**
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
303  *
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.
311  *
312  * What Matches:
313  *
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
318  * does not match.
319  *
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.
326  *
327  * What Makes Good usb_device_id Tables:
328  *
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.
335  *
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.
342  *
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
347  * its own class.
348  *
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).
354  *
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.
359  */
360 const struct usb_device_id *usb_match_id(struct usb_interface *interface,
361                                          const struct usb_device_id *id)
362 {
363         /* proc_connectinfo in devio.c may call us with id == NULL. */
364         if (id == NULL)
365                 return NULL;
366
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))
375                         return id;
376         }
377
378         return NULL;
379 }
380 EXPORT_SYMBOL_GPL_FUTURE(usb_match_id);
381
382 int usb_device_match(struct device *dev, struct device_driver *drv)
383 {
384         struct usb_interface *intf;
385         struct usb_driver *usb_drv;
386         const struct usb_device_id *id;
387
388         /* check for generic driver, which we don't match any device with */
389         if (drv == &usb_generic_driver)
390                 return 0;
391
392         intf = to_usb_interface(dev);
393         usb_drv = to_usb_driver(drv);
394
395         id = usb_match_id(intf, usb_drv->id_table);
396         if (id)
397                 return 1;
398
399         id = usb_match_dynamic_id(intf, usb_drv);
400         if (id)
401                 return 1;
402         return 0;
403 }
404
405 /**
406  * usb_register_driver - register a USB driver
407  * @new_driver: USB operations for the driver
408  * @owner: module owner of this driver.
409  *
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.
414  *
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.
418  */
419 int usb_register_driver(struct usb_driver *new_driver, struct module *owner)
420 {
421         int retval = 0;
422
423         if (usb_disabled())
424                 return -ENODEV;
425
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);
433
434         retval = driver_register(&new_driver->driver);
435
436         if (!retval) {
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);
441         } else {
442                 printk(KERN_ERR "%s: error %d registering driver %s\n",
443                         usbcore_name, retval, new_driver->name);
444         }
445
446         return retval;
447 }
448 EXPORT_SYMBOL_GPL_FUTURE(usb_register_driver);
449
450 /**
451  * usb_deregister - unregister a USB driver
452  * @driver: USB operations of the driver to unregister
453  * Context: must be able to sleep
454  *
455  * Unlinks the specified driver from the internal USB driver list.
456  *
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.
460  */
461 void usb_deregister(struct usb_driver *driver)
462 {
463         pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name);
464
465         usb_remove_newid_file(driver);
466         usb_free_dynids(driver);
467         driver_unregister(&driver->driver);
468
469         usbfs_update_special();
470 }
471 EXPORT_SYMBOL_GPL_FUTURE(usb_deregister);