File: synaptics.c

package info (click to toggle)
tpconfig 3.1.3-15
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 848 kB
  • sloc: ansic: 3,362; sh: 447; makefile: 14
file content (1035 lines) | stat: -rw-r--r-- 30,057 bytes parent folder | download | duplicates (4)
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
/***********************************************************************
* File name: synaptics.c                                               *
* Written by: Bruce Kall (extracted from tpconfig.c by C. Scott Ananian*
* Language: C                                                          *
* Version: 1.0                                                         *
* Purpose: Routines to manipulate the Synaptics touchpad.              *
*                                                                      *
*                                                                      *
***********************************************************************/

 
/* Copyright (C) 1997  C. Scott Ananian <cananian@alumni.princeton.edu>
 * Copyright (c) 1998-2000 Bruce Kalk <kall@compass.com>
 *
 * Currently maintained by: Bruce Kall, <kall@compass.com>
 *
 *   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.
 *
 *   $Id: synaptics.c,v 1.9 2002/02/22 20:31:30 bruce Exp $
 */

/*$Log: synaptics.c,v $
 *Revision 1.9  2002/02/22 20:31:30  bruce
 *Added for Mode 4 and Mode 4 options .. -M
 *
 *Revision 1.8  2001/04/23 20:37:38  bruce
 *Chanegd if(b1=AUX_ACK) to while(b1=AUX_ACK) to get
 *around kernel ack problem.
 *
 *Revision 1.7  2000/10/31 18:39:15  cph
 *Fix tab problem.
 *
 *Revision 1.6  2000/10/31 18:38:15  cph
 *Implement --sleep option to control sleep mode.
 *
 *Revision 1.5  2000/10/31 18:29:00  cph
 *Fix bug: was calling show_tap_mode in two places that should have been
 *calling show_packet_mode.
 *Fix bug: disallow -m for single_mode_byte pads.
 *Adjust usage message to show options appropriate for this pad.
 *
 *Revision 1.4  2000/10/31 18:17:40  cph
 *Fix error in usage message.  Add comments back to option switch.
 *
 *Revision 1.3  2000/10/31 18:07:49  cph
 ** Extern declarations for utilities are now in .h file.
 *
 ** Fix typo in usage.
 *
 ** Fix bug in send_cmd.
 *
 ** Add high-level debugging output.
 *
 ** Capture version information in is_Synaptics to eliminate unneeded
 *  additional identify request in set_firmware_options.
 *
 *Revision 1.2  2000/10/31 15:18:32  cph
 *Complete rewrite.  Modularized code, added symbolic definitions for
 *commands and bit layouts.
 *
 *Revision 1.1  2000/09/28 13:37:36  bruce
 *Initial revision
 **/

static char rcsid[]="$Header: /home/bruce/linux-stuff/tpconfig/tpconfig-3.1.3/RCS/synaptics.c,v 1.9 2002/02/22 20:31:30 bruce Exp $";

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <getopt.h>
#include <termios.h>
#include "tpconfig.h"

/* Capability bits */
#define STP_CAP_EXTENDED	0x8000 /* 1 = other cap bits supported */
#define STP_CAP_SLEEP		0x0010 /* 1 = sleep mode */
#define STP_CAP_FOUR_BUTTON	0x0004 /* 1 = four buttons */
#define STP_CAP_MULTI_FINGER	0x0002 /* 1 = multi-finger detection */
#define STP_CAP_PALM_DETECT	0x0001 /* 1 = palm detection */

/* Mode bits */
#define STP_MODE_ABSOLUTE(m)	(((m) & 0x80) != 0)
#define STP_MODE_RATE(m)	(((m) & 0x40) != 0)
#define STP_MODE_SLEEP(m)	(((m) & 0x08) != 0)
#define STP_MODE_GESTURE(m)	(((m) & 0x04) == 0)
#define STP_MODE_WMODE(m)	(((m) & 0x01) != 0)
/* STP_MODE_WMODE valid only in absolute mode when STP_CAP_EXTENDED is set. */

#define STP_SET_MODE_ABSOLUTE(m, a)	(((m) &~ 0x80) | ((a) << 7))
#define STP_SET_MODE_RATE(m, a)		(((m) &~ 0x40) | ((a) << 6))
#define STP_SET_MODE_SLEEP(m, a)	(((m) &~ 0x08) | ((a) << 3))
#define STP_SET_MODE_GESTURE(m, a)	(((m) &~ 0x04) | ((!a) << 2))
#define STP_SET_MODE_WMODE(m, a)	(((m) &~ 0x01) | (a))

