Skip to content

Latest commit

 

History

History
768 lines (445 loc) · 7.14 KB

File metadata and controls

768 lines (445 loc) · 7.14 KB

Device Model (Linux Kernel)

Table of Contents

  1. Introduction
  2. What is Device Model?
  3. Why Do We Need Device Model?
  4. Goals of Device Model
  5. Components of Device Model
  6. Bus
  7. Device
  8. Driver
  9. Class
  10. Kobject
  11. Kset
  12. Sysfs
  13. How Device Model Works
  14. Device Registration Flow
  15. Driver Binding Process
  16. Memory Representation
  17. Real Embedded Example
  18. Advantages
  19. Disadvantages
  20. Common Mistakes
  21. Interview Questions
  22. Quick Revision
  23. Conclusion

Introduction

The Linux Device Model is a framework used by the Linux kernel to represent:

  • Devices
  • Device Drivers
  • Buses
  • Classes

It provides:

  • Uniform device representation
  • Automatic device-driver matching
  • Power management
  • Sysfs interface
  • Hotplug support

The Linux Device Model is widely used in:

  • Embedded Linux
  • Linux Device Drivers
  • Android Kernel
  • Automotive Linux
  • IoT Systems

What is Device Model?

Definition

The Linux Device Model is a framework that manages the relationship between:

  • Devices
  • Drivers
  • Buses
  • Classes

and exposes them through Sysfs.


Interview Answer

The Linux Device Model is a kernel framework used to organize devices, drivers, buses, and classes, providing automatic driver binding and sysfs representation.


Why Do We Need Device Model?

Without Device Model:

Device
   ↓

Driver

(No standard structure)

Difficult to manage

No sysfs

No hotplug

With Device Model:

Device

↓

Bus

↓

Driver

↓

Class

↓

Sysfs

↓

User Space

Benefits

  • Standard representation
  • Automatic matching
  • Power management
  • Dynamic device creation
  • Sysfs interface

Goals of Device Model

  1. Device Management
  2. Driver Binding
  3. Hotplug Support
  4. Power Management
  5. Sysfs Representation
  6. Standardized Architecture

Components of Device Model

Linux Device Model

│

├── Bus

├── Device

├── Driver

├── Class

├── Kobject

├── Kset

└── Sysfs

Bus

Definition

A bus connects devices and drivers.

Examples:

  • PCI
  • USB
  • I2C
  • SPI
  • Platform Bus

Structure

struct bus_type
{
    const char *name;

    int (*match)(...);

    int (*probe)(...);

    void (*remove)(...);
};

Responsibilities

  • Device registration
  • Driver registration
  • Device-driver matching

Device

Definition

Represents a physical or virtual hardware component.

Examples:

  • UART
  • SPI Controller
  • Ethernet Controller
  • GPIO Controller

Structure

struct device
{
    struct kobject kobj;

    struct bus_type *bus;

    struct device_driver *driver;

    struct device *parent;
};

Responsibilities

  • Device Information
  • Parent-child hierarchy
  • Sysfs representation

Driver

Definition

Software component controlling a device.


Structure

struct device_driver
{
    const char *name;

    struct bus_type *bus;

    int (*probe)(...);

    void (*remove)(...);
};

Responsibilities

  • Initialize device

  • Configure hardware

  • Interrupt handling

  • Data transfer


Class

Definition

Groups similar devices together.

Examples:

  • net

  • tty

  • input

  • block


Sysfs Example

/sys/class/

├── net

├── tty

├── input

└── block

Kobject

Definition

Kernel object providing:

  • Object hierarchy

  • Reference counting

  • Sysfs support


Structure

struct kobject
{
    const char *name;

    struct kref kref;

    struct kset *kset;
};

Kset

Definition

Collection of kobjects.


Example

Kset

│

├── Device1

├── Device2

└── Device3

Sysfs

Definition

Virtual filesystem exposing kernel objects.

Mounted at:

/sys

Example

/sys

├── bus

├── devices

├── class

└── firmware

Why Sysfs?

  • User space visibility

  • Device information

  • Runtime configuration

  • Debugging


How Device Model Works

Device Registered

↓

Bus Registered

↓

Driver Registered

↓

Bus Match Function

↓

Probe Function Called

↓

Driver Attached

↓

Sysfs Entries Created

Device Registration Flow

device_register(dev);

Internally:

device_register()

↓

device_initialize()

↓

kobject_init()

↓

bus_add_device()

↓

sysfs_create()

↓

Device Ready

Driver Binding Process

Device Appears

↓

Bus Searches Driver

↓

match()

↓

probe()

↓

Driver Bound

↓

Operational

Memory Representation

RAM

------------------------------------------------

struct device

|

+--- kobject

|

+--- bus

|

+--- driver

|

+--- parent

------------------------------------------------


struct device_driver

|

+--- name

|

+--- probe

|

+--- remove

------------------------------------------------

Real Embedded Example

I2C Device

I2C Bus

│

├── EEPROM

├── RTC

└── Temperature Sensor

Driver:

static struct i2c_driver temp_driver =
{
    .probe  = temp_probe,

    .remove = temp_remove,
};

Sysfs

/sys/bus/i2c/devices/

├── 0-0050

├── 0-0068

└── 0-0048

Advantages

✔ Standardized architecture

✔ Automatic driver binding

✔ Sysfs interface

✔ Power management

✔ Hotplug support

✔ Device hierarchy


Disadvantages

✘ Complex structures

✘ Difficult debugging

✘ Steep learning curve

✘ Many layers of abstraction


Common Mistakes

❌ Forgetting device registration

❌ Missing probe function

❌ Incorrect bus matching

❌ Reference count leaks

❌ Improper sysfs cleanup


Interview Questions

What is Linux Device Model?

Framework managing devices, drivers, buses, and classes.


Why do we need Device Model?

For:

  • Driver binding

  • Sysfs

  • Power management

  • Hotplug support


What are main components?

  • Device

  • Driver

  • Bus

  • Class

  • Kobject

  • Kset

  • Sysfs


What is probe()?

Called when device matches driver.


What is Sysfs?

Virtual filesystem exposing kernel objects.


Quick Revision

Device Model

↓

Bus

↓

Device

↓

Driver

↓

Class

↓

Kobject

↓

Kset

↓

Sysfs

↓

Automatic Driver Binding

↓

Embedded Linux

Conclusion

The Linux Device Model is the backbone of Linux device management.

It organizes:

  • Devices
  • Drivers
  • Buses
  • Classes

and provides:

  • Automatic driver matching
  • Sysfs representation
  • Power management
  • Hotplug support

Understanding the Device Model is essential for:

  • Linux Device Drivers
  • Embedded Linux
  • Android Kernel
  • Automotive Linux
  • System Programming