xref: /dragonfly/sys/dev/misc/evdev/evdev.c (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 #include "opt_evdev.h"
31 
32 #include <sys/param.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
39 
40 #include <sys/devfs.h>  /* XXX detach driver when client is reading */
41 
42 /* Use FreeBSD's bitstring.h locally. */
43 #include "freebsd-bitstring.h"
44 
45 #include <dev/misc/evdev/evdev.h>
46 #include <dev/misc/evdev/evdev_private.h>
47 #include <dev/misc/evdev/input.h>
48 
49 #ifdef EVDEV_DEBUG
50 #define   debugf(evdev, fmt, args...)   kprintf("evdev: " fmt "\n", ##args)
51 #else
52 #define   debugf(evdev, fmt, args...)
53 #endif
54 
55 #ifdef FEATURE
56 FEATURE(evdev, "Input event devices support");
57 #ifdef EVDEV_SUPPORT
58 FEATURE(evdev_support, "Evdev support in hybrid drivers");
59 #endif
60 #endif
61 
62 enum evdev_sparse_result
63 {
64           EV_SKIP_EVENT,                /* Event value not changed */
65           EV_REPORT_EVENT,    /* Event value changed */
66           EV_REPORT_MT_SLOT,  /* Event value and MT slot number changed */
67 };
68 
69 MALLOC_DEFINE(M_EVDEV, "evdev", "evdev memory");
70 
71 /*
72  * By default, to avoid duplicate events (in particular for mouse movement),
73  * make evdev only collect events from actual hardware devices and not from
74  * sysmouse or kbdmux.
75  */
76 #if 0
77 int evdev_rcpt_mask = EVDEV_RCPT_SYSMOUSE | EVDEV_RCPT_KBDMUX;
78 #endif
79 int evdev_rcpt_mask = EVDEV_RCPT_HW_MOUSE | EVDEV_RCPT_HW_KBD;
80 int evdev_sysmouse_t_axis = 0;
81 
82 /*
83  * For the sake of clarity, evdev_rcpt_mask should be properly called
84  * evdev_sender_mask since the drivers (sysmouse, kbdmux, mouse and keyboard
85  * hardware drivers) send events *to* evdev, they do not receive events
86  * *from* evdev. Accordingly, we have changed the description of the sysctl
87  * to "Who sends events", but kept all the variable names so importing
88  * future changes is easier.
89  */
90 
91 SYSCTL_NODE(_kern, OID_AUTO, evdev, CTLFLAG_RW, 0, "Evdev args");
92 #ifdef EVDEV_SUPPORT
93 SYSCTL_INT(_kern_evdev, OID_AUTO, rcpt_mask, CTLFLAG_RW, &evdev_rcpt_mask, 0,
94     "Who sends events: bit0 - sysmouse, bit1 - kbdmux, "
95     "bit2 - mouse hardware, bit3 - keyboard hardware");
96 SYSCTL_INT(_kern_evdev, OID_AUTO, sysmouse_t_axis, CTLFLAG_RW,
97     &evdev_sysmouse_t_axis, 0, "Extract T-axis from 0-none, 1-ums, 2-psm");
98 #endif
99 SYSCTL_NODE(_kern_evdev, OID_AUTO, input, CTLFLAG_RD, 0,
100     "Evdev input devices");
101 
102 static void evdev_start_repeat(struct evdev_dev *, uint16_t);
103 static void evdev_stop_repeat(struct evdev_dev *);
104 static int evdev_check_event(struct evdev_dev *, uint16_t, uint16_t, int32_t);
105 
106 struct evdev_dev *
evdev_alloc(void)107 evdev_alloc(void)
108 {
109 
110           return kmalloc(sizeof(struct evdev_dev), M_EVDEV, M_WAITOK | M_ZERO);
111 }
112 
113 void
evdev_free(struct evdev_dev * evdev)114 evdev_free(struct evdev_dev *evdev)
115 {
116           if (evdev) {
117                     if (evdev->ev_cdev != NULL && evdev->ev_cdev->si_drv1 != NULL)
118                               evdev_unregister(evdev);
119                     kfree(evdev, M_EVDEV);
120           }
121 }
122 
123 static struct input_absinfo *
evdev_alloc_absinfo(void)124 evdev_alloc_absinfo(void)
125 {
126 
127           return (kmalloc(sizeof(struct input_absinfo) * ABS_CNT, M_EVDEV,
128               M_WAITOK | M_ZERO));
129 }
130 
131 static void
evdev_free_absinfo(struct input_absinfo * absinfo)132 evdev_free_absinfo(struct input_absinfo *absinfo)
133 {
134 
135           kfree(absinfo, M_EVDEV);
136 }
137 
138 int
evdev_set_report_size(struct evdev_dev * evdev,size_t report_size)139 evdev_set_report_size(struct evdev_dev *evdev, size_t report_size)
140 {
141           if (report_size > KEY_CNT + REL_CNT + ABS_CNT + MAX_MT_SLOTS * MT_CNT +
142               MSC_CNT + LED_CNT + SND_CNT + SW_CNT + FF_CNT)
143                     return (EINVAL);
144 
145           evdev->ev_report_size = report_size;
146           return (0);
147 }
148 
149 static size_t
evdev_estimate_report_size(struct evdev_dev * evdev)150 evdev_estimate_report_size(struct evdev_dev *evdev)
151 {
152           size_t size = 0;
153           int res;
154 
155           /*
156            * Keyboards generate one event per report but other devices with
157            * buttons like mouses can report events simultaneously
158            */
159           bit_ffs_at(evdev->ev_key_flags, KEY_OK, KEY_CNT - KEY_OK, &res);
160           if (res == -1)
161                     bit_ffs(evdev->ev_key_flags, BTN_MISC, &res);
162           size += (res != -1);
163           bit_count(evdev->ev_key_flags, BTN_MISC, KEY_OK - BTN_MISC, &res);
164           size += res;
165 
166           /* All relative axes can be reported simultaneously */
167           bit_count(evdev->ev_rel_flags, 0, REL_CNT, &res);
168           size += res;
169 
170           /*
171            * All absolute axes can be reported simultaneously.
172            * Multitouch axes can be reported ABS_MT_SLOT times
173            */
174           if (evdev->ev_absinfo != NULL) {
175                     bit_count(evdev->ev_abs_flags, 0, ABS_CNT, &res);
176                     size += res;
177                     bit_count(evdev->ev_abs_flags, ABS_MT_FIRST, MT_CNT, &res);
178                     if (res > 0) {
179                               res++;    /* ABS_MT_SLOT or SYN_MT_REPORT */
180                               if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
181                                         /* MT type B */
182                                         size += res * MAXIMAL_MT_SLOT(evdev);
183                               else
184                                         /* MT type A */
185                                         size += res * (MAX_MT_REPORTS - 1);
186                     }
187           }
188 
189           /* All misc events can be reported simultaneously */
190           bit_count(evdev->ev_msc_flags, 0, MSC_CNT, &res);
191           size += res;
192 
193           /* All leds can be reported simultaneously */
194           bit_count(evdev->ev_led_flags, 0, LED_CNT, &res);
195           size += res;
196 
197           /* Assume other events are generated once per report */
198           bit_ffs(evdev->ev_snd_flags, SND_CNT, &res);
199           size += (res != -1);
200 
201           bit_ffs(evdev->ev_sw_flags, SW_CNT, &res);
202           size += (res != -1);
203 
204           /* XXX: FF part is not implemented yet */
205 
206           size++;             /* SYN_REPORT */
207           return (size);
208 }
209 
210 static void
evdev_sysctl_create(struct evdev_dev * evdev)211 evdev_sysctl_create(struct evdev_dev *evdev)
212 {
213           struct sysctl_oid *ev_sysctl_tree;
214           char ev_unit_str[8];
215 
216           ksnprintf(ev_unit_str, sizeof(ev_unit_str), "%d", evdev->ev_unit);
217           sysctl_ctx_init(&evdev->ev_sysctl_ctx);
218 
219           ev_sysctl_tree = SYSCTL_ADD_NODE(&evdev->ev_sysctl_ctx,
220               SYSCTL_STATIC_CHILDREN(_kern_evdev_input), OID_AUTO,
221               ev_unit_str, CTLFLAG_RD, NULL, "device index");
222 
223           SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
224               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "name", CTLFLAG_RD,
225               evdev->ev_name, 0,
226               "Input device name");
227 
228           SYSCTL_ADD_STRUCT(&evdev->ev_sysctl_ctx,
229               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "id", CTLFLAG_RD,
230               &evdev->ev_id, input_id,
231               "Input device identification");
232 
233           /* ioctl returns ENOENT if phys is not set. sysctl returns "" here */
234           SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
235               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "phys", CTLFLAG_RD,
236               evdev->ev_shortname, 0,
237               "Input device short name");
238 
239           /* ioctl returns ENOENT if uniq is not set. sysctl returns "" here */
240           SYSCTL_ADD_STRING(&evdev->ev_sysctl_ctx,
241               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "uniq", CTLFLAG_RD,
242               evdev->ev_serial, 0,
243               "Input device unique number");
244 
245           SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
246               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "props", CTLFLAG_RD,
247               evdev->ev_prop_flags, sizeof(evdev->ev_prop_flags), "",
248               "Input device properties");
249 
250           SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
251               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "type_bits", CTLFLAG_RD,
252               evdev->ev_type_flags, sizeof(evdev->ev_type_flags), "",
253               "Input device supported events types");
254 
255           SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
256               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "key_bits", CTLFLAG_RD,
257               evdev->ev_key_flags, sizeof(evdev->ev_key_flags),
258               "", "Input device supported keys");
259 
260           SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
261               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "rel_bits", CTLFLAG_RD,
262               evdev->ev_rel_flags, sizeof(evdev->ev_rel_flags), "",
263               "Input device supported relative events");
264 
265           SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
266               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "abs_bits", CTLFLAG_RD,
267               evdev->ev_abs_flags, sizeof(evdev->ev_abs_flags), "",
268               "Input device supported absolute events");
269 
270           SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
271               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "msc_bits", CTLFLAG_RD,
272               evdev->ev_msc_flags, sizeof(evdev->ev_msc_flags), "",
273               "Input device supported miscellaneous events");
274 
275           SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
276               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "led_bits", CTLFLAG_RD,
277               evdev->ev_led_flags, sizeof(evdev->ev_led_flags), "",
278               "Input device supported LED events");
279 
280           SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
281               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "snd_bits", CTLFLAG_RD,
282               evdev->ev_snd_flags, sizeof(evdev->ev_snd_flags), "",
283               "Input device supported sound events");
284 
285           SYSCTL_ADD_OPAQUE(&evdev->ev_sysctl_ctx,
286               SYSCTL_CHILDREN(ev_sysctl_tree), OID_AUTO, "sw_bits", CTLFLAG_RD,
287               evdev->ev_sw_flags, sizeof(evdev->ev_sw_flags), "",
288               "Input device supported switch events");
289 }
290 
291 static int
evdev_register_common(struct evdev_dev * evdev)292 evdev_register_common(struct evdev_dev *evdev)
293 {
294           int ret;
295 
296           debugf(evdev, "%s: registered evdev provider: %s <%s>\n",
297               evdev->ev_shortname, evdev->ev_name, evdev->ev_serial);
298 
299           /* Initialize internal structures */
300           LIST_INIT(&evdev->ev_clients);
301 
302           if (evdev_event_supported(evdev, EV_REP) &&
303               bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
304                     /* Initialize callout */
305                     callout_init_lk(&evdev->ev_rep_callout, evdev->ev_state_lock);
306 
307                     if (evdev->ev_rep[REP_DELAY] == 0 &&
308                         evdev->ev_rep[REP_PERIOD] == 0) {
309                               /* Supply default values */
310                               evdev->ev_rep[REP_DELAY] = 250;
311                               evdev->ev_rep[REP_PERIOD] = 33;
312                     }
313           }
314 
315           /* Initialize multitouch protocol type B states or A to B converter */
316           if (bit_test(evdev->ev_abs_flags, ABS_MT_SLOT) ||
317               bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK))
318                     evdev_mt_init(evdev);
319 
320           /* Estimate maximum report size */
321           if (evdev->ev_report_size == 0) {
322                     ret = evdev_set_report_size(evdev,
323                         evdev_estimate_report_size(evdev));
324                     if (ret != 0)
325                               goto bail_out;
326           }
327 
328           /* Create char device node */
329           ret = evdev_cdev_create(evdev);
330           if (ret != 0)
331                     goto bail_out;
332 
333           /* Create sysctls (for device enumeration without /dev/input access rights) */
334           evdev_sysctl_create(evdev);
335 
336 bail_out:
337           return (ret);
338 }
339 
340 int
evdev_register(struct evdev_dev * evdev)341 evdev_register(struct evdev_dev *evdev)
342 {
343           int ret;
344 
345           evdev->ev_lock_type = EV_LOCK_INTERNAL;
346           evdev->ev_state_lock = &evdev->ev_mtx;
347           lockinit(&evdev->ev_mtx, "evmtx", 0, 0);
348 
349           ret = evdev_register_common(evdev);
350           if (ret != 0)
351                     lockuninit(&evdev->ev_mtx);
352 
353           return (ret);
354 }
355 
356 int
evdev_register_mtx(struct evdev_dev * evdev,struct lock * mtx)357 evdev_register_mtx(struct evdev_dev *evdev, struct lock *mtx)
358 {
359 
360           evdev->ev_lock_type = EV_LOCK_MTX;
361           evdev->ev_state_lock = mtx;
362           return (evdev_register_common(evdev));
363 }
364 
365 int
evdev_unregister(struct evdev_dev * evdev)366 evdev_unregister(struct evdev_dev *evdev)
367 {
368           struct evdev_client *client, *tmp;
369           int ret;
370           debugf(evdev, "%s: unregistered evdev provider: %s\n",
371               evdev->ev_shortname, evdev->ev_name);
372 
373           sysctl_ctx_free(&evdev->ev_sysctl_ctx);
374 
375           EVDEV_LOCK(evdev);
376           evdev->ev_cdev->si_drv1 = NULL;
377           /* Wake up sleepers */
378           LIST_FOREACH_MUTABLE(client, &evdev->ev_clients, ec_link, tmp) {
379                     evdev_revoke_client(client);
380                     evdev_dispose_client(evdev, client);
381                     EVDEV_CLIENT_LOCKQ(client);
382                     evdev_notify_event(client);
383                     EVDEV_CLIENT_UNLOCKQ(client);
384                     if (evdev->ev_cdev) {
385                               devfs_assume_knotes(evdev->ev_cdev, &client->kqinfo);
386                     }
387           }
388           EVDEV_UNLOCK(evdev);
389 
390           ret = evdev_cdev_destroy(evdev);
391           evdev->ev_cdev = NULL;
392           if (ret == 0 && evdev->ev_lock_type == EV_LOCK_INTERNAL) {
393                     lockuninit(&evdev->ev_mtx);
394           }
395 
396           /*
397            * For some devices, e.g. keyboards, ev_absinfo and ev_mt
398            * may be NULL, so check before freeing them.
399            */
400           if (evdev->ev_absinfo != NULL)
401               evdev_free_absinfo(evdev->ev_absinfo);
402           if (evdev->ev_mt != NULL)
403               evdev_mt_free(evdev);
404 
405           return (ret);
406 }
407 
408 inline void
evdev_set_name(struct evdev_dev * evdev,const char * name)409 evdev_set_name(struct evdev_dev *evdev, const char *name)
410 {
411 
412           ksnprintf(evdev->ev_name, NAMELEN, "%s", name);
413 }
414 
415 inline void
evdev_set_id(struct evdev_dev * evdev,uint16_t bustype,uint16_t vendor,uint16_t product,uint16_t version)416 evdev_set_id(struct evdev_dev *evdev, uint16_t bustype, uint16_t vendor,
417     uint16_t product, uint16_t version)
418 {
419 
420           evdev->ev_id = (struct input_id) {
421                     .bustype = bustype,
422                     .vendor = vendor,
423                     .product = product,
424                     .version = version
425           };
426 }
427 
428 inline void
evdev_set_phys(struct evdev_dev * evdev,const char * name)429 evdev_set_phys(struct evdev_dev *evdev, const char *name)
430 {
431 
432           ksnprintf(evdev->ev_shortname, NAMELEN, "%s", name);
433 }
434 
435 inline void
evdev_set_serial(struct evdev_dev * evdev,const char * serial)436 evdev_set_serial(struct evdev_dev *evdev, const char *serial)
437 {
438 
439           ksnprintf(evdev->ev_serial, NAMELEN, "%s", serial);
440 }
441 
442 inline void
evdev_set_methods(struct evdev_dev * evdev,void * softc,const struct evdev_methods * methods)443 evdev_set_methods(struct evdev_dev *evdev, void *softc,
444     const struct evdev_methods *methods)
445 {
446 
447           evdev->ev_methods = methods;
448           evdev->ev_softc = softc;
449 }
450 
451 inline void *
evdev_get_softc(struct evdev_dev * evdev)452 evdev_get_softc(struct evdev_dev *evdev)
453 {
454 
455           return (evdev->ev_softc);
456 }
457 
458 inline void
evdev_support_prop(struct evdev_dev * evdev,uint16_t prop)459 evdev_support_prop(struct evdev_dev *evdev, uint16_t prop)
460 {
461 
462           KASSERT(prop < INPUT_PROP_CNT, ("invalid evdev input property"));
463           bit_set(evdev->ev_prop_flags, prop);
464 }
465 
466 inline void
evdev_support_event(struct evdev_dev * evdev,uint16_t type)467 evdev_support_event(struct evdev_dev *evdev, uint16_t type)
468 {
469 
470           KASSERT(type < EV_CNT, ("invalid evdev event property"));
471           bit_set(evdev->ev_type_flags, type);
472 }
473 
474 inline void
evdev_support_key(struct evdev_dev * evdev,uint16_t code)475 evdev_support_key(struct evdev_dev *evdev, uint16_t code)
476 {
477 
478           KASSERT(code < KEY_CNT, ("invalid evdev key property"));
479           bit_set(evdev->ev_key_flags, code);
480 }
481 
482 inline void
evdev_support_rel(struct evdev_dev * evdev,uint16_t code)483 evdev_support_rel(struct evdev_dev *evdev, uint16_t code)
484 {
485 
486           KASSERT(code < REL_CNT, ("invalid evdev rel property"));
487           bit_set(evdev->ev_rel_flags, code);
488 }
489 
490 inline void
evdev_support_abs(struct evdev_dev * evdev,uint16_t code,int32_t minimum,int32_t maximum,int32_t fuzz,int32_t flat,int32_t resolution)491 evdev_support_abs(struct evdev_dev *evdev, uint16_t code, int32_t minimum,
492     int32_t maximum, int32_t fuzz, int32_t flat, int32_t resolution)
493 {
494           struct input_absinfo absinfo;
495 
496           KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
497 
498           absinfo = (struct input_absinfo) {
499                     .value = 0,
500                     .minimum = minimum,
501                     .maximum = maximum,
502                     .fuzz = fuzz,
503                     .flat = flat,
504                     .resolution = resolution,
505           };
506           evdev_set_abs_bit(evdev, code);
507           evdev_set_absinfo(evdev, code, &absinfo);
508 }
509 
510 inline void
evdev_set_abs_bit(struct evdev_dev * evdev,uint16_t code)511 evdev_set_abs_bit(struct evdev_dev *evdev, uint16_t code)
512 {
513 
514           KASSERT(code < ABS_CNT, ("invalid evdev abs property"));
515           if (evdev->ev_absinfo == NULL)
516                     evdev->ev_absinfo = evdev_alloc_absinfo();
517           bit_set(evdev->ev_abs_flags, code);
518 }
519 
520 inline void
evdev_support_msc(struct evdev_dev * evdev,uint16_t code)521 evdev_support_msc(struct evdev_dev *evdev, uint16_t code)
522 {
523 
524           KASSERT(code < MSC_CNT, ("invalid evdev msc property"));
525           bit_set(evdev->ev_msc_flags, code);
526 }
527 
528 
529 inline void
evdev_support_led(struct evdev_dev * evdev,uint16_t code)530 evdev_support_led(struct evdev_dev *evdev, uint16_t code)
531 {
532 
533           KASSERT(code < LED_CNT, ("invalid evdev led property"));
534           bit_set(evdev->ev_led_flags, code);
535 }
536 
537 inline void
evdev_support_snd(struct evdev_dev * evdev,uint16_t code)538 evdev_support_snd(struct evdev_dev *evdev, uint16_t code)
539 {
540 
541           KASSERT(code < SND_CNT, ("invalid evdev snd property"));
542           bit_set(evdev->ev_snd_flags, code);
543 }
544 
545 inline void
evdev_support_sw(struct evdev_dev * evdev,uint16_t code)546 evdev_support_sw(struct evdev_dev *evdev, uint16_t code)
547 {
548 
549           KASSERT(code < SW_CNT, ("invalid evdev sw property"));
550           bit_set(evdev->ev_sw_flags, code);
551 }
552 
553 bool
evdev_event_supported(struct evdev_dev * evdev,uint16_t type)554 evdev_event_supported(struct evdev_dev *evdev, uint16_t type)
555 {
556 
557           KASSERT(type < EV_CNT, ("invalid evdev event property"));
558           return (bit_test(evdev->ev_type_flags, type));
559 }
560 
561 inline void
evdev_set_absinfo(struct evdev_dev * evdev,uint16_t axis,struct input_absinfo * absinfo)562 evdev_set_absinfo(struct evdev_dev *evdev, uint16_t axis,
563     struct input_absinfo *absinfo)
564 {
565 
566           KASSERT(axis < ABS_CNT, ("invalid evdev abs property"));
567 
568           if (axis == ABS_MT_SLOT &&
569               (absinfo->maximum < 1 || absinfo->maximum >= MAX_MT_SLOTS))
570                     return;
571 
572           if (evdev->ev_absinfo == NULL)
573                     evdev->ev_absinfo = evdev_alloc_absinfo();
574 
575           if (axis == ABS_MT_SLOT)
576                     evdev->ev_absinfo[ABS_MT_SLOT].maximum = absinfo->maximum;
577           else
578                     memcpy(&evdev->ev_absinfo[axis], absinfo,
579                         sizeof(struct input_absinfo));
580 }
581 
582 inline void
evdev_set_repeat_params(struct evdev_dev * evdev,uint16_t property,int value)583 evdev_set_repeat_params(struct evdev_dev *evdev, uint16_t property, int value)
584 {
585 
586           KASSERT(property < REP_CNT, ("invalid evdev repeat property"));
587           evdev->ev_rep[property] = value;
588 }
589 
590 inline void
evdev_set_flag(struct evdev_dev * evdev,uint16_t flag)591 evdev_set_flag(struct evdev_dev *evdev, uint16_t flag)
592 {
593 
594           KASSERT(flag < EVDEV_FLAG_CNT, ("invalid evdev flag property"));
595           bit_set(evdev->ev_flags, flag);
596 }
597 
598 static int
evdev_check_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)599 evdev_check_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
600     int32_t value)
601 {
602 
603           if (type >= EV_CNT)
604                     return (EINVAL);
605 
606           /* Allow SYN events implicitly */
607           if (type != EV_SYN && !evdev_event_supported(evdev, type))
608                     return (EINVAL);
609 
610           switch (type) {
611           case EV_SYN:
612                     if (code >= SYN_CNT)
613                               return (EINVAL);
614                     break;
615 
616           case EV_KEY:
617                     if (code >= KEY_CNT)
618                               return (EINVAL);
619                     if (!bit_test(evdev->ev_key_flags, code))
620                               return (EINVAL);
621                     break;
622 
623           case EV_REL:
624                     if (code >= REL_CNT)
625                               return (EINVAL);
626                     if (!bit_test(evdev->ev_rel_flags, code))
627                               return (EINVAL);
628                     break;
629 
630           case EV_ABS:
631                     if (code >= ABS_CNT)
632                               return (EINVAL);
633                     if (!bit_test(evdev->ev_abs_flags, code))
634                               return (EINVAL);
635                     if (code == ABS_MT_SLOT &&
636                         (value < 0 || value > MAXIMAL_MT_SLOT(evdev)))
637                               return (EINVAL);
638                     if (ABS_IS_MT(code) && evdev->ev_mt == NULL &&
639                         bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
640                               return (EINVAL);
641                     break;
642 
643           case EV_MSC:
644                     if (code >= MSC_CNT)
645                               return (EINVAL);
646                     if (!bit_test(evdev->ev_msc_flags, code))
647                               return (EINVAL);
648                     break;
649 
650           case EV_LED:
651                     if (code >= LED_CNT)
652                               return (EINVAL);
653                     if (!bit_test(evdev->ev_led_flags, code))
654                               return (EINVAL);
655                     break;
656 
657           case EV_SND:
658                     if (code >= SND_CNT)
659                               return (EINVAL);
660                     if (!bit_test(evdev->ev_snd_flags, code))
661                               return (EINVAL);
662                     break;
663 
664           case EV_SW:
665                     if (code >= SW_CNT)
666                               return (EINVAL);
667                     if (!bit_test(evdev->ev_sw_flags, code))
668                               return (EINVAL);
669                     break;
670 
671           case EV_REP:
672                     if (code >= REP_CNT)
673                               return (EINVAL);
674                     break;
675 
676           default:
677                     return (EINVAL);
678           }
679 
680           return (0);
681 }
682 
683 static void
evdev_modify_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t * value)684 evdev_modify_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
685     int32_t *value)
686 {
687           int32_t fuzz, old_value, abs_change;
688 
689           EVDEV_LOCK_ASSERT(evdev);
690 
691           switch (type) {
692           case EV_KEY:
693                     if (!evdev_event_supported(evdev, EV_REP))
694                               break;
695 
696                     if (!bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT)) {
697                               /* Detect driver key repeats. */
698                               if (bit_test(evdev->ev_key_states, code) &&
699                                   *value == KEY_EVENT_DOWN)
700                                         *value = KEY_EVENT_REPEAT;
701                     } else {
702                               /* Start/stop callout for evdev repeats */
703                               if (bit_test(evdev->ev_key_states, code) == !*value &&
704                                   !LIST_EMPTY(&evdev->ev_clients)) {
705                                         if (*value == KEY_EVENT_DOWN)
706                                                   evdev_start_repeat(evdev, code);
707                                         else
708                                                   evdev_stop_repeat(evdev);
709                               }
710                     }
711                     break;
712 
713           case EV_ABS:
714                     if (code == ABS_MT_SLOT)
715                               break;
716                     else if (!ABS_IS_MT(code))
717                               old_value = evdev->ev_absinfo[code].value;
718                     else if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
719                               /* Pass MT protocol type A events as is */
720                               break;
721                     else if (code == ABS_MT_TRACKING_ID) {
722                               *value = evdev_mt_reassign_id(evdev,
723                                   evdev_mt_get_last_slot(evdev), *value);
724                               break;
725                     } else
726                               old_value = evdev_mt_get_value(evdev,
727                                   evdev_mt_get_last_slot(evdev), code);
728 
729                     fuzz = evdev->ev_absinfo[code].fuzz;
730                     if (fuzz == 0)
731                               break;
732 
733                     abs_change = abs(*value - old_value);
734                     if (abs_change < fuzz / 2)
735                               *value = old_value;
736                     else if (abs_change < fuzz)
737                               *value = (old_value * 3 + *value) / 4;
738                     else if (abs_change < fuzz * 2)
739                               *value = (old_value + *value) / 2;
740                     break;
741           }
742 }
743 
744 static enum evdev_sparse_result
evdev_sparse_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)745 evdev_sparse_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
746     int32_t value)
747 {
748           int32_t last_mt_slot;
749 
750           EVDEV_LOCK_ASSERT(evdev);
751 
752           /*
753            * For certain event types, update device state bits
754            * and convert level reporting to edge reporting
755            */
756           switch (type) {
757           case EV_KEY:
758                     switch (value) {
759                     case KEY_EVENT_UP:
760                     case KEY_EVENT_DOWN:
761                               if (bit_test(evdev->ev_key_states, code) == value)
762                                         return (EV_SKIP_EVENT);
763                               bit_change(evdev->ev_key_states, code, value);
764                               break;
765 
766                     case KEY_EVENT_REPEAT:
767                               if (bit_test(evdev->ev_key_states, code) == 0 ||
768                                   !evdev_event_supported(evdev, EV_REP))
769                                         return (EV_SKIP_EVENT);
770                               break;
771 
772                     default:
773                                return (EV_SKIP_EVENT);
774                     }
775                     break;
776 
777           case EV_LED:
778                     if (bit_test(evdev->ev_led_states, code) == value)
779                               return (EV_SKIP_EVENT);
780                     bit_change(evdev->ev_led_states, code, value);
781                     break;
782 
783           case EV_SND:
784                     bit_change(evdev->ev_snd_states, code, value);
785                     break;
786 
787           case EV_SW:
788                     if (bit_test(evdev->ev_sw_states, code) == value)
789                               return (EV_SKIP_EVENT);
790                     bit_change(evdev->ev_sw_states, code, value);
791                     break;
792 
793           case EV_REP:
794                     if (evdev->ev_rep[code] == value)
795                               return (EV_SKIP_EVENT);
796                     evdev_set_repeat_params(evdev, code, value);
797                     break;
798 
799           case EV_REL:
800                     if (value == 0)
801                               return (EV_SKIP_EVENT);
802                     break;
803 
804           /* For EV_ABS, save last value in absinfo and ev_mt_states */
805           case EV_ABS:
806                     switch (code) {
807                     case ABS_MT_SLOT:
808                               /* Postpone ABS_MT_SLOT till next event */
809                               evdev_mt_set_last_slot(evdev, value);
810                               return (EV_SKIP_EVENT);
811 
812                     case ABS_MT_FIRST ... ABS_MT_LAST:
813                               /* Pass MT protocol type A events as is */
814                               if (!bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
815                                         break;
816                               /* Don`t repeat MT protocol type B events */
817                               last_mt_slot = evdev_mt_get_last_slot(evdev);
818                               if (evdev_mt_get_value(evdev, last_mt_slot, code)
819                                    == value)
820                                         return (EV_SKIP_EVENT);
821                               evdev_mt_set_value(evdev, last_mt_slot, code, value);
822                               if (last_mt_slot != CURRENT_MT_SLOT(evdev)) {
823                                         CURRENT_MT_SLOT(evdev) = last_mt_slot;
824                                         evdev->ev_report_opened = true;
825                                         return (EV_REPORT_MT_SLOT);
826                               }
827                               break;
828 
829                     default:
830                               if (evdev->ev_absinfo[code].value == value)
831                                         return (EV_SKIP_EVENT);
832                               evdev->ev_absinfo[code].value = value;
833                     }
834                     break;
835 
836           case EV_SYN:
837                     if (code == SYN_REPORT) {
838                               /* Count empty reports as well as non empty */
839                               evdev->ev_report_count++;
840                               /* Skip empty reports */
841                               if (!evdev->ev_report_opened)
842                                         return (EV_SKIP_EVENT);
843                               evdev->ev_report_opened = false;
844                               return (EV_REPORT_EVENT);
845                     }
846                     break;
847           }
848 
849           evdev->ev_report_opened = true;
850           return (EV_REPORT_EVENT);
851 }
852 
853 static void
evdev_propagate_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)854 evdev_propagate_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
855     int32_t value)
856 {
857           struct evdev_client *client;
858 
859           debugf(evdev, "%s pushed event %d/%d/%d",
860               evdev->ev_shortname, type, code, value);
861 
862           EVDEV_LOCK_ASSERT(evdev);
863 
864           /* Propagate event through all clients */
865           LIST_FOREACH(client, &evdev->ev_clients, ec_link) {
866                     if (evdev->ev_grabber != NULL && evdev->ev_grabber != client)
867                               continue;
868 
869                     EVDEV_CLIENT_LOCKQ(client);
870                     evdev_client_push(client, type, code, value);
871                     if (type == EV_SYN && code == SYN_REPORT)
872                               evdev_notify_event(client);
873                     EVDEV_CLIENT_UNLOCKQ(client);
874           }
875 
876           evdev->ev_event_count++;
877 }
878 
879 void
evdev_send_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)880 evdev_send_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
881     int32_t value)
882 {
883           enum evdev_sparse_result sparse;
884 
885           EVDEV_LOCK_ASSERT(evdev);
886 
887           evdev_modify_event(evdev, type, code, &value);
888           sparse =  evdev_sparse_event(evdev, type, code, value);
889           switch (sparse) {
890           case EV_REPORT_MT_SLOT:
891                     /* report postponed ABS_MT_SLOT */
892                     evdev_propagate_event(evdev, EV_ABS, ABS_MT_SLOT,
893                         CURRENT_MT_SLOT(evdev));
894                     /* FALLTHROUGH */
895           case EV_REPORT_EVENT:
896                     evdev_propagate_event(evdev, type, code, value);
897                     /* FALLTHROUGH */
898           case EV_SKIP_EVENT:
899                     break;
900           }
901 }
902 
903 int
evdev_push_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)904 evdev_push_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
905     int32_t value)
906 {
907 
908           if (evdev_check_event(evdev, type, code, value) != 0)
909                     return (EINVAL);
910 
911           EVDEV_ENTER(evdev);
912 
913           if (type == EV_SYN && code == SYN_REPORT &&
914               bit_test(evdev->ev_abs_flags, ABS_MT_SLOT))
915                     evdev_mt_sync_frame(evdev);
916           else
917                     if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK) &&
918                         evdev_mt_record_event(evdev, type, code, value))
919                               goto exit;
920 
921           evdev_send_event(evdev, type, code, value);
922 exit:
923           EVDEV_EXIT(evdev);
924 
925           return (0);
926 }
927 
928 int
evdev_inject_event(struct evdev_dev * evdev,uint16_t type,uint16_t code,int32_t value)929 evdev_inject_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
930     int32_t value)
931 {
932           int ret = 0;
933 
934           switch (type) {
935           case EV_REP:
936                     /* evdev repeats should not be processed by hardware driver */
937                     if (bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
938                               goto push;
939                     /* FALLTHROUGH */
940           case EV_LED:
941           case EV_MSC:
942           case EV_SND:
943           case EV_FF:
944                     if (evdev->ev_methods != NULL &&
945                         evdev->ev_methods->ev_event != NULL)
946                               evdev->ev_methods->ev_event(evdev,
947                                   type, code, value);
948                     /*
949                      * Leds and driver repeats should be reported in ev_event
950                      * method body to interoperate with kbdmux states and rates
951                      * propagation so both ways (ioctl and evdev) of changing it
952                      * will produce only one evdev event report to client.
953                      */
954                     if (type == EV_LED || type == EV_REP)
955                               break;
956                     /* FALLTHROUGH */
957           case EV_SYN:
958           case EV_KEY:
959           case EV_REL:
960           case EV_ABS:
961           case EV_SW:
962 push:
963                     if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
964                               EVDEV_LOCK(evdev);
965                     ret = evdev_push_event(evdev, type,  code, value);
966                     if (evdev->ev_lock_type != EV_LOCK_INTERNAL)
967                               EVDEV_UNLOCK(evdev);
968                     break;
969 
970           default:
971                     ret = EINVAL;
972           }
973 
974           return (ret);
975 }
976 
977 int
evdev_register_client(struct evdev_dev * evdev,struct evdev_client * client)978 evdev_register_client(struct evdev_dev *evdev, struct evdev_client *client)
979 {
980           int ret = 0;
981 
982           debugf(evdev, "adding new client for device %s", evdev->ev_shortname);
983 
984           EVDEV_LOCK_ASSERT(evdev);
985 
986           if (LIST_EMPTY(&evdev->ev_clients) && evdev->ev_methods != NULL &&
987               evdev->ev_methods->ev_open != NULL) {
988                     debugf(evdev, "calling ev_open() on device %s",
989                         evdev->ev_shortname);
990                     ret = evdev->ev_methods->ev_open(evdev);
991           }
992           if (ret == 0)
993                     LIST_INSERT_HEAD(&evdev->ev_clients, client, ec_link);
994           return (ret);
995 }
996 
997 void
evdev_dispose_client(struct evdev_dev * evdev,struct evdev_client * client)998 evdev_dispose_client(struct evdev_dev *evdev, struct evdev_client *client)
999 {
1000           debugf(evdev, "removing client for device %s", evdev->ev_shortname);
1001 
1002           EVDEV_LOCK_ASSERT(evdev);
1003 
1004           LIST_REMOVE(client, ec_link);
1005           if (LIST_EMPTY(&evdev->ev_clients)) {
1006                     if (evdev->ev_methods != NULL &&
1007                         evdev->ev_methods->ev_close != NULL)
1008                               (void)evdev->ev_methods->ev_close(evdev);
1009                     if (evdev_event_supported(evdev, EV_REP) &&
1010                         bit_test(evdev->ev_flags, EVDEV_FLAG_SOFTREPEAT))
1011                               evdev_stop_repeat(evdev);
1012           }
1013           evdev_release_client(evdev, client);
1014 }
1015 
1016 int
evdev_grab_client(struct evdev_dev * evdev,struct evdev_client * client)1017 evdev_grab_client(struct evdev_dev *evdev, struct evdev_client *client)
1018 {
1019 
1020           EVDEV_LOCK_ASSERT(evdev);
1021 
1022           if (evdev->ev_grabber != NULL)
1023                     return (EBUSY);
1024 
1025           evdev->ev_grabber = client;
1026 
1027           return (0);
1028 }
1029 
1030 int
evdev_release_client(struct evdev_dev * evdev,struct evdev_client * client)1031 evdev_release_client(struct evdev_dev *evdev, struct evdev_client *client)
1032 {
1033 
1034           EVDEV_LOCK_ASSERT(evdev);
1035 
1036           if (evdev->ev_grabber != client)
1037                     return (EINVAL);
1038 
1039           evdev->ev_grabber = NULL;
1040 
1041           return (0);
1042 }
1043 
1044 bool
evdev_is_grabbed(struct evdev_dev * evdev)1045 evdev_is_grabbed(struct evdev_dev *evdev)
1046 {
1047 #if 0
1048           if (kdb_active || SCHEDULER_STOPPED())
1049                     return (false);
1050 #endif
1051           /*
1052            * The function is intended to be called from evdev-unrelated parts of
1053            * code like syscons-compatible parts of mouse and keyboard drivers.
1054            * That makes unlocked read-only access acceptable.
1055            */
1056           return (evdev->ev_grabber != NULL);
1057 }
1058 
1059 static void
evdev_repeat_callout(void * arg)1060 evdev_repeat_callout(void *arg)
1061 {
1062           struct evdev_dev *evdev = (struct evdev_dev *)arg;
1063 
1064           evdev_send_event(evdev, EV_KEY, evdev->ev_rep_key, KEY_EVENT_REPEAT);
1065           evdev_send_event(evdev, EV_SYN, SYN_REPORT, 1);
1066 
1067           if (evdev->ev_rep[REP_PERIOD])
1068                     callout_reset(&evdev->ev_rep_callout,
1069                         evdev->ev_rep[REP_PERIOD] * hz / 1000,
1070                         evdev_repeat_callout, evdev);
1071           else
1072                     evdev->ev_rep_key = KEY_RESERVED;
1073 }
1074 
1075 static void
evdev_start_repeat(struct evdev_dev * evdev,uint16_t key)1076 evdev_start_repeat(struct evdev_dev *evdev, uint16_t key)
1077 {
1078 
1079           EVDEV_LOCK_ASSERT(evdev);
1080 
1081           if (evdev->ev_rep[REP_DELAY]) {
1082                     evdev->ev_rep_key = key;
1083                     callout_reset(&evdev->ev_rep_callout,
1084                         evdev->ev_rep[REP_DELAY] * hz / 1000,
1085                         evdev_repeat_callout, evdev);
1086           }
1087 }
1088 
1089 static void
evdev_stop_repeat(struct evdev_dev * evdev)1090 evdev_stop_repeat(struct evdev_dev *evdev)
1091 {
1092 
1093           EVDEV_LOCK_ASSERT(evdev);
1094 
1095           if (evdev->ev_rep_key != KEY_RESERVED) {
1096                     callout_stop(&evdev->ev_rep_callout);
1097                     evdev->ev_rep_key = KEY_RESERVED;
1098           }
1099 }
1100 
1101 static int
evdev_load(module_t mod,int cmd,void * arg)1102 evdev_load(module_t mod, int cmd, void *arg)
1103 {
1104           int error = 0;
1105 
1106           switch (cmd) {
1107           case MOD_LOAD:
1108                     kprintf("evdev device loaded.\n");
1109                     break;
1110           case MOD_UNLOAD:
1111                     kprintf("evdev device unloaded.\n");
1112                     break;
1113 
1114           default:
1115                     error = EOPNOTSUPP;
1116                     break;
1117     }
1118 
1119     return (error);
1120 }
1121 
1122 DEV_MODULE(evdev, evdev_load, NULL);
1123 MODULE_VERSION(evdev, 1);
1124