| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
|
|
By vikram_cvk at 2004-07-16 05:05
|
|
Introduction
We often come across a situation where a USB device which runs perfectly on Windows platform does not even get detected on Linux. Lack of support for USB devices is one of the reason why some people don't embrace Linux. Now there is a new API by name Libusb which helps the developers to develop USB device drivers on the fly!
What is Libusb
Libusb is a high-level language API which conceals low-level kernel interactions with the USB modules. It provides a set of function which are adequate to develop a device driver for a USB device from the Userspace.
Libusb is not complex
For any wannabe Linux Kernel programmers developing device driver as a Kernel module is a herculean task. Developing kernel modules requires fair degree of proficiency in 'C' language and also good idea of kernel subsystems, data structures etc. All these are enough to put-off a developer from venturing into Device Driver programming.Libusb has been designed to address this shortcoming. Simplified interface allows developers to develop USB drivers from the userspace . Libusb library functions provide high level abstraction to the Kernel structures and allows the developers to have access to these structures through the USBFS(USBfilesystem).
Its Cross-platform
Beauty of Libusb lies in its cross platform functionality. Driver written for one platform could be easily ported onto another platform with little or no changes, currently following operating systems are supported by Libusb.
Linux
FreeBSD
Darwin
OS X
This HOWTO focuses on how Libusb can be used on Linux platform. For information about other platforms goto http://http://libusb.sourceforge.net/.
LIBUSB ON LINUX
Linux is the most popular platform for the Libusb API,the reason being growing popularity of Linux as a stable OS. On Linux Libusb makes of the USBFS file system. by default USBFS is automatically mounted when the system is booted.
What is USBFS
USBFS is a filesystem specifically designed for USB devices, by default this filesystem gets mounted when the system is booted and it can be found at /proc/bus/usb/. This filesystem consists of information about all the USB devices that are connected to the computer.Libusb makes use of this filesystem to interact with the USB devices.
Following C program can be a stepping stone into the world of Libusb.This program can be used to gather all the technical/hardware details of a USB device connected to the computer ,ensure that some USB device is connected into the USB port.
Details like Vendor-Id , Product-Id ,Endpoint addresses of a USB device is of paramount importance for a device driver developer.
/* testlibusb.c */
#include
#include
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;
usb_init();
usb_find_busses();
usb_find_devices();
printf("bus/device idVendor/idProduct\n");
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);
udev = usb_open(dev);
if (udev) {
if (dev->descriptor.iManufacturer) {
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");
}
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;
}
The above program should be compiled as
(root$)gcc -o usbdevice_details testlibusb.c -I/usr/local/include -L. -lnsl -lm -lc -L/usr/local/lib -lusb
(root$)./usbdevice_details (enter)
Following is the output of the above command ,its the listing of a USB pen drive connected to my system.
The first line displays the bus-name/device-name & device-id/product-id and rest of the listing is self-descriptive.
001/004 0EA0/2168
- Manufacturer : USB
- Product : Flash Disk
- Serial Number: 4CE45C4E403EE53D
wTotalLength: 39
bNumInterfaces: 1
bConfigurationValue: 1
iConfiguration: 0
bmAttributes: 80h
MaxPower: 100
bInterfaceNumber: 0
bAlternateSetting: 0
bNumEndpoints: 3
bInterfaceClass: 8
bInterfaceSubClass: 6
bInterfaceProtocol: 80
iInterface: 0
bEndpointAddress: 81h
bmAttributes: 02h
wMaxPacketSize: 64
bInterval: 0
bRefresh: 0
bSynchAddress: 0
bEndpointAddress: 02h
bmAttributes: 02h
wMaxPacketSize: 64
bInterval: 0
bRefresh: 0
bSynchAddress: 0
bEndpointAddress: 83h
bmAttributes: 03h
wMaxPacketSize: 2
bInterval: 1
bRefresh: 0
bSynchAddress: 0
Before executing the above program download the current version of Libusb library from, http://http://libusb.sourceforge.net/. The above program can also be found under the tests directory of Libusb directory (after u install it)
Now I will explain in brief some of the functions and attributes dealt in the above program.
usb_init() - Used to initialize Libusb and establish connection with kernel structures .
usb_find_busses() - Looks for all the USB busses on the computer.
usb_find_devices() - Looks for all the USB devices connected to the computer.
usb_open(dev) - Opens the device 'dev' which is given as argument to this function.
usb_get_string_simple() - Used to extract the string descriptor of the device taken argument.
Important attributes of USB devices useful in device driver coding
Configuration and Endpoints are one of the two important descriptors of any USB device. These descriptors are defined using the
|
|
|
|
All times are GMT -5. The time now is 05:50 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Click Here to receive a complimentary subscription courtesy of LQ.

