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