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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/*
* TIMA Uevent Support
*
*/
#include <asm/uaccess.h>
#include <linux/list.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/syscalls.h>
#include "tima_uevent.h"
// Forward declaration
struct inode;
// Global declaration
static DEFINE_MUTEX(tima_uevent_mutex);
static struct class *tima_uevent_class;
struct device *tima_uevent_dev;
EXPORT_SYMBOL_GPL(tima_uevent_dev);
static LIST_HEAD(tima_uevent_list);
static DEFINE_SPINLOCK(tima_uevent_list_lock);
static ssize_t show_tima_uevent(struct device *dev,
struct device_attribute *attr, char *buf)
{
if (!dev)
return -ENODEV;
return sprintf(buf, "%s\n", "TIMA uevent data");
}
static DEVICE_ATTR(name, S_IRUGO, show_tima_uevent, NULL);
static int
tima_uevent_release(struct inode *inode, struct file *file)
{
printk(KERN_WARNING
"%s TIMA uevent driver is released.",
__func__);
module_put(THIS_MODULE);
return 0;
}
static bool
tima_uevent_validate(void)
{
printk(KERN_ERR
"Validate caller (%s)",
current->comm);
return true;
}
static ssize_t
tima_uevent_read(struct file *filp, char __user *buff,
size_t len, loff_t *offset)
{
char tima_uevent[80] = "TIMA uevent read";
char *data;
int size = strlen(tima_uevent);
int retval = 0;
if (!tima_uevent_validate()) {
printk(KERN_ERR
"%s invalid request.\n",
__func__);
return -EPERM;
}
data = kzalloc(sizeof(tima_uevent), GFP_KERNEL);
if (!data) {
printk(KERN_ERR
"%s out of memory.\n",
__func__);
return -ENOMEM;
}
spin_lock(&tima_uevent_list_lock);
snprintf(data, size, "%c%s", size - 2, tima_uevent);
if (copy_to_user(buff, data, size)) {
retval = -EFAULT;
goto out;
}
out:
spin_unlock(&tima_uevent_list_lock);
kfree(data);
if (retval) {
return retval;
} else {
return size;
}
}
static ssize_t
tima_uevent_write(struct file *filp, const char __user *buff,
size_t len, loff_t *off)
{
int retval = 0;
char *req;
if (!tima_uevent_validate()) {
printk(KERN_ERR
"%s invalid request.\n",
__func__);
return -EPERM;
}
req = kzalloc(256, GFP_KERNEL);
if (!req) {
printk(KERN_ERR
"%s out of memory.\n",
__func__);
return -ENOMEM;
}
/* Copy from user with truncation. */
if (copy_from_user(req, buff, min((size_t)256, len))) {
printk(KERN_ERR
"%s copy_from_user failed.\n",
__func__);
retval = -EFAULT;
goto out;
}
/* Terminate the request string if it has been truncated. */
if (len > 255)
req[255] = '\0';
printk(KERN_DEBUG "%s TIMA uevent request (%s).\n", __func__, req);
out:
kfree(req);
return retval;
}
static struct file_operations tima_uevent_fops = {
.owner = THIS_MODULE,
.read = tima_uevent_read,
.write = tima_uevent_write,
.release = tima_uevent_release
};
struct miscdevice tima_uevent_mdev = {
.minor = MISC_DYNAMIC_MINOR,
.name = TIMA_UEVENT_DEV,
.fops = &tima_uevent_fops,
};
static int __init tima_uevent_init(void)
{
int retval = 0;
retval = misc_register(&tima_uevent_mdev);
if (retval)
goto out;
printk(KERN_ALERT "%s\n", __func__);
tima_uevent_class = class_create(THIS_MODULE, "tima");
if (IS_ERR(tima_uevent_class)) {
retval = PTR_ERR(tima_uevent_class);
goto error;
}
tima_uevent_dev = device_create(tima_uevent_class,
NULL /* parent */, 0 /* dev_t */, NULL /* drvdata */,
TIMA_UEVENT_DEV);
if (IS_ERR(tima_uevent_dev)) {
retval = PTR_ERR(tima_uevent_dev);
goto error_destroy;
}
retval = device_create_file(tima_uevent_dev, &dev_attr_name);
if (retval)
goto error_destroy;
printk(KERN_DEBUG "TIMA uevent driver (%s) is initialized.\n", TIMA_UEVENT_DEV);
return 0;
error_destroy:
kfree(tima_uevent_dev);
device_destroy(tima_uevent_class, 0);
error:
misc_deregister(&tima_uevent_mdev);
out:
printk(KERN_ERR "%s: TIMA uevent driver initialization failed\n", __FILE__);
return retval;
}
static void __exit tima_uevent_exit (void)
{
class_destroy(tima_uevent_class);
printk(KERN_ALERT "%s\n", __func__);
}
MODULE_AUTHOR("Ken Chen, geng.c@sta.samsung.com");
MODULE_DESCRIPTION("TIMA uevent driver");
MODULE_VERSION("0.1");
module_init(tima_uevent_init);
module_exit(tima_uevent_exit);
/* char *envp[2]; */
/* env[0] = data */
/* env[1] = NULL */
/* kobject_uevent_env(&tima_uevent_dev->kobj, KOBJ_CHANGE, envp); */
|