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.8 2004/05/13 23:49:16 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;
112 cdevsw_add(&gp_cdevsw);
114 gpib_port = dvp->id_iobase;
117 if ((inb(KSR)&0xF7)==0x34) sc->sc_type=3;
118 else if ((inb(KSR)&0xF7)==0x24) sc->sc_type=2;
119 else if ((inb(KSR)&0xF7)==0x14) sc->sc_type=1;
127 * Attach device and print the type of card to the screen.
131 struct isa_device *isdp;
133 struct gpib_softc *sc = &gpib_sc;
135 sc->sc_unit = isdp->id_unit;
137 printf ("gp%d: type AT-GPIB/TNT\n",sc->sc_unit);
139 printf ("gp%d: type AT-GPIB chip NAT4882B\n",sc->sc_unit);
141 printf ("gp%d: type AT-GPIB chip NAT4882A\n",sc->sc_unit);
142 sc->sc_flags |=ATTACHED;
144 make_dev(&gp_cdevsw, 0, 0, 0, 0600, "gp");
150 * New open on device.
152 * More than 1 open is not allowed on the entire device.
153 * i.e. even if gpib5 is open, we can't open another minor device
156 gpopen(dev, flags, fmt, td)
162 struct gpib_softc *sc = &gpib_sc;
168 /* minor number out of limits ? */
173 if (!(sc->sc_flags&ATTACHED)) { /* not attached */
178 if (sc->sc_flags&OPEN) { /* too late .. */
182 /* Have memory for buffer? */
183 sc->sc_inbuf = malloc(BUFSIZE, M_DEVBUF, M_WAITOK);
184 if (sc->sc_inbuf == 0)
187 if (initgpib()) return(EBUSY);
188 sc->sc_flags |= OPEN;
191 if (unit!=0) { /*Someone is trying to access an actual device*/
192 /*So.. we'll address it to listen*/
197 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
199 outb(CDOR,(unit&31)+32);/*address device to listen*/
203 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
204 outb (CDOR,64); /*Address controller (me) to talk*/
207 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
208 outb(AUXMR,gts); /*Set to Standby (Controller)*/
213 while (!(status&2)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
214 /*Set up the TURBO488 registers*/
215 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
216 outb(CNT0,0); /*NOTE this does not enable DMA to the host computer!!*/
221 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
222 outb(CMDR,0x10); /*RESET fifos*/
223 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
234 gpclose(dev, flags, fmt, td)
240 struct gpib_softc *sc = &gpib_sc;
242 unsigned char status;
245 if (unit!=0) { /*Here we need to send the last character with EOS*/
246 /*and unaddress the listening device*/
251 /*Wait for fifo to become empty*/
253 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
255 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
257 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
259 /*Wait for DONE and STOP*/
260 if (status==EWOULDBLOCK) do {
261 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
263 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
265 /*Shut down TURBO488 */
266 outb(IMR2,0x00); /*DISABLE DMA to turbo488*/
267 outb(CMDR,0x20); /*soft reset turbo488*/
268 outb(CMDR,0x10); /*reset fifos*/
271 /*Send last byte with EOI set*/
272 /*Send second to last byte if there are 2 bytes left*/
273 if (status==EWOULDBLOCK) {
276 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
277 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
279 outb(CDOR,oldbytes[0]); /*Send second to last byte*/
280 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
281 status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
284 outb(AUXMR,seoi); /*Set EOI for the last byte*/
285 outb(AUXMR,0x5E); /*Clear SYNC*/
287 outb(CDOR,oldbytes[0]);
290 outb(CDOR,oldbytes[1]);
292 outb (CDOR,13); /*Send a CR.. we've got trouble*/
293 printf("gpib: Warning: gpclose called with nothing left in buffer\n");
298 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
299 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
302 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
303 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
304 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
307 outb(AUXMR,tca); /* Regain full control of the bus*/
312 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
313 outb(CDOR,63); /*unlisten*/
316 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
317 outb(AUXMR,0x5E); /*Clear SYNC*/
318 outb (CDOR,95);/*untalk*/
321 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
322 /*gotolocal(minor(dev));*/
325 sc->sc_flags = ATTACHED;
326 free(sc->sc_inbuf, M_DEVBUF);
327 sc->sc_inbuf = 0; /* Sanity */
333 * Copy from user's buffer, then write to GPIB device referenced
337 gpwrite(dev, uio, ioflag)
345 while ((gpib_sc.sc_count = MIN(BUFSIZE-1, uio->uio_resid)) > 0) {
346 /* If there were >1 bytes left over, send them */
348 sendrawgpibfifo(minor(dev),oldbytes,2);
350 /*If there was 1 character left, put it at the beginning
353 (gpib_sc.sc_inbuf)[0]=oldbytes[0];
354 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
355 /* get from user-space */
356 uiomove(gpib_sc.sc_inbuf+1, gpib_sc.sc_count, uio);
360 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
361 /* get from user-space */
362 uiomove(gpib_sc.sc_inbuf, gpib_sc.sc_count, uio);
365 /*NOTE we always leave one byte in case this is the last write
366 so close can send EOI with the last byte There may be 2 bytes
367 since we are doing 16 bit transfers.(note the -1 in the count below)*/
368 /*If count<=2 we'll either pick it up on the next write or on close*/
369 if (gpib_sc.sc_count>2) {
370 count = sendrawgpibfifo(minor(dev),gpib_sc.sc_cp,gpib_sc.sc_count-1);
374 oldcount=gpib_sc.sc_count-count; /*Set # of remaining bytes*/
375 gpib_sc.sc_count-=count;
376 gpib_sc.sc_cp+=count; /*point char pointer to remaining bytes*/
378 else oldcount=gpib_sc.sc_count;
379 oldbytes[0]=gpib_sc.sc_cp[0];
381 oldbytes[1]=gpib_sc.sc_cp[1];
385 /* Here is how you would usually access a GPIB device
386 An exception would be a plotter or printer that you can just
387 write to using a minor device = its GPIB address */
390 gpioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct thread *td)
392 struct gpibdata *gd = (struct gpibdata *)data;
398 sendgpibfifo(gd->address,gd->data,*(gd->count));
402 result=readgpibfifo(gd->address,gd->data,*(gd->count));
411 trigger(gd->address);
415 enableremote(gd->address);
419 gotolocal(gd->address);
428 menableremote(gd->data);
432 mgotolocal(gd->data);
436 *(gd->data)=spoll(gd->address);
450 /*Just in case you want a dump of the registers...*/
452 static void showregs() {
453 printf ("NAT4882:\n");
454 printf ("ISR1=%X\t",inb(ISR1));
455 printf ("ISR2=%X\t",inb(ISR2));
456 printf ("SPSR=%X\t",inb(SPSR));
457 printf ("KSR =%X\t",inb(KSR));
458 printf ("ADSR=%X\t",inb(ADSR));
459 printf ("CPTR=%X\t",inb(CPTR));
460 printf ("SASR=%X\t",inb(SASR));
461 printf ("ADR0=%X\t",inb(ADR0));
462 printf ("ISR0=%X\t",inb(ISR0));
463 printf ("ADR1=%X\t",inb(ADR1));
464 printf ("BSR =%X\n",inb(BSR));
466 printf ("Turbo488\n");
467 printf ("STS1=%X ",inb(STS1));
468 printf ("STS2=%X ",inb(STS2));
469 printf ("ISR3=%X ",inb(ISR3));
470 printf ("CNT0=%X ",inb(CNT0));
471 printf ("CNT1=%X ",inb(CNT1));
472 printf ("CNT2=%X ",inb(CNT2));
473 printf ("CNT3=%X ",inb(CNT3));
474 printf ("IMR3=%X ",inb(IMR3));
475 printf ("TIMER=%X\n",inb(TIMER));
480 /*Set up the NAT4882 and TURBO488 registers */
481 /*This will be nonsense to you unless you have a data sheet from
482 National Instruments. They should give you one if you call them*/
494 outb(INTR,0); /* Put interrupt line in tri-state mode??*/
495 outb(AUXMR,chip_reset);
497 outb(IMR1,0x10); /* send interrupt to TURBO488 when END received*/
499 outb(IMR0,0x90); /* Do we want nba here too??? */
509 outb(EOSR,10); /*set EOS message to newline*/
510 /*should I make the default to interpret END as EOS?*/
511 /*It isn't now. The following changes this*/
512 outb(AUXMR,0x80); /*No special EOS handling*/
513 /*outb(AUXMR,0x88) */ /* Transmit END with EOS*/
514 /*outb(AUXMR,0x84) */ /* Set END on EOS received*/
515 /*outb(AUXMR,0x8C) */ /* Do both of the above*/
518 /* outb(AUXMR,hldi); */ /*Perform RFD Holdoff for all data in*/
519 /*Not currently supported*/
523 tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
525 outb(AUXMR,sic_rsc_off);
532 /*This is kind of Brute force.. But it works*/
537 outb(AUXMR,chip_reset);
541 These will also make little sense unless you have a data sheet.
542 Note that the routines with an "m" in the beginning are for
543 accessing multiple devices in one call*/
546 /*This is one thing I could not figure out how to do correctly.
547 I tried to use the auxilary command to enable remote, but it
548 never worked. Here, I bypass everything and write to the BSR
549 to enable the remote line. NOTE that these lines are effectively
550 "OR'ed" with the actual lines, so writing a 1 to the bit in the BSR
551 forces the GPIB line true, no matter what the fancy circuitry of the
552 NAT4882 wants to do with it*/
555 enableremote(unsigned char device)
560 if (status==EWOULDBLOCK) do {
561 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
563 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
565 outb(BSR,1); /*Set REN bit on GPIB*/
566 if (status==EWOULDBLOCK) do {
567 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
569 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
570 outb(CDOR,(device&31)+32); /*address device to listen*/
571 if (status==EWOULDBLOCK) do {
572 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
574 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
575 outb (CDOR,63); /*Unaddress device*/
576 if (status==EWOULDBLOCK) do {
577 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
579 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
582 /*This does not release the REM line on the gpib port, because if it did,
583 all the remote devices would go to local mode. This only sends the
584 gotolocal message to one device. Currently, REM is always held true
585 after enableremote is called, and is reset only on a close of the
589 gotolocal(unsigned char device)
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*/
598 outb(CDOR,(device&31)+32);
600 if (status==EWOULDBLOCK) do {
601 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
603 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
605 outb(AUXMR,0x5E); /*Clear SYNC*/
608 if (status==EWOULDBLOCK) do {
609 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
611 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
614 outb (CDOR,63);/*unaddress device*/
616 if (status==EWOULDBLOCK) do {
617 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
619 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
625 menableremote(unsigned char *device)
627 int status, counter = 0;
630 if (status==EWOULDBLOCK) do {
631 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
633 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
635 outb(BSR,1); /*Set REN bit on GPIB*/
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*/
642 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
645 while (device[counter]<32);
647 if (status==EWOULDBLOCK) do {
648 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
650 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
652 outb (CDOR,63); /*Unaddress device*/
653 if (status==EWOULDBLOCK) do {
654 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
656 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
661 mgotolocal(unsigned char *device)
665 if (device[counter]<32) do {
666 if (status==EWOULDBLOCK) do {
667 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
669 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
670 outb(CDOR,(device[counter]&31)+32);
672 } while (device[counter]<32);
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*/
678 outb(AUXMR,0x5E); /*Clear SYNC*/
682 if (status==EWOULDBLOCK) do {
683 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
685 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
687 outb (CDOR,63);/*unaddress device*/
688 if (status==EWOULDBLOCK) do {
689 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
691 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
695 /*Trigger a device. What happens depends on how the device is
699 trigger(unsigned char device)
704 if (!(inb(ISR2)&0x08)) do {
705 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
707 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
708 outb(CDOR,(device&31)+32); /*address device to listen*/
709 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
710 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
712 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
714 outb (CDOR,8); /*send GET*/
716 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
717 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
719 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
721 outb (CDOR,63);/*unaddress device*/
722 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
723 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
725 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
731 /*Trigger multiple devices by addressing them all to listen, and then
735 mtrigger(unsigned char *device)
736 { int status=EWOULDBLOCK;
740 if (device[counter]<32)
741 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
742 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
744 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
745 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
748 while (device[counter]<32);
749 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
750 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
752 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
753 outb (CDOR,8); /*send GET*/
755 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
756 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
758 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
760 outb (CDOR,63);/*unaddress device*/
761 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
762 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
764 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
770 /*This is not used now, but it should work with NI's 8 bit gpib board
771 since it does not use the TURBO488 registers at all */
774 /*Send data through the TURBO488 FIFOS to a device that is already
775 addressed to listen. This is used by the write call when someone is
776 writing to a printer or plotter, etc... */
777 /*The last byte of each write is held off until either the next
778 write or close, so it can be sent with EOI set*/
781 sendrawgpibfifo(unsigned char device,char *data,int count)
797 /*Wait for fifo to become not full if it is full */
799 if (!(inb(ISR3)&0x08)) do {
800 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
801 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
803 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
805 if((count>1)&&(inb(ISR3)&0x08)){
806 outw(FIFOB,*(unsigned*)(data+counter));
807 /* printf ("gpib: sent:%c,%c\n",data[counter],data[counter+1]);*/
813 while ((count>1)&&(status==EWOULDBLOCK));
814 /*The write routine and close routine must check if there is 1
815 byte left and handle it accordingly*/
818 /*Return the number of bytes written to the device*/
826 sendgpibfifo(unsigned char device,char *data,int count)
833 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
839 if (!(inb(ISR2)&8)) do
840 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
841 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
843 outb(CDOR,(device&31)+32);/*address device to listen*/
845 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
846 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
847 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
848 outb (CDOR,64); /*Address controller (me) to talk*/
850 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
851 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
852 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
854 outb(AUXMR,gts); /*Set to Standby (Controller)*/
864 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
865 outb(CMDR,0x10); /*RESET fifos*/
866 outb(CCRG,seoi); /*program to send EOI at end*/
867 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
870 /*Wait for fifo to become not full if it is full */
872 if (!(inb(ISR3)&0x08)) do {
873 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
874 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
876 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
878 if((count>1)&&(inb(ISR3)&0x08)){
879 /*if(count==2) outb(CFG,15+0x40); *//*send eoi when done*/
880 outw(FIFOB,*(unsigned*)(data+counter));
886 while ((count>2)&&(status==EWOULDBLOCK));
888 if (count==2&&status==EWOULDBLOCK) {
889 /*Wait for fifo to become not full*/
890 if(status==EWOULDBLOCK&&!(inb(ISR3)&0x08)) do {
891 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",SLEEP_MIN);
893 while (!(inb(ISR3)&0x08)&&status==EWOULDBLOCK); /*Fifo is full*/
894 /*outb(CFG,0x40+15);*//*send eoi when done*/
895 outb(FIFOB,data[counter]);
903 /*Wait for fifo to become empty*/
904 if (status==EWOULDBLOCK) do {
905 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
907 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
909 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
911 /*Wait for DONE and STOP*/
912 if (status==EWOULDBLOCK) do {
913 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
915 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
917 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
918 outb(CMDR,0x20); /*soft reset turbo488*/
919 outb(CMDR,0x10); /*reset fifos*/
922 /*Send last byte with EOI set*/
923 /*Here EOI is handled correctly since the string to be sent */
924 /*is actually all sent during the ioctl. (See above)*/
926 if (count==1&&status==EWOULDBLOCK) { /*Count should always=1 here*/
929 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
930 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
932 outb(AUXMR,seoi); /*Set EOI for the last byte*/
933 outb(AUXMR,0x5E); /*Clear SYNC*/
934 outb(CDOR,data[counter]);
940 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
941 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
944 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
945 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
946 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
947 outb(AUXMR,tca); /* Regain full control of the bus*/
950 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
951 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
952 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
954 outb(CDOR,63); /*unlisten*/
957 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
958 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
959 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
962 outb(AUXMR,0x5E); /*Clear SYNC*/
963 outb (CDOR,95);/*untalk*/
964 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
965 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
966 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
976 readgpibfifo(unsigned char device,char *data,int count)
985 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
990 outb(CFG,14+0x60+1); /* Halt on int,read, fifo B first, CCEN TMOE TIM */
991 outb(CMDR,0x10); /*RESET fifos*/
992 outb(CCRG,tcs); /*program to tcs at end*/
993 outb(CMDR,0x08);/*STOP??*/
999 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1000 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1002 outb (CDOR,32); /*Address controller (me) to listen*/
1005 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1006 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1008 outb(CDOR,(device&31)+64);/*address device to talk*/
1012 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1013 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1015 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1020 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
1025 if (!(status1&0x01)&&(status1&0x04)){
1028 *(unsigned*)(data+counter)=inword;
1029 /* printf ("Read:%c,%c\n",data[counter],data[counter+1]);*/
1033 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",4);
1036 while (!(status1&0x01)&&status==EWOULDBLOCK);
1037 if(!(status2 & 0x04)){ /*Only 1 byte came in on last 16 bit transfer*/
1042 outb(CMDR,0x08); /*send STOP*/
1045 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1047 while(!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*wait for DONE and STOP*/
1050 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
1051 outb(CMDR,0x20); /*soft reset turbo488*/
1052 outb(CMDR,0x10); /*reset fifos*/
1055 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1056 while (!(inb(ISR1)&2));*/
1057 outb(AUXMR,tca); /* Regain full control of the bus*/
1061 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1062 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1063 outb(CDOR,63); /*unlisten*/
1066 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1067 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1069 outb(AUXMR,0x5E); /*Clear SYNC*/
1070 outb (CDOR,95);/*untalk*/
1072 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1073 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1081 /* Return the status byte from device */
1083 spoll(unsigned char device)
1085 int status=EWOULDBLOCK;
1086 unsigned int statusbyte;
1088 if (!(inb(ISR2)&8)) do
1089 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1090 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1092 outb(CDOR,(device&31)+64);/*address device to talk*/
1094 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1095 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1096 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1098 outb (CDOR,32); /*Address controller (me) to listen*/
1100 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1101 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1102 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1104 outb (CDOR,0x18); /*Send SPE (serial poll enable)*/
1105 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1106 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1107 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1109 /*wait for bus to be synced*/
1110 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1111 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1112 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1114 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1116 if (!(inb(ISR1)&1)&&status==EWOULDBLOCK) do
1117 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1118 while (!(inb(ISR1)&1)&&status==EWOULDBLOCK);
1120 outb(AUXMR,tcs); /* Take control after next read*/
1121 statusbyte=inb(DIR);
1123 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1124 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1125 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1127 outb(CDOR,0x19); /*SPD (serial poll disable)*/
1129 /*wait for bus to be synced*/
1130 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1131 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1132 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1135 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1136 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1137 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1139 outb(CDOR,95); /*untalk*/
1141 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1142 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1143 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1145 outb (CDOR,63);/*unlisten*/
1146 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1147 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1148 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1150 /*wait for bus to be synced*/
1151 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1152 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1153 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);