Mastering Linux Device Driver Development

Book description

Master the art of developing customized device drivers for your embedded Linux systems

Key Features

  • Stay up to date with the Linux PCI, ASoC, and V4L2 subsystems and write device drivers for them
  • Get to grips with the Linux kernel power management infrastructure
  • Adopt a practical approach to customizing your Linux environment using best practices

Book Description

Linux is one of the fastest-growing operating systems around the world, and in the last few years, the Linux kernel has evolved significantly to support a wide variety of embedded devices with its improved subsystems and a range of new features. With this book, you'll find out how you can enhance your skills to write custom device drivers for your Linux operating system.

Mastering Linux Device Driver Development provides complete coverage of kernel topics, including video and audio frameworks, that usually go unaddressed. You'll work with some of the most complex and impactful Linux kernel frameworks, such as PCI, ALSA for SoC, and Video4Linux2, and discover expert tips and best practices along the way. In addition to this, you'll understand how to make the most of frameworks such as NVMEM and Watchdog. Once you've got to grips with Linux kernel helpers, you'll advance to working with special device types such as Multi-Function Devices (MFD) followed by video and audio device drivers.

By the end of this book, you'll be able to write feature-rich device drivers and integrate them with some of the most complex Linux kernel frameworks, including V4L2 and ALSA for SoC.

What you will learn

  • Explore and adopt Linux kernel helpers for locking, work deferral, and interrupt management
  • Understand the Regmap subsystem to manage memory accesses and work with the IRQ subsystem
  • Get to grips with the PCI subsystem and write reliable drivers for PCI devices
  • Write full multimedia device drivers using ALSA SoC and the V4L2 framework
  • Build power-aware device drivers using the kernel power management framework
  • Find out how to get the most out of miscellaneous kernel subsystems such as NVMEM and Watchdog

Who this book is for

This book is for embedded developers, Linux system engineers, and system programmers who want to explore Linux kernel frameworks and subsystems. C programming skills and a basic understanding of driver development are necessary to get started with this book.

