4

I have written basic char driver.

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

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 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.

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

Thank you.

Vijay Kalyanam
  • 315
  • 3
  • 14
  • Possible duplicate of [Create a device node in code](http://stackoverflow.com/questions/5970595/create-a-device-node-in-code) – Tsyvarev Dec 25 '16 at 16:19

1 Answers1

5

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

Now, while this is done, it doesn't mean that the device node is created for you.

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 is not going to be use in the latest kernel, where we give NAME?

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() if you know ahead of time exactly which device numbers you want.
  2. alloc_chrdev_region() for dynamically allocated a device number, done by kernel.
Sumit Gemini
  • 1,766
  • 1
  • 14
  • 18
  • 1
    Thanks a lot Sumit. I was referring to 2.4 kernel doc and entered into loop. Your explanation cleared off confusions in a single shot. – Vijay Kalyanam Dec 26 '16 at 09:38
  • @VijayKalyanam Welcome :-) – Sumit Gemini Dec 26 '16 at 09:50
  • Hi Sumit, I have modified the code according to new kernel behavior. still i don't see device is not listed under /dev/. This is the code. ------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------- – Vijay Kalyanam Dec 27 '16 at 09:52
  • 1
    Hi Sumit, I got the resolution. there is a bug in my code. Corrected it. Now my own module is working and is operation. Thank you for your response Bro. – Vijay Kalyanam Dec 28 '16 at 07:44