Linux Device Driver Development
eBook - ePub

Linux Device Driver Development

John Madieu

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

Linux Device Driver Development

John Madieu

Book details
Book preview
Table of contents
Citations

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

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
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.
Do you support text-to-speech?
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.
Is Linux Device Driver Development an online PDF/ePUB?
Yes, you can access Linux Device Driver Development by John Madieu in PDF and/or ePUB format, as well as other popular books in Informatique & Administration du système. We have over one million books available in our catalogue for you to explore.

Information

Year
2022
ISBN
9781803235943

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