How to Create a Device File Dynamically in the Device Driver Code in Linux

Beaglebone black board






In this article, we show how to create a device file dynamically in the device driver code in linux.

In the linux operating system, there is a directory, /dev

Each device connected to the linux operating system has its own device file in this directory.

So when you are creating a device driver for a certain device, whether it's a character device, block device, network device, etc., you need to create a device file.

In linux, a device file can be created dynamically through the code you write (instead of manually creating the device file in the /dev directory) to access the hardware.

A user-level program such as udevd can populate the /dev directory with device files dynamically.

The udev program listens to the uevents generated by hot plug events or kernel modules.

When the udev program receives the uevents, it scans the subdirectories of /sys/class looking for the 'dev' files to create device files.

For each such 'dev' file, which represents a combination of major and minor number for a device, the udev program creates a corresponding device file in the /dev directory.

So udev relies on device information (such as the device's file name and major and minor numbers) being exported to the user space through sysfs. udev looks for a file named 'dev' in the /sys/class/ tree of sysfs to determine what major and minor numbers are assigned to a specific device. The driver exports all the information regarding the device (device file name, major, minor number) to sysfs by calling the function device_create().

uevents are generated either when the device driver executes kernel APIs to trigger the dynamic creation of the device files or when a hot-pluggable device such as a USB peripheral is plugged into the system. In our case, we generate the device files through the kernel APIs we use in our code.

To create a device file, we need to use 2 functions (APIs).

The first is the class_create() function.

This function creates a directory in the sysfs directory, so that its path is, /sys/class/

The class_create() function takes in 2 parameters and follows the general format shown below.





The owner will be set to THIS_MODULE

The class_name will be the name that you want to assign to the class. This is a custom name you create that should be descriptive of the character device you are using. In our example, we call it "pcd_class".

Since the next function, device_create(), uses a reference to this class_create() function as an input to one of its parameters, we create a variable name and set it equal to this function in our code.

The next function we need to create a device file is the device_create() function.

This function creates a subdirectory under the /sys/class// directory with the device name.

This function also populates the sysfs entry with the dev file, which consists of the major and minor numbers, separated by a : (colon) character.

So the code related to the class_create() function is shown below.





The device_create() function takes in 5 parameters and follows the general format shown below.





The first parameter to this function is a reference to the class_create() function we previously did.

The second parameter is the pointer to the parent struct device of this new device. Since we do not have one in this case, we set this parameter to NULL. If this device is a child of some parent device, then you would specify that in this field.

The third parameter is the dev_t variable for the character device to be added.

The fourth parameter is private data that may exist for a character device. In this case, there is none, so we specify NULL for this field.

The fifth parameter is name we specify for the device. This is what you want the name of the device to be.

So the code related to the device_create() function is shown below.





These are the 2 functions needed to create a device file for a device in linux.

The full code in our device driver file would be shown as following.

Note that we have not yet defined the methods in the device driver that decide what occurs when a user system call is generated by the user, such as open(), close(), read(), write(), or llseek() methods.





After you build this file without errors, then you must run the following command below to insert the linux character device driver module into the kernel.





The insmod command inserts the character device driver module into the kernel.

It won't show up on the linux operating system until it is inserted into the linux kernel. So this is what we must do.

So after this, we can do a number of checks to see if our device file has really been created.

One of the first most obvious things we can do is check the /dev directory on our linux operating system. The device should show and be registered in this directory after the insmod command is run.

This is shown below.


Checking the /dev directory for device file creation in linux


If you look in this /dev directory, you can see the pcd device.

Note that the device file name that appears in this directory will be that provided in the device_create() function. We named our device "pcd", so that is what shows up in the /dev (devices) directory.

Another thing you can do is go to the /sys/class/pcd_class/pcd directory and type in the following command.





This command will show you the major and minor number of the device you created.

This is shown below.


Checking the sys directory for device file creation in linux using the cat dev command


The other command you can use is shown below.





This command will also give you the major and minor of the device that you created, as well as the device name.

This is shown below.


Checking the sys directory for device file creation in linux using the cat uevent command


Another thing you can do is without having to go into the /dev directory, you can run the following command below.





If the device exists in the /dev directory, then this command will return the major and minor number.

This is shown below.


Checking the dev directory for device file creation in linux using the ls -l command


You can see that the major number is 235 and the minor number is 0.

Now let's run dmesg to see the output of our kernel module.

All you have to do is go to the linux command terminal and type in, dmesg

The output of the program is as follow shown below.


Output of a linux character device driver kernel module with the insmod command


Since we created our linux kernel module to output the major and minor device number, we see this in the output.

Now let us remove this character device driver kernel module from the kernel.

This is done using the following command shown below.





Now, in the linux terminal module, type in, dmesg


Output of a linux character device driver kernel module with the rmmod command


You can see that the module has been unloaded.

So this is how to create a device file dynamically in the device driver code in linux.



Related Resources





HTML Comment Box is loading comments...