Sunday, 15 May 2011

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



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

i trying perform read , write operations on pen drive.

details: vendor id: 8564 , product id: 1000. transcend jetflash mass storage device.

i keen know whether possible accomplish read/write on pen drive. if is, going happen way have tried in code provided below.

i have learnt methods device id, product id , endpoint addresses. have implemented.

here device getting acknowledged , opened. and, interface claims successful.

but mass transfer functions homecoming -1.

what 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("\nfailed initialise libusb\n"); homecoming 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 utilize confignum configuration of 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 sent : %s", my_string); e = libusb_bulk_transfer(handle, bulk_ep_out, my_string, bytes_read, &transferred, 5000); printf("\nxfer returned %d", e); printf("\nsent %d bytes string: %s\n", transferred, my_string); libusb_bulk_transfer(handle, bulk_ep_in, my_string1, 256, &transferred, 5000); printf("\nxfer returned %d", e); //it returns -1... error, guess. printf("\nreceived %d bytes string: %s\n", transferred, my_string1); e = libusb_release_interface(handle, 0); libusb_close(handle); libusb_exit(null); homecoming 0; }

try code given below , should work on lpc2148. have tested lpc2148 configured receive interrupt usb after write happens (from user-space) , rtc starts running.

answering question whether involves kernel driver in read/write or not: far have studied, have detach kernel driver , claim interface using libusb apis. though not sure whether can done without detaching 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; info = (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); info = null; homecoming 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); (interface_index=0;interface_index<config->bnuminterfaces;interface_index++) { const struct libusb_interface *iface = &config->interface[interface_index]; (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 endpoint: %d", endpoint->bmattributes); printf("\n\tinterval polling info tranfer: %d\n", endpoint->binterval); } } libusb_free_config_descriptor(null); homecoming 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 = 0, index; char str1[64], str2[64]; char found = 0; // init libusb r = libusb_init(null); if(r < 0) { printf("\nfailed initialise libusb\n"); homecoming 1; } else printf("\ninit successful!\n"); // list of usb devices cnt = libusb_get_device_list(null, &devs); if (cnt < 0) { printf("\nthere no usb devices on bus\n"); homecoming -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 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); homecoming 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); homecoming -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); homecoming -1; } else printf("\ndevice 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); homecoming -1; } } e = libusb_claim_interface(handle, 0); if(e < 0) { printf("\ncannot claim interface"); libusb_free_device_list(devs, 1); libusb_close(handle); homecoming -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 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 string: %s\n", transferred, my_string); } else printf("\nerror in write! e = %d , transferred = %d\n", e, transferred); sleep(3); = 0; for(i = 0; < 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 string lpc2148 sleep(1); } else { printf("\nerror in read! e = %d , received = %d\n", e, received); homecoming -1; } } e = libusb_release_interface(handle, 0); libusb_close(handle); libusb_exit(null); printf("\n"); homecoming 0; }

usb libusb

No comments:

Post a Comment