工作中遇到的编写Linux驱动的常见错误和注意事项整理,将不断更新。
问题1、驱动的init函数声明错误
出错:
[root@localhost]# insmod phyinfo.ko
insmod: error inserting 'phyinfo.ko': -159951552 Success
insmod: error inserting 'phyinfo.ko': -159951552 Success
原因:
驱动实现不对:
static void phyinfo_init(void)
{
......
......
}
module_init(phyinfo_init);
解决:
phyinfo_init函数必须有返回值!如可以实现定义如下:
static int phyinfo_init(void)
{
......
......
return 0;
}
问题2、linux/config.h头文件问题
出错:
/root/source/my-drivers/chartest/chartest.c:1:26: error: linux/config.h: No such file or directory
make[2]: *** [/root/source/my-drivers/chartest/chartest.o] Error 1
make[1]: *** [_module_/root/source/my-drivers/chartest] Error 2
make[1]: Leaving directory `/root/source/linux-2.6.30.5'
make: *** [modules] Error 2
make[2]: *** [/root/source/my-drivers/chartest/chartest.o] Error 1
make[1]: *** [_module_/root/source/my-drivers/chartest] Error 2
make[1]: Leaving directory `/root/source/linux-2.6.30.5'
make: *** [modules] Error 2
原因:
The file include/linux/config.h has been removed from 2.6.19 kernel.
解决:
1、把#include <linux/config.h>修改为如下:
#ifndef _LINUX_CONFIG_H
#define _LINUX_CONFIG_H
#include <linux/autoconf.h>
#endif
#define _LINUX_CONFIG_H
#include <linux/autoconf.h>
#endif
2、直接删掉#include <linux/config.h>
3、touch一个空的linux/config.h
3、touch一个空的linux/config.h
问题3、错误现象:
/home/neil/source/my-drivers/char-simple/char-simple.c:158: error: invalid storage class for function ?._inittest?
/home/neil/source/my-drivers/char-simple/char-simple.c:158: warning: ?.lias?.attribute ignored
/home/neil/source/my-drivers/char-simple/char-simple.c:158: warning: ?.lias?.attribute ignored
原因:
源码的一个函数实现时,少了一个“}”!!
源码的一个函数实现时,少了一个“}”!!
问题4、模块调用内核中函数时的编译
如果模块A要用内核中自定义的一个函数,要如下来做:
1)函数实现时,EXPORT_SYMBOL
2)模块中include此函数的头文件
1)函数实现时,EXPORT_SYMBOL
2)模块中include此函数的头文件
3)模块编译指向的内核源码,必须编译过带此函数的内核(kernel config中有此函数),不然出错如下:
[root@localhost r8168-8.014.00-2840]# make
......
......
ERROR: Kernel configuration is invalid.
include/linux/autoconf.h or include/config/auto.conf are missing.
Run 'make oldconfig && make prepare' on kernel src to fix it.
include/linux/autoconf.h or include/config/auto.conf are missing.
Run 'make oldconfig && make prepare' on kernel src to fix it.
WARNING: Symbol version dump /work/zhaoweixing/linux-2.6.30-kdm201s/Module.symvers
is missing; modules will have no dependencies and modversions.
4)如果编译过的内核中,没有把调用的函数编译进去,会出如下信息(我调用的函数是set_eth_link_led):
WARNING: "set_eth_link_led" [/work/zhaoweixing/new-r8168/e2prom-less/r8168-8.014.00-2840/src/r8168.ko] undefined!
问题5、出错现象,运行测试程序出错如下:
[root@localhost my-drivers]# ./test
open: No such device or address
[root@localhost my-drivers]# ./test
open: No such device or address
分析:
驱动写好并编译后的操作:
1)insmod mydriver.ko
2)mknod /dev/chartest c 249 0
3)编写测试程序
$cat test-user.c
#define DEVICE "/dev/chartest"
int main()
{
int fd=0;
fd=open(DEVICE,O_RDWR);
if(fd<0)
perror("open");
close(fd);
return 0;
}
驱动写好并编译后的操作:
1)insmod mydriver.ko
2)mknod /dev/chartest c 249 0
3)编写测试程序
$cat test-user.c
#define DEVICE "/dev/chartest"
int main()
{
int fd=0;
fd=open(DEVICE,O_RDWR);
if(fd<0)
perror("open");
close(fd);
return 0;
}
解决:在驱动的初始化函数中:
cdev_add(my_cdev, 0, CDEV_NUM); 中间参数不对,修改为如下:
dev_t devno=MKDEV(chartest_major,chartest_minor+0);
result = cdev_add(my_cdev, devno, CDEV_NUM);
result = cdev_add(my_cdev, devno, CDEV_NUM);