A device is an impotent part of our day-to-day life. For example, think about a printer, it can print your documents. But, without a printer driver, it does not work properly. So, let’s talk about how to build your own driver for Linux.

Introduction

The advantage of free operating systems is, anyone can do anything for the source code. Such an OS source is Linux. You can get any flavor of Linux OS. It may be Ubuntu, Fedora, Kali, cent, Debian, or some other distro. However, most of them are working on the same principles. Therefore you can try any Linux distro for testing on Device drivers making with me.

Warnings!

Before we start talking about how to write a device driver, let’s talk about the things that we need to be careful, when we writing the device drivers.

When we writing a Linux Device Driver, we are writing the code for the Kernel. First, we write the code, then we build it and then we attach (hope attach is the suitable word to understand) it to the Kernel. Also, I hope you know that the Kernel is the core of your OS. If you are doing something stupid with your driver, then the Kernel will get crashed. Or my be your hardware will get damaged!

The most common Kernel crashing situation is De-referencing a Null pointer. Normally after we made the device driver, we communicate with the Kernel space and the user space with pointers. Therefore this error can happen mistakenly. By debugging the code we can go through this problem. Also, after crashing your kernel, you can just restart your OS to reset the things.

The second dangerous thing that could happen while making the driver is, writing data on wrong memory locations. If you write data on wrong memory locations, sometimes the driver may work as you wish, or else it will crash the Kernel. But doing so sometimes will make harmful things for other processors. Therefore be careful with writing on the wrong memory locations.

Next, the worst case which could happen is damaging your hardware. Also, you can refer GPIOs warnings to understand more about this case. If we change the directions of GPIOs in the wrong manner, then your hardware will damage. When we compiling a code, it normally shows the errors. But when we compiling the kernel code, it will not show the wrong parts sometimes. Therefore be careful about that too.

The following figure will show some cases of GPIOs directions.

Figure copy from http://www.udoo.org/docs/img/UDOO_GPIO_Warnings.jpg

Safety

So, before you crash your hardware, let’s see what can we do for safety.
Simply we can use a VM for testing the driver codes. Also, we can use a Raspberry pi board or UDOO board for testing.

So these are the things we need to be careful and pre-set for testing on Linux drivers. Also, you need to know about the C language for implementing drivers.

Yeah, That’s it I think. Let’s write a code form next post 🙂

***Please note that all of the content on this post was originally published on champlnx.blogspot.com. We have migrated these posts to www.champlnx.com for your convenience and continued access.

Leave a Reply

Your email address will not be published. Required fields are marked *