Vij*_*nam 3 linux kernel linux-device-driver linux-kernel
我写了基本的char驱动程序.
使用cdev_alloc,cdev_init,cdev_add完成了使用内核注册char设备.Major = 117,Minor = 1.
cdev_add函数retrun成功.我正在尝试检查是否创建了char设备.我没有在/ dev /或/ dev/char下找到任何设备,主要没有117.
register_chrdev不会在我们提供NAME的最新内核中使用.但是cdev_add仅使用主号执行内核的char设备注册.
我对最新的内核行为感到困惑.
我是否需要将register_chrdev与cdev_add一起使用?或者我需要使用mknod commad在/ dev /中显示设备吗?
谢谢.
cdev是内核的char设备表示,用于将cdev与一组file_operations相关联.这些file_operations在设备节点上执行,通常位于/ dev下,或者取决于您创建设备节点的位置.
cdev_init()用于将cdev与一组file_operations相关联.最后,在设备上调用cdev_add()使其生效,这样用户就可以访问它们.
现在,虽然这样做了,但这并不意味着为您创建了设备节点.
这可以通过使用mknod实用程序或使用device_create()函数手动完成.设备节点通常与类相关联.因此,我们需要首先创建一个类(使用class_create()),然后使用该类创建设备节点.
这是如何获取设备节点的示例.
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)
register_chrdev不会在我们给NAME的最新内核中使用吗?
register_chrdev()是在内核2.4中使用的旧方法,但在内核2.6中用register_chrdev_region()和alloc_chardev_region()替换.