Table of contents

  1. Mastering Linux Device Driver Development
  2. Why subscribe?
  3. Contributors
  4. About the author
  5. About the reviewers
  6. Packt is searching for authors like you
  7. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the color images
    5. Conventions used
    6. Get in touch
    7. Reviews
  8. Section 1:Kernel Core Frameworks for Embedded Device Driver Development
  9. Chapter 1: Linux Kernel Concepts for Embedded Developers
    1. Technical requirements
    2. The kernel locking API and shared objects
      1. Spinlocks
      2. Mutexes
      3. The try-lock method
      4. Waiting, sensing, and blocking in the Linux kernel
    3. Work deferring mechanisms
      1. SoftIRQs
      2. Tasklets
      3. Workqueues
      4. Workqueues – a new generation
    4. Linux kernel interrupt management
      1. Designing an interrupt handler
    5. Summary
  10. Chapter 2: Leveraging the Regmap API and Simplifying the Code
    1. Technical requirements
    2. Introduction to regmap and its data structures – I2C, SPI, and MMIO
      1. Accessing device registers
      2. Quick recap on Linux kernel IRQ management
      3. Regmap IRQ API and data structures
    3. Summary
  11. Chapter 3: Delving into the MFD Subsystem and Syscon API
    1. Technical requirements
    2. Introducing the MFD subsystem and Syscon APIs
    3. Device tree binding for MFD devices
    4. Understanding Syscon and simple-mfd
      1. Introducing simple-mfd
    5. Summary
  12. Chapter 4: Storming the Common Clock Framework
    1. Technical requirements
    2. CCF data structures and interfaces
      1. Understanding struct clk_hw and its dependencies
      2. Registering/unregistering the clock provider
      3. Exposing clocks to others (in detail)
    3. Writing a clock provider driver
      1. Providing clock ops
      2. Putting it all together – global overview
      3. Grabbing and releasing clocks
      4. Preparing/unpreparing clocks
      5. Rate functions
      6. Parent functions
      7. Putting it all together
  13. Section 2: Multimedia and Power Saving in Embedded Linux Systems
  14. Chapter 5: ALSA SoC Framework – Leveraging Codec and Platform Class Drivers
    1. Technical requirements
    2. Introduction to ASoC
      1. ASoC Digital Audio Interface
      2. ASoC sub-elements
    3. Writing codec class drivers
      1. Codec DAI and PCM (AKA DSP) configurations
      2. The concept of controls
      3. The concept of DAPM
    4. Writing the platform class driver
      1. The CPU DAI driver
      2. The platform DMA driver AKA PCM DMA driver
    5. Summary
  15. Chapter 6: ALSA SoC Framework – Delving into the Machine Class Drivers
    1. Technical requirements
    2. Introduction to machine class drivers
      1. The DAI link
    3. Machine routing consideration
      1. Codec pins
      2. Board connectors
      3. Machine routing
    4. Clocking and formatting considerations
    5. Sound card registration
    6. Leveraging the simple-card machine driver
      1. Codec-less sound cards
    7. Summary
  16. Chapter 7: Demystifying V4L2 and Video Capture Device Drivers
    1. Technical requirements
    2. Framework architecture and the main data structures
      1. Initializing and registering a V4L2 device
    3. Introducing video device drivers – the bridge driver
      1. Initializing and registering the video device
      2. Video device file operations
      3. V4L2 ioctl handling
      4. The videobuf2 interface and APIs
    4. The concept of sub-devices
      1. Sub-device initialization
      2. Sub-device operations
      3. Traditional sub-device (un)registration
    5. V4L2 controls infrastructure
      1. A word about control inheritance
    6. Summary
  17. Chapter 8: Integrating with V4L2 Async and Media Controller Frameworks
    1. Technical requirements
    2. The V4L2 async interface and the concept of graph binding
      1. Graph binding
      2. The V4L2 async and graph-oriented API
      3. V4L2 async
    3. The Linux media controller framework
      1. The media controller abstraction model
      2. Integrating media controller support in the driver
      3. Media controller from user space
    4. Summary
  18. Chapter 9:Leveraging the V4L2 API from the User Space
    1. Technical requirements
    2. Introduction to V4L2 from user space
      1. The V4L2 user space API
    3. Video device opening and property management
      1. Querying the device capabilities
    4. Buffer management
      1. Image (buffer) format
      2. Requesting buffers
      3. Enqueueing the buffer and enabling streaming
      4. Dequeuing buffers
    5. V4L2 user space tools
      1. Using v4l2-ctl
      2. Debugging V4L2 in user space
    6. Summary
  19. Chapter 10: Linux Kernel Power Management
    1. Technical requirements
    2. The concept of power management on Linux-based systems
      1. Runtime power management
      2. System power management sleep states
    3. Adding power management capabilities to device drivers
      1. Implementing runtime PM capability
      2. System suspend and resume sequences
      3. Implementing system sleep capability
    4. Being a source of system wakeup
      1. Wakeup source and sysfs (or debugfs)
      2. The IRQF_NO_SUSPEND flag
    5. Summary
  20. Section 3: Staying Up to Date with Other Linux Kernel Subsystems
  21. Chapter 11: Writing PCI Device Drivers
    1. Technical requirements
    2. Introduction to PCI buses and interfaces
      1. Terminology
      2. PCI bus enumeration, device configuration, and addressing
      3. PCI address spaces
      4. Interrupt distribution
    3. The Linux kernel PCI subsystem and data structures
      1. PCI data structures
      2. Overview of the PCI driver structure
    4. PCI and Direct Memory Access (DMA)
      1. PCI coherent (aka consistent) mapping
      2. Streaming DMA mapping
    5. Summary
  22. Chapter 12: Leveraging the NVMEM Framework
    1. Technical requirements
    2. Introducing NVMEM data structures and APIs
    3. Writing the NVMEM provider driver
      1. NVMEM device (un)registration
      2. Implementing NVMEM read/write callbacks
      3. Device tree bindings for NVMEM providers
    4. NVMEM consumer driver APIs
      1. NVMEM in user space
    5. Summary
  23. Chapter 13: Watchdog Device Drivers
    1. Technical requirements
    2. Watchdog data structures and APIs
      1. Registering/unregistering a watchdog device
      2. Handling pretimeouts and governors
      3. GPIO-based watchdogs
    3. The watchdog user space interface
      1. Starting and stopping the watchdog
      2. Getting watchdog capabilities and identity
      3. Setting and getting the timeout and pretimeout
      4. Getting the (boot/reboot) status
      5. The watchdog sysfs interface
    4. Summary
  24. Chapter 14: Linux Kernel Debugging Tips and Best Practices
    1. Technical requirements
    2. Understanding the Linux kernel release process
    3. Linux kernel development tips
      1. Message printing
    4. Linux kernel tracing and performance analysis
      1. Using Ftrace to instrument the code
    5. Linux kernel debugging tips
      1. Oops and panic analysis
      2. Using objdump to identify the faulty code line in the kernel module
    6. Summary
  25. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Mastering Linux Device Driver Development
  • Author(s): John Madieu
  • Release date: January 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781789342048