Linux设备模型(总线、设备、驱动程序和类)之一:bus_type
来源:互联网 发布:靠谱网络兼职平台 编辑:程序博客网 时间:2024/05/16 04:18
总线
总线是处理器和一个或多个设备之间的通道,在设备模型中, 所有的设备都通过总线相连, 甚至是内部的虚拟"platform"总线。
可以通过ls -l /sys/bus看到系统加载的所有总线。
drwxr-xr-x root root 1970-01-01 00:02 platform
drwxr-xr-x root root 1970-01-01 00:02 spi
drwxr-xr-x root root 1970-01-01 00:02 scsi
drwxr-xr-x root root 1970-01-01 00:02 usb
drwxr-xr-x root root 1970-01-01 00:02 serio
drwxr-xr-x root root 1970-01-01 00:02 i2c
drwxr-xr-x root root 1970-01-01 00:02 mmc
drwxr-xr-x root root 1970-01-01 00:02 sdio
drwxr-xr-x root root 1970-01-01 00:02 ac97
总线可以相互插入。设备模型展示了总线和它们所控制的设备之间的实际连接。
在 Linux 设备模型中, 总线由 bus_type 结构表示, 定义在 <linux/device.h> :
struct bus_type {
const char * name;/*总线类型名称*/
struct module * owner;/*指向模块的指针(如果有), 此模块负责操作这个总线*/
struct kset subsys;/*与该总线相关的子系统*/
struct kset drivers;/*总线驱动程序的kset*/
struct kset devices;/* 挂在该总线的所有设备的kset*/
struct klist klist_devices;/*与该总线相关的驱动程序链表*/
struct klist klist_drivers;/*挂接在该总线的设备链表*/
struct blocking_notifier_head bus_notifier;
struct bus_attribute * bus_attrs; /*总线属性*/
struct device_attribute * dev_attrs; /*设备属性,指向为每个加入总线的设备建立的默认属性链表*/
struct driver_attribute * drv_attrs; /*驱动程序属性*/
struct bus_attribute drivers_autoprobe_attr;/*驱动自动探测属性*/
struct bus_attribute drivers_probe_attr;/*驱动探测属性*/
int (*match)(struct device * dev, struct device_driver * drv);
int (*uevent)(struct device *dev, char **envp,
int num_envp, char *buffer, int buffer_size);
int (*probe)(struct device * dev);
int (*remove)(struct device * dev);
void (*shutdown)(struct device * dev);
int (*suspend)(struct device * dev, pm_message_t state);
int (*suspend_late)(struct device * dev, pm_message_t state);
int (*resume_early)(struct device * dev);
nt (*resume)(struct device * dev);
/*处理热插拔、电源管理、探测和移除等事件的方法*/
unsigned int drivers_autoprobe:1;
};
1 总线的注册和删除
总线的主要注册步骤:
(1)申明和初始化 bus_type 结构体。只有很少的 bus_type 成员需要初始化,大部分都由设备模型核心控制。但必须为总线指定名字及一些必要的方法。例如:
struct bus_type ldd_bus_type = {
.name = "ldd",
.match = ldd_match,
.uevent = ldd_uevent,
};
(2)调用bus_register函数注册总线。
int bus_register(struct bus_type * bus)
ret = bus_register(&ldd_bus_type);
if (ret)
return ret;
void bus_unregister(struct bus_type *bus);
2 总线方法
在 bus_type 结构中定义了许多方法,它们允许总线核心作为设备核心和单独的驱动程序之间提供服务的中介,主要介绍以下两个方法:
int (*match)(struct device * dev, struct device_driver * drv);
/*当一个新设备或者驱动被添加到这个总线时,这个方法会被调用一次或多次,若指定的驱动程序能够处理指定的设备,则返回非零值。必须在总线层使用这个函数, 因为那里存在正确的逻辑,核心内核不知道如何为每个总线类型匹配设备和驱动程序*/
int (*uevent)(struct device *dev, char **envp, int num_envp, char *buffer, intbuffer_size);
/*在为用户空间产生热插拔事件之前,这个方法允许总线添加环境变量(参数和 kset 的uevent方法相同)*/
lddbus的match和uevent方法:
static int ldd_match(struct device *dev, struct device_driver *driver)
{
return !strncmp(dev->bus_id, driver->name, strlen(driver->name));
}/*仅简单比较驱动和设备的名字*/
/*当涉及实际硬件时, match 函数常常对设备提供的硬件 ID 和驱动所支持的 ID 做比较*/
static int ldd_uevent(struct device *dev, char **envp, int num_envp, char *buffer, intbuffer_size)
{
envp[0] = buffer;
if (snprintf(buffer, buffer_size, "LDDBUS_VERSION=%s",
Version) >= buffer_size)
return -ENOMEM;
envp[1] = NULL;
return 0;
}/*在环境变量中加入 lddbus 源码的当前版本号*/
对设备和驱动的迭代
若要编写总线层代码, 可能不得不对所有已经注册到总线的设备或驱动进行一些操作,这可能需要仔细研究嵌入到 bus_type 结构中的其他数据结构, 但最好使用内核提供的辅助函数:
int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, int(*fn)(struct device *, void *));
int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, void *data, int(*fn)(struct device_driver *, void *));
/*这两个函数迭代总线上的每个设备或驱动程序, 将关联的 device 或 device_driver 传递给 fn, 同时传递 data 值。若 start 为 NULL, 则从第一个设备开始; 否则从 start 之后的第一个设备开始。若 fn 返回非零值, 迭代停止并且那个值从 bus_for_each_dev 或bus_for_each_drv 返回。*/
3 总线属性
几乎 Linux 设备模型中的每一层都提供添加属性的函数, 总线层也不例外。bus_attribute 类型定义在 <linux/device.h>如下:
struct bus_attribute {
struct attribute attr;
ssize_t (*show)(struct bus_type *, char * buf);
ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
};
可以看出struct bus_attribute 和struct attribute 很相似,其实大部分在 kobject 级上的设备模型层都是以这种方式工作。
内核提供了一个宏在编译时创建和初始化 bus_attribute 结构:
BUS_ATTR(_name,_mode,_show,_store)/*这个宏声明一个结构, 将 bus_attr_ 作为给定 _name 的前缀来创建总线的真正名称*/
/*总线的属性必须显式调用 bus_create_file 来创建:*/
int bus_create_file(struct bus_type *bus, struct bus_attribute *attr);
/*删除总线的属性调用:*/
void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr);
例如创建一个包含源码版本号简单属性文件方法如下:
static ssize_t show_bus_version(struct bus_type *bus, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s/n", Version);
}
static BUS_ATTR(version, S_IRUGO, show_bus_version, NULL);
/*在模块加载时创建属性文件:*/
if (bus_create_file(&ldd_bus_type, &bus_attr_version))
printk(KERN_NOTICE "Unable to create version attribute/n");
/*这个调用创建一个包含 lddbus 代码的版本号的属性文件(/sys/bus/ldd/version)*/
- Linux设备模型(总线、设备、驱动程序和类)之一:bus_type
- Linux设备模型(总线、设备、驱动程序和类)之一:bus_type
- Linux设备模型(总线、设备、驱动程序和类)之一:bus_type
- Linux设备模型(总线、设备、驱动程序和类)之一:bus_type
- Linux设备模型(总线、设备、驱动程序和类)
- Linux设备模型(总线、设备、驱动程序和类)
- Linux设备模型(总线、设备、驱动程序和类)
- Linux设备模型(总线、设备、驱动程序和类)
- Linux设备模型(总线、设备、驱动程序和类)
- Linux设备驱动程序学习(13)-Linux设备模型(总线、设备、驱动程序和类)
- Linux设备驱动程序学习(13)-Linux设备模型(总线、设备、驱动程序和类)【转】
- Linux设备驱动程序学习(13)-Linux设备模型(总线、设备、驱动程序和类)
- Linux设备驱动程序学习(13)-Linux设备模型(总线、设备、驱动程序和类)
- Linux设备驱动程序学习-Linux设备模型(总线、设备、驱动程序和类)
- Linux设备驱动程序学习-Linux设备模型(总线、设备、驱动程序和类)
- 操作系统_再识(Linux设备驱动程序学习-Linux设备模型(总线、设备、驱动程序和类))
- (收藏) Linux设备驱动程序学习-Linux设备模型(总线、设备、驱动程序和类)
- 转载_Linux设备模型(总线、设备、驱动程序和类)
- ATL Internals 2ed复习.chapter 3.CComSafeArray
- 黑马程序员 Object类,包和匿名内部类
- Bridge模式
- peewee 一个轻量级的ORM (三)
- oracle分页存储过程
- Linux设备模型(总线、设备、驱动程序和类)之一:bus_type
- 【数据结构】递归实现链表逆序
- ZOJ 3648 Gao the Grid II
- 存储过程中输入表名还有输出游标以及其测试块
- 《影响力》读书笔记,第七章 稀缺
- 单链表
- 如何判断Android系统的版本
- Linux设备模型(总线、设备、驱动程序和类)之二:device
- 系统消息 -- 键鼠消息