Writing an Example Driver From Scratch: Chapter -5 User access.

Now that we have our driver ready ,we need user application to talk to our device using the driver that we have written.

As the device only support read and write operations, we will write a simple C code that will just open the device and let the user either write into the device or read from the device.

The device node /dev/char_arr that we created by default would allow only root to do read and write. To let any user not having root privileges to read write we will have to change the permissions of the file, i.e of /dev/char_arr, which can be done by

sudo chmod 777 /dev/char_arr

Now to write our user application, we will use the following system calls.

open ( "file name" , "read or write access")

read ("File handle returned by open", "memory buffer to read the data into", "number of bytes of data to be read" )

write("File handle returned by open", "memory buffer to write data into", "number of bytes of data to be written" )

In our case we want to open the file "/dev/char_arr" and we want to do bot read as well as write operations on it, so the second argument will be O_RDWR

open returns a integer value which will act as a file handle to access the file for the rest of the program.

int fd;
fd = open("/dev/char_arr",O_RDWR);

The value of "fd" obtained above will be used to read and write into the file.

To read from the device file the system call would be


Where read_buf is an array of characters, in which we will store the data that we read from the device.

To write into the device file the system call would be


Where write_buf is an array of characters into which we will store the data that we want tor write into the device.

The header files that would be needed to have access to these system calls is fcntl.h

Using the above system calls provide switch case statement where the user can choose to read or write and then call the corresponding system call.

Once you write the "c" code using the above functions, compile it using gcc compiler, assuming the name of the file having the "c" code is user_app.c .

An example code for the user application is given below


Compile it as follows

cc user_app.c -o user_app

This will generate an executable of the name user_app. Run the this executable.

$ ./user_app

Press r to read from device or w to write the device

(Enter w first time)

Enter the data to be written into device

(Enter any data)

Now run the application again

$ ./user_app

Press r to read from device or w to write the device

(Enter r the second time)

The data in the device is (What ever data you entered in the previous run)

From the above example we can see that the user application was able to store data into the device and retrieve from it when the corresponding calls were made.

Your first sample driver and the user application are ready you can try experimenting with them, and explore further.

Note : It would be safer if you did kernel programming in a virtual machine until you are confident about that you are code will not affect the system in any way.

Follow by Email