A short introduction to libusb

In this section, I will take you through the steps necessary to query the Luxeed keyboard device using the programming language C and libusb.

A simple libusb program

The first thing that should be done is to install the libusb development package. This varies from distribution to distribution, but the package to install is generally called something like libusb-dev.

You should also bookmark the libusb API page as it is an invaluable source of information concerning the library, and ultimately, the tool which you will use to make the keyboard blink.

To use the Luxeed keyboard we need to find its Vendor ID and Product ID. This information is used by the program to query the correct device, and you can think of it as a unique identifier for this particular brand and model.

Connect the keyboard, and then execute the following command in a shell as root: lsusb -v

Among the output you should be able to spot the entries corresponding to the keyboard:

...

idVendor           0x534b
idProduct          0x0600
bcdDevice           30.01
iManufacturer           1 Luxiium Lighting@Technology,Inc
iProduct                2 LED_KeyBoard
...
 

As you can see above, 0x534B is the Vendor ID and 0x0600 is the Product ID.

Now it's time to code! Let's assume that you have a basic C program:

#include <stdio.h>

int main (int argc,char **argv)
{
  printf("Lets look for the Luxeed LED keyboard...\n");
  return 0;
}
 

To use libusb we need to first add the libusb header file to the top of the file: 

#include <usb.h>
 

Also, we need a function to search for the keyboard device:

static struct usb_device *findKeyboard(uint16_t vendor, uint16_t product)
{
  struct usb_bus *bus;
  struct usb_device *dev;
  struct usb_bus *busses;

  usb_init();
  usb_find_busses();
  usb_find_devices();
  busses = usb_get_busses();

  for (bus = busses; bus; bus = bus->next)
    for (dev = bus->devices; dev; dev = dev->next)
      if ((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
        return dev;

  return NULL;
}
 

To avoid redundancy, the explanations to the above function calls are explained in the libusb api. However, the general gist of it is that it initializes the libusb subsystem, iterates over all the USB busses and their devices until it finds a device with the idVendor and idProduct matching the keyboard. If one is found, we return its device structure, else null.

Let's replace the contents of the main function with the following. It will call the function we just added and output the result.

printf("Trying to locate the Luxeed LED keyboard...");

// Call the findKeyboard function and store the returned results.
// The function takes the vendor id and product id of the desired keyboard as arguments
// so that it knows what to look for
struct usb_device *dev;
dev = findKeyboard(0x534B,0x0600);

//If the keyboard wasn't found the function will have returned NULL
if (dev == NULL) {
    fprintf(stderr, "Not found!\n");
    return 1;
}
//If it gets this far, it means it found the keyboard
printf("Found it! Now make me blink already!\n");
return 0;
 
 

Now, it's time to compile and try this out. Save the file as find_keyboard.c and simply execute gcc as you normally would, but with the added parameter -lusb so that it will link to the libusb library.

gcc -o find_keyboard find_keyboard.c -lusb
 
Download the complete source code here: find_keyboard.c

All done! That wasn't hard at all was it? :)

Please keep in mind that I am also new to libusb, so if you know a better way of doing or explaining things, then please don't hesitate to contact me and I will update the page and and accredit you.

Comments

Have you given up on creating this driver?

Hi,
I have been awaiting more posts on your USB reverse engineering pages. I have a USB device called a "Traveler USB Microscope SU 1071 vid = 0x1871, pid = 0x01b0
which does not seem to work under Linux.
You can see the product here: http://www.traveler-service.de/cms/

I will use your find_keyboard.c as a starting point to see if
I can start and make a driver for it. Any more information
on writing usb drives would be help full.
Best regard David.