字符设备驱动框架
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#define VSER_MAJOR 256
#define VSER_MIINOR 0
#define VSER_DEV_CNT 1
#define VSER_DEV_NAME "vser"
static char[255] ibuf;
static int ibuf_open(struct inode *inode, struct file *filp){
for (int i = 0; i < 255; i++){
ibuf[i] = '\0';
}
return 0;
}
static int ibuf_release(struct inode *inode, struct file *filp){
for (int i = 0; i < 255; i++){
ibuf[i] = '\0';
}
return 0;
}
static ssize_t ibuf_read(struct file *filp, char __user *buf, size_t count, loff_t *pos){
}
static ssize_t ibuf_write(struct file *filp, const char __user *buf, size_t count, loff_t *pos){
}
static struct cdev vsdev;
static struct file_operations vser_ops = {
.owner = THIS_MODULE,
.open = ibuf_open,
.release = ibuf_release,
.read = ibuf_read,
.write = ibuf_write,
};
static int _init vser_init(void){
int ret;
dev_t dev;
dev = MKDEV(VSER_MAJOR, VSER_MINOR);
ret = register_chrdev_region(dev, VSER_DEV_CNT, VSER_DEV_NAME);
if(ret)
goto reg_err;
cdev_init(&vsdev, &vser_ops);
vsdev.owner = THIS_MODULE;
ret = cdev_add(&vsdev, dev, VSER_DEV_CNT);
if(ret)
goto add_err;
return 0;
reg_err:
return ret;
add_err:
unregister_chrdev_region(dev, VSER_DEV_CNT);
}
static void _exit vser_exit(void){
dev_t dev;
dev = MKDEV(VSER_MAJOR, VSER_MINOR);
cdev_del(&vsdev);
unregister_chrdev_region(dev, VSER_DEV_CNT);
}
module_init(vser_init);
module_exit(vser_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Konata <konata@aliyun.com>");
MODULE_DESCRIPTION("Test Module");
MODULE_ALIAS("loopback-serial");