编写Linux驱动常见错误
来源:互联网 发布:mac怎么查找文件路径 编辑:程序博客网 时间:2024/05/16 10:22
转自http://neilengineer.cublog.cn/
工作中遇到的编写Linux驱动的常见错误和注意事项整理。
1、init函数返回值问题,exit函数不需要返回值
2、头文件不存在的问题
3、缺少某些符号如 “}” “;”
4、模块调用内核中函数时的编译
5、No such device or address
6、对函数固定格式的错误使用
问题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.hhas 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
1)函数实现时,EXPORT_SYMBOL
用EXPORT_SYMBOL可以将一个函数以符号的方式导出给其他模块使用
2)模块中include此函数的头文件
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);//MKDEV是将主设备号和次设备号转换成dev_t类型的一个内核函数。
#define MKDEV(major,minor) (((major) << MINORBITS) | (minor))
result = cdev_add(my_cdev, devno, CDEV_NUM);用gcc编译c程序的时候 经常会出现
implicit declaration of function '...' 的warning
偶经过这几天的经验,发现主要有2种情况会产生这种warning
1
2
第二种情况如此:
camif_fsm.c中的函数定义:
void camif_start_c_with_p(camif_cfg_t *cfg, camif_cfg_t *other)
{
// cfg->gc->other = get_camif(CODEC_MINOR);
cfg->gc->other = other;
camif_start_p_with_c(cfg);
}
该函数的声明未包含在camif.h文件中,这样在调用此函数时由于camif_start_p_with_c(cfg)函数的定义在此函数之前,故产生此错误:warning: implicit declaration of function `camif_start_c_with_p'
6、对函数固定格式的错误使用
1、warning: function declaration isn't a prototype
这种错误也是很常见的,主要是对一些函数固定格式的错误使用。比如int function()改称int function(void)
static int xxxx_init(void)记住不要缺少static,而static void xxxx_exit(void)阅读全文
0 0
- 编写Linux驱动常见错误
- 编写Linux驱动常见错误
- Linux 驱动调试错误常见关键字搜索
- Linux 驱动常见错误返回值
- [Linux]编写Linux驱动常见陷阱及解决办法
- 编写Java常见错误
- 驱动编写错误解决
- linux-2.6.32 norflash 驱动编写 "probe fail"错误解决之道
- Linux驱动程式编写
- 编写Linux设备驱动
- Linux驱动编写入门
- 如何编写linux驱动
- Linux按键驱动编写
- Linux编写驱动模块
- Linux驱动编写经验
- linux 简单驱动编写
- linux常见错误解决方案
- linux 安装常见错误
- UITableView收起键盘
- django爬坑之旅之urls的配置
- 科普!!!软件工程中的xp指的是什么?
- LSH之simHash算法
- 中文分词词性对照表
- 编写Linux驱动常见错误
- leetcode刷题日记—— Longest Continuous Increasing Subsequence
- 百度网盘
- 【学习笔记】nodejs之hello world~
- Java基础知识-第一篇
- Qualifier使用
- webpack2学习笔记<演示示例>
- 一、Java窗体设置
- 用反射改变属性的值