使用libusb:libusb_bulk_transfer()在笔驱动器上读/写

使用libusb:libusb_bulk_transfer()在笔驱动器上读/写,usb,libusb,Usb,Libusb,我正在尝试对一个磁盘执行读写操作 详细信息:供应商ID:8564和产品ID:1000。它是JetFlash大容量存储设备 我很想知道是否有可能在笔驱动器上实现读/写。如果是,那么它将以我在下面提供的代码中尝试的方式发生 我已经学会了获取设备id、产品id和端点地址的方法。这就是我所实现的 在这里,设备被确认并打开。而且,即使是界面也声称它是成功的 但批量传递函数返回-1 原因是什么 #include <stdio.h> #include <sys/types.h> #in

我正在尝试对一个磁盘执行读写操作

详细信息:供应商ID:8564和产品ID:1000。它是JetFlash大容量存储设备

我很想知道是否有可能在笔驱动器上实现读/写。如果是,那么它将以我在下面提供的代码中尝试的方式发生

我已经学会了获取设备id、产品id和端点地址的方法。这就是我所实现的

在这里,设备被确认并打开。而且,即使是界面也声称它是成功的

但批量传递函数返回-1

原因是什么

#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include </usr/include/libusb-1.0/libusb.h>

#define BULK_EP_OUT     0x82
#define BULK_EP_IN      0x02

int main(void)
{
    int r = 0, e = 0;
    struct libusb_device_handle *handle = NULL;
    struct libusb_device **devs;
    struct libusb_device *dev;
    struct libusb_device_descriptor desc;
    char str1[256], str2[256];

    /* Init libusb */
    r = libusb_init(NULL);
    if (r < 0)
    {
        printf("\nfailed to initialise libusb\n");
        return 1;
    }

    handle = libusb_open_device_with_vid_pid(NULL, 0x8564, 0x1000);
    if(handle == NULL)
    {
        printf("\nError in device opening!");
    }
    else
        printf("\nDevice Opened");

    // Tell libusb to use the CONFIGNUM configuration of the device

    libusb_set_configuration(handle, 1);
    if(libusb_kernel_driver_active(handle, 0) == 1)
    {
        printf("\nKernel Driver Active");
        if(libusb_detach_kernel_driver(handle, 0) == 0)
            printf("\nKernel Driver Detached!");
    }

    e = libusb_claim_interface(handle, 0);
    if(e < 0)
    {
        printf("\nCannot Claim Interface");
    }
    else
        printf("\nClaimed Interface");

      /* Communicate */

    int bytes_read;
    int nbytes = 256;
    unsigned char *my_string, *my_string1;
    int transferred = 0;
    my_string = (unsigned char *) malloc (nbytes + 1);
    my_string1 = (unsigned char *) malloc (nbytes + 1);

    strcpy(my_string, "divesd");
    printf("\nTo be sent : %s", my_string);

    e = libusb_bulk_transfer(handle, BULK_EP_OUT, my_string, bytes_read, &transferred, 5000);
    printf("\nXfer returned with %d", e);
    printf("\nSent %d bytes with string: %s\n", transferred, my_string);

    libusb_bulk_transfer(handle, BULK_EP_IN, my_string1, 256, &transferred, 5000);
    printf("\nXfer returned with %d", e);     //It returns -1... This is an error, I guess.
    printf("\nReceived %d bytes with string: %s\n", transferred, my_string1);

    e = libusb_release_interface(handle, 0);
    libusb_close(handle);
    libusb_exit(NULL);
    return 0;
}
#包括
#包括
#包括
#包括
#定义批量输出0x82
#在0x02中定义批量_EP_
内部主(空)
{
int r=0,e=0;
struct libusb_device_handle*handle=NULL;
结构libu_设备**开发者;
结构libusb_设备*dev;
结构libusb_设备描述符desc;
字符str1[256],str2[256];
/*初始化libusb*/
r=libusb_init(空);
if(r<0)
{
printf(“\n未能初始化libusb\n”);
返回1;
}
handle=libusb\u open\u device\u,带有\u vid\u pid(NULL,0x8564,0x1000);
if(handle==NULL)
{
printf(“\n设备打开时出错!”);
}
其他的
printf(“\n设备已打开”);
//告诉libusb使用设备的CONFIGNUM配置
libusb_设置_配置(句柄,1);
如果(libusb_内核_驱动程序_处于活动状态(句柄,0)=1)
{
printf(“\n网络驱动程序激活”);
如果(libusb_detach_内核_驱动程序(句柄,0)=0)
printf(“\nKernel驱动程序已分离!”);
}
e=libusb_声明_接口(句柄,0);
if(e<0)
{
printf(“\n无法声明接口”);
}
其他的
printf(“\n目标接口”);
/*沟通*/
读取整数字节;
int nbytes=256;
无符号字符*my_字符串,*my_字符串1;
int=0;
my_字符串=(无符号字符*)malloc(n字节+1);
my_string1=(无符号字符*)malloc(n字节+1);
strcpy(我的弦,“跳水”);
printf(“\n待发送:%s”,我的字符串);
e=libusb_bulk_传输(句柄、bulk_EP_OUT、my_字符串、字节读取和传输,5000);
printf(“\n传递返回%d”,e);
printf(“\n发送%d个字节,字符串:%s\n”,已传输,我的\u字符串);
libusb_bulk_transfer(句柄、bulk_EP_IN、my_string1、256和transfer,5000);
printf(“\nXfer返回了%d”,e);//它返回-1……我想这是一个错误。
printf(“\n收到%d个字节,字符串为:%s\n”,已传输,my\u string1);
e=libusb_释放_接口(句柄,0);
libu_关闭(手柄);
libu_退出(空);
返回0;
}

