linux设备驱动程序--类class的实现
#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");
linux设备驱动程序--类class的实现的更多相关文章
- Linux设备驱动程序学习----2.内核模块与应用程序的对比
内核模块与应用程序的对比 更多内容请参考Linux设备驱动程序学习----目录 1. 内核模块与应用程序的对比 内核模块和应用程序之间的不同之处: 大多数中小规模的应用程序是从头到尾执行单个任务,而模 ...
- linux设备驱动程序-设备树(1)-dtb转换成device_node
linux设备驱动程序-设备树(1)-dtb转换成device_node 本设备树解析基于arm平台 从start_kernel开始 linux最底层的初始化部分在HEAD.s中,这是汇编代码,我们暂 ...
- linux设备驱动程序-i2c(1):i2c总线的添加与实现
linux设备驱动程序-i2c(1):i2c总线的添加与实现 (基于4.14内核版本) 在上一章节linux设备驱动程序-i2c(0)-i2c设备驱动源码实现中,我们演示了i2c设备驱动程序的源码实现 ...
- 嵌入式Linux设备驱动程序:在运行时读取驱动程序状态
嵌入式Linux设备驱动程序:在运行时读取驱动程序状态 Embedded Linux device drivers: Reading driver state at runtime 在运行时了解驱动程 ...
- 嵌入式Linux设备驱动程序:用户空间中的设备驱动程序
嵌入式Linux设备驱动程序:用户空间中的设备驱动程序 Embedded Linux device drivers: Device drivers in user space Interfacing ...
- 嵌入式Linux设备驱动程序:发现硬件配置
嵌入式Linux设备驱动程序:发现硬件配置 Embedded Linux device drivers: Discovering the hardware configuration Interfac ...
- linux设备驱动程序该添加哪些头文件以及驱动常用头文件介绍(转)
原文链接:http://blog.chinaunix.net/uid-22609852-id-3506475.html 驱动常用头文件介绍 #include <linux/***.h> 是 ...
- 【转】linux设备驱动程序中的阻塞机制
原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275272.html 阻塞与非阻塞是设备访问的两种方式.在写阻塞与非阻塞的驱动程序时,经 ...
- Linux设备驱动程序 第三版 读书笔记(一)
Linux设备驱动程序 第三版 读书笔记(一) Bob Zhang 2017.08.25 编写基本的Hello World模块 #include <linux/init.h> #inclu ...
随机推荐
- [linux RedHat]windows下使用putty远程连接linux 下载JDK和tomcat
本文地址:http://blog.csdn.net/sushengmiyan/article/details/43154543 本文作者:sushengmiyan ------------------ ...
- Google Dremel数据模型详解(下)
"神秘"的r和d 单从数据结构来看的话,我们可以这样解释r和d的含义.r代表着当前字段与前一字段的关系,是在哪一层合并的,即公共的父结点在哪?举例来说,假如我们重建到了Code=' ...
- 6.1、Android Studio的Android Monitor概览
Android Monitor帮助你监测你的应用的性能,以帮助你合理的进行优化,调试,提升.如下功能: 1. Log消息,系统定义的或者开发者定义的. 2. 内存,CPU和GPU使用情况. 3. 网络 ...
- 4. React 属性和状态介绍
React 中的属性和状态初看之下可以互相替代,但是在 React 的设计哲学中两者有着截然不同的使用方式和使用场景. 属性的含义和用法 props = propert ...
- SSH深度历险(三) EJB Session Bean有状态和无状态的区别与联系
刚开始对两种sessionbean存在误解,认为有状态是实例一直存在,保存每次调用后的状态,并对下一次调用起作用,而认为无状态是每次调用实例化一次,不保留用户信息.仔细分析并用实践检验后,会发现,事实 ...
- iOS动画进阶 - 教你写 Slack 的 Loading 动画
(转载自:http://blog.csdn.net/wang631106979/article/details/52473985) 如果移动端访问不佳,可以访问我的个人博客 前几天看了一篇关于动画的博 ...
- 下载android4.4.2源码全过程(附已下载的源码)
今天在下载andriod源码,特来与大家分享一下我的经验.当然,网上教下载源码的教程较多,本文主要针对在GFW下下载源码出现的各种问题的解决方法. 1.首先安装下载客户端git , curl. 命令如 ...
- Android进阶(二)https请求No peer certificate的解决方法.
在做Android客户端通过https协议访问12306,并爬取数据时,出现了如下错误: 其中有一条错误提示是 javax.net.ssl.SSLPeerUnverifiedException: No ...
- Google Guava的5个鲜为人知的特性
译文出处: 花名有孚 原文出处:takipi.com Google Guava有哪些比较冷门但却又实用的特性呢? 它是最流行的开源库之一,你应该听过它的大名,它诞生的地方正是人们举办真正的魁地奇比 ...
- UNIX环境高级编程——存储映射I/O(mmap函数)
共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共 ...