summaryrefslogtreecommitdiff
path: root/sys/dev/pckbport/synaptics.c
blob: 76582c93018b602b8d7d9806b0fe4b7cb794cc46 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
/*	$NetBSD: synaptics.c,v 1.81 2022/09/28 16:43:00 nia Exp $	*/

/*
 * Copyright (c) 2005, Steve C. Woodford
 * Copyright (c) 2004, Ales Krenek
 * Copyright (c) 2004, Kentaro A. Kurahone
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * 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.
 *   * Neither the name of the authors nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
 * COPYRIGHT OWNER 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.
 *
 */

/*
 * TODO:
 *	- Make the sysctl values per-instance instead of global.
 *	- Consider setting initial scaling factors at runtime according
 *	  to the values returned by the 'Read Resolutions' command.
 *	- Support the serial protocol (we only support PS/2 for now)
 *	- Support auto-repeat for up/down button Z-axis emulation.
 *	- Maybe add some more gestures (can we use Palm support somehow?)
 */

#include "opt_pms.h"

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: synaptics.c,v 1.81 2022/09/28 16:43:00 nia Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <sys/kernel.h>
#include <sys/proc.h>

#include <sys/bus.h>

#include <dev/pckbport/pckbportvar.h>

#include <dev/pckbport/synapticsreg.h>
#include <dev/pckbport/synapticsvar.h>

#include <dev/pckbport/pmsreg.h>
#include <dev/pckbport/pmsvar.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>

/*
 * Absolute-mode packets are decoded and passed around using
 * the following structure.
 */
struct synaptics_packet {
	signed short	sp_x;	/* Unscaled absolute X/Y coordinates */
	signed short	sp_y;
	u_char	sp_z;		/* Z (pressure) */
	signed short	sp_sx;	/* Unscaled absolute X/Y coordinates */
	signed short	sp_sy;  /* for secondary finger */
	u_char	sp_sz;		/* Z (pressure) */
	u_char	sp_w;		/* W (contact patch width) */
	u_char  sp_primary;	/* seen primary finger packet */
	u_char  sp_secondary;	/* seen secondary finger packet */
	u_char	sp_finger_status; /* seen extended finger packet */
	u_char	sp_finger_count; /* number of fingers seen */
	char	sp_left;	/* Left mouse button status */
	char	sp_right;	/* Right mouse button status */
	char	sp_middle;	/* Middle button status (possibly emulated) */
	char	sp_up;		/* Up button status */
	char	sp_down;	/* Down button status */
};

static void pms_synaptics_input(void *, int);
static void pms_synaptics_process_packet(struct pms_softc *,
		struct synaptics_packet *);
static void pms_sysctl_synaptics(struct sysctllog **);
static int pms_sysctl_synaptics_verify(SYSCTLFN_ARGS);

/* Controlled by sysctl. */
static int synaptics_up_down_emul = 3;
static int synaptics_up_down_motion_delta = 1;
static int synaptics_gesture_move = 200;
static int synaptics_gesture_length = 20;
static int synaptics_edge_left = SYNAPTICS_EDGE_LEFT;
static int synaptics_edge_right = SYNAPTICS_EDGE_RIGHT;
static int synaptics_edge_top = SYNAPTICS_EDGE_TOP;
static int synaptics_edge_bottom = SYNAPTICS_EDGE_BOTTOM;
static int synaptics_edge_motion_delta = 32;
static u_int synaptics_finger_high = SYNAPTICS_FINGER_LIGHT + 5;
static u_int synaptics_finger_low = SYNAPTICS_FINGER_LIGHT - 10;
static int synaptics_horiz_pct = 0;
static int synaptics_vert_pct = 0;
static int synaptics_button_pct = 30;
static int synaptics_button_boundary;
static int synaptics_button2;
static int synaptics_button3;
static int synaptics_two_fingers_emul = 0;
static int synaptics_scale_x = 8;
static int synaptics_scale_y = 8;
static int synaptics_scale_z = 32;
static int synaptics_max_speed_x = 32;
static int synaptics_max_speed_y = 32;
static int synaptics_max_speed_z = 2;
static int synaptics_movement_threshold = 4;
static int synaptics_movement_enable = 1;
static int synaptics_button_region_movement = 1;
static bool synaptics_aux_mid_button_scroll = TRUE;
static int synaptics_debug = 0;

#define	DPRINTF(LEVEL, SC, FMT, ARGS...) do					      \
{									      \
	if (synaptics_debug >= LEVEL) {						      \
		struct pms_softc *_dprintf_psc =			      \
		    container_of((SC), struct pms_softc, u.synaptics);	      \
		device_printf(_dprintf_psc->sc_dev, FMT, ##ARGS);	      \
	}								      \
} while (0)

/* Sysctl nodes. */
static int synaptics_button_boundary_nodenum;
static int synaptics_button2_nodenum;
static int synaptics_button3_nodenum;
static int synaptics_up_down_emul_nodenum;
static int synaptics_up_down_motion_delta_nodenum;
static int synaptics_gesture_move_nodenum;
static int synaptics_gesture_length_nodenum;
static int synaptics_edge_left_nodenum;
static int synaptics_edge_right_nodenum;
static int synaptics_edge_top_nodenum;
static int synaptics_edge_bottom_nodenum;
static int synaptics_edge_motion_delta_nodenum;
static int synaptics_finger_high_nodenum;
static int synaptics_finger_low_nodenum;
static int synaptics_two_fingers_emul_nodenum;
static int synaptics_scale_x_nodenum;
static int synaptics_scale_y_nodenum;
static int synaptics_scale_z_nodenum;
static int synaptics_max_speed_x_nodenum;
static int synaptics_max_speed_y_nodenum;
static int synaptics_max_speed_z_nodenum;
static int synaptics_movement_threshold_nodenum;
static int synaptics_movement_enable_nodenum;
static int synaptics_button_region_movement_nodenum;
static int synaptics_aux_mid_button_scroll_nodenum;
static int synaptics_horiz_pct_nodenum;
static int synaptics_vert_pct_nodenum;
static int synaptics_button_pct_nodenum;

/*
 * copy of edges so we can recalculate edge limit if there is 
 * vertical scroll region
 */
static int synaptics_actual_edge_right;
static int synaptics_actual_edge_bottom;

static int synaptics_old_vert_pct = 0;
static int synaptics_old_horiz_pct = 0;
static int synaptics_old_button_pct = 0;
static int synaptics_old_button_boundary = SYNAPTICS_EDGE_BOTTOM;
static int synaptics_old_horiz_edge = SYNAPTICS_EDGE_BOTTOM;
static int synaptics_old_vert_edge = SYNAPTICS_EDGE_RIGHT;

/*
 * This holds the processed packet data, it is global because multiple
 * packets from the trackpad may be processed when handling multiple
 * fingers on the trackpad to gather all the data.
 */
static struct synaptics_packet packet;

static int
synaptics_poll_cmd(struct pms_softc *psc, ...)
{
	u_char cmd[4];
	size_t i;
	va_list ap;

	va_start(ap, psc);

	for (i = 0; i < __arraycount(cmd); i++)
		if ((cmd[i] = (u_char)va_arg(ap, int)) == 0)
			break;
	va_end(ap);

	int res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, i, 0,
    	    NULL, 0);
	if (res)
		aprint_error_dev(psc->sc_dev, "command error %#x\n", cmd[0]);
	return res;
}

static int
synaptics_poll_reset(struct pms_softc *psc)
{
	u_char resp[2];
	int res;

	u_char cmd[1] = { PMS_RESET };
	res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 2,
	    resp, 1);
	aprint_debug_dev(psc->sc_dev, "reset %d 0x%02x 0x%02x\n",
	    res, resp[0], resp[1]);
	return res;
}

static int
synaptics_special_read(struct pms_softc *psc, u_char slice, u_char resp[3])
{
	u_char cmd[1] = { PMS_SEND_DEV_STATUS };
	int res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, slice);

	return res | pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 1, 3, resp, 0);
}

static int
synaptics_special_write(struct pms_softc *psc, u_char command, u_char arg)
{
	int res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot, arg);
	if (res)
		return res;

	u_char cmd[2];
	cmd[0] = PMS_SET_SAMPLE;
	cmd[1] = command;
	res = pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot,
	    cmd, 2, 0, NULL, 0);
	return res;
}

