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