1/*-
2 * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#ifndef	_DEV_EVDEV_EVDEV_H
28#define	_DEV_EVDEV_EVDEV_H
29
30#include <sys/types.h>
31#include <sys/epoch.h>
32#include <sys/kbio.h>
33#include <dev/evdev/input.h>
34#include <dev/kbd/kbdreg.h>
35
36#define	NAMELEN		80
37
38struct evdev_dev;
39
40typedef int (evdev_open_t)(struct evdev_dev *);
41typedef int (evdev_close_t)(struct evdev_dev *);
42typedef void (evdev_event_t)(struct evdev_dev *, uint16_t, uint16_t, int32_t);
43typedef void (evdev_keycode_t)(struct evdev_dev *,
44    struct input_keymap_entry *);
45
46/*
47 * Keyboard and mouse events recipient mask.
48 * evdev_rcpt_mask variable should be respected by keyboard and mouse drivers
49 * that are able to send events through both evdev and sysmouse/kbdmux
50 * interfaces so user can choose preferred one to not receive one event twice.
51 */
52#define	EVDEV_RCPT_SYSMOUSE	(1<<0)
53#define	EVDEV_RCPT_KBDMUX	(1<<1)
54#define	EVDEV_RCPT_HW_MOUSE	(1<<2)
55#define	EVDEV_RCPT_HW_KBD	(1<<3)
56extern int evdev_rcpt_mask;
57/*
58 * Sysmouse protocol does not support horizontal wheel movement reporting.
59 * To overcome this limitation different drivers use different sysmouse proto
60 * extensions. Set kern.evdev.sysmouse_t_axis to tell sysmouse evdev driver
61 * which protocol extension is used.
62 * 0 - do not extract horizontal wheel movement (default).
63 * 1 - ums(4) horizontal wheel encoding. T-axis is mapped to buttons 6 and 7
64 * 2 - psm(4) wheels encoding: z = 1,-1 - vert. wheel, z = 2,-2 - horiz. wheel
65 */
66enum
67{
68	EVDEV_SYSMOUSE_T_AXIS_NONE = 0,
69	EVDEV_SYSMOUSE_T_AXIS_UMS = 1,
70	EVDEV_SYSMOUSE_T_AXIS_PSM = 2,
71};
72extern int evdev_sysmouse_t_axis;
73
74#define	ABS_MT_FIRST	ABS_MT_TOUCH_MAJOR
75#define	ABS_MT_LAST	ABS_MT_TOOL_Y
76#define	ABS_IS_MT(x)	((x) >= ABS_MT_FIRST && (x) <= ABS_MT_LAST)
77#define	ABS_MT_INDEX(x)	((x) - ABS_MT_FIRST)
78#define	MT_CNT		(ABS_MT_INDEX(ABS_MT_LAST) + 1)
79/* Multitouch protocol type A */
80#define	MAX_MT_REPORTS	5
81/* Multitouch protocol type B interface */
82#define	MAX_MT_SLOTS	16
83
84#define	EVDEV_FLAG_SOFTREPEAT	0x00	/* use evdev to repeat keys */
85#define	EVDEV_FLAG_MT_STCOMPAT	0x01	/* autogenerate ST-compatible events
86					 * for MT protocol type B reports */
87#define	EVDEV_FLAG_MT_AUTOREL	0x02	/* Autorelease MT-slots not listed in
88					 * current MT protocol type B report */
89#define	EVDEV_FLAG_EXT_EPOCH	0x03	/* evdev_push_* is allways called with
90					 * input (global) epoch entered */
91#define	EVDEV_FLAG_MT_KEEPID	0x04	/* Do not reassign tracking ID */
92#define	EVDEV_FLAG_MT_TRACK	0x05	/* Assign touch to slot by evdev */
93#define	EVDEV_FLAG_MAX		0x1F
94#define	EVDEV_FLAG_CNT		(EVDEV_FLAG_MAX + 1)
95
96struct evdev_methods
97{
98	evdev_open_t		*ev_open;
99	evdev_close_t		*ev_close;
100	evdev_event_t		*ev_event;
101	evdev_keycode_t		*ev_get_keycode;
102	evdev_keycode_t		*ev_set_keycode;
103};
104
105union evdev_mt_slot {
106	int32_t         val[MT_CNT];
107	struct {
108		int32_t maj;		/* ABS_MT_TOUCH_MAJOR */
109		int32_t min;		/* ABS_MT_TOUCH_MINOR */
110		int32_t w_maj;		/* ABS_MT_WIDTH_MAJOR */
111		int32_t w_min;		/* ABS_MT_WIDTH_MINOR */
112		int32_t ori;		/* ABS_MT_ORIENTATION */
113		int32_t x;		/* ABS_MT_POSITION_X */
114		int32_t y;		/* ABS_MT_POSITION_Y */
115		int32_t type;		/* ABS_MT_TOOL_TYPE */
116		int32_t blob_id;	/* ABS_MT_BLOB_ID */
117		int32_t id;		/* ABS_MT_TRACKING_ID */
118		int32_t p;		/* ABS_MT_PRESSURE */
119		int32_t dist;		/* ABS_MT_DISTANCE */
120		int32_t tool_x;		/* ABS_MT_TOOL_X */
121		int32_t tool_y;		/* ABS_MT_TOOL_Y */
122	};
123};
124_Static_assert(offsetof(union evdev_mt_slot, tool_y) ==
125    offsetof(union evdev_mt_slot, val[ABS_MT_INDEX(ABS_MT_TOOL_Y)]),
126    "evdev_mt_slot array members does not match their structure aliases");
127
128/* Input device interface: */
129struct evdev_dev *evdev_alloc(void);
130void evdev_free(struct evdev_dev *);
131void evdev_set_name(struct evdev_dev *, const char *);
132void evdev_set_id(struct evdev_dev *, uint16_t, uint16_t, uint16_t, uint16_t);
133void evdev_set_phys(struct evdev_dev *, const char *);
134void evdev_set_serial(struct evdev_dev *, const char *);
135void evdev_set_methods(struct evdev_dev *, void *,
136    const struct evdev_methods *);
137int evdev_register(struct evdev_dev *);
138int evdev_register_mtx(struct evdev_dev *, struct mtx *);
139int evdev_unregister(struct evdev_dev *);
140int evdev_push_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
141void evdev_support_prop(struct evdev_dev *, uint16_t);
142void evdev_support_event(struct evdev_dev *, uint16_t);
143void evdev_support_key(struct evdev_dev *, uint16_t);
144void evdev_support_rel(struct evdev_dev *, uint16_t);
145void evdev_support_abs(struct evdev_dev *, uint16_t, int32_t, int32_t, int32_t,
146   int32_t, int32_t);
147void evdev_support_msc(struct evdev_dev *, uint16_t);
148void evdev_support_led(struct evdev_dev *, uint16_t);
149void evdev_support_snd(struct evdev_dev *, uint16_t);
150void evdev_support_sw(struct evdev_dev *, uint16_t);
151void evdev_set_repeat_params(struct evdev_dev *, uint16_t, int);
152int evdev_set_report_size(struct evdev_dev *, size_t);
153void evdev_set_flag(struct evdev_dev *, uint16_t);
154void *evdev_get_softc(struct evdev_dev *);
155bool evdev_is_grabbed(struct evdev_dev *);
156
157/* Multitouch related functions: */
158int evdev_mt_id_to_slot(struct evdev_dev *, int32_t);
159int evdev_mt_push_slot(struct evdev_dev *, int, union evdev_mt_slot *);
160int evdev_mt_push_frame(struct evdev_dev *, union evdev_mt_slot *, int);
161void evdev_mt_match_frame(struct evdev_dev *, union evdev_mt_slot *, int);
162union evdev_mt_slot *evdev_mt_get_match_slots(struct evdev_dev *);
163void evdev_mt_push_autorel(struct evdev_dev *);
164
165/* Utility functions: */
166uint16_t evdev_hid2key(int);
167void evdev_support_all_known_keys(struct evdev_dev *);
168uint16_t evdev_scancode2key(int *, int);
169void evdev_push_mouse_btn(struct evdev_dev *, int);
170void evdev_push_leds(struct evdev_dev *, int);
171void evdev_push_repeats(struct evdev_dev *, keyboard_t *);
172void evdev_support_nfingers(struct evdev_dev *, int);
173void evdev_push_nfingers(struct evdev_dev *, int);
174
175/* Event reporting shortcuts: */
176static __inline int
177evdev_sync(struct evdev_dev *evdev)
178{
179
180	return (evdev_push_event(evdev, EV_SYN, SYN_REPORT, 1));
181}
182
183static __inline int
184evdev_mt_sync(struct evdev_dev *evdev)
185{
186
187	return (evdev_push_event(evdev, EV_SYN, SYN_MT_REPORT, 1));
188}
189
190static __inline int
191evdev_push_key(struct evdev_dev *evdev, uint16_t code, int32_t value)
192{
193
194	return (evdev_push_event(evdev, EV_KEY, code, value != 0));
195}
196
197static __inline int
198evdev_push_rel(struct evdev_dev *evdev, uint16_t code, int32_t value)
199{
200
201	return (evdev_push_event(evdev, EV_REL, code, value));
202}
203
204static __inline int
205evdev_push_abs(struct evdev_dev *evdev, uint16_t code, int32_t value)
206{
207
208	return (evdev_push_event(evdev, EV_ABS, code, value));
209}
210
211static __inline int
212evdev_push_msc(struct evdev_dev *evdev, uint16_t code, int32_t value)
213{
214
215	return (evdev_push_event(evdev, EV_MSC, code, value));
216}
217
218static __inline int
219evdev_push_led(struct evdev_dev *evdev, uint16_t code, int32_t value)
220{
221
222	return (evdev_push_event(evdev, EV_LED, code, value != 0));
223}
224
225static __inline int
226evdev_push_snd(struct evdev_dev *evdev, uint16_t code, int32_t value)
227{
228
229	return (evdev_push_event(evdev, EV_SND, code, value));
230}
231
232static __inline int
233evdev_push_sw(struct evdev_dev *evdev, uint16_t code, int32_t value)
234{
235
236	return (evdev_push_event(evdev, EV_SW, code, value != 0));
237}
238
239#endif	/* _DEV_EVDEV_EVDEV_H */
240