summaryrefslogtreecommitdiff
path: root/src/microtouch.c
blob: 29dd853253bb45cdcc1a6c21f7453179d055a2d9 (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
/* 
 * Copyright (c) 1998  Metro Link Incorporated
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, cpy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Except as contained in this notice, the name of the Metro Link shall not be
 * used in advertising or otherwise to promote the sale, use or other dealings
 * in this Software without prior written authorization from Metro Link.
 *
 */
/* 
 * Based, in part, on code with the following copyright notice:
 *
 * Copyright 1996 by Patrick Lecoanet, France. <lecoanet@cenaath.cena.dgac.fr>       
 *                                                                            
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is  hereby granted without fee, provided that
 * the  above copyright   notice appear  in   all  copies and  that both  that
 * copyright  notice   and   this  permission   notice  appear  in  supporting
 * documentation, and that   the  name of  Patrick  Lecoanet not  be  used  in
 * advertising or publicity pertaining to distribution of the software without
 * specific,  written      prior  permission.     Patrick Lecoanet   makes  no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.                   
 *                                                                            
 * PATRICK LECOANET DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT  SHALL PATRICK LECOANET BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA  OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS  ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 */
/* $XFree86: xc/programs/Xserver/hw/xfree86/input/microtouch/microtouch.c,v 1.11 1999/08/28 10:43:36 dawes Exp $ */

#define _microtouch_C_
/*****************************************************************************
 *	Standard Headers
 ****************************************************************************/

#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	*/

/*****************************************************************************
 *	Local Headers
 ****************************************************************************/
#include "microtouch.h"

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

/*****************************************************************************
 *	Local Variables
 ****************************************************************************/

static InputInfoPtr
MuTouchPreInit(InputDriverPtr drv, IDevPtr dev, int flags);


InputDriverRec MICROTOUCH = {
  1,
  "microtouch",
  NULL,
  MuTouchPreInit,
  /*MuTouchUnInit*/ NULL,
  NULL,
  0
};

#ifdef XFree86LOADER

static XF86ModuleVersionInfo VersionRec =
{
	"microtouch",
	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 const char *reqSymbols[] = {
	"AddEnabledDevice",
	"ErrorF",
	"InitButtonClassDeviceStruct",
	"InitProximityClassDeviceStruct",
	"InitValuatorAxisStruct",
	"InitValuatorClassDeviceStruct",
	"RemoveEnabledDevice",
	"Xcalloc",
	"Xfree",
	"XisbBlockDuration",
	"XisbFree",
	"XisbNew",
	"XisbRead",
	"XisbTrace",
	"XisbWrite",
	"screenInfo",
	"xf86AddInputDriver",
	"xf86AllocateInput",
	"xf86CloseSerial",
	"xf86CollectInputOptions",
	"xf86ErrorF",
	"xf86ErrorFVerb",
	"xf86FindOptionValue",
	"xf86FlushInput",
	"xf86GetMotionEvents",
	"xf86GetVerbosity",
	"xf86LoaderReqSymLists",
	"xf86MotionHistoryAllocate",
	"xf86Msg",
	"xf86NameCmp",
	"xf86OpenSerial",
	"xf86OptionListCreate",
	"xf86OptionListFree",
	"xf86OptionListReport",
	"xf86PostButtonEvent",
	"xf86PostMotionEvent",
	"xf86PostProximityEvent",
	"xf86ProcessCommonOptions",
	"xf86ScaleAxis",
	"xf86SetIntOption",
	"xf86SetSerial",
	"xf86SetStrOption",
	"xf86XInputSetScreen",
	"xf86XInputSetSendCoreEvents",
	"xf86memset",
	"xf86sscanf",
	"xf86strcmp",
	"xf86strlen",
	"xf86strncmp",
	"xf86strncpy",
        NULL
};

static pointer
SetupProc(	pointer module,
			pointer options,
			int *errmaj,
			int *errmin )
{
        xf86LoaderReqSymLists(reqSymbols, NULL);
	xf86AddInputDriver(&MICROTOUCH, module, 0);
	return (pointer) 1;
}

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

#endif /* XFree86LOADER */


static const char *default_options[] =
{
	"BaudRate", "9600",
	"StopBits", "1",
	"DataBits", "8",
	"Parity", "None",
	"Vmin", "5",
	"Vtime", "1",
	"FlowControl", "None"
};
static const char *fallback_options[] =
{
	"BaudRate", "9600",
	"StopBits", "2",
	"DataBits", "7",
	"Parity", "None",
	"Vmin", "1",
	"Vtime", "1",
	"FlowControl", "None"
};


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



static InputInfoPtr
MuTouchPreInit(InputDriverPtr drv, IDevPtr dev, int flags)
{
	LocalDevicePtr local;
	MuTPrivatePtr priv;

	char *s;

	priv = xcalloc (1, sizeof (MuTPrivateRec));
	if (!priv)
		return NULL;

	local = xf86AllocateInput(drv, 0);
	if (!local) {
		xfree(priv);
		return NULL;
	}

	local->type_name = XI_TOUCHSCREEN;
	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->dev = NULL;
	local->private = priv;
	local->private_flags = 0;
	local->flags = XI86_POINTER_CAPABLE | XI86_SEND_DRAG_EVENTS;
	local->conf_idev = dev;

	xf86CollectInputOptions(local, default_options, NULL);

	xf86OptionListReport(local->options);

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

	priv->min_x = xf86SetIntOption( local->options, "MinX", 0 );
	priv->max_x = xf86SetIntOption( local->options, "MaxX", 1000 );
	priv->min_y = xf86SetIntOption( local->options, "MinY", 0 );
	priv->max_y = xf86SetIntOption( local->options, "MaxY", 1000 );
	priv->screen_num = xf86SetIntOption( local->options, "ScreenNumber", 0 );
	priv->button_number = xf86SetIntOption( local->options, "ButtonNumber", 1 );

	s = xf86FindOptionValue (local->options, "ReportingMode");
	if ((s) && (xf86NameCmp (s, "raw") == 0))
		priv->reporting_mode = TS_Raw;
	else
		priv->reporting_mode = TS_Scaled;

	priv->buffer = XisbNew (local->fd, 200);
	priv->proximity = FALSE;
	priv->button_down = FALSE;

	DBG (9, XisbTrace (priv->buffer, 1));

	MuTNewPacket (priv);
	if (QueryHardware(local) != Success)
	{
		ErrorF ("Unable to query/initialize MicroTouch hardware.\n");
		goto SetupProc_fail;
	}

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


	/* prepare to process touch packets */
	MuTNewPacket (priv);

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

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

  SetupProc_fail:
	if ((local) && (local->fd))
		xf86CloseSerial (local->fd);
	if ((local) && (local->name))
		xfree (local->name);

	if ((priv) && (priv->buffer))
		XisbFree (priv->buffer);
	if (priv)
		xfree (priv);
	return (local);
}

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:
	case DEVICE_CLOSE:
		RetValue = DeviceOff( dev );
		break;
	default:
		RetValue = BadValue;
	}

	return( RetValue );
}

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

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

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

	xf86FlushInput(local->fd);
	AddEnabledDevice (local->fd);
	dev->public.on = TRUE;
	return (Success);
}

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

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

	RemoveEnabledDevice (local->fd);
	dev->public.on = FALSE;
	return (Success);
}

