Linux Device Driver Development - Second Edition

Book description

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 mechanisms

Key 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 subsystems

Book Description

Linux 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 subsystems

Who this book is for

This 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.

Table of contents

  1. Linux Device Driver Development Second Edition
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Section 1 -Linux Kernel Development Basics
  7. Chapter 1: Introduction to Kernel Development
    1. Setting up the development environment
      1. Setting up the host machine
      2. Getting the sources
    2. Configuring and building the Linux kernel
      1. Specifying compilation options
      2. Understanding the kernel configuration process
      3. Building the Linux kernel
    3. Building and installing modules
    4. Summary
  8. Chapter 2: Understanding Linux Kernel Module Basic Concepts
    1. An introduction to the concept of modules
      1. Case study – module skeleton
    2. Building a Linux kernel module
      1. Understanding the Linux kernel build system
      2. Out-of-tree building
      3. In-tree building
    3. Handling module parameters
    4. Dealing with symbol exports and module dependencies
      1. An introduction to the concept of module dependencies
    5. Learning some Linux kernel programming tips
      1. Error handling
      2. Message printing – goodbye printk, long life dev_*, pr_*, and net_* APIs
    6. Summary
  9. Chapter 3: Dealing with Kernel Core Helpers
    1. Linux kernel locking mechanisms and shared resources
      1. Spinlocks
      2. Mutexes
      3. Trylock methods
    2. Dealing with kernel waiting, sleeping, and delay mechanisms
      1. Wait queue
      2. Simple sleeping in the kernel
      3. Kernel delay or busy waiting
    3. Understanding Linux kernel time management
      1. The concepts of clocksource, clockevent, and tick device
      2. Using standard kernel low-precision (low-res) timers
      3. High-resolution timers (hrtimers)
    4. Implementing work-deferring mechanisms
      1. Softirqs
      2. Tasklets
      3. Workqueues
      4. Workqueues' new generation
    5. Kernel interrupt handling
      1. Designing and registering an interrupt handler
    6. Summary
  10. Chapter 4: Writing Character Device Drivers
    1. The concept of major and minor
    2. Character device data structure introduction
      1. An introduction to device file operations
      2. File representation in the kernel
    3. Creating a device node
      1. Device identification
      2. Registration and deregistration of character device numbers
      3. Initializing and registering a character device on the system
    4. Implementing file operations
      1. Exchanging data between the kernel space and user space
      2. Implementing the open file operation
      3. Implementing the release file operation
      4. Implementing the write file operation
      5. Implementing the read file operation
      6. Implementing the llseek file operation
      7. The poll method
      8. The ioctl method
    5. Summary
  11. Section 2 - Linux Kernel Platform Abstraction and Device Drivers
  12. Chapter 5: Understanding and Leveraging the Device Tree
    1. Understanding the basic concept of the device tree mechanism
      1. The device tree naming convention
      2. An introduction to the concept of aliases, labels, phandles, and paths
      3. Understanding overwriting nodes and properties
      4. Device tree sources and compilers
    2. Representing and addressing devices
      1. Handling SPI and I2C device addressing
      2. Memory-mapped devices and device addressing
    3. Handling resources
      1. The struct resource
      2. Extracting application-specific data
    4. Summary
  13. Chapter 6: Introduction to Devices, Drivers, and Platform Abstraction
    1. Linux kernel platform abstraction and data structures
      1. Device base structure
      2. Device driver base structure
      3. Device/driver matching and module (auto) loading
      4. Device declaration – populating devices
      5. Bus structure
    2. Device and driver matching mechanism explained
      1. Case study – the OF matching mechanism
    3. Summary
  14. Chapter 7: Understanding the Concept of Platform Devices and Drivers
    1. Understanding the platform core abstraction in the Linux kernel
    2. Dealing with platform devices
      1. Allocating and registering platform devices
      2. How not to allocate platform devices to your code
      3. Working with platform resources
    3. Platform driver abstraction and architecture
      1. Probing and releasing the platform devices
      2. Provisioning supported devices in the driver
      3. Driver initialization and registration
    4. Example of writing a platform driver from scratch
    5. Summary
  15. Chapter 8: Writing I2C Device Drivers
    1. I2C framework abstractions in the Linux kernel
      1. A brief introduction to struct i2c_adapter
      2. I2C client and driver data structures
      3. I2C communication APIs
    2. The I2C driver abstraction and architecture
      1. Probing the I2C device
      2. Implementing the i2c_driver.remove method
      3. Driver initialization and registration
      4. Provisioning devices in the driver
      5. Instantiating I2C devices
    3. How not to write I2C device drivers
    4. Summary
  16. Chapter 9: Writing SPI Device Drivers
    1. Understanding the SPI framework abstractions in the Linux kernel
      1. Brief introduction to struct spi_controller
      2. The struct spi_device structure
      3. The spi_driver structure
      4. The message transfer data structures
      5. Accessing the SPI device
    2. Dealing with the SPI driver abstraction and architecture
      1. Probing the device
      2. Provisioning devices in the driver
      3. Implementing the spi_driver.remove method
      4. Driver initialization and registration
      5. Instantiating SPI devices
    3. Learning how not to write SPI device drivers
    4. Summary
  17. Section 3 - Making the Most out of Your Hardware
  18. Chapter 10: Understanding the Linux Kernel Memory Allocation
    1. An introduction to Linux kernel memory-related terms
      1. Kernel address space layout on 32-bit systems – the concept of low and high memory
      2. An overview of a process address space from the kernel
      3. Understanding the concept of VMA
    2. Demystifying address translation and MMU
      1. Page lookup and the TLB
      2. The page allocator
      3. The slab allocator
      4. kmalloc family allocation
      5. vmalloc family allocation
      6. A short story about process memory allocation under the hood
    3. Working with I/O memory to talk to hardware
      1. PIO device access
      2. MMIO device access
    4. Memory (re)mapping
      1. Understanding the use of kmap
      2. Mapping kernel memory to user space
    5. Summary
  19. Chapter 11: Implementing Direct Memory Access (DMA) Support
    1. Setting up DMA mappings
      1. The concept of cache coherency and DMA
      2. Memory mappings for DMA
    2. Introduction to the concept of completion
    3. Working with the DMA engine's API
      1. A brief introduction to the DMA controller interface
      2. Handling device DMA addressing capabilities
      3. Requesting a DMA channel
      4. Configuring the DMA channel
      5. Configuring the DMA transfer
      6. Submitting the DMA transfer
      7. Issuing pending DMA requests and waiting for callback notification
    4. Putting it all together – Single-buffer DMA mapping
    5. A word on cyclic DMA
    6. Understanding DMA and DT bindings
      1. Consumer binding
    7. Summary
  20. Chapter 12: Abstracting Memory Access – Introduction to the Regmap API: a Register Map Abstraction
    1. Introduction to the Regmap data structures
      1. Understanding the struct regmap_config structure
    2. Handling Regmap initialization
    3. Using Regmap register access functions
      1. Bulk and multiple registers reading/writing APIs
      2. Understanding the Regmap caching system
    4. Regmap-based SPI driver example – putting it all together
      1. A Regmap example
    5. Leveraging Regmap from the user space
    6. Summary
  21. Chapter 13: Demystifying the Kernel IRQ Framework
    1. Brief presentation of interrupts
    2. Understanding interrupt controllers and interrupt multiplexing
    3. Diving into advanced peripheral IRQ management
      1. Understanding IRQ and propagation
      2. Chaining IRQs
    4. Demystifying per-CPU interrupts
      1. SGIs and IPIs
    5. Summary
  22. Chapter 14: Introduction to the Linux Device Model
    1. Introduction to LDM data structures
      1. The bus data structure
      2. The driver data structure
      3. The device data structure
    2. Getting deeper inside LDM
      1. Understanding the kobject structure
      2. Understanding the kobj_type structure
      3. Understanding the kset structure
      4. Working with non-default attributes
    3. Overview of the device model from sysfs
      1. Creating device-, driver-, bus- and class-related attributes
      2. Making a sysfs attribute poll- and select-compatible
    4. Summary
  23. Section 4 - Misc Kernel Subsystems for the Embedded World
  24. Chapter 15: Digging into the IIO Framework
    1. Introduction to IIO data structures
      1. Understanding the struct iio_dev structure
      2. Understanding the struct iio_info structure
      3. The concept of IIO channels
      4. Distinguishing channels
      5. Putting it all together – writing a dummy IIO driver
    2. Integrating IIO triggered buffer support
      1. IIO trigger and sysfs (user space)
      2. IIO buffers
      3. Putting it all together
    3. Accessing IIO data
      1. Single-shot capture
      2. Accessing the data buffer
    4. Dealing with the in-kernel IIO consumer interface
      1. Consumer kernel API
    5. Writing user-space IIO applications
      1. Scanning and creating an IIO context
      2. Walking through and managing IIO devices
      3. Walking through and managing IIO channels
      4. Working with a trigger
      5. Creating a buffer and reading data samples
    6. Walking through user-space IIO tools
    7. Summary
  25. Chapter 16: Getting the Most Out of the Pin Controller and GPIO Subsystems
    1. Introduction to some hardware terms
    2. Introduction to the pin control subsystem
    3. Dealing with the GPIO controller interface
      1. Writing a GPIO controller driver
      2. GPIO controller bindings
    4. Getting the most out of the GPIO consumer interface
      1. Integer-based GPIO interface – now deprecated
      2. Descriptor-based GPIO interface: the new and recommended way
    5. Learning how not to write GPIO client drivers
      1. Goodbye to the legacy GPIO sysfs interface
      2. Welcome to the Libgpiod GPIO library
      3. The GPIO aggregator
    6. Summary
  26. Chapter 17: Leveraging the Linux Kernel Input Subsystem
    1. Introduction to the Linux kernel input subsystem – its data structures and APIs
    2. Allocating and registering an input device
    3. Using polled input devices
    4. Generating and reporting input events
    5. Handling input devices from the user space
    6. Summary
    7. Why subscribe?
  27. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: Linux Device Driver Development - Second Edition
  • Author(s): John Madieu
  • Release date: April 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803240060