Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux kernel device driver programming [closed]

I want to learn linux kernel device driver programming. So can anyone please post good tutorials pages or links here. I am new to linux kernel environment. I have searched for it but I don't know how to start and which one to read for easy understanding basics. Thanks in advance.

like image 919
Raj Avatar asked Jun 27 '12 06:06

Raj


People also ask

What do you mean by Linux kernel and device drivers?

Device drivers take on a special role in the Linux kernel. They are distinct “black boxes” that make a particular piece of hardware respond to a well-defined internal programming interface; they hide completely the details of how the device works.

What is device driver programming in C?

Device drivers are typically written in C, using the Driver Development Kit (DDK). There are functional and object-oriented ways to program drivers, depending on the language chosen to write in. It is generally not possible to program a driver in Visual Basic or other high-level languages.

How do I program a Linux driver?

There are two ways of programming a Linux device driver: Compile the driver along with the kernel, which is monolithic in Linux. Implement the driver as a kernel module, in which case you won't need to recompile the kernel.


1 Answers

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.

like image 61
eepp Avatar answered Nov 14 '22 02:11

eepp