ted*_*ted 5 linux driver linux-device-driver
我正在重新编写驱动程序,并在LDD3中遇到了cdev接口.阅读http://lwn.net/Articles/195805/后,我比开明更困惑.从那里的评论
- 为了使设备实际出现在文件系统中,您必须调用
device_create (class, parent_dev, devno, device_name).- 为了调用
device_create你需要有一个设备类对象:要么使用现有的一个类,要么创建自己的类create_class (THIS_MODULE, class_name)我认为这仅适用于sysfs.
那么,新界面是否试图改变失败的东西,因此建议继续使用device_create?
如果cdev建议如何创建sysfs条目?
我从来没有完全理解拥有一个设备类的好处,是否有一个点,如果是这样,我如何用cdev实现它?
rag*_*276 11
cdev是内核的char设备表示.一般的想法是cdev与一组相关联file_operations.这些file_operations是在设备节点上执行的,通常位于设备节点下/dev.cdev_init()用于将a 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, 0, 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, NULL, "my_dev%d", i);
/* Now make the device live for the users to access */
cdev_add(&my_cdev[i], curr_dev, 1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,逐一回答你的问题:
那么,新界面是否试图改变失败的东西,因此建议继续使用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条目.
希望这有帮助.