5.1、cdev
struct cdev *my_cdev = cdev_alloc();
void cdev_init(struct cdev *cdev, const struct file_operations *fops);
int cdev_add(struct cdev *dev, dev_t num, unsigned count);
void cdev_del(struct cedv *);
5.2、示例代码
#include<linux/module.h>
#include<linux/init.h>
#include<linux/kdev_t.h>
#include<linux/fs.h>
#include<linux/cdev.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("ZZX");
static int major = 230;
static int minor = 0;
static dev_t devno;
static struct cdev cdev;
static int hello_open(struct inode *inode, struct file *ifilep)
{
printk("hello open\n");
return 0;
}
static struct file_operations hello_ops =
{
.open = hello_open,
};
static int hello_init(void)
{
int result;
int error;
printk("hello_init\n");
devno = MKDEV(major, minor);
result = register_chrdev_region(devno, 1, "dev_test");
if(result < 0){
printk("register_chrdev_region fauil\n");
return result;
}
cdev_init(&cdev, &hello_ops);
error = cdev_add(&cdev, devno, 1);
if(error < 0)
{
printk("cdev_add fail\n");
unregister_chrdev_region(devno,1);
return error;
}
return 0;
}
static void hello_exit(void)
{
printk("hello_exit\n");
cdev_del(&cdev);
unregister_chrdev_region(devno,1);
return;
}
module_init(hello_init);
module_exit(hello_exit);
mknode /dev/test c 230 0