Linux Device Driver Programming Lecture 36- File operations structure initialization

  • Post author:
  • Post category:Blog

 

File operations structure initialization

 

 

In this article, let’s discuss the next step is to initialize this file operations variable.

This is just a simple structure variable initialization. Structure variable initialization you can do in many ways.

In this article, we will discuss the crucial step of initializing the file operations variable in Linux driver development. This process involves setting up a structure variable with function pointers for various file operations. There are two common methods for initializing such structures.

One is you can initialize at the time of creation itself. Or you can also initialize this variable inside the module initialization function.

Initialize the file operation variable inside the module initialization
Figure 1. Initialize the file operation variable inside the module initialization

 

For each file operation, you can initialize it in a straightforward manner. For example, pcd_fops.open = pcd_open; is used for the open operation. Similar assignments can be made for read, write, lseek, and other operations.

But, in kernel programming, when we deal with file operations structure, we usually don’t initialize like this. We do it during the file operations variable creation itself. You create the variable and initialize it.

 

How to initialize the structure at the time of creation?

File operations structure initialization.
Figure 2. Structure member elements initialization

 

There are two ways. Now, let me explain that. Let’s say there is a structure called struct CarModel, as shown in Figure 2. And this structure has got 4 member elements. And let’s say I want to create and initialize a variable. I create a variable carBMW. 

struct CarModel CarBMW={2021,15000,220,1330}; //c89 method. Order is important 

Here, CarBMW is a variable of type struct CarModel. And I want to initialize it.

What I do is, I just use the curly brackets, and I give the values. This initialization method is introduced in C89 standard. Here the problem is order is important. You cannot randomly assign these values. Here, 2021 will go to carNumber. 15K will go carPrice. 220 will go to carMaxSpeed. So, the order is important.

 

Struct CarModel CarBMW={.carNumber=2021,.carWeight=1330,.carMaxSpeed=220,.carPrice =15000 }; //C99 method using designated initializers

In C99 method, a new way is introduced. Here, the order is not important. Because it uses designated initializers. The syntax is you use a dot(.) operator, and use a member element name, and initialize that with the value. I can keep this a .carNumber = 2021 at the end, or I can keep it anywhere. Here the order is not important.

For our file operations structure, it has got a lot of member elements. For this example, it doesn’t matter whether you use C89 method or C90 method. Because there are only four member elements. We can maintain the order. But, for the linux file operation structure, it is not possible to maintain the order. It is possible, but it would become difficult. That’s why a let’s use the C99 method. That is using the dot(.) operator to initialize the member elements.

 

Let’s go back to our code. What you should be doing is here, just is equal to let’s open the bracket and let’s use the dot operator. You can pick any member element.

Let’s start with open. .open=pcd_open. After that, you have to give a comma. Give a comma .write=pcd_write. .read =pcd_read.

Do you notice how convenient it is?

I’m not referring to the file operations structure while writing this. I just need to know the name of the memory element. That’s it. Order is not important. .

llseek = pcd_lseek, after that owner field, is important. You should initialize this  to THIS_MODULE.  End this initialization and give a semicolon, as shown in Figure 3.

File operations structure initialization.
Figure 3. Structure initialization

 

Complete up to here, and I’ll see you in the following articles.

 

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.