简体   繁体   中英

Character device file not present after reboot in linux

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 /dev dynamically, instead of creating them with the mknod command. The basic idea is to create a custom device class from your module initialization function by calling class_create , and then to add devices to the class by calling device_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 declared static like so:

static struct class *foo_class;

Your module init function needs to create the class and check for errors:

    foo_class = class_create(THIS_MODULE, "foo");
    if (IS_ERR(foo_class)) {
        /* Failed to create class. */
        rc = PTR_ERR(foo_class);
        goto fail_class_create;
    }

(Here, goto fail_class_create jumps 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_create function 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:

    class_destroy(foo_class);

While the class is created, you can create (and destroy) devices belonging to that class (I call it a "class device"), by calling device_create to create a device, and device_destroy to 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:

    struct device *csdev;
    /* ... */
    csdev = device_create(foo_class, hwdev, MKDEV(foo_major, minor), privdata, "foo%u", minor);
    if (IS_ERR(csdev)) {
        /* Failed to create device. */
        rc = PTR_ERR(csdev);
        /* Do any clean-up here. */
    }

(Here, foo_class points to the custom class created earlier; hwdev points to an underlying "hardware device" or can be set to NULL if there is no underlying hardware device; foo_major is your major device number (as allocated by register_chrdev , minor is the minor device number of the device you want to create, privdata is a private data pointer, usually pointing to some private data structure for your device, but it can be NULL ; 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 minor is 0, the device would be dynamically created as /dev/foo0 .

To destroy the device, call device_destroy as follows:

    device_destroy(foo_class, MKDEV(foo_major, minor));

(Here, foo_class , foo_major and minor are the same as passed to device_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:

MODULE_LICENSE("GPL");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM