LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 639|回复: 3

register_chdev老是失败,着急啊?不明白为什么?

[复制链接]
发表于 2005-2-24 19:05:50 | 显示全部楼层 |阅读模式
源码如下,这是一个简单的内核驱动代码:
#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>


int driver_open(struct inode *i,struct file *f)
{
        printk("<1>open function.\n");
        return  0;
}

struct file_operations *fops={
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        driver_open,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
};


int init_module(void)
{
        int devnum;
        devnum=register_chrdev(40,"driver",fops);
        printk("^_^major num is %d\n",devnum);
        if(devnum)       
                {printk("<1>hello,world.\n");
                return 0;}
        else {printk("<1>error.\n"); return 0;}
}

void cleanup_module(void)
{
        unregister_chrdev(40,"driver");
        printk("<1>Goodbye,Crule world.\n");
}

MODULE_LICENSE("GPL");

我在mknod /dev/driver c 40 0之后,插入这个驱动模块,可是老是出现error,也就是register_chdev根本没有成功?
为什么啊?着急。
发表于 2005-2-27 22:25:24 | 显示全部楼层
fops最好标记化定义
struct file_operations *fops = {
{
open: driver_open
}
如果不能确定40没有被占用,最好动态分配
devnum=register_chrdev(0,"driver",fops);
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-2-28 13:06:09 | 显示全部楼层

标记化定义在编译时老是出现错误?为什么啊?

Post by dongni386
fops最好标记化定义
struct file_operations *fops = {
{
open: driver_open
}
如果不能确定40没有被占用,最好动态分配
devnum=register_chrdev(0,"driver",fops);


[root@localhost test]# make
gcc -c -O -W -DMODULE -D__KERNEL_ -I/usr/src/linux/include test.c
test.c:9: warning: `struct file' declared inside parameter list
test.c:9: warning: its scope is only this definition or declaration, which is probably not what you want
test.c:9: warning: `struct inode' declared inside parameter list
test.c:24: error: field name not in record or union initializer
test.c:24: error: (near initialization for `fops')
test.c:24: warning: initialization from incompatible pointer type
make: *** [test.o] Error 1

看错误信息好象是说,open不在file_operations的定义中,这很奇怪啊。
回复 支持 反对

使用道具 举报

发表于 2005-2-28 16:25:58 | 显示全部楼层
因为他给你的代码有错误,你不看就用了
struct file_operations *fops = {    //是个变量,而不是指针,所以要去掉"*"
{
open: driver_open
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表