-
Notifications
You must be signed in to change notification settings - Fork 1
/
virtual_controller.c
670 lines (585 loc) · 15.5 KB
/
virtual_controller.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// SPDX-License-Identifier: GPL-2.0-only
/*
* Handheld device wrapper userspace helper
*
* Copyright (c) 2024 Chris Morgan <macromorgan@hotmail.com>
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <sys/epoll.h>
#include <sys/ioctl.h>
#define DEVICE_NAME "Virtual Gamepad"
#define DEVICE_VID 0x1234
#define DEVICE_PID 0x5678
#define MAX_EVENTS 64
/* Maximum number of devices of each type we support (arbitrary). */
#define MAX_DEVS 8
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(*array))
#define TEST_BIT(bit, array) (array[bit / 8] & (1 << (bit % 8)))
/*
* The struct that contains the necessary data to manage the virtual
* input device. We currently support a single force feedback device,
* multiple abs devices, and multiple key devices.
*/
struct virtual_device {
struct uinput_setup usetup;
struct uinput_abs_setup uabssetup[ABS_MAX];
int uinput_fd;
int ff_fd;
int abs_fd[MAX_DEVS];
int key_fd[MAX_DEVS];
};
struct dev_info {
const char name[256];
};
/*
* List of all the "devices of interest" that we're looking to
* capture. Only the first 8 key and abs devices and last ff device
* that match the names below will be used by the driver.
*/
static struct dev_info input_devs[] = {
{ .name = "adc-joystick" },
{ .name = "adc-keys" },
{ .name = "adc-trigger" },
{ .name = "gpio-keys" },
{ .name = "gpio-keys-control" },
{ .name = "gpio-keys-vol" },
{ .name = "gpio-vibrator" },
{ .name = "gpio-vibrator-l" },
{ .name = "gpio-vibrator-r" },
{ .name = "pwm-vibrator" },
{ .name = "pwm-vibrator-l" },
{ .name = "pwm-vibrator-r" },
};
/**
* enumerate_abs_devices() - Identify ABS axes and features
* @v_dev: pointer to virtual_device struct
*
* Enumerate ABS axes and add them to the uinput virtual device.
* Return number of devices found on success or negative on error.
*/
int enumerate_abs_devices(struct virtual_device *v_dev)
{
uint8_t abs_b[ABS_MAX/8 + 1];
int dev_count = 0;
int ret = 0;
uint8_t abs_index = 0;
if (v_dev->abs_fd[0] <= 0)
return 0;
for (int dev_num = 0; dev_num < MAX_DEVS; dev_num++) {
if (v_dev->abs_fd[dev_num] > 0)
dev_count += 1;
}
for (int k = 0; k < dev_count; k++) {
ioctl(v_dev->abs_fd[k],
EVIOCGBIT(EV_ABS, sizeof(abs_b)), abs_b);
for (int i = 0; i < ABS_MAX; i++) {
if (TEST_BIT(i, abs_b)) {
ret = ioctl(v_dev->abs_fd[k], EVIOCGABS(i),
&v_dev->uabssetup[i].absinfo);
if (ret)
continue;
ret = ioctl(v_dev->uinput_fd,
UI_SET_ABSBIT, i);
if (ret)
continue;
abs_index |= i;
v_dev->uabssetup[i].code = i;
ret = ioctl(v_dev->uinput_fd, UI_ABS_SETUP,
&v_dev->uabssetup[i]);
if (ret)
printf("Unable to set abs axis %d\n",
i);
}
}
}
v_dev->usetup.id.version |= abs_index;
return dev_count;
}
/**
* enumerate_ff_device() - Identify supported force feedback features
* @v_dev: pointer to virtual_device struct
*
* Enumerate force feedback features and add them to the uinput virtual
* device. Return 0 on success.
*/
int enumerate_ff_device(struct virtual_device *v_dev)
{
uint8_t ff_b[FF_MAX/8 + 1];
int ret = 0;
uint8_t ff_index = 0;
if (v_dev->ff_fd <= 0)
return 0;
ret = ioctl(v_dev->ff_fd,
EVIOCGBIT(EV_FF, sizeof(ff_b)), ff_b);
if (ret < 0) {
printf("Unable to enumerate FF device: %d\n", ret);
return -ENODEV;
}
for (int i = 0; i < FF_MAX; i++) {
if (TEST_BIT(i, ff_b)) {
ioctl(v_dev->uinput_fd, UI_SET_FFBIT, i);
ff_index |= i;
}
}
ret = ioctl(v_dev->ff_fd, EVIOCGEFFECTS, &v_dev->usetup.ff_effects_max);
if (ret < 0) {
printf("Unable to determine max FF effects\n");
return -EIO;
};
v_dev->usetup.id.version ^= ff_index;
return 0;
}
/**
* enumerate_key_devices() - Identify supported keys
* @v_dev: pointer to virtual_device struct
*
* Enumerate keys and add them to the uinput virtual device. Return
* number of keys identified.
*/
int enumerate_key_devices(struct virtual_device *v_dev)
{
uint8_t key_b[KEY_MAX/8 + 1];
int dev_count = 0;
int keys = 0;
uint16_t key_index = 0;
for (int dev_num = 0; dev_num < MAX_DEVS; dev_num++) {
if (v_dev->key_fd[dev_num] > 0)
dev_count += 1;
}
for (int k = 0; k < dev_count; k++) {
ioctl(v_dev->key_fd[k],
EVIOCGBIT(EV_KEY, sizeof(key_b)), key_b);
for (int i = 0; i < KEY_MAX; i++) {
if (TEST_BIT(i, key_b)) {
ioctl(v_dev->uinput_fd,
UI_SET_KEYBIT, i);
key_index |= (i << 6);
keys += 1;
}
}
}
v_dev->usetup.id.version ^= key_index;
return keys;
}
/**
* input_device_match() - Check input device name against table
* @name: pointer to device name to check
*
* Check if the device name is one that we want to monitor. Return
* 1 if there is a match and 0 if there is not.
*/
int input_device_match(char *name)
{
for (int i = 0; i < (int)ARRAY_SIZE(input_devs); i++) {
if (!strcmp(name, input_devs[i].name))
return 1;
}
return 0;
}
/**
* iterate_input_devices() - Identify input devices to be monitored
* @v_dev: pointer to virtual_device struct
*
* Iterate over all of the event input devices to find the ones we
* want to monitor and start adding them to the virtual_device struct.
* FF devices are closed as read-only and reopened as write-only, since
* we need to write to them but not necessarily read them. Return is
* total number of devices found.
*
*/
int iterate_input_devices(struct virtual_device *v_dev)
{
char fd_dev[20];
char name[256];
int fd, ret;
int count = 0;
int key_devs = 0;
int abs_devs = 0;
unsigned long evbit = 0;
for (int i = 0; i < 256; i++) {
sprintf(fd_dev, "/dev/input/event%d", i);
fd = open(fd_dev, O_RDONLY);
if (fd == -1)
continue;
ioctl(fd, EVIOCGNAME(256), name);
ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), &evbit);
close(fd);
ret = input_device_match(name);
if (!ret)
continue;
if (evbit & (1 << EV_FF)) {
v_dev->ff_fd = open(fd_dev, O_WRONLY);
printf("Found EV_FF: %s\n", fd_dev);
count += 1;
}
if (evbit & (1 << EV_ABS)) {
if (abs_devs >= MAX_DEVS)
continue;
v_dev->abs_fd[abs_devs] = open(fd_dev,
O_RDONLY |
O_NONBLOCK);
printf("Found EV_ABS: %s\n", fd_dev);
count += 1;
abs_devs += 1;
}
if (evbit & (1 << EV_KEY)) {
if (key_devs >= MAX_DEVS)
continue;
v_dev->key_fd[key_devs] = open(fd_dev,
O_RDONLY |
O_NONBLOCK);
printf("Found EV_KEY: %s\n", fd_dev);
count += 1;
key_devs += 1;
}
}
return count;
}
/**
* create_uinput_device() - Create a new composite uinput device
* @v_dev: pointer to virtual input device
*
* Create a uinput device for use by userspace to combine all of the
* monitored input devices into a single one that applications are
* more capable of dealing with. Return 0 on success, negative on
* error.
*
*/
int create_uinput_device(struct virtual_device *v_dev)
{
int ret = 0;
v_dev->uinput_fd = open("/dev/uinput", O_RDWR | O_NONBLOCK |
O_DSYNC | O_RSYNC);
if (v_dev->uinput_fd == -1)
return -ENODEV;
if (v_dev->abs_fd[0] > 0) {
ret = ioctl(v_dev->uinput_fd, UI_SET_EVBIT, EV_ABS);
if (ret)
return ret;
ret = enumerate_abs_devices(v_dev);
if (!(ret > 0)) {
printf("No ABS devices found\n");
return -ENODEV;
}
}
if (v_dev->key_fd[0] > 0) {
ret = ioctl(v_dev->uinput_fd, UI_SET_EVBIT, EV_KEY);
if (ret)
return ret;
ret = enumerate_key_devices(v_dev);
if (!(ret > 0)) {
printf("No keys found\n");
return -ENODEV;
}
}
if (v_dev->ff_fd > 0) {
ret = ioctl(v_dev->uinput_fd, UI_SET_EVBIT, EV_FF);
if (ret)
return ret;
ret = enumerate_ff_device(v_dev);
if (ret)
return ret;
}
v_dev->usetup.id.bustype = BUS_HOST;
v_dev->usetup.id.vendor = DEVICE_VID;
v_dev->usetup.id.product = DEVICE_PID;
sprintf(v_dev->usetup.name, DEVICE_NAME);
ret = ioctl(v_dev->uinput_fd, UI_DEV_SETUP, &v_dev->usetup);
if (ret)
return ret;
ret = ioctl(v_dev->uinput_fd, UI_DEV_CREATE);
if (ret)
return ret;
return 0;
}
/**
* handle_uinput_ff_upload() - Capture and respond to ff_upload
* requests
* @v_dev: main virtual device struct
* @ev: input_event initiating ff upload
*
* Handle the necessary IOCTLs used for processing an incoming ff
* effect upload. Read the upload request from the uinput device and
* replay it to the physical ff device. Read the response from the
* physical ff device and replay it back to the uinput device.
* Return value is 0 for success, negative for error.
*/
int handle_uinput_ff_upload(struct virtual_device *v_dev,
struct input_event ev)
{
struct uinput_ff_upload ff_payload;
struct ff_effect effect;
int ret = 0;
ff_payload.request_id = ev.value;
ret = ioctl(v_dev->uinput_fd, UI_BEGIN_FF_UPLOAD, &ff_payload);
if (ret)
return ret;
effect = ff_payload.effect;
effect.id = -1;
ret = ioctl(v_dev->ff_fd, EVIOCSFF, &effect);
if (ret)
return ret;
ff_payload.retval = ret;
ret = ioctl(v_dev->uinput_fd, UI_END_FF_UPLOAD, &ff_payload);
if (ret)
return ret;
return 0;
}
/**
* handle_uinput_ff_erase() - Capture and respond to ff_erase
* requests
* @v_dev: main virtual device struct
* @ev: input_event initiating ff erase
*
* Handle the necessary IOCTLs used for processing an incoming ff
* effect erase. Read the erase request from the uinput device and
* replay it to the physical ff device. Read the response from the
* physical ff device and replay it back to the uinput device.
* Return value is 0 for success, negative for error.
*/
int handle_uinput_ff_erase(struct virtual_device *v_dev,
struct input_event ev)
{
struct uinput_ff_erase ff_payload;
int ret = 0;
ff_payload.request_id = ev.value;
ret = ioctl(v_dev->uinput_fd, UI_BEGIN_FF_ERASE, &ff_payload);
if (ret)
return ret;
ret = ioctl(v_dev->ff_fd, EVIOCRMFF, ff_payload.effect_id);
if (ret)
return ret;
ff_payload.retval = ret;
ret = ioctl(v_dev->uinput_fd, UI_END_FF_ERASE, &ff_payload);
if (ret)
return ret;
return 0;
}
/**
* set_ff_gain() - Set gain on physical ff hardware
* @v_dev: main virtual device struct
* @gain: value to set for gain
*
* Set the gain value of the physical ff hardware based on event
* received by uinput device. Return value is 0 for success,
* negative for error.
*/
int set_ff_gain(struct virtual_device *v_dev, __u16 gain)
{
struct input_event ff_event = {
.type = EV_FF,
.code = FF_GAIN,
.value = gain,
};
int ret = 0;
ret = write(v_dev->ff_fd, (const void *) &ff_event,
sizeof(ff_event));
if (ret != sizeof(ff_event)) {
printf("Could not set device gain\n");
return -EIO;
}
return 0;
}
/**
* set_ff_effect_status() - Set ff effect on physical ff hardware
* @v_dev: main virtual device struct
* @int: id of effect
* @status: status of effect - 1 or 0
*
* Set the effect status of the physical ff hardware based on event
* received by uinput device. Return value is 0 for success,
* negative for error.
*/
int set_ff_effect_status(struct virtual_device *v_dev, int effect,
int status)
{
struct input_event ff_event = {
.type = EV_FF,
.code = effect,
.value = status,
};
int ret = 0;
ret = write(v_dev->ff_fd, (const void *) &ff_event,
sizeof(ff_event));
if (ret != sizeof(ff_event)) {
printf("Could not set effect status\n");
return -EIO;
}
return 0;
}
/**
* handle_ff_events() - Respond to ff_events
*
* @v_dev: main virtual device struct
* @ev: input_event initiating ff upload
*
* Dispatch an ff event to the correct ff handler. Return value is 0
* for success, negative for error. For some reason it was insufficient
* to simply forward the input_event, we had to create a new one.
*/
int handle_ff_events(struct virtual_device *v_dev,
struct input_event ev)
{
int ret = 0;
if (ev.code == FF_GAIN)
return set_ff_gain(v_dev, ev.value);
if (ev.code <= FF_GAIN)
return set_ff_effect_status(v_dev, ev.code, ev.value);
return ret;
}
/**
* parse_ev_incoming() - Process incoming event and hand off to correct
* helper function.
*
* @v_dev: main virtual device struct
* @fd_in: file descriptor responsible for event
*
* Process an EPOLLIN request and hand off necessary data to correct
* function. Return value is 0 for success, negative for error.
*/
void parse_ev_incoming(struct virtual_device *v_dev, int fd_in)
{
struct input_event ev;
int len, ret;
len = read(fd_in, &ev, sizeof(ev));
if (len != -1) {
switch (ev.type) {
case EV_SYN:
case EV_ABS:
case EV_KEY:
if (v_dev->uinput_fd != fd_in) {
ret = write(v_dev->uinput_fd,
&ev, sizeof(ev));
if (ret < 0)
printf("Event dropped\n");
}
break;
case EV_UINPUT:
if (ev.code == UI_FF_UPLOAD) {
handle_uinput_ff_upload(v_dev, ev);
break;
} else if (ev.code == UI_FF_ERASE) {
handle_uinput_ff_erase(v_dev, ev);
break;
}
printf("UINPUT ev %d not handled\n", ev.code);
break;
case EV_FF:
if (v_dev->uinput_fd == fd_in)
handle_ff_events(v_dev, ev);
break;
default:
/* Catch for events we don't support yet */
printf("EV type %d EV code %d not handled\n",
ev.type, ev.code);
}
} else {
printf("read failed descriptor %d, errno %d\n",
fd_in, errno);
}
}
/**
* define_epoll_fds() - Add all required file descriptors to epoll to
* be monitored.
*
* @v_dev: main virtual device struct
* @ep_fd: epoll file descriptor
*
* Iterate through all file descriptors to monitor and add those which
* need to be monitored by epoll. At a minimum we need to monitor the
* uinput device for force feedback effects and at least 1 of either
* an ABS device or 1 or more key devices.
*/
int define_epoll_fds(struct virtual_device *v_dev, int ep_fd)
{
struct epoll_event event;
int ret = 0;
event.events = EPOLLIN;
event.data.fd = v_dev->uinput_fd;
ret = epoll_ctl(ep_fd, EPOLL_CTL_ADD, v_dev->uinput_fd,
&event);
if (ret == -1) {
printf("Cannot monitor uinput device\n");
return -1;
}
for (int i = 0; i < MAX_DEVS; i++) {
if (!(v_dev->abs_fd[i] > 0))
continue;
event.events = EPOLLIN;
event.data.fd = v_dev->abs_fd[i];
ret = epoll_ctl(ep_fd, EPOLL_CTL_ADD, v_dev->abs_fd[i],
&event);
if (ret == -1) {
printf("Cannot monitor abs device %d\n", i);
return -1;
}
}
for (int i = 0; i < MAX_DEVS; i++) {
if (!(v_dev->key_fd[i] > 0))
continue;
event.events = EPOLLIN;
event.data.fd = v_dev->key_fd[i];
ret = epoll_ctl(ep_fd, EPOLL_CTL_ADD, v_dev->key_fd[i],
&event);
if (ret == -1) {
printf("Cannot monitor key device %d\n", i);
return -1;
}
}
return 0;
}
int main(void)
{
struct epoll_event event_queue[MAX_EVENTS];
struct virtual_device *v_dev;
int ep_fd;
int ret = 0;
v_dev = malloc(sizeof(struct virtual_device));
if (v_dev == NULL) {
printf("Unable to allocate memory for virtual dev.\n");
return -ENOMEM;
};
memset(v_dev, 0, sizeof(struct virtual_device));
ret = iterate_input_devices(v_dev);
if (ret == 0) {
printf("No input devices found to capture\n");
return -ENODEV;
}
ret = create_uinput_device(v_dev);
if (ret) {
printf("Unable to create uinput device: %d\n", ret);
return -ENODEV;
}
ep_fd = epoll_create1(0);
if (ep_fd == -1) {
printf("Unable to start epoll\n");
return -1;
}
ret = define_epoll_fds(v_dev, ep_fd);
if (ret) {
printf("Cannot monitor input devices: %d\n", ret);
return ret;
}
while (1) {
int n, i;
n = epoll_wait(ep_fd, event_queue, (MAX_DEVS * 3), -1);
for (i = 0; i < n; i++) {
if (event_queue[i].events & EPOLLIN)
parse_ev_incoming(v_dev,
event_queue[i].data.fd);
else {
printf("epoll error, type %u\n",
event_queue[i].events);
close(event_queue[i].data.fd);
continue;
}
}
}
}