设备描述结构体
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); }
设备注册
1)int device_register(struct device *dev)
注册设备
2)void device_unregister(struct device *dev)
注销设备
**一条总线也是个设备,也必须按设备注册**
设备属性
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)
删除属性
bus模块
#include <linux/device.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/string.h> MODULE_AUTHOR("cicue"); 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);
device模块
#include <linux/device.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/string.h> MODULE_AUTHOR("cicue"); 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);
驱动描述结构体
struct device_driver { const char *name; /*驱动程序的名字( 体现在 sysfs 中 )*/ struct bus_type *bus; /*驱动程序所在的总线*/ struct module *owner; const char *mod_name; 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); struct attribute_group **groups; struct dev_pm_ops *pm; struct driver_private *p; }
1)int driver_register(struct device_driver *drv)
注册驱动
2)void driver_unregister(struct device_driver *drv)
注销驱动
驱动属性
struct driver_attribute { struct attribute attr; ssize_t (*show)(struct device_driver *drv, char *buf); ssize_t (*store)(struct device_driver *drv, const char *buf, size_t count); } 1)int driver_create_file(struct device_driver * drv, struct driver_attribute * attr) 创建属性 2)void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr) 删除属性
驱动测试函数源码
#include <linux/device.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/string.h> MODULE_AUTHOR("David Xie"); MODULE_LICENSE("Dual BSD/GPL"); extern struct bus_type my_bus_type; /*当驱动找到对应的设备时会执行该函数*/ static int my_probe(struct device *dev) { printk("Driver found device which my driver can handle!\n"); return 0; } static int my_remove(struct device *dev) { printk("Driver found device unpluged!\n"); return 0; } struct device_driver my_driver = { .name = "my_dev", .bus = &my_bus_type, .probe = my_probe, .remove = my_remove, }; static ssize_t mydriver_show(struct device_driver *driver, char *buf) { return sprintf(buf, "%s\n", "This is my driver!"); } static DRIVER_ATTR(drv, S_IRUGO, mydriver_show, NULL); static int __init my_driver_init(void) { int ret = 0; /*注册驱动*/ driver_register(&my_driver); /*创建属性文件*/ driver_create_file(&my_driver, &driver_attr_drv); return ret; } static void my_driver_exit(void) { driver_unregister(&my_driver); } module_init(my_driver_init); module_exit(my_driver_exit);