1 /*
2 * Copyright 2023 Google LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT zephyr_native_linux_evdev
8
9 #include <cmdline.h>
10 #include <nsi_host_trampolines.h>
11 #include <posix_native_task.h>
12 #include <zephyr/device.h>
13 #include <zephyr/input/input.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/logging/log.h>
16 #include <zephyr/sys/util.h>
17
18 #include "linux_evdev_bottom.h"
19
20 LOG_MODULE_REGISTER(linux_evdev, CONFIG_INPUT_LOG_LEVEL);
21
22 static int linux_evdev_fd = -1;
23 static const char *linux_evdev_path;
24 static struct k_thread linux_evdev_thread;
25 static K_KERNEL_STACK_DEFINE(linux_evdev_thread_stack,
26 CONFIG_ARCH_POSIX_RECOMMENDED_STACK_SIZE);
27
linux_evdev_options(void)28 static void linux_evdev_options(void)
29 {
30 static struct args_struct_t linux_evdev_options[] = {
31 {
32 .is_mandatory = true,
33 .option = "evdev",
34 .name = "path",
35 .type = 's',
36 .dest = (void *)&linux_evdev_path,
37 .descript = "Path of the evdev device to use",
38 },
39 ARG_TABLE_ENDMARKER,
40 };
41
42 native_add_command_line_opts(linux_evdev_options);
43 }
44
linux_evdev_check_arg(void)45 static void linux_evdev_check_arg(void)
46 {
47 if (linux_evdev_path == NULL) {
48 posix_print_error_and_exit(
49 "Error: evdev device missing.\n"
50 "Please specify an evdev device with the --evdev "
51 "argument when using CONFIG_NATIVE_LINUX_EVDEV=y\n");
52 }
53 }
54
linux_evdev_cleanup(void)55 static void linux_evdev_cleanup(void)
56 {
57 if (linux_evdev_fd >= 0) {
58 nsi_host_close(linux_evdev_fd);
59 }
60 }
61
62 NATIVE_TASK(linux_evdev_options, PRE_BOOT_1, 10);
63 NATIVE_TASK(linux_evdev_check_arg, PRE_BOOT_2, 10);
64 NATIVE_TASK(linux_evdev_cleanup, ON_EXIT, 10);
65
linux_evdev_thread_fn(void * p1,void * p2,void * p3)66 static void linux_evdev_thread_fn(void *p1, void *p2, void *p3)
67 {
68 const struct device *dev = p1;
69 uint16_t type;
70 uint16_t code;
71 int32_t value;
72 int ret;
73
74 while (true) {
75 ret = linux_evdev_read(linux_evdev_fd, &type, &code, &value);
76 if (ret == NATIVE_LINUX_EVDEV_NO_DATA) {
77 /* Let other threads run. */
78 k_sleep(K_MSEC(CONFIG_NATIVE_LINUX_THREAD_SLEEP_MS));
79 continue;
80 } else if (ret < 0) {
81 return;
82 }
83
84 LOG_DBG("evdev event: type=%d code=%d val=%d", type, code, value);
85
86 if (type == 0) { /* EV_SYN */
87 input_report(dev, 0, 0, 0, true, K_FOREVER);
88 } else if (type == INPUT_EV_KEY && value == 2) {
89 /* nothing, ignore key repeats */
90 } else {
91 input_report(dev, type, code, value, false, K_FOREVER);
92 }
93 }
94 }
95
linux_evdev_init(const struct device * dev)96 static int linux_evdev_init(const struct device *dev)
97 {
98 linux_evdev_fd = linux_evdev_open(linux_evdev_path);
99
100 k_thread_create(&linux_evdev_thread, linux_evdev_thread_stack,
101 K_KERNEL_STACK_SIZEOF(linux_evdev_thread_stack),
102 linux_evdev_thread_fn, (void *)dev, NULL, NULL,
103 CONFIG_NATIVE_LINUX_EVDEV_THREAD_PRIORITY, 0, K_NO_WAIT);
104
105 k_thread_name_set(&linux_evdev_thread, dev->name);
106
107 return 0;
108 }
109
110 BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 1,
111 "Only one zephyr,native-linux-evdev compatible node is supported");
112
113 DEVICE_DT_INST_DEFINE(0, linux_evdev_init, NULL,
114 NULL, NULL,
115 POST_KERNEL, CONFIG_INPUT_INIT_PRIORITY, NULL);
116