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