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