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.16 2008/08/02 01:14:42 dillon 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/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 make_dev(&gp_ops, sc->sc_unit, 0, 0, 0600, "gp");
135 * New open on device.
137 * More than 1 open is not allowed on the entire device.
138 * i.e. even if gpib5 is open, we can't open another minor device
141 gpopen(struct dev_open_args *ap)
143 cdev_t dev = ap->a_head.a_dev;
144 struct gpib_softc *sc = &gpib_sc;
150 /* minor number out of limits ? */
155 if (!(sc->sc_flags&ATTACHED)) { /* not attached */
160 if (sc->sc_flags&OPEN) { /* too late .. */
164 sc->sc_inbuf = kmalloc(BUFSIZE, M_DEVBUF, M_WAITOK);
166 if (initgpib()) return(EBUSY);
167 sc->sc_flags |= OPEN;
170 if (unit!=0) { /*Someone is trying to access an actual device*/
171 /*So.. we'll address it to listen*/
176 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
178 outb(CDOR,(unit&31)+32);/*address device to listen*/
182 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
183 outb (CDOR,64); /*Address controller (me) to talk*/
186 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
187 outb(AUXMR,gts); /*Set to Standby (Controller)*/
192 while (!(status&2)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
193 /*Set up the TURBO488 registers*/
194 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
195 outb(CNT0,0); /*NOTE this does not enable DMA to the host computer!!*/
200 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
201 outb(CMDR,0x10); /*RESET fifos*/
202 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
213 gpclose(struct dev_close_args *ap)
215 cdev_t dev = ap->a_head.a_dev;
216 struct gpib_softc *sc = &gpib_sc;
218 unsigned char status;
221 if (unit!=0) { /*Here we need to send the last character with EOS*/
222 /*and unaddress the listening device*/
227 /*Wait for fifo to become empty*/
229 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
231 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
233 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
235 /*Wait for DONE and STOP*/
236 if (status==EWOULDBLOCK) do {
237 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
239 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
241 /*Shut down TURBO488 */
242 outb(IMR2,0x00); /*DISABLE DMA to turbo488*/
243 outb(CMDR,0x20); /*soft reset turbo488*/
244 outb(CMDR,0x10); /*reset fifos*/
247 /*Send last byte with EOI set*/
248 /*Send second to last byte if there are 2 bytes left*/
249 if (status==EWOULDBLOCK) {
252 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
253 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
255 outb(CDOR,oldbytes[0]); /*Send second to last byte*/
256 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
257 status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
260 outb(AUXMR,seoi); /*Set EOI for the last byte*/
261 outb(AUXMR,0x5E); /*Clear SYNC*/
263 outb(CDOR,oldbytes[0]);
266 outb(CDOR,oldbytes[1]);
268 outb (CDOR,13); /*Send a CR.. we've got trouble*/
269 kprintf("gpib: Warning: gpclose called with nothing left in buffer\n");
274 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
275 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
278 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
279 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
280 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
283 outb(AUXMR,tca); /* Regain full control of the bus*/
288 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
289 outb(CDOR,63); /*unlisten*/
292 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
293 outb(AUXMR,0x5E); /*Clear SYNC*/
294 outb (CDOR,95);/*untalk*/
297 while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
298 /*gotolocal(minor(dev));*/
301 sc->sc_flags = ATTACHED;
302 kfree(sc->sc_inbuf, M_DEVBUF);
303 sc->sc_inbuf = 0; /* Sanity */
309 * Copy from user's buffer, then write to GPIB device referenced
313 gpwrite(struct dev_write_args *ap)
315 cdev_t dev = ap->a_head.a_dev;
316 struct uio *uio = ap->a_uio;
320 while ((gpib_sc.sc_count = MIN(BUFSIZE-1, uio->uio_resid)) > 0) {
321 /* If there were >1 bytes left over, send them */
323 sendrawgpibfifo(minor(dev),oldbytes,2);
325 /*If there was 1 character left, put it at the beginning
328 (gpib_sc.sc_inbuf)[0]=oldbytes[0];
329 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
330 /* get from user-space */
331 uiomove(gpib_sc.sc_inbuf+1, gpib_sc.sc_count, uio);
335 gpib_sc.sc_cp = gpib_sc.sc_inbuf;
336 /* get from user-space */
337 uiomove(gpib_sc.sc_inbuf, gpib_sc.sc_count, uio);
340 /*NOTE we always leave one byte in case this is the last write
341 so close can send EOI with the last byte There may be 2 bytes
342 since we are doing 16 bit transfers.(note the -1 in the count below)*/
343 /*If count<=2 we'll either pick it up on the next write or on close*/
344 if (gpib_sc.sc_count>2) {
345 count = sendrawgpibfifo(minor(dev),gpib_sc.sc_cp,gpib_sc.sc_count-1);
349 oldcount=gpib_sc.sc_count-count; /*Set # of remaining bytes*/
350 gpib_sc.sc_count-=count;
351 gpib_sc.sc_cp+=count; /*point char pointer to remaining bytes*/
353 else oldcount=gpib_sc.sc_count;
354 oldbytes[0]=gpib_sc.sc_cp[0];
356 oldbytes[1]=gpib_sc.sc_cp[1];
360 /* Here is how you would usually access a GPIB device
361 An exception would be a plotter or printer that you can just
362 write to using a minor device = its GPIB address */
365 gpioctl(struct dev_ioctl_args *ap)
367 struct gpibdata *gd = (struct gpibdata *)ap->a_data;
373 sendgpibfifo(gd->address,gd->data,*(gd->count));
377 result=readgpibfifo(gd->address,gd->data,*(gd->count));
386 trigger(gd->address);
390 enableremote(gd->address);
394 gotolocal(gd->address);
403 menableremote(gd->data);
407 mgotolocal(gd->data);
411 *(gd->data)=spoll(gd->address);
425 /*Just in case you want a dump of the registers...*/
427 static void showregs() {
428 kprintf ("NAT4882:\n");
429 kprintf ("ISR1=%X\t",inb(ISR1));
430 kprintf ("ISR2=%X\t",inb(ISR2));
431 kprintf ("SPSR=%X\t",inb(SPSR));
432 kprintf ("KSR =%X\t",inb(KSR));
433 kprintf ("ADSR=%X\t",inb(ADSR));
434 kprintf ("CPTR=%X\t",inb(CPTR));
435 kprintf ("SASR=%X\t",inb(SASR));
436 kprintf ("ADR0=%X\t",inb(ADR0));
437 kprintf ("ISR0=%X\t",inb(ISR0));
438 kprintf ("ADR1=%X\t",inb(ADR1));
439 kprintf ("BSR =%X\n",inb(BSR));
441 kprintf ("Turbo488\n");
442 kprintf ("STS1=%X ",inb(STS1));
443 kprintf ("STS2=%X ",inb(STS2));
444 kprintf ("ISR3=%X ",inb(ISR3));
445 kprintf ("CNT0=%X ",inb(CNT0));
446 kprintf ("CNT1=%X ",inb(CNT1));
447 kprintf ("CNT2=%X ",inb(CNT2));
448 kprintf ("CNT3=%X ",inb(CNT3));
449 kprintf ("IMR3=%X ",inb(IMR3));
450 kprintf ("TIMER=%X\n",inb(TIMER));
455 /*Set up the NAT4882 and TURBO488 registers */
456 /*This will be nonsense to you unless you have a data sheet from
457 National Instruments. They should give you one if you call them*/
469 outb(INTR,0); /* Put interrupt line in tri-state mode??*/
470 outb(AUXMR,chip_reset);
472 outb(IMR1,0x10); /* send interrupt to TURBO488 when END received*/
474 outb(IMR0,0x90); /* Do we want nba here too??? */
484 outb(EOSR,10); /*set EOS message to newline*/
485 /*should I make the default to interpret END as EOS?*/
486 /*It isn't now. The following changes this*/
487 outb(AUXMR,0x80); /*No special EOS handling*/
488 /*outb(AUXMR,0x88) */ /* Transmit END with EOS*/
489 /*outb(AUXMR,0x84) */ /* Set END on EOS received*/
490 /*outb(AUXMR,0x8C) */ /* Do both of the above*/
493 /* outb(AUXMR,hldi); */ /*Perform RFD Holdoff for all data in*/
494 /*Not currently supported*/
498 tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
500 outb(AUXMR,sic_rsc_off);
507 /*This is kind of Brute force.. But it works*/
512 outb(AUXMR,chip_reset);
516 These will also make little sense unless you have a data sheet.
517 Note that the routines with an "m" in the beginning are for
518 accessing multiple devices in one call*/
521 /*This is one thing I could not figure out how to do correctly.
522 I tried to use the auxilary command to enable remote, but it
523 never worked. Here, I bypass everything and write to the BSR
524 to enable the remote line. NOTE that these lines are effectively
525 "OR'ed" with the actual lines, so writing a 1 to the bit in the BSR
526 forces the GPIB line true, no matter what the fancy circuitry of the
527 NAT4882 wants to do with it*/
530 enableremote(unsigned char device)
535 if (status==EWOULDBLOCK) do {
536 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
538 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
540 outb(BSR,1); /*Set REN bit on GPIB*/
541 if (status==EWOULDBLOCK) do {
542 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
544 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
545 outb(CDOR,(device&31)+32); /*address device to listen*/
546 if (status==EWOULDBLOCK) do {
547 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
549 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
550 outb (CDOR,63); /*Unaddress device*/
551 if (status==EWOULDBLOCK) do {
552 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
554 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
557 /*This does not release the REM line on the gpib port, because if it did,
558 all the remote devices would go to local mode. This only sends the
559 gotolocal message to one device. Currently, REM is always held true
560 after enableremote is called, and is reset only on a close of the
564 gotolocal(unsigned char device)
568 if (status==EWOULDBLOCK) do {
569 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
571 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
573 outb(CDOR,(device&31)+32);
575 if (status==EWOULDBLOCK) do {
576 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
578 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
580 outb(AUXMR,0x5E); /*Clear SYNC*/
583 if (status==EWOULDBLOCK) do {
584 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
586 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
589 outb (CDOR,63);/*unaddress device*/
591 if (status==EWOULDBLOCK) do {
592 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
594 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
600 menableremote(unsigned char *device)
602 int status, counter = 0;
605 if (status==EWOULDBLOCK) do {
606 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
608 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
610 outb(BSR,1); /*Set REN bit on GPIB*/
613 if (status==EWOULDBLOCK) do {
614 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
616 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
617 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
620 while (device[counter]<32);
622 if (status==EWOULDBLOCK) do {
623 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
625 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
627 outb (CDOR,63); /*Unaddress device*/
628 if (status==EWOULDBLOCK) do {
629 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
631 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
636 mgotolocal(unsigned char *device)
640 if (device[counter]<32) do {
641 if (status==EWOULDBLOCK) do {
642 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
644 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
645 outb(CDOR,(device[counter]&31)+32);
647 } while (device[counter]<32);
648 if (status==EWOULDBLOCK) do {
649 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
651 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
653 outb(AUXMR,0x5E); /*Clear SYNC*/
657 if (status==EWOULDBLOCK) do {
658 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
660 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
662 outb (CDOR,63);/*unaddress device*/
663 if (status==EWOULDBLOCK) do {
664 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
666 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
670 /*Trigger a device. What happens depends on how the device is
674 trigger(unsigned char device)
679 if (!(inb(ISR2)&0x08)) do {
680 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
682 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
683 outb(CDOR,(device&31)+32); /*address device to listen*/
684 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
685 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
687 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
689 outb (CDOR,8); /*send GET*/
691 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
692 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
694 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
696 outb (CDOR,63);/*unaddress device*/
697 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
698 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
700 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
706 /*Trigger multiple devices by addressing them all to listen, and then
710 mtrigger(unsigned char *device)
711 { int status=EWOULDBLOCK;
715 if (device[counter]<32)
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*/
720 outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
723 while (device[counter]<32);
724 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
725 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
727 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
728 outb (CDOR,8); /*send GET*/
730 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
731 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
733 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
735 outb (CDOR,63);/*unaddress device*/
736 if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
737 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
739 while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
745 /*This is not used now, but it should work with NI's 8 bit gpib board
746 since it does not use the TURBO488 registers at all */
749 /*Send data through the TURBO488 FIFOS to a device that is already
750 addressed to listen. This is used by the write call when someone is
751 writing to a printer or plotter, etc... */
752 /*The last byte of each write is held off until either the next
753 write or close, so it can be sent with EOI set*/
756 sendrawgpibfifo(unsigned char device,char *data,int count)
772 /*Wait for fifo to become not full if it is full */
774 if (!(inb(ISR3)&0x08)) do {
775 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
776 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
778 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
780 if((count>1)&&(inb(ISR3)&0x08)){
781 outw(FIFOB,*(unsigned*)(data+counter));
782 /* kprintf ("gpib: sent:%c,%c\n",data[counter],data[counter+1]);*/
788 while ((count>1)&&(status==EWOULDBLOCK));
789 /*The write routine and close routine must check if there is 1
790 byte left and handle it accordingly*/
793 /*Return the number of bytes written to the device*/
801 sendgpibfifo(unsigned char device,char *data,int count)
808 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
814 if (!(inb(ISR2)&8)) do
815 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
816 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
818 outb(CDOR,(device&31)+32);/*address device to listen*/
820 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
821 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
822 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
823 outb (CDOR,64); /*Address controller (me) to talk*/
825 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
826 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
827 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
829 outb(AUXMR,gts); /*Set to Standby (Controller)*/
839 outb(CFG,0x47); /* 16 bit, write, fifo B first, TMOE TIM */
840 outb(CMDR,0x10); /*RESET fifos*/
841 outb(CCRG,seoi); /*program to send EOI at end*/
842 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
845 /*Wait for fifo to become not full if it is full */
847 if (!(inb(ISR3)&0x08)) do {
848 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
849 if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
851 while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
853 if((count>1)&&(inb(ISR3)&0x08)){
854 /*if(count==2) outb(CFG,15+0x40); *//*send eoi when done*/
855 outw(FIFOB,*(unsigned*)(data+counter));
861 while ((count>2)&&(status==EWOULDBLOCK));
863 if (count==2&&status==EWOULDBLOCK) {
864 /*Wait for fifo to become not full*/
865 if(status==EWOULDBLOCK&&!(inb(ISR3)&0x08)) do {
866 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",SLEEP_MIN);
868 while (!(inb(ISR3)&0x08)&&status==EWOULDBLOCK); /*Fifo is full*/
869 /*outb(CFG,0x40+15);*//*send eoi when done*/
870 outb(FIFOB,data[counter]);
878 /*Wait for fifo to become empty*/
879 if (status==EWOULDBLOCK) do {
880 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
882 while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
884 outb(CMDR,0x08); /*Issue STOP to TURBO488*/
886 /*Wait for DONE and STOP*/
887 if (status==EWOULDBLOCK) do {
888 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
890 while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
892 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
893 outb(CMDR,0x20); /*soft reset turbo488*/
894 outb(CMDR,0x10); /*reset fifos*/
897 /*Send last byte with EOI set*/
898 /*Here EOI is handled correctly since the string to be sent */
899 /*is actually all sent during the ioctl. (See above)*/
901 if (count==1&&status==EWOULDBLOCK) { /*Count should always=1 here*/
904 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
905 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
907 outb(AUXMR,seoi); /*Set EOI for the last byte*/
908 outb(AUXMR,0x5E); /*Clear SYNC*/
909 outb(CDOR,data[counter]);
915 if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
916 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
919 if (!(inb(ISR1)&2)&&status==EWOULDBLOCK) do
920 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
921 while (!(inb(ISR1)&2)&&status==EWOULDBLOCK);
922 outb(AUXMR,tca); /* Regain full control of the bus*/
925 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
926 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
927 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
929 outb(CDOR,63); /*unlisten*/
932 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
933 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
934 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
937 outb(AUXMR,0x5E); /*Clear SYNC*/
938 outb (CDOR,95);/*untalk*/
939 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
940 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
941 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
951 readgpibfifo(unsigned char device,char *data,int count)
960 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
965 outb(CFG,14+0x60+1); /* Halt on int,read, fifo B first, CCEN TMOE TIM */
966 outb(CMDR,0x10); /*RESET fifos*/
967 outb(CCRG,tcs); /*program to tcs at end*/
968 outb(CMDR,0x08);/*STOP??*/
974 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
975 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
977 outb (CDOR,32); /*Address controller (me) to listen*/
980 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
981 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
983 outb(CDOR,(device&31)+64);/*address device to talk*/
987 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
988 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
990 outb(AUXMR,gts); /*Set to Standby (Controller)*/
995 outb(CMDR,0x04); /*Tell TURBO488 to GO*/
1000 if (!(status1&0x01)&&(status1&0x04)){
1003 *(unsigned*)(data+counter)=inword;
1004 /* kprintf ("Read:%c,%c\n",data[counter],data[counter+1]);*/
1008 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",4);
1011 while (!(status1&0x01)&&status==EWOULDBLOCK);
1012 if(!(status2 & 0x04)){ /*Only 1 byte came in on last 16 bit transfer*/
1017 outb(CMDR,0x08); /*send STOP*/
1020 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1022 while(!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*wait for DONE and STOP*/
1025 outb(IMR2,0x00); /*we have to enable DMA (0x30) for turbo488 to work*/
1026 outb(CMDR,0x20); /*soft reset turbo488*/
1027 outb(CMDR,0x10); /*reset fifos*/
1030 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1031 while (!(inb(ISR1)&2));*/
1032 outb(AUXMR,tca); /* Regain full control of the bus*/
1036 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1037 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1038 outb(CDOR,63); /*unlisten*/
1041 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1042 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1044 outb(AUXMR,0x5E); /*Clear SYNC*/
1045 outb (CDOR,95);/*untalk*/
1047 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1048 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1056 /* Return the status byte from device */
1058 spoll(unsigned char device)
1060 int status=EWOULDBLOCK;
1061 unsigned int statusbyte;
1063 if (!(inb(ISR2)&8)) do
1064 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1065 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1067 outb(CDOR,(device&31)+64);/*address device to talk*/
1069 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1070 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1071 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1073 outb (CDOR,32); /*Address controller (me) to listen*/
1075 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1076 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1077 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1079 outb (CDOR,0x18); /*Send SPE (serial poll enable)*/
1080 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1081 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1082 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1084 /*wait for bus to be synced*/
1085 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1086 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1087 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1089 outb(AUXMR,gts); /*Set to Standby (Controller)*/
1091 if (!(inb(ISR1)&1)&&status==EWOULDBLOCK) do
1092 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1093 while (!(inb(ISR1)&1)&&status==EWOULDBLOCK);
1095 outb(AUXMR,tcs); /* Take control after next read*/
1096 statusbyte=inb(DIR);
1098 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1099 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1100 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1102 outb(CDOR,0x19); /*SPD (serial poll disable)*/
1104 /*wait for bus to be synced*/
1105 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1106 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1107 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);
1110 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1111 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1112 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1114 outb(CDOR,95); /*untalk*/
1116 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1117 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1118 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1120 outb (CDOR,63);/*unlisten*/
1121 if (!(inb(ISR2)&8)&&status==EWOULDBLOCK) do
1122 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1123 while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1125 /*wait for bus to be synced*/
1126 if (!(inb(ISR0)&1)&&status==EWOULDBLOCK) do
1127 status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1128 while (!(inb(ISR0)&1)&&status==EWOULDBLOCK);