q2113435929的个人空间 https://www.eechina.com/space-uid-126185.html [收藏] [复制] [RSS]

博客

UT4412BV03开发板学习Linux设备驱动模型(二)

已有 1477 次阅读2015-10-16 14:25 | 4412, 开发板

UT4412BV03开发板学习Linux设备驱动模型(二)

 

设备驱动模型有三个重要部分分别是总线(bus_type),设备(device),驱动(driver)

下面对三个组件分别进行介绍。

 

一.总线

从硬件上来讲,物理总线有数据总线和地址总线,在设备驱动模型中所有设备都是通过总线相连接的,驱动程序依附在总线上,下面将表示总线,设备,驱动三者之间的关系

了解了总线的结构之后,下面具体说明总线中用到的一些结构体及相关的函数。

#include <asm/device.h>

1.总线的数据结构bus_type

struct bus_type {

 const char *name; //总线的名字

 struct bus_attribute *bus_attrs; //总线属性和导出到sysfs中的方法

 struct device_attribute *dev_attrs; //设备属性和导出到sysfs中的方法

 struct driver_attribute *drv_attrs; //驱动程序属性和导出到sysfs中的方法

 

 int (*match)(struct device *dev, struct device_driver *drv);//匹配函数,检验参数二的驱动是否支持参数一的设备

 //当一条总线上新设备或新驱动被添加时,会一次或多次调用该函数,

  // 如果指定的驱动能适用于指定的设备,那么该函数返回非0,否则返回0

 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; //总线私有数据

};

2.总线属性数据结构

struct bus_attribute {

 struct attribute attr; //总线属性的变量

 ssize_t (*show)(struct bus_type *bus, char *buf); //属性读函数

 ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count); //属性写函数

};

struct attribute {

 const char *name; //属性名字

 struct module *owner;

 mode_t mode; //属性读写权限

};

3.初始化bus_attribute结构体用的宏

#define BUS_ATTR(_name, _mode, _show, _store) \

struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store)

例: static BUS_ATTR(config, 0644, ap_config_time_show,ap_config_time_store);

对此宏进行扩展为

   struct bus_attribute bus_attr_config_time={

       .attr={.name=config_time,.mode=0644},


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
返回顶部