sysfs屬性組建立例程
阿新 • • 發佈:2018-12-27
#include <linux/init.h> #include <linux/module.h> #include <linux/kobject.h> #include <linux/sysfs.h> #include <linux/string.h> static ssize_t attr_a_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { int rec = 0; sscanf(buf, "%x", &rec); ... return count; } static ssize_t attr_b_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { int rec = 0; sscanf(buf, "%x", &rec); ... return count; } static struct kobject *hello_kobj; struct mcp23017_control_attribute { struct attribute attr; ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr, char *buf); ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t n); }; static struct mcp23017_control_attribute hello_attribute[] = { __ATTR(attr_a, 0777, NULL, attr_a_store), __ATTR(attr_b, 0777, NULL, attr_b_store), }; static int __init hello_init(void) { mcp23017_kobj = kobject_create_and_add("hello", NULL); for(i=0; i < 2; i++) { sysfs_create_file(hello_kobj, &hello_attribute[i].attr); } } static void __exit hello_exit(void) { } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL");