summaryrefslogtreecommitdiff
path: root/backend/plustek-pp_io.c
blob: af3e8c43ea5c828bcdd52b2ea265006b79371e6b (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
/* @file plustekpp-io.c
 * @brief as the name says, here we have all the I/O
 *        functions according to the parallel port hardware
 *
 * based on sources acquired from Plustek Inc.
 * Copyright (C) 1998 Plustek Inc.
 * Copyright (C) 2000-2013 Gerhard Jaeger <gerhard@gjaeger.de>
 *
 * History:
 * - 0.37 - initial version
 *        - added Kevins' suggestions
 * - 0.38 - added Asic 98003 stuff and ioP98ReadWriteTest()
 *        - added IODataRegisterToDAC()
 *        - replaced function IOSPPWrite by IOMoveDataToScanner
 *        - modified ioP98OpenScanPath again and reuse V0.36 stuff again
 *        - added IO functions
 * - 0.39 - added IO functions
 *        - added f97003 stuff from A3I code
 * - 0.40 - no changes
 * - 0.41 - no changes
 * - 0.42 - changed include names
 * - 0.43 - no changes
 * - 0.44 - fix format string issues, as Long types default to int32_t
 *          now
 * .
 * <hr>
 * This file is part of the SANE package.
 *
 * 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, see <https://www.gnu.org/licenses/>.
 *
 * As a special exception, the authors of SANE give permission for
 * additional uses of the libraries contained in this release of SANE.
 *
 * The exception is that, if you link a SANE library with other files
 * to produce an executable, this does not by itself cause the
 * resulting executable to be covered by the GNU General Public
 * License.  Your use of that executable is in no way restricted on
 * account of linking the SANE library code into it.
 *
 * This exception does not, however, invalidate any other reasons why
 * the executable file might be covered by the GNU General Public
 * License.
 *
 * If you submit changes to SANE to the maintainers to be included in
 * a subsequent release, you agree by submitting the changes that
 * those changes may be distributed with this exception intact.
 *
 * If you write modifications of your own for SANE, it is your choice
 * whether to permit this exception to apply to your modifications.
 * If you do not wish that, delete this exception notice.
 * <hr>
 */
#include "plustek-pp_scan.h"

/*************************** some prototypes *********************************/

static Bool fnEPPRead  ( pScanData ps, pUChar pBuffer, ULong ulSize );
static Bool fnSPPRead  ( pScanData ps, pUChar pBuffer, ULong ulSize );
static Bool fnBiDirRead( pScanData ps, pUChar pBuffer, ULong ulSize );

typedef struct {
	pFnReadData func;
	char       *name;
} ioReadFuncDef;

static ioReadFuncDef ioReadFunc[3] = {
	{ fnEPPRead,   "fnEPPRead"   },
	{ fnSPPRead,   "fnSPPRead"   },
	{ fnBiDirRead, "fnBiDirRead" }
};

/*************************** some definitions ********************************/

#define _MEMTEST_SIZE   1280

/*************************** local functions *********************************/

/** we provide some functions to read data from SPP port according to
 * the speed we have detected (ReadWriteTest!!)
 */
static Byte ioDataFromSPPFast( pScanData ps )
{
	Byte bData, tmp;

	/* notify asic we will read the high nibble data from status port */
	if( _FALSE == ps->f97003 ) {
		_OUTB_CTRL( ps, ps->CtrlReadHighNibble );
		_DO_UDELAY( 1 );
	}

	/* read high nibble */
    bData  = _INB_STATUS( ps );
	bData &= 0xf0;

    _OUTB_CTRL( ps, ps->CtrlReadLowNibble );
	_DO_UDELAY( 1 );

	/* read low nibble */
	tmp = _INB_STATUS( ps );

	/* combine with low nibble */
    bData |= (tmp >> 4);

    _OUTB_CTRL( ps, _CTRL_GENSIGNAL );
	_DO_UDELAY( 1 );

    return bData;
}

static Byte ioDataFromSPPMiddle( pScanData ps )
{
	Byte bData, tmp;

	/* notify asic we will read the high nibble data from status port */
	if( _FALSE == ps->f97003 ) {
		_OUTB_CTRL( ps, ps->CtrlReadHighNibble );
		_DO_UDELAY( 1 );
	}

	/* read high nibble */
	_INB_STATUS( ps );
    bData  = _INB_STATUS( ps );
	bData &= 0xf0;

    _OUTB_CTRL( ps, ps->CtrlReadLowNibble );
	_DO_UDELAY( 1 );

	/* read low nibble */
	_INB_STATUS( ps );
	tmp = _INB_STATUS( ps );

	/* combine with low nibble */
    bData |= (tmp >> 4);

    _OUTB_CTRL( ps, _CTRL_GENSIGNAL );
	_DO_UDELAY( 1 );

    return bData;
}

static UChar ioDataFromSPPSlow( pScanData ps )
{
	Byte bData, tmp;

	/* notify asic we will read the high nibble data from status port */
	if( _FALSE == ps->f97003 ) {
		_OUTB_CTRL( ps, ps->CtrlReadHighNibble );
		_DO_UDELAY( 2 );
	}

	/* read high nibble */
	_INB_STATUS( ps );
	_INB_STATUS( ps );
    bData  = _INB_STATUS( ps );
	bData &= 0xf0;

    _OUTB_CTRL( ps, ps->CtrlReadLowNibble );
	_DO_UDELAY( 2 );

	/* read low nibble */
	_INB_STATUS( ps );
	_INB_STATUS( ps );
	tmp = _INB_STATUS( ps );

	/* combine with low nibble */
    bData |= (tmp >> 4);

    _OUTB_CTRL( ps, _CTRL_GENSIGNAL );
	_DO_UDELAY( 2 );

    return bData;
}

static UChar ioDataFromSPPSlowest( pScanData ps )
{
	Byte bData, tmp;

	/* notify asic we will read the high nibble data from status port */
	if( _FALSE == ps->f97003 ) {
		_OUTB_CTRL( ps, ps->CtrlReadHighNibble );
		_DO_UDELAY( 3 );
	}

	/* read high nibble */
	_INB_STATUS( ps );
	_INB_STATUS( ps );
	_INB_STATUS( ps );
    bData  = _INB_STATUS( ps );
	bData &= 0xf0;

    _OUTB_CTRL( ps, ps->CtrlReadLowNibble );
	_DO_UDELAY( 3 );

	/* read low nibble */
	_INB_STATUS( ps );
	_INB_STATUS( ps );
	_INB_STATUS( ps );
	tmp = _INB_STATUS( ps );

	/* combine with low nibble */
    bData |= (tmp >> 4);

    _OUTB_CTRL( ps, _CTRL_GENSIGNAL );
	_DO_UDELAY( 3 );

    return bData;
}

/** Read data from STATUS port. We have to read twice and combine two nibble
 *  data to one byte.
 */
static Bool fnSPPRead( pScanData ps, pUChar pBuffer, ULong ulSize )
{
	switch( ps->IO.delay ) {

		case 0:
			for (; ulSize; ulSize--, pBuffer++)
				*pBuffer = ioDataFromSPPFast( ps );
			break;

		case 1:
			for (; ulSize; ulSize--, pBuffer++)
				*pBuffer = ioDataFromSPPMiddle( ps );
			break;

		case 2:
			for (; ulSize; ulSize--, pBuffer++)
				*pBuffer = ioDataFromSPPSlow( ps );
			break;

		default:
			for (; ulSize; ulSize--, pBuffer++)
				*pBuffer = ioDataFromSPPSlowest( ps );
			break;
	}

    return _TRUE;
}


/** Using buffered I/O to read data from EPP Data Port
 */
static Bool fnEPPRead( pScanData ps, pUChar pBuffer, ULong ulSize )
{
	register ULong i;

	if( _IS_ASIC98(ps->sCaps.AsicID)) {

#ifndef __KERNEL__
		sanei_pp_set_datadir( ps->pardev, SANEI_PP_DATAIN );
#else
		_OUTB_CTRL( ps, (_CTRL_GENSIGNAL + _CTRL_DIRECTION));
		_DO_UDELAY( 1 );
#endif
		for( i = 0; i < ulSize; i++ )
			pBuffer[i] = _INB_EPPDATA( ps );

#ifndef __KERNEL__
		sanei_pp_set_datadir( ps->pardev, SANEI_PP_DATAOUT );
#else
		_OUTB_CTRL( ps, _CTRL_GENSIGNAL );
		_DO_UDELAY( 1 );
#endif
	} else {

		for( i = 0; i < ulSize; i++ )
			pBuffer[i] = _INB_EPPDATA( ps );
	}

	return _TRUE;
}

/**
 */
static Bool fnBiDirRead( pScanData ps, pUChar pBuffer, ULong ulSize )
{
	UChar start, end;

	start = _CTRL_START_BIDIREAD;
	end   = _CTRL_END_BIDIREAD;

#ifndef __KERNEL__
	sanei_pp_set_datadir( ps->pardev, SANEI_PP_DATAIN );

	if( !sanei_pp_uses_directio()) {
		start &= ~_CTRL_DIRECTION;
		end   &= ~_CTRL_DIRECTION;
	}
#else
	if( _IS_ASIC98(ps->sCaps.AsicID)) {
		_OUTB_CTRL( ps, (_CTRL_GENSIGNAL + _CTRL_DIRECTION));
	}
#endif

	switch( ps->IO.delay ) {

		case 0:
		    for( ; ulSize; ulSize--, pBuffer++ ) {
				_OUTB_CTRL( ps, start );
				*pBuffer = _INB_DATA( ps );
				_OUTB_CTRL( ps, end );
			}
			break;

		case 1:
			_DO_UDELAY( 1 );
		    for(; ulSize; ulSize--, pBuffer++ ) {
				_OUTB_CTRL( ps, start );
				_DO_UDELAY( 1 );

				*pBuffer = _INB_DATA( ps );

				_OUTB_CTRL( ps, end );
				_DO_UDELAY( 1 );
			}
			break;

		default:
			_DO_UDELAY( 2 );
		    for(; ulSize; ulSize--, pBuffer++ ) {
				_OUTB_CTRL( ps, start );
				_DO_UDELAY( 2 );

				*pBuffer = _INB_DATA( ps );

				_OUTB_CTRL( ps, end );
				_DO_UDELAY( 2 );
			}
			break;

	}

#ifndef __KERNEL__
	sanei_pp_set_datadir( ps->pardev, SANEI_PP_DATAOUT );
#else
	if( _IS_ASIC98(ps->sCaps.AsicID)) {
		_OUTB_CTRL( ps, _CTRL_GENSIGNAL );
	}
#endif
	return _TRUE;
}

/** as the name says, we switch to SPP mode
 */
static void ioSwitchToSPPMode( pScanData ps )
{
	/* save the control and data port value
	 */
	ps->IO.bOldControlValue = _INB_CTRL( ps );
	ps->IO.bOldDataValue    = _INB_DATA( ps );

	_OUTB_CTRL( ps, _CTRL_GENSIGNAL );	/* 0xc4 */
	_DO_UDELAY( 2 );
}

/** restore the port settings
 */
static void ioRestoreParallelMode( pScanData ps )
{
	_OUTB_CTRL( ps, ps->IO.bOldControlValue & 0x3f );
	_DO_UDELAY( 1 );

	_OUTB_DATA( ps, ps->IO.bOldDataValue );
	_DO_UDELAY( 1 );
}

/** try to connect to scanner (ASIC 9600x and 98001)
 */
_LOC void ioP98001EstablishScannerConnection( pScanData ps, ULong delTime )
{
	_OUTB_DATA( ps, _ID_TO_PRINTER );
    _DO_UDELAY( delTime );

	_OUTB_DATA( ps, _ID1ST );
    _DO_UDELAY( delTime );

	_OUTB_DATA( ps, _ID2ND );
    _DO_UDELAY( delTime );

	_OUTB_DATA( ps, _ID3RD );
    _DO_UDELAY( delTime );

    _OUTB_DATA( ps, _ID4TH );
    _DO_UDELAY( delTime );
}

/** try to connect to scanner (ASIC 98003)
 */
static void ioP98003EstablishScannerConnection( pScanData ps, ULong delTime )
{
	_OUTB_DATA( ps, _ID1ST );
	_DO_UDELAY( delTime );

	_OUTB_DATA( ps, _ID2ND );
	_DO_UDELAY( delTime );

	_OUTB_DATA( ps, _ID3RD );
	_DO_UDELAY( delTime );

	_OUTB_DATA( ps, _ID4TH );
	_DO_UDELAY( delTime );
}

/** switch the printer interface to scanner
 */
static Bool ioP96OpenScanPath( pScanData ps )
{
	if( 0 == ps->IO.bOpenCount ) {

		/* not established */
		ioSwitchToSPPMode( ps );

		/* Scanner command sequence to open scanner path */
		ioP98001EstablishScannerConnection( ps, 5 );
	}
#ifdef DEBUG
	else
		DBG( DBG_IO, "!!!! Path already open (%u)!!!!\n", ps->IO.bOpenCount );
#endif

	ps->IO.bOpenCount++;			/* increment the opened count */

/*
 * CHECK to we really need that !!
 */
	ps->IO.useEPPCmdMode = _FALSE;
	return _TRUE;
}

/** try to connect to scanner
 */
static Bool ioP98OpenScanPath( pScanData ps )
{
    Byte  tmp;
    ULong dw;
	ULong dwTime = 1;

	if( 0 == ps->IO.bOpenCount ) {

		/* not established */
		ioSwitchToSPPMode( ps );

		for( dw = 10; dw; dw-- ) {

			/*
			 * this seems to be necessary...
			 */
 			if( _ASIC_IS_98001 == ps->sCaps.AsicID ) {
				ioP98001EstablishScannerConnection( ps, dw );
#if 0
				ioP98001EstablishScannerConnection( ps, dw );
				ioP98001EstablishScannerConnection( ps, dw );
#endif
			} else {
				ioP98003EstablishScannerConnection( ps, dw );
			}

			_INB_STATUS( ps );
			tmp = _INB_STATUS( ps );

			if( 0x50 == ( tmp & 0xf0 )) {

				ps->IO.bOpenCount = 1;

				if( ps->sCaps.AsicID == IODataFromRegister(ps, ps->RegAsicID)) {
					return _TRUE;
				}
				ps->IO.bOpenCount = 0;
			}

			dwTime++;
		}
		DBG( DBG_IO, "ioP98OpenScanPath() failed!\n" );
		return _FALSE;
	}
#ifdef DEBUG
	else
		DBG( DBG_IO, "!!!! Path already open (%u)!!!!\n", ps->IO.bOpenCount );
#endif

	ps->IO.bOpenCount++;			/* increment the opened count */
	return _TRUE;
}

/** Switch back to printer mode.
 * Restore the printer control/data port value.
 */
static void ioCloseScanPath( pScanData ps )
{
	if( ps->IO.bOpenCount && !(--ps->IO.bOpenCount)) {

#ifdef DEBUG
		ps->IO.bOpenCount = 1;
#endif
		IORegisterToScanner( ps, 0xff );

		/*
		 * back to pass-through printer mode
		 */
		IORegisterToScanner( ps, ps->RegSwitchBus );
#ifdef DEBUG
        ps->IO.bOpenCount = 0;
#endif
		ps->IO.useEPPCmdMode = _FALSE;

		ioRestoreParallelMode( ps );
	}
}

/** check the memory to see that the data-transfers will work.
 * (ASIC 9800x only)
 */
static int ioP98ReadWriteTest( pScanData ps )
{
	UChar  tmp;
	ULong  ul;
	pUChar buffer;
	int	   retval;

	DBG( DBG_LOW, "ioP98ReadWriteTest()\n" );

	/* _MEMTEST_SIZE: Read, _MEMTEST_SIZE:Write */
	buffer = _KALLOC( sizeof(UChar) * _MEMTEST_SIZE*2, GFP_KERNEL );
	if( NULL == buffer )
		return _E_ALLOC;

	/* prepare content */
	for( ul = 0; ul < _MEMTEST_SIZE; ul++ )
	    buffer[ul] = (UChar)ul;

	ps->OpenScanPath(ps);

	/* avoid switching to Lamp0, when previously scanned in transp./neg mode */
	tmp = ps->bLastLampStatus + _SCAN_BYTEMODE;
	IODataToRegister( ps, ps->RegScanControl, tmp );

	IODataToRegister( ps, ps->RegModelControl, (_LED_ACTIVITY | _LED_CONTROL));

	IODataToRegister( ps, ps->RegModeControl, _ModeMappingMem );
	IODataToRegister( ps, ps->RegMemoryLow,  0 );
	IODataToRegister( ps, ps->RegMemoryHigh, 0 );

	/* fill to buffer */
	IOMoveDataToScanner( ps, buffer, _MEMTEST_SIZE );

	IODataToRegister( ps, ps->RegModeControl, _ModeMappingMem );
	IODataToRegister( ps, ps->RegMemoryLow,  0 );
	IODataToRegister( ps, ps->RegMemoryHigh, 0 );
	IODataToRegister( ps, ps->RegWidthPixelsLow,  0 );
	IODataToRegister( ps, ps->RegWidthPixelsHigh, 5 );

	ps->AsicReg.RD_ModeControl = _ModeReadMappingMem;

	if( _ASIC_IS_98001 == ps->sCaps.AsicID )
		ps->CloseScanPath( ps );

	IOReadScannerImageData( ps, buffer + _MEMTEST_SIZE, _MEMTEST_SIZE );

	if( _ASIC_IS_98003 == ps->sCaps.AsicID )
		ps->CloseScanPath( ps );

	/* check the result ! */
	retval = _OK;

	for( ul = 0; ul < _MEMTEST_SIZE; ul++ ) {
		if( buffer[ul] != buffer[ul+_MEMTEST_SIZE] ) {
			DBG( DBG_HIGH, "Error in memory test at pos %u (%u != %u)\n",
				 ul, buffer[ul], buffer[ul+_MEMTEST_SIZE] );
			retval = _E_NO_DEV;
			break;
		}
	}

	_KFREE(buffer);
	return retval;
}

/** Put data to DATA port and trigger hardware through CONTROL port to read it.
 */
static void ioSPPWrite( pScanData ps, pUChar pBuffer, ULong size )
{
	DBG( DBG_IO , "Moving %u bytes to scanner, IODELAY = %u...\n",
					size, ps->IO.delay );
	switch( ps->IO.delay ) {

		case 0:
		    for (; size; size--, pBuffer++) {
				_OUTB_DATA( ps, *pBuffer );
				_OUTB_CTRL( ps, _CTRL_START_DATAWRITE );
				_OUTB_CTRL( ps, _CTRL_END_DATAWRITE );
        	}
			break;

		case 1:
		case 2:
		    for (; size; size--, pBuffer++) {
				_OUTB_DATA( ps, *pBuffer );
				_DO_UDELAY( 1 );
				_OUTB_CTRL( ps, _CTRL_START_DATAWRITE );
				_DO_UDELAY( 1 );
				_OUTB_CTRL( ps, _CTRL_END_DATAWRITE );
				_DO_UDELAY( 2 );
        	}
			break;

		default:
		    for (; size; size--, pBuffer++) {
				_OUTB_DATA( ps, *pBuffer );
				_DO_UDELAY( 1 );
				_OUTB_CTRL( ps, _CTRL_START_DATAWRITE );
				_DO_UDELAY( 2 );
				_OUTB_CTRL( ps, _CTRL_END_DATAWRITE );
				_DO_UDELAY( 3 );
        	}
			break;
	}
	DBG( DBG_IO , "... done.\n" );
}

/** set the scanner to "read" data mode
 */
static void ioEnterReadMode( pScanData ps )
{
	if( ps->IO.portMode != _PORT_SPP ) {

		_DO_UDELAY( 1 );
		IORegisterToScanner( ps, ps->RegEPPEnable );

		if( _IS_ASIC98( ps->sCaps.AsicID ))
			ps->IO.useEPPCmdMode = _TRUE;
	}

	if( _ASIC_IS_98003 == ps->sCaps.AsicID )
		ps->IO.bOldControlValue = _INB_CTRL( ps );

	/* ask ASIC to enter read mode */
	IORegisterToScanner( ps, ps->RegReadDataMode );
}

/************************ exported functions *********************************/

/** here we do some init work
 */
_LOC int IOInitialize( pScanData ps )
{
	DBG( DBG_HIGH, "IOInitialize()\n" );

	if( NULL == ps )
		return _E_NULLPTR;

	if( _IS_ASIC98(ps->sCaps.AsicID)) {

		ps->OpenScanPath  = ioP98OpenScanPath;
		ps->ReadWriteTest = ioP98ReadWriteTest;

	} else if( _IS_ASIC96(ps->sCaps.AsicID)) {

		ps->OpenScanPath = ioP96OpenScanPath;

	} else {

		DBG( DBG_HIGH , "NOT SUPPORTED ASIC !!!\n" );
		return _E_NOSUPP;
	}

	ps->CloseScanPath   = ioCloseScanPath;
	ps->Device.ReadData = ioReadFunc[ps->IO.portMode].func;
	DBG( DBG_HIGH, "* using readfunction >%s<\n",
	                  ioReadFunc[ps->IO.portMode].name );
	return _OK;
}

/** Write specific length buffer to scanner
 * The scan path is already established
 */
_LOC void IOMoveDataToScanner( pScanData ps, pUChar pBuffer, ULong size )
{
#ifdef DEBUG
	if( 0 == ps->IO.bOpenCount )
		DBG( DBG_IO, "IOMoveDataToScanner - no connection!\n" );
#endif

	IORegisterToScanner( ps, ps->RegInitDataFifo );
	IORegisterToScanner( ps, ps->RegWriteDataMode );

	ioSPPWrite( ps, pBuffer, size );
}

/** Calling SITUATION: Scanner path is established.
 * download a scanstate-table
 */
_LOC void IODownloadScanStates( pScanData ps )
{
	TimerDef timer;
#ifdef DEBUG
	if( 0 == ps->IO.bOpenCount )
		DBG( DBG_IO, "IODownloadScanStates - no connection!\n" );
#endif

	IORegisterToScanner( ps, ps->RegScanStateControl );

	ioSPPWrite( ps, ps->a_nbNewAdrPointer, _SCANSTATE_BYTES );

	if( ps->Scan.fRefreshState ) {

		IORegisterToScanner( ps, ps->RegRefreshScanState );

		MiscStartTimer( &timer, (_SECOND/2));
		do {

			if (!( IOGetScanState( ps, _TRUE) & _SCANSTATE_STOP))
				break;
		}
		while( !MiscCheckTimer(&timer));
	}
}

/** Calling SITUATION: Scanner path is established.
 * Write a data to asic
 */
_LOC void IODataToScanner( pScanData ps, Byte bValue )
{
	ULong deltime = 4;

#ifdef DEBUG
	if( 0 == ps->IO.bOpenCount )
        DBG( DBG_IO, "IODataToScanner - no connection!\n" );
#endif

	if( ps->IO.delay < 2 )
		deltime = 2;

    /* output data */
   	_OUTB_DATA( ps, bValue );
	_DO_UDELAY( deltime );

	/* notify asic there is data */
    _OUTB_CTRL( ps, _CTRL_START_DATAWRITE );
	_DO_UDELAY( deltime );

		/* end write cycle */
   	_OUTB_CTRL( ps, _CTRL_END_DATAWRITE );
	_DO_UDELAY( deltime-1 );
}

/** Calling SITUATION: Scanner path is established.
 * Write a data to specific asic's register
 */
_LOC void IODataToRegister( pScanData ps, Byte bReg, Byte bData )
{
#ifdef DEBUG
    if( 0 == ps->IO.bOpenCount )
        DBG( DBG_IO, "IODataToRegister - no connection!\n" );
#endif

	/* specify register */
    IORegisterToScanner( ps, bReg );

	/* then write the content */
	IODataToScanner( ps, bData );
}

/** Calling SITUATION: Scanner path is established.
 * Read the content of specific asic's register
 */
_LOC Byte IODataFromRegister( pScanData ps, Byte bReg )
{
	IORegisterToScanner( ps, bReg );

	if( 0 == ps->IO.delay )
		return ioDataFromSPPFast( ps );
	else if( 1 == ps->IO.delay )
		return ioDataFromSPPMiddle( ps );
	else if( 2 == ps->IO.delay )
		return ioDataFromSPPSlow( ps );
	else
		return ioDataFromSPPSlowest( ps );
}

/** Calling SITUATION: Scanner path is established.
 * Write a register to asic (used for a command without parameter)
 */
_LOC void IORegisterToScanner( pScanData ps, Byte bReg )
{
#ifdef DEBUG
    if( 0 == ps->IO.bOpenCount )
        DBG( DBG_IO, "IORegisterToScanner - no connection!\n" );
#endif

    /*
     * write data to port
     */
	_OUTB_DATA( ps, bReg );

    /*
     * depending on the mode, generate the trigger signals
     */
    if( ps->IO.useEPPCmdMode ) {

		_DO_UDELAY( 5 );

		_OUTB_CTRL( ps, _CTRL_EPPSIGNAL_WRITE);	/* 0xc5 */
		_DO_UDELAY( 5 );

		_OUTB_CTRL( ps, _CTRL_EPPTRIG_REGWRITE);/* 0xcd */
		_DO_UDELAY( 5 );

		_OUTB_CTRL( ps, _CTRL_EPPSIGNAL_WRITE);	/* 0xc5 */
		_DO_UDELAY( 5 );

		_OUTB_CTRL( ps, _CTRL_END_REGWRITE);    /* 0xc4 */

    } else {
		if( ps->IO.delay < 2 ) {

			_DO_UDELAY( 1 );
			_OUTB_CTRL( ps, _CTRL_START_REGWRITE);
			_DO_UDELAY( 1 );
			_OUTB_CTRL( ps, _CTRL_END_REGWRITE);
		} else {

			_DO_UDELAY( 2 );
			_OUTB_CTRL( ps, _CTRL_START_REGWRITE);
			_DO_UDELAY( 2 );
			_OUTB_CTRL( ps, _CTRL_END_REGWRITE);
			_DO_UDELAY( 2 );
		}
    }
}

/** write data to the DAC - ASIC 98001/3 only
 */
_LOC void IODataRegisterToDAC( pScanData ps, Byte bReg, Byte bData )
{
    ULong i;

	IODataToRegister( ps, ps->RegADCAddress, bReg );
    IODataToRegister( ps, ps->RegADCData,    bData );
    IODataToRegister( ps, ps->RegADCSerialOutStr, bData );

    /* TEST: ORG was 1 ms for ASIC 98001 */
    _DO_UDELAY( 12 );

    for( i = 4; i; i-- ) {

    	_OUTB_CTRL( ps, _CTRL_START_DATAWRITE );
        _DO_UDELAY( 5 );
    	_OUTB_CTRL( ps, _CTRL_END_DATAWRITE );
        _DO_UDELAY( 12 );
    }
}

/** Calling SITUATION: Scanner path was not established.
 * Read the content of specific asics' register
 */
_LOC Byte IODataRegisterFromScanner( pScanData ps, Byte bReg )
{
    Byte bData;

    ps->OpenScanPath( ps );
    bData = IODataFromRegister( ps, bReg );
    ps->CloseScanPath( ps );

    return bData;
}

/** Calling SITUATION: Scanner path not established.
 * Write a value of register to asic
 */
_LOC void IOCmdRegisterToScanner( pScanData ps, Byte bReg, Byte bData )
{
    ps->OpenScanPath( ps );
    IODataToRegister( ps, bReg, bData );
    ps->CloseScanPath( ps );
}

/** Calling SITUATION: Scanner path not established.
 * Write a register to asic (used for a command without parameter)
 */
_LOC void IORegisterDirectToScanner( pScanData ps, Byte bReg )
{
    ps->OpenScanPath( ps );				/* establish the connection */
    IORegisterToScanner( ps, bReg );	/* write register to asic	*/
    ps->CloseScanPath( ps );			/* disconnect				*/
}

/** perform a SW reset of ASIC 98003 models
 */
_LOC void IOSoftwareReset( pScanData ps )
{
    if( _ASIC_IS_98003 != ps->sCaps.AsicID )
        return;

    ps->OpenScanPath( ps );

    IODataToRegister( ps, ps->RegTestMode, _SW_TESTMODE );

    ioSwitchToSPPMode( ps );

    _OUTB_DATA( ps, _RESET1ST );
    _DODELAY( 5 );

    _OUTB_DATA( ps, _RESET2ND );
    _DODELAY( 5 );

    _OUTB_DATA( ps, _RESET3RD );
    _DODELAY( 5 );

    _OUTB_DATA( ps, _RESET4TH );
    _DODELAY( 5 );

    ioRestoreParallelMode( ps );

    /* reset test mode register */
    IODataToRegister( ps, ps->RegTestMode, 0 );
    IODataToRegister( ps, ps->RegScanControl, ps->AsicReg.RD_ScanControl );

    ps->CloseScanPath( ps );
}

/** Read specific length data from scanner and the method depends on the
 *  mode defined in registry.
 */
_LOC void IOReadScannerImageData( pScanData ps, pUChar pBuf, ULong size )
{
	if( _ASIC_IS_98003 != ps->sCaps.AsicID )
		ps->OpenScanPath( ps);

	if( _IS_ASIC98( ps->sCaps.AsicID))
		IODataToRegister( ps, ps->RegModeControl, ps->AsicReg.RD_ModeControl );

	/* enter read mode */
	ioEnterReadMode( ps );

	/* call corresponding read proc */
	ps->Device.ReadData( ps, pBuf, size );

	/* Clear EPP/ECP read mode by simply close scanner path and re-open it */
	ps->CloseScanPath( ps );

	if( _ASIC_IS_98003 == ps->sCaps.AsicID )
		ps->OpenScanPath( ps );
}

#ifdef __KERNEL__

/** the wrapper functions to support delayed and non-delayed I/O
 */
_LOC void IOOut( Byte data, UShort port )
{
	DBG( DBG_IOF, "outb(0x%04x, 0x%02x)\n", port, data );
	outb( data, port );
}

_LOC void IOOutDelayed( Byte data, UShort port )
{
	DBG( DBG_IOF, "outb_p(0x%04x, 0x%02x)\n", port, data );
	outb_p( data, port );
}

_LOC Byte IOIn( UShort port )
{
#ifdef DEBUG
	Byte data = inb( port );

	DBG( DBG_IOF, "inb(0x%04x) = 0x%02x\n", port, data );
	return data;
#else
	return inb( port );
#endif
}

_LOC Byte IOInDelayed( UShort port )
{
#ifdef DEBUG
	Byte data = inb_p( port );

	DBG( DBG_IOF, "inb_p(0x%04x) = 0x%02x\n", port, data );
	return data;
#else
	return inb_p( port );
#endif
}
#endif /* guard __KERNEL__ */

/* END PLUSTEK-PP_IO.C ......................................................*/