4 * (C) Copyright Linus Torvalds 1999
5 * (C) Copyright Johannes Erdfelt 1999
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 (kernel hotplug, usb_device_id)
11 * (C) Copyright Yggdrasil Computing, Inc. 2000
12 * (usb_device_id matching changes by Adam J. Richter)
14 * NOTE! This is not actually a driver at all, rather this is
15 * just a collection of helper routines that implement the
16 * generic USB things that the real drivers can use..
18 * Think of this as a "USB library" rather than anything else.
19 * It should be considered a slave, with no callbacks. Callbacks
22 * $Id: usb.c,v 1.53 2000/01/14 16:19:09 acher Exp $
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/string.h>
28 #include <linux/bitops.h>
29 #include <linux/malloc.h>
30 #include <linux/interrupt.h> /* for in_interrupt() */
31 #include <linux/kmod.h>
32 #include <linux/init.h>
33 #include <linux/devfs_fs_kernel.h>
35 #ifdef CONFIG_USB_DEBUG
40 #include <linux/usb.h>
42 #define DEVNUM_ROUND_ROBIN /***** OPTION *****/
43 #ifdef DEVNUM_ROUND_ROBIN
44 static int devnum_next
= 1;
47 static const int usb_bandwidth_option
=
48 #ifdef CONFIG_USB_BANDWIDTH
54 extern int usb_hub_init(void);
55 extern void usb_hub_cleanup(void);
58 * Prototypes for the device driver probing/loading functions
60 static void usb_find_drivers(struct usb_device
*);
61 static int usb_find_interface_driver(struct usb_device
*, unsigned int);
62 static void usb_check_support(struct usb_device
*);
65 * We have a per-interface "registered driver" list.
67 LIST_HEAD(usb_driver_list
);
68 LIST_HEAD(usb_bus_list
);
70 devfs_handle_t usb_devfs_handle
; /* /dev/usb dir. */
72 static struct usb_busmap busmap
;
74 static struct usb_driver
*usb_minors
[16];
77 * usb_register - register a USB driver
78 * @new_driver: USB operations for the driver
80 * Registers a USB driver with the USB core. The list of unattached
81 * interfaces will be rescanned whenever a new driver is added, allowing
82 * the new driver to attach to any recognized devices.
83 * Returns a negative error code on failure and 0 on success.
85 int usb_register(struct usb_driver
*new_driver
)
87 if (new_driver
->fops
!= NULL
) {
88 if (usb_minors
[new_driver
->minor
/16]) {
89 err("error registering %s driver", new_driver
->name
);
92 usb_minors
[new_driver
->minor
/16] = new_driver
;
95 info("registered new driver %s", new_driver
->name
);
97 init_MUTEX(&new_driver
->serialize
);
99 /* Add it to the list of known drivers */
100 list_add_tail(&new_driver
->driver_list
, &usb_driver_list
);
108 * usb_scan_devices - scans all unclaimed USB interfaces
110 * Goes through all unclaimed USB interfaces, and offers them to all
111 * registered USB drivers through the 'probe' function.
112 * This will automatically be called after usb_register is called.
113 * It is called by some of the USB subsystems after one of their subdrivers
116 void usb_scan_devices(void)
118 struct list_head
*tmp
;
120 tmp
= usb_bus_list
.next
;
121 while (tmp
!= &usb_bus_list
) {
122 struct usb_bus
*bus
= list_entry(tmp
,struct usb_bus
, bus_list
);
125 usb_check_support(bus
->root_hub
);
130 * This function is part of a depth-first search down the device tree,
131 * removing any instances of a device driver.
133 static void usb_drivers_purge(struct usb_driver
*driver
,struct usb_device
*dev
)
138 err("null device being purged!!!");
142 for (i
=0; i
<USB_MAXCHILDREN
; i
++)
143 if (dev
->children
[i
])
144 usb_drivers_purge(driver
, dev
->children
[i
]);
149 for (i
= 0; i
< dev
->actconfig
->bNumInterfaces
; i
++) {
150 struct usb_interface
*interface
= &dev
->actconfig
->interface
[i
];
152 if (interface
->driver
== driver
) {
153 down(&driver
->serialize
);
154 driver
->disconnect(dev
, interface
->private_data
);
155 up(&driver
->serialize
);
156 usb_driver_release_interface(driver
, interface
);
158 * This will go through the list looking for another
159 * driver that can handle the device
161 usb_find_interface_driver(dev
, i
);
167 * usb_deregister - unregister a USB driver
168 * @driver: USB operations of the driver to unregister
170 * Unlinks the specified driver from the internal USB driver list.
172 void usb_deregister(struct usb_driver
*driver
)
174 struct list_head
*tmp
;
176 info("deregistering driver %s", driver
->name
);
177 if (driver
->fops
!= NULL
)
178 usb_minors
[driver
->minor
/16] = NULL
;
181 * first we remove the driver, to be sure it doesn't get used by
182 * another thread while we are stepping through removing entries
184 list_del(&driver
->driver_list
);
186 tmp
= usb_bus_list
.next
;
187 while (tmp
!= &usb_bus_list
) {
188 struct usb_bus
*bus
= list_entry(tmp
,struct usb_bus
,bus_list
);
191 usb_drivers_purge(driver
, bus
->root_hub
);
195 struct usb_interface
*usb_ifnum_to_if(struct usb_device
*dev
, unsigned ifnum
)
199 for (i
= 0; i
< dev
->actconfig
->bNumInterfaces
; i
++)
200 if (dev
->actconfig
->interface
[i
].altsetting
[0].bInterfaceNumber
== ifnum
)
201 return &dev
->actconfig
->interface
[i
];
206 struct usb_endpoint_descriptor
*usb_epnum_to_ep_desc(struct usb_device
*dev
, unsigned epnum
)
210 for (i
= 0; i
< dev
->actconfig
->bNumInterfaces
; i
++)
211 for (j
= 0; j
< dev
->actconfig
->interface
[i
].num_altsetting
; j
++)
212 for (k
= 0; k
< dev
->actconfig
->interface
[i
].altsetting
[j
].bNumEndpoints
; k
++)
213 if (epnum
== dev
->actconfig
->interface
[i
].altsetting
[j
].endpoint
[k
].bEndpointAddress
)
214 return &dev
->actconfig
->interface
[i
].altsetting
[j
].endpoint
[k
];
222 * returns (approximate) USB bus time in nanoseconds for a USB transaction.
224 static long usb_calc_bus_time (int low_speed
, int input_dir
, int isoc
, int bytecount
)
228 if (low_speed
) /* no isoc. here */
232 tmp
= (67667L * (31L + 10L * BitTime (bytecount
))) / 1000L;
233 return (64060L + (2 * BW_HUB_LS_SETUP
) + BW_HOST_DELAY
+ tmp
);
237 tmp
= (66700L * (31L + 10L * BitTime (bytecount
))) / 1000L;
238 return (64107L + (2 * BW_HUB_LS_SETUP
) + BW_HOST_DELAY
+ tmp
);
242 /* for full-speed: */
244 if (!isoc
) /* Input or Output */
246 tmp
= (8354L * (31L + 10L * BitTime (bytecount
))) / 1000L;
247 return (9107L + BW_HOST_DELAY
+ tmp
);
252 tmp
= (8354L * (31L + 10L * BitTime (bytecount
))) / 1000L;
253 return (((input_dir
) ? 7268L : 6265L) + BW_HOST_DELAY
+ tmp
);
257 * usb_check_bandwidth():
259 * old_alloc is from host_controller->bandwidth_allocated in microseconds;
260 * bustime is from calc_bus_time(), but converted to microseconds.
262 * returns <bustime in us> if successful,
263 * or USB_ST_BANDWIDTH_ERROR if bandwidth request fails.
266 * This initial implementation does not use Endpoint.bInterval
267 * in managing bandwidth allocation.
268 * It probably needs to be expanded to use Endpoint.bInterval.
269 * This can be done as a later enhancement (correction).
270 * This will also probably require some kind of
271 * frame allocation tracking...meaning, for example,
272 * that if multiple drivers request interrupts every 10 USB frames,
273 * they don't all have to be allocated at
274 * frame numbers N, N+10, N+20, etc. Some of them could be at
275 * N+11, N+21, N+31, etc., and others at
276 * N+12, N+22, N+32, etc.
277 * However, this first cut at USB bandwidth allocation does not
278 * contain any frame allocation tracking.
280 int usb_check_bandwidth (struct usb_device
*dev
, struct urb
*urb
)
283 int old_alloc
= dev
->bus
->bandwidth_allocated
;
284 unsigned int pipe
= urb
->pipe
;
287 bustime
= usb_calc_bus_time (usb_pipeslow(pipe
), usb_pipein(pipe
),
288 usb_pipeisoc(pipe
), usb_maxpacket(dev
, pipe
, usb_pipeout(pipe
)));
289 if (usb_pipeisoc(pipe
))
290 bustime
= NS_TO_US(bustime
) / urb
->number_of_packets
;
292 bustime
= NS_TO_US(bustime
);
294 new_alloc
= old_alloc
+ (int)bustime
;
295 /* what new total allocated bus time would be */
297 if (new_alloc
> FRAME_TIME_MAX_USECS_ALLOC
)
298 dbg("usb-check-bandwidth %sFAILED: was %u, would be %u, bustime = %ld us",
299 usb_bandwidth_option
? "" : "would have ",
300 old_alloc
, new_alloc
, bustime
);
302 if (!usb_bandwidth_option
) /* don't enforce it */
304 return (new_alloc
<= FRAME_TIME_MAX_USECS_ALLOC
) ? bustime
: USB_ST_BANDWIDTH_ERROR
;
307 void usb_claim_bandwidth (struct usb_device
*dev
, struct urb
*urb
, int bustime
, int isoc
)
309 dev
->bus
->bandwidth_allocated
+= bustime
;
311 dev
->bus
->bandwidth_isoc_reqs
++;
313 dev
->bus
->bandwidth_int_reqs
++;
314 urb
->bandwidth
= bustime
;
316 #ifdef USB_BANDWIDTH_MESSAGES
317 dbg("bandwidth alloc increased by %d to %d for %d requesters",
319 dev
->bus
->bandwidth_allocated
,
320 dev
->bus
->bandwidth_int_reqs
+ dev
->bus
->bandwidth_isoc_reqs
);
325 * usb_release_bandwidth():
327 * called to release a pipe's bandwidth (in microseconds)
329 void usb_release_bandwidth(struct usb_device
*dev
, struct urb
*urb
, int isoc
)
331 dev
->bus
->bandwidth_allocated
-= urb
->bandwidth
;
333 dev
->bus
->bandwidth_isoc_reqs
--;
335 dev
->bus
->bandwidth_int_reqs
--;
337 #ifdef USB_BANDWIDTH_MESSAGES
338 dbg("bandwidth alloc reduced by %d to %d for %d requesters",
340 dev
->bus
->bandwidth_allocated
,
341 dev
->bus
->bandwidth_int_reqs
+ dev
->bus
->bandwidth_isoc_reqs
);
347 * usb_alloc_bus - creates a new USB host controller structure
348 * @op: pointer to a struct usb_operations that this bus structure should use
350 * Creates a USB host controller bus structure with the specified
351 * usb_operations and initializes all the necessary internal objects.
353 * If no memory is available, NULL is returned.
355 * The caller should call usb_free_bus() when it is finished with the structure.
357 struct usb_bus
*usb_alloc_bus(struct usb_operations
*op
)
361 bus
= kmalloc(sizeof(*bus
), GFP_KERNEL
);
365 memset(&bus
->devmap
, 0, sizeof(struct usb_devmap
));
368 bus
->root_hub
= NULL
;
371 bus
->bandwidth_allocated
= 0;
372 bus
->bandwidth_int_reqs
= 0;
373 bus
->bandwidth_isoc_reqs
= 0;
375 INIT_LIST_HEAD(&bus
->bus_list
);
376 INIT_LIST_HEAD(&bus
->inodes
);
382 * usb_free_bus - frees the memory used by a bus structure
383 * @bus: pointer to the bus to free
386 void usb_free_bus(struct usb_bus
*bus
)
395 * usb_register_bus - registers the USB host controller with the usb core
396 * @bus: pointer to the bus to register
399 void usb_register_bus(struct usb_bus
*bus
)
403 busnum
= find_next_zero_bit(busmap
.busmap
, USB_MAXBUS
, 1);
404 if (busnum
< USB_MAXBUS
) {
405 set_bit(busnum
, busmap
.busmap
);
406 bus
->busnum
= busnum
;
408 warn("too many buses");
410 /* Add it to the list of buses */
411 list_add(&bus
->bus_list
, &usb_bus_list
);
413 usbdevfs_add_bus(bus
);
415 info("new USB bus registered, assigned bus number %d", bus
->busnum
);
418 void usb_deregister_bus(struct usb_bus
*bus
)
420 info("USB bus %d deregistered", bus
->busnum
);
423 * NOTE: make sure that all the devices are removed by the
424 * controller code, as well as having it call this when cleaning
427 list_del(&bus
->bus_list
);
429 usbdevfs_remove_bus(bus
);
431 clear_bit(bus
->busnum
, busmap
.busmap
);
435 * This function is for doing a depth-first search for devices which
436 * have support, for dynamic loading of driver modules.
438 static void usb_check_support(struct usb_device
*dev
)
443 err("null device being checked!!!");
447 for (i
=0; i
<USB_MAXCHILDREN
; i
++)
448 if (dev
->children
[i
])
449 usb_check_support(dev
->children
[i
]);
454 /* now we check this device */
456 for (i
= 0; i
< dev
->actconfig
->bNumInterfaces
; i
++)
457 usb_find_interface_driver(dev
, i
);
462 * This is intended to be used by usb device drivers that need to
463 * claim more than one interface on a device at once when probing
464 * (audio and acm are good examples). No device driver should have
465 * to mess with the internal usb_interface or usb_device structure
468 void usb_driver_claim_interface(struct usb_driver
*driver
, struct usb_interface
*iface
, void* priv
)
470 if (!iface
|| !driver
)
473 dbg("%s driver claimed interface %p", driver
->name
, iface
);
475 iface
->driver
= driver
;
476 iface
->private_data
= priv
;
477 } /* usb_driver_claim_interface() */
480 * This should be used by drivers to check other interfaces to see if
481 * they are available or not.
483 int usb_interface_claimed(struct usb_interface
*iface
)
488 return (iface
->driver
!= NULL
);
489 } /* usb_interface_claimed() */
492 * This should be used by drivers to release their claimed interfaces
494 void usb_driver_release_interface(struct usb_driver
*driver
, struct usb_interface
*iface
)
496 /* this should never happen, don't release something that's not ours */
497 if (!iface
|| iface
->driver
!= driver
)
500 iface
->driver
= NULL
;
501 iface
->private_data
= NULL
;
505 /* usb_match_id searches an array of usb_device_id's and returns
506 the first one that matches the device and interface.
509 "id" is an array of usb_device_id's is terminated by an entry
510 containing all zeroes.
512 "dev" and "interface" are the device and interface for which
515 If no match is found or if the "id" pointer is NULL, then
516 usb_match_id returns NULL.
519 What constitutes a match:
521 A zero in any element of a usb_device_id entry is a wildcard
522 (i.e., that field always matches). For there to be a match,
523 *every* nonzero element of the usb_device_id must match the
524 provided device and interface in. The comparison is for equality,
525 except for one pair of fields: usb_match_id.bcdDevice_{lo,hi} define
526 an inclusive range that dev->descriptor.bcdDevice must be in.
528 If interface->altsettings does not exist (i.e., there are no
529 interfaces defined), then bInterface{Class,SubClass,Protocol}
530 only match if they are all zeroes.
533 What constitutes a good "usb_device_id"?
535 The match algorithm is very simple, so that intelligence in
536 driver selection must come from smart driver id records.
537 Unless you have good reasons to use another selection policy,
538 provide match elements only in related groups:
540 * device specifiers (vendor and product IDs; and maybe
541 a revision range for that product);
542 * generic device specs (class/subclass/protocol);
543 * interface specs (class/subclass/protocol).
545 Within those groups, work from least specific to most specific.
546 For example, don't give a product version range without vendor
549 "driver_info" is not considered by the kernel matching algorithm,
550 but you can create a wildcard "matches anything" usb_device_id
551 as your driver's "modules.usbmap" entry if you provide only an
552 id with a nonzero "driver_info" field.
555 const struct usb_device_id
*
556 usb_match_id(struct usb_device
*dev
, struct usb_interface
*interface
,
557 const struct usb_device_id
*id
)
559 struct usb_interface_descriptor
*intf
= 0;
561 /* proc_connectinfo in devio.c may call us with id == NULL. */
565 /* It is important to check that id->driver_info is nonzero,
566 since an entry that is all zeroes except for a nonzero
567 id->driver_info is the way to create an entry that
568 indicates that the driver want to examine every
569 device and interface. */
570 for (; id
->idVendor
|| id
->bDeviceClass
|| id
->bInterfaceClass
||
571 id
->driver_info
; id
++) {
574 id
->idVendor
!= dev
->descriptor
.idVendor
)
578 id
->idProduct
!= dev
->descriptor
.idProduct
)
581 /* No need to test id->bcdDevice_lo != 0, since 0 is never
582 greater than any unsigned number. */
583 if (id
->bcdDevice_lo
> dev
->descriptor
.bcdDevice
)
586 if (id
->bcdDevice_hi
&&
587 id
->bcdDevice_hi
< dev
->descriptor
.bcdDevice
)
590 if (id
->bDeviceClass
&&
591 id
->bDeviceClass
!= dev
->descriptor
.bDeviceClass
)
594 if (id
->bDeviceSubClass
&&
595 id
->bDeviceSubClass
!= dev
->descriptor
.bDeviceSubClass
)
598 if (id
->bDeviceProtocol
&&
599 id
->bDeviceProtocol
!= dev
->descriptor
.bDeviceProtocol
)
602 intf
= &interface
->altsetting
[interface
->act_altsetting
];
604 if (id
->bInterfaceClass
605 && id
->bInterfaceClass
!= intf
->bInterfaceClass
)
608 if (id
->bInterfaceSubClass
&&
609 id
->bInterfaceSubClass
!= intf
->bInterfaceSubClass
)
612 if (id
->bInterfaceProtocol
613 && id
->bInterfaceProtocol
!= intf
->bInterfaceProtocol
)
623 * This entrypoint gets called for each new device.
625 * We now walk the list of registered USB drivers,
626 * looking for one that will accept this interface.
628 * "New Style" drivers use a table describing the devices and interfaces
629 * they handle. Those tables are available to user mode tools deciding
630 * whether to load driver modules for a new device.
632 * The probe return value is changed to be a private pointer. This way
633 * the drivers don't have to dig around in our structures to set the
634 * private pointer if they only need one interface.
636 * Returns: 0 if a driver accepted the interface, -1 otherwise
638 static int usb_find_interface_driver(struct usb_device
*dev
, unsigned ifnum
)
640 struct list_head
*tmp
;
641 struct usb_interface
*interface
;
643 const struct usb_device_id
*id
;
644 struct usb_driver
*driver
;
647 if ((!dev
) || (ifnum
>= dev
->actconfig
->bNumInterfaces
)) {
648 err("bad find_interface_driver params");
652 interface
= dev
->actconfig
->interface
+ ifnum
;
654 if (usb_interface_claimed(interface
))
658 for (tmp
= usb_driver_list
.next
; tmp
!= &usb_driver_list
;) {
660 driver
= list_entry(tmp
, struct usb_driver
, driver_list
);
663 down(&driver
->serialize
);
664 id
= driver
->id_table
;
665 /* new style driver? */
667 for (i
= 0; i
< interface
->num_altsetting
; i
++) {
668 interface
->act_altsetting
= i
;
669 id
= usb_match_id(dev
, interface
, id
);
671 private = driver
->probe(dev
,ifnum
,id
);
676 /* if driver not bound, leave defaults unchanged */
678 interface
->act_altsetting
= 0;
680 else /* "old style" driver */
681 private = driver
->probe(dev
, ifnum
, NULL
);
683 up(&driver
->serialize
);
685 usb_driver_claim_interface(driver
, interface
, private);
694 #ifdef CONFIG_HOTPLUG
697 * USB hotplugging invokes what /proc/sys/kernel/hotplug says
698 * (normally /sbin/hotplug) when USB devices get added or removed.
700 * This invokes a user mode policy agent, typically helping to load driver
701 * or other modules, configure the device, and more. Drivers can provide
702 * a MODULE_DEVICE_TABLE to help with module loading subtasks.
704 * Some synchronization is important: removes can't start processing
705 * before the add-device processing completes, and vice versa. That keeps
706 * a stack of USB-related identifiers stable while they're in use. If we
707 * know that agents won't complete after they return (such as by forking
708 * a process that completes later), it's enough to just waitpid() for the
709 * agent -- as is currently done.
711 * The reason: we know we're called either from khubd (the typical case)
712 * or from root hub initialization (init, kapmd, modprobe, etc). In both
713 * cases, we know no other thread can recycle our address, since we must
714 * already have been serialized enough to prevent that.
716 static void call_policy (char *verb
, struct usb_device
*dev
)
718 char *argv
[3], **envp
, *buf
, *scratch
;
721 if (!hotplug_path
[0])
723 if (in_interrupt ()) {
724 dbg ("In_interrupt");
727 if (!current
->fs
->root
) {
728 /* statically linked USB is initted rather early */
729 dbg ("call_policy %s, num %d -- no FS yet", verb
, dev
->devnum
);
732 if (dev
->devnum
< 0) {
733 dbg ("device already deleted ??");
736 if (!(envp
= (char **) kmalloc (20 * sizeof (char *), GFP_KERNEL
))) {
740 if (!(buf
= kmalloc (256, GFP_KERNEL
))) {
746 /* only one standardized param to hotplug command: type */
747 argv
[0] = hotplug_path
;
751 /* minimal command environment */
752 envp
[i
++] = "HOME=/";
753 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
756 /* hint that policy agent should enter no-stdout debug mode */
757 envp
[i
++] = "DEBUG=kernel";
759 /* extensible set of named bus-specific parameters,
760 * supporting multiple driver selection algorithms.
764 /* action: add, remove */
765 envp
[i
++] = scratch
;
766 scratch
+= sprintf (scratch
, "ACTION=%s", verb
) + 1;
768 #ifdef CONFIG_USB_DEVICEFS
769 /* If this is available, userspace programs can directly read
770 * all the device descriptors we don't tell them about. Or
771 * even act as usermode drivers.
773 * FIXME reduce hardwired intelligence here
775 envp
[i
++] = "DEVFS=/proc/bus/usb";
776 envp
[i
++] = scratch
;
777 scratch
+= sprintf (scratch
, "DEVICE=/proc/bus/usb/%03d/%03d",
778 dev
->bus
->busnum
, dev
->devnum
) + 1;
781 /* per-device configuration hacks are common */
782 envp
[i
++] = scratch
;
783 scratch
+= sprintf (scratch
, "PRODUCT=%x/%x/%x",
784 dev
->descriptor
.idVendor
,
785 dev
->descriptor
.idProduct
,
786 dev
->descriptor
.bcdDevice
) + 1;
788 /* class-based driver binding models */
789 envp
[i
++] = scratch
;
790 scratch
+= sprintf (scratch
, "TYPE=%d/%d/%d",
791 dev
->descriptor
.bDeviceClass
,
792 dev
->descriptor
.bDeviceSubClass
,
793 dev
->descriptor
.bDeviceProtocol
) + 1;
794 if (dev
->descriptor
.bDeviceClass
== 0) {
795 int alt
= dev
->actconfig
->interface
[0].act_altsetting
;
797 /* a simple/common case: one config, one interface, one driver
798 * with current altsetting being a reasonable setting.
799 * everything needs a smart agent and usbdevfs; or can rely on
800 * device-specific binding policies.
802 envp
[i
++] = scratch
;
803 scratch
+= sprintf (scratch
, "INTERFACE=%d/%d/%d",
804 dev
->actconfig
->interface
[0].altsetting
[alt
].bInterfaceClass
,
805 dev
->actconfig
->interface
[0].altsetting
[alt
].bInterfaceSubClass
,
806 dev
->actconfig
->interface
[0].altsetting
[alt
].bInterfaceProtocol
)
808 /* INTERFACE-0, INTERFACE-1, ... ? */
811 /* assert: (scratch - buf) < sizeof buf */
813 /* NOTE: user mode daemons can call the agents too */
815 dbg ("kusbd: %s %s %d", argv
[0], verb
, dev
->devnum
);
816 value
= call_usermodehelper (argv
[0], argv
, envp
);
820 dbg ("kusbd policy returned 0x%x", value
);
826 call_policy (char *verb
, struct usb_device
*dev
)
833 * This entrypoint gets called for each new device.
835 * All interfaces are scanned for matching drivers.
837 static void usb_find_drivers(struct usb_device
*dev
)
840 unsigned rejected
= 0;
841 unsigned claimed
= 0;
843 for (ifnum
= 0; ifnum
< dev
->actconfig
->bNumInterfaces
; ifnum
++) {
844 /* if this interface hasn't already been claimed */
845 if (!usb_interface_claimed(dev
->actconfig
->interface
+ ifnum
)) {
846 if (usb_find_interface_driver(dev
, ifnum
))
854 dbg("unhandled interfaces on device");
857 warn("USB device %d (vend/prod 0x%x/0x%x) is not claimed by any active driver.",
859 dev
->descriptor
.idVendor
,
860 dev
->descriptor
.idProduct
);
862 usb_show_device(dev
);
868 * Only HC's should call usb_alloc_dev and usb_free_dev directly
869 * Anybody may use usb_inc_dev_use or usb_dec_dev_use
871 struct usb_device
*usb_alloc_dev(struct usb_device
*parent
, struct usb_bus
*bus
)
873 struct usb_device
*dev
;
875 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
879 memset(dev
, 0, sizeof(*dev
));
882 dev
->parent
= parent
;
883 atomic_set(&dev
->refcnt
, 1);
884 INIT_LIST_HEAD(&dev
->inodes
);
885 INIT_LIST_HEAD(&dev
->filelist
);
887 dev
->bus
->op
->allocate(dev
);
892 void usb_free_dev(struct usb_device
*dev
)
894 if (atomic_dec_and_test(&dev
->refcnt
)) {
895 dev
->bus
->op
->deallocate(dev
);
896 usb_destroy_configuration(dev
);
901 void usb_inc_dev_use(struct usb_device
*dev
)
903 atomic_inc(&dev
->refcnt
);
906 /* -------------------------------------------------------------------------------------
907 * New USB Core Functions
908 * -------------------------------------------------------------------------------------*/
911 * usb_alloc_urb - creates a new urb for a USB driver to use
912 * @iso_packets: number of iso packets for this urb
914 * Creates an urb for the USB driver to use and returns a pointer to it.
915 * If no memory is available, NULL is returned.
917 * If the driver want to use this urb for interrupt, control, or bulk
918 * endpoints, pass '0' as the number of iso packets.
920 * The driver should call usb_free_urb() when it is finished with the urb.
922 urb_t
*usb_alloc_urb(int iso_packets
)
926 urb
= (urb_t
*)kmalloc(sizeof(urb_t
) + iso_packets
* sizeof(iso_packet_descriptor_t
),
927 in_interrupt() ? GFP_ATOMIC
: GFP_KERNEL
);
929 err("alloc_urb: kmalloc failed");
933 memset(urb
, 0, sizeof(*urb
));
935 spin_lock_init(&urb
->lock
);
941 * usb_free_urb - frees the memory used by a urb
942 * @urb: pointer to the urb to free
944 * If an urb is created with a call to usb_create_urb() it should be
945 * cleaned up with a call to usb_free_urb() when the driver is finished
948 void usb_free_urb(urb_t
* urb
)
953 /*-------------------------------------------------------------------*/
954 int usb_submit_urb(urb_t
*urb
)
957 return urb
->dev
->bus
->op
->submit_urb(urb
);
962 /*-------------------------------------------------------------------*/
963 int usb_unlink_urb(urb_t
*urb
)
966 return urb
->dev
->bus
->op
->unlink_urb(urb
);
970 /*-------------------------------------------------------------------*
971 * COMPLETION HANDLERS *
972 *-------------------------------------------------------------------*/
974 /*-------------------------------------------------------------------*
975 * completion handler for compatibility wrappers (sync control/bulk) *
976 *-------------------------------------------------------------------*/
977 static void usb_api_blocking_completion(urb_t
*urb
)
979 api_wrapper_data
*awd
= (api_wrapper_data
*)urb
->context
;
981 if (waitqueue_active(awd
->wakeup
))
982 wake_up(awd
->wakeup
);
985 dbg("(blocking_completion): waitqueue empty!");
986 // even occurs if urb was unlinked by timeout...
990 /*-------------------------------------------------------------------*
991 * COMPATIBILITY STUFF *
992 *-------------------------------------------------------------------*/
994 // Starts urb and waits for completion or timeout
995 static int usb_start_wait_urb(urb_t
*urb
, int timeout
, int* actual_length
)
997 DECLARE_WAITQUEUE(wait
, current
);
998 DECLARE_WAIT_QUEUE_HEAD(wqh
);
999 api_wrapper_data awd
;
1003 init_waitqueue_head(&wqh
);
1004 current
->state
= TASK_INTERRUPTIBLE
;
1005 add_wait_queue(&wqh
, &wait
);
1006 urb
->context
= &awd
;
1007 status
= usb_submit_urb(urb
);
1009 // something went wrong
1011 current
->state
= TASK_RUNNING
;
1012 remove_wait_queue(&wqh
, &wait
);
1016 if (urb
->status
== -EINPROGRESS
) {
1017 while (timeout
&& urb
->status
== -EINPROGRESS
)
1018 status
= timeout
= schedule_timeout(timeout
);
1022 current
->state
= TASK_RUNNING
;
1023 remove_wait_queue(&wqh
, &wait
);
1027 printk("usb_control/bulk_msg: timeout\n");
1028 usb_unlink_urb(urb
); // remove urb safely
1029 status
= -ETIMEDOUT
;
1031 status
= urb
->status
;
1034 *actual_length
= urb
->actual_length
;
1040 /*-------------------------------------------------------------------*/
1041 // returns status (negative) or length (positive)
1042 int usb_internal_control_msg(struct usb_device
*usb_dev
, unsigned int pipe
,
1043 devrequest
*cmd
, void *data
, int len
, int timeout
)
1049 urb
= usb_alloc_urb(0);
1053 FILL_CONTROL_URB(urb
, usb_dev
, pipe
, (unsigned char*)cmd
, data
, len
, /* build urb */
1054 (usb_complete_t
)usb_api_blocking_completion
,0);
1056 retv
= usb_start_wait_urb(urb
, timeout
, &length
);
1065 * usb_control_msg - Builds a control urb, sends it off and waits for completion
1066 * @dev: pointer to the usb device to send the message to
1067 * @pipe: endpoint "pipe" to send the message to
1068 * @request: USB message request value
1069 * @requesttype: USB message request type value
1070 * @value: USB message value
1071 * @index: USB message index value
1072 * @data: pointer to the data to send
1073 * @size: length in bytes of the data to send
1074 * @timeout: time to wait for the message to complete before timing out (if 0 the wait is forever)
1076 * This function sends a simple control message to a specified endpoint
1077 * and waits for the message to complete, or timeout.
1079 * If successful, it returns 0, othwise a negative error number.
1081 * Don't use this function from within an interrupt context, like a
1082 * bottom half handler. If you need a asyncronous message, or need to send
1083 * a message from within interrupt context, use usb_submit_urb()
1085 int usb_control_msg(struct usb_device
*dev
, unsigned int pipe
, __u8 request
, __u8 requesttype
,
1086 __u16 value
, __u16 index
, void *data
, __u16 size
, int timeout
)
1088 devrequest
*dr
= kmalloc(sizeof(devrequest
), GFP_KERNEL
);
1094 dr
->requesttype
= requesttype
;
1095 dr
->request
= request
;
1096 dr
->value
= cpu_to_le16p(&value
);
1097 dr
->index
= cpu_to_le16p(&index
);
1098 dr
->length
= cpu_to_le16p(&size
);
1100 //dbg("usb_control_msg");
1102 ret
= usb_internal_control_msg(dev
, pipe
, dr
, data
, size
, timeout
);
1111 * usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
1112 * @usb_dev: pointer to the usb device to send the message to
1113 * @pipe: endpoint "pipe" to send the message to
1114 * @data: pointer to the data to send
1115 * @len: length in bytes of the data to send
1116 * @actual_length: pointer to a location to put the actual length transfered in bytes
1117 * @timeout: time to wait for the message to complete before timing out (if 0 the wait is forever)
1119 * This function sends a simple bulk message to a specified endpoint
1120 * and waits for the message to complete, or timeout.
1122 * If successful, it returns 0, othwise a negative error number.
1123 * The number of actual bytes transferred will be plaed in the
1124 * actual_timeout paramater.
1126 * Don't use this function from within an interrupt context, like a
1127 * bottom half handler. If you need a asyncronous message, or need to
1128 * send a message from within interrupt context, use usb_submit_urb()
1130 int usb_bulk_msg(struct usb_device
*usb_dev
, unsigned int pipe
,
1131 void *data
, int len
, int *actual_length
, int timeout
)
1138 urb
=usb_alloc_urb(0);
1142 FILL_BULK_URB(urb
,usb_dev
,pipe
,(unsigned char*)data
,len
, /* build urb */
1143 (usb_complete_t
)usb_api_blocking_completion
,0);
1145 return usb_start_wait_urb(urb
,timeout
,actual_length
);
1149 * usb_get_current_frame_number()
1151 * returns the current frame number for the parent USB bus/controller
1152 * of the given USB device.
1154 int usb_get_current_frame_number(struct usb_device
*usb_dev
)
1156 return usb_dev
->bus
->op
->get_frame_number (usb_dev
);
1158 /*-------------------------------------------------------------------*/
1160 static int usb_parse_endpoint(struct usb_device
*dev
, struct usb_endpoint_descriptor
*endpoint
, unsigned char *buffer
, int size
)
1162 struct usb_descriptor_header
*header
;
1163 unsigned char *begin
;
1164 int parsed
= 0, len
, numskipped
;
1166 header
= (struct usb_descriptor_header
*)buffer
;
1168 /* Everything should be fine being passed into here, but we sanity */
1170 if (header
->bLength
> size
) {
1171 err("ran out of descriptors parsing");
1175 if (header
->bDescriptorType
!= USB_DT_ENDPOINT
) {
1176 warn("unexpected descriptor 0x%X, expecting endpoint descriptor, type 0x%X",
1177 endpoint
->bDescriptorType
, USB_DT_ENDPOINT
);
1181 if (header
->bLength
== USB_DT_ENDPOINT_AUDIO_SIZE
)
1182 memcpy(endpoint
, buffer
, USB_DT_ENDPOINT_AUDIO_SIZE
);
1184 memcpy(endpoint
, buffer
, USB_DT_ENDPOINT_SIZE
);
1186 le16_to_cpus(&endpoint
->wMaxPacketSize
);
1188 buffer
+= header
->bLength
;
1189 size
-= header
->bLength
;
1190 parsed
+= header
->bLength
;
1192 /* Skip over the rest of the Class Specific or Vendor Specific */
1196 while (size
>= sizeof(struct usb_descriptor_header
)) {
1197 header
= (struct usb_descriptor_header
*)buffer
;
1199 if (header
->bLength
< 2) {
1200 err("invalid descriptor length of %d", header
->bLength
);
1204 /* If we find another descriptor which is at or below us */
1205 /* in the descriptor heirarchy then we're done */
1206 if ((header
->bDescriptorType
== USB_DT_ENDPOINT
) ||
1207 (header
->bDescriptorType
== USB_DT_INTERFACE
) ||
1208 (header
->bDescriptorType
== USB_DT_CONFIG
) ||
1209 (header
->bDescriptorType
== USB_DT_DEVICE
))
1212 dbg("skipping descriptor 0x%X",
1213 header
->bDescriptorType
);
1216 buffer
+= header
->bLength
;
1217 size
-= header
->bLength
;
1218 parsed
+= header
->bLength
;
1221 dbg("skipped %d class/vendor specific endpoint descriptors", numskipped
);
1223 /* Copy any unknown descriptors into a storage area for drivers */
1224 /* to later parse */
1225 len
= (int)(buffer
- begin
);
1227 endpoint
->extra
= NULL
;
1228 endpoint
->extralen
= 0;
1232 endpoint
->extra
= kmalloc(len
, GFP_KERNEL
);
1234 if (!endpoint
->extra
) {
1235 err("couldn't allocate memory for endpoint extra descriptors");
1236 endpoint
->extralen
= 0;
1240 memcpy(endpoint
->extra
, begin
, len
);
1241 endpoint
->extralen
= len
;
1246 static int usb_parse_interface(struct usb_device
*dev
, struct usb_interface
*interface
, unsigned char *buffer
, int size
)
1248 int i
, len
, numskipped
, retval
, parsed
= 0;
1249 struct usb_descriptor_header
*header
;
1250 struct usb_interface_descriptor
*ifp
;
1251 unsigned char *begin
;
1253 interface
->act_altsetting
= 0;
1254 interface
->num_altsetting
= 0;
1255 interface
->max_altsetting
= USB_ALTSETTINGALLOC
;
1257 interface
->altsetting
= kmalloc(sizeof(struct usb_interface_descriptor
) * interface
->max_altsetting
, GFP_KERNEL
);
1259 if (!interface
->altsetting
) {
1260 err("couldn't kmalloc interface->altsetting");
1265 if (interface
->num_altsetting
>= interface
->max_altsetting
) {
1269 oldmas
= interface
->max_altsetting
;
1270 interface
->max_altsetting
+= USB_ALTSETTINGALLOC
;
1271 if (interface
->max_altsetting
> USB_MAXALTSETTING
) {
1272 warn("too many alternate settings (max %d)",
1277 ptr
= interface
->altsetting
;
1278 interface
->altsetting
= kmalloc(sizeof(struct usb_interface_descriptor
) * interface
->max_altsetting
, GFP_KERNEL
);
1279 if (!interface
->altsetting
) {
1280 err("couldn't kmalloc interface->altsetting");
1281 interface
->altsetting
= ptr
;
1284 memcpy(interface
->altsetting
, ptr
, sizeof(struct usb_interface_descriptor
) * oldmas
);
1289 ifp
= interface
->altsetting
+ interface
->num_altsetting
;
1290 interface
->num_altsetting
++;
1292 memcpy(ifp
, buffer
, USB_DT_INTERFACE_SIZE
);
1294 /* Skip over the interface */
1295 buffer
+= ifp
->bLength
;
1296 parsed
+= ifp
->bLength
;
1297 size
-= ifp
->bLength
;
1302 /* Skip over any interface, class or vendor descriptors */
1303 while (size
>= sizeof(struct usb_descriptor_header
)) {
1304 header
= (struct usb_descriptor_header
*)buffer
;
1306 if (header
->bLength
< 2) {
1307 err("invalid descriptor length of %d", header
->bLength
);
1311 /* If we find another descriptor which is at or below */
1312 /* us in the descriptor heirarchy then return */
1313 if ((header
->bDescriptorType
== USB_DT_INTERFACE
) ||
1314 (header
->bDescriptorType
== USB_DT_ENDPOINT
) ||
1315 (header
->bDescriptorType
== USB_DT_CONFIG
) ||
1316 (header
->bDescriptorType
== USB_DT_DEVICE
))
1321 buffer
+= header
->bLength
;
1322 parsed
+= header
->bLength
;
1323 size
-= header
->bLength
;
1327 dbg("skipped %d class/vendor specific interface descriptors", numskipped
);
1329 /* Copy any unknown descriptors into a storage area for */
1330 /* drivers to later parse */
1331 len
= (int)(buffer
- begin
);
1336 ifp
->extra
= kmalloc(len
, GFP_KERNEL
);
1339 err("couldn't allocate memory for interface extra descriptors");
1343 memcpy(ifp
->extra
, begin
, len
);
1344 ifp
->extralen
= len
;
1347 /* Did we hit an unexpected descriptor? */
1348 header
= (struct usb_descriptor_header
*)buffer
;
1349 if ((size
>= sizeof(struct usb_descriptor_header
)) &&
1350 ((header
->bDescriptorType
== USB_DT_CONFIG
) ||
1351 (header
->bDescriptorType
== USB_DT_DEVICE
)))
1354 if (ifp
->bNumEndpoints
> USB_MAXENDPOINTS
) {
1355 warn("too many endpoints");
1359 ifp
->endpoint
= (struct usb_endpoint_descriptor
*)
1360 kmalloc(ifp
->bNumEndpoints
*
1361 sizeof(struct usb_endpoint_descriptor
), GFP_KERNEL
);
1362 if (!ifp
->endpoint
) {
1363 err("out of memory");
1367 memset(ifp
->endpoint
, 0, ifp
->bNumEndpoints
*
1368 sizeof(struct usb_endpoint_descriptor
));
1370 for (i
= 0; i
< ifp
->bNumEndpoints
; i
++) {
1371 header
= (struct usb_descriptor_header
*)buffer
;
1373 if (header
->bLength
> size
) {
1374 err("ran out of descriptors parsing");
1378 retval
= usb_parse_endpoint(dev
, ifp
->endpoint
+ i
, buffer
, size
);
1387 /* We check to see if it's an alternate to this one */
1388 ifp
= (struct usb_interface_descriptor
*)buffer
;
1389 if (size
< USB_DT_INTERFACE_SIZE
||
1390 ifp
->bDescriptorType
!= USB_DT_INTERFACE
||
1391 !ifp
->bAlternateSetting
)
1398 int usb_parse_configuration(struct usb_device
*dev
, struct usb_config_descriptor
*config
, char *buffer
)
1400 int i
, retval
, size
;
1401 struct usb_descriptor_header
*header
;
1403 memcpy(config
, buffer
, USB_DT_CONFIG_SIZE
);
1404 le16_to_cpus(&config
->wTotalLength
);
1405 size
= config
->wTotalLength
;
1407 if (config
->bNumInterfaces
> USB_MAXINTERFACES
) {
1408 warn("too many interfaces");
1412 config
->interface
= (struct usb_interface
*)
1413 kmalloc(config
->bNumInterfaces
*
1414 sizeof(struct usb_interface
), GFP_KERNEL
);
1415 dbg("kmalloc IF %p, numif %i", config
->interface
, config
->bNumInterfaces
);
1416 if (!config
->interface
) {
1417 err("out of memory");
1421 memset(config
->interface
, 0,
1422 config
->bNumInterfaces
* sizeof(struct usb_interface
));
1424 buffer
+= config
->bLength
;
1425 size
-= config
->bLength
;
1427 for (i
= 0; i
< config
->bNumInterfaces
; i
++) {
1428 int numskipped
, len
;
1431 /* Skip over the rest of the Class Specific or Vendor */
1432 /* Specific descriptors */
1435 while (size
>= sizeof(struct usb_descriptor_header
)) {
1436 header
= (struct usb_descriptor_header
*)buffer
;
1438 if ((header
->bLength
> size
) || (header
->bLength
< 2)) {
1439 err("invalid descriptor length of %d", header
->bLength
);
1443 /* If we find another descriptor which is at or below */
1444 /* us in the descriptor heirarchy then we're done */
1445 if ((header
->bDescriptorType
== USB_DT_ENDPOINT
) ||
1446 (header
->bDescriptorType
== USB_DT_INTERFACE
) ||
1447 (header
->bDescriptorType
== USB_DT_CONFIG
) ||
1448 (header
->bDescriptorType
== USB_DT_DEVICE
))
1451 dbg("skipping descriptor 0x%X", header
->bDescriptorType
);
1454 buffer
+= header
->bLength
;
1455 size
-= header
->bLength
;
1458 dbg("skipped %d class/vendor specific endpoint descriptors", numskipped
);
1460 /* Copy any unknown descriptors into a storage area for */
1461 /* drivers to later parse */
1462 len
= (int)(buffer
- begin
);
1464 config
->extra
= NULL
;
1465 config
->extralen
= 0;
1467 config
->extra
= kmalloc(len
, GFP_KERNEL
);
1468 if (!config
->extra
) {
1469 err("couldn't allocate memory for config extra descriptors");
1470 config
->extralen
= 0;
1474 memcpy(config
->extra
, begin
, len
);
1475 config
->extralen
= len
;
1478 retval
= usb_parse_interface(dev
, config
->interface
+ i
, buffer
, size
);
1489 void usb_destroy_configuration(struct usb_device
*dev
)
1496 if (dev
->rawdescriptors
) {
1497 for (i
= 0; i
< dev
->descriptor
.bNumConfigurations
; i
++)
1498 kfree(dev
->rawdescriptors
[i
]);
1500 kfree(dev
->rawdescriptors
);
1503 for (c
= 0; c
< dev
->descriptor
.bNumConfigurations
; c
++) {
1504 struct usb_config_descriptor
*cf
= &dev
->config
[c
];
1509 for (i
= 0; i
< cf
->bNumInterfaces
; i
++) {
1510 struct usb_interface
*ifp
=
1513 if (!ifp
->altsetting
)
1516 for (j
= 0; j
< ifp
->num_altsetting
; j
++) {
1517 struct usb_interface_descriptor
*as
=
1518 &ifp
->altsetting
[j
];
1527 for(k
= 0; k
< as
->bNumEndpoints
; k
++) {
1528 if(as
->endpoint
[k
].extra
) {
1529 kfree(as
->endpoint
[k
].extra
);
1532 kfree(as
->endpoint
);
1535 kfree(ifp
->altsetting
);
1537 kfree(cf
->interface
);
1542 /* for returning string descriptors in UTF-16LE */
1543 static int ascii2utf (char *ascii
, __u8
*utf
, int utfmax
)
1547 for (retval
= 0; *ascii
&& utfmax
> 1; utfmax
-= 2, retval
+= 2) {
1548 *utf
++ = *ascii
++ & 0x7f;
1555 * root_hub_string is used by each host controller's root hub code,
1556 * so that they're identified consistently throughout the system.
1558 int usb_root_hub_string (int id
, int serial
, char *type
, __u8
*data
, int len
)
1562 // assert (len > (2 * (sizeof (buf) + 1)));
1563 // assert (strlen (type) <= 8);
1567 *data
++ = 4; *data
++ = 3; /* 4 bytes data */
1568 *data
++ = 0; *data
++ = 0; /* some language id */
1572 } else if (id
== 1) {
1573 sprintf (buf
, "%x", serial
);
1575 // product description
1576 } else if (id
== 2) {
1577 sprintf (buf
, "USB %s Root Hub", type
);
1579 // id 3 == vendor description
1581 // unsupported IDs --> "stall"
1585 data
[0] = 2 + ascii2utf (buf
, data
+ 2, len
- 2);
1591 * __usb_get_extra_descriptor() finds a descriptor of specific type in the
1592 * extra field of the interface and endpoint descriptor structs.
1595 int __usb_get_extra_descriptor(char *buffer
, unsigned size
, unsigned char type
, void **ptr
)
1597 struct usb_descriptor_header
*header
;
1599 while (size
>= sizeof(struct usb_descriptor_header
)) {
1600 header
= (struct usb_descriptor_header
*)buffer
;
1602 if (header
->bLength
< 2) {
1603 err("invalid descriptor length of %d", header
->bLength
);
1607 if (header
->bDescriptorType
== type
) {
1612 buffer
+= header
->bLength
;
1613 size
-= header
->bLength
;
1619 * Something got disconnected. Get rid of it, and all of its children.
1621 void usb_disconnect(struct usb_device
**pdev
)
1623 struct usb_device
* dev
= *pdev
;
1631 info("USB disconnect on device %d", dev
->devnum
);
1633 if (dev
->actconfig
) {
1634 for (i
= 0; i
< dev
->actconfig
->bNumInterfaces
; i
++) {
1635 struct usb_interface
*interface
= &dev
->actconfig
->interface
[i
];
1636 struct usb_driver
*driver
= interface
->driver
;
1638 down(&driver
->serialize
);
1639 driver
->disconnect(dev
, interface
->private_data
);
1640 up(&driver
->serialize
);
1641 usb_driver_release_interface(driver
, interface
);
1646 /* Free up all the children.. */
1647 for (i
= 0; i
< USB_MAXCHILDREN
; i
++) {
1648 struct usb_device
**child
= dev
->children
+ i
;
1650 usb_disconnect(child
);
1653 /* Let policy agent unload modules etc */
1654 call_policy ("remove", dev
);
1656 /* Free the device number and remove the /proc/bus/usb entry */
1657 if (dev
->devnum
> 0) {
1658 clear_bit(dev
->devnum
, &dev
->bus
->devmap
.devicemap
);
1659 usbdevfs_remove_device(dev
);
1662 /* Free up the device itself */
1667 * Connect a new USB device. This basically just initializes
1668 * the USB device information and sets up the topology - it's
1669 * up to the low-level driver to reset the port and actually
1670 * do the setup (the upper levels don't know how to do that).
1672 void usb_connect(struct usb_device
*dev
)
1675 // FIXME needs locking for SMP!!
1676 /* why? this is called only from the hub thread,
1677 * which hopefully doesn't run on multiple CPU's simultaneously 8-)
1679 dev
->descriptor
.bMaxPacketSize0
= 8; /* Start off at 8 bytes */
1680 #ifndef DEVNUM_ROUND_ROBIN
1681 devnum
= find_next_zero_bit(dev
->bus
->devmap
.devicemap
, 128, 1);
1682 #else /* round_robin alloc of devnums */
1683 /* Try to allocate the next devnum beginning at devnum_next. */
1684 devnum
= find_next_zero_bit(dev
->bus
->devmap
.devicemap
, 128, devnum_next
);
1686 devnum
= find_next_zero_bit(dev
->bus
->devmap
.devicemap
, 128, 1);
1688 devnum_next
= devnum
+ 1;
1689 if (devnum_next
>= 128)
1691 #endif /* round_robin alloc of devnums */
1694 set_bit(devnum
, dev
->bus
->devmap
.devicemap
);
1695 dev
->devnum
= devnum
;
1700 * These are the actual routines to send
1701 * and receive control messages.
1704 #define GET_TIMEOUT 3
1705 #define SET_TIMEOUT 3
1707 int usb_set_address(struct usb_device
*dev
)
1709 return usb_control_msg(dev
, usb_snddefctrl(dev
), USB_REQ_SET_ADDRESS
,
1710 0, dev
->devnum
, 0, NULL
, 0, HZ
* GET_TIMEOUT
);
1713 int usb_get_descriptor(struct usb_device
*dev
, unsigned char type
, unsigned char index
, void *buf
, int size
)
1718 memset(buf
,0,size
); // Make sure we parse really received data
1721 if ((result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
1722 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
,
1723 (type
<< 8) + index
, 0, buf
, size
, HZ
* GET_TIMEOUT
)) > 0 ||
1725 break; /* retry if the returned length was 0; flaky device */
1730 int usb_get_class_descriptor(struct usb_device
*dev
, int ifnum
,
1731 unsigned char type
, unsigned char id
, void *buf
, int size
)
1733 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
1734 USB_REQ_GET_DESCRIPTOR
, USB_RECIP_INTERFACE
| USB_DIR_IN
,
1735 (type
<< 8) + id
, ifnum
, buf
, size
, HZ
* GET_TIMEOUT
);
1738 int usb_get_string(struct usb_device
*dev
, unsigned short langid
, unsigned char index
, void *buf
, int size
)
1740 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
1741 USB_REQ_GET_DESCRIPTOR
, USB_DIR_IN
,
1742 (USB_DT_STRING
<< 8) + index
, langid
, buf
, size
, HZ
* GET_TIMEOUT
);
1745 int usb_get_device_descriptor(struct usb_device
*dev
)
1747 int ret
= usb_get_descriptor(dev
, USB_DT_DEVICE
, 0, &dev
->descriptor
,
1748 sizeof(dev
->descriptor
));
1750 le16_to_cpus(&dev
->descriptor
.bcdUSB
);
1751 le16_to_cpus(&dev
->descriptor
.idVendor
);
1752 le16_to_cpus(&dev
->descriptor
.idProduct
);
1753 le16_to_cpus(&dev
->descriptor
.bcdDevice
);
1758 int usb_get_status(struct usb_device
*dev
, int type
, int target
, void *data
)
1760 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
1761 USB_REQ_GET_STATUS
, USB_DIR_IN
| type
, 0, target
, data
, 2, HZ
* GET_TIMEOUT
);
1764 int usb_get_protocol(struct usb_device
*dev
, int ifnum
)
1769 if ((ret
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
1770 USB_REQ_GET_PROTOCOL
, USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1771 0, ifnum
, &type
, 1, HZ
* GET_TIMEOUT
)) < 0)
1777 int usb_set_protocol(struct usb_device
*dev
, int ifnum
, int protocol
)
1779 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1780 USB_REQ_SET_PROTOCOL
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1781 protocol
, ifnum
, NULL
, 0, HZ
* SET_TIMEOUT
);
1784 int usb_set_idle(struct usb_device
*dev
, int ifnum
, int duration
, int report_id
)
1786 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1787 USB_REQ_SET_IDLE
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1788 (duration
<< 8) | report_id
, ifnum
, NULL
, 0, HZ
* SET_TIMEOUT
);
1791 void usb_set_maxpacket(struct usb_device
*dev
)
1795 for (i
=0; i
<dev
->actconfig
->bNumInterfaces
; i
++) {
1796 struct usb_interface
*ifp
= dev
->actconfig
->interface
+ i
;
1797 struct usb_interface_descriptor
*as
= ifp
->altsetting
+ ifp
->act_altsetting
;
1798 struct usb_endpoint_descriptor
*ep
= as
->endpoint
;
1801 for (e
=0; e
<as
->bNumEndpoints
; e
++) {
1802 b
= ep
[e
].bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
1803 if ((ep
[e
].bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) ==
1804 USB_ENDPOINT_XFER_CONTROL
) { /* Control => bidirectional */
1805 dev
->epmaxpacketout
[b
] = ep
[e
].wMaxPacketSize
;
1806 dev
->epmaxpacketin
[b
] = ep
[e
].wMaxPacketSize
;
1808 else if (usb_endpoint_out(ep
[e
].bEndpointAddress
)) {
1809 if (ep
[e
].wMaxPacketSize
> dev
->epmaxpacketout
[b
])
1810 dev
->epmaxpacketout
[b
] = ep
[e
].wMaxPacketSize
;
1813 if (ep
[e
].wMaxPacketSize
> dev
->epmaxpacketin
[b
])
1814 dev
->epmaxpacketin
[b
] = ep
[e
].wMaxPacketSize
;
1821 * endp: endpoint number in bits 0-3;
1822 * direction flag in bit 7 (1 = IN, 0 = OUT)
1824 int usb_clear_halt(struct usb_device
*dev
, int pipe
)
1828 int endp
=usb_pipeendpoint(pipe
)|(usb_pipein(pipe
)<<7);
1831 if (!usb_endpoint_halted(dev, endp & 0x0f, usb_endpoint_out(endp)))
1835 result
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1836 USB_REQ_CLEAR_FEATURE
, USB_RECIP_ENDPOINT
, 0, endp
, NULL
, 0, HZ
* SET_TIMEOUT
);
1838 /* don't clear if failed */
1842 result
= usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
1843 USB_REQ_GET_STATUS
, USB_DIR_IN
| USB_RECIP_ENDPOINT
, 0, endp
,
1844 &status
, sizeof(status
), HZ
* SET_TIMEOUT
);
1848 if (le16_to_cpu(status
) & 1)
1849 return -EPIPE
; /* still halted */
1851 usb_endpoint_running(dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
));
1853 /* toggle is reset on clear */
1855 usb_settoggle(dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
), 0);
1860 int usb_set_interface(struct usb_device
*dev
, int interface
, int alternate
)
1862 struct usb_interface
*iface
;
1865 iface
= usb_ifnum_to_if(dev
, interface
);
1867 warn("selecting invalid interface %d", interface
);
1871 if ((ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1872 USB_REQ_SET_INTERFACE
, USB_RECIP_INTERFACE
, alternate
,
1873 interface
, NULL
, 0, HZ
* 5)) < 0)
1876 iface
->act_altsetting
= alternate
;
1877 dev
->toggle
[0] = 0; /* 9.1.1.5 says to do this */
1879 usb_set_maxpacket(dev
);
1883 int usb_set_configuration(struct usb_device
*dev
, int configuration
)
1886 struct usb_config_descriptor
*cp
= NULL
;
1888 for (i
=0; i
<dev
->descriptor
.bNumConfigurations
; i
++) {
1889 if (dev
->config
[i
].bConfigurationValue
== configuration
) {
1890 cp
= &dev
->config
[i
];
1895 warn("selecting invalid configuration %d", configuration
);
1899 if ((ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1900 USB_REQ_SET_CONFIGURATION
, 0, configuration
, 0, NULL
, 0, HZ
* SET_TIMEOUT
)) < 0)
1903 dev
->actconfig
= cp
;
1906 usb_set_maxpacket(dev
);
1911 int usb_get_report(struct usb_device
*dev
, int ifnum
, unsigned char type
, unsigned char id
, void *buf
, int size
)
1913 return usb_control_msg(dev
, usb_rcvctrlpipe(dev
, 0),
1914 USB_REQ_GET_REPORT
, USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1915 (type
<< 8) + id
, ifnum
, buf
, size
, HZ
* GET_TIMEOUT
);
1918 int usb_set_report(struct usb_device
*dev
, int ifnum
, unsigned char type
, unsigned char id
, void *buf
, int size
)
1920 return usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
1921 USB_REQ_SET_REPORT
, USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1922 (type
<< 8) + id
, ifnum
, buf
, size
, HZ
);
1925 int usb_get_configuration(struct usb_device
*dev
)
1928 unsigned int cfgno
, length
;
1929 unsigned char buffer
[8];
1930 unsigned char *bigbuffer
;
1931 struct usb_config_descriptor
*desc
=
1932 (struct usb_config_descriptor
*)buffer
;
1934 if (dev
->descriptor
.bNumConfigurations
> USB_MAXCONFIG
) {
1935 warn("too many configurations");
1939 if (dev
->descriptor
.bNumConfigurations
< 1) {
1940 warn("not enough configurations");
1944 dev
->config
= (struct usb_config_descriptor
*)
1945 kmalloc(dev
->descriptor
.bNumConfigurations
*
1946 sizeof(struct usb_config_descriptor
), GFP_KERNEL
);
1948 err("out of memory");
1951 memset(dev
->config
, 0, dev
->descriptor
.bNumConfigurations
*
1952 sizeof(struct usb_config_descriptor
));
1954 dev
->rawdescriptors
= (char **)kmalloc(sizeof(char *) *
1955 dev
->descriptor
.bNumConfigurations
, GFP_KERNEL
);
1956 if (!dev
->rawdescriptors
) {
1957 err("out of memory");
1961 for (cfgno
= 0; cfgno
< dev
->descriptor
.bNumConfigurations
; cfgno
++) {
1962 /* We grab the first 8 bytes so we know how long the whole */
1963 /* configuration is */
1964 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
, buffer
, 8);
1967 err("unable to get descriptor");
1969 err("config descriptor too short (expected %i, got %i)", 8, result
);
1975 /* Get the full buffer */
1976 length
= le16_to_cpu(desc
->wTotalLength
);
1978 bigbuffer
= kmalloc(length
, GFP_KERNEL
);
1980 err("unable to allocate memory for configuration descriptors");
1985 /* Now that we know the length, get the whole thing */
1986 result
= usb_get_descriptor(dev
, USB_DT_CONFIG
, cfgno
, bigbuffer
, length
);
1988 err("couldn't get all of config descriptors");
1993 if (result
< length
) {
1994 err("config descriptor too short (expected %i, got %i)", length
, result
);
2000 dev
->rawdescriptors
[cfgno
] = bigbuffer
;
2002 result
= usb_parse_configuration(dev
, &dev
->config
[cfgno
], bigbuffer
);
2004 dbg("descriptor data left");
2005 else if (result
< 0) {
2013 dev
->descriptor
.bNumConfigurations
= cfgno
;
2019 * returns string length (> 0) or error (< 0)
2021 int usb_string(struct usb_device
*dev
, int index
, char *buf
, size_t size
)
2023 unsigned char *tbuf
;
2025 unsigned int u
, idx
;
2027 if (size
<= 0 || !buf
|| !index
)
2030 tbuf
= kmalloc(256, GFP_KERNEL
);
2034 /* get langid for strings if it's not yet known */
2035 if (!dev
->have_langid
) {
2036 err
= usb_get_string(dev
, 0, 0, tbuf
, 4);
2038 err("error getting string descriptor 0 (error=%d)", err
);
2040 } else if (tbuf
[0] < 4) {
2041 err("string descriptor 0 too short");
2045 dev
->have_langid
= -1;
2046 dev
->string_langid
= tbuf
[2] | (tbuf
[3]<< 8);
2047 /* always use the first langid listed */
2048 dbg("USB device number %d default language ID 0x%x",
2049 dev
->devnum
, dev
->string_langid
);
2054 * Just ask for a maximum length string and then take the length
2055 * that was returned.
2057 err
= usb_get_string(dev
, dev
->string_langid
, index
, tbuf
, 255);
2061 size
--; /* leave room for trailing NULL char in output buffer */
2062 for (idx
= 0, u
= 2; u
< err
; u
+= 2) {
2065 if (tbuf
[u
+1]) /* high byte */
2066 buf
[idx
++] = '?'; /* non-ASCII character */
2068 buf
[idx
++] = tbuf
[u
];
2079 * By the time we get here, the device has gotten a new device ID
2080 * and is in the default state. We need to identify the thing and
2081 * get the ball rolling..
2083 * Returns 0 for success, != 0 for error.
2085 int usb_new_device(struct usb_device
*dev
)
2089 /* USB v1.1 5.5.3 */
2090 /* We read the first 8 bytes from the device descriptor to get to */
2091 /* the bMaxPacketSize0 field. Then we set the maximum packet size */
2092 /* for the control pipe, and retrieve the rest */
2093 dev
->epmaxpacketin
[0] = 8;
2094 dev
->epmaxpacketout
[0] = 8;
2096 err
= usb_set_address(dev
);
2098 err("USB device not accepting new address=%d (error=%d)",
2100 clear_bit(dev
->devnum
, &dev
->bus
->devmap
.devicemap
);
2105 wait_ms(10); /* Let the SET_ADDRESS settle */
2107 err
= usb_get_descriptor(dev
, USB_DT_DEVICE
, 0, &dev
->descriptor
, 8);
2110 err("USB device not responding, giving up (error=%d)", err
);
2112 err("USB device descriptor short read (expected %i, got %i)", 8, err
);
2113 clear_bit(dev
->devnum
, &dev
->bus
->devmap
.devicemap
);
2117 dev
->epmaxpacketin
[0] = dev
->descriptor
.bMaxPacketSize0
;
2118 dev
->epmaxpacketout
[0] = dev
->descriptor
.bMaxPacketSize0
;
2120 err
= usb_get_device_descriptor(dev
);
2121 if (err
< sizeof(dev
->descriptor
)) {
2123 err("unable to get device descriptor (error=%d)", err
);
2125 err("USB device descriptor short read (expected %i, got %i)",
2126 sizeof(dev
->descriptor
), err
);
2128 clear_bit(dev
->devnum
, &dev
->bus
->devmap
.devicemap
);
2133 err
= usb_get_configuration(dev
);
2135 err("unable to get device %d configuration (error=%d)",
2137 clear_bit(dev
->devnum
, &dev
->bus
->devmap
.devicemap
);
2143 /* we set the default configuration here */
2144 err
= usb_set_configuration(dev
, dev
->config
[0].bConfigurationValue
);
2146 err("failed to set device %d default configuration (error=%d)",
2148 clear_bit(dev
->devnum
, &dev
->bus
->devmap
.devicemap
);
2153 dbg("new device strings: Mfr=%d, Product=%d, SerialNumber=%d",
2154 dev
->descriptor
.iManufacturer
, dev
->descriptor
.iProduct
, dev
->descriptor
.iSerialNumber
);
2156 if (dev
->descriptor
.iManufacturer
)
2157 usb_show_string(dev
, "Manufacturer", dev
->descriptor
.iManufacturer
);
2158 if (dev
->descriptor
.iProduct
)
2159 usb_show_string(dev
, "Product", dev
->descriptor
.iProduct
);
2160 if (dev
->descriptor
.iSerialNumber
)
2161 usb_show_string(dev
, "SerialNumber", dev
->descriptor
.iSerialNumber
);
2164 /* now that the basic setup is over, add a /proc/bus/usb entry */
2165 usbdevfs_add_device(dev
);
2167 /* find drivers willing to handle this device */
2168 usb_find_drivers(dev
);
2170 /* userspace may load modules and/or configure further */
2171 call_policy ("add", dev
);
2176 static int usb_open(struct inode
* inode
, struct file
* file
)
2178 int minor
= MINOR(inode
->i_rdev
);
2179 struct usb_driver
*c
= usb_minors
[minor
/16];
2181 struct file_operations
*old_fops
, *new_fops
= NULL
;
2184 * No load-on-demand? Randy, could you ACK that it's really not
2185 * supposed to be done? -- AV
2187 if (!c
|| !(new_fops
= fops_get(c
->fops
)))
2189 old_fops
= file
->f_op
;
2190 file
->f_op
= new_fops
;
2191 /* Curiouser and curiouser... NULL ->open() as "no device" ? */
2192 if (file
->f_op
->open
)
2193 err
= file
->f_op
->open(inode
,file
);
2195 fops_put(file
->f_op
);
2196 file
->f_op
= fops_get(old_fops
);
2202 static struct file_operations usb_fops
= {
2207 int usb_major_init(void)
2209 if (devfs_register_chrdev(USB_MAJOR
, "usb", &usb_fops
)) {
2210 err("unable to get major %d for usb devices", USB_MAJOR
);
2214 usb_devfs_handle
= devfs_mk_dir(NULL
, "usb", NULL
);
2219 void usb_major_cleanup(void)
2221 devfs_unregister(usb_devfs_handle
);
2222 devfs_unregister_chrdev(USB_MAJOR
, "usb");
2226 #ifdef CONFIG_PROC_FS
2227 struct list_head
*usb_driver_get_list(void)
2229 return &usb_driver_list
;
2232 struct list_head
*usb_bus_get_list(void)
2234 return &usb_bus_list
;
2242 static int __init
usb_init(void)
2254 static void __exit
usb_exit(void)
2256 usb_major_cleanup();
2261 module_init(usb_init
);
2262 module_exit(usb_exit
);
2265 * USB may be built into the kernel or be built as modules.
2266 * If the USB core [and maybe a host controller driver] is built
2267 * into the kernel, and other device drivers are built as modules,
2268 * then these symbols need to be exported for the modules to use.
2270 EXPORT_SYMBOL(usb_ifnum_to_if
);
2271 EXPORT_SYMBOL(usb_epnum_to_ep_desc
);
2273 EXPORT_SYMBOL(usb_register
);
2274 EXPORT_SYMBOL(usb_deregister
);
2275 EXPORT_SYMBOL(usb_scan_devices
);
2276 EXPORT_SYMBOL(usb_alloc_bus
);
2277 EXPORT_SYMBOL(usb_free_bus
);
2278 EXPORT_SYMBOL(usb_register_bus
);
2279 EXPORT_SYMBOL(usb_deregister_bus
);
2280 EXPORT_SYMBOL(usb_alloc_dev
);
2281 EXPORT_SYMBOL(usb_free_dev
);
2282 EXPORT_SYMBOL(usb_inc_dev_use
);
2284 EXPORT_SYMBOL(usb_driver_claim_interface
);
2285 EXPORT_SYMBOL(usb_interface_claimed
);
2286 EXPORT_SYMBOL(usb_driver_release_interface
);
2287 EXPORT_SYMBOL(usb_match_id
);
2289 EXPORT_SYMBOL(usb_root_hub_string
);
2290 EXPORT_SYMBOL(usb_new_device
);
2291 EXPORT_SYMBOL(usb_reset_device
);
2292 EXPORT_SYMBOL(usb_connect
);
2293 EXPORT_SYMBOL(usb_disconnect
);
2295 EXPORT_SYMBOL(usb_check_bandwidth
);
2296 EXPORT_SYMBOL(usb_claim_bandwidth
);
2297 EXPORT_SYMBOL(usb_release_bandwidth
);
2299 EXPORT_SYMBOL(usb_set_address
);
2300 EXPORT_SYMBOL(usb_get_descriptor
);
2301 EXPORT_SYMBOL(usb_get_class_descriptor
);
2302 EXPORT_SYMBOL(__usb_get_extra_descriptor
);
2303 EXPORT_SYMBOL(usb_get_device_descriptor
);
2304 EXPORT_SYMBOL(usb_get_string
);
2305 EXPORT_SYMBOL(usb_string
);
2306 EXPORT_SYMBOL(usb_get_protocol
);
2307 EXPORT_SYMBOL(usb_set_protocol
);
2308 EXPORT_SYMBOL(usb_get_report
);
2309 EXPORT_SYMBOL(usb_set_report
);
2310 EXPORT_SYMBOL(usb_set_idle
);
2311 EXPORT_SYMBOL(usb_clear_halt
);
2312 EXPORT_SYMBOL(usb_set_interface
);
2313 EXPORT_SYMBOL(usb_get_configuration
);
2314 EXPORT_SYMBOL(usb_set_configuration
);
2316 EXPORT_SYMBOL(usb_get_current_frame_number
);
2318 EXPORT_SYMBOL(usb_alloc_urb
);
2319 EXPORT_SYMBOL(usb_free_urb
);
2320 EXPORT_SYMBOL(usb_submit_urb
);
2321 EXPORT_SYMBOL(usb_unlink_urb
);
2323 EXPORT_SYMBOL(usb_control_msg
);
2324 EXPORT_SYMBOL(usb_bulk_msg
);
2326 EXPORT_SYMBOL(usb_devfs_handle
);