/* Old-style mode bits */
#define STP_OMODE_CORNER(m)	(((m) & 0x80000000) >> 31)
#define STP_OMODE_Z_THRESH(m)	(((m) & 0x70000000) >> 28)
#define STP_OMODE_TAP_MODE(m)	(((m) & 0x0C000000) >> 26)
#define STP_OMODE_EDGE_MOTN(m)	(((m) & 0x03000000) >> 24)
#define STP_OMODE_ABSOLUTE(m)	(((m) & 0x00800000) >> 23)
#define STP_OMODE_RATE(m)	(((m) & 0x00400000) >> 22)
#define STP_OMODE_BAUD(m)	(((m) & 0x00080000) >> 19)
#define STP_OMODE_3_BUTTON(m)	(((m) & 0x00040000) >> 18)
#define STP_OMODE_MIDDLE(m)	(((m) & 0x00020000) >> 17)
#define STP_OMODE_HOP(m)	(((m) & 0x00010000) >> 16)
#define STP_OMODE_RIGHT_MGN(m)	(((m) & 0x0000F000) >> 12)
#define STP_OMODE_LEFT_MGN(m)	(((m) & 0x00000F00) >> 8)
#define STP_OMODE_TOP_MGN(m)	(((m) & 0x000000F0) >> 4)
#define STP_OMODE_BOTTOM_MGN(m)	 ((m) & 0x0000000F)

#define STP_SET_OMODE_CORNER(m, a)	(((m) &~ 0x80000000) | ((a) << 31))
#define STP_SET_OMODE_Z_THRESH(m, a)	(((m) &~ 0x70000000) | ((a) << 28))
#define STP_SET_OMODE_TAP_MODE(m, a)	(((m) &~ 0x0C000000) | ((a) << 26))
#define STP_SET_OMODE_EDGE_MOTN(m, a)	(((m) &~ 0x03000000) | ((a) << 24))
#define STP_SET_OMODE_ABSOLUTE(m, a)	(((m) &~ 0x00800000) | ((a) << 23))
#define STP_SET_OMODE_RATE(m, a)	(((m) &~ 0x00400000) | ((a) << 22))
#define STP_SET_OMODE_BAUD(m, a)	(((m) &~ 0x00080000) | ((a) << 19))
#define STP_SET_OMODE_3_BUTTON(m, a)	(((m) &~ 0x00040000) | ((a) << 18))
#define STP_SET_OMODE_MIDDLE(m, a)	(((m) &~ 0x00020000) | ((a) << 17))
#define STP_SET_OMODE_HOP(m, a)		(((m) &~ 0x00010000) | ((a) << 16))
#define STP_SET_OMODE_RIGHT_MGN(m, a)	(((m) &~ 0x0000F000) | ((a) << 12))
#define STP_SET_OMODE_LEFT_MGN(m, a)	(((m) &~ 0x00000F00) | ((a) << 8))
#define STP_SET_OMODE_TOP_MGN(m, a)	(((m) &~ 0x000000F0) | ((a) << 4))
#define STP_SET_OMODE_BOTTOM_MGN(m, a)	(((m) &~ 0x0000000F) |  (a))

/* Model ID bits */
#define STP_INFO_ROT180(id)	(((id) & 0x800000) >> 23)
#define STP_INFO_PORTRAIT(id)	(((id) & 0x400000) >> 22)
#define STP_INFO_SENSOR(id)	(((id) & 0x3F0000) >> 16)
#define STP_INFO_HARDWARE(id)	(((id) & 0x00FE00) >> 9)
#define STP_INFO_NEW_ABS(id)	(((id) & 0x000080) >> 7)
#define STP_INFO_CAP_PEN(id)	(((id) & 0x000040) >> 6)
#define STP_INFO_SIMPLE_CMD(id)	(((id) & 0x000020) >> 5)
#define STP_INFO_GEOMETRY(id)	 ((id) & 0x00000F)

/* Sensor types */
#define STP_SENSOR_UNKNOWN	0
#define STP_SENSOR_STANDARD	1
#define STP_SENSOR_MINI		2
#define STP_SENSOR_SUPER	3
#define STP_SENSOR_FLEXIBLE	7
#define STP_SENSOR_ULTRA_THIN	8
#define STP_SENSOR_WIDE_MODULE	9
#define STP_SENSOR_STAMP_MODULE	11
#define STP_SENSOR_SUBMINI	12
#define STP_SENSOR_MULTISWITCH	13
#define STP_SENSOR_ADV_TECH	15
#define STP_SENSOR_ULTRA_THIN_R	16

/* Geometry types */
#define STP_GEOMETRY_UNKNOWN	0
#define STP_GEOMETRY_STANDARD	1
#define STP_GEOMETRY_OVAL	2

