Linux Device Driver Programming Lecture 44- lseek method

  • Post author:
  • Post category:Blog

 

lseek method

 

 

In this article, let’s understand how to implement the drivers lseek method as shown in the figure 1.

Figure 1. Drivers Lseek method
Figure 1. Drivers lseek method

For the lseek, the VFS gives you all these three information that is file object, offset, and whence information that is the origin. This whence can be any below three values. SEEK_SET, SEEK_CUR, or SEEK_END. 

You have to use one switch case in your method in order to understand what exactly is whence. If whence is a SEEK_SET, then the file position, the current file position, will be set to the offset, that is, this field. That’s a meaning of SEEK_SET. Just equate the offset to the current file position.

How do you get the current file position?

The current file position is one of the member element of this file object. That’s why we can dereference, and we can get the file position information.

 

SEEK_CUR means, this(filp->f_pos = filp->f_pos +off) is the meaning of SEEK_CUR. You have to update the file position by adding the offset to the current value of the file position.

What is SEEK_END?

That is seek from the end. You should go to the end first. DEV_MEM_SIZE is our device maximum size, and for that, you have to add the offset, that is SEEK_END. But this may not be valid in our case because our device is an array. It has got only 512 bytes. 

So, moving beyond that may cause problem. That doesn’t seem to be practical in our case. You should put appropriate restriction in your code. But, this is a general meaning of what is SEEK_SET, SEEK_CUR, and SEEK_END. You can change this meaning according to your device and your driver’s implementation. So, we will see that while we code for the lseek method.

I suggest you to code for this method, and I will implement lseek method in the following article.

 

Get the Linux Device Driver Full Course 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.

Leave a Reply