加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux cdev vs register_chrdev

发布时间:2020-12-13 19:05:40 所属栏目:Linux 来源:网络整理
导读:我正在重新编写驱动程序,并在LDD3中遇到了cdev接口.阅读http://lwn.net/Articles/195805/后,我比开明更困惑.从那里的评论 In order the device to actually appear in the file system,you have to call device_create (class,parent_dev,devno,device_name)

我正在重新编写驱动程序,并在LDD3中遇到了cdev接口.阅读http://lwn.net/Articles/195805/后,我比开明更困惑.从那里的评论

  • In order the device to actually appear in the file system,you have to call device_create (class,parent_dev,devno,device_name).
  • In order to call device_create you need to have a device class object: either use one of the existing classes,or create your own with create_class (THIS_MODULE,class_name)

I think this is for sysfs only.

>那么,新界面是否尝试改变失败的东西,
因此建议继续使用device_create吗?
>如果建议使用cdev,我该如何创建sysfs条目?
>我从未完全理解拥有设备类的好处,是
有一个点,如果是这样,我该如何实现它
CDEV?

最佳答案
cdev是内核的char设备表示.一般的想法是将cdev与一组file_operations相关联.这些file_operations在设备节点上执行,通常存在于/ dev下. cdev_init()用于将cdev与一组file_operations关联/链接.最后,在设备上调用cdev_add()使其生效,这样用户就可以访问它们.

现在,虽然这样做了,但这并不意味着为您创建了设备节点.这是通过使用mknod实用程序手动完成的(如LDD3中所述).通常,驱动程序应该创建设备节点.这是使用device_create()函数实现的.设备节点通常与类相关联.因此,我们需要首先创建一个类(使用class_create()),然后使用该类创建设备节点.

让我通过一个例子解释一下.仅考虑init函数(此处避免错误处理以保持清晰度):

struct class *my_class;
struct cdev my_cdev[N_MINORS];    
dev_t dev_num;

static int __init my_init(void)
{
    int i;
    dev_t curr_dev;

    /* Request the kernel for N_MINOR devices */
    alloc_chrdev_region(&dev_num,N_MINORS,"my_driver");

    /* Create a class : appears at /sys/class */
    my_class = class_create(THIS_MODULE,"my_driver_class");

    /* Initialize and create each of the device(cdev) */
    for (i = 0; i < N_MINORS; i++) {

        /* Associate the cdev with a set of file_operations */
        cdev_init(&my_cdev[i],&fops);

        /* Build up the current device number. To be used further */
        curr_dev = MKDEV(MAJOR(dev_num),MINOR(dev_num) + i);

        /* Create a device node for this device. Look,the class is
         * being used here. The same class is associated with N_MINOR
         * devices. Once the function returns,device nodes will be
         * created as /dev/my_dev0,/dev/my_dev1,... You can also view
         * the devices under /sys/class/my_driver_class.
         */
        device_create(my_class,NULL,curr_dev,"my_dev%d",i);

        /* Now make the device live for the users to access */
        cdev_add(&my_cdev[i],1); 
    }

    return 0;
}

现在,逐一回答你的问题:

那么,新界面是否试图改变失败的东西,因此建议继续使用device_create?
这不是一个新的界面.这可以说是一种扩展,通常用于创建设备节点.它还为我们提供了创建sysfs属性的优势,它为访问内核资源提供了非常灵活的方法.函数device_create()返回一个指向struct device的指针,它在内核中具有非常强大的意义.看看LDD3中关于“Linux设备模型”的章节.

如果建议使用cdev,我该如何创建sysfs条目?
cdev和sysfs条目彼此独立.即使没有cdev,您也可以创建sysfs条目.再看一下LDD3中关于“Linux设备模型”的章节.您还可以查看此示例代码以创建sysfs条目:http://lxr.free-electrons.com/source/samples/kobject/kobject-example.c

我从来没有完全理解拥有一个设备类的好处,是否有一个点,我如何用cdev实现它?
我希望上面的代码回答这个问题.

通常,您可能不会在驱动程序中使用cdev. cdev是一个非常低级别的表示.使用cdev可以为设备类型构建许多强大的框架,例如input,tty,ALSA,IIO等.所有这些框架都是基于cdev构建的.所以,你可能不会直接使用cdev.相反,您可以注册这些框架并以更有效的方式访问您的设备.注册这些框架还可以为您创建设备节点和sysfs条目.

希望这有帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读