Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

usb - Read/write on a pen drive using libusb: libusb_bulk_transfer()

I am trying to perform read and write operations on a pen drive.

Details: Vendor ID: 8564 and Product ID: 1000. It is a Transcend JetFlash mass storage device.

I am keen to know that whether it is possible to achieve a read/write on a pen drive. If it is, then is it going to happen the way I have tried in the code provided below.

I have learnt the methods to get the device id, product id and endpoint addresses. This is what I have implemented.

Here the device is getting acknowledged and opened. And, even the interface claims it is successful.

But bulk transfer functions return -1.

What is the explanation?

#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("
failed to initialise libusb
");
        return 1;
    }

    handle = libusb_open_device_with_vid_pid(NULL, 0x8564, 0x1000);
    if(handle == NULL)
    {
        printf("
Error in device opening!");
    }
    else
        printf("
Device 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("
Kernel Driver Active");
        if(libusb_detach_kernel_driver(handle, 0) == 0)
            printf("
Kernel Driver Detached!");
    }

    e = libusb_claim_interface(handle, 0);
    if(e < 0)
    {
        printf("
Cannot Claim Interface");
    }
    else
        printf("
Claimed 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("
To be sent : %s", my_string);

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

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

    e = libusb_release_interface(handle, 0);
    libusb_close(handle);
    libusb_exit(NULL);
    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try the code given below and it should work on LPC2148. I have tested this with an LPC2148 configured to receive an interrupt from USB after a write happens (from user-space) and the RTC starts running.

Answering to your question whether it involves a kernel driver in read/write or not: As far as I have studied, you have to detach the kernel driver and claim the interface using libusb APIs. Though I am not sure whether it can be done without detaching it or not.

#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("
Interface Descriptors: ");
    printf("
Number of Interfaces: %d", config->bNumInterfaces);
    printf("
Length: %d", config->bLength);
    printf("
Desc_Type: %d", config->bDescriptorType);
    printf("
Config_index: %d", config->iConfiguration);
    printf("
Total length: %lu", config->wTotalLength);
    printf("
Configuration Value: %d", config->bConfigurationValue);
    printf("
Configuration Attributes: %d", config->bmAttributes);
    printf("
MaxPower(mA): %d
", 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("
EndPoint Descriptors: ");
            printf("
Size of EndPoint Descriptor: %d", endpoint->bLength);
            printf("
Type of Descriptor: %d", endpoint->bDescriptorType);
            printf("
Endpoint Address: 0x0%x", endpoint->bEndpointAddress);
            printf("
Maximum Packet Size: %x", endpoint->wMaxPacketSize);
            printf("
Attributes applied to Endpoint: %d", endpoint->bmAttributes);
            printf("
Interval for Polling for data Tranfer: %d
", 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("
Failed to initialise libusb
");
        return 1;
    }
    else
        printf("
Init successful!
");

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

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

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

        printf("
Device Descriptors: ");
        printf("
Vendor ID: %x", desc.idVendor);
        printf("
Product ID: %x", desc.idProduct);
        printf("
Serial Number: %x", desc.iSerialNumber);
        printf("
Size of Device Descriptor: %d", desc.bLength);
        printf("
Type of Descriptor: %d", desc.bDescriptorType);
        printf("
USB Specification Release Number: %d", desc.bcdUSB);
        printf("
Device Release Number: %d", desc.bcdDevice);
        printf("
Device Class: %d", desc.bDeviceClass);
        printf("
Device Sub-Class: %d", desc.bDeviceSubClass);
        printf("
Device Protocol: %d", desc.bDeviceProtocol);
        printf("
Max. Packet Size: %d", desc.bMaxPacketSize0);
        printf("
Number of Configurations: %d
", 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("
Manufactured: %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("
Product: %s", str2);
        printf("
----------------------------------------");

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

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

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

    libusb_free_device_list(devs, 1);

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

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

    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, '', 64);
    memset(my_string1, '', 64);

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

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

    e = libusb_bulk_transfer(handle, BULK_EP_IN, my_string, length, &transferred, 0);
    if(e == 0 && transferred == length)
    {
        printf("
Write successful!");
        printf("
Sent %d bytes with string: %s
", transferred, my_string);
    }
    else
        printf("
Error in write! e = %d and transferred = %d
", 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("
Received: ");
            printf("%c", my_string1[i]); //Will read a string from LPC2148
            sleep(1);
        }
        else
        {
            printf("
Error in read! e = %d and received = %d
", e, received);
            return -1;
        }
    }

    e = libusb_release_interface(handle, 0);

    libusb_close(handle);
    libusb_exit(NULL);

    printf("
");
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...