#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的实现的更多相关文章

  1. Linux设备驱动程序学习----2.内核模块与应用程序的对比

    内核模块与应用程序的对比 更多内容请参考Linux设备驱动程序学习----目录 1. 内核模块与应用程序的对比 内核模块和应用程序之间的不同之处: 大多数中小规模的应用程序是从头到尾执行单个任务,而模 ...

  2. linux设备驱动程序-设备树(1)-dtb转换成device_node

    linux设备驱动程序-设备树(1)-dtb转换成device_node 本设备树解析基于arm平台 从start_kernel开始 linux最底层的初始化部分在HEAD.s中,这是汇编代码,我们暂 ...

  3. linux设备驱动程序-i2c(1):i2c总线的添加与实现

    linux设备驱动程序-i2c(1):i2c总线的添加与实现 (基于4.14内核版本) 在上一章节linux设备驱动程序-i2c(0)-i2c设备驱动源码实现中,我们演示了i2c设备驱动程序的源码实现 ...

  4. 嵌入式Linux设备驱动程序:在运行时读取驱动程序状态

    嵌入式Linux设备驱动程序:在运行时读取驱动程序状态 Embedded Linux device drivers: Reading driver state at runtime 在运行时了解驱动程 ...

  5. 嵌入式Linux设备驱动程序:用户空间中的设备驱动程序

    嵌入式Linux设备驱动程序:用户空间中的设备驱动程序 Embedded Linux device drivers: Device drivers in user space Interfacing ...

  6. 嵌入式Linux设备驱动程序:发现硬件配置

    嵌入式Linux设备驱动程序:发现硬件配置 Embedded Linux device drivers: Discovering the hardware configuration Interfac ...

  7. linux设备驱动程序该添加哪些头文件以及驱动常用头文件介绍(转)

    原文链接:http://blog.chinaunix.net/uid-22609852-id-3506475.html 驱动常用头文件介绍 #include <linux/***.h> 是 ...

  8. 【转】linux设备驱动程序中的阻塞机制

    原文网址:http://www.cnblogs.com/geneil/archive/2011/12/04/2275272.html 阻塞与非阻塞是设备访问的两种方式.在写阻塞与非阻塞的驱动程序时,经 ...

  9. Linux设备驱动程序 第三版 读书笔记(一)

    Linux设备驱动程序 第三版 读书笔记(一) Bob Zhang 2017.08.25 编写基本的Hello World模块 #include <linux/init.h> #inclu ...

随机推荐

  1. android listview 使用

    今天在做项目的时候用了自定义listview以及自定义的item.adapter.现在把其中需要注意的地方记录下来: 1.item内如果有button等控件时,在监听listview的onitemcl ...

  2. Spring开发环境搭建教程

    Spring开发环境搭建 JDK7以上版本 eclispe for j2ee 4.0以上版本 Spring frameWorks 3.0以上版本 至于前两个我们就不介绍,直接百度就可以了,对于Spri ...

  3. 3.2、Android Studio在物理设备中运行APP

    当你构建一个Android应用时,在发布给用户之前,在物理设备上测试一下你的应用是非常必要的. 你可以使用Android设备作为运行.调试和测试应用的环境.包含在SDK中的工具让你在编译完成后在设备中 ...

  4. bash与ksh数组使用

    区别: bash与ksh在数组的使用中,最大的不同在于数组的定义. bash: declare -a arrayname ksh:set -A arrayname 其实,数组不用非要定义,在赋值的时候 ...

  5. Servlet之Response对象

    下面的方法可用于在 Servlet 程序中设置 HTTP 响应报头.这些方法通过HttpServletResponse 对象可用. 1    String encodeRedirectURL(Stri ...

  6. Android Studio科普篇——1.几个个性化设置

    本人未研读过android-studio使用文档,亦未去好好琢磨它的各种使用技巧等.以下内容均来自个人使用经验,如有讹误,还请指正. 1.主题. File->Settings, 搜索Theme, ...

  7. MANIFEST.MF Error: No available bundle exports package

    Issue: When you imported some 3rd jars and compiled MANIFEST.MF, you may got following compling erro ...

  8. tomcat服务器虚拟目录的映射方式

    lWEB应用程序指供浏览器问的程序,通常也简称为web应用 l l一个web应用由多个静态web资源和动态web资源组成,如: •html.css.js文件 •jsp文件.servlet程序.支持ja ...

  9. Android项目-高考作文-抽象BaseAdapter

    1, 在使用baseAdapter的时候,总是有需要重复的代码如: @Override public int getCount() { return list.size(); } @Override ...

  10. 怎样将Android手机弄死机?

    将Android手机弄死机目前知道有两种方式: 第一种: 以root权限在shell中执行 stop 通过 start 但是这种串口还是可以用 第二种: suecho "c" &g ...