設備描述結構體node
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)
註銷設備
**一條總線也是個設備,也必須按設備註冊**linux
設備屬性函數
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模塊this
#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模塊
spa
#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)code
註冊驅動
2)void driver_unregister(struct device_driver *drv)orm
註銷驅動ci
驅動屬性
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);