|
Latest Threads
LQ News
LQ Podcast
LQ Radio
|
|
#include <stdio.h> main() { Printf("hello world\n"); }#include <stdio.h>
main() {
Printf("hello world\n");
}
So (according to the preview button) you lose your indentation but I have no idea what happened to the #include directives in this post. Perhaps the author could fix it up with some 'code' tags. The ref to the sourceforge site also needs changing so it comes out as a link.
I've been searching for documentation on USB device drivers recently. There are a few good references but overall the situation is not very satisfactory. So almost anything is a positive contribution. If I understand correctly libusb lets you access USB devices even if they don't have kernel-level drivers. As the author suggests the (again rather brief) documentation at http://libusb.sourceforge.net/doc is worth a look for further code and examples. Apparently gphoto and rio500 are based on libusb.
#include <stdio.h>
#include <stdlib.h>
#include <usb.h>
I do not know if all of those are necessary. After compiling and testing I noticed that usb_get_string_simple does not return any string via string parameter when testing the program as a user. The return value of the function usb_get_string_simple is -1 all the time.
But when I changed to root user and tested the program, everything worked fine.
Anyway, I wonder if this phenomen has something to do with udev? With two different live-cd (gentoo and SuSE) boot process stopped just after udev:
...
Starting udev....
... udev running.
Starting hardware detection...
Activating usb devices... <-- boot stops here
(the above example is with the SuSE 10.1 installation CD-ROM and repair system option).
I found the article very interesting though it end with a hanging thought (i.e., These descriptors are defined using the) and that was the end.
I am looking for any information that anyone can give me about writing a USB device driver that has to be run in User Space.
Thanks,
John V.
Here is the end of the article :
Important attributes of USB devices useful in device driver coding
Configuration and Endpoints are one of the two important descriptors of any USB device. These desciptors are defined using the struct usb_config_descriptor and struct_usb_endpoint_descriptor respectively .
dev->descriptor.idVendor : Reveals the Vendor-Id of the USB device connected to the system.
dev->descriptor.idProduct : Reveals the Product-Id of the USB device connected to the system.
dev->descriptor.iManufacturer : Reveals the name of the Manufacturer USB device connected to the system.
EndpointAddress:Combination of endpoint address and enpoint direction on a USB device.
InterfaceNumber : One of the several interfaces that is allocated to the connected USB device.
AlternateSetting:This is part of the a single interface allocated to the USB device.
Prerequites for Libusb programming
Linux system with Kernel 2.4 above series.
Proficiency in C language.
Good understanding of USB device internals.
Idea about USBFS.
$ /sbin/lsusb
Note: I'm using Fedora 8 64-bit as a VMWare guest.
Please tell me what's wrong with it.
Thanks in advance!
/* * testlibusb.c * * Test suite program */ #include <stdio.h> #include <string.h> #include "usb.h" #define snprintf printf #define ID_PRODUCT 0x0200 #define ID_VENDOR 0x8235 #define CONFIGURATION 1 #define INTERFACE_ 0 #define ALTINTERFACE 0 #define TIMEOUT 5000 #define OUT_ENDPOINT 0x01 #define IN_ENDPOINT 0x82 // #define SET_ALTINTERFACE_ONCE int verbose = 0; unsigned char first = true; 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 print_device(struct usb_device *dev, int level) { usb_dev_handle *udev; char description[256]; char string[256]; int ret, i; udev = usb_open(dev); if (udev) { if (dev->descriptor.iManufacturer) { ret = usb_get_string_simple(udev, dev->descriptor.iManufacturer, string, sizeof(string)); if (ret > 0) snprintf(description, sizeof(description), "%s - ", string); else snprintf(description, sizeof(description), "%04X - ", dev->descriptor.idVendor); } else snprintf(description, sizeof(description), "%04X - ", dev->descriptor.idVendor); if (dev->descriptor.iProduct) { ret = usb_get_string_simple(udev, dev->descriptor.iProduct, string, sizeof(string)); if (ret > 0) snprintf(description + strlen(description), sizeof(description) - strlen(description), "%s", string); else snprintf(description + strlen(description), sizeof(description) - strlen(description), "%04X", dev->descriptor.idProduct); } else snprintf(description + strlen(description), sizeof(description) - strlen(description), "%04X", dev->descriptor.idProduct); } else snprintf(description, sizeof(description), "%04X - %04X", dev->descriptor.idVendor, dev->descriptor.idProduct); printf("%.*sDev #%d: %s\n", level * 2, " ", dev->devnum, description); if (udev && verbose) { if (dev->descriptor.iSerialNumber) { ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof(string)); if (ret > 0) printf("%.*s - Serial Number: %s\n", level * 2, " ", string); } } if (udev) usb_close(udev); if (verbose) { if (!dev->config) { printf(" Couldn't retrieve descriptors\n"); return 0; } for (i = 0; i < dev->descriptor.bNumConfigurations; i++) print_configuration(&dev->config[i]); } else { for (i = 0; i < dev->num_children; i++) print_device(dev->children[i], level + 1); } return 0; } int read(struct usb_dev_handle *handle) { if (usb_claim_interface(handle, INTERFACE_) < 0) { printf("error on usb_claim_interface: %s\n", usb_strerror()); return -1; } printf("usb_claim_interface successful\n"); #ifdef SET_ALTINTERFACE_ONCE if (first) { first = false; #endif if (usb_set_altinterface(handle, ALTINTERFACE) < 0){ printf("usb_set_altinterface failed: %s\n", usb_strerror()); } #ifdef SET_ALTINTERFACE_ONCE } #endif int size = 512, res; char *data = (char *) malloc(size*sizeof(char)); res = usb_bulk_read(handle, IN_ENDPOINT, data, size, TIMEOUT); if (res < 0){ printf("usb_bulk_read failed: %s\n", usb_strerror()); } printf("usb_bulk_read: %d bytes read: ", res); for (int i = 0; i < res; ++i) { printf("%3x ", data[i]); } printf("\n"); usb_release_interface(handle, INTERFACE_); free(data); } int write(struct usb_dev_handle *handle) { int size = 12; char *data = (char *) malloc(size*sizeof(char)); data[0] = 0x33; data[1] = 0x5B; data[2] = 0x02; data[3] = 0x01; data[4] = 0x00; data[5] = 0x05; data[6] = 0x01; data[7] = 0x03; data[8] = 0x07; data[9] = 0x0F; data[10] = 0x7F; data[11] = 0x1F; // data = {0x33, 0x5B, 0x02, 0x01, 0x00, 0x05, 0x01, 0x03, 0x07, 0x0F, 0x7F, 0x1F}; if (usb_claim_interface(handle, INTERFACE_) < 0) { printf("error on usb_claim_interface: %s\n", usb_strerror()); return -1; } printf("usb_claim_interface successful\n"); #ifdef SET_ALTINTERFACE_ONCE if (first) { first = false; #endif if (usb_set_altinterface(handle, ALTINTERFACE) < 0){ printf("usb_set_altinterface failed: %s\n", usb_strerror()); } #ifdef SET_ALTINTERFACE_ONCE } #endif printf("usb_bulk_write: writing %d bytes: ", size); for (int i = 0; i < size; ++i) { printf("%3x ", data[i]); } printf("\n"); int res = usb_bulk_write(handle, OUT_ENDPOINT, data, size, TIMEOUT); if (res < 0){ printf("usb_bulk_write failed: %s\n", usb_strerror()); return -1; } printf("usb_bulk_write: %d bytes written\n", res); usb_release_interface(handle, INTERFACE_); free(data); } int readWrite(struct usb_dev_handle *handle) { int size = 512; char *data = (char *) malloc(size*sizeof(char)); printf("type a string...\n"); scanf("%s", data); // Get a string if (usb_claim_interface(handle, INTERFACE_) < 0) { printf("error on usb_claim_interface: %s\n", usb_strerror()); system("PAUSE"); return -1; } printf("usb_claim_interface successful\n"); #ifdef SET_ALTINTERFACE_ONCE if (first) { first = false; #endif if (usb_set_altinterface(handle, ALTINTERFACE) < 0){ printf("usb_set_altinterface failed: %s\n", usb_strerror()); } #ifdef SET_ALTINTERFACE_ONCE } #endif if (usb_bulk_write(handle, OUT_ENDPOINT, data, strlen(data), 3000) < 0){ printf("usb_bulk_write failed: %s\n", usb_strerror()); system("PAUSE"); return -1; } strcpy(data, "12345678901234567890"); printf("%s\n", "read data"); if (usb_bulk_read(handle, IN_ENDPOINT, data, size, 3000) < 0){ printf("usb_bulk_read failed: %s\n", usb_strerror()); } printf("output %d, %s\n", size, data); // for (int i = 0; i < size; ++i) { // printf("%4x ", data[i]); // } usb_release_interface(handle, INTERFACE_); free(data); } int readWriteLoop(struct usb_dev_handle *handle) { int NOF_LOOPS = 20; int size = 12; char *data = (char *) malloc(size*sizeof(char)); data[0] = 0x33; data[1] = 0x5B; data[2] = 0x02; data[3] = 0x01; data[4] = 0x00; data[5] = 0x05; data[6] = 0x01; data[7] = 0x03; data[8] = 0x07; data[9] = 0x0F; data[10] = 0x7F; data[11] = 0x1F; // data = {0x33, 0x5B, 0x02, 0x01, 0x00, 0x05, 0x01, 0x03, 0x07, 0x0F, 0x7F, 0x1F}; if (usb_claim_interface(handle, INTERFACE_) < 0) { printf("error on usb_claim_interface: %s\n", usb_strerror()); return -1; } printf("usb_claim_interface successful\n"); #ifdef SET_ALTINTERFACE_ONCE if (first) { first = false; #endif if (usb_set_altinterface(handle, ALTINTERFACE) < 0){ printf("usb_set_altinterface failed: %s\n", usb_strerror()); } #ifdef SET_ALTINTERFACE_ONCE } #endif printf("usb_bulk_write: writing %d bytes: ", size); for (int i = 0; i < size; ++i) { printf("%3x ", data[i]); } printf("\n------------------------\n"); for (int var = 0; var < NOF_LOOPS; ++var) { int res = usb_bulk_write(handle, OUT_ENDPOINT, data, size, TIMEOUT); if (res < 0){ printf("usb_bulk_write failed: %s\n", usb_strerror()); return -1; } printf("usb_bulk_write: %d bytes written\n", res); int size = 64; char *data = (char *) malloc(size*sizeof(char)); res = usb_bulk_read(handle, IN_ENDPOINT, data, size, TIMEOUT); if (res < 0){ printf("usb_bulk_read failed: %s\n", usb_strerror()); } printf("usb_bulk_read: %d bytes read: ", res); for (int i = 0; i < res; ++i) { printf("%3x ", data[i]); } printf("\n"); } usb_release_interface(handle, INTERFACE_); free(data); } void logDevices() { struct usb_bus *bus; printf("log devices...\n"); for (bus = usb_get_busses(); bus; bus = bus->next) { if (bus->root_dev && !verbose) print_device(bus->root_dev, 0); else { struct usb_device *dev; for (dev = bus->devices; dev; dev = dev->next) print_device(dev, 0); } } } int main(int argc, char *argv[]) { struct usb_bus *bus; struct usb_device *dev; struct usb_dev_handle *handle; bool run = true; if (argc > 1 && !strcmp(argv[1], "-v")) verbose = 1; usb_set_debug(255); printf("initialize libraray, find busses and devices\n"); usb_init(); usb_find_busses(); usb_find_devices(); if (verbose) logDevices(); int size = 512; char *data = (char *) malloc(size*sizeof(char)); printf("Search for device with idVendor 0x%x and idProduct 0x%x\n", ID_VENDOR, ID_PRODUCT); for (bus = usb_get_busses(); bus; bus = bus->next) { if (verbose) printf("Found bus %s\n", bus->dirname); for (dev = bus->devices; dev; dev = dev->next) { if (verbose) printf("Found device with idVendor 0x%x and idProduct 0x%x\n", dev->descriptor.idVendor, dev->descriptor.idProduct); if ((dev->descriptor.idProduct == ID_PRODUCT) && (dev->descriptor.idVendor == ID_VENDOR)){ printf("Device found -> open\n"); handle = usb_open(dev); if (!handle) { printf("invalid handle: %s\n", usb_strerror()); system("PAUSE"); return -1; } printf("Set configuration\n"); if (usb_set_configuration(handle, CONFIGURATION) < 0) { printf("error on usb_set_configuration: %s\n", usb_strerror()); system("PAUSE"); return -1; } printf("Set altinterface (must failed)\n"); #ifdef SET_ALTINTERFACE_ONCE if (first) { first = false; #endif if (usb_set_altinterface(handle, ALTINTERFACE) < 0){ printf("usb_set_altinterface failed: %s\n", usb_strerror()); } #ifdef SET_ALTINTERFACE_ONCE } #endif printf("w=write, r=read, x=exit, t=write+read, u=write+read(2), l=r/w loop, z=reset and open\n"); while (run) { scanf("%s", data); switch (data[0]) { case 'w': // write write(handle); break; case 'r': // read read(handle); break; case 'x': // exit run = false; break; case 't': // write + read if (write(handle)) { read(handle); } break; case 'u': // write + read readWrite(handle); break; case 'l': // loop readWriteLoop(handle); break; case 's': // reset first flag (set_altinterface()) first = true; break; case 'z': // reset and open usb_reset(handle); handle = usb_open(dev); break; default: break; } } printf("\ndone\n"); } } } free(data); system("PAUSE"); return 1; }thx
Thanks
#include <stdio.h>
#include <usb.h>
Under Ubuntu linux you must compile using sudo as well as run the program using sudo to get it to work..
this is a good starting article.