Wednesday, 15 January 2014

libusb bulk transfer -



libusb bulk transfer -

i trying implement user space usb driver using libusb1.0.9. have lpc2148 blueboard(arm7) me..this board loaded opensource usb stack/firmware mr. bertrik sikken. user space driver trying read write board. getting garbage data. want know flow of mass tranfer. transfer/transaction there kernel device driver involved?? , need usb gadget device driver also?? not able understand info gets copied. of import thing when read/write interrupt gets generated , can see right info on lcd. need read/write usbrxdata/usbtxdata? please needfull.

i tried below code mass transfer read , write..

int usb_read(struct libusb_device *dev,struct libusb_device_handle *hdevice) { char *data,*data1; struct libusb_endpoint_descriptor *ep; struct libusb_interface_descriptor *id; int len=64,r,ret_alt,ret_clm,ret_rst,i; struct libusb_device **list; info = (char *)malloc(512); //allocation of buffers data1 = (char *)malloc(512); memset(data,'\0',512); memset(data1,'\0',512); if(hdevice==null) { printf("\nno device found\n"); homecoming 0; } int ret_open = libusb_open(dev,&hdevice); if(ret_open!=0) { printf("error in libusb_open\n"); libusb_free_device_list(list,1); homecoming -1; } char str_tx[512]="g"; //data send device char str_rx[512]; //receive string info = str_tx; printf("data::%s\t,str::%s\n",data,str_tx); //printf("%c\n",data); ep = active_config(dev,hdevice); printf("after ep\n"); //printf("alt_interface = %d\n",alt_interface); ret_rst = libusb_reset_device(hdevice); if(ret_rst < 0) { printf("error in reset :: %d",ret_rst); homecoming -1; } printf("original data1 : %s\n",data1); r = libusb_bulk_transfer(hdevice,0x08,str_tx,512,&len,0); //write device buffer info printf("error number :: %d\n",r); int le = ep->bendpointaddress; int ty = ep->bdescriptortype; int y = ep->bmattributes; printf("y::%d\tatt:: %d\n",y,ep->bmattributes); if(r==-1) printf("error in io\n"); if(r==0) { printf("data returned :: %s\n",data); printf("len= %d\n",len); printf("device button pressed!!!!\n"); } else { printf("error in mass transfer\n"); homecoming -1; } r = libusb_bulk_transfer(hdevice,0x82,data1,512,&len,0); //read device buffer data1 //str_rx = data1; //printf("end point address::%d\n",le); //printf("end point desc.type::%d\n",ty); if(r==-1) printf("error in io\n"); if(r==0) { printf("data1 returned::%s\n",data1); //received string in data1 printf("len= %d\n",len); printf("device button pressed!!!!\n"); } else { printf("error in mass transfer\n"); homecoming -1; } 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); 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 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 os 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\tno. of configuraions : %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 lenght if(e == 0) { printf("\nreceived: "); printf("%c",my_string1[i]); //will read string lcp2148 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; }

bulk libusb data-transfer-objects

No comments:

Post a Comment