在cdev_add()在117上成功注册了main之后,char设备出现在哪里? [英] where do char device appear after cdev_add() registers successfully with major on 117.

查看:123
本文介绍了在cdev_add()在117上成功注册了main之后,char设备出现在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了基本的char驱动程序.

I have written basic char driver.

已使用cdev_alloc,cdev_init,cdev_add完成了char设备与内核的注册. 专业= 117,未成年人= 1.

Registration of char device with kernel has been done using cdev_alloc, cdev_init, cdev_add. Major = 117, Minor = 1.

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

cdev_add function retrun success. I am trying to check whether char device created or not. I dont find any device under /dev/ or /dev/char with major no 117.

register_chrdev不会在我们提供NAME的最新内核中使用. 但是cdev_add仅使用主数执行内核中的char设备注册.

register_chrdev is not going to be use in the latest kernel, where we give NAME. but cdev_add perform char device registration with kernel using major number only.

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

I am confused with the latest kernel behavior.

我需要和cdev_add一起使用register_chrdev吗? 或者 我是否需要使用mknod的commad到showup设备中的/dev/?

Do i need to use register_chrdev along with cdev_add ? or Do i need to use mknod commad to showup device in /dev/ ?

谢谢.

推荐答案

cdev 是内核的char设备表示,并将cdev与一组file_operations关联.这些file_operation在设备节点上执行,通常在/dev下或根据您在哪里创建设备节点而存在.

cdev is the char device representation of the kernel and is to associate cdev with a set of file_operations. These file_operations are performed on a device node, typically present under /dev or depending on you where you create a device node.

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

cdev_init() is used to associate a cdev with a set of file_operations. Finally cdev_add() is called on the device to make it live, such that, the user could access them.

现在,完成此操作后,并不意味着已为您创建设备节点.

这可以通过使用 mknod实用工具或使用 device_create()函数手动完成.设备节点通常与一个类别相关联.因此,我们需要先创建一个类(使用class_create()),然后使用该类创建设备节点.

This is done manually either by using mknod utility or using device_create() function. Device nodes are generally associated with a class. Thus, we need to create a class first(using class_create()) and then create device nodes using that class.

这是获取设备节点的示例.

This is an example how to get device node.

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不会在最新的内核中使用, 给NAME?

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()取代.

register_chrdev() is the older way which was using in kernel 2.4, but in kernel 2.6 is replaced with register_chrdev_region() and alloc_chardev_region().

  1. register_chrdev_region(),如果您提前知道所需的设备号.
  2. alloc_chrdev_region()用于动态分配设备号,由内核完成.

这篇关于在cdev_add()在117上成功注册了main之后,char设备出现在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