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<linux/device.h>中定义:

  1、

  1. #define class_create(owner, name) \
  2. ({ \
  3. static struct lock_class_key __key; \
  4. __class_create(owner, name, &__key); \owner 是拥有这个class的模块,name是要创建的class的名字
  5. })

* 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

  1. extern struct class * __must_check __class_create(struct module*owner,
  2. const char *name,
  3. struct lock_class_key *key);

  2、

  1. /**
  2. * class_destroy - destroys a struct class structure
  3. * @cls: pointer to the struct class that is to be destroyed
  4. *
  5. * Note, the pointer to be destroyed must have been created with a call
  6. * to class_create().
  7. */
  8. void class_destroy(struct class *cls)

  3、

  1. /**
  2. * device_create - creates a device and registers it with sysfs
  3. * @class: pointer to the struct class that this device should be registered to
  4. * @parent: pointer to the parent struct device of this new device, if any
  5. * @devt: the dev_t for the char device to be added
  6. * @drvdata: the data to be added to the device for callbacks
  7. * @fmt: string for the device's name
  8. *
  9. * This function can be used by char device classes. A struct device
  10. * will be created in sysfs, registered to the specified class.
  11. *
  12. * A "dev" file will be created, showing the dev_t for the device, if
  13. * the dev_t is not 0,0.
  14. * If a pointer to a parent struct device is passed in, the newly created
  15. * struct device will be a child of that device in sysfs.
  16. * The pointer to the struct device will be returned from the call.
  17. * Any further sysfs files that might be required can be created using this
  18. * pointer.
  19. *
  20. * Note: the struct class passed to this function must have previously
  21. * been created with a call to class_create().
  22. */
  23. struct device *device_create(struct class *class, struct device *parent,
  24. dev_t devt, void *drvdata, const char *fmt, ...)

  4

  1. /**
  2. * device_destroy - removes a device that was created with device_create()
  3. * @class: pointer to the struct class that this device was registered with
  4. * @devt: the dev_t of the device that was previously registered
  5. *
  6. * This call unregisters and cleans up a device that was created with a
  7. * call to device_create().
  8. */
  9. 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

  1. #include <linux/module.h>
  2.  
  3. #include <linux/kernel.h>
  4.  
  5. #include <linux/fs.h>
  6.  
  7. #include <linux/init.h>
  8.  
  9. #include <linux/delay.h>
  10.  
  11. #include <asm/uaccess.h>
  12.  
  13. #include <asm/irq.h>
  14.  
  15. #include <asm/io.h>
  16.  
  17. #include <linux/device.h> //for mdev
  18.  
  19. MODULE_LICENSE("GPL");
  20.  
  21. static struct class *fir_driv_class; //定义一个类
  22.  
  23. static int fir_driv_open(struct inode *inode,struct file *file)
  24.  
  25. {
  26.  
  27. printk("first dirve open --xmkk\n");
  28.  
  29. return ;
  30.  
  31. }
  32.  
  33. static int fir_driv_write(struct inode *inode ,struct file *file)
  34.  
  35. {
  36.  
  37. printk("first dirve write --xmkk\n");
  38.  
  39. return ;
  40.  
  41. }
  42.  
  43. static struct file_operations fir_driv_fops={
  44.  
  45. .owner = THIS_MODULE,
  46.  
  47. .open = fir_driv_open,
  48.  
  49. .write = fir_driv_write,
  50.  
  51. };
  52.  
  53. int major;
  54.  
  55. static int __init fir_driv_init(void)
  56.  
  57. {
  58.  
  59. printk("<1>\n Hello,First drive!\n");
  60.  
  61. major=register_chrdev(, "fir_dev", &fir_driv_fops);
  62.  
  63. //新建类
  64.  
  65. fir_driv_class = class_create(THIS_MODULE, "fir_dev");
  66.  
  67. if(IS_ERR(fir_driv_class))
  68.  
  69. return PTR_ERR(fir_driv_class);
  70.  
  71. //创建设备 /dev/fir_dev
  72.  
  73. device_create(fir_driv_class,NULL,MKDEV(major, 0),NULL,"fir_dev");
  74.  
  75. return ;
  76. }
  77.  
  78. static void __exit fir_driv_exit(void)
  79.  
  80. {
  81. printk("<1>\n Exit!\n");
  82.  
  83. //删除设备结点
  84.  
  85. device_destroy(fir_driv_class,MKDEV(major, 0));
  86.  
  87. class_destroy(fir_driv_class);
  88.  
  89. unregister_chrdev(major, "fir_dev");
  90.  
  91. }
  92.  
  93. module_init(fir_driv_init);
  94.  
  95. module_exit(fir_driv_exit);
  96.  
  97. MODULE_LICENSE("GPL");

