Character device registration contd.
In the previous article, we completed cdev_init(). Now, we have to register our cdev structure with kernels VFS, that is, the virtual file system. That we do by cdev_add() as shown in Figure 1.
This is a registration. That was just initialization, and this is a real registration.
The first parameter is the cdev structure, you have to mention pointer to your a cdev structure.
And look at the second parameter. This is a device Number. We are registering a cdev structure with a device number. This device number you obtained in alloc_chrdev_region() that you need to mention here.
And the third argument is number of consecutive minor numbers corresponding to this device.
So, in our case, this would be one. Because there is only one device. We have got only one device, only one minor number. That’s why this should be 1 in our case.
And after that, we’ll see more examples of how to use this a kernel API when we have a more than one minor numbers.
Let’s add this function call in our driver (as shown in Figure 2).
The third step is to register the cdev structure with VFS.
Basically, we call it as register a device. Our device is nothing but a cdev structure. cdev_add address of pcd_cdev. Second parameter is a device_number, device number. So, the minor number count is 1. We want to register one device, remember that. That’s why I’m using one here.
Here we have done one mistake. Actually, you should not be doing this owner initialization here. Look at the cdev_init implementation (shown in Figure 3). Here, the first cdev structure is zeroed out, all the member elements are zeroed.
That means if you initialize any member element of the cdev, then it will be invalidated. That’s why we shouldn’t be doing a owner initialization during cdev_init. So, that’s why let’s remove this a code from initialize the cdev structure with fops and let’s put it here in register a device with VFS. Let’s save and exit, and it builds. So far, we have completed these two steps.
In the upcoming article, let’s create the device files.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1