13.总线设备驱动模型(1)-总线设备驱动模型

总线

在 Linux 内核中,  总线由 bus_type  结构表示,

定义在 <linux/device.h>
struct bus_type {
const char *name; /* 总线 名称*/
int (*match) (struct device *dev, struct device_driver *drv); /* 驱动 与 设备的 匹配函数*/
………

}

int (*match)(struct device * dev, struct device_driver * drv)
当一 个 新 设备或者 新 驱动 被添加到这 个总线时 ,该 函数被调 用。 用 于判断指定 的驱动程序是否能处理指定 的设备 。 若 可 以 , 则返回非零 


总线的注 册 使用 如下 函数
bus_register(struct bus_type *bus)
若 成 功 , 新 的总线将 被添加进系统 ,并 可 在/sys/bus  下看 到 相 应的 目录 。

总线的注 销 使用:
void bus_unregister(struct bus_type *bus)

驱动

在Linux内核中,  驱动由device_driver 结构表示 。
struct device_driver {
{
const char *name; /* 驱动 名称*/
struct bus_type *bus; /* 驱动程 序 所在的总线*/
int (*probe) (struct device *dev);
………
}


驱动的注册使用 如下 函数
int driver_register(struct device_driver *drv)

驱动的注销使用 :
void driver_unregister(struct device_driver *drv)

设备

在Linux内 内 核中,  设备 由 struct device 结构表示 。
struct device {
{

const char *init_name; /*init_name是新添加的,替代了原来的bus_id,但是init_name不能直接被读写操作*/

struct kobject kobj;/*代表该设备并将其连接到结构体系中的 kobject; 注意:作为通用的规则, device->kobj->parent 应等于 device->parent->kobj*/
struct bus_type *bus; /* 设备所在的总线*/
………
}

设备的注 册 使用 如下 函数
int device_register(struct device *dev)


设备 的注 销 使用 :
void device_unregister(struct device *dev)


bus.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>

MODULE_LICENSE("GPL");

int my_match(struct device *dev, struct device_driver *drv)
{
    return !strncmp(dev->kobj.name,drv->name,strlen(drv->name));

}  

struct bus_type my_bus_type = {
.name = "my_bus",
.match = my_match,
};

EXPORT_SYMBOL(my_bus_type);

int my_bus_init()
{
int ret;
ret = bus_register(&my_bus_type);
return ret;
}

void my_bus_exit()
{
bus_unregister(&my_bus_type);
}

module_init(my_bus_init);
module_exit(my_bus_exit);


driver.c

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

MODULE_LICENSE("GPL");

extern struct bus_type my_bus_type;

int my_probe(struct device *dev)
{
    printk("driver found the device it can handle!\n");
    return 0;
}

struct device_driver my_driver = {
    .name = "my_dev",
    .bus = &my_bus_type,
    .probe = my_probe,
};

int my_driver_init()
{
int ret;
ret = driver_register(&my_driver);
return ret;
}

void my_driver_exit()
{
driver_unregister(&my_driver);
}

module_init(my_driver_init);
module_exit(my_driver_exit);

device.c

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

MODULE_LICENSE("GPL");

extern struct bus_type my_bus_type;

struct device my_dev = {
     .init_name = "my_dev",
     .bus = &my_bus_type,
};

int my_device_init()
{
     int ret;
     ret = device_register(&my_dev);
     return ret;
}

void my_device_exit()
{
device_unregister(&my_dev);
}

module_init(my_device_init);
module_exit(my_device_exit);

需要注意的是linux-2.6.30内核以前,没有init_name元素,而是元素bus_id,这个主要是实现设备名的填充,但是linux-2.6.30内核之后的在struct device中init_name代替了bus_id,但是需要注意的是init_name不能直接被读写,当需要读写设备名时只能采用特定的函数实现:dev_name(),set_dev_name()。当直接读写init_name会导致内核错误,出现Unable to handle kernel NULL pointer dereference at virtual address 00000000的错误。
注意:bus.c  my_match()
return !strncmp(dev->init.name,drv->name,strlen(drv->name));
device_register()->device_add()->
if (dev->init_name) {
dev_set_name(dev, "%s", dev->init_name);
dev->init_name = NULL;
}


http://www.cnblogs.com/andtt/articles/2178905.html

http://www.51hei.com/mcu/2934.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值