tc1989tc 发表于 2013-05-04 21:33

自己写的驱动 出现问题望指导

每次打开设备的时候就会死机怎么回事啊
代码如下:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#define TEST_FAULT   0
#define TEST_SUCCESS1

char test_buff = {0};

int test_open(struct inode *inode, struct file *filp)
{
       
        printk(KERN_NOTICE"open test chrdev\n");
        printk(KERN_NOTICE"%s\n", test_buff);
       
        return TEST_SUCCESS;
}

int test_close(struct inode *inode, struct file *filp)
{
        printk(KERN_NOTICE"close test chrdev\n");
        printk(KERN_NOTICE"%s\n", test_buff);
        return TEST_SUCCESS;
}

int test_read(struct file *filp, char *buff, int count, loff_t *offp)
{
        int len = 0;
        len = copy_to_user(buff, test_buff, 7);
        return len;
}

int test_write(struct file *filp, char *buff, int count, loff_t *offp)
{
        int len = 0;
        len = copy_from_user(yun, buff, 7);
        return len;
}

struct file_operations test_fops = {
        .owner = THIS_MODULE,
       
        .read = test_read,
        .write = test_write,
       
        .open = test_open,
        .release = test_close,
        };
static int cdev_test_init(void)
{
        dev_t num_cdev;
        int major = 789, minor = 0;
        struct cdev *test_cdev = NULL;
        num_cdev = MKDEV(major, minor);
        register_chrdev_region(num_cdev, 1, "cdev_all");
        memcpy(test_buff, "hello world haha", 16);
        test_buff = '\0';
        //分配设备
       
        test_cdev = cdev_alloc();
        if(NULL == test_cdev)
        {
                printk(KERN_NOTICE"cdev alloc fault\n");
                return TEST_FAULT;
        }
        cdev_init(test_cdev,&test_fops);
        test_cdev->owner = THIS_MODULE;
        test_cdev->ops = &test_fops;
        //注册设备
        if(cdev_add(test_cdev, num_cdev, 1))
        {
                printk(KERN_NOTICE"cdev add fualt\n");
                return TEST_FAULT;
        }


        return TEST_SUCCESS;
}

static int cdev_test_exit(void)
{
        printk(KERN_NOTICE"%s",test_buff);
        return TEST_SUCCESS;
}

module_init(cdev_test_init);
module_exit(cdev_test_exit);
MODULE_LICENSE("GPL");
运行出错消息:


tc1989tc 发表于 2013-05-04 21:35

回复 1# tc1989tc


    在用kgdb调试的时候,,,好像提示的是在内核调用chr_open系统函数的时候 报的段错误。。。。但是我看不来是什么问题

井蛙夏虫 发表于 2013-05-04 22:23

我对驱动程序只了解一点点,对于你的程序,我只能说一下几处疑惑,希望对你有帮助。
1.驱动程序不能调用memcpy在用户空间和内核空间之间拷贝,而应该用copy_from_user和copy_to_user代替。而你的程序在init方法中调用了,难道在init方法中可以?
2.一般系统调用返回0表示成功,而你将SUCCESS定义为1,FAULT定义为0,是否表示初始化失败了?
3.既然调用了cdev_alloc,为什么又调用cdev_init?

tc1989tc 发表于 2013-05-04 23:03

回复 3# 井蛙夏虫

谢谢哥们   在init初始化中用memcpy不是从用户空间复制到内核空间 所以可以这么用
问题就是出在你说的返回值是0和1 的那点   
但是还是还是没搞懂返回相反的值会造成内核段错误真心伤不起啊
希望还有懂的朋友继续指导哈
   
页: [1]
查看完整版本: 自己写的驱动 出现问题望指导