Chinaunix首页 | 论坛 | 博客
  • 博客访问: 657946
  • 博文数量: 185
  • 博客积分: 1875
  • 博客等级: 上尉
  • 技术积分: 2107
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-23 23:21
个人简介

有时候,就是想窥视一下不知道的东东,因为好奇!

文章分类

全部博文(185)

文章存档

2024年(1)

2023年(3)

2020年(1)

2019年(1)

2018年(1)

2017年(2)

2016年(69)

2015年(53)

2014年(14)

2013年(1)

2012年(5)

2011年(25)

2010年(9)

分类: LINUX

2010-11-18 23:50:41

下面是简单的反映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。

阅读(1217) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-19 15:34:18

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com