#include <linux/module.h> #include <linux/types.h> #include <linux/kobject.h> static ssize_t sysfs_read(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { return sprintf(buf, "%s\n", "sysfs test read,created by vincent"); } static ssize_t sysfs_write(struct kobject *kobj, struct kobj_attribute *attr, const char *buf,ssize_t count) { printk("\nfrom user,length=0x%X,content=%s\n",count,buf); if(count) return count; else return 1 ; } //static struct kobj_attribute my_sysfs_read = __ATTR(read, S_IRUGO, sysfs_read, NULL); //static struct kobj_attribute my_sysfs_write = __ATTR(write, S_IWUGO, NULL,sysfs_write); static struct kobj_attribute my_sysfs_read = __ATTR(aaa, 0664, sysfs_read, NULL); static struct kobj_attribute my_sysfs_write = __ATTR(bbb, 0664, NULL,sysfs_write); static struct attribute *my_sysfs_test[] = { &my_sysfs_read.attr, &my_sysfs_write.attr, NULL, }; static struct attribute_group my_attr_group = { .attrs = my_sysfs_test, }; static int sysfs_status = 0 ; struct kobject *soc_kobj = NULL; int my_sysfs_init(void) { int ret = 0; printk("my_sysfs_init: %s\n",__func__); soc_kobj = kobject_create_and_add("my_sysfs", NULL); if (!soc_kobj) goto err_board_obj; ret = sysfs_create_group(soc_kobj, &my_attr_group); if (ret) goto err_soc_sysfs_create; sysfs_status = 1; /* init func must contain a return vaule,otherwise meet warning when insmod this module */ return 0; sysfs_status = 0; err_soc_sysfs_create: kobject_put(soc_kobj); sysfs_remove_group(soc_kobj, &my_attr_group); printk("\n sysfs_create_group ERROR : %s\n",__func__); return 0; err_board_obj: printk("\n object_create_and_add ERROR : %s\n",__func__); return 0; } void my_sysfs_exit(void) { printk("\n my_sysfs_exit : %s\n",__func__); if(sysfs_status == 1) { sysfs_status = 0; kobject_put(soc_kobj); sysfs_remove_group(soc_kobj, &my_attr_group); } } MODULE_AUTHOR("derek yi"); MODULE_LICENSE("Dual BSD/GPL"); module_init(my_sysfs_init); module_exit(my_sysfs_exit);