#include <linux/module.h> #include <linux/fs.h> #include <linux/sched.h> #include <linux/delay.h> #include <linux/atomic.h> #include <linux/types.h> #include <linux/interrupt.h> #include <linux/kthread.h> #include <linux/slab.h> #include <linux/io.h> #include <linux/gpio.h> #include <linux/dma-mapping.h> //定义设备名称为test-dev #define DEV_NAME "test-dev" //定义并初始化一个类 struct class cla = { .name = "test-cla", //将类的名字设置为test-cla .owner = THIS_MODULE, //该类的拥有者为这个模块 }; int test_open(struct inode *node, struct file *filp) { printk("test open\n"); return 0; } int test_close(struct inode *node, struct file *filp) { printk("test close\n"); return 0; } //填充并初始化file_operations结构体 struct file_operations fops = { .owner = THIS_MODULE, .open = test_open, .release = test_close, }; //定义设备 struct device dev = { .init_name = DEV_NAME, .class = &cla, //设备归类cla; .release = test_release, }; //定义主设备号和次设备号 int major = 0; int minor = 0; int test_init(void) { int ret; printk("test init\n"); //将类进行注册 ret = class_register(&cla); //如果返回值不为0,返回错误值 if(IS_ERR_VALUE(ret)) { return ret; } //注册一个字符设备驱动 ret = register_chrdev(major, DEV_NAME, &fops); //如果注册不成功返回错误值并撤销类的实现 if(IS_ERR_VALUE(ret)) { class_unregister(&cla); return ret; } major = ret; //printk("major = %d\n", major); //申请主设备号与次设备号 dev.devt = MKDEV(major, minor); //将设备进行注册 ret = device_register(&dev); //如果设备注册不成功,撤销类设备注册并解除字符设备驱动的注册 if(IS_ERR_VALUE(ret)) { class_unregister(&cla); unregister_chrdev(major, DEV_NAME); return ret; } return 0; } void test_exit(void) { printk("test exit\n"); //解除字符设备的注册 unregister_chrdev(major, DEV_NAME); //解决类设备注册 device_unregister(&dev); class_unregister(&cla); } module_init(test_init); module_exit(test_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("yangyx"); MODULE_VERSION("1.1");