Linux Device Driver Development
eBook - ePub

Linux Device Driver Development

  1. 708 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Linux Device Driver Development

About this book

Get up to speed with the most important concepts in driver development and focus on common embedded system requirements such as memory management, interrupt management, and locking mechanismsKey Featuresโ€ข Write feature-rich and customized Linux device drivers for any character, SPI, and I2C deviceโ€ข Develop a deep understanding of locking primitives, IRQ management, memory management, DMA, and so onโ€ข Gain practical experience in the embedded side of Linux using GPIO, IIO, and input subsystemsBook DescriptionLinux is by far the most-used kernel on embedded systems. Thanks to its subsystems, the Linux kernel supports almost all of the application fields in the industrial world. This updated second edition of Linux Device Driver Development is a comprehensive introduction to the Linux kernel world and the different subsystems that it is made of, and will be useful for embedded developers from any discipline.You'll learn how to configure, tailor, and build the Linux kernel. Filled with real-world examples, the book covers each of the most-used subsystems in the embedded domains such as GPIO, direct memory access, interrupt management, and I2C/SPI device drivers. This book will show you how Linux abstracts each device from a hardware point of view and how a device is bound to its driver(s). You'll also see how interrupts are propagated in the system as the book covers the interrupt processing mechanisms in-depth and describes every kernel structure and API involved. This new edition also addresses how not to write device drivers using user space libraries for GPIO clients, I2C, and SPI drivers.By the end of this Linux book, you'll be able to write device drivers for most of the embedded devices out there. What you will learnโ€ข Download, configure, build, and tailor the Linux kernelโ€ข Describe the hardware using a device treeโ€ข Write feature-rich platform drivers and leverage I2C and SPI busesโ€ข Get the most out of the new concurrency managed workqueue infrastructureโ€ข Understand the Linux kernel timekeeping mechanism and use time-related APIsโ€ข Use the regmap framework to factor the code and make it genericโ€ข Offload CPU for memory copies using DMAโ€ข Interact with the real world using GPIO, IIO, and input subsystemsWho this book is forThis Linux OS book is for embedded system and embedded Linux enthusiasts/developers who want to get started with Linux kernel development and leverage its subsystems. Electronic hackers and hobbyists interested in Linux kernel development as well as anyone looking to interact with the platform using GPIO, IIO, and input subsystems will also find this book useful.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, weโ€™ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere โ€” even offline. Perfect for commutes or when youโ€™re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Linux Device Driver Development by John Madieu in PDF and/or ePUB format, as well as other popular books in Computer Science & Operating Systems. We have over one million books available in our catalogue for you to explore.

Information

Section 1 -Linux Kernel Development Basics

This section helps you make your first step into Linux kernel development. Here, we introduce the Linux kernel infrastructure (its structure and its build system), its compilation, and device driver development. As a mandatory step, we introduce the most used concepts kernel developers must know, such as sleeping, locking, basic work scheduling, and interrupt handling mechanisms. Last, we introduce the indispensable character device drivers, allowing interactions between kernel space and user space, via either standard system calls or an extended set of commands.
The following chapters will be covered in this section:
  • Chapter 1, Introduction to Kernel Development
  • Chapter 2, Understanding Linux Kernel Module Basic Concepts
  • Chapter 3, Dealing with Kernel Core Helpers
  • Chapter 4, Writing Character Device Drivers

Chapter 1: Introduction to Kernel Development

Linux started as a hobby project in 1991 by a Finnish student, Linus Torvalds. The project has gradually grown and continues to do so, with roughly a thousand contributors around the world. Nowadays, Linux is a must, in embedded systems as well as on servers. A kernel is a central part of an operating system, and its development is not straightforward. Linux offers many advantages over other operating systems; it is free of charge, well documented with a large community, is portable across different platforms, provides access to the source code, and has a lot of free open source software.
This book will try to be as generic as possible. There is a special topic, known as the device tree, that is not a full x86 feature yet. This topic will be dedicated to ARM processors, especially those that fully support the device tree. Why those architectures? Because they are mostly used on desktops and servers (for x86), as well as embedded systems (ARM).
In this chapter, we will cover the following topics:
  • Setting up the development environment
  • Understanding the kernel configuration process
  • Building your kernel

Setting up the development environment

