linux设备驱动程序--类class的实现
来源:互联网 发布:淘宝卖手工竹制品 编辑:程序博客网 时间:2024/05/14 20:56
#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");
0 0
- linux设备驱动程序--类class的实现
- Linux设备驱动程序之三 ---- LED驱动程序的实现
- 嵌入式Linux中I2C设备驱动程序的研究与实现
- LINUX下的设备驱动程序
- Linux设备驱动程序的基本知识
- Linux i2c设备的驱动程序
- 遍历PCI设备的Linux设备驱动程序
- Linux设备驱动程序学习笔记3——简单的字符设备框架实现
- Linux设备驱动程序学习笔记4——简单的字符设备实现
- Linux下的设备驱动程序的编写
- Linux设备驱动程序之二 ---- 完善上一节的驱动程序
- linux最小设备驱动程序-驱动程序代码的基本骨架
- 虚拟设备驱动程序的设计与实现
- 虚拟设备驱动程序的设计与实现
- 虚拟设备驱动程序的设计与实现
- 如何编写Linux的设备驱动程序
- 如何编写Linux的设备驱动程序
- 如何编写Linux的设备驱动程序
- sparkStreaming
- jquery each 跳出循环
- Tomcat指定JAVA_HOME而不用默认环境变量
- C语言选择排序算法及代码
- IDEA常用快捷键
- linux设备驱动程序--类class的实现
- Java基础-值传递与地址传递
- iOS开发之 Realm框架基础知识详解
- Objective-C编码规范:26个方面解决iOS开发问题
- codeforces 672D Robin Hood (二分)
- 设计模式-----单例模式
- 记录几个比较有用的自定义viewpager
- 为什么起床后不能收邮件?
- 《Motion Design for iOS》(十七)