您的位置:首页 > 运维架构 > Linux

Linux总线设备驱动模型

2017-04-06 20:43 330 查看

Linux总线设备驱动模型

总线设计

总线描述

总线是处理器和设备之间的通道,在设备模型中,所有的设备读通过总线相连,甚至是内部的虚拟“platform”总线。在Linux设备模型中,总线由bus_type结构表示,定义在<linux/device.h>

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 (*suspend_late)(struct device *dev, pm_message_t state);

int (*resume_early)(struct device *dev);

int (*resume)(struct device *dev);

struct dev_pm_ops *pm;

struct bus_type_private *p;

}

总线注册

bus_register(structbus_type * bus)

若成功,新的总线将被添加进系统,并可在sysfs的/sys/bus下看到。

总线删除

voidbus_unregister(struct bus_type *bus)

总线属性

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);

}

创建属性

intbus_create_file(struct bus_type *bus,struct bus_attribute *attr)

删除属性

void bus_remove_file(structbus_type*bus, struct bus_attribute *attr)

 

设备设计

设备描述

         Linux系统中的每一个设备由一个structdevice描述:

struct device {

…… …… …… ………… ……

struct kobject kobj;

char bus_id[BUS_ID_SIZE]; /*在总线上唯一标识该设备的字符串 */

struct bus_type *bus; /* 设备所在总线 */

struct device_driver *driver; /*管理该设备的驱动*/

void *driver_data; /*该设备驱动使用的私有数据成员 *

struct klist_node knode_class;

struct class *class;

struct attribute_group **groups;

void (*release)(struct device *dev);

}

设备注册

intdevice_register(struct device *dev)

         在驱动注册函数中内核会在相应的my_bus总线上遍历所有的设备,做匹配操作,匹配函数为my_match(bus.c)匹配方式为设备名和驱动注册的名字是否一致,如果匹配成功,驱动函数为my_probe被调用。

设备注销

         voiddevice_unregister(struct device *dev)

设备属性

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);

}

创建属性

intdevice_create_file(struct device*device, struct device_attribute * entry)

删除属性

voiddevice_remove_file(struct device *dev, struct device_attribute * attr)

驱动设计

驱动描述

         驱动程序由structdevice_driver描述:

struct device_driver {

const char *name; /*驱动驱动 程序 的 名字 字( 体 体 现在 在 sysfs 中 中 )*/

struct bus_type *bus; /*驱动 驱动 程序所在的总线 所在的总线*/

struct module *owner;

const char  *mod_name;

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);

struct attribute_group **groups;

struct dev_pm_ops *pm;

struct driver_private *p;

}

驱动注册

         intdriver_register(struct device_driver *drv)

驱动注销

         voiddriver_unregister(struct device_driver *drv)

驱动属性

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);

}

创建属性

int driver_create_file(struct device_driver * drv,

struct driver_attribute * attr)

删除属性

void driver_remove_file(struct device_driver * drv,

struct driver_attribute * attr)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息