使用class_create(),device_create()为设备自动创建文件结点

转载:http://www.cnblogs.com/hnrainll/archive/2011/06/24/2088576.html

我们在刚开始写Linux设备驱动程序的时候, 很多时候都是利用mknod命令手动创建设备节点,实际上Linux内核为我们提供了一组函数,可以用来在模块加载的时候自动在/dev目录下创建相应设 备节点,并在卸载模块时删除该节点,当然前提条件是用户空间移植了udev。
首先,我们需要使用class_create()函数为设备创建一个类结构struct class,然后通过device_create()函数,为这个类的设备创建一个设备节点。

struct class *class_create(struct module *owner, const char *name);
    owner:模块的拥有着,一般为"THIS_MODULE"
    name:模块的名称,创建成功后,将显示在"/sys/class"中
struct device *device_create(struct class *class, struct device *parent,
                 dev_t devt, void *drvdata, const char *fmt, ...);
    class:当前设备注册的类结构体指针
    parent:指向当前设备的父设备,不存在就为NULL
    devt:当前字符设备的设备号
    drvdata:需要回调数据的结构体指针
    fmt:设备名称

下面给出例子udev.c:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>

MODULE_LICENSE ("GPL");

int hello_major = 555;
int hello_minor = 0;
int number_of_devices = 1;

struct cdev cdev;
dev_t dev = 0;
struct class *my_class;

struct file_operations hello_fops = {
      .owner = THIS_MODULE
};

static int __init hello_2_init (void)
{
    int result;
    dev = MKDEV (hello_major, hello_minor);
    result = register_chrdev_region (dev, number_of_devices, "hello");
    if (result < 0) {
       printk (KERN_WARNING "hello: can't get major number %d\n", hello_major);
       return result;
    }

   cdev_init (&cdev, &hello_fops);
   cdev.owner = THIS_MODULE;
   cdev.ops = &hello_fops;
   result = cdev_add (&cdev, MKDEV (hello_major, hello_minor) , 1);
   if (result) {
       printk (KERN_NOTICE "Error %d adding cdev\n", result);
       return result;
   }
    /* create your own class under /sysfs */
    my_class = class_create(THIS_MODULE, "my_class");
    if(IS_ERR(my_class)) 
    {
        printk("Err: failed in creating class./n");
        return -1; 
    }

    /* register your own device in sysfs, and this will cause udev to create corresponding device node */
    device_create( my_class, NULL, MKDEV(hello_major, 0), my_class, "hello-%d", 0 );

    printk (KERN_INFO "Registered character driver/n");
    return 0;
}

static void __exit hello_2_exit (void)
{
    dev_t devno = MKDEV (hello_major, hello_minor);

    cdev_del (&cdev);

    device_destroy(my_class, MKDEV(hello_major, 0));         //delete device node under /dev
    class_destroy(my_class);                               //delete class created by us

    unregister_chrdev_region (devno, number_of_devices);

    printk (KERN_INFO "char driver cleaned up/n");
}

module_init (hello_2_init);
module_exit (hello_2_exit);

执行sudo insmod udev.ko,可以发现在/dev目录下已经生成了hello-0文件,/sys/class/目录中存在my_class目录
执行sudo rmmod udev.ko,/dev目录下hello-0文件已经删除,同样my_class目录也不存在了

代码:http://yunpan.cn/cjnmhAZ6u2Q3c (提取码:51af)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值