免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: yyykkk1229
打印 上一主题 下一主题

如何编写LINUX设备驱动程序 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-08-07 10:18 |显示全部楼层 |倒序浏览
5可用积分
http://www.it.com.cn/f/edu/0411/30/53811.htm
按照上面连接的范例想要编写设备驱动程序
得到如下错误,请教高手该如何修改
test.c:13: warning: `struct file' declared inside parameter list
test.c:13: warning: its scope is only this definition or declaration, which is probably not what you want
test.c:13: warning: `struct inode' declared inside parameter list
test.c: In function `read_test':
test.c:18: `VERIFY_WRITE' undeclared (first use in this function)
test.c:18: (Each undeclared identifier is reported only once
test.c:18: for each function it appears in.)
test.c:21: parse error before "for"
test.c: At top level:
test.c:30: warning: `struct file' declared inside parameter list
test.c:30: warning: `struct inode' declared inside parameter list
test.c:35: warning: `struct file' declared inside parameter list
test.c:35: warning: `struct inode' declared inside parameter list
test.c: In function `open_tibet':
test.c:37: `MOD_ING_USE_COUNT' undeclared (first use in this function)
test.c: At top level:
test.c:41: warning: `struct file' declared inside parameter list
test.c:41: warning: `struct inode' declared inside parameter list
test.c: In function `release_tibet':
test.c:43: union has no member named `usecount'
test.c: At top level:
test.c:46: variable `test_fops' has initializer but incomplete type
test.c:47: warning: excess elements in struct initializer
test.c:47: warning: (near initialization for `test_fops')
test.c:48: warning: excess elements in struct initializer
test.c:48: warning: (near initialization for `test_fops')
test.c:49: `write_test' undeclared here (not in a function)
test.c:49: warning: excess elements in struct initializer
test.c:49: warning: (near initialization for `test_fops')
test.c:50: warning: excess elements in struct initializer
test.c:50: warning: (near initialization for `test_fops')
test.c:51: warning: excess elements in struct initializer
test.c:51: warning: (near initialization for `test_fops')
test.c:52: warning: excess elements in struct initializer
test.c:52: warning: (near initialization for `test_fops')
test.c:53: warning: excess elements in struct initializer
test.c:53: warning: (near initialization for `test_fops')
test.c:54: `open_test' undeclared here (not in a function)
test.c:54: warning: excess elements in struct initializer
test.c:54: warning: (near initialization for `test_fops')
test.c:55: `release_test' undeclared here (not in a function)
test.c:55: warning: excess elements in struct initializer
test.c:55: warning: (near initialization for `test_fops')
test.c:56: warning: excess elements in struct initializer
test.c:56: warning: (near initialization for `test_fops')
test.c:57: warning: excess elements in struct initializer
test.c:57: warning: (near initialization for `test_fops')
test.c: In function `init_module':
test.c:64: parse error before '=' token
test.c:67: `KERN_INFO' undeclared (first use in this function)
test.c:67: parse error before string constant
/usr/include/linux/timer.h: At top level:
test.c:46: storage size of `test_fops' isn't known

<LINUX设备驱动>那本书的第三章看完了,但由于没有具体例子,很多地方都搞不懂,麻烦高手帮忙,谢谢!!!

论坛徽章:
0
2 [报告]
发表于 2008-08-07 10:39 |显示全部楼层
顶一下........

论坛徽章:
0
3 [报告]
发表于 2008-08-07 11:17 |显示全部楼层
hello world 只是模块的加载啊
驱动设备的加载我没有找到想对应的例子,我用的是2.4.20-8版本,可能是编写版本的冲突啊
谁能给个2.4.20-8版本的范例,最简单的就可以了,只是为了了解这个过程,谢谢

论坛徽章:
0
4 [报告]
发表于 2008-08-07 11:39 |显示全部楼层
楼上的,非常感谢你的程序,那你没有理解我的意思,模块的编译我已经会了,我需要一个驱动设备的程序,2者是不一样的,编译后的.o文件是能连接到/dev当中的,驱动设备比模块要复杂许多,不过还是非常感谢你的回答~

[ 本帖最后由 yyykkk1229 于 2008-8-7 11:40 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-08-07 13:50 |显示全部楼层
感谢,可是还是同样的错误,说出现fops时出错,跟我刚才的问题一样,我刚才设置那个文件是用的是test_fops
Major = register_chrdev(0, DEVICE_NAME, &fops);
这句话的DEVICE_NAME应该如何设置呀

论坛徽章:
0
6 [报告]
发表于 2008-08-07 14:20 |显示全部楼层
LINUXKERNEL_INSTALL_DIR=/usr/src/linux-2.4.20-8

obj-m += test.o

all:
        make -C $(LINUXKERNEL_INSTALL_DIR) M=`pwd` modules
clean:
        rm -f *.o *.ko mmtest

这是我的makefile文件,我害怕makefile编译的不对,用gcc -Wall -DMODULE -D__KERNEL__ -DLINUX -I /usr/src/linux-2.4.20-8/include test.c 这样编译也是出错的

我的开头是如下的程序
#define __NO_VERSION__

#include <linux/module.h>
#include <linux/config.h>
#include <linux/version.h>

#include <asm/uaccess.h>

#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>

有没有可能是因为我的库不包含有file_operations呀  
麻烦你了,谢谢

论坛徽章:
0
7 [报告]
发表于 2008-08-07 14:44 |显示全部楼层
那个是成功的...insmod可以加载模块

论坛徽章:
0
8 [报告]
发表于 2008-08-07 15:08 |显示全部楼层
加了,依然不行,我重新编译了一下内核模块,错误发生了改变,但依然看不懂
test.c:7:25: asm/uaccess.h: No such file or directory
In file included from /usr/include/linux/fs.h:23,
                 from test.c:10:
/usr/include/linux/string.h:8:2: warning: #warning Using kernel header in userland!
In file included from /usr/include/linux/sched.h:14,
                 from /usr/include/linux/mm.h:4,
                 from test.c:11:
/usr/include/linux/timex.h:173: field `time' has incomplete type
/usr/include/linux/timex.h:188: confused by earlier errors, bailing out

论坛徽章:
0
9 [报告]
发表于 2008-08-07 16:37 |显示全部楼层
非常感谢楼上的回答,虽然说自己还是很模糊
慢慢琢磨吧,哎...现在思路有些乱

论坛徽章:
0
10 [报告]
发表于 2008-08-08 10:26 |显示全部楼层
非常感谢dreamice的意见,可能是因为我表达的不够准确,导致了问题的复杂话,还是很感谢他的帮助
这里我总结一下吧,首先字符驱动程序最好放在/usr/src/linux/drivers/char这个文件夹的目录底下,其他的也是可以的,但所编写的程序在中间部分需要加上对字符设备注册的代码,这些我还在研究,感觉很麻烦,没必要每次都编写,继续研究中...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP