English 中文(简体)
cdev_add和装置_create function?
原标题:Diffrences between cdev_add and device_create function?

I am newbie on linux device driver development. I can t understand what cdev_add actually do.I viewed some simple char device driver code and I saw, cdev_add and device_create function used together. For instance:

/* Create device class, visible in /sys/class */
dummy_class = class_create(THIS_MODULE, "dummy_char_class");
if (IS_ERR(dummy_class)) {
    pr_err("Error creating dummy char class.
");
    unregister_chrdev_region(MKDEV(major, 0), 1);
    return PTR_ERR(dummy_class);
}

/* Initialize the char device and tie a file_operations to it */
cdev_init(&dummy_cdev, &dummy_fops);
dummy_cdev.owner = THIS_MODULE;
/* Now make the device live for the users to access */
cdev_add(&dummy_cdev, devt, 1);

dummy_device = device_create(dummy_class,
                            NULL,   /* no parent device */
                            devt,    /* associated dev_t */
                            NULL,   /* no additional data */
                            "dummy_char");  /* device name */

该法典中究竟是怎样的。

最佳回答

为了使用个性驱动器,首先,你应在系统中登记。 然后,你应将其暴露于用户空间。

cdev_init and cdev_add functions perform the character device registration. cdev_add adds the character device to the system. When cdev_add function successfully completes, the device is live and the kernel can invoke its operations.

为了从用户空间获取这一装置,请在<代码>/dev/code>上设置一个装置节点。 您通过创建使用sysfs上登记。 利用device_create function.device_create将在dev<<>>>>>>>>>>>上建立装置文档。

http://lwn.net/Kernel/LDD3/“rel=“noretinger”>HCH设备司机,第三版,第3章(Char司机)对这一过程作了很好的描述(_stat_createice_devcreate没有在书内述及)。

问题回答

简短的答复:

  • cdev_add() adds the device to a kernel hash table cdev_map. Once this function succeeds, you can see the device in /proc/devices, and you can call its struct file_operations callbacks.
  • device_add() adds the device to the device model. Once this function succeeds, you can see the device in /sys/devices/.
  • If you enabled devtmpfs, device_add() auto creates device nodes under /dev/. But if you do not use device_add(), you can still create the device nodes manually by mknod. Your device still works.
  • struct cdev and struct device can be used together, or separately.
    • When using together, device_add() can create device nodes for you automatically, and userspace tools like udev has opportunities to be notified with uevent, thus can handle your device with udev rules.

详见中文。 但我也.。 我没有时间将其翻译成英文。 页: 1

struct struct cdev struct device
设备类型 字符设备 通用设备
范围 /dev/ 下的设备节点 设备模型和 sysfs
API cdev_ 开头的函数; dev_t device_ 开头的函数

http://www.un.org/Depts/DGACM/index_spanish.htm 页: 1

  • 关联使用时,可以有所侧重。比如 drivers/char/mem.c 侧重于 struct cdev, 但它也使用了 struct device:
    • 通过 sysfs 导出一小部分信息;
    • 若配置了 devtmpfs, 可以自动在 /dev/ 下创建设备节点;
    • 由于内存字符设备是在内核初始化早期创建的,此时用户空间的 udev 程序远未启动,所以 udev 无法获得相关的 udev 事件。
  • 自己写一个内核模块,只使用 struct cdev、不使用 struct device:
    1. 动态申请设备号,可以在 /proc/devices 里查到,假如为 511.
    2. 手动创建设备节点,如 mknod -m 666 /dev/toy0 c 511 0
    3. echo whatever > /dev/toy0 可以成功。

strdev >, > >, >,<>cdevice_add(>, :

  • 没有驱动程序、没有总线;
  • struct device, 但向设备模型导出的信息只有 3 条,即 class ("mem")、设备号、名字。
// drivers/char/mem.c
static int __init chr_dev_init(void)
{
    // 该函数将调用 device_add() - devtmpfs_create_node() 从而在 /dev/ 下创建设备节点
    device_create(&mem_class, NULL, MKDEV(MEM_MAJOR, minor),
            NULL, devlist[minor].name);
}





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...