一、概述
Linux 设备模型(Device Model)是内核中统一管理硬件设备和驱动程序 的框架。它通过**总线(bus)、设备(device)、驱动(driver)**三要素的分离与匹配,实现了设备发现、驱动绑定、电源管理、热插拔等功能。
三者间的关系:
1 2 3 bus_type -> device -> device_driver 设备注册到总线 → 驱动注册到总线 → 总线执行 match → 绑定 → probe()
二、kobject
kobject 是 Linux 设备驱动模型的基础,也是设备模型中抽象的一部分。如果想了解设备驱动模型就需要明白 kobject 的构成或原理。Linux 内核为了兼容各种形形色色的设备,就需要对各种设备的共性进行抽象,抽象出一个基类,其余的设备只需要继承此基类就可以了。而此基类就是 kobject,但是 C 语言没有面向对象语法,这时候就需要将此基类(Kobject)嵌入到具体的结构体中,从而就可以访问控制此设备的操作。
2.1 struct kobject
kobject 是设备模型的基础对象 ,提供引用计数、sysfs 表示和层次结构。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 struct kobject { const char *name; struct list_head entry ; struct kobject *parent ; struct kset *kset ; const struct kobj_type *ktype ; struct kernfs_node *sd ; struct kref kref ; 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 层次:
1 2 3 4 5 6 7 8 9 kset (bus) ├── kobject /sys/ bus/pci ├── kobject devices/ │ ├── kobject 0000 :00 :1 f.2 │ │ └── kobject → 在 /sys/ bus/pci/ devices/0000 :00 :1 f.2 │ └── kobject 0000 :00 :02.0 └── kobject drivers/ ├── kobject ehci-pci/ └── kobject xhci-hcd/
kobject 的作用 :
引用计数(kref)确保对象在使用时不被释放;
自动在 sysfs 中创建目录;
通过 kset 形成层次结构;
通过 uevent 机制通知用户空间。
2.2 kobject function
2.2.1 kobject_init
该函数用于初始化一个kobject结构。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 void kobject_init (struct kobject *kobj, struct kobj_type *ktype) { char *err_str; if (!kobj) { err_str = "invalid kobject pointer!" ; goto error; } if (!ktype) { err_str = "must have a ktype to be initialized properly!\n" ; goto error; } if (kobj->state_initialized) { printk(KERN_ERR "kobject (%p): tried to init an initialized " "object, something is seriously wrong.\n" , kobj); dump_stack(); } kobject_init_internal(kobj); kobj->ktype = ktype; return ; error: printk(KERN_ERR "kobject (%p): %s\n" , kobj, err_str); dump_stack(); }
除了 ktype 成员外,真正的初始化在 kobject_init_internal 函数中实现。
1 2 3 4 5 6 7 8 9 10 11 static void kobject_init_internal (struct kobject *kobj) { if (!kobj) return ; kref_init(&kobj->kref); INIT_LIST_HEAD(&kobj->entry); kobj->state_in_sysfs = 0 ; kobj->state_add_uevent_sent = 0 ; kobj->state_remove_uevent_sent = 0 ; kobj->state_initialized = 1 ; }
2.2.2 kobject_set_name
用于设置 kobject 中的 name 成员
1 2 3 4 5 6 7 8 9 10 11 int kobject_set_name (struct kobject *kobj, const char *fmt, ...) { va_list vargs; int retval; va_start(vargs, fmt); retval = kobject_set_name_vargs(kobj, fmt, vargs); va_end(vargs); return retval; }
如果 kobject 已经被加入到系统当中,就必须使用 kobject_rename 函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 int kobject_set_name_vargs (struct kobject *kobj, const char *fmt, va_list vargs) { const char *old_name = kobj->name; char *s; if (kobj->name && !fmt) return 0 ; kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs); if (!kobj->name) { kobj->name = old_name; return -ENOMEM; } while ((s = strchr (kobj->name, '/' ))) s[0 ] = '!' ; kfree(old_name); return 0 ; }
2.2.3 kobject_get
增加 kobject 的引用计数
1 2 3 4 5 6 7 8 9 10 struct kobject *kobject_get (struct kobject *kobj) { if (kobj) kref_get(&kobj->kref); return kobj; }
2.2.4 kobject_put
减少 kobject 的引用计数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void kobject_put (struct kobject *kobj) { if (kobj) { if (!kobj->state_initialized) WARN(1 , KERN_WARNING "kobject: '%s' (%p): is not " "initialized, yet kobject_put() is being " "called.\n" , kobject_name(kobj), kobj); kref_put(&kobj->kref, kobject_release); } }
如果引用计数变为 0,就调用 kobject_release 函数。
2.2.5 kobject_add
增加一个 kobject 到 sys 目录下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 int kobject_add (struct kobject *kobj, struct kobject *parent, const char *fmt, ...) { va_list args; int retval; if (!kobj) return -EINVAL; if (!kobj->state_initialized) { printk(KERN_ERR "kobject '%s' (%p): tried to add an " "uninitialized object, something is seriously wrong.\n" , kobject_name(kobj), kobj); dump_stack(); return -EINVAL; } va_start(args, fmt); retval = kobject_add_varg(kobj, parent, fmt, args); va_end(args); return retval; }
1 2 3 4 5 6 7 8 9 10 11 12 13 static int kobject_add_varg (struct kobject *kobj, struct kobject *parent, const char *fmt, va_list vargs) { int retval; retval = kobject_set_name_vargs(kobj, fmt, vargs); if (retval) { printk(KERN_ERR "kobject: can not set name properly!\n" ); return retval; } kobj->parent = parent; return kobject_add_internal(kobj); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 static int kobject_add_internal (struct kobject *kobj) { int error = 0 ; struct kobject *parent ; if (!kobj) return -ENOENT; if (!kobj->name || !kobj->name[0 ]) { WARN(1 , "kobject: (%p): attempted to be registered with empty " "name!\n" , kobj); return -EINVAL; } parent = kobject_get(kobj->parent); if (kobj->kset) { if (!parent) parent = kobject_get(&kobj->kset->kobj); kobj_kset_join(kobj); kobj->parent = parent; } pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n" , kobject_name(kobj), kobj, __func__, parent ? kobject_name(parent) : "<NULL>" , kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>" ); error = create_dir(kobj); if (error) { kobj_kset_leave(kobj); kobject_put(parent); kobj->parent = NULL ; if (error == -EEXIST) WARN(1 , "%s failed for %s with " "-EEXIST, don't try to register things with " "the same name in the same directory.\n" , __func__, kobject_name(kobj)); else WARN(1 , "%s failed for %s (error: %d parent: %s)\n" , __func__, kobject_name(kobj), error, parent ? kobject_name(parent) : "'none'" ); } else kobj->state_in_sysfs = 1 ; return error; }
2.2.6 kobject_create
动态分析一个 kobject
1 2 3 4 5 6 7 8 9 10 11 struct kobject *kobject_create (void ) { struct kobject *kobj ; kobj = kzalloc(sizeof (*kobj), GFP_KERNEL); if (!kobj) return NULL ; kobject_init(kobj, &dynamic_kobj_ktype); return kobj; }
2.2.7 kobject_del
从 sys 删除对应的 kobj
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void kobject_del (struct kobject *kobj) { struct kernfs_node *sd ; if (!kobj) return ; sd = kobj->sd; sysfs_remove_dir(kobj); sysfs_put(sd); kobj->state_in_sysfs = 0 ; kobj_kset_leave(kobj); kobject_put(kobj->parent); kobj->parent = NULL ; }
三、总线
3.1 struct bus_type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 struct bus_type { const char *name; const char *dev_name; const struct attribute_group **bus_groups ; const struct attribute_group **dev_groups ; const struct attribute_group **drv_groups ; int (*match)(struct device *dev, const struct device_driver *drv); int (*uevent)(const struct device *dev, struct kobj_uevent_env *env); int (*probe)(struct device *dev); void (*sync_state)(struct device *dev); void (*remove)(struct device *dev); void (*shutdown)(struct device *dev); const struct cpumask *(*irq_get_affinity )(struct device *dev , unsigned int irq_vec ); int (*online)(struct device *dev); int (*offline)(struct device *dev); int (*suspend)(struct device *dev, pm_message_t state); int (*resume)(struct device *dev); int (*num_vf)(struct device *dev); int (*dma_configure)(struct device *dev); void (*dma_cleanup)(struct device *dev); const struct dev_pm_ops *pm ; bool driver_override; bool need_parent_lock; };
常见总线类型:
总线
说明
platform
SoC 内部设备(最常用)
pci
PCI/PCIe 设备
usb
USB 设备
i2c
I2C 总线设备
spi
SPI 总线设备
virtio
虚拟化设备
ccw
IBM channel 总线
amba
ARM AMBA 总线
3.2 总线注册
1 2 3 4 5 6 7 8 9 10 11 bus_register(&my_bus_type); device_register(&my_dev); driver_register(&my_driver); bus_type.match(dev, drv)
四、设备
4.1 struct device
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 struct device { struct kobject kobj ; struct device *parent ; struct device_private *p ; const char *init_name; const struct device_type *type ; const struct bus_type *bus ; struct device_driver *driver ; void *platform_data; void *driver_data; struct dev_pm_info power ; struct device_node *of_node ; struct fwnode_handle *fwnode ; dev_t devt; struct class *class ; };
4.2 设备注册流程
1 2 3 4 5 6 7 8 9 10 device_register (dev) ↓ device_initialze (dev) 初始化 kobj、引用计数 ↓ device_add (dev) 添加到设备层次 ↓ kobject_add () bus_add_device () bus_probe_device () 创建 sysfs 目录 添加到总线 尝试绑定驱动
4.3 设备号
1 2 3 4 5 6 7 8 9 10 MKDEV(major, minor) MAJOR(dev_t ) MINOR(dev_t ) alloc_chrdev_region(&dev, baseminor, count, name); register_chrdev_region(MKDEV(major, minor), count, name);
五、驱动
5.1 struct device_driver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 struct device_driver { const char *name; const struct bus_type *bus ; struct module *owner ; bool suppress_bind_attrs; enum probe_type probe_type ; const struct of_device_id *of_match_table ; const struct acpi_device_id *acpi_match_table ; 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 (*resume)(struct device *dev); const struct attribute_group **groups ; const struct dev_pm_ops *pm ; };
5.2 驱动注册流程
1 2 3 4 5 6 7 8 9 10 driver_register (drv) ↓ bus_add_driver (drv) 添加到总线 ↓ driver_attach (drv) 遍历总线设备匹配 ↓__driver_attac (dev, drv) 匹配成功 → probe
5.3 匹配方式
匹配类型
说明
设备树匹配
of_match_table — compatible 字符串比较
ACPI 匹配
acpi_match_table — ACPI 设备 ID
ID 表匹配
id_table — 设备 ID(PCI、USB 等)
名称匹配
name — 直接名称比较
六、设备类
设备类(class)提供功能分组 ,不直接参与设备-驱动绑定。
6.1 struct class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 struct class { const char *name; struct module *owner ; struct class_attribute *class_attrs ; const struct attribute_group **dev_groups ; struct kobject *dev_kobj ; int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env); char *(*devnode)(struct device *dev, umode_t *mode); void (*class_release)(struct class *class); void (*dev_release)(struct device *dev); int (*suspend)(struct device *dev, pm_message_t state); int (*resume)(struct device *dev); const struct kobj_ns_type_operations *ns_type ; const void *(*namespace)(struct device *dev); const struct dev_pm_ops *pm ; struct subsys_private *p ; };
6.2 常见设备类
类名
sysfs 路径
说明
block
/sys/class/block/
块设备(磁盘、分区)
net
/sys/class/net/
网络设备
tty
/sys/class/tty/
终端设备
input
/sys/class/input/
输入设备
drm
/sys/class/drm/
显示设备
i2c-adapter
/sys/class/i2c-adapter/
I2C 控制器
pci_bus
/sys/bus/pci/
PCI 总线
scsi_device
/sys/class/scsi_device/
SCSI 设备
类 vs 总线 :总线关注设备-驱动的匹配和绑定;类关注用户空间的分组和接口。一个设备可以同时属于某个总线和某个类。
七、probe 流程
probe 是设备模型中最关键的环节 — 当驱动匹配到设备时调用 probe() 初始化硬件。
1 2 3 4 5 6 7 8 9 10 11 设备注册/驱动注册 ↓bus_type .match(dev, drv) 添加到总线 ↓ 匹配成功 device_driver.probe(dev) ↓ 或通过总线 bus_type .probe(dev) ↓ drv->probe(dev) 驱动特定初始化
八、sysfs
sysfs(/sys)是设备模型的用户空间接口 ,将内核对象层次导出为文件系统。
8.1 sysfs 目录映射
sysfs 路径
内核对象
/sys/bus/
所有注册的总线
/sys/class/
所有注册的设备类
/sys/devices/
所有设备(全局设备树)
/sys/dev/
按设备号索引的符号链接
/sys/firmware/
固件接口
/sys/module/
已加载模块
/sys/power/
系统电源状态
8.2 属性导出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 static DEVICE_ATTR_RO (my_status) ; static DEVICE_ATTR_RW (my_config) ; static DEVICE_ATTR_WO (my_reset) ; static BUS_ATTR_RO (my_bus_attr) ;static DRIVER_ATTR_RO (my_drv_attr) ; device_create_file(&dev, &dev_attr_my_status); device_remove_file(&dev, &dev_attr_my_status);