Linux Device Driver Programming Lecture 35- Implementing file operation methods

  • Post author:
  • Post category:Blog

 

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). 

Figure 1. Open /include/linux/fs.h
Figure 1. Open /include/linux/fs.h

 

Inside, we’ll find the file_operations structure, which contains member elements that are essentially function pointers (Figure 2).

Figure 2. File operations
Figure 2. File operations

 

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).

Figure 3. Copy some file operations into text file
Figure 3. Copy some file operations into text file

 

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.

Figure 4. Replacing with our method name
Figure 4. Replacing with our method name

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.

file operations in Linux
Figure 5. Creating a variables to pcd_lseek

 

For read, create variables for the file pointer (filp), buffer (buff), count (count), and file position (f_pos) (Figure 6).

Figure 6. Creating a variables to pcd_read
Figure 6. Creating a variables to pcd_read

 

Similarly, for write, create variables for the file pointer (filp), constant char_user *, buffer (buff), count (count), and file position (f_pos) (Figure 7).

file operations in Linux
Figure 7. Creating a variables to pcd_write

 

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.

file operations in Linux
Figure 8. Creating a variables to pcd_open

 

Finally, for release the same, create variables for the inode (inode) and the file pointer (filp) (Figure 9).

file operations in Linux
Figure 9. Creating a variables to pcd_release

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.

file operations in Linux
Figure 10. Adding Return value to the methods

 

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.

file operations in Linux
Figure 11. Copy the methods into main.c

 

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.

file operations in Linux
Figure 12. Make execution

 

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

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.