Search code examples
linuxkernellinux-device-driverdevice-driver

Why is there an incompatible pointer type warning?


I'm writing a Linux device driver using kernel 3.13.0 and I'm confused as to why I'm getting this warning.

warning: initialization from incompatible pointer type [enabled by default]
     .read = read_proc,
     ^
warning: (near initialization for ‘proc_fops.read’) [enabled by default]

As far as I can tell my file_operations setup for the proc functions are identical to the device functions. I can read/write to /dev/MyDevice with no issue and there are no warnings. The proc write function does not throw a warning, only the read. What did I do wrong?

/*****************************************************************************/
//DEVICE OPERATIONS
/*****************************************************************************/ 
static ssize_t dev_read(struct file *pfil, char __user *pBuf, size_t
  len, loff_t *p_off)
{
    //Not relevant to this question
}

static ssize_t dev_write(struct file *pfil, const char __user *pBuf,
                         size_t len, loff_t *p_off)
{
    //Not relevant to this question
}

static struct file_operations dev_fops =
{ //None of these cause a warning but the code is identical the proc code below
    .owner = THIS_MODULE,
    .read = dev_read,
    .write = dev_write
};

/*****************************************************************************/
//PROCESS OPERATIONS
/*****************************************************************************/
static int read_proc(struct file *pfil, char __user *pBuf, size_t
              len, loff_t *p_off)
{
    //Not relevant to this question
}

static ssize_t write_proc(struct file *pfil, const char __user *pBuf,
                         size_t len, loff_t *p_off)
{
    //Not relevant to this question
}

struct file_operations proc_fops = 
{
    .owner = THIS_MODULE,
    .write = write_proc,
    .read = read_proc, //This line causes the warning.
};

EDIT: So the answer is that I'm an idiot for not seeing the "int" versus "ssize_t". Thank you everyone! Both Codenheim and Andrew Medico had the correct answer at roughly the same time but I chose Medico's because it's more pedantic and obvious for future visitors.


Solution

  • The return type of your read_proc function (which throws the warning) does not match the the function that compiles cleanly.

    static ssize_t dev_read(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off)
    

    vs.

    static int read_proc(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off)
    

    ssize_t and int may be different sizes. Your function's return type should be ssize_t.