In order to write a Linux device driver (Character-device, Block-device, etc...), it is necessary to create a kernel module that has an entry and exit points.
By itself, the kernel module does nothing; it has no meaningful way to communicate with the userspace.
Using the entry point it is possible to create a new character-device, for example, which is then used to communicate with the userspace.
Building and running the module
In order to compile the driver, it is necessary to have the Linux Kernel source tree.
Assuming the sources are at /lib/modules/<kernel-version>, the following Makefile will compile the file driver.c into the driver.ko Kernel Object
Notice how this Makefile calls make in the build directory of the Kernel.
When the compilation step finishes successfully, the src directory of the driver will look somewhat like this:
In order to "run" the module, it is necessary to insert into the running kernel:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0