linux mdev 命令,linux设备驱动----利用mdev(udev)自动创建设备文件节点

1、mdev的使用方法和原理:

mdev是busybox 自带的一个简化版的udev,适合于嵌入式的应用埸合。其具有使用简单的特点。它的作用,就是在系统启动和热插拔或动态加载驱动程序时,自动产生驱动程序所需的节点文件。在以busybox 为基础构建嵌入式linux 的根文件系统时,使用它是最优的选择

下面介绍使用方法:

以字符设备char_dev为例,在驱动初始化的代码里调用class_create为该设备创建一个class,再为每个设备调用class_device_create创建对应的设备,这样的module被加载时,undev daemon就会自动在/dev下创建char_dev设备文件。大概方法如下:

struct class *myclass = class_create(THIS_MODULE, “char_dev”);

class_device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “char_dev”);

当然,在exit函数中要把创建的class移除:

class_destory(&xxx_dev->cdev);

class_device_desotry(my_class,MKDEV(major_num,0));

以上函数均在头文件#include中定义:

1、

#define class_create(owner, name) \

({ \

static struct lock_class_key __key; \

__class_create(owner, name, &__key); \owner 是拥有这个class的模块,name是要创建的class的名字

})

* class_create - create a struct class structure

* @owner: pointer to the module that is to "own" this struct class

* @name: pointer to a string for the name of this class.

* @key: the lock_class_key for this class; used by mutex lock debugging

extern struct class * __must_check __class_create(struct module*owner,

const char *name,

struct lock_class_key *key);

2、

/**

* class_destroy - destroys a struct class structure

* @cls: pointer to the struct class that is to be destroyed

*

* Note, the pointer to be destroyed must have been created with a call

* to class_create().

*/

void class_destroy(struct class *cls)

3、

/**

* device_create - creates a device and registers it with sysfs

* @class: pointer to the struct class that this device should be registered to

* @parent: pointer to the parent struct device of this new device, if any

* @devt: the dev_t for the char device to be added

* @drvdata: the data to be added to the device for callbacks

* @fmt: string for the device's name

*

* This function can be used by char device classes. A struct device

* will be created in sysfs, registered to the specified class.

*

* A "dev" file will be created, showing the dev_t for the device, if

* the dev_t is not 0,0.

* If a pointer to a parent struct device is passed in, the newly created

* struct device will be a child of that device in sysfs.

* The pointer to the struct device will be returned from the call.

* Any further sysfs files that might be required can be created using this

* pointer.

*

* Note: the struct class passed to this function must have previously

* been created with a call to class_create().

*/

struct device *device_create(struct class *class, struct device *parent,

dev_t devt, void *drvdata, const char *fmt, ...)

4

/**

* device_destroy - removes a device that was created with device_create()

* @class: pointer to the struct class that this device was registered with

* @devt: the dev_t of the device that was previously registered

*

* This call unregisters and cleans up a device that was created with a

* call to device_create().

*/

void device_destroy(struct class *class, dev_t devt)

补充:

在Linux 2.6中,针对上面的这个问题不同的版本有些修改,使用前要先查看下/.../include/linux /device.h里的函数声明,如我用的是Linux 2.6.29,里面就没有class_device_create函数,而直接使用device_create就可以了,而在之前的版本如Li nux 2.6.15,里面就要用class _device_create函数

2、实例:fir_driv_auto.c

#include

#include

#include

#include

#include

#include

#include

#include

#include //for mdev

MODULE_LICENSE("GPL");

static struct class *fir_driv_class; //定义一个类

static int fir_driv_open(struct inode *inode,struct file *file)

{

printk("first dirve open --xmkk\n");

return ;

}

static int fir_driv_write(struct inode *inode ,struct file *file)

{

printk("first dirve write --xmkk\n");

return ;

}

static struct file_operations fir_driv_fops={

.owner = THIS_MODULE,

.open = fir_driv_open,

.write = fir_driv_write,

};

int major;

static int __init fir_driv_init(void)

{

printk("<1>\n Hello,First drive!\n");

major=register_chrdev(, "fir_dev", &fir_driv_fops);

//新建类

fir_driv_class = class_create(THIS_MODULE, "fir_dev");

if(IS_ERR(fir_driv_class))

return PTR_ERR(fir_driv_class);

//创建设备 /dev/fir_dev

device_create(fir_driv_class,NULL,MKDEV(major, 0),NULL,"fir_dev");

return ;

}

