1. 学习目标

  1. 理解 Linux 为什么需要设备模型

理解 Linux 如何统一管理:

CPU
内存
外设
总线
驱动

  • 理解 Device Model 核心结构

理解四个核心对象:

bus
device
driver
class

  • 理解驱动匹配机制

理解 Linux 如何匹配:

device <-> driver

并触发:

probe()

  • 理解 sysfs 与设备模型关系

理解:

/sys

目录如何反映设备结构


  • 理解 Device Model 在驱动中的作用

理解 Linux 驱动架构:

bus
device
driver

以及:

platform
i2c
spi
usb

都是 Device Model 的实现


2. 为什么需要设备模型

早期 Linux 驱动结构比较简单:

driver <-> hardware

问题是:

设备越来越多
总线越来越复杂

例如:

PCI
USB
I2C
SPI
Platform

Linux 需要一个统一框架管理设备。

于是引入:

Linux Device Model

3. Device Model 核心结构

Linux 设备模型核心结构:

bus
device
driver
class

关系结构:

           bus
            
    ┌───────┴────────┐
 device             driver

当:

device  driver 匹配

Linux 自动调用:

probe()

4. bus(总线)

总线表示:

设备连接方式

例如:

PCI
USB
I2C
SPI
platform

Linux 使用:

struct bus_type

表示总线:

示例:

struct bus_type i2c_bus_type

bus 的作用

bus 主要负责:

管理 device
管理 driver
匹配 device  driver

匹配流程:

device register
driver register
bus match
probe()

5. device(设备)

device 表示:

一个硬件设备

例如:

UART controller
I2C device
SPI flash
USB camera

Linux 使用:

struct device

表示设备。


struct device

核心成员:

struct device
{
    struct device *parent;
    struct bus_type *bus;
    struct device_driver *driver;
};

关键成员:

bus
driver

6. driver (驱动)

driver 表示:

设备驱动程序

Linux 使用:

struct device_driver

结构:

struct device_driver
{
    const char *name;
    struct bus_type *bus;
};

驱动会注册到:

bus

7. device 与 driver 匹配

Linux 会在 bus 上匹配:

device
driver

匹配成功后:

probe()

被调用。

流程:

driver register
      
device register
      
bus match
      
probe()

示例

例如:

i2c device

匹配:

i2c driver

成功后:

i2c_probe()

被调用。

8. class(设备类)

class 用于:

用户空间设备管理

例如:

/dev/input
/dev/tty
/dev/video

Linux 使用:

struct class

表示设备类。


class 结构

class 用于:

创建设备节点

例如:

/dev/mydev

代码:

class_create()
device_create()

9. sysfs 与 Device Model

Linux 通过:

sysfs

展示设备模型。

路径:

/sys

例如:

/sys/bus
/sys/devices
/sys/class

/sys/bus

表示系统总线:

/sys/bus/i2c
/sys/bus/spi
/sys/bus/platform

/sys/devices

表示系统设备:

/sys/devices/platform

/sys/calss

表示类:

/sys/class/input
/sys/class/net

10. Device Model 层次结构

完整结构:

               device
                 
                 
               bus
                 
        ┌────────┴────────┐
     driver              class

更完整架构:

Hardware
   
   
Device
   
   
Bus
   
   
Driver
   
   
Subsystem

11. Device Model 与驱动关系

Linux 所有驱动都基于 Device Model。

例如:

子系统 bus
platform platform bus
i2c i2c bus
spi spi bus
usb usb bus
pci pci bus

示例

I2C 驱动结构:

i2c bus
   
   ├── i2c device
   
   └── i2c driver

12. Device Model 工作流程

完整流程:

Device register
      
Driver register
      
Bus match
      
probe()
      
Driver init

13. Deive Model 示例

例如:

SPI Flash

系统结构:

SPI bus
   
spi_device
   
spi_driver
   
probe()

14. 驱动常见问题

probe 没有调用

原因:

device  driver 不匹配

例如:

compatible 不一致

sysfs 没有设备

原因:

device 没有注册

设备节点不存在

原因:

class 未创建

15. 总结

Linux Device Model 核心结构:

bus
device
driver
class

关系:

bus
 
 ├── device
 
 └── driver

匹配成功:

probe()

最重要原则:

Linux 驱动 = Device Model + Subsystem


16. Q&A

16.1 结构理解

  1. 为什么 Linux 需要 Device Model?

  2. bus 的作用是什么?

16.2 设备匹配

  1. device 和 driver 如何匹配?

  2. probe 为什么会被调用?

16.3 sysfs

  1. /sys/bus/sys/devices 有什么区别?

  2. /sys/class 的作用是什么?