Merge from vendor branch BSDINSTALLER:
[dragonfly.git] / sys / dev / misc / gpib / gpib.c
1
2 /*
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)
7  *
8  * Written by Fred Cawthorne (fcawth@delphi.umd.edu)
9  * Some sections were based partly on the lpt driver.
10  *  (some remnants may remain)
11  *
12  * This software is distributed with NO WARRANTIES, not even the implied
13  * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  *
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.
18  *
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 $
21  *
22  */
23 /*Please read the README file for usage information*/
24
25 #include <sys/param.h>
26 #include <sys/systm.h>
27 #include <sys/conf.h>
28 #include <sys/device.h>
29 #include <sys/uio.h>
30 #include <sys/malloc.h>
31 #include "gpibreg.h"
32 #include "gpib.h"
33 #include <bus/isa/i386/isa_device.h>
34
35 #define GPIBPRI  PCATCH
36 #define SLEEP_MAX 1000
37 #define SLEEP_MIN 4
38
39
40
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);
46 #if 0
47 static void showregs(void);
48 #endif
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);
56
57 static int gpprobe(struct isa_device *dvp);
58 static int gpattach(struct isa_device *dvp);
59
60 struct   isa_driver gpdriver = {gpprobe, gpattach, "gp"};
61
62 static  d_open_t        gpopen;
63 static  d_close_t       gpclose;
64 static  d_write_t       gpwrite;
65 static  d_ioctl_t       gpioctl;
66
67 #define CDEV_MAJOR 44
68 static struct dev_ops gp_ops = {
69         { "gp", CDEV_MAJOR, 0 },
70         .d_open =       gpopen,
71         .d_close =      gpclose,
72         .d_write =      gpwrite,
73         .d_ioctl =      gpioctl,
74 };
75
76 #define   BUFSIZE      1024
77 #define   ATTACHED     0x08
78 #define   OPEN         0x04
79 #define   INIT         0x02
80
81
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? */
90 static int oldcount;
91 static char oldbytes[2];
92 /*Probe routine*/
93 /*This needs to be changed to be a bit more robust*/
94 static int
95 gpprobe(struct isa_device *dvp)
96 {
97         int     status;
98         struct gpib_softc *sc = &gpib_sc;
99
100         gpib_port = dvp->id_iobase;
101         status=1;
102         sc->sc_type=3;
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;
106           else status=0;
107
108         return (status);
109 }
110
111 /*
112  * gpattach()
113  *  Attach device and print the type of card to the screen.
114  */
115 static int
116 gpattach(struct isa_device *isdp)
117 {
118         struct   gpib_softc   *sc = &gpib_sc;
119
120         sc->sc_unit = isdp->id_unit;
121         if (sc->sc_type==3)
122            kprintf ("gp%d: type AT-GPIB/TNT\n",sc->sc_unit);
123         if (sc->sc_type==2)
124            kprintf ("gp%d: type AT-GPIB chip NAT4882B\n",sc->sc_unit);
125         if (sc->sc_type==1)
126            kprintf ("gp%d: type AT-GPIB chip NAT4882A\n",sc->sc_unit);
127         sc->sc_flags |=ATTACHED;
128
129         dev_ops_add(&gp_ops, -1, sc->sc_unit);
130         make_dev(&gp_ops, sc->sc_unit, 0, 0, 0600, "gp");
131         return (1);
132 }
133
134 /*
135  * gpopen()
136  *      New open on device.
137  *
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
140  */
141 static  int
142 gpopen(struct dev_open_args *ap)
143 {
144         cdev_t dev = ap->a_head.a_dev;
145         struct gpib_softc *sc = &gpib_sc;
146         u_char unit;
147         int status;
148
149        unit= minor(dev);
150
151         /* minor number out of limits ? */
152         if (unit >= 32)
153                 return (ENXIO);
154
155         /* Attached ? */
156         if (!(sc->sc_flags&ATTACHED)) { /* not attached */
157                 return(ENXIO);
158         }
159
160         /* Already open  */
161         if (sc->sc_flags&OPEN) { /* too late .. */
162                 return(EBUSY);
163         }
164
165         sc->sc_inbuf = kmalloc(BUFSIZE, M_DEVBUF, M_WAITOK);
166
167         if (initgpib()) return(EBUSY);
168         sc->sc_flags |= OPEN;
169         sc->sc_count = 0;
170         oldcount=0;
171 if (unit!=0) {  /*Someone is trying to access an actual device*/
172                 /*So.. we'll address it to listen*/
173 enableremote(unit);
174  do {
175  status=inb(ISR2);
176  }
177  while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
178
179  outb(CDOR,(unit&31)+32);/*address device to listen*/
180
181  do
182  status=inb(ISR2);
183  while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
184  outb (CDOR,64); /*Address controller (me) to talk*/
185  do status=inb(ISR2);
186
187  while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
188  outb(AUXMR,gts); /*Set to Standby (Controller)*/
189
190
191  do
192  status=inb(ISR1);
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!!*/
197  outb(CNT1,0);
198  outb(CNT2,0);
199  outb(CNT3,0);
200  outb(CMDR,0x20);
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*/
204 }
205         return(0);
206 }
207
208
209 /*
210  * gpclose()
211  *      Close gpib device.
212  */
213 static  int
214 gpclose(struct dev_close_args *ap)
215 {
216         cdev_t dev = ap->a_head.a_dev;
217         struct gpib_softc *sc = &gpib_sc;
218         unsigned char unit;
219         unsigned char status;
220
221         unit=minor(dev);
222 if (unit!=0) { /*Here we need to send the last character with EOS*/
223                /*and unaddress the listening device*/
224
225
226   status=EWOULDBLOCK;
227
228   /*Wait for fifo to become empty*/
229   do {
230   status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
231    }
232   while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
233
234  outb(CMDR,0x08); /*Issue STOP to TURBO488*/
235
236   /*Wait for DONE and STOP*/
237  if (status==EWOULDBLOCK) do {
238    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
239    }
240   while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
241
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*/
246
247
248 /*Send last byte with EOI set*/
249 /*Send second to last byte if there are 2 bytes left*/
250 if (status==EWOULDBLOCK)  {
251
252 do
253  if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
254 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
255 if (oldcount==2){
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);
259  }
260
261    outb(AUXMR,seoi);  /*Set EOI for the last byte*/
262    outb(AUXMR,0x5E); /*Clear SYNC*/
263    if (oldcount==1)
264    outb(CDOR,oldbytes[0]);
265     else
266    if (oldcount==2)
267    outb(CDOR,oldbytes[1]);
268    else {
269    outb (CDOR,13); /*Send a CR.. we've got trouble*/
270    kprintf("gpib: Warning: gpclose called with nothing left in buffer\n");
271    }
272 }
273
274 do
275  if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
276 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
277
278
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);
282
283
284  outb(AUXMR,tca); /* Regain full control of the bus*/
285
286
287  do
288   status=inb(ISR2);
289  while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
290  outb(CDOR,63); /*unlisten*/
291  do
292   status=inb(ISR2);
293  while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
294  outb(AUXMR,0x5E); /*Clear SYNC*/
295  outb (CDOR,95);/*untalk*/
296  do
297   status=inb(ISR2);
298  while (!(status&8)&&tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1)==EWOULDBLOCK);
299 /*gotolocal(minor(dev));*/
300 }
301         closegpib();
302         sc->sc_flags = ATTACHED;
303         kfree(sc->sc_inbuf, M_DEVBUF);
304         sc->sc_inbuf = 0;       /* Sanity */
305         return(0);
306 }
307
308 /*
309  * gpwrite()
310  *      Copy from user's buffer, then write to GPIB device referenced
311  *    by minor(dev).
312  */
313 static  int
314 gpwrite(struct dev_write_args *ap)
315 {
316         cdev_t dev = ap->a_head.a_dev;
317         struct uio *uio = ap->a_uio;
318         int err,count;
319
320         /* main loop */
321         while ((gpib_sc.sc_count = MIN(BUFSIZE-1, uio->uio_resid)) > 0) {
322                 /*  If there were >1 bytes left over, send them  */
323                 if (oldcount==2)
324                   sendrawgpibfifo(minor(dev),oldbytes,2);
325
326                 /*If there was 1 character left, put it at the beginning
327                    of the new buffer*/
328                 if (oldcount==1){
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);
333                    gpib_sc.sc_count++;
334                    }
335                  else {
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);
339                     }
340
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);
347                 err=!count;
348                 if (err)
349                         return(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*/
353               }
354                 else oldcount=gpib_sc.sc_count;
355                   oldbytes[0]=gpib_sc.sc_cp[0];
356                 if (oldcount==2)
357                   oldbytes[1]=gpib_sc.sc_cp[1];
358         }
359         return(0);
360 }
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 */
364
365 static  int
366 gpioctl(struct dev_ioctl_args *ap)
367 {
368         struct gpibdata *gd = (struct gpibdata *)ap->a_data;
369         int     error,result;
370         error = 0;
371
372         switch (ap->a_cmd) {
373         case GPIBWRITE:
374                 sendgpibfifo(gd->address,gd->data,*(gd->count));
375                 error=0;
376                 break;
377         case GPIBREAD:
378                 result=readgpibfifo(gd->address,gd->data,*(gd->count));
379                 *(gd->count)=result;
380                 error=0;
381                 break;
382         case GPIBINIT:
383                 initgpib();
384                 error=0;
385                 break;
386         case GPIBTRIGGER:
387                 trigger(gd->address);
388                 error=0;
389                 break;
390         case GPIBREMOTE:
391                 enableremote(gd->address);
392                 error=0;
393                 break;
394         case GPIBLOCAL:
395                 gotolocal(gd->address);
396                 error=0;
397                 break;
398
399         case GPIBMTRIGGER:
400                 mtrigger(gd->data);
401                 error=0;
402                 break;
403         case GPIBMREMOTE:
404                 menableremote(gd->data);
405                 error=0;
406                 break;
407         case GPIBMLOCAL:
408                 mgotolocal(gd->data);
409                 error=0;
410                 break;
411         case GPIBSPOLL:
412                 *(gd->data)=spoll(gd->address);
413                 error=0;
414                 break;
415         default:
416                 error = ENODEV;
417         }
418
419         return(error);
420 }
421
422
423
424
425 #if 0
426 /*Just in case you want a dump of the registers...*/
427
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));
441
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));
452
453
454  }
455 #endif
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*/
459
460 static int
461 initgpib(void) {
462   outb(CMDR,0x20);
463   outb(CFG,0x16);
464   outb(IMR3,0);
465   outb(CMDR,0x10);
466   outb(CNT0,0);
467   outb(CNT1,0);
468   outb(CNT2,0);
469   outb(CNT3,0);
470   outb(INTR,0); /* Put interrupt line in tri-state mode??*/
471   outb(AUXMR,chip_reset);
472
473   outb(IMR1,0x10); /* send interrupt to TURBO488 when END received*/
474   outb(IMR2,0);
475   outb(IMR0,0x90); /* Do we want nba here too??? */
476   outb(ADMR,1);
477   outb(ADR,0);
478   outb(ADR,128);
479   outb(AUXMR,0xE9);
480   outb(AUXMR,0x49);
481   outb(AUXMR,0x70);
482   outb(AUXMR,0xD0);
483   outb(AUXMR,0xA0);
484
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*/
492
493
494  /* outb(AUXMR,hldi); */ /*Perform RFD Holdoff for all data in*/
495                          /*Not currently supported*/
496
497   outb(AUXMR,pon);
498   outb(AUXMR,sic_rsc);
499  tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
500
501   outb(AUXMR,sic_rsc_off);
502
503 return(0);
504
505
506    }
507
508 /*This is kind of Brute force..  But it works*/
509
510 static void 
511 closegpib(void) 
512 {
513    outb(AUXMR,chip_reset);
514 }
515
516 /*GPIB ROUTINES:
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*/
520
521
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*/
529
530 static void
531 enableremote(unsigned char device)
532 {
533  int status;
534
535 status=EWOULDBLOCK;
536   if (status==EWOULDBLOCK) do {
537    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
538    }
539   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
540
541  outb(BSR,1);           /*Set REN bit on GPIB*/
542   if (status==EWOULDBLOCK) do {
543    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
544    }
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);
549    }
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);
554    }
555   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
556
557  }
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
562   gpib device */
563
564 static void
565 gotolocal(unsigned char device)
566 { int status;
567   status=EWOULDBLOCK;
568
569   if (status==EWOULDBLOCK) do {
570    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
571    }
572   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
573
574 outb(CDOR,(device&31)+32);
575
576   if (status==EWOULDBLOCK) do {
577    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
578    }
579   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
580
581 outb(AUXMR,0x5E);  /*Clear SYNC*/
582  outb (CDOR,1);
583
584   if (status==EWOULDBLOCK) do {
585    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
586    }
587   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
588
589  outb(AUXMR,0x5E);
590  outb (CDOR,63);/*unaddress device*/
591
592   if (status==EWOULDBLOCK) do {
593    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
594    }
595   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
596
597  }
598
599
600 static void
601 menableremote(unsigned char *device)
602 {
603  int status, counter = 0;
604
605 status=EWOULDBLOCK;
606   if (status==EWOULDBLOCK) do {
607    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
608    }
609   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
610
611  outb(BSR,1);           /*Set REN bit on GPIB*/
612  do
613   {
614   if (status==EWOULDBLOCK) do {
615    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
616    }
617   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
618  outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
619  counter++;
620  }
621  while (device[counter]<32);
622
623   if (status==EWOULDBLOCK) do {
624    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
625    }
626   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
627
628  outb (CDOR,63); /*Unaddress device*/
629   if (status==EWOULDBLOCK) do {
630    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
631    }
632   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
633
634  }
635
636 static void
637 mgotolocal(unsigned char *device)
638 { int status;
639   int counter=0;
640 status=EWOULDBLOCK;
641  if (device[counter]<32) do {
642   if (status==EWOULDBLOCK) do {
643    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
644    }
645   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
646  outb(CDOR,(device[counter]&31)+32);
647  counter++;
648  } while (device[counter]<32);
649   if (status==EWOULDBLOCK) do {
650    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
651    }
652   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
653
654  outb(AUXMR,0x5E);  /*Clear SYNC*/
655  outb (CDOR,1);
656
657
658   if (status==EWOULDBLOCK) do {
659    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
660    }
661   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
662  outb(AUXMR,0x5E);
663  outb (CDOR,63);/*unaddress device*/
664   if (status==EWOULDBLOCK) do {
665    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",2);
666    }
667   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
668
669
670  }
671 /*Trigger a device.  What happens depends on how the device is
672  configured.  */
673
674 static void 
675 trigger(unsigned char device)
676 { int status;
677
678 status=EWOULDBLOCK;
679  if (device<32)  {
680   if (!(inb(ISR2)&0x08)) do {
681    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
682    }
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);
687    }
688   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
689
690  outb (CDOR,8);  /*send GET*/
691
692   if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
693    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
694    }
695   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
696  outb (AUXMR,0x5E);
697  outb (CDOR,63);/*unaddress device*/
698   if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
699    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
700    }
701   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
702
703
704  }
705 }
706
707 /*Trigger multiple devices by addressing them all to listen, and then
708   sending GET*/
709
710 static void
711 mtrigger(unsigned char *device)
712 { int status=EWOULDBLOCK;
713   int counter=0;
714  if(device[0]<32){
715  do {
716  if (device[counter]<32)
717   if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
718    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
719    }
720   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
721  outb(CDOR,(device[counter]&31)+32); /*address device to listen*/
722  counter++;
723    }
724  while (device[counter]<32);
725   if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
726    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
727    }
728   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
729  outb (CDOR,8);  /*send GET*/
730
731   if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
732    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
733    }
734   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
735  outb (AUXMR,0x5E);
736  outb (CDOR,63);/*unaddress device*/
737   if (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK) do {
738    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
739    }
740   while (!(inb(ISR2)&0x08)&&status==EWOULDBLOCK); /*Wait to send next cmd*/
741
742
743  }
744 }
745
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 */
748
749
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*/
755
756 static int
757 sendrawgpibfifo(unsigned char device,char *data,int count)
758  {
759  int status;
760  int counter;
761  int fifopos;
762  int sleeptime;
763
764
765  sleeptime=SLEEP_MIN;
766  counter=0;
767
768
769  fifopos=0;
770
771 status=EWOULDBLOCK;
772  do {
773   /*Wait for fifo to become not full if it is full */
774   sleeptime=SLEEP_MIN;
775   if (!(inb(ISR3)&0x08)) do {
776    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
777    if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
778    }
779   while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
780
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]);*/
784
785   counter+=2;
786   count-=2;
787    }
788   }
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*/
792
793
794 /*Return the number of bytes written to the device*/
795  return(counter);
796
797
798
799 }
800
801 static int
802 sendgpibfifo(unsigned char device,char *data,int count)
803  {
804  int status;
805  int counter;
806  int fifopos;
807  int sleeptime;
808
809 outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
810  outb(CNT0,0);
811  outb(CNT1,0);
812  outb(CNT2,0);
813  outb(CNT3,0);
814 status=EWOULDBLOCK;
815  if (!(inb(ISR2)&8)) do
816    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
817  while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
818
819  outb(CDOR,(device&31)+32);/*address device to listen*/
820
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*/
825
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);
829
830  outb(AUXMR,gts); /*Set to Standby (Controller)*/
831  fifopos=0;
832
833  sleeptime=SLEEP_MIN;
834  counter=0;
835
836
837  fifopos=0;
838
839  outb(CMDR,0x20);
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*/
844 status=EWOULDBLOCK;
845  do {
846   /*Wait for fifo to become not full if it is full */
847   sleeptime=SLEEP_MIN;
848   if (!(inb(ISR3)&0x08)) do {
849    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",sleeptime);
850    if (sleeptime<SLEEP_MAX) sleeptime=sleeptime*2;
851    }
852   while (!(inb(ISR3)&0x08)&&(status==EWOULDBLOCK)); /*Fifo is full*/
853
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));
857
858   counter+=2;
859   count-=2;
860    }
861   }
862  while ((count>2)&&(status==EWOULDBLOCK));
863
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);
868    }
869   while (!(inb(ISR3)&0x08)&&status==EWOULDBLOCK); /*Fifo is full*/
870   /*outb(CFG,0x40+15);*//*send eoi when done*/
871   outb(FIFOB,data[counter]);
872   counter++;
873   count--;
874   }
875
876
877  /*outb(CMDR,0x04);*/
878
879   /*Wait for fifo to become empty*/
880   if (status==EWOULDBLOCK) do {
881   status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
882    }
883   while ((inb(ISR3)&0x04)&&status==EWOULDBLOCK); /*Fifo is not empty*/
884
885  outb(CMDR,0x08); /*Issue STOP to TURBO488*/
886
887   /*Wait for DONE and STOP*/
888  if (status==EWOULDBLOCK) do {
889    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
890    }
891   while (!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*not done and stop*/
892
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*/
896
897
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)*/
901
902 if (count==1&&status==EWOULDBLOCK)  {  /*Count should always=1 here*/
903
904 do
905  if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
906 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
907
908    outb(AUXMR,seoi);  /*Set EOI for the last byte*/
909    outb(AUXMR,0x5E); /*Clear SYNC*/
910    outb(CDOR,data[counter]);
911  counter++;
912  count--;
913 }
914
915 do
916  if (!(inb(ISR1)&2)) status=tsleep((caddr_t)&gpib_sc, GPIBPRI,"gpibpoll",1);
917 while (!(inb(ISR1)&2)&&(status==EWOULDBLOCK));
918
919
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*/
924
925
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);
929
930    outb(CDOR,63); /*unlisten*/
931
932
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);
936
937
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);
943
944
945  return(counter);
946
947
948
949 }
950
951 static int
952 readgpibfifo(unsigned char device,char *data,int count)
953 {
954  int status;
955  int status2 = 0;
956  int status1;
957  int counter;
958  int fifopos;
959  unsigned inword;
960
961  outb(IMR2,0x30); /*we have to enable DMA (0x30) for turbo488 to work*/
962  /*outb(IMR3,0x1F);
963  outb(INTR,1); */
964  outb(CMDR,0x20);
965
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??*/
970
971
972
973 status=EWOULDBLOCK;
974 do
975    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
976  while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
977
978  outb (CDOR,32); /*Address controller (me) to listen*/
979
980   do
981    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
982  while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
983
984  outb(CDOR,(device&31)+64);/*address device to talk*/
985
986
987   do
988    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
989  while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
990
991  outb(AUXMR,gts); /*Set to Standby (Controller)*/
992
993  counter=0;
994  fifopos=0;
995
996   outb(CMDR,0x04); /*Tell TURBO488 to GO*/
997
998
999  do {
1000    status1=inb(ISR3);
1001    if (!(status1&0x01)&&(status1&0x04)){
1002    status2=inb(STS2);
1003    inword=inw(FIFOB);
1004    *(unsigned*)(data+counter)=inword;
1005   /* kprintf ("Read:%c,%c\n",data[counter],data[counter+1]);*/
1006   counter+=2;
1007   }
1008  else {
1009    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",4);
1010   }
1011  }
1012  while (!(status1&0x01)&&status==EWOULDBLOCK);
1013  if(!(status2 & 0x04)){ /*Only 1 byte came in on last 16 bit transfer*/
1014   data[counter-1]=0;
1015   counter--; }
1016   else
1017     data[counter]=0;
1018  outb(CMDR,0x08); /*send STOP*/
1019
1020  do{
1021    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1022    }
1023  while(!(inb(ISR3)&0x11)&&status==EWOULDBLOCK); /*wait for DONE and STOP*/
1024  outb(AUXMR,0x55);
1025
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*/
1029
1030 /* do
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*/
1034
1035
1036  do
1037    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1038  while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1039  outb(CDOR,63); /*unlisten*/
1040
1041  do
1042    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1043  while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1044
1045 outb(AUXMR,0x5E); /*Clear SYNC*/
1046  outb (CDOR,95);/*untalk*/
1047  do
1048    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1049  while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1050
1051  return(counter);
1052
1053
1054 }
1055
1056
1057 /* Return the status byte from device */
1058 static char
1059 spoll(unsigned char device)
1060  {
1061  int status=EWOULDBLOCK;
1062  unsigned int statusbyte;
1063
1064  if (!(inb(ISR2)&8)) do
1065    status=tsleep((caddr_t)&gpib_sc,GPIBPRI,"gpibpoll",1);
1066  while (!(inb(ISR2)&8)&&status==EWOULDBLOCK);
1067
1068  outb(CDOR,(device&31)+64);/*address device to talk*/
1069
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);
1073
1074  outb (CDOR,32); /*Address controller (me) to listen*/
1075
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);
1079  outb(AUXMR,0x5E);
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);
1084
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);
1089
1090  outb(AUXMR,gts); /*Set to Standby (Controller)*/
1091
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);
1095  outb(AUXMR,0x5E);
1096  outb(AUXMR,tcs); /* Take control after next read*/
1097  statusbyte=inb(DIR);
1098
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);
1102
1103  outb(CDOR,0x19); /*SPD (serial poll disable)*/
1104
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);
1109
1110
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);
1114
1115 outb(CDOR,95); /*untalk*/
1116
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);
1120  outb(AUXMR,0x5E);
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);
1125
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);
1130
1131
1132  return(statusbyte);
1133
1134
1135 }