Libusb库的使用

转自:http://blog.sina.com.cn/s/blog_4a5b632801009upv.html

 

使用libusb之前你的linux系统必须装有usb文件系统,这里还介绍了使用hiddev设备文件来访问设备,目的在于不仅可以比较出usb的易用性,还提供了一个转化成libusb驱动的案例。
3.1 find 设备
任何驱动第一步首先是寻找到要操作的设备,我们先来看看HID驱动是怎样寻找到设备的。我们假设寻找设备的函数Device_Find(注:代码只是为了方便解说,不保证代码的健全)
int Device_Find()
{
    char dir_str[100];  
    char hiddev[100];   
DIR dir;              
memset (dir_str, 0 , sizeof(dir_str));
memset (hiddev, 0 , sizeof(hiddev));
   
dir=opendir("/dev/usb/hid");
    if(dir){
       
        sprintf(dir_str,"/dev/usb/hid/");
        closedir(dir);
    }else{
       
        sprintf(dir_str,"/dev/usb/");
    }
   
for(i = 0; i < DEVICE_MINOR; i++) {
   
        sprintf(hiddev, "%shiddev%d", dir_str,i);
      
       fd = open(hiddev, O_RDWR);
       if(fd > 0) {
          
          ioctl(fd, HIDIOCGDEVINFO, &info);
   
             
           if(info.vendor== VENDOR_ID && info.product== PRODUCT_ID) {
               
                  
               device_num++;  
           }
           close(fd);
       }
    }
    return device_num;        
}
我们再来看libusb是如何来寻找和初始化设备
int Device_Find()
{
struct usb_bus             *busses;
    int                           device_num = 0;
    device_num = 0;      
   
    usb_init();           
    usb_find_busses();  
    usb_find_devices();
   
   
busses = usb_get_busses();
    struct usb_bus       *bus;
   
    for (bus = busses; bus; bus = bus->next) {
        struct usb_device *dev;
       
        for (dev = bus->devices; dev; dev = dev->next) {
          
if(dev->descriptor.idVendor==VENDOR_ID&& dev->descriptor.idProduct == PRODUCT_ID) {
               
                  
               device_num++;  
             
             
    }
    return device_num;       
}
注:在新版本的libusb中,usb_get_busses就可以不用了,这个函数是返回系统上的usb总线链表句柄
这里我们直接用usb_busses变量,这个变量在usb.h中被定义为外部变量
所以可以直接写成这样:
struct usb_bus    *bus;
        for (bus = usb_busses; bus; bus = bus->next) {
               struct usb_device *dev;
        for (dev = bus->devices; dev; dev = dev->next) {
          
        }
}
3.2 打开设备
假设我们定义的打开设备的函数名是device_open,
int Device_Open()
{
    int handle;
   
handle = open(“hiddev0”, O_RDONLY);
}
int Device_Open()
{
struct usb_device*    udev;
usb_dev_handle*        device_handle;
device_handle = usb_open(udev);
}
3.3 读写设备和操作设备
假设我们的设备使用控制传输方式,至于批处理传输和中断传输限于篇幅这里不介绍
我们这里定义三个函数,Device_Write, Device_Read, Device_Report
Device_Report 功能发送接收函数
Device_Write 功能写数据
Device_Read   功能读数据
Device_Write和Device_Read调用Device_Report发送写的信息和读的信息,开发者根据发送的命令协议来设计,我们这里只简单实现发送数据的函数。
假设我们要给设备发送72字节的数据,头8个字节是报告头,是我们定义的和设备相关的规则,后64位是数据。
HID驱动的实现(这里只是用代码来有助理解,代码是伪代码)
int Device_Report(int fd, unsigned char *buffer72)
{
int       ret;
int      index;
    unsigned char send_data[72];
unsigned char recv_data[72];
    struct hiddev_usage_ref uref;
    struct hiddev_report_info rinfo;
    ret = ioctl(fd, HIDIOCINITREPORT, 0);
    if( ret !=0) {
        return NOT_OPENED_DEVICE;
    }
   
    for(index = 0; index < 72; index++) {
       
    uref.report_type = HID_REPORT_TYPE_FEATURE;
    uref.report_id = HID_REPORT_ID_FIRST;
    uref.usage_index = index;
    uref.field_index = 0;
    uref.value = send_data[index];
    ioctl(fd, HIDIOCGUCODE, &uref);
    ret=ioctl(fd, HIDIOCSUSAGE, &uref);
    if(ret != 0 ){
           return UNKNOWN_ERROR;
    }
}
rinfo.report_type = HID_REPORT_TYPE_FEATURE;
rinfo.report_id = HID_REPORT_ID_FIRST;
rinfo.num_fields = 1;
ret=ioctl(fd, HIDIOCSREPORT, &rinfo);  
if(ret != 0) {
        return WRITE_REPORT;
}
ret = ioctl(fd, HIDIOCINITREPORT, 0);
for(index = 0; index < 72; index++) {
    uref.report_type = HID_REPORT_TYPE_FEATURE;
    uref.report_id = HID_REPORT_ID_FIRST;
    uref.usage_index = index;
    uref.field_index = 0;
    ioctl(fd, HIDIOCGUCODE, &uref);
    ret = ioctl(fd, HIDIOCGUSAGE, &uref);
    if(ret != 0 ) {
        return UNKNOWN_ERROR;
    }
    recv_data[index] = uref.value;
}
memcpy(buffer72, recv_data, 72);
return SUCCESS;
}
libusb驱动的实现
int Device_Report(int fd, unsigned char *buffer72)
{
   
    usb_dev_handle* Device_handle;
   
   
    unsigned char   send_data[72];
    unsigned char   recv_data[72];
   
    int              send_len;
    int             recv_len;
   
   
    memset(send_data, 0 , sizeof(send_data));
    memset(recv_data, 0 , sizeof(recv_data));
   
   
    Device_handle = (usb_dev_handle*)(g_list[fd].device_handle);
    if (Device_handle == NULL) {
        return NOT_OPENED_DEVICE;
}
usb_claim_interface(Device_handle, 0);
send_len = usb_control_msg(Device_handle,
USB_ENDPOINT_OUT + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
                               HID_REPORT_SET,
                               0x300,
                               0,
                               send_data, 72, USB_TIMEOUT);
if (send_len < 0) {
        return WRITE_REPORT;
}
if (send_len != 72) {
        return send_len;
}
recv_len = usb_control_msg(Device_handle,
USB_ENDPOINT_IN + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
                               HID_REPORT_GET,
                               0x300,
                                 0,
                               recv_data, 72, USB_TIMEOUT);
                                                   
    if (recv_len < 0) {
        printf("failed to retrieve report from USB device!/n");
        return READ_REPORT;
    }
   
    if (recv_len != 72) {
        return recv_len;
    }
   
   
   
    usb_release_interface(RY2_handle, 0);
    memcpy(buffer72, recv_data, 72);
return SUCCESS;
}
3.4 关闭设备
假设我们定义的关闭设备的函数名是Device_Close()
int Device_Close()
{
    int handle;
   
handle = open(“hiddev0”, O_RDONLY);
close( handle );
}
int Device_Close()
{
struct usb_device*    udev;
usb_dev_handle*        device_handle;
device_handle = usb_open(udev);
usb_close(device_handle);
}
libusb 的驱动框架
前面我们看了些主要的libusb函数的使用,这里我们把前面的内容归纳下:
一般的驱动应该都包含如下接口:
Device_Find();
Device_Open();
Device_Write();
Device_Read();
Device_Close();
具体代码如下:
#include <usb.h>
typedef struct
{
    struct usb_device*    udev;
    usb_dev_handle*        device_handle;
   
} device_descript;
#define USB_TIMEOUT     10000
#define VENDOR_ID    0xffff    
#define PRODUCT_ID   0xffff
#define DEVICE_MINOR 16
int     g_num;
device_descript g_list[ DEVICE_MINOR ];
int Device_Find()
{
    struct usb_bus       *bus;
    struct usb_device *dev;
    g_num = 0;
    usb_find_busses();
    usb_find_devices();
   
   
    for (bus = usb_busses; bus; bus = bus->next) {
        for (dev = bus->devices; dev; dev = dev->next) {
if(dev->descriptor.idVendor==VENDOR_ID&& dev->descriptor.idProduct == PRODUCT_ID) {
                   
                    if (g_num < DEVICE_MINOR) {
                     g_list[g_num].udev = dev;  
                     g_num ++;
                                 
                 
        }
    }
   
    return g_num;
}
int Device_Open()
{
   
    if(g_list[g_num].udev != NULL) {
        g_list[g_num].device_handle = usb_open(g_list[g_num].udev);
}
}
int DeviceWite(int handle)
{
   
}
int DeviceOpen(int handle)
{
   
}
void Device_close(int handle)
{
   
}
    到此,使用libusb进行驱动开发介绍完了,通过对库所提供的API的使用可以体会到libusb的易用性。
Logo

更多推荐