-
Notifications
You must be signed in to change notification settings - Fork 54.5k
/
hid-multitouch.c
2153 lines (1839 loc) · 59.3 KB
/
hid-multitouch.c
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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* HID driver for multitouch panels
*
* Copyright (c) 2010-2012 Stephane Chatty <chatty@enac.fr>
* Copyright (c) 2010-2013 Benjamin Tissoires <benjamin.tissoires@gmail.com>
* Copyright (c) 2010-2012 Ecole Nationale de l'Aviation Civile, France
* Copyright (c) 2012-2013 Red Hat, Inc
*
* This code is partly based on hid-egalax.c:
*
* Copyright (c) 2010 Stephane Chatty <chatty@enac.fr>
* Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
* Copyright (c) 2010 Canonical, Ltd.
*
* This code is partly based on hid-3m-pct.c:
*
* Copyright (c) 2009-2010 Stephane Chatty <chatty@enac.fr>
* Copyright (c) 2010 Henrik Rydberg <rydberg@euromail.se>
* Copyright (c) 2010 Canonical, Ltd.
*/
/*
*/
/*
* This driver is regularly tested thanks to the test suite in hid-tools[1].
* Please run these regression tests before patching this module so that
* your patch won't break existing known devices.
*
* [1] https://gitlab.freedesktop.org/libevdev/hid-tools
*/
#include <linux/device.h>
#include <linux/hid.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/input/mt.h>
#include <linux/jiffies.h>
#include <linux/string.h>
#include <linux/timer.h>
MODULE_AUTHOR("Stephane Chatty <chatty@enac.fr>");
MODULE_AUTHOR("Benjamin Tissoires <benjamin.tissoires@gmail.com>");
MODULE_DESCRIPTION("HID multitouch panels");
MODULE_LICENSE("GPL");
#include "hid-ids.h"
/* quirks to control the device */
#define MT_QUIRK_NOT_SEEN_MEANS_UP BIT(0)
#define MT_QUIRK_SLOT_IS_CONTACTID BIT(1)
#define MT_QUIRK_CYPRESS BIT(2)
#define MT_QUIRK_SLOT_IS_CONTACTNUMBER BIT(3)
#define MT_QUIRK_ALWAYS_VALID BIT(4)
#define MT_QUIRK_VALID_IS_INRANGE BIT(5)
#define MT_QUIRK_VALID_IS_CONFIDENCE BIT(6)
#define MT_QUIRK_CONFIDENCE BIT(7)
#define MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE BIT(8)
#define MT_QUIRK_NO_AREA BIT(9)
#define MT_QUIRK_IGNORE_DUPLICATES BIT(10)
#define MT_QUIRK_HOVERING BIT(11)
#define MT_QUIRK_CONTACT_CNT_ACCURATE BIT(12)
#define MT_QUIRK_FORCE_GET_FEATURE BIT(13)
#define MT_QUIRK_FIX_CONST_CONTACT_ID BIT(14)
#define MT_QUIRK_TOUCH_SIZE_SCALING BIT(15)
#define MT_QUIRK_STICKY_FINGERS BIT(16)
#define MT_QUIRK_ASUS_CUSTOM_UP BIT(17)
#define MT_QUIRK_WIN8_PTP_BUTTONS BIT(18)
#define MT_QUIRK_SEPARATE_APP_REPORT BIT(19)
#define MT_INPUTMODE_TOUCHSCREEN 0x02
#define MT_INPUTMODE_TOUCHPAD 0x03
#define MT_BUTTONTYPE_CLICKPAD 0
enum latency_mode {
HID_LATENCY_NORMAL = 0,
HID_LATENCY_HIGH = 1,
};
#define MT_IO_FLAGS_RUNNING 0
#define MT_IO_FLAGS_ACTIVE_SLOTS 1
#define MT_IO_FLAGS_PENDING_SLOTS 2
static const bool mtrue = true; /* default for true */
static const bool mfalse; /* default for false */
static const __s32 mzero; /* default for 0 */
#define DEFAULT_TRUE ((void *)&mtrue)
#define DEFAULT_FALSE ((void *)&mfalse)
#define DEFAULT_ZERO ((void *)&mzero)
struct mt_usages {
struct list_head list;
__s32 *x, *y, *cx, *cy, *p, *w, *h, *a;
__s32 *contactid; /* the device ContactID assigned to this slot */
bool *tip_state; /* is the touch valid? */
bool *inrange_state; /* is the finger in proximity of the sensor? */
bool *confidence_state; /* is the touch made by a finger? */
};
struct mt_application {
struct list_head list;
unsigned int application;
unsigned int report_id;
struct list_head mt_usages; /* mt usages list */
__s32 quirks;
__s32 *scantime; /* scantime reported */
__s32 scantime_logical_max; /* max value for raw scantime */
__s32 *raw_cc; /* contact count in the report */
int left_button_state; /* left button state */
unsigned int mt_flags; /* flags to pass to input-mt */
unsigned long *pending_palm_slots; /* slots where we reported palm
* and need to release */
__u8 num_received; /* how many contacts we received */
__u8 num_expected; /* expected last contact index */
__u8 buttons_count; /* number of physical buttons per touchpad */
__u8 touches_by_report; /* how many touches are present in one report:
* 1 means we should use a serial protocol
* > 1 means hybrid (multitouch) protocol
*/
__s32 dev_time; /* the scan time provided by the device */
unsigned long jiffies; /* the frame's jiffies */
int timestamp; /* the timestamp to be sent */
int prev_scantime; /* scantime reported previously */
bool have_contact_count;
};
struct mt_class {
__s32 name; /* MT_CLS */
__s32 quirks;
__s32 sn_move; /* Signal/noise ratio for move events */
__s32 sn_width; /* Signal/noise ratio for width events */
__s32 sn_height; /* Signal/noise ratio for height events */
__s32 sn_pressure; /* Signal/noise ratio for pressure events */
__u8 maxcontacts;
bool is_indirect; /* true for touchpads */
bool export_all_inputs; /* do not ignore mouse, keyboards, etc... */
};
struct mt_report_data {
struct list_head list;
struct hid_report *report;
struct mt_application *application;
bool is_mt_collection;
};
struct mt_device {
struct mt_class mtclass; /* our mt device class */
struct timer_list release_timer; /* to release sticky fingers */
struct hid_device *hdev; /* hid_device we're attached to */
unsigned long mt_io_flags; /* mt flags (MT_IO_FLAGS_*) */
__u8 inputmode_value; /* InputMode HID feature value */
__u8 maxcontacts;
bool is_buttonpad; /* is this device a button pad? */
bool serial_maybe; /* need to check for serial protocol */
struct list_head applications;
struct list_head reports;
};
static void mt_post_parse_default_settings(struct mt_device *td,
struct mt_application *app);
static void mt_post_parse(struct mt_device *td, struct mt_application *app);
/* classes of device behavior */
#define MT_CLS_DEFAULT 0x0001
#define MT_CLS_SERIAL 0x0002
#define MT_CLS_CONFIDENCE 0x0003
#define MT_CLS_CONFIDENCE_CONTACT_ID 0x0004
#define MT_CLS_CONFIDENCE_MINUS_ONE 0x0005
#define MT_CLS_DUAL_INRANGE_CONTACTID 0x0006
#define MT_CLS_DUAL_INRANGE_CONTACTNUMBER 0x0007
/* reserved 0x0008 */
#define MT_CLS_INRANGE_CONTACTNUMBER 0x0009
#define MT_CLS_NSMU 0x000a
/* reserved 0x0010 */
/* reserved 0x0011 */
#define MT_CLS_WIN_8 0x0012
#define MT_CLS_EXPORT_ALL_INPUTS 0x0013
#define MT_CLS_WIN_8_DUAL 0x0014
/* vendor specific classes */
#define MT_CLS_3M 0x0101
/* reserved 0x0102 */
#define MT_CLS_EGALAX 0x0103
#define MT_CLS_EGALAX_SERIAL 0x0104
#define MT_CLS_TOPSEED 0x0105
#define MT_CLS_PANASONIC 0x0106
#define MT_CLS_FLATFROG 0x0107
#define MT_CLS_GENERALTOUCH_TWOFINGERS 0x0108
#define MT_CLS_GENERALTOUCH_PWT_TENFINGERS 0x0109
#define MT_CLS_LG 0x010a
#define MT_CLS_ASUS 0x010b
#define MT_CLS_VTL 0x0110
#define MT_CLS_GOOGLE 0x0111
#define MT_CLS_RAZER_BLADE_STEALTH 0x0112
#define MT_CLS_SMART_TECH 0x0113
#define MT_DEFAULT_MAXCONTACT 10
#define MT_MAX_MAXCONTACT 250
/*
* Resync device and local timestamps after that many microseconds without
* receiving data.
*/
#define MAX_TIMESTAMP_INTERVAL 1000000
#define MT_USB_DEVICE(v, p) HID_DEVICE(BUS_USB, HID_GROUP_MULTITOUCH, v, p)
#define MT_BT_DEVICE(v, p) HID_DEVICE(BUS_BLUETOOTH, HID_GROUP_MULTITOUCH, v, p)
/*
* these device-dependent functions determine what slot corresponds
* to a valid contact that was just read.
*/
static int cypress_compute_slot(struct mt_application *application,
struct mt_usages *slot)
{
if (*slot->contactid != 0 || application->num_received == 0)
return *slot->contactid;
else
return -1;
}
static const struct mt_class mt_classes[] = {
{ .name = MT_CLS_DEFAULT,
.quirks = MT_QUIRK_ALWAYS_VALID |
MT_QUIRK_CONTACT_CNT_ACCURATE },
{ .name = MT_CLS_NSMU,
.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP },
{ .name = MT_CLS_SERIAL,
.quirks = MT_QUIRK_ALWAYS_VALID},
{ .name = MT_CLS_CONFIDENCE,
.quirks = MT_QUIRK_VALID_IS_CONFIDENCE },
{ .name = MT_CLS_CONFIDENCE_CONTACT_ID,
.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
MT_QUIRK_SLOT_IS_CONTACTID },
{ .name = MT_CLS_CONFIDENCE_MINUS_ONE,
.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE },
{ .name = MT_CLS_DUAL_INRANGE_CONTACTID,
.quirks = MT_QUIRK_VALID_IS_INRANGE |
MT_QUIRK_SLOT_IS_CONTACTID,
.maxcontacts = 2 },
{ .name = MT_CLS_DUAL_INRANGE_CONTACTNUMBER,
.quirks = MT_QUIRK_VALID_IS_INRANGE |
MT_QUIRK_SLOT_IS_CONTACTNUMBER,
.maxcontacts = 2 },
{ .name = MT_CLS_INRANGE_CONTACTNUMBER,
.quirks = MT_QUIRK_VALID_IS_INRANGE |
MT_QUIRK_SLOT_IS_CONTACTNUMBER },
{ .name = MT_CLS_WIN_8,
.quirks = MT_QUIRK_ALWAYS_VALID |
MT_QUIRK_IGNORE_DUPLICATES |
MT_QUIRK_HOVERING |
MT_QUIRK_CONTACT_CNT_ACCURATE |
MT_QUIRK_STICKY_FINGERS |
MT_QUIRK_WIN8_PTP_BUTTONS,
.export_all_inputs = true },
{ .name = MT_CLS_EXPORT_ALL_INPUTS,
.quirks = MT_QUIRK_ALWAYS_VALID |
MT_QUIRK_CONTACT_CNT_ACCURATE,
.export_all_inputs = true },
{ .name = MT_CLS_WIN_8_DUAL,
.quirks = MT_QUIRK_ALWAYS_VALID |
MT_QUIRK_IGNORE_DUPLICATES |
MT_QUIRK_HOVERING |
MT_QUIRK_CONTACT_CNT_ACCURATE |
MT_QUIRK_WIN8_PTP_BUTTONS,
.export_all_inputs = true },
/*
* vendor specific classes
*/
{ .name = MT_CLS_3M,
.quirks = MT_QUIRK_VALID_IS_CONFIDENCE |
MT_QUIRK_SLOT_IS_CONTACTID |
MT_QUIRK_TOUCH_SIZE_SCALING,
.sn_move = 2048,
.sn_width = 128,
.sn_height = 128,
.maxcontacts = 60,
},
{ .name = MT_CLS_EGALAX,
.quirks = MT_QUIRK_SLOT_IS_CONTACTID |
MT_QUIRK_VALID_IS_INRANGE,
.sn_move = 4096,
.sn_pressure = 32,
},
{ .name = MT_CLS_EGALAX_SERIAL,
.quirks = MT_QUIRK_SLOT_IS_CONTACTID |
MT_QUIRK_ALWAYS_VALID,
.sn_move = 4096,
.sn_pressure = 32,
},
{ .name = MT_CLS_TOPSEED,
.quirks = MT_QUIRK_ALWAYS_VALID,
.is_indirect = true,
.maxcontacts = 2,
},
{ .name = MT_CLS_PANASONIC,
.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP,
.maxcontacts = 4 },
{ .name = MT_CLS_GENERALTOUCH_TWOFINGERS,
.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
MT_QUIRK_VALID_IS_INRANGE |
MT_QUIRK_SLOT_IS_CONTACTID,
.maxcontacts = 2
},
{ .name = MT_CLS_GENERALTOUCH_PWT_TENFINGERS,
.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
MT_QUIRK_SLOT_IS_CONTACTID
},
{ .name = MT_CLS_FLATFROG,
.quirks = MT_QUIRK_NOT_SEEN_MEANS_UP |
MT_QUIRK_NO_AREA,
.sn_move = 2048,
.maxcontacts = 40,
},
{ .name = MT_CLS_LG,
.quirks = MT_QUIRK_ALWAYS_VALID |
MT_QUIRK_FIX_CONST_CONTACT_ID |
MT_QUIRK_IGNORE_DUPLICATES |
MT_QUIRK_HOVERING |
MT_QUIRK_CONTACT_CNT_ACCURATE },
{ .name = MT_CLS_ASUS,
.quirks = MT_QUIRK_ALWAYS_VALID |
MT_QUIRK_CONTACT_CNT_ACCURATE |
MT_QUIRK_ASUS_CUSTOM_UP },
{ .name = MT_CLS_VTL,
.quirks = MT_QUIRK_ALWAYS_VALID |
MT_QUIRK_CONTACT_CNT_ACCURATE |
MT_QUIRK_FORCE_GET_FEATURE,
},
{ .name = MT_CLS_GOOGLE,
.quirks = MT_QUIRK_ALWAYS_VALID |
MT_QUIRK_CONTACT_CNT_ACCURATE |
MT_QUIRK_SLOT_IS_CONTACTID |
MT_QUIRK_HOVERING
},
{ .name = MT_CLS_RAZER_BLADE_STEALTH,
.quirks = MT_QUIRK_ALWAYS_VALID |
MT_QUIRK_IGNORE_DUPLICATES |
MT_QUIRK_HOVERING |
MT_QUIRK_CONTACT_CNT_ACCURATE |
MT_QUIRK_WIN8_PTP_BUTTONS,
},
{ .name = MT_CLS_SMART_TECH,
.quirks = MT_QUIRK_ALWAYS_VALID |
MT_QUIRK_IGNORE_DUPLICATES |
MT_QUIRK_CONTACT_CNT_ACCURATE |
MT_QUIRK_SEPARATE_APP_REPORT,
},
{ }
};
static ssize_t mt_show_quirks(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct hid_device *hdev = to_hid_device(dev);
struct mt_device *td = hid_get_drvdata(hdev);
return sprintf(buf, "%u\n", td->mtclass.quirks);
}
static ssize_t mt_set_quirks(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct hid_device *hdev = to_hid_device(dev);
struct mt_device *td = hid_get_drvdata(hdev);
struct mt_application *application;
unsigned long val;
if (kstrtoul(buf, 0, &val))
return -EINVAL;
td->mtclass.quirks = val;
list_for_each_entry(application, &td->applications, list) {
application->quirks = val;
if (!application->have_contact_count)
application->quirks &= ~MT_QUIRK_CONTACT_CNT_ACCURATE;
}
return count;
}
static DEVICE_ATTR(quirks, S_IWUSR | S_IRUGO, mt_show_quirks, mt_set_quirks);
static struct attribute *sysfs_attrs[] = {
&dev_attr_quirks.attr,
NULL
};
static const struct attribute_group mt_attribute_group = {
.attrs = sysfs_attrs
};
static void mt_get_feature(struct hid_device *hdev, struct hid_report *report)
{
int ret;
u32 size = hid_report_len(report);
u8 *buf;
/*
* Do not fetch the feature report if the device has been explicitly
* marked as non-capable.
*/
if (hdev->quirks & HID_QUIRK_NO_INIT_REPORTS)
return;
buf = hid_alloc_report_buf(report, GFP_KERNEL);
if (!buf)
return;
ret = hid_hw_raw_request(hdev, report->id, buf, size,
HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
if (ret < 0) {
dev_warn(&hdev->dev, "failed to fetch feature %d\n",
report->id);
} else {
ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
size, 0);
if (ret)
dev_warn(&hdev->dev, "failed to report feature\n");
}
kfree(buf);
}
static void mt_feature_mapping(struct hid_device *hdev,
struct hid_field *field, struct hid_usage *usage)
{
struct mt_device *td = hid_get_drvdata(hdev);
switch (usage->hid) {
case HID_DG_CONTACTMAX:
mt_get_feature(hdev, field->report);
td->maxcontacts = field->value[0];
if (!td->maxcontacts &&
field->logical_maximum <= MT_MAX_MAXCONTACT)
td->maxcontacts = field->logical_maximum;
if (td->mtclass.maxcontacts)
/* check if the maxcontacts is given by the class */
td->maxcontacts = td->mtclass.maxcontacts;
break;
case HID_DG_BUTTONTYPE:
if (usage->usage_index >= field->report_count) {
dev_err(&hdev->dev, "HID_DG_BUTTONTYPE out of range\n");
break;
}
mt_get_feature(hdev, field->report);
if (field->value[usage->usage_index] == MT_BUTTONTYPE_CLICKPAD)
td->is_buttonpad = true;
break;
case 0xff0000c5:
/* Retrieve the Win8 blob once to enable some devices */
if (usage->usage_index == 0)
mt_get_feature(hdev, field->report);
break;
}
}
static void set_abs(struct input_dev *input, unsigned int code,
struct hid_field *field, int snratio)
{
int fmin = field->logical_minimum;
int fmax = field->logical_maximum;
int fuzz = snratio ? (fmax - fmin) / snratio : 0;
input_set_abs_params(input, code, fmin, fmax, fuzz, 0);
input_abs_set_res(input, code, hidinput_calc_abs_res(field, code));
}
static struct mt_usages *mt_allocate_usage(struct hid_device *hdev,
struct mt_application *application)
{
struct mt_usages *usage;
usage = devm_kzalloc(&hdev->dev, sizeof(*usage), GFP_KERNEL);
if (!usage)
return NULL;
/* set some defaults so we do not need to check for null pointers */
usage->x = DEFAULT_ZERO;
usage->y = DEFAULT_ZERO;
usage->cx = DEFAULT_ZERO;
usage->cy = DEFAULT_ZERO;
usage->p = DEFAULT_ZERO;
usage->w = DEFAULT_ZERO;
usage->h = DEFAULT_ZERO;
usage->a = DEFAULT_ZERO;
usage->contactid = DEFAULT_ZERO;
usage->tip_state = DEFAULT_FALSE;
usage->inrange_state = DEFAULT_FALSE;
usage->confidence_state = DEFAULT_TRUE;
list_add_tail(&usage->list, &application->mt_usages);
return usage;
}
static struct mt_application *mt_allocate_application(struct mt_device *td,
struct hid_report *report)
{
unsigned int application = report->application;
struct mt_application *mt_application;
mt_application = devm_kzalloc(&td->hdev->dev, sizeof(*mt_application),
GFP_KERNEL);
if (!mt_application)
return NULL;
mt_application->application = application;
INIT_LIST_HEAD(&mt_application->mt_usages);
if (application == HID_DG_TOUCHSCREEN)
mt_application->mt_flags |= INPUT_MT_DIRECT;
/*
* Model touchscreens providing buttons as touchpads.
*/
if (application == HID_DG_TOUCHPAD) {
mt_application->mt_flags |= INPUT_MT_POINTER;
td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
}
mt_application->scantime = DEFAULT_ZERO;
mt_application->raw_cc = DEFAULT_ZERO;
mt_application->quirks = td->mtclass.quirks;
mt_application->report_id = report->id;
list_add_tail(&mt_application->list, &td->applications);
return mt_application;
}
static struct mt_application *mt_find_application(struct mt_device *td,
struct hid_report *report)
{
unsigned int application = report->application;
struct mt_application *tmp, *mt_application = NULL;
list_for_each_entry(tmp, &td->applications, list) {
if (application == tmp->application) {
if (!(td->mtclass.quirks & MT_QUIRK_SEPARATE_APP_REPORT) ||
tmp->report_id == report->id) {
mt_application = tmp;
break;
}
}
}
if (!mt_application)
mt_application = mt_allocate_application(td, report);
return mt_application;
}
static struct mt_report_data *mt_allocate_report_data(struct mt_device *td,
struct hid_report *report)
{
struct mt_report_data *rdata;
struct hid_field *field;
int r, n;
rdata = devm_kzalloc(&td->hdev->dev, sizeof(*rdata), GFP_KERNEL);
if (!rdata)
return NULL;
rdata->report = report;
rdata->application = mt_find_application(td, report);
if (!rdata->application) {
devm_kfree(&td->hdev->dev, rdata);
return NULL;
}
for (r = 0; r < report->maxfield; r++) {
field = report->field[r];
if (!(HID_MAIN_ITEM_VARIABLE & field->flags))
continue;
for (n = 0; n < field->report_count; n++) {
if (field->usage[n].hid == HID_DG_CONTACTID)
rdata->is_mt_collection = true;
}
}
list_add_tail(&rdata->list, &td->reports);
return rdata;
}
static struct mt_report_data *mt_find_report_data(struct mt_device *td,
struct hid_report *report)
{
struct mt_report_data *tmp, *rdata = NULL;
list_for_each_entry(tmp, &td->reports, list) {
if (report == tmp->report) {
rdata = tmp;
break;
}
}
if (!rdata)
rdata = mt_allocate_report_data(td, report);
return rdata;
}
static void mt_store_field(struct hid_device *hdev,
struct mt_application *application,
__s32 *value,
size_t offset)
{
struct mt_usages *usage;
__s32 **target;
if (list_empty(&application->mt_usages))
usage = mt_allocate_usage(hdev, application);
else
usage = list_last_entry(&application->mt_usages,
struct mt_usages,
list);
if (!usage)
return;
target = (__s32 **)((char *)usage + offset);
/* the value has already been filled, create a new slot */
if (*target != DEFAULT_TRUE &&
*target != DEFAULT_FALSE &&
*target != DEFAULT_ZERO) {
if (usage->contactid == DEFAULT_ZERO ||
usage->x == DEFAULT_ZERO ||
usage->y == DEFAULT_ZERO) {
hid_dbg(hdev,
"ignoring duplicate usage on incomplete");
return;
}
usage = mt_allocate_usage(hdev, application);
if (!usage)
return;
target = (__s32 **)((char *)usage + offset);
}
*target = value;
}
#define MT_STORE_FIELD(__name) \
mt_store_field(hdev, app, \
&field->value[usage->usage_index], \
offsetof(struct mt_usages, __name))
static int mt_touch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
struct hid_field *field, struct hid_usage *usage,
unsigned long **bit, int *max, struct mt_application *app)
{
struct mt_device *td = hid_get_drvdata(hdev);
struct mt_class *cls = &td->mtclass;
int code;
struct hid_usage *prev_usage = NULL;
/*
* Model touchscreens providing buttons as touchpads.
*/
if (field->application == HID_DG_TOUCHSCREEN &&
(usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON) {
app->mt_flags |= INPUT_MT_POINTER;
td->inputmode_value = MT_INPUTMODE_TOUCHPAD;
}
/* count the buttons on touchpads */
if ((usage->hid & HID_USAGE_PAGE) == HID_UP_BUTTON)
app->buttons_count++;
if (usage->usage_index)
prev_usage = &field->usage[usage->usage_index - 1];
switch (usage->hid & HID_USAGE_PAGE) {
case HID_UP_GENDESK:
switch (usage->hid) {
case HID_GD_X:
if (prev_usage && (prev_usage->hid == usage->hid)) {
code = ABS_MT_TOOL_X;
MT_STORE_FIELD(cx);
} else {
code = ABS_MT_POSITION_X;
MT_STORE_FIELD(x);
}
set_abs(hi->input, code, field, cls->sn_move);
/*
* A system multi-axis that exports X and Y has a high
* chance of being used directly on a surface
*/
if (field->application == HID_GD_SYSTEM_MULTIAXIS) {
__set_bit(INPUT_PROP_DIRECT,
hi->input->propbit);
input_set_abs_params(hi->input,
ABS_MT_TOOL_TYPE,
MT_TOOL_DIAL,
MT_TOOL_DIAL, 0, 0);
}
return 1;
case HID_GD_Y:
if (prev_usage && (prev_usage->hid == usage->hid)) {
code = ABS_MT_TOOL_Y;
MT_STORE_FIELD(cy);
} else {
code = ABS_MT_POSITION_Y;
MT_STORE_FIELD(y);
}
set_abs(hi->input, code, field, cls->sn_move);
return 1;
}
return 0;
case HID_UP_DIGITIZER:
switch (usage->hid) {
case HID_DG_INRANGE:
if (app->quirks & MT_QUIRK_HOVERING) {
input_set_abs_params(hi->input,
ABS_MT_DISTANCE, 0, 1, 0, 0);
}
MT_STORE_FIELD(inrange_state);
return 1;
case HID_DG_CONFIDENCE:
if ((cls->name == MT_CLS_WIN_8 ||
cls->name == MT_CLS_WIN_8_DUAL) &&
(field->application == HID_DG_TOUCHPAD ||
field->application == HID_DG_TOUCHSCREEN))
app->quirks |= MT_QUIRK_CONFIDENCE;
if (app->quirks & MT_QUIRK_CONFIDENCE)
input_set_abs_params(hi->input,
ABS_MT_TOOL_TYPE,
MT_TOOL_FINGER,
MT_TOOL_PALM, 0, 0);
MT_STORE_FIELD(confidence_state);
return 1;
case HID_DG_TIPSWITCH:
if (field->application != HID_GD_SYSTEM_MULTIAXIS)
input_set_capability(hi->input,
EV_KEY, BTN_TOUCH);
MT_STORE_FIELD(tip_state);
return 1;
case HID_DG_CONTACTID:
MT_STORE_FIELD(contactid);
app->touches_by_report++;
return 1;
case HID_DG_WIDTH:
if (!(app->quirks & MT_QUIRK_NO_AREA))
set_abs(hi->input, ABS_MT_TOUCH_MAJOR, field,
cls->sn_width);
MT_STORE_FIELD(w);
return 1;
case HID_DG_HEIGHT:
if (!(app->quirks & MT_QUIRK_NO_AREA)) {
set_abs(hi->input, ABS_MT_TOUCH_MINOR, field,
cls->sn_height);
/*
* Only set ABS_MT_ORIENTATION if it is not
* already set by the HID_DG_AZIMUTH usage.
*/
if (!test_bit(ABS_MT_ORIENTATION,
hi->input->absbit))
input_set_abs_params(hi->input,
ABS_MT_ORIENTATION, 0, 1, 0, 0);
}
MT_STORE_FIELD(h);
return 1;
case HID_DG_TIPPRESSURE:
set_abs(hi->input, ABS_MT_PRESSURE, field,
cls->sn_pressure);
MT_STORE_FIELD(p);
return 1;
case HID_DG_SCANTIME:
input_set_capability(hi->input, EV_MSC, MSC_TIMESTAMP);
app->scantime = &field->value[usage->usage_index];
app->scantime_logical_max = field->logical_maximum;
return 1;
case HID_DG_CONTACTCOUNT:
app->have_contact_count = true;
app->raw_cc = &field->value[usage->usage_index];
return 1;
case HID_DG_AZIMUTH:
/*
* Azimuth has the range of [0, MAX) representing a full
* revolution. Set ABS_MT_ORIENTATION to a quarter of
* MAX according the definition of ABS_MT_ORIENTATION
*/
input_set_abs_params(hi->input, ABS_MT_ORIENTATION,
-field->logical_maximum / 4,
field->logical_maximum / 4,
cls->sn_move ?
field->logical_maximum / cls->sn_move : 0, 0);
MT_STORE_FIELD(a);
return 1;
case HID_DG_CONTACTMAX:
/* contact max are global to the report */
return -1;
case HID_DG_TOUCH:
/* Legacy devices use TIPSWITCH and not TOUCH.
* Let's just ignore this field. */
return -1;
}
/* let hid-input decide for the others */
return 0;
case HID_UP_BUTTON:
code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
/*
* MS PTP spec says that external buttons left and right have
* usages 2 and 3.
*/
if ((app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS) &&
field->application == HID_DG_TOUCHPAD &&
(usage->hid & HID_USAGE) > 1)
code--;
if (field->application == HID_GD_SYSTEM_MULTIAXIS)
code = BTN_0 + ((usage->hid - 1) & HID_USAGE);
hid_map_usage(hi, usage, bit, max, EV_KEY, code);
input_set_capability(hi->input, EV_KEY, code);
return 1;
case 0xff000000:
/* we do not want to map these: no input-oriented meaning */
return -1;
}
return 0;
}
static int mt_compute_slot(struct mt_device *td, struct mt_application *app,
struct mt_usages *slot,
struct input_dev *input)
{
__s32 quirks = app->quirks;
if (quirks & MT_QUIRK_SLOT_IS_CONTACTID)
return *slot->contactid;
if (quirks & MT_QUIRK_CYPRESS)
return cypress_compute_slot(app, slot);
if (quirks & MT_QUIRK_SLOT_IS_CONTACTNUMBER)
return app->num_received;
if (quirks & MT_QUIRK_SLOT_IS_CONTACTID_MINUS_ONE)
return *slot->contactid - 1;
return input_mt_get_slot_by_key(input, *slot->contactid);
}
static void mt_release_pending_palms(struct mt_device *td,
struct mt_application *app,
struct input_dev *input)
{
int slotnum;
bool need_sync = false;
for_each_set_bit(slotnum, app->pending_palm_slots, td->maxcontacts) {
clear_bit(slotnum, app->pending_palm_slots);
input_mt_slot(input, slotnum);
input_mt_report_slot_state(input, MT_TOOL_PALM, false);
need_sync = true;
}
if (need_sync) {
input_mt_sync_frame(input);
input_sync(input);
}
}
/*
* this function is called when a whole packet has been received and processed,
* so that it can decide what to send to the input layer.
*/
static void mt_sync_frame(struct mt_device *td, struct mt_application *app,
struct input_dev *input)
{
if (app->quirks & MT_QUIRK_WIN8_PTP_BUTTONS)
input_event(input, EV_KEY, BTN_LEFT, app->left_button_state);
input_mt_sync_frame(input);
input_event(input, EV_MSC, MSC_TIMESTAMP, app->timestamp);
input_sync(input);
mt_release_pending_palms(td, app, input);
app->num_received = 0;
app->left_button_state = 0;
if (test_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags))
set_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags);
else
clear_bit(MT_IO_FLAGS_PENDING_SLOTS, &td->mt_io_flags);
clear_bit(MT_IO_FLAGS_ACTIVE_SLOTS, &td->mt_io_flags);
}
static int mt_compute_timestamp(struct mt_application *app, __s32 value)
{
long delta = value - app->prev_scantime;
unsigned long jdelta = jiffies_to_usecs(jiffies - app->jiffies);
app->jiffies = jiffies;
if (delta < 0)
delta += app->scantime_logical_max;
/* HID_DG_SCANTIME is expressed in 100us, we want it in us. */
delta *= 100;
if (jdelta > MAX_TIMESTAMP_INTERVAL)
/* No data received for a while, resync the timestamp. */
return 0;
else
return app->timestamp + delta;
}
static int mt_touch_event(struct hid_device *hid, struct hid_field *field,
struct hid_usage *usage, __s32 value)
{
/* we will handle the hidinput part later, now remains hiddev */
if (hid->claimed & HID_CLAIMED_HIDDEV && hid->hiddev_hid_event)
hid->hiddev_hid_event(hid, field, usage, value);
return 1;
}
static int mt_process_slot(struct mt_device *td, struct input_dev *input,
struct mt_application *app,
struct mt_usages *slot)
{
struct input_mt *mt = input->mt;
__s32 quirks = app->quirks;
bool valid = true;
bool confidence_state = true;
bool inrange_state = false;
int active;
int slotnum;
int tool = MT_TOOL_FINGER;
if (!slot)
return -EINVAL;
if ((quirks & MT_QUIRK_CONTACT_CNT_ACCURATE) &&
app->num_received >= app->num_expected)
return -EAGAIN;
if (!(quirks & MT_QUIRK_ALWAYS_VALID)) {
if (quirks & MT_QUIRK_VALID_IS_INRANGE)
valid = *slot->inrange_state;
if (quirks & MT_QUIRK_NOT_SEEN_MEANS_UP)
valid = *slot->tip_state;
if (quirks & MT_QUIRK_VALID_IS_CONFIDENCE)
valid = *slot->confidence_state;
if (!valid)
return 0;
}
slotnum = mt_compute_slot(td, app, slot, input);
if (slotnum < 0 || slotnum >= td->maxcontacts)
return 0;