/* Synaptics special queries */
#define STP_QRY_IDENTIFY	0x00
#define STP_QRY_READ_MODES	0x01
#define STP_QRY_READ_CAPS	0x02
#define STP_QRY_READ_MODEL_ID	0x03
#define STP_QRY_READ_SN_PREFIX	0x06
#define STP_QRY_READ_SN_SUFFIX	0x07
#define STP_QRY_READ_RES	0x08

static unsigned int fw_version_major;
static unsigned int fw_version_minor;
static unsigned int fw_version;
static unsigned int model_id;
/* Firmware prior to version 3.2 has four mode bytes */
#define QUAD_MODE_BYTE		0x0302

void
synaptics_usage (void) 
{
  printf
    ("Options for a Synaptics TouchPad:\n"
     "\n"
     "  -i, --info                    display current TouchPad configuration\n"
     "  -x, --reset                   perform a software reset on the TouchPad\n"
     "  -q, --quiet, --silent         suppress verbose output\n"
     "\n" 
     "  -a                            display absolute/relative mode\n"
     "      --absolute, --relative    set absolute/relative mode\n"
     "  -r, --rate=[0,1]              display/set reporting rate\n"
     "                                   0 = normal, 1 = high\n");
  if (single_mode_byte)
    printf
      ("  -t, --tapmode=[0-1]           display/set tapping mode:\n"
       "                                   0 = no tap gestures\n"
       "                                   1 = tap and drag\n"
       "  -s, --sleep=[0-1]             display/set sleep mode:\n"
       "                                   0 = disable, 1 = enable\n");
  else
    {
      printf
	("  -t, --tapmode=[0-3]           display/set tapping mode:\n"
	 "                                   0 = no tap gestures\n"
	 "                                   1 = tap-to-click only\n"
	 "                                   2 = tap and non-locking drag\n"
	 "                                   3 = tap and locking drag\n"
	 "  -2, --two-button              set two-button mode\n"
	 "  -3, --three-button            set three-button mode\n"
	 "  -c, --corner=[0-1]            display/enable corner taps:\n"
	 "                                   0 = disable, 1 = enable\n"
	 "  -e, --edgemode=[0,1,3]        display/set edge motion:\n"
	 "                                   0 = never\n"
	 "                                   1 = always\n"
	 "                                   3 = only during drag\n"
	 "  -m                            display which button corner taps simulate\n"
	 "      --middle-button           make corner taps simulate middle button\n"
	 "      --right-button            make corner taps simulate right button\n"
	 "  -z                            display z threshold setting\n"
	 "      --zthreshold=[0-7]        set z threshold (tap sensitivity; 0=light)\n"
	 "      --threshold=[0-7]         same as --zthreshold\n");
      if (fw_version < QUAD_MODE_BYTE)
	{
	  printf
	    ("  -M                            display edge margins\n"
	     "      --right-margin=[0-15]     set right edge margin (0=narrow)\n"
	     "      --left-margin=[0-15]      set left edge margin (0=narrow)\n"
	     "      --top-margin=[0-15]       set top edge margin (0=narrow)\n"
	     "      --bottom-margin=[0-15]    set bottom edge margin (0=narrow)\n");
	}
    }
  printf
    ("\n"
     "  -h, --help                    display this help and exit\n"
     "  -v, --version                 output version information\n"
     "  -D, --debug=[0-3]             generate debug output\n"
     "  -d, --device=DEVICE           use alternate device file\n"
     "\n");
}


/******************** Synaptics Specific Functions ****************************/

/* use the Synaptics extended ps/2 syntax to write a special command byte */
static void
send_cmd (int fd, byte cmd) 
{
  putbyte (fd, AUX_SET_SCALE11);
  putbyte (fd, AUX_SET_RES);
  putbyte (fd, (cmd & 0xC0) >> 6);
  putbyte (fd, AUX_SET_RES);
  putbyte (fd, (cmd & 0x30) >> 4);
  putbyte (fd, AUX_SET_RES);
  putbyte (fd, (cmd & 0x0C) >> 2);
  putbyte (fd, AUX_SET_RES);
  putbyte (fd,  cmd & 0x03);
}

static void
status_rqst (int fd, byte cmd, byte *byte1, byte *byte2, byte *byte3) 
{
  send_cmd (fd, cmd);
  putbyte (fd, AUX_GET_STATUS);
  *byte1 = getbyte (fd);
  *byte2 = getbyte (fd);
  *byte3 = getbyte (fd);
  if (DEBUG_LEVEL)
    fprintf (stderr, "[query %#02x => %#02x %#02x %#02x]\n",
	     cmd, *byte1, *byte2, *byte3);
}

