Linux Device Driver Programming Lecture 58| Pcd driver with multiple devices lseek implementation

 

Pcd driver with multiple devices lseek implementation

 

 

Now, let’s test our lseek method. For that, just type man lseek( as shown in Figure 1) to understand how to use lseek.

Figure 1. Man lseek
Figure 1. Man lseek

 

So, lseek takes three arguments you already know this. The first one is the fd (that is the file descriptor), and offset, and whence information as shown in Figure 2.

Figure 2. Lseek arguement
Figure 2. Lseek arguement

 

The whence information could be anything. SEEK_SET, SEEK_CUR, or SEEK_END. Suppose, if it is SEEK_END, let’s say. The file offset is set to the size of the file plus offset bytes. Here, you have to mention how many bytes you want to seek with reference to this whence information.

 

Let’s try this. Let’s go to our application and activate this code block( shown in Figure 3).

Figure 3. Activate the code block
Figure 3. Activate the code block

 

Activate this for lseek testing. So, let’s try to seek for 10 position, let’s say, from the end. And from the end, the file position tries to go to 10 position further. That is not allowed for our device. Because our devices has some limitation. It has got fixed of memory. 

Figure 4. Seeking for 10 positions
Figure 4. Seeking for 10 positions

 

So, once you reach the end of the memory, you cannot seek further. That’s the end. That’s why I think I would expect our driver to return the error code here. 

If this fails, then there is a problem. So, if return is less than 0, lseek out. Here, out you can do close_fd here. close_fd target you can give. I would give close_fd here.

Figure 5. close_fd
Figure 5. close_fd

You have to close_fd first, then return.

 

Before testing, remember that, so we have to initialize our drivers lseek method in the file operation structure of our driver. 

 

Figure 6. Open your driver source code
Figure 6. Open your driver source code

 

That’s why open your driver source code, go to the file operation structure, and you have to initialize the function pointer for the lseek member element of this structure. .llseek  = pcd_lseek. This you have to include.

Figure 7. Include .llseek = pcd_lseek
Figure 7. Include .llseek = pcd_lseek

 

And after that, unload the driver. Build the project once again and then load back. And now, let’s compile our application, and let’s execute.

Linux device driver: Pcd driver with multiple devices lseek implementation
Figure 8. Remove the module and build the project

 

Then load back as shown in Figure 9.

Linux device driver: Pcd driver with multiple devices lseek implementation
Figure 9. Load the module

 

Here you can see that the open was successful, but the lseek failed. That’s a invalid argument. Because you are trying to seek 10 positions further from the device end, and that’s not allowed for a Driver. I mean, this is entirely driver-dependent.

Linux device driver: Pcd driver with multiple devices lseek implementation
Figure 10. ./devread 100

 

It depends on how you code your driver. So, now let’s change our application.  And let’s do something else here, SEEK_SET. So, you can do SEEK_SET.

Linux device driver: Pcd driver with multiple devices lseek implementation
Figure 11. Change lseek to SEEK_SET

 

Let’s see what happens.

Linux device driver: Pcd driver with multiple devices lseek implementation
Figure 12. ./devread 10

 

There is no data in the device actually. 

Linux device driver: Pcd driver with multiple devices lseek implementation
Figure 13. Putting data into /dev/pcdev-3 and execute ./devread 10

 

Let’s put some data. And now, let’s try to do this. So, you can see that the position was first move to 10 bytes away from the beginning. You can see that the output is not from the beginning of this line.

Output is actually at the offset of 10. That’s why, first, the file position is adjusted to the offset, which we mentioned that is 10 offsets. And from there, it printed the data.

 

What if I give -10 here? (Shown in Figure 14).

Linux device driver: Pcd driver with multiple devices lseek implementation
Figure 14. Lseek with -10 offset

 

Let’s see whether it takes that condition or not. That’s a invalid argument, as shown in Figure 15.

Linux device driver: driver with multiple devices lseek implementation
Figure 15. ./devread 10

 

I think our driver checks that. Try with different combinations of user-level system calls, and with that note, I would like to end this article here and let me know if you have any questions. I will see you in the following article.

 

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.