-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsysfs_dynamic.c
131 lines (107 loc) · 3.32 KB
/
sysfs_dynamic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Kernel module to create sysfs for dynamic data
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/sysfs.h>
#define LENGTH 3
const static char* module_name = "test";
struct kobj_ext {
struct kobj_attribute kattr;
u32 payload;
};
struct ms_module {
struct kobject *kobj;
struct attribute_group agrp;
struct kobj_ext *objs[LENGTH];
struct attribute *kattrs[LENGTH + 1];
} g_module;
// Function to handle show/store via sysfs
static ssize_t sysfs_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) {
struct kobj_ext *e_kobj = container_of(attr, struct kobj_ext, kattr);
return sprintf(buf, "%d\n", e_kobj->payload);
}
static ssize_t sysfs_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) {
struct kobj_ext *e_kobj = container_of(attr, struct kobj_ext, kattr);
sscanf(buf, "%du", &e_kobj->payload);
pr_info("store = %d\n", e_kobj->payload);
return count;
}
//
// Module entry point
//
static int __init ms_init(void) {
int ret;
int idx;
pr_info("ms: init\n");
/* Setup logic */
for (idx = 0; idx < LENGTH; ++idx) {
g_module.objs[idx] = kzalloc(sizeof(struct kobj_ext), GFP_KERNEL);
if (!g_module.objs[idx]) {
pr_err("Unable to alloc memory\n");
ret = -ENOMEM;
goto err_objs;
}
g_module.objs[idx]->payload = idx;
g_module.objs[idx]->kattr.attr.name = kasprintf(GFP_KERNEL, "idx%d", idx);
g_module.objs[idx]->kattr.attr.mode = S_IWUSR | S_IRUGO;
g_module.objs[idx]->kattr.show = sysfs_show;
g_module.objs[idx]->kattr.store = sysfs_store;
g_module.kattrs[idx] = &g_module.objs[idx]->kattr.attr;
}
g_module.kattrs[LENGTH] = NULL;
/* Create entry in sysfs (/sys/<module_name>) */
g_module.kobj = kobject_create_and_add(module_name, kernel_kobj->parent);
if (IS_ERR(g_module.kobj)) {
pr_err("Unable to create kobject for sysfs\n");
ret = -EIO;
goto err_objs;
}
/* Prepare sysfs group */
g_module.agrp.name = kasprintf(GFP_KERNEL, "%s", "root");
g_module.agrp.attrs = g_module.kattrs;
/* Add attributes in sysfs */
ret = sysfs_create_group(g_module.kobj, &g_module.agrp);
if (ret != 0) {
pr_err("Unable to create sysfs group\n");
goto err_kobject;
}
pr_info("Sysfs interface # /sys/%s/%s\n", module_name, g_module.agrp.name);
return 0;
err_objs:
for (idx = 0; idx < LENGTH - 1; ++idx) {
if (g_module.objs[idx]) {
kfree(g_module.objs[idx]);
}
}
err_kobject:
kobject_put(g_module.kobj);
return ret;
}
//
// Module exit point
//
static void __exit ms_exit(void) {
pr_info("ms: exit\n");
for (idx = 0; idx < LENGTH - 1; ++idx) {
if (g_module.objs[idx]) {
kfree(g_module.objs[idx]);
}
}
sysfs_remove_group(g_module.kobj, &g_module.agrp);
kobject_put(g_module.kobj);
}
//
// Setup module entry and exit points
//
module_init(ms_init);
module_exit(ms_exit);
//
// Setup module info
//
MODULE_VERSION("0.0.1");
MODULE_DESCRIPTION("ms: A module with sysfs");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("aakbar5 <[email protected]>");