How to Test a Linux Character Device Driver

Beaglebone black board






In this article, we show how to test a linux character device driver.

Previously, we created a pseudo character device driver

Now we need to be able to test out our driver to see if our device driver code functions properly.

We can test out various file operation methods that we have created, including our read and write file operations.

So the first thing you have to do is obviously make sure that your device driver code builds without any errors.

So go into the directory containing your device driver and build it to make sure there are no errors.

When you build your character device driver, you should see an output similar to that shown below.


Building the linux character device driver


Now that you have built the character device driver with no errors, we can insert the character device driver module into the kernel using the insmod command.

If you named your file pcd, then you need to run the statement shown below.





This is shown below.


Inserting the linux character device driver into the kernel


After this, you can run the dmesg command and see the output of the character device driver code.





After you run this code, you should see an output similar to that below.


Checking the output of the linux character device driver with dmesg


So we can see our device number above, which is 235:0

Next, let's go into testing some of the file operation methods in our device driver code.


Testing the Write File Operation Method

So now we test the write file operation method in the linux terminal by using the echo statement.

The first thing you should do to make the code work is to go in as the root user. This is done using the statement below.





Now we will write the statement, "Character Device First Written Statement", to the character device file.

This is done with the statement below.





This is shown below.


Writing to a linux character device file


To see what was performed when we ran this statement, run dmesg again.

You should get an output similar to that below.


Write to the linux character device file successful shown with dmesg


So you can see that the file was successfully opened.

Then you see the statement, Write requested for 41 bytes

You see that the 41 bytes was successfully written.

Then the file is closed.

So, basically our write statement was successful.

Our write statement is 41 bytes, which is why this number of bytes was written.


Testing the Read File Operation Method

So now we test the read file operation method in the linux terminal by using the cat statement.

All we have to do is enter in the cat command followed by the path to the file on our linux computer. This is shown below.





This is shown below.


Reading from a linux character device file


So we read from the linux character device using the cat command and we get what we wrote previously to the device file using the echo command.

If we run dmesg | tail, we can see exactly what was done in this process.

This is shown below.


Read from linux character device file successful shown with dmesg


So let's go through what is going on in this code.

First, the file was opened successfully.

A read was requested for 131072 bytes.

This amount of bytes is not because we requested a read for this amount of bytes, but because it's just how the cat application is written.

Because our file only has 512 bytes in total, this is the number of bytes which are read successfully.

The reason this command is run again is because cat originally requests for 131072, so it runs again after the file position is updated to 512 bytes.

Because there are no bytes after byte 512, it reads 0 bytes.

The file then is closed.

So this is how to test the write and read file operation methods of a character device driver.



Related Resources





HTML Comment Box is loading comments...