尝试下面给出的代码,它应该可以工作。我已经用一个LPC2148进行了测试,该LPC2148配置为在写入发生(从用户空间)和RTC开始运行后从USB接收中断

回答您的问题,它是否涉及读/写内核驱动程序:据我所研究,您必须分离内核驱动程序并使用libusbapi声明接口。虽然我不确定是否可以在不拆下的情况下完成

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include </usr/local/include/libusb-1.0/libusb.h>

#define BULK_EP_OUT     0x82
#define BULK_EP_IN      0x08

int interface_ref = 0;
int alt_interface, interface_number;

int print_configuration(struct libusb_device_handle *hDevice, struct libusb_config_descriptor *config)
{
    char *data;
    int index;

    data = (char *)malloc(512);
    memset(data, 0, 512);

    index = config->iConfiguration;

    libusb_get_string_descriptor_ascii(hDevice, index, data, 512);

    printf("\nInterface Descriptors: ");
    printf("\n\tNumber of Interfaces: %d", config->bNumInterfaces);
    printf("\n\tLength: %d", config->bLength);
    printf("\n\tDesc_Type: %d", config->bDescriptorType);
    printf("\n\tConfig_index: %d", config->iConfiguration);
    printf("\n\tTotal length: %lu", config->wTotalLength);
    printf("\n\tConfiguration Value: %d", config->bConfigurationValue);
    printf("\n\tConfiguration Attributes: %d", config->bmAttributes);
    printf("\n\tMaxPower(mA): %d\n", config->MaxPower);

    free(data);
    data = NULL;
    return 0;
}

struct libusb_endpoint_descriptor* active_config(struct libusb_device *dev, struct libusb_device_handle *handle)
{
    struct libusb_device_handle *hDevice_req;
    struct libusb_config_descriptor *config;
    struct libusb_endpoint_descriptor *endpoint;
    int altsetting_index, interface_index=0, ret_active;
    int i, ret_print;

    hDevice_req = handle;

    ret_active = libusb_get_active_config_descriptor(dev, &config);
    ret_print = print_configuration(hDevice_req, config);

    for (interface_index=0;interface_index<config->bNumInterfaces;interface_index++)
    {
        const struct libusb_interface *iface = &config->interface[interface_index];
        for (altsetting_index=0; altsetting_index<iface->num_altsetting; altsetting_index++)
        {
            const struct libusb_interface_descriptor *altsetting = &iface->altsetting[altsetting_index];

            int endpoint_index;
            for(endpoint_index=0; endpoint_index<altsetting->bNumEndpoints; endpoint_index++)
            {
                const struct libusb_endpoint_desriptor *ep = &altsetting->endpoint[endpoint_index];
                endpoint = ep;
                alt_interface = altsetting->bAlternateSetting;
                interface_number = altsetting->bInterfaceNumber;
            }

            printf("\nEndPoint Descriptors: ");
            printf("\n\tSize of EndPoint Descriptor: %d", endpoint->bLength);
            printf("\n\tType of Descriptor: %d", endpoint->bDescriptorType);
            printf("\n\tEndpoint Address: 0x0%x", endpoint->bEndpointAddress);
            printf("\n\tMaximum Packet Size: %x", endpoint->wMaxPacketSize);
            printf("\n\tAttributes applied to Endpoint: %d", endpoint->bmAttributes);
            printf("\n\tInterval for Polling for data Tranfer: %d\n", endpoint->bInterval);
        }
    }
    libusb_free_config_descriptor(NULL);
    return endpoint;
}

