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
|
//---------------------------------------------------------------------
//
// T338.C
//
// Trantor T338 Logic Module. Contains functions to access the T338
// adapter.
//
// Revisions:
// 02-01-93 KJB First.
// 02-23-93 KJB Reorganized, supports dataunderrun with long delay
// for under run on large xfers. Can we fix this?
// 03-11-93 JAP Changed retcode equates to reflect new names.
// 03-11-93 KJB Changed to use N5380Enable/DisableDmaRead/Write
// routines.
// 03-12-93 KJB Now supports polling thru CardInterrupt and
// StartCommandInterrupt/FinishCommandInterrupt.
// 03-19-93 JAP Implemented condition build FAR and NEAR pointers
// 03-22-93 KJB Added support for scatter gather: T338DoIo.
// 03-24-93 KJB Fixed SetScsiMode so it does not reset the n5380!
// 05-14-93 KJB Added CardParseCommandString for card specific
// standard string parsing across platforms.
// Changed CardCheckAdapter to accept an
// Initialization info from command line, ie
// force bi-directional ports, etc.
// All functions that used to take an PBASE_REGISTER
// parameter now take PWORKSPACE. CardCheckAdapter
// takes the both a PINIT and a PWORKSPACE parameters.
// 05-14-93 KJB Remove all WINNT specific #ifdef i386 references.
// 05-14-93 KJB Removed P3CDoIo, it did not work for scatter gather.
// 05-16-93 KJB Fixed parameter bugs introduced while doing the
// PWORKSPACE changes.
// 05-17-93 KJB Fixed compiler warnings.
//
//---------------------------------------------------------------------
#include CARDTXXX_H
// Local Functions
VOID T338PutControl(PADAPTER_INFO g,UCHAR mode, UCHAR reg);
VOID T338SetPrinterMode(PADAPTER_INFO g, UCHAR data, UCHAR control);
VOID T338SetScsiMode(PADAPTER_INFO g, PUCHAR data, PUCHAR control);
//
// T338PutControl
//
// Puts a control byte to the T338 style adapter. This sets the mode
// to IOR or IOW and the address byte of the N5380 register.
//
VOID T338PutControl(PADAPTER_INFO g,UCHAR mode, UCHAR reg)
{
UCHAR tmp;
// the following bits are active low: IOW, IOR, MR
tmp = reg | (mode ^ (T338_MR | T338_IOW | T338_IOR));
// put the control byte on the data lines
ParallelPortPut(g->BaseIoAddress,PARALLEL_DATA,tmp);
// assert slc to indicate byte is there
ParallelPortPut(g->BaseIoAddress,PARALLEL_CONTROL,P_SLC);
// clear slc
ParallelPortPut(g->BaseIoAddress,PARALLEL_CONTROL,0);
}
//
// T338SetPrinterMode
//
// This routine sets the T338 to printer pass through mode. This is the
// default mode and should be set after the brief use of scsi mode.
//
VOID T338SetPrinterMode(PADAPTER_INFO g, UCHAR data, UCHAR control)
{
UCHAR tmp;
// do we have to disable interrupts?
// negate all control signals...
T338PutControl(g,0,0);
// restore data register
ParallelPortPut(g->BaseIoAddress,PARALLEL_DATA,data);
// leave p_init negated (1)
tmp = control | P_INIT;
ParallelPortPut(g->BaseIoAddress,PARALLEL_CONTROL,tmp);
}
//
// T338SetScsiMode
//
// This routine sets the T338 into scsi mode. Now the parallel port can
// be used to send commands the the n5380. This mode should be set only
// briefly during when the scsi command is being executed.
//
VOID T338SetScsiMode(PADAPTER_INFO g, PUCHAR data, PUCHAR control)
{
UCHAR tmp;
// save parallel data
ParallelPortGet(g->BaseIoAddress,PARALLEL_DATA,data);
// zero data register
// note: the signals IOW,IOR,MR are active low, so assert them..
ParallelPortPut(g->BaseIoAddress,PARALLEL_DATA,
T338_MR | T338_IOW | T338_IOR);
// save parallel control
ParallelPortGet(g->BaseIoAddress,PARALLEL_CONTROL,control);
*control = *control & (P_BUFEN ^ 0xff);
// clear p_init and set p_slc
tmp = (*control & (P_INIT ^ 0xff) ) | P_SLC;
ParallelPortPut(g->BaseIoAddress,PARALLEL_CONTROL,tmp);
// clear p_init and set p_slc
tmp = (*control & (P_INIT ^ 0xff) ) | P_SLC;
ParallelPortPut(g->BaseIoAddress,PARALLEL_CONTROL,tmp);
// clear slc, leave p_init asserted (0)
tmp = tmp & (P_SLC ^ 0xff);
ParallelPortPut(g->BaseIoAddress,PARALLEL_CONTROL,tmp);
}
//
// T338CheckAdapter
//
// This routine is used to sense the presense of the T338 adapter out
// on the Parallel port. It will only detect the adapter if a device
// is providing termination power.
//
BOOLEAN T338CheckAdapter(PADAPTER_INFO g)
{
UCHAR data;
UCHAR control;
BOOLEAN rval;
// set scsi mode
T338SetScsiMode(g,&data,&control);
// reset the 5380
T338PutControl(g,T338_MR,0);
T338PutControl(g,0,0);
// check to see if a 5380 is there
rval = N5380CheckAdapter(g);
// set parallel port for use by printer
T338SetPrinterMode(g,data,control);
return rval;
}
//
// T338DoCommand
//
// Called by the main loop to start a scsi command. This functions is the
// main entry point for all cards. It returns an SRB status code as defined
// in ..\..\inc\srb.h. A status code of RET_STATUS_PENDING means that the
// request has been sent to the controller and an interrupt is needed to
// finish the request. When this interrupt occurs CardFinishCommandInterrupt
// will be called.
//
USHORT T338DoCommand(PTSRB t)
{
USHORT rval;
UCHAR data;
UCHAR control;
PADAPTER_INFO g = t->pWorkspace;
// put the parallel adapter into scsi mode
T338SetScsiMode(g, &data, &control);
// execute the complete command now, without interrupts
rval = ScsiDoCommand(t);
// put the parallel adapter back to parallel mode
T338SetPrinterMode(g, data, control);
return rval;
}
//
// T338StartCommandInterrupt
//
// This routines allow the driver to be polled by checking its
// CardInterrupt by for example using the timer interrupt, since
// the T338 does not support interrupts on its own.
//
//
USHORT T338StartCommandInterrupt(PTSRB t)
{
USHORT rval;
UCHAR data;
UCHAR control;
PADAPTER_INFO g = t->pWorkspace;
// put the parallel adapter into scsi mode
T338SetScsiMode(g, &data, &control);
// execute the complete command now, without interrupts
rval = ScsiStartCommandInterrupt(t);
// put the parallel adapter back to parallel mode
T338SetPrinterMode(g, data, control);
return rval;
}
//
// T338FinishCommandInterrupt
//
// This routines allow the driver to be polled by checking its
// CardInterrupt by for example using the timer interrupt, since
// the T338 does not support interrupts on its own.
//
//
USHORT T338FinishCommandInterrupt(PTSRB t)
{
USHORT rval;
UCHAR data;
UCHAR control;
PADAPTER_INFO g = t->pWorkspace;
// put the T338 into ScsiMode
T338SetScsiMode(g, &data, &control);
// execute the complete command now, without interrupts
rval = ScsiFinishCommandInterrupt(t);
// put the parallel adapter back to parallel mode
T338SetPrinterMode(g, data, control);
return rval;
}
//
// T338StartCommandInterrupt
//
// This routines allow the driver to be polled by checking its
// CardInterrupt by for example using the timer interrupt, since
// the T338 does not support interrupts on its own.
//
BOOLEAN T338Interrupt(PADAPTER_INFO g)
{
BOOLEAN rval;
UCHAR data;
UCHAR control;
// put the parallel adapter into scsi mode
T338SetScsiMode(g, &data, &control);
rval = N5380Interrupt(g);
// put the parallel adapter back to parallel mode
T338SetPrinterMode(g, data, control);
return rval;
}
//
//
// T338ResetBus
//
// Resets the SCSI Bus
//
VOID T338ResetBus(PADAPTER_INFO g)
{
UCHAR data;
UCHAR control;
// put the parallel adapter into scsi mode
T338SetScsiMode(g, &data, &control);
// execute the complete command now, without interrupts
N5380ResetBus(g);
// put the parallel adapter back to parallel mode
T338SetPrinterMode(g, data, control);
}
//
// T338WriteBytesFast
//
// This routine is used by the ScsiFnc routines to write bytes to the scsi
// bus quickly. The ScsiFnc routines don't know how to do this quickly for
// a particular card, so they call this. This routine can be mapped to the
// slower ScsiWriteBytesSlow routine for small transferrs or if this routine
// is not supported.
//
USHORT T338WriteBytesFast (PADAPTER_INFO g, PUCHAR pbytes,
ULONG len, PULONG pActualLen, UCHAR phase)
{
USHORT rval = 0;
// use slow mode for odd xfers (inquiry type commands) & audio
if (len % 512) {
return ScsiWriteBytesSlow (g, pbytes, len,
pActualLen, phase);
}
// start dma mode
N5380EnableDmaWrite (g);
// put the T338 into write dma mode
T338PutControl (g,T338_IOW,0);
{
ULONG xfer_count = len;
PBASE_REGISTER baseIoAddress = g->BaseIoAddress;
_asm {
push esi
push ds
#ifdef MODE_32BIT
mov edx,baseIoAddress
mov esi,pbytes
mov ecx,len
#else
mov dx, word ptr baseIoAddress
mov si, word ptr pbytes
mov cx, word ptr len
mov ds, word ptr pbytes+2
#endif // MODE_32BIT
add dx,2 // dx points to control reg
get_bytes:
dec dx // dx points to status register
in al,dx
test al,P_BUSY
jnz big_wait
ready:
dec dx // dx points to parallel data reg
mov al,[esi]
out dx,al
// assert DACK
add dx,2 // dx points to control reg
mov al, P_AFX
out dx,al
// deassert DACK
mov al,0
out dx,al
inc esi
dec ecx
jnz get_bytes
}
goto done_asm;
_asm {
big_wait:
in al,dx
test al,P_BUSY
jz ready
in al,dx
test al,P_BUSY
jz ready
in al,dx
test al,P_BUSY
jz ready
in al,dx
test al,P_BUSY
jz ready
// wait for a while before going to a bigger timeout
push ecx
push ebx
mov ebx,TIMEOUT_READWRITE_LOOP
loop0:
mov ecx,0x10000
loop1:
in al,dx
test al,P_BUSY
jz ready1
in al,dx
test al,P_BUSY
jz ready1
dec ecx
jnz loop1
dec ebx
jnz loop0
pop ebx
pop ecx
jmp short error
ready1:
pop ebx
pop ecx
jmp short ready
error:
mov rval,RET_STATUS_TIMEOUT
done_asm:
pop ds
pop esi
#ifdef MODE_32BIT
mov xfer_count,ecx
#else
mov word ptr xfer_count,ecx
#endif
}
// compute actual xfer len
*pActualLen = len - xfer_count;
}
// clear the dma bit of 5380
N5380DisableDmaWrite (g);
// if data underrun, return the under/over run error message
if (rval) {
UCHAR tmp;
// phase mismatch means data under/over run
N5380GetPhase (g,&tmp);
if (tmp == PHASE_STATUS) {
rval = RET_STATUS_DATA_OVERRUN;
}
}
return rval;
}
//
// T338ReadBytesFast
//
// This routine is used by the ScsiFnc routines to write bytes to the scsi
// bus quickly. The ScsiFnc routines don't know how to do this quickly for
// a particular card, so they call this. This routine can be mapped to the
// slower ScsiReadBytesSlow routine for small transferrs or if this routine
// is not supported.
//
#pragma optimize("",off)
USHORT T338ReadBytesFast (PADAPTER_INFO g, PUCHAR pbytes,
ULONG len, PULONG pActualLen, UCHAR phase)
{
USHORT rval = 0;
// use slow mode for small xfers (inquiry type commands) and audio
if (len % 512) {
return ScsiReadBytesSlow (g, pbytes, len,
pActualLen, phase);
}
// start dma read
N5380EnableDmaRead (g);
// put the t338 into read mode
T338PutControl (g,T338_IOR,0);
// to be fast, for 386 machines, this must be coded in assembly
// for inline assembly, we don't have to save eax-edx registers
{
ULONG xfer_count = len;
PBASE_REGISTER baseIoAddress = g->BaseIoAddress;
_asm {
push esi
push ds
#ifdef MODE_32BIT
mov edx, baseIoAddress
mov esi,pbytes
mov ecx,len
#else
mov dx, word ptr baseIoAddress
mov si, word ptr pbytes
mov cx, word ptr len
mov ds, word ptr pbytes+2
#endif // MODE_32BIT
inc dx // dx points to status register
get_bytes:
in al,dx
test al,P_BUSY
jnz big_wait
ready:
// assert DACK, the P_AFX bit
inc dx // dx points to control register
mov al,P_AFX
out dx,al
// select high nibble
sub dx,2 // dx points to data register
mov al,0x80
out dx,al
// get high nibble
inc dx // dx points to status register
in al,dx
mov ah,al
// select lower nibble
dec dx // dx points to data register
xor al,al
out dx,al
// calculate high nibble
shl ah,1
and ah,0f0h
// get lower nibble
inc dx // dx points to status register
in al,dx
mov bh,al
// deassert DACK, clear P_AFX
inc dx // dx points to control register
xor al,al
out dx,al
dec dx // dx points to status register
// compute low nibble and the whole byte
shr bh,1
shr bh,1
shr bh,1
and bh,0fh
or ah,bh
mov al,ah
// store data and loop
mov [esi],al
inc esi
dec ecx
jnz get_bytes
}
goto done_asm;
_asm {
big_wait:
in al,dx
test al,P_BUSY
jz ready
in al,dx
test al,P_BUSY
jz ready
in al,dx
test al,P_BUSY
jz ready
in al,dx
test al,P_BUSY
jz ready
// wait for a while before going to a bigger timeout
push ecx
push ebx
mov ebx,TIMEOUT_READWRITE_LOOP
loop0:
mov ecx,0x10000
loop1:
in al,dx
test al,P_BUSY
jz ready1
in al,dx
test al,P_BUSY
jz ready1
dec ecx
jnz loop1
dec ebx
jnz loop0
pop ebx
pop ecx
jmp short error
ready1:
pop ebx
pop ecx
}
goto ready;
_asm {
error:
mov rval,RET_STATUS_TIMEOUT
done_asm:
pop ds
pop esi
#ifdef MODE_32BIT
mov xfer_count,ecx
#else
mov word ptr xfer_count,ecx
#endif
}
// compute actual xfer len
*pActualLen = len - xfer_count;
}
// zero control register, disable read dma mode
ParallelPortPut (g->BaseIoAddress,PARALLEL_CONTROL,0);
// clear the dma read mode
N5380DisableDmaRead (g);
// if data underrun, return the under/over run error message
if (rval) {
UCHAR tmp;
// phase mismatch means data under/over run
N5380GetPhase (g,&tmp);
if (tmp == PHASE_STATUS) {
rval = RET_STATUS_DATA_OVERRUN;
}
}
return rval;
}
#pragma optimize("",on)
//
// N5380PortPut
//
// This routine is used by the N5380.C module to write byte to a 5380
// controller. This allows the module to be card independent. Other
// modules that assume a N5380 may also use this function.
//
VOID N5380PortPut (PADAPTER_INFO g,UCHAR reg,UCHAR byte)
{
// set T338 logic into data write mode
T338PutControl (g, T338_IOW, reg);
// write the byte
ParallelPortPut (g->BaseIoAddress, PARALLEL_DATA, byte);
// toggle the strobe line
ParallelPortPut (g->BaseIoAddress, PARALLEL_CONTROL, P_STB);
ParallelPortPut (g->BaseIoAddress, PARALLEL_CONTROL, 0);
// clear data write mode
T338PutControl (g, 0, 0);
}
//
// N5380PortGet
//
// This routine is used by the N5380.C module to get a byte from a 5380
// controller. This allows the module to be card independent. Other
// modules that assume a N5380 may also use this function.
//
VOID N5380PortGet (PADAPTER_INFO g, UCHAR reg, PUCHAR byte)
{
UCHAR tmp,tmp1;
// set T338 logic to read mode
T338PutControl (g, T338_IOR, reg);
// select high nibble
ParallelPortPut (g->BaseIoAddress, PARALLEL_DATA, 0x80);
// assert stb
ParallelPortPut (g->BaseIoAddress, PARALLEL_CONTROL, P_STB);
// read high nibble
ParallelPortGet (g->BaseIoAddress, PARALLEL_STATUS, &tmp);
// compute high nibble
tmp = (tmp << 1) & 0xf0;
// select low nibble
ParallelPortPut (g->BaseIoAddress, PARALLEL_DATA, 0x00);
// read low nibble
ParallelPortGet (g->BaseIoAddress, PARALLEL_STATUS, &tmp1);
// compute low nibble
tmp1 = (tmp1 >> 3) & 0x0f;
// compute and return byte
*byte = tmp1 | tmp;
// clear slc
ParallelPortPut (g->BaseIoAddress, PARALLEL_CONTROL, 0);
// clear data read mode
T338PutControl (g, 0, 0);
}
|