linux下的目录 sys,Linux驱动学习 —— 在/sys下面创建目录示例

1 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__

2

3 #include

4 #include

5 #include

6 #include

7

8 static struct kobject *k_obj =NULL;9 static struct kobject *k_obj_2 =NULL;10

11 static char node_one_buf[20] = {0};12

13 static ssize_t sysfs_demo_show_node_one(struct kobject *kobj, struct kobj_attribute *attr, char *buf)14 {15 pr_info("enter, node: %s\n", attr->attr.name);16 return sprintf(buf, "%s: %s\n", attr->attr.name, node_one_buf);17 }18

19 static ssize_t sysfs_demo_store_node_one(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t n)20 {21 pr_info("enter, node: %s\n", attr->attr.name);22

23 sprintf(node_one_buf, "%s", buf);24

25 returnn;26 }27

28 static struct kobj_attribute node_one_attribute =

29 __ATTR(node_one, S_IWUSR|S_IRUGO, sysfs_demo_show_node_one, sysfs_demo_store_node_one);30

31 static ssize_t sysfs_demo_show_node_two(struct kobject *kobj, struct kobj_attribute *attr, char *buf)32 {33

34 return sprintf(buf, "%s\n", attr->attr.name);35 }36

37 static struct kobj_attribute node_two_attribute =

38 __ATTR(node_two, S_IWUSR|S_IRUGO, sysfs_demo_show_node_two, NULL);39

40 static struct attribute *sysfs_demo_attributes[] ={41 &node_one_attribute.attr,42 &node_two_attribute.attr,43 NULL44 };45

46 static const struct attribute_group sysfs_demo_attr_group ={47 .attrs =sysfs_demo_attributes,48 };49

50 static char node_three_buf[20] = {0};51

52 static ssize_t sysfs_demo_show_node_three(struct kobject *kobj, struct kobj_attribute *attr, char *buf)53 {54 pr_info("enter, node: %s\n", attr->attr.name);55 return sprintf(buf, "%s: %s\n", attr->attr.name, node_three_buf);56 }57

58 static ssize_t sysfs_demo_store_node_three(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t n)59 {60 pr_info("enter, node: %s\n", attr->attr.name);61

62 sprintf(node_three_buf, "%s", buf);63

64 returnn;65 }66

67 static struct kobj_attribute node_three_attribute =

68 __ATTR(node_three, S_IWUSR|S_IRUGO, sysfs_demo_show_node_three, sysfs_demo_store_node_three);69

70 static ssize_t sysfs_demo_show_node_four(struct kobject *kobj, struct kobj_attribute *attr, char *buf)71 {72

73 return sprintf(buf, "%s\n", attr->attr.name);74 }75

76 static struct kobj_attribute node_four_attribute =

77 __ATTR(node_four, S_IWUSR|S_IRUGO, sysfs_demo_show_node_four, NULL);78

79 static struct attribute *sysfs_demo2_attributes[] ={80 &node_three_attribute.attr,81 &node_four_attribute.attr,82 NULL83 };84

85 static const struct attribute_group sysfs_demo2_attr_group ={86 .attrs =sysfs_demo2_attributes,87 };88

89 static int __init sysfs_demo_init(void)90 {91 if ((k_obj = kobject_create_and_add("sysfs_demo", NULL)) ==NULL ) {92 pr_err("sysfs_demo sys node create error \n");93 goto out;94 }95

96 if(sysfs_create_group(k_obj, &sysfs_demo_attr_group) ) {97 pr_err("sysfs_create_group failed\n");98 gotoout2;99 }100

101 if ((k_obj_2 = kobject_create_and_add("sysfs_demo_2", k_obj)) ==NULL ) {102 pr_err("hwinfo sys node create error \n");103 gotoout3;104 }105

106 if(sysfs_create_group(k_obj_2, &sysfs_demo2_attr_group) ) {107 pr_err("sysfs_create_group failed\n");108 gotoout4;109 }110

111 pr_info("enter.\n");112 return 0;113 out4:114 kobject_put(k_obj_2);115 out3:116 sysfs_remove_group(k_obj, &sysfs_demo_attr_group);117 out2:118 kobject_put(k_obj);119 out:120 return -1;121 }122 module_init(sysfs_demo_init);123

124 static void __exit sysfs_demo_exit(void)125 {126 pr_info("enter.\n");127

128 if(k_obj) {129 sysfs_remove_group(k_obj, &sysfs_demo_attr_group);130 if(k_obj_2) {131 sysfs_remove_group(k_obj_2, &sysfs_demo2_attr_group);132 kobject_put(k_obj_2);133 }134 kobject_put(k_obj);135 }136

137 }138 module_exit(sysfs_demo_exit);139

140 MODULE_LICENSE("GPL");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值