static void __exit fir_driv_exit(void)

{

printk("<1>\n Exit!\n");

//删除设备结点

device_destroy(fir_driv_class,MKDEV(major, 0));

class_destroy(fir_driv_class);

unregister_chrdev(major, "fir_dev");

}

module_init(fir_driv_init);

module_exit(fir_driv_exit);

MODULE_LICENSE("GPL");

platform型设备在&sol;dev目录下自动创建设备节点的分析【转】

转自:http://blog.csdn.net/rockrockwu/article/details/7357648 系统启动过程中platform设备.驱动注册完毕,为什么在/dev目录下就自动创建 ...

linux驱动开发(四) 字符设备驱动框架&lpar;自动创建设备节点&rpar;

代码如下 #include #include #include # ...

linux下自动创建设备文件节点---class

在驱动模块初始化函数中实现设备节点的自动创建 我们在刚开始写Linux设备驱动程序的时候,很多时候都是利用mknod命令手动创建设备节点,实际上Linux内核为我们提供了一组函数,可以用来在模块加载的 ...

class&lowbar;create&lpar;&rpar;&comma;device&lowbar;create自动创建设备文件结点

class_create(),device_create自动创建设备文件结点 从linux 内核2.6的某个版本之后,devfs不复存在,udev成为devfs的替代.相比devfs,udev有很多优 ...

class&lowbar;create&lpar;&rpar;&comma;device&lowbar;create自动创建设备文件结点【转】

本文参考来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhenwenxian/archive/2010/03/28/5424434.aspx 本文转自:http://ww ...

使用class 自动创建设备节点

#include // __init __exit #include // module_init module_ ...

Linux内核驱动学习(四)Platform设备驱动模型

Linux platform设备驱动模型 文章目录 Linux platform设备驱动模型 前言 框架 设备与驱动的分离 设备(device) 驱动(driver) 匹配(match) 参考 前言 ...

solr 6&period;0 没有schema&period;xml未自动创建schema文件

solr 6.0 没有schema.xml未自动创建schema文件 摘要:在之前的Solr版本中(Solr5之前),在创建core的时候,Solr会自动创建好schema.xml,但是在之后的版本中 ...

3)利用Build&period;php自动创建目录和文件

(1)首先做法参照: thinkphp5的手册的  命令行--->自动生成目录结构 或者看云的资料:https://www.kancloud.cn/manual/thinkphp5/118021 ...

随机推荐

python自动开发之第十三天

1.Paramiko模块下的demo.py程序     前面利用Python中的Paramiko模块可以进行SSH的连接,以及用来传送文件(SFTP),但是无论是哪一种方式,连接都是短暂的,并非是长连 ...

Ehcache 整合Spring 使用页面、对象缓存(转)

Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

Javascript 进阶 面向对象编程 继承的一个样例

Javascript的难点就是面向对象编程,上一篇介绍了Javascript的两种继承方式:Javascript 进阶 继承.这篇使用一个样例来展示js怎样面向对象编程.以及怎样基于类实现继承. 1. ...

NOI2017&amp&semi;&amp&semi;codeM2017游记

7.17 坐了一天动车到绍兴,宿舍环境什么的还是很棒的. 7.18 早上开幕式,没啥好看的,例行节目+讲话. 下午笔试,顺利满分,不过ccz挂了一道多选,99分,影响应该不是很大. 练习赛出人意料地没 ...

通过URL传递PDF名称参数显示PDF

1

使用signalr实现网页和微信公众号实时聊天&lpar;上&rpar;

最近项目中需要实现客户在公众号中和客服(客服使用后台网站系统)进行实时聊天的功能.折腾了一段时间,实现了这个功能.现在将过程记录下,以便有相同需求的同行可以参考,也是自己做个总结.这篇是上,用手机编辑 ...

C语言ini格式配置文件的读写

