0
votes

The problem: I have a pointer to struct file called flip, an int called cmd and an unsigned long called arg. The private_data field in struct file points the private_data structure is defined as follows:

typedef struct {
    unsigned char key;
    int read_state;
} my_private_data;

and ioctl function is defined as follows:

int my_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{

    switch (cmd) {
        case CMD_CHANGE_KEY:
            filp->private_data->key = (char)cmd; 
            filp->private_data->read_state = (int)arg; 
            break;
        case CMD_SET_READ_STATE:
            filp->private_data->key = (char)cmd;
            filp->private_data->read_state = (int)arg;
            break;
        case CMD_ZERO:
            kfree(buff_caesar);
            break;
    }
    return 0;
}

However, when I try to compile the code, I get the following warning/error:

Warning: dereferencing 'void *' pointer.
Requesting for Member 'key' in something not a struct or union.
Warning: dereferencing 'void *' pointer.
Requesting for Member 'read_state' in something not a struct or union.

What can I do to fix this?

Here's the open method:

int my_open(struct inode *inode, struct file *filp)
{
    int minor = MINOR(inode->i_rdev);

    
    if(minor == ONE) {
        /* Caesar*/
        filp->f_op = &fops_caesar;
    }
    else if(minor == TWO){
        /*Xor*/
        filp->f_op = &fops_xor;
    }
    else return -ENODEV;

    
    my_private_data* privateData = NULL;
    privateData = kmalloc(sizeof(my_private_date),GFP_KERNEL);
    if (privateData==NULL){
        return -1;
    }
    filp->private_data = privateData;

    return 0;
}
1

1 Answers

0
votes

The struct file member .private_data is defined as a void *, so filp->private_data itself has no members. If you want to store data items to your structure, you could access them with a local of that type; eg, within my_ioctl() :

my_private_data *info;
. . .

if (filp->private_data == NULL) {
    error handling
}
info = filp->private_data;
. . .
info->key = (char) cmd;
info->read_state = (int) arg;