implementing ioctl's for device drivers

From: sapna (sapna.nagaraj_at_gmail.com)
Date: 04/29/05

  • Next message: sbj: "You can check out what we have as reference kernels for our boards"
    Date: 29 Apr 2005 08:52:12 -0700
    
    

    hi,

    thanks for the help.. im inserting the code ive used for the simple
    ioctl.

    //chardev.c
    /*all necessary header files included*/

    #include chardev.h /*header file for ioctl*/

      static int Device_open=0;
      static char Message[80];
      static char *msg_ptr;
     
      static int device_open(struct inode *inode, struct file *file)
    {
      if(Device_open)
          return -EBUSY;
       Device_open++;
       MOD_INC_USE_COUNT;
       return 0;
    }

       static void device_release(struct inode *inode, struct file *file)
    {
       Device_open--;
       MOD_DEC_USE_COUNT;
       return 0;
    }

    int device_ioctl( struct inode *inode, struct file *file,
                          unsigned int ioctl_num,
                            unsigned long ioctl_param)
    {
        int i;
        char *temp;
        temp=(char *)ioctl_param;
        get_user(i,temp);
        printk(KERN_ALERT" the value passed from user space is %d\n",i);
    }

    struct file_operations Fops = {
                                      .open=device_open,
                                      .release=device_release,
                                      .ioctl=device_ioctl
                                   };
    // then ive written the init & cleanup functions..//

    now the user space program
    //ioctl.c

    //included fcntl.h,unistd.h,sys/ioctl.h & chardev.h

    ioctl_set_length( int file_desc, int *pt)
    {
       int ret_val = ioctl(file_desc, IOCTL_SET_LENGTH, pt);
        if(ret_val < 0)
         {
           printf("ioctl_set_length failed");
              exit(-1)
          }
    }

    main()
    {
      int file_desc, ret_val;
       int pass;
       int *p;
       p= &pass;
       printf("enter val to be passed");
       scanf("%d",&pass);
        file_desc=open("chardev",0);
         if(file_desc<0)
         {
           printf("cant open device file %s","chardev");
             exit(-1);
          }
    ioctl_set_length(file_desc,p);
    close(file_desc);
    }

    // now the chardev.h header file with ioctl definitions

    //chardev.h

    #include <linux/ioctl.h>
    #define IOCTL_SET_LENGTH _IOWR(majornum, 0, int *)

    we did try out using copy_from_user but it gave no different results.
    our code is still not working. the problem remains the value of
    ioctl_param. its not the same as address of the variable "pass" in
    ioctl.c(user space)
    get_user is returning a -ve value.
    what could be the problem?


  • Next message: sbj: "You can check out what we have as reference kernels for our boards"

    Relevant Pages