总线
总线是处理器与一个或者多个设备之间的通道。在设备模型中,所有的设备都通过总线相连。
在Linux设备模型中,用bus_type结构表示总线,它的定义含在<linux/device.h>中。其结构如下:
struct bus_type {
const char * name;//总线名字
struct subsystemsubsys;
struct kset drivers;//总线的驱动程序
struct kset devices;//插入总线的所有设备
struct klist klist_devices;
struct klist klist_drivers;
struct bus_attribute* bus_attrs;
struct device_attribute* dev_attrs;
struct driver_attribute* drv_attrs;
int (*match)(struct device * dev, struct device_driver * drv);
int (*uevent)(struct device *dev, char **envp,
int num_envp, char *buffer, int buffer_size);
int (*probe)(struct device * dev);
int (*remove)(struct device * dev);
void (*shutdown)(struct device * dev);
int (*suspend)(struct device * dev, pm_message_t state);
int (*resume)(struct device * dev);
};
const char * name;//总线名字
struct subsystemsubsys;
struct kset drivers;//总线的驱动程序
struct kset devices;//插入总线的所有设备
struct klist klist_devices;
struct klist klist_drivers;
struct bus_attribute* bus_attrs;
struct device_attribute* dev_attrs;
struct driver_attribute* drv_attrs;
int (*match)(struct device * dev, struct device_driver * drv);
int (*uevent)(struct device *dev, char **envp,
int num_envp, char *buffer, int buffer_size);
int (*probe)(struct device * dev);
int (*remove)(struct device * dev);
void (*shutdown)(struct device * dev);
int (*suspend)(struct device * dev, pm_message_t state);
int (*resume)(struct device * dev);
};
总线的注册
一般只有少数的bus_type成员需要初始化,其中大部分都由设备模型核心所控制,所以一般代码设置bus_type结构如下:
struct bus_type ldd_bus_type = {
.name = "ldd",
.match = ldd_match,
.match = ldd_match,
.hotplug = ldd_hotplug,
};
};
其注册的代码如下:
ret = bus_register(&ldd_bus_type);
if (ret)
return ret;
if (ret)
return ret;
删除总线的方法:
void bus_unregister(struct bus_type *bus);
总线方法
在bus_type结构中,定义了许多用于在设备核心与单独的驱动程序之间提供服务的方法,有:
1、int (*match)(struct device *device, struct device_driver *driver);
当一个总线上的新设备或者新驱动程序被添加时,会一次或多次调用这个函数。该方法用于判断指定的驱动程序能否处理指定的设备。如果能,返回非0值。
eg:
static int ldd_match(struct device *dev, struct device_driver *driver)
{
return !strncmp(dev->bus_id, driver->name, strlen(driver->name));
}
{
return !strncmp(dev->bus_id, driver->name, strlen(driver->name));
}
调用硬件时,match函数通常对设备提供的硬件ID和驱动所支持的ID做某种类型的比较。
2、int (*hotplug) (struct device *device, char **envp, int num_envp, char
*buffer, int buffer_size);
在为用户空间产生热插拔事件前,这个方法允许总线添加环境变量。
eg:在环境变量中添加当前版本号
static int ldd_hotplug(struct device *dev, char **envp, int num_envp,
char *buffer, int buffer_size)
{
envp[0] = buffer;
if (snprintf(buffer, buffer_size, "LDDBUS_VERSION=%s",Version) >= buffer_size)
return -ENOMEM;
char *buffer, int buffer_size)
{
envp[0] = buffer;
if (snprintf(buffer, buffer_size, "LDDBUS_VERSION=%s",Version) >= buffer_size)
return -ENOMEM;
envp[1] = NULL;
return 0;
}
return 0;
}
总线属性
总线属性bus_attribute类型定义在<linux/device.h>中,其代码如下:
struct bus_attribute {
struct attributeattr;
ssize_t (*show)(struct bus_type *, char * buf);
ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
};
struct attributeattr;
ssize_t (*show)(struct bus_type *, char * buf);
ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
};
创建和初始化总线属性的宏:
BUS_ATTR(name,mode,show,store);
创建属于总线的任何属性,都需要调用bus_create_file函数:
int bus_create_file(struct bus_type *bus, struct bus_attribute *attr);
删除属性的函数为:
void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr);
eg:
static ssize_t show_bus_version(struct bus_type *bus, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", Version);
}
{
return snprintf(buf, PAGE_SIZE, "%s\n", Version);
}
static BUS_ATTR(version, S_IRUGO, show_bus_version, NULL);
if (bus_create_file(&ldd_bus_type, &bus_attr_version))
printk(KERN_NOTICE "Unable to create version attribute\n");
下面给出一个关于总线驱动的基本程序:
if (bus_create_file(&ldd_bus_type, &bus_attr_version))
printk(KERN_NOTICE "Unable to create version attribute\n");
下面给出一个关于总线驱动的基本程序:
#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
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->bus_id, driver->name, strlen(driver->name));
}
struct bus_type my_bus_type = {
.name = "my_bus",
.match = my_match,
};
static ssize_t show_bus_version(struct bus_type *bus, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", 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");
return ret;
}
static void my_bus_exit(void)
{
bus_unregister(&my_bus_type);
}
module_init(my_bus_init);
module_exit(my_bus_exit);
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
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->bus_id, driver->name, strlen(driver->name));
}
struct bus_type my_bus_type = {
.name = "my_bus",
.match = my_match,
};
static ssize_t show_bus_version(struct bus_type *bus, char *buf)
{
return snprintf(buf, PAGE_SIZE, "%s\n", 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");
return ret;
}
static void my_bus_exit(void)
{
bus_unregister(&my_bus_type);
}
module_init(my_bus_init);
module_exit(my_bus_exit);