- 论坛徽章:
- 16
|
每次打开设备的时候就会死机 怎么回事啊
代码如下:
#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_SUCCESS 1
char test_buff[100] = {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[17] = '\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");
运行出错消息:
|
|