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