1 /*-
2 * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org>
3 * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #ifndef _DEV_EVDEV_EVDEV_PRIVATE_H
29 #define _DEV_EVDEV_EVDEV_PRIVATE_H
30
31 #include <sys/bitstring.h>
32 #include <sys/ck.h>
33 #include <sys/epoch.h>
34 #include <sys/kbio.h>
35 #include <sys/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/mutex.h>
38 #include <sys/queue.h>
39 #include <sys/selinfo.h>
40 #include <sys/sx.h>
41 #include <sys/sysctl.h>
42
43 #include <dev/evdev/evdev.h>
44 #include <dev/evdev/input.h>
45 #include <dev/kbd/kbdreg.h>
46
47 #define NAMELEN 80
48
49 /*
50 * bitstr_t implementation must be identical to one found in EVIOCG*
51 * libevdev ioctls. Our bitstring(3) API is compatible since r299090.
52 */
53 _Static_assert(sizeof(bitstr_t) == sizeof(unsigned long),
54 "bitstr_t size mismatch");
55
56 MALLOC_DECLARE(M_EVDEV);
57
58 struct evdev_client;
59 struct evdev_mt;
60
61 #define CURRENT_MT_SLOT(evdev) ((evdev)->ev_absinfo[ABS_MT_SLOT].value)
62 #define MAXIMAL_MT_SLOT(evdev) ((evdev)->ev_absinfo[ABS_MT_SLOT].maximum)
63
64 enum evdev_key_events
65 {
66 KEY_EVENT_UP,
67 KEY_EVENT_DOWN,
68 KEY_EVENT_REPEAT
69 };
70
71 /* evdev clock IDs in Linux semantic */
72 enum evdev_clock_id
73 {
74 EV_CLOCK_REALTIME = 0, /* UTC clock */
75 EV_CLOCK_MONOTONIC, /* monotonic, stops on suspend */
76 EV_CLOCK_BOOTTIME /* monotonic, suspend-awared */
77 };
78
79 /*
80 * Locking.
81 *
82 * Internal evdev structures are protected with next locks:
83 * State lock (s) - Internal state. The data it protects is changed
84 * by incoming evdev events and some ioctls.
85 * Client list epoch (l) - Read access to client list.
86 * Client list lock (l) - Write access to client list.
87 * Client queue locks (q) - One lock per client to serialize access to data
88 * available through character device node.
89 *
90 * Depending on evdev_register_() suffix evdev can run in following modes:
91 * 1. Internal epoch. evdev_register(). All locks are internal.
92 * 2. External epoch. Evdev expects to be run under input epoch entered by
93 * parent driver. The mode is enabled with EVDEV_FLAG_EXT_EPOCH flag.
94 * 3. External mutex. evdev_register_mtx(). Evdev uses mutex provided by parent
95 * driver as both "State lock" and "Client list lock". This mode is
96 * deprecated as it causes ev_open and ev_close handlers to be called with
97 * parent driver mutex taken.
98 */
99 #define INPUT_EPOCH global_epoch_preempt
100
101 enum evdev_lock_type
102 {
103 EV_LOCK_INTERNAL = 0, /* Internal epoch */
104 EV_LOCK_MTX, /* Driver`s mutex */
105 EV_LOCK_EXT_EPOCH, /* External epoch */
106 };
107
108 struct evdev_dev
109 {
110 char ev_name[NAMELEN];
111 char ev_shortname[NAMELEN];
112 char ev_serial[NAMELEN];
113 struct cdev * ev_cdev;
114 uid_t ev_cdev_uid;
115 gid_t ev_cdev_gid;
116 int ev_cdev_mode;
117 int ev_unit;
118 enum evdev_lock_type ev_lock_type;
119 struct mtx * ev_state_lock; /* State lock */
120 struct mtx ev_mtx; /* Internal state lock */
121 struct sx ev_list_lock; /* Client list lock */
122 struct input_id ev_id;
123 struct evdev_client * ev_grabber; /* (s) */
124 size_t ev_report_size;
125
126 /* Supported features: */
127 bitstr_t bit_decl(ev_prop_flags, INPUT_PROP_CNT);
128 bitstr_t bit_decl(ev_type_flags, EV_CNT);
129 bitstr_t bit_decl(ev_key_flags, KEY_CNT);
130 bitstr_t bit_decl(ev_rel_flags, REL_CNT);
131 bitstr_t bit_decl(ev_abs_flags, ABS_CNT);
132 bitstr_t bit_decl(ev_msc_flags, MSC_CNT);
133 bitstr_t bit_decl(ev_led_flags, LED_CNT);
134 bitstr_t bit_decl(ev_snd_flags, SND_CNT);
135 bitstr_t bit_decl(ev_sw_flags, SW_CNT);
136 struct input_absinfo * ev_absinfo; /* (s) */
137 bitstr_t bit_decl(ev_flags, EVDEV_FLAG_CNT);
138
139 /* Repeat parameters & callout: */
140 int ev_rep[REP_CNT]; /* (s) */
141 struct callout ev_rep_callout; /* (s) */
142 uint16_t ev_rep_key; /* (s) */
143
144 /* State: */
145 bitstr_t bit_decl(ev_key_states, KEY_CNT); /* (s) */
146 bitstr_t bit_decl(ev_led_states, LED_CNT); /* (s) */
147 bitstr_t bit_decl(ev_snd_states, SND_CNT); /* (s) */
148 bitstr_t bit_decl(ev_sw_states, SW_CNT); /* (s) */
149 bool ev_report_opened; /* (s) */
150
151 /* KDB state: */
152 bool ev_kdb_active;
153 bitstr_t bit_decl(ev_kdb_led_states, LED_CNT);
154
155 /* Multitouch protocol type B state: */
156 struct evdev_mt * ev_mt; /* (s) */
157
158 /* Counters: */
159 uint64_t ev_event_count; /* (s) */
160 uint64_t ev_report_count; /* (s) */
161
162 /* Parent driver callbacks: */
163 const struct evdev_methods * ev_methods;
164 void * ev_softc;
165
166 /* Sysctl: */
167 struct sysctl_ctx_list ev_sysctl_ctx;
168
169 LIST_ENTRY(evdev_dev) ev_link;
170 CK_SLIST_HEAD(, evdev_client) ev_clients; /* (l) */
171 };
172
173 #define SYSTEM_CONSOLE_LOCK &Giant
174
175 #define EVDEV_LOCK(evdev) mtx_lock((evdev)->ev_state_lock)
176 #define EVDEV_UNLOCK(evdev) mtx_unlock((evdev)->ev_state_lock)
177 #define EVDEV_LOCK_ASSERT(evdev) do { \
178 if ((evdev)->ev_state_lock != SYSTEM_CONSOLE_LOCK) \
179 mtx_assert((evdev)->ev_state_lock, MA_OWNED); \
180 } while (0)
181 #define EVDEV_ENTER(evdev) do { \
182 if ((evdev)->ev_lock_type != EV_LOCK_MTX) \
183 EVDEV_LOCK(evdev); \
184 else \
185 EVDEV_LOCK_ASSERT(evdev); \
186 } while (0)
187 #define EVDEV_EXIT(evdev) do { \
188 if ((evdev)->ev_lock_type != EV_LOCK_MTX) \
189 EVDEV_UNLOCK(evdev); \
190 } while (0)
191
192 #define EVDEV_LIST_LOCK(evdev) do { \
193 if ((evdev)->ev_lock_type == EV_LOCK_MTX) \
194 EVDEV_LOCK(evdev); \
195 else \
196 sx_xlock(&(evdev)->ev_list_lock); \
197 } while (0)
198 #define EVDEV_LIST_UNLOCK(evdev) do { \
199 if ((evdev)->ev_lock_type == EV_LOCK_MTX) \
200 EVDEV_UNLOCK(evdev); \
201 else \
202 sx_unlock(&(evdev)->ev_list_lock); \
203 } while (0)
204 #define EVDEV_LIST_LOCK_ASSERT(evdev) do { \
205 if ((evdev)->ev_lock_type == EV_LOCK_MTX) \
206 EVDEV_LOCK_ASSERT(evdev); \
207 else \
208 sx_assert(&(evdev)->ev_list_lock, MA_OWNED); \
209 } while (0)
210 static inline int
EVDEV_LIST_LOCK_SIG(struct evdev_dev * evdev)211 EVDEV_LIST_LOCK_SIG(struct evdev_dev *evdev)
212 {
213 if (evdev->ev_lock_type == EV_LOCK_MTX) {
214 EVDEV_LOCK(evdev);
215 return (0);
216 }
217 return (sx_xlock_sig(&evdev->ev_list_lock));
218 }
219
220 struct evdev_client
221 {
222 struct evdev_dev * ec_evdev;
223 struct mtx ec_buffer_mtx; /* Client queue lock */
224 size_t ec_buffer_size;
225 size_t ec_buffer_head; /* (q) */
226 size_t ec_buffer_tail; /* (q) */
227 size_t ec_buffer_ready; /* (q) */
228 enum evdev_clock_id ec_clock_id;
229 struct selinfo ec_selp; /* (q) */
230 struct sigio * ec_sigio;
231 bool ec_async; /* (q) */
232 bool ec_revoked; /* (l) */
233 bool ec_blocked; /* (q) */
234 bool ec_selected; /* (q) */
235
236 CK_SLIST_ENTRY(evdev_client) ec_link; /* (l) */
237
238 struct input_event ec_buffer[]; /* (q) */
239 };
240
241 #define EVDEV_CLIENT_LOCKQ(client) mtx_lock(&(client)->ec_buffer_mtx)
242 #define EVDEV_CLIENT_UNLOCKQ(client) mtx_unlock(&(client)->ec_buffer_mtx)
243 #define EVDEV_CLIENT_LOCKQ_ASSERT(client) \
244 mtx_assert(&(client)->ec_buffer_mtx, MA_OWNED)
245 #define EVDEV_CLIENT_EMPTYQ(client) \
246 ((client)->ec_buffer_head == (client)->ec_buffer_ready)
247 #define EVDEV_CLIENT_SIZEQ(client) \
248 (((client)->ec_buffer_ready + (client)->ec_buffer_size - \
249 (client)->ec_buffer_head) % (client)->ec_buffer_size)
250
251 /* bitstring(3) helper */
252 static inline void
bit_change(bitstr_t * bitstr,int bit,int value)253 bit_change(bitstr_t *bitstr, int bit, int value)
254 {
255 if (value)
256 bit_set(bitstr, bit);
257 else
258 bit_clear(bitstr, bit);
259 }
260
261 /* Input device interface: */
262 void evdev_send_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
263 int evdev_inject_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
264 int evdev_cdev_create(struct evdev_dev *);
265 int evdev_cdev_destroy(struct evdev_dev *);
266 bool evdev_event_supported(struct evdev_dev *, uint16_t);
267 void evdev_set_abs_bit(struct evdev_dev *, uint16_t);
268 void evdev_set_absinfo(struct evdev_dev *, uint16_t, struct input_absinfo *);
269 void evdev_restore_after_kdb(struct evdev_dev *);
270
271 /* Client interface: */
272 int evdev_register_client(struct evdev_dev *, struct evdev_client *);
273 void evdev_dispose_client(struct evdev_dev *, struct evdev_client *);
274 int evdev_grab_client(struct evdev_dev *, struct evdev_client *);
275 int evdev_release_client(struct evdev_dev *, struct evdev_client *);
276 void evdev_client_push(struct evdev_client *, uint16_t, uint16_t, int32_t);
277 void evdev_notify_event(struct evdev_client *);
278 void evdev_revoke_client(struct evdev_client *);
279
280 /* Multitouch related functions: */
281 void evdev_mt_init(struct evdev_dev *);
282 void evdev_mt_free(struct evdev_dev *);
283 void evdev_mt_sync_frame(struct evdev_dev *);
284 int evdev_mt_get_last_slot(struct evdev_dev *);
285 void evdev_mt_set_last_slot(struct evdev_dev *, int);
286 int32_t evdev_mt_get_value(struct evdev_dev *, int, int16_t);
287 void evdev_mt_set_value(struct evdev_dev *, int, int16_t, int32_t);
288 int32_t evdev_mt_reassign_id(struct evdev_dev *, int, int32_t);
289 bool evdev_mt_record_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
290
291 /* Utility functions: */
292 void evdev_client_dumpqueue(struct evdev_client *);
293 void evdev_send_nfingers(struct evdev_dev *, int);
294
295 #endif /* _DEV_EVDEV_EVDEV_PRIVATE_H */
296