static Bool
DeviceInit (DeviceIntPtr dev)
{
	LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
	MuTPrivatePtr priv = (MuTPrivatePtr) (local->private);
	unsigned char map[] =
	{0, 1};

	/* 
	 * these have to be here instead of in the SetupProc, because when the
	 * SetupProc is run at server startup, screenInfo is not setup yet
	 */
	priv->screen_width = screenInfo.screens[priv->screen_num]->width;
	priv->screen_height = screenInfo.screens[priv->screen_num]->height;

	/* 
	 * Device reports button press for 1 button.
	 */
	if (InitButtonClassDeviceStruct (dev, 1, map) == FALSE)
	{
		ErrorF ("Unable to allocate MicroTouch touchscreen ButtonClassDeviceStruct\n");
		return !Success;
	}

	/* 
	 * Device reports motions on 2 axes in absolute coordinates.
	 * Axes min and max values are reported in raw coordinates.
	 */
	if (InitValuatorClassDeviceStruct (dev, 2, xf86GetMotionEvents,
									local->history_size, Absolute) == FALSE)
	{
		ErrorF ("Unable to allocate MicroTouch touchscreen ValuatorClassDeviceStruct\n");
		return !Success;
	}
	else
	{
		InitValuatorAxisStruct (dev, 0, priv->min_x, priv->max_x,
								9500,
								0 /* min_res */ ,
								9500 /* max_res */ );
		InitValuatorAxisStruct (dev, 1, priv->min_y, priv->max_y,
								10500,
								0 /* min_res */ ,
								10500 /* max_res */ );
	}

	if (InitProximityClassDeviceStruct (dev) == FALSE)
	{
		ErrorF ("Unable to allocate MicroTouch touchscreen ProximityClassDeviceStruct\n");
		return !Success;
	}

	/* 
	 * Allocate the motion events buffer.
	 */
	xf86MotionHistoryAllocate (local);
	return (Success);
}

