Search code examples
linuxkerneldrivers

How the function works cdev_add()?


Do I understand correctly that when the structure is initialized

struct dev_t dev;
dev = MKDEV(major,minor_first);

I create only the device file, it's right to say - to the node. Next, should I indicate how I will work with this device? To do this, you need the function

cdev_add(&my_ch_dev, dev, minor_count);

after

cdev_init(&my_ch_dev ,&dev_fops);

So, I mean that my driver will work with the created node as a character device? Thanks in advance!


Solution

  • Here is the details how it works

    • dev = MKDEV(major,minor_first); Still kernel doesn't know whether we selected major/minor number or not, so for this you need to register he device by calling register_chrdev_region()
    • register_chrdev_region(dev,minor_count,"AYRAT_DEVICE"); so till now number(major/minor) has reserved the name(dev) so that other driver will not get the same name. Next you need to register your driver with kernel.
    • register with cdev by calling cdev_init(&my_ch_dev ,&dev_fops); Next you need to inform to kernel that we filled all member of struct cdev, so for this use cdev_add().
    • cdev_add(&my_ch_dev, dev, minor_count);