依赖的类 /*1 utils.h *# A variety of utility functions. *# *# Some of the functions are duplicates of we ...

【java初探】——格式化字符串

String 类的静态方法format()方法用于创建格式化字符串,format()方法有两种重载形式: format(String fromat,Object...args) 该方法使用指定的格式字 ...

Linux设备驱动Hello World程序介绍

自古以来,学习一门新编程语言的第一步就是写一个打印“hello world”的程序(可以看这个帖子供罗列了300个“hello world”程序例子)在本 ...

部署showdoc

1.下载 https://github.com/star7th/showdoc 2.解压 sudo tar -zvxf ~/showdoc-2.4.5.tar.gz -C /home/wwwroot/ ...

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完整版:https://download.csdn.net/download/qq_27595745/89522468 【课程大纲】 1-1 什么是java 1-2 认识java语言 1-3 java平台的体系结构 1-4 java SE环境安装和配置 2-1 java程序简介 2-2 计算机中的程序 2-3 java程序 2-4 java类库组织结构和文档 2-5 java虚拟机简介 2-6 java的垃圾回收器 2-7 java上机练习 3-1 java语言基础入门 3-2 数据的分类 3-3 标识符、关键字和常量 3-4 运算符 3-5 表达式 3-6 顺序结构和选择结构 3-7 循环语句 3-8 跳转语句 3-9 MyEclipse工具介绍 3-10 java基础知识章节练习 4-1 一维数组 4-2 数组应用 4-3 多维数组 4-4 排序算法 4-5 增强for循环 4-6 数组和排序算法章节练习 5-0 抽象和封装 5-1 面向过程的设计思想 5-2 面向对象的设计思想 5-3 抽象 5-4 封装 5-5 属性 5-6 方法的定义 5-7 this关键字 5-8 javaBean 5-9 包 package 5-10 抽象和封装章节练习 6-0 继承和多态 6-1 继承 6-2 object类 6-3 多态 6-4 访问修饰符 6-5 static修饰符 6-6 final修饰符 6-7 abstract修饰符 6-8 接口 6-9 继承和多态 章节练习 7-1 面向对象的分析与设计简介 7-2 对象模型建立 7-3 类之间的关系 7-4 软件的可维护与复用设计原则 7-5 面向对象的设计与分析 章节练习 8-1 内部类与包装器 8-2 对象包装器 8-3 装箱和拆箱 8-4 练习题 9-1 常用类介绍 9-2 StringBuffer和String Builder类 9-3 Rintime类的使用 9-4 日期类简介 9-5 java程序国际化的实现 9-6 Random类和Math类 9-7 枚举 9-8 练习题 10-1 java异常处理 10-2 认识异常 10-3 使用try和catch捕获异常 10-4 使用throw和throws引发异常 10-5 finally关键字 10-6 getMessage和printStackTrace方法 10-7 异常分类 10-8 自定义异常类 10-9 练习题 11-1 Java集合框架和泛型机制 11-2 Collection接口 11-3 Set接口实现类 11-4 List接口实现类 11-5 Map接口 11-6 Collections类 11-7 泛型概述 11-8 练习题 12-1 多线程 12-2 线程的生命周期 12-3 线程的调度和优先级 12-4 线程的同步 12-5 集合类的同步问题 12-6 用Timer类调度任务 12-7 练习题 13-1 Java IO 13-2 Java IO原理 13-3 流类的结构 13-4 文件流 13-5 缓冲流 13-6 转换流 13-7 数据流 13-8 打印流 13-9 对象流 13-10 随机存取文件流 13-11 zip文件流 13-12 练习题 14-1 图形用户界面设计 14-2 事件处理机制 14-3 AWT常用组件 14-4 swing简介 14-5 可视化开发swing组件 14-6 声音的播放和处理 14-7 2D图形的绘制 14-8 练习题 15-1 反射 15-2 使用Java反射机制 15-3 反射与动态代理 15-4 练习题 16-1 Java标注 16-2 JDK内置的基本标注类型 16-3 自定义标注类型 16-4 对标注进行标注 16-5 利用反射获取标注信息 16-6 练习题 17-1 顶目实战1-单机版五子棋游戏 17-2 总体设计 17-3 代码实现 17-4 程序的运行与发布 17-5 手动生成可执行JAR文件 17-6 练习题 18-1 Java数据库编程 18-2 JDBC类和接口 18-3 JDBC操作SQL 18-4 JDBC基本示例 18-5 JDBC应用示例 18-6 练习题 19-1 。。。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值