3 * GPIB driver for FreeBSD.
4 * Version 0.1 (No interrupts, no DMA)
5 * Supports National Instruments AT-GPIB and AT-GPIB/TNT boards.
6 * (AT-GPIB not tested, but it should work)
8 * Written by Fred Cawthorne (fcawth@delphi.umd.edu)
9 * Some sections were based partly on the lpt driver.
10 * (some remnants may remain)
12 * This software is distributed with NO WARRANTIES, not even the implied
13 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * The author grants any other persons or organizations permission to use
16 * or modify this software as long as this message is kept with the software,
17 * all derivative works or modified versions.
19 * $FreeBSD: src/sys/i386/isa/gpib.c,v 1.29 2000/01/29 16:17:32 peter Exp $
22 /*Please read the README file for usage information*/
24 #include <sys/param.h>
25 #include <sys/systm.h>
27 #include <sys/device.h>
29 #include <sys/malloc.h>
32 #include <bus/isa/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;
66 static struct dev_ops gp_ops = {
80 static struct gpib_softc {
81 char *sc_cp; /* current data to send */
82 int sc_count; /* bytes queued in sc_inbuf */
83 int sc_type; /* Type of gpib controller */
84 u_char sc_flags; /* flags (open and internal) */
85 char sc_unit; /* gpib device number */
86 char *sc_inbuf; /* buffer for data */
87 } gpib_sc; /* only support one of these? */
89 static char oldbytes[2];
91 /*This needs to be changed to be a bit more robust*/
93 gpprobe(struct isa_device *dvp)
96 struct gpib_softc *sc = &gpib_sc;
98 gpib_port = dvp->id_iobase;
101 if ((inb(KSR)&0xF7)==0x34) sc->sc_type=3;
102 else if ((inb(KSR)&0xF7)==0x24) sc->sc_type=2;
103 else if ((inb(KSR)&0xF7)==0x14) sc->sc_type=1;
111 * Attach device and print the type of card to the screen.
114 gpattach(struct isa_device *isdp)
116 struct gpib_softc *sc = &gpib_sc;
118 sc->sc_unit = isdp->id_unit;
120 kprintf ("gp%d: type AT-GPIB/TNT\n",sc->sc_unit);
122 kprintf ("gp%d: type AT-GPIB chip NAT4882B\n",sc->sc_unit);
124 kprintf ("gp%d: type AT-GPIB chip NAT4882A\n",sc->sc_unit);
125 sc->sc_flags |=ATTACHED;
127 make_dev(&gp_ops, sc->sc_unit, 0, 0, 0600, "gp");
133 * New open on device.
135 * More than 1 open is not allowed on the entire device.
136 * i.e. even if gpib5 is open, we can't open another minor device
139 gpopen(struct dev_open_args *ap)
141 cdev_t dev = ap->a_head.a_dev;
142 struct gpib_softc *sc = &gpib_sc;
148 /* minor number out of limits ? */
153 if (!(sc->sc_flags&ATTACHED)) { /* not attached */
158 if (sc->sc_flags&OPEN) { /* too late .. */
162 sc->sc_inbuf = kmalloc(BUFSIZE, M_DEVBUF, M_WAITOK);
164 if (initgpib()) return(EBUSY);
165 sc->sc_flags |= OPEN;
168 if (unit!=0) { /*Someone is trying to access an actual device*/
169 /*So.. we'll address it to listen*/
174 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
176 outb(CDOR,(unit&31)+32);/*address device to listen*/
180 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
181 outb (CDOR,64); /*Address controller (me) to talk*/
184 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
185 outb(AUXMR,gts); /*Set to Standby (Controller)*/
190 while (!(status&2)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
191 /*Set up the TURBO488 registers*/
192 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
193 outb(CNT0,0); /*NOTE this does not enable DMA to the host computer!!*/
198 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
199 outb(CMDR,0x10); /*RESET fifos*/
200 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
211 gpclose(struct dev_close_args *ap)
213 cdev_t dev = ap->a_head.a_dev;
214 struct gpib_softc *sc = &gpib_sc;
216 unsigned char status;
219 if (unit!=0) { /*Here we need to send the last character with EOS*/
220 /*and unaddress the listening device*/
225 /*Wait for fifo to become empty*/
227 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
229 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
231 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
233 /*Wait for DONE and STOP*/
234 if (status==EWOULDBLOCK) do {
235 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
237 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
239 /*Shut down TURBO488 */
240 outb(IMR2,0x00); /*DISABLE DMA to turbo488*/
241 outb(CMDR,0x20); /*soft reset turbo488*/
242 outb(CMDR,0x10); /*reset fifos*/
245 /*Send last byte with EOI set*/
246 /*Send second to last byte if there are 2 bytes left*/
247 if (status==EWOULDBLOCK) {
250 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
251 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
253 outb(CDOR,oldbytes[0]); /*Send second to last byte*/
254 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
255 status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
258 outb(AUXMR,seoi); /*Set EOI for the last byte*/
259 outb(AUXMR,0x5E); /*Clear SYNC*/
261 outb(CDOR,oldbytes[0]);
264 outb(CDOR,oldbytes[1]);
266 outb (CDOR,13); /*Send a CR.. we've got trouble*/
267 kprintf("gpib: Warning: gpclose called with nothing left in buffer\n");
272 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
273 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
276 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
277 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
278 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
281 outb(AUXMR,tca); /* Regain full control of the bus*/
286 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
287 outb(CDOR,63); /*unlisten*/
290 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
291 outb(AUXMR,0x5E); /*Clear SYNC*/
292 outb (CDOR,95);/*untalk*/
295 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
296 /*gotolocal(minor(dev));*/
299 sc->sc_flags = ATTACHED;
300 kfree(sc->sc_inbuf, M_DEVBUF);
301 sc->sc_inbuf = 0; /* Sanity */
307 * Copy from user's buffer, then write to GPIB device referenced
311 gpwrite(struct dev_write_args *ap)
313 cdev_t dev = ap->a_head.a_dev;
314 struct uio *uio = ap->a_uio;
318 while ((gpib_sc.sc_count = MIN(BUFSIZE-1, uio->uio_resid)) > 0) {
319 /* If there were >1 bytes left over, send them */
321 sendrawgpibfifo(minor(dev),oldbytes,2);
323 /*If there was 1 character left, put it at the beginning
326 (gpib_sc.sc_inbuf)[0]=oldbytes[0];
327 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
328 /* get from user-space */
329 uiomove(gpib_sc.sc_inbuf+1, gpib_sc.sc_count, uio);
333 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
334 /* get from user-space */
335 uiomove(gpib_sc.sc_inbuf, gpib_sc.sc_count, uio);
338 /*NOTE we always leave one byte in case this is the last write
339 so close can send EOI with the last byte There may be 2 bytes
340 since we are doing 16 bit transfers.(note the -1 in the count below)*/
341 /*If count<=2 we'll either pick it up on the next write or on close*/
342 if (gpib_sc.sc_count>2) {
343 count = sendrawgpibfifo(minor(dev),gpib_sc.sc_cp,gpib_sc.sc_count-1);
347 oldcount=gpib_sc.sc_count-count; /*Set # of remaining bytes*/
348 gpib_sc.sc_count-=count;
349 gpib_sc.sc_cp+=count; /*point char pointer to remaining bytes*/
351 else oldcount=gpib_sc.sc_count;
352 oldbytes[0]=gpib_sc.sc_cp[0];
354 oldbytes[1]=gpib_sc.sc_cp[1];
358 /* Here is how you would usually access a GPIB device
359 An exception would be a plotter or printer that you can just
360 write to using a minor device = its GPIB address */
363 gpioctl(struct dev_ioctl_args *ap)
365 struct gpibdata *gd = (struct gpibdata *)ap->a_data;
371 sendgpibfifo(gd->address,gd->data,*(gd->count));
375 result=readgpibfifo(gd->address,gd->data,*(gd->count));
384 trigger(gd->address);
388 enableremote(gd->address);
392 gotolocal(gd->address);
401 menableremote(gd->data);
405 mgotolocal(gd->data);
409 *(gd->data)=spoll(gd->address);
423 /*Just in case you want a dump of the registers...*/
425 static void showregs() {
426 kprintf ("NAT4882:\n");
427 kprintf ("ISR1=%X\t",inb(ISR1));
428 kprintf ("ISR2=%X\t",inb(ISR2));
429 kprintf ("SPSR=%X\t",inb(SPSR));
430 kprintf ("KSR =%X\t",inb(KSR));
431 kprintf ("ADSR=%X\t",inb(ADSR));
432 kprintf ("CPTR=%X\t",inb(CPTR));
433 kprintf ("SASR=%X\t",inb(SASR));
434 kprintf ("ADR0=%X\t",inb(ADR0));
435 kprintf ("ISR0=%X\t",inb(ISR0));
436 kprintf ("ADR1=%X\t",inb(ADR1));
437 kprintf ("BSR =%X\n",inb(BSR));
439 kprintf ("Turbo488\n");
440 kprintf ("STS1=%X ",inb(STS1));
441 kprintf ("STS2=%X ",inb(STS2));
442 kprintf ("ISR3=%X ",inb(ISR3));
443 kprintf ("CNT0=%X ",inb(CNT0));
444 kprintf ("CNT1=%X ",inb(CNT1));
445 kprintf ("CNT2=%X ",inb(CNT2));
446 kprintf ("CNT3=%X ",inb(CNT3));
447 kprintf ("IMR3=%X ",inb(IMR3));
448 kprintf ("TIMER=%X\n",inb(TIMER));
453 /*Set up the NAT4882 and TURBO488 registers */
454 /*This will be nonsense to you unless you have a data sheet from
455 National Instruments. They should give you one if you call them*/
467 outb(INTR,0); /* Put interrupt line in tri-state mode??*/
468 outb(AUXMR,chip_reset);
470 outb(IMR1,0x10); /* send interrupt to TURBO488 when END received*/
472 outb(IMR0,0x90); /* Do we want nba here too??? */
482 outb(EOSR,10); /*set EOS message to newline*/
483 /*should I make the default to interpret END as EOS?*/
484 /*It isn't now. The following changes this*/
485 outb(AUXMR,0x80); /*No special EOS handling*/
486 /*outb(AUXMR,0x88) */ /* Transmit END with EOS*/
487 /*outb(AUXMR,0x84) */ /* Set END on EOS received*/
488 /*outb(AUXMR,0x8C) */ /* Do both of the above*/
491 /* outb(AUXMR,hldi); */ /*Perform RFD Holdoff for all data in*/
492 /*Not currently supported*/
496 tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
498 outb(AUXMR,sic_rsc_off);
505 /*This is kind of Brute force.. But it works*/
510 outb(AUXMR,chip_reset);
514 These will also make little sense unless you have a data sheet.
515 Note that the routines with an "m" in the beginning are for
516 accessing multiple devices in one call*/
519 /*This is one thing I could not figure out how to do correctly.
520 I tried to use the auxilary command to enable remote, but it
521 never worked. Here, I bypass everything and write to the BSR
522 to enable the remote line. NOTE that these lines are effectively
523 "OR'ed" with the actual lines, so writing a 1 to the bit in the BSR
524 forces the GPIB line true, no matter what the fancy circuitry of the
525 NAT4882 wants to do with it*/
528 enableremote(unsigned char device)
533 if (status==EWOULDBLOCK) do {
534 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
536 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
538 outb(BSR,1); /*Set REN bit on GPIB*/
539 if (status==EWOULDBLOCK) do {
540 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
542 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
543 outb(CDOR,(device&31)+32); /*address device to listen*/
544 if (status==EWOULDBLOCK) do {
545 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
547 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
548 outb (CDOR,63); /*Unaddress device*/
549 if (status==EWOULDBLOCK) do {
550 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
552 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
555 /*This does not release the REM line on the gpib port, because if it did,
556 all the remote devices would go to local mode. This only sends the
557 gotolocal message to one device. Currently, REM is always held true
558 after enableremote is called, and is reset only on a close of the
562 gotolocal(unsigned char device)
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*/
571 outb(CDOR,(device&31)+32);
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*/
578 outb(AUXMR,0x5E); /*Clear SYNC*/
581 if (status==EWOULDBLOCK) do {
582 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
584 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
587 outb (CDOR,63);/*unaddress device*/
589 if (status==EWOULDBLOCK) do {
590 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
592 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
598 menableremote(unsigned char *device)
600 int status, counter = 0;
603 if (status==EWOULDBLOCK) do {
604 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
606 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
608 outb(BSR,1); /*Set REN bit on GPIB*/
611 if (status==EWOULDBLOCK) do {
612 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
614 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
615 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
618 while (device[counter]<32);
620 if (status==EWOULDBLOCK) do {
621 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
623 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
625 outb (CDOR,63); /*Unaddress device*/
626 if (status==EWOULDBLOCK) do {
627 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
629 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
634 mgotolocal(unsigned char *device)
638 if (device[counter]<32) do {
639 if (status==EWOULDBLOCK) do {
640 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
642 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
643 outb(CDOR,(device[counter]&31)+32);
645 } while (device[counter]<32);
646 if (status==EWOULDBLOCK) do {
647 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
649 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
651 outb(AUXMR,0x5E); /*Clear SYNC*/
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*/
660 outb (CDOR,63);/*unaddress device*/
661 if (status==EWOULDBLOCK) do {
662 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
664 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
668 /*Trigger a device. What happens depends on how the device is
672 trigger(unsigned char device)
677 if (!(inb(ISR2)&0x08)) do {
678 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
680 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
681 outb(CDOR,(device&31)+32); /*address device to listen*/
682 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
683 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
685 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
687 outb (CDOR,8); /*send GET*/
689 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
690 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
692 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
694 outb (CDOR,63);/*unaddress device*/
695 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
696 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
698 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
704 /*Trigger multiple devices by addressing them all to listen, and then
708 mtrigger(unsigned char *device)
709 { int status=EWOULDBLOCK;
713 if (device[counter]<32)
714 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
715 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
717 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
718 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
721 while (device[counter]<32);
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*/
726 outb (CDOR,8); /*send GET*/
728 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
729 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
731 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
733 outb (CDOR,63);/*unaddress device*/
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*/
743 /*This is not used now, but it should work with NI's 8 bit gpib board
744 since it does not use the TURBO488 registers at all */
747 /*Send data through the TURBO488 FIFOS to a device that is already
748 addressed to listen. This is used by the write call when someone is
749 writing to a printer or plotter, etc... */
750 /*The last byte of each write is held off until either the next
751 write or close, so it can be sent with EOI set*/
754 sendrawgpibfifo(unsigned char device,char *data,int count)
770 /*Wait for fifo to become not full if it is full */
772 if (!(inb(ISR3)&0x08)) do {
773 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
774 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
776 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
778 if((count>1)&&(inb(ISR3)&0x08)){
779 outw(FIFOB,*(unsigned*)(data+counter));
780 /* kprintf ("gpib: sent:%c,%c\n",data[counter],data[counter+1]);*/
786 while ((count>1)&&(status==EWOULDBLOCK));
787 /*The write routine and close routine must check if there is 1
788 byte left and handle it accordingly*/
791 /*Return the number of bytes written to the device*/
799 sendgpibfifo(unsigned char device,char *data,int count)
806 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
812 if (!(inb(ISR2)&8)) do
813 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
814 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
816 outb(CDOR,(device&31)+32);/*address device to listen*/
818 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
819 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
820 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
821 outb (CDOR,64); /*Address controller (me) to talk*/
823 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
824 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
825 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
827 outb(AUXMR,gts); /*Set to Standby (Controller)*/
837 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
838 outb(CMDR,0x10); /*RESET fifos*/
839 outb(CCRG,seoi); /*program to send EOI at end*/
840 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
843 /*Wait for fifo to become not full if it is full */
845 if (!(inb(ISR3)&0x08)) do {
846 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
847 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
849 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
851 if((count>1)&&(inb(ISR3)&0x08)){
852 /*if(count==2) outb(CFG,15+0x40); *//*send eoi when done*/
853 outw(FIFOB,*(unsigned*)(data+counter));
859 while ((count>2)&&(status==EWOULDBLOCK));
861 if (count==2&&status==EWOULDBLOCK) {
862 /*Wait for fifo to become not full*/
863 if(status==EWOULDBLOCK&&!(inb(ISR3)&0x08)) do {
864 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",SLEEP_MIN);
866 while (!(inb(ISR3)&0x08)&&status==EWOULDBLOCK); /*Fifo is full*/
867 /*outb(CFG,0x40+15);*//*send eoi when done*/
868 outb(FIFOB,data[counter]);
876 /*Wait for fifo to become empty*/
877 if (status==EWOULDBLOCK) do {
878 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
880 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
882 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
884 /*Wait for DONE and STOP*/
885 if (status==EWOULDBLOCK) do {
886 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
888 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
890 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
891 outb(CMDR,0x20); /*soft reset turbo488*/
892 outb(CMDR,0x10); /*reset fifos*/
895 /*Send last byte with EOI set*/
896 /*Here EOI is handled correctly since the string to be sent */
897 /*is actually all sent during the ioctl. (See above)*/
899 if (count==1&&status==EWOULDBLOCK) { /*Count should always=1 here*/
902 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
903 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
905 outb(AUXMR,seoi); /*Set EOI for the last byte*/
906 outb(AUXMR,0x5E); /*Clear SYNC*/
907 outb(CDOR,data[counter]);
913 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
914 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
917 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
918 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
919 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
920 outb(AUXMR,tca); /* Regain full control of the bus*/
923 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
924 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
925 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
927 outb(CDOR,63); /*unlisten*/
930 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
931 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
932 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
935 outb(AUXMR,0x5E); /*Clear SYNC*/
936 outb (CDOR,95);/*untalk*/
937 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
938 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
939 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
949 readgpibfifo(unsigned char device,char *data,int count)
958 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
963 outb(CFG,14+0x60+1); /* Halt on int,read, fifo B first, CCEN TMOE TIM */
964 outb(CMDR,0x10); /*RESET fifos*/
965 outb(CCRG,tcs); /*program to tcs at end*/
966 outb(CMDR,0x08);/*STOP??*/
972 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
973 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
975 outb (CDOR,32); /*Address controller (me) to listen*/
978 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
979 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
981 outb(CDOR,(device&31)+64);/*address device to talk*/
985 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
986 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
988 outb(AUXMR,gts); /*Set to Standby (Controller)*/
993 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
998 if (!(status1&0x01)&&(status1&0x04)){
1001 *(unsigned*)(data+counter)=inword;
1002 /* kprintf ("Read:%c,%c\n",data[counter],data[counter+1]);*/
1006 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",4);
1009 while (!(status1&0x01)&&status==EWOULDBLOCK);
1010 if(!(status2 & 0x04)){ /*Only 1 byte came in on last 16 bit transfer*/
1015 outb(CMDR,0x08); /*send STOP*/
1018 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1020 while(!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*wait for DONE and STOP*/
1023 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
1024 outb(CMDR,0x20); /*soft reset turbo488*/
1025 outb(CMDR,0x10); /*reset fifos*/
1028 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1029 while (!(inb(ISR1)&2));*/
1030 outb(AUXMR,tca); /* Regain full control of the bus*/
1034 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1035 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1036 outb(CDOR,63); /*unlisten*/
1039 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1040 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1042 outb(AUXMR,0x5E); /*Clear SYNC*/
1043 outb (CDOR,95);/*untalk*/
1045 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1046 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1054 /* Return the status byte from device */
1056 spoll(unsigned char device)
1058 int status=EWOULDBLOCK;
1059 unsigned int statusbyte;
1061 if (!(inb(ISR2)&8)) do
1062 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1063 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1065 outb(CDOR,(device&31)+64);/*address device to talk*/
1067 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1068 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1069 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1071 outb (CDOR,32); /*Address controller (me) to listen*/
1073 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1074 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1075 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1077 outb (CDOR,0x18); /*Send SPE (serial poll enable)*/
1078 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1079 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1080 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1082 /*wait for bus to be synced*/
1083 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1084 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1085 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1087 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1089 if (!(inb(ISR1)&1)&&status==EWOULDBLOCK) do
1090 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1091 while (!(inb(ISR1)&1)&&status==EWOULDBLOCK);
1093 outb(AUXMR,tcs); /* Take control after next read*/
1094 statusbyte=inb(DIR);
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,0x19); /*SPD (serial poll disable)*/
1102 /*wait for bus to be synced*/
1103 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1104 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1105 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
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,95); /*untalk*/
1114 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1115 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1116 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1118 outb (CDOR,63);/*unlisten*/
1119 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1120 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1121 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1123 /*wait for bus to be synced*/
1124 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1125 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1126 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);