summaryrefslogtreecommitdiff
path: root/synaptics.c
blob: 56b38c8279032dcf4eabb26246e0ece68159dd3b (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
/*
 *   2003 Jörg Bösner <ich@joerg-boesner.de>
 *     patch for switching the touchpad off (for example, when a
 *     USB mouse is connected)
 *
 *   2003 Hartwig Felger <hgfelger@hgfelger.de>
 *     patch to make the horizontal-wheel-replacement-buttons work.
 *
 *   2002 Peter Osterlund <petero2@telia.com>
 *     patches for fast scrolling, palm detection, edge motion,
 *     horizontal scrolling
 *
 *   2002 S. Lehner <sam_x@bluemail.ch>
 *     for newer Firmware (5.8) protocol changes for 3rd to 6th button
 *
 *	 Copyright (C) 2001 Stefan Gmeiner <riddlebox@freesurf.ch>
 *     start merging tpconfig and gpm code to a xfree-input module
 *     adding some changes and extensions (ex. 3rd and 4th button)
 *
 *   Copyright (c) 1999 Henry Davies <hdavies@ameritech.net> for the
 *     absolute to relative translation code (from the gpm-source)
 *     and some other ideas
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation; either version 2
 *   of the License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * Trademarks are the property of their respective owners.
 *
 */


/*****************************************************************************
 *	Standard Headers
 ****************************************************************************/

#include <sys/ioctl.h>
#include <misc.h>
#include <xf86.h>
#define NEED_XF86_TYPES
#include <xf86_ansic.h>
#include <xf86_OSproc.h>
#include <xf86Xinput.h>
#include <xisb.h>
#include <exevents.h>			/* Needed for InitValuator/Proximity stuff	*/
#include "mipointer.h"
#ifdef XFREE_4_0_3
#include <xf86Optrec.h>  		/* needed for Options */
#endif


/*****************************************************************************
 *	Local Headers
 ****************************************************************************/
#define SYNAPTICS_PRIVATE
#include "synaptics.h"
#include "ps2comm.h"

/*****************************************************************************
 *	Variables without includable headers
 ****************************************************************************/

/*****************************************************************************
 *	Local Variables and Types
 ****************************************************************************/

/*
 * 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.
 */
#define XMIN_NOMINAL 1472
#define XMAX_NOMINAL 5472
#define YMIN_NOMINAL 1408
#define YMAX_NOMINAL 4448

#define MAX_UNSYNC_PACKETS 10				/* i.e. 10 to 60 bytes */

typedef enum {
    BOTTOM_EDGE = 1,
    TOP_EDGE = 2,
    LEFT_EDGE = 4,
    RIGHT_EDGE = 8,
    LEFT_BOTTOM_EDGE = BOTTOM_EDGE | LEFT_EDGE,
    RIGHT_BOTTOM_EDGE = BOTTOM_EDGE | RIGHT_EDGE,
    RIGHT_TOP_EDGE = TOP_EDGE | RIGHT_EDGE,
    LEFT_TOP_EDGE = TOP_EDGE | LEFT_EDGE
} edge_type;

#define MAX(a, b) (((a)>(b))?(a):(b))
#define MIN(a, b) (((a)<(b))?(a):(b))
#define TIME_DIFF(a, b) ((long)((a)-(b)))
#define SYSCALL(call) while (((call) == -1) && (errno == EINTR))

/* for auto-dev: */
#define DEV_INPUT_EVENT "/dev/input"
#define EVENT_DEV_NAME "event"

#define VERSION "0.11.6"

/*****************************************************************************
 * Forward declaration
 ****************************************************************************/
static InputInfoPtr
SynapticsPreInit(InputDriverPtr drv, IDevPtr dev, int flags);


InputDriverRec SYNAPTICS = {
    1,
    "synaptics",
    NULL,
    SynapticsPreInit,
    /*SynapticsUnInit*/ NULL,
    NULL,
    0
};

#ifdef XFree86LOADER

static XF86ModuleVersionInfo VersionRec = {
    "synaptics",
    MODULEVENDORSTRING,
    MODINFOSTRING1,
    MODINFOSTRING2,
    XF86_VERSION_CURRENT,
    1, 0, 0,
    ABI_CLASS_XINPUT,
    ABI_XINPUT_VERSION,
    MOD_CLASS_XINPUT,
    {0, 0, 0, 0}				/* signature, to be patched into the file by
						 * a tool */
};


static pointer
SetupProc(pointer module, pointer options, int *errmaj, int *errmin)
{
    xf86AddInputDriver(&SYNAPTICS, module, 0);
    return module;
}

XF86ModuleData synapticsModuleData = {&VersionRec, &SetupProc, NULL };

#endif /* XFree86LOADER */


/*****************************************************************************
 *	Function Definitions
 ****************************************************************************/

static void
SetDeviceAndProtocol(LocalDevicePtr local)
{
    char *str_par;
    SynapticsPrivatePtr priv = local->private;

    priv->proto = SYN_PROTO_PSAUX;
    str_par = xf86FindOptionValue(local->options, "Protocol");
    if (str_par && !strcmp(str_par, "event")) {
	priv->proto = SYN_PROTO_EVENT;
    } else if (str_par && !strcmp(str_par, "psaux")) {
	/* Already set up */
    } else { /* default to auto-dev */
	/* We are trying to find the right eventX Device, or fall back to
	   the psaux Protocol and the given Device from XF86Config */
	int fd = -1;
	int i;
	for (i = 0; ; i++) {
	    char fname[64];
	    struct input_id id;
	    int ret;

	    sprintf(fname, "%s/%s%d", DEV_INPUT_EVENT, EVENT_DEV_NAME, i);
	    SYSCALL(fd = open(fname, O_RDONLY));
	    if (fd < 0) {
		if (errno == ENOENT) {
		    ErrorF("%s no synaptics event device found\n", local->name);
		    break;
		} else {
		    continue;
		}
	    }
	    SYSCALL(ret = ioctl(fd, EVIOCGID, &id));
	    SYSCALL(close(fd));
	    if (ret >= 0) {
		if ((id.bustype == BUS_I8042) &&
		    (id.vendor == 0x0002) &&
		    (id.product == PSMOUSE_SYNAPTICS)) {
		    priv->proto = SYN_PROTO_EVENT;
		    xf86Msg(X_PROBED, "%s auto-dev sets Synaptics Device to %s\n",
			    local->name, fname);
		    xf86ReplaceStrOption(local->options, "Device", fname);
		    break;
		}
	    }
	}
    }
}

/*
 *  called by the module loader for initialization
 */
static InputInfoPtr
SynapticsPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
{
    LocalDevicePtr local;
    SynapticsPrivatePtr priv;
#ifdef XFREE_4_0_3
    XF86OptionPtr optList;
#else
    pointer optList;
#endif
    char *str_par;
    int shmid;
    unsigned long now;

    /* allocate memory for SynaticsPrivateRec */
    priv = xcalloc (1, sizeof (SynapticsPrivateRec));
    if (!priv)
	return NULL;

    /* Allocate a new InputInfoRec and add it to the head xf86InputDevs. */
    local = xf86AllocateInput(drv, 0);
    if (!local) {
	xfree(priv);
	return NULL;
    }

    /* initialize the InputInfoRec */
    local->name                    = dev->identifier;
    local->type_name               = XI_MOUSE; /* XI_TOUCHPAD and KDE killed the X-Server at startup ? */
    local->device_control          = DeviceControl;
    local->read_input              = ReadInput;
    local->control_proc            = ControlProc;
    local->close_proc              = CloseProc;
    local->switch_mode             = SwitchMode;
    local->conversion_proc         = ConvertProc;
    local->reverse_conversion_proc = NULL;
    local->dev                     = NULL;
    local->private                 = priv;
    local->private_flags           = 0;
    local->flags                   = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
    local->conf_idev               = dev;
    local->motion_history_proc     = xf86GetMotionEvents;
    local->history_size            = 0;
    local->always_core_feedback    = 0;

    xf86CollectInputOptions(local, NULL, NULL);

    xf86OptionListReport(local->options);

    SetDeviceAndProtocol(local);

    /* open the touchpad device */
    local->fd = xf86OpenSerial (local->options);
    if (local->fd == -1) {
	ErrorF ("Synaptics driver unable to open device\n");
	goto SetupProc_fail;
    }
    xf86ErrorFVerb( 6, "port opened successfully\n" );

    /* initialize variables */
    priv->timer = NULL;
    priv->repeatButtons = 0;
    priv->nextRepeat = 0;
    now = GetTimeInMillis();
    priv->count_packet_finger = 0;
    priv->tapping_millis = now;
    priv->button_delay_millis = now;
    priv->touch_on.millis = now;

    /* install shared memory or normal memory for parameter */
    priv->shm_config = FALSE;
    if (xf86SetBoolOption(local->options, "SHMConfig", FALSE)) {
	if ((shmid = xf86shmget(SHM_SYNAPTICS, 0, 0)) != -1)
	    xf86shmctl(shmid, XF86IPC_RMID, NULL);
	if ((shmid = xf86shmget(SHM_SYNAPTICS, sizeof(SynapticsSHM),
				0777 | XF86IPC_CREAT)) == -1) {
	    xf86Msg(X_ERROR, "%s error shmget\n", local->name);
	    goto SetupProc_fail;
	}
	else if ((priv->synpara = (SynapticsSHM*) xf86shmat(shmid, NULL, 0)) == NULL) {
	    xf86Msg(X_ERROR, "%s error shmat\n", local->name);
	    goto SetupProc_fail;
	}
	priv->shm_config = TRUE;
    } else {
	priv->synpara = xcalloc (1, sizeof (SynapticsSHM));
	if (!priv->synpara)
	    goto SetupProc_fail;
    }

    /* read the parameter */
    priv->synpara->left_edge = xf86SetIntOption(local->options, "LeftEdge", 1900);
    priv->synpara->right_edge = xf86SetIntOption(local->options, "RightEdge", 5400);
    priv->synpara->top_edge = xf86SetIntOption(local->options, "TopEdge", 1900);
    priv->synpara->bottom_edge = xf86SetIntOption(local->options, "BottomEdge", 4000);
    priv->synpara->finger_low = xf86SetIntOption(local->options, "FingerLow", 25);
    priv->synpara->finger_high = xf86SetIntOption(local->options, "FingerHigh", 30);
    priv->synpara->tap_time = xf86SetIntOption(local->options, "MaxTapTime", 180);
    priv->synpara->tap_move = xf86SetIntOption(local->options, "MaxTapMove", 220);
    priv->synpara->emulate_mid_button_time = xf86SetIntOption(local->options,
							      "EmulateMidButtonTime", 75);
    priv->synpara->scroll_dist_vert = xf86SetIntOption(local->options, "VertScrollDelta", 100);
    priv->synpara->scroll_dist_horiz = xf86SetIntOption(local->options, "HorizScrollDelta", 100);
    priv->synpara->edge_motion_speed = xf86SetIntOption(local->options, "EdgeMotionSpeed", 40);
    priv->synpara->repeater = xf86SetStrOption(local->options, "Repeater", NULL);
    priv->synpara->updown_button_scrolling = xf86SetBoolOption(local->options, "UpDownScrolling", TRUE);
    priv->synpara->touchpad_off = xf86SetBoolOption(local->options, "TouchpadOff", FALSE);

    str_par = xf86FindOptionValue(local->options, "MinSpeed");
    if ((!str_par) || (xf86sscanf(str_par, "%lf", &priv->synpara->min_speed) != 1))
	priv->synpara->min_speed=0.02;
    str_par = xf86FindOptionValue(local->options, "MaxSpeed");
    if ((!str_par) || (xf86sscanf(str_par, "%lf", &priv->synpara->max_speed) != 1))
	priv->synpara->max_speed=0.18;
    str_par = xf86FindOptionValue(local->options, "AccelFactor");
    if ((!str_par) || (xf86sscanf(str_par, "%lf", &priv->synpara->accl) != 1))
	priv->synpara->accl=0.0015;

    /* Warn about (and fix) mis-configured TopEdge/BottomEdge parameters */
    if (priv->synpara->top_edge > priv->synpara->bottom_edge) {
	int tmp = priv->synpara->top_edge;
	priv->synpara->top_edge = priv->synpara->bottom_edge;
	priv->synpara->bottom_edge = tmp;
	xf86Msg(X_WARNING, "%s: TopEdge is bigger than BottomEdge. Fixing.\n",
		local->name);
    }

    priv->buffer = XisbNew(local->fd, 200);
    DBG(9, XisbTrace (priv->buffer, 1));

    if (priv->synpara->repeater) {
	/* create repeater fifo */
	if ((xf86mknod(priv->synpara->repeater, 666, XF86_S_IFIFO) != 0) &&
	    (xf86errno != xf86_EEXIST)) {
	    xf86Msg(X_ERROR, "%s can't create repeater fifo\n", local->name);
	    xf86free(priv->synpara->repeater);
	    priv->synpara->repeater = NULL;
	    priv->fifofd = -1;
	} else {
	    /* open the repeater fifo */
	    optList = xf86NewOption("Device", priv->synpara->repeater);
	    if ((priv->fifofd = xf86OpenSerial(optList)) == -1) {
		xf86Msg(X_ERROR, "%s repeater device open failed\n", local->name);
		xf86free(priv->synpara->repeater);
		priv->synpara->repeater = NULL;
		priv->fifofd = -1;
	    }
	}
    }

    if (QueryHardware(local) != Success) {
	xf86Msg(X_ERROR, "%s Unable to query/initialize Synaptics hardware.\n", local->name);
	goto SetupProc_fail;
    }

    local->history_size = xf86SetIntOption( local->options, "HistorySize", 0 );

    /* this results in an xstrdup that must be freed later */
    /*local->name = xf86SetStrOption( local->options, "DeviceName", "Synaptics-Touchpad" );*/
    xf86ProcessCommonOptions(local, local->options);
    local->flags |= XI86_CONFIGURED;

    if (local->fd != -1) {
	xf86RemoveEnabledDevice (local);
	if (priv->buffer) {
	    XisbFree(priv->buffer);
	    priv->buffer = NULL;
	}
	xf86CloseSerial(local->fd);
    }
    local->fd = -1;
    return local;

 SetupProc_fail:
    if (local->fd >= 0) {
	RemoveEnabledDevice (local->fd);
	xf86CloseSerial (local->fd);
	local->fd = -1;
    }

    /* If it fails, the name will be printed
       if (local->name)
       xfree (local->name);
    */

    if (priv->buffer)
	XisbFree (priv->buffer);
    if (priv->synpara) {
	if (priv->shm_config) {
	    if ((shmid = xf86shmget(SHM_SYNAPTICS, 0, 0)) != -1)
		xf86shmctl(shmid, XF86IPC_RMID, NULL);
	} else {
	    xfree (priv->synpara);
	}
    }
    /* Freeing priv makes the X server crash. Don't know why.
       xfree (priv);
    */
    return local;
}

/*
 *  Alter the control parameters for the mouse. Note that all special
 *  protocol values are handled by dix.
 */
static void
SynapticsCtrl(DeviceIntPtr device, PtrCtrl *ctrl)
{
    DBG(3, ErrorF("SynapticsCtrl called.\n"));
    /*
      pInfo = device->public.devicePrivate;
      pMse = pInfo->private;

      pMse->num       = ctrl->num;
      pMse->den       = ctrl->den;
      pMse->threshold = ctrl->threshold;
    */
}

static Bool
DeviceControl (DeviceIntPtr dev, int mode)
{
    Bool RetValue;

    switch (mode) {
    case DEVICE_INIT:
	DeviceInit(dev);
	RetValue = Success;
	break;
    case DEVICE_ON:
	RetValue = DeviceOn( dev );
	break;
    case DEVICE_OFF:
	RetValue = DeviceOff( dev );
	break;
    case DEVICE_CLOSE:
	{
	    int shmid;
	    LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
	    SynapticsPrivatePtr priv = (SynapticsPrivatePtr) (local->private);
	    RetValue = DeviceOff( dev );
	    if (priv->shm_config)
		if ((shmid = xf86shmget(SHM_SYNAPTICS, 0, 0)) != -1)
		    xf86shmctl(shmid, XF86IPC_RMID, NULL);
	}
	break;
    default:
	RetValue = BadValue;
    }

    return RetValue;
}

static Bool
DeviceOn (DeviceIntPtr dev)
{
    LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
    SynapticsPrivatePtr priv = (SynapticsPrivatePtr) (local->private);

    DBG(3, ErrorF("Synaptics DeviceOn called\n"));

    local->fd = xf86OpenSerial(local->options);
    if (local->fd == -1) {
	xf86Msg(X_WARNING, "%s: cannot open input device\n", local->name);
	return !Success;
    }

    /* Try to grab the event device so that data doesn't leak to /dev/input/mice */
    if (priv->proto == SYN_PROTO_EVENT) {
	int ret;
	SYSCALL(ret = ioctl(local->fd, EVIOCGRAB, (pointer)1));
	if (ret < 0) {
	    xf86Msg(X_WARNING, "%s can't grab event device\n",
		    local->name, errno);
	}
    }

    priv->buffer = XisbNew(local->fd, 64);
    if (!priv->buffer) {
	xf86CloseSerial(local->fd);
	local->fd = -1;
	return !Success;
    }

    xf86FlushInput(local->fd);

    /* reinit the pad */
    QueryHardware(local);
    xf86AddEnabledDevice (local);
    dev->public.on = TRUE;

    return Success;
}

static Bool
DeviceOff(DeviceIntPtr dev)
{
    LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
    SynapticsPrivatePtr priv = (SynapticsPrivatePtr) (local->private);

    DBG(3, ErrorF("Synaptics DeviceOff called\n"));

    if (local->fd != -1) {
	xf86RemoveEnabledDevice (local);
	if (priv->proto == SYN_PROTO_PSAUX)
	    synaptics_set_mode(local->fd, 0);
	if (priv->buffer) {
	    XisbFree(priv->buffer);
	    priv->buffer = NULL;
	}
	xf86CloseSerial(local->fd);
    }
    dev->public.on = FALSE;
    return Success;
}

static Bool
DeviceInit(DeviceIntPtr dev)
{
    LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
    unsigned char map[] = {0, 1, 2, 3, 4, 5, 6, 7};

    DBG(3, ErrorF("Synaptics DeviceInit called\n"));

    dev->public.on = FALSE;

    InitPointerDeviceStruct((DevicePtr)dev, map,
			    7,
			    miPointerGetMotionEvents, SynapticsCtrl,
			    miPointerGetMotionBufferSize());

    /* X valuator */
    xf86InitValuatorAxisStruct(dev, 0, 0, -1, 1, 0, 1);
    xf86InitValuatorDefaults(dev, 0);
    /* Y valuator */
    xf86InitValuatorAxisStruct(dev, 1, 0, -1, 1, 0, 1);
    xf86InitValuatorDefaults(dev, 1);

    xf86MotionHistoryAllocate(local);

    return Success;
}

static int
move_distance(int dx, int dy)
{
    return xf86sqrt((dx * dx) + (dy * dy));
}

static edge_type
edge_detection(SynapticsPrivatePtr priv, int x, int y)
{
    edge_type edge = 0;

    if (x > priv->synpara->right_edge)
	edge |= RIGHT_EDGE;
    else if (x < priv->synpara->left_edge)
	edge |= LEFT_EDGE;

    if (y < priv->synpara->top_edge)
	edge |= TOP_EDGE;
    else if (y > priv->synpara->bottom_edge)
	edge |= BOTTOM_EDGE;

    return edge;
}

static CARD32
timerFunc(OsTimerPtr timer, CARD32 now, pointer arg)
{
    LocalDevicePtr local = (LocalDevicePtr) (arg);
    SynapticsPrivatePtr priv = (SynapticsPrivatePtr) (local->private);
    struct SynapticsHwState hw;
    int delay;
    int sigstate;
    CARD32 wakeUpTime;

    sigstate = xf86BlockSIGIO ();

    hw = priv->hwState;
    hw.millis = now;
    delay = HandleState(local, &hw);

    /*
     * Workaround for wraparound bug in the TimerSet function. This bug is already
     * fixed in CVS, but this driver needs to work with XFree86 versions 4.2.x and
     * 4.3.x too.
     */
    wakeUpTime = now + delay;
    if (wakeUpTime <= now)
	wakeUpTime = 0xffffffffL;

    priv->timer = TimerSet(priv->timer, TimerAbsolute, wakeUpTime, timerFunc, local);

    xf86UnblockSIGIO (sigstate);

    return 0;
}


#define MOVE_HIST(a) (priv->move_hist[((priv->count_packet_finger-(a))%SYNAPTICS_MOVE_HISTORY)])

static int clamp(int val, int min, int max)
{
    if (val < min)
	return min;
    else if (val < max)
	return val;
    else
	return max;
}


/*
 *  called for each full received packet from the touchpad
 */
static void
ReadInput(LocalDevicePtr local)
{
    SynapticsPrivatePtr priv = (SynapticsPrivatePtr) (local->private);
    struct SynapticsHwState hw;
    int delay = 0;
    Bool newDelay = FALSE;

    while (SynapticsGetHwState(local, priv, &hw) == Success) {
	hw.millis = GetTimeInMillis();
	delay = HandleState(local, &hw);
	newDelay = TRUE;
    }

    if (newDelay)
	priv->timer = TimerSet(priv->timer, 0, delay, timerFunc, local);
}

static int
HandleMidButtonEmulation(SynapticsPrivatePtr priv, struct SynapticsHwState* hw, long* delay)
{
    SynapticsSHMPtr para = priv->synpara;
    Bool done = FALSE;
    long timeleft;
    int mid = 0;

    while (!done) {
	switch (priv->mid_emu_state) {
	case MBE_OFF:
	    priv->button_delay_millis = hw->millis;
	    if (hw->left) {
		priv->mid_emu_state = MBE_LEFT;
	    } else if (hw->right) {
		priv->mid_emu_state = MBE_RIGHT;
	    } else {
		done = TRUE;
	    }
	    break;
	case MBE_LEFT:
	    timeleft = TIME_DIFF(priv->button_delay_millis + para->emulate_mid_button_time,
				 hw->millis);
	    if (timeleft > 0)
		*delay = MIN(*delay, timeleft);
	    if (!hw->left || (timeleft <= 0)) {
		hw->left = TRUE;
		priv->mid_emu_state = MBE_TIMEOUT;
		done = TRUE;
	    } else if (hw->right) {
		priv->mid_emu_state = MBE_MID;
	    } else {
		hw->left = FALSE;
		done = TRUE;
	    }
	    break;
	case MBE_RIGHT:
	    timeleft = TIME_DIFF(priv->button_delay_millis + para->emulate_mid_button_time,
				 hw->millis);
	    if (timeleft > 0)
		*delay = MIN(*delay, timeleft);
	    if (!hw->right || (timeleft <= 0)) {
		hw->right = TRUE;
		priv->mid_emu_state = MBE_TIMEOUT;
		done = TRUE;
	    } else if (hw->left) {
		priv->mid_emu_state = MBE_MID;
	    } else {
		hw->right = FALSE;
		done = TRUE;
	    }
	    break;
	case MBE_MID:
	    if (!hw->left && !hw->right) {
		priv->mid_emu_state = MBE_OFF;
	    } else {
		mid = TRUE;
		hw->left = hw->right = FALSE;
		done = TRUE;
	    }
	    break;
	case MBE_TIMEOUT:
	    if (!hw->left && !hw->right) {
		priv->mid_emu_state = MBE_OFF;
	    } else {
		done = TRUE;
	    }
	}
    }
    return mid;
}

static int
SynapticsDetectFinger(SynapticsPrivatePtr priv, struct SynapticsHwState* hw)
{
    SynapticsSHMPtr para = priv->synpara;
    int finger;

    /* finger detection thru pressure and threshold */
    finger = (((hw->z > para->finger_high) && !priv->finger_flag) ||
	      ((hw->z > para->finger_low)  &&  priv->finger_flag));

    /* palm detection */
    if (finger) {
	if ((hw->z > 200) && (hw->fingerWidth > 10))
	    priv->palm = TRUE;
    } else {
	priv->palm = FALSE;
    }
    if (hw->x == 0)
	priv->avg_width = 0;
    else
	priv->avg_width += (hw->fingerWidth - priv->avg_width + 1) / 2;
    if (finger && !priv->finger_flag) {
	int safe_width = MAX(hw->fingerWidth, priv->avg_width);
	if (hw->twoFingers || hw->threeFingers)
	    finger = TRUE;			/* more than one finger -> not a palm */
	else if ((safe_width < 6) && (priv->prev_z < para->finger_high))
	    finger = TRUE;			/* thin finger, distinct touch -> not a palm */
	else if ((safe_width < 7) && (priv->prev_z < para->finger_high / 2))
	    finger = TRUE;			/* thin finger, distinct touch -> not a palm */
	else if (hw->z > priv->prev_z + 1)	/* z not stable, may be a palm */
	    finger = FALSE;
	else if (hw->z < priv->prev_z - 5)	/* z not stable, may be a palm */
	    finger = FALSE;
	else if (hw->z > 200)			/* z too large -> probably palm */
	    finger = FALSE;
	else if (hw->fingerWidth > 10)		/* finger width too large -> probably palm */
	    finger = FALSE;
    }
    priv->prev_z = hw->z;

    return finger;
}

/*
 * React on changes in the hardware state. This function is called every time
 * the hardware state changes. The return value is used to specify how many
 * milliseconds to wait before calling the function again if no state change
 * occurs.
 */
static int
HandleState(LocalDevicePtr local, struct SynapticsHwState* hw)
{
    SynapticsPrivatePtr priv = (SynapticsPrivatePtr) (local->private);
    SynapticsSHMPtr para = priv->synpara;
    Bool finger;
    int dist, dx, dy, buttons, id;
    edge_type edge;
    Bool mid;
    double speed, integral;
    int change;
    int scroll_up, scroll_down, scroll_left, scroll_right;
    int double_click;
    long delay = 1000000000;
    long timeleft;

    /* If touchpad is switched off, we skip the whole thing and return delay */
    if (para->touchpad_off == TRUE)
	return delay;

    edge = edge_detection(priv, hw->x, hw->y);

    dx = dy = 0;

    /* update finger position in shared memory */
    para->x = hw->x;
    para->y = hw->y;
    para->z = hw->z;
    if (hw->oneFinger)
	para->numFingers = 1;
    else if (hw->twoFingers)
	para->numFingers = 2;
    else if (hw->threeFingers)
	para->numFingers = 3;
    else
	para->numFingers = 0;
    para->fingerWidth = hw->fingerWidth;
    para->left = hw->left;
    para->right = hw->right;
    para->up = hw->up;
    para->down = hw->down;

    /* 3rd button emulation */
    mid = HandleMidButtonEmulation(priv, hw, &delay);

    /* Up/Down-button scrolling or middle/double-click */
    double_click = FALSE;
    if (!para->updown_button_scrolling) {
	if (hw->down)
	    { /* map down-button to middle-button */
		mid = TRUE;
	    }

	if (hw->up)
	    { /* up-button generates double-click */
		if (!priv->prev_up)
		    double_click = TRUE;
	    }
	priv->prev_up = hw->up;

	/* reset up/down button events */
	hw->up = hw->down = FALSE;
    }

    finger = SynapticsDetectFinger(priv, hw);

    /* tap and drag detection */
    if (priv->palm) {
	/* Palm detected, skip tap/drag processing */
    } else if (finger && !priv->finger_flag)
	{ /* touched */
	    DBG(7, ErrorF("touched - x:%d, y:%d millis:%lu\n", hw->x, hw->y, hw->millis));
	    if (priv->tap) {
		DBG(7, ErrorF("drag detected - tap time:%lu\n", priv->tapping_millis));
		priv->drag = TRUE; /* drag gesture */
	    }
	    priv->touch_on.x = hw->x;
	    priv->touch_on.y = hw->y;
	    priv->touch_on.millis = hw->millis;
	}
    else if (!finger && priv->finger_flag)
	{ /* untouched */
	    DBG(7, ErrorF("untouched - x:%d, y:%d millis:%lu finger:%d\n",
			  hw->x, hw->y, hw->millis, priv->finger_count));
	    /* check if
	       1. the tap is in tap_time
	       2. the max movement is in tap_move or more than one finger are tapped */
	    if ((TIME_DIFF(hw->millis, priv->touch_on.millis + para->tap_time) < 0) &&
		(((abs(hw->x - priv->touch_on.x) < para->tap_move) &&
		  (abs(hw->y - priv->touch_on.y) < para->tap_move)) ||
		 priv->finger_count)) {
		if (priv->drag) {
		    DBG(7, ErrorF("double tapping detected\n"));
		    priv->doubletap = TRUE;
		    priv->tap = FALSE;
		} else {
		    DBG(7, ErrorF("tapping detected @ "));
		    priv->tapping_millis = hw->millis;
		    priv->tap = TRUE;
		    if (priv->finger_count == 0) {
			switch (edge) {
			case RIGHT_TOP_EDGE:
			    DBG(7, ErrorF("right top edge\n"));
			    priv->tap_mid = TRUE;
			    break;
			case RIGHT_BOTTOM_EDGE:
			    DBG(7, ErrorF("right bottom edge\n"));
			    priv->tap_right = TRUE;
			    break;
			case LEFT_TOP_EDGE:
			    DBG(7, ErrorF("left top edge\n"));
			    break;
			case LEFT_BOTTOM_EDGE:
			    DBG(7, ErrorF("left bottom edge\n"));
			    break;
			default:
			    DBG(7, ErrorF("no edge\n"));
			    priv->tap_left = TRUE;
			}
		    } else {
			switch (priv->finger_count) {
			case 2:
			    DBG(7, ErrorF("two finger tap\n"));
			    priv->tap_mid = TRUE;
			    break;
			case 3:
			    DBG(7, ErrorF("three finger tap\n"));
			    priv->tap_right = TRUE;
			    break;
			default:
			    DBG(7, ErrorF("one finger\n"));
			    priv->tap_left = TRUE;
			}
		    }
		}
	    } /* tap detection */
	    priv->drag = FALSE;
	} /* finger lost */

    /* detecting 2 and 3 fingers */
    timeleft = TIME_DIFF(priv->touch_on.millis + para->tap_time, hw->millis);
    if (timeleft > 0)
	delay = MIN(delay, timeleft);
    if (finger && /* finger is on the surface */
	(timeleft > 0)) /* tap time is not succeeded */
	{ /* count fingers when reported */
	    if (hw->twoFingers && (priv->finger_count == 0))
		priv->finger_count = 2;
	    if (hw->threeFingers)
		priv->finger_count = 3;
	}
    else
	{ /* reset finger counts */
	    priv->finger_count = 0;
	}

    /* reset tapping button flags */
    if (!priv->tap && !priv->drag && !priv->doubletap) {
	priv->tap_left = priv->tap_mid = priv->tap_right = FALSE;
    }

    /* tap processing */
    timeleft = TIME_DIFF(priv->tapping_millis + para->tap_time, hw->millis);
    if (timeleft > 0)
	delay = MIN(delay, timeleft);
    if (priv->tap && (timeleft > 0)) {
	hw->left  |= priv->tap_left;
	mid       |= priv->tap_mid;
	hw->right |= priv->tap_right;
    } else {
	priv->tap = FALSE;
    }

    /* drag processing */
    if (priv->drag) {
	hw->left  |= priv->tap_left;
	mid       |= priv->tap_mid;
	hw->right |= priv->tap_right;
    }

    /* double tap processing */
    if (priv->doubletap && !priv->finger_flag) {
	hw->left  |= priv->tap_left;
	mid       |= priv->tap_mid;
	hw->right |= priv->tap_right;
	priv->doubletap = FALSE;
    }

    /* scroll detection */
    if (finger && !priv->finger_flag) {
	if (edge & RIGHT_EDGE) {
	    priv->vert_scroll_on = TRUE;
	    priv->scroll_y = hw->y;
	    DBG(7, ErrorF("vert edge scroll detected on right edge\n"));
	}
	if (edge & BOTTOM_EDGE) {
	    priv->horiz_scroll_on = TRUE;
	    priv->scroll_x = hw->x;
	    DBG(7, ErrorF("horiz edge scroll detected on bottom edge\n"));
	}
    }
    if (priv->vert_scroll_on && (!(edge & RIGHT_EDGE) || !finger || priv->palm)) {
	DBG(7, ErrorF("vert edge scroll off\n"));
	priv->vert_scroll_on = FALSE;
    }
    if (priv->horiz_scroll_on && (!(edge & BOTTOM_EDGE) || !finger || priv->palm)) {
	DBG(7, ErrorF("horiz edge scroll off\n"));
	priv->horiz_scroll_on = FALSE;
    }

    /* scroll processing */
    scroll_up = 0;
    scroll_down = 0;
    if (priv->vert_scroll_on) {
	/* + = down, - = up */
	while (hw->y - priv->scroll_y > para->scroll_dist_vert) {
	    scroll_down++;
	    priv->scroll_y += para->scroll_dist_vert;
	}
	while (hw->y - priv->scroll_y < -para->scroll_dist_vert) {
	    scroll_up++;
	    priv->scroll_y -= para->scroll_dist_vert;
	}
    }
    scroll_left = 0;
    scroll_right = 0;
    if (priv->horiz_scroll_on) {
	/* + = right, - = left */
	while (hw->x - priv->scroll_x > para->scroll_dist_horiz) {
	    scroll_right++;
	    priv->scroll_x += para->scroll_dist_horiz;
	}
	while (hw->x - priv->scroll_x < -para->scroll_dist_horiz) {
	    scroll_left++;
	    priv->scroll_x -= para->scroll_dist_horiz;
	}
    }

    /* movement */
    if (finger && !priv->vert_scroll_on && !priv->horiz_scroll_on &&
	!priv->finger_count && !priv->palm) {
	delay = MIN(delay, 13);
	if (priv->count_packet_finger > 3) { /* min. 3 packets */
	    dx = (hw->x - MOVE_HIST(2).x) / 2;
	    dy = (hw->y - MOVE_HIST(2).y) / 2;

	    if (priv->drag) {
		if (edge & RIGHT_EDGE) {
		    dx += clamp(para->edge_motion_speed - dx, 0, para->edge_motion_speed);
		} else if (edge & LEFT_EDGE) {
		    dx -= clamp(para->edge_motion_speed + dx, 0, para->edge_motion_speed);
		}
		if (edge & TOP_EDGE) {
		    dy -= clamp(para->edge_motion_speed + dy, 0, para->edge_motion_speed);
		} else if (edge & BOTTOM_EDGE) {
		    dy += clamp(para->edge_motion_speed - dy, 0, para->edge_motion_speed);
		}
	    }

	    /* speed in depence of distance/packet */
	    dist = move_distance( dx, dy );
	    speed = dist * para->accl;
	    if (speed > para->max_speed)
		{ /* set max speed factor */
		    speed = para->max_speed;
		}
	    else if (speed < para->min_speed)
		{ /* set min speed factor */
		    speed = para->min_speed;
		}

	    /* save the fraction for adding to the next priv->count_packet */
	    priv->frac_x = xf86modf((dx * speed) + priv->frac_x, &integral);
	    dx = integral;
	    priv->frac_y = xf86modf((dy * speed) + priv->frac_y, &integral);
	    dy = integral;
	}

	priv->count_packet_finger++;
    }
    else
	{ /* reset packet counter */
	    priv->count_packet_finger = 0;
	}


    buttons = ((hw->left  ? 0x01 : 0) |
	       (mid       ? 0x02 : 0) |
	       (hw->right ? 0x04 : 0) |
	       (hw->up    ? 0x08 : 0) |
	       (hw->down  ? 0x10 : 0) |
	       (hw->cbLeft  ? 0x20 : 0) |
	       (hw->cbRight ? 0x40 : 0));

    /* Flags */
    priv->finger_flag = finger;

    /* generate a history of the absolute positions */
    MOVE_HIST(0).y = hw->y;
    MOVE_HIST(0).x = hw->x;

    /* Post events */
    if (dx || dy)
	xf86PostMotionEvent(local->dev, 0, 0, 2, dx, dy);

    change = buttons ^ priv->lastButtons;
    while (change) {
	id = ffs(change); /* number of first set bit 1..32 is returned */
	change &= ~(1 << (id - 1));
	xf86PostButtonEvent(local->dev, FALSE, id, (buttons & (1 << (id - 1))), 0, 0);
    }
    priv->lastButtons = buttons;

    while (scroll_up-- > 0) {
	xf86PostButtonEvent(local->dev, FALSE, 4, !hw->up, 0, 0);
	xf86PostButtonEvent(local->dev, FALSE, 4, hw->up, 0, 0);
    }
    while (scroll_down-- > 0) {
	xf86PostButtonEvent(local->dev, FALSE, 5, !hw->down, 0, 0);
	xf86PostButtonEvent(local->dev, FALSE, 5, hw->down, 0, 0);
    }
    while (scroll_left-- > 0) {
	xf86PostButtonEvent(local->dev, FALSE, 6, TRUE, 0, 0);
	xf86PostButtonEvent(local->dev, FALSE, 6, FALSE, 0, 0);
    }
    while (scroll_right-- > 0) {
	xf86PostButtonEvent(local->dev, FALSE, 7, TRUE, 0, 0);
	xf86PostButtonEvent(local->dev, FALSE, 7, FALSE, 0, 0);
    }
    if (double_click) {
	int i;
	for (i = 0; i < 2; i++) {
	    xf86PostButtonEvent(local->dev, FALSE, 1, !hw->left, 0, 0);
	    xf86PostButtonEvent(local->dev, FALSE, 1, hw->left, 0, 0);
	}
    }


    /*
     * Handle auto repeat buttons
     */
    if ((hw->up || hw->down || hw->cbLeft || hw->cbRight) &&
	para->updown_button_scrolling) {
	priv->repeatButtons = buttons & 0x78;
	if (!priv->nextRepeat) {
	    priv->nextRepeat = hw->millis + 200;
	}
    } else {
	priv->repeatButtons = 0;
	priv->nextRepeat = 0;
    }

    if (priv->repeatButtons) {
	timeleft = TIME_DIFF(priv->nextRepeat, hw->millis);
	if (timeleft > 0)
	    delay = MIN(delay, timeleft);
	if (timeleft <= 0) {
	    int change, id;
	    change = priv->repeatButtons;
	    while (change) {
		id = ffs(change);
		change &= ~(1 << (id - 1));
		xf86PostButtonEvent(local->dev, FALSE, id, FALSE, 0, 0);
		xf86PostButtonEvent(local->dev, FALSE, id, TRUE, 0, 0);
	    }

	    priv->nextRepeat = hw->millis + 100;
	    delay = MIN(delay, 100);
	}
    }

    return delay;
}

static int
ControlProc(LocalDevicePtr local, xDeviceCtl * control)
{
    DBG(3, ErrorF("Control Proc called\n"));
    return Success;
}


static void
CloseProc (LocalDevicePtr local)
{
    DBG(3, ErrorF("Close Proc called\n"));
}

static int
SwitchMode (ClientPtr client, DeviceIntPtr dev, int mode)
{
    ErrorF("SwitchMode called\n");
    return Success;
}

static Bool
ConvertProc (LocalDevicePtr local,
	     int first,
	     int num,
	     int v0,
	     int v1,
	     int v2,
	     int v3,
	     int v4,
	     int v5,
	     int *x,
	     int *y)
{
    if (first != 0 || num != 2)
	return FALSE;

    *x = v0;
    *y = v1;

    return TRUE;
}


static Bool
QueryHardware (LocalDevicePtr local)
{
    SynapticsPrivatePtr priv = (SynapticsPrivatePtr) local->private;
    SynapticsSHMPtr para = priv->synpara;
    int retries;
    int mode;

    xf86Msg(X_INFO, "xfree driver for the synaptics touchpad %s\n", VERSION);

    if (priv->proto == SYN_PROTO_EVENT)
	return Success;

    /* is the synaptics touchpad active? */
    priv->isSynaptics = QueryIsSynaptics(local->fd);

    if ((!priv->isSynaptics) && (!para->repeater || (priv->fifofd == -1))) {
	xf86Msg(X_ERROR, "%s no synaptics touchpad detected and no repeater device\n",
		local->name);
	priv->isSynaptics = TRUE;
	return !Success;
    }
    para->isSynaptics = priv->isSynaptics;

    priv->protoBufTail = 0;
    if (!priv->isSynaptics) {
	xf86Msg(X_PROBED, "%s no synaptics touchpad, data piped to repeater fifo\n", local->name);
	synaptics_reset(local->fd);
	SynapticsEnableDevice(local->fd);
	return Success;
    }

    xf86Msg(X_PROBED, "%s synaptics touchpad found\n", local->name);

    retries = 3;
    while ((retries++ <= 3) && (synaptics_reset(local->fd) != Success))
	xf86Msg(X_ERROR, "%s reset failed\n", local->name);

    if (synaptics_identify(local->fd, &priv->identity) != Success)
	return !Success;
    para->identity = priv->identity;

    if (synaptics_model_id(local->fd, &priv->model_id) != Success)
	return !Success;
    para->model_id = priv->model_id;

    if (synaptics_capability(local->fd, &priv->capabilities, &priv->ext_cap) != Success)
	return !Success;
    para->capabilities = priv->capabilities;
    para->ext_cap = priv->ext_cap;

    mode = SYN_BIT_ABSOLUTE_MODE | SYN_BIT_HIGH_RATE;
    if (SYN_ID_MAJOR(priv->identity) >= 4)
	mode |= SYN_BIT_DISABLE_GESTURE;
    if (SYN_CAP_EXTENDED(priv->capabilities))
	mode |= SYN_BIT_W_MODE;
    if (synaptics_set_mode(local->fd, mode) != Success)
	return !Success;

    SynapticsEnableDevice(local->fd);

    PrintIdent(priv);

    return Success;
}

static Bool
SynapticsGetHwState(LocalDevicePtr local, SynapticsPrivatePtr priv,
		    struct SynapticsHwState *hw)
{
    if (priv->proto == SYN_PROTO_PSAUX) {
	return SynapticsParseRawPacket(local, priv, hw);
    } else if (priv->proto == SYN_PROTO_EVENT) {
	return SynapticsParseEventData(local, priv, hw);
    } else {
	return !Success;
    }
}

static Bool
SynapticsParseEventData(LocalDevicePtr local, SynapticsPrivatePtr priv,
			struct SynapticsHwState *hw)
{
    struct input_event ev;

    while (SynapticsReadEvent(priv, &ev) == Success) {
	switch (ev.type) {
	case EV_SYN:
	    switch (ev.code) {
	    case SYN_REPORT:
		*hw = priv->hwState;
		return Success;
	    }
	case EV_KEY:
	    switch (ev.code) {
	    case BTN_LEFT:
		priv->hwState.left = (ev.value ? TRUE : FALSE);
		break;
	    case BTN_RIGHT:
		priv->hwState.right = (ev.value ? TRUE : FALSE);
		break;
	    case BTN_FORWARD:
		priv->hwState.up = (ev.value ? TRUE : FALSE);
		break;
	    case BTN_BACK:
		priv->hwState.down = (ev.value ? TRUE : FALSE);
		break;
	    case BTN_0:						/* multi-btn-0 */
		priv->hwState.up = (ev.value ? TRUE : FALSE);
		break;
	    case BTN_1:						/* multi-btn-1 */
		priv->hwState.down = (ev.value ? TRUE : FALSE);
		break;
	    case BTN_2:						/* multi-btn-2 */
		priv->hwState.cbLeft = (ev.value ? TRUE : FALSE);
		break;
	    case BTN_3:						/* multi-btn-3 */
		priv->hwState.cbRight = (ev.value ? TRUE : FALSE);
		break;
	    case BTN_TOOL_FINGER:
		priv->hwState.oneFinger = (ev.value ? TRUE : FALSE);
		break;
	    case BTN_TOOL_DOUBLETAP:
		priv->hwState.twoFingers = (ev.value ? TRUE : FALSE);
		break;
	    case BTN_TOOL_TRIPLETAP:
		priv->hwState.threeFingers = (ev.value ? TRUE : FALSE);
		break;
	    }
	    break;
	case EV_ABS:
	    switch (ev.code) {
	    case ABS_X:
		priv->hwState.x = ev.value;
		break;
	    case ABS_Y:
		if (priv->swapY) {
		    priv->hwState.y = YMAX_NOMINAL + YMIN_NOMINAL - ev.value;
		} else {
		    priv->hwState.y = ev.value;
		}
		break;
	    case ABS_PRESSURE:
		priv->hwState.z = ev.value;
		break;
	    case ABS_TOOL_WIDTH:
		priv->hwState.fingerWidth = ev.value;
		break;
	    }
	    break;
	case EV_MSC:
	    switch (ev.code) {
	    case MSC_GESTURE:
		priv->swapY = TRUE;
		switch (ev.value) {
		case 0:
		    priv->hwState.oneFinger = FALSE;
		    priv->hwState.twoFingers = TRUE;
		    priv->hwState.threeFingers = FALSE;
		    priv->hwState.fingerWidth = 5;
		    break;
		case 1:
		    priv->hwState.oneFinger = FALSE;
		    priv->hwState.twoFingers = FALSE;
		    priv->hwState.threeFingers = TRUE;
		    priv->hwState.fingerWidth = 5;
		    break;
		default:
		    priv->hwState.oneFinger = TRUE;
		    priv->hwState.twoFingers = FALSE;
		    priv->hwState.threeFingers = FALSE;
		    priv->hwState.fingerWidth = ev.value;
		    break;
		}
		break;
	    }
	    break;
	}
    }
    return !Success;
}

static Bool
SynapticsReadEvent(SynapticsPrivatePtr priv, struct input_event *ev)
{
    int i, c;
    unsigned char *pBuf, u;

    for (i = 0; i < sizeof(struct input_event); i++) {
	if ((c = XisbRead(priv->buffer)) < 0)
	    return !Success;
	u = (unsigned char)c;
	pBuf = (unsigned char *)ev;
	pBuf[i] = u;
    }
    return Success;
}

static Bool
SynapticsParseRawPacket(LocalDevicePtr local, SynapticsPrivatePtr priv,
			struct SynapticsHwState *hw)
{
    Bool ret = SynapticsGetPacket(local, priv);
    int newabs = SYN_MODEL_NEWABS(priv->model_id);
    unsigned char *buf;
    int w;

    if (ret != Success)
	return ret;

    buf = priv->protoBuf;
    if (newabs) {			    /* newer protos...*/
	DBG(7, ErrorF("using new protocols\n"));
	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];
	w = (((buf[0] & 0x30) >> 2) |
	     ((buf[0] & 0x04) >> 1) |
	     ((buf[3] & 0x04) >> 2));

	hw->left  = (buf[0] & 0x01) ? 1 : 0;
	hw->right = (buf[0] & 0x02) ? 1 : 0;
	hw->up    = 0;
	hw->down  = 0;

	if (SYN_CAP_EXTENDED(priv->capabilities)) {
	    if (SYN_CAP_FOUR_BUTTON(priv->capabilities)) {
		hw->up = ((buf[3] & 0x01)) ? 1 : 0;
		if (hw->left)
		    hw->up = !hw->up;
		hw->down = ((buf[3] & 0x02)) ? 1 : 0;
		if (hw->right)
		    hw->down = !hw->down;
	    }
	    if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap)) {
		/* aka. type with 6 buttons */
		if ((buf[3]&2) ? !hw->right : hw->right) {
		    if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap) > 2) {
			hw->cbLeft  = (buf[4] & 0x02) ? TRUE : FALSE;
			hw->cbRight = (buf[5] & 0x02) ? TRUE : FALSE;
		    }
		    hw->up      = (buf[4] & 0x01) ? TRUE : FALSE;
		    hw->down    = (buf[5] & 0x01) ? TRUE : FALSE;
		} else {
		    hw->cbLeft = hw->cbRight = hw->up = hw->down = FALSE;
		}
	    }
	}
    } else {			    /* old proto...*/
	DBG(7, ErrorF("using old protocol\n"));
	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));
	w = (((buf[1] & 0x80) >> 4) |
	     ((buf[0] & 0x04) >> 1));

	hw->left  = (buf[0] & 0x01) ? 1 : 0;
	hw->right = (buf[0] & 0x02) ? 1 : 0;
	hw->up    = 0;
	hw->down  = 0;
	hw->cbLeft = hw->cbRight = hw->up = hw->down = FALSE;
    }

    hw->y = YMAX_NOMINAL + YMIN_NOMINAL - hw->y;

    hw->oneFinger = FALSE;
    hw->twoFingers = FALSE;
    hw->threeFingers = FALSE;
    hw->fingerWidth = 0;

    if (hw->z > 0) {
	int w_ok = 0;
	/*
	 * Use capability bits to decide if the w value is valid.
	 * If not, set it to 5, which corresponds to a finger of
	 * normal width.
	 */
	if (SYN_CAP_EXTENDED(priv->capabilities)) {
	    if ((w >= 0) && (w <= 1)) {
		w_ok = SYN_CAP_MULTIFINGER(priv->capabilities);
	    } else if (w == 2) {
		w_ok = SYN_MODEL_PEN(priv->model_id);
	    } else if ((w >= 4) && (w <= 15)) {
		w_ok = SYN_CAP_PALMDETECT(priv->capabilities);
	    }
	}
	if (!w_ok)
	    w = 5;

	switch (w) {
	case 0:
	    hw->twoFingers = TRUE;
	    hw->fingerWidth = 5;
	    break;
	case 1:
	    hw->threeFingers = TRUE;
	    hw->fingerWidth = 5;
	    break;
	default:
	    hw->oneFinger = TRUE;
	    hw->fingerWidth = w;
	    break;
	}
    }

    priv->hwState = *hw;

    return Success;
}

