总线
在 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/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);
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