5、 总线注册/删除 1)总线的注册使用:bus_register(struct bus_type * bus) 若成功,新的总线将被添加进系统,并可在sysfs 的 /sys/bus 下看到。 2)总线的删除使用:void bus_unregister(struct bus_type *bus)6、 总线方法 1)int (*match)(struct device * dev, struct device_driver * drv) 当一个新设备或者驱动被添加到这个总线时,该方法被调用。用于判断指定的驱动程序是否能处理指定的设备。若可以,则返回非零值。 2)int (*uevent)(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size) 在为用户空间产生热插拔事件之前,这个方法允许总线添加环境变量。7、 总线属性 总线属性由结构bus_attribute 描述,定义如下: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);}
1)int bus_create_file(struct bus_type *bus, struct bus_attribute *attr) 创建属性 2)void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr) 删除属性
8、实例分析
Bus_basic.c源码
#include <linux/device.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/string.h>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));}/*声明总线*/struct bus_type my_bus_type = { .name = "my_bus", //总线名字 .match = my_match, //总线match函数指针};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"); return ret;}/*模块卸载函数*/static void my_bus_exit(void){ bus_unregister(&my_bus_type);}module_init(my_bus_init);
module_exit(my_bus_exit);
9、 试验结果
无做什么,记得为自己而做,那就毫无怨言。