static void
pms_synaptics_set_boundaries(void)
{
	if (synaptics_vert_pct != synaptics_old_vert_pct ) {
		synaptics_edge_right = synaptics_actual_edge_right -
		    ((unsigned long) synaptics_vert_pct *
		    (synaptics_actual_edge_right - synaptics_edge_left)) / 100;
		synaptics_old_vert_pct = synaptics_vert_pct;
		synaptics_old_vert_edge = synaptics_edge_right;
	}

	if (synaptics_edge_right != synaptics_old_vert_edge) {
		if (synaptics_edge_right >= synaptics_actual_edge_right) {
			synaptics_vert_pct = 0;
			synaptics_edge_right = synaptics_actual_edge_right;
		} else {
			synaptics_vert_pct = 100 -
			    ((unsigned long) 100 * synaptics_edge_right) /
			    (synaptics_actual_edge_right - synaptics_edge_left);
		}
		synaptics_old_vert_pct = synaptics_vert_pct;
		synaptics_old_vert_edge = synaptics_edge_right;
	}

	if (synaptics_horiz_pct != synaptics_old_horiz_pct ) {
		synaptics_edge_bottom = synaptics_actual_edge_bottom +
		    ((unsigned long) synaptics_horiz_pct *
		    (synaptics_edge_top - synaptics_actual_edge_bottom)) / 100;
		synaptics_old_horiz_pct = synaptics_horiz_pct;
		synaptics_old_horiz_edge = synaptics_edge_bottom;
	}

	if (synaptics_edge_bottom != synaptics_old_horiz_edge) {
		if (synaptics_edge_bottom <= synaptics_actual_edge_bottom) {
			synaptics_vert_pct = 0;
			synaptics_edge_bottom = synaptics_actual_edge_bottom;
		} else {
			synaptics_horiz_pct = 100 -
			    ((unsigned long) 100 * synaptics_edge_bottom) /
			    (synaptics_edge_top - synaptics_actual_edge_bottom);
		}
		synaptics_old_horiz_edge = synaptics_edge_bottom;
	}

	if (synaptics_button_pct != synaptics_old_button_pct) {
		synaptics_old_button_pct = synaptics_button_pct;
	}

	if (synaptics_button_boundary != synaptics_old_button_boundary) {
		if (synaptics_button_boundary <= synaptics_edge_bottom) {
			synaptics_button_pct = 0;
		} else if (synaptics_button_boundary >= synaptics_edge_top) {
			synaptics_button_pct = 100;
		} else {
			synaptics_button_pct =
			    (synaptics_button_boundary - synaptics_edge_bottom)
			    * 100
			    / (synaptics_edge_top - synaptics_edge_bottom);
		}
		synaptics_old_button_pct = synaptics_button_pct;
	}

	/*
	 * calculate the button boundary
	 */
	if (synaptics_edge_top > synaptics_edge_bottom) {
		synaptics_button_boundary = synaptics_edge_bottom + 
		    ((unsigned long) synaptics_button_pct * 
		    (synaptics_edge_top - synaptics_edge_bottom)) / 100;
	} else {
		synaptics_button_boundary = synaptics_edge_bottom;
	}
	synaptics_old_button_boundary = synaptics_button_boundary;

	synaptics_button2 = synaptics_edge_left +
	    (synaptics_edge_right - synaptics_edge_left) / 3;
	synaptics_button3 = synaptics_edge_left +
	    2 * (synaptics_edge_right - synaptics_edge_left) / 3;

}

static void
pms_synaptics_probe_extended(struct pms_softc *psc)
{
	struct synaptics_softc *sc = &psc->u.synaptics;
	u_char resp[3];
	int res;

	aprint_debug_dev(psc->sc_dev,
	    "synaptics_probe: Capabilities 0x%04x.\n", sc->caps);
	if (sc->caps & SYNAPTICS_CAP_PASSTHROUGH)
		sc->flags |= SYN_FLAG_HAS_PASSTHROUGH;

	if (sc->caps & SYNAPTICS_CAP_PALMDETECT)
		sc->flags |= SYN_FLAG_HAS_PALM_DETECT;

	if (sc->caps & SYNAPTICS_CAP_MULTIDETECT)
		sc->flags |= SYN_FLAG_HAS_MULTI_FINGER;

	if (sc->caps & SYNAPTICS_CAP_MULTIFINGERREPORT)
		sc->flags |= SYN_FLAG_HAS_MULTI_FINGER_REPORT;

	/* Ask about extra buttons to detect up/down. */
	if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08)
	    >= SYNAPTICS_EXTENDED_QUERY)
	{
		res = synaptics_special_read(psc, SYNAPTICS_EXTENDED_QUERY, resp);
		if (res == 0) {
			sc->num_buttons = (resp[1] >> 4);
			if (sc->num_buttons > 0)
				sc->button_mask = sc->button_mask <<
				    ((sc->num_buttons + 1) >> 1);

			aprint_debug_dev(psc->sc_dev,
			    "%s: Extended Buttons: %d.\n", __func__,
			    sc->num_buttons);

			aprint_debug_dev(psc->sc_dev, "%s: Extended "
			    "Capabilities: 0x%02x 0x%02x 0x%02x.\n", __func__,
			    resp[0], resp[1], resp[2]);
			if (sc->num_buttons >= 2) {
				/* Yes. */
				sc->flags |= SYN_FLAG_HAS_UP_DOWN_BUTTONS;
			}
			if (resp[0] & 0x1) {
				/* Vertical scroll area */
				sc->flags |= SYN_FLAG_HAS_VERTICAL_SCROLL;
			}
			if (resp[0] & 0x2) {
				/* Horizontal scroll area */
				sc->flags |= SYN_FLAG_HAS_HORIZONTAL_SCROLL;
			}
			if (resp[0] & 0x4) {
				/* Extended W-Mode */
				sc->flags |= SYN_FLAG_HAS_EXTENDED_WMODE;
			}
		}
	}

	/* Ask about click pad */
	if (((sc->caps & SYNAPTICS_CAP_EXTNUM) + 0x08) >=
	    SYNAPTICS_CONTINUED_CAPABILITIES)
	{
		res = synaptics_special_read(psc,
		    SYNAPTICS_CONTINUED_CAPABILITIES, resp);

/*
 * The following describes response for the
 * SYNAPTICS_CONTINUED_CAPABILITIES query.
 *
 * byte	mask	name			meaning
 * ----	----	-------			------------
 * 0	0x01	adjustable threshold	capacitive button sensitivity
 *					can be adjusted
 * 0	0x02	report max		query 0x0d gives max coord reported
 * 0	0x04	clearpad		sensor is ClearPad product
 * 0	0x08	advanced gesture	not particularly meaningful
 * 0	0x10	clickpad bit 0		1-button ClickPad
 * 0	0x60	multifinger mode	identifies firmware finger counting
 *					(not reporting!) algorithm.
 *					Not particularly meaningful
 * 0	0x80	covered pad		W clipped to 14, 15 == pad mostly covered
 * 1	0x01	clickpad bit 1		2-button ClickPad
 * 1	0x02	deluxe LED controls	touchpad support LED commands
 *					ala multimedia control bar
 * 1	0x04	reduced filtering	firmware does less filtering on
 *					position data, driver should watch
 *					for noise.
 * 1	0x08	image sensor		image sensor tracks 5 fingers, but only
 *					reports 2.
 * 1	0x10	uniform clickpad	whole clickpad moves instead of being
 *					hinged at the top.
 * 1	0x20	report min		query 0x0f gives min coord reported
 */
		if (res == 0) {
			uint val = SYN_CCAP_VALUE(resp);

			aprint_debug_dev(psc->sc_dev, "%s: Continued "
			    "Capabilities 0x%02x 0x%02x 0x%02x.\n", __func__,
			    resp[0], resp[1], resp[2]);
			switch (SYN_CCAP_CLICKPAD_TYPE(val)) {
			case 0: /* not a clickpad */
				break;
			case 1:
				sc->flags |= SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD;
				break;
			case 2:
				sc->flags |= SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD;
				break;
			case 3: /* reserved */
			default:
				/* unreached */
				break;
			}

			if ((val & SYN_CCAP_HAS_ADV_GESTURE_MODE))
				sc->flags |= SYN_FLAG_HAS_ADV_GESTURE_MODE;

			if ((val & SYN_CCAP_REPORT_MAX))
				sc->flags |= SYN_FLAG_HAS_MAX_REPORT;

			if ((val & SYN_CCAP_REPORT_MIN))
				sc->flags |= SYN_FLAG_HAS_MIN_REPORT;
		}
	}
}

static const struct {
	int bit;
	const char *desc;
} syn_flags[] = {
	{ SYN_FLAG_HAS_EXTENDED_WMODE, "Extended W mode", },
	{ SYN_FLAG_HAS_PASSTHROUGH, "Passthrough", },
	{ SYN_FLAG_HAS_MIDDLE_BUTTON, "Middle button", },
	{ SYN_FLAG_HAS_BUTTONS_4_5, "Buttons 4/5", },
	{ SYN_FLAG_HAS_UP_DOWN_BUTTONS, "Up/down buttons", },
	{ SYN_FLAG_HAS_PALM_DETECT, "Palm detect", },
	{ SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD, "One button click pad", },
	{ SYN_FLAG_HAS_TWO_BUTTON_CLICKPAD, "Two button click pad", },
	{ SYN_FLAG_HAS_VERTICAL_SCROLL, "Vertical scroll", },
	{ SYN_FLAG_HAS_HORIZONTAL_SCROLL, "Horizontal scroll", },
	{ SYN_FLAG_HAS_MULTI_FINGER_REPORT, "Multi-finger Report", },
	{ SYN_FLAG_HAS_MULTI_FINGER, "Multi-finger", },
	{ SYN_FLAG_HAS_MAX_REPORT, "Reports max", },
	{ SYN_FLAG_HAS_MIN_REPORT, "Reports min", },
};

