设备驱动模型

来源:互联网 发布:网络安全技术是什么 编辑:程序博客网 时间:2024/06/05 15:34

随着技术的不断进步,系统的拓扑结构也越来越复杂,对智能电源管理、热插拔的支持要求也越来越高,2.4内核已经难以满足这些需求。为适应这种形势的需要,Linux 2.6内核提供了全新的内核设备模型。

设备模型元素:总线   驱动      设备

总线

总线是处理器设备之间的通道在设备模型中,所有的设备都通过总线相连,甚至是

内部的虚拟“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(struct bus_type * bus) 若成功,新的总线将被添加进系统,并可在sysfs 的/sys/bus 下看到。

总线的删除使用: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) 在为用户空间产生热插拔事件之前,这个方法允许总线添加环境变量。

 

总线属性

总线属性由结构bus_attribute 描述,定义如下:

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

}

 

int bus_create_file(struct bus_type *bus,struct bus_attribute *attr)  创建属性

void bus_remove_file(struct bus_type*bus,struct bus_attribute *attr) 删除属性

 

实例Bus_basic.c

 

设备描述

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

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

}

 

设备注册 

int device_register(struct device *dev)   注册设备  

void device_unregister(struct device *dev)    注销设备

**一条总线也是个设备,也必须按设备注册**

 

设备属性

设备属性由struct device_attribute 描述:

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,constchar *buf, size_t count);

}

 

int device_create_file(struct device*device,struct device_attribute * entry)创建属性

void device_remove_file(struct device *dev,struct device_attribute * attr)删除属性

 

实例Bus.c    Device.c

 

 

驱动描述

驱动程序由struct device_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;

}

 

驱动注册/注销

int driver_register(struct device_driver *drv)   注册驱动

void driver_unregister(struct device_driver *drv)    注销驱动

 

驱动属性

驱动的属性使用struct driver_attribute 来描述:

struct driver_attribute {

struct attribute attr;

ssize_t (*show)(struct device_driver *drv,char *buf);

ssize_t (*store)(struct device_driver *drv,constchar *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)  删除属性

 

实例driver.c

原创粉丝点击