您的位置:首页 > 其它

总线设备驱动模型(bus/platform)

2017-11-26 17:37 267 查看
总线描述结构

在 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; /*设备的名字*/

struct bus_type *bus; /*设备所在的总线*/

………

}

设备驱动注册与注销

设备的注册使用如下函数

int device_register(struct device *dev)

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

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

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

#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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: