1305706Sgonzo/*-
2305706Sgonzo * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org>
3324768Swulf * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@FreeBSD.org>
4305706Sgonzo * All rights reserved.
5305706Sgonzo *
6305706Sgonzo * Redistribution and use in source and binary forms, with or without
7305706Sgonzo * modification, are permitted provided that the following conditions
8305706Sgonzo * are met:
9305706Sgonzo * 1. Redistributions of source code must retain the above copyright
10305706Sgonzo *    notice, this list of conditions and the following disclaimer.
11305706Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
12305706Sgonzo *    notice, this list of conditions and the following disclaimer in the
13305706Sgonzo *    documentation and/or other materials provided with the distribution.
14305706Sgonzo *
15305706Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16305706Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17305706Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18305706Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19305706Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20305706Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21305706Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22305706Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23305706Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24305706Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25305706Sgonzo * SUCH DAMAGE.
26305706Sgonzo *
27305706Sgonzo * $FreeBSD: stable/11/sys/dev/evdev/evdev_private.h 344984 2019-03-10 20:58:24Z wulf $
28305706Sgonzo */
29305706Sgonzo
30305706Sgonzo#ifndef	_DEV_EVDEV_EVDEV_PRIVATE_H
31305706Sgonzo#define	_DEV_EVDEV_EVDEV_PRIVATE_H
32305706Sgonzo
33305706Sgonzo#include <sys/bitstring.h>
34324768Swulf#include <sys/kbio.h>
35324768Swulf#include <sys/malloc.h>
36305706Sgonzo#include <sys/queue.h>
37305706Sgonzo#include <sys/selinfo.h>
38344984Swulf#include <sys/sysctl.h>
39324768Swulf
40305706Sgonzo#include <dev/evdev/evdev.h>
41305706Sgonzo#include <dev/evdev/input.h>
42305706Sgonzo#include <dev/kbd/kbdreg.h>
43305706Sgonzo
44305706Sgonzo#define	NAMELEN		80
45305706Sgonzo
46305706Sgonzo/*
47305706Sgonzo * bitstr_t implementation must be identical to one found in EVIOCG*
48305706Sgonzo * libevdev ioctls. Our bitstring(3) API is compatible since r299090.
49305706Sgonzo */
50305706Sgonzo_Static_assert(sizeof(bitstr_t) == sizeof(unsigned long),
51305706Sgonzo    "bitstr_t size mismatch");
52305706Sgonzo
53305706SgonzoMALLOC_DECLARE(M_EVDEV);
54305706Sgonzo
55305706Sgonzostruct evdev_client;
56305706Sgonzostruct evdev_mt;
57305706Sgonzo
58305706Sgonzo#define	CURRENT_MT_SLOT(evdev)	((evdev)->ev_absinfo[ABS_MT_SLOT].value)
59305706Sgonzo#define	MAXIMAL_MT_SLOT(evdev)	((evdev)->ev_absinfo[ABS_MT_SLOT].maximum)
60305706Sgonzo
61305706Sgonzoenum evdev_key_events
62305706Sgonzo{
63305706Sgonzo	KEY_EVENT_UP,
64305706Sgonzo	KEY_EVENT_DOWN,
65305706Sgonzo	KEY_EVENT_REPEAT
66305706Sgonzo};
67305706Sgonzo
68305706Sgonzo/* evdev clock IDs in Linux semantic */
69305706Sgonzoenum evdev_clock_id
70305706Sgonzo{
71305706Sgonzo	EV_CLOCK_REALTIME = 0,	/* UTC clock */
72305706Sgonzo	EV_CLOCK_MONOTONIC,	/* monotonic, stops on suspend */
73305706Sgonzo	EV_CLOCK_BOOTTIME	/* monotonic, suspend-awared */
74305706Sgonzo};
75305706Sgonzo
76307777Sgonzoenum evdev_lock_type
77307777Sgonzo{
78307777Sgonzo	EV_LOCK_INTERNAL = 0,	/* Internal evdev mutex */
79307777Sgonzo	EV_LOCK_MTX,		/* Driver`s mutex */
80307777Sgonzo};
81307777Sgonzo
82305706Sgonzostruct evdev_dev
83305706Sgonzo{
84305706Sgonzo	char			ev_name[NAMELEN];
85305706Sgonzo	char			ev_shortname[NAMELEN];
86305706Sgonzo	char			ev_serial[NAMELEN];
87305706Sgonzo	struct cdev *		ev_cdev;
88305706Sgonzo	int			ev_unit;
89307777Sgonzo	enum evdev_lock_type	ev_lock_type;
90307777Sgonzo	struct mtx *		ev_lock;
91305706Sgonzo	struct mtx		ev_mtx;
92305706Sgonzo	struct input_id		ev_id;
93305706Sgonzo	struct evdev_client *	ev_grabber;
94305706Sgonzo	size_t			ev_report_size;
95305706Sgonzo
96305706Sgonzo	/* Supported features: */
97305706Sgonzo	bitstr_t		bit_decl(ev_prop_flags, INPUT_PROP_CNT);
98305706Sgonzo	bitstr_t		bit_decl(ev_type_flags, EV_CNT);
99305706Sgonzo	bitstr_t		bit_decl(ev_key_flags, KEY_CNT);
100305706Sgonzo	bitstr_t		bit_decl(ev_rel_flags, REL_CNT);
101305706Sgonzo	bitstr_t		bit_decl(ev_abs_flags, ABS_CNT);
102305706Sgonzo	bitstr_t		bit_decl(ev_msc_flags, MSC_CNT);
103305706Sgonzo	bitstr_t		bit_decl(ev_led_flags, LED_CNT);
104305706Sgonzo	bitstr_t		bit_decl(ev_snd_flags, SND_CNT);
105305706Sgonzo	bitstr_t		bit_decl(ev_sw_flags, SW_CNT);
106305706Sgonzo	struct input_absinfo *	ev_absinfo;
107305706Sgonzo	bitstr_t		bit_decl(ev_flags, EVDEV_FLAG_CNT);
108305706Sgonzo
109305706Sgonzo	/* Repeat parameters & callout: */
110305706Sgonzo	int			ev_rep[REP_CNT];
111305706Sgonzo	struct callout		ev_rep_callout;
112305706Sgonzo	uint16_t		ev_rep_key;
113305706Sgonzo
114305706Sgonzo	/* State: */
115305706Sgonzo	bitstr_t		bit_decl(ev_key_states, KEY_CNT);
116305706Sgonzo	bitstr_t		bit_decl(ev_led_states, LED_CNT);
117305706Sgonzo	bitstr_t		bit_decl(ev_snd_states, SND_CNT);
118305706Sgonzo	bitstr_t		bit_decl(ev_sw_states, SW_CNT);
119305706Sgonzo	bool			ev_report_opened;
120305706Sgonzo
121305706Sgonzo	/* Multitouch protocol type B state: */
122305706Sgonzo	struct evdev_mt *	ev_mt;
123305706Sgonzo
124305706Sgonzo	/* Counters: */
125305706Sgonzo	uint64_t		ev_event_count;
126305706Sgonzo	uint64_t		ev_report_count;
127305706Sgonzo
128305706Sgonzo	/* Parent driver callbacks: */
129307777Sgonzo	const struct evdev_methods * ev_methods;
130305706Sgonzo	void *			ev_softc;
131305706Sgonzo
132344984Swulf	/* Sysctl: */
133344984Swulf	struct sysctl_ctx_list	ev_sysctl_ctx;
134344984Swulf
135305706Sgonzo	LIST_ENTRY(evdev_dev) ev_link;
136305706Sgonzo	LIST_HEAD(, evdev_client) ev_clients;
137305706Sgonzo};
138305706Sgonzo
139307777Sgonzo#define	EVDEV_LOCK(evdev)		mtx_lock((evdev)->ev_lock)
140307777Sgonzo#define	EVDEV_UNLOCK(evdev)		mtx_unlock((evdev)->ev_lock)
141307777Sgonzo#define	EVDEV_LOCK_ASSERT(evdev)	mtx_assert((evdev)->ev_lock, MA_OWNED)
142324768Swulf#define	EVDEV_ENTER(evdev)	do {					\
143324768Swulf	if ((evdev)->ev_lock_type == EV_LOCK_INTERNAL)			\
144324768Swulf		EVDEV_LOCK(evdev);					\
145324768Swulf	else								\
146324768Swulf		EVDEV_LOCK_ASSERT(evdev);				\
147324768Swulf} while (0)
148324768Swulf#define	EVDEV_EXIT(evdev)	do {					\
149324768Swulf	if ((evdev)->ev_lock_type == EV_LOCK_INTERNAL)			\
150324768Swulf		EVDEV_UNLOCK(evdev);					\
151324768Swulf} while (0)
152305706Sgonzo
153305706Sgonzostruct evdev_client
154305706Sgonzo{
155305706Sgonzo	struct evdev_dev *	ec_evdev;
156305706Sgonzo	struct mtx		ec_buffer_mtx;
157305706Sgonzo	size_t			ec_buffer_size;
158305706Sgonzo	size_t			ec_buffer_head;
159305706Sgonzo	size_t			ec_buffer_tail;
160305706Sgonzo	size_t			ec_buffer_ready;
161305706Sgonzo	enum evdev_clock_id	ec_clock_id;
162305706Sgonzo	struct selinfo		ec_selp;
163305706Sgonzo	struct sigio *		ec_sigio;
164305706Sgonzo	bool			ec_async;
165305706Sgonzo	bool			ec_revoked;
166305706Sgonzo	bool			ec_blocked;
167305706Sgonzo	bool			ec_selected;
168305706Sgonzo
169305706Sgonzo	LIST_ENTRY(evdev_client) ec_link;
170305706Sgonzo
171305706Sgonzo	struct input_event	ec_buffer[];
172305706Sgonzo};
173305706Sgonzo
174305706Sgonzo#define	EVDEV_CLIENT_LOCKQ(client)	mtx_lock(&(client)->ec_buffer_mtx)
175305706Sgonzo#define	EVDEV_CLIENT_UNLOCKQ(client)	mtx_unlock(&(client)->ec_buffer_mtx)
176305706Sgonzo#define EVDEV_CLIENT_LOCKQ_ASSERT(client) \
177305706Sgonzo    mtx_assert(&(client)->ec_buffer_mtx, MA_OWNED)
178305706Sgonzo#define	EVDEV_CLIENT_EMPTYQ(client) \
179305706Sgonzo    ((client)->ec_buffer_head == (client)->ec_buffer_ready)
180305706Sgonzo#define	EVDEV_CLIENT_SIZEQ(client) \
181305706Sgonzo    (((client)->ec_buffer_ready + (client)->ec_buffer_size - \
182305706Sgonzo      (client)->ec_buffer_head) % (client)->ec_buffer_size)
183305706Sgonzo
184305706Sgonzo/* Input device interface: */
185305706Sgonzovoid evdev_send_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
186305706Sgonzoint evdev_inject_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
187305706Sgonzoint evdev_cdev_create(struct evdev_dev *);
188305706Sgonzoint evdev_cdev_destroy(struct evdev_dev *);
189305706Sgonzobool evdev_event_supported(struct evdev_dev *, uint16_t);
190305706Sgonzovoid evdev_set_abs_bit(struct evdev_dev *, uint16_t);
191305706Sgonzovoid evdev_set_absinfo(struct evdev_dev *, uint16_t, struct input_absinfo *);
192305706Sgonzo
193305706Sgonzo/* Client interface: */
194305706Sgonzoint evdev_register_client(struct evdev_dev *, struct evdev_client *);
195305706Sgonzovoid evdev_dispose_client(struct evdev_dev *, struct evdev_client *);
196305706Sgonzoint evdev_grab_client(struct evdev_dev *, struct evdev_client *);
197305706Sgonzoint evdev_release_client(struct evdev_dev *, struct evdev_client *);
198305706Sgonzovoid evdev_client_push(struct evdev_client *, uint16_t, uint16_t, int32_t);
199305706Sgonzovoid evdev_notify_event(struct evdev_client *);
200305706Sgonzovoid evdev_revoke_client(struct evdev_client *);
201305706Sgonzo
202305706Sgonzo/* Multitouch related functions: */
203305706Sgonzovoid evdev_mt_init(struct evdev_dev *);
204305706Sgonzovoid evdev_mt_free(struct evdev_dev *);
205305706Sgonzoint32_t evdev_get_last_mt_slot(struct evdev_dev *);
206305706Sgonzovoid evdev_set_last_mt_slot(struct evdev_dev *, int32_t);
207305706Sgonzoint32_t evdev_get_mt_value(struct evdev_dev *, int32_t, int16_t);
208305706Sgonzovoid evdev_set_mt_value(struct evdev_dev *, int32_t, int16_t, int32_t);
209305706Sgonzovoid evdev_send_mt_compat(struct evdev_dev *);
210307777Sgonzovoid evdev_send_mt_autorel(struct evdev_dev *);
211305706Sgonzo
212305706Sgonzo/* Utility functions: */
213305706Sgonzovoid evdev_client_dumpqueue(struct evdev_client *);
214305706Sgonzo
215305706Sgonzo#endif	/* _DEV_EVDEV_EVDEV_PRIVATE_H */
216