I have just started to explore Linux character device drivers. I have made a simple kernel module in which i am registering the device with register_chrdev() function. I have passed 0 as argument to the function and the kernel returns me the free major number available. After that i am using mknod command to create a character device file with the returned major number and i am successfully able to do that. I have loaded the driver into the kernel and the communication between driver ,device file and user space application is fine.
The problem is that when i reboot my system the character device file(created using mknod) is not there in /dev directory.
So please suggest the solution to this problem so that my character device file will present in /dev directory even after reboot.
One solution is to make your driver create the files in
/devdynamically, instead of creating them with themknodcommand. The basic idea is to create a custom device class from your module initialization function by callingclass_create, and then to add devices to the class by callingdevice_create.You'll need a variable of type
struct class *to hold a pointer to the custom class. This variable needs to be accessed by various functions in your module, so needs to be declared outside any functions, and would normally be declaredstaticlike so:Your module init function needs to create the class and check for errors:
(Here,
goto fail_class_createjumps to a label to clean up anything done so far before returning an error. If you don't like this "on error goto" pattern, feel free to clean up explicitly here before returning an error.)If the
class_createfunction is successful, it should be destroyed when no longer needed in your module exit function, and also as part of cleaning up if there are errors further down in your module init function:While the class is created, you can create (and destroy) devices belonging to that class (I call it a "class device"), by calling
device_createto create a device, anddevice_destroyto destroy a device. Both of those functions use a device node number (a combination of major and minor device number) to specify the class device to be created or destroyed. For example a class device can be created as follows:(Here,
foo_classpoints to the custom class created earlier;hwdevpoints to an underlying "hardware device" or can be set toNULLif there is no underlying hardware device;foo_majoris your major device number (as allocated byregister_chrdev,minoris the minor device number of the device you want to create,privdatais a private data pointer, usually pointing to some private data structure for your device, but it can beNULL; the remaining parameters are consist of a printf-style format string plus any extra parameters needed by the format string to create the device name.)In the above example, if
minoris 0, the device would be dynamically created as/dev/foo0.To destroy the device, call
device_destroyas follows:(Here,
foo_class,foo_majorandminorare the same as passed todevice_create.)The above functions are exported as GPL only, so if you want to use them, your module will need to declare its licence using the following declaration: