next up previous contents
Next: Host Controller Up: Operating System Aspects Previous: Selection of driver

Programming for USB

Before transfering data to or from a device, a handle needs to be obtained to the device. libusb is a user-level library used to implement client-side functionality for USB on Linux. libusb requires that the function usb_init() be called before any other function is called. To get to access the various devices on the bus, two functions need be called in sequence: usb_find_busses(), usb_find_devices(). Calling these functions leads to initialization of a global list named usb_busses of type usb_bus. The number of elements in this list is equal to the number of busses on the system. The usb_bus structure has a field named devices, which in itself is a list of all devices on the bus. A brief description on the internals of the functions follows.

usb_find_busses iterates through the /proc/bus/usb/ directory to generate the list of busses and accordingly initialized the usb_busses list.usb_find_devices then iterates through the directories corresponding to the busses on the system, and creates a usb_device entry on the bus for each file(device) inside the directory.
As mentioned earlier, each device on the bus is represented as a file in the usb filesystem (at /proc/bus/usb/001 directory). The contents of the file represent the device descriptor for the associated device. The descriptor may be read from the file to initialize a pointer of type dev, to point to the device as follows:

    ret = read(fd, (void *)&dev->descriptor, sizeof(dev->descriptor));
fd is a file descriptor corresponding to the device file inside the usb filesystem.

After calling the two functions (usb_find_busses() and usb_find_devices()), the devices list in the appropriate bus may be iterated along to get access to each of the devices. On reaching the desired device, a handle to the device may be obtained by calling the usb_open() method, passing a pointer to the usb_device structure as a parameter. The handle may then be used to read to, write from and configure the device.
The range of functions broadly classify into two classes:

*
those for configuring the device: usb_set_configuration, usb_claim_interface
*
those for doing data transfer: usb_bulk_write,usb_bulk_read,usb_control_msg


next up previous contents
Next: Host Controller Up: Operating System Aspects Previous: Selection of driver
Bhanu Nagendra P.
2003-07-28