#define WORD_ASSEMBLY(byte1, byte2) (((byte2) << 7) | (byte1))

static void
ReadInput (LocalDevicePtr local)
{
	int x, y;
	int type;
	MuTPrivatePtr priv = (MuTPrivatePtr) (local->private);

	/* 
	 * set blocking to -1 on the first call because we know there is data to
	 * read. Xisb automatically clears it after one successful read so that
	 * succeeding reads are preceeded buy a select with a 0 timeout to prevent
	 * read from blocking indefinately.
	 */
	XisbBlockDuration (priv->buffer, -1);
	while (MuTGetPacket (priv) == Success)
	{
		type = priv->packet[0];
		x = WORD_ASSEMBLY (priv->packet[1], priv->packet[2]);
		y = WORD_ASSEMBLY (priv->packet[3], priv->packet[4]);

		if (priv->reporting_mode == TS_Scaled)
		{
			x = xf86ScaleAxis (x, 0, priv->screen_width, priv->min_x,
							   priv->max_x);
			y = xf86ScaleAxis (y, 0, priv->screen_height, priv->min_y,
							   priv->max_y);
		}

		xf86XInputSetScreen (local, priv->screen_num, x, y);

		if ((priv->proximity == FALSE) && (type & MuT_CONTACT))
		{
			priv->proximity = TRUE;
			xf86PostProximityEvent (local->dev, 1, 0, 2, x, y);
		}

		/* 
		 * Send events.
		 *
		 * We *must* generate a motion before a button change if pointer
		 * location has changed as DIX assumes this. This is why we always
		 * emit a motion, regardless of the kind of packet processed.
		 */
		xf86PostMotionEvent (local->dev, TRUE, 0, 2, x, y);

		/* 
		 * Emit a button press or release.
		 */
		if ((priv->button_down == FALSE) && (type & MuT_CONTACT))

		{
			xf86PostButtonEvent (local->dev, TRUE,
								 priv->button_number, 1, 0, 2, x, y);
			priv->button_down = TRUE;
		}
		if ((priv->button_down == TRUE) && !(type & MuT_CONTACT))
		{
			xf86PostButtonEvent (local->dev, TRUE,
								 priv->button_number, 0, 0, 2, x, y);
			priv->button_down = FALSE;
		}
		/* 
		 * the untouch should always come after the button release
		 */
		if ((priv->proximity == TRUE) && !(type & MuT_CONTACT))

		{
			priv->proximity = FALSE;
			xf86PostProximityEvent (local->dev, 0, 0, 2, x, y);
		}

		xf86ErrorFVerb( 3, "TouchScreen: x(%d), y(%d), %d %d %s\n",
						x, y, type, type & MuT_CONTACT,
						(type & MuT_CONTACT) ? "Press" : "Release" );
	}
}

static int
ControlProc (LocalDevicePtr local, xDeviceCtl * control)
{
	xDeviceTSCalibrationCtl *c = (xDeviceTSCalibrationCtl *) control;
	MuTPrivatePtr priv = (MuTPrivatePtr) (local->private);

	priv->min_x = c->min_x;
	priv->max_x = c->max_x;
	priv->min_y = c->min_y;
	priv->max_y = c->max_y;
	return (Success);
}

static void
CloseProc (LocalDevicePtr local)
{
}

static int
SwitchMode (ClientPtr client, DeviceIntPtr dev, int mode)
{
	LocalDevicePtr local = (LocalDevicePtr) dev->public.devicePrivate;
	MuTPrivatePtr priv = (MuTPrivatePtr) (local->private);
	if ((mode == TS_Raw) || (mode == TS_Scaled))
	{
		priv->reporting_mode = mode;
		return (Success);
	}
	else if ((mode == SendCoreEvents) || (mode == DontSendCoreEvents))
	{
		xf86XInputSetSendCoreEvents (local, (mode == SendCoreEvents));
		return (Success);
	}
	else
		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)
{
	MuTPrivatePtr priv = (MuTPrivatePtr) (local->private);

	if (priv->reporting_mode == TS_Raw)
	{
		*x = xf86ScaleAxis (v0, 0, priv->screen_width, priv->min_x,
							priv->max_x);
		*y = xf86ScaleAxis (v1, 0, priv->screen_height, priv->min_y,
							priv->max_y);
	}
	else
	{
		*x = v0;
		*y = v1;
	}
	return (TRUE);
}

