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