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.10 2005/12/11 01:54:08 swildner 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>
32 #include <bus/isa/i386/isa_device.h>
34 #define GPIBPRI PCATCH
35 #define SLEEP_MAX 1000
40 static int initgpib(void);
41 static void closegpib(void);
42 static int sendgpibfifo(unsigned char device,char *data,int count);
43 static int sendrawgpibfifo(unsigned char device,char *data,int count);
44 static int readgpibfifo(unsigned char device,char *data,int count);
46 static void showregs(void);
48 static void enableremote(unsigned char device);
49 static void gotolocal(unsigned char device);
50 static void menableremote(unsigned char *device);
51 static void mgotolocal(unsigned char *device);
52 static void mtrigger(unsigned char *device);
53 static void trigger(unsigned char device);
54 static char spoll(unsigned char device);
56 static int gpprobe(struct isa_device *dvp);
57 static int gpattach(struct isa_device *dvp);
59 struct isa_driver gpdriver = {gpprobe, gpattach, "gp"};
61 static d_open_t gpopen;
62 static d_close_t gpclose;
63 static d_write_t gpwrite;
64 static d_ioctl_t gpioctl;
67 static struct cdevsw gp_cdevsw = {
81 /* strategy */ nostrategy,
92 static struct gpib_softc {
93 char *sc_cp; /* current data to send */
94 int sc_count; /* bytes queued in sc_inbuf */
95 int sc_type; /* Type of gpib controller */
96 u_char sc_flags; /* flags (open and internal) */
97 char sc_unit; /* gpib device number */
98 char *sc_inbuf; /* buffer for data */
99 } gpib_sc; /* only support one of these? */
101 static char oldbytes[2];
103 /*This needs to be changed to be a bit more robust*/
105 gpprobe(struct isa_device *dvp)
108 struct gpib_softc *sc = &gpib_sc;
110 gpib_port = dvp->id_iobase;
113 if ((inb(KSR)&0xF7)==0x34) sc->sc_type=3;
114 else if ((inb(KSR)&0xF7)==0x24) sc->sc_type=2;
115 else if ((inb(KSR)&0xF7)==0x14) sc->sc_type=1;
123 * Attach device and print the type of card to the screen.
126 gpattach(struct isa_device *isdp)
128 struct gpib_softc *sc = &gpib_sc;
130 sc->sc_unit = isdp->id_unit;
132 printf ("gp%d: type AT-GPIB/TNT\n",sc->sc_unit);
134 printf ("gp%d: type AT-GPIB chip NAT4882B\n",sc->sc_unit);
136 printf ("gp%d: type AT-GPIB chip NAT4882A\n",sc->sc_unit);
137 sc->sc_flags |=ATTACHED;
139 cdevsw_add(&gp_cdevsw, -1, sc->sc_unit);
140 make_dev(&gp_cdevsw, sc->sc_unit, 0, 0, 0600, "gp");
146 * New open on device.
148 * More than 1 open is not allowed on the entire device.
149 * i.e. even if gpib5 is open, we can't open another minor device
152 gpopen(dev_t dev, int flags, int fmt, struct thread *td)
154 struct gpib_softc *sc = &gpib_sc;
160 /* minor number out of limits ? */
165 if (!(sc->sc_flags&ATTACHED)) { /* not attached */
170 if (sc->sc_flags&OPEN) { /* too late .. */
174 /* Have memory for buffer? */
175 sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
176 if (sc->sc_inbuf == 0)
179 if (initgpib()) return(EBUSY);
180 sc->sc_flags |= OPEN;
183 if (unit!=0) { /*Someone is trying to access an actual device*/
184 /*So.. we'll address it to listen*/
189 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
191 outb(CDOR,(unit&31)+32);/*address device to listen*/
195 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
196 outb (CDOR,64); /*Address controller (me) to talk*/
199 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
200 outb(AUXMR,gts); /*Set to Standby (Controller)*/
205 while (!(status&2)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
206 /*Set up the TURBO488 registers*/
207 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
208 outb(CNT0,0); /*NOTE this does not enable DMA to the host computer!!*/
213 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
214 outb(CMDR,0x10); /*RESET fifos*/
215 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
226 gpclose(dev_t dev, int flags, int fmt, struct thread *td)
228 struct gpib_softc *sc = &gpib_sc;
230 unsigned char status;
233 if (unit!=0) { /*Here we need to send the last character with EOS*/
234 /*and unaddress the listening device*/
239 /*Wait for fifo to become empty*/
241 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
243 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
245 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
247 /*Wait for DONE and STOP*/
248 if (status==EWOULDBLOCK) do {
249 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
251 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
253 /*Shut down TURBO488 */
254 outb(IMR2,0x00); /*DISABLE DMA to turbo488*/
255 outb(CMDR,0x20); /*soft reset turbo488*/
256 outb(CMDR,0x10); /*reset fifos*/
259 /*Send last byte with EOI set*/
260 /*Send second to last byte if there are 2 bytes left*/
261 if (status==EWOULDBLOCK) {
264 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
265 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
267 outb(CDOR,oldbytes[0]); /*Send second to last byte*/
268 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
269 status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
272 outb(AUXMR,seoi); /*Set EOI for the last byte*/
273 outb(AUXMR,0x5E); /*Clear SYNC*/
275 outb(CDOR,oldbytes[0]);
278 outb(CDOR,oldbytes[1]);
280 outb (CDOR,13); /*Send a CR.. we've got trouble*/
281 printf("gpib: Warning: gpclose called with nothing left in buffer\n");
286 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
287 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
290 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
291 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
292 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
295 outb(AUXMR,tca); /* Regain full control of the bus*/
300 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
301 outb(CDOR,63); /*unlisten*/
304 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
305 outb(AUXMR,0x5E); /*Clear SYNC*/
306 outb (CDOR,95);/*untalk*/
309 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
310 /*gotolocal(minor(dev));*/
313 sc->sc_flags = ATTACHED;
314 free(sc->sc_inbuf, M_DEVBUF);
315 sc->sc_inbuf = 0; /* Sanity */
321 * Copy from user's buffer, then write to GPIB device referenced
325 gpwrite(dev_t dev, struct uio *uio, int ioflag)
330 while ((gpib_sc.sc_count = MIN(BUFSIZE-1, uio->uio_resid)) > 0) {
331 /* If there were >1 bytes left over, send them */
333 sendrawgpibfifo(minor(dev),oldbytes,2);
335 /*If there was 1 character left, put it at the beginning
338 (gpib_sc.sc_inbuf)[0]=oldbytes[0];
339 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
340 /* get from user-space */
341 uiomove(gpib_sc.sc_inbuf+1, gpib_sc.sc_count, uio);
345 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
346 /* get from user-space */
347 uiomove(gpib_sc.sc_inbuf, gpib_sc.sc_count, uio);
350 /*NOTE we always leave one byte in case this is the last write
351 so close can send EOI with the last byte There may be 2 bytes
352 since we are doing 16 bit transfers.(note the -1 in the count below)*/
353 /*If count<=2 we'll either pick it up on the next write or on close*/
354 if (gpib_sc.sc_count>2) {
355 count = sendrawgpibfifo(minor(dev),gpib_sc.sc_cp,gpib_sc.sc_count-1);
359 oldcount=gpib_sc.sc_count-count; /*Set # of remaining bytes*/
360 gpib_sc.sc_count-=count;
361 gpib_sc.sc_cp+=count; /*point char pointer to remaining bytes*/
363 else oldcount=gpib_sc.sc_count;
364 oldbytes[0]=gpib_sc.sc_cp[0];
366 oldbytes[1]=gpib_sc.sc_cp[1];
370 /* Here is how you would usually access a GPIB device
371 An exception would be a plotter or printer that you can just
372 write to using a minor device = its GPIB address */
375 gpioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
377 struct gpibdata *gd = (struct gpibdata *)data;
383 sendgpibfifo(gd->address,gd->data,*(gd->count));
387 result=readgpibfifo(gd->address,gd->data,*(gd->count));
396 trigger(gd->address);
400 enableremote(gd->address);
404 gotolocal(gd->address);
413 menableremote(gd->data);
417 mgotolocal(gd->data);
421 *(gd->data)=spoll(gd->address);
435 /*Just in case you want a dump of the registers...*/
437 static void showregs() {
438 printf ("NAT4882:\n");
439 printf ("ISR1=%X\t",inb(ISR1));
440 printf ("ISR2=%X\t",inb(ISR2));
441 printf ("SPSR=%X\t",inb(SPSR));
442 printf ("KSR =%X\t",inb(KSR));
443 printf ("ADSR=%X\t",inb(ADSR));
444 printf ("CPTR=%X\t",inb(CPTR));
445 printf ("SASR=%X\t",inb(SASR));
446 printf ("ADR0=%X\t",inb(ADR0));
447 printf ("ISR0=%X\t",inb(ISR0));
448 printf ("ADR1=%X\t",inb(ADR1));
449 printf ("BSR =%X\n",inb(BSR));
451 printf ("Turbo488\n");
452 printf ("STS1=%X ",inb(STS1));
453 printf ("STS2=%X ",inb(STS2));
454 printf ("ISR3=%X ",inb(ISR3));
455 printf ("CNT0=%X ",inb(CNT0));
456 printf ("CNT1=%X ",inb(CNT1));
457 printf ("CNT2=%X ",inb(CNT2));
458 printf ("CNT3=%X ",inb(CNT3));
459 printf ("IMR3=%X ",inb(IMR3));
460 printf ("TIMER=%X\n",inb(TIMER));
465 /*Set up the NAT4882 and TURBO488 registers */
466 /*This will be nonsense to you unless you have a data sheet from
467 National Instruments. They should give you one if you call them*/
479 outb(INTR,0); /* Put interrupt line in tri-state mode??*/
480 outb(AUXMR,chip_reset);
482 outb(IMR1,0x10); /* send interrupt to TURBO488 when END received*/
484 outb(IMR0,0x90); /* Do we want nba here too??? */
494 outb(EOSR,10); /*set EOS message to newline*/
495 /*should I make the default to interpret END as EOS?*/
496 /*It isn't now. The following changes this*/
497 outb(AUXMR,0x80); /*No special EOS handling*/
498 /*outb(AUXMR,0x88) */ /* Transmit END with EOS*/
499 /*outb(AUXMR,0x84) */ /* Set END on EOS received*/
500 /*outb(AUXMR,0x8C) */ /* Do both of the above*/
503 /* outb(AUXMR,hldi); */ /*Perform RFD Holdoff for all data in*/
504 /*Not currently supported*/
508 tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
510 outb(AUXMR,sic_rsc_off);
517 /*This is kind of Brute force.. But it works*/
522 outb(AUXMR,chip_reset);
526 These will also make little sense unless you have a data sheet.
527 Note that the routines with an "m" in the beginning are for
528 accessing multiple devices in one call*/
531 /*This is one thing I could not figure out how to do correctly.
532 I tried to use the auxilary command to enable remote, but it
533 never worked. Here, I bypass everything and write to the BSR
534 to enable the remote line. NOTE that these lines are effectively
535 "OR'ed" with the actual lines, so writing a 1 to the bit in the BSR
536 forces the GPIB line true, no matter what the fancy circuitry of the
537 NAT4882 wants to do with it*/
540 enableremote(unsigned char device)
545 if (status==EWOULDBLOCK) do {
546 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
548 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
550 outb(BSR,1); /*Set REN bit on GPIB*/
551 if (status==EWOULDBLOCK) do {
552 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
554 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
555 outb(CDOR,(device&31)+32); /*address device to listen*/
556 if (status==EWOULDBLOCK) do {
557 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
559 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
560 outb (CDOR,63); /*Unaddress device*/
561 if (status==EWOULDBLOCK) do {
562 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
564 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
567 /*This does not release the REM line on the gpib port, because if it did,
568 all the remote devices would go to local mode. This only sends the
569 gotolocal message to one device. Currently, REM is always held true
570 after enableremote is called, and is reset only on a close of the
574 gotolocal(unsigned char 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*/
583 outb(CDOR,(device&31)+32);
585 if (status==EWOULDBLOCK) do {
586 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
588 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
590 outb(AUXMR,0x5E); /*Clear SYNC*/
593 if (status==EWOULDBLOCK) do {
594 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
596 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
599 outb (CDOR,63);/*unaddress device*/
601 if (status==EWOULDBLOCK) do {
602 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
604 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
610 menableremote(unsigned char *device)
612 int status, counter = 0;
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*/
620 outb(BSR,1); /*Set REN bit on GPIB*/
623 if (status==EWOULDBLOCK) do {
624 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
626 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
627 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
630 while (device[counter]<32);
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 (CDOR,63); /*Unaddress device*/
638 if (status==EWOULDBLOCK) do {
639 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
641 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
646 mgotolocal(unsigned char *device)
650 if (device[counter]<32) do {
651 if (status==EWOULDBLOCK) do {
652 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
654 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
655 outb(CDOR,(device[counter]&31)+32);
657 } while (device[counter]<32);
658 if (status==EWOULDBLOCK) do {
659 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
661 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
663 outb(AUXMR,0x5E); /*Clear SYNC*/
667 if (status==EWOULDBLOCK) do {
668 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
670 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
672 outb (CDOR,63);/*unaddress device*/
673 if (status==EWOULDBLOCK) do {
674 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
676 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
680 /*Trigger a device. What happens depends on how the device is
684 trigger(unsigned char device)
689 if (!(inb(ISR2)&0x08)) do {
690 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
692 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
693 outb(CDOR,(device&31)+32); /*address device to listen*/
694 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
695 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
697 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
699 outb (CDOR,8); /*send GET*/
701 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
702 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
704 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
706 outb (CDOR,63);/*unaddress device*/
707 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
708 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
710 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
716 /*Trigger multiple devices by addressing them all to listen, and then
720 mtrigger(unsigned char *device)
721 { int status=EWOULDBLOCK;
725 if (device[counter]<32)
726 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
727 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
729 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
730 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
733 while (device[counter]<32);
734 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
735 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
737 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
738 outb (CDOR,8); /*send GET*/
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*/
745 outb (CDOR,63);/*unaddress device*/
746 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
747 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
749 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
755 /*This is not used now, but it should work with NI's 8 bit gpib board
756 since it does not use the TURBO488 registers at all */
759 /*Send data through the TURBO488 FIFOS to a device that is already
760 addressed to listen. This is used by the write call when someone is
761 writing to a printer or plotter, etc... */
762 /*The last byte of each write is held off until either the next
763 write or close, so it can be sent with EOI set*/
766 sendrawgpibfifo(unsigned char device,char *data,int count)
782 /*Wait for fifo to become not full if it is full */
784 if (!(inb(ISR3)&0x08)) do {
785 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
786 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
788 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
790 if((count>1)&&(inb(ISR3)&0x08)){
791 outw(FIFOB,*(unsigned*)(data+counter));
792 /* printf ("gpib: sent:%c,%c\n",data[counter],data[counter+1]);*/
798 while ((count>1)&&(status==EWOULDBLOCK));
799 /*The write routine and close routine must check if there is 1
800 byte left and handle it accordingly*/
803 /*Return the number of bytes written to the device*/
811 sendgpibfifo(unsigned char device,char *data,int count)
818 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
824 if (!(inb(ISR2)&8)) do
825 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
826 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
828 outb(CDOR,(device&31)+32);/*address device to listen*/
830 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
831 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
832 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
833 outb (CDOR,64); /*Address controller (me) to talk*/
835 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
836 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
837 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
839 outb(AUXMR,gts); /*Set to Standby (Controller)*/
849 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
850 outb(CMDR,0x10); /*RESET fifos*/
851 outb(CCRG,seoi); /*program to send EOI at end*/
852 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
855 /*Wait for fifo to become not full if it is full */
857 if (!(inb(ISR3)&0x08)) do {
858 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
859 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
861 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
863 if((count>1)&&(inb(ISR3)&0x08)){
864 /*if(count==2) outb(CFG,15+0x40); *//*send eoi when done*/
865 outw(FIFOB,*(unsigned*)(data+counter));
871 while ((count>2)&&(status==EWOULDBLOCK));
873 if (count==2&&status==EWOULDBLOCK) {
874 /*Wait for fifo to become not full*/
875 if(status==EWOULDBLOCK&&!(inb(ISR3)&0x08)) do {
876 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",SLEEP_MIN);
878 while (!(inb(ISR3)&0x08)&&status==EWOULDBLOCK); /*Fifo is full*/
879 /*outb(CFG,0x40+15);*//*send eoi when done*/
880 outb(FIFOB,data[counter]);
888 /*Wait for fifo to become empty*/
889 if (status==EWOULDBLOCK) do {
890 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
892 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
894 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
896 /*Wait for DONE and STOP*/
897 if (status==EWOULDBLOCK) do {
898 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
900 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
902 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
903 outb(CMDR,0x20); /*soft reset turbo488*/
904 outb(CMDR,0x10); /*reset fifos*/
907 /*Send last byte with EOI set*/
908 /*Here EOI is handled correctly since the string to be sent */
909 /*is actually all sent during the ioctl. (See above)*/
911 if (count==1&&status==EWOULDBLOCK) { /*Count should always=1 here*/
914 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
915 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
917 outb(AUXMR,seoi); /*Set EOI for the last byte*/
918 outb(AUXMR,0x5E); /*Clear SYNC*/
919 outb(CDOR,data[counter]);
925 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
926 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
929 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
930 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
931 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
932 outb(AUXMR,tca); /* Regain full control of the bus*/
935 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
936 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
937 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
939 outb(CDOR,63); /*unlisten*/
942 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
943 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
944 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
947 outb(AUXMR,0x5E); /*Clear SYNC*/
948 outb (CDOR,95);/*untalk*/
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);
961 readgpibfifo(unsigned char device,char *data,int count)
970 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
975 outb(CFG,14+0x60+1); /* Halt on int,read, fifo B first, CCEN TMOE TIM */
976 outb(CMDR,0x10); /*RESET fifos*/
977 outb(CCRG,tcs); /*program to tcs at end*/
978 outb(CMDR,0x08);/*STOP??*/
984 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
985 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
987 outb (CDOR,32); /*Address controller (me) to listen*/
990 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
991 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
993 outb(CDOR,(device&31)+64);/*address device to talk*/
997 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
998 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1000 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1005 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
1010 if (!(status1&0x01)&&(status1&0x04)){
1013 *(unsigned*)(data+counter)=inword;
1014 /* printf ("Read:%c,%c\n",data[counter],data[counter+1]);*/
1018 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",4);
1021 while (!(status1&0x01)&&status==EWOULDBLOCK);
1022 if(!(status2 & 0x04)){ /*Only 1 byte came in on last 16 bit transfer*/
1027 outb(CMDR,0x08); /*send STOP*/
1030 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1032 while(!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*wait for DONE and STOP*/
1035 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
1036 outb(CMDR,0x20); /*soft reset turbo488*/
1037 outb(CMDR,0x10); /*reset fifos*/
1040 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1041 while (!(inb(ISR1)&2));*/
1042 outb(AUXMR,tca); /* Regain full control of the bus*/
1046 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1047 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1048 outb(CDOR,63); /*unlisten*/
1051 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1052 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1054 outb(AUXMR,0x5E); /*Clear SYNC*/
1055 outb (CDOR,95);/*untalk*/
1057 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1058 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1066 /* Return the status byte from device */
1068 spoll(unsigned char device)
1070 int status=EWOULDBLOCK;
1071 unsigned int statusbyte;
1073 if (!(inb(ISR2)&8)) do
1074 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1075 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1077 outb(CDOR,(device&31)+64);/*address device to talk*/
1079 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1080 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1081 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1083 outb (CDOR,32); /*Address controller (me) to listen*/
1085 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1086 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1087 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1089 outb (CDOR,0x18); /*Send SPE (serial poll enable)*/
1090 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1091 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1092 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1094 /*wait for bus to be synced*/
1095 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1096 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1097 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1099 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1101 if (!(inb(ISR1)&1)&&status==EWOULDBLOCK) do
1102 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1103 while (!(inb(ISR1)&1)&&status==EWOULDBLOCK);
1105 outb(AUXMR,tcs); /* Take control after next read*/
1106 statusbyte=inb(DIR);
1108 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1109 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1110 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1112 outb(CDOR,0x19); /*SPD (serial poll disable)*/
1114 /*wait for bus to be synced*/
1115 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1116 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1117 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1120 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1121 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1122 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1124 outb(CDOR,95); /*untalk*/
1126 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1127 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1128 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1130 outb (CDOR,63);/*unlisten*/
1131 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1132 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1133 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1135 /*wait for bus to be synced*/
1136 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1137 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1138 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);