Find the answer to your Linux question:
Results 1 to 5 of 5
Hi all, I have developed a User Space USB Device Driver for some specific device....the problem is I want to develop an Application that works on this Device Driver....Application should ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    11

    Help: USB Device Driver

    Hi all,
    I have developed a User Space USB Device Driver for some specific device....the problem is I want to develop an Application that works on this Device Driver....Application should function such that whenever I issue a
    write( ) command the write( ) function of the Driver should get executed....
    Thus I want to some how incorporate the functioning similar to Structure file_operations in my User Space Driver so that proper mapping can be done...

    Please help me in how to incorporate this function into my driver ????

    Regards
    Ameya Verma

  2. #2
    Just Joined!
    Join Date
    Jun 2008
    Posts
    39
    Will you post the relevant parts of your USB driver code and you USB application code?

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    11
    Quote Originally Posted by felPmy View Post
    Will you post the relevant parts of your USB driver code and you USB application code?
    hi this is the code along with the comments.....I want to write an application on this which will issue commands like write( ) and these commands will ultimately invoke the write function of the driver......currently I have just given USB_BULK_WRITE( )....

    #include <stdio.h>
    #include <string.h>
    #include <usb.h>
    #include<stdlib.h>
    #include<ctype.h>

    #define TEST_VENDOR_ID 0xffff
    #define TEST_PROD_ID 0xffff

    static struct usb_device *dev_init(void)
    {
    struct usb_bus *usb_bus;
    struct usb_device *dev;

    usb_init();
    usb_find_busses();
    usb_find_devices();

    for (usb_bus = usb_busses;usb_bus;usb_bus = usb_bus->next)
    {
    for (dev = usb_bus->devices;dev;dev = dev->next)
    {
    if ((dev->descriptor.idVendor == TEST_VENDOR_ID) && (dev->descriptor.idProduct == TEST_PROD_ID))
    return dev;
    }
    }

    return NULL;
    }

    int main()
    {
    struct usb_device *usb_dev;
    struct usb_dev_handle *usb_test_handle;
    int chk_set_config = 0;
    int chk_set_altint = 0;
    int chk_claim_interface = 0;
    int chk_rel_dri = 0;
    int config_val = 0;
    int chk_direction = 0;
    int bit_to_mask = 0;

    /********** FOR THE BULK WRITE *****************/
    int chk_bulk_write = 0;
    char *bulk_write;
    int end_pt_add_write = 0;

    /********** FOR THE BULK READ*****************/
    int chk_bulk_read = 0;
    char *bulk_read;
    int end_pt_add_read = 0;

    /********** TO RESET THE BULK IN ENDPOINT ************/
    int chk_rst_ep_in = 0;

    /************************************************** ****/
    struct usb_interface_descriptor *interface_desc;


    /************************************************** ****/
    /************ FOR THE DRIVERS NAME ********************/
    char *dri_name;
    int chk_dri_name = 0;
    /************************************************** *****/
    /***************** CHOICE ******************************/
    int choice = 0;
    printf("PLZ ENTER THE CHOICE\n");
    scanf("&#37;d",&choice);
    if(choice == 1){
    dri_name = (char *)malloc(20*sizeof(char));

    /********** ALLOCATING SPACE TO THE BUFFERS **********/
    bulk_write = (char *)malloc(10*sizeof(char));
    bulk_read = (char *)malloc(256*sizeof(char));
    /**********************************************/

    usb_dev = dev_init();
    if (usb_dev == NULL) {
    printf("Device not found \n");
    return -1;
    }
    usb_test_handle = usb_open(usb_dev);
    if (usb_test_handle == NULL) {
    printf("ERROR in USB_HANDLE \n");
    return -1;
    }
    chk_claim_interface = usb_claim_interface(usb_test_handle,0);

    if(chk_claim_interface < 0){
    printf("CHK VALUE %d\n",chk_claim_interface);
    printf("ERROR IN CLAIMING THE INTERFACE\n");
    return -1;
    }

    /********* NOW RETRIEVING THE DRIVERS NAME *************************************************/
    chk_dri_name = usb_get_driver_np(usb_test_handle,usb_dev->config[0].interface[0].altsetting[0].bInterfaceNumber,dri_name,20);
    printf("DRIVER NAME %s\n",dri_name);
    if(chk_dri_name){
    printf("CHK DRI NAME VALUE %d\n",chk_dri_name);
    printf("ERROR IN DRIVER NAME\n");
    printf("ERROR %s\n",usb_strerror());
    }
    /***************** CODE TO TRANSFER THE DATA ***********************************/

    /***************** INITIALIZING THE WRITE BUFFER ***********************************/
    *bulk_write = 0x61;
    *(bulk_write+ 1) = 0x62;
    *(bulk_write+ 2) = 0x63;
    *(bulk_write+ 3) = 0x64;
    *(bulk_write+ 4) = 0x65;
    *(bulk_write+ 5) = 0x0D;

    /***************** SETTING THE END POINT ADDRESS FOR THE WRITE OPERATION ***********/
    end_pt_add_write = usb_dev->config[0].interface[0].altsetting[0].endpoint[1].bEndpointAddress;

    /***************** SETTING THE END POINT ADDRESS FOR THE READ OPERATION ***********/
    end_pt_add_read = usb_dev->config[0].interface[0].altsetting[0].endpoint[0].bEndpointAddress;

    /***************** NOW CARRYING OUT THE BULK WRITE OPERATION ***********************/
    chk_bulk_write = usb_bulk_write(usb_test_handle,end_pt_add_write,bu lk_write,6,100);

    if(chk_bulk_write > 0){
    printf("NO OF BYTES WRITTEN -- %d\n",chk_bulk_write);
    }
    else
    {
    printf("ERROR IN WRITING \n");
    printf("ERROR--%s\n",usb_strerror());
    fclose(fp);
    usb_release_interface(usb_test_handle,0);
    usb_close(usb_test_handle);
    printf("AFTER RELEASING THE INTERFACE INSIDE THE EROOR IN WRITE \n");
    return -1;
    }

    /***************** END OF BULK WRITE OPERATION ************************************/
    /************************************************** ********************************/

    /***************** NOW CARRYING OUT THE BULK READ OPERATION ***********************/

    /***************** FIRST RESETTING THE BULK IN ENDPOINT ***************************/
    chk_rst_ep_in = usb_resetep(usb_test_handle,end_pt_add_read);
    if(chk_rst_ep_in){
    printf("ERROR IN RESETTING THE END POINT\n");
    printf("ERROR %s\n",usb_strerror());
    }

    chk_bulk_read = usb_bulk_read(usb_test_handle,end_pt_add_read,bulk _read,256,100);

    printf("THE READ BUFFER -%s-\n",bulk_read);

    if(chk_bulk_read > 0){
    printf("NO OF BYTES READ -- %d\n",chk_bulk_read);
    }
    else
    {
    printf("ERROR IN READING \n");
    printf("ERROR--%s\n",usb_strerror());
    fclose(fp);
    usb_release_interface(usb_test_handle,0);
    usb_close(usb_test_handle);
    printf("AFTER RELEASING THE INTERFACE \n");
    return -1;
    }

    /***************** END OF BULK WRITE OPERATION ************************************/
    /************************************************** ********************************/

    /***************** ENTERING THE BUFFER READ INTO THE FILE /home/testusr1/file1.txt *******/
    fputs(bulk_read,fp);
    }
    choice = 0;
    printf("ENTER THE CHOICE AGAIN\n");
    scanf("%d",&choice);

    if(choice == 2){
    /***************** NOW CLOSING THE FILE AND RELEASING ALL THE RESOURCES ******************/
    fclose(fp);
    usb_release_interface(usb_test_handle,0);
    printf("AFTER RELEASING THE INTERFACE \n");
    usb_close(usb_test_handle);
    printf("EXITTING \n");
    }
    }

  4. #4
    Just Joined!
    Join Date
    Jun 2008
    Posts
    39
    Will you explain more about what you would like to do?
    "I want to write an application on this which will issue commands like write( ) and these commands will ultimately invoke the write function of the driver......currently I have just given USB_BULK_WRITE( )...."
    Are you writing a USB driver that is a loadable kernel module that you want to load and then also want to write an application that will use that driver? What kernel are you writing this for, use shell command "uname -r"? What USB device is it for?

  5. #5
    Just Joined!
    Join Date
    Apr 2008
    Posts
    11
    Hi,
    First of all I am using kernel version 2.6.9....
    Secondly I dont want to develop any loadable Kernel Module.....I have this driver(above written code) that carries out read & write operations on the specific device.
    Now I want to develop an Application that will access this Driver and carry out the Read & Write Operations.
    Ex :-
    My Application would work like this...

    check_read = read("FILENAME',options,options)

    and then this command will invoke some read( ) function of the driver.
    ex :- (Drivers Read Function)
    test_read()
    {
    usb_bulk_read();}

    I want to map this call from the application to the Driver......How can I do it ????
    Please do reply
    NOTE:- If you have developed a Kernel Space Driver then my requirement is some what similar to the struct file_operations structure ie I require some structure or something which will map calls to the Driver....I only need to issue the read( ) command...

    Regards
    Ameya Verma

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...