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.9 2004/05/19 22:52:42 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>
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.
127 struct isa_device *isdp;
129 struct gpib_softc *sc = &gpib_sc;
131 sc->sc_unit = isdp->id_unit;
133 printf ("gp%d: type AT-GPIB/TNT\n",sc->sc_unit);
135 printf ("gp%d: type AT-GPIB chip NAT4882B\n",sc->sc_unit);
137 printf ("gp%d: type AT-GPIB chip NAT4882A\n",sc->sc_unit);
138 sc->sc_flags |=ATTACHED;
140 cdevsw_add(&gp_cdevsw, -1, sc->sc_unit);
141 make_dev(&gp_cdevsw, sc->sc_unit, 0, 0, 0600, "gp");
147 * New open on device.
149 * More than 1 open is not allowed on the entire device.
150 * i.e. even if gpib5 is open, we can't open another minor device
153 gpopen(dev, flags, fmt, td)
159 struct gpib_softc *sc = &gpib_sc;
165 /* minor number out of limits ? */
170 if (!(sc->sc_flags&ATTACHED)) { /* not attached */
175 if (sc->sc_flags&OPEN) { /* too late .. */
179 /* Have memory for buffer? */
180 sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
181 if (sc->sc_inbuf == 0)
184 if (initgpib()) return(EBUSY);
185 sc->sc_flags |= OPEN;
188 if (unit!=0) { /*Someone is trying to access an actual device*/
189 /*So.. we'll address it to listen*/
194 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
196 outb(CDOR,(unit&31)+32);/*address device to listen*/
200 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
201 outb (CDOR,64); /*Address controller (me) to talk*/
204 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
205 outb(AUXMR,gts); /*Set to Standby (Controller)*/
210 while (!(status&2)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
211 /*Set up the TURBO488 registers*/
212 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
213 outb(CNT0,0); /*NOTE this does not enable DMA to the host computer!!*/
218 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
219 outb(CMDR,0x10); /*RESET fifos*/
220 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
231 gpclose(dev, flags, fmt, td)
237 struct gpib_softc *sc = &gpib_sc;
239 unsigned char status;
242 if (unit!=0) { /*Here we need to send the last character with EOS*/
243 /*and unaddress the listening device*/
248 /*Wait for fifo to become empty*/
250 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
252 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
254 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
256 /*Wait for DONE and STOP*/
257 if (status==EWOULDBLOCK) do {
258 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
260 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
262 /*Shut down TURBO488 */
263 outb(IMR2,0x00); /*DISABLE DMA to turbo488*/
264 outb(CMDR,0x20); /*soft reset turbo488*/
265 outb(CMDR,0x10); /*reset fifos*/
268 /*Send last byte with EOI set*/
269 /*Send second to last byte if there are 2 bytes left*/
270 if (status==EWOULDBLOCK) {
273 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
274 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
276 outb(CDOR,oldbytes[0]); /*Send second to last byte*/
277 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
278 status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
281 outb(AUXMR,seoi); /*Set EOI for the last byte*/
282 outb(AUXMR,0x5E); /*Clear SYNC*/
284 outb(CDOR,oldbytes[0]);
287 outb(CDOR,oldbytes[1]);
289 outb (CDOR,13); /*Send a CR.. we've got trouble*/
290 printf("gpib: Warning: gpclose called with nothing left in buffer\n");
295 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
296 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
299 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
300 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
301 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
304 outb(AUXMR,tca); /* Regain full control of the bus*/
309 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
310 outb(CDOR,63); /*unlisten*/
313 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
314 outb(AUXMR,0x5E); /*Clear SYNC*/
315 outb (CDOR,95);/*untalk*/
318 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
319 /*gotolocal(minor(dev));*/
322 sc->sc_flags = ATTACHED;
323 free(sc->sc_inbuf, M_DEVBUF);
324 sc->sc_inbuf = 0; /* Sanity */
330 * Copy from user's buffer, then write to GPIB device referenced
334 gpwrite(dev, uio, ioflag)
342 while ((gpib_sc.sc_count = MIN(BUFSIZE-1, uio->uio_resid)) > 0) {
343 /* If there were >1 bytes left over, send them */
345 sendrawgpibfifo(minor(dev),oldbytes,2);
347 /*If there was 1 character left, put it at the beginning
350 (gpib_sc.sc_inbuf)[0]=oldbytes[0];
351 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
352 /* get from user-space */
353 uiomove(gpib_sc.sc_inbuf+1, gpib_sc.sc_count, uio);
357 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
358 /* get from user-space */
359 uiomove(gpib_sc.sc_inbuf, gpib_sc.sc_count, uio);
362 /*NOTE we always leave one byte in case this is the last write
363 so close can send EOI with the last byte There may be 2 bytes
364 since we are doing 16 bit transfers.(note the -1 in the count below)*/
365 /*If count<=2 we'll either pick it up on the next write or on close*/
366 if (gpib_sc.sc_count>2) {
367 count = sendrawgpibfifo(minor(dev),gpib_sc.sc_cp,gpib_sc.sc_count-1);
371 oldcount=gpib_sc.sc_count-count; /*Set # of remaining bytes*/
372 gpib_sc.sc_count-=count;
373 gpib_sc.sc_cp+=count; /*point char pointer to remaining bytes*/
375 else oldcount=gpib_sc.sc_count;
376 oldbytes[0]=gpib_sc.sc_cp[0];
378 oldbytes[1]=gpib_sc.sc_cp[1];
382 /* Here is how you would usually access a GPIB device
383 An exception would be a plotter or printer that you can just
384 write to using a minor device = its GPIB address */
387 gpioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
389 struct gpibdata *gd = (struct gpibdata *)data;
395 sendgpibfifo(gd->address,gd->data,*(gd->count));
399 result=readgpibfifo(gd->address,gd->data,*(gd->count));
408 trigger(gd->address);
412 enableremote(gd->address);
416 gotolocal(gd->address);
425 menableremote(gd->data);
429 mgotolocal(gd->data);
433 *(gd->data)=spoll(gd->address);
447 /*Just in case you want a dump of the registers...*/
449 static void showregs() {
450 printf ("NAT4882:\n");
451 printf ("ISR1=%X\t",inb(ISR1));
452 printf ("ISR2=%X\t",inb(ISR2));
453 printf ("SPSR=%X\t",inb(SPSR));
454 printf ("KSR =%X\t",inb(KSR));
455 printf ("ADSR=%X\t",inb(ADSR));
456 printf ("CPTR=%X\t",inb(CPTR));
457 printf ("SASR=%X\t",inb(SASR));
458 printf ("ADR0=%X\t",inb(ADR0));
459 printf ("ISR0=%X\t",inb(ISR0));
460 printf ("ADR1=%X\t",inb(ADR1));
461 printf ("BSR =%X\n",inb(BSR));
463 printf ("Turbo488\n");
464 printf ("STS1=%X ",inb(STS1));
465 printf ("STS2=%X ",inb(STS2));
466 printf ("ISR3=%X ",inb(ISR3));
467 printf ("CNT0=%X ",inb(CNT0));
468 printf ("CNT1=%X ",inb(CNT1));
469 printf ("CNT2=%X ",inb(CNT2));
470 printf ("CNT3=%X ",inb(CNT3));
471 printf ("IMR3=%X ",inb(IMR3));
472 printf ("TIMER=%X\n",inb(TIMER));
477 /*Set up the NAT4882 and TURBO488 registers */
478 /*This will be nonsense to you unless you have a data sheet from
479 National Instruments. They should give you one if you call them*/
491 outb(INTR,0); /* Put interrupt line in tri-state mode??*/
492 outb(AUXMR,chip_reset);
494 outb(IMR1,0x10); /* send interrupt to TURBO488 when END received*/
496 outb(IMR0,0x90); /* Do we want nba here too??? */
506 outb(EOSR,10); /*set EOS message to newline*/
507 /*should I make the default to interpret END as EOS?*/
508 /*It isn't now. The following changes this*/
509 outb(AUXMR,0x80); /*No special EOS handling*/
510 /*outb(AUXMR,0x88) */ /* Transmit END with EOS*/
511 /*outb(AUXMR,0x84) */ /* Set END on EOS received*/
512 /*outb(AUXMR,0x8C) */ /* Do both of the above*/
515 /* outb(AUXMR,hldi); */ /*Perform RFD Holdoff for all data in*/
516 /*Not currently supported*/
520 tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
522 outb(AUXMR,sic_rsc_off);
529 /*This is kind of Brute force.. But it works*/
534 outb(AUXMR,chip_reset);
538 These will also make little sense unless you have a data sheet.
539 Note that the routines with an "m" in the beginning are for
540 accessing multiple devices in one call*/
543 /*This is one thing I could not figure out how to do correctly.
544 I tried to use the auxilary command to enable remote, but it
545 never worked. Here, I bypass everything and write to the BSR
546 to enable the remote line. NOTE that these lines are effectively
547 "OR'ed" with the actual lines, so writing a 1 to the bit in the BSR
548 forces the GPIB line true, no matter what the fancy circuitry of the
549 NAT4882 wants to do with it*/
552 enableremote(unsigned char device)
557 if (status==EWOULDBLOCK) do {
558 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
560 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
562 outb(BSR,1); /*Set REN bit on GPIB*/
563 if (status==EWOULDBLOCK) do {
564 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
566 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
567 outb(CDOR,(device&31)+32); /*address device to listen*/
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,63); /*Unaddress device*/
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*/
579 /*This does not release the REM line on the gpib port, because if it did,
580 all the remote devices would go to local mode. This only sends the
581 gotolocal message to one device. Currently, REM is always held true
582 after enableremote is called, and is reset only on a close of the
586 gotolocal(unsigned char device)
590 if (status==EWOULDBLOCK) do {
591 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
593 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
595 outb(CDOR,(device&31)+32);
597 if (status==EWOULDBLOCK) do {
598 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
600 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
602 outb(AUXMR,0x5E); /*Clear SYNC*/
605 if (status==EWOULDBLOCK) do {
606 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
608 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
611 outb (CDOR,63);/*unaddress device*/
613 if (status==EWOULDBLOCK) do {
614 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
616 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
622 menableremote(unsigned char *device)
624 int status, counter = 0;
627 if (status==EWOULDBLOCK) do {
628 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
630 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
632 outb(BSR,1); /*Set REN bit on GPIB*/
635 if (status==EWOULDBLOCK) do {
636 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
638 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
639 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
642 while (device[counter]<32);
644 if (status==EWOULDBLOCK) do {
645 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
647 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
649 outb (CDOR,63); /*Unaddress device*/
650 if (status==EWOULDBLOCK) do {
651 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
653 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
658 mgotolocal(unsigned char *device)
662 if (device[counter]<32) do {
663 if (status==EWOULDBLOCK) do {
664 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
666 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
667 outb(CDOR,(device[counter]&31)+32);
669 } while (device[counter]<32);
670 if (status==EWOULDBLOCK) do {
671 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
673 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
675 outb(AUXMR,0x5E); /*Clear SYNC*/
679 if (status==EWOULDBLOCK) do {
680 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
682 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
684 outb (CDOR,63);/*unaddress device*/
685 if (status==EWOULDBLOCK) do {
686 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
688 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
692 /*Trigger a device. What happens depends on how the device is
696 trigger(unsigned char device)
701 if (!(inb(ISR2)&0x08)) do {
702 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
704 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
705 outb(CDOR,(device&31)+32); /*address device to listen*/
706 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
707 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
709 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
711 outb (CDOR,8); /*send GET*/
713 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
714 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
716 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
718 outb (CDOR,63);/*unaddress device*/
719 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
720 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
722 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
728 /*Trigger multiple devices by addressing them all to listen, and then
732 mtrigger(unsigned char *device)
733 { int status=EWOULDBLOCK;
737 if (device[counter]<32)
738 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
739 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
741 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
742 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
745 while (device[counter]<32);
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*/
750 outb (CDOR,8); /*send GET*/
752 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
753 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
755 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
757 outb (CDOR,63);/*unaddress device*/
758 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
759 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
761 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
767 /*This is not used now, but it should work with NI's 8 bit gpib board
768 since it does not use the TURBO488 registers at all */
771 /*Send data through the TURBO488 FIFOS to a device that is already
772 addressed to listen. This is used by the write call when someone is
773 writing to a printer or plotter, etc... */
774 /*The last byte of each write is held off until either the next
775 write or close, so it can be sent with EOI set*/
778 sendrawgpibfifo(unsigned char device,char *data,int count)
794 /*Wait for fifo to become not full if it is full */
796 if (!(inb(ISR3)&0x08)) do {
797 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
798 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
800 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
802 if((count>1)&&(inb(ISR3)&0x08)){
803 outw(FIFOB,*(unsigned*)(data+counter));
804 /* printf ("gpib: sent:%c,%c\n",data[counter],data[counter+1]);*/
810 while ((count>1)&&(status==EWOULDBLOCK));
811 /*The write routine and close routine must check if there is 1
812 byte left and handle it accordingly*/
815 /*Return the number of bytes written to the device*/
823 sendgpibfifo(unsigned char device,char *data,int count)
830 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
836 if (!(inb(ISR2)&8)) do
837 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
838 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
840 outb(CDOR,(device&31)+32);/*address device to listen*/
842 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
843 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
844 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
845 outb (CDOR,64); /*Address controller (me) to talk*/
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);
851 outb(AUXMR,gts); /*Set to Standby (Controller)*/
861 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
862 outb(CMDR,0x10); /*RESET fifos*/
863 outb(CCRG,seoi); /*program to send EOI at end*/
864 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
867 /*Wait for fifo to become not full if it is full */
869 if (!(inb(ISR3)&0x08)) do {
870 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
871 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
873 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
875 if((count>1)&&(inb(ISR3)&0x08)){
876 /*if(count==2) outb(CFG,15+0x40); *//*send eoi when done*/
877 outw(FIFOB,*(unsigned*)(data+counter));
883 while ((count>2)&&(status==EWOULDBLOCK));
885 if (count==2&&status==EWOULDBLOCK) {
886 /*Wait for fifo to become not full*/
887 if(status==EWOULDBLOCK&&!(inb(ISR3)&0x08)) do {
888 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",SLEEP_MIN);
890 while (!(inb(ISR3)&0x08)&&status==EWOULDBLOCK); /*Fifo is full*/
891 /*outb(CFG,0x40+15);*//*send eoi when done*/
892 outb(FIFOB,data[counter]);
900 /*Wait for fifo to become empty*/
901 if (status==EWOULDBLOCK) do {
902 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
904 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
906 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
908 /*Wait for DONE and STOP*/
909 if (status==EWOULDBLOCK) do {
910 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
912 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
914 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
915 outb(CMDR,0x20); /*soft reset turbo488*/
916 outb(CMDR,0x10); /*reset fifos*/
919 /*Send last byte with EOI set*/
920 /*Here EOI is handled correctly since the string to be sent */
921 /*is actually all sent during the ioctl. (See above)*/
923 if (count==1&&status==EWOULDBLOCK) { /*Count should always=1 here*/
926 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
927 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
929 outb(AUXMR,seoi); /*Set EOI for the last byte*/
930 outb(AUXMR,0x5E); /*Clear SYNC*/
931 outb(CDOR,data[counter]);
937 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
938 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
941 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
942 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
943 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
944 outb(AUXMR,tca); /* Regain full control of the bus*/
947 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
948 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
949 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
951 outb(CDOR,63); /*unlisten*/
954 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
955 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
956 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
959 outb(AUXMR,0x5E); /*Clear SYNC*/
960 outb (CDOR,95);/*untalk*/
961 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
962 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
963 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
973 readgpibfifo(unsigned char device,char *data,int count)
982 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
987 outb(CFG,14+0x60+1); /* Halt on int,read, fifo B first, CCEN TMOE TIM */
988 outb(CMDR,0x10); /*RESET fifos*/
989 outb(CCRG,tcs); /*program to tcs at end*/
990 outb(CMDR,0x08);/*STOP??*/
996 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
997 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
999 outb (CDOR,32); /*Address controller (me) to listen*/
1002 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1003 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1005 outb(CDOR,(device&31)+64);/*address device to talk*/
1009 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1010 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1012 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1017 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
1022 if (!(status1&0x01)&&(status1&0x04)){
1025 *(unsigned*)(data+counter)=inword;
1026 /* printf ("Read:%c,%c\n",data[counter],data[counter+1]);*/
1030 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",4);
1033 while (!(status1&0x01)&&status==EWOULDBLOCK);
1034 if(!(status2 & 0x04)){ /*Only 1 byte came in on last 16 bit transfer*/
1039 outb(CMDR,0x08); /*send STOP*/
1042 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1044 while(!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*wait for DONE and STOP*/
1047 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
1048 outb(CMDR,0x20); /*soft reset turbo488*/
1049 outb(CMDR,0x10); /*reset fifos*/
1052 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1053 while (!(inb(ISR1)&2));*/
1054 outb(AUXMR,tca); /* Regain full control of the bus*/
1058 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1059 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1060 outb(CDOR,63); /*unlisten*/
1063 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1064 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1066 outb(AUXMR,0x5E); /*Clear SYNC*/
1067 outb (CDOR,95);/*untalk*/
1069 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1070 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1078 /* Return the status byte from device */
1080 spoll(unsigned char device)
1082 int status=EWOULDBLOCK;
1083 unsigned int statusbyte;
1085 if (!(inb(ISR2)&8)) do
1086 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1087 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1089 outb(CDOR,(device&31)+64);/*address device to talk*/
1091 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1092 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1093 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1095 outb (CDOR,32); /*Address controller (me) to listen*/
1097 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1098 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1099 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1101 outb (CDOR,0x18); /*Send SPE (serial poll enable)*/
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 /*wait for bus to be synced*/
1107 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1108 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1109 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1111 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1113 if (!(inb(ISR1)&1)&&status==EWOULDBLOCK) do
1114 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1115 while (!(inb(ISR1)&1)&&status==EWOULDBLOCK);
1117 outb(AUXMR,tcs); /* Take control after next read*/
1118 statusbyte=inb(DIR);
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,0x19); /*SPD (serial poll disable)*/
1126 /*wait for bus to be synced*/
1127 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1128 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1129 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1132 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1133 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1134 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1136 outb(CDOR,95); /*untalk*/
1138 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1139 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1140 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1142 outb (CDOR,63);/*unlisten*/
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 /*wait for bus to be synced*/
1148 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1149 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1150 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);