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