static void
set_modes (int fd, unsigned int modes)
{
  if (DEBUG_LEVEL > DEBUG_LOW)
    fprintf (stderr, "[set modes: %#02x]\n", modes);
  if (single_mode_byte)
    {
      send_cmd (fd, modes);
      putbyte (fd, AUX_SET_SAMPLE);
      putbyte (fd, 20);
    }
  else
    {
      send_cmd (fd, (modes & 0xFF000000) >> 24);
      putbyte (fd, AUX_SET_SAMPLE);
      putbyte (fd, 10);
      send_cmd (fd, (modes & 0x00FF0000) >> 16);
      putbyte (fd, AUX_SET_SAMPLE);
      putbyte (fd, 20);
      if (fw_version < QUAD_MODE_BYTE)
	{
	  send_cmd (fd, (modes & 0x0000FF00) >> 8);
	  putbyte (fd, AUX_SET_SAMPLE);
	  putbyte (fd, 40);
	  send_cmd (fd, (modes & 0x000000FF));
	  putbyte (fd, AUX_SET_SAMPLE);
	  putbyte (fd, 60);
	}
    }
}

static void
query_identify (int fd,
		unsigned int *version_major,
		unsigned int *version_minor,
		unsigned int *model_code)
{
  byte b1, b2, b3;
  status_rqst (fd, STP_QRY_IDENTIFY, &b1, &b2, &b3);
  if (!ignore_selected_assertions)
    /* Recent 2.6 kernels return 0x03 instead.  */
    assert (b2 == 0x47 || b2 == 0x03);
  if (version_major) (*version_major) = (b3 & 0x0F);
  if (version_minor) (*version_minor) = b1;
  if (model_code)    (*model_code)    = ((b3 & 0xF0) >> 4);
}

static unsigned int
query_modes (int fd)
{
  byte b1, b2, b3;
  byte b4, b5, b6;
  status_rqst (fd, STP_QRY_READ_MODES, &b1, &b2, &b3);
  if (!ignore_selected_assertions)
    /* Recent 2.6 kernels return 0x03 instead.  */
    assert (b2 == 0x47 || b2 == 0x03);
  if (single_mode_byte)
    {
      if (!ignore_selected_assertions)
	assert (b1 == 0x3B);
      return b3;
    }
  else
    {
      /* Firmware prior to version 3.2 has four mode bytes */
      if (fw_version < QUAD_MODE_BYTE)
	{
	  status_rqst (fd, STP_QRY_READ_CAPS, &b4, &b5, &b6);
	  if (!ignore_selected_assertions)
	    /* Recent 2.6 kernels return 0x03 instead.  */
	    assert (b5 == 0x47 || b5 == 0x03);
	  return ((unsigned int) b1 << 24) | ((unsigned int) b3 << 16) |
	    ((unsigned int) b4 << 8) | (unsigned int) b6;
	}
      return ((unsigned int) b1 << 24) | ((unsigned int) b3 << 16);
    }
}

static unsigned int
query_caps (int fd)
{
  byte b1, b2, b3;
  status_rqst (fd, STP_QRY_READ_CAPS, &b1, &b2, &b3);
  assert (b2 == 0x47);
  return ((unsigned int) b1 << 8) | (unsigned int) b3;
}

static unsigned int
query_model_id (int fd)
{
  byte b1, b2, b3;
  unsigned int id;
  status_rqst (fd, STP_QRY_READ_MODEL_ID, &b1, &b2, &b3);
  id = ((b2 & 1)
	? 0x000001
	: (((unsigned int) b1 << 16)
	   | ((unsigned int) b2 << 8)
	   | (unsigned int) b3));
  single_mode_byte = STP_INFO_SIMPLE_CMD (id);
  return id;
}

static void
query_serial_number (int fd, unsigned int *sn_high, unsigned int *sn_low)
{
  byte b1, b2, b3, b4, b5, b6;
  status_rqst (fd, STP_QRY_READ_SN_PREFIX, &b1, &b2, &b3);
  status_rqst (fd, STP_QRY_READ_SN_SUFFIX, &b4, &b5, &b6);
  (*sn_high) = (b2 & 0xF0) >> 4;
  (*sn_low) = (((unsigned int) b1 << 24)
	       | ((unsigned int) b4 << 16)
	       | ((unsigned int) b5 << 8)
	       | (unsigned int) b6);
}

static void
query_resolutions (int fd, unsigned int *xres, unsigned int *yres)
{
  byte b1, b2, b3;
  status_rqst (fd, STP_QRY_READ_RES, &b1, &b2, &b3);
  if (!ignore_selected_assertions)
    assert ((b2 & 0x80) != 0);
  (*xres) = b1;
  (*yres) = b3;
}