/*
 * Decide if the current packet stored in priv->protoBuf is valid.
 */
static Bool
PacketOk(SynapticsPrivatePtr priv)
{
    unsigned char *buf = priv->protoBuf;
    int newabs = SYN_MODEL_NEWABS(priv->model_id);

    if (newabs ? ((buf[0] & 0xC8) != 0x80) : ((buf[0] & 0xC0) != 0xC0)) {
	DBG(4, ErrorF("Synaptics driver lost sync at 1st byte\n"));
	return FALSE;
    }

    if (!newabs && ((buf[1] & 0x60) != 0x00)) {
	DBG(4, ErrorF("Synaptics driver lost sync at 2nd byte\n"));
	return FALSE;
    }

    if ((newabs ? ((buf[3] & 0xc8) != 0xc0) : ((buf[3] & 0xc0) != 0x80))) {
	DBG(4, ErrorF("Synaptics driver lost sync at 4th byte\n"));
	return FALSE;
    }

    if (!newabs && ((buf[4] & 0x60) != 0x00)) {
	DBG(4, ErrorF("Synaptics driver lost sync at 5th byte\n"));
	return FALSE;
    }

    return TRUE;
}

static Bool
SynapticsGetPacket(LocalDevicePtr local, SynapticsPrivatePtr priv)
{
    int count = 0;
    int c;
    unsigned char u;

    while ((c = XisbRead(priv->buffer)) >= 0) {
	u = (unsigned char)c;

	/* test if there is a reset sequence received */
	if ((c == 0x00) && (priv->lastByte == 0xAA)) {
	    if (xf86WaitForInput(local->fd, 50000) == 0) {
		DBG(7, ErrorF("Reset received\n"));
		QueryHardware(local);
	    } else
		DBG(3, ErrorF("faked reset received\n"));
	}
	priv->lastByte = u;

	/* when there is no synaptics touchpad pipe the data to the repeater fifo */
	if (!priv->isSynaptics) {
	    xf86write(priv->fifofd, &u, 1);
	    if (++count >= 3)
		return !Success;
	    continue;
	}

	/* to avoid endless loops */
	if (count++ > 30) {
	    ErrorF("Synaptics driver lost sync... got gigantic packet!\n");
	    return !Success;
	}

	priv->protoBuf[priv->protoBufTail++] = u;

	/* Check that we have a valid packet. If not, we are out of sync,
	   so we throw away the first byte in the packet.*/
	if (priv->protoBufTail >= 6) {
	    if (!PacketOk(priv)) {
		int i;
		for (i = 0; i < priv->protoBufTail - 1; i++)
		    priv->protoBuf[i] = priv->protoBuf[i + 1];
		priv->protoBufTail--;
		priv->outOfSync++;
		if (priv->outOfSync > MAX_UNSYNC_PACKETS) {
		    priv->outOfSync = 0;
		    DBG(3, ErrorF("Synaptics synchronization lost too long -> reset touchpad.\n"));
		    QueryHardware(local); /* including a reset */
		    continue;
		}
	    }
	}

	if (priv->protoBufTail >= 6)
	    { /* Full packet received */
		if (priv->outOfSync > 0) {
		    priv->outOfSync = 0;
		    DBG(4, ErrorF("Synaptics driver resynced.\n"));
		}
		priv->protoBufTail = 0;
		return Success;
	    }
    }

    return !Success;
}

