标题: Structure of Linux Kernel Device Driver(Part II) [打印本页] 作者: 丝 时间: 2024-7-16 21:01 标题: Structure of Linux Kernel Device Driver(Part II) Structure of Linux Kernel Device Driver
Memory-mapped I/O: 与硬件设备共享内存地址空间举行通信,(have the I/O devices mapped to our address spaces, so in the address space you have the registers there where you can talk to the hardware ),这种方法更为常见
Linux驱动模型提供了多个设备驱动抽象(abstraction to device drivers),这能够使驱动代码更模块化、可重用而且轻易维护。
该驱动模型的组成如下:
Framework:根据设备类型导出的尺度化接口
Buses:从设备驱动中抽象出来的设备信息以及设备所连接的位置
使用驱动框架(linux/leds.h for led device)将接口尺度化后,意味着驱动开辟者不再需要定义file_operations来指定回调函数的行为,这些接口以及对应回调函数都由对应的框架完成定义。Users know beforehand the interface provided by a driver based on its class or type.
为了避免硬件资源被一个设备独占,需要使用特定的API进举措态控制,比如对于LED设备,Linux内核中实现了一种生产者/消费者的模型(gpiolib)来管理GPIO资源。
GPIO producer,雷同于GPIO控制器的驱动
GPIO consumer,雷同于LED设备的驱动
如下,使用了框架将用户接口尺度化,并使用内核接口管理硬件资源:
#include <linux/leds.h>
// other header files
struct drvled_data_st {
struct gpio_desc *desc; // change from "void __iomem *regbase;"
struct led_classdev led_cdev; // change from "cdev"
result = led_classdev_register(NULL, &drvled_data->led_cdev);
if (result) {
// handle error
}
// ...
}
static void __exit drvled_exit(void)
{
led_classdev_unregister(&drvled_data->led_cdev);
// not use the iounmap()
gpio_free(GPIO_NUM);
release_mem_region(GPIO1_BASE, GPIO1_SIZE);
kfree(drvled_data);
}
// ...
复制代码
使用设备框架对驱动举行重组后,用户不再直接与/dev/目录下的设备文件举行交互,而是在目录/sys/class/led目录下找到全部的LED类的设备,进入所注册的驱动目录下,可以找到控制LED设备的接口,这些接口仍然以文件的形式存在,但是和之前所定义的接口相比会更加尺度化,也就是险些全部的LED设备都可以使用如许的接口举行控制。
Bus infrastructure
末了,可以使用总线框架实现设备与驱动的解耦合。总线框架组成如下:
Bus Core: 对于给定总线类型(USB core, PCI core, etc)所实现的API, (represented in the kernel by the "bus_type" structure)
Bus adapters:总线控制器驱动, (represented in the kernel by the "device_driver" structure)
Bus drivers: 负责管理连接到总线的设备, (represented in the kernel by the "device_driver" structure)
Bus devices: 全部连接到总线的设备, (represented in the kernel by the structure "device")