使用内核版本-linux-3.2
错误1:
- make -C /lib/modules/3.2.0/build M=/home/wrk/works/drivers/ldd3/examples/scullc LDDINC=/home/wrk/works/drivers/ldd3/examples/scullc modules
- make[1]: Entering directory `/home/wrk/works/drivers/linux-kernel/linux-3.2'
- scripts/Makefile.build:49: *** CFLAGS was changed in "/home/wrk/works/drivers/ldd3/examples/scullc/Makefile". Fix it to use ccflags-y. Stop.
- make[1]: *** [_module_/home/wrk/works/drivers/ldd3/examples/scullc] Error 2
- make[1]: Leaving directory `/home/wrk/works/drivers/linux-kernel/linux-3.2'
- make: *** [modules] Error 2
错误2:
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:52:1: error: unknown type name ‘kmem_cache_t’
这是因为kmem_cache_t结构被换成了kmem_cache。而kmem_cache定义在<linux/slab_def.h>和<linux/slub_def.h>。但是你并不需要包含这两个文件中的一个。因为<linux/slab.h>会根据CONFIG_SLUB等来判断具体包含<linux/slab_def.h>,还是<linux/slub_def.h>。故例程中需要将
kmem_cache_t *scullc_cache;
改为:
struct kmem_cache *scullc_cache;
错误3:
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c: In function ‘scullc_defer_op’:
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:437:54: error: macro "INIT_WORK" passed 3 arguments, but takes just 2
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:437:2: error: ‘INIT_WORK’ undeclared (first use in this function)
- INIT_WORK(&stuff->work, scull_do_deferred_op, stuff);
- INIT_WORK(&stuff->work, scull_do_deferred_op);
并且将scullc_do_deferred_op函数改为
- static void scullc_do_deferred_op(struct work_struct *p)
- {
- struct async_work *stuff = container_of(p, struct async_work, work);
- aio_complete(stuff->iocb, stuff->result, 0);
- kfree(stuff);
- }
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:467:2: error: unknown field ‘ioctl’ specified in initializer
错误5:
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:558:4: error: too many arguments to function ‘kmem_cache_create’
struct kmem_cache *kmem_cache_create(const char *, size_t, size_t, unsigned long, void (*)(void *));故调用它的时候应该将参数做出相应的改变。需要将最后一个NULL去掉。
下面来解决warning:
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c: In function ‘scullc_defer_op’:
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:437:2: warning: assignment from incompatible pointer type [enabled by default]
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:438:2: warning: passing argument 1 of ‘schedule_delayed_work’ from incompatible pointer type [enabled by default]
- include/linux/workqueue.h:363:12: note: expected ‘struct delayed_work *’ but argument is of type ‘struct work_struct *’
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c: At top level:
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:467:2: warning: initialization from incompatible pointer type [enabled by default]
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:467:2: warning: (near initialization for ‘scullc_fops.unlocked_ioctl’) [enabled by default]
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:470:2: warning: initialization from incompatible pointer type [enabled by default]
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:470:2: warning: (near initialization for ‘scullc_fops.aio_read’) [enabled by default]
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:471:2: warning: initialization from incompatible pointer type [enabled by default]
- /home/wrk/works/drivers/ldd3/examples/scullc/main.c:471:2: warning: (near initialization for ‘scullc_fops.aio_write’) [enabled by default]
- struct async_work {
- struct kiocb *iocb;
- int result;
- struct work_struct work;
- };
- struct async_work {
- struct kiocb *iocb;
- int result;
- struct delayed_work delayed_work;
- }
- INIT_WORK(&stuff->work, scull_do_deferred_op, stuff);
- schedule_delayed_work(&stuff->work, HZ/100);
- INIT_DELAYED_WORK(&(stuff->delayed_work), scullc_do_deferred_op);
- schedule_delayed_work(&(stuff->delayed_work), HZ/100);
(我的环境是 linux-3.5,部分适用,但还有以下的问题出错 )
1、main.c:652: error: implicit declaration of function ‘init_MUTEX’
该错误是因为init_MUTEX函数再新到内核中已经被废弃,不再使用,改成sema_init函数。因为原来到 init_MUTEX的实现里面调用到也是这个函数。
/*init_MUTEX(&scull_devices[i].sem);改成下面那样即可*/
sema_init(&scull_devices[i].sem,1);
是说SPIN_LOCK_UNLOCKED不被认识了,其实也是被废弃了,改用DEFINE_SPINLOCK即可,如
3. 另外,在编译成功后,运行 sudo ./scull_load 时会出错:
/*static spinlock_t scull_w_lock = SPIN_LOCK_UNLOCKED; 改成下面到方式*/
static DEFINE_SPINLOCK(scull_w_lock);
awk: line 1: syntax error at or near ==mknod: "0" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。mknod: "1" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。mknod: "2" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。mknod: "3" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。chgrp: 无法访问"/dev/scull[0-3]": 没有那个文件或目录chmod: 无法访问"/dev/scull[0-3]": 没有那个文件或目录mknod: "4" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。mknod: "5" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。mknod: "6" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。mknod: "7" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。chgrp: 无法访问"/dev/scullpipe[0-3]": 没有那个文件或目录chmod: 无法访问"/dev/scullpipe[0-3]": 没有那个文件或目录mknod: "8" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。chgrp: 无法访问"/dev/scullsingle": 没有那个文件或目录chmod: 无法访问"/dev/scullsingle": 没有那个文件或目录mknod: "9" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。chgrp: 无法访问"/dev/sculluid": 没有那个文件或目录chmod: 无法访问"/dev/sculluid": 没有那个文件或目录mknod: "10" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。chgrp: 无法访问"/dev/scullwuid": 没有那个文件或目录chmod: 无法访问"/dev/scullwuid": 没有那个文件或目录mknod: "11" 后缺少操作数请尝试执行"mknod --help"来获取更多信息。chgrp: 无法访问"/dev/scullpriv": 没有那个文件或目录chmod: 无法访问"/dev/scullpriv": 没有那个文件或目录spark@spark-Dell:~/LDD3-ex/examples/scull$ ls /dev/scull*/dev/scull /dev/scullpipe
从第一行可看出,在运行 awk 命令时就已经出错,导致最后无法创建 scull0~3 ,解决办法:
在 scull_load 中,把
major=$(awk "\\$2==\"$module\" {print \\$1}" /proc/devices)
改为
major=$(awk "\$2==\"$module\" {print \$1}" /proc/devices)
至此,编译及加载都已OK
spark@spark-Dell:~/LDD3-ex/examples/scull$ sudo ./scull_load
insmod: error inserting './scull.ko': -1 File exists
spark@spark-Dell:~/LDD3-ex/examples/scull$ sudo ./scull_unload
spark@spark-Dell:~/LDD3-ex/examples/scull$ sudo ./scull_load
spark@spark-Dell:~/LDD3-ex/examples/scull$ ls /dev/scull*
/dev/scull /dev/scull2 /dev/scullpipe0 /dev/scullpipe3 /dev/sculluid
/dev/scull0 /dev/scull3 /dev/scullpipe1 /dev/scullpriv /dev/scullwuid
/dev/scull1 /dev/scullpipe /dev/scullpipe2 /dev/scullsingle
spark@spark-Dell:~/LDD3-ex/examples/scull$ ls /proc/scull*
/proc/scullmem /proc/scullpipe /proc/scullseq