int main(void)
{
    int r = 1;
    struct libusb_device **devs;
    struct libusb_device_handle *handle = NULL, *hDevice_expected = NULL;
    struct libusb_device *dev, *dev_expected;

    struct libusb_device_descriptor desc;
    struct libusb_endpoint_descriptor *epdesc;
    struct libusb_interface_descriptor *intdesc;

    ssize_t cnt;
    int e = 0, config2;
    int i = 0, index;
    char str1[64], str2[64];
    char found = 0;

    // Init libusb
    r = libusb_init(NULL);
    if(r < 0)
    {
        printf("\nFailed to initialise libusb\n");
        return 1;
    }
    else
        printf("\nInit successful!\n");

    // Get a list of USB devices
    cnt = libusb_get_device_list(NULL, &devs);
    if (cnt < 0)
    {
        printf("\nThere are no USB devices on the bus\n");
        return -1;
    }
    printf("\nDevice count: %d\n-------------------------------\n", cnt);

    while ((dev = devs[i++]) != NULL)
    {
        r = libusb_get_device_descriptor(dev, &desc);
        if (r < 0)
            {
            printf("Failed to get device descriptor\n");
            libusb_free_device_list(devs, 1);
            libusb_close(handle);
            break;
        }

        e = libusb_open(dev, &handle);
        if (e < 0)
        {
            printf("Error opening device\n");
            libusb_free_device_list(devs, 1);
            libusb_close(handle);
            break;
        }

        printf("\nDevice Descriptors: ");
        printf("\n\tVendor ID: %x", desc.idVendor);
        printf("\n\tProduct ID: %x", desc.idProduct);
        printf("\n\tSerial Number: %x", desc.iSerialNumber);
        printf("\n\tSize of Device Descriptor: %d", desc.bLength);
        printf("\n\tType of Descriptor: %d", desc.bDescriptorType);
        printf("\n\tUSB Specification Release Number: %d", desc.bcdUSB);
        printf("\n\tDevice Release Number: %d", desc.bcdDevice);
        printf("\n\tDevice Class: %d", desc.bDeviceClass);
        printf("\n\tDevice Sub-Class: %d", desc.bDeviceSubClass);
        printf("\n\tDevice Protocol: %d", desc.bDeviceProtocol);
        printf("\n\tMax. Packet Size: %d", desc.bMaxPacketSize0);
        printf("\n\tNumber of Configurations: %d\n", desc.bNumConfigurations);

        e = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, (unsigned char*) str1, sizeof(str1));
        if (e < 0)
        {
        libusb_free_device_list(devs, 1);
            libusb_close(handle);
            break;
        }
        printf("\nManufactured: %s", str1);

        e = libusb_get_string_descriptor_ascii(handle, desc.iProduct, (unsigned char*) str2, sizeof(str2));
        if(e < 0)
        {
        libusb_free_device_list(devs, 1);
            libusb_close(handle);
            break;
        }
        printf("\nProduct: %s", str2);
        printf("\n----------------------------------------");

        if(desc.idVendor == 0xffff && desc.idProduct == 0x4)
        {
           found = 1;
        break;
        }
    }//end of while
    if(found == 0)
    {
        printf("\nDevice NOT found\n");
        libusb_free_device_list(devs, 1);
        libusb_close(handle);
        return 1;
    }
    else
    {
        printf("\nDevice found");
        dev_expected = dev;
        hDevice_expected = handle;
    }

    e = libusb_get_configuration(handle, &config2);
    if(e!=0)
    {
        printf("\n***Error in libusb_get_configuration\n");
        libusb_free_device_list(devs, 1);
        libusb_close(handle);
        return -1;
    }
    printf("\nConfigured value: %d", config2);

    if(config2 != 1)
    {
        libusb_set_configuration(handle, 1);
        if(e!=0)
        {
            printf("Error in libusb_set_configuration\n");
            libusb_free_device_list(devs, 1);
            libusb_close(handle);
            return -1;
        }
        else
            printf("\nDevice is in configured state!");
    }

    libusb_free_device_list(devs, 1);

    if(libusb_kernel_driver_active(handle, 0) == 1)
    {
        printf("\nKernel Driver Active");
        if(libusb_detach_kernel_driver(handle, 0) == 0)
            printf("\nKernel Driver Detached!");
        else
        {
            printf("\nCouldn't detach kernel driver!\n");
            libusb_free_device_list(devs, 1);
            libusb_close(handle);
            return -1;
        }
    }

    e = libusb_claim_interface(handle, 0);
    if(e < 0)
    {
        printf("\nCannot Claim Interface");
        libusb_free_device_list(devs, 1);
        libusb_close(handle);
        return -1;
    }
    else
        printf("\nClaimed Interface\n");

    active_config(dev_expected, hDevice_expected);

    //   Communicate

    char *my_string, *my_string1;
    int transferred = 0;
    int received = 0;
    int length = 0;

    my_string = (char *)malloc(nbytes + 1);
    my_string1 = (char *)malloc(nbytes + 1);

    memset(my_string, '\0', 64);
    memset(my_string1, '\0', 64);

    strcpy(my_string, "Prasad Divesd");
    length = strlen(my_string);

    printf("\nTo be sent: %s", my_string);

    e = libusb_bulk_transfer(handle, BULK_EP_IN, my_string, length, &transferred, 0);
    if(e == 0 && transferred == length)
    {
        printf("\nWrite successful!");
        printf("\nSent %d bytes with string: %s\n", transferred, my_string);
    }
    else
        printf("\nError in write! e = %d and transferred = %d\n", e, transferred);

    sleep(3);
    i = 0;

    for(i = 0; i < length; i++)
    {
        e = libusb_bulk_transfer(handle, BULK_EP_OUT, my_string1, 64, &received, 0);  //64: Max Packet Length
        if(e == 0)
        {
            printf("\nReceived: ");
            printf("%c", my_string1[i]); //Will read a string from LPC2148
            sleep(1);
        }
        else
        {
            printf("\nError in read! e = %d and received = %d\n", e, received);
            return -1;
        }
    }

    e = libusb_release_interface(handle, 0);

    libusb_close(handle);
    libusb_exit(NULL);

    printf("\n");
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义批量输出0x82
#在0x08中定义批量_EP_
int接口_ref=0;
int alt_接口,接口编号;
int打印配置(结构libusb\u设备\u句柄*hDevice,结构libusb\u配置\u描述符*config)
{
字符*数据;
整数指数;
数据=(字符*)malloc(512);
memset(数据,0512);
index=config->i配置;
libusb_get_string_descriptor_ascii(hDevice,index,data,512);
printf(“\n接口描述符:”);
printf(“\n\t接口数量:%d”,配置->bNuminInterfaces);
printf(“\n\t长度:%d”,配置->混合);
printf(“\n\tDesc\u类型:%d”,配置->bDescriptorType);
printf(“\n\t配置索引:%d”,配置->图标配置);
printf(“\n\t总长度:%lu”,配置->总长度);
printf(“\n\t配置值:%d”,配置->配置值);
printf(“\n\t配置属性:%d”,配置->配置属性);
printf(“\n\t功耗(mA):%d\n”,配置->最大功耗);
免费(数据);
数据=空;
返回0;
}
struct libusb_endpoint_descriptor*active_config(struct libusb_device*dev,struct libusb_device_handle*handle)
{
结构libusb_设备_句柄*hDevice_请求;
结构libusb_config_描述符*config;
结构libusb_endpoint_描述符*endpoint;
int ALTSETING_index,接口_index=0,ret_active;
int i,重新打印;
hDevice_req=句柄;
ret\u active=libusb\u get\u active\u config\u描述符(开发和配置);
ret_print=打印配置(hDevice_req,config);
对于(接口索引=0;接口索引XbNuminterFaces;接口索引++)
{
const struct libusb_interface*iface=&config->interface[interface_index];
对于(altsetting_index=0;altsetting_indexnum_altsetting;altsetting_index++)
{
常量结构libusb_接口_描述符*altsetting=&iface->altsetting[altsetting_index];
int-endpoint_指数;
对于(端点索引=0;端点索引XBNumendpoints;端点索引++)
{
const struct libusb_endpoint_desriptor*ep=&altsetting->endpoint[endpoint_index];
终点=ep;
alt_接口=altsetting->bAlternateSetting;
接口号=altsetting->B接口号;
}
printf(“\n点描述符:”);
printf(“\n\t端点描述符的大小:%d”,端点->混合);
printf(“\n\t描述符类型:%d”,端点->bDescriptorType);
printf(“\n\tEndpoint地址:0x0%x”,端点->bendpoint地址);
printf(“\n\t最大数据包大小:%x”,端点->wMaxPacketSize);
printf(“\n\t应用于端点的属性:%d”,端点->属性);
printf(“\n\