Chinaunix首页 | 论坛 | 博客
  • 博客访问: 409267
  • 博文数量: 82
  • 博客积分: 2600
  • 博客等级: 少校
  • 技术积分: 961
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-08 21:44
文章分类
文章存档

2013年(2)

2012年(56)

2010年(24)

我的朋友

分类: LINUX

2012-05-13 00:54:58

作者:吴新武,讲师。

linux提供了新的设备模型:总线(bus)、设备(device)、驱动(driver)。其中总线是处理器与设备之间通道,在设备模型中,所有的设备都通过总线相连;设备是对于一个设备的详细信息描述,驱动是设备的相关驱动。其基本关系如下:bus 相当于一个容器,是device 和device_driver 的管理机构,它包含了一个device 集合和一个driver 集合。其中,device集合包含了挂在该总线下的所有设备,这些设备通过链表链接起来;driver集合包含了挂在该总线下的所有驱动程序,这些驱动程序通过链表链接起来。

1、关于总线bus

(1)bus的结构体

struct bus_type,该结构体中有个函数指针成员:int (*match)(struct device * dev, struct device_driver * drv); 这个函数指针非常重要,当一个新的设备或驱动被添加到一个总线时会被调用,是建立总线上设备与驱动的桥梁。

(2)总线的操作:

注册:int bus_register(struct bus_type * bus)
        注销:void bus_unregister(struct bus_type *bus);

(3)总线属性 bus_attribute

struct bus_attribute BUS_ATTR(name, mode, show, store);

这个宏声明一个结构, 产生它的名子通过前缀字符串 bus_attr_ 到给定的名字(bus_attr_name),然后利用bus_create_file来创建总线属性

int bus_create_file(struct bus_type *bus, struct bus_attribute *attr);

参数中的attr,即为bus_attr_name。

2、关于device

(1)在底层,一个设备对于一个设备结构体struct device
        (2)设备的操作

注册设备:int device_register(sruct device *dev)
        注销设备:void device_unregister(struct device *dev);

(3)设备属性:

sysfs 中的设备入口可有属性. 相关的结构是:

struct device_attribute这个属性结构可在编译时建立, 使用宏:

DEVICE_ATTR(name, mode, show, store);

结果结构通过前缀 dev_attr_ 到给定名子上来命名. 属性文件的实际管理使用通常的函数对来处理:

int device_create_file(struct device *device, struct device_attribute *entry);
        void device_remove_file(struct device *dev, struct device_attribute *attr);

3、关于driver

(1)driver结构体为struct device_driver

(2)驱动程序的操作

驱动程序的注册int driver_register(struct device_driver *drv);
        驱动程序的注销void driver_unregister(struct device_driver *drv);

(3)驱动程序的属性

struct driver_attribute
        DRIVER_ATTR(_name,_mode,_show,_store) /*属性文件创建的方法:*/
        int driver_create_file(struct device_driver * drv, struct driver_attribute * attr);//创建设备驱动的属性
        void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr);//删除设备驱动的属性

阅读(1212) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~