static Bool
xf86MuTSendCommand (unsigned char *type, MuTPrivatePtr priv)
{
	int r;
	int retries = MuT_RETRIES;

	while (retries--)
	{
		if (xf86MuTSendPacket (type, strlen ( (char *)type), priv) != Success)
			continue;
		r = xf86MuTWaitReply ( (unsigned char *)MuT_OK, priv);
		if (r == ACK)
			return (TRUE);
		else if (r == NACK)
			return (FALSE);
	}
	return (FALSE);
}

/* 
 * The microtouch SMT3 factory default is 72N, but the recommended operating
 * mode is 81N. This code first tries 81N, but if that fails it switches to
 * 72N and puts the controller in 81N before proceeding
 */
static Bool
QueryHardware (LocalDevicePtr local)
{
        MuTPrivatePtr priv = (MuTPrivatePtr) local->private;
	pointer	fallback;
	Bool ret = Success;
	int cs7 = FALSE;

	fallback = xf86OptionListCreate (fallback_options,
				(sizeof (fallback_options) / sizeof (fallback_options[0])), 0);

	priv->cs7flag = TRUE;
	if (!xf86MuTSendCommand ( (unsigned char *)MuT_RESET, priv))
	{
		xf86ErrorFVerb( 5, 
			  "Switching Com Parameters to CS7, 2 stop bits, no parity\n" );
		xf86SetSerial (priv->buffer->fd, fallback);
		cs7 = TRUE;
		if (!xf86MuTSendCommand ( (unsigned char *)MuT_RESET, priv))
		{
			ret = !Success;
			goto done;
		}
	}
	if (!xf86MuTSendCommand ( (unsigned char *)MuT_ABDISABLE, priv))
	{
		ret = !Success;
		goto done;
	}
	if (!xf86MuTSendCommand ( (unsigned char *)MuT_SETRATE, priv))
	{
		ret = !Success;
		goto done;
	}
	if (cs7)
	{
		xf86ErrorFVerb( 5, 
		  "Switching Com Parameters back to CS8, 1 stop bit, no parity\n" );
		xf86SetSerial (priv->buffer->fd, local->options);
	}
	priv->cs7flag = FALSE;
	if (!xf86MuTSendCommand ( (unsigned char *)MuT_FORMAT_TABLET, priv))
	{
		ret = !Success;
		goto done;
	}
	if (!xf86MuTSendCommand ( (unsigned char *)MuT_MODE_STREAM, priv))
	{
		ret = !Success;
		goto done;
	}
	if (!xf86MuTSendCommand ( (unsigned char *)MuT_PARAM_LOCK, priv))
	{
		ret = !Success;
		goto done;
	}
	if( 1 )		/* Was:   if (xf86Verbose), but can't do that in 3.9N...	*/
	{
		if (xf86MuTSendPacket ( (unsigned char *)MuT_OUTPUT_IDENT, strlen (MuT_OUTPUT_IDENT),
							   priv) == Success)
		{
			if (MuTGetPacket (priv) == Success)
				xf86MuTPrintIdent (priv->packet);
		}

		/* some microtouch controllers support one command, some support the
		 * * other. If the first one get's a NACK, try the second. They both
		 * * return the same packet. */
		if (xf86MuTSendPacket ( (unsigned char *)MuT_UNIT_VERIFY, strlen (MuT_UNIT_VERIFY),
							   priv) == Success)
		{
			if ((MuTGetPacket (priv) == Success) &&
				(strcmp ( (char *)&(priv->packet[1]), MuT_ERROR) == 0))
			{
				if (xf86MuTSendPacket ( (unsigned char *)MuT_UNIT_TYPE,
								   strlen (MuT_UNIT_TYPE), priv) == Success)
				{
					if ((MuTGetPacket (priv) != Success))
					{
						ret = !Success;
						goto done;
					}
				}
			}
			ret = xf86MuTPrintHwStatus (priv->packet);
		}
	}

  done:
	xf86OptionListFree (fallback);

	return (ret);
}

