New to Linux Kernel/Driver Development

New to Linux Kernel/Driver development

Given a C file, you have to look at the functions it calls and data structures it uses, rather than worrying about particular files.

There are two basic routes to developing your own device driver:

  • Take a driver that is similar to yours; strip out the code that isn't applicable to your device, and fill in new code for your device.
  • Start with the very basic pieces of a device driver, and add pieces a little at a time until your device begins to function.

The files that compose your driver will make more sense as you complete this process. Do consider what belongs in each file, but to some extent, dividing a driver among files is more an art than a science. Smaller drivers often fit into just one or two files.

A bit of design may also be good. Consider what you device does, and what your driver will need to do. Based on that, you should be able to map out what functions a device driver will need to have.

I also believe Linux Device Drivers, Third Edition may help you get on your way to driver development.

Linux files themselves include files based on what they do, what layer they are in, and what layer they access of the call stack. The Big Picture truly informs how each file is related to the next.

setting up a development environment for Linux device driver

You can try one more way:

  1. If you have native windows, install virtual machine software such as
    Virtual box. Get your favourite Linux distribution (no bias, just
    an example - Ubuntu) and install it through Virtual box.

  2. Get the latest kernel (or of your choice) from kernel.org.

  3. Choose the platform you want to build this kernel for. E.g arm64 or x86.

  4. In case you do not have real boards (e.g RPi for arm variant), you can use qemu-arm64 or qemu-x86 to run your compiled kernel. This is also a good option when users do not have the boards.

  5. Another good use case for using qemu for the newbie kernel developers is even they write some modules which crashes, then the qemu instance is crashed so no harm.

I think using qemu is a good option for people who starts to learn kernel programming and also want to try writing some of their modules and do not intend to purchase hardware at this point of time.

How safe is that to develop a simple linux kernel module/driver on my own machine?

I had these issues when I started writing device drivers.

  1. System hangs - These happen to every kernel newbie. They're caused by the Linux kernel is doing useless work repeatedly. You won't even be able to move the mouse.
  2. Kernel crashes - Messages like 'System program problem detected' appear when you boot into your system again.
  3. As you start writing advanced drivers like network device drivers, your Ethernet or wireless cards may stop working. Restarting your system might fix this issue, but it might not.

A real kernel developer doesn't mess around with VMs. Don't be afraid to test and code on real machine. I compiled a separate kernel exclusively for testing device drivers. I have one kernel for testing and another for application programming.

If you want to test drivers on a newly built kernel, this is a nice guide on installing a new kernel.

Linux kernel device driver programming

Depends on your current skills. If you're really new to Linux, perhaps you should start with user space system programming with Advanced Linux Programming. You'll get good knowledge of Unix system calls and other concepts such as signals, processes/threads and so on with this free resource. This is a must (understanding the user space API) if you're developing on the kernel side since the role of a kernel is providing services to users in a secure way.

Otherwise one often cited book is Linux Device Drivers, Third Edition (LDD3). Keep in mind that this edition was written at the time of Linux 2.6.10 and some things changed since then. This article shows the differences as 2.6 evolved (until 2.6.31, that is, so not very useful). I should mention martinezjavier/ldd3, which contains example drivers of LDD3 updated for more recent kernels (thanks to 42n4 for pointing that out).

Another interesting book that's not as often cited is Essential Linux Device Drivers. You won't find a free version of this one, but it still features an interesting approach. What I like about this one is it covers lots of different device types and is up-to-date as of 2.6.24, which is a bit better than LDD.

Finally, one great book about the kernel itself (not specifically for drivers) is Understanding the Linux Kernel, 3rd Edition. This covers in-depth kernel facilities and internal mechanisms. It's up-to-date as of 2.6.11.

As for online tutorials, I found this post on Pete's Blog is a really great example. Not only does it show how to create a character device (the most easy kernel driver type, i.e. the one you should start with), it uses modern Linux kernel features in an easy to understand fashion, including:

  • use of udev
  • use of a kernel data structure (FIFO)
  • use of kernel synchronization (mutex)
  • use of Sysfs with custom attributes
  • module options for insmod

Plus: it's aimed at Linux 3.0, which means it's more up-to-date compared to other resources.

You might also like this post about how to create Sysfs entries manually, although the Linux device model will take care of registering your device as a Sysfs entry if you don't need additional nodes or attributes.

Edit: I should add that the best way to learn real Linux device driver programming is to look at actual drivers. There are thousands of drivers in drivers. Start reading and understanding the concept of simple ones like drivers/leds and you will see how rewarding this is.



Related Topics



Leave a reply



Submit