gpt4 book ai didi

linux - cdev_add() 在 117 上向 major 成功注册后,字符设备出现在哪里。

转载 作者:可可西里 更新时间:2023-11-01 11:48:35 26 4
gpt4 key购买 nike

我已经编写了基本的字符驱动程序。

已使用 cdev_alloc、cdev_init、cdev_add 向内核注册字符设备。主要 = 117,次要 = 1。

cdev_add 函数重新运行成功。我正在尝试检查是否创建了字符设备。我没有在/dev/或/dev/char 下找到任何主要编号为 117 的设备。

register_chrdev 不会在最新的内核中使用,我们在这里给出 NAME。但 cdev_add 仅使用主编号向内核执行字符设备注册。

我对最新的内核行为感到困惑。

我需要将 register_chrdev 与 cdev_add 一起使用吗?或者我需要使用 mknod 命令来在/dev/中显示设备吗?

谢谢。

最佳答案

cdev是内核的字符设备表示,是将cdev与一组file_operations相关联。这些 file_operations 在设备节点上执行,通常位于/dev 下或取决于您创 build 备节点的位置。

cdev_init() 用于将 cdev 与一组 file_operations 相关联。最后在设备上调用 cdev_add() 使其生效,这样用户就可以访问它们。

现在,虽然这已经完成,但这并不意味着设备节点已为您创建

这是通过使用mknod 实用程序 或使用device_create() 函数手动完成的。设备节点通常与一个类相关联。因此,我们需要先创建一个类(使用 class_create()),然后使用该类创 build 备节点。

这是一个如何获取设备节点的例子。

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;
}

register_chrdev is not going to be use in the latest kernel, where we give NAME?

register_chrdev() 是在内核 2.4 中使用的旧方法,但在内核 2.6 中被替换为 register_chrdev_region() 和 alloc_chardev_region()。

  1. register_chrdev_region() 如果您提前确切知道您想要的设备编号。
  2. alloc_chrdev_region() 用于动态分配设备号,由内核完成。

关于linux - cdev_add() 在 117 上向 major 成功注册后,字符设备出现在哪里。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41320939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com