Understanding write method
In the previous article, you successfully implemented the read method. Now, let’s discuss the write method.
What is write?
Write means, copy data from user space to the kernel space.
In our case, we have to copy data from the user buffer to our device memory buffer. For this, we are going to use copy_from_user.
And the steps what you should be following in this a write method is exactly similar to what you have done in read method. Instead of copy_to_user, you have to use copy_from_user. That’s it. Other steps will be the same.
And you should be careful with the return value here. You should return number of bytes successfully written or error code. You should not return 0 here. So, when you adjust this ‘count’ and if count turns be 0, that means there is no memory left in the device memory buffer.
That’s why you should be returning the error code ENOMEM. In the case of read, it was fine. You can return 0 saying nothing to read. But in the case of write, you cannot return 0 saying nothing to write. You cannot do that. I hope you can take care of that scenario. Not able to write anything means no memory left on the device. We should return a appropriate error code. I would suggest you to go back to the code and try to implement this write method.
Get the Full Course On Linux Device Driver Here.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1