Linux cdev vs register_chrdev
我正在重新编写驱动程序,并在LDD3中遇到了cdev接口.阅读
http://lwn.net/Articles/195805/后,我比开明更困惑.从那里的评论
>那么,新界面是否尝试改变失败的东西, 解决方法
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? 如果建议使用cdev,我该如何创建sysfs条目? 我从来没有完全理解拥有一个设备类的好处,是否有一个点,我如何用cdev实现它? 通常,您可能不会在驱动程序中使用cdev. cdev是一个非常低级别的表示.使用cdev可以为设备类型构建许多强大的框架,例如input,tty,ALSA,IIO等.所有这些框架都是基于cdev构建的.所以,你可能不会直接使用cdev.相反,您可以注册这些框架并以更有效的方式访问您的设备.注册这些框架还可以为您创建设备节点和sysfs条目. 希望这有帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |