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