13.总线设备驱动模型(1)-总线设备驱动模型
2017-01-04 01:15
190 查看
总线
在 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
相关文章推荐
- 13.总线设备驱动模型(2)-平台总线设备设计
- 总线设备驱动模型总结
- Linux 设备模型 --- 总线设备驱动模型 --- 驱动
- Linux SPI总线设备驱动模型详解
- 驱动分层/分离,总线驱动设备模型
- usb驱动开发4之总线设备驱动模型
- Linux平台总线驱动设备模型
- Linux 内核--总线设备驱动模型(字符设备 && misc字符设备)
- 20150226 IMX257 总线设备驱动模型编程之驱动篇
- Linux设备模型--总线、驱动、设备、设备类 (相关结构介绍)
- 总线设备驱动模型---platform
- 设备驱动基础1:设备模型之总线,驱动,设备
- Linux 设备总线驱动模型
- 平台总线设备驱动模型——代码分析
- Linux设备模型(总线、设备、驱动程序和类)之三:device_driver && 多厂家驱动自动识别
- linux设备总线驱动模型 之 platform总线驱动
- Linux总线、设备、驱动模型
- linux驱动摸索 --DS1302芯片移植(基于总线驱动设备模型)
- linux平台总线驱动设备模型之点亮LED