Skip to content
Advertisement

Error initialization from incompatible pointer of IOCTL function in Linux kernel 4.8.0-53-generic Linux Mint 64 bit

I’ve got an error while writing a char device module, using Ioctl command.

static struct file_operations my_fops =
{
    .unlocked_ioctl = my_ioctl, error is here. I can not fix this.
};

Note: please ignore all my print_k.

Please, help me fix this. My thanks to all of you.

Here is my code :

static long my_ioctl(struct file *f,unsigned int cm,unsigned long arg[b]) 
{  
    int re; 
    unsigned long arg[3];

    switch (cm) 
    { 
        case H_ADD:          

          arg[2] = arg[0] + arg[1];  
          print_k("Driver:Calculating is complete,Result = %d n",arg[2]); 
        break;  
        case H_SUB: 
          print_k ("Driver: Start ...n"); 
          arg[2] = arg[0] - arg[1];  
          print_k("Driver:Calculating is complete,Result = %d n",arg[2]);
        break; 
        case H_MULL:  
          print_k ("Driver: Start ...n");  
          arg[2] = arg[0] * arg[1]; 
          print_k("Driver:Calculating is complete,Result = %d n",arg[2]); 
        break; 
        case H_DIV:  
          print_k ("Driver: Start ...n");  
          arg[2] = arg[0] / arg[1]; 
          print_k("Driver:Calculating is complete,Result = %d n",arg[2]); 
        break; 
        default:  
          print_k ("Driver: I don't have this operation!n"); 
        re = -Er; 
        break; 
        }  
    return re; 
} 

static struct file_operations my_fops =
{
    .unlocked_ioctl = my_ioctl, 
};

Advertisement

Answer

The third argument unsigned long arg[b] in the function prototype is dubious. It should be simply unsigned long arg even if it’s supposed to be a pointer. It’s easy to cast it to the type of interest within the function body.

..the optional arg argument is passed in the form of an unsigned long, regardless of whether it was given by the user as an integer or a pointer.

( Linux Device Drivers 3, Chapter 6, Section 1 )

Also, it’s wrong to declare a variable within the function body which has the same name as one of the arguments. Please choose another name for unsigned long arg[3];.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement