Linux Device Driver Programming Lecture 42- Understanding write method

 

Understanding write method

 

 

In the previous article, you successfully implemented the read method. Now, let’s discuss the write method.

Figure 1. Write method copy data from user space to kernel space
Figure 1. Write method copy data from user space to kernel space

 

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.

Linux device driver
Figure 2. Steps following in this write method

 

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

 

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.