Implementing file operation methods
In the previous article, we explored character driver file operation methods.
In this article, let’s implement all those drivers file operation methods. The best way to implement these methods is to copy the prototypes from the file operations structure.
Accessing File Operations Structure
To kickstart the implementation process, let’s navigate to the Linux source tree and open the include/linux/fs.h
file (Figure 1).
Inside, we’ll find the file_operations
structure, which contains member elements that are essentially function pointers (Figure 2).
You have to maintain the same prototype. These arguments we actually discussed in the previous article. It’s better if you copy the prototype by referring to this structure.
Implementing File Operation Functions
We’ll be initializing five file operation methods: llseek
, read
, write
, open
, and release
. Copy these member elements to a text file for reference (Figure 3).
Method Name Replacement
Now, let’s replace these placeholders with our custom method names for clarity and consistency. Rename (*llseek)
as pcd_lseek
, (*read)
as pcd_read
, (*write)
as pcd_write
, (*open)
as pcd_open
, and (*release)
as pcd_release
. Remove the semicolon and provide the function body as shown in Figure 4.
These are our file operation methods.
Creating Method Variables
For each method, we need to create variables that match the data types specified in the prototypes.
For llseek
first variable is a pointer to a file object. Let me give a variable name filp
(file pointer). And next is offset
and whence
, as shown in Figure 5.
For read
, create variables for the file pointer (filp
), buffer (buff
), count (count
), and file position (f_pos
) (Figure 6).
Similarly, for write
, create variables for the file pointer (filp
), constant char_user *,
buffer (buff
), count (count
), and file position (f_pos
) (Figure 7).
For open
, first is inode
. It is a pointer variable. This could be anything some people use to underscore, know how some people use Pinard. You can use anything you want. It’s just a variable name. Then is the file pointer (filp
) , as shown in Figure 8.
Finally, for release
the same, create variables for the inode (inode
) and the file pointer (filp
) (Figure 9).
We just created variables for our methods.
Adding Return Values
All these methods have return values, which we’ll set to 0 for now (Figure 10). We can later refine these return values to handle errors more effectively.
And now, let’s copy these methods. Let’s go to our project, open our main.c, and now you have to paste all these methods before this file operation variable. Let’s paste as shown in Figure 11.
And after that, our next step is to initialize this file operation variable with these methods. Before doing that, let’s save and exit and just do make. And you should not through any errors, as shown in Figure 12.
Complete upto here, and in the next article, let’s initialize our file operation variable.
Get the Full Course on Linux Device Driver Here
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1