二. 总线bus、设备device、驱动driver
----总线、设备、驱动是建立在kobject和kset基础之上的,也是设备驱动程序员与之打交道最多的部分。
----总线是Linux设备驱动模型中最核心的框架,设备与驱动都围绕着总线工作,总线可以是实际物理总线(比如PCI总线,I2C总线)的抽象,
----也可以是出去驱动模型构架需要产生的虚拟"平台"总线,因为符合Linux驱动模型的设备与驱动必须挂在一根总线上。
----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 subsys_private - structure to hold the private to the driver core portions of the bus_type/class structure.
*
* @subsys - the struct kset that defines this subsystem
* @devices_kset - the list of devices associated /*设备集合*/
*
* @drivers_kset - the list of drivers associated /*驱动集合*/
* @klist_devices - the klist to iterate over the @devices_kset
* @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.
*
* @class_interfaces - list of class_interfaces associated
* @glue_dirs - "glue" directory to put in-between the parent device to
* avoid namespace conflicts
* @class_mutex - mutex to protect the children, devices, and interfaces lists.
* @class - pointer back to the struct class that this structure is associated
* with.
*
* This structure is the one that is the actual kobject allowing struct
* bus_type/class to be statically allocated safely. Nothing outside of the
* driver core should ever touch these fields.
*/
---struct subsys_private {
struct kset subsys; /*用来表示bus所在的子系统,在内核中所有通过bus_register注册进系统的bus所在的kset都指向bus_kset*/
struct kset *devices_kset;
struct kset *drivers_kset;
struct klist klist_devices;
struct klist klist_drivers;
struct blocking_notifier_head bus_notifier;
unsigned int drivers_autoprobe:1;
struct bus_type *bus; /*指向与subsys_private相关的bus_type*/
struct list_head class_interfaces;
struct kset glue_dirs;
struct mutex class_mutex;
struct class *class;
---};
---/*struct kset subsys; 用来表示bus所在的子系统,
---在内核中所有通过bus_register注册进系统的bus所在的kset都指向bus_kset(总线属性--以及方法),
---bus_kset是所有bus内核对象的容器
---bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); --->bus_init()
---*/
/*
--bus_register - register a bus and enstable a directory blow the /sys/bus/.
--struct kset *kset_create_and_add(const char *name,
const struct kset_uevent_ops *uevent_ops,
struct kobject *parent_kobj)
*/
--int bus_register(struct bus_type *bus)
--{
----retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
----priv->subsys.kobj.kset = bus_kset;
----priv->subsys.kobj.ktype = &bus_ktype;
----priv->devices_kset = kset_create_and_add("devices", NULL, /*在priv_subys.kobj目录下建立devices目录*/
&priv->subsys.kobj);
----priv->drivers_kset = kset_create_and_add("drivers", NULL,
&priv->subsys.kobj);
--}