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