免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2481 | 回复: 2
打印 上一主题 下一主题

加载驱动出错!!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-26 20:53 |只看该作者 |倒序浏览
我写了一个测试使用的字符驱动如下:
###################################################hello.c
#include <linux/version.h>
#include <linux/config.h>

#if CONFIG_MODVERSIONS == 1
#define MODVERSIONS
#include <linux/modversions.h>
#endif

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/wait.h>
#include <linux/slab.h>
#include <linux/sched.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/ioctl.h>
#include "hello.h"


struct  file_operations   hello_fops =
{
        .owner           = THIS_MODULE,
        .llseek             = hello_llseek,
        .open            = hello_open,
        .release         = hello_release,
};

loff_t hello_llseek(struct file * filp, loff_t off, int whence)
{
        return 0 ;
}

/*Open方法*/
int  hello_open(struct inode *inode, struct  file  *filp)
{
        printk("hello: open  /dev/hello!!!\n") ;
        try_module_get(THIS_MODULE) ;
        return 0 ;       
}

/*Release 方法*/
int  hello_release(struct inode *inode, struct  file  *filp)
{
        printk("hello: close /dev/hello!!!\n") ;
        module_put(THIS_MODULE) ;
        return 0 ;       
}

static  int  __init  my_init_function(void)
{
        int       result, err ;
        /*注册驱动设备主,次设备号*/
        if(hello_major)
        {
                /*指定主设备号*/
                hello_dev = MKDEV(hello_major,  hello_minor) ;
                result =  register_chrdev_region(hello_dev,  1,  "Hello") ;
        }
        else
        {
                /*动态分配主设备号*/
                result = alloc_chrdev_region(&hello_dev, hello_minor, 1, "Hello") ;
                hello_major = MAJOR(hello_dev) ;
        }
        if(result < 0)
        {
                printk(KERN_ERR "hello: can't  get  major %d\n", hello_major) ;
                return -1 ;
        }
       
        /*注册字符设备驱动程序*/
        phello_cdev = cdev_alloc() ;
        if(phello_cdev != NULL)
        {
                cdev_init(phello_cdev,  &hello_fops) ;
                phello_cdev->ops = &hello_fops ;
                phello_cdev->owner = THIS_MODULE ;
                err = cdev_add(phello_cdev,  hello_dev,  1) ;
                if(err)
                {
                        printk(KERN_NOTICE "Error %d adding hello_cdev", err);       
                }
                else
                {
                        printk("Success adding hello_cdev!\n") ;
                }
        }
        else
        {
                printk(KERN_ERR "hello: Register char hello_dev error\n") ;
                return -1 ;
        }
        return  0 ;
}

static  void  __exit   my_cleanup_function(void)
{
        unregister_chrdev_region(hello_dev,  1) ;
}

module_init(my_init_function) ;
module_exit(my_cleanup_function) ;


MODULE_AUTHOR("CHENHUI") ;
MODULE_DESCRIPTION("Test") ;
MODULE_LICENSE("GPL") ;

###########################################################hello.h
#ifndef   _HELLO_H_
#define   _HELLO_H_

#define MYDRIVER_NAME "Hello"
#undef MYDRIVER_MAJOR
#ifndef MYDRIVER_MAJOR
#define MYDRIVER_MAJOR 0   /* dynamic major by default */
#endif


loff_t   hello_llseek (struct file *filp, loff_t off, int whence);
int       hello_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
int       hello_open(struct inode *inode, struct file *filp);
int       hello_release(struct inode *inode, struct file *filp);

extern struct file_operations hello_fops;


struct  cdev   *phello_cdev ;               //字符设备的节点
dev_t   hello_dev ;                              //驱动设备号的节点

/*给主,次设备号赋值*/
int  hello_major  =  MYDRIVER_MAJOR;
int  hello_minor = 0 ;
#endif

*******************************************************************************
我的问题如下:
1.我编译的时候使用的是make -C /usr/src/linux SUBDIRS=$PWD modules,第一次运行这个命令的时候,为什么会编译内核?我按照《linux 设备驱动 第三版》上的写法make -C /usr/src/linux M ='pwd' modules  结果只会编译内核,我的目录下根本没有任何新的文件(*.ko  *.mod.o等等)生成?
2.当我使用make -C /usr/src/linux SUBDIRS=$PWD modules编译成功后,运行#insmod hello.ko后,我使用dmesg看到系统提示:“no version for"struct_module" found:kernel tainted”,请问这是怎么回事?
3.最后一个让我很郁闷的问题:第一次运行#insmod hello.ko后,察看/proc/devices里面还是有我的设备的,设备号是252,然后我运行:#rmmod hello.ko.然后再次运行:#insmod hello.ko,结果系统就抱错了:我对照代码。是我的cdev_add函数出错了,错误号是-17,请问怎么解决?


谢谢大家!请大家指点一下。十分感激!!!!

论坛徽章:
5
2 [报告]
发表于 2006-04-27 12:49 |只看该作者
google
你的问题太常见, 怎不自己找一下答案

论坛徽章:
0
3 [报告]
发表于 2006-04-28 11:24 |只看该作者
google
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP