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