evdev_private.h revision 305706
1305706Sgonzo/*-
2305706Sgonzo * Copyright (c) 2014 Jakub Wojciech Klama <jceel@FreeBSD.org>
3305706Sgonzo * Copyright (c) 2015-2016 Vladimir Kondratyev <wulf@cicgroup.ru>
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: head/sys/dev/evdev/evdev_private.h 305706 2016-09-11 18:56:38Z gonzo $
28305706Sgonzo */
29305706Sgonzo
30305706Sgonzo#ifndef	_DEV_EVDEV_EVDEV_PRIVATE_H
31305706Sgonzo#define	_DEV_EVDEV_EVDEV_PRIVATE_H
32305706Sgonzo
33305706Sgonzo#include <sys/bitstring.h>
34305706Sgonzo#include <sys/queue.h>
35305706Sgonzo#include <sys/malloc.h>
36305706Sgonzo#include <sys/kbio.h>
37305706Sgonzo#include <sys/selinfo.h>
38305706Sgonzo#include <dev/evdev/evdev.h>
39305706Sgonzo#include <dev/evdev/input.h>
40305706Sgonzo#include <dev/kbd/kbdreg.h>
41305706Sgonzo
42305706Sgonzo#define	NAMELEN		80
43305706Sgonzo
44305706Sgonzo/*
45305706Sgonzo * bitstr_t implementation must be identical to one found in EVIOCG*
46305706Sgonzo * libevdev ioctls. Our bitstring(3) API is compatible since r299090.
47305706Sgonzo */
48305706Sgonzo_Static_assert(sizeof(bitstr_t) == sizeof(unsigned long),
49305706Sgonzo    "bitstr_t size mismatch");
50305706Sgonzo
51305706SgonzoMALLOC_DECLARE(M_EVDEV);
52305706Sgonzo
53305706Sgonzostruct evdev_client;
54305706Sgonzostruct evdev_mt;
55305706Sgonzo
56305706Sgonzo#define	CURRENT_MT_SLOT(evdev)	((evdev)->ev_absinfo[ABS_MT_SLOT].value)
57305706Sgonzo#define	MAXIMAL_MT_SLOT(evdev)	((evdev)->ev_absinfo[ABS_MT_SLOT].maximum)
58305706Sgonzo
59305706Sgonzoenum evdev_key_events
60305706Sgonzo{
61305706Sgonzo	KEY_EVENT_UP,
62305706Sgonzo	KEY_EVENT_DOWN,
63305706Sgonzo	KEY_EVENT_REPEAT
64305706Sgonzo};
65305706Sgonzo
66305706Sgonzo/* evdev clock IDs in Linux semantic */
67305706Sgonzoenum evdev_clock_id
68305706Sgonzo{
69305706Sgonzo	EV_CLOCK_REALTIME = 0,	/* UTC clock */
70305706Sgonzo	EV_CLOCK_MONOTONIC,	/* monotonic, stops on suspend */
71305706Sgonzo	EV_CLOCK_BOOTTIME	/* monotonic, suspend-awared */
72305706Sgonzo};
73305706Sgonzo
74305706Sgonzostruct evdev_dev
75305706Sgonzo{
76305706Sgonzo	char			ev_name[NAMELEN];
77305706Sgonzo	char			ev_shortname[NAMELEN];
78305706Sgonzo	char			ev_serial[NAMELEN];
79305706Sgonzo	struct cdev *		ev_cdev;
80305706Sgonzo	int			ev_unit;
81305706Sgonzo	struct mtx		ev_mtx;
82305706Sgonzo	struct input_id		ev_id;
83305706Sgonzo	struct evdev_client *	ev_grabber;
84305706Sgonzo	size_t			ev_report_size;
85305706Sgonzo
86305706Sgonzo	/* Supported features: */
87305706Sgonzo	bitstr_t		bit_decl(ev_prop_flags, INPUT_PROP_CNT);
88305706Sgonzo	bitstr_t		bit_decl(ev_type_flags, EV_CNT);
89305706Sgonzo	bitstr_t		bit_decl(ev_key_flags, KEY_CNT);
90305706Sgonzo	bitstr_t		bit_decl(ev_rel_flags, REL_CNT);
91305706Sgonzo	bitstr_t		bit_decl(ev_abs_flags, ABS_CNT);
92305706Sgonzo	bitstr_t		bit_decl(ev_msc_flags, MSC_CNT);
93305706Sgonzo	bitstr_t		bit_decl(ev_led_flags, LED_CNT);
94305706Sgonzo	bitstr_t		bit_decl(ev_snd_flags, SND_CNT);
95305706Sgonzo	bitstr_t		bit_decl(ev_sw_flags, SW_CNT);
96305706Sgonzo	struct input_absinfo *	ev_absinfo;
97305706Sgonzo	bitstr_t		bit_decl(ev_flags, EVDEV_FLAG_CNT);
98305706Sgonzo
99305706Sgonzo	/* Repeat parameters & callout: */
100305706Sgonzo	int			ev_rep[REP_CNT];
101305706Sgonzo	struct callout		ev_rep_callout;
102305706Sgonzo	uint16_t		ev_rep_key;
103305706Sgonzo
104305706Sgonzo	/* State: */
105305706Sgonzo	bitstr_t		bit_decl(ev_key_states, KEY_CNT);
106305706Sgonzo	bitstr_t		bit_decl(ev_led_states, LED_CNT);
107305706Sgonzo	bitstr_t		bit_decl(ev_snd_states, SND_CNT);
108305706Sgonzo	bitstr_t		bit_decl(ev_sw_states, SW_CNT);
109305706Sgonzo	bool			ev_report_opened;
110305706Sgonzo
111305706Sgonzo	/* Multitouch protocol type B state: */
112305706Sgonzo	struct evdev_mt *	ev_mt;
113305706Sgonzo
114305706Sgonzo	/* Counters: */
115305706Sgonzo	uint64_t		ev_event_count;
116305706Sgonzo	uint64_t		ev_report_count;
117305706Sgonzo
118305706Sgonzo	/* Parent driver callbacks: */
119305706Sgonzo	struct evdev_methods *	ev_methods;
120305706Sgonzo	void *			ev_softc;
121305706Sgonzo
122305706Sgonzo	LIST_ENTRY(evdev_dev) ev_link;
123305706Sgonzo	LIST_HEAD(, evdev_client) ev_clients;
124305706Sgonzo};
125305706Sgonzo
126305706Sgonzo#define	EVDEV_LOCK(evdev)		mtx_lock(&(evdev)->ev_mtx)
127305706Sgonzo#define	EVDEV_UNLOCK(evdev)		mtx_unlock(&(evdev)->ev_mtx)
128305706Sgonzo#define	EVDEV_LOCK_ASSERT(evdev)	mtx_assert(&(evdev)->ev_mtx, MA_OWNED)
129305706Sgonzo
130305706Sgonzostruct evdev_client
131305706Sgonzo{
132305706Sgonzo	struct evdev_dev *	ec_evdev;
133305706Sgonzo	struct mtx		ec_buffer_mtx;
134305706Sgonzo	size_t			ec_buffer_size;
135305706Sgonzo	size_t			ec_buffer_head;
136305706Sgonzo	size_t			ec_buffer_tail;
137305706Sgonzo	size_t			ec_buffer_ready;
138305706Sgonzo	enum evdev_clock_id	ec_clock_id;
139305706Sgonzo	struct selinfo		ec_selp;
140305706Sgonzo	struct sigio *		ec_sigio;
141305706Sgonzo	bool			ec_async;
142305706Sgonzo	bool			ec_revoked;
143305706Sgonzo	bool			ec_blocked;
144305706Sgonzo	bool			ec_selected;
145305706Sgonzo
146305706Sgonzo	LIST_ENTRY(evdev_client) ec_link;
147305706Sgonzo
148305706Sgonzo	struct input_event	ec_buffer[];
149305706Sgonzo};
150305706Sgonzo
151305706Sgonzo#define	EVDEV_CLIENT_LOCKQ(client)	mtx_lock(&(client)->ec_buffer_mtx)
152305706Sgonzo#define	EVDEV_CLIENT_UNLOCKQ(client)	mtx_unlock(&(client)->ec_buffer_mtx)
153305706Sgonzo#define EVDEV_CLIENT_LOCKQ_ASSERT(client) \
154305706Sgonzo    mtx_assert(&(client)->ec_buffer_mtx, MA_OWNED)
155305706Sgonzo#define	EVDEV_CLIENT_EMPTYQ(client) \
156305706Sgonzo    ((client)->ec_buffer_head == (client)->ec_buffer_ready)
157305706Sgonzo#define	EVDEV_CLIENT_SIZEQ(client) \
158305706Sgonzo    (((client)->ec_buffer_ready + (client)->ec_buffer_size - \
159305706Sgonzo      (client)->ec_buffer_head) % (client)->ec_buffer_size)
160305706Sgonzo
161305706Sgonzo/* Input device interface: */
162305706Sgonzovoid evdev_send_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
163305706Sgonzoint evdev_inject_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
164305706Sgonzoint evdev_cdev_create(struct evdev_dev *);
165305706Sgonzoint evdev_cdev_destroy(struct evdev_dev *);
166305706Sgonzobool evdev_event_supported(struct evdev_dev *, uint16_t);
167305706Sgonzovoid evdev_set_abs_bit(struct evdev_dev *, uint16_t);
168305706Sgonzovoid evdev_set_absinfo(struct evdev_dev *, uint16_t, struct input_absinfo *);
169305706Sgonzo
170305706Sgonzo/* Client interface: */
171305706Sgonzoint evdev_register_client(struct evdev_dev *, struct evdev_client *);
172305706Sgonzovoid evdev_dispose_client(struct evdev_dev *, struct evdev_client *);
173305706Sgonzoint evdev_grab_client(struct evdev_dev *, struct evdev_client *);
174305706Sgonzoint evdev_release_client(struct evdev_dev *, struct evdev_client *);
175305706Sgonzovoid evdev_client_push(struct evdev_client *, uint16_t, uint16_t, int32_t);
176305706Sgonzovoid evdev_notify_event(struct evdev_client *);
177305706Sgonzovoid evdev_revoke_client(struct evdev_client *);
178305706Sgonzo
179305706Sgonzo/* Multitouch related functions: */
180305706Sgonzovoid evdev_mt_init(struct evdev_dev *);
181305706Sgonzovoid evdev_mt_free(struct evdev_dev *);
182305706Sgonzoint32_t evdev_get_last_mt_slot(struct evdev_dev *);
183305706Sgonzovoid evdev_set_last_mt_slot(struct evdev_dev *, int32_t);
184305706Sgonzoint32_t evdev_get_mt_value(struct evdev_dev *, int32_t, int16_t);
185305706Sgonzovoid evdev_set_mt_value(struct evdev_dev *, int32_t, int16_t, int32_t);
186305706Sgonzovoid evdev_send_mt_compat(struct evdev_dev *);
187305706Sgonzo
188305706Sgonzo/* Utility functions: */
189305706Sgonzovoid evdev_client_dumpqueue(struct evdev_client *);
190305706Sgonzo
191305706Sgonzo#endif	/* _DEV_EVDEV_EVDEV_PRIVATE_H */
192