Linux Device Driver Programming Lecture 54| Pcd driver with multiple devices code implementation part-6

 

Pcd driver with multiple devices code implementation part-6

 

 

Let’s implement the read method. I’m going to activate the code block, as shown in Figure 1.

Figure 1. Pcd_read method
Figure 1. Pcd_read method

 

First of all, we will keep this statement a read requested, and after that, the current file position, and after that, we have to adjust the count. So, you have to compare with the maximum device size. You have to get the  maximum size of the current device.

In the open method, you already saved the device private data in the file pointer. You can extract that from this pointer.

 

Let’s create one structure variable for our device private data  pcdev_data = so you can extract it from file pointer private_data. This is actually a void pointer.

That’s why you have to typecast here. Struct pcdev_private_data*  as shown in Figure 2.

Figure 2. Creating structure variable for our device private data.
Figure 2. Creating structure variable for our device private data.

 

So, int max_size is equal to, now you can use this pointer pcdev_data to extract the size information as shown in Figure 3.

Figure 3. Extract the size information using pcdev_data
Figure 3. Extract the size information using pcdev_data

 

Now, we can use this max_size variable here. If this current file position + count is greater than max_size. 

Figure 4. Adjusting the count
Figure 4. Adjusting the count

 

Then count is equal to max_size minus the current file position(shown in Figure 4).

 

And after that, copy_to_user. In the copy_to_user, the first argument is always the destination. This is the read, so destination will be user, so buf. That’s correct. And this should be the device_buffer. How do you get the device_buffer? You have to point to the device_buffer. So, we can get that from our device  private data structure pcdev_data,dereference that. And let’s point to the buffer address + you have to add the current file position.*f_pos.

Figure 5. Copy to user, Pcd driver with multiple devices code
Figure 5. Copy to user

 

After that, you have to update the current file position here. That is fine. Number of bytes successfully read updated file position, and return number of bytes which have been successfully read. That completes our read method.

Let’s save and exit and let’s try to build it.

 

Let’s complete write method, the same thing you should need as read method (shown in Figure 6).

Figure 6. Write method, Pcd driver with multiple devices code
Figure 6. Write method

 

Copy_from_user. Now the destination is our device_buffer. That’s why, we have to change this first argument here. pcdev_data buffer + the file position. And after that, everything will be as it is.

Figure 7. Copy_from_user, Pcd driver with multiple devices code
Figure 7. Copy_from_user

 

We successfully completed our write method as well. And now, let’s quickly test it(by building).

 

Let’s implement the lseek method as well. Let’s paste these two lines here, as shown in Figure 8. And create one temp variable, lseek requested, current value of the file position.

Figure 8. pcd_lseek, Pcd driver with multiple devices code
Figure 8. pcd_lseek

 

This is whence information offset. Here, just replace this with max_size and offset. So, offset is given from this method itself. You are updating the file position. Everywhere change this DEV_MEM_SIZE with the max_size.

 

Let’s go to the release method. In the release method, we have got nothing to do,  so that’s why we just return 0.

Figure 9. Release method,  Pcd driver with multiple devices code
Figure 9. Release method

 

That completes our exercise and let’s test this in the next article, and after that, let me come back to this check_permission. I will see you in the next 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.