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 ...
随机推荐
- Gazebo機器人仿真學習探索筆記(二)基本使用說明
在完成Gazebo7安裝後,需要熟悉Gazebo,方便之後使用. 部分源代碼可以參考:https://bitbucket.org/osrf/gazebo/src/ 如果還沒有安裝請參考之前內容完成安裝 ...
- Dynamics CRM2015 页面导航栏顶部全局快速查找功能配置
在CRM2015中微软加入了新的快速查找功能,让你的数据查找更加方便,功能栏如下图所示,直接可以框中输入搜索项进行搜索. 但该功能是需要进行些配置,具体的配置在设置-管理-系统设置中,默认的就是红框中 ...
- .so的封装调用
.so的创建和调用有一个特点,我们要知道.so的调用并不一定必须在Activity中进行,那么制作时也并不一定要在Activity中,但是,一旦.so制作成功,那么再调用时,调用的java类就必须跟制 ...
- 取KindEditor中的textarea的值区不到的解决方案,固定kindEditor的高度
可以通过下面的方式取到textarea的值 var content = $(document.getElementsByTagName('iframe')[0].contentWindow.do ...
- SQL join 语句 画图果然更容易理解
我认为 Ligaya Turmelle 的关于SQL联合(join)语句的帖子对于新手开发者来说是份很好的材料.SQL 联合语句好像是基于集合的,用韦恩图来解释咋一看是很自然而然的.不过正如在她的帖子 ...
- Mybatis接口编程原理分析(三)
前面两篇博客Mybatis接口编程原理分析(一)和Mybatis接口编程原理分析(二)我们介绍了MapperProxyFactory.MapperProxy和MapperMethod的操作及源码分析, ...
- ROS_Kinetic_12 ROS程序基础Eclipse_C++(三)usb camera
ROS_Kinetic_12 ROS程序基础Eclipse_C++(三)usb camera 软件包下载地址:https://github.com/bosch-ros-pkg/usb_cam 下载后, ...
- Linux多线程实践(2) --线程基本API
POSIX线程库 与线程有关的函数构成了一个完整的系列,绝大多数函数的名字都是以"pthread_"开头,要使用这些函数库,要通过引入头文<pthread.h>,而且链 ...
- StoreType.java 存储方式
StoreType.java 存储方式 http://injavawetrust.iteye.com package com.iteye.injavawetrust.miner; /** * 存储方式 ...
- python的read() 、readline()、readlines()、xreadlines()
先来一个小例子: import sys dir= os.path.dirname(os.path.abspath(__file__)) file_path='%s/test.txt' % dir f ...