/* initialize and determine whether this is a synaptics touchpad */
int
is_Synaptics (int fd)
{
  byte b1, b2, b3;
  int retval;
  status_rqst (fd, STP_QRY_IDENTIFY, &b1, &b2, &b3);
#if defined(__linux) && !defined(PATCHED_LINUX_KERNEL)
  while (b1 == AUX_ACK)
    {
      /* 2.2.x kernel fix an extra ack preceeded the status */
      b1 = b2;
      b2 = b3;
      b3 = getbyte (fd);
    }
#endif
  /* Recent 2.6 kernels return 0x03 instead.  */
  retval = (b2 == 0x47 || b2 == 0x03);
  if (retval)
    {
      fw_version_major = (b3 & 0x0F);
      fw_version_minor = b1;
      fw_version = (fw_version_major << 8) | fw_version_minor;
    }
  return retval;
}

void
reset_synaptics (int fd)
{
  fprintf (stdout,"Performing software reset on TouchPad.\n");
  putbyte (fd, AUX_RESET);
  getbyte_expected (fd, AUX_RESET_ACK1, "software reset ACK 1");
  getbyte_expected (fd, AUX_RESET_ACK2, "software reset ACK 2");
  putbyte (fd, AUX_ENABLE_DEV);
  fprintf (stdout, "Software reset of TouchPad complete.\n");
}

void
set_firmware_options (int fd, FILE *out)
{
  model_id = query_model_id (fd);
  firmware_rev
    = ((float) fw_version_major) + ((float) fw_version_minor / 10.0);
  fprintf (out, "Firmware: %d.%d (%s-byte mode).\n",
	   fw_version_major, fw_version_minor,
	   single_mode_byte ? "single" : "multiple");
}

static void
show_packet_mode (unsigned int modes, FILE *info)
{
  if (single_mode_byte)
    fprintf (info, "Packets: %s, %d packets per second.\n",
	     (STP_MODE_ABSOLUTE (modes)
	      ? (STP_INFO_NEW_ABS (model_id)
		 ? "absolute (new style)"
		 : "absolute (old style)")
	      : "relative"),
	     STP_MODE_RATE (modes) ? 80 : 40);
  else
    fprintf (info, "Packets: %s, %d packets per second.\n",
	     STP_OMODE_ABSOLUTE (modes) ? "absolute" : "relative",
	     STP_OMODE_RATE (modes) ? 80 : 40);
}

static void
show_sleep_mode (unsigned int modes, FILE *info)
{
  fprintf (info, "Sleep mode: %s.\n",
	   STP_MODE_SLEEP (modes) ? "on" : "off");
}

static void
show_tap_mode (unsigned int modes, FILE *info)
{
  if (single_mode_byte)
    fprintf (info, "Tap gestures: %s.\n",
	     STP_MODE_GESTURE (modes) ? "enabled" : "disabled");
  else
    {
      fprintf (info, "Corner taps %s;\t\t",
	       STP_OMODE_CORNER (modes) ? "enabled" : "disabled");
      switch (STP_OMODE_TAP_MODE (modes))
	{
	case 0:
	  fprintf (info, "no tap gestures");
	  break;
	case 1:
	  fprintf (info, "tap-to-click only");
	  break;
	case 2:
	  fprintf (info, "tap and non-locking drag");
	  break;
	case 3:
	  fprintf (info, "tap and locking drag");
	  break;
	}
      fprintf (info, ".\n");
    }
}

static void
show_edge_motion (unsigned int modes, FILE *info)
{
  fprintf (info, "Edge motion: ");
  switch (STP_OMODE_EDGE_MOTN (modes))
    {
    case 0:
      fprintf (info, "none");
      break;
    case 1:
      fprintf (info, "always");
      break;
    case 2:
      fprintf (info, "[illegal value 2]");
      break;
    case 3:
      fprintf (info, "during drag");
      break;
    }
  fprintf (info, ".\n");
}

static void
show_z_threshold (unsigned int modes, FILE *info)
{
  fprintf (info, "Z threshold: %d of 7.\n", STP_OMODE_Z_THRESH (modes));
}

static void
show_button_mode (unsigned int modes, FILE *info)
{
  fprintf (info, "%d button mode; corner tap is %s button click.\n",
	   STP_OMODE_3_BUTTON (modes) ? 3 : 2,
	   STP_OMODE_MIDDLE (modes) ? "middle" : "right");
}
static void
show_margins (unsigned int modes, FILE *info)
{
  fprintf (info, "Right margin: %ld, Left margin: %ld,\n",
	   STP_OMODE_RIGHT_MGN (modes), STP_OMODE_LEFT_MGN (modes));
  fprintf (info, "Top margin: %ld, Bottom margin: %ld.\n",
	   STP_OMODE_TOP_MGN (modes), STP_OMODE_BOTTOM_MGN (modes));
}

