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
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..
19 * Think of this as a "USB library" rather than anything else.
20 * It should be considered a slave, with no callbacks. Callbacks
24 #include <linux/config.h>
26 #ifdef CONFIG_USB_DEBUG
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/usb.h>
45 #include <asm/scatterlist.h>
47 #include <linux/dma-mapping.h>
51 #include <asm/arch-moxacpu/gpio.h>
53 extern int usb_hub_init(void);
54 extern void usb_hub_cleanup(void);
55 extern int usb_major_init(void);
56 extern void usb_major_cleanup(void);
57 extern int usb_host_init(void);
58 extern void usb_host_cleanup(void);
61 const char *usbcore_name
= "usbcore";
63 int nousb
; /* Disable USB when built into kernel image */
64 /* Not honored on modular build */
67 static int generic_probe (struct device
*dev
)
71 static int generic_remove (struct device
*dev
)
76 static struct device_driver usb_generic_driver
= {
79 .probe
= generic_probe
,
80 .remove
= generic_remove
,
83 static int usb_generic_driver_data
;
85 /* called from driver core with usb_bus_type.subsys writelock */
86 int usb_probe_interface(struct device
*dev
)
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
;
93 dev_dbg(dev
, "%s\n", __FUNCTION__
);
97 if (interface_to_usbdev(intf
)->state
== USB_STATE_SUSPENDED
)
100 id
= usb_match_id (intf
, driver
->id_table
);
102 dev_dbg (dev
, "%s - got id\n", __FUNCTION__
);
103 error
= driver
->probe (intf
, id
);
109 /* called from driver core with usb_bus_type.subsys writelock */
110 int usb_unbind_interface(struct device
*dev
)
112 struct usb_interface
*intf
= to_usb_interface(dev
);
113 struct usb_driver
*driver
= to_usb_driver(intf
->dev
.driver
);
115 /* release all urbs for this interface */
116 usb_disable_interface(interface_to_usbdev(intf
), intf
);
118 if (driver
&& driver
->disconnect
)
119 driver
->disconnect(intf
);
121 /* reset other interface state */
122 usb_set_interface(interface_to_usbdev(intf
),
123 intf
->altsetting
[0].desc
.bInterfaceNumber
,
125 usb_set_intfdata(intf
, NULL
);
131 * usb_register - register a USB driver
132 * @new_driver: USB operations for the driver
134 * Registers a USB driver with the USB core. The list of unattached
135 * interfaces will be rescanned whenever a new driver is added, allowing
136 * the new driver to attach to any recognized devices.
137 * Returns a negative error code on failure and 0 on success.
139 * NOTE: if you want your driver to use the USB major number, you must call
140 * usb_register_dev() to enable that functionality. This function no longer
141 * takes care of that.
143 int usb_register(struct usb_driver
*new_driver
)
150 new_driver
->driver
.name
= (char *)new_driver
->name
;
151 new_driver
->driver
.bus
= &usb_bus_type
;
152 new_driver
->driver
.probe
= usb_probe_interface
;
153 new_driver
->driver
.remove
= usb_unbind_interface
;
155 retval
= driver_register(&new_driver
->driver
);
158 pr_info("%s: registered new driver %s\n",
159 usbcore_name
, new_driver
->name
);
160 usbfs_update_special();
162 printk(KERN_ERR
"%s: error %d registering driver %s\n",
163 usbcore_name
, retval
, new_driver
->name
);
170 * usb_deregister - unregister a USB driver
171 * @driver: USB operations of the driver to unregister
172 * Context: must be able to sleep
174 * Unlinks the specified driver from the internal USB driver list.
176 * NOTE: If you called usb_register_dev(), you still need to call
177 * usb_deregister_dev() to clean up your driver's allocated minor numbers,
178 * this * call will no longer do it for you.
180 void usb_deregister(struct usb_driver
*driver
)
182 pr_info("%s: deregistering driver %s\n", usbcore_name
, driver
->name
);
184 driver_unregister (&driver
->driver
);
186 usbfs_update_special();
190 * usb_ifnum_to_if - get the interface object with a given interface number
191 * @dev: the device whose current configuration is considered
192 * @ifnum: the desired interface
194 * This walks the device descriptor for the currently active configuration
195 * and returns a pointer to the interface with that particular interface
198 * Note that configuration descriptors are not required to assign interface
199 * numbers sequentially, so that it would be incorrect to assume that
200 * the first interface in that descriptor corresponds to interface zero.
201 * This routine helps device drivers avoid such mistakes.
202 * However, you should make sure that you do the right thing with any
203 * alternate settings available for this interfaces.
205 * Don't call this function unless you are bound to one of the interfaces
206 * on this device or you own the dev->serialize semaphore!
208 struct usb_interface
*usb_ifnum_to_if(struct usb_device
*dev
, unsigned ifnum
)
210 struct usb_host_config
*config
= dev
->actconfig
;
215 for (i
= 0; i
< config
->desc
.bNumInterfaces
; i
++)
216 if (config
->interface
[i
]->altsetting
[0]
217 .desc
.bInterfaceNumber
== ifnum
)
218 return config
->interface
[i
];
224 * usb_altnum_to_altsetting - get the altsetting structure with a given
225 * alternate setting number.
226 * @intf: the interface containing the altsetting in question
227 * @altnum: the desired alternate setting number
229 * This searches the altsetting array of the specified interface for
230 * an entry with the correct bAlternateSetting value and returns a pointer
231 * to that entry, or null.
233 * Note that altsettings need not be stored sequentially by number, so
234 * it would be incorrect to assume that the first altsetting entry in
235 * the array corresponds to altsetting zero. This routine helps device
236 * drivers avoid such mistakes.
238 * Don't call this function unless you are bound to the intf interface
239 * or you own the device's ->serialize semaphore!
241 struct usb_host_interface
*usb_altnum_to_altsetting(struct usb_interface
*intf
,
246 for (i
= 0; i
< intf
->num_altsetting
; i
++) {
247 if (intf
->altsetting
[i
].desc
.bAlternateSetting
== altnum
)
248 return &intf
->altsetting
[i
];
254 * usb_epnum_to_ep_desc - get the endpoint object with a given endpoint number
255 * @dev: the device whose current configuration+altsettings is considered
256 * @epnum: the desired endpoint, masked with USB_DIR_IN as appropriate.
258 * This walks the device descriptor for the currently active configuration,
259 * and returns a pointer to the endpoint with that particular endpoint
262 * Note that interface descriptors are not required to list endpoint
263 * numbers in any standardized order, so that it would be wrong to
264 * assume that ep2in precedes either ep5in, ep2out, or even ep1out.
265 * This routine helps device drivers avoid such mistakes.
267 struct usb_endpoint_descriptor
*
268 usb_epnum_to_ep_desc(struct usb_device
*dev
, unsigned epnum
)
270 struct usb_host_config
*config
= dev
->actconfig
;
275 for (i
= 0; i
< config
->desc
.bNumInterfaces
; i
++) {
276 struct usb_interface
*intf
;
277 struct usb_host_interface
*alt
;
279 /* only endpoints in current altsetting are active */
280 intf
= config
->interface
[i
];
281 alt
= intf
->cur_altsetting
;
283 for (k
= 0; k
< alt
->desc
.bNumEndpoints
; k
++)
284 if (epnum
== alt
->endpoint
[k
].desc
.bEndpointAddress
)
285 return &alt
->endpoint
[k
].desc
;
292 * usb_driver_claim_interface - bind a driver to an interface
293 * @driver: the driver to be bound
294 * @iface: the interface to which it will be bound; must be in the
295 * usb device's active configuration
296 * @priv: driver data associated with that interface
298 * This is used by usb device drivers that need to claim more than one
299 * interface on a device when probing (audio and acm are current examples).
300 * No device driver should directly modify internal usb_interface or
301 * usb_device structure members.
303 * Few drivers should need to use this routine, since the most natural
304 * way to bind to an interface is to return the private data from
305 * the driver's probe() method.
307 * Callers must own the driver model's usb bus writelock. So driver
308 * probe() entries don't need extra locking, but other call contexts
309 * may need to explicitly claim that lock.
311 int usb_driver_claim_interface(struct usb_driver
*driver
, struct usb_interface
*iface
, void* priv
)
313 struct device
*dev
= &iface
->dev
;
318 dev
->driver
= &driver
->driver
;
319 usb_set_intfdata(iface
, priv
);
321 /* if interface was already added, bind now; else let
322 * the future device_add() bind it, bypassing probe()
324 if (!list_empty (&dev
->bus_list
))
325 device_bind_driver(dev
);
331 * usb_driver_release_interface - unbind a driver from an interface
332 * @driver: the driver to be unbound
333 * @iface: the interface from which it will be unbound
335 * This can be used by drivers to release an interface without waiting
336 * for their disconnect() methods to be called. In typical cases this
337 * also causes the driver disconnect() method to be called.
339 * This call is synchronous, and may not be used in an interrupt context.
340 * Callers must own the usb_device serialize semaphore and the driver model's
341 * usb bus writelock. So driver disconnect() entries don't need extra locking,
342 * but other call contexts may need to explicitly claim those locks.
344 void usb_driver_release_interface(struct usb_driver
*driver
,
345 struct usb_interface
*iface
)
347 struct device
*dev
= &iface
->dev
;
349 /* this should never happen, don't release something that's not ours */
350 if (!dev
->driver
|| dev
->driver
!= &driver
->driver
)
353 /* don't disconnect from disconnect(), or before dev_add() */
354 if (!list_empty (&dev
->driver_list
) && !list_empty (&dev
->bus_list
))
355 device_release_driver(dev
);
358 usb_set_intfdata(iface
, NULL
);
362 * usb_match_id - find first usb_device_id matching device or interface
363 * @interface: the interface of interest
364 * @id: array of usb_device_id structures, terminated by zero entry
366 * usb_match_id searches an array of usb_device_id's and returns
367 * the first one matching the device or interface, or null.
368 * This is used when binding (or rebinding) a driver to an interface.
369 * Most USB device drivers will use this indirectly, through the usb core,
370 * but some layered driver frameworks use it directly.
371 * These device tables are exported with MODULE_DEVICE_TABLE, through
372 * modutils and "modules.usbmap", to support the driver loading
373 * functionality of USB hotplugging.
377 * The "match_flags" element in a usb_device_id controls which
378 * members are used. If the corresponding bit is set, the
379 * value in the device_id must match its corresponding member
380 * in the device or interface descriptor, or else the device_id
383 * "driver_info" is normally used only by device drivers,
384 * but you can create a wildcard "matches anything" usb_device_id
385 * as a driver's "modules.usbmap" entry if you provide an id with
386 * only a nonzero "driver_info" field. If you do this, the USB device
387 * driver's probe() routine should use additional intelligence to
388 * decide whether to bind to the specified interface.
390 * What Makes Good usb_device_id Tables:
392 * The match algorithm is very simple, so that intelligence in
393 * driver selection must come from smart driver id records.
394 * Unless you have good reasons to use another selection policy,
395 * provide match elements only in related groups, and order match
396 * specifiers from specific to general. Use the macros provided
397 * for that purpose if you can.
399 * The most specific match specifiers use device descriptor
400 * data. These are commonly used with product-specific matches;
401 * the USB_DEVICE macro lets you provide vendor and product IDs,
402 * and you can also match against ranges of product revisions.
403 * These are widely used for devices with application or vendor
404 * specific bDeviceClass values.
406 * Matches based on device class/subclass/protocol specifications
407 * are slightly more general; use the USB_DEVICE_INFO macro, or
408 * its siblings. These are used with single-function devices
409 * where bDeviceClass doesn't specify that each interface has
412 * Matches based on interface class/subclass/protocol are the
413 * most general; they let drivers bind to any interface on a
414 * multiple-function device. Use the USB_INTERFACE_INFO
415 * macro, or its siblings, to match class-per-interface style
416 * devices (as recorded in bDeviceClass).
418 * Within those groups, remember that not all combinations are
419 * meaningful. For example, don't give a product version range
420 * without vendor and product IDs; or specify a protocol without
421 * its associated class and subclass.
423 const struct usb_device_id
*
424 usb_match_id(struct usb_interface
*interface
, const struct usb_device_id
*id
)
426 struct usb_host_interface
*intf
;
427 struct usb_device
*dev
;
429 /* proc_connectinfo in devio.c may call us with id == NULL. */
433 intf
= interface
->cur_altsetting
;
434 dev
= interface_to_usbdev(interface
);
436 /* It is important to check that id->driver_info is nonzero,
437 since an entry that is all zeroes except for a nonzero
438 id->driver_info is the way to create an entry that
439 indicates that the driver want to examine every
440 device and interface. */
441 for (; id
->idVendor
|| id
->bDeviceClass
|| id
->bInterfaceClass
||
442 id
->driver_info
; id
++) {
444 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_VENDOR
) &&
445 id
->idVendor
!= dev
->descriptor
.idVendor
)
448 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_PRODUCT
) &&
449 id
->idProduct
!= dev
->descriptor
.idProduct
)
452 /* No need to test id->bcdDevice_lo != 0, since 0 is never
453 greater than any unsigned number. */
454 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
) &&
455 (id
->bcdDevice_lo
> dev
->descriptor
.bcdDevice
))
458 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
) &&
459 (id
->bcdDevice_hi
< dev
->descriptor
.bcdDevice
))
462 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_CLASS
) &&
463 (id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
))
466 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_SUBCLASS
) &&
467 (id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
))
470 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_DEV_PROTOCOL
) &&
471 (id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
))
474 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_CLASS
) &&
475 (id
->bInterfaceClass
!= intf
->desc
.bInterfaceClass
))
478 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_SUBCLASS
) &&
479 (id
->bInterfaceSubClass
!= intf
->desc
.bInterfaceSubClass
))
482 if ((id
->match_flags
& USB_DEVICE_ID_MATCH_INT_PROTOCOL
) &&
483 (id
->bInterfaceProtocol
!= intf
->desc
.bInterfaceProtocol
))
493 * usb_find_interface - find usb_interface pointer for driver and device
494 * @drv: the driver whose current configuration is considered
495 * @minor: the minor number of the desired device
497 * This walks the driver device list and returns a pointer to the interface
498 * with the matching minor. Note, this only works for devices that share the
501 struct usb_interface
*usb_find_interface(struct usb_driver
*drv
, int minor
)
503 struct list_head
*entry
;
505 struct usb_interface
*intf
;
507 list_for_each(entry
, &drv
->driver
.devices
) {
508 dev
= container_of(entry
, struct device
, driver_list
);
510 /* can't look at usb devices, only interfaces */
511 if (dev
->driver
== &usb_generic_driver
)
514 intf
= to_usb_interface(dev
);
515 if (intf
->minor
== -1)
517 if (intf
->minor
== minor
)
521 /* no device found that matches */
525 static int usb_device_match (struct device
*dev
, struct device_driver
*drv
)
527 struct usb_interface
*intf
;
528 struct usb_driver
*usb_drv
;
529 const struct usb_device_id
*id
;
531 /* check for generic driver, which we don't match any device with */
532 if (drv
== &usb_generic_driver
)
535 intf
= to_usb_interface(dev
);
537 usb_drv
= to_usb_driver(drv
);
538 id
= usb_drv
->id_table
;
540 id
= usb_match_id (intf
, usb_drv
->id_table
);
548 #ifdef CONFIG_HOTPLUG
551 * USB hotplugging invokes what /proc/sys/kernel/hotplug says
552 * (normally /sbin/hotplug) when USB devices get added or removed.
554 * This invokes a user mode policy agent, typically helping to load driver
555 * or other modules, configure the device, and more. Drivers can provide
556 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
558 * We're called either from khubd (the typical case) or from root hub
559 * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle
560 * delays in event delivery. Use sysfs (and DEVPATH) to make sure the
561 * device (and this configuration!) are still present.
563 static int usb_hotplug (struct device
*dev
, char **envp
, int num_envp
,
564 char *buffer
, int buffer_size
)
566 struct usb_interface
*intf
;
567 struct usb_device
*usb_dev
;
575 /* driver is often null here; dev_dbg() would oops */
576 pr_debug ("usb %s: hotplug\n", dev
->bus_id
);
578 /* Must check driver_data here, as on remove driver is always NULL */
579 if ((dev
->driver
== &usb_generic_driver
) ||
580 (dev
->driver_data
== &usb_generic_driver_data
))
583 intf
= to_usb_interface(dev
);
584 usb_dev
= interface_to_usbdev (intf
);
586 if (usb_dev
->devnum
< 0) {
587 pr_debug ("usb %s: already deleted?\n", dev
->bus_id
);
591 pr_debug ("usb %s: bus removed?\n", dev
->bus_id
);
597 #ifdef CONFIG_USB_DEVICEFS
598 /* If this is available, userspace programs can directly read
599 * all the device descriptors we don't tell them about. Or
600 * even act as usermode drivers.
602 * FIXME reduce hardwired intelligence here
604 envp
[i
++] = scratch
;
605 length
+= snprintf (scratch
, buffer_size
- length
,
606 "DEVICE=/proc/bus/usb/%03d/%03d",
607 usb_dev
->bus
->busnum
, usb_dev
->devnum
);
608 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
614 /* per-device configurations are common */
615 envp
[i
++] = scratch
;
616 length
+= snprintf (scratch
, buffer_size
- length
, "PRODUCT=%x/%x/%x",
617 usb_dev
->descriptor
.idVendor
,
618 usb_dev
->descriptor
.idProduct
,
619 usb_dev
->descriptor
.bcdDevice
);
620 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
625 /* class-based driver binding models */
626 envp
[i
++] = scratch
;
627 length
+= snprintf (scratch
, buffer_size
- length
, "TYPE=%d/%d/%d",
628 usb_dev
->descriptor
.bDeviceClass
,
629 usb_dev
->descriptor
.bDeviceSubClass
,
630 usb_dev
->descriptor
.bDeviceProtocol
);
631 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
636 if (usb_dev
->descriptor
.bDeviceClass
== 0) {
637 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
639 /* 2.4 only exposed interface zero. in 2.5, hotplug
640 * agents are called for all interfaces, and can use
641 * $DEVPATH/bInterfaceNumber if necessary.
643 envp
[i
++] = scratch
;
644 length
+= snprintf (scratch
, buffer_size
- length
,
645 "INTERFACE=%d/%d/%d",
646 alt
->desc
.bInterfaceClass
,
647 alt
->desc
.bInterfaceSubClass
,
648 alt
->desc
.bInterfaceProtocol
);
649 if ((buffer_size
- length
<= 0) || (i
>= num_envp
))
662 static int usb_hotplug (struct device
*dev
, char **envp
,
663 int num_envp
, char *buffer
, int buffer_size
)
668 #endif /* CONFIG_HOTPLUG */
671 * usb_release_dev - free a usb device structure when all users of it are finished.
672 * @dev: device that's been disconnected
674 * Will be called only by the device core when all users of this usb device are
677 static void usb_release_dev(struct device
*dev
)
679 struct usb_device
*udev
;
681 udev
= to_usb_device(dev
);
683 if (udev
->bus
&& udev
->bus
->op
&& udev
->bus
->op
->deallocate
)
684 udev
->bus
->op
->deallocate(udev
);
685 usb_destroy_configuration(udev
);
686 usb_bus_put(udev
->bus
);
691 * usb_alloc_dev - usb device constructor (usbcore-internal)
692 * @parent: hub to which device is connected; null to allocate a root hub
693 * @bus: bus used to access the device
694 * @port: zero based index of port; ignored for root hubs
695 * Context: !in_interrupt ()
697 * Only hub drivers (including virtual root hub drivers for host
698 * controllers) should ever call this.
700 * This call may not be used in a non-sleeping context.
703 usb_alloc_dev(struct usb_device
*parent
, struct usb_bus
*bus
, unsigned port
)
705 struct usb_device
*dev
;
707 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
711 memset(dev
, 0, sizeof(*dev
));
713 bus
= usb_bus_get(bus
);
719 device_initialize(&dev
->dev
);
720 dev
->dev
.bus
= &usb_bus_type
;
721 dev
->dev
.dma_mask
= bus
->controller
->dma_mask
;
722 dev
->dev
.driver_data
= &usb_generic_driver_data
;
723 dev
->dev
.driver
= &usb_generic_driver
;
724 dev
->dev
.release
= usb_release_dev
;
725 dev
->state
= USB_STATE_ATTACHED
;
727 /* Save readable and stable topology id, distinguishing devices
728 * by location for diagnostics, tools, driver model, etc. The
729 * string is a path along hub ports, from the root. Each device's
730 * dev->devpath will be stable until USB is re-cabled, and hubs
731 * are often labeled with these port numbers. The bus_id isn't
732 * as stable: bus->busnum changes easily from modprobe order,
733 * cardbus or pci hotplugging, and so on.
735 if (unlikely (!parent
)) {
736 dev
->devpath
[0] = '0';
738 dev
->dev
.parent
= bus
->controller
;
739 sprintf (&dev
->dev
.bus_id
[0], "usb%d", bus
->busnum
);
741 /* match any labeling on the hubs; it's one-based */
742 if (parent
->devpath
[0] == '0')
743 snprintf (dev
->devpath
, sizeof dev
->devpath
,
746 snprintf (dev
->devpath
, sizeof dev
->devpath
,
747 "%s.%d", parent
->devpath
, port
+ 1);
749 dev
->dev
.parent
= &parent
->dev
;
750 sprintf (&dev
->dev
.bus_id
[0], "%d-%s",
751 bus
->busnum
, dev
->devpath
);
753 /* hub driver sets up TT records */
757 dev
->parent
= parent
;
758 INIT_LIST_HEAD(&dev
->filelist
);
760 init_MUTEX(&dev
->serialize
);
762 if (dev
->bus
->op
->allocate
)
763 dev
->bus
->op
->allocate(dev
);
769 * usb_get_dev - increments the reference count of the usb device structure
770 * @dev: the device being referenced
772 * Each live reference to a device should be refcounted.
774 * Drivers for USB interfaces should normally record such references in
775 * their probe() methods, when they bind to an interface, and release
776 * them by calling usb_put_dev(), in their disconnect() methods.
778 * A pointer to the device with the incremented reference counter is returned.
780 struct usb_device
*usb_get_dev(struct usb_device
*dev
)
783 get_device(&dev
->dev
);
788 * usb_put_dev - release a use of the usb device structure
789 * @dev: device that's been disconnected
791 * Must be called when a user of a device is finished with it. When the last
792 * user of the device calls this function, the memory of the device is freed.
794 void usb_put_dev(struct usb_device
*dev
)
797 put_device(&dev
->dev
);
801 * usb_get_intf - increments the reference count of the usb interface structure
802 * @intf: the interface being referenced
804 * Each live reference to a interface must be refcounted.
806 * Drivers for USB interfaces should normally record such references in
807 * their probe() methods, when they bind to an interface, and release
808 * them by calling usb_put_intf(), in their disconnect() methods.
810 * A pointer to the interface with the incremented reference counter is
813 struct usb_interface
*usb_get_intf(struct usb_interface
*intf
)
816 get_device(&intf
->dev
);
821 * usb_put_intf - release a use of the usb interface structure
822 * @intf: interface that's been decremented
824 * Must be called when a user of an interface is finished with it. When the
825 * last user of the interface calls this function, the memory of the interface
828 void usb_put_intf(struct usb_interface
*intf
)
831 put_device(&intf
->dev
);
834 static struct usb_device
*match_device(struct usb_device
*dev
,
835 u16 vendor_id
, u16 product_id
)
837 struct usb_device
*ret_dev
= NULL
;
840 dev_dbg(&dev
->dev
, "check for vendor %04x, product %04x ...\n",
841 dev
->descriptor
.idVendor
,
842 dev
->descriptor
.idProduct
);
844 /* see if this device matches */
845 if ((dev
->descriptor
.idVendor
== vendor_id
) &&
846 (dev
->descriptor
.idProduct
== product_id
)) {
847 dev_dbg (&dev
->dev
, "matched this device!\n");
848 ret_dev
= usb_get_dev(dev
);
852 /* look through all of the children of this device */
853 for (child
= 0; child
< dev
->maxchild
; ++child
) {
854 if (dev
->children
[child
]) {
855 ret_dev
= match_device(dev
->children
[child
],
856 vendor_id
, product_id
);
866 * usb_find_device - find a specific usb device in the system
867 * @vendor_id: the vendor id of the device to find
868 * @product_id: the product id of the device to find
870 * Returns a pointer to a struct usb_device if such a specified usb
871 * device is present in the system currently. The usage count of the
872 * device will be incremented if a device is found. Make sure to call
873 * usb_put_dev() when the caller is finished with the device.
875 * If a device with the specified vendor and product id is not found,
878 struct usb_device
*usb_find_device(u16 vendor_id
, u16 product_id
)
880 struct list_head
*buslist
;
882 struct usb_device
*dev
= NULL
;
884 down(&usb_bus_list_lock
);
885 for (buslist
= usb_bus_list
.next
;
886 buslist
!= &usb_bus_list
;
887 buslist
= buslist
->next
) {
888 bus
= container_of(buslist
, struct usb_bus
, bus_list
);
891 dev
= match_device(bus
->root_hub
, vendor_id
, product_id
);
896 up(&usb_bus_list_lock
);
901 * usb_get_current_frame_number - return current bus frame number
902 * @dev: the device whose bus is being queried
904 * Returns the current frame number for the USB host controller
905 * used with the given USB device. This can be used when scheduling
906 * isochronous requests.
908 * Note that different kinds of host controller have different
909 * "scheduling horizons". While one type might support scheduling only
910 * 32 frames into the future, others could support scheduling up to
911 * 1024 frames into the future.
913 int usb_get_current_frame_number(struct usb_device
*dev
)
915 return dev
->bus
->op
->get_frame_number (dev
);
918 /*-------------------------------------------------------------------*/
920 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
921 * extra field of the interface and endpoint descriptor structs.
924 int __usb_get_extra_descriptor(char *buffer
, unsigned size
,
925 unsigned char type
, void **ptr
)
927 struct usb_descriptor_header
*header
;
929 while (size
>= sizeof(struct usb_descriptor_header
)) {
930 header
= (struct usb_descriptor_header
*)buffer
;
932 if (header
->bLength
< 2) {
934 "%s: bogus descriptor, type %d length %d\n",
936 header
->bDescriptorType
,
941 if (header
->bDescriptorType
== type
) {
946 buffer
+= header
->bLength
;
947 size
-= header
->bLength
;
953 * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP
954 * @dev: device the buffer will be used with
955 * @size: requested buffer size
956 * @mem_flags: affect whether allocation may block
957 * @dma: used to return DMA address of buffer
959 * Return value is either null (indicating no buffer could be allocated), or
960 * the cpu-space pointer to a buffer that may be used to perform DMA to the
961 * specified device. Such cpu-space buffers are returned along with the DMA
962 * address (through the pointer provided).
964 * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags
965 * to avoid behaviors like using "DMA bounce buffers", or tying down I/O
966 * mapping hardware for long idle periods. The implementation varies between
967 * platforms, depending on details of how DMA will work to this device.
968 * Using these buffers also helps prevent cacheline sharing problems on
969 * architectures where CPU caches are not DMA-coherent.
971 * When the buffer is no longer used, free it with usb_buffer_free().
973 void *usb_buffer_alloc (
974 struct usb_device
*dev
,
980 if (!dev
|| !dev
->bus
|| !dev
->bus
->op
|| !dev
->bus
->op
->buffer_alloc
)
982 return dev
->bus
->op
->buffer_alloc (dev
->bus
, size
, mem_flags
, dma
);
986 * usb_buffer_free - free memory allocated with usb_buffer_alloc()
987 * @dev: device the buffer was used with
988 * @size: requested buffer size
989 * @addr: CPU address of buffer
990 * @dma: DMA address of buffer
992 * This reclaims an I/O buffer, letting it be reused. The memory must have
993 * been allocated using usb_buffer_alloc(), and the parameters must match
994 * those provided in that allocation request.
996 void usb_buffer_free (
997 struct usb_device
*dev
,
1003 if (!dev
|| !dev
->bus
|| !dev
->bus
->op
|| !dev
->bus
->op
->buffer_free
)
1005 dev
->bus
->op
->buffer_free (dev
->bus
, size
, addr
, dma
);
1009 * usb_buffer_map - create DMA mapping(s) for an urb
1010 * @urb: urb whose transfer_buffer/setup_packet will be mapped
1012 * Return value is either null (indicating no buffer could be mapped), or
1013 * the parameter. URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are
1014 * added to urb->transfer_flags if the operation succeeds. If the device
1015 * is connected to this system through a non-DMA controller, this operation
1018 * This call would normally be used for an urb which is reused, perhaps
1019 * as the target of a large periodic transfer, with usb_buffer_dmasync()
1020 * calls to synchronize memory and dma state.
1022 * Reverse the effect of this call with usb_buffer_unmap().
1024 struct urb
*usb_buffer_map (struct urb
*urb
)
1026 struct usb_bus
*bus
;
1027 struct device
*controller
;
1031 || !(bus
= urb
->dev
->bus
)
1032 || !(controller
= bus
->controller
))
1035 if (controller
->dma_mask
) {
1036 urb
->transfer_dma
= dma_map_single (controller
,
1037 urb
->transfer_buffer
, urb
->transfer_buffer_length
,
1038 usb_pipein (urb
->pipe
)
1039 ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
1040 if (usb_pipecontrol (urb
->pipe
))
1041 urb
->setup_dma
= dma_map_single (controller
,
1043 sizeof (struct usb_ctrlrequest
),
1045 // FIXME generic api broken like pci, can't report errors
1046 // if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;
1048 urb
->transfer_dma
= ~0;
1049 urb
->transfer_flags
|= (URB_NO_TRANSFER_DMA_MAP
1050 | URB_NO_SETUP_DMA_MAP
);
1054 /* XXX DISABLED, no users currently. If you wish to re-enable this
1055 * XXX please determine whether the sync is to transfer ownership of
1056 * XXX the buffer from device to cpu or vice verse, and thusly use the
1057 * XXX appropriate _for_{cpu,device}() method. -DaveM
1062 * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s)
1063 * @urb: urb whose transfer_buffer/setup_packet will be synchronized
1065 void usb_buffer_dmasync (struct urb
*urb
)
1067 struct usb_bus
*bus
;
1068 struct device
*controller
;
1071 || !(urb
->transfer_flags
& URB_NO_TRANSFER_DMA_MAP
)
1073 || !(bus
= urb
->dev
->bus
)
1074 || !(controller
= bus
->controller
))
1077 if (controller
->dma_mask
) {
1078 dma_sync_single (controller
,
1079 urb
->transfer_dma
, urb
->transfer_buffer_length
,
1080 usb_pipein (urb
->pipe
)
1081 ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
1082 if (usb_pipecontrol (urb
->pipe
))
1083 dma_sync_single (controller
,
1085 sizeof (struct usb_ctrlrequest
),
1092 * usb_buffer_unmap - free DMA mapping(s) for an urb
1093 * @urb: urb whose transfer_buffer will be unmapped
1095 * Reverses the effect of usb_buffer_map().
1097 void usb_buffer_unmap (struct urb
*urb
)
1099 struct usb_bus
*bus
;
1100 struct device
*controller
;
1103 || !(urb
->transfer_flags
& URB_NO_TRANSFER_DMA_MAP
)
1105 || !(bus
= urb
->dev
->bus
)
1106 || !(controller
= bus
->controller
))
1109 if (controller
->dma_mask
) {
1110 dma_unmap_single (controller
,
1111 urb
->transfer_dma
, urb
->transfer_buffer_length
,
1112 usb_pipein (urb
->pipe
)
1113 ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
1114 if (usb_pipecontrol (urb
->pipe
))
1115 dma_unmap_single (controller
,
1117 sizeof (struct usb_ctrlrequest
),
1120 urb
->transfer_flags
&= ~(URB_NO_TRANSFER_DMA_MAP
1121 | URB_NO_SETUP_DMA_MAP
);
1125 * usb_buffer_map_sg - create scatterlist DMA mapping(s) for an endpoint
1126 * @dev: device to which the scatterlist will be mapped
1127 * @pipe: endpoint defining the mapping direction
1128 * @sg: the scatterlist to map
1129 * @nents: the number of entries in the scatterlist
1131 * Return value is either < 0 (indicating no buffers could be mapped), or
1132 * the number of DMA mapping array entries in the scatterlist.
1134 * The caller is responsible for placing the resulting DMA addresses from
1135 * the scatterlist into URB transfer buffer pointers, and for setting the
1136 * URB_NO_TRANSFER_DMA_MAP transfer flag in each of those URBs.
1138 * Top I/O rates come from queuing URBs, instead of waiting for each one
1139 * to complete before starting the next I/O. This is particularly easy
1140 * to do with scatterlists. Just allocate and submit one URB for each DMA
1141 * mapping entry returned, stopping on the first error or when all succeed.
1142 * Better yet, use the usb_sg_*() calls, which do that (and more) for you.
1144 * This call would normally be used when translating scatterlist requests,
1145 * rather than usb_buffer_map(), since on some hardware (with IOMMUs) it
1146 * may be able to coalesce mappings for improved I/O efficiency.
1148 * Reverse the effect of this call with usb_buffer_unmap_sg().
1150 int usb_buffer_map_sg (struct usb_device
*dev
, unsigned pipe
,
1151 struct scatterlist
*sg
, int nents
)
1153 struct usb_bus
*bus
;
1154 struct device
*controller
;
1157 || usb_pipecontrol (pipe
)
1158 || !(bus
= dev
->bus
)
1159 || !(controller
= bus
->controller
)
1160 || !controller
->dma_mask
)
1163 // FIXME generic api broken like pci, can't report errors
1164 return dma_map_sg (controller
, sg
, nents
,
1165 usb_pipein (pipe
) ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
1168 /* XXX DISABLED, no users currently. If you wish to re-enable this
1169 * XXX please determine whether the sync is to transfer ownership of
1170 * XXX the buffer from device to cpu or vice verse, and thusly use the
1171 * XXX appropriate _for_{cpu,device}() method. -DaveM
1176 * usb_buffer_dmasync_sg - synchronize DMA and CPU view of scatterlist buffer(s)
1177 * @dev: device to which the scatterlist will be mapped
1178 * @pipe: endpoint defining the mapping direction
1179 * @sg: the scatterlist to synchronize
1180 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1182 * Use this when you are re-using a scatterlist's data buffers for
1183 * another USB request.
1185 void usb_buffer_dmasync_sg (struct usb_device
*dev
, unsigned pipe
,
1186 struct scatterlist
*sg
, int n_hw_ents
)
1188 struct usb_bus
*bus
;
1189 struct device
*controller
;
1192 || !(bus
= dev
->bus
)
1193 || !(controller
= bus
->controller
)
1194 || !controller
->dma_mask
)
1197 dma_sync_sg (controller
, sg
, n_hw_ents
,
1198 usb_pipein (pipe
) ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
1203 * usb_buffer_unmap_sg - free DMA mapping(s) for a scatterlist
1204 * @dev: device to which the scatterlist will be mapped
1205 * @pipe: endpoint defining the mapping direction
1206 * @sg: the scatterlist to unmap
1207 * @n_hw_ents: the positive return value from usb_buffer_map_sg
1209 * Reverses the effect of usb_buffer_map_sg().
1211 void usb_buffer_unmap_sg (struct usb_device
*dev
, unsigned pipe
,
1212 struct scatterlist
*sg
, int n_hw_ents
)
1214 struct usb_bus
*bus
;
1215 struct device
*controller
;
1218 || !(bus
= dev
->bus
)
1219 || !(controller
= bus
->controller
)
1220 || !controller
->dma_mask
)
1223 dma_unmap_sg (controller
, sg
, n_hw_ents
,
1224 usb_pipein (pipe
) ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
);
1227 static int usb_generic_suspend(struct device
*dev
, u32 state
)
1229 struct usb_interface
*intf
;
1230 struct usb_driver
*driver
;
1232 if (dev
->driver
== &usb_generic_driver
)
1233 return usb_suspend_device (to_usb_device(dev
), state
);
1235 if ((dev
->driver
== NULL
) ||
1236 (dev
->driver_data
== &usb_generic_driver_data
))
1239 intf
= to_usb_interface(dev
);
1240 driver
= to_usb_driver(dev
->driver
);
1242 if (driver
->suspend
)
1243 return driver
->suspend(intf
, state
);
1247 static int usb_generic_resume(struct device
*dev
)
1249 struct usb_interface
*intf
;
1250 struct usb_driver
*driver
;
1252 /* devices resume through their hub */
1253 if (dev
->driver
== &usb_generic_driver
)
1254 return usb_resume_device (to_usb_device(dev
));
1256 if ((dev
->driver
== NULL
) ||
1257 (dev
->driver_data
== &usb_generic_driver_data
))
1260 intf
= to_usb_interface(dev
);
1261 driver
= to_usb_driver(dev
->driver
);
1264 return driver
->resume(intf
);
1268 struct bus_type usb_bus_type
= {
1270 .match
= usb_device_match
,
1271 .hotplug
= usb_hotplug
,
1272 .suspend
= usb_generic_suspend
,
1273 .resume
= usb_generic_resume
,
1278 static int __init
usb_setup_disable(char *str
)
1284 /* format to disable USB on kernel command line is: nousb */
1285 __setup("nousb", usb_setup_disable
);
1290 * for external read access to <nousb>
1292 int usb_disabled(void)
1297 //#if defined(CONFIG_ARCH_W311)
1298 //#define USB_CTRL_GPIO (1<28)
1299 #if defined(CONFIG_ARCH_W321) || defined(CONFIG_ARCH_W311)
1300 #define USB_CTRL_GPIO (1<31)
1301 #elif defined(CONFIG_ARCH_W341)
1302 #define USB_CTRL_GPIO (1<18)
1308 static int __init
usb_init(void)
1312 pr_info ("%s: USB support disabled\n", usbcore_name
);
1316 /*johnson add for fix reboot problem of USB(needed that sure GPIO pin is avaliable)
1317 #if defined(CONFIG_ARCH_W311) || defined(CONFIG_ARCH_W321) || defined(CONFIG_ARCH_W341)
1318 mcpu_gpio_inout(USB_CTRL_GPIO,MCPU_GPIO_OUTPUT);
1319 mcpu_gpio_set(USB_CTRL_GPIO,MCPU_GPIO_LOW);
1321 mcpu_gpio_set(USB_CTRL_GPIO,MCPU_GPIO_HIGH);
1325 retval
= bus_register(&usb_bus_type
);
1328 retval
= usb_host_init();
1330 goto host_init_failed
;
1331 retval
= usb_major_init();
1333 goto major_init_failed
;
1334 retval
= usbfs_init();
1336 goto fs_init_failed
;
1337 retval
= usb_hub_init();
1339 goto hub_init_failed
;
1341 retval
= driver_register(&usb_generic_driver
);
1349 usb_major_cleanup();
1353 bus_unregister(&usb_bus_type
);
1361 static void __exit
usb_exit(void)
1363 /* This will matter if shutdown/reboot does exitcalls. */
1367 driver_unregister(&usb_generic_driver
);
1368 usb_major_cleanup();
1372 bus_unregister(&usb_bus_type
);
1375 subsys_initcall(usb_init
);
1376 module_exit(usb_exit
);
1379 * USB may be built into the kernel or be built as modules.
1380 * These symbols are exported for device (or host controller)
1381 * driver modules to use.
1383 EXPORT_SYMBOL(usb_epnum_to_ep_desc
);
1385 EXPORT_SYMBOL(usb_register
);
1386 EXPORT_SYMBOL(usb_deregister
);
1387 EXPORT_SYMBOL(usb_disabled
);
1389 EXPORT_SYMBOL(usb_alloc_dev
);
1390 EXPORT_SYMBOL(usb_put_dev
);
1391 EXPORT_SYMBOL(usb_get_dev
);
1392 EXPORT_SYMBOL(usb_hub_tt_clear_buffer
);
1394 EXPORT_SYMBOL(usb_driver_claim_interface
);
1395 EXPORT_SYMBOL(usb_driver_release_interface
);
1396 EXPORT_SYMBOL(usb_match_id
);
1397 EXPORT_SYMBOL(usb_find_interface
);
1398 EXPORT_SYMBOL(usb_ifnum_to_if
);
1399 EXPORT_SYMBOL(usb_altnum_to_altsetting
);
1401 EXPORT_SYMBOL(usb_reset_device
);
1402 EXPORT_SYMBOL(usb_disconnect
);
1404 EXPORT_SYMBOL(__usb_get_extra_descriptor
);
1406 EXPORT_SYMBOL(usb_find_device
);
1407 EXPORT_SYMBOL(usb_get_current_frame_number
);
1409 EXPORT_SYMBOL (usb_buffer_alloc
);
1410 EXPORT_SYMBOL (usb_buffer_free
);
1412 EXPORT_SYMBOL (usb_buffer_map
);
1414 EXPORT_SYMBOL (usb_buffer_dmasync
);
1416 EXPORT_SYMBOL (usb_buffer_unmap
);
1418 EXPORT_SYMBOL (usb_buffer_map_sg
);
1420 EXPORT_SYMBOL (usb_buffer_dmasync_sg
);
1422 EXPORT_SYMBOL (usb_buffer_unmap_sg
);
1424 MODULE_LICENSE("GPL");