学习内容:总线、设备、驱动
驱动模型:
为了方便维护设备和驱动,内核抽象出三个类:
总线 struct bus_type;
设备 struct device;
驱动 struct device_driver;
这里的总线是抽象的该念总线,逻辑层面的,只是为了管理匹配
向内核注册并和总线关联的设备和驱动的,匹配规则决定于总线。
--------------------------------------------------------------------------------
总线对应结构体:
struct bus_type {
const char *name; //总线的名字,注册成功后会见于/sys/bus/
const char *dev_name;
struct device *dev_root;
struct bus_attribute *bus_attrs;
struct device_attribute *dev_attrs;
struct driver_attribute *drv_attrs;
int (*match)(struct device *dev, struct device_driver *drv); //匹配规则
............
struct subsys_private *p;
};
int (*match)(struct device *dev, struct device_driver *drv); //匹配规则
匹配规则由编写驱动者自己定义。但是匹配成功必须返回1,否则返回0;
总线的注册:
int bus_register(struct bus_type *bus);
void bus_unregister(struct bus_type *bus);
--------------------------------------------------------------------------------
设备对应结构体:
struct device {
struct device *parent;
struct device_private *p;
struct kobject kobj;
const char *init_name; //设备的名字,在/sys/bus/xx/devices目录下可查看
const struct device_type *type;
struct mutex mutex;
struct bus_type *bus; //用于关联的总线
struct device_driver *driver; //匹配成功后指向设备的驱动结构体变量。
void *platform_data;
struct dev_pm_info power;
struct dev_pm_domain *pm_domain;
..........
struct klist_node knode_class;
struct class *class;
const struct attribute_group **groups; //必须实现,当设备从内核移除时会调用此函数
void (*release)(struct device *dev);
};
设备的注册及移除:
int device_register(struct device *dev);
void device_unregister(struct device *dev);
---------------------------------------------------------------------------------------
驱动对应结构体:
struct device_driver {
const char *name; //驱动的名字
struct bus_type *bus; //驱动需要关联的总线
struct module *owner;
const char *mod_name;
bool suppress_bind_attrs;
const struct of_device_id;
int (*probe) (struct device *dev); //匹配成功,内核自动调用probe
int (*remove) (struct device *dev); //移除驱动时会自动调用remove
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;//很重要
};
驱动的注册及移除:
int driver_register(struct device_driver *drv)
void driver_unregister(struct device_driver *drv)