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.
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.
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.
Now, we can use this max_size variable here. If this current file position + count is greater than max_size.
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.
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).
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.
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.
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.
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