int
pms_synaptics_probe_init(void *vsc)
{
	struct pms_softc *psc = vsc;
	struct synaptics_softc *sc = &psc->u.synaptics;
	u_char cmd[1], resp[3];
	int res, ver_minor, ver_major;
	struct sysctllog *clog = NULL;

	res = pms_sliced_command(psc->sc_kbctag, psc->sc_kbcslot,
	    SYNAPTICS_IDENTIFY_TOUCHPAD);
	cmd[0] = PMS_SEND_DEV_STATUS;
	res |= pckbport_poll_cmd(psc->sc_kbctag, psc->sc_kbcslot, cmd, 1, 3,
	    resp, 0);
	if (res) {
		aprint_debug_dev(psc->sc_dev,
		    "synaptics_probe: Identify Touchpad error.\n");
		/*
		 * Reset device in case the probe confused it.
		 */
 doreset:
		(void)synaptics_poll_reset(psc);
		return res;
	}

	if (resp[1] != SYNAPTICS_MAGIC_BYTE) {
		aprint_debug_dev(psc->sc_dev,
		    "synaptics_probe: Not synaptics.\n");
		res = 1;
		goto doreset;
	}

	sc->flags = 0;
	sc->num_buttons = 0;
	sc->button_mask = 0xff;

	pms_synaptics_set_boundaries();

	/* Check for minimum version and print a nice message. */
	ver_major = resp[2] & 0x0f;
	ver_minor = resp[0];
	aprint_normal_dev(psc->sc_dev, "Synaptics touchpad version %d.%d\n",
	    ver_major, ver_minor);
	if (ver_major * 10 + ver_minor < SYNAPTICS_MIN_VERSION) {
		/* No capability query support. */
		sc->caps = 0;
		goto done;
	}


	/* Query the hardware capabilities. */
	res = synaptics_special_read(psc, SYNAPTICS_READ_CAPABILITIES, resp);
	if (res) {
		/* Hmm, failed to get capabilities. */
		aprint_error_dev(psc->sc_dev,
		    "synaptics_probe: Failed to query capabilities.\n");
		goto doreset;
	}

	sc->caps = SYNAPTICS_CAP_VALUE(resp);

	if (sc->caps & SYNAPTICS_CAP_MBUTTON)
		sc->flags |= SYN_FLAG_HAS_MIDDLE_BUTTON;

	if (sc->caps & SYNAPTICS_CAP_4BUTTON)
		sc->flags |= SYN_FLAG_HAS_BUTTONS_4_5;

	if (sc->caps & SYNAPTICS_CAP_EXTENDED) {
		pms_synaptics_probe_extended(psc);
	}

	if (sc->flags) {
		const char comma[] = ", ";
		const char *sep = "";
		aprint_normal_dev(psc->sc_dev, "");
		for (size_t f = 0; f < __arraycount(syn_flags); f++) {
			if (sc->flags & syn_flags[f].bit) {
				aprint_normal("%s%s", sep, syn_flags[f].desc);
				sep = comma;
			}
		}
		aprint_normal("\n");
	}

	if (sc->flags & SYN_FLAG_HAS_MAX_REPORT) {
		res = synaptics_special_read(psc, SYNAPTICS_READ_MAX_COORDS,
		    resp);
		if (res) {
			aprint_error_dev(psc->sc_dev,
			    "synaptics_probe: Failed to query max coords.\n");
		} else {
			synaptics_edge_right = (resp[0] << 5) +
			    ((resp[1] & 0x0f) << 1);
			synaptics_edge_top = (resp[2] << 5) + 
			    ((resp[1] & 0xf0) >> 3);

			synaptics_actual_edge_right = synaptics_edge_right;

			/*
			 * If we have vertical scroll then steal 10%
			 * for that region.
			 */
			if (sc->flags & SYN_FLAG_HAS_VERTICAL_SCROLL)
				synaptics_edge_right -=
				    synaptics_edge_right / 10;

			aprint_normal_dev(psc->sc_dev,
			    "Probed max coordinates right: %d, top: %d\n",
			    synaptics_edge_right, synaptics_edge_top);
		}
	}

	if (sc->flags & SYN_FLAG_HAS_MIN_REPORT) {
		res = synaptics_special_read(psc, SYNAPTICS_READ_MIN_COORDS,
		    resp);
		if (res) {
			aprint_error_dev(psc->sc_dev,
			    "synaptics_probe: Failed to query min coords.\n");
		} else {
			synaptics_edge_left = (resp[0] << 5) +
			    ((resp[1] & 0x0f) << 1);
			synaptics_edge_bottom = (resp[2] << 5) + 
			    ((resp[1] & 0xf0) >> 3);

			synaptics_actual_edge_bottom = synaptics_edge_bottom;

			/*
			 * If we have horizontal scroll then steal 10%
			 * for that region.
			 */
			if (sc->flags & SYN_FLAG_HAS_HORIZONTAL_SCROLL)
				synaptics_horiz_pct = 10;

			aprint_normal_dev(psc->sc_dev,
			    "Probed min coordinates left: %d, bottom: %d\n",
			    synaptics_edge_left, synaptics_edge_bottom);
		}
	}

	pms_synaptics_set_boundaries();

done:
	pms_sysctl_synaptics(&clog);
	pckbport_set_inputhandler(psc->sc_kbctag, psc->sc_kbcslot,
	    pms_synaptics_input, psc, device_xname(psc->sc_dev));

	return (0);
}

void
pms_synaptics_enable(void *vsc)
{
	struct pms_softc *psc = vsc;
	struct synaptics_softc *sc = &psc->u.synaptics;
	u_char enable_modes;
	int res, i;

	if (sc->flags & SYN_FLAG_HAS_PASSTHROUGH) {
		/*
		 * Extended capability probes can confuse the passthrough
		 * device; reset the touchpad now to cure that.
		 */
		res = synaptics_poll_reset(psc);
	}

	/*
	 * Enable Absolute mode with W (width) reporting, and set
	 * the packet rate to maximum (80 packets per second). Enable
	 * extended W mode if supported so we can report second finger
	 * position.
	 */
	enable_modes =
	   SYNAPTICS_MODE_ABSOLUTE | SYNAPTICS_MODE_W | SYNAPTICS_MODE_RATE;

	if (sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE)
		enable_modes |= SYNAPTICS_MODE_EXTENDED_W;

	/*
 	* Synaptics documentation says to disable device before
 	* setting mode.
 	*/
	synaptics_poll_cmd(psc, PMS_DEV_DISABLE, 0);
	/* a couple of set scales to clear out pending commands */
	for (i = 0; i < 2; i++)
		synaptics_poll_cmd(psc, PMS_SET_SCALE11, 0);

	res = synaptics_special_write(psc, SYNAPTICS_CMD_SET_MODE2, enable_modes);
	if (res)
		aprint_error("synaptics: set mode error\n");

	/* a couple of set scales to clear out pending commands */
	for (i = 0; i < 2; i++)
		synaptics_poll_cmd(psc, PMS_SET_SCALE11, 0);

	/* Set advanced gesture mode */
	if ((sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE) ||
	    (sc->flags & SYN_FLAG_HAS_ADV_GESTURE_MODE))
		synaptics_special_write(psc, SYNAPTICS_WRITE_DELUXE_3, 0x3); 

	/* Disable motion in the button region for clickpads */
	if(sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD)
		synaptics_button_region_movement = 0;

	sc->up_down = 0;
	sc->prev_fingers = 0;
	sc->gesture_start_x = sc->gesture_start_y = 0;
	sc->gesture_start_packet = 0;
	sc->gesture_tap_packet = 0;
	sc->gesture_type = 0;
	sc->gesture_buttons = 0;
	sc->total_packets = 0;
	for (i = 0; i < SYN_MAX_FINGERS; i++) {
		sc->rem_x[i] = sc->rem_y[i] = sc->rem_z[i] = 0;
	}
	sc->button_history = 0;

	/* clear the packet decode structure */
	memset(&packet, 0, sizeof(packet));
}

void
pms_synaptics_resume(void *vsc)
{
	(void)synaptics_poll_reset(vsc);
}

