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.14 2006/12/22 23:26:17 swildner Exp $
23 /*Please read the README file for usage information*/
25 #include <sys/param.h>
26 #include <sys/systm.h>
28 #include <sys/device.h>
30 #include <sys/malloc.h>
33 #include <bus/isa/i386/isa_device.h>
35 #define GPIBPRI PCATCH
36 #define SLEEP_MAX 1000
41 static int initgpib(void);
42 static void closegpib(void);
43 static int sendgpibfifo(unsigned char device,char *data,int count);
44 static int sendrawgpibfifo(unsigned char device,char *data,int count);
45 static int readgpibfifo(unsigned char device,char *data,int count);
47 static void showregs(void);
49 static void enableremote(unsigned char device);
50 static void gotolocal(unsigned char device);
51 static void menableremote(unsigned char *device);
52 static void mgotolocal(unsigned char *device);
53 static void mtrigger(unsigned char *device);
54 static void trigger(unsigned char device);
55 static char spoll(unsigned char device);
57 static int gpprobe(struct isa_device *dvp);
58 static int gpattach(struct isa_device *dvp);
60 struct isa_driver gpdriver = {gpprobe, gpattach, "gp"};
62 static d_open_t gpopen;
63 static d_close_t gpclose;
64 static d_write_t gpwrite;
65 static d_ioctl_t gpioctl;
68 static struct dev_ops gp_ops = {
69 { "gp", CDEV_MAJOR, 0 },
82 static struct gpib_softc {
83 char *sc_cp; /* current data to send */
84 int sc_count; /* bytes queued in sc_inbuf */
85 int sc_type; /* Type of gpib controller */
86 u_char sc_flags; /* flags (open and internal) */
87 char sc_unit; /* gpib device number */
88 char *sc_inbuf; /* buffer for data */
89 } gpib_sc; /* only support one of these? */
91 static char oldbytes[2];
93 /*This needs to be changed to be a bit more robust*/
95 gpprobe(struct isa_device *dvp)
98 struct gpib_softc *sc = &gpib_sc;
100 gpib_port = dvp->id_iobase;
103 if ((inb(KSR)&0xF7)==0x34) sc->sc_type=3;
104 else if ((inb(KSR)&0xF7)==0x24) sc->sc_type=2;
105 else if ((inb(KSR)&0xF7)==0x14) sc->sc_type=1;
113 * Attach device and print the type of card to the screen.
116 gpattach(struct isa_device *isdp)
118 struct gpib_softc *sc = &gpib_sc;
120 sc->sc_unit = isdp->id_unit;
122 kprintf ("gp%d: type AT-GPIB/TNT\n",sc->sc_unit);
124 kprintf ("gp%d: type AT-GPIB chip NAT4882B\n",sc->sc_unit);
126 kprintf ("gp%d: type AT-GPIB chip NAT4882A\n",sc->sc_unit);
127 sc->sc_flags |=ATTACHED;
129 dev_ops_add(&gp_ops, -1, sc->sc_unit);
130 make_dev(&gp_ops, sc->sc_unit, 0, 0, 0600, "gp");
136 * New open on device.
138 * More than 1 open is not allowed on the entire device.
139 * i.e. even if gpib5 is open, we can't open another minor device
142 gpopen(struct dev_open_args *ap)
144 cdev_t dev = ap->a_head.a_dev;
145 struct gpib_softc *sc = &gpib_sc;
151 /* minor number out of limits ? */
156 if (!(sc->sc_flags&ATTACHED)) { /* not attached */
161 if (sc->sc_flags&OPEN) { /* too late .. */
165 /* Have memory for buffer? */
166 sc->sc_inbuf = kmalloc(BUFSIZE, M_DEVBUF, M_WAITOK);
167 if (sc->sc_inbuf == 0)
170 if (initgpib()) return(EBUSY);
171 sc->sc_flags |= OPEN;
174 if (unit!=0) { /*Someone is trying to access an actual device*/
175 /*So.. we'll address it to listen*/
180 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
182 outb(CDOR,(unit&31)+32);/*address device to listen*/
186 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
187 outb (CDOR,64); /*Address controller (me) to talk*/
190 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
191 outb(AUXMR,gts); /*Set to Standby (Controller)*/
196 while (!(status&2)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
197 /*Set up the TURBO488 registers*/
198 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
199 outb(CNT0,0); /*NOTE this does not enable DMA to the host computer!!*/
204 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
205 outb(CMDR,0x10); /*RESET fifos*/
206 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
217 gpclose(struct dev_close_args *ap)
219 cdev_t dev = ap->a_head.a_dev;
220 struct gpib_softc *sc = &gpib_sc;
222 unsigned char status;
225 if (unit!=0) { /*Here we need to send the last character with EOS*/
226 /*and unaddress the listening device*/
231 /*Wait for fifo to become empty*/
233 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
235 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
237 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
239 /*Wait for DONE and STOP*/
240 if (status==EWOULDBLOCK) do {
241 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
243 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
245 /*Shut down TURBO488 */
246 outb(IMR2,0x00); /*DISABLE DMA to turbo488*/
247 outb(CMDR,0x20); /*soft reset turbo488*/
248 outb(CMDR,0x10); /*reset fifos*/
251 /*Send last byte with EOI set*/
252 /*Send second to last byte if there are 2 bytes left*/
253 if (status==EWOULDBLOCK) {
256 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
257 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
259 outb(CDOR,oldbytes[0]); /*Send second to last byte*/
260 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
261 status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
264 outb(AUXMR,seoi); /*Set EOI for the last byte*/
265 outb(AUXMR,0x5E); /*Clear SYNC*/
267 outb(CDOR,oldbytes[0]);
270 outb(CDOR,oldbytes[1]);
272 outb (CDOR,13); /*Send a CR.. we've got trouble*/
273 kprintf("gpib: Warning: gpclose called with nothing left in buffer\n");
278 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
279 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
282 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
283 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
284 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
287 outb(AUXMR,tca); /* Regain full control of the bus*/
292 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
293 outb(CDOR,63); /*unlisten*/
296 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
297 outb(AUXMR,0x5E); /*Clear SYNC*/
298 outb (CDOR,95);/*untalk*/
301 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
302 /*gotolocal(minor(dev));*/
305 sc->sc_flags = ATTACHED;
306 kfree(sc->sc_inbuf, M_DEVBUF);
307 sc->sc_inbuf = 0; /* Sanity */
313 * Copy from user's buffer, then write to GPIB device referenced
317 gpwrite(struct dev_write_args *ap)
319 cdev_t dev = ap->a_head.a_dev;
320 struct uio *uio = ap->a_uio;
324 while ((gpib_sc.sc_count = MIN(BUFSIZE-1, uio->uio_resid)) > 0) {
325 /* If there were >1 bytes left over, send them */
327 sendrawgpibfifo(minor(dev),oldbytes,2);
329 /*If there was 1 character left, put it at the beginning
332 (gpib_sc.sc_inbuf)[0]=oldbytes[0];
333 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
334 /* get from user-space */
335 uiomove(gpib_sc.sc_inbuf+1, gpib_sc.sc_count, uio);
339 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
340 /* get from user-space */
341 uiomove(gpib_sc.sc_inbuf, gpib_sc.sc_count, uio);
344 /*NOTE we always leave one byte in case this is the last write
345 so close can send EOI with the last byte There may be 2 bytes
346 since we are doing 16 bit transfers.(note the -1 in the count below)*/
347 /*If count<=2 we'll either pick it up on the next write or on close*/
348 if (gpib_sc.sc_count>2) {
349 count = sendrawgpibfifo(minor(dev),gpib_sc.sc_cp,gpib_sc.sc_count-1);
353 oldcount=gpib_sc.sc_count-count; /*Set # of remaining bytes*/
354 gpib_sc.sc_count-=count;
355 gpib_sc.sc_cp+=count; /*point char pointer to remaining bytes*/
357 else oldcount=gpib_sc.sc_count;
358 oldbytes[0]=gpib_sc.sc_cp[0];
360 oldbytes[1]=gpib_sc.sc_cp[1];
364 /* Here is how you would usually access a GPIB device
365 An exception would be a plotter or printer that you can just
366 write to using a minor device = its GPIB address */
369 gpioctl(struct dev_ioctl_args *ap)
371 struct gpibdata *gd = (struct gpibdata *)ap->a_data;
377 sendgpibfifo(gd->address,gd->data,*(gd->count));
381 result=readgpibfifo(gd->address,gd->data,*(gd->count));
390 trigger(gd->address);
394 enableremote(gd->address);
398 gotolocal(gd->address);
407 menableremote(gd->data);
411 mgotolocal(gd->data);
415 *(gd->data)=spoll(gd->address);
429 /*Just in case you want a dump of the registers...*/
431 static void showregs() {
432 kprintf ("NAT4882:\n");
433 kprintf ("ISR1=%X\t",inb(ISR1));
434 kprintf ("ISR2=%X\t",inb(ISR2));
435 kprintf ("SPSR=%X\t",inb(SPSR));
436 kprintf ("KSR =%X\t",inb(KSR));
437 kprintf ("ADSR=%X\t",inb(ADSR));
438 kprintf ("CPTR=%X\t",inb(CPTR));
439 kprintf ("SASR=%X\t",inb(SASR));
440 kprintf ("ADR0=%X\t",inb(ADR0));
441 kprintf ("ISR0=%X\t",inb(ISR0));
442 kprintf ("ADR1=%X\t",inb(ADR1));
443 kprintf ("BSR =%X\n",inb(BSR));
445 kprintf ("Turbo488\n");
446 kprintf ("STS1=%X ",inb(STS1));
447 kprintf ("STS2=%X ",inb(STS2));
448 kprintf ("ISR3=%X ",inb(ISR3));
449 kprintf ("CNT0=%X ",inb(CNT0));
450 kprintf ("CNT1=%X ",inb(CNT1));
451 kprintf ("CNT2=%X ",inb(CNT2));
452 kprintf ("CNT3=%X ",inb(CNT3));
453 kprintf ("IMR3=%X ",inb(IMR3));
454 kprintf ("TIMER=%X\n",inb(TIMER));
459 /*Set up the NAT4882 and TURBO488 registers */
460 /*This will be nonsense to you unless you have a data sheet from
461 National Instruments. They should give you one if you call them*/
473 outb(INTR,0); /* Put interrupt line in tri-state mode??*/
474 outb(AUXMR,chip_reset);
476 outb(IMR1,0x10); /* send interrupt to TURBO488 when END received*/
478 outb(IMR0,0x90); /* Do we want nba here too??? */
488 outb(EOSR,10); /*set EOS message to newline*/
489 /*should I make the default to interpret END as EOS?*/
490 /*It isn't now. The following changes this*/
491 outb(AUXMR,0x80); /*No special EOS handling*/
492 /*outb(AUXMR,0x88) */ /* Transmit END with EOS*/
493 /*outb(AUXMR,0x84) */ /* Set END on EOS received*/
494 /*outb(AUXMR,0x8C) */ /* Do both of the above*/
497 /* outb(AUXMR,hldi); */ /*Perform RFD Holdoff for all data in*/
498 /*Not currently supported*/
502 tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
504 outb(AUXMR,sic_rsc_off);
511 /*This is kind of Brute force.. But it works*/
516 outb(AUXMR,chip_reset);
520 These will also make little sense unless you have a data sheet.
521 Note that the routines with an "m" in the beginning are for
522 accessing multiple devices in one call*/
525 /*This is one thing I could not figure out how to do correctly.
526 I tried to use the auxilary command to enable remote, but it
527 never worked. Here, I bypass everything and write to the BSR
528 to enable the remote line. NOTE that these lines are effectively
529 "OR'ed" with the actual lines, so writing a 1 to the bit in the BSR
530 forces the GPIB line true, no matter what the fancy circuitry of the
531 NAT4882 wants to do with it*/
534 enableremote(unsigned char device)
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*/
544 outb(BSR,1); /*Set REN bit on GPIB*/
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*/
549 outb(CDOR,(device&31)+32); /*address device to listen*/
550 if (status==EWOULDBLOCK) do {
551 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
553 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
554 outb (CDOR,63); /*Unaddress device*/
555 if (status==EWOULDBLOCK) do {
556 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
558 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
561 /*This does not release the REM line on the gpib port, because if it did,
562 all the remote devices would go to local mode. This only sends the
563 gotolocal message to one device. Currently, REM is always held true
564 after enableremote is called, and is reset only on a close of the
568 gotolocal(unsigned char device)
572 if (status==EWOULDBLOCK) do {
573 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
575 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
577 outb(CDOR,(device&31)+32);
579 if (status==EWOULDBLOCK) do {
580 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
582 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
584 outb(AUXMR,0x5E); /*Clear SYNC*/
587 if (status==EWOULDBLOCK) do {
588 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
590 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
593 outb (CDOR,63);/*unaddress device*/
595 if (status==EWOULDBLOCK) do {
596 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
598 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
604 menableremote(unsigned char *device)
606 int status, counter = 0;
609 if (status==EWOULDBLOCK) do {
610 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
612 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
614 outb(BSR,1); /*Set REN bit on GPIB*/
617 if (status==EWOULDBLOCK) do {
618 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
620 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
621 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
624 while (device[counter]<32);
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*/
631 outb (CDOR,63); /*Unaddress device*/
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*/
640 mgotolocal(unsigned char *device)
644 if (device[counter]<32) do {
645 if (status==EWOULDBLOCK) do {
646 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
648 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
649 outb(CDOR,(device[counter]&31)+32);
651 } while (device[counter]<32);
652 if (status==EWOULDBLOCK) do {
653 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
655 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
657 outb(AUXMR,0x5E); /*Clear SYNC*/
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*/
666 outb (CDOR,63);/*unaddress device*/
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*/
674 /*Trigger a device. What happens depends on how the device is
678 trigger(unsigned char device)
683 if (!(inb(ISR2)&0x08)) do {
684 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
686 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
687 outb(CDOR,(device&31)+32); /*address device to listen*/
688 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
689 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
691 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
693 outb (CDOR,8); /*send GET*/
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*/
700 outb (CDOR,63);/*unaddress device*/
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*/
710 /*Trigger multiple devices by addressing them all to listen, and then
714 mtrigger(unsigned char *device)
715 { int status=EWOULDBLOCK;
719 if (device[counter]<32)
720 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
721 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
723 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
724 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
727 while (device[counter]<32);
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*/
732 outb (CDOR,8); /*send GET*/
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*/
739 outb (CDOR,63);/*unaddress device*/
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*/
749 /*This is not used now, but it should work with NI's 8 bit gpib board
750 since it does not use the TURBO488 registers at all */
753 /*Send data through the TURBO488 FIFOS to a device that is already
754 addressed to listen. This is used by the write call when someone is
755 writing to a printer or plotter, etc... */
756 /*The last byte of each write is held off until either the next
757 write or close, so it can be sent with EOI set*/
760 sendrawgpibfifo(unsigned char device,char *data,int count)
776 /*Wait for fifo to become not full if it is full */
778 if (!(inb(ISR3)&0x08)) do {
779 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
780 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
782 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
784 if((count>1)&&(inb(ISR3)&0x08)){
785 outw(FIFOB,*(unsigned*)(data+counter));
786 /* kprintf ("gpib: sent:%c,%c\n",data[counter],data[counter+1]);*/
792 while ((count>1)&&(status==EWOULDBLOCK));
793 /*The write routine and close routine must check if there is 1
794 byte left and handle it accordingly*/
797 /*Return the number of bytes written to the device*/
805 sendgpibfifo(unsigned char device,char *data,int count)
812 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
818 if (!(inb(ISR2)&8)) do
819 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
820 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
822 outb(CDOR,(device&31)+32);/*address device to listen*/
824 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
825 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
826 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
827 outb (CDOR,64); /*Address controller (me) to talk*/
829 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
830 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
831 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
833 outb(AUXMR,gts); /*Set to Standby (Controller)*/
843 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
844 outb(CMDR,0x10); /*RESET fifos*/
845 outb(CCRG,seoi); /*program to send EOI at end*/
846 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
849 /*Wait for fifo to become not full if it is full */
851 if (!(inb(ISR3)&0x08)) do {
852 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
853 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
855 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
857 if((count>1)&&(inb(ISR3)&0x08)){
858 /*if(count==2) outb(CFG,15+0x40); *//*send eoi when done*/
859 outw(FIFOB,*(unsigned*)(data+counter));
865 while ((count>2)&&(status==EWOULDBLOCK));
867 if (count==2&&status==EWOULDBLOCK) {
868 /*Wait for fifo to become not full*/
869 if(status==EWOULDBLOCK&&!(inb(ISR3)&0x08)) do {
870 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",SLEEP_MIN);
872 while (!(inb(ISR3)&0x08)&&status==EWOULDBLOCK); /*Fifo is full*/
873 /*outb(CFG,0x40+15);*//*send eoi when done*/
874 outb(FIFOB,data[counter]);
882 /*Wait for fifo to become empty*/
883 if (status==EWOULDBLOCK) do {
884 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
886 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
888 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
890 /*Wait for DONE and STOP*/
891 if (status==EWOULDBLOCK) do {
892 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
894 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
896 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
897 outb(CMDR,0x20); /*soft reset turbo488*/
898 outb(CMDR,0x10); /*reset fifos*/
901 /*Send last byte with EOI set*/
902 /*Here EOI is handled correctly since the string to be sent */
903 /*is actually all sent during the ioctl. (See above)*/
905 if (count==1&&status==EWOULDBLOCK) { /*Count should always=1 here*/
908 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
909 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
911 outb(AUXMR,seoi); /*Set EOI for the last byte*/
912 outb(AUXMR,0x5E); /*Clear SYNC*/
913 outb(CDOR,data[counter]);
919 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
920 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
923 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
924 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
925 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
926 outb(AUXMR,tca); /* Regain full control of the bus*/
929 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
930 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
931 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
933 outb(CDOR,63); /*unlisten*/
936 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
937 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
938 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
941 outb(AUXMR,0x5E); /*Clear SYNC*/
942 outb (CDOR,95);/*untalk*/
943 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
944 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
945 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
955 readgpibfifo(unsigned char device,char *data,int count)
964 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
969 outb(CFG,14+0x60+1); /* Halt on int,read, fifo B first, CCEN TMOE TIM */
970 outb(CMDR,0x10); /*RESET fifos*/
971 outb(CCRG,tcs); /*program to tcs at end*/
972 outb(CMDR,0x08);/*STOP??*/
978 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
979 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
981 outb (CDOR,32); /*Address controller (me) to listen*/
984 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
985 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
987 outb(CDOR,(device&31)+64);/*address device to talk*/
991 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
992 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
994 outb(AUXMR,gts); /*Set to Standby (Controller)*/
999 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
1004 if (!(status1&0x01)&&(status1&0x04)){
1007 *(unsigned*)(data+counter)=inword;
1008 /* kprintf ("Read:%c,%c\n",data[counter],data[counter+1]);*/
1012 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",4);
1015 while (!(status1&0x01)&&status==EWOULDBLOCK);
1016 if(!(status2 & 0x04)){ /*Only 1 byte came in on last 16 bit transfer*/
1021 outb(CMDR,0x08); /*send STOP*/
1024 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1026 while(!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*wait for DONE and STOP*/
1029 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
1030 outb(CMDR,0x20); /*soft reset turbo488*/
1031 outb(CMDR,0x10); /*reset fifos*/
1034 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1035 while (!(inb(ISR1)&2));*/
1036 outb(AUXMR,tca); /* Regain full control of the bus*/
1040 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1041 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1042 outb(CDOR,63); /*unlisten*/
1045 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1046 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1048 outb(AUXMR,0x5E); /*Clear SYNC*/
1049 outb (CDOR,95);/*untalk*/
1051 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1052 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1060 /* Return the status byte from device */
1062 spoll(unsigned char device)
1064 int status=EWOULDBLOCK;
1065 unsigned int statusbyte;
1067 if (!(inb(ISR2)&8)) do
1068 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1069 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1071 outb(CDOR,(device&31)+64);/*address device to talk*/
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,32); /*Address controller (me) to listen*/
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,0x18); /*Send SPE (serial poll enable)*/
1084 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1085 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1086 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1088 /*wait for bus to be synced*/
1089 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1090 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1091 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1093 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1095 if (!(inb(ISR1)&1)&&status==EWOULDBLOCK) do
1096 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1097 while (!(inb(ISR1)&1)&&status==EWOULDBLOCK);
1099 outb(AUXMR,tcs); /* Take control after next read*/
1100 statusbyte=inb(DIR);
1102 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1103 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1104 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1106 outb(CDOR,0x19); /*SPD (serial poll disable)*/
1108 /*wait for bus to be synced*/
1109 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1110 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1111 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
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,95); /*untalk*/
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,63);/*unlisten*/
1125 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1126 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1127 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1129 /*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);