On this chapter web page I provide additional materials that align with the content in the book, but were written subsequent to its publication. The content in on this page is focused on Linux Kernel Programming for the BeagleBone platform. This is an advanced topic, and it is best understood if you have a good handle on the materials in Chapters 3, 5 and 6 of the book before tackling it.

Writing a Linux Loadable Kernel Module (LKM)

Writing a Linux Loadable Kernel Module Part 1

In the series of three articles that are linked here, I describe how you can write a Linux loadable kernel module (LKM) for an embedded Linux device. I begin with a straightforward “Hello World!” loadable kernel module (LKM) and work towards developing a module that can control GPIOs on the BeagleBone through the use of IRQs. This is a complex topic, which I have broken up over a number of articles, each providing a practical example and outcome. There are entire books written on this topic, so it is difficult to cover absolutely every aspect. I have aligned the tasks performed against the configurations that are provided within the book.

The first article, “Writing a Linux Kernel Module — Part 1: Introduction,” was released on the 15th April 2015. It is focused on the system configuration, tools and code required to build and deploy a “Hello World!” kernel module. Please click on the image page on the left to view the full article at my blog site.

Get Source Code

Source Code for this Series of Articles

Get Source Code

All of the code for this discussion is available in the GitHub repository for the book Exploring BeagleBone. The code can be viewed publicly at: the ExploringBB GitHub Kernel Project directory, and/or you can clone the repository on your BeagleBone (or other Linux device) by typing:

DoxygenHTML DoxygenPDF

The /extras/kernel/ directory and its sub-directories provide the source code for this series of articles. The auto-generated Doxygen documentation for these code examples is available in HTML format and PDF format.

The second article, “Writing a Linux Kernel Module — Part 2: A Character Device,” was released on the 18th April 2015. This article describes a straightforward character driver that can be used to pass information between a Linux user-space program and a loadable kernel module (LKM), which is running in Linux kernel space. In this example, a C user-space application sends a string to the LKM. The LKM then responds with the message that was sent along with the number of letters that the sent message contains. Later in the article I describe why we need to solve synchronization problems that arise with this approach, and I provide a version of the program that uses mutexes to provide a solution.

The third article “Writing a Linux Kernel Module — Part 3: Buttons and LEDs” is focused on education and training rather than a deep practical need — I believe that if you can read a button press and flash an LED then you are well on your way to interfacing to most digital devices. Therefore, there are three different LKMs described in this article, each with its own individual purpose and needs:

  • Example 1: Button Press, LED Light: In this example an LED lights when a button is pressed — simple! (no not really). To perform this task, the concept of kernel interrupts is introduced and the use of the library of code that can be accessed using linux/gpio.h. This example is used to test the interrupt performance.
  • Example 2: Enhanced Button GPIO Driver: This example is used to introduce kobjects and a mechanism for adding your own entries to Sysfs. This allows you to send data to and receive data from the LKM at run time. The example also introduces the use of timing in kernel code.
  • Example 3: Enhanced LED GPIO Driver: This example is used to flash an LED, which allows for a discussion on Linux kthreads. Essentially, an LED is flashed at a frequency using a kernel module, which can be controlled from Linux user space.

Video Demonstration

A short YouTube video is provided here that presents an overview of the functionality of the LKMs that are developed in this article.

Video 1: A video of the functionality of the LKMs that are described in this article.

Recommended Books on the Content in this Chapter

The following books contain further information on Linux kernel development. It is difficult to get up-to-date books on Linux development, but most of the principles described in these books hold true today: