xref: /freebsd/sys/dev/evdev/evdev_private.h (revision 255538cd906045095d0c2113ae6c4731ce36c0cf)
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 	dev_t			ev_devnum;
118 	int			ev_unit;
119 	enum evdev_lock_type	ev_lock_type;
120 	struct mtx *		ev_state_lock;	/* State lock */
121 	struct mtx		ev_mtx;		/* Internal state lock */
122 	struct sx		ev_list_lock;	/* Client list lock */
123 	struct input_id		ev_id;
124 	struct evdev_client *	ev_grabber;			/* (s) */
125 	size_t			ev_report_size;
126 
127 	/* Supported features: */
128 	bitstr_t		bit_decl(ev_prop_flags, INPUT_PROP_CNT);
129 	bitstr_t		bit_decl(ev_type_flags, EV_CNT);
130 	bitstr_t		bit_decl(ev_key_flags, KEY_CNT);
131 	bitstr_t		bit_decl(ev_rel_flags, REL_CNT);
132 	bitstr_t		bit_decl(ev_abs_flags, ABS_CNT);
133 	bitstr_t		bit_decl(ev_msc_flags, MSC_CNT);
134 	bitstr_t		bit_decl(ev_led_flags, LED_CNT);
135 	bitstr_t		bit_decl(ev_snd_flags, SND_CNT);
136 	bitstr_t		bit_decl(ev_sw_flags, SW_CNT);
137 	struct input_absinfo *	ev_absinfo;			/* (s) */
138 	bitstr_t		bit_decl(ev_flags, EVDEV_FLAG_CNT);
139 
140 	/* Repeat parameters & callout: */
141 	int			ev_rep[REP_CNT];		/* (s) */
142 	struct callout		ev_rep_callout;			/* (s) */
143 	uint16_t		ev_rep_key;			/* (s) */
144 
145 	/* State: */
146 	bitstr_t		bit_decl(ev_key_states, KEY_CNT); /* (s) */
147 	bitstr_t		bit_decl(ev_led_states, LED_CNT); /* (s) */
148 	bitstr_t		bit_decl(ev_snd_states, SND_CNT); /* (s) */
149 	bitstr_t		bit_decl(ev_sw_states, SW_CNT);	/* (s) */
150 	bool			ev_report_opened;		/* (s) */
151 
152 	/* KDB state: */
153 	bool			ev_kdb_active;
154 	bitstr_t		bit_decl(ev_kdb_led_states, LED_CNT);
155 
156 	/* Multitouch protocol type B state: */
157 	struct evdev_mt *	ev_mt;				/* (s) */
158 
159 	/* Counters: */
160 	uint64_t		ev_event_count;			/* (s) */
161 	uint64_t		ev_report_count;		/* (s) */
162 
163 	/* Parent driver callbacks: */
164 	const struct evdev_methods * ev_methods;
165 	void *			ev_softc;
166 
167 	/* Sysctl: */
168 	struct sysctl_ctx_list	ev_sysctl_ctx;
169 
170 	LIST_ENTRY(evdev_dev) ev_link;
171 	CK_SLIST_HEAD(, evdev_client) ev_clients;		/* (l) */
172 };
173 
174 #define	SYSTEM_CONSOLE_LOCK	&Giant
175 
176 #define	EVDEV_LOCK(evdev)		mtx_lock((evdev)->ev_state_lock)
177 #define	EVDEV_UNLOCK(evdev)		mtx_unlock((evdev)->ev_state_lock)
178 #define	EVDEV_LOCK_ASSERT(evdev)	do {				\
179 	if ((evdev)->ev_state_lock != SYSTEM_CONSOLE_LOCK)		\
180 		mtx_assert((evdev)->ev_state_lock, MA_OWNED);		\
181 } while (0)
182 #define	EVDEV_ENTER(evdev)	do {					\
183 	if ((evdev)->ev_lock_type != EV_LOCK_MTX)			\
184 		EVDEV_LOCK(evdev);					\
185 	else								\
186 		EVDEV_LOCK_ASSERT(evdev);				\
187 } while (0)
188 #define	EVDEV_EXIT(evdev)	do {					\
189 	if ((evdev)->ev_lock_type != EV_LOCK_MTX)			\
190 		EVDEV_UNLOCK(evdev);					\
191 } while (0)
192 
193 #define	EVDEV_LIST_LOCK(evdev)	do {					\
194 	if ((evdev)->ev_lock_type == EV_LOCK_MTX)			\
195 		EVDEV_LOCK(evdev);					\
196 	else								\
197 		sx_xlock(&(evdev)->ev_list_lock);			\
198 } while (0)
199 #define	EVDEV_LIST_UNLOCK(evdev)	do {				\
200 	if ((evdev)->ev_lock_type == EV_LOCK_MTX)			\
201 		EVDEV_UNLOCK(evdev);					\
202 	else								\
203 		sx_unlock(&(evdev)->ev_list_lock);			\
204 } while (0)
205 #define	EVDEV_LIST_LOCK_ASSERT(evdev)	do {				\
206 	if ((evdev)->ev_lock_type == EV_LOCK_MTX)			\
207 		EVDEV_LOCK_ASSERT(evdev);				\
208 	else								\
209 		sx_assert(&(evdev)->ev_list_lock, MA_OWNED);		\
210 } while (0)
211 static inline int
212 EVDEV_LIST_LOCK_SIG(struct evdev_dev *evdev)
213 {
214 	if (evdev->ev_lock_type == EV_LOCK_MTX) {
215 		EVDEV_LOCK(evdev);
216 		return (0);
217 	}
218 	return (sx_xlock_sig(&evdev->ev_list_lock));
219 }
220 
221 struct evdev_client
222 {
223 	struct evdev_dev *	ec_evdev;
224 	struct mtx		ec_buffer_mtx;	/* Client queue lock */
225 	size_t			ec_buffer_size;
226 	size_t			ec_buffer_head;		/* (q) */
227 	size_t			ec_buffer_tail;		/* (q) */
228 	size_t			ec_buffer_ready;	/* (q) */
229 	enum evdev_clock_id	ec_clock_id;
230 	struct selinfo		ec_selp;		/* (q) */
231 	struct sigio *		ec_sigio;
232 	bool			ec_async;		/* (q) */
233 	bool			ec_revoked;		/* (l) */
234 	bool			ec_blocked;		/* (q) */
235 	bool			ec_selected;		/* (q) */
236 
237 	CK_SLIST_ENTRY(evdev_client) ec_link;		/* (l) */
238 
239 	struct input_event	ec_buffer[];		/* (q) */
240 };
241 
242 #define	EVDEV_CLIENT_LOCKQ(client)	mtx_lock(&(client)->ec_buffer_mtx)
243 #define	EVDEV_CLIENT_UNLOCKQ(client)	mtx_unlock(&(client)->ec_buffer_mtx)
244 #define EVDEV_CLIENT_LOCKQ_ASSERT(client) \
245     mtx_assert(&(client)->ec_buffer_mtx, MA_OWNED)
246 #define	EVDEV_CLIENT_EMPTYQ(client) \
247     ((client)->ec_buffer_head == (client)->ec_buffer_ready)
248 #define	EVDEV_CLIENT_SIZEQ(client) \
249     (((client)->ec_buffer_ready + (client)->ec_buffer_size - \
250       (client)->ec_buffer_head) % (client)->ec_buffer_size)
251 
252 /* bitstring(3) helper */
253 static inline void
254 bit_change(bitstr_t *bitstr, int bit, int value)
255 {
256 	if (value)
257 		bit_set(bitstr, bit);
258 	else
259 		bit_clear(bitstr, bit);
260 }
261 
262 /* Input device interface: */
263 void evdev_send_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
264 int evdev_inject_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
265 int evdev_cdev_create(struct evdev_dev *);
266 int evdev_cdev_destroy(struct evdev_dev *);
267 bool evdev_event_supported(struct evdev_dev *, uint16_t);
268 void evdev_set_abs_bit(struct evdev_dev *, uint16_t);
269 void evdev_set_absinfo(struct evdev_dev *, uint16_t, struct input_absinfo *);
270 void evdev_restore_after_kdb(struct evdev_dev *);
271 
272 /* Client interface: */
273 int evdev_register_client(struct evdev_dev *, struct evdev_client *);
274 void evdev_dispose_client(struct evdev_dev *, struct evdev_client *);
275 int evdev_grab_client(struct evdev_dev *, struct evdev_client *);
276 int evdev_release_client(struct evdev_dev *, struct evdev_client *);
277 void evdev_client_push(struct evdev_client *, uint16_t, uint16_t, int32_t);
278 void evdev_notify_event(struct evdev_client *);
279 void evdev_revoke_client(struct evdev_client *);
280 
281 /* Multitouch related functions: */
282 void evdev_mt_init(struct evdev_dev *);
283 void evdev_mt_free(struct evdev_dev *);
284 void evdev_mt_sync_frame(struct evdev_dev *);
285 int evdev_mt_get_last_slot(struct evdev_dev *);
286 void evdev_mt_set_last_slot(struct evdev_dev *, int);
287 int32_t evdev_mt_get_value(struct evdev_dev *, int, int16_t);
288 void evdev_mt_set_value(struct evdev_dev *, int, int16_t, int32_t);
289 int32_t evdev_mt_reassign_id(struct evdev_dev *, int, int32_t);
290 bool evdev_mt_record_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
291 
292 /* Utility functions: */
293 void evdev_client_dumpqueue(struct evdev_client *);
294 void evdev_send_nfingers(struct evdev_dev *, int);
295 
296 #endif	/* _DEV_EVDEV_EVDEV_PRIVATE_H */
297