static void
MuTNewPacket (MuTPrivatePtr priv)
{
	priv->lex_mode = microtouch_normal;
	priv->packeti = 0;
	priv->binary_pkt = FALSE;
}

/* 
 ***************************************************************************
 *
 * xf86MuTSendPacket --
 *  Emit a variable length packet to the controller.
 *  The function expects a valid buffer containing the
 *  command to be sent to the controller.  The command
 *  size is in len
 *  The buffer is filled with the leading and trailing
 *  character before sending.
 *
 ***************************************************************************
 */
static Bool
xf86MuTSendPacket (unsigned char *type, int len, MuTPrivatePtr priv)
{
	int result;
	unsigned char req[MuT_PACKET_SIZE];

	memset (req, 0, MuT_PACKET_SIZE);
	strncpy ((char *) &req[1],  (char *)type, strlen ( (char *)type));
	req[0] = MuT_LEAD_BYTE;
	req[len + 1] = MuT_TRAIL_BYTE;

	result = XisbWrite (priv->buffer, req, len + 2);
	if (result != len + 2)
	{
		xf86ErrorFVerb( 5, "System error while sending to MicroTouch touchscreen.\n" );
		return !Success;
	}
	else
		return Success;
}

/* 
 ***************************************************************************
 *   0 ACK
 *  -1 NACK
 *  -2 timeout
 *  -3 wrong packet type
 ***************************************************************************
 */
static int
xf86MuTWaitReply (unsigned char *type, MuTPrivatePtr priv)
{
	Bool ok;
	int wrong, empty;

	wrong = MuT_MAX_WRONG_PACKETS;
	empty = MuT_MAX_EMPTY_PACKETS;
	do
	{
		ok = !Success;

		/* 
		 * Wait half a second for the reply. The fuse counts down each
		 * timeout and each wrong packet.
		 */
		xf86ErrorFVerb( 4, "Waiting %d ms for data from port\n", MuT_MAX_WAIT / 1000 );
		MuTNewPacket (priv);
		XisbBlockDuration (priv->buffer, MuT_MAX_WAIT);
		ok = MuTGetPacket (priv);
		/* 
		 * type is a NULL terminated string of 0 - 2 characters. An empty
		 * string for type indicates that any reply type is acceptable.
		 */
		if (ok != Success)
		{
			xf86ErrorFVerb( 4, "Recieved empty packet.\n" );
			empty--;
			continue;
		}
		if (ok == Success)
		{
			/* 
			 * this bit of weirdness attempts to detect an ACK from the
			 * controller when it is in 7bit mode and the computer is in 8bit
			 * mode. If we see this pattern and send a NACK here the next
			 * level up will switch to 7 bit mode and try again
			 */
			if (priv->cs7flag && (priv->packet[1] == MuT_OK7) &&
				(priv->packet[2] == '\0'))
			{
				xf86ErrorFVerb( 4, "Detected the 7 bit ACK in 8bit mode.\n" );
				return (NACK);
			}
			if (strcmp ( (char *)&(priv->packet[1]),  (char *)type) == 0)
			{
				xf86ErrorFVerb( 5, "\t\tgot an ACK\n" );
				return (ACK);
			}
			else if (strcmp ( (char *)&(priv->packet[1]), MuT_ERROR) == 0)
			{
				xf86ErrorFVerb( 5, "\t\tgot a NACK\n" );
				return (NACK);
			}
			else
			{
				xf86ErrorFVerb( 2, "Wrong reply received\n" );
				ok = !Success;
				wrong--;
			}
		}
	}
	while (ok != Success && wrong && empty);

	if (wrong)
		return (TIMEOUT);
	else
		return (WRONG_PACKET);
}

