Linux设备模型之总线 设备 和驱动


Linux内核修炼之道》读书笔记

1、

设备模型的上层建筑由总线(bus) 、设备(device)、 驱动(device_driver)这3个数据结构构成,设备模型表示了它们之间的连接关系。

在设备模型中,所有的设备都通过总线连接。总线可以是物理存在的,也可以是虚拟的。比如内部的platform总线。

设备是连接到某条物理或虚拟总线上的对象。可能是真正的物理对象,也可能的是虚拟对象。

驱动是用来和设备通信的软件程序。驱动可以从设备获得数据,也可以把相应数据发给设备进行处理。

2、数据结构

(1)、总线

struct bus_type {
const char *name;总线类型的名称
struct bus_attribute*bus_attrs;
struct device_attribute*dev_attrs;
struct driver_attribute*drv_attrs;

int (*match)(struct device *dev, struct device_driver *drv);设备和驱动能否对应,就是有该总线的match方式决定。不同总线的match方式不一样。
int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
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);
int (*resume)(struct device *dev);

struct pm_ext_ops *pm;
struct bus_type_private *p;
};

现在如上数据结构和书中讲的有所不同,只不过有包装了一层数据结构struct bus_type_private *p,源码如下:

/**
 * struct bus_type_private - structure to hold the private to the driver core portions of the bus_type structure.
 *
 * 
@subsys - the struct kset that defines this bus.  This is the main kobject

subsys描述该总线的子系统,它连接到全局量kset bus_subsys中。
 * @drivers_kset - the list of drivers associated with this bus

该总线系统里所有驱动的集合
 * @devices_kset - the list of devices associated with this bus

该总线系统里所有设备的集合
 * @klist_devices - the klist to iterate over the @devices_kset

该总线里的设备用klist指针连成一个链表
 * @klist_drivers - the klist to iterate over the @drivers_kset

驱动链表
 * @bus_notifier - the bus notifier list for anything that cares about things
 * on this bus.
 * @bus - pointer back to the struct bus_type that this structure is associated
 * with.
 *
 * This structure is the one that is the actual kobject allowing struct
 * bus_type to be statically allocated safely.  Nothing outside of the driver
 * core should ever touch these fields.
 */
struct bus_type_private {
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;
unsigned int drivers_autoprobe:1;
struct bus_type *bus;
};

在sysfs文件系统中,我们可以清晰地看到它们之间的联系。kset bus_subsys对应于/sys/bus这个目录。每个bus_type对象都对应/sys/bus目录下的一个子目录,如PCI类型对应于/sys/bus/pci。

在每个这样的目录下都存在两个子目录:devices和drivers。

  • 1
  • 2
  • 3
  • 下一页

相关内容