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.15 2008/01/06 16:55:50 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 sc
->sc_inbuf
= kmalloc(BUFSIZE
, M_DEVBUF
, M_WAITOK
);
167 if (initgpib()) return(EBUSY
);
168 sc
->sc_flags
|= OPEN
;
171 if (unit
!=0) { /*Someone is trying to access an actual device*/
172 /*So.. we'll address it to listen*/
177 while (!(status
&8)&&tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1)==EWOULDBLOCK
);
179 outb(CDOR
,(unit
&31)+32);/*address device to listen*/
183 while (!(status
&8)&&tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1)==EWOULDBLOCK
);
184 outb (CDOR
,64); /*Address controller (me) to talk*/
187 while (!(status
&8)&&tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1)==EWOULDBLOCK
);
188 outb(AUXMR
,gts
); /*Set to Standby (Controller)*/
193 while (!(status
&2)&&tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1)==EWOULDBLOCK
);
194 /*Set up the TURBO488 registers*/
195 outb(IMR2
,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
196 outb(CNT0
,0); /*NOTE this does not enable DMA to the host computer!!*/
201 outb(CFG
,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
202 outb(CMDR
,0x10); /*RESET fifos*/
203 outb(CMDR
,0x04); /*Tell TURBO488 to GO*/
214 gpclose(struct dev_close_args
*ap
)
216 cdev_t dev
= ap
->a_head
.a_dev
;
217 struct gpib_softc
*sc
= &gpib_sc
;
219 unsigned char status
;
222 if (unit
!=0) { /*Here we need to send the last character with EOS*/
223 /*and unaddress the listening device*/
228 /*Wait for fifo to become empty*/
230 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
232 while ((inb(ISR3
)&0x04)&&status
==EWOULDBLOCK
); /*Fifo is not empty*/
234 outb(CMDR
,0x08); /*Issue STOP to TURBO488*/
236 /*Wait for DONE and STOP*/
237 if (status
==EWOULDBLOCK
) do {
238 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
240 while (!(inb(ISR3
)&0x11)&&status
==EWOULDBLOCK
); /*not done and stop*/
242 /*Shut down TURBO488 */
243 outb(IMR2
,0x00); /*DISABLE DMA to turbo488*/
244 outb(CMDR
,0x20); /*soft reset turbo488*/
245 outb(CMDR
,0x10); /*reset fifos*/
248 /*Send last byte with EOI set*/
249 /*Send second to last byte if there are 2 bytes left*/
250 if (status
==EWOULDBLOCK
) {
253 if (!(inb(ISR1
)&2)) status
=tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1);
254 while (!(inb(ISR1
)&2)&&(status
==EWOULDBLOCK
));
256 outb(CDOR
,oldbytes
[0]); /*Send second to last byte*/
257 while (!(inb(ISR1
)&2)&&(status
==EWOULDBLOCK
));
258 status
=tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1);
261 outb(AUXMR
,seoi
); /*Set EOI for the last byte*/
262 outb(AUXMR
,0x5E); /*Clear SYNC*/
264 outb(CDOR
,oldbytes
[0]);
267 outb(CDOR
,oldbytes
[1]);
269 outb (CDOR
,13); /*Send a CR.. we've got trouble*/
270 kprintf("gpib: Warning: gpclose called with nothing left in buffer\n");
275 if (!(inb(ISR1
)&2)) status
=tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1);
276 while (!(inb(ISR1
)&2)&&(status
==EWOULDBLOCK
));
279 if (!(inb(ISR1
)&2)&&status
==EWOULDBLOCK
) do
280 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
281 while (!(inb(ISR1
)&2)&&status
==EWOULDBLOCK
);
284 outb(AUXMR
,tca
); /* Regain full control of the bus*/
289 while (!(status
&8)&&tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1)==EWOULDBLOCK
);
290 outb(CDOR
,63); /*unlisten*/
293 while (!(status
&8)&&tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1)==EWOULDBLOCK
);
294 outb(AUXMR
,0x5E); /*Clear SYNC*/
295 outb (CDOR
,95);/*untalk*/
298 while (!(status
&8)&&tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1)==EWOULDBLOCK
);
299 /*gotolocal(minor(dev));*/
302 sc
->sc_flags
= ATTACHED
;
303 kfree(sc
->sc_inbuf
, M_DEVBUF
);
304 sc
->sc_inbuf
= 0; /* Sanity */
310 * Copy from user's buffer, then write to GPIB device referenced
314 gpwrite(struct dev_write_args
*ap
)
316 cdev_t dev
= ap
->a_head
.a_dev
;
317 struct uio
*uio
= ap
->a_uio
;
321 while ((gpib_sc
.sc_count
= MIN(BUFSIZE
-1, uio
->uio_resid
)) > 0) {
322 /* If there were >1 bytes left over, send them */
324 sendrawgpibfifo(minor(dev
),oldbytes
,2);
326 /*If there was 1 character left, put it at the beginning
329 (gpib_sc
.sc_inbuf
)[0]=oldbytes
[0];
330 gpib_sc
.sc_cp
= gpib_sc
.sc_inbuf
;
331 /* get from user-space */
332 uiomove(gpib_sc
.sc_inbuf
+1, gpib_sc
.sc_count
, uio
);
336 gpib_sc
.sc_cp
= gpib_sc
.sc_inbuf
;
337 /* get from user-space */
338 uiomove(gpib_sc
.sc_inbuf
, gpib_sc
.sc_count
, uio
);
341 /*NOTE we always leave one byte in case this is the last write
342 so close can send EOI with the last byte There may be 2 bytes
343 since we are doing 16 bit transfers.(note the -1 in the count below)*/
344 /*If count<=2 we'll either pick it up on the next write or on close*/
345 if (gpib_sc
.sc_count
>2) {
346 count
= sendrawgpibfifo(minor(dev
),gpib_sc
.sc_cp
,gpib_sc
.sc_count
-1);
350 oldcount
=gpib_sc
.sc_count
-count
; /*Set # of remaining bytes*/
351 gpib_sc
.sc_count
-=count
;
352 gpib_sc
.sc_cp
+=count
; /*point char pointer to remaining bytes*/
354 else oldcount
=gpib_sc
.sc_count
;
355 oldbytes
[0]=gpib_sc
.sc_cp
[0];
357 oldbytes
[1]=gpib_sc
.sc_cp
[1];
361 /* Here is how you would usually access a GPIB device
362 An exception would be a plotter or printer that you can just
363 write to using a minor device = its GPIB address */
366 gpioctl(struct dev_ioctl_args
*ap
)
368 struct gpibdata
*gd
= (struct gpibdata
*)ap
->a_data
;
374 sendgpibfifo(gd
->address
,gd
->data
,*(gd
->count
));
378 result
=readgpibfifo(gd
->address
,gd
->data
,*(gd
->count
));
387 trigger(gd
->address
);
391 enableremote(gd
->address
);
395 gotolocal(gd
->address
);
404 menableremote(gd
->data
);
408 mgotolocal(gd
->data
);
412 *(gd
->data
)=spoll(gd
->address
);
426 /*Just in case you want a dump of the registers...*/
428 static void showregs() {
429 kprintf ("NAT4882:\n");
430 kprintf ("ISR1=%X\t",inb(ISR1
));
431 kprintf ("ISR2=%X\t",inb(ISR2
));
432 kprintf ("SPSR=%X\t",inb(SPSR
));
433 kprintf ("KSR =%X\t",inb(KSR
));
434 kprintf ("ADSR=%X\t",inb(ADSR
));
435 kprintf ("CPTR=%X\t",inb(CPTR
));
436 kprintf ("SASR=%X\t",inb(SASR
));
437 kprintf ("ADR0=%X\t",inb(ADR0
));
438 kprintf ("ISR0=%X\t",inb(ISR0
));
439 kprintf ("ADR1=%X\t",inb(ADR1
));
440 kprintf ("BSR =%X\n",inb(BSR
));
442 kprintf ("Turbo488\n");
443 kprintf ("STS1=%X ",inb(STS1
));
444 kprintf ("STS2=%X ",inb(STS2
));
445 kprintf ("ISR3=%X ",inb(ISR3
));
446 kprintf ("CNT0=%X ",inb(CNT0
));
447 kprintf ("CNT1=%X ",inb(CNT1
));
448 kprintf ("CNT2=%X ",inb(CNT2
));
449 kprintf ("CNT3=%X ",inb(CNT3
));
450 kprintf ("IMR3=%X ",inb(IMR3
));
451 kprintf ("TIMER=%X\n",inb(TIMER
));
456 /*Set up the NAT4882 and TURBO488 registers */
457 /*This will be nonsense to you unless you have a data sheet from
458 National Instruments. They should give you one if you call them*/
470 outb(INTR
,0); /* Put interrupt line in tri-state mode??*/
471 outb(AUXMR
,chip_reset
);
473 outb(IMR1
,0x10); /* send interrupt to TURBO488 when END received*/
475 outb(IMR0
,0x90); /* Do we want nba here too??? */
485 outb(EOSR
,10); /*set EOS message to newline*/
486 /*should I make the default to interpret END as EOS?*/
487 /*It isn't now. The following changes this*/
488 outb(AUXMR
,0x80); /*No special EOS handling*/
489 /*outb(AUXMR,0x88) */ /* Transmit END with EOS*/
490 /*outb(AUXMR,0x84) */ /* Set END on EOS received*/
491 /*outb(AUXMR,0x8C) */ /* Do both of the above*/
494 /* outb(AUXMR,hldi); */ /*Perform RFD Holdoff for all data in*/
495 /*Not currently supported*/
499 tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
501 outb(AUXMR
,sic_rsc_off
);
508 /*This is kind of Brute force.. But it works*/
513 outb(AUXMR
,chip_reset
);
517 These will also make little sense unless you have a data sheet.
518 Note that the routines with an "m" in the beginning are for
519 accessing multiple devices in one call*/
522 /*This is one thing I could not figure out how to do correctly.
523 I tried to use the auxilary command to enable remote, but it
524 never worked. Here, I bypass everything and write to the BSR
525 to enable the remote line. NOTE that these lines are effectively
526 "OR'ed" with the actual lines, so writing a 1 to the bit in the BSR
527 forces the GPIB line true, no matter what the fancy circuitry of the
528 NAT4882 wants to do with it*/
531 enableremote(unsigned char device
)
536 if (status
==EWOULDBLOCK
) do {
537 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
539 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
541 outb(BSR
,1); /*Set REN bit on GPIB*/
542 if (status
==EWOULDBLOCK
) do {
543 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
545 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
546 outb(CDOR
,(device
&31)+32); /*address device to listen*/
547 if (status
==EWOULDBLOCK
) do {
548 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
550 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
551 outb (CDOR
,63); /*Unaddress device*/
552 if (status
==EWOULDBLOCK
) do {
553 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
555 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
558 /*This does not release the REM line on the gpib port, because if it did,
559 all the remote devices would go to local mode. This only sends the
560 gotolocal message to one device. Currently, REM is always held true
561 after enableremote is called, and is reset only on a close of the
565 gotolocal(unsigned char device
)
569 if (status
==EWOULDBLOCK
) do {
570 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
572 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
574 outb(CDOR
,(device
&31)+32);
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*/
581 outb(AUXMR
,0x5E); /*Clear SYNC*/
584 if (status
==EWOULDBLOCK
) do {
585 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
587 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
590 outb (CDOR
,63);/*unaddress device*/
592 if (status
==EWOULDBLOCK
) do {
593 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
595 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
601 menableremote(unsigned char *device
)
603 int status
, counter
= 0;
606 if (status
==EWOULDBLOCK
) do {
607 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
609 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
611 outb(BSR
,1); /*Set REN bit on GPIB*/
614 if (status
==EWOULDBLOCK
) do {
615 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
617 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
618 outb(CDOR
,(device
[counter
]&31)+32); /*address device to listen*/
621 while (device
[counter
]<32);
623 if (status
==EWOULDBLOCK
) do {
624 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
626 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
628 outb (CDOR
,63); /*Unaddress device*/
629 if (status
==EWOULDBLOCK
) do {
630 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
632 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
637 mgotolocal(unsigned char *device
)
641 if (device
[counter
]<32) do {
642 if (status
==EWOULDBLOCK
) do {
643 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
645 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
646 outb(CDOR
,(device
[counter
]&31)+32);
648 } while (device
[counter
]<32);
649 if (status
==EWOULDBLOCK
) do {
650 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
652 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
654 outb(AUXMR
,0x5E); /*Clear SYNC*/
658 if (status
==EWOULDBLOCK
) do {
659 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
661 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
663 outb (CDOR
,63);/*unaddress device*/
664 if (status
==EWOULDBLOCK
) do {
665 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",2);
667 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
671 /*Trigger a device. What happens depends on how the device is
675 trigger(unsigned char device
)
680 if (!(inb(ISR2
)&0x08)) do {
681 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
683 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
684 outb(CDOR
,(device
&31)+32); /*address device to listen*/
685 if (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
) do {
686 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
688 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
690 outb (CDOR
,8); /*send GET*/
692 if (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
) do {
693 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
695 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
697 outb (CDOR
,63);/*unaddress device*/
698 if (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
) do {
699 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
701 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
707 /*Trigger multiple devices by addressing them all to listen, and then
711 mtrigger(unsigned char *device
)
712 { int status
=EWOULDBLOCK
;
716 if (device
[counter
]<32)
717 if (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
) do {
718 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
720 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
721 outb(CDOR
,(device
[counter
]&31)+32); /*address device to listen*/
724 while (device
[counter
]<32);
725 if (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
) do {
726 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
728 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
729 outb (CDOR
,8); /*send GET*/
731 if (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
) do {
732 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
734 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
736 outb (CDOR
,63);/*unaddress device*/
737 if (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
) do {
738 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
740 while (!(inb(ISR2
)&0x08)&&status
==EWOULDBLOCK
); /*Wait to send next cmd*/
746 /*This is not used now, but it should work with NI's 8 bit gpib board
747 since it does not use the TURBO488 registers at all */
750 /*Send data through the TURBO488 FIFOS to a device that is already
751 addressed to listen. This is used by the write call when someone is
752 writing to a printer or plotter, etc... */
753 /*The last byte of each write is held off until either the next
754 write or close, so it can be sent with EOI set*/
757 sendrawgpibfifo(unsigned char device
,char *data
,int count
)
773 /*Wait for fifo to become not full if it is full */
775 if (!(inb(ISR3
)&0x08)) do {
776 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",sleeptime
);
777 if (sleeptime
<SLEEP_MAX
) sleeptime
=sleeptime
*2;
779 while (!(inb(ISR3
)&0x08)&&(status
==EWOULDBLOCK
)); /*Fifo is full*/
781 if((count
>1)&&(inb(ISR3
)&0x08)){
782 outw(FIFOB
,*(unsigned*)(data
+counter
));
783 /* kprintf ("gpib: sent:%c,%c\n",data[counter],data[counter+1]);*/
789 while ((count
>1)&&(status
==EWOULDBLOCK
));
790 /*The write routine and close routine must check if there is 1
791 byte left and handle it accordingly*/
794 /*Return the number of bytes written to the device*/
802 sendgpibfifo(unsigned char device
,char *data
,int count
)
809 outb(IMR2
,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
815 if (!(inb(ISR2
)&8)) do
816 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
817 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
819 outb(CDOR
,(device
&31)+32);/*address device to listen*/
821 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
822 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
823 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
824 outb (CDOR
,64); /*Address controller (me) to talk*/
826 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
827 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
828 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
830 outb(AUXMR
,gts
); /*Set to Standby (Controller)*/
840 outb(CFG
,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
841 outb(CMDR
,0x10); /*RESET fifos*/
842 outb(CCRG
,seoi
); /*program to send EOI at end*/
843 outb(CMDR
,0x04); /*Tell TURBO488 to GO*/
846 /*Wait for fifo to become not full if it is full */
848 if (!(inb(ISR3
)&0x08)) do {
849 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",sleeptime
);
850 if (sleeptime
<SLEEP_MAX
) sleeptime
=sleeptime
*2;
852 while (!(inb(ISR3
)&0x08)&&(status
==EWOULDBLOCK
)); /*Fifo is full*/
854 if((count
>1)&&(inb(ISR3
)&0x08)){
855 /*if(count==2) outb(CFG,15+0x40); *//*send eoi when done*/
856 outw(FIFOB
,*(unsigned*)(data
+counter
));
862 while ((count
>2)&&(status
==EWOULDBLOCK
));
864 if (count
==2&&status
==EWOULDBLOCK
) {
865 /*Wait for fifo to become not full*/
866 if(status
==EWOULDBLOCK
&&!(inb(ISR3
)&0x08)) do {
867 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",SLEEP_MIN
);
869 while (!(inb(ISR3
)&0x08)&&status
==EWOULDBLOCK
); /*Fifo is full*/
870 /*outb(CFG,0x40+15);*//*send eoi when done*/
871 outb(FIFOB
,data
[counter
]);
879 /*Wait for fifo to become empty*/
880 if (status
==EWOULDBLOCK
) do {
881 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
883 while ((inb(ISR3
)&0x04)&&status
==EWOULDBLOCK
); /*Fifo is not empty*/
885 outb(CMDR
,0x08); /*Issue STOP to TURBO488*/
887 /*Wait for DONE and STOP*/
888 if (status
==EWOULDBLOCK
) do {
889 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
891 while (!(inb(ISR3
)&0x11)&&status
==EWOULDBLOCK
); /*not done and stop*/
893 outb(IMR2
,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
894 outb(CMDR
,0x20); /*soft reset turbo488*/
895 outb(CMDR
,0x10); /*reset fifos*/
898 /*Send last byte with EOI set*/
899 /*Here EOI is handled correctly since the string to be sent */
900 /*is actually all sent during the ioctl. (See above)*/
902 if (count
==1&&status
==EWOULDBLOCK
) { /*Count should always=1 here*/
905 if (!(inb(ISR1
)&2)) status
=tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1);
906 while (!(inb(ISR1
)&2)&&(status
==EWOULDBLOCK
));
908 outb(AUXMR
,seoi
); /*Set EOI for the last byte*/
909 outb(AUXMR
,0x5E); /*Clear SYNC*/
910 outb(CDOR
,data
[counter
]);
916 if (!(inb(ISR1
)&2)) status
=tsleep((caddr_t
)&gpib_sc
, GPIBPRI
,"gpibpoll",1);
917 while (!(inb(ISR1
)&2)&&(status
==EWOULDBLOCK
));
920 if (!(inb(ISR1
)&2)&&status
==EWOULDBLOCK
) do
921 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
922 while (!(inb(ISR1
)&2)&&status
==EWOULDBLOCK
);
923 outb(AUXMR
,tca
); /* Regain full control of the bus*/
926 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
927 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
928 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
930 outb(CDOR
,63); /*unlisten*/
933 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
934 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
935 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
938 outb(AUXMR
,0x5E); /*Clear SYNC*/
939 outb (CDOR
,95);/*untalk*/
940 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
941 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
942 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
952 readgpibfifo(unsigned char device
,char *data
,int count
)
961 outb(IMR2
,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
966 outb(CFG
,14+0x60+1); /* Halt on int,read, fifo B first, CCEN TMOE TIM */
967 outb(CMDR
,0x10); /*RESET fifos*/
968 outb(CCRG
,tcs
); /*program to tcs at end*/
969 outb(CMDR
,0x08);/*STOP??*/
975 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
976 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
978 outb (CDOR
,32); /*Address controller (me) to listen*/
981 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
982 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
984 outb(CDOR
,(device
&31)+64);/*address device to talk*/
988 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
989 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
991 outb(AUXMR
,gts
); /*Set to Standby (Controller)*/
996 outb(CMDR
,0x04); /*Tell TURBO488 to GO*/
1001 if (!(status1
&0x01)&&(status1
&0x04)){
1004 *(unsigned*)(data
+counter
)=inword
;
1005 /* kprintf ("Read:%c,%c\n",data[counter],data[counter+1]);*/
1009 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",4);
1012 while (!(status1
&0x01)&&status
==EWOULDBLOCK
);
1013 if(!(status2
& 0x04)){ /*Only 1 byte came in on last 16 bit transfer*/
1018 outb(CMDR
,0x08); /*send STOP*/
1021 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1023 while(!(inb(ISR3
)&0x11)&&status
==EWOULDBLOCK
); /*wait for DONE and STOP*/
1026 outb(IMR2
,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
1027 outb(CMDR
,0x20); /*soft reset turbo488*/
1028 outb(CMDR
,0x10); /*reset fifos*/
1031 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1032 while (!(inb(ISR1)&2));*/
1033 outb(AUXMR
,tca
); /* Regain full control of the bus*/
1037 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1038 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
1039 outb(CDOR
,63); /*unlisten*/
1042 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1043 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
1045 outb(AUXMR
,0x5E); /*Clear SYNC*/
1046 outb (CDOR
,95);/*untalk*/
1048 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1049 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
1057 /* Return the status byte from device */
1059 spoll(unsigned char device
)
1061 int status
=EWOULDBLOCK
;
1062 unsigned int statusbyte
;
1064 if (!(inb(ISR2
)&8)) do
1065 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1066 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
1068 outb(CDOR
,(device
&31)+64);/*address device to talk*/
1070 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
1071 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1072 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
1074 outb (CDOR
,32); /*Address controller (me) to listen*/
1076 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
1077 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1078 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
1080 outb (CDOR
,0x18); /*Send SPE (serial poll enable)*/
1081 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
1082 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1083 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
1085 /*wait for bus to be synced*/
1086 if (!(inb(ISR0
)&1)&&status
==EWOULDBLOCK
) do
1087 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1088 while (!(inb(ISR0
)&1)&&status
==EWOULDBLOCK
);
1090 outb(AUXMR
,gts
); /*Set to Standby (Controller)*/
1092 if (!(inb(ISR1
)&1)&&status
==EWOULDBLOCK
) do
1093 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1094 while (!(inb(ISR1
)&1)&&status
==EWOULDBLOCK
);
1096 outb(AUXMR
,tcs
); /* Take control after next read*/
1097 statusbyte
=inb(DIR);
1099 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
1100 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1101 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
1103 outb(CDOR
,0x19); /*SPD (serial poll disable)*/
1105 /*wait for bus to be synced*/
1106 if (!(inb(ISR0
)&1)&&status
==EWOULDBLOCK
) do
1107 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1108 while (!(inb(ISR0
)&1)&&status
==EWOULDBLOCK
);
1111 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
1112 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1113 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
1115 outb(CDOR
,95); /*untalk*/
1117 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
1118 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1119 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
1121 outb (CDOR
,63);/*unlisten*/
1122 if (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
) do
1123 status
=tsleep((caddr_t
)&gpib_sc
,GPIBPRI
,"gpibpoll",1);
1124 while (!(inb(ISR2
)&8)&&status
==EWOULDBLOCK
);
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
);