xref: /freebsd/sys/dev/evdev/evdev_private.h (revision 0957b409)
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  * $FreeBSD$
28  */
29 
30 #ifndef	_DEV_EVDEV_EVDEV_PRIVATE_H
31 #define	_DEV_EVDEV_EVDEV_PRIVATE_H
32 
33 #include <sys/bitstring.h>
34 #include <sys/kbio.h>
35 #include <sys/malloc.h>
36 #include <sys/queue.h>
37 #include <sys/selinfo.h>
38 #include <sys/sysctl.h>
39 
40 #include <dev/evdev/evdev.h>
41 #include <dev/evdev/input.h>
42 #include <dev/kbd/kbdreg.h>
43 
44 #define	NAMELEN		80
45 
46 /*
47  * bitstr_t implementation must be identical to one found in EVIOCG*
48  * libevdev ioctls. Our bitstring(3) API is compatible since r299090.
49  */
50 _Static_assert(sizeof(bitstr_t) == sizeof(unsigned long),
51     "bitstr_t size mismatch");
52 
53 MALLOC_DECLARE(M_EVDEV);
54 
55 struct evdev_client;
56 struct evdev_mt;
57 
58 #define	CURRENT_MT_SLOT(evdev)	((evdev)->ev_absinfo[ABS_MT_SLOT].value)
59 #define	MAXIMAL_MT_SLOT(evdev)	((evdev)->ev_absinfo[ABS_MT_SLOT].maximum)
60 
61 enum evdev_key_events
62 {
63 	KEY_EVENT_UP,
64 	KEY_EVENT_DOWN,
65 	KEY_EVENT_REPEAT
66 };
67 
68 /* evdev clock IDs in Linux semantic */
69 enum evdev_clock_id
70 {
71 	EV_CLOCK_REALTIME = 0,	/* UTC clock */
72 	EV_CLOCK_MONOTONIC,	/* monotonic, stops on suspend */
73 	EV_CLOCK_BOOTTIME	/* monotonic, suspend-awared */
74 };
75 
76 enum evdev_lock_type
77 {
78 	EV_LOCK_INTERNAL = 0,	/* Internal evdev mutex */
79 	EV_LOCK_MTX,		/* Driver`s mutex */
80 };
81 
82 struct evdev_dev
83 {
84 	char			ev_name[NAMELEN];
85 	char			ev_shortname[NAMELEN];
86 	char			ev_serial[NAMELEN];
87 	struct cdev *		ev_cdev;
88 	int			ev_unit;
89 	enum evdev_lock_type	ev_lock_type;
90 	struct mtx *		ev_lock;
91 	struct mtx		ev_mtx;
92 	struct input_id		ev_id;
93 	struct evdev_client *	ev_grabber;
94 	size_t			ev_report_size;
95 
96 	/* Supported features: */
97 	bitstr_t		bit_decl(ev_prop_flags, INPUT_PROP_CNT);
98 	bitstr_t		bit_decl(ev_type_flags, EV_CNT);
99 	bitstr_t		bit_decl(ev_key_flags, KEY_CNT);
100 	bitstr_t		bit_decl(ev_rel_flags, REL_CNT);
101 	bitstr_t		bit_decl(ev_abs_flags, ABS_CNT);
102 	bitstr_t		bit_decl(ev_msc_flags, MSC_CNT);
103 	bitstr_t		bit_decl(ev_led_flags, LED_CNT);
104 	bitstr_t		bit_decl(ev_snd_flags, SND_CNT);
105 	bitstr_t		bit_decl(ev_sw_flags, SW_CNT);
106 	struct input_absinfo *	ev_absinfo;
107 	bitstr_t		bit_decl(ev_flags, EVDEV_FLAG_CNT);
108 
109 	/* Repeat parameters & callout: */
110 	int			ev_rep[REP_CNT];
111 	struct callout		ev_rep_callout;
112 	uint16_t		ev_rep_key;
113 
114 	/* State: */
115 	bitstr_t		bit_decl(ev_key_states, KEY_CNT);
116 	bitstr_t		bit_decl(ev_led_states, LED_CNT);
117 	bitstr_t		bit_decl(ev_snd_states, SND_CNT);
118 	bitstr_t		bit_decl(ev_sw_states, SW_CNT);
119 	bool			ev_report_opened;
120 
121 	/* KDB state: */
122 	bool			ev_kdb_active;
123 	bitstr_t		bit_decl(ev_kdb_led_states, LED_CNT);
124 
125 	/* Multitouch protocol type B state: */
126 	struct evdev_mt *	ev_mt;
127 
128 	/* Counters: */
129 	uint64_t		ev_event_count;
130 	uint64_t		ev_report_count;
131 
132 	/* Parent driver callbacks: */
133 	const struct evdev_methods * ev_methods;
134 	void *			ev_softc;
135 
136 	/* Sysctl: */
137 	struct sysctl_ctx_list	ev_sysctl_ctx;
138 
139 	LIST_ENTRY(evdev_dev) ev_link;
140 	LIST_HEAD(, evdev_client) ev_clients;
141 };
142 
143 #define	SYSTEM_CONSOLE_LOCK	&Giant
144 
145 #define	EVDEV_LOCK(evdev)		mtx_lock((evdev)->ev_lock)
146 #define	EVDEV_UNLOCK(evdev)		mtx_unlock((evdev)->ev_lock)
147 #define	EVDEV_LOCK_ASSERT(evdev)	do {				\
148 	if ((evdev)->ev_lock != SYSTEM_CONSOLE_LOCK)			\
149 		mtx_assert((evdev)->ev_lock, MA_OWNED);			\
150 } while (0)
151 #define	EVDEV_ENTER(evdev)	do {					\
152 	if ((evdev)->ev_lock_type == EV_LOCK_INTERNAL)			\
153 		EVDEV_LOCK(evdev);					\
154 	else								\
155 		EVDEV_LOCK_ASSERT(evdev);				\
156 } while (0)
157 #define	EVDEV_EXIT(evdev)	do {					\
158 	if ((evdev)->ev_lock_type == EV_LOCK_INTERNAL)			\
159 		EVDEV_UNLOCK(evdev);					\
160 } while (0)
161 
162 struct evdev_client
163 {
164 	struct evdev_dev *	ec_evdev;
165 	struct mtx		ec_buffer_mtx;
166 	size_t			ec_buffer_size;
167 	size_t			ec_buffer_head;
168 	size_t			ec_buffer_tail;
169 	size_t			ec_buffer_ready;
170 	enum evdev_clock_id	ec_clock_id;
171 	struct selinfo		ec_selp;
172 	struct sigio *		ec_sigio;
173 	bool			ec_async;
174 	bool			ec_revoked;
175 	bool			ec_blocked;
176 	bool			ec_selected;
177 
178 	LIST_ENTRY(evdev_client) ec_link;
179 
180 	struct input_event	ec_buffer[];
181 };
182 
183 #define	EVDEV_CLIENT_LOCKQ(client)	mtx_lock(&(client)->ec_buffer_mtx)
184 #define	EVDEV_CLIENT_UNLOCKQ(client)	mtx_unlock(&(client)->ec_buffer_mtx)
185 #define EVDEV_CLIENT_LOCKQ_ASSERT(client) \
186     mtx_assert(&(client)->ec_buffer_mtx, MA_OWNED)
187 #define	EVDEV_CLIENT_EMPTYQ(client) \
188     ((client)->ec_buffer_head == (client)->ec_buffer_ready)
189 #define	EVDEV_CLIENT_SIZEQ(client) \
190     (((client)->ec_buffer_ready + (client)->ec_buffer_size - \
191       (client)->ec_buffer_head) % (client)->ec_buffer_size)
192 
193 /* Input device interface: */
194 void evdev_send_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
195 int evdev_inject_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
196 int evdev_cdev_create(struct evdev_dev *);
197 int evdev_cdev_destroy(struct evdev_dev *);
198 bool evdev_event_supported(struct evdev_dev *, uint16_t);
199 void evdev_set_abs_bit(struct evdev_dev *, uint16_t);
200 void evdev_set_absinfo(struct evdev_dev *, uint16_t, struct input_absinfo *);
201 void evdev_restore_after_kdb(struct evdev_dev *);
202 
203 /* Client interface: */
204 int evdev_register_client(struct evdev_dev *, struct evdev_client *);
205 void evdev_dispose_client(struct evdev_dev *, struct evdev_client *);
206 int evdev_grab_client(struct evdev_dev *, struct evdev_client *);
207 int evdev_release_client(struct evdev_dev *, struct evdev_client *);
208 void evdev_client_push(struct evdev_client *, uint16_t, uint16_t, int32_t);
209 void evdev_notify_event(struct evdev_client *);
210 void evdev_revoke_client(struct evdev_client *);
211 
212 /* Multitouch related functions: */
213 void evdev_mt_init(struct evdev_dev *);
214 void evdev_mt_free(struct evdev_dev *);
215 int32_t evdev_get_last_mt_slot(struct evdev_dev *);
216 void evdev_set_last_mt_slot(struct evdev_dev *, int32_t);
217 int32_t evdev_get_mt_value(struct evdev_dev *, int32_t, int16_t);
218 void evdev_set_mt_value(struct evdev_dev *, int32_t, int16_t, int32_t);
219 void evdev_send_mt_compat(struct evdev_dev *);
220 void evdev_send_mt_autorel(struct evdev_dev *);
221 
222 /* Utility functions: */
223 void evdev_client_dumpqueue(struct evdev_client *);
224 
225 #endif	/* _DEV_EVDEV_EVDEV_PRIVATE_H */
226