static void
PrintIdent(SynapticsPrivatePtr priv)
{
    xf86Msg(X_PROBED, " Synaptics Touchpad, model: %d\n", SYN_ID_MODEL(priv->identity));
    xf86Msg(X_PROBED, " Firmware: %d.%d\n", SYN_ID_MAJOR(priv->identity),
	    SYN_ID_MINOR(priv->identity));

    if (SYN_MODEL_ROT180(priv->model_id))
	xf86Msg(X_PROBED, " 180 degree mounted touchpad\n");
    if (SYN_MODEL_PORTRAIT(priv->model_id))
	xf86Msg(X_PROBED, " portrait touchpad\n");
    xf86Msg(X_PROBED, " Sensor: %d\n", SYN_MODEL_SENSOR(priv->model_id));
    if (SYN_MODEL_NEWABS(priv->model_id))
	xf86Msg(X_PROBED, " new absolute packet format\n");
    if (SYN_MODEL_PEN(priv->model_id))
	xf86Msg(X_PROBED, " pen detection\n");

    if (SYN_CAP_EXTENDED(priv->capabilities)) {
	xf86Msg(X_PROBED, " Touchpad has extended capability bits\n");
	if (SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap))
	    xf86Msg(X_PROBED, " -> %d mulit-buttons, i.e. besides standard buttons\n",
		    (int)(SYN_CAP_MULTI_BUTTON_NO(priv->ext_cap)));
	else if (SYN_CAP_FOUR_BUTTON(priv->capabilities))
	    xf86Msg(X_PROBED, " -> four buttons\n");
	if (SYN_CAP_MULTIFINGER(priv->capabilities))
	    xf86Msg(X_PROBED, " -> multifinger detection\n");
	if (SYN_CAP_PALMDETECT(priv->capabilities))
	    xf86Msg(X_PROBED, " -> palm detection\n");
    }
}