Writing device drivers in Linux: A brief tutorial - Compiling Errors and Warnings

The article:

Writing device drivers in Linux: A brief tutorial

Is a great starting point for Linux Kernel Development but it needs some simple updates. The first error you will find when compiling is:


/home/peter/devel/kdpeter/2012/bricks/original-post/memory.c:3:26: fatal error: linux/config.h: No such file or directory compilation terminated.


To fix, remove the line from the source code:
#include <linux/config.h>
Then some warnings:
/home/peter/devel/kdpeter/2012/bricks/original-post/memory.c:28:3: warning: initialization from incompatible pointer type [enabled by default]
/home/peter/devel/kdpeter/2012/bricks/original-post/memory.c:28:3: warning: (near initialization for ‘memory_fops.write’) [enabled by default]
/home/peter/devel/kdpeter/2012/bricks/original-post/memory.c: In function ‘memory_write’:
/home/peter/devel/kdpeter/2012/bricks/original-post/memory.c:110:17: warning: ignoring return value of ‘copy_from_user’, declared with attribute warn_unused_result [-Wunused-result]
/home/peter/devel/kdpeter/2012/bricks/original-post/memory.c: In function ‘memory_read’:
/home/peter/devel/kdpeter/2012/bricks/original-post/memory.c:94:15: warning: ignoring return value of ‘copy_to_user’, declared with attribute warn_unused_result [-Wunused-result]

To fix the warning with the "memory_write function" change the line from:
ssize_t memory_write(struct file *filp, char *buf, size_t count, loff_t *f_pos);
To:
ssize_t memory_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos);
And change the line from:
ssize_t memory_write( struct file *filp, char *buf, size_t count, loff_t *f_pos) {
To:
ssize_t memory_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos) {
To fix the warnings about ignoring return value of "copy_from_user" and "copy_to_user", create a long inside both memory_read() and memory_write() to store the return value of copy_to/from_user.
ssize_t memory_read(struct ... )
{
        long ret;
        ret = copy_to_user(...);
        ...
}
ssize_t memory_write(struct ...)
{
        long ret;
        ...
        ret = copy_from_user(...);
        ...
}

Trying to compile again, other warning will be shown:
 /home/peter/devel/kdpeter/2012/bricks/original-post/memory.c:112:6: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
The error is on the line:
        tmp=buf+count-1;
buf is defined as "const char *buf" while tmp is defined as "char *tmp". Assigning const char * to char * can be considered an error. To fix it, make tmp also const char *. From:
   char *tmp;
To:
        const char *tmp;
The updated version of the source code is available here

Popular posts from this blog

Adding a serial console to the Buffalo Air Station WZR-1750DHP

How to tell rpmbuild the number of simultaneous jobs? (make -j)

Buffalo WZR-1750DHP + OpenWRT