Milinda Pathirage’s Blog

Computers are fascinating machines, but they’re mostly a reflection of the people using them

Getting Started with libusb on Ubuntu

November 12th, 2008 · 4 Comments · Uncategorized, linux

What is libusb?

libusb is a library which user level applications can used to access USB devices without worrying about the underlying operating system. Or we can see it as a high-level API which wraps low-level kernel interactions with USB modules and provide developer with the facility to implement device driver for a USB device from the user space.

libusb is easy to use and libusb API provide high level abstraction to the Kernel structures and allows the developers to have access to these structures through the USBFS(USBfilesystem).

libusb supports Linux, FreeBSD, NetBSD, OpenBSD, Darwin, MacOS X and Windows through the libusb-win32 project.

Installing libusb on Ubuntu

libusb-0.1-4 and libusb-dev packages are available in Ubuntu 8.04 repositories. You can use follwing command to install them.

sudo apt-get install libusb-dev libusb-0.1-4

Simple Example

Here is a simple example application which gather all the possible technical/hardware details of a USB devices connected to the computer. Some devices will not contain some details. This example is based on the Developing Linux Device Drivers using Libusb API article. Inline comments will describe the code.

Make sure that you have at least one USB device plugged into your computer.

#include <stdio.h>
#include <usb.h>

void print_endpoint(struct usb_endpoint_descriptor *endpoint) {
    printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
    printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
    printf(" wMaxPacketSize: %d\n", endpoint->wMaxPacketSize);
    printf(" bInterval: %d\n", endpoint->bInterval);
    printf(" bRefresh: %d\n", endpoint->bRefresh);
    printf(" bSynchAddress: %d\n", endpoint->bSynchAddress);
}

void print_altsetting(struct usb_interface_descriptor *interface) {
    int i;

    printf(" bInterfaceNumber: %d\n", interface->bInterfaceNumber);
    printf(" bAlternateSetting: %d\n", interface->bAlternateSetting);
    printf(" bNumEndpoints: %d\n", interface->bNumEndpoints);
    printf(" bInterfaceClass: %d\n", interface->bInterfaceClass);
    printf(" bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
    printf(" bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
    printf(" iInterface: %d\n", interface->iInterface);

    for (i = 0; i < interface->bNumEndpoints; i++)
        print_endpoint(&interface->endpoint[i]);
}

void print_interface(struct usb_interface *interface) {
    int i;

    for (i = 0; i < interface->num_altsetting; i++)
        print_altsetting(&interface->altsetting[i]);
}

void print_configuration(struct usb_config_descriptor *config) {
    int i;

    printf(" wTotalLength: %d\n", config->wTotalLength);
    printf(" bNumInterfaces: %d\n", config->bNumInterfaces);
    printf(" bConfigurationValue: %d\n", config->bConfigurationValue);
    printf(" iConfiguration: %d\n", config->iConfiguration);
    printf(" bmAttributes: %02xh\n", config->bmAttributes);
    printf(" MaxPower: %d\n", config->MaxPower);

    for (i = 0; i < config->bNumInterfaces; i++)
        print_interface(&config->interface[i]);
}

int main(void) {

    struct usb_bus *bus;
    struct usb_device *dev;

    /* Initialize libusb */
    usb_init();

    /* Find all USB busses on system */
    usb_find_busses();

    /* Find all devices on all USB devices */
    usb_find_devices();

    printf("bus/device idVendor/idProduct\n");

    /* usb_busses is a global variable. */
    for (bus = usb_busses; bus; bus = bus->next) {
        for (dev = bus->devices; dev; dev = dev->next) {
            int ret, i;
            char string[256];
            usb_dev_handle *udev;

            printf("%s/%s %04X/%04X\n", bus->dirname, dev->filename,
                    dev->descriptor.idVendor, dev->descriptor.idProduct);
            
            /* Opens a USB device */            
            udev = usb_open(dev);
            if (udev) {
                if (dev->descriptor.iManufacturer) {
                    
                    /* Retrieves a string descriptor from a device using the first language */
                    ret = usb_get_string_simple(udev, dev->descriptor.iManufacturer, string, sizeof (string));
                    if (ret > 0)
                        printf("- Manufacturer : %s\n", string);
                    else
                        printf("- Unable to fetch manufacturer string\n");
                }

                if (dev->descriptor.iProduct) {
                    ret = usb_get_string_simple(udev, dev->descriptor.iProduct, string, sizeof (string));
                    if (ret > 0)
                        printf("- Product : %s\n", string);
                    else
                        printf("- Unable to fetch product string\n");
                }

                if (dev->descriptor.iSerialNumber) {
                    ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof (string));
                    if (ret > 0)
                        printf("- Serial Number: %s\n", string);
                    else
                        printf("- Unable to fetch serial number string\n");
                }

                /* Closes a USB device */
                usb_close(udev);
            }

            if (!dev->config) {
                printf(" Couldn't retrieve descriptors\n");
                continue;
            }

            for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
                print_configuration(&dev->config[i]);
        }
    }

    return 0;
}

For further details about the structures please look at the /usr/include/usb.h file.

Sphere: Related Content

Related posts brought to you by Yet Another Related Posts Plugin.

Tags:

4 responses so far ↓

  • 1 Mel // Mar 6, 2009 at 6:05 pm

    I tried to run this code on my computer but it does not find the usb.h file. I found the file in /usr/include but the compiler does not see it. Do you know where the problem could be?

  • 2 Milinda // Mar 8, 2009 at 11:10 am

    try -I/usr/include option while compiling the code.

  • 3 rashidSAX // May 17, 2009 at 7:49 pm

    This is the basic code on all sites.

    Do you have examples actually from sending/receiving data from/to the device?

    Thanks.

  • 4 Milinda // May 19, 2009 at 1:44 am

    Please have a look at this example: http://www.linuxjournal.com/article/7353

Leave a Comment