Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Linux Device Driver Development - Second Edition

You're reading from  Linux Device Driver Development - Second Edition

Product type Book
Published in Apr 2022
Publisher Packt
ISBN-13 9781803240060
Pages 708 pages
Edition 2nd Edition
Languages
Author (1):
John Madieu John Madieu
Profile icon John Madieu

Table of Contents (23) Chapters

Preface Section 1 -Linux Kernel Development Basics
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 Section 2 - Linux Kernel Platform Abstraction and Device Drivers
Chapter 5: Understanding and Leveraging the Device Tree Chapter 6: Introduction to Devices, Drivers, and Platform Abstraction Chapter 7: Understanding the Concept of Platform Devices and Drivers Chapter 8: Writing I2C Device Drivers Chapter 9: Writing SPI Device Drivers Section 3 - Making the Most out of Your Hardware
Chapter 10: Understanding the Linux Kernel Memory Allocation Chapter 11: Implementing Direct Memory Access (DMA) Support Chapter 12: Abstracting Memory Access – Introduction to the Regmap API: a Register Map Abstraction Chapter 13: Demystifying the Kernel IRQ Framework Chapter 14: Introduction to the Linux Device Model Section 4 - Misc Kernel Subsystems for the Embedded World
Chapter 15: Digging into the IIO Framework Chapter 16: Getting the Most Out of the Pin Controller and GPIO Subsystems Chapter 17: Leveraging the Linux Kernel Input Subsystem Other Books You May Enjoy

Chapter 14: Introduction to the Linux Device Model

Until version 2.5, the Linux kernel had no way to describe and manage objects, and its code reusability was not as enhanced as it is now. In other words, there was no device topology, nor organization as we know it is in sysfs nowadays. There was no information on subsystem relationships, nor on how the system is put together. Then came the Linux Device Model (LDM), which introduced the following features:

  • The concept of classes. They are used to group devices of the same type or that expose the same functionalities (for example, mice and keyboards are both input devices).
  • Communication with the user space through a virtual filesystem, allowing you to manage and enumerate devices and the properties they expose from user space.
  • Object life cycle management using reference counting.
  • A power management facility, allowing you to handle the order in which devices should shut down.
  • The reusability of the code. Classes...

Introduction to LDM data structures

The Linux device model introduced device hierarchy. It is built on top of a few data structures. Among these is the bus, which is represented in the kernel as an instance of struct bus_type; the device driver, which is represented by a struct device_driver structure; and the device, which is the last element and is represented as an instance of the struct device structure. In this section, we will introduce all those structures and learn how they interact each with other.

The bus data structure

A bus is a channel link between devices and the processor. The hardware entity that manages the bus and exports its protocol to devices is called the bus controller. For example, the USB controller provides USB support, while the I2C controller provides I2C bus support. However, the bus controller, being a device on its own, must be registered like any device. It will be the parent of the devices that need to sit on this bus. In other words, every device...

Getting deeper inside LDM

So far, we have discussed buses, drivers, and devices, which were used to build the system device topology. While this is true, the previous topics were the tip of the iceberg. Under the hood, LDM relies on the three lowest level data structures, which are kobject, kobj_type, and kset. These are used to link the objects.

Before we go any further, let's define some of the terms that will be used throughout this chapter:

  • sysfs: sysfs is an in-memory virtual filesystem that shows the hierarchy of kernel objects, abstracted by instances of struct kobject.
  • Attribute: An attribute (or sysfs attribute) appears as a file in sysfs. From within the kernel, it can be mapped to anything: a variable, a device property, a buffer, or anything useful to the driver that may need to be exported to the world.

In this section, we will learn how each of these structures is involved in the device model.

Understanding the kobject structure

struct...

Overview of the device model from sysfs

sysfs is a non-persistent virtual filesystem that provides a global view of the system and exposes the kernel objects hierarchy (topology) using their kobjects. Each kobject shows up as a directory. The files in these directories represent the kernel variables that are exported by the related kobject. These files are called attributes and can be read or written.

If any registered kobject creates a directory in sysfs, where the directory is created depends on the parent of this kobject (which is also a kobject, thus highlighting internal object hierarchies). In sysfs, top-level directories represent the common ancestors of object hierarchies or the subsystems that the objects belong to.

These top-level sysfs directories can be found in the /sys/ directory, as follows:

/sys$ tree -L 1
├── block
├── bus
├── class
├── dev
├── devices...

Summary

By completing this chapter, you should be familiar with LDM, its data structures (bus, class, device, and driver), and its low-level data structures, which are kobject, kset, kobj_type, and attributes (or a group of these). You should now know how objects are represented within the kernel (device topology) and be able to create an attribute (or group) that exposes your device or driver features and properties through sysfs.

In the next chapter, we will cover the IIO (Industrial I/O) framework, which heavily uses the power of sysfs.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Linux Device Driver Development - Second Edition
Published in: Apr 2022 Publisher: Packt ISBN-13: 9781803240060
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}