linux设备驱动----利用mdev(udev)自动创建设备文件节点的更多相关文章

  1. platform型设备在/dev目录下自动创建设备节点的分析【转】

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

  2. linux驱动开发(四) 字符设备驱动框架(自动创建设备节点)

    代码如下 #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> # ...

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

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

  4. class_create(),device_create自动创建设备文件结点

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

  5. class_create(),device_create自动创建设备文件结点【转】

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

  6. 使用class 自动创建设备节点

    #include <linux/init.h>// __init __exit #include <linux/module.h> // module_init module_ ...

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

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

  8. solr 6.0 没有schema.xml未自动创建schema文件

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

  9. 3)利用Build.php自动创建目录和文件

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

随机推荐

  1. ionic+angulajs

    基于ionic+angulajs的混合开发实现地铁APP 项目源码地址:https://github.com/zhangxy1035/SubwayMap 一.项目简介 在该项目中的地铁app是基于io ...

  2. Oracle登陆及修改用户密码

    16:20 2013/7/7 Oracle 登陆 管理员登陆 sqlplus sys/root as sysdba (密码认证)这个root是安装数据库最初你输入的口令 sqlplus     /as ...

  3. sql关键查询

    情境:保留表A数据,且A表与B表是一对多关系 SELECT tuf.Id,tuf.FileName,tuf.type,tuf.url,tum.MachineId,tum.IsDownland,tum. ...

  4. 用JAVA实现数字水印(可见)

    数字水印有可见不可见之分,可见的比如课件上印有学校校徽,微博发图片会水印上上传者的信息及微博logo等. 用java实现可见的数字水印,草人主要是用到了java.awt包中的AlphaComposit ...

  5. Oracle的登录操作

    在完美的启动Oracle数据库之后就可以登录数据库了: 1. 首先登录时使用的用户名默认是“SYSTEM”密码是你安装的时候自行设置的. 登录使用的命令是“sqlplus / as sysdba”之后 ...

  6. PHP漏洞全解(五)-SQL注入攻击

    本文主要介绍针对PHP网站的SQL注入攻击.所谓的SQL注入攻击,即一部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,使应用程序存在安全隐患.用户可以提交一段数据库查询代码,根据程序返 ...

  7. 转一贴,今天实在写累了,也看累了--【Python异步非阻塞IO多路复用Select/Poll/Epoll使用】

    下面这篇,原理理解了, 再结合 这一周来的心得体会,整个框架就差不多了... http://www.haiyun.me/archives/1056.html 有许多封装好的异步非阻塞IO多路复用框架, ...

  8. [Gauss]POJ2065 SETI

    题意: *代表0,a-z代表1-26 题目第三行给了一个公式 f (k) = $\sum\limits_{i=0}^{n-1} a_i k^i \pmod{P}$  {f(i)是输入的一串字符串中第i ...

  9. 『Asp.Net 组件』Asp.Net 服务器组件 内嵌CSS:将CSS封装到程序集中

    代码: <span style="font-family:Microsoft YaHei; font-size:12px">using System; using Sy ...

  10. 目标识别:Bag-of-words表示图像

    BOW (bag of words) 模型简介 Bag of words模型最初被用在文本分类中,将文档表示成特征矢量.它的基本思想是假定对于一个文本,忽略其词序和语法.句法,仅仅将其看做是一些词汇的 ...