总线:驱动和设备挂在总线上,总线也是联系驱动和设备的纽带。
struct bus_type {
const char *name; //总线的名称
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, struct kobj_uevent_env *env); //保存环境变量
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 struct dev_pm_ops *pm;
struct bus_type_private *p;
};
设备:
struct device {
struct device *parent; //驱动设备的上一目录
struct device_private *p;
struct kobject kobj;
const char *init_name; /* initial name of the device */
struct device_type *type;
struct semaphore sem; /* semaphore to synchronize calls to
* its driver.
*/
struct bus_type *bus; /* type of bus device is on */
struct device_driver *driver; /* which driver has allocated this
device */
void *platform_data; /* Platform specific data, device
core doesn't touch it */
struct dev_pm_info power;
#ifdef CONFIG_NUMA
int numa_node; /* NUMA node this device is close to */
#endif
u64 *dma_mask; /* dma mask (if dma'able device) */
u64 coherent_dma_mask;/* Like dma_mask, but for
alloc_coherent mappings as
not all hardware supports
64 bit addresses for consistent
allocations such descriptors. */
struct device_dma_parameters *dma_parms;
struct list_head dma_pools; /* dma pools (if dma'ble) */
struct dma_coherent_mem *dma_mem; /* internal for coherent mem
override */
/* arch specific additions */
struct dev_archdata archdata;
dev_t devt; /* dev_t, creates the sysfs "dev" */
spinlock_t devres_lock;
struct list_head devres_head;
struct klist_node knode_class;
struct class *class;
const struct attribute_group **groups; /* optional groups */
void (*release)(struct device *dev);
};
驱动:
struct device_driver {
const char *name; //名字
struct bus_type *bus; //挂在的总线
struct module *owner;
const char *mod_name; /* used for built-in modules */
bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
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 struct attribute_group **groups;
const struct dev_pm_ops *pm;
struct driver_private *p;
};
attribute结构体:
struct attribute {
const char *name; //文件的名称
struct module *owner;
mode_t mode; //访问权限
};
总线的属性:
struct bus_attribute {
struct attribute attr;
ssize_t (*show)(struct bus_type *bus, char *buf); //<span style="color: rgb(153, 153, 153); font-family: "Microsoft YaHei"; font-size: 14px;">用户读文件属性时,该函数将属性值存入buf中返回给用户。</span>
ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);//用户写属性文件时,该函数将用户写入的值传入属性值中。
};
设备的属性:
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);
};
驱动的属性:
struct driver_attribute {
struct attribute attr;
ssize_t (*show)(struct device_driver *driver, char *buf);
ssize_t (*store)(struct device_driver *driver, const char *buf,
size_t count);
};
总线、设备和驱动都有一个属性,属性都包含attribute结构体。总线、设备和驱动的属性可以通过 BUS_ATTR、DEVICE_ATTR、DRIVER_ATTR宏生成。 总线也是一种设备,也需要注册。
总线:
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/device.h>
MODULE_LICENSE("Dual BSD/GPL");
static char *Version = "$Revision: 1.0 $";
static int match_my(struct device *dev,struct device_driver *driver) //用来匹配驱动和总线
{
return !strncmp(dev_name(dev),driver->name,strlen(driver->name));
}
struct bus_type buss= { //定义总线
.name = "my_bus",
.match = match_my,
};
static ssize_t show_info(struct bus_type *bus,char *buf) //当用户调用时返回总线的属性值
{
return sprintf(buf,"%s\n", Version);
}
static void bus_release(struct device dev) //释放总线占有的资源
{
printk("this is releasa\n");
}
static BUS_ATTR(version,S_IRUGO,show_info,NULL); //设置总线的属性
struct device dev_bus ={ //定义总线设备
.init_name = "bus_dev",
.release = bus_release,
};
static int bus_init(void)
{
int ret;
ret = bus_register(&buss); //注册总线设备
if(ret)
return ret;
if(bus_create_file(&buss,&bus_attr_version)) //创建总线文件
printk("create fail\n");
ret = device_register(&dev_bus); //注册总线驱动设备
if(ret)
printk("dev_bus is fail\n");
return ret;
}
static void bus_exit(void)
{
bus_unregister(&buss); //清除总线设备
device_unregister(&dev_bus); //清除总线设备驱动
}
EXPORT_SYMBOL(buss); //导出总线函数对内核全部可见
EXPORT_SYMBOL(dev_bus); //同上
module_init(bus_init);//模块加载
module_exit(bus_exit);//模块卸载
设备:
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/types.h>
#include <linux/unistd.h>
#include <linux/fcntl.h>
MODULE_LICENSE("Dual BSD/GPL");
extern struct device dev_bus;
extern struct bus_type buss;
static void release_dev(struct device *dev) //执行清理工作
{
printk("this is dev\n");
}
struct device dev = { //设备
.init_name = "dev",
.bus = &buss,
.parent = &dev_bus,
.release = release_dev,
};
static ssize_t show_dev_info(struct device *dev,char *buf) //用户调用时返回设备信息
{
return sprintf(buf,"%s\n","this is device");
}
static DEVICE_ATTR(dev,S_IRUGO,show_dev_info,NULL); //设置设备属性
static int dev_init(void)
{
int ret = 0;
ret = device_register(&dev);
device_create_file(&dev,&dev_attr_dev);
return ret;
}
static void dev_exit(void)
{
device_unregister(&dev);
}
module_init(dev_init);
module_exit(dev_exit);
驱动:
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/device.h>
MODULE_LICENSE("Dual BSD/GPL");
extern struct bus_type buss;
static int remove_driver (struct device *dev) //移除设备时执行
{
printk("remove device\n");
}
static int probe_driver(struct device *dev) //插入匹配到设备时执行
{
printk("init dev\n");
}
struct device_driver driver = { //驱动
.name = "dev",
.bus = &buss,
.remove = remove_driver,
.probe = probe_driver,
};
static ssize_t show_driver_info(struct device_driver *driver,char *buf) //用户获得驱动信息
{
return sprintf(buf,"%s\n","driver_info");
}
static DRIVER_ATTR(driver,S_IRUGO,show_driver_info,NULL); //设置驱动属性
static int ddriver_init()
{
int ret = 0;
ret = driver_register(&driver);
driver_create_file(&driver,&driver_attr_driver);
return ret;
}
static void driver_exit(void)
{
driver_unregister(&driver);
}
module_init(ddriver_init);
module_exit(driver_exit);
获取设备名称时不能直接通过initname获取,由如下代码可知:
if (dev->init_name)
{
dev_set_name(dev, "%s", dev->init_name);
dev->init_name = NULL; //已经被置为空了
}
可以通过dev_name()获得。