- 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;
- };
- /*linux-3.1.6*/
- 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);
- 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 (*resume)(struct device *dev);
-
- const struct dev_pm_ops *pm;
-
- struct subsys_private *p;
- };
- struct bus_type my_bus_type = {
- .name = "my_bus",
- .match = my_match,
- };
- int bus_register(struct bus_type * bus)
- ret = bus_register(&my_bus_type);
- if (ret)
- return ret;
- void bus_unregister(struct bus_type *bus)
- int (*match)(struct device * dev, struct device_driver * drv);
- /*当一个新设备或者驱动被添加到这个总线时,这个方法会被调用一次或多次,若指定的驱动程序能够处理指定的设备,则返回非零值。*/
-
- int (*uevent)(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
- /*在为用户空间产生热插拔事件之前,这个方法允许总线添加环境变量(参数和 kset 的uevent方法相同)*/
- static int my_match(struct device *dev, struct device_driver *driver)
- {
- return !strncmp(dev->bus_id, driver->name, strlen(driver->name));
- }
- 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 返回。*/
- 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);
- };
- 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);
- /*
- * Export a simple attribute.
- */
- static ssize_t show_bus_version(struct bus_type *bus, char *buf)
- {
- return snprintf(buf, PAGE_SIZE, "%s\n", Version);
- }
-
- static BUS_ATTR(version_file, S_IRUGO, show_bus_version, NULL);
- //将会在/sys/bus/my_bus/下显示version_file文件
- if (bus_create_file(&my_bus_type, &bus_attr_version_file))
- printk(KERN_NOTICE "Fail to create version attribute!\n");
- struct device {
- struct klist klist_children;
- struct klist_node knode_parent; /* node in sibling list */
- struct klist_node knode_driver;
- struct klist_node knode_bus;
- struct device *parent;/* 设备的 "父" 设备,该设备所属的设备,通常一个父设备是某种总线或者主控制器. 如果 parent 是 NULL, 则该设备是顶层设备,较少见 */
-
- struct kobject kobj;/*代表该设备并将其连接到结构体系中的 kobject; 注意:作为通用的规则, device->kobj->parent 应等于 device->parent->kobj*/
- char bus_id[BUS_ID_SIZE];/*在总线上唯一标识该设备的字符串;例如: PCI 设备使用标准的 PCI ID 格式, 包含:域, 总线, 设备, 和功能号.*/
- struct device_type *type;
- unsigned is_registered:1;
- unsigned uevent_suppress:1;
- struct device_attribute uevent_attr;
- struct device_attribute *devt_attr;
-
- struct semaphore sem; /* semaphore to synchronize calls to its driver. */
- struct bus_type * bus; /*标识该设备连接在何种类型的总线上*/
- struct device_driver *driver; /*管理该设备的驱动程序*/
- void *driver_data; /*该设备驱动使用的私有数据成员*/
- void *platform_data; /* Platform specific data, device core doesn't touch it */
- struct dev_pm_info power;
-
- #ifdef CONFIG_NUMA
- int numa_node; /* NUMA node this device is close to */
- #endif
- u64 *dma_mask; /* dma mask (if dma'able device) */
- u64 coherent_dma_mask;/* Like dma_mask, but for
- alloc_coherent mappings as
- not all hardware supports
- 64 bit addresses for consistent
- allocations such descriptors. */
-
- struct list_head dma_pools; /* dma pools (if dma'ble) */
-
- struct dma_coherent_mem *dma_mem; /* internal for coherent mem override */
- /* arch specific additions */
- struct dev_archdata archdata;
-
- spinlock_t devres_lock;
- struct list_head devres_head;
-
- /* class_device migration path */
- struct list_head node;
- struct class *class;
- dev_t devt; /* dev_t, creates the sysfs "dev" */
- struct attribute_group **groups; /* optional groups */
-
- void (*release)(struct device * dev);/*当这个设备的最后引用被删除时,内核调用该方法; 它从被嵌入的 kobject 的 release 方法中调用。所有注册到核心的设备结构必须有一个 release 方法, 否则内核将打印错误信息*/
- };
- /*在注册 struct device 前,最少要设置parent, bus_id, bus, 和 release 成员*/
- int device_register(struct device *dev);
- void device_unregister(struct device *dev);
- static void my_bus_release(struct device *dev)
- {
- printk(KERN_DEBUG "my bus release\n");
- }
- struct device my_bus_dev = {
- .bus_id = "my_bus0",
- .release = my_bus_release
- };
- /*注册总线设备*/
- ret = device_register(&my_bus_dev);
- if (ret)
- printk(KERN_NOTICE "Fail to register device:my_bus!\n");
- /* interface for exporting device attributes 这个结构体和《LDD3》中的不同,已经被更新过了,请特别注意!*/
- struct device_attribute {
- struct attribute attr;
- ssize_t (*show)(struct device *dev, struct device_attribute *attr,char *buf);
- ssize_t (*store)(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);
- };
-
- /*设备属性结构可在编译时建立, 使用以下宏:*/
- DEVICE_ATTR(_name,_mode,_show,_store);
- /*这个宏声明一个结构, 将 dev_attr_ 作为给定 _name 的前缀来命名设备属性
-
- /*属性文件的实际处理使用以下函数:*/
- int device_create_file(struct device *device, struct device_attribute * entry);
- void device_remove_file(struct device * dev, struct device_attribute * attr);
lddbus 驱动创建了它自己的 device 类型,并期望每个设备驱动使用这个类型来注册它们的设备:
struct ldd_device { |
lddbus 导出的注册和注销接口如下:
/* |
- /*定义在<linux/device.h>*/
- struct device_driver {
- const char * name;/*驱动程序的名字( 在 sysfs 中出现 )*/
- struct bus_type * bus;/*驱动程序所操作的总线类型*/
-
- struct kobject kobj;/*内嵌的kobject对象*/
- struct klist klist_devices;/*当前驱动程序能操作的设备链表*/
- struct klist_node knode_bus;
-
- struct module * owner;
- const char * mod_name; /* used for built-in modules */
- struct module_kobject * mkobj;
-
- 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);
- };
-
- /*注册device_driver 结构的函数是:*/
- int driver_register(struct device_driver *drv);
- void driver_unregister(struct device_driver *drv);
-
- /*driver的属性结构在:*/
- struct driver_attribute {
- struct attribute attr;
- ssize_t (*show)(struct device_driver *drv, char *buf);
- ssize_t (*store)(struct device_driver *drv, const char *buf, size_t count);
- };
- DRIVER_ATTR(_name,_mode,_show,_store)
-
- /*属性文件创建的方法:*/
- int driver_create_file(struct device_driver * drv, struct driver_attribute * attr);
- void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr);
-
- /*bus_type 结构含有一个成员( drv_attrs ) 指向一组为属于该总线的所有设备创建的默认属性*/
- struct device_driver {
- const char *name;
- struct bus_type *bus;
-
- struct module *owner;
- const char *mod_name; /* used for built-in modules */
-
- bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
-
- const struct of_device_id *of_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;
-
- struct driver_private *p;
- };
- struct driver_private {
- struct kobject kobj;
- struct klist klist_devices;
- struct klist_node knode_bus;
- struct module_kobject *mkobj;
- struct device_driver *driver;
- };
- #define to_driver(obj) container_of(obj, struct driver_private, kobj)
对大多数驱动程序核心结构, device_driver 结构通常被嵌入到一个更高层的、总线相关的结构中。
以lddbus 子系统为例,它定义了ldd_driver 结构:
struct ldd_driver { |
lddbus总线中相关的驱动注册和注销函数是:
/* |
在sculld 中创建的 ldd_driver 结构如下:
/* Device model stuff */ |
类 子系统
类是一个设备的高层视图, 它抽象出了底层的实现细节,从而允许用户空间使用设备所提供的功能, 而不用关心设备是如何连接和工作的。类成员通常由上层代码所控制, 而无需驱动的明确支持。但有些情况下驱动也需要直接处理类。
几乎所有的类都显示在 /sys/class 目录中。出于历史的原因,有一个例外:块设备显示在 /sys/block目录中。在许多情况, 类子系统是向用户空间导出信息的最好方法。当类子系统创建一个类时, 它将完全拥有这个类,根本不用担心哪个模块拥有那些属性,而且信息的表示也比较友好。
为了管理类,驱动程序核心导出了一些接口,其目的之一是提供包含设备号的属性以便自动创建设备节点,所以udev的使用离不开类。 类函数和结构与设备模型的其他部分遵循相同的模式,所以真正崭新的概念是很少的。
注意:class_simple 是老接口,在2.6.13中已被删除,这里不再研究。
管理类的接口
类由 struct class 的结构体来定义:
/* |
/*in Linux 2.6.26.5*/ |
类设备(在新内核中已被删除)
类存在的真正目的是给作为类成员的各个设备提供一个容器,成员由 struct class_device 来表示:
struct class_device { |
类接口
类子系统有一个 Linux 设备模型的其他部分找不到的附加概念,称为“接口”, 可将它理解为一种设备加入或离开类时获得信息的触发机制,结构体如下:struct class_interface { /*当一个类设备被加入到在 class_interface 结构中指定的类时, 将调用接口的 add 函数,进行一些设备需要的额外设置,通常是添加更多属性或其他的一些工作*/ |
![](/blog/image/attachicons/rar.gif)