static int
get_argument (unsigned int limit)
{
  int delta;
  if (optarg[0] == '\0')
    return -1;
  else if (optarg[1] == '\0')
    {
      delta = optarg[0] - '0';
      return (delta >= 0 && delta <= limit ? delta : -1);
    }
  else if (optarg[2] == '\0')
    {
      delta = (optarg[0] - '0') * 10 + optarg[1] - '0';
      return (delta >= 0 && delta <= limit ? delta : -1);
    }
  else
    return -1;
}

static void
set_z_threshold (int fd)
{
  if (single_mode_byte)
    {
      fprintf (stderr, "Z threshold not supported on this TouchPad.\n");
      return;
    }
  if (optarg)
    {
      int a = get_argument (7);
      if (DEBUG_LEVEL)
        fprintf (stderr, "Z threshold set as \"%s\".\n", optarg);
      if (a < 0)
	{
	  fprintf (stderr, "Invalid Z threshold \"%s\" [use 0-7].\n", optarg);
	  return;
	}
      set_modes (fd, STP_SET_OMODE_Z_THRESH (query_modes (fd), a));
    }
  if (!silent)
    show_z_threshold (query_modes (fd), stdout);
}

static void
set_tap_mode (int fd)
{
  unsigned int limit = single_mode_byte ? 1 : 3;
  if (optarg)
    {
      int a = get_argument (limit);
      if (DEBUG_LEVEL)
        fprintf (stderr, "Tap mode set as \"%s\".\n", optarg);
      if (a < 0)
	{
	  fprintf (stderr, "Invalid tap mode \"%s\" [use 0-%d].\n",
		   optarg, limit);
	  return;
	}
      if (single_mode_byte)
	set_modes (fd, STP_SET_MODE_GESTURE (query_modes (fd), a));
      else
	set_modes (fd, STP_SET_OMODE_TAP_MODE (query_modes (fd), a));
    }
  if (!silent)
    show_tap_mode (query_modes (fd), stdout);
}

static void
set_edge_motion (int fd)
{
  if (single_mode_byte)
    {
      fprintf (stderr, "Edge motion not supported on this TouchPad.\n");
      return;
    }
  if (optarg)
    {
      int a = get_argument (3);
      if (DEBUG_LEVEL)
        fprintf (stderr, "Edge motion set as \"%s\".\n", optarg);
      if (a < 0 || a == 2)
	{
	  fprintf (stderr, "Invalid edge motion \"%s\" [use 0, 1, 3].\n",
		   optarg);
	  return;
	}
      set_modes (fd, STP_SET_OMODE_EDGE_MOTN (query_modes (fd), a));
    }
  if (!silent)
    show_edge_motion (query_modes (fd), stdout);
}

static void
set_corner_mode (int fd)
{
  if (single_mode_byte)
    {
      fprintf (stderr, "Corner mode not supported on this TouchPad.\n");
      return;
    }
  if (optarg)
    {
      int a = get_argument (1);
      if (DEBUG_LEVEL)
        fprintf (stderr, "Corner mode set as \"%s\".\n", optarg);
      if (a < 0)
	{
	  fprintf (stderr, "Invalid corner mode \"%s\" [use 0-1].\n", optarg);
	  return;
	}
      set_modes (fd, STP_SET_OMODE_CORNER (query_modes (fd), a));
    }
  if (!silent)
    show_tap_mode (query_modes (fd), stdout);
}

static void
set_packet_mode (int fd, int absolute)
{
  if (single_mode_byte)
    set_modes (fd, STP_SET_MODE_ABSOLUTE (query_modes (fd), absolute));
  else
    set_modes (fd, STP_SET_OMODE_ABSOLUTE (query_modes (fd), absolute));
  if (!silent)
    show_packet_mode (query_modes (fd), stdout);
}

static void
set_rate (int fd)
{
  if (optarg)
    {
      int a = get_argument (1);
      if (DEBUG_LEVEL)
        fprintf (stderr, "Rate set as \"%s\".\n", optarg);
      if (a < 0)
	{
	  fprintf (stderr, "Invalid rate \"%s\" [use 0-%c].\n",
		   optarg);
	  return;
	}
      if (single_mode_byte)
	set_modes (fd, STP_SET_MODE_RATE (query_modes (fd), a));
      else
	set_modes (fd, STP_SET_OMODE_RATE (query_modes (fd), a));
    }
  if (!silent)
    show_packet_mode (query_modes (fd), stdout);
}

static void
set_button_mode (int fd, int three)
{
  if (single_mode_byte)
    {
      fprintf (stderr, "Button mode not supported on this TouchPad.\n");
      return;
    }
  set_modes (fd, STP_SET_OMODE_3_BUTTON (query_modes (fd), three));
  if (!silent)
    show_button_mode (query_modes (fd), stdout);
}

