1. 设备管理模型
设备模型是内核提供的一个编写驱动的架构。
设备管理是设备-总线-驱动结构。
linux中的设备是由树状模型组织的,从sysfs中可以查看树状结构。
他本身实现了:
- 电源管理
- 热插拔(hotplug)事件管理
2. 基本数据结构
- kobject, kset, uevent
- device, device_driver, bus, class
2.1 kobject
许多内核对象都是由kobject作为基类派生的。相同类型的kobject通过其内嵌的list_head链成一个链表,然后使用另外一个结构体kset(kobject的子类)来指向和管理这个列表。
struct kobject {
const char *name;
struct list_head entry;
struct kobject *parent;
struct kset *kset; /* 给相关的kobject分组,是kobj的集合 */
struct kobj_type *ktype; /* 抽象出了一些kobj和sysfs的通用接口,描述kobj的行为 */
struct kernfs_node *sd; /* sysfs 的文件节点 */
struct kref kref; /* kobject的引用计数 */
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
struct delayed_work release;
#endif
unsigned int state_initialized:1;
unsigned int state_in_sysfs:1;
unsigned int state_add_uevent_sent:1;
unsigned int state_remove_uevent_sent:1;
unsigned int uevent_suppress:1;
};
一个新的kobject要加入到设备管理模型中,需要使用函数:
/* 创建新的kobj */
struct kobject * __must_check kobject_create_and_add(const char *name,
struct kobject *parent);
/* parent : 在sysfs中的父目录,如果为NULL则表示在"/sys/"目录下 */
/* 添加已存在的的kobj */
int kobject_init_and_add(struct kobject *kobj,
struct kobj_type *ktype,
struct kobject *parent,
const char *fmt, ...