-
Notifications
You must be signed in to change notification settings - Fork 0
/
synaptics.c
1909 lines (1601 loc) · 49.7 KB
/
synaptics.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
/*
* Synaptics TouchPad PS/2 mouse driver
*
* 2003 Dmitry Torokhov <[email protected]>
* Added support for pass-through port. Special thanks to Peter Berg Larsen
* for explaining various Synaptics quirks.
*
* 2003 Peter Osterlund <[email protected]>
* Ported to 2.5 input device infrastructure.
*
* Copyright (C) 2001 Stefan Gmeiner <[email protected]>
* start merging tpconfig and gpm code to a xfree-input module
* adding some changes and extensions (ex. 3rd and 4th button)
*
* Copyright (c) 1997 C. Scott Ananian <[email protected]>
* Copyright (c) 1998-2000 Bruce Kalk <[email protected]>
* code for the special synaptics commands (from the tpconfig-source)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* Trademarks are the property of their respective owners.
*/
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/dmi.h>
#include <linux/input/mt.h>
#include <linux/serio.h>
#include <linux/libps2.h>
#include <linux/rmi.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include "psmouse.h"
#include "synaptics.h"
/*
* The x/y limits are taken from the Synaptics TouchPad interfacing Guide,
* section 2.3.2, which says that they should be valid regardless of the
* actual size of the sensor.
* Note that newer firmware allows querying device for maximum useable
* coordinates.
*/
#define XMIN 0
#define XMAX 6143
#define YMIN 0
#define YMAX 6143
#define XMIN_NOMINAL 1472
#define XMAX_NOMINAL 5472
#define YMIN_NOMINAL 1408
#define YMAX_NOMINAL 4448
/* Size in bits of absolute position values reported by the hardware */
#define ABS_POS_BITS 13
/*
* These values should represent the absolute maximum value that will
* be reported for a positive position value. Some Synaptics firmware
* uses this value to indicate a finger near the edge of the touchpad
* whose precise position cannot be determined.
*
* At least one touchpad is known to report positions in excess of this
* value which are actually negative values truncated to the 13-bit
* reporting range. These values have never been observed to be lower
* than 8184 (i.e. -8), so we treat all values greater than 8176 as
* negative and any other value as positive.
*/
#define X_MAX_POSITIVE 8176
#define Y_MAX_POSITIVE 8176
/* maximum ABS_MT_POSITION displacement (in mm) */
#define DMAX 10
/*****************************************************************************
* Stuff we need even when we do not want native Synaptics support
****************************************************************************/
/*
* Set the synaptics touchpad mode byte by special commands
*/
static int synaptics_mode_cmd(struct psmouse *psmouse, u8 mode)
{
u8 param[1];
int error;
error = ps2_sliced_command(&psmouse->ps2dev, mode);
if (error)
return error;
param[0] = SYN_PS_SET_MODE2;
error = ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_SETRATE);
if (error)
return error;
return 0;
}
int synaptics_detect(struct psmouse *psmouse, bool set_properties)
{
struct ps2dev *ps2dev = &psmouse->ps2dev;
u8 param[4] = { 0 };
ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
ps2_command(ps2dev, param, PSMOUSE_CMD_SETRES);
ps2_command(ps2dev, param, PSMOUSE_CMD_GETINFO);
if (param[1] != 0x47)
return -ENODEV;
if (set_properties) {
psmouse->vendor = "Synaptics";
psmouse->name = "TouchPad";
}
return 0;
}
void synaptics_reset(struct psmouse *psmouse)
{
/* reset touchpad back to relative mode, gestures enabled */
synaptics_mode_cmd(psmouse, 0);
}
#if defined(CONFIG_MOUSE_PS2_SYNAPTICS) || \
defined(CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS)
/* This list has been kindly provided by Synaptics. */
static const char * const topbuttonpad_pnp_ids[] = {
"LEN0017",
"LEN0018",
"LEN0019",
"LEN0023",
"LEN002A",
"LEN002B",
"LEN002C",
"LEN002D",
"LEN002E",
"LEN0033", /* Helix */
"LEN0034", /* T431s, L440, L540, T540, W540, X1 Carbon 2nd */
"LEN0035", /* X240 */
"LEN0036", /* T440 */
"LEN0037", /* X1 Carbon 2nd */
"LEN0038",
"LEN0039", /* T440s */
"LEN0041",
"LEN0042", /* Yoga */
"LEN0045",
"LEN0047",
"LEN0049",
"LEN2000", /* S540 */
"LEN2001", /* Edge E431 */
"LEN2002", /* Edge E531 */
"LEN2003",
"LEN2004", /* L440 */
"LEN2005",
"LEN2006", /* Edge E440/E540 */
"LEN2007",
"LEN2008",
"LEN2009",
"LEN200A",
"LEN200B",
NULL
};
static const char * const smbus_pnp_ids[] = {
/* all of the topbuttonpad_pnp_ids are valid, we just add some extras */
"LEN0048", /* X1 Carbon 3 */
"LEN0046", /* X250 */
"LEN004a", /* W541 */
"LEN005b", /* P50 */
"LEN005e", /* T560 */
"LEN0071", /* T480 */
"LEN0072", /* X1 Carbon Gen 5 (2017) - Elan/ALPS trackpoint */
"LEN0073", /* X1 Carbon G5 (Elantech) */
"LEN0092", /* X1 Carbon 6 */
"LEN0096", /* X280 */
"LEN0097", /* X280 -> ALPS trackpoint */
"LEN200f", /* T450s */
"SYN3052", /* HP EliteBook 840 G4 */
"SYN3221", /* HP 15-ay000 */
NULL
};
static const char * const forcepad_pnp_ids[] = {
"SYN300D",
"SYN3014",
NULL
};
/*
* Send a command to the synpatics touchpad by special commands
*/
static int synaptics_send_cmd(struct psmouse *psmouse, u8 cmd, u8 *param)
{
int error;
error = ps2_sliced_command(&psmouse->ps2dev, cmd);
if (error)
return error;
error = ps2_command(&psmouse->ps2dev, param, PSMOUSE_CMD_GETINFO);
if (error)
return error;
return 0;
}
static int synaptics_query_int(struct psmouse *psmouse, u8 query_cmd, u32 *val)
{
int error;
union {
__be32 be_val;
char buf[4];
} resp = { 0 };
error = synaptics_send_cmd(psmouse, query_cmd, resp.buf + 1);
if (error)
return error;
*val = be32_to_cpu(resp.be_val);
return 0;
}
/*
* Identify Touchpad
* See also the SYN_ID_* macros
*/
static int synaptics_identify(struct psmouse *psmouse,
struct synaptics_device_info *info)
{
int error;
error = synaptics_query_int(psmouse, SYN_QUE_IDENTIFY, &info->identity);
if (error)
return error;
return SYN_ID_IS_SYNAPTICS(info->identity) ? 0 : -ENXIO;
}
/*
* Read the model-id bytes from the touchpad
* see also SYN_MODEL_* macros
*/
static int synaptics_model_id(struct psmouse *psmouse,
struct synaptics_device_info *info)
{
return synaptics_query_int(psmouse, SYN_QUE_MODEL, &info->model_id);
}
/*
* Read the firmware id from the touchpad
*/
static int synaptics_firmware_id(struct psmouse *psmouse,
struct synaptics_device_info *info)
{
return synaptics_query_int(psmouse, SYN_QUE_FIRMWARE_ID,
&info->firmware_id);
}
/*
* Read the board id and the "More Extended Queries" from the touchpad
* The board id is encoded in the "QUERY MODES" response
*/
static int synaptics_query_modes(struct psmouse *psmouse,
struct synaptics_device_info *info)
{
u8 bid[3];
int error;
/* firmwares prior 7.5 have no board_id encoded */
if (SYN_ID_FULL(info->identity) < 0x705)
return 0;
error = synaptics_send_cmd(psmouse, SYN_QUE_MODES, bid);
if (error)
return error;
info->board_id = ((bid[0] & 0xfc) << 6) | bid[1];
if (SYN_MEXT_CAP_BIT(bid[0]))
return synaptics_query_int(psmouse, SYN_QUE_MEXT_CAPAB_10,
&info->ext_cap_10);
return 0;
}
/*
* Read the capability-bits from the touchpad
* see also the SYN_CAP_* macros
*/
static int synaptics_capability(struct psmouse *psmouse,
struct synaptics_device_info *info)
{
int error;
error = synaptics_query_int(psmouse, SYN_QUE_CAPABILITIES,
&info->capabilities);
if (error)
return error;
info->ext_cap = info->ext_cap_0c = 0;
/*
* Older firmwares had submodel ID fixed to 0x47
*/
if (SYN_ID_FULL(info->identity) < 0x705 &&
SYN_CAP_SUBMODEL_ID(info->capabilities) != 0x47) {
return -ENXIO;
}
/*
* Unless capExtended is set the rest of the flags should be ignored
*/
if (!SYN_CAP_EXTENDED(info->capabilities))
info->capabilities = 0;
if (SYN_EXT_CAP_REQUESTS(info->capabilities) >= 1) {
error = synaptics_query_int(psmouse, SYN_QUE_EXT_CAPAB,
&info->ext_cap);
if (error) {
psmouse_warn(psmouse,
"device claims to have extended capabilities, but I'm not able to read them.\n");
} else {
/*
* if nExtBtn is greater than 8 it should be considered
* invalid and treated as 0
*/
if (SYN_CAP_MULTI_BUTTON_NO(info->ext_cap) > 8)
info->ext_cap &= ~SYN_CAP_MB_MASK;
}
}
if (SYN_EXT_CAP_REQUESTS(info->capabilities) >= 4) {
error = synaptics_query_int(psmouse, SYN_QUE_EXT_CAPAB_0C,
&info->ext_cap_0c);
if (error)
psmouse_warn(psmouse,
"device claims to have extended capability 0x0c, but I'm not able to read it.\n");
}
return 0;
}
/*
* Read touchpad resolution and maximum reported coordinates
* Resolution is left zero if touchpad does not support the query
*/
static int synaptics_resolution(struct psmouse *psmouse,
struct synaptics_device_info *info)
{
u8 resp[3];
int error;
if (SYN_ID_MAJOR(info->identity) < 4)
return 0;
error = synaptics_send_cmd(psmouse, SYN_QUE_RESOLUTION, resp);
if (!error) {
if (resp[0] != 0 && (resp[1] & 0x80) && resp[2] != 0) {
info->x_res = resp[0]; /* x resolution in units/mm */
info->y_res = resp[2]; /* y resolution in units/mm */
}
}
if (SYN_EXT_CAP_REQUESTS(info->capabilities) >= 5 &&
SYN_CAP_MAX_DIMENSIONS(info->ext_cap_0c)) {
error = synaptics_send_cmd(psmouse,
SYN_QUE_EXT_MAX_COORDS, resp);
if (error) {
psmouse_warn(psmouse,
"device claims to have max coordinates query, but I'm not able to read it.\n");
} else {
info->x_max = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
info->y_max = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
psmouse_info(psmouse,
"queried max coordinates: x [..%d], y [..%d]\n",
info->x_max, info->y_max);
}
}
if (SYN_CAP_MIN_DIMENSIONS(info->ext_cap_0c) &&
(SYN_EXT_CAP_REQUESTS(info->capabilities) >= 7 ||
/*
* Firmware v8.1 does not report proper number of extended
* capabilities, but has been proven to report correct min
* coordinates.
*/
SYN_ID_FULL(info->identity) == 0x801)) {
error = synaptics_send_cmd(psmouse,
SYN_QUE_EXT_MIN_COORDS, resp);
if (error) {
psmouse_warn(psmouse,
"device claims to have min coordinates query, but I'm not able to read it.\n");
} else {
info->x_min = (resp[0] << 5) | ((resp[1] & 0x0f) << 1);
info->y_min = (resp[2] << 5) | ((resp[1] & 0xf0) >> 3);
psmouse_info(psmouse,
"queried min coordinates: x [%d..], y [%d..]\n",
info->x_min, info->y_min);
}
}
return 0;
}
static int synaptics_query_hardware(struct psmouse *psmouse,
struct synaptics_device_info *info)
{
int error;
memset(info, 0, sizeof(*info));
error = synaptics_identify(psmouse, info);
if (error)
return error;
error = synaptics_model_id(psmouse, info);
if (error)
return error;
error = synaptics_firmware_id(psmouse, info);
if (error)
return error;
error = synaptics_query_modes(psmouse, info);
if (error)
return error;
error = synaptics_capability(psmouse, info);
if (error)
return error;
error = synaptics_resolution(psmouse, info);
if (error)
return error;
return 0;
}
#endif /* CONFIG_MOUSE_PS2_SYNAPTICS || CONFIG_MOUSE_PS2_SYNAPTICS_SMBUS */
#ifdef CONFIG_MOUSE_PS2_SYNAPTICS
static bool cr48_profile_sensor;
#define ANY_BOARD_ID 0
struct min_max_quirk {
const char * const *pnp_ids;
struct {
u32 min, max;
} board_id;
u32 x_min, x_max, y_min, y_max;
};
static const struct min_max_quirk min_max_pnpid_table[] = {
{
(const char * const []){"LEN0033", NULL},
{ANY_BOARD_ID, ANY_BOARD_ID},
1024, 5052, 2258, 4832
},
{
(const char * const []){"LEN0042", NULL},
{ANY_BOARD_ID, ANY_BOARD_ID},
1232, 5710, 1156, 4696
},
{
(const char * const []){"LEN0034", "LEN0036", "LEN0037",
"LEN0039", "LEN2002", "LEN2004",
NULL},
{ANY_BOARD_ID, 2961},
1024, 5112, 2024, 4832
},
{
(const char * const []){"LEN2000", NULL},
{ANY_BOARD_ID, ANY_BOARD_ID},
1024, 5113, 2021, 4832
},
{
(const char * const []){"LEN2001", NULL},
{ANY_BOARD_ID, ANY_BOARD_ID},
1024, 5022, 2508, 4832
},
{
(const char * const []){"LEN2006", NULL},
{2691, 2691},
1024, 5045, 2457, 4832
},
{
(const char * const []){"LEN2006", NULL},
{ANY_BOARD_ID, ANY_BOARD_ID},
1264, 5675, 1171, 4688
},
{ }
};
/*****************************************************************************
* Synaptics communications functions
****************************************************************************/
/*
* Synaptics touchpads report the y coordinate from bottom to top, which is
* opposite from what userspace expects.
* This function is used to invert y before reporting.
*/
static int synaptics_invert_y(int y)
{
return YMAX_NOMINAL + YMIN_NOMINAL - y;
}
/*
* Apply quirk(s) if the hardware matches
*/
static void synaptics_apply_quirks(struct psmouse *psmouse,
struct synaptics_device_info *info)
{
int i;
for (i = 0; min_max_pnpid_table[i].pnp_ids; i++) {
if (!psmouse_matches_pnp_id(psmouse,
min_max_pnpid_table[i].pnp_ids))
continue;
if (min_max_pnpid_table[i].board_id.min != ANY_BOARD_ID &&
info->board_id < min_max_pnpid_table[i].board_id.min)
continue;
if (min_max_pnpid_table[i].board_id.max != ANY_BOARD_ID &&
info->board_id > min_max_pnpid_table[i].board_id.max)
continue;
info->x_min = min_max_pnpid_table[i].x_min;
info->x_max = min_max_pnpid_table[i].x_max;
info->y_min = min_max_pnpid_table[i].y_min;
info->y_max = min_max_pnpid_table[i].y_max;
psmouse_info(psmouse,
"quirked min/max coordinates: x [%d..%d], y [%d..%d]\n",
info->x_min, info->x_max,
info->y_min, info->y_max);
break;
}
}
static bool synaptics_has_agm(struct synaptics_data *priv)
{
return (SYN_CAP_ADV_GESTURE(priv->info.ext_cap_0c) ||
SYN_CAP_IMAGE_SENSOR(priv->info.ext_cap_0c));
}
static int synaptics_set_advanced_gesture_mode(struct psmouse *psmouse)
{
static u8 param = 0xc8;
int error;
error = ps2_sliced_command(&psmouse->ps2dev, SYN_QUE_MODEL);
if (error)
return error;
error = ps2_command(&psmouse->ps2dev, ¶m, PSMOUSE_CMD_SETRATE);
if (error)
return error;
return 0;
}
static int synaptics_set_mode(struct psmouse *psmouse)
{
struct synaptics_data *priv = psmouse->private;
int error;
priv->mode = 0;
if (priv->absolute_mode)
priv->mode |= SYN_BIT_ABSOLUTE_MODE;
if (priv->disable_gesture)
priv->mode |= SYN_BIT_DISABLE_GESTURE;
if (psmouse->rate >= 80)
priv->mode |= SYN_BIT_HIGH_RATE;
if (SYN_CAP_EXTENDED(priv->info.capabilities))
priv->mode |= SYN_BIT_W_MODE;
error = synaptics_mode_cmd(psmouse, priv->mode);
if (error)
return error;
if (priv->absolute_mode && synaptics_has_agm(priv)) {
error = synaptics_set_advanced_gesture_mode(psmouse);
if (error) {
psmouse_err(psmouse,
"Advanced gesture mode init failed: %d\n",
error);
return error;
}
}
return 0;
}
static void synaptics_set_rate(struct psmouse *psmouse, unsigned int rate)
{
struct synaptics_data *priv = psmouse->private;
if (rate >= 80) {
priv->mode |= SYN_BIT_HIGH_RATE;
psmouse->rate = 80;
} else {
priv->mode &= ~SYN_BIT_HIGH_RATE;
psmouse->rate = 40;
}
synaptics_mode_cmd(psmouse, priv->mode);
}
/*****************************************************************************
* Synaptics pass-through PS/2 port support
****************************************************************************/
static int synaptics_pt_write(struct serio *serio, u8 c)
{
struct psmouse *parent = serio_get_drvdata(serio->parent);
u8 rate_param = SYN_PS_CLIENT_CMD; /* indicates that we want pass-through port */
int error;
error = ps2_sliced_command(&parent->ps2dev, c);
if (error)
return error;
error = ps2_command(&parent->ps2dev, &rate_param, PSMOUSE_CMD_SETRATE);
if (error)
return error;
return 0;
}
static int synaptics_pt_start(struct serio *serio)
{
struct psmouse *parent = serio_get_drvdata(serio->parent);
struct synaptics_data *priv = parent->private;
serio_pause_rx(parent->ps2dev.serio);
priv->pt_port = serio;
serio_continue_rx(parent->ps2dev.serio);
return 0;
}
static void synaptics_pt_stop(struct serio *serio)
{
struct psmouse *parent = serio_get_drvdata(serio->parent);
struct synaptics_data *priv = parent->private;
serio_pause_rx(parent->ps2dev.serio);
priv->pt_port = NULL;
serio_continue_rx(parent->ps2dev.serio);
}
static int synaptics_is_pt_packet(u8 *buf)
{
return (buf[0] & 0xFC) == 0x84 && (buf[3] & 0xCC) == 0xC4;
}
static void synaptics_pass_pt_packet(struct serio *ptport, u8 *packet)
{
struct psmouse *child = serio_get_drvdata(ptport);
if (child && child->state == PSMOUSE_ACTIVATED) {
serio_interrupt(ptport, packet[1], 0);
serio_interrupt(ptport, packet[4], 0);
serio_interrupt(ptport, packet[5], 0);
if (child->pktsize == 4)
serio_interrupt(ptport, packet[2], 0);
} else {
serio_interrupt(ptport, packet[1], 0);
}
}
static void synaptics_pt_activate(struct psmouse *psmouse)
{
struct synaptics_data *priv = psmouse->private;
struct psmouse *child = serio_get_drvdata(priv->pt_port);
/* adjust the touchpad to child's choice of protocol */
if (child) {
if (child->pktsize == 4)
priv->mode |= SYN_BIT_FOUR_BYTE_CLIENT;
else
priv->mode &= ~SYN_BIT_FOUR_BYTE_CLIENT;
if (synaptics_mode_cmd(psmouse, priv->mode))
psmouse_warn(psmouse,
"failed to switch guest protocol\n");
}
}
static void synaptics_pt_create(struct psmouse *psmouse)
{
struct serio *serio;
serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
if (!serio) {
psmouse_err(psmouse,
"not enough memory for pass-through port\n");
return;
}
serio->id.type = SERIO_PS_PSTHRU;
strlcpy(serio->name, "Synaptics pass-through", sizeof(serio->name));
strlcpy(serio->phys, "synaptics-pt/serio0", sizeof(serio->name));
serio->write = synaptics_pt_write;
serio->start = synaptics_pt_start;
serio->stop = synaptics_pt_stop;
serio->parent = psmouse->ps2dev.serio;
psmouse->pt_activate = synaptics_pt_activate;
psmouse_info(psmouse, "serio: %s port at %s\n",
serio->name, psmouse->phys);
serio_register_port(serio);
}
/*****************************************************************************
* Functions to interpret the absolute mode packets
****************************************************************************/
static void synaptics_parse_agm(const u8 buf[],
struct synaptics_data *priv,
struct synaptics_hw_state *hw)
{
struct synaptics_hw_state *agm = &priv->agm;
int agm_packet_type;
agm_packet_type = (buf[5] & 0x30) >> 4;
switch (agm_packet_type) {
case 1:
/* Gesture packet: (x, y, z) half resolution */
agm->w = hw->w;
agm->x = (((buf[4] & 0x0f) << 8) | buf[1]) << 1;
agm->y = (((buf[4] & 0xf0) << 4) | buf[2]) << 1;
agm->z = ((buf[3] & 0x30) | (buf[5] & 0x0f)) << 1;
break;
case 2:
/* AGM-CONTACT packet: we are only interested in the count */
priv->agm_count = buf[1];
break;
default:
break;
}
}
static void synaptics_parse_ext_buttons(const u8 buf[],
struct synaptics_data *priv,
struct synaptics_hw_state *hw)
{
unsigned int ext_bits =
(SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap) + 1) >> 1;
unsigned int ext_mask = GENMASK(ext_bits - 1, 0);
hw->ext_buttons = buf[4] & ext_mask;
hw->ext_buttons |= (buf[5] & ext_mask) << ext_bits;
}
static int synaptics_parse_hw_state(const u8 buf[],
struct synaptics_data *priv,
struct synaptics_hw_state *hw)
{
memset(hw, 0, sizeof(struct synaptics_hw_state));
if (SYN_MODEL_NEWABS(priv->info.model_id)) {
hw->w = (((buf[0] & 0x30) >> 2) |
((buf[0] & 0x04) >> 1) |
((buf[3] & 0x04) >> 2));
if (synaptics_has_agm(priv) && hw->w == 2) {
synaptics_parse_agm(buf, priv, hw);
return 1;
}
hw->x = (((buf[3] & 0x10) << 8) |
((buf[1] & 0x0f) << 8) |
buf[4]);
hw->y = (((buf[3] & 0x20) << 7) |
((buf[1] & 0xf0) << 4) |
buf[5]);
hw->z = buf[2];
hw->left = (buf[0] & 0x01) ? 1 : 0;
hw->right = (buf[0] & 0x02) ? 1 : 0;
if (priv->is_forcepad) {
/*
* ForcePads, like Clickpads, use middle button
* bits to report primary button clicks.
* Unfortunately they report primary button not
* only when user presses on the pad above certain
* threshold, but also when there are more than one
* finger on the touchpad, which interferes with
* out multi-finger gestures.
*/
if (hw->z == 0) {
/* No contacts */
priv->press = priv->report_press = false;
} else if (hw->w >= 4 && ((buf[0] ^ buf[3]) & 0x01)) {
/*
* Single-finger touch with pressure above
* the threshold. If pressure stays long
* enough, we'll start reporting primary
* button. We rely on the device continuing
* sending data even if finger does not
* move.
*/
if (!priv->press) {
priv->press_start = jiffies;
priv->press = true;
} else if (time_after(jiffies,
priv->press_start +
msecs_to_jiffies(50))) {
priv->report_press = true;
}
} else {
priv->press = false;
}
hw->left = priv->report_press;
} else if (SYN_CAP_CLICKPAD(priv->info.ext_cap_0c)) {
/*
* Clickpad's button is transmitted as middle button,
* however, since it is primary button, we will report
* it as BTN_LEFT.
*/
hw->left = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
} else if (SYN_CAP_MIDDLE_BUTTON(priv->info.capabilities)) {
hw->middle = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
if (hw->w == 2)
hw->scroll = (s8)buf[1];
}
if (SYN_CAP_FOUR_BUTTON(priv->info.capabilities)) {
hw->up = ((buf[0] ^ buf[3]) & 0x01) ? 1 : 0;
hw->down = ((buf[0] ^ buf[3]) & 0x02) ? 1 : 0;
}
if (SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap) > 0 &&
((buf[0] ^ buf[3]) & 0x02)) {
synaptics_parse_ext_buttons(buf, priv, hw);
}
} else {
hw->x = (((buf[1] & 0x1f) << 8) | buf[2]);
hw->y = (((buf[4] & 0x1f) << 8) | buf[5]);
hw->z = (((buf[0] & 0x30) << 2) | (buf[3] & 0x3F));
hw->w = (((buf[1] & 0x80) >> 4) | ((buf[0] & 0x04) >> 1));
hw->left = (buf[0] & 0x01) ? 1 : 0;
hw->right = (buf[0] & 0x02) ? 1 : 0;
}
/*
* Convert wrap-around values to negative. (X|Y)_MAX_POSITIVE
* is used by some firmware to indicate a finger at the edge of
* the touchpad whose precise position cannot be determined, so
* convert these values to the maximum axis value.
*/
if (hw->x > X_MAX_POSITIVE)
hw->x -= 1 << ABS_POS_BITS;
else if (hw->x == X_MAX_POSITIVE)
hw->x = XMAX;
if (hw->y > Y_MAX_POSITIVE)
hw->y -= 1 << ABS_POS_BITS;
else if (hw->y == Y_MAX_POSITIVE)
hw->y = YMAX;
return 0;
}
static void synaptics_report_semi_mt_slot(struct input_dev *dev, int slot,
bool active, int x, int y)
{
input_mt_slot(dev, slot);
input_mt_report_slot_state(dev, MT_TOOL_FINGER, active);
if (active) {
input_report_abs(dev, ABS_MT_POSITION_X, x);
input_report_abs(dev, ABS_MT_POSITION_Y, synaptics_invert_y(y));
}
}
static void synaptics_report_semi_mt_data(struct input_dev *dev,
const struct synaptics_hw_state *a,
const struct synaptics_hw_state *b,
int num_fingers)
{
if (num_fingers >= 2) {
synaptics_report_semi_mt_slot(dev, 0, true, min(a->x, b->x),
min(a->y, b->y));
synaptics_report_semi_mt_slot(dev, 1, true, max(a->x, b->x),
max(a->y, b->y));
} else if (num_fingers == 1) {
synaptics_report_semi_mt_slot(dev, 0, true, a->x, a->y);
synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
} else {
synaptics_report_semi_mt_slot(dev, 0, false, 0, 0);
synaptics_report_semi_mt_slot(dev, 1, false, 0, 0);
}
}
static void synaptics_report_ext_buttons(struct psmouse *psmouse,
const struct synaptics_hw_state *hw)
{
struct input_dev *dev = psmouse->dev;
struct synaptics_data *priv = psmouse->private;
int ext_bits = (SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap) + 1) >> 1;
int i;
if (!SYN_CAP_MULTI_BUTTON_NO(priv->info.ext_cap))
return;
/* Bug in FW 8.1 & 8.2, buttons are reported only when ExtBit is 1 */
if ((SYN_ID_FULL(priv->info.identity) == 0x801 ||
SYN_ID_FULL(priv->info.identity) == 0x802) &&
!((psmouse->packet[0] ^ psmouse->packet[3]) & 0x02))
return;
if (!SYN_CAP_EXT_BUTTONS_STICK(priv->info.ext_cap_10)) {
for (i = 0; i < ext_bits; i++) {
input_report_key(dev, BTN_0 + 2 * i,
hw->ext_buttons & BIT(i));
input_report_key(dev, BTN_1 + 2 * i,
hw->ext_buttons & BIT(i + ext_bits));
}
return;
}
/*
* This generation of touchpads has the trackstick buttons
* physically wired to the touchpad. Re-route them through
* the pass-through interface.
*/
if (priv->pt_port) {
u8 pt_buttons;
/* The trackstick expects at most 3 buttons */
pt_buttons = SYN_EXT_BUTTON_STICK_L(hw->ext_buttons) |
SYN_EXT_BUTTON_STICK_R(hw->ext_buttons) << 1 |
SYN_EXT_BUTTON_STICK_M(hw->ext_buttons) << 2;
serio_interrupt(priv->pt_port,
PSMOUSE_OOB_EXTRA_BTNS, SERIO_OOB_DATA);
serio_interrupt(priv->pt_port, pt_buttons, SERIO_OOB_DATA);
}
}
static void synaptics_report_buttons(struct psmouse *psmouse,
const struct synaptics_hw_state *hw)
{
struct input_dev *dev = psmouse->dev;
struct synaptics_data *priv = psmouse->private;
input_report_key(dev, BTN_LEFT, hw->left);
input_report_key(dev, BTN_RIGHT, hw->right);
if (SYN_CAP_MIDDLE_BUTTON(priv->info.capabilities))
input_report_key(dev, BTN_MIDDLE, hw->middle);
if (SYN_CAP_FOUR_BUTTON(priv->info.capabilities)) {
input_report_key(dev, BTN_FORWARD, hw->up);
input_report_key(dev, BTN_BACK, hw->down);
}
synaptics_report_ext_buttons(psmouse, hw);
}
static void synaptics_report_mt_data(struct psmouse *psmouse,
const struct synaptics_hw_state *sgm,
int num_fingers)
{
struct input_dev *dev = psmouse->dev;
struct synaptics_data *priv = psmouse->private;
const struct synaptics_hw_state *hw[2] = { sgm, &priv->agm };
struct input_mt_pos pos[2];
int slot[2], nsemi, i;
nsemi = clamp_val(num_fingers, 0, 2);
for (i = 0; i < nsemi; i++) {
pos[i].x = hw[i]->x;
pos[i].y = synaptics_invert_y(hw[i]->y);
}
input_mt_assign_slots(dev, slot, pos, nsemi, DMAX * priv->info.x_res);
for (i = 0; i < nsemi; i++) {
input_mt_slot(dev, slot[i]);
input_mt_report_slot_state(dev, MT_TOOL_FINGER, true);
input_report_abs(dev, ABS_MT_POSITION_X, pos[i].x);
input_report_abs(dev, ABS_MT_POSITION_Y, pos[i].y);
input_report_abs(dev, ABS_MT_PRESSURE, hw[i]->z);