static void
set_corner_click (int fd, int middle)
{
  if (single_mode_byte)
    {
      fprintf (stderr, "Corner click not supported on this TouchPad.\n");
      return;
    }
  set_modes (fd, STP_SET_OMODE_MIDDLE (query_modes (fd), middle));
  if (!silent)
    show_button_mode (query_modes (fd), stdout);
}

static void
set_sleep_mode (int fd)
{
  if (!single_mode_byte)
    {
      fprintf (stderr, "Sleep mode not supported on this TouchPad.\n");
      return;
    }
  if (optarg)
    {
      int a = get_argument (1);
      if (DEBUG_LEVEL)
        fprintf (stderr, "Sleep mode set as \"%s\".\n", optarg);
      if (a < 0)
	{
	  fprintf (stderr, "Invalid sleep mode \"%s\" [use 0-1].\n", optarg);
	  return;
	}
      set_modes (fd, STP_SET_MODE_SLEEP (query_modes (fd), a));
    }
  if (!silent)
    show_sleep_mode (query_modes (fd), stdout);
}

static void
set_right_margin (int fd)
{
  if (single_mode_byte || (fw_version >= QUAD_MODE_BYTE))
    {
      fprintf (stderr, "Adjustable margins not supported on this TouchPad.\n");
      return;
    }
  if (optarg)
    {
      int a = get_argument (15);
      if (DEBUG_LEVEL)
        fprintf (stderr, "Right margin set as \"%s\".\n", optarg);
      if (a < 0)
	{
	  fprintf (stderr, "Invalid right margin \"%s\" [use 0-15].\n", optarg);
	  return;
	}
      set_modes (fd, STP_SET_OMODE_RIGHT_MGN (query_modes (fd), a));
    }
  if (!silent)
    show_margins (query_modes (fd), stdout);
}

static void
set_left_margin (int fd)
{
  if (single_mode_byte || (fw_version >= QUAD_MODE_BYTE))
    {
      fprintf (stderr, "Adjustable margins not supported on this TouchPad.\n");
      return;
    }
  if (optarg)
    {
      int a = get_argument (15);
      if (DEBUG_LEVEL)
        fprintf (stderr, "Left margin set as \"%s\".\n", optarg);
      if (a < 0)
	{
	  fprintf (stderr, "Invalid left margin \"%s\" [use 0-15].\n", optarg);
	  return;
	}
      set_modes (fd, STP_SET_OMODE_LEFT_MGN (query_modes (fd), a));
    }
  if (!silent)
    show_margins (query_modes (fd), stdout);
}

static void
set_top_margin (int fd)
{
  if (single_mode_byte || (fw_version >= QUAD_MODE_BYTE))
    {
      fprintf (stderr, "Adjustable margins not supported on this TouchPad.\n");
      return;
    }
  if (optarg)
    {
      int a = get_argument (15);
      if (DEBUG_LEVEL)
        fprintf (stderr, "Top margin set as \"%s\".\n", optarg);
      if (a < 0)
	{
	  fprintf (stderr, "Invalid top margin \"%s\" [use 0-15].\n", optarg);
	  return;
	}
      set_modes (fd, STP_SET_OMODE_TOP_MGN (query_modes (fd), a));
    }
  if (!silent)
    show_margins (query_modes (fd), stdout);
}

static void
set_bottom_margin (int fd)
{
  if (single_mode_byte || (fw_version >= QUAD_MODE_BYTE))
    {
      fprintf (stderr, "Adjustable margins not supported on this TouchPad.\n");
      return;
    }
  if (optarg)
    {
      int a = get_argument (15);
      if (DEBUG_LEVEL)
        fprintf (stderr, "Bottom margin set as \"%s\".\n", optarg);
      if (a < 0)
	{
	  fprintf (stderr, "Invalid bottom margin \"%s\" [use 0-15].\n", optarg);
	  return;
	}
      set_modes (fd, STP_SET_OMODE_BOTTOM_MGN (query_modes (fd), a));
    }
  if (!silent)
    show_margins (query_modes (fd), stdout);
}

static void
synaptics_info (int fd, FILE *info)
{
  unsigned int modes;

  fprintf (info, "Sensor type: ");
  switch (STP_INFO_SENSOR (model_id))
    {
    case STP_SENSOR_STANDARD:
      fprintf (info, "Standard TouchPad");
      break;
    case STP_SENSOR_MINI:
      fprintf (info, "Mini module");
      break;
    case STP_SENSOR_SUPER:
      fprintf (info, "Super module");
      break;
    case STP_SENSOR_FLEXIBLE:
      fprintf (info, "Flexible pad");
      break;
    case STP_SENSOR_ULTRA_THIN:
      fprintf (info, "Ultra-thin module");
      break;
    case STP_SENSOR_WIDE_MODULE:
      fprintf (info, "Wide pad module");
      break;
    case STP_SENSOR_STAMP_MODULE:
      fprintf (info, "Stamp pad module");
      break;
    case STP_SENSOR_SUBMINI:
      fprintf (info, "SubMini module");
      break;
    case STP_SENSOR_MULTISWITCH:
      fprintf (info, "MultiSwitch module");
      break;
    case STP_SENSOR_ADV_TECH:
      fprintf (info, "Advanced Technology Pad");
      break;
    case STP_SENSOR_ULTRA_THIN_R:
      fprintf (info, "Ultra-thin module, connector reversed");
      break;
    default:
      fprintf (info, "unknown (%d)", STP_INFO_SENSOR (model_id));
      break;
    }
  fprintf (info, ".\n");
  fprintf (info, "Geometry: ");
  switch (STP_INFO_GEOMETRY (model_id))
    {
    case STP_GEOMETRY_STANDARD:
      fprintf (info, "rectangular");
      break;
    case STP_GEOMETRY_OVAL:
      fprintf (info, "oval");
      break;
    default:
      fprintf (info, "unknown (%d)", STP_INFO_GEOMETRY (model_id));
      break;
    }
  fprintf (info, "/%s/%s.\n",
	   (STP_INFO_PORTRAIT (model_id)) ? "portrait" : "landscape",
	   (STP_INFO_ROT180 (model_id)) ? "down" : "up");
  modes = query_modes (fd);
  show_packet_mode (modes, info);
  show_tap_mode (modes, info);
  if (single_mode_byte)
    show_sleep_mode (modes, info);
  else
    {
      show_edge_motion (modes, info);
      show_z_threshold (modes, info);
      show_button_mode (modes, info);
      if (fw_version < QUAD_MODE_BYTE)
	show_margins (modes, info);
    }
}

void
synaptics_functions (int c, int fd, char **argv)
{
  switch (c) 
    {
    case 'h': /* --help */
      printf ("Usage: %s [OPTION]...\n", (argv[0]));
      synaptics_usage ();
      printf ("Report bugs to <kall@compass.com>\n");
      break;
    case 'q': /* --quiet */
      silent = 1;
      break;
    case 'x': /* --reset */
      reset_synaptics (fd);
      break;
    case 'i': /* --info */
      if (!silent)
	synaptics_info (fd, stdout);
      break;
    case 'z': /* --zthreshold */
      set_z_threshold (fd);
      break;
    case 't': /* --tapmode */
      set_tap_mode (fd);
      break;
    case 'e': /* --edgemode */
      set_edge_motion (fd);
      break;
    case 'c': /* --corner */
      set_corner_mode (fd);
      break;
    case 'A': /* --absolute */
      set_packet_mode (fd, 1);
      break;
    case 'R': /* --relative */
      set_packet_mode (fd, 0);
      break;
    case 'a':
      if (optarg)
	{
	  int a = get_argument (1);
	  if (a < 0)
	    fprintf (stderr, "Invalid packet mode \"%s\" [use 0-1].\n",
		     optarg);
	  else
	    set_packet_mode (fd, a);
	}
      else if (!silent)
	show_tap_mode (query_modes (fd), stdout);
      break;
    case 'r': /* --rate */
      set_rate (fd);
      break;
    case '2': /* --two-button */
      set_button_mode (fd, 0);
      break;
    case '3': /* --three-button */
      set_button_mode (fd, 1);
      break;
    case '<': /* --middle-button */
      set_corner_click (fd, 1);
      break;
    case '>': /* --right-button */
      set_corner_click (fd, 0);
      break;
    case 'm':
      if (single_mode_byte)
	fprintf (stderr, "Corner click not supported on this TouchPad.\n");
      else if (optarg)
	{
	  int a = get_argument (1);
	  if (a < 0)
	    fprintf (stderr, "Invalid corner-click mode \"%s\" [use 0-1].\n",
		     optarg);
	  else
	    set_corner_click (fd, a);
	}
      else if (!silent)
	show_button_mode (query_modes (fd), stdout);
      break;
    case 's': /* --sleep */
      set_sleep_mode (fd);
      break;
    case 'M':
      if (single_mode_byte || (fw_version >= QUAD_MODE_BYTE))
	fprintf (stderr, "Adjustable margins not supported on this TouchPad.\n");
      else if (!silent)
	/* Firmware prior to version 3.2 has four mode bytes */
	show_margins (query_modes (fd), stdout);
      break;
    case '4': /* --right-margin */
      set_right_margin (fd);
      break;
    case '5': /* --left-margin */
      set_left_margin (fd);
      break;
    case '6': /* --top-margin */
      set_top_margin (fd);
      break;
    case '7': /* --bottom-margin */
      set_bottom_margin (fd);
      break;
    case '?':
      break;
    default:
      fprintf (stderr, "Unknown option %c.\n", c);
      break;
    }
}