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 $
20 * $DragonFly: src/sys/dev/misc/gpib/gpib.c,v 1.4 2003/07/21 05:50:40 dillon Exp $
23 /*Please read the README file for usage information*/
25 #include <sys/param.h>
26 #include <sys/systm.h>
29 #include <sys/malloc.h>
30 #include <i386/isa/gpibreg.h>
31 #include <i386/isa/gpib.h>
32 #include <i386/isa/isa_device.h>
34 #define MIN(a,b) ((a < b) ? a : b)
36 #define GPIBPRI PCATCH
37 #define SLEEP_MAX 1000
42 static int initgpib(void);
43 static void closegpib(void);
44 static int sendgpibfifo(unsigned char device,char *data,int count);
45 static int sendrawgpibfifo(unsigned char device,char *data,int count);
46 static int readgpibfifo(unsigned char device,char *data,int count);
48 static void showregs(void);
50 static void enableremote(unsigned char device);
51 static void gotolocal(unsigned char device);
52 static void menableremote(unsigned char *device);
53 static void mgotolocal(unsigned char *device);
54 static void mtrigger(unsigned char *device);
55 static void trigger(unsigned char device);
56 static char spoll(unsigned char device);
58 static int gpprobe(struct isa_device *dvp);
59 static int gpattach(struct isa_device *dvp);
61 struct isa_driver gpdriver = {gpprobe, gpattach, "gp"};
63 static d_open_t gpopen;
64 static d_close_t gpclose;
65 static d_write_t gpwrite;
66 static d_ioctl_t gpioctl;
69 static struct cdevsw gp_cdevsw = {
83 /* strategy */ nostrategy,
94 static struct gpib_softc {
95 char *sc_cp; /* current data to send */
96 int sc_count; /* bytes queued in sc_inbuf */
97 int sc_type; /* Type of gpib controller */
98 u_char sc_flags; /* flags (open and internal) */
99 char sc_unit; /* gpib device number */
100 char *sc_inbuf; /* buffer for data */
101 } gpib_sc; /* only support one of these? */
103 static char oldbytes[2];
105 /*This needs to be changed to be a bit more robust*/
107 gpprobe(struct isa_device *dvp)
110 struct gpib_softc *sc = &gpib_sc;
114 cdevsw_add(&gp_cdevsw);
116 gpib_port = dvp->id_iobase;
119 if ((inb(KSR)&0xF7)==0x34) sc->sc_type=3;
120 else if ((inb(KSR)&0xF7)==0x24) sc->sc_type=2;
121 else if ((inb(KSR)&0xF7)==0x14) sc->sc_type=1;
129 * Attach device and print the type of card to the screen.
133 struct isa_device *isdp;
135 struct gpib_softc *sc = &gpib_sc;
137 sc->sc_unit = isdp->id_unit;
139 printf ("gp%d: type AT-GPIB/TNT\n",sc->sc_unit);
141 printf ("gp%d: type AT-GPIB chip NAT4882B\n",sc->sc_unit);
143 printf ("gp%d: type AT-GPIB chip NAT4882A\n",sc->sc_unit);
144 sc->sc_flags |=ATTACHED;
146 make_dev(&gp_cdevsw, 0, 0, 0, 0600, "gp");
152 * New open on device.
154 * More than 1 open is not allowed on the entire device.
155 * i.e. even if gpib5 is open, we can't open another minor device
158 gpopen(dev, flags, fmt, p)
164 struct gpib_softc *sc = &gpib_sc;
170 /* minor number out of limits ? */
175 if (!(sc->sc_flags&ATTACHED)) { /* not attached */
180 if (sc->sc_flags&OPEN) { /* too late .. */
184 /* Have memory for buffer? */
185 sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
186 if (sc->sc_inbuf == 0)
189 if (initgpib()) return(EBUSY);
190 sc->sc_flags |= OPEN;
193 if (unit!=0) { /*Someone is trying to access an actual device*/
194 /*So.. we'll address it to listen*/
199 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
201 outb(CDOR,(unit&31)+32);/*address device to listen*/
205 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
206 outb (CDOR,64); /*Address controller (me) to talk*/
209 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
210 outb(AUXMR,gts); /*Set to Standby (Controller)*/
215 while (!(status&2)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
216 /*Set up the TURBO488 registers*/
217 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
218 outb(CNT0,0); /*NOTE this does not enable DMA to the host computer!!*/
223 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
224 outb(CMDR,0x10); /*RESET fifos*/
225 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
236 gpclose(dev, flags, fmt, p)
242 struct gpib_softc *sc = &gpib_sc;
244 unsigned char status;
247 if (unit!=0) { /*Here we need to send the last character with EOS*/
248 /*and unaddress the listening device*/
253 /*Wait for fifo to become empty*/
255 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
257 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
259 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
261 /*Wait for DONE and STOP*/
262 if (status==EWOULDBLOCK) do {
263 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
265 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
267 /*Shut down TURBO488 */
268 outb(IMR2,0x00); /*DISABLE DMA to turbo488*/
269 outb(CMDR,0x20); /*soft reset turbo488*/
270 outb(CMDR,0x10); /*reset fifos*/
273 /*Send last byte with EOI set*/
274 /*Send second to last byte if there are 2 bytes left*/
275 if (status==EWOULDBLOCK) {
278 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
279 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
281 outb(CDOR,oldbytes[0]); /*Send second to last byte*/
282 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
283 status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
286 outb(AUXMR,seoi); /*Set EOI for the last byte*/
287 outb(AUXMR,0x5E); /*Clear SYNC*/
289 outb(CDOR,oldbytes[0]);
292 outb(CDOR,oldbytes[1]);
294 outb (CDOR,13); /*Send a CR.. we've got trouble*/
295 printf("gpib: Warning: gpclose called with nothing left in buffer\n");
300 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
301 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
304 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
305 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
306 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
309 outb(AUXMR,tca); /* Regain full control of the bus*/
314 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
315 outb(CDOR,63); /*unlisten*/
318 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
319 outb(AUXMR,0x5E); /*Clear SYNC*/
320 outb (CDOR,95);/*untalk*/
323 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
324 /*gotolocal(minor(dev));*/
327 sc->sc_flags = ATTACHED;
328 free(sc->sc_inbuf, M_DEVBUF);
329 sc->sc_inbuf = 0; /* Sanity */
335 * Copy from user's buffer, then write to GPIB device referenced
339 gpwrite(dev, uio, ioflag)
347 while ((gpib_sc.sc_count = MIN(BUFSIZE-1, uio->uio_resid)) > 0) {
348 /* If there were >1 bytes left over, send them */
350 sendrawgpibfifo(minor(dev),oldbytes,2);
352 /*If there was 1 character left, put it at the beginning
355 (gpib_sc.sc_inbuf)[0]=oldbytes[0];
356 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
357 /* get from user-space */
358 uiomove(gpib_sc.sc_inbuf+1, gpib_sc.sc_count, uio);
362 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
363 /* get from user-space */
364 uiomove(gpib_sc.sc_inbuf, gpib_sc.sc_count, uio);
367 /*NOTE we always leave one byte in case this is the last write
368 so close can send EOI with the last byte There may be 2 bytes
369 since we are doing 16 bit transfers.(note the -1 in the count below)*/
370 /*If count<=2 we'll either pick it up on the next write or on close*/
371 if (gpib_sc.sc_count>2) {
372 count = sendrawgpibfifo(minor(dev),gpib_sc.sc_cp,gpib_sc.sc_count-1);
376 oldcount=gpib_sc.sc_count-count; /*Set # of remaining bytes*/
377 gpib_sc.sc_count-=count;
378 gpib_sc.sc_cp+=count; /*point char pointer to remaining bytes*/
380 else oldcount=gpib_sc.sc_count;
381 oldbytes[0]=gpib_sc.sc_cp[0];
383 oldbytes[1]=gpib_sc.sc_cp[1];
387 /* Here is how you would usually access a GPIB device
388 An exception would be a plotter or printer that you can just
389 write to using a minor device = its GPIB address */
392 gpioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
394 struct gpibdata *gd = (struct gpibdata *)data;
400 sendgpibfifo(gd->address,gd->data,*(gd->count));
404 result=readgpibfifo(gd->address,gd->data,*(gd->count));
413 trigger(gd->address);
417 enableremote(gd->address);
421 gotolocal(gd->address);
430 menableremote(gd->data);
434 mgotolocal(gd->data);
438 *(gd->data)=spoll(gd->address);
452 /*Just in case you want a dump of the registers...*/
454 static void showregs() {
455 printf ("NAT4882:\n");
456 printf ("ISR1=%X\t",inb(ISR1));
457 printf ("ISR2=%X\t",inb(ISR2));
458 printf ("SPSR=%X\t",inb(SPSR));
459 printf ("KSR =%X\t",inb(KSR));
460 printf ("ADSR=%X\t",inb(ADSR));
461 printf ("CPTR=%X\t",inb(CPTR));
462 printf ("SASR=%X\t",inb(SASR));
463 printf ("ADR0=%X\t",inb(ADR0));
464 printf ("ISR0=%X\t",inb(ISR0));
465 printf ("ADR1=%X\t",inb(ADR1));
466 printf ("BSR =%X\n",inb(BSR));
468 printf ("Turbo488\n");
469 printf ("STS1=%X ",inb(STS1));
470 printf ("STS2=%X ",inb(STS2));
471 printf ("ISR3=%X ",inb(ISR3));
472 printf ("CNT0=%X ",inb(CNT0));
473 printf ("CNT1=%X ",inb(CNT1));
474 printf ("CNT2=%X ",inb(CNT2));
475 printf ("CNT3=%X ",inb(CNT3));
476 printf ("IMR3=%X ",inb(IMR3));
477 printf ("TIMER=%X\n",inb(TIMER));
482 /*Set up the NAT4882 and TURBO488 registers */
483 /*This will be nonsense to you unless you have a data sheet from
484 National Instruments. They should give you one if you call them*/
496 outb(INTR,0); /* Put interrupt line in tri-state mode??*/
497 outb(AUXMR,chip_reset);
499 outb(IMR1,0x10); /* send interrupt to TURBO488 when END received*/
501 outb(IMR0,0x90); /* Do we want nba here too??? */
511 outb(EOSR,10); /*set EOS message to newline*/
512 /*should I make the default to interpret END as EOS?*/
513 /*It isn't now. The following changes this*/
514 outb(AUXMR,0x80); /*No special EOS handling*/
515 /*outb(AUXMR,0x88) */ /* Transmit END with EOS*/
516 /*outb(AUXMR,0x84) */ /* Set END on EOS received*/
517 /*outb(AUXMR,0x8C) */ /* Do both of the above*/
520 /* outb(AUXMR,hldi); */ /*Perform RFD Holdoff for all data in*/
521 /*Not currently supported*/
525 tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
527 outb(AUXMR,sic_rsc_off);
534 /*This is kind of Brute force.. But it works*/
539 outb(AUXMR,chip_reset);
543 These will also make little sense unless you have a data sheet.
544 Note that the routines with an "m" in the beginning are for
545 accessing multiple devices in one call*/
548 /*This is one thing I could not figure out how to do correctly.
549 I tried to use the auxilary command to enable remote, but it
550 never worked. Here, I bypass everything and write to the BSR
551 to enable the remote line. NOTE that these lines are effectively
552 "OR'ed" with the actual lines, so writing a 1 to the bit in the BSR
553 forces the GPIB line true, no matter what the fancy circuitry of the
554 NAT4882 wants to do with it*/
557 enableremote(unsigned char device)
562 if (status==EWOULDBLOCK) do {
563 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
565 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
567 outb(BSR,1); /*Set REN bit on GPIB*/
568 if (status==EWOULDBLOCK) do {
569 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
571 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
572 outb(CDOR,(device&31)+32); /*address device to listen*/
573 if (status==EWOULDBLOCK) do {
574 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
576 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
577 outb (CDOR,63); /*Unaddress device*/
578 if (status==EWOULDBLOCK) do {
579 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
581 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
584 /*This does not release the REM line on the gpib port, because if it did,
585 all the remote devices would go to local mode. This only sends the
586 gotolocal message to one device. Currently, REM is always held true
587 after enableremote is called, and is reset only on a close of the
591 gotolocal(unsigned char device)
595 if (status==EWOULDBLOCK) do {
596 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
598 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
600 outb(CDOR,(device&31)+32);
602 if (status==EWOULDBLOCK) do {
603 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
605 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
607 outb(AUXMR,0x5E); /*Clear SYNC*/
610 if (status==EWOULDBLOCK) do {
611 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
613 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
616 outb (CDOR,63);/*unaddress device*/
618 if (status==EWOULDBLOCK) do {
619 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
621 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
627 menableremote(unsigned char *device)
629 int status, counter = 0;
632 if (status==EWOULDBLOCK) do {
633 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
635 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
637 outb(BSR,1); /*Set REN bit on GPIB*/
640 if (status==EWOULDBLOCK) do {
641 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
643 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
644 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
647 while (device[counter]<32);
649 if (status==EWOULDBLOCK) do {
650 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
652 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
654 outb (CDOR,63); /*Unaddress device*/
655 if (status==EWOULDBLOCK) do {
656 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
658 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
663 mgotolocal(unsigned char *device)
667 if (device[counter]<32) do {
668 if (status==EWOULDBLOCK) do {
669 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
671 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
672 outb(CDOR,(device[counter]&31)+32);
674 } while (device[counter]<32);
675 if (status==EWOULDBLOCK) do {
676 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
678 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
680 outb(AUXMR,0x5E); /*Clear SYNC*/
684 if (status==EWOULDBLOCK) do {
685 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
687 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
689 outb (CDOR,63);/*unaddress device*/
690 if (status==EWOULDBLOCK) do {
691 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
693 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
697 /*Trigger a device. What happens depends on how the device is
701 trigger(unsigned char device)
706 if (!(inb(ISR2)&0x08)) do {
707 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
709 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
710 outb(CDOR,(device&31)+32); /*address device to listen*/
711 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
712 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
714 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
716 outb (CDOR,8); /*send GET*/
718 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
719 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
721 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
723 outb (CDOR,63);/*unaddress device*/
724 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
725 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
727 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
733 /*Trigger multiple devices by addressing them all to listen, and then
737 mtrigger(unsigned char *device)
738 { int status=EWOULDBLOCK;
742 if (device[counter]<32)
743 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
744 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
746 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
747 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
750 while (device[counter]<32);
751 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
752 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
754 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
755 outb (CDOR,8); /*send GET*/
757 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
758 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
760 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
762 outb (CDOR,63);/*unaddress device*/
763 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
764 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
766 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
772 /*This is not used now, but it should work with NI's 8 bit gpib board
773 since it does not use the TURBO488 registers at all */
776 /*Send data through the TURBO488 FIFOS to a device that is already
777 addressed to listen. This is used by the write call when someone is
778 writing to a printer or plotter, etc... */
779 /*The last byte of each write is held off until either the next
780 write or close, so it can be sent with EOI set*/
783 sendrawgpibfifo(unsigned char device,char *data,int count)
799 /*Wait for fifo to become not full if it is full */
801 if (!(inb(ISR3)&0x08)) do {
802 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
803 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
805 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
807 if((count>1)&&(inb(ISR3)&0x08)){
808 outw(FIFOB,*(unsigned*)(data+counter));
809 /* printf ("gpib: sent:%c,%c\n",data[counter],data[counter+1]);*/
815 while ((count>1)&&(status==EWOULDBLOCK));
816 /*The write routine and close routine must check if there is 1
817 byte left and handle it accordingly*/
820 /*Return the number of bytes written to the device*/
828 sendgpibfifo(unsigned char device,char *data,int count)
835 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
841 if (!(inb(ISR2)&8)) do
842 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
843 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
845 outb(CDOR,(device&31)+32);/*address device to listen*/
847 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
848 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
849 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
850 outb (CDOR,64); /*Address controller (me) to talk*/
852 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
853 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
854 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
856 outb(AUXMR,gts); /*Set to Standby (Controller)*/
866 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
867 outb(CMDR,0x10); /*RESET fifos*/
868 outb(CCRG,seoi); /*program to send EOI at end*/
869 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
872 /*Wait for fifo to become not full if it is full */
874 if (!(inb(ISR3)&0x08)) do {
875 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
876 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
878 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
880 if((count>1)&&(inb(ISR3)&0x08)){
881 /*if(count==2) outb(CFG,15+0x40); *//*send eoi when done*/
882 outw(FIFOB,*(unsigned*)(data+counter));
888 while ((count>2)&&(status==EWOULDBLOCK));
890 if (count==2&&status==EWOULDBLOCK) {
891 /*Wait for fifo to become not full*/
892 if(status==EWOULDBLOCK&&!(inb(ISR3)&0x08)) do {
893 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",SLEEP_MIN);
895 while (!(inb(ISR3)&0x08)&&status==EWOULDBLOCK); /*Fifo is full*/
896 /*outb(CFG,0x40+15);*//*send eoi when done*/
897 outb(FIFOB,data[counter]);
905 /*Wait for fifo to become empty*/
906 if (status==EWOULDBLOCK) do {
907 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
909 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
911 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
913 /*Wait for DONE and STOP*/
914 if (status==EWOULDBLOCK) do {
915 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
917 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
919 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
920 outb(CMDR,0x20); /*soft reset turbo488*/
921 outb(CMDR,0x10); /*reset fifos*/
924 /*Send last byte with EOI set*/
925 /*Here EOI is handled correctly since the string to be sent */
926 /*is actually all sent during the ioctl. (See above)*/
928 if (count==1&&status==EWOULDBLOCK) { /*Count should always=1 here*/
931 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
932 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
934 outb(AUXMR,seoi); /*Set EOI for the last byte*/
935 outb(AUXMR,0x5E); /*Clear SYNC*/
936 outb(CDOR,data[counter]);
942 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
943 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
946 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
947 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
948 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
949 outb(AUXMR,tca); /* Regain full control of the bus*/
952 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
953 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
954 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
956 outb(CDOR,63); /*unlisten*/
959 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
960 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
961 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
964 outb(AUXMR,0x5E); /*Clear SYNC*/
965 outb (CDOR,95);/*untalk*/
966 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
967 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
968 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
978 readgpibfifo(unsigned char device,char *data,int count)
987 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
992 outb(CFG,14+0x60+1); /* Halt on int,read, fifo B first, CCEN TMOE TIM */
993 outb(CMDR,0x10); /*RESET fifos*/
994 outb(CCRG,tcs); /*program to tcs at end*/
995 outb(CMDR,0x08);/*STOP??*/
1001 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1002 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1004 outb (CDOR,32); /*Address controller (me) to listen*/
1007 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1008 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1010 outb(CDOR,(device&31)+64);/*address device to talk*/
1014 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1015 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1017 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1022 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
1027 if (!(status1&0x01)&&(status1&0x04)){
1030 *(unsigned*)(data+counter)=inword;
1031 /* printf ("Read:%c,%c\n",data[counter],data[counter+1]);*/
1035 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",4);
1038 while (!(status1&0x01)&&status==EWOULDBLOCK);
1039 if(!(status2 & 0x04)){ /*Only 1 byte came in on last 16 bit transfer*/
1044 outb(CMDR,0x08); /*send STOP*/
1047 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1049 while(!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*wait for DONE and STOP*/
1052 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
1053 outb(CMDR,0x20); /*soft reset turbo488*/
1054 outb(CMDR,0x10); /*reset fifos*/
1057 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1058 while (!(inb(ISR1)&2));*/
1059 outb(AUXMR,tca); /* Regain full control of the bus*/
1063 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1064 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1065 outb(CDOR,63); /*unlisten*/
1068 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1069 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1071 outb(AUXMR,0x5E); /*Clear SYNC*/
1072 outb (CDOR,95);/*untalk*/
1074 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1075 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1083 /* Return the status byte from device */
1085 spoll(unsigned char device)
1087 int status=EWOULDBLOCK;
1088 unsigned int statusbyte;
1090 if (!(inb(ISR2)&8)) do
1091 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1092 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1094 outb(CDOR,(device&31)+64);/*address device to talk*/
1096 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1097 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1098 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1100 outb (CDOR,32); /*Address controller (me) to listen*/
1102 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1103 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1104 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1106 outb (CDOR,0x18); /*Send SPE (serial poll enable)*/
1107 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1108 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1109 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1111 /*wait for bus to be synced*/
1112 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1113 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1114 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1116 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1118 if (!(inb(ISR1)&1)&&status==EWOULDBLOCK) do
1119 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1120 while (!(inb(ISR1)&1)&&status==EWOULDBLOCK);
1122 outb(AUXMR,tcs); /* Take control after next read*/
1123 statusbyte=inb(DIR);
1125 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1126 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1127 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1129 outb(CDOR,0x19); /*SPD (serial poll disable)*/
1131 /*wait for bus to be synced*/
1132 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1133 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1134 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1137 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1138 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1139 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1141 outb(CDOR,95); /*untalk*/
1143 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1144 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1145 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1147 outb (CDOR,63);/*unlisten*/
1148 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1149 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1150 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1152 /*wait for bus to be synced*/
1153 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1154 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1155 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);