3 * GPIB driver for FreeBSD.
4 * Version 0.1 (No interrupts, no DMA)
5 * Supports National Instruments AT-GPIB and AT-GPIB/TNT boards.
6 * (AT-GPIB not tested, but it should work)
8 * Written by Fred Cawthorne (fcawth@delphi.umd.edu)
9 * Some sections were based partly on the lpt driver.
10 * (some remnants may remain)
12 * This software is distributed with NO WARRANTIES, not even the implied
13 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * The author grants any other persons or organizations permission to use
16 * or modify this software as long as this message is kept with the software,
17 * all derivative works or modified versions.
19 * $FreeBSD: src/sys/i386/isa/gpib.c,v 1.29 2000/01/29 16:17:32 peter Exp $
22 /*Please read the README file for usage information*/
24 #include <sys/param.h>
25 #include <sys/systm.h>
28 #include <sys/malloc.h>
29 #include <i386/isa/gpibreg.h>
30 #include <i386/isa/gpib.h>
31 #include <i386/isa/isa_device.h>
33 #define MIN(a,b) ((a < b) ? a : b)
35 #define GPIBPRI (PZERO+8)|PCATCH
36 #define SLEEP_MAX 1000
41 static int initgpib(void);
42 static void closegpib(void);
43 static int sendgpibfifo(unsigned char device,char *data,int count);
44 static int sendrawgpibfifo(unsigned char device,char *data,int count);
45 static int readgpibfifo(unsigned char device,char *data,int count);
47 static void showregs(void);
49 static void enableremote(unsigned char device);
50 static void gotolocal(unsigned char device);
51 static void menableremote(unsigned char *device);
52 static void mgotolocal(unsigned char *device);
53 static void mtrigger(unsigned char *device);
54 static void trigger(unsigned char device);
55 static char spoll(unsigned char device);
57 static int gpprobe(struct isa_device *dvp);
58 static int gpattach(struct isa_device *dvp);
60 struct isa_driver gpdriver = {gpprobe, gpattach, "gp"};
62 static d_open_t gpopen;
63 static d_close_t gpclose;
64 static d_write_t gpwrite;
65 static d_ioctl_t gpioctl;
68 static struct cdevsw gp_cdevsw = {
76 /* strategy */ nostrategy,
91 static struct gpib_softc {
92 char *sc_cp; /* current data to send */
93 int sc_count; /* bytes queued in sc_inbuf */
94 int sc_type; /* Type of gpib controller */
95 u_char sc_flags; /* flags (open and internal) */
96 char sc_unit; /* gpib device number */
97 char *sc_inbuf; /* buffer for data */
98 } gpib_sc; /* only support one of these? */
100 static char oldbytes[2];
102 /*This needs to be changed to be a bit more robust*/
104 gpprobe(struct isa_device *dvp)
107 struct gpib_softc *sc = &gpib_sc;
111 cdevsw_add(&gp_cdevsw);
113 gpib_port = dvp->id_iobase;
116 if ((inb(KSR)&0xF7)==0x34) sc->sc_type=3;
117 else if ((inb(KSR)&0xF7)==0x24) sc->sc_type=2;
118 else if ((inb(KSR)&0xF7)==0x14) sc->sc_type=1;
126 * Attach device and print the type of card to the screen.
130 struct isa_device *isdp;
132 struct gpib_softc *sc = &gpib_sc;
134 sc->sc_unit = isdp->id_unit;
136 printf ("gp%d: type AT-GPIB/TNT\n",sc->sc_unit);
138 printf ("gp%d: type AT-GPIB chip NAT4882B\n",sc->sc_unit);
140 printf ("gp%d: type AT-GPIB chip NAT4882A\n",sc->sc_unit);
141 sc->sc_flags |=ATTACHED;
143 make_dev(&gp_cdevsw, 0, 0, 0, 0600, "gp");
149 * New open on device.
151 * More than 1 open is not allowed on the entire device.
152 * i.e. even if gpib5 is open, we can't open another minor device
155 gpopen(dev, flags, fmt, p)
161 struct gpib_softc *sc = &gpib_sc;
167 /* minor number out of limits ? */
172 if (!(sc->sc_flags&ATTACHED)) { /* not attached */
177 if (sc->sc_flags&OPEN) { /* too late .. */
181 /* Have memory for buffer? */
182 sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
183 if (sc->sc_inbuf == 0)
186 if (initgpib()) return(EBUSY);
187 sc->sc_flags |= OPEN;
190 if (unit!=0) { /*Someone is trying to access an actual device*/
191 /*So.. we'll address it to listen*/
196 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
198 outb(CDOR,(unit&31)+32);/*address device to listen*/
202 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
203 outb (CDOR,64); /*Address controller (me) to talk*/
206 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
207 outb(AUXMR,gts); /*Set to Standby (Controller)*/
212 while (!(status&2)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
213 /*Set up the TURBO488 registers*/
214 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
215 outb(CNT0,0); /*NOTE this does not enable DMA to the host computer!!*/
220 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
221 outb(CMDR,0x10); /*RESET fifos*/
222 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
233 gpclose(dev, flags, fmt, p)
239 struct gpib_softc *sc = &gpib_sc;
241 unsigned char status;
244 if (unit!=0) { /*Here we need to send the last character with EOS*/
245 /*and unaddress the listening device*/
250 /*Wait for fifo to become empty*/
252 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
254 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
256 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
258 /*Wait for DONE and STOP*/
259 if (status==EWOULDBLOCK) do {
260 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
262 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
264 /*Shut down TURBO488 */
265 outb(IMR2,0x00); /*DISABLE DMA to turbo488*/
266 outb(CMDR,0x20); /*soft reset turbo488*/
267 outb(CMDR,0x10); /*reset fifos*/
270 /*Send last byte with EOI set*/
271 /*Send second to last byte if there are 2 bytes left*/
272 if (status==EWOULDBLOCK) {
275 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
276 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
278 outb(CDOR,oldbytes[0]); /*Send second to last byte*/
279 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
280 status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
283 outb(AUXMR,seoi); /*Set EOI for the last byte*/
284 outb(AUXMR,0x5E); /*Clear SYNC*/
286 outb(CDOR,oldbytes[0]);
289 outb(CDOR,oldbytes[1]);
291 outb (CDOR,13); /*Send a CR.. we've got trouble*/
292 printf("gpib: Warning: gpclose called with nothing left in buffer\n");
297 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
298 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
301 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
302 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
303 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
306 outb(AUXMR,tca); /* Regain full control of the bus*/
311 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
312 outb(CDOR,63); /*unlisten*/
315 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
316 outb(AUXMR,0x5E); /*Clear SYNC*/
317 outb (CDOR,95);/*untalk*/
320 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
321 /*gotolocal(minor(dev));*/
324 sc->sc_flags = ATTACHED;
325 free(sc->sc_inbuf, M_DEVBUF);
326 sc->sc_inbuf = 0; /* Sanity */
332 * Copy from user's buffer, then write to GPIB device referenced
336 gpwrite(dev, uio, ioflag)
344 while ((gpib_sc.sc_count = MIN(BUFSIZE-1, uio->uio_resid)) > 0) {
345 /* If there were >1 bytes left over, send them */
347 sendrawgpibfifo(minor(dev),oldbytes,2);
349 /*If there was 1 character left, put it at the beginning
352 (gpib_sc.sc_inbuf)[0]=oldbytes[0];
353 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
354 /* get from user-space */
355 uiomove(gpib_sc.sc_inbuf+1, gpib_sc.sc_count, uio);
359 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
360 /* get from user-space */
361 uiomove(gpib_sc.sc_inbuf, gpib_sc.sc_count, uio);
364 /*NOTE we always leave one byte in case this is the last write
365 so close can send EOI with the last byte There may be 2 bytes
366 since we are doing 16 bit transfers.(note the -1 in the count below)*/
367 /*If count<=2 we'll either pick it up on the next write or on close*/
368 if (gpib_sc.sc_count>2) {
369 count = sendrawgpibfifo(minor(dev),gpib_sc.sc_cp,gpib_sc.sc_count-1);
373 oldcount=gpib_sc.sc_count-count; /*Set # of remaining bytes*/
374 gpib_sc.sc_count-=count;
375 gpib_sc.sc_cp+=count; /*point char pointer to remaining bytes*/
377 else oldcount=gpib_sc.sc_count;
378 oldbytes[0]=gpib_sc.sc_cp[0];
380 oldbytes[1]=gpib_sc.sc_cp[1];
384 /* Here is how you would usually access a GPIB device
385 An exception would be a plotter or printer that you can just
386 write to using a minor device = its GPIB address */
389 gpioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
391 struct gpibdata *gd = (struct gpibdata *)data;
397 sendgpibfifo(gd->address,gd->data,*(gd->count));
401 result=readgpibfifo(gd->address,gd->data,*(gd->count));
410 trigger(gd->address);
414 enableremote(gd->address);
418 gotolocal(gd->address);
427 menableremote(gd->data);
431 mgotolocal(gd->data);
435 *(gd->data)=spoll(gd->address);
449 /*Just in case you want a dump of the registers...*/
451 static void showregs() {
452 printf ("NAT4882:\n");
453 printf ("ISR1=%X\t",inb(ISR1));
454 printf ("ISR2=%X\t",inb(ISR2));
455 printf ("SPSR=%X\t",inb(SPSR));
456 printf ("KSR =%X\t",inb(KSR));
457 printf ("ADSR=%X\t",inb(ADSR));
458 printf ("CPTR=%X\t",inb(CPTR));
459 printf ("SASR=%X\t",inb(SASR));
460 printf ("ADR0=%X\t",inb(ADR0));
461 printf ("ISR0=%X\t",inb(ISR0));
462 printf ("ADR1=%X\t",inb(ADR1));
463 printf ("BSR =%X\n",inb(BSR));
465 printf ("Turbo488\n");
466 printf ("STS1=%X ",inb(STS1));
467 printf ("STS2=%X ",inb(STS2));
468 printf ("ISR3=%X ",inb(ISR3));
469 printf ("CNT0=%X ",inb(CNT0));
470 printf ("CNT1=%X ",inb(CNT1));
471 printf ("CNT2=%X ",inb(CNT2));
472 printf ("CNT3=%X ",inb(CNT3));
473 printf ("IMR3=%X ",inb(IMR3));
474 printf ("TIMER=%X\n",inb(TIMER));
479 /*Set up the NAT4882 and TURBO488 registers */
480 /*This will be nonsense to you unless you have a data sheet from
481 National Instruments. They should give you one if you call them*/
493 outb(INTR,0); /* Put interrupt line in tri-state mode??*/
494 outb(AUXMR,chip_reset);
496 outb(IMR1,0x10); /* send interrupt to TURBO488 when END received*/
498 outb(IMR0,0x90); /* Do we want nba here too??? */
508 outb(EOSR,10); /*set EOS message to newline*/
509 /*should I make the default to interpret END as EOS?*/
510 /*It isn't now. The following changes this*/
511 outb(AUXMR,0x80); /*No special EOS handling*/
512 /*outb(AUXMR,0x88) */ /* Transmit END with EOS*/
513 /*outb(AUXMR,0x84) */ /* Set END on EOS received*/
514 /*outb(AUXMR,0x8C) */ /* Do both of the above*/
517 /* outb(AUXMR,hldi); */ /*Perform RFD Holdoff for all data in*/
518 /*Not currently supported*/
522 tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
524 outb(AUXMR,sic_rsc_off);
531 /*This is kind of Brute force.. But it works*/
536 outb(AUXMR,chip_reset);
540 These will also make little sense unless you have a data sheet.
541 Note that the routines with an "m" in the beginning are for
542 accessing multiple devices in one call*/
545 /*This is one thing I could not figure out how to do correctly.
546 I tried to use the auxilary command to enable remote, but it
547 never worked. Here, I bypass everything and write to the BSR
548 to enable the remote line. NOTE that these lines are effectively
549 "OR'ed" with the actual lines, so writing a 1 to the bit in the BSR
550 forces the GPIB line true, no matter what the fancy circuitry of the
551 NAT4882 wants to do with it*/
554 enableremote(unsigned char device)
559 if (status==EWOULDBLOCK) do {
560 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
562 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
564 outb(BSR,1); /*Set REN bit on GPIB*/
565 if (status==EWOULDBLOCK) do {
566 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
568 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
569 outb(CDOR,(device&31)+32); /*address device to listen*/
570 if (status==EWOULDBLOCK) do {
571 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
573 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
574 outb (CDOR,63); /*Unaddress device*/
575 if (status==EWOULDBLOCK) do {
576 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
578 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
581 /*This does not release the REM line on the gpib port, because if it did,
582 all the remote devices would go to local mode. This only sends the
583 gotolocal message to one device. Currently, REM is always held true
584 after enableremote is called, and is reset only on a close of the
588 gotolocal(unsigned char device)
592 if (status==EWOULDBLOCK) do {
593 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
595 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
597 outb(CDOR,(device&31)+32);
599 if (status==EWOULDBLOCK) do {
600 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
602 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
604 outb(AUXMR,0x5E); /*Clear SYNC*/
607 if (status==EWOULDBLOCK) do {
608 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
610 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
613 outb (CDOR,63);/*unaddress device*/
615 if (status==EWOULDBLOCK) do {
616 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
618 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
624 menableremote(unsigned char *device)
626 int status, counter = 0;
629 if (status==EWOULDBLOCK) do {
630 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
632 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
634 outb(BSR,1); /*Set REN bit on GPIB*/
637 if (status==EWOULDBLOCK) do {
638 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
640 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
641 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
644 while (device[counter]<32);
646 if (status==EWOULDBLOCK) do {
647 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
649 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
651 outb (CDOR,63); /*Unaddress device*/
652 if (status==EWOULDBLOCK) do {
653 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
655 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
660 mgotolocal(unsigned char *device)
664 if (device[counter]<32) do {
665 if (status==EWOULDBLOCK) do {
666 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
668 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
669 outb(CDOR,(device[counter]&31)+32);
671 } while (device[counter]<32);
672 if (status==EWOULDBLOCK) do {
673 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
675 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
677 outb(AUXMR,0x5E); /*Clear SYNC*/
681 if (status==EWOULDBLOCK) do {
682 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
684 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
686 outb (CDOR,63);/*unaddress device*/
687 if (status==EWOULDBLOCK) do {
688 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
690 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
694 /*Trigger a device. What happens depends on how the device is
698 trigger(unsigned char device)
703 if (!(inb(ISR2)&0x08)) do {
704 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
706 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
707 outb(CDOR,(device&31)+32); /*address device to listen*/
708 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
709 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
711 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
713 outb (CDOR,8); /*send GET*/
715 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
716 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
718 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
720 outb (CDOR,63);/*unaddress device*/
721 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
722 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
724 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
730 /*Trigger multiple devices by addressing them all to listen, and then
734 mtrigger(unsigned char *device)
735 { int status=EWOULDBLOCK;
739 if (device[counter]<32)
740 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
741 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
743 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
744 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
747 while (device[counter]<32);
748 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
749 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
751 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
752 outb (CDOR,8); /*send GET*/
754 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
755 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
757 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
759 outb (CDOR,63);/*unaddress device*/
760 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
761 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
763 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
769 /*This is not used now, but it should work with NI's 8 bit gpib board
770 since it does not use the TURBO488 registers at all */
773 /*Send data through the TURBO488 FIFOS to a device that is already
774 addressed to listen. This is used by the write call when someone is
775 writing to a printer or plotter, etc... */
776 /*The last byte of each write is held off until either the next
777 write or close, so it can be sent with EOI set*/
780 sendrawgpibfifo(unsigned char device,char *data,int count)
796 /*Wait for fifo to become not full if it is full */
798 if (!(inb(ISR3)&0x08)) do {
799 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
800 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
802 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
804 if((count>1)&&(inb(ISR3)&0x08)){
805 outw(FIFOB,*(unsigned*)(data+counter));
806 /* printf ("gpib: sent:%c,%c\n",data[counter],data[counter+1]);*/
812 while ((count>1)&&(status==EWOULDBLOCK));
813 /*The write routine and close routine must check if there is 1
814 byte left and handle it accordingly*/
817 /*Return the number of bytes written to the device*/
825 sendgpibfifo(unsigned char device,char *data,int count)
832 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
838 if (!(inb(ISR2)&8)) do
839 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
840 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
842 outb(CDOR,(device&31)+32);/*address device to listen*/
844 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
845 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
846 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
847 outb (CDOR,64); /*Address controller (me) to talk*/
849 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
850 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
851 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
853 outb(AUXMR,gts); /*Set to Standby (Controller)*/
863 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
864 outb(CMDR,0x10); /*RESET fifos*/
865 outb(CCRG,seoi); /*program to send EOI at end*/
866 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
869 /*Wait for fifo to become not full if it is full */
871 if (!(inb(ISR3)&0x08)) do {
872 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
873 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
875 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
877 if((count>1)&&(inb(ISR3)&0x08)){
878 /*if(count==2) outb(CFG,15+0x40); *//*send eoi when done*/
879 outw(FIFOB,*(unsigned*)(data+counter));
885 while ((count>2)&&(status==EWOULDBLOCK));
887 if (count==2&&status==EWOULDBLOCK) {
888 /*Wait for fifo to become not full*/
889 if(status==EWOULDBLOCK&&!(inb(ISR3)&0x08)) do {
890 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",SLEEP_MIN);
892 while (!(inb(ISR3)&0x08)&&status==EWOULDBLOCK); /*Fifo is full*/
893 /*outb(CFG,0x40+15);*//*send eoi when done*/
894 outb(FIFOB,data[counter]);
902 /*Wait for fifo to become empty*/
903 if (status==EWOULDBLOCK) do {
904 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
906 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
908 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
910 /*Wait for DONE and STOP*/
911 if (status==EWOULDBLOCK) do {
912 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
914 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
916 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
917 outb(CMDR,0x20); /*soft reset turbo488*/
918 outb(CMDR,0x10); /*reset fifos*/
921 /*Send last byte with EOI set*/
922 /*Here EOI is handled correctly since the string to be sent */
923 /*is actually all sent during the ioctl. (See above)*/
925 if (count==1&&status==EWOULDBLOCK) { /*Count should always=1 here*/
928 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
929 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
931 outb(AUXMR,seoi); /*Set EOI for the last byte*/
932 outb(AUXMR,0x5E); /*Clear SYNC*/
933 outb(CDOR,data[counter]);
939 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
940 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
943 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
944 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
945 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
946 outb(AUXMR,tca); /* Regain full control of the bus*/
949 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
950 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
951 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
953 outb(CDOR,63); /*unlisten*/
956 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
957 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
958 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
961 outb(AUXMR,0x5E); /*Clear SYNC*/
962 outb (CDOR,95);/*untalk*/
963 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
964 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
965 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
975 readgpibfifo(unsigned char device,char *data,int count)
984 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
989 outb(CFG,14+0x60+1); /* Halt on int,read, fifo B first, CCEN TMOE TIM */
990 outb(CMDR,0x10); /*RESET fifos*/
991 outb(CCRG,tcs); /*program to tcs at end*/
992 outb(CMDR,0x08);/*STOP??*/
998 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
999 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1001 outb (CDOR,32); /*Address controller (me) to listen*/
1004 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1005 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1007 outb(CDOR,(device&31)+64);/*address device to talk*/
1011 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1012 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1014 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1019 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
1024 if (!(status1&0x01)&&(status1&0x04)){
1027 *(unsigned*)(data+counter)=inword;
1028 /* printf ("Read:%c,%c\n",data[counter],data[counter+1]);*/
1032 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",4);
1035 while (!(status1&0x01)&&status==EWOULDBLOCK);
1036 if(!(status2 & 0x04)){ /*Only 1 byte came in on last 16 bit transfer*/
1041 outb(CMDR,0x08); /*send STOP*/
1044 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1046 while(!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*wait for DONE and STOP*/
1049 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
1050 outb(CMDR,0x20); /*soft reset turbo488*/
1051 outb(CMDR,0x10); /*reset fifos*/
1054 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1055 while (!(inb(ISR1)&2));*/
1056 outb(AUXMR,tca); /* Regain full control of the bus*/
1060 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1061 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1062 outb(CDOR,63); /*unlisten*/
1065 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1066 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1068 outb(AUXMR,0x5E); /*Clear SYNC*/
1069 outb (CDOR,95);/*untalk*/
1071 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1072 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1080 /* Return the status byte from device */
1082 spoll(unsigned char device)
1084 int status=EWOULDBLOCK;
1085 unsigned int statusbyte;
1087 if (!(inb(ISR2)&8)) do
1088 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1089 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1091 outb(CDOR,(device&31)+64);/*address device to talk*/
1093 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1094 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1095 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1097 outb (CDOR,32); /*Address controller (me) to listen*/
1099 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1100 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1101 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1103 outb (CDOR,0x18); /*Send SPE (serial poll enable)*/
1104 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1105 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1106 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1108 /*wait for bus to be synced*/
1109 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1110 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1111 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1113 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1115 if (!(inb(ISR1)&1)&&status==EWOULDBLOCK) do
1116 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1117 while (!(inb(ISR1)&1)&&status==EWOULDBLOCK);
1119 outb(AUXMR,tcs); /* Take control after next read*/
1120 statusbyte=inb(DIR);
1122 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1123 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1124 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1126 outb(CDOR,0x19); /*SPD (serial poll disable)*/
1128 /*wait for bus to be synced*/
1129 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1130 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1131 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1134 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1135 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1136 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1138 outb(CDOR,95); /*untalk*/
1140 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1141 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1142 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1144 outb (CDOR,63);/*unlisten*/
1145 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1146 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1147 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1149 /*wait for bus to be synced*/
1150 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1151 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1152 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);