Linux驱动开发-12、总线设备驱动模型

总线设备驱动模型

1、概念:将属于同一种接口(如USBI2C等)的不同设备挂载在总线上,设备可以通过总线匹配到相应的驱动程序。

 

2、优点:

a) 支持热插拔

b) 驱动程序可移植性提高

3、关系图解 ---- 头文件  #include<linuxx/device.h>


 

4、图解说明

a) 总线设备包含三个结构;既设备,设备的驱动,总线

b) 设备和其驱动通过struct bus_type 结构体实现挂载在同一条总线上

c) 总线结构体中的match匹配函数将通过设备和设备驱动结构体中的name成员来匹配,如果名字相同,则匹配上,返回0。然后调用设备驱动中的 probe 函数来驱动设备;其实这里的匹配函数是搜索挂载在总线上的所有设备和驱动,然后逐个匹配

 

5、总线描述结构及函数

struct bus_type {

const char*name;/*总线名,在/sys/bus目录下注册的名*/

...

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 (*resume)(struct device *dev);

 

const struct dev_pm_ops *pm;

 

struct subsys_private *p;

};

注册:int  bus_register(struct bus_type *bus); 注册成功将在/sys/bus下生成目录文件

 

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

 

6、设备驱动结构描述及函数

struct device_driver {

const char*name;/*必须和设备结构体中的name名相同*/

struct bus_type*bus;

....

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

...

};

注册:int  driver_register(struct device_driver *drv);

 

注销:void driver_unregister(struct device_driver *drv);

 

7、设备描述结构及函数

 

struct device {

....

struct kobject kobj; /*设备初始化名字之后存放到该结构体中*/

const char *init_name; /* initial name of the device */

...

 

struct bus_type *bus; /* type of bus device is on */

struct device_driver *driver; /* which driver has allocated this device */

...

}

注册函数:int __must_check device_register(struct device *dev);

 

注销函数:void device_unregister(struct device *dev);

  

8、程序设计

a) 初始化

b) 注册

c) 注销

d) 总结:Linux 每一种设备都是这种套路,彼此之间相对独立,只有在某一方面通过某个结构体联系在一起

 

 

驱动程序设计

总线创建

/************************************

作者:hntea

时间:2016/3/16

函数功能:总线测试

查看/sys/bus 目录下生成目录

************************************/

 

#include<linux/init.h>

#include<linux/module.h>

#include<linux/device.h>

 

 

/*初始化新总线*/

static int newmatch(struct device *dev, struct device_driver *drv)

{

/*总线驱动会自动去寻找匹配,这里这么写就够了*/

   return !strncmp(dev->kobj.name,drv->name,strlen(drv->name));

}

struct bus_type newbus =

{

.name = "newbus",

.match = newmatch, /*匹配函数*/

};

EXPORT_SYMBOL(newbus); /*符号输出*/

static int newbusInit(void)

{

int ret = 0;

ret = bus_register(&newbus);

return ret;

}

 

static void newbusExit(void)

{

bus_unregister(&newbus);

}

 

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Hntea");

 

module_init(newbusInit);

module_exit(newbusExit);

 

设备驱动创建

/************************************

作者:hntea

时间:2016/3/16

函数功能:总线测试

 

************************************/

#include<linux/init.h>

#include<linux/module.h>

#include<linux/device.h>

 

 

/*初始化设备驱动*/

extern struct bus_type newbus ;

int A_probe(struct device *dev)

{

printk("I find A");

return 0;

}

struct device_driver  A_drv =

{

.name = "A_device",

.bus = &newbus,

.probe = A_probe,

};

 

static int a_driverInit(void)

{

int ret = 0;

ret = driver_register(&A_drv);

return ret;

}

 

static void a_driverExit(void)

{

driver_unregister(&A_drv);

}

 

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Hntea");

module_init(a_driverInit);

module_exit(a_driverExit);

 

设备创建

/************************************

作者:hntea

时间:2016/3/16

函数功能:总线测试

************************************/

 

#include<linux/init.h>

#include<linux/module.h>

#include<linux/device.h>

 

/*初始化设备*/

extern struct bus_type newbus ;

 

/*这个函数要填充,要卸载时会有警告*/

void A_release(struct device *dev)

{

 

}

struct device A_dev=

{

.init_name = "A_device", /* initial name of the device */

.bus  = &newbus, /* type of bus device is on */

.release = A_release

};

static int a_deviceInit(void)

{

int ret = 0;

ret = device_register(&A_dev);

return ret;

}

 

static void a_deviceExit(void)

{

device_unregister(&A_dev);

}

 

MODULE_LICENSE("GPL");

MODULE_AUTHOR("Hntea");

 

module_init(a_deviceInit);

module_exit(a_deviceExit);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux驱动总线设备模型(Driver model)是一种抽象的设备模型,用于描述系统中各种设备设备之间的关系。它提供了一种标准的设备访问接口,使得设备驱动程序可以独立于硬件平台而存在,从而提高了设备驱动程序的可移植性和可维护性。 在Linux中,驱动总线设备模型包括以下几个主要的概念: 1. 总线(Bus):总线是一种连接设备的物理或逻辑结构,用于实现设备之间的通信。Linux支持多种总线类型,例如PCI、USB、I2C等。 2. 设备(Device):设备是指在总线上注册的硬件设备,每个设备都有一个唯一的设备树路径和设备标识符。设备可以包括子设备和属性,例如硬件资源、中断、供电等信息。 3. 驱动程序(Driver):驱动程序是指用于控制设备的软件程序,它通过向设备发送命令和读取设备的状态来实现对设备的控制。驱动程序可以注册到总线上,当设备被插入到总线上时,总线会自动匹配相应的驱动程序并加载它。 4. 类(Class):类是一组具有相似功能的设备的集合,例如输入设备、网络设备、存储设备等。类提供了一些通用的接口和属性,使得驱动程序可以更加方便地操作设备Linux驱动总线设备模型的主要优点包括: 1. 支持多种总线类型,使得驱动程序可以在不同的硬件平台上运行。 2. 提供标准的设备访问接口,使得驱动程序可以独立于硬件平台而存在。 3. 支持设备热插拔和动态配置,使得系统更加灵活和可扩展。 4. 提供了类的概念,使得驱动程序可以更加方便地操作设备

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值