现在的位置: 首页 > 综合 > 正文

bus, device, driver之间是怎么一回事

2018年02月07日 ⁄ 综合 ⁄ 共 2633字 ⁄ 字号 评论关闭

分类: LINUX

下面是简单的反映bus, device, driver之间关系的代码。将一个名为zldevice的设备,一个名为zldriver的驱动挂在一个名为zlbus的总线上。



下面是一个虚拟总线的代码:


#include <linux/module.h>

#include <linux/init.h>


#include <linux/fs.h>

#include <linux/cdev.h>

#include <linux/mm.h>

#include <linux/ioport.h>

#include <linux/interrupt.h>

#include <linux/delay.h>

#include <linux/device.h>


#include <asm/io.h>


MODULE_LICENSE("GPL");

MODULE_AUTHOR("zl");

struct bus_type zl_bus = { 

    .name = "zlbus", //this string
will be show as /sys/bus/zlbus


};


EXPORT_SYMBOL(zl_bus);


int test_init(void)

{

    int ret = 0;


    bus_register(&zl_bus);


    return ret;

}


void test_exit(void)

{

    bus_unregister(&zl_bus);

}


module_init(test_init);

module_exit(test_exit);

插入以上虚拟总线代码编译生成的模块后:

   1.在/sys/bus/目录下有zlbus目录生成,

   2.在/sys/bus/zlbus/下有devices,drivers两个目录和drivers_autoprobe,drivers_probe,uevent三个文件出现。


下面是接在zl_bus虚拟总线下的一个设备的代码:


#include <linux/module.h>

#include <linux/init.h>


#include <linux/fs.h>

#include <linux/cdev.h>

#include <linux/mm.h>

#include <linux/ioport.h>

#include <linux/interrupt.h>

#include <linux/delay.h>

#include <linux/device.h>


#include <asm/io.h>


MODULE_LICENSE("GPL");

MODULE_AUTHOR("zl");

extern struct bus_type zl_bus;

struct device zl_device_struct = { 

    //.bus_id = "zldevice", //2.6.13

    .init_name = "zldevice", //2.6.36
this string
will be show as /sys/devices/zldevice

    .bus = &zl_bus,

};

int test_init(void)

{

    int ret = 0;


    device_register(&zl_device_struct);

    

    return ret;

}


void test_exit(void)

{

    device_unregister(&zl_device_struct);

}


module_init(test_init);

module_exit(test_exit);

插入以上虚拟设备代码编译生成的模块后:

   1.有/sys/devices/zldevice目录生成。

   2.有/sys/bus/zlbus/devices/zldevice文件生成。并且/sys/bus/zlbus/devices/zldevice -> ../../../devices/zldevice

   4.以上zldevice目录下有power目录,及uevent,subsystem文件出现。

   5.其中subsystem -> ../../bus/zlbus


下面是接在zl_bus虚拟总线下的一个驱动的代码:

#include <linux/module.h>

#include <linux/init.h>


#include <linux/fs.h>

#include <linux/cdev.h>

#include <linux/mm.h>

#include <linux/ioport.h>

#include <linux/interrupt.h>

#include <linux/delay.h>

#include <linux/device.h>


#include <asm/io.h>


MODULE_LICENSE("GPL");

MODULE_AUTHOR("zl");

extern struct bus_type zl_bus;

struct device_driver zl_driver_struct = { 

     .name = "zldriver",
//
this string will be show as/sys/bus/zlbus/drivers/zldriver

     .bus = &zl_bus,

};

int test_init(void)

{

    int ret = 0;


    driver_register(&zl_driver_struct);

    return ret;

}


void test_exit(void)

{

    driver_unregister(&zl_driver_struct);

}


module_init(test_init);

module_exit(test_exit);

插入以上虚拟设备驱动代码编译生成的模块后:

   1.在/sys/bus/zlbus/drivers目录下有zldriver目录生成。

   2./sys/bus/zlbus/drivers/zldriver目录下有bind,unbind,uevent文件及zldevice出现。

   3.其中zldevice -> ../../../../devices/zldevice。

抱歉!评论已关闭.