“Linux drivers 框架”通常可以从两个层面理解:
下面用通俗方式说明。
Linux 是宏内核,驱动直接运行在内核态:
用户空间程序
↓ (系统调用)
内核 (进程调度 / 内存 / 文件系统)
↓
驱动框架 (bus / class / driver)
↓
具体硬件 (CPU 外设 / PCI / USB / I2C ...)
驱动的作用:
让硬件“看起来像文件或设备”,供用户空间使用。
Linux 驱动并不是“随便写个函数”,而是基于一套统一模型。
核心对象:
关系:
bus
├── device
└── driver
内核负责:
用于没有标准总线的硬件(SoC 内部设备):
platform_device // 描述硬件资源
platform_driver // 驱动逻辑
示例:
最常见驱动类型:
register_chrdev()
file_operations
用户空间表现:
/dev/xxx
适合:
用于:
特点:
用于:
接口:
struct net_device
用户空间:
ifconfig / ip
Linux 把一类设备抽象成框架,驱动只需“填空”:
| 框架 | 用途 |
|---|---|
| input | 键盘/鼠标/触摸 |
| i2c | I2C 设备 |
| spi | SPI 设备 |
| usb | USB 设备 |
| gpio | 通用 IO |
| regulator | 电源管理 |
| pinctrl | 引脚复用 |
| drm | 显示 |
| alsa | 音频 |
好处:
最小字符驱动结构:
static const struct file_operations fops = {
.open = drv_open,
.read = drv_read,
.write = drv_write,
};
module_init(xxx_init);
module_exit(xxx_exit);
编译后:
insmod drv.ko
Linux drivers 框架 = 内核提供的一套统一机制,用来描述硬件、匹配驱动、暴露接口,并管理设备生命周期。
如果你愿意,我可以: