-
Notifications
You must be signed in to change notification settings - Fork 15
/
vinput.c
396 lines (311 loc) · 8.39 KB
/
vinput.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#define DEBUG
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include "vinput.h"
#define DRIVER_NAME "vinput"
#define dev_to_vinput(dev) container_of(dev, struct vinput, dev)
static DECLARE_BITMAP(vinput_ids, VINPUT_MINORS);
static LIST_HEAD(vinput_devices);
static LIST_HEAD(vinput_vdevices);
static dev_t vinput_dev;
static struct spinlock vinput_lock;
static struct class vinput_class;
struct vinput_device *vinput_get_device_by_type(const char *type)
{
int found = 0;
struct vinput_device *vinput;
struct list_head *curr;
spin_lock(&vinput_lock);
list_for_each(curr, &vinput_devices) {
vinput = list_entry(curr, struct vinput_device, list);
if (vinput && strncmp(type, vinput->name, strlen(vinput->name)) == 0) {
found = 1;
break;
}
}
spin_unlock(&vinput_lock);
if (found)
return vinput;
return ERR_PTR(-ENODEV);
}
struct vinput *vinput_get_vdevice_by_id(long id)
{
struct vinput *vinput = NULL;
struct list_head *curr;
spin_lock(&vinput_lock);
list_for_each(curr, &vinput_vdevices) {
vinput = list_entry(curr, struct vinput, list);
if (vinput && vinput->id == id)
break;
}
spin_unlock(&vinput_lock);
if (vinput && vinput->id == id)
return vinput;
return ERR_PTR(-ENODEV);
}
static int vinput_open(struct inode *inode, struct file *file)
{
int err = 0;
struct vinput *vinput = NULL;
vinput = vinput_get_vdevice_by_id(iminor(inode));
if (IS_ERR(vinput))
err = PTR_ERR(vinput);
else
file->private_data = vinput;
return err;
}
static int vinput_release(struct inode *inode, struct file *file)
{
return 0;
}
static ssize_t vinput_read(struct file *file, char __user *buffer,
size_t count, loff_t *offset)
{
int len;
char buff[VINPUT_MAX_LEN + 1];
struct vinput *vinput = file->private_data;
len = vinput->type->ops->read(vinput, buff, count);
if (*offset > len)
count = 0;
else if (count + *offset > VINPUT_MAX_LEN)
count = len - *offset;
if (copy_to_user(buffer, buff + *offset, count))
count = -EFAULT;
*offset += count;
return count;
}
static ssize_t vinput_write(struct file *file, const char __user *buffer,
size_t count, loff_t *offset)
{
char buff[VINPUT_MAX_LEN + 1];
struct vinput *vinput = file->private_data;
memset(buff, 0, sizeof(char) * (VINPUT_MAX_LEN + 1));
if (count > VINPUT_MAX_LEN) {
dev_warn(&vinput->dev, "Too long. %d bytes allowed\n", VINPUT_MAX_LEN);
return -EINVAL;
}
if (copy_from_user(buff, buffer, count))
return -EFAULT;
return vinput->type->ops->send(vinput, buff, count);
}
static const struct file_operations vinput_fops = {
.owner = THIS_MODULE,
.open = vinput_open,
.release = vinput_release,
.read = vinput_read,
.write = vinput_write,
};
static void vinput_unregister_vdevice(struct vinput *vinput)
{
input_unregister_device(vinput->input);
if (vinput->type->ops->kill)
vinput->type->ops->kill(vinput);
}
static void vinput_destroy_vdevice(struct vinput *vinput)
{
/* Remove from the list first */
spin_lock(&vinput_lock);
list_del(&vinput->list);
clear_bit(vinput->id, vinput_ids);
spin_unlock(&vinput_lock);
module_put(THIS_MODULE);
kfree(vinput);
}
static void vinput_release_dev(struct device *dev)
{
struct vinput *vinput = dev_to_vinput(dev);
int id = vinput->id;
vinput_destroy_vdevice(vinput);
pr_debug("released vinput%d.\n", id);
}
static struct vinput *vinput_alloc_vdevice(void)
{
int err;
struct vinput *vinput = kzalloc(sizeof(struct vinput), GFP_KERNEL);
try_module_get(THIS_MODULE);
memset(vinput, 0, sizeof(struct vinput));
spin_lock_init(&vinput->lock);
spin_lock(&vinput_lock);
vinput->id = find_first_zero_bit(vinput_ids, VINPUT_MINORS);
if (vinput->id >= VINPUT_MINORS) {
err = -ENOBUFS;
goto fail_id;
}
set_bit(vinput->id, vinput_ids);
list_add(&vinput->list, &vinput_vdevices);
spin_unlock(&vinput_lock);
/* allocate the input device */
vinput->input = input_allocate_device();
if (vinput->input == NULL) {
pr_err("vinput: Cannot allocate vinput input device\n");
err = -ENOMEM;
goto fail_input_dev;
}
/* initialize device */
vinput->dev.class = &vinput_class;
vinput->dev.release = vinput_release_dev;
vinput->dev.devt = MKDEV(vinput_dev, vinput->id);
dev_set_name(&vinput->dev, DRIVER_NAME "%lu", vinput->id);
return vinput;
fail_input_dev:
spin_lock(&vinput_lock);
list_del(&vinput->list);
fail_id:
spin_unlock(&vinput_lock);
module_put(THIS_MODULE);
kfree(vinput);
return ERR_PTR(err);
}
static int vinput_register_vdevice(struct vinput *vinput)
{
int err = 0;
/* register the input device */
vinput->input->name = vinput->type->name;
vinput->input->phys = "vinput";
vinput->input->dev.parent = &vinput->dev;
vinput->input->id.bustype = BUS_VIRTUAL;
vinput->input->id.product = 0x0000;
vinput->input->id.vendor = 0x0000;
vinput->input->id.version = 0x0000;
err = vinput->type->ops->init(vinput);
if (err == 0)
dev_info(&vinput->dev, "Registered virtual input %s %ld\n",
vinput->type->name, vinput->id);
return err;
}
static ssize_t export_store(struct class *class, struct class_attribute *attr,
const char *buf, size_t len)
{
int err;
struct vinput *vinput;
struct vinput_device *device;
device = vinput_get_device_by_type(buf);
if (IS_ERR(device)) {
pr_info("vinput: This virtual device isn't registered\n");
err = PTR_ERR(device);
goto fail;
}
vinput = vinput_alloc_vdevice();
if (IS_ERR(vinput)) {
err = PTR_ERR(vinput);
goto fail;
}
vinput->type = device;
err = device_register(&vinput->dev);
if (err < 0)
goto fail_register;
err = vinput_register_vdevice(vinput);
if (err < 0)
goto fail_register_vinput;
return len;
fail_register_vinput:
device_unregister(&vinput->dev);
fail_register:
vinput_destroy_vdevice(vinput);
fail:
return err;
}
static ssize_t unexport_store(struct class *class, struct class_attribute *attr,
const char *buf, size_t len)
{
int err;
unsigned long id;
struct vinput *vinput;
err = kstrtol(buf, 10, &id);
if (err) {
err = -EINVAL;
goto failed;
}
vinput = vinput_get_vdevice_by_id(id);
if (IS_ERR(vinput)) {
pr_err("vinput: No such vinput device %ld\n", id);
err = PTR_ERR(vinput);
goto failed;
}
vinput_unregister_vdevice(vinput);
device_unregister(&vinput->dev);
return len;
failed:
return err;
}
static CLASS_ATTR_WO(export);
static CLASS_ATTR_WO(unexport);
static struct attribute *vinput_class_attrs[] = {
&class_attr_export.attr,
&class_attr_unexport.attr,
NULL,
};
ATTRIBUTE_GROUPS(vinput_class);
static struct class vinput_class = {
.name = "vinput",
.owner = THIS_MODULE,
.class_groups = vinput_class_groups,
};
int vinput_register(struct vinput_device *dev)
{
spin_lock(&vinput_lock);
list_add(&dev->list, &vinput_devices);
spin_unlock(&vinput_lock);
pr_info("vinput: registered new virtual input device '%s'\n",
dev->name);
return 0;
}
EXPORT_SYMBOL(vinput_register);
void vinput_unregister(struct vinput_device *dev)
{
struct list_head *curr, *next;
/* Remove from the list first */
spin_lock(&vinput_lock);
list_del(&dev->list);
spin_unlock(&vinput_lock);
/* unregister all devices of thhis type */
list_for_each_safe(curr, next, &vinput_vdevices) {
struct vinput *vinput = list_entry(curr, struct vinput, list);
if (vinput && vinput->type == dev) {
vinput_unregister_vdevice(vinput);
device_unregister(&vinput->dev);
}
}
pr_info("vinput: unregistered virtual input device '%s'\n",
dev->name);
}
EXPORT_SYMBOL(vinput_unregister);
static int __init vinput_init(void)
{
int err = 0;
pr_info("vinput: Loading virtual input driver\n");
vinput_dev = register_chrdev(0, DRIVER_NAME, &vinput_fops);
if (vinput_dev < 0) {
pr_err("vinput: Unable to allocate char dev region\n");
goto failed_alloc;
}
spin_lock_init(&vinput_lock);
err = class_register(&vinput_class);
if (err < 0) {
pr_err("vinput: Unable to register vinput class\n");
goto failed_class;
}
return 0;
failed_class:
class_unregister(&vinput_class);
failed_alloc:
return err;
}
static void __exit vinput_end(void)
{
pr_info("vinput: Unloading virtual input driver\n");
unregister_chrdev(vinput_dev, DRIVER_NAME);
class_unregister(&vinput_class);
}
module_init(vinput_init);
module_exit(vinput_end);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Tristan Lelong <[email protected]>");
MODULE_DESCRIPTION("emulate input events thru /dev/[vkbd | vts | vmouse]");