LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1361|回复: 0

請教有關libusb 為何USB Devicev總是顯示busy狀態

[复制链接]
发表于 2008-6-25 16:13:34 | 显示全部楼层 |阅读模式
請教一下
我在網路上參考了很多libusb example
但我在執行usb_claim_interface()這個function總是失敗
錯誤訊息如下:
Check that you have permissions to write to 001/004 and, if you don't, that you set up hotplug (http://linux-hotplug.sourceforge.net/) correctly.
USB error: could not claim interface 0: Device or resource busy

我使用的OS為Linux Redhat 9.0 (核心2.4.29)
硬體為USB 512M Device隨身碟(U盤)

我瀏覽的一些網站 也有人提出相同問題
但回覆者大多指向OS核心版本問題?!

也有回覆者說 不需要call usb_claim_interface()
因為OS已經默認?!

我也嘗試不呼叫usb_claim_interface() , 直接call usb_bulk_write()
錯誤訊息如下:
USB error: error writing to bulk endpoint 2: Device or resource busy


請問各位前輩 有什麼建議嗎
謝謝

Source Code
==============================================
#include <stdio.h>
#include <stdlib.h>
#include <usb.h>

#define VENDOR_ID          0x08ec
#define PRODUCT_ID               0x0008
#define DEVICE_MINOR           16
#define USB_TIMEOUT            10000           
#define HID_REPORT_SET     0x09
#define HID_REPORT_GET           0x01
typedef struct
{
        struct usb_device         *udev;
        usb_dev_handle                *device_handle;
}device_descript;

int                         g_num;
device_descript         g_list[DEVICE_MINOR];

void usbio_main(struct usb_device *dev)
{
    usb_dev_handle *dev_handle;

    dev_handle = usb_open(dev);

    if (dev_handle == NULL)
    {
        printf("Specified USB IO Open Failed...\n");
        return;
    }
    printf("Specified USB IO Open Success...\n");
    int status = usbio_read(dev_handle);
    printf("Error Code : %d\n",status);
}

int usbio_read(usb_dev_handle *Device_handle)
{   
    unsigned char send_data[72];
    unsigned char recv_data[72];
    int        send_len,recv_len;

    memset(send_data, 0 , sizeof(send_data));   
    memset(recv_data, 0 , sizeof(recv_data));   
      
    if (Device_handle == NULL)
         return -999;
   
    usb_claim_interface(Device_handle, 0);

    char WtpCmd[100]={'0'};
    int dwCmdSize=100;
    int write_bytes = usb_bulk_write(Device_handle,0x02,(char*)&WtpCmd,dwCmdSize,200);
    printf("write bytes %d\n",write_bytes);
    printf("%s\n",usb_strerror());

    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 1234;
    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 -9999;   
     }        
     if (recv_len != 72)
     {        
                      return recv_len;   
     }      
     usb_release_interface(Device_handle, 0);

     unsigned char buffer72[72];
   
     memcpy(buffer72, recv_data, 72);
   
     printf("recv : %c\n",buffer72[0]);
     return 1;
   
    usb_close(Device_handle);
}

struct usb_device *usbio_probe()
{
    struct usb_bus         *bus;
    struct usb_device         *dev;

    g_num=0;
    usb_init();
    usb_set_debug(255);
    usb_find_busses();
    usb_find_devices();

    for (bus = usb_busses; bus; bus = bus->next)
    {

        for (dev = bus->devices; dev; dev = dev->next)
        {
            struct usb_device_descriptor *desc;
            desc = &(dev->descriptor);
                   
            if(g_num<DEVICE_MINOR)
            {       
                g_list[g_num].udev=dev;
                g_num++;       
            }       
            
            printf("Vendor/Product ID: %04x:%04x\n", desc->idVendor,desc->idProduct);
            if ((desc->idVendor == VENDOR_ID) && (desc->idProduct == PRODUCT_ID))
                return dev;
        }
    }
    return NULL;
}

int main()
{
    struct usb_device                         *dev;
    struct usb_device_descriptor         *desc;

    dev = usbio_probe();
    desc = &(dev->descriptor);

    if (dev == NULL) {
        printf("Specified USB IO Card not Found...\n");
        return -1;
    }

    printf("Specified USB IO Card found...\n");
    printf("Vendor/Product ID: %04x:%04x\n", desc->idVendor,desc->idProduct);

    usbio_main(dev);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表