1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
|
/*
* GPIB driver for FreeBSD.
* Version 0.1 (No interrupts, no DMA)
* Supports National Instruments AT-GPIB and AT-GPIB/TNT boards.
* (AT-GPIB not tested, but it should work)
*
* Written by Fred Cawthorne (fcawth@delphi.umd.edu)
* Some sections were based partly on the lpt driver.
* (some remnants may remain)
*
* This software is distributed with NO WARRANTIES, not even the implied
* warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* The author grants any other persons or organizations permission to use
* or modify this software as long as this message is kept with the software,
* all derivative works or modified versions.
*
*
*/
/*Please read the README file for usage information*/
#include "gp.h"
#if NGP > 0
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#ifdef DEVFS
#include <sys/devfsext.h>
#endif /*DEVFS*/
#include <i386/isa/gpibreg.h>
#include <i386/isa/gpib.h>
#include <i386/isa/isa_device.h>
#define MIN(a,b) ((a < b) ? a : b)
#define GPIBPRI (PZERO+8)|PCATCH
#define SLEEP_MAX 1000
#define SLEEP_MIN 4
static int initgpib(void);
static void closegpib(void);
static int sendgpibfifo(unsigned char device,char *data,int count);
static int sendrawgpibfifo(unsigned char device,char *data,int count);
static int readgpibfifo(unsigned char device,char *data,int count);
#if 0
static void showregs(void);
#endif
static void enableremote(unsigned char device);
static void gotolocal(unsigned char device);
static void menableremote(unsigned char *device);
static void mgotolocal(unsigned char *device);
static void mtrigger(unsigned char *device);
static void trigger(unsigned char device);
static char spoll(unsigned char device);
static int gpprobe(struct isa_device *dvp);
static int gpattach(struct isa_device *dvp);
struct isa_driver gpdriver = {gpprobe, gpattach, "gp"};
static d_open_t gpopen;
static d_close_t gpclose;
static d_write_t gpwrite;
static d_ioctl_t gpioctl;
#define CDEV_MAJOR 44
static struct cdevsw gp_cdevsw =
{ gpopen, gpclose, noread, gpwrite, /*44*/
gpioctl, nostop, nullreset, nodevtotty,/* GPIB */
seltrue, nommap, NULL, "gp", NULL, -1 };
#define BUFSIZE 1024
#define ATTACHED 0x08
#define OPEN 0x04
#define INIT 0x02
static struct gpib_softc {
char *sc_cp; /* current data to send */
int sc_count; /* bytes queued in sc_inbuf */
int sc_type; /* Type of gpib controller */
u_char sc_flags; /* flags (open and internal) */
char sc_unit; /* gpib device number */
char *sc_inbuf; /* buffer for data */
#ifdef DEVFS
void *devfs_token; /* handle for devfs entry */
#endif
} gpib_sc; /* only support one of these? */
static int oldcount;
static char oldbytes[2];
/*Probe routine*/
/*This needs to be changed to be a bit more robust*/
static int
gpprobe(struct isa_device *dvp)
{
int status;
struct gpib_softc *sc = &gpib_sc;
gpib_port = dvp->id_iobase;
status=1;
sc->sc_type=3;
if ((inb(KSR)&0xF7)==0x34) sc->sc_type=3;
else if ((inb(KSR)&0xF7)==0x24) sc->sc_type=2;
else if ((inb(KSR)&0xF7)==0x14) sc->sc_type=1;
else status=0;
return (status);
}
/*
* gpattach()
* Attach device and print the type of card to the screen.
*/
static int
gpattach(isdp)
struct isa_device *isdp;
{
struct gpib_softc *sc = &gpib_sc;
sc->sc_unit = isdp->id_unit;
if (sc->sc_type==3)
printf ("gp%d: type AT-GPIB/TNT\n",sc->sc_unit);
if (sc->sc_type==2)
printf ("gp%d: type AT-GPIB chip NAT4882B\n",sc->sc_unit);
if (sc->sc_type==1)
printf ("gp%d: type AT-GPIB chip NAT4882A\n",sc->sc_unit);
sc->sc_flags |=ATTACHED;
#ifdef DEVFS
sc->devfs_token =
devfs_add_devswf(&gp_cdevsw, 0, DV_CHR, 0, 0, 0600, "gp");
#endif
return (1);
}
/*
* gpopen()
* New open on device.
*
* More than 1 open is not allowed on the entire device.
* i.e. even if gpib5 is open, we can't open another minor device
*/
static int
gpopen(dev, flags, fmt, p)
dev_t dev;
int flags;
int fmt;
struct proc *p;
{
struct gpib_softc *sc = &gpib_sc;
u_char unit;
int status;
unit= minor(dev);
/* minor number out of limits ? */
if (unit >= 32)
return (ENXIO);
/* Attached ? */
if (!(sc->sc_flags&ATTACHED)) { /* not attached */
return(ENXIO);
}
/* Already open */
if (sc->sc_flags&OPEN) { /* too late .. */
return(EBUSY);
}
/* Have memory for buffer? */
sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
if (sc->sc_inbuf == 0)
return(ENOMEM);
if (initgpib()) return(EBUSY);
sc->sc_flags |= OPEN;
sc->sc_count = 0;
oldcount=0;
if (unit!=0) { /*Someone is trying to access an actual device*/
/*So.. we'll address it to listen*/
enableremote(unit);
do {
status=inb(ISR2);
}
while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
outb(CDOR,(unit&31)+32);/*address device to listen*/
do
status=inb(ISR2);
while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
outb (CDOR,64); /*Address controller (me) to talk*/
do status=inb(ISR2);
while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
outb(AUXMR,gts); /*Set to Standby (Controller)*/
do
status=inb(ISR1);
while (!(status&2)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
/*Set up the TURBO488 registers*/
outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
outb(CNT0,0); /*NOTE this does not enable DMA to the host computer!!*/
outb(CNT1,0);
outb(CNT2,0);
outb(CNT3,0);
outb(CMDR,0x20);
outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
outb(CMDR,0x10); /*RESET fifos*/
outb(CMDR,0x04); /*Tell TURBO488 to GO*/
}
return(0);
}
/*
* gpclose()
* Close gpib device.
*/
static int
gpclose(dev, flags, fmt, p)
dev_t dev;
int flags;
int fmt;
struct proc *p;
{
struct gpib_softc *sc = &gpib_sc;
unsigned char unit;
unsigned char status;
unit=minor(dev);
if (unit!=0) { /*Here we need to send the last character with EOS*/
/*and unaddress the listening device*/
status=EWOULDBLOCK;
/*Wait for fifo to become empty*/
do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
outb(CMDR,0x08); /*Issue STOP to TURBO488*/
/*Wait for DONE and STOP*/
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
/*Shut down TURBO488 */
outb(IMR2,0x00); /*DISABLE DMA to turbo488*/
outb(CMDR,0x20); /*soft reset turbo488*/
outb(CMDR,0x10); /*reset fifos*/
/*Send last byte with EOI set*/
/*Send second to last byte if there are 2 bytes left*/
if (status==EWOULDBLOCK) {
do
if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
if (oldcount==2){
outb(CDOR,oldbytes[0]); /*Send second to last byte*/
while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
}
outb(AUXMR,seoi); /*Set EOI for the last byte*/
outb(AUXMR,0x5E); /*Clear SYNC*/
if (oldcount==1)
outb(CDOR,oldbytes[0]);
else
if (oldcount==2)
outb(CDOR,oldbytes[1]);
else {
outb (CDOR,13); /*Send a CR.. we've got trouble*/
printf("gpib: Warning: gpclose called with nothing left in buffer\n");
}
}
do
if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
outb(AUXMR,tca); /* Regain full control of the bus*/
do
status=inb(ISR2);
while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
outb(CDOR,63); /*unlisten*/
do
status=inb(ISR2);
while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
outb(AUXMR,0x5E); /*Clear SYNC*/
outb (CDOR,95);/*untalk*/
do
status=inb(ISR2);
while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
/*gotolocal(minor(dev));*/
}
closegpib();
sc->sc_flags = ATTACHED;
free(sc->sc_inbuf, M_DEVBUF);
sc->sc_inbuf = 0; /* Sanity */
return(0);
}
/*
* gpwrite()
* Copy from user's buffer, then write to GPIB device referenced
* by minor(dev).
*/
static int
gpwrite(dev, uio, ioflag)
dev_t dev;
struct uio *uio;
int ioflag;
{
int err,count;
/* main loop */
while ((gpib_sc.sc_count = MIN(BUFSIZE-1, uio->uio_resid)) > 0) {
/* If there were >1 bytes left over, send them */
if (oldcount==2)
sendrawgpibfifo(minor(dev),oldbytes,2);
/*If there was 1 character left, put it at the beginning
of the new buffer*/
if (oldcount==1){
(gpib_sc.sc_inbuf)[0]=oldbytes[0];
gpib_sc.sc_cp = gpib_sc.sc_inbuf;
/* get from user-space */
uiomove(gpib_sc.sc_inbuf+1, gpib_sc.sc_count, uio);
gpib_sc.sc_count++;
}
else {
gpib_sc.sc_cp = gpib_sc.sc_inbuf;
/* get from user-space */
uiomove(gpib_sc.sc_inbuf, gpib_sc.sc_count, uio);
}
/*NOTE we always leave one byte in case this is the last write
so close can send EOI with the last byte There may be 2 bytes
since we are doing 16 bit transfers.(note the -1 in the count below)*/
/*If count<=2 we'll either pick it up on the next write or on close*/
if (gpib_sc.sc_count>2) {
count = sendrawgpibfifo(minor(dev),gpib_sc.sc_cp,gpib_sc.sc_count-1);
err=!count;
if (err)
return(1);
oldcount=gpib_sc.sc_count-count; /*Set # of remaining bytes*/
gpib_sc.sc_count-=count;
gpib_sc.sc_cp+=count; /*point char pointer to remaining bytes*/
}
else oldcount=gpib_sc.sc_count;
oldbytes[0]=gpib_sc.sc_cp[0];
if (oldcount==2)
oldbytes[1]=gpib_sc.sc_cp[1];
}
return(0);
}
/* Here is how you would usually access a GPIB device
An exception would be a plotter or printer that you can just
write to using a minor device = its GPIB address */
static int
gpioctl(dev_t dev, int cmd, caddr_t data, int flags, struct proc *p)
{
struct gpibdata *gd = (struct gpibdata *)data;
int error,result;
error = 0;
switch (cmd) {
case GPIBWRITE:
sendgpibfifo(gd->address,gd->data,*(gd->count));
error=0;
break;
case GPIBREAD:
result=readgpibfifo(gd->address,gd->data,*(gd->count));
*(gd->count)=result;
error=0;
break;
case GPIBINIT:
initgpib();
error=0;
break;
case GPIBTRIGGER:
trigger(gd->address);
error=0;
break;
case GPIBREMOTE:
enableremote(gd->address);
error=0;
break;
case GPIBLOCAL:
gotolocal(gd->address);
error=0;
break;
case GPIBMTRIGGER:
mtrigger(gd->data);
error=0;
break;
case GPIBMREMOTE:
menableremote(gd->data);
error=0;
break;
case GPIBMLOCAL:
mgotolocal(gd->data);
error=0;
break;
case GPIBSPOLL:
*(gd->data)=spoll(gd->address);
error=0;
break;
default:
error = ENODEV;
}
return(error);
}
#if 0
/*Just in case you want a dump of the registers...*/
static void showregs() {
printf ("NAT4882:\n");
printf ("ISR1=%X\t",inb(ISR1));
printf ("ISR2=%X\t",inb(ISR2));
printf ("SPSR=%X\t",inb(SPSR));
printf ("KSR =%X\t",inb(KSR));
printf ("ADSR=%X\t",inb(ADSR));
printf ("CPTR=%X\t",inb(CPTR));
printf ("SASR=%X\t",inb(SASR));
printf ("ADR0=%X\t",inb(ADR0));
printf ("ISR0=%X\t",inb(ISR0));
printf ("ADR1=%X\t",inb(ADR1));
printf ("BSR =%X\n",inb(BSR));
printf ("Turbo488\n");
printf ("STS1=%X ",inb(STS1));
printf ("STS2=%X ",inb(STS2));
printf ("ISR3=%X ",inb(ISR3));
printf ("CNT0=%X ",inb(CNT0));
printf ("CNT1=%X ",inb(CNT1));
printf ("CNT2=%X ",inb(CNT2));
printf ("CNT3=%X ",inb(CNT3));
printf ("IMR3=%X ",inb(IMR3));
printf ("TIMER=%X\n",inb(TIMER));
}
#endif
/*Set up the NAT4882 and TURBO488 registers */
/*This will be nonsense to you unless you have a data sheet from
National Instruments. They should give you one if you call them*/
static int
initgpib() {
outb(CMDR,0x20);
outb(CFG,0x16);
outb(IMR3,0);
outb(CMDR,0x10);
outb(CNT0,0);
outb(CNT1,0);
outb(CNT2,0);
outb(CNT3,0);
outb(INTR,0); /* Put interrupt line in tri-state mode??*/
outb(AUXMR,chip_reset);
outb(IMR1,0x10); /* send interrupt to TURBO488 when END received*/
outb(IMR2,0);
outb(IMR0,0x90); /* Do we want nba here too??? */
outb(ADMR,1);
outb(ADR,0);
outb(ADR,128);
outb(AUXMR,0xE9);
outb(AUXMR,0x49);
outb(AUXMR,0x70);
outb(AUXMR,0xD0);
outb(AUXMR,0xA0);
outb(EOSR,10); /*set EOS message to newline*/
/*should I make the default to interpret END as EOS?*/
/*It isn't now. The following changes this*/
outb(AUXMR,0x80); /*No special EOS handling*/
/*outb(AUXMR,0x88) */ /* Transmit END with EOS*/
/*outb(AUXMR,0x84) */ /* Set END on EOS received*/
/*outb(AUXMR,0x8C) */ /* Do both of the above*/
/* outb(AUXMR,hldi); */ /*Perform RFD Holdoff for all data in*/
/*Not currently supported*/
outb(AUXMR,pon);
outb(AUXMR,sic_rsc);
tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
outb(AUXMR,sic_rsc_off);
return(0);
}
/*This is kind of Brute force.. But it works*/
static void
closegpib()
{
outb(AUXMR,chip_reset);
}
/*GPIB ROUTINES:
These will also make little sense unless you have a data sheet.
Note that the routines with an "m" in the beginning are for
accessing multiple devices in one call*/
/*This is one thing I could not figure out how to do correctly.
I tried to use the auxilary command to enable remote, but it
never worked. Here, I bypass everything and write to the BSR
to enable the remote line. NOTE that these lines are effectively
"OR'ed" with the actual lines, so writing a 1 to the bit in the BSR
forces the GPIB line true, no matter what the fancy circuitry of the
NAT4882 wants to do with it*/
static void
enableremote(unsigned char device)
{
int status;
status=EWOULDBLOCK;
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(BSR,1); /*Set REN bit on GPIB*/
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(CDOR,(device&31)+32); /*address device to listen*/
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb (CDOR,63); /*Unaddress device*/
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
}
/*This does not release the REM line on the gpib port, because if it did,
all the remote devices would go to local mode. This only sends the
gotolocal message to one device. Currently, REM is always held true
after enableremote is called, and is reset only on a close of the
gpib device */
static void
gotolocal(unsigned char device)
{ int status;
status=EWOULDBLOCK;
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(CDOR,(device&31)+32);
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(AUXMR,0x5E); /*Clear SYNC*/
outb (CDOR,1);
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(AUXMR,0x5E);
outb (CDOR,63);/*unaddress device*/
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
}
static void
menableremote(unsigned char *device)
{
int status, counter = 0;
status=EWOULDBLOCK;
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(BSR,1); /*Set REN bit on GPIB*/
do
{
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
counter++;
}
while (device[counter]<32);
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb (CDOR,63); /*Unaddress device*/
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
}
static void
mgotolocal(unsigned char *device)
{ int status;
int counter=0;
status=EWOULDBLOCK;
if (device[counter]<32) do {
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(CDOR,(device[counter]&31)+32);
counter++;
} while (device[counter]<32);
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(AUXMR,0x5E); /*Clear SYNC*/
outb (CDOR,1);
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(AUXMR,0x5E);
outb (CDOR,63);/*unaddress device*/
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
}
/*Trigger a device. What happens depends on how the device is
configured. */
static void
trigger(unsigned char device)
{ int status;
status=EWOULDBLOCK;
if (device<32) {
if (!(inb(ISR2)&0x08)) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(CDOR,(device&31)+32); /*address device to listen*/
if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb (CDOR,8); /*send GET*/
if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb (AUXMR,0x5E);
outb (CDOR,63);/*unaddress device*/
if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
}
}
/*Trigger multiple devices by addressing them all to listen, and then
sending GET*/
static void
mtrigger(unsigned char *device)
{ int status=EWOULDBLOCK;
int counter=0;
if(device[0]<32){
do {
if (device[counter]<32)
if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
counter++;
}
while (device[counter]<32);
if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb (CDOR,8); /*send GET*/
if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
outb (AUXMR,0x5E);
outb (CDOR,63);/*unaddress device*/
if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
}
}
/*This is not used now, but it should work with NI's 8 bit gpib board
since it does not use the TURBO488 registers at all */
/*Send data through the TURBO488 FIFOS to a device that is already
addressed to listen. This is used by the write call when someone is
writing to a printer or plotter, etc... */
/*The last byte of each write is held off until either the next
write or close, so it can be sent with EOI set*/
static int
sendrawgpibfifo(unsigned char device,char *data,int count)
{
int status;
int counter;
int fifopos;
int sleeptime;
sleeptime=SLEEP_MIN;
counter=0;
fifopos=0;
status=EWOULDBLOCK;
do {
/*Wait for fifo to become not full if it is full */
sleeptime=SLEEP_MIN;
if (!(inb(ISR3)&0x08)) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
}
while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
if((count>1)&&(inb(ISR3)&0x08)){
outw(FIFOB,*(unsigned*)(data+counter));
/* printf ("gpib: sent:%c,%c\n",data[counter],data[counter+1]);*/
counter+=2;
count-=2;
}
}
while ((count>1)&&(status==EWOULDBLOCK));
/*The write routine and close routine must check if there is 1
byte left and handle it accordingly*/
/*Return the number of bytes written to the device*/
return(counter);
}
static int
sendgpibfifo(unsigned char device,char *data,int count)
{
int status;
int counter;
int fifopos;
int sleeptime;
outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
outb(CNT0,0);
outb(CNT1,0);
outb(CNT2,0);
outb(CNT3,0);
status=EWOULDBLOCK;
if (!(inb(ISR2)&8)) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(CDOR,(device&31)+32);/*address device to listen*/
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb (CDOR,64); /*Address controller (me) to talk*/
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(AUXMR,gts); /*Set to Standby (Controller)*/
fifopos=0;
sleeptime=SLEEP_MIN;
counter=0;
fifopos=0;
outb(CMDR,0x20);
outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
outb(CMDR,0x10); /*RESET fifos*/
outb(CCRG,seoi); /*program to send EOI at end*/
outb(CMDR,0x04); /*Tell TURBO488 to GO*/
status=EWOULDBLOCK;
do {
/*Wait for fifo to become not full if it is full */
sleeptime=SLEEP_MIN;
if (!(inb(ISR3)&0x08)) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
}
while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
if((count>1)&&(inb(ISR3)&0x08)){
/*if(count==2) outb(CFG,15+0x40); *//*send eoi when done*/
outw(FIFOB,*(unsigned*)(data+counter));
counter+=2;
count-=2;
}
}
while ((count>2)&&(status==EWOULDBLOCK));
if (count==2&&status==EWOULDBLOCK) {
/*Wait for fifo to become not full*/
if(status==EWOULDBLOCK&&!(inb(ISR3)&0x08)) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",SLEEP_MIN);
}
while (!(inb(ISR3)&0x08)&&status==EWOULDBLOCK); /*Fifo is full*/
/*outb(CFG,0x40+15);*//*send eoi when done*/
outb(FIFOB,data[counter]);
counter++;
count--;
}
/*outb(CMDR,0x04);*/
/*Wait for fifo to become empty*/
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
outb(CMDR,0x08); /*Issue STOP to TURBO488*/
/*Wait for DONE and STOP*/
if (status==EWOULDBLOCK) do {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
outb(CMDR,0x20); /*soft reset turbo488*/
outb(CMDR,0x10); /*reset fifos*/
/*Send last byte with EOI set*/
/*Here EOI is handled correctly since the string to be sent */
/*is actually all sent during the ioctl. (See above)*/
if (count==1&&status==EWOULDBLOCK) { /*Count should always=1 here*/
do
if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
outb(AUXMR,seoi); /*Set EOI for the last byte*/
outb(AUXMR,0x5E); /*Clear SYNC*/
outb(CDOR,data[counter]);
counter++;
count--;
}
do
if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
outb(AUXMR,tca); /* Regain full control of the bus*/
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(CDOR,63); /*unlisten*/
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(AUXMR,0x5E); /*Clear SYNC*/
outb (CDOR,95);/*untalk*/
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
return(counter);
}
static int
readgpibfifo(unsigned char device,char *data,int count)
{
int status;
int status2 = 0;
int status1;
int counter;
int fifopos;
unsigned inword;
outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
/*outb(IMR3,0x1F);
outb(INTR,1); */
outb(CMDR,0x20);
outb(CFG,14+0x60+1); /* Halt on int,read, fifo B first, CCEN TMOE TIM */
outb(CMDR,0x10); /*RESET fifos*/
outb(CCRG,tcs); /*program to tcs at end*/
outb(CMDR,0x08);/*STOP??*/
status=EWOULDBLOCK;
do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb (CDOR,32); /*Address controller (me) to listen*/
do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(CDOR,(device&31)+64);/*address device to talk*/
do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(AUXMR,gts); /*Set to Standby (Controller)*/
counter=0;
fifopos=0;
outb(CMDR,0x04); /*Tell TURBO488 to GO*/
do {
status1=inb(ISR3);
if (!(status1&0x01)&&(status1&0x04)){
status2=inb(STS2);
inword=inw(FIFOB);
*(unsigned*)(data+counter)=inword;
/* printf ("Read:%c,%c\n",data[counter],data[counter+1]);*/
counter+=2;
}
else {
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",4);
}
}
while (!(status1&0x01)&&status==EWOULDBLOCK);
if(!(status2 & 0x04)){ /*Only 1 byte came in on last 16 bit transfer*/
data[counter-1]=0;
counter--; }
else
data[counter]=0;
outb(CMDR,0x08); /*send STOP*/
do{
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
}
while(!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*wait for DONE and STOP*/
outb(AUXMR,0x55);
outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
outb(CMDR,0x20); /*soft reset turbo488*/
outb(CMDR,0x10); /*reset fifos*/
/* do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR1)&2));*/
outb(AUXMR,tca); /* Regain full control of the bus*/
do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(CDOR,63); /*unlisten*/
do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(AUXMR,0x5E); /*Clear SYNC*/
outb (CDOR,95);/*untalk*/
do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
return(counter);
}
/* Return the status byte from device */
static char
spoll(unsigned char device)
{
int status=EWOULDBLOCK;
unsigned int statusbyte;
if (!(inb(ISR2)&8)) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(CDOR,(device&31)+64);/*address device to talk*/
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb (CDOR,32); /*Address controller (me) to listen*/
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(AUXMR,0x5E);
outb (CDOR,0x18); /*Send SPE (serial poll enable)*/
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
/*wait for bus to be synced*/
if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
outb(AUXMR,gts); /*Set to Standby (Controller)*/
if (!(inb(ISR1)&1)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR1)&1)&&status==EWOULDBLOCK);
outb(AUXMR,0x5E);
outb(AUXMR,tcs); /* Take control after next read*/
statusbyte=inb(DIR);
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(CDOR,0x19); /*SPD (serial poll disable)*/
/*wait for bus to be synced*/
if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(CDOR,95); /*untalk*/
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
outb(AUXMR,0x5E);
outb (CDOR,63);/*unlisten*/
if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
/*wait for bus to be synced*/
if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
return(statusbyte);
}
static gp_devsw_installed = 0;
static void
gp_drvinit(void *unused)
{
dev_t dev;
if( ! gp_devsw_installed ) {
dev = makedev(CDEV_MAJOR, 0);
cdevsw_add(&dev,&gp_cdevsw, NULL);
gp_devsw_installed = 1;
}
}
SYSINIT(gpdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,gp_drvinit,NULL)
#endif /* NGPIB > 0 */
|