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
|
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
struct demo_attr {
struct attribute attr;
int value;
};
static struct demo_attr notify = {
.attr.name="notify",
.attr.mode = 0644,
.value = 0,
};
static struct demo_attr trigger = {
.attr.name="trigger",
.attr.mode = 0644,
.value = 0,
};
static struct attribute *demoattr[] = {
¬ify.attr,
&trigger.attr,
NULL
};
static struct kobject *demo_kobj;
static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
{
struct demo_attr *a = container_of(attr, struct demo_attr, attr);
pr_debug("RT demo kmod: show called (%s)\n", a->attr.name);
return scnprintf(buf, PAGE_SIZE, "%s: %d\n", a->attr.name, a->value);
}
static ssize_t store(struct kobject *kobj, struct attribute *attr,
const char *buf, size_t len)
{
struct demo_attr *a = container_of(attr, struct demo_attr, attr);
if (!strncmp(a->attr.name, "notify", sizeof("notify")))
return len;
sscanf(buf, "%d", &a->value);
notify.value = a->value;
pr_debug("RT demo kmod: sysfs_notify store %s = %d\n", a->attr.name,
a->value);
sysfs_notify(demo_kobj, NULL, "notify");
return len;
}
static struct sysfs_ops demoops = {
.show = show,
.store = store,
};
static struct kobj_type mytype = {
.sysfs_ops = &demoops,
.default_attrs = demoattr,
};
static int __init demo_module_init(void)
{
int err = -1;
pr_debug("RT demo kmod: init\n");
demo_kobj = kzalloc(sizeof(*demo_kobj), GFP_KERNEL);
if (demo_kobj) {
kobject_init(demo_kobj, &mytype);
if (kobject_add(demo_kobj, NULL, "%s", "demo-poll")) {
err = -1;
pr_err("RT demo kmod: kobject_add() failed\n");
kobject_put(demo_kobj);
demo_kobj = NULL;
}
err = 0;
}
return err;
}
static void __exit demo_module_exit(void)
{
if (demo_kobj) {
kobject_put(demo_kobj);
kfree(demo_kobj);
}
pr_debug("RT demo kmod: exit\n");
}
module_init(demo_module_init);
module_exit(demo_module_exit);
MODULE_LICENSE("GPL");
|