static void
pms_sysctl_synaptics(struct sysctllog **clog)
{
	int rc, root_num;
	const struct sysctlnode *node;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "synaptics",
	    SYSCTL_DESCR("Synaptics touchpad controls"),
	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
	    goto err;

	root_num = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "up_down_emulation",
	    SYSCTL_DESCR("Middle button/Z-axis emulation with up/down buttons"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_up_down_emul,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_up_down_emul_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "up_down_motion_delta",
	    SYSCTL_DESCR("Up/down button Z-axis emulation rate"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_up_down_motion_delta,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_up_down_motion_delta_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "gesture_move",
	    SYSCTL_DESCR("Movement greater than this between taps cancels gesture"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_gesture_move,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_gesture_move_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "gesture_length",
	    SYSCTL_DESCR("Time period in which tap is recognised as a gesture"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_gesture_length,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_gesture_length_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "edge_left",
	    SYSCTL_DESCR("Define left edge of touchpad"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_edge_left,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_edge_left_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "edge_right",
	    SYSCTL_DESCR("Define right edge of touchpad"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_edge_right,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_edge_right_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "edge_top",
	    SYSCTL_DESCR("Define top edge of touchpad"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_edge_top,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_edge_top_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "edge_bottom",
	    SYSCTL_DESCR("Define bottom edge of touchpad"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_edge_bottom,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_edge_bottom_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "edge_motion_delta",
	    SYSCTL_DESCR("Define edge motion rate"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_edge_motion_delta,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_edge_motion_delta_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "finger_high",
	    SYSCTL_DESCR("Define finger applied pressure threshold"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_finger_high,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_finger_high_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "finger_low",
	    SYSCTL_DESCR("Define finger removed pressure threshold"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_finger_low,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_finger_low_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "two_fingers_emulation",
	    SYSCTL_DESCR("Map two fingers to middle button"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_two_fingers_emul,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_two_fingers_emul_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "scale_x",
	    SYSCTL_DESCR("Horizontal movement scale factor"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_scale_x,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_scale_x_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "scale_y",
	    SYSCTL_DESCR("Vertical movement scale factor"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_scale_y,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_scale_y_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "scale_z",
	    SYSCTL_DESCR("Sroll wheel emulation scale factor"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_scale_z,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_scale_z_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "max_speed_x",
	    SYSCTL_DESCR("Horizontal movement maximum speed"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_max_speed_x,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_max_speed_x_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "max_speed_y",
	    SYSCTL_DESCR("Vertical movement maximum speed"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_max_speed_y,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_max_speed_y_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "max_speed_z",
	    SYSCTL_DESCR("Scroll wheel emulation maximum speed"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_max_speed_z,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_max_speed_z_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "movement_threshold",
	    SYSCTL_DESCR("Minimum reported movement threshold"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_movement_threshold,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_movement_threshold_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "movement_enable",
	    SYSCTL_DESCR("Enable movement reporting"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_movement_enable,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_movement_enable_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "button_region_movement_enable",
	    SYSCTL_DESCR("Enable movement within clickpad button region"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_button_region_movement,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_button_region_movement_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "button_boundary",
	    SYSCTL_DESCR("Top edge of button area"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_button_boundary,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_button_boundary_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "button2_edge",
	    SYSCTL_DESCR("Left edge of button 2 region"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_button2,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_button2_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "button3_edge",
	    SYSCTL_DESCR("Left edge of button 3 region"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_button3,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_button3_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_BOOL, "aux_mid_button_scroll",
	    SYSCTL_DESCR("Interpret Y-Axis movement with the middle button held as scrolling on the passthrough device (e.g. TrackPoint)"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_aux_mid_button_scroll,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_aux_mid_button_scroll_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "vert_scroll_percent",
	    SYSCTL_DESCR("Percent of trackpad width to reserve for vertical scroll region"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_vert_pct,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_vert_pct_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "horizontal_scroll_percent",
	    SYSCTL_DESCR("Percent of trackpad height to reserve for scroll region"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_horiz_pct,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_horiz_pct_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "button_region_percent",
	    SYSCTL_DESCR("Percent of trackpad height to reserve for button region"),
	    pms_sysctl_synaptics_verify, 0,
	    &synaptics_button_pct,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	synaptics_button_pct_nodenum = node->sysctl_num;

	if ((rc = sysctl_createv(clog, 0, NULL, &node,
	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
	    CTLTYPE_INT, "debug",
	    SYSCTL_DESCR("Enable debug output"),
	    NULL, 0,
	    &synaptics_debug,
	    0, CTL_HW, root_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		goto err;

	return;

err:
	aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc);
}

static int
pms_sysctl_synaptics_verify(SYSCTLFN_ARGS)
{
	int error, t;
	struct sysctlnode node;

	node = *rnode;
	t = *(int *)rnode->sysctl_data;
	node.sysctl_data = &t;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	/* Sanity check the params. */
	if (node.sysctl_num == synaptics_up_down_emul_nodenum) {
		if (t < 0 || t > 3)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_two_fingers_emul_nodenum) {
		if (t < 0 || t > 2)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_gesture_length_nodenum ||
	    node.sysctl_num == synaptics_edge_motion_delta_nodenum ||
	    node.sysctl_num == synaptics_up_down_motion_delta_nodenum ||
	    node.sysctl_num == synaptics_max_speed_x_nodenum ||
	    node.sysctl_num == synaptics_max_speed_y_nodenum ||
	    node.sysctl_num == synaptics_max_speed_z_nodenum) {
		if (t < 0)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_edge_left_nodenum ||
	    node.sysctl_num == synaptics_edge_bottom_nodenum) {
		if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 2))
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_edge_right_nodenum ||
	    node.sysctl_num == synaptics_edge_top_nodenum) {
		if (t < (SYNAPTICS_EDGE_MAX / 2))
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_scale_x_nodenum ||
	    node.sysctl_num == synaptics_scale_y_nodenum ||
	    node.sysctl_num == synaptics_scale_z_nodenum) {
		if (t < 1 || t > (SYNAPTICS_EDGE_MAX / 4))
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_finger_high_nodenum) {
		if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
		    t < synaptics_finger_low)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_finger_low_nodenum) {
		if (t < 0 || t > SYNAPTICS_FINGER_PALM ||
		    t > synaptics_finger_high)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_gesture_move_nodenum ||
	    node.sysctl_num == synaptics_movement_threshold_nodenum) {
		if (t < 0 || t > (SYNAPTICS_EDGE_MAX / 4))
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_button_boundary_nodenum) {
		if (t < 0 || t < synaptics_edge_bottom ||
		    t > synaptics_edge_top)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_button2_nodenum ||
	    node.sysctl_num == synaptics_button3_nodenum) {
		if (t < synaptics_edge_left || t > synaptics_edge_right)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_movement_enable_nodenum) {
		if (t < 0 || t > 1)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_button_region_movement_nodenum) {
		if (t < 0 || t > 1)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_aux_mid_button_scroll_nodenum) {
		if (t < 0 || t > 1)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_vert_pct_nodenum) {
		if (t < 0 || t > 100)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_horiz_pct_nodenum) {
		if (t < 0 || t > 100)
			return (EINVAL);
	} else
	if (node.sysctl_num == synaptics_button_pct_nodenum) {
		if (t < 0 || t > 100)
			return (EINVAL);
	} else
		return (EINVAL);

	*(int *)rnode->sysctl_data = t;

	pms_synaptics_set_boundaries();

	return (0);
}

/*
 * Extract the number of fingers from the current packet and return
 * it to the caller.
 */
static unsigned
pms_synaptics_get_fingers(struct pms_softc *psc, u_char w, short z)
{
	struct synaptics_softc *sc = &psc->u.synaptics;
	unsigned short ew_mode;
	unsigned fingers;

	fingers = 0;


	/*
	 * If w is zero and z says no fingers then return
	 * no fingers, w == can also mean 2 fingers... confusing.
	 */
	if (w == 0 && z == SYNAPTICS_FINGER_NONE)
		return 0;

	if ((sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE) &&
	    (w == SYNAPTICS_WIDTH_EXTENDED_W)) {
		ew_mode = psc->packet[5] >> 4;
		switch (ew_mode)
		{
		case SYNAPTICS_EW_WHEEL:
			break;

		case SYNAPTICS_EW_SECONDARY_FINGER:
			/* to get here we must have 2 fingers at least */
			fingers = 2;
			break;

		case SYNAPTICS_EW_FINGER_STATUS:
			fingers = psc->packet[1] & 0x0f;
			break;

		default:
			aprint_error_dev(psc->sc_dev,
			    "invalid extended w mode %d\n",
			    ew_mode);
			return 0; /* pretend there are no fingers */
		}
	} else {

		fingers = 1;

		/*
		 * If SYN_FLAG_HAS_MULTI_FINGER is set then check
		 * sp_w is below SYNAPTICS_WIDTH_FINGER_MIN, if it is
		 * then this will be the finger count.
		 *
		 * There are a couple of "special" values otherwise
		 * just punt with one finger, if this really is a palm
		 * then it will be caught later.
		 */
		if (sc->flags & (SYN_FLAG_HAS_MULTI_FINGER | SYN_FLAG_HAS_MULTI_FINGER_REPORT)) {
			if (w == SYNAPTICS_WIDTH_TWO_FINGERS)
				fingers = 2;
			else if (w == SYNAPTICS_WIDTH_THREE_OR_MORE)
				fingers = 3;
		}

	}

	return fingers;
}

/* Masks for the first byte of a packet */
#define PMS_LBUTMASK 0x01
#define PMS_RBUTMASK 0x02
#define PMS_MBUTMASK 0x04

static void
pms_synaptics_parse(struct pms_softc *psc)
{
	struct synaptics_softc *sc = &psc->u.synaptics;
	struct synaptics_packet nsp;
	char new_buttons, ew_mode;
	uint8_t btn_mask, packet4, packet5;
	unsigned v, primary_finger, secondary_finger;
	int ext_left = -1, ext_right = -1, ext_middle = -1,
	    ext_up = -1, ext_down = -1;

	sc->total_packets++;

	memcpy(&nsp, &packet, sizeof(packet));

	/* Width of finger */
	nsp.sp_w = ((psc->packet[0] & 0x30) >> 2)
	    + ((psc->packet[0] & 0x04) >> 1)
	    + ((psc->packet[3] & 0x04) >> 2);

	v = 0;
	primary_finger = 0;
	secondary_finger = 0;
	if ((sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE) &&
	    (nsp.sp_w == SYNAPTICS_WIDTH_EXTENDED_W)) {
		ew_mode = psc->packet[5] >> 4;
		switch (ew_mode)
		{
		case SYNAPTICS_EW_WHEEL:
			/* scroll wheel report, ignore for now */
			aprint_debug_dev(psc->sc_dev, "mouse wheel packet\n");
			return;

		case SYNAPTICS_EW_SECONDARY_FINGER:
			/* parse the second finger report */

			nsp.sp_secondary = 1;

			nsp.sp_sx = ((psc->packet[1] & 0xfe) << 1)
			    + ((psc->packet[4] & 0x0f) << 9);
			nsp.sp_sy = ((psc->packet[2] & 0xfe) << 1)
			    + ((psc->packet[4] & 0xf0) << 5);
			nsp.sp_sz = (psc->packet[3] & 0x30)
			    + ((psc->packet[5] & 0x0e) << 1);

			/*
			 * Check if the x and y are non-zero that they
			 * are within the bounds of the trackpad
			 * otherwise ignore the packet.
			 */
			if (((nsp.sp_sx != 0) &&
			    ((nsp.sp_sx < synaptics_edge_left) ||
			     (nsp.sp_sx > synaptics_edge_right))) ||
			   ((nsp.sp_sy != 0) &&
			    ((nsp.sp_sy < synaptics_edge_bottom) ||
			     (nsp.sp_sy > synaptics_edge_top)))) {
				sc->gesture_type = 0;
				sc->gesture_buttons = 0;
				sc->total_packets--;
				DPRINTF(20, sc,
				    "synaptics_parse: dropping out of bounds "
				    "packet sp_sx %d sp_sy %d\n",
				    nsp.sp_sx, nsp.sp_sy);
				return;
			}

			/* work out the virtual finger width */
			v = 8 + (psc->packet[1] & 0x01) +
				((psc->packet[2] & 0x01) << 1) +
				((psc->packet[5] & 0x01) << 2);

			/* keep same buttons down as primary */
			nsp.sp_left = sc->button_history & PMS_LBUTMASK;
			nsp.sp_middle = sc->button_history & PMS_MBUTMASK;
			nsp.sp_right = sc->button_history & PMS_RBUTMASK;
			break;

		case SYNAPTICS_EW_FINGER_STATUS:
			/* This works but what is it good for?
			 * it gives us an index of the primary/secondary
			 * fingers but no other packets pass this
			 * index.
			 *
			 * XXX Park this, possibly handle a finger
			 * XXX change if indexes change.
			 */
			primary_finger = psc->packet[2];
			secondary_finger = psc->packet[4];
			nsp.sp_finger_status = 1;
			nsp.sp_finger_count = pms_synaptics_get_fingers(psc,
			    nsp.sp_w, nsp.sp_z);
			goto skip_position;

		default:
			aprint_error_dev(psc->sc_dev,
			    "invalid extended w mode %d\n",
			    ew_mode);
			return;
		}
	} else {
		nsp.sp_primary = 1;

		/*
		 * If the trackpad has external buttons and one of
		 * those buttons is pressed then the lower bits of
		 * x and y are "stolen" for button status. We can tell
		 * this has happened by doing an xor of the two right
		 * button status bits residing in byte 0 and 3, if the
		 * result is non-zero then there is an external button
		 * report and the position bytes need to be masked.
		 */
		btn_mask = 0xff;
		if ((sc->num_buttons > 0) &&
		    ((psc->packet[0] & PMS_RBUTMASK) ^
		     (psc->packet[3] & PMS_RBUTMASK))) {
			btn_mask = sc->button_mask;
		}

		packet4 = psc->packet[4] & btn_mask;
		packet5 = psc->packet[5] & btn_mask;

		/*
		 * If SYN_FLAG_HAS_MULTI_FINGER is set then check
		 * sp_w is below SYNAPTICS_WIDTH_FINGER_MIN, if it is
		 * then this will be the finger count.
		 *
		 * There are a couple of "special" values otherwise
		 * just punt with one finger, if this really is a palm
		 * then it will be caught later.
		 */
		if ((sc->flags & SYN_FLAG_HAS_MULTI_FINGER) &&
		    ((nsp.sp_w == SYNAPTICS_WIDTH_TWO_FINGERS) ||
		     (nsp.sp_w == SYNAPTICS_WIDTH_THREE_OR_MORE))) {
			/*
			 * To make life interesting if there are
			 * two or more fingers on the touchpad then
			 * the coordinate reporting changes and an extra
			 * "virtual" finger width is reported.
			 */
			nsp.sp_x = (packet4 & 0xfc) +
				((packet4 & 0x02) << 1) +
				((psc->packet[1] & 0x0f) << 8) +
				((psc->packet[3] & 0x10) << 8);

			nsp.sp_y = (packet5 & 0xfc) +
				((packet5 & 0x02) << 1) +
				((psc->packet[1] & 0xf0) << 4) +
				((psc->packet[3] & 0x20) << 7);

			/* Pressure */
			nsp.sp_z = psc->packet[2] & 0xfe;

			/* derive the virtual finger width */
			v = 8 + ((packet4 & 0x02) >> 1) +
				(packet5 & 0x02) +
				((psc->packet[2] & 0x01) << 2);

		} else {
			/* Absolute X/Y coordinates of finger */
			nsp.sp_x = packet4 +
				((psc->packet[1] & 0x0f) << 8) +
				((psc->packet[3] & 0x10) << 8);

			nsp.sp_y = packet5 +
				((psc->packet[1] & 0xf0) << 4) +
				((psc->packet[3] & 0x20) << 7);

			/* Pressure */
			nsp.sp_z = psc->packet[2];
		}

		/*
		 * Check if the x and y are non-zero that they
		 * are within the bounds of the trackpad
		 * otherwise ignore the packet.
		 */
		if (((nsp.sp_x != 0) &&
		    ((nsp.sp_x < synaptics_edge_left) ||
		     (nsp.sp_x > synaptics_edge_right))) ||
		    ((nsp.sp_y != 0) &&
		    ((nsp.sp_y < synaptics_edge_bottom) ||
		     (nsp.sp_y > synaptics_edge_top)))) {
			sc->gesture_type = 0;
			sc->gesture_buttons = 0;
			sc->total_packets--;
			DPRINTF(20, sc,
			    "synaptics_parse: dropping out of bounds packet "
			    "sp_x %d sp_y %d\n",
			    nsp.sp_x, nsp.sp_y);
			return;
		}

		nsp.sp_finger_count = pms_synaptics_get_fingers(psc,
		    nsp.sp_w, nsp.sp_z);

		/*
		 * We don't have extended W so we only know if there
		 * are multiple fingers on the touchpad, only the primary
		 * location is reported so just pretend we have an
		 * unmoving second finger.
		 */
		if (((sc->flags & SYN_FLAG_HAS_EXTENDED_WMODE)
			!= SYN_FLAG_HAS_EXTENDED_WMODE) &&
		    (nsp.sp_finger_count > 1)) {
			nsp.sp_secondary = 1;
			nsp.sp_sx = 0;
			nsp.sp_sy = 0;
			nsp.sp_sz = 0;
		}

		if ((psc->packet[0] ^ psc->packet[3]) & 0x02) {
			/* extended buttons */

			aprint_debug_dev(psc->sc_dev,
			    "synaptics_parse: %02x %02x %02x %02x %02x %02x\n",
			    psc->packet[0], psc->packet[1], psc->packet[2],
			    psc->packet[3], psc->packet[4], psc->packet[5]);

			if ((psc->packet[4] & SYN_1BUTMASK) != 0)
				ext_left = PMS_LBUTMASK;
			else
				ext_left = 0;

			if ((psc->packet[4] & SYN_3BUTMASK) != 0)
				ext_middle = PMS_MBUTMASK;
			else
				ext_middle = 0;

			if ((psc->packet[5] & SYN_2BUTMASK) != 0)
				ext_right = PMS_RBUTMASK;
			else
				ext_right = 0;

			if ((psc->packet[5] & SYN_4BUTMASK) != 0)
				ext_up = 1;
			else
				ext_up = 0;

			if ((psc->packet[4] & SYN_5BUTMASK) != 0)
				ext_down = 1;
			else
				ext_down = 0;
		} else {
			/* Left/Right button handling. */
			nsp.sp_left = psc->packet[0] & PMS_LBUTMASK;
			nsp.sp_right = psc->packet[0] & PMS_RBUTMASK;
		}

		/* Up/Down buttons. */
		if (sc->flags & SYN_FLAG_HAS_BUTTONS_4_5) {
			/* Old up/down buttons. */
			nsp.sp_up = nsp.sp_left ^
		    	    (psc->packet[3] & PMS_LBUTMASK);
			nsp.sp_down = nsp.sp_right ^
		    	    (psc->packet[3] & PMS_RBUTMASK);
		} else if (sc->flags & SYN_FLAG_HAS_UP_DOWN_BUTTONS &&
	   	    ((psc->packet[0] & PMS_RBUTMASK) ^
	   	    (psc->packet[3] & PMS_RBUTMASK))) {
			/* New up/down button. */
			nsp.sp_up = psc->packet[4] & SYN_1BUTMASK;
			nsp.sp_down = psc->packet[5] & SYN_2BUTMASK;
		} else {
			nsp.sp_up = 0;
			nsp.sp_down = 0;
		}

		new_buttons = 0;
		if(sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) {
			/* This is not correctly specified. Read this button press
		 	* from L/U bit.  Emulate 3 buttons by checking the
		 	* coordinates of the click and returning the appropriate
		 	* button code.  Outside the button region default to a
		 	* left click.
		 	*/
			u_char bstate = (psc->packet[0] ^ psc->packet[3])
					    & 0x01;
			if (nsp.sp_y < synaptics_button_boundary) {
				if (nsp.sp_x > synaptics_button3) {
					nsp.sp_right =
			   			bstate ? PMS_RBUTMASK : 0;
				} else if (nsp.sp_x > synaptics_button2) {
					nsp.sp_middle =
				   		bstate ? PMS_MBUTMASK : 0;
				} else {
					nsp.sp_left = bstate ? PMS_LBUTMASK : 0;
				}
			} else
				nsp.sp_left = bstate ? 1 : 0;
			new_buttons = nsp.sp_left | nsp.sp_middle | nsp.sp_right;
			if (new_buttons != sc->button_history) {
				if (sc->button_history == 0)
					sc->button_history = new_buttons;
				else if (new_buttons == 0) {
					sc->button_history = 0;
				       /* ensure all buttons are cleared just in
				 	* case finger comes off in a different
				 	* region.
				 	*/
					nsp.sp_left = 0;
					nsp.sp_middle = 0;
					nsp.sp_right = 0;
				} else {
					/* make sure we keep the same button even
				 	* if the finger moves to a different
				 	* region.  This precludes chording
				 	* but, oh well.
				 	*/
					nsp.sp_left = sc->button_history & PMS_LBUTMASK;
					nsp.sp_middle = sc->button_history
				    	& PMS_MBUTMASK;
					nsp.sp_right = sc->button_history & PMS_RBUTMASK;
				}
			}
		} else if (sc->flags & SYN_FLAG_HAS_MIDDLE_BUTTON) {
			/* Old style Middle Button. */
			nsp.sp_middle = (psc->packet[0] & PMS_LBUTMASK) ^
		    	    (psc->packet[3] & PMS_LBUTMASK);
		} else {
			nsp.sp_middle = 0;
		}

		/*
		 * Overlay extended button state if anything changed,
		 * preserve the state if a button is being held.
		 */
		if (ext_left != -1)
			nsp.sp_left = sc->ext_left = ext_left;
		else if (sc->ext_left != 0)
			nsp.sp_left = sc->ext_left;

		if (ext_right != -1)
			nsp.sp_right = sc->ext_right = ext_right;
		else if (sc->ext_right != 0)
			nsp.sp_right = sc->ext_right;

		if (ext_middle != -1)
			nsp.sp_middle = sc->ext_middle = ext_middle;
		else if (sc->ext_middle != 0)
			nsp.sp_middle = sc->ext_middle;

		if (ext_up != -1)
			nsp.sp_up = sc->ext_up = ext_up;
		else if (sc->ext_up != 0)
			nsp.sp_up = sc->ext_up;

		if (ext_down != -1)
			nsp.sp_down = sc->ext_down = ext_down;
		else if (sc->ext_down != 0)
			nsp.sp_down = sc->ext_down;

		switch (synaptics_up_down_emul) {
		case 1:
			/* Do middle button emulation using up/down buttons */
			nsp.sp_middle = nsp.sp_up | nsp.sp_down;
			nsp.sp_up = nsp.sp_down = 0;
			break;
		case 3:
			/* Do left/right button emulation using up/down buttons */
			nsp.sp_left = nsp.sp_left | nsp.sp_up;
			nsp.sp_right = nsp.sp_right | nsp.sp_down;
			nsp.sp_up = nsp.sp_down = 0;
			break;
		default:
			/*
			 * Don't do any remapping...
			 * Z-axis emulation is handled in pms_synaptics_process_packet
			 */
			break;
		}
	}

	/* set the finger count only if we haven't seen an extended-w
	 * finger count status
	 */
	if (nsp.sp_finger_status == 0)
		nsp.sp_finger_count = pms_synaptics_get_fingers(psc, nsp.sp_w,
		    nsp.sp_z);

skip_position:
	DPRINTF(20, sc,
	    "synaptics_parse: sp_x %d sp_y %d sp_z %d, sp_sx %d, sp_sy %d, "
	    "sp_sz %d, sp_w %d sp_finger_count %d, sp_primary %d, "
	    "sp_secondary %d, v %d, primary_finger %d, secondary_finger %d\n",
	    nsp.sp_x, nsp.sp_y, nsp.sp_z, nsp.sp_sx,
	    nsp.sp_sy, nsp.sp_sz, nsp.sp_w, nsp.sp_finger_count, 
	    nsp.sp_primary, nsp.sp_secondary, v, primary_finger,
	    secondary_finger);

	pms_synaptics_process_packet(psc, &nsp);

	/* Clear fingers */
	if ((nsp.sp_primary && nsp.sp_finger_count <= 1) || nsp.sp_secondary) {
		nsp.sp_primary = 0;
		nsp.sp_secondary = 0;
		nsp.sp_finger_status = 0;
	}

	memcpy(&packet, &nsp, sizeof(packet));
}

/*
 * Passthrough is used for e.g. TrackPoints and additional pointing
 * devices connected to a Synaptics touchpad.
 */
static void
pms_synaptics_passthrough(struct pms_softc *psc)
{
	int dx, dy, dz;
	int buttons, changed;
	int s;

	buttons = ((psc->packet[1] & PMS_LBUTMASK) ? 0x20 : 0) |
		((psc->packet[1] & PMS_MBUTMASK) ? 0x40 : 0) |
		((psc->packet[1] & PMS_RBUTMASK) ? 0x80 : 0);

	dx = psc->packet[4];
	if (dx >= 128)
		dx -= 256;
	if (dx == -128)
		dx = -127;

	dy = psc->packet[5];
	if (dy >= 128)
		dy -= 256;
	if (dy == -128)
		dy = -127;

	dz = 0;

	changed = buttons ^ (psc->buttons & 0xe0);
	psc->buttons ^= changed;

	if (dx || dy || dz || changed) {
		s = spltty();
		/*
		 * If the middle button is held, interpret movement as
		 * scrolling.
		 */
		if (synaptics_aux_mid_button_scroll &&
		    dy && (psc->buttons & 0x2)) {
			wsmouse_precision_scroll(psc->sc_wsmousedev, dx, dy);
		} else {
			buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
			wsmouse_input(psc->sc_wsmousedev,
				buttons, dx, dy, dz, 0,
				WSMOUSE_INPUT_DELTA);
		}
		splx(s);
	}
}

static void
pms_synaptics_input(void *vsc, int data)
{
	struct pms_softc *psc = vsc;
	struct timeval diff;

	if (!psc->sc_enabled) {
		/* Interrupts are not expected. Discard the byte. */
		return;
	}

	getmicrouptime(&psc->current);

	if (psc->inputstate > 0) {
		timersub(&psc->current, &psc->last, &diff);
		if (diff.tv_sec > 0 || diff.tv_usec >= 40000) {
			aprint_debug_dev(psc->sc_dev,
			    "pms_synaptics_input: unusual delay (%ld.%06ld s), "
			    "scheduling reset\n",
			    (long)diff.tv_sec, (long)diff.tv_usec);
			printf("pms_synaptics_input: unusual delay (%ld.%06ld s), "
			    "scheduling reset\n",
			    (long)diff.tv_sec, (long)diff.tv_usec);
			psc->inputstate = 0;
			psc->sc_enabled = 0;
			wakeup(&psc->sc_enabled);
			return;
		}
	}
	psc->last = psc->current;

	switch (psc->inputstate) {
	case -5:
	case -4:
	case -3:
	case -2:
	case -1:
	case 0:
		if ((data & 0xc8) != 0x80) {
			aprint_debug_dev(psc->sc_dev,
			    "pms_synaptics_input: 0x%02x out of sync\n", data);
			/* use negative counts to limit resync phase */
			psc->inputstate--;
			return;	/* not in sync yet, discard input */
		}
		psc->inputstate = 0;
		/*FALLTHROUGH*/

	case -6:
	case 3:
		if ((data & 8) == 8) {
			aprint_debug_dev(psc->sc_dev,
			    "pms_synaptics_input: dropped in relative mode, reset\n");
			psc->inputstate = 0;
			psc->sc_enabled = 0;
			wakeup(&psc->sc_enabled);
			return;
		}
	}

	psc->packet[psc->inputstate++] = data & 0xff;
	if (psc->inputstate == 6) {
		/*
		 * We have a complete packet.
		 * Extract the pertinent details.
		 */
		psc->inputstate = 0;
		if ((psc->packet[0] & 0xfc) == 0x84 &&
		    (psc->packet[3] & 0xcc) == 0xc4) {
			/* W = SYNAPTICS_WIDTH_PASSTHROUGH, PS/2 passthrough */
			pms_synaptics_passthrough(psc);
		} else {
			pms_synaptics_parse(psc);
		}
	}
}

static inline int
synaptics_finger_detect(struct synaptics_softc *sc, struct synaptics_packet *sp,
    int *palmp)
{
	int fingers;

	/* Assume no palm */
	*palmp = 0;

	/*
	 * Apply some hysteresis when checking for a finger.
	 * When the finger is first applied, we ignore it until the
	 * pressure exceeds the 'high' threshold. The finger is considered
	 * removed only when pressure falls beneath the 'low' threshold.
	 */
	if ((sc->prev_fingers == 0 && sp->sp_z > synaptics_finger_high) ||
	    (sc->prev_fingers != 0 && sp->sp_z > synaptics_finger_low))
		fingers = 1;
	else
		fingers = 0;

	/*
	 * If the pad can't do palm detection, skip the rest.
	 */
	if (fingers == 0 || (sc->flags & SYN_FLAG_HAS_PALM_DETECT) == 0)
		return (fingers);

	/*
	 * Palm detection
	 */
	if (sp->sp_z > SYNAPTICS_FINGER_FLAT &&
	    sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)
		*palmp = 1;

	if (sc->prev_fingers == 0 &&
	    (sp->sp_z > SYNAPTICS_FINGER_FLAT ||
	     sp->sp_w >= SYNAPTICS_WIDTH_PALM_MIN)) {
		/*
		 * Contact area or pressure is too great to be a finger.
		 * Just ignore it for now.
		 */
		return (0);
	}

	/*
	 * Detect 2 and 3 fingers if supported, but only if multiple
	 * fingers appear within the tap gesture time period.
	 */
	if ((sc->flags & SYN_FLAG_HAS_MULTI_FINGER) &&
	    ((SYN_TIME(sc, sc->gesture_start_packet)
	     < synaptics_gesture_length) ||
	    SYN_TIME(sc, sc->gesture_start_packet)
	     < synaptics_gesture_length)) {
		switch (sp->sp_w) {
		case SYNAPTICS_WIDTH_TWO_FINGERS:
			fingers = 2;
			break;

		case SYNAPTICS_WIDTH_THREE_OR_MORE:
			fingers = 3;
			break;

		default:
			/*
			 * The width value can report spurious single-finger
			 * events after a multi-finger event.
			 */
			fingers = sc->prev_fingers <= 1 ? 1 : sc->prev_fingers;
			break;
		}
	}

	return (fingers);
}

static inline void
synaptics_gesture_detect(struct synaptics_softc *sc,
    struct synaptics_packet *sp, int fingers)
{
	int gesture_len, gesture_buttons;
	int set_buttons;

	gesture_len = SYN_TIME(sc, sc->gesture_start_packet);
	gesture_buttons = sc->gesture_buttons;

	if (fingers > 0 && (fingers == sc->prev_fingers)) {
		/* Finger is still present */
		sc->gesture_move_x = abs(sc->gesture_start_x - sp->sp_x);
		sc->gesture_move_y = abs(sc->gesture_start_y - sp->sp_y);
	} else
	if (fingers && sc->prev_fingers == 0) {
		/*
		 * Finger was just applied.
		 * If the previous gesture was a single-click, set things
		 * up to deal with a possible drag or double-click gesture.
		 * Basically, if the finger is removed again within
		 * 'synaptics_gesture_length' packets, this is treated
		 * as a double-click. Otherwise we will emulate holding
		 * the left button down whilst dragging the mouse.
		 */
		if (SYN_IS_SINGLE_TAP(sc->gesture_type))
			sc->gesture_type |= SYN_GESTURE_DRAG;

		sc->gesture_start_x = abs(sp->sp_x);
		sc->gesture_start_y = abs(sp->sp_y);
		sc->gesture_move_x = 0;
		sc->gesture_move_y = 0;
		sc->gesture_start_packet = sc->total_packets;

		DPRINTF(10, sc, "Finger applied:"
		    " gesture_start_x: %d"
		    " gesture_start_y: %d\n",
		    sc->gesture_start_x, sc->gesture_start_y);
	} else
	if (fingers == 0 && sc->prev_fingers != 0) {
		/*
		 * Finger was just removed.
		 * Check if the contact time and finger movement were
		 * small enough to qualify as a gesture.
		 * Ignore finger movement if multiple fingers were
		 * detected (the pad may report coordinates for any
		 * of the fingers).
		 */

		DPRINTF(10, sc, "Finger removed: gesture_len: %d (%d)\n",
		    gesture_len, synaptics_gesture_length);
		DPRINTF(10, sc, "gesture_move_x: %d (%d) sp_x: %d\n",
		    sc->gesture_move_x, synaptics_gesture_move, abs(sp->sp_x));
		DPRINTF(10, sc, "gesture_move_y: %d (%d) sp_y: %d\n",
		    sc->gesture_move_y, synaptics_gesture_move, abs(sp->sp_y));

		if (gesture_len < synaptics_gesture_length &&
		    ((sc->gesture_move_x < synaptics_gesture_move &&
		     sc->gesture_move_y < synaptics_gesture_move))) {
			/*
			 * Looking good so far.
			 */
			if (SYN_IS_DRAG(sc->gesture_type)) {
				/*
				 * Promote this gesture to double-click.
				 */
				sc->gesture_type |= SYN_GESTURE_DOUBLE;
				sc->gesture_type &= ~SYN_GESTURE_SINGLE;
			} else {
				/*
				 * Single tap gesture. Set the tap length timer
				 * and flag a single-click.
				 */
				sc->gesture_tap_packet = sc->total_packets;
				sc->gesture_type |= SYN_GESTURE_SINGLE;

				/*
				 * The gesture can be modified depending on
				 * the number of fingers detected.
				 *
				 * 1: Normal left button emulation.
				 * 2: Either middle button or right button
				 *    depending on the value of the two_fingers
				 *    sysctl variable.
				 * 3: Right button.
				 */
				switch (sc->prev_fingers) {
				case 2:
					if (synaptics_two_fingers_emul == 1)
						gesture_buttons |= PMS_RBUTMASK;
					else
					if (synaptics_two_fingers_emul == 2)
						gesture_buttons |= PMS_MBUTMASK;
					break;
				case 3:
					gesture_buttons |= PMS_RBUTMASK;
					break;
				default:
					gesture_buttons |= PMS_LBUTMASK;
					break;
				}
			}
		}

		/*
		 * Always clear drag state when the finger is removed.
		 */
		sc->gesture_type &= ~SYN_GESTURE_DRAG;
	}

	if (sc->gesture_type == 0) {
		/*
		 * There is no gesture in progress.
		 * Clear emulated button state.
		 */
		sc->gesture_buttons = 0;
		return;
	}

	/*
	 * A gesture is in progress.
	 */
	set_buttons = 0;

	if (SYN_IS_SINGLE_TAP(sc->gesture_type)) {
		/*
		 * Single-click.
		 * Activate the relevant button(s) until the
		 * gesture tap timer has expired.
		 */
		if (SYN_TIME(sc, sc->gesture_tap_packet) <
		    synaptics_gesture_length)
			set_buttons = 1;
		else
			sc->gesture_type &= ~SYN_GESTURE_SINGLE;
		DPRINTF(10, sc, "synaptics_gesture: single tap, buttons %d\n",
		    set_buttons);
		DPRINTF(10, sc, "synaptics_gesture: single tap, tap at %d, current %d\n",
		    sc->gesture_tap_packet, sc->total_packets);
		DPRINTF(10, sc, "synaptics_gesture: single tap, tap_time %d, gesture len %d\n",
		    SYN_TIME(sc, sc->gesture_tap_packet), synaptics_gesture_length);
	} else
	if (SYN_IS_DOUBLE_TAP(sc->gesture_type) && sc->prev_fingers == 0) {
		/*
		 * Double-click.
		 * Activate the relevant button(s) once.
		 */
		set_buttons = 1;
		sc->gesture_type &= ~SYN_GESTURE_DOUBLE;
	}

	if (set_buttons || SYN_IS_DRAG(sc->gesture_type)) {
		/*
		 * Single-click and drag.
		 * Maintain button state until the finger is removed.
		 */
		sp->sp_left |= gesture_buttons & PMS_LBUTMASK;
		sp->sp_right |= gesture_buttons & PMS_RBUTMASK;
		sp->sp_middle |= gesture_buttons & PMS_MBUTMASK;
	}

	sc->gesture_buttons = gesture_buttons;
}

static inline int
synaptics_filter_policy(struct synaptics_softc *sc, int finger, int *history,
			int value, u_int count)
{
	int a, b, rv;

	/*
	 * Once we've accumulated at least SYN_HIST_SIZE values, combine
	 * each new value with the previous two and return the average.
	 *
	 * This is necessary when the touchpad is operating in 80 packets
	 * per second mode, as it performs little internal filtering on
	 * reported values.
	 *
	 * Using a rolling average helps to filter out jitter caused by
	 * tiny finger movements.
	 */
	if (count >= SYN_HIST_SIZE) {
		a = (history[(count - 2) % SYN_HIST_SIZE] +
		     history[(count - 1) % SYN_HIST_SIZE]) / 2;

		b = (value + history[(count - 1) % SYN_HIST_SIZE]) / 2;

		rv = b - a;

		/*
		 * Don't report the movement if it's below a certain
		 * threshold.
		 */
		if (abs(rv) < synaptics_movement_threshold)
			rv = 0;
	} else
		rv = 0;

	/*
	 * Add the new value to the history buffer.
	 */
	history[(count + 0) % SYN_HIST_SIZE] = value;

	return (rv);
}

/* Edge detection */
#define	SYN_EDGE_TOP		1
#define	SYN_EDGE_BOTTOM		2
#define	SYN_EDGE_LEFT		4
#define	SYN_EDGE_RIGHT		8

static inline int
synaptics_check_edge(int x, int y)
{
	int rv = 0;

	if (x < synaptics_edge_left)
		rv |= SYN_EDGE_LEFT;
	else
	if (x > synaptics_edge_right)
		rv |= SYN_EDGE_RIGHT;

	if (y < synaptics_edge_bottom)
		rv |= SYN_EDGE_BOTTOM;
	else
	if (y > synaptics_edge_top)
		rv |= SYN_EDGE_TOP;

	return (rv);
}

static inline int
synaptics_edge_motion(struct synaptics_softc *sc, int delta, int dir)
{

	/*
	 * When edge motion is enabled, synaptics_edge_motion_delta is
	 * combined with the current delta, together with the direction
	 * in which to simulate the motion. The result is added to
	 * the delta derived from finger movement. This provides a smooth
	 * transition from finger movement to edge motion.
	 */
	delta = synaptics_edge_motion_delta + (dir * delta);
	if (delta < 0)
		return (0);
	if (delta > synaptics_edge_motion_delta)
		return (synaptics_edge_motion_delta);
	return (delta);
}

static inline int
synaptics_scale(int delta, int scale, int *remp)
{
	int rv;

	/*
	 * Scale the raw delta in Synaptics coordinates (0-6143) into
	 * something more reasonable by dividing the raw delta by a
	 * scale factor. Any remainder from the previous scale result
	 * is added to the current delta before scaling.
	 * This prevents loss of resolution for very small/slow
	 * movements of the finger.
	 */
	delta += *remp;
	rv = delta / scale;
	*remp = delta % scale;

	return (rv);
}

static inline void
synaptics_movement(struct synaptics_softc *sc, struct synaptics_packet *sp,
    int *dxp, int *dyp, int *dzp, int *sdxp, int *sdyp, int *sdzp)
{
	int dx, dy, dz, sdx, sdy, sdz, edge;

	dx = dy = dz = sdx = sdy = sdz = 0;

	/*
	 * Compute the next values of dx, dy, dz, sdx, sdy, sdz.
	 */
	dx = synaptics_filter_policy(sc, 0,
	    sc->history_x[SYN_PRIMARY_FINGER], sp->sp_x,
	    sc->packet_count[SYN_PRIMARY_FINGER]);
	dy = synaptics_filter_policy(sc, 0,
	    sc->history_y[SYN_PRIMARY_FINGER], sp->sp_y,
	    sc->packet_count[SYN_PRIMARY_FINGER]);
	sc->packet_count[SYN_PRIMARY_FINGER]++;

	if (sp->sp_finger_count > 1) {
		sdx = synaptics_filter_policy(sc, 1,
		    sc->history_x[SYN_SECONDARY_FINGER], sp->sp_sx,
		    sc->packet_count[SYN_SECONDARY_FINGER]);
		sdy = synaptics_filter_policy(sc, 1,
		    sc->history_y[SYN_SECONDARY_FINGER], sp->sp_sy,
		    sc->packet_count[SYN_SECONDARY_FINGER]);
		sc->packet_count[SYN_SECONDARY_FINGER]++;
		DPRINTF(10, sc, "synaptics_movement: dx %d dy %d sdx %d sdy %d\n",
		    dx, dy, sdx, sdy);
	}

	/*
	 * If we're dealing with a drag gesture, and the finger moves to
	 * the edge of the touchpad, apply edge motion emulation if it
	 * is enabled.
	 */
	if (synaptics_edge_motion_delta && SYN_IS_DRAG(sc->gesture_type)) {
		edge = synaptics_check_edge(sp->sp_x, sp->sp_y);

		if (edge & SYN_EDGE_LEFT)
			dx -= synaptics_edge_motion(sc, dx, 1);
		if (edge & SYN_EDGE_RIGHT)
			dx += synaptics_edge_motion(sc, dx, -1);
		if (edge & SYN_EDGE_BOTTOM)
			dy -= synaptics_edge_motion(sc, dy, 1);
		if (edge & SYN_EDGE_TOP)
			dy += synaptics_edge_motion(sc, dy, -1);

		if (sp->sp_finger_count > 1) {
			edge = synaptics_check_edge(sp->sp_sx, sp->sp_sy);

			if (edge & SYN_EDGE_LEFT)
				sdx -= synaptics_edge_motion(sc, sdx, 1);
			if (edge & SYN_EDGE_RIGHT)
				sdx += synaptics_edge_motion(sc, sdx, -1);
			if (edge & SYN_EDGE_BOTTOM)
				sdy -= synaptics_edge_motion(sc, sdy, 1);
			if (edge & SYN_EDGE_TOP)
				sdy += synaptics_edge_motion(sc, sdy, -1);
		}
	}

	/*
	 * Apply scaling to the deltas
	 */
	dx = synaptics_scale(dx, synaptics_scale_x,
	    &sc->rem_x[SYN_PRIMARY_FINGER]);
	dy = synaptics_scale(dy, synaptics_scale_y,
	    &sc->rem_y[SYN_PRIMARY_FINGER]);
	dz = synaptics_scale(dz, synaptics_scale_z,
	    &sc->rem_z[SYN_PRIMARY_FINGER]);

	if (sp->sp_finger_count > 1) {
		sdx = synaptics_scale(sdx, synaptics_scale_x,
		    &sc->rem_x[SYN_SECONDARY_FINGER]);
		sdy = synaptics_scale(sdy, synaptics_scale_y,
		    &sc->rem_y[SYN_SECONDARY_FINGER]);
		sdz = synaptics_scale(sdz, synaptics_scale_z,
		    &sc->rem_z[SYN_SECONDARY_FINGER]);

		DPRINTF(10, sc,
		    "synaptics_movement 2: dx %d dy %d sdx %d sdy %d\n",
		    dx, dy, sdx, sdy);
	}

	/*
	 * Clamp deltas to specified maximums.
	 */
	if (abs(dx) > synaptics_max_speed_x)
		dx = ((dx >= 0)? 1 : -1) * synaptics_max_speed_x;
	if (abs(dy) > synaptics_max_speed_y)
		dy = ((dy >= 0)? 1 : -1) * synaptics_max_speed_y;
	if (abs(dz) > synaptics_max_speed_z)
		dz = ((dz >= 0)? 1 : -1) * synaptics_max_speed_z;

	if (sp->sp_finger_count > 1) {
		if (abs(sdx) > synaptics_max_speed_x)
			sdx = ((sdx >= 0)? 1 : -1) * synaptics_max_speed_x;
		if (abs(dy) > synaptics_max_speed_y)
			sdy = ((sdy >= 0)? 1 : -1) * synaptics_max_speed_y;
		if (abs(sdz) > synaptics_max_speed_z)
			sdz = ((sdz >= 0)? 1 : -1) * synaptics_max_speed_z;
	}

	*dxp = dx;
	*dyp = dy;
	*dzp = dz;
	*sdxp = sdx;
	*sdyp = sdy;
	*sdzp = sdz;

}

static void
pms_synaptics_process_packet(struct pms_softc *psc, struct synaptics_packet *sp)
{
	struct synaptics_softc *sc = &psc->u.synaptics;
	int dx, dy, dz, sdx, sdy, sdz;
	int fingers, palm, buttons, changed;
	int s;

	sdx = sdy = sdz = 0;

	/*
	 * Do Z-axis emulation using up/down buttons if required.
	 * Note that the pad will send a one second burst of packets
	 * when an up/down button is pressed and held. At the moment
	 * we don't deal with auto-repeat, so convert the burst into
	 * a one-shot.
	 */
	dz = 0;
	if (synaptics_up_down_emul == 2) {
		if (sc->up_down == 0) {
			if (sp->sp_up && sp->sp_down) {
				sp->sp_middle = 1;
			} else if (sp->sp_up) {
				dz = -synaptics_up_down_motion_delta;
			} else if (sp->sp_down) {
				dz = synaptics_up_down_motion_delta;
			}
		}

		sc->up_down = sp->sp_up | sp->sp_down;
		sp->sp_up = sp->sp_down = 0;
	}

	/*
	 * Determine whether or not a finger is on the pad.
	 * On some pads, this will return the number of fingers
	 * detected.
	 */
	fingers = synaptics_finger_detect(sc, sp, &palm);

	/*
	 * Do gesture processing only if we didn't detect a palm.
	 */
	if (palm == 0)
		synaptics_gesture_detect(sc, sp, fingers);
	else
		sc->gesture_type = sc->gesture_buttons = 0;

	/*
	 * Determine what buttons to report
	 */
	buttons = (sp->sp_left ? 0x1 : 0) |
	    (sp->sp_middle ? 0x2 : 0) |
	    (sp->sp_right ? 0x4 : 0) |
	    (sp->sp_up ? 0x8 : 0) |
	    (sp->sp_down ? 0x10 : 0);
	changed = buttons ^ (psc->buttons & 0x1f);
	psc->buttons ^= changed;

	sc->prev_fingers = fingers;

	/*
	 * Do movement processing IFF we have a single finger and no palm or
	 * a secondary finger and no palm.
	 */
	if (palm == 0 && synaptics_movement_enable) {
		if (fingers > 0) {
			synaptics_movement(sc, sp, &dx, &dy, &dz, &sdx, &sdy,
			    &sdz);

			/*
			 * Check if there are two fingers, if there are then
			 * check if we have a clickpad, if we do then we
			 * don't scroll iff one of the fingers is in the
			 * button region.  Otherwise interpret as a scroll
			 */
			if (sp->sp_finger_count >= 2 && sc->gesture_type == 0 ) {
				if (!(sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) ||
				    ((sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) &&
				    (sp->sp_y > synaptics_button_boundary)  &&
				    (sp->sp_sy > synaptics_button_boundary))) {
					s = spltty();
					wsmouse_precision_scroll(
					    psc->sc_wsmousedev, dx, dy);
					splx(s);
					return;
				}
			}

			/*
			 * Allow user to turn off movements in the button
			 * region of a click pad.
			 */
			if (sc->flags & SYN_FLAG_HAS_ONE_BUTTON_CLICKPAD) {
				if ((sp->sp_y < synaptics_button_boundary) &&
				    (!synaptics_button_region_movement)) {
					dx = dy = dz = 0;
				}

				if ((sp->sp_sy < synaptics_button_boundary) &&
				    (!synaptics_button_region_movement)) {
					sdx = sdy = sdz = 0;
				}
			}

			/* Now work out which finger to report, just
			 * go with the one that has moved the most.
			 */
			if ((abs(dx) + abs(dy) + abs(dz)) <
			    (abs(sdx) + abs(sdy) + abs(sdz))) {
				/* secondary finger wins */
				dx = sdx;
				dy = sdy;
				dz = sdz;
			}
		} else {
			/*
			 * No valid finger. Therefore no movement.
			 */
			sc->rem_x[SYN_PRIMARY_FINGER] = 0;
			sc->rem_y[SYN_PRIMARY_FINGER] = 0;
			sc->rem_x[SYN_SECONDARY_FINGER] = 0;
			sc->rem_y[SYN_SECONDARY_FINGER] = 0;
			dx = dy = 0;

			sc->packet_count[SYN_PRIMARY_FINGER] = 0;
			sc->packet_count[SYN_SECONDARY_FINGER] = 0;
		}
	} else {
		/*
		 * No valid finger. Therefore no movement.
		 */
		sc->rem_x[SYN_PRIMARY_FINGER] = 0;
		sc->rem_y[SYN_PRIMARY_FINGER] = 0;
		sc->rem_z[SYN_PRIMARY_FINGER] = 0;
		dx = dy = dz = 0;
	}

	DPRINTF(10, sc,
	    "pms_synaptics_process_packet: dx %d dy %d dz %d sdx %d sdy %d sdz %d\n",
	    dx, dy, dz, sdx, sdy, sdz);

	/*
	 * Pass the final results up to wsmouse_input() if necessary.
	 */
	if (dx || dy || dz || changed) {
		buttons = (psc->buttons & 0x1f) | ((psc->buttons >> 5) & 0x7);
		s = spltty();
		wsmouse_input(psc->sc_wsmousedev,
				buttons,
				dx, dy, dz, 0,
		    		WSMOUSE_INPUT_DELTA);
		splx(s);
	}
}