When you're working in embedded system fields, there are terms you must be familiar with, before even setting up your environment. They are as follows:
  • Target: This is the machine that the binaries resulting from the build process are produced for. This is the machine that is going to run the binary.
  • Host: This is the machine where the build process takes place.
  • Compilation: This is also called native compilation or a native build. This happens when the target and the host are the same; that is, when you're building on machine A (the host) a binary that is going to be executed on the same machine (A, the target) or a machine of the same kind. Native compilation requires a native compiler. Therefore, a native compiler is one where the target and the host are the same.
  • Cross-compilation: Here, the target and the host are different. It is where you build a binary from machine A (the host) that is going to be executed on machine B (the target). In this case, the host (machine A) must have installed the cross-compiler that supports the target architecture. Thus, a cross-compiler is a compiler where the target is different from the host.
Because embedded computers have limited or reduced resources (CPU, RAM, disk, and so on), it is common for the hosts to be x86 machines, which are much more powerful and have far more resources to speed up the development process. However, over the past few years, embedded computers have become more powerful, and they tend to be used for native compilation (thus used as the host). A typical example is the Raspberry Pi 4, which has a powerful quad-core CPU and up to 8 GB of RAM.
In this chapter, we will be using an x86 machine as the host, either to create a native build or for cross-compilation. So, any "native build" term will refer to an "x86 native build." Due to this, I'm running Ubuntu 18.04.
To quickly check this information, you can use the following command:
lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 18.04.5 LTS
Release: 18.04
Codename: bionic
My computer is an ASUS RoG, with a 16 core AMD Ryzen CPU (you can use the lscpu command to pull this information out), 16 GB of RAM, 256 GB of SSD, and a 1 TB magnetic hard drive (information that you can obtain using the df -h command). That said, a quad-core CPU and 4 or 8 GB of RAM could be enough, but at the cost of an increased build duration. My favorite editor is Vim, but you are free to use the one you are most comfortable with. If you are using a desktop machine, you could use Visual Studio Code (VS Code), which is becoming widely used.
Now that we are familiar with the compilation-related keywords we will be using, we can start preparing the host machine.

Setting up the host machine

Before you can start the development process, you need to set up an environment. The environment that's dedicated to Linux development is quite simple โ€“ on Debian-based systems, at least (which is our case).
On the host machine, you need to install a few packages, as follows:
$ sudo apt update
$ sudo apt install gawk wget git diffstat unzip \
texinfo gcc-multilib build-essential chrpath socat \
libsdl1.2-dev xterm ncurses-dev lzop libelf-dev make
In the preceding code, we installed a few development tools and some mandatory libraries so that we have a nice user interface when we're configuring the Linux kernel.
Now, we need to install the compiler and the tools (linker, assembler, and so on) for the build process to work properly and produce the executable for the target. This set of tools is called Binutils, and the compiler + Binutils (+ other build-time dependency libraries if any) combo is called toolchain. So, you need to understand what is meant by "I need a toolchain for <this> architecture" or similar sentences.

Understanding and installing toolchains

Before we can start compiling, we need to install the necessary packages and tools for native or ARM cross-compiling; that is, the toolchains. GCC is the compiler that's supported by the Linux kernel. A lot of macros that are defined in the kernel are GCC-related. Due to this, we will use GCC as our (cro...

Table of contents

  1. Linux Device Driver Development Second Edition
  2. Contributors
  3. Preface
  4. Section 1 -Linux Kernel Development Basics
  5. Chapter 1: Introduction to Kernel Development
  6. Chapter 2: Understanding Linux Kernel Module Basic Concepts
  7. Chapter 3: Dealing with Kernel Core Helpers
  8. Chapter 4: Writing Character Device Drivers
  9. Section 2 - Linux Kernel Platform Abstraction and Device Drivers
  10. Chapter 5: Understanding and Leveraging the Device Tree
  11. Chapter 6: Introduction to Devices, Drivers, and Platform Abstraction
  12. Chapter 7: Understanding the Concept of Platform Devices and Drivers
  13. Chapter 8: Writing I2C Device Drivers
  14. Chapter 9: Writing SPI Device Drivers
  15. Section 3 - Making the Most out of Your Hardware
  16. Chapter 10: Understanding the Linux Kernel Memory Allocation
  17. Chapter 11: Implementing Direct Memory Access (DMA) Support
  18. Chapter 12: Abstracting Memory Access โ€“ Introduction to the Regmap API: a Register Map Abstraction
  19. Chapter 13: Demystifying the Kernel IRQ Framework
  20. Chapter 14: Introduction to the Linux Device Model
  21. Section 4 - Misc Kernel Subsystems for the Embedded World
  22. Chapter 15: Digging into the IIO Framework
  23. Chapter 16: Getting the Most Out of the Pin Controller and GPIO Subsystems
  24. Chapter 17: Leveraging the Linux Kernel Input Subsystem
  25. Other Books You May Enjoy