linux设备驱动模型之 device(设备)原理与实例分析

1、 设备描述
        Linux 系统中的每个设备由一个 struct device 描述:
struct device {
...... ...... ...... ...... ...... ......
struct kobject kobj;
char bus_id[BUS_ID_SIZE]; /*在总线上唯一标识该设备的字符串 */
struct bus_type /* 设备所在总线 */
*bus;
struct device_driver *driver; /*管理该设备的驱动*/
void *driver_data;
/*该设备驱动使用的私有数据成员 *
struct klist_node knode_class;
struct class *class;
struct attribute_group
**groups;
void (*release)(struct device *dev);
}
2、 设备注册
       1)int device_register(struct device *dev)
            注册设备
      2)void device_unregister(struct device *dev)
           注销设备
      **一条总线也是个设备,也必须按设备注册**
3、 设备属性
       设备属性由struct device_attribute 描述:
struct device_attribute
{
struct attribute attr;
ssize_t (*show)(struct device *dev, struct device_attribute
*attr,char *buf);
ssize_t (*store)(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count);
}

       1)int device_create_file(struct device *device, struct device_attribute * entry)
             创建属性
       2)void device_remove_file(struct device * dev, struct device_attribute * attr)
            删除属性
4、 实例分析

      1)Bus.c源码

#include
#include
#include
#include
#include

MODULE_AUTHOR("yinjiabin");
MODULE_LICENSE("Dual BSD/GPL");

static char *Version = "$Revision: 1.0 $";


/*当一个新设备或者驱动被添加到这个总线时,该方法被调用。用于判断指定的驱动程序是否能处理指定的设备。若可以,则返回非零值。*/

static int my_match(struct device *dev, struct device_driver *driver)
{
        return !strncmp(dev->kobj.name, driver->name, strlen(driver->name));
}

static void my_bus_release(struct device *dev)
{
        printk(KERN_DEBUG "my bus release\n");
}

struct device my_bus = {
        .kobj.name   = "my_bus0",
        .release  = my_bus_release
};

/*声明总线*/
struct bus_type my_bus_type = {
        .name = "my_bus",  //总线名字
        .match = my_match, //总线match函数指针
};

EXPORT_SYMBOL(my_bus);      //导出my_bus
EXPORT_SYMBOL(my_bus_type); //导出my_bus_type


/*
 * Export a simple attribute.
 */
static ssize_t show_bus_version(struct bus_type *bus, char *buf)
{
        return snprintf(buf, PAGE_SIZE, "%s\n", Version);
}


/*内核代码中如此定义:#define BUS_ATTR(_name, _mode, _show, _store) \
struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store),

它将bus_attr_作为给定的name的前缀来创建总线的真正名称。对应下面的是bus_attr_version*/
static BUS_ATTR(version, S_IRUGO, show_bus_version, NULL);

/*模块加载函数*/
static int __init my_bus_init(void)
{
        int ret;

        /*注册总线*/
        ret = bus_register(&my_bus_type);
        if (ret)
                return ret;

        /*创建属性文件*/
        if (bus_create_file(&my_bus_type, &bus_attr_version))
                printk(KERN_NOTICE "Fail to create version attribute!\n");

        /*注册总线设备*/
        ret = device_register(&my_bus);
        if (ret)
                printk(KERN_NOTICE "Fail to register device:my_bus!\n");

        return ret;
}

/*模块卸载函数*/
static void my_bus_exit(void)
{
        device_unregister(&my_bus);
        bus_unregister(&my_bus_type);
}

module_init(my_bus_init);
module_exit(my_bus_exit);


      2)Device.c源码

#include
#include
#include
#include
#include

MODULE_AUTHOR("yinjiabin");
MODULE_LICENSE("Dual BSD/GPL");

extern struct device my_bus;
extern struct bus_type my_bus_type;

/* Why need this ?*/
static void my_dev_release(struct device *dev)
{

}

struct device my_dev = {
        .init_name = "my_dev",
        .bus = &my_bus_type,   //指明设备所属的总线
        .parent = &my_bus,     //该设备的父设备,总线也是一种设备
        .release = my_dev_release,
};

static ssize_t mydev_show(struct device *dev,struct device_attribute *attr, char *buf)
{
        return sprintf(buf, "%s\n", "This is my device!");
}

static DEVICE_ATTR(dev, S_IRUGO, mydev_show, NULL);

static int __init my_device_init(void)
{
        int ret = 0;

        /* 初始化设备 */
        ///strncpy(my_dev->kobj.name, "my_dev", strlen(my_dev->name));

        /*注册设备*/
        device_register(&my_dev);

        /*创建属性文件*/
        device_create_file(&my_dev, &dev_attr_dev);

        return ret;

}

static void my_device_exit(void)
{
        device_unregister(&my_dev);
}

module_init(my_device_init);

module_exit(my_device_exit);




你可能感兴趣的:(linux驱动学习)