Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/usb/usb.c | |
3 | * | |
4 | * (C) Copyright Linus Torvalds 1999 | |
5 | * (C) Copyright Johannes Erdfelt 1999-2001 | |
6 | * (C) Copyright Andreas Gal 1999 | |
7 | * (C) Copyright Gregory P. Smith 1999 | |
8 | * (C) Copyright Deti Fliegl 1999 (new USB architecture) | |
9 | * (C) Copyright Randy Dunlap 2000 | |
10 | * (C) Copyright David Brownell 2000-2004 | |
11 | * (C) Copyright Yggdrasil Computing, Inc. 2000 | |
12 | * (usb_device_id matching changes by Adam J. Richter) | |
13 | * (C) Copyright Greg Kroah-Hartman 2002-2003 | |
14 | * | |
15 | * NOTE! This is not actually a driver at all, rather this is | |
16 | * just a collection of helper routines that implement the | |
17 | * generic USB things that the real drivers can use.. | |
18 | * | |
19 | * Think of this as a "USB library" rather than anything else. | |
20 | * It should be considered a slave, with no callbacks. Callbacks | |
21 | * are evil. | |
22 | */ | |
23 | ||
24 | #include <linux/config.h> | |
25 | ||
26 | #ifdef CONFIG_USB_DEBUG | |
27 | #define DEBUG | |
28 | #else | |
29 | #undef DEBUG | |
30 | #endif | |
31 | ||
32 | #include <linux/module.h> | |
33 | #include <linux/string.h> | |
34 | #include <linux/bitops.h> | |
35 | #include <linux/slab.h> | |
36 | #include <linux/interrupt.h> /* for in_interrupt() */ | |
37 | #include <linux/kmod.h> | |
38 | #include <linux/init.h> | |
39 | #include <linux/spinlock.h> | |
40 | #include <linux/errno.h> | |
41 | #include <linux/smp_lock.h> | |
42 | #include <linux/rwsem.h> | |
43 | #include <linux/usb.h> | |
44 | ||
45 | #include <asm/io.h> | |
46 | #include <asm/scatterlist.h> | |
47 | #include <linux/mm.h> | |
48 | #include <linux/dma-mapping.h> | |
49 | ||
50 | #include "hcd.h" | |
51 | #include "usb.h" | |
52 | ||
1da177e4 LT |
53 | |
54 | const char *usbcore_name = "usbcore"; | |
55 | ||
56 | static int nousb; /* Disable USB when built into kernel image */ | |
57 | /* Not honored on modular build */ | |
58 | ||
59 | static DECLARE_RWSEM(usb_all_devices_rwsem); | |
60 | ||
61 | ||
62 | static int generic_probe (struct device *dev) | |
63 | { | |
64 | return 0; | |
65 | } | |
66 | static int generic_remove (struct device *dev) | |
67 | { | |
3b4d7f79 AS |
68 | struct usb_device *udev = to_usb_device(dev); |
69 | ||
70 | /* if this is only an unbind, not a physical disconnect, then | |
71 | * unconfigure the device */ | |
72 | if (udev->state == USB_STATE_CONFIGURED) | |
73 | usb_set_configuration(udev, 0); | |
74 | ||
75 | /* in case the call failed or the device was suspended */ | |
76 | if (udev->state >= USB_STATE_CONFIGURED) | |
77 | usb_disable_device(udev, 0); | |
1da177e4 LT |
78 | return 0; |
79 | } | |
80 | ||
81 | static struct device_driver usb_generic_driver = { | |
82 | .owner = THIS_MODULE, | |
83 | .name = "usb", | |
84 | .bus = &usb_bus_type, | |
85 | .probe = generic_probe, | |
86 | .remove = generic_remove, | |
87 | }; | |
88 | ||
89 | static int usb_generic_driver_data; | |
90 | ||
91 | /* called from driver core with usb_bus_type.subsys writelock */ | |
92 | static int usb_probe_interface(struct device *dev) | |
93 | { | |
94 | struct usb_interface * intf = to_usb_interface(dev); | |
95 | struct usb_driver * driver = to_usb_driver(dev->driver); | |
96 | const struct usb_device_id *id; | |
97 | int error = -ENODEV; | |
98 | ||
99 | dev_dbg(dev, "%s\n", __FUNCTION__); | |
100 | ||
101 | if (!driver->probe) | |
102 | return error; | |
103 | /* FIXME we'd much prefer to just resume it ... */ | |
104 | if (interface_to_usbdev(intf)->state == USB_STATE_SUSPENDED) | |
105 | return -EHOSTUNREACH; | |
106 | ||
107 | id = usb_match_id (intf, driver->id_table); | |
108 | if (id) { | |
109 | dev_dbg (dev, "%s - got id\n", __FUNCTION__); | |
110 | intf->condition = USB_INTERFACE_BINDING; | |
111 | error = driver->probe (intf, id); | |
112 | intf->condition = error ? USB_INTERFACE_UNBOUND : | |
113 | USB_INTERFACE_BOUND; | |
114 | } | |
115 | ||
116 | return error; | |
117 | } | |
118 | ||
119 | /* called from driver core with usb_bus_type.subsys writelock */ | |
120 | static int usb_unbind_interface(struct device *dev) | |
121 | { | |
122 | struct usb_interface *intf = to_usb_interface(dev); | |
123 | struct usb_driver *driver = to_usb_driver(intf->dev.driver); | |
124 | ||
125 | intf->condition = USB_INTERFACE_UNBINDING; | |
126 | ||
127 | /* release all urbs for this interface */ | |
128 | usb_disable_interface(interface_to_usbdev(intf), intf); | |
129 | ||
130 | if (driver && driver->disconnect) | |
131 | driver->disconnect(intf); | |
132 | ||
133 | /* reset other interface state */ | |
134 | usb_set_interface(interface_to_usbdev(intf), | |
135 | intf->altsetting[0].desc.bInterfaceNumber, | |
136 | 0); | |
137 | usb_set_intfdata(intf, NULL); | |
138 | intf->condition = USB_INTERFACE_UNBOUND; | |
139 | ||
140 | return 0; | |
141 | } | |
142 | ||
143 | /** | |
144 | * usb_register - register a USB driver | |
145 | * @new_driver: USB operations for the driver | |
146 | * | |
147 | * Registers a USB driver with the USB core. The list of unattached | |
148 | * interfaces will be rescanned whenever a new driver is added, allowing | |
149 | * the new driver to attach to any recognized devices. | |
150 | * Returns a negative error code on failure and 0 on success. | |
151 | * | |
152 | * NOTE: if you want your driver to use the USB major number, you must call | |
153 | * usb_register_dev() to enable that functionality. This function no longer | |
154 | * takes care of that. | |
155 | */ | |
156 | int usb_register(struct usb_driver *new_driver) | |
157 | { | |
158 | int retval = 0; | |
159 | ||
160 | if (nousb) | |
161 | return -ENODEV; | |
162 | ||
163 | new_driver->driver.name = (char *)new_driver->name; | |
164 | new_driver->driver.bus = &usb_bus_type; | |
165 | new_driver->driver.probe = usb_probe_interface; | |
166 | new_driver->driver.remove = usb_unbind_interface; | |
167 | new_driver->driver.owner = new_driver->owner; | |
168 | ||
169 | usb_lock_all_devices(); | |
170 | retval = driver_register(&new_driver->driver); | |
171 | usb_unlock_all_devices(); | |
172 | ||
173 | if (!retval) { | |
174 | pr_info("%s: registered new driver %s\n", | |
175 | usbcore_name, new_driver->name); | |
176 | usbfs_update_special(); | |
177 | } else { | |
178 | printk(KERN_ERR "%s: error %d registering driver %s\n", | |
179 | usbcore_name, retval, new_driver->name); | |
180 | } | |
181 | ||
182 | return retval; | |
183 | } | |
184 | ||
185 | /** | |
186 | * usb_deregister - unregister a USB driver | |
187 | * @driver: USB operations of the driver to unregister | |
188 | * Context: must be able to sleep | |
189 | * | |
190 | * Unlinks the specified driver from the internal USB driver list. | |
191 | * | |
192 | * NOTE: If you called usb_register_dev(), you still need to call | |
193 | * usb_deregister_dev() to clean up your driver's allocated minor numbers, | |
194 | * this * call will no longer do it for you. | |
195 | */ | |
196 | void usb_deregister(struct usb_driver *driver) | |
197 | { | |
198 | pr_info("%s: deregistering driver %s\n", usbcore_name, driver->name); | |
199 | ||
200 | usb_lock_all_devices(); | |
201 | driver_unregister (&driver->driver); | |
202 | usb_unlock_all_devices(); | |
203 | ||
204 | usbfs_update_special(); | |
205 | } | |
206 | ||
207 | /** | |
208 | * usb_ifnum_to_if - get the interface object with a given interface number | |
209 | * @dev: the device whose current configuration is considered | |
210 | * @ifnum: the desired interface | |
211 | * | |
212 | * This walks the device descriptor for the currently active configuration | |
213 | * and returns a pointer to the interface with that particular interface | |
214 | * number, or null. | |
215 | * | |
216 | * Note that configuration descriptors are not required to assign interface | |
217 | * numbers sequentially, so that it would be incorrect to assume that | |
218 | * the first interface in that descriptor corresponds to interface zero. | |
219 | * This routine helps device drivers avoid such mistakes. | |
220 | * However, you should make sure that you do the right thing with any | |
221 | * alternate settings available for this interfaces. | |
222 | * | |
223 | * Don't call this function unless you are bound to one of the interfaces | |
224 | * on this device or you have locked the device! | |
225 | */ | |
226 | struct usb_interface *usb_ifnum_to_if(struct usb_device *dev, unsigned ifnum) | |
227 | { | |
228 | struct usb_host_config *config = dev->actconfig; | |
229 | int i; | |
230 | ||
231 | if (!config) | |
232 | return NULL; | |
233 | for (i = 0; i < config->desc.bNumInterfaces; i++) | |
234 | if (config->interface[i]->altsetting[0] | |
235 | .desc.bInterfaceNumber == ifnum) | |
236 | return config->interface[i]; | |
237 | ||
238 | return NULL; | |
239 | } | |
240 | ||
241 | /** | |
242 | * usb_altnum_to_altsetting - get the altsetting structure with a given | |
243 | * alternate setting number. | |
244 | * @intf: the interface containing the altsetting in question | |
245 | * @altnum: the desired alternate setting number | |
246 | * | |
247 | * This searches the altsetting array of the specified interface for | |
248 | * an entry with the correct bAlternateSetting value and returns a pointer | |
249 | * to that entry, or null. | |
250 | * | |
251 | * Note that altsettings need not be stored sequentially by number, so | |
252 | * it would be incorrect to assume that the first altsetting entry in | |
253 | * the array corresponds to altsetting zero. This routine helps device | |
254 | * drivers avoid such mistakes. | |
255 | * | |
256 | * Don't call this function unless you are bound to the intf interface | |
257 | * or you have locked the device! | |
258 | */ | |
259 | struct usb_host_interface *usb_altnum_to_altsetting(struct usb_interface *intf, | |
260 | unsigned int altnum) | |
261 | { | |
262 | int i; | |
263 | ||
264 | for (i = 0; i < intf->num_altsetting; i++) { | |
265 | if (intf->altsetting[i].desc.bAlternateSetting == altnum) | |
266 | return &intf->altsetting[i]; | |
267 | } | |
268 | return NULL; | |
269 | } | |
270 | ||
271 | /** | |
272 | * usb_driver_claim_interface - bind a driver to an interface | |
273 | * @driver: the driver to be bound | |
274 | * @iface: the interface to which it will be bound; must be in the | |
275 | * usb device's active configuration | |
276 | * @priv: driver data associated with that interface | |
277 | * | |
278 | * This is used by usb device drivers that need to claim more than one | |
279 | * interface on a device when probing (audio and acm are current examples). | |
280 | * No device driver should directly modify internal usb_interface or | |
281 | * usb_device structure members. | |
282 | * | |
283 | * Few drivers should need to use this routine, since the most natural | |
284 | * way to bind to an interface is to return the private data from | |
285 | * the driver's probe() method. | |
286 | * | |
287 | * Callers must own the device lock and the driver model's usb_bus_type.subsys | |
288 | * writelock. So driver probe() entries don't need extra locking, | |
289 | * but other call contexts may need to explicitly claim those locks. | |
290 | */ | |
291 | int usb_driver_claim_interface(struct usb_driver *driver, | |
292 | struct usb_interface *iface, void* priv) | |
293 | { | |
294 | struct device *dev = &iface->dev; | |
295 | ||
296 | if (dev->driver) | |
297 | return -EBUSY; | |
298 | ||
299 | dev->driver = &driver->driver; | |
300 | usb_set_intfdata(iface, priv); | |
301 | iface->condition = USB_INTERFACE_BOUND; | |
302 | ||
303 | /* if interface was already added, bind now; else let | |
304 | * the future device_add() bind it, bypassing probe() | |
305 | */ | |
273971ba | 306 | if (klist_node_attached(&dev->knode_bus)) |
1da177e4 LT |
307 | device_bind_driver(dev); |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
312 | /** | |
313 | * usb_driver_release_interface - unbind a driver from an interface | |
314 | * @driver: the driver to be unbound | |
315 | * @iface: the interface from which it will be unbound | |
316 | * | |
317 | * This can be used by drivers to release an interface without waiting | |
318 | * for their disconnect() methods to be called. In typical cases this | |
319 | * also causes the driver disconnect() method to be called. | |
320 | * | |
321 | * This call is synchronous, and may not be used in an interrupt context. | |
322 | * Callers must own the device lock and the driver model's usb_bus_type.subsys | |
323 | * writelock. So driver disconnect() entries don't need extra locking, | |
324 | * but other call contexts may need to explicitly claim those locks. | |
325 | */ | |
326 | void usb_driver_release_interface(struct usb_driver *driver, | |
327 | struct usb_interface *iface) | |
328 | { | |
329 | struct device *dev = &iface->dev; | |
330 | ||
331 | /* this should never happen, don't release something that's not ours */ | |
332 | if (!dev->driver || dev->driver != &driver->driver) | |
333 | return; | |
334 | ||
f4096618 AS |
335 | /* don't release from within disconnect() */ |
336 | if (iface->condition != USB_INTERFACE_BOUND) | |
337 | return; | |
338 | ||
339 | /* release only after device_add() */ | |
340 | if (klist_node_attached(&dev->knode_bus)) { | |
341 | iface->condition = USB_INTERFACE_UNBINDING; | |
1da177e4 | 342 | device_release_driver(dev); |
f4096618 | 343 | } |
1da177e4 LT |
344 | |
345 | dev->driver = NULL; | |
346 | usb_set_intfdata(iface, NULL); | |
347 | iface->condition = USB_INTERFACE_UNBOUND; | |
348 | } | |
349 | ||
350 | /** | |
351 | * usb_match_id - find first usb_device_id matching device or interface | |
352 | * @interface: the interface of interest | |
353 | * @id: array of usb_device_id structures, terminated by zero entry | |
354 | * | |
355 | * usb_match_id searches an array of usb_device_id's and returns | |
356 | * the first one matching the device or interface, or null. | |
357 | * This is used when binding (or rebinding) a driver to an interface. | |
358 | * Most USB device drivers will use this indirectly, through the usb core, | |
359 | * but some layered driver frameworks use it directly. | |
360 | * These device tables are exported with MODULE_DEVICE_TABLE, through | |
361 | * modutils and "modules.usbmap", to support the driver loading | |
362 | * functionality of USB hotplugging. | |
363 | * | |
364 | * What Matches: | |
365 | * | |
366 | * The "match_flags" element in a usb_device_id controls which | |
367 | * members are used. If the corresponding bit is set, the | |
368 | * value in the device_id must match its corresponding member | |
369 | * in the device or interface descriptor, or else the device_id | |
370 | * does not match. | |
371 | * | |
372 | * "driver_info" is normally used only by device drivers, | |
373 | * but you can create a wildcard "matches anything" usb_device_id | |
374 | * as a driver's "modules.usbmap" entry if you provide an id with | |
375 | * only a nonzero "driver_info" field. If you do this, the USB device | |
376 | * driver's probe() routine should use additional intelligence to | |
377 | * decide whether to bind to the specified interface. | |
378 | * | |
379 | * What Makes Good usb_device_id Tables: | |
380 | * | |
381 | * The match algorithm is very simple, so that intelligence in | |
382 | * driver selection must come from smart driver id records. | |
383 | * Unless you have good reasons to use another selection policy, | |
384 | * provide match elements only in related groups, and order match | |
385 | * specifiers from specific to general. Use the macros provided | |
386 | * for that purpose if you can. | |
387 | * | |
388 | * The most specific match specifiers use device descriptor | |
389 | * data. These are commonly used with product-specific matches; | |
390 | * the USB_DEVICE macro lets you provide vendor and product IDs, | |
391 | * and you can also match against ranges of product revisions. | |
392 | * These are widely used for devices with application or vendor | |
393 | * specific bDeviceClass values. | |
394 | * | |
395 | * Matches based on device class/subclass/protocol specifications | |
396 | * are slightly more general; use the USB_DEVICE_INFO macro, or | |
397 | * its siblings. These are used with single-function devices | |
398 | * where bDeviceClass doesn't specify that each interface has | |
399 | * its own class. | |
400 | * | |
401 | * Matches based on interface class/subclass/protocol are the | |
402 | * most general; they let drivers bind to any interface on a | |
403 | * multiple-function device. Use the USB_INTERFACE_INFO | |
404 | * macro, or its siblings, to match class-per-interface style | |
405 | * devices (as recorded in bDeviceClass). | |
406 | * | |
407 | * Within those groups, remember that not all combinations are | |
408 | * meaningful. For example, don't give a product version range | |
409 | * without vendor and product IDs; or specify a protocol without | |
410 | * its associated class and subclass. | |
411 | */ | |
412 | const struct usb_device_id * | |
413 | usb_match_id(struct usb_interface *interface, const struct usb_device_id *id) | |
414 | { | |
415 | struct usb_host_interface *intf; | |
416 | struct usb_device *dev; | |
417 | ||
418 | /* proc_connectinfo in devio.c may call us with id == NULL. */ | |
419 | if (id == NULL) | |
420 | return NULL; | |
421 | ||
422 | intf = interface->cur_altsetting; | |
423 | dev = interface_to_usbdev(interface); | |
424 | ||
425 | /* It is important to check that id->driver_info is nonzero, | |
426 | since an entry that is all zeroes except for a nonzero | |
427 | id->driver_info is the way to create an entry that | |
428 | indicates that the driver want to examine every | |
429 | device and interface. */ | |
430 | for (; id->idVendor || id->bDeviceClass || id->bInterfaceClass || | |
431 | id->driver_info; id++) { | |
432 | ||
433 | if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && | |
434 | id->idVendor != le16_to_cpu(dev->descriptor.idVendor)) | |
435 | continue; | |
436 | ||
437 | if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && | |
438 | id->idProduct != le16_to_cpu(dev->descriptor.idProduct)) | |
439 | continue; | |
440 | ||
441 | /* No need to test id->bcdDevice_lo != 0, since 0 is never | |
442 | greater than any unsigned number. */ | |
443 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && | |
444 | (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice))) | |
445 | continue; | |
446 | ||
447 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && | |
448 | (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice))) | |
449 | continue; | |
450 | ||
451 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && | |
452 | (id->bDeviceClass != dev->descriptor.bDeviceClass)) | |
453 | continue; | |
454 | ||
455 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && | |
456 | (id->bDeviceSubClass!= dev->descriptor.bDeviceSubClass)) | |
457 | continue; | |
458 | ||
459 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && | |
460 | (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol)) | |
461 | continue; | |
462 | ||
463 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && | |
464 | (id->bInterfaceClass != intf->desc.bInterfaceClass)) | |
465 | continue; | |
466 | ||
467 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && | |
468 | (id->bInterfaceSubClass != intf->desc.bInterfaceSubClass)) | |
469 | continue; | |
470 | ||
471 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && | |
472 | (id->bInterfaceProtocol != intf->desc.bInterfaceProtocol)) | |
473 | continue; | |
474 | ||
475 | return id; | |
476 | } | |
477 | ||
478 | return NULL; | |
479 | } | |
480 | ||
6034a080 | 481 | |
482 | static int __find_interface(struct device * dev, void * data) | |
483 | { | |
484 | struct usb_interface ** ret = (struct usb_interface **)data; | |
485 | struct usb_interface * intf = *ret; | |
486 | int *minor = (int *)data; | |
487 | ||
488 | /* can't look at usb devices, only interfaces */ | |
489 | if (dev->driver == &usb_generic_driver) | |
490 | return 0; | |
491 | ||
492 | intf = to_usb_interface(dev); | |
493 | if (intf->minor != -1 && intf->minor == *minor) { | |
494 | *ret = intf; | |
495 | return 1; | |
496 | } | |
497 | return 0; | |
498 | } | |
499 | ||
1da177e4 LT |
500 | /** |
501 | * usb_find_interface - find usb_interface pointer for driver and device | |
502 | * @drv: the driver whose current configuration is considered | |
503 | * @minor: the minor number of the desired device | |
504 | * | |
505 | * This walks the driver device list and returns a pointer to the interface | |
506 | * with the matching minor. Note, this only works for devices that share the | |
507 | * USB major number. | |
508 | */ | |
509 | struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor) | |
510 | { | |
ff710710 | 511 | struct usb_interface *intf = (struct usb_interface *)(long)minor; |
6034a080 | 512 | int ret; |
1da177e4 | 513 | |
6034a080 | 514 | ret = driver_for_each_device(&drv->driver, NULL, &intf, __find_interface); |
1da177e4 | 515 | |
6034a080 | 516 | return ret ? intf : NULL; |
1da177e4 LT |
517 | } |
518 | ||
519 | static int usb_device_match (struct device *dev, struct device_driver *drv) | |
520 | { | |
521 | struct usb_interface *intf; | |
522 | struct usb_driver *usb_drv; | |
523 | const struct usb_device_id *id; | |
524 | ||
525 | /* check for generic driver, which we don't match any device with */ | |
526 | if (drv == &usb_generic_driver) | |
527 | return 0; | |
528 | ||
529 | intf = to_usb_interface(dev); | |
530 | usb_drv = to_usb_driver(drv); | |
531 | ||
532 | id = usb_match_id (intf, usb_drv->id_table); | |
533 | if (id) | |
534 | return 1; | |
535 | ||
536 | return 0; | |
537 | } | |
538 | ||
539 | ||
540 | #ifdef CONFIG_HOTPLUG | |
541 | ||
542 | /* | |
543 | * USB hotplugging invokes what /proc/sys/kernel/hotplug says | |
544 | * (normally /sbin/hotplug) when USB devices get added or removed. | |
545 | * | |
546 | * This invokes a user mode policy agent, typically helping to load driver | |
547 | * or other modules, configure the device, and more. Drivers can provide | |
548 | * a MODULE_DEVICE_TABLE to help with module loading subtasks. | |
549 | * | |
550 | * We're called either from khubd (the typical case) or from root hub | |
551 | * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle | |
552 | * delays in event delivery. Use sysfs (and DEVPATH) to make sure the | |
553 | * device (and this configuration!) are still present. | |
554 | */ | |
555 | static int usb_hotplug (struct device *dev, char **envp, int num_envp, | |
556 | char *buffer, int buffer_size) | |
557 | { | |
558 | struct usb_interface *intf; | |
559 | struct usb_device *usb_dev; | |
560 | int i = 0; | |
561 | int length = 0; | |
562 | ||
563 | if (!dev) | |
564 | return -ENODEV; | |
565 | ||
566 | /* driver is often null here; dev_dbg() would oops */ | |
567 | pr_debug ("usb %s: hotplug\n", dev->bus_id); | |
568 | ||
569 | /* Must check driver_data here, as on remove driver is always NULL */ | |
570 | if ((dev->driver == &usb_generic_driver) || | |
571 | (dev->driver_data == &usb_generic_driver_data)) | |
572 | return 0; | |
573 | ||
574 | intf = to_usb_interface(dev); | |
575 | usb_dev = interface_to_usbdev (intf); | |
576 | ||
577 | if (usb_dev->devnum < 0) { | |
578 | pr_debug ("usb %s: already deleted?\n", dev->bus_id); | |
579 | return -ENODEV; | |
580 | } | |
581 | if (!usb_dev->bus) { | |
582 | pr_debug ("usb %s: bus removed?\n", dev->bus_id); | |
583 | return -ENODEV; | |
584 | } | |
585 | ||
586 | #ifdef CONFIG_USB_DEVICEFS | |
587 | /* If this is available, userspace programs can directly read | |
588 | * all the device descriptors we don't tell them about. Or | |
589 | * even act as usermode drivers. | |
590 | * | |
591 | * FIXME reduce hardwired intelligence here | |
592 | */ | |
593 | if (add_hotplug_env_var(envp, num_envp, &i, | |
594 | buffer, buffer_size, &length, | |
595 | "DEVICE=/proc/bus/usb/%03d/%03d", | |
596 | usb_dev->bus->busnum, usb_dev->devnum)) | |
597 | return -ENOMEM; | |
598 | #endif | |
599 | ||
600 | /* per-device configurations are common */ | |
601 | if (add_hotplug_env_var(envp, num_envp, &i, | |
602 | buffer, buffer_size, &length, | |
603 | "PRODUCT=%x/%x/%x", | |
604 | le16_to_cpu(usb_dev->descriptor.idVendor), | |
605 | le16_to_cpu(usb_dev->descriptor.idProduct), | |
606 | le16_to_cpu(usb_dev->descriptor.bcdDevice))) | |
607 | return -ENOMEM; | |
608 | ||
609 | /* class-based driver binding models */ | |
610 | if (add_hotplug_env_var(envp, num_envp, &i, | |
611 | buffer, buffer_size, &length, | |
612 | "TYPE=%d/%d/%d", | |
613 | usb_dev->descriptor.bDeviceClass, | |
614 | usb_dev->descriptor.bDeviceSubClass, | |
615 | usb_dev->descriptor.bDeviceProtocol)) | |
616 | return -ENOMEM; | |
617 | ||
618 | if (usb_dev->descriptor.bDeviceClass == 0) { | |
619 | struct usb_host_interface *alt = intf->cur_altsetting; | |
620 | ||
621 | /* 2.4 only exposed interface zero. in 2.5, hotplug | |
622 | * agents are called for all interfaces, and can use | |
623 | * $DEVPATH/bInterfaceNumber if necessary. | |
624 | */ | |
625 | if (add_hotplug_env_var(envp, num_envp, &i, | |
626 | buffer, buffer_size, &length, | |
627 | "INTERFACE=%d/%d/%d", | |
628 | alt->desc.bInterfaceClass, | |
629 | alt->desc.bInterfaceSubClass, | |
630 | alt->desc.bInterfaceProtocol)) | |
631 | return -ENOMEM; | |
632 | ||
633 | if (add_hotplug_env_var(envp, num_envp, &i, | |
634 | buffer, buffer_size, &length, | |
fb3b4ebc | 635 | "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X", |
1da177e4 LT |
636 | le16_to_cpu(usb_dev->descriptor.idVendor), |
637 | le16_to_cpu(usb_dev->descriptor.idProduct), | |
638 | le16_to_cpu(usb_dev->descriptor.bcdDevice), | |
1da177e4 LT |
639 | usb_dev->descriptor.bDeviceClass, |
640 | usb_dev->descriptor.bDeviceSubClass, | |
641 | usb_dev->descriptor.bDeviceProtocol, | |
642 | alt->desc.bInterfaceClass, | |
643 | alt->desc.bInterfaceSubClass, | |
644 | alt->desc.bInterfaceProtocol)) | |
645 | return -ENOMEM; | |
646 | } else { | |
647 | if (add_hotplug_env_var(envp, num_envp, &i, | |
648 | buffer, buffer_size, &length, | |
fb3b4ebc | 649 | "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic*isc*ip*", |
1da177e4 LT |
650 | le16_to_cpu(usb_dev->descriptor.idVendor), |
651 | le16_to_cpu(usb_dev->descriptor.idProduct), | |
652 | le16_to_cpu(usb_dev->descriptor.bcdDevice), | |
1da177e4 LT |
653 | usb_dev->descriptor.bDeviceClass, |
654 | usb_dev->descriptor.bDeviceSubClass, | |
655 | usb_dev->descriptor.bDeviceProtocol)) | |
656 | return -ENOMEM; | |
657 | } | |
658 | ||
659 | envp[i] = NULL; | |
660 | ||
661 | return 0; | |
662 | } | |
663 | ||
664 | #else | |
665 | ||
666 | static int usb_hotplug (struct device *dev, char **envp, | |
667 | int num_envp, char *buffer, int buffer_size) | |
668 | { | |
669 | return -ENODEV; | |
670 | } | |
671 | ||
672 | #endif /* CONFIG_HOTPLUG */ | |
673 | ||
674 | /** | |
675 | * usb_release_dev - free a usb device structure when all users of it are finished. | |
676 | * @dev: device that's been disconnected | |
677 | * | |
678 | * Will be called only by the device core when all users of this usb device are | |
679 | * done. | |
680 | */ | |
681 | static void usb_release_dev(struct device *dev) | |
682 | { | |
683 | struct usb_device *udev; | |
684 | ||
685 | udev = to_usb_device(dev); | |
686 | ||
687 | usb_destroy_configuration(udev); | |
688 | usb_bus_put(udev->bus); | |
689 | kfree(udev->product); | |
690 | kfree(udev->manufacturer); | |
691 | kfree(udev->serial); | |
692 | kfree(udev); | |
693 | } | |
694 | ||
695 | /** | |
696 | * usb_alloc_dev - usb device constructor (usbcore-internal) | |
697 | * @parent: hub to which device is connected; null to allocate a root hub | |
698 | * @bus: bus used to access the device | |
699 | * @port1: one-based index of port; ignored for root hubs | |
700 | * Context: !in_interrupt () | |
701 | * | |
702 | * Only hub drivers (including virtual root hub drivers for host | |
703 | * controllers) should ever call this. | |
704 | * | |
705 | * This call may not be used in a non-sleeping context. | |
706 | */ | |
707 | struct usb_device * | |
708 | usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1) | |
709 | { | |
710 | struct usb_device *dev; | |
711 | ||
712 | dev = kmalloc(sizeof(*dev), GFP_KERNEL); | |
713 | if (!dev) | |
714 | return NULL; | |
715 | ||
716 | memset(dev, 0, sizeof(*dev)); | |
717 | ||
718 | bus = usb_bus_get(bus); | |
719 | if (!bus) { | |
720 | kfree(dev); | |
721 | return NULL; | |
722 | } | |
723 | ||
724 | device_initialize(&dev->dev); | |
725 | dev->dev.bus = &usb_bus_type; | |
726 | dev->dev.dma_mask = bus->controller->dma_mask; | |
727 | dev->dev.driver_data = &usb_generic_driver_data; | |
728 | dev->dev.driver = &usb_generic_driver; | |
729 | dev->dev.release = usb_release_dev; | |
730 | dev->state = USB_STATE_ATTACHED; | |
731 | ||
732 | INIT_LIST_HEAD(&dev->ep0.urb_list); | |
733 | dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE; | |
734 | dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT; | |
735 | /* ep0 maxpacket comes later, from device descriptor */ | |
736 | dev->ep_in[0] = dev->ep_out[0] = &dev->ep0; | |
737 | ||
738 | /* Save readable and stable topology id, distinguishing devices | |
739 | * by location for diagnostics, tools, driver model, etc. The | |
740 | * string is a path along hub ports, from the root. Each device's | |
741 | * dev->devpath will be stable until USB is re-cabled, and hubs | |
742 | * are often labeled with these port numbers. The bus_id isn't | |
743 | * as stable: bus->busnum changes easily from modprobe order, | |
744 | * cardbus or pci hotplugging, and so on. | |
745 | */ | |
746 | if (unlikely (!parent)) { | |
747 | dev->devpath [0] = '0'; | |
748 | ||
749 | dev->dev.parent = bus->controller; | |
750 | sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum); | |
751 | } else { | |
752 | /* match any labeling on the hubs; it's one-based */ | |
753 | if (parent->devpath [0] == '0') | |
754 | snprintf (dev->devpath, sizeof dev->devpath, | |
755 | "%d", port1); | |
756 | else | |
757 | snprintf (dev->devpath, sizeof dev->devpath, | |
758 | "%s.%d", parent->devpath, port1); | |
759 | ||
760 | dev->dev.parent = &parent->dev; | |
761 | sprintf (&dev->dev.bus_id[0], "%d-%s", | |
762 | bus->busnum, dev->devpath); | |
763 | ||
764 | /* hub driver sets up TT records */ | |
765 | } | |
766 | ||
767 | dev->bus = bus; | |
768 | dev->parent = parent; | |
769 | INIT_LIST_HEAD(&dev->filelist); | |
770 | ||
771 | init_MUTEX(&dev->serialize); | |
772 | ||
773 | return dev; | |
774 | } | |
775 | ||
776 | /** | |
777 | * usb_get_dev - increments the reference count of the usb device structure | |
778 | * @dev: the device being referenced | |
779 | * | |
780 | * Each live reference to a device should be refcounted. | |
781 | * | |
782 | * Drivers for USB interfaces should normally record such references in | |
783 | * their probe() methods, when they bind to an interface, and release | |
784 | * them by calling usb_put_dev(), in their disconnect() methods. | |
785 | * | |
786 | * A pointer to the device with the incremented reference counter is returned. | |
787 | */ | |
788 | struct usb_device *usb_get_dev(struct usb_device *dev) | |
789 | { | |
790 | if (dev) | |
791 | get_device(&dev->dev); | |
792 | return dev; | |
793 | } | |
794 | ||
795 | /** | |
796 | * usb_put_dev - release a use of the usb device structure | |
797 | * @dev: device that's been disconnected | |
798 | * | |
799 | * Must be called when a user of a device is finished with it. When the last | |
800 | * user of the device calls this function, the memory of the device is freed. | |
801 | */ | |
802 | void usb_put_dev(struct usb_device *dev) | |
803 | { | |
804 | if (dev) | |
805 | put_device(&dev->dev); | |
806 | } | |
807 | ||
808 | /** | |
809 | * usb_get_intf - increments the reference count of the usb interface structure | |
810 | * @intf: the interface being referenced | |
811 | * | |
812 | * Each live reference to a interface must be refcounted. | |
813 | * | |
814 | * Drivers for USB interfaces should normally record such references in | |
815 | * their probe() methods, when they bind to an interface, and release | |
816 | * them by calling usb_put_intf(), in their disconnect() methods. | |
817 | * | |
818 | * A pointer to the interface with the incremented reference counter is | |
819 | * returned. | |
820 | */ | |
821 | struct usb_interface *usb_get_intf(struct usb_interface *intf) | |
822 | { | |
823 | if (intf) | |
824 | get_device(&intf->dev); | |
825 | return intf; | |
826 | } | |
827 | ||
828 | /** | |
829 | * usb_put_intf - release a use of the usb interface structure | |
830 | * @intf: interface that's been decremented | |
831 | * | |
832 | * Must be called when a user of an interface is finished with it. When the | |
833 | * last user of the interface calls this function, the memory of the interface | |
834 | * is freed. | |
835 | */ | |
836 | void usb_put_intf(struct usb_interface *intf) | |
837 | { | |
838 | if (intf) | |
839 | put_device(&intf->dev); | |
840 | } | |
841 | ||
842 | ||
843 | /* USB device locking | |
844 | * | |
845 | * Although locking USB devices should be straightforward, it is | |
846 | * complicated by the way the driver-model core works. When a new USB | |
847 | * driver is registered or unregistered, the core will automatically | |
848 | * probe or disconnect all matching interfaces on all USB devices while | |
849 | * holding the USB subsystem writelock. There's no good way for us to | |
850 | * tell which devices will be used or to lock them beforehand; our only | |
851 | * option is to effectively lock all the USB devices. | |
852 | * | |
853 | * We do that by using a private rw-semaphore, usb_all_devices_rwsem. | |
854 | * When locking an individual device you must first acquire the rwsem's | |
855 | * readlock. When a driver is registered or unregistered the writelock | |
856 | * must be held. These actions are encapsulated in the subroutines | |
857 | * below, so all a driver needs to do is call usb_lock_device() and | |
858 | * usb_unlock_device(). | |
859 | * | |
860 | * Complications arise when several devices are to be locked at the same | |
861 | * time. Only hub-aware drivers that are part of usbcore ever have to | |
862 | * do this; nobody else needs to worry about it. The problem is that | |
863 | * usb_lock_device() must not be called to lock a second device since it | |
864 | * would acquire the rwsem's readlock reentrantly, leading to deadlock if | |
865 | * another thread was waiting for the writelock. The solution is simple: | |
866 | * | |
867 | * When locking more than one device, call usb_lock_device() | |
868 | * to lock the first one. Lock the others by calling | |
869 | * down(&udev->serialize) directly. | |
870 | * | |
871 | * When unlocking multiple devices, use up(&udev->serialize) | |
872 | * to unlock all but the last one. Unlock the last one by | |
873 | * calling usb_unlock_device(). | |
874 | * | |
875 | * When locking both a device and its parent, always lock the | |
876 | * the parent first. | |
877 | */ | |
878 | ||
879 | /** | |
880 | * usb_lock_device - acquire the lock for a usb device structure | |
881 | * @udev: device that's being locked | |
882 | * | |
883 | * Use this routine when you don't hold any other device locks; | |
884 | * to acquire nested inner locks call down(&udev->serialize) directly. | |
885 | * This is necessary for proper interaction with usb_lock_all_devices(). | |
886 | */ | |
887 | void usb_lock_device(struct usb_device *udev) | |
888 | { | |
889 | down_read(&usb_all_devices_rwsem); | |
890 | down(&udev->serialize); | |
891 | } | |
892 | ||
893 | /** | |
894 | * usb_trylock_device - attempt to acquire the lock for a usb device structure | |
895 | * @udev: device that's being locked | |
896 | * | |
897 | * Don't use this routine if you already hold a device lock; | |
898 | * use down_trylock(&udev->serialize) instead. | |
899 | * This is necessary for proper interaction with usb_lock_all_devices(). | |
900 | * | |
901 | * Returns 1 if successful, 0 if contention. | |
902 | */ | |
903 | int usb_trylock_device(struct usb_device *udev) | |
904 | { | |
905 | if (!down_read_trylock(&usb_all_devices_rwsem)) | |
906 | return 0; | |
907 | if (down_trylock(&udev->serialize)) { | |
908 | up_read(&usb_all_devices_rwsem); | |
909 | return 0; | |
910 | } | |
911 | return 1; | |
912 | } | |
913 | ||
914 | /** | |
915 | * usb_lock_device_for_reset - cautiously acquire the lock for a | |
916 | * usb device structure | |
917 | * @udev: device that's being locked | |
918 | * @iface: interface bound to the driver making the request (optional) | |
919 | * | |
920 | * Attempts to acquire the device lock, but fails if the device is | |
921 | * NOTATTACHED or SUSPENDED, or if iface is specified and the interface | |
922 | * is neither BINDING nor BOUND. Rather than sleeping to wait for the | |
923 | * lock, the routine polls repeatedly. This is to prevent deadlock with | |
924 | * disconnect; in some drivers (such as usb-storage) the disconnect() | |
3ea15966 | 925 | * or suspend() method will block waiting for a device reset to complete. |
1da177e4 LT |
926 | * |
927 | * Returns a negative error code for failure, otherwise 1 or 0 to indicate | |
928 | * that the device will or will not have to be unlocked. (0 can be | |
929 | * returned when an interface is given and is BINDING, because in that | |
930 | * case the driver already owns the device lock.) | |
931 | */ | |
932 | int usb_lock_device_for_reset(struct usb_device *udev, | |
933 | struct usb_interface *iface) | |
934 | { | |
3ea15966 AS |
935 | unsigned long jiffies_expire = jiffies + HZ; |
936 | ||
1da177e4 LT |
937 | if (udev->state == USB_STATE_NOTATTACHED) |
938 | return -ENODEV; | |
939 | if (udev->state == USB_STATE_SUSPENDED) | |
940 | return -EHOSTUNREACH; | |
941 | if (iface) { | |
942 | switch (iface->condition) { | |
943 | case USB_INTERFACE_BINDING: | |
944 | return 0; | |
945 | case USB_INTERFACE_BOUND: | |
946 | break; | |
947 | default: | |
948 | return -EINTR; | |
949 | } | |
950 | } | |
951 | ||
952 | while (!usb_trylock_device(udev)) { | |
3ea15966 AS |
953 | |
954 | /* If we can't acquire the lock after waiting one second, | |
955 | * we're probably deadlocked */ | |
956 | if (time_after(jiffies, jiffies_expire)) | |
957 | return -EBUSY; | |
958 | ||
1da177e4 LT |
959 | msleep(15); |
960 | if (udev->state == USB_STATE_NOTATTACHED) | |
961 | return -ENODEV; | |
962 | if (udev->state == USB_STATE_SUSPENDED) | |
963 | return -EHOSTUNREACH; | |
964 | if (iface && iface->condition != USB_INTERFACE_BOUND) | |
965 | return -EINTR; | |
966 | } | |
967 | return 1; | |
968 | } | |
969 | ||
970 | /** | |
971 | * usb_unlock_device - release the lock for a usb device structure | |
972 | * @udev: device that's being unlocked | |
973 | * | |
974 | * Use this routine when releasing the only device lock you hold; | |
975 | * to release inner nested locks call up(&udev->serialize) directly. | |
976 | * This is necessary for proper interaction with usb_lock_all_devices(). | |
977 | */ | |
978 | void usb_unlock_device(struct usb_device *udev) | |
979 | { | |
980 | up(&udev->serialize); | |
981 | up_read(&usb_all_devices_rwsem); | |
982 | } | |
983 | ||
984 | /** | |
985 | * usb_lock_all_devices - acquire the lock for all usb device structures | |
986 | * | |
987 | * This is necessary when registering a new driver or probing a bus, | |
988 | * since the driver-model core may try to use any usb_device. | |
989 | */ | |
990 | void usb_lock_all_devices(void) | |
991 | { | |
992 | down_write(&usb_all_devices_rwsem); | |
993 | } | |
994 | ||
995 | /** | |
996 | * usb_unlock_all_devices - release the lock for all usb device structures | |
997 | */ | |
998 | void usb_unlock_all_devices(void) | |
999 | { | |
1000 | up_write(&usb_all_devices_rwsem); | |
1001 | } | |
1002 | ||
1003 | ||
1004 | static struct usb_device *match_device(struct usb_device *dev, | |
1005 | u16 vendor_id, u16 product_id) | |
1006 | { | |
1007 | struct usb_device *ret_dev = NULL; | |
1008 | int child; | |
1009 | ||
1010 | dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n", | |
1011 | le16_to_cpu(dev->descriptor.idVendor), | |
1012 | le16_to_cpu(dev->descriptor.idProduct)); | |
1013 | ||
1014 | /* see if this device matches */ | |
1015 | if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) && | |
1016 | (product_id == le16_to_cpu(dev->descriptor.idProduct))) { | |
1017 | dev_dbg (&dev->dev, "matched this device!\n"); | |
1018 | ret_dev = usb_get_dev(dev); | |
1019 | goto exit; | |
1020 | } | |
1021 | ||
1022 | /* look through all of the children of this device */ | |
1023 | for (child = 0; child < dev->maxchild; ++child) { | |
1024 | if (dev->children[child]) { | |
1025 | down(&dev->children[child]->serialize); | |
1026 | ret_dev = match_device(dev->children[child], | |
1027 | vendor_id, product_id); | |
1028 | up(&dev->children[child]->serialize); | |
1029 | if (ret_dev) | |
1030 | goto exit; | |
1031 | } | |
1032 | } | |
1033 | exit: | |
1034 | return ret_dev; | |
1035 | } | |
1036 | ||
1037 | /** | |
1038 | * usb_find_device - find a specific usb device in the system | |
1039 | * @vendor_id: the vendor id of the device to find | |
1040 | * @product_id: the product id of the device to find | |
1041 | * | |
1042 | * Returns a pointer to a struct usb_device if such a specified usb | |
1043 | * device is present in the system currently. The usage count of the | |
1044 | * device will be incremented if a device is found. Make sure to call | |
1045 | * usb_put_dev() when the caller is finished with the device. | |
1046 | * | |
1047 | * If a device with the specified vendor and product id is not found, | |
1048 | * NULL is returned. | |
1049 | */ | |
1050 | struct usb_device *usb_find_device(u16 vendor_id, u16 product_id) | |
1051 | { | |
1052 | struct list_head *buslist; | |
1053 | struct usb_bus *bus; | |
1054 | struct usb_device *dev = NULL; | |
1055 | ||
1056 | down(&usb_bus_list_lock); | |
1057 | for (buslist = usb_bus_list.next; | |
1058 | buslist != &usb_bus_list; | |
1059 | buslist = buslist->next) { | |
1060 | bus = container_of(buslist, struct usb_bus, bus_list); | |
1061 | if (!bus->root_hub) | |
1062 | continue; | |
1063 | usb_lock_device(bus->root_hub); | |
1064 | dev = match_device(bus->root_hub, vendor_id, product_id); | |
1065 | usb_unlock_device(bus->root_hub); | |
1066 | if (dev) | |
1067 | goto exit; | |
1068 | } | |
1069 | exit: | |
1070 | up(&usb_bus_list_lock); | |
1071 | return dev; | |
1072 | } | |
1073 | ||
1074 | /** | |
1075 | * usb_get_current_frame_number - return current bus frame number | |
1076 | * @dev: the device whose bus is being queried | |
1077 | * | |
1078 | * Returns the current frame number for the USB host controller | |
1079 | * used with the given USB device. This can be used when scheduling | |
1080 | * isochronous requests. | |
1081 | * | |
1082 | * Note that different kinds of host controller have different | |
1083 | * "scheduling horizons". While one type might support scheduling only | |
1084 | * 32 frames into the future, others could support scheduling up to | |
1085 | * 1024 frames into the future. | |
1086 | */ | |
1087 | int usb_get_current_frame_number(struct usb_device *dev) | |
1088 | { | |
1089 | return dev->bus->op->get_frame_number (dev); | |
1090 | } | |
1091 | ||
1092 | /*-------------------------------------------------------------------*/ | |
1093 | /* | |
1094 | * __usb_get_extra_descriptor() finds a descriptor of specific type in the | |
1095 | * extra field of the interface and endpoint descriptor structs. | |
1096 | */ | |
1097 | ||
1098 | int __usb_get_extra_descriptor(char *buffer, unsigned size, | |
1099 | unsigned char type, void **ptr) | |
1100 | { | |
1101 | struct usb_descriptor_header *header; | |
1102 | ||
1103 | while (size >= sizeof(struct usb_descriptor_header)) { | |
1104 | header = (struct usb_descriptor_header *)buffer; | |
1105 | ||
1106 | if (header->bLength < 2) { | |
1107 | printk(KERN_ERR | |
1108 | "%s: bogus descriptor, type %d length %d\n", | |
1109 | usbcore_name, | |
1110 | header->bDescriptorType, | |
1111 | header->bLength); | |
1112 | return -1; | |
1113 | } | |
1114 | ||
1115 | if (header->bDescriptorType == type) { | |
1116 | *ptr = header; | |
1117 | return 0; | |
1118 | } | |
1119 | ||
1120 | buffer += header->bLength; | |
1121 | size -= header->bLength; | |
1122 | } | |
1123 | return -1; | |
1124 | } | |
1125 | ||
1126 | /** | |
1127 | * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP | |
1128 | * @dev: device the buffer will be used with | |
1129 | * @size: requested buffer size | |
1130 | * @mem_flags: affect whether allocation may block | |
1131 | * @dma: used to return DMA address of buffer | |
1132 | * | |
1133 | * Return value is either null (indicating no buffer could be allocated), or | |
1134 | * the cpu-space pointer to a buffer that may be used to perform DMA to the | |
1135 | * specified device. Such cpu-space buffers are returned along with the DMA | |
1136 | * address (through the pointer provided). | |
1137 | * | |
1138 | * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags | |
1139 | * to avoid behaviors like using "DMA bounce buffers", or tying down I/O | |
1140 | * mapping hardware for long idle periods. The implementation varies between | |
1141 | * platforms, depending on details of how DMA will work to this device. | |
1142 | * Using these buffers also helps prevent cacheline sharing problems on | |
1143 | * architectures where CPU caches are not DMA-coherent. | |
1144 | * | |
1145 | * When the buffer is no longer used, free it with usb_buffer_free(). | |
1146 | */ | |
1147 | void *usb_buffer_alloc ( | |
1148 | struct usb_device *dev, | |
1149 | size_t size, | |
5db539e4 | 1150 | unsigned mem_flags, |
1da177e4 LT |
1151 | dma_addr_t *dma |
1152 | ) | |
1153 | { | |
1154 | if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc) | |
1155 | return NULL; | |
1156 | return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma); | |
1157 | } | |
1158 | ||
1159 | /** | |
1160 | * usb_buffer_free - free memory allocated with usb_buffer_alloc() | |
1161 | * @dev: device the buffer was used with | |
1162 | * @size: requested buffer size | |
1163 | * @addr: CPU address of buffer | |
1164 | * @dma: DMA address of buffer | |
1165 | * | |
1166 | * This reclaims an I/O buffer, letting it be reused. The memory must have | |
1167 | * been allocated using usb_buffer_alloc(), and the parameters must match | |
1168 | * those provided in that allocation request. | |
1169 | */ | |
1170 | void usb_buffer_free ( | |
1171 | struct usb_device *dev, | |
1172 | size_t size, | |
1173 | void *addr, | |
1174 | dma_addr_t dma | |
1175 | ) | |
1176 | { | |
1177 | if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free) | |
1178 | return; | |
1179 | dev->bus->op->buffer_free (dev->bus, size, addr, dma); | |
1180 | } | |
1181 | ||
1182 | /** | |
1183 | * usb_buffer_map - create DMA mapping(s) for an urb | |
1184 | * @urb: urb whose transfer_buffer/setup_packet will be mapped | |
1185 | * | |
1186 | * Return value is either null (indicating no buffer could be mapped), or | |
1187 | * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are | |
1188 | * added to urb->transfer_flags if the operation succeeds. If the device | |
1189 | * is connected to this system through a non-DMA controller, this operation | |
1190 | * always succeeds. | |
1191 | * | |
1192 | * This call would normally be used for an urb which is reused, perhaps | |
1193 | * as the target of a large periodic transfer, with usb_buffer_dmasync() | |
1194 | * calls to synchronize memory and dma state. | |
1195 | * | |
1196 | * Reverse the effect of this call with usb_buffer_unmap(). | |
1197 | */ | |
1198 | #if 0 | |
1199 | struct urb *usb_buffer_map (struct urb *urb) | |
1200 | { | |
1201 | struct usb_bus *bus; | |
1202 | struct device *controller; | |
1203 | ||
1204 | if (!urb | |
1205 | || !urb->dev | |
1206 | || !(bus = urb->dev->bus) | |
1207 | || !(controller = bus->controller)) | |
1208 | return NULL; | |
1209 | ||
1210 | if (controller->dma_mask) { | |
1211 | urb->transfer_dma = dma_map_single (controller, | |
1212 | urb->transfer_buffer, urb->transfer_buffer_length, | |
1213 | usb_pipein (urb->pipe) | |
1214 | ? DMA_FROM_DEVICE : DMA_TO_DEVICE); | |
1215 | if (usb_pipecontrol (urb->pipe)) | |
1216 | urb->setup_dma = dma_map_single (controller, | |
1217 | urb->setup_packet, | |
1218 | sizeof (struct usb_ctrlrequest), | |
1219 | DMA_TO_DEVICE); | |
1220 | // FIXME generic api broken like pci, can't report errors | |
1221 | // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0; | |
1222 | } else | |
1223 | urb->transfer_dma = ~0; | |
1224 | urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | |
1225 | | URB_NO_SETUP_DMA_MAP); | |
1226 | return urb; | |
1227 | } | |
1228 | #endif /* 0 */ | |
1229 | ||
1230 | /* XXX DISABLED, no users currently. If you wish to re-enable this | |
1231 | * XXX please determine whether the sync is to transfer ownership of | |
1232 | * XXX the buffer from device to cpu or vice verse, and thusly use the | |
1233 | * XXX appropriate _for_{cpu,device}() method. -DaveM | |
1234 | */ | |
1235 | #if 0 | |
1236 | ||
1237 | /** | |
1238 | * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s) | |
1239 | * @urb: urb whose transfer_buffer/setup_packet will be synchronized | |
1240 | */ | |
1241 | void usb_buffer_dmasync (struct urb *urb) | |
1242 | { | |
1243 | struct usb_bus *bus; | |
1244 | struct device *controller; | |
1245 | ||
1246 | if (!urb | |
1247 | || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) | |
1248 | || !urb->dev | |
1249 | || !(bus = urb->dev->bus) | |
1250 | || !(controller = bus->controller)) | |
1251 | return; | |
1252 | ||
1253 | if (controller->dma_mask) { | |
1254 | dma_sync_single (controller, | |
1255 | urb->transfer_dma, urb->transfer_buffer_length, | |
1256 | usb_pipein (urb->pipe) | |
1257 | ? DMA_FROM_DEVICE : DMA_TO_DEVICE); | |
1258 | if (usb_pipecontrol (urb->pipe)) | |
1259 | dma_sync_single (controller, | |
1260 | urb->setup_dma, | |
1261 | sizeof (struct usb_ctrlrequest), | |
1262 | DMA_TO_DEVICE); | |
1263 | } | |
1264 | } | |
1265 | #endif | |
1266 | ||
1267 | /** | |
1268 | * usb_buffer_unmap - free DMA mapping(s) for an urb | |
1269 | * @urb: urb whose transfer_buffer will be unmapped | |
1270 | * | |
1271 | * Reverses the effect of usb_buffer_map(). | |
1272 | */ | |
1273 | #if 0 | |
1274 | void usb_buffer_unmap (struct urb *urb) | |
1275 | { | |
1276 | struct usb_bus *bus; | |
1277 | struct device *controller; | |
1278 | ||
1279 | if (!urb | |
1280 | || !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP) | |
1281 | || !urb->dev | |
1282 | || !(bus = urb->dev->bus) | |
1283 | || !(controller = bus->controller)) | |
1284 | return; | |
1285 | ||
1286 | if (controller->dma_mask) { | |
1287 | dma_unmap_single (controller, | |
1288 | urb->transfer_dma, urb->transfer_buffer_length, | |
1289 | usb_pipein (urb->pipe) | |
1290 | ? DMA_FROM_DEVICE : DMA_TO_DEVICE); | |
1291 | if (usb_pipecontrol (urb->pipe)) | |
1292 | dma_unmap_single (controller, | |
1293 | urb->setup_dma, | |
1294 | sizeof (struct usb_ctrlrequest), | |
1295 | DMA_TO_DEVICE); | |
1296 | } | |
1297 | urb->transfer_flags &= ~(URB_NO_TRANSFER_DMA_MAP | |
1298 | | URB_NO_SETUP_DMA_MAP); | |
1299 | } | |
1300 | #endif /* 0 */ | |
1301 | ||
1302 | /** | |
1303 | * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint | |
1304 | * @dev: device to which the scatterlist will be mapped | |
1305 | * @pipe: endpoint defining the mapping direction | |
1306 | * @sg: the scatterlist to map | |
1307 | * @nents: the number of entries in the scatterlist | |
1308 | * | |
1309 | * Return value is either < 0 (indicating no buffers could be mapped), or | |
1310 | * the number of DMA mapping array entries in the scatterlist. | |
1311 | * | |
1312 | * The caller is responsible for placing the resulting DMA addresses from | |
1313 | * the scatterlist into URB transfer buffer pointers, and for setting the | |
1314 | * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs. | |
1315 | * | |
1316 | * Top I/O rates come from queuing URBs, instead of waiting for each one | |
1317 | * to complete before starting the next I/O. This is particularly easy | |
1318 | * to do with scatterlists. Just allocate and submit one URB for each DMA | |
1319 | * mapping entry returned, stopping on the first error or when all succeed. | |
1320 | * Better yet, use the usb_sg_*() calls, which do that (and more) for you. | |
1321 | * | |
1322 | * This call would normally be used when translating scatterlist requests, | |
1323 | * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it | |
1324 | * may be able to coalesce mappings for improved I/O efficiency. | |
1325 | * | |
1326 | * Reverse the effect of this call with usb_buffer_unmap_sg(). | |
1327 | */ | |
1328 | int usb_buffer_map_sg (struct usb_device *dev, unsigned pipe, | |
1329 | struct scatterlist *sg, int nents) | |
1330 | { | |
1331 | struct usb_bus *bus; | |
1332 | struct device *controller; | |
1333 | ||
1334 | if (!dev | |
1335 | || usb_pipecontrol (pipe) | |
1336 | || !(bus = dev->bus) | |
1337 | || !(controller = bus->controller) | |
1338 | || !controller->dma_mask) | |
1339 | return -1; | |
1340 | ||
1341 | // FIXME generic api broken like pci, can't report errors | |
1342 | return dma_map_sg (controller, sg, nents, | |
1343 | usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); | |
1344 | } | |
1345 | ||
1346 | /* XXX DISABLED, no users currently. If you wish to re-enable this | |
1347 | * XXX please determine whether the sync is to transfer ownership of | |
1348 | * XXX the buffer from device to cpu or vice verse, and thusly use the | |
1349 | * XXX appropriate _for_{cpu,device}() method. -DaveM | |
1350 | */ | |
1351 | #if 0 | |
1352 | ||
1353 | /** | |
1354 | * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s) | |
1355 | * @dev: device to which the scatterlist will be mapped | |
1356 | * @pipe: endpoint defining the mapping direction | |
1357 | * @sg: the scatterlist to synchronize | |
1358 | * @n_hw_ents: the positive return value from usb_buffer_map_sg | |
1359 | * | |
1360 | * Use this when you are re-using a scatterlist's data buffers for | |
1361 | * another USB request. | |
1362 | */ | |
1363 | void usb_buffer_dmasync_sg (struct usb_device *dev, unsigned pipe, | |
1364 | struct scatterlist *sg, int n_hw_ents) | |
1365 | { | |
1366 | struct usb_bus *bus; | |
1367 | struct device *controller; | |
1368 | ||
1369 | if (!dev | |
1370 | || !(bus = dev->bus) | |
1371 | || !(controller = bus->controller) | |
1372 | || !controller->dma_mask) | |
1373 | return; | |
1374 | ||
1375 | dma_sync_sg (controller, sg, n_hw_ents, | |
1376 | usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); | |
1377 | } | |
1378 | #endif | |
1379 | ||
1380 | /** | |
1381 | * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist | |
1382 | * @dev: device to which the scatterlist will be mapped | |
1383 | * @pipe: endpoint defining the mapping direction | |
1384 | * @sg: the scatterlist to unmap | |
1385 | * @n_hw_ents: the positive return value from usb_buffer_map_sg | |
1386 | * | |
1387 | * Reverses the effect of usb_buffer_map_sg(). | |
1388 | */ | |
1389 | void usb_buffer_unmap_sg (struct usb_device *dev, unsigned pipe, | |
1390 | struct scatterlist *sg, int n_hw_ents) | |
1391 | { | |
1392 | struct usb_bus *bus; | |
1393 | struct device *controller; | |
1394 | ||
1395 | if (!dev | |
1396 | || !(bus = dev->bus) | |
1397 | || !(controller = bus->controller) | |
1398 | || !controller->dma_mask) | |
1399 | return; | |
1400 | ||
1401 | dma_unmap_sg (controller, sg, n_hw_ents, | |
1402 | usb_pipein (pipe) ? DMA_FROM_DEVICE : DMA_TO_DEVICE); | |
1403 | } | |
1404 | ||
27d72e85 | 1405 | static int usb_generic_suspend(struct device *dev, pm_message_t message) |
1da177e4 LT |
1406 | { |
1407 | struct usb_interface *intf; | |
1408 | struct usb_driver *driver; | |
1409 | ||
1410 | if (dev->driver == &usb_generic_driver) | |
27d72e85 | 1411 | return usb_suspend_device (to_usb_device(dev), message); |
1da177e4 LT |
1412 | |
1413 | if ((dev->driver == NULL) || | |
1414 | (dev->driver_data == &usb_generic_driver_data)) | |
1415 | return 0; | |
1416 | ||
1417 | intf = to_usb_interface(dev); | |
1418 | driver = to_usb_driver(dev->driver); | |
1419 | ||
1420 | /* there's only one USB suspend state */ | |
ca078bae | 1421 | if (intf->dev.power.power_state.event) |
1da177e4 LT |
1422 | return 0; |
1423 | ||
1424 | if (driver->suspend) | |
27d72e85 | 1425 | return driver->suspend(intf, message); |
1da177e4 LT |
1426 | return 0; |
1427 | } | |
1428 | ||
1429 | static int usb_generic_resume(struct device *dev) | |
1430 | { | |
1431 | struct usb_interface *intf; | |
1432 | struct usb_driver *driver; | |
1433 | ||
1434 | /* devices resume through their hub */ | |
1435 | if (dev->driver == &usb_generic_driver) | |
1436 | return usb_resume_device (to_usb_device(dev)); | |
1437 | ||
1438 | if ((dev->driver == NULL) || | |
1439 | (dev->driver_data == &usb_generic_driver_data)) | |
1440 | return 0; | |
1441 | ||
1442 | intf = to_usb_interface(dev); | |
1443 | driver = to_usb_driver(dev->driver); | |
1444 | ||
1445 | if (driver->resume) | |
1446 | return driver->resume(intf); | |
1447 | return 0; | |
1448 | } | |
1449 | ||
1450 | struct bus_type usb_bus_type = { | |
1451 | .name = "usb", | |
1452 | .match = usb_device_match, | |
1453 | .hotplug = usb_hotplug, | |
1454 | .suspend = usb_generic_suspend, | |
1455 | .resume = usb_generic_resume, | |
1456 | }; | |
1457 | ||
1458 | #ifndef MODULE | |
1459 | ||
1460 | static int __init usb_setup_disable(char *str) | |
1461 | { | |
1462 | nousb = 1; | |
1463 | return 1; | |
1464 | } | |
1465 | ||
1466 | /* format to disable USB on kernel command line is: nousb */ | |
1467 | __setup("nousb", usb_setup_disable); | |
1468 | ||
1469 | #endif | |
1470 | ||
1471 | /* | |
1472 | * for external read access to <nousb> | |
1473 | */ | |
1474 | int usb_disabled(void) | |
1475 | { | |
1476 | return nousb; | |
1477 | } | |
1478 | ||
1479 | /* | |
1480 | * Init | |
1481 | */ | |
1482 | static int __init usb_init(void) | |
1483 | { | |
1484 | int retval; | |
1485 | if (nousb) { | |
1486 | pr_info ("%s: USB support disabled\n", usbcore_name); | |
1487 | return 0; | |
1488 | } | |
1489 | ||
1490 | retval = bus_register(&usb_bus_type); | |
1491 | if (retval) | |
1492 | goto out; | |
1493 | retval = usb_host_init(); | |
1494 | if (retval) | |
1495 | goto host_init_failed; | |
1496 | retval = usb_major_init(); | |
1497 | if (retval) | |
1498 | goto major_init_failed; | |
fbf82fd2 KS |
1499 | retval = usb_register(&usbfs_driver); |
1500 | if (retval) | |
1501 | goto driver_register_failed; | |
1502 | retval = usbdev_init(); | |
1503 | if (retval) | |
1504 | goto usbdevice_init_failed; | |
1da177e4 LT |
1505 | retval = usbfs_init(); |
1506 | if (retval) | |
1507 | goto fs_init_failed; | |
1508 | retval = usb_hub_init(); | |
1509 | if (retval) | |
1510 | goto hub_init_failed; | |
1da177e4 LT |
1511 | retval = driver_register(&usb_generic_driver); |
1512 | if (!retval) | |
1513 | goto out; | |
1514 | ||
1515 | usb_hub_cleanup(); | |
1516 | hub_init_failed: | |
1517 | usbfs_cleanup(); | |
1518 | fs_init_failed: | |
fbf82fd2 KS |
1519 | usbdev_cleanup(); |
1520 | usbdevice_init_failed: | |
1521 | usb_deregister(&usbfs_driver); | |
1522 | driver_register_failed: | |
1523 | usb_major_cleanup(); | |
1da177e4 LT |
1524 | major_init_failed: |
1525 | usb_host_cleanup(); | |
1526 | host_init_failed: | |
1527 | bus_unregister(&usb_bus_type); | |
1528 | out: | |
1529 | return retval; | |
1530 | } | |
1531 | ||
1532 | /* | |
1533 | * Cleanup | |
1534 | */ | |
1535 | static void __exit usb_exit(void) | |
1536 | { | |
1537 | /* This will matter if shutdown/reboot does exitcalls. */ | |
1538 | if (nousb) | |
1539 | return; | |
1540 | ||
1541 | driver_unregister(&usb_generic_driver); | |
1542 | usb_major_cleanup(); | |
1543 | usbfs_cleanup(); | |
fbf82fd2 KS |
1544 | usb_deregister(&usbfs_driver); |
1545 | usbdev_cleanup(); | |
1da177e4 LT |
1546 | usb_hub_cleanup(); |
1547 | usb_host_cleanup(); | |
1548 | bus_unregister(&usb_bus_type); | |
1549 | } | |
1550 | ||
1551 | subsys_initcall(usb_init); | |
1552 | module_exit(usb_exit); | |
1553 | ||
1554 | /* | |
1555 | * USB may be built into the kernel or be built as modules. | |
1556 | * These symbols are exported for device (or host controller) | |
1557 | * driver modules to use. | |
1558 | */ | |
1559 | ||
1560 | EXPORT_SYMBOL(usb_register); | |
1561 | EXPORT_SYMBOL(usb_deregister); | |
1562 | EXPORT_SYMBOL(usb_disabled); | |
1563 | ||
a3fdf4eb | 1564 | EXPORT_SYMBOL_GPL(usb_get_intf); |
1565 | EXPORT_SYMBOL_GPL(usb_put_intf); | |
1566 | ||
1da177e4 LT |
1567 | EXPORT_SYMBOL(usb_alloc_dev); |
1568 | EXPORT_SYMBOL(usb_put_dev); | |
1569 | EXPORT_SYMBOL(usb_get_dev); | |
1570 | EXPORT_SYMBOL(usb_hub_tt_clear_buffer); | |
1571 | ||
1572 | EXPORT_SYMBOL(usb_lock_device); | |
1573 | EXPORT_SYMBOL(usb_trylock_device); | |
1574 | EXPORT_SYMBOL(usb_lock_device_for_reset); | |
1575 | EXPORT_SYMBOL(usb_unlock_device); | |
1576 | ||
1577 | EXPORT_SYMBOL(usb_driver_claim_interface); | |
1578 | EXPORT_SYMBOL(usb_driver_release_interface); | |
1579 | EXPORT_SYMBOL(usb_match_id); | |
1580 | EXPORT_SYMBOL(usb_find_interface); | |
1581 | EXPORT_SYMBOL(usb_ifnum_to_if); | |
1582 | EXPORT_SYMBOL(usb_altnum_to_altsetting); | |
1583 | ||
1584 | EXPORT_SYMBOL(usb_reset_device); | |
1585 | EXPORT_SYMBOL(usb_disconnect); | |
1586 | ||
1587 | EXPORT_SYMBOL(__usb_get_extra_descriptor); | |
1588 | ||
1589 | EXPORT_SYMBOL(usb_find_device); | |
1590 | EXPORT_SYMBOL(usb_get_current_frame_number); | |
1591 | ||
1592 | EXPORT_SYMBOL (usb_buffer_alloc); | |
1593 | EXPORT_SYMBOL (usb_buffer_free); | |
1594 | ||
1595 | #if 0 | |
1596 | EXPORT_SYMBOL (usb_buffer_map); | |
1597 | EXPORT_SYMBOL (usb_buffer_dmasync); | |
1598 | EXPORT_SYMBOL (usb_buffer_unmap); | |
1599 | #endif | |
1600 | ||
1601 | EXPORT_SYMBOL (usb_buffer_map_sg); | |
1602 | #if 0 | |
1603 | EXPORT_SYMBOL (usb_buffer_dmasync_sg); | |
1604 | #endif | |
1605 | EXPORT_SYMBOL (usb_buffer_unmap_sg); | |
1606 | ||
1607 | MODULE_LICENSE("GPL"); |