static Bool
MuTGetPacket (MuTPrivatePtr priv)
{
	int count = 0;
	int c;

	while ((c = XisbRead (priv->buffer)) >= 0)
	{
		if (count++ > 100)
		{
			MuTNewPacket (priv);
			return (!Success);
		}

		switch (priv->lex_mode)
		{
		case microtouch_normal:
			if ((c == MuT_LEAD_BYTE) ||
				(priv->cs7flag && ((c & 0x7f) == MuT_LEAD_BYTE)))
			{
				xf86ErrorFVerb( 8, "Saw MuT_LEAD_BYTE\n" );
				priv->packet[priv->packeti++] = (unsigned char) c;
				priv->lex_mode = microtouch_body;
			}
			/* 
			 * binary touch packets do not have LEAD_BYTE or TRAIL_BYTE
			 * Instead, only the first byte has the 8th bit set.
			 */
			if (c & 0x80)
			{
				xf86ErrorFVerb( 8, "Saw BINARY start\n" );
				priv->packet[priv->packeti++] = (unsigned char) c;
				priv->lex_mode = mtouch_binary;
				priv->bin_byte = 0;
			}
			break;

		case mtouch_binary:
			priv->packet[priv->packeti++] = (unsigned char) c;
			priv->bin_byte++;
			if (priv->bin_byte == 4)
			{
				xf86ErrorFVerb( 8, "got a good BINARY packet\n" );
				MuTNewPacket (priv);
				priv->binary_pkt = TRUE;
				return (Success);
			}
			break;

		case microtouch_body:
			/* 
			 * apparently a new packet can start in the middle of another
			 * packet if they host sends something at the right time to
			 * trigger it.
			 */
			if ((c == MuT_LEAD_BYTE) ||
				(priv->cs7flag && ((c & 0x7f) == MuT_LEAD_BYTE)))
			{
				priv->packeti = 0;
			}
			if ((c == MuT_TRAIL_BYTE) ||
				(priv->cs7flag && ((c & 0x7f) == MuT_TRAIL_BYTE)))
			{
				/* null terminate the packet */
				priv->packet[priv->packeti++] = '\0';
				xf86ErrorFVerb( 8, "got a good packet\n" );
				MuTNewPacket (priv);
				return (Success);
			}
			else
				priv->packet[priv->packeti++] = (unsigned char) c;
			break;

		}
	}
	return (!Success);
}

/* 
 ***************************************************************************
 *
 * xf86MuTPrintIdent --
 *  Print type of touchscreen and features on controller board.
 *
 ***************************************************************************
 */
static void
xf86MuTPrintIdent (unsigned char *packet)
{
	int vers, rev;

	if (strlen ( (char *)packet) < 6)
		return;
	xf86Msg( X_PROBED, " MicroTouch touchscreen is " );
	if (strncmp ((char *) &packet[1], MuT_TOUCH_PEN_IDENT, 2) == 0)
		xf86ErrorF( "a TouchPen.\n" );
	else if (strncmp ((char *) &packet[1], MuT_SMT3_IDENT, 2) == 0)
		xf86ErrorF( "a Serial/SMT3.\n" );
	else if (strncmp ((char *) &packet[1], MuT_GENERAL_IDENT, 2) == 0)
		xf86ErrorF( "an SMT2, SMT3V or SMT3RV.\n" );
	else
		xf86ErrorF( "Unknown Type %c%c.\n", packet[1], packet[2] );
	sscanf ((char *) &packet[3], "%2d%2d", &vers, &rev);
	xf86Msg( X_PROBED, " MicroTouch controller firmware revision is %d.%d.\n", vers, rev);
}

/* 
 ***************************************************************************
 *
 * xf86MuTPrintHwStatus --
 *  Print status of hardware. That is if the controller report errors,
 *  decode and display them.
 *
 ***************************************************************************
 */
static Bool
xf86MuTPrintHwStatus (unsigned char *packet)
{
	int i;
	int err;

	for (i = 3; i < 7; i++)
	{
		if (packet[i] == 'R')
		{
			xf86Msg( X_PROBED,
							" MicroTouch controller is a resistive type.\n" );
		}

	}
	if (packet[7] == '1')
	{
		xf86Msg( X_PROBED, 
				" MicroTouch controller reports the following errors:\n" );
		err = packet[8];
		if (err & 0x01)
			xf86ErrorF( "\tReserved\n" );
		if (err & 0x02)
			xf86ErrorF( "\tROM error. Firmware checksum verification error.\n" );
		if (err & 0x04)
			xf86ErrorF( "\tPWM error. Unable to establish PWM operating range at power-up.\n" );
		if (err & 0x08)
			xf86ErrorF( "\tNOVRAM error. The operating parameters in the controller NOVRAM are invalid.\n" );
		if (err & 0x10)
			xf86ErrorF( "\tHWD error. The controller hardware failed.\n" );
		if (err & 0x20)
			xf86ErrorF( "\tReserved\n" );
		if (err & 0x40)
			xf86ErrorF( "\tCable NOVRAM error. The linearization data in the cable NOVRAM is invalid.\n" );
		if (err & 0x80)
			xf86ErrorF( "\tNOVRAM2 error. The linearization data in the controller NOVRAM is invalid.\n" );
		return (!Success);
	}

	return (Success);
}