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、

#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 <linux/module.h>

#include <linux/kernel.h>

#include <linux/fs.h>

#include <linux/init.h>

#include <linux/delay.h>

#include <asm/uaccess.h>

#include <asm/irq.h>

#include <asm/io.h>

#include <linux/device.h>       //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");

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. Catch Application Exceptions in a Windows Forms Application

    You need to handle the System.Windows.Forms.Application.ThreadException event for Windows Forms. Thi ...

  2. js: get event handler bound to the element

    jQuery._data(jQuery(this)[0], "events" ).click[0].handler $._data( $("#myabc")[0 ...

  3. Python按照索引访问list

    由于list是一个有序集合,所以,我们可以用一个list按分数从高到低表示出班里的3个同学: >>> L = ['Adam', 'Lisa', 'Bart'] 那我们如何从list中 ...

  4. caffe之(四)全连接层

    在caffe中,网络的结构由prototxt文件中给出,由一些列的Layer(层)组成,常用的层如:数据加载层.卷积操作层.pooling层.非线性变换层.内积运算层.归一化层.损失计算层等:本篇主要 ...

  5. Twitter:蓄水池储水量问题

    早上买了两个饼夹肉,我吃了一个,辣椒粉好多,现在一直在实验室喝水. 一.倒数第n位 今年暑假去世纪佳缘面试,其中一题就是这个,只能遍历一遍链表求出倒数第n位. 答案是两个指针,第一个在头部设为A,第二 ...

  6. iOS 并发:NSOperation 与调度队列入门(1)

    一直以来,并发都被视为 iOS 开发中的「洪水猛兽」.许多开发者都将其视为危险地带,唯恐避之而不及.更有谣传认为,多线程代码应该尽力避免.笔者同意,如果你对并发的了解不够深入,就容易造成危险.但是,危 ...

  7. Linux内核spin_lock与spin_lock_irq分析

    http://blog.csdn.net/zhanglei4214/article/details/6837697

  8. UL LI 布局 TAB 切换条

    web页面实现tab的功能有几种实现方式,下面是使用UL LI DIV方式实现的tab. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  9. Altium Designer 常用快捷键总结

    原理图:1:按住shift  拖动某个元件,可快速复制.2:按住鼠标滚轮 鼠标上下滑动 放大缩小.3:按住Ctrl 按住鼠标右键 鼠标上下滑动也放大缩小.4:按住Ctrl 拖动某个元件 可以移动位置 ...

  10. python属性查找

    python中执行obj.attr时,将调用特殊方法obj.__getattribute__('attr'),该方法执行搜索来查找该属性,通常涉及检查特性.查找实例字典.查找类字典以及搜索基类.如果搜 ...