5a179b9a07f8205e7940a7bb0bf862eac28e3961
[dragonfly.git] / sys / dev / misc / labpc / labpc.c
1 /*
2  * Copyright (c) 1995 HD Associates, Inc.
3  * All rights reserved.
4  *
5  * HD Associates, Inc.
6  * PO Box 276
7  * Pepperell, MA 01463-0276
8  * dufault@hda.com
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by HD Associates, Inc.
21  * 4. The name of HD Associates, Inc.
22  *    may not be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * Written by:
38  * Peter Dufault
39  * dufault@hda.com
40  *
41  * $FreeBSD: src/sys/i386/isa/labpc.c,v 1.35 1999/09/25 18:24:08 phk Exp $
42  * $DragonFly: src/sys/dev/misc/labpc/labpc.c,v 1.2 2003/06/17 04:28:37 dillon Exp $
43  *
44  */
45
46 #include "labpc.h"
47 #include "opt_debug_outb.h"
48 #include <sys/param.h>
49
50 #include <sys/systm.h>
51
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/buf.h>
55 #define b_actf  b_act.tqe_next
56 #include <sys/dataacq.h>
57 #include <sys/conf.h>
58
59 #ifdef LOUTB
60 #include <machine/clock.h>
61 #endif
62
63 #include <i386/isa/isa_device.h>
64
65
66
67 /* Miniumum timeout:
68  */
69 #ifndef LABPC_MIN_TMO
70 #define LABPC_MIN_TMO (hz)
71 #endif
72
73 #ifndef LABPC_DEFAULT_HERZ
74 #define LABPC_DEFAULT_HERZ 500
75 #endif
76
77 /* Minor number:
78  * UUSIDCCC
79  * UU: Board unit.
80  * S: SCAN bit for scan enable.
81  * I: INTERVAL for interval support
82  * D: 1: Digital I/O, 0: Analog I/O
83  * CCC: Channel.
84  *  Analog (D==0):
85  *  input: channel must be 0 to 7.
86  *  output: channel must be 0 to 2
87  *          0: D-A 0
88  *          1: D-A 1
89  *          2: Alternate channel 0 then 1
90  *
91  *  Digital (D==1):
92  *  input: Channel must be 0 to 2.
93  *  output: Channel must be 0 to 2.
94  */
95
96 /* Up to four boards:
97  */
98 #define MAX_UNITS 4
99 #define UNIT(dev) (((minor(dev) & 0xB0) >> 6) & 0x3)
100
101 #define SCAN(dev)     ((minor(dev) & 0x20) >> 5)
102 #define INTERVAL(dev) ((minor(dev) & 0x10) >> 4)
103 #define DIGITAL(dev)  ((minor(dev) & 0x08) >> 3)
104
105 /* Eight channels:
106  */
107
108 #define CHAN(dev) (minor(dev) & 0x7)
109
110 /* History: Derived from "dt2811.c" March 1995
111  */
112
113 struct ctlr
114 {
115         int err;
116 #define DROPPED_INPUT 0x100
117         int base;
118         int unit;
119         unsigned long flags;
120 #define BUSY 0x00000001
121
122         u_char cr_image[4];
123
124         u_short sample_us;
125
126         struct buf start_queue; /* Start queue */
127         struct buf *last;       /* End of start queue */
128         u_char *data;
129         u_char *data_end;
130         long tmo;                       /* Timeout in Herz */
131         long min_tmo;           /* Timeout in Herz */
132         int cleared_intr;
133
134         int gains[8];
135
136         dev_t dev;                      /* Copy of device */
137
138         void (*starter)(struct ctlr *ctlr, long count);
139         void (*stop)(struct ctlr *ctlr);
140         void (*intr)(struct ctlr *ctlr);
141
142         /* Digital I/O support.  Copy of Data Control Register for 8255:
143          */
144         u_char dcr_val, dcr_is;
145
146         /*
147          * Handle for canceling our timeout.
148          */
149         struct callout_handle ch;
150
151         /* Device configuration structure:
152          */
153 };
154
155 #ifdef LOUTB
156 /* loutb is a slow outb for debugging.  The overrun test may fail
157  * with this for some slower processors.
158  */
159 static __inline void loutb(int port, u_char val)
160 {
161         outb(port, val);
162         DELAY(1);
163 }
164 #else
165 #define loutb(port, val) outb(port, val)
166 #endif
167
168 static struct ctlr **labpcs;    /* XXX: Should be dynamic */
169
170 /* CR_EXPR: A macro that sets the shadow register in addition to
171  * sending out the data.
172  */
173 #define CR_EXPR(LABPC, CR, EXPR) do { \
174         (LABPC)->cr_image[CR - 1] EXPR ; \
175         loutb(((LABPC)->base + ( (CR == 4) ? (0x0F) : (CR - 1))), ((LABPC)->cr_image[(CR - 1)])); \
176 } while (0)
177
178 #define CR_CLR(LABPC, CR) CR_EXPR(LABPC, CR, &=0)
179 #define CR_REFRESH(LABPC, CR) CR_EXPR(LABPC, CR, &=0xff)
180 #define CR_SET(LABPC, CR, EXPR) CR_EXPR(LABPC, CR, = EXPR)
181
182 /* Configuration and Status Register Group.
183  */
184 #define     CR1(LABPC) ((LABPC)->base + 0x00)   /* Page 4-5 */
185         #define SCANEN    0x80
186         #define GAINMASK  0x70
187         #define GAIN(LABPC, SEL) do { \
188                 (LABPC)->cr_image[1 - 1] &= ~GAINMASK; \
189                 (LABPC)->cr_image[1 - 1] |= (SEL << 4); \
190                 loutb((LABPC)->base + (1 - 1), (LABPC)->cr_image[(1 - 1)]); \
191                 } while (0)
192
193         #define TWOSCMP   0x08
194         #define MAMASK    0x07
195         #define MA(LABPC, SEL) do { \
196                 (LABPC)->cr_image[1 - 1] &= ~MAMASK; \
197                 (LABPC)->cr_image[1 - 1] |= SEL; \
198                 loutb((LABPC)->base + (1 - 1), (LABPC)->cr_image[(1 - 1)]); \
199                 } while (0)
200
201 #define  STATUS(LABPC) ((LABPC)->base + 0x00)   /* Page 4-7 */
202         #define LABPCPLUS 0x80
203         #define EXTGATA0  0x40
204         #define GATA0     0x20
205         #define DMATC     0x10
206         #define CNTINT    0x08
207         #define OVERFLOW  0x04
208         #define OVERRUN   0x02
209         #define DAVAIL    0x01
210
211 #define     CR2(LABPC) ((LABPC)->base + 0x01)   /* Page 4-9 */
212         #define LDAC1     0x80
213         #define LDAC0     0x40
214         #define _2SDAC1   0x20
215         #define _2SDAC0   0x10
216         #define TBSEL     0x08
217         #define SWTRIG    0x04
218         #define HWTRIG    0x02
219         #define PRETRIG   0x01
220         #define SWTRIGGERRED(LABPC) ((LABPC->cr_image[1]) & SWTRIG)
221
222 #define     CR3(LABPC) ((LABPC)->base + 0x02)   /* Page 4-11 */
223         #define FIFOINTEN 0x20
224         #define ERRINTEN  0x10
225         #define CNTINTEN  0x08
226         #define TCINTEN   0x04
227         #define DIOINTEN  0x02
228         #define DMAEN     0x01
229
230         #define ALLINTEN  0x3E
231         #define FIFOINTENABLED(LABPC) ((LABPC->cr_image[2]) & FIFOINTEN)
232
233 #define     CR4(LABPC) ((LABPC)->base + 0x0F)   /* Page 4-13 */
234         #define ECLKRCV   0x10
235         #define SE_D      0x08
236         #define ECKDRV    0x04
237         #define EOIRCV    0x02
238         #define INTSCAN   0x01
239
240 /* Analog Input Register Group
241  */
242 #define   ADFIFO(LABPC) ((LABPC)->base + 0x0A)  /* Page 4-16 */
243 #define  ADCLEAR(LABPC) ((LABPC)->base + 0x08)  /* Page 4-18 */
244 #define  ADSTART(LABPC) ((LABPC)->base + 0x03)  /* Page 4-19 */
245 #define DMATCICLR(LABPC) ((LABPC)->base + 0x0A) /* Page 4-20 */
246
247 /* Analog Output Register Group
248  */
249 #define    DAC0L(LABPC) ((LABPC)->base + 0x04)  /* Page 4-22 */
250 #define    DAC0H(LABPC) ((LABPC)->base + 0x05)  /* Page 4-22 */
251 #define    DAC1L(LABPC) ((LABPC)->base + 0x06)  /* Page 4-22 */
252 #define    DAC1H(LABPC) ((LABPC)->base + 0x07)  /* Page 4-22 */
253
254 /* 8253 registers:
255  */
256 #define A0DATA(LABPC) ((LABPC)->base + 0x14)
257 #define A1DATA(LABPC) ((LABPC)->base + 0x15)
258 #define A2DATA(LABPC) ((LABPC)->base + 0x16)
259 #define AMODE(LABPC) ((LABPC)->base + 0x17)
260
261 #define TICR(LABPC) ((LABPC)->base + 0x0c)
262
263 #define B0DATA(LABPC) ((LABPC)->base + 0x18)
264 #define B1DATA(LABPC) ((LABPC)->base + 0x19)
265 #define B2DATA(LABPC) ((LABPC)->base + 0x1A)
266 #define BMODE(LABPC) ((LABPC)->base + 0x1B)
267
268 /* 8255 registers:
269  */
270
271 #define PORTX(LABPC, X) ((LABPC)->base + 0x10 + X)
272
273 #define PORTA(LABPC) PORTX(LABPC, 0)
274 #define PORTB(LABPC) PORTX(LABPC, 1)
275 #define PORTC(LABPC) PORTX(LABPC, 2)
276
277 #define DCR(LABPC) ((LABPC)->base + 0x13)
278
279 static int labpcattach(struct isa_device *dev);
280 static int labpcprobe(struct isa_device *dev);
281 struct isa_driver labpcdriver =
282         { labpcprobe, labpcattach, "labpc", 0  };
283
284 static  d_open_t        labpcopen;
285 static  d_close_t       labpcclose;
286 static  d_ioctl_t       labpcioctl;
287 static  d_strategy_t    labpcstrategy;
288
289 #define CDEV_MAJOR 66
290 static struct cdevsw labpc_cdevsw = {
291         /* open */      labpcopen,
292         /* close */     labpcclose,
293         /* read */      physread,
294         /* write */     physwrite,
295         /* ioctl */     labpcioctl,
296         /* poll */      nopoll,
297         /* mmap */      nommap,
298         /* strategy */  labpcstrategy,
299         /* name */      "labpc",
300         /* maj */       CDEV_MAJOR,
301         /* dump */      nodump,
302         /* psize */     nopsize,
303         /* flags */     0,
304         /* bmaj */      -1
305 };
306
307 static ointhand2_t labpcintr;
308 static void start(struct ctlr *ctlr);
309
310 static void
311 bp_done(struct buf *bp, int err)
312 {
313         bp->b_error = err;
314
315         if (err || bp->b_resid)
316         {
317                 bp->b_flags |= B_ERROR;
318         }
319
320         biodone(bp);
321 }
322
323 static void tmo_stop(void *p);
324
325 static void
326 done_and_start_next(struct ctlr *ctlr, struct buf *bp, int err)
327 {
328         bp->b_resid = ctlr->data_end - ctlr->data;
329
330         ctlr->data = 0;
331
332         ctlr->start_queue.b_actf = bp->b_actf;
333         bp_done(bp, err);
334
335         untimeout(tmo_stop, ctlr, ctlr->ch);
336
337         start(ctlr);
338 }
339
340 static __inline void
341 ad_clear(struct ctlr *ctlr)
342 {
343         int i;
344         loutb(ADCLEAR(ctlr), 0);
345         for (i = 0; i < 10000 && (inb(STATUS(ctlr)) & GATA0); i++)
346                 ;
347         (void)inb(ADFIFO(ctlr));
348         (void)inb(ADFIFO(ctlr));
349 }
350
351 /* reset: Reset the board following the sequence on page 5-1
352  */
353 static __inline void
354 reset(struct ctlr *ctlr)
355 {
356         int s = splhigh();
357
358         CR_CLR(ctlr, 3);        /* Turn off interrupts first */
359         splx(s);
360
361         CR_CLR(ctlr, 1);
362         CR_CLR(ctlr, 2);
363         CR_CLR(ctlr, 4);
364
365         loutb(AMODE(ctlr), 0x34);
366         loutb(A0DATA(ctlr),0x0A);
367         loutb(A0DATA(ctlr),0x00);
368
369         loutb(DMATCICLR(ctlr), 0x00);
370         loutb(TICR(ctlr), 0x00);
371
372         ad_clear(ctlr);
373
374         loutb(DAC0L(ctlr), 0);
375         loutb(DAC0H(ctlr), 0);
376         loutb(DAC1L(ctlr), 0);
377         loutb(DAC1H(ctlr), 0);
378
379         ad_clear(ctlr);
380 }
381
382 /* overrun: slam the start convert register and OVERRUN should get set:
383  */
384 static u_char
385 overrun(struct ctlr *ctlr)
386 {
387         int i;
388
389         u_char status = inb(STATUS(ctlr));
390         for (i = 0; ((status & OVERRUN) == 0) && i < 100; i++)
391         {
392                 loutb(ADSTART(ctlr), 1);
393                 status = inb(STATUS(ctlr));
394         }
395
396         return status;
397 }
398
399 static int
400 labpcinit(void)
401 {
402         if (NLABPC > MAX_UNITS)
403                 return 0;
404
405         labpcs = malloc(NLABPC * sizeof(struct ctlr *), M_DEVBUF, M_NOWAIT);
406         if (labpcs)
407         {
408                 bzero(labpcs, NLABPC * sizeof(struct ctlr *));
409                 return 1;
410         }
411         cdevsw_add(&labpc_cdevsw);
412         return 0;
413 }
414
415 static int
416 labpcprobe(struct isa_device *dev)
417 {
418         static int unit;
419         struct ctlr scratch, *ctlr;
420         u_char status;
421
422         if (!labpcs)
423         {
424                 if (labpcinit() == 0)
425                 {
426                         printf("labpcprobe: init failed\n");
427                         return 0;
428                 }
429         }
430
431         if (unit > NLABPC)
432         {
433                 printf("Too many LAB-PCs.  Reconfigure O/S.\n");
434                 return 0;
435         }
436         ctlr = &scratch;        /* Need somebody with the right base for the macros */
437         ctlr->base = dev->id_iobase;
438
439         /* XXX: There really isn't a perfect way to probe this board.
440          *      Here is my best attempt:
441          */
442         reset(ctlr);
443
444         /* After reset none of these bits should be set:
445          */
446         status = inb(STATUS(ctlr));
447         if (status & (GATA0 | OVERFLOW | DAVAIL | OVERRUN))
448                 return 0;
449
450         /* Now try to overrun the board FIFO and get the overrun bit set:
451          */
452         status = overrun(ctlr);
453
454         if ((status & OVERRUN) == 0)    /* No overrun bit set? */
455                 return 0;
456
457         /* Assume we have a board.
458          */
459         reset(ctlr);
460
461         if ( (labpcs[unit] = malloc(sizeof(struct ctlr), M_DEVBUF, M_NOWAIT)) )
462         {
463                 struct ctlr *l = labpcs[unit];
464
465                 bzero(l, sizeof(struct ctlr));
466                 l->base = ctlr->base;
467                 dev->id_unit = l->unit = unit;
468
469                 unit++;
470                 return 0x20;
471         }
472         else
473         {
474                 printf("labpc%d: Can't malloc.\n", unit);
475                 return 0;
476         }
477 }
478
479 /* attach: Set things in a normal state.
480  */
481 static int
482 labpcattach(struct isa_device *dev)
483 {
484         struct ctlr *ctlr = labpcs[dev->id_unit];
485
486         dev->id_ointr = labpcintr;
487         callout_handle_init(&ctlr->ch);
488         ctlr->sample_us = (1000000.0 / (double)LABPC_DEFAULT_HERZ) + .50;
489         reset(ctlr);
490
491         ctlr->min_tmo = LABPC_MIN_TMO;
492
493         ctlr->dcr_val = 0x80;
494         ctlr->dcr_is = 0x80;
495         loutb(DCR(ctlr), ctlr->dcr_val);
496
497         make_dev(&labpc_cdevsw, 0, 0, 0, 0600, "labpc%d", dev->id_unit);
498         return 1;
499 }
500
501 /* Null handlers:
502  */
503 static void null_intr (struct ctlr *ctlr)             { }
504 static void null_start(struct ctlr *ctlr, long count) { }
505 static void null_stop (struct ctlr *ctlr)             { }
506
507 static __inline void
508 trigger(struct ctlr *ctlr)
509 {
510         CR_EXPR(ctlr, 2, |= SWTRIG);
511 }
512
513 static void
514 ad_start(struct ctlr *ctlr, long count)
515 {
516         if (!SWTRIGGERRED(ctlr)) {
517                 int chan = CHAN(ctlr->dev);
518                 CR_EXPR(ctlr, 1, &= ~SCANEN);
519                 CR_EXPR(ctlr, 2, &= ~TBSEL);
520
521                 MA(ctlr, chan);
522                 GAIN(ctlr, ctlr->gains[chan]);
523
524                 if (SCAN(ctlr->dev))
525                         CR_EXPR(ctlr, 1, |= SCANEN);
526
527                 loutb(AMODE(ctlr), 0x34);
528                 loutb(A0DATA(ctlr), (u_char)((ctlr->sample_us & 0xff)));
529                 loutb(A0DATA(ctlr), (u_char)((ctlr->sample_us >> 8)&0xff));
530                 loutb(AMODE(ctlr), 0x70);
531
532                 ad_clear(ctlr);
533                 trigger(ctlr);
534         }
535
536         ctlr->tmo = ((count + 16) * (long)ctlr->sample_us * hz) / 1000000 +
537                 ctlr->min_tmo;
538 }
539
540 static void
541 ad_interval_start(struct ctlr *ctlr, long count)
542 {
543         int chan = CHAN(ctlr->dev);
544         int n_frames = count / (chan + 1);
545
546         if (!SWTRIGGERRED(ctlr)) {
547                 CR_EXPR(ctlr, 1, &= ~SCANEN);
548                 CR_EXPR(ctlr, 2, &= ~TBSEL);
549
550                 MA(ctlr, chan);
551                 GAIN(ctlr, ctlr->gains[chan]);
552
553                 /* XXX: Is it really possible that you clear INTSCAN as
554                  * the documentation says?  That seems pretty unlikely.
555                  */
556                 CR_EXPR(ctlr, 4, &= ~INTSCAN);  /* XXX: Is this possible? */
557
558                 /* Program the sample interval counter to run as fast as
559                  * possible.
560                  */
561                 loutb(AMODE(ctlr), 0x34);
562                 loutb(A0DATA(ctlr), (u_char)(0x02));
563                 loutb(A0DATA(ctlr), (u_char)(0x00));
564                 loutb(AMODE(ctlr), 0x70);
565
566                 /* Program the interval scanning counter to run at the sample
567                  * frequency.
568                  */
569                 loutb(BMODE(ctlr), 0x74);
570                 loutb(B1DATA(ctlr), (u_char)((ctlr->sample_us & 0xff)));
571                 loutb(B1DATA(ctlr), (u_char)((ctlr->sample_us >> 8)&0xff));
572                 CR_EXPR(ctlr, 1, |= SCANEN);
573
574                 ad_clear(ctlr);
575                 trigger(ctlr);
576         }
577
578         /* Each frame time takes two microseconds per channel times
579          * the number of channels being sampled plus the sample period.
580          */
581         ctlr->tmo = ((n_frames + 16) *
582         ((long)ctlr->sample_us + (chan + 1 ) * 2 ) * hz) / 1000000 +
583                 ctlr->min_tmo;
584 }
585
586 static void
587 all_stop(struct ctlr *ctlr)
588 {
589         reset(ctlr);
590 }
591
592 static void
593 tmo_stop(void *p)
594 {
595         struct ctlr *ctlr = (struct ctlr *)p;
596         struct buf *bp;
597
598         int s = spltty();
599
600         if (ctlr == 0)
601         {
602                 printf("labpc?: Null ctlr struct?\n");
603                 splx(s);
604                 return;
605         }
606
607         printf("labpc%d: timeout", ctlr->unit);
608
609         (*ctlr->stop)(ctlr);
610
611         bp = ctlr->start_queue.b_actf;
612
613         if (bp == 0) {
614                 printf(", Null bp.\n");
615                 splx(s);
616                 return;
617         }
618
619         printf("\n");
620
621         done_and_start_next(ctlr, bp, ETIMEDOUT);
622
623         splx(s);
624 }
625
626 static void ad_intr(struct ctlr *ctlr)
627 {
628         u_char status;
629
630         if (ctlr->cr_image[2] == 0)
631         {
632                 if (ctlr->cleared_intr)
633                 {
634                         ctlr->cleared_intr = 0;
635                         return;
636                 }
637
638                 printf("ad_intr (should not happen) interrupt with interrupts off\n");
639                 printf("status %x, cr3 %x\n", inb(STATUS(ctlr)), ctlr->cr_image[2]);
640                 return;
641         }
642
643         while ( (status = (inb(STATUS(ctlr)) & (DAVAIL|OVERRUN|OVERFLOW)) ) )
644         {
645                 if ((status & (OVERRUN|OVERFLOW)))
646                 {
647                         struct buf *bp = ctlr->start_queue.b_actf;
648
649                         printf("ad_intr: error: bp %p, data %p, status %x",
650                             (void *)bp, (void *)ctlr->data, status);
651
652                         if (status & OVERRUN)
653                                 printf(" Conversion overrun (multiple A-D trigger)");
654
655                         if (status & OVERFLOW)
656                                 printf(" FIFO overflow");
657
658                         printf("\n");
659
660                         if (bp)
661                         {
662                                 done_and_start_next(ctlr, bp, EIO);
663                                 return;
664                         }
665                         else
666                         {
667                                 printf("ad_intr: (should not happen) error between records\n");
668                                 ctlr->err = status;     /* Set overrun condition */
669                                 return;
670                         }
671                 }
672                 else    /* FIFO interrupt */
673                 {
674                         struct buf *bp = ctlr->start_queue.b_actf;
675
676                         if (ctlr->data)
677                         {
678                                 *ctlr->data++ = inb(ADFIFO(ctlr));
679                                 if (ctlr->data == ctlr->data_end)       /* Normal completion */
680                                 {
681                                         done_and_start_next(ctlr, bp, 0);
682                                         return;
683                                 }
684                         }
685                         else    /* Interrupt with no where to put the data.  */
686                         {
687                                 printf("ad_intr: (should not happen) dropped input.\n");
688                                 (void)inb(ADFIFO(ctlr));
689
690                                 printf("bp %p, status %x, cr3 %x\n",
691                                     (void *)bp, status, ctlr->cr_image[2]);
692
693                                 ctlr->err = DROPPED_INPUT;
694                                 return;
695                         }
696                 }
697         }
698 }
699
700 static void labpcintr(int unit)
701 {
702         struct ctlr *ctlr = labpcs[unit];
703         (*ctlr->intr)(ctlr);
704 }
705
706 /* lockout_multiple_opens: Return whether or not we can open again, or
707  * if the new mode is inconsistent with an already opened mode.
708  * We only permit multiple opens for digital I/O now.
709  */
710
711 static int
712 lockout_multiple_open(dev_t current, dev_t next)
713 {
714         return ! (DIGITAL(current) && DIGITAL(next));
715 }
716
717 static  int
718 labpcopen(dev_t dev, int flags, int fmt, struct proc *p)
719 {
720         u_short unit = UNIT(dev);
721
722         struct ctlr *ctlr;
723
724         if (unit >= MAX_UNITS)
725                 return ENXIO;
726
727         ctlr = labpcs[unit];
728
729         if (ctlr == 0)
730                 return ENXIO;
731
732         /* Don't allow another open if we have to change modes.
733          */
734
735         if ( (ctlr->flags & BUSY) == 0)
736         {
737                 ctlr->flags |= BUSY;
738
739                 reset(ctlr);
740
741                 ctlr->err = 0;
742                 ctlr->dev = dev;
743
744                 ctlr->intr = null_intr;
745                 ctlr->starter = null_start;
746                 ctlr->stop = null_stop;
747         }
748         else if (lockout_multiple_open(ctlr->dev, dev))
749                 return EBUSY;
750
751         return 0;
752 }
753
754 static  int
755 labpcclose(dev_t dev, int flags, int fmt, struct proc *p)
756 {
757         struct ctlr *ctlr = labpcs[UNIT(dev)];
758
759         (*ctlr->stop)(ctlr);
760
761         ctlr->flags &= ~BUSY;
762
763         return 0;
764 }
765
766 /*
767  * Start: Start a frame going in or out.
768  */
769 static void
770 start(struct ctlr *ctlr)
771 {
772         struct buf *bp;
773
774         if ((bp = ctlr->start_queue.b_actf) == 0)
775         {
776                 /* We must turn off FIFO interrupts when there is no
777                  * place to put the data.  We have to get back to
778                  * reading before the FIFO overflows.
779                  */
780                 CR_EXPR(ctlr, 3, &= ~(FIFOINTEN|ERRINTEN));
781                 ctlr->cleared_intr = 1;
782                 ctlr->start_queue.b_bcount = 0;
783                 return;
784         }
785
786         ctlr->data = (u_char *)bp->b_data;
787         ctlr->data_end = ctlr->data + bp->b_bcount;
788
789         if (ctlr->err)
790         {
791                 printf("labpc start: (should not happen) error between records.\n");
792                 done_and_start_next(ctlr, bp, EIO);
793                 return;
794         }
795
796         if (ctlr->data == 0)
797         {
798                 printf("labpc start: (should not happen) NULL data pointer.\n");
799                 done_and_start_next(ctlr, bp, EIO);
800                 return;
801         }
802
803
804         (*ctlr->starter)(ctlr, bp->b_bcount);
805
806         if (!FIFOINTENABLED(ctlr))      /* We can store the data again */
807         {
808                 CR_EXPR(ctlr, 3, |= (FIFOINTEN|ERRINTEN));
809
810                 /* Don't wait for the interrupts to fill things up.
811                  */
812                 (*ctlr->intr)(ctlr);
813         }
814
815         ctlr->ch = timeout(tmo_stop, ctlr, ctlr->tmo);
816 }
817
818 static void
819 ad_strategy(struct buf *bp, struct ctlr *ctlr)
820 {
821         int s;
822
823         s = spltty();
824         bp->b_actf = NULL;
825
826         if (ctlr->start_queue.b_bcount)
827         {
828                 ctlr->last->b_actf = bp;
829                 ctlr->last = bp;
830         }
831         else
832         {
833                 ctlr->start_queue.b_bcount = 1;
834                 ctlr->start_queue.b_actf = bp;
835                 ctlr->last = bp;
836                 start(ctlr);
837         }
838         splx(s);
839 }
840
841 /* da_strategy: Send data to the D-A.  The CHAN field should be
842  * 0: D-A port 0
843  * 1: D-A port 1
844  * 2: Alternate port 0 then port 1
845  *
846  * XXX:
847  *
848  * 1. There is no state for CHAN field 2:
849  * the first sample in each buffer goes to channel 0.
850  *
851  * 2. No interrupt support yet.
852  */
853 static void
854 da_strategy(struct buf *bp, struct ctlr *ctlr)
855 {
856         int len;
857         u_char *data;
858         int port;
859         int i;
860
861         switch(CHAN(bp->b_dev))
862         {
863                 case 0:
864                         port = DAC0L(ctlr);
865                         break;
866
867                 case 1:
868                         port = DAC1L(ctlr);
869                         break;
870
871                 case 2: /* Device 2 handles both ports interleaved. */
872                         if (bp->b_bcount <= 2)
873                         {
874                                 port = DAC0L(ctlr);
875                                 break;
876                         }
877
878                         len = bp->b_bcount / 2;
879                         data = (u_char *)bp->b_data;
880
881                         for (i = 0; i < len; i++)
882                         {
883                                 loutb(DAC0H(ctlr), *data++);
884                                 loutb(DAC0L(ctlr), *data++);
885                                 loutb(DAC1H(ctlr), *data++);
886                                 loutb(DAC1L(ctlr), *data++);
887                         }
888
889                         bp->b_resid = bp->b_bcount & 3;
890                         bp_done(bp, 0);
891                         return;
892
893                 default:
894                         bp_done(bp, ENXIO);
895                         return;
896         }
897
898         /* Port 0 or 1 falls through to here.
899          */
900         if (bp->b_bcount & 1)   /* Odd transfers are illegal */
901                 bp_done(bp, EIO);
902
903         len = bp->b_bcount;
904         data = (u_char *)bp->b_data;
905
906         for (i = 0; i < len; i++)
907         {
908                 loutb(port + 1, *data++);
909                 loutb(port, *data++);
910         }
911
912         bp->b_resid = 0;
913
914         bp_done(bp, 0);
915 }
916
917 /* Input masks for MODE 0 of the ports treating PC as a single
918  * 8 bit port.  Set these bits to set the port to input.
919  */
920                             /* A     B    lowc  highc combined */
921 static u_char set_input[] = { 0x10, 0x02, 0x01,  0x08,  0x09 };
922
923 static void flush_dcr(struct ctlr *ctlr)
924 {
925         if (ctlr->dcr_is != ctlr->dcr_val)
926         {
927                 loutb(DCR(ctlr), ctlr->dcr_val);
928                 ctlr->dcr_is = ctlr->dcr_val;
929         }
930 }
931
932 /* do: Digital output
933  */
934 static void
935 digital_out_strategy(struct buf *bp, struct ctlr *ctlr)
936 {
937         int len;
938         u_char *data;
939         int port;
940         int i;
941         int chan = CHAN(bp->b_dev);
942
943         ctlr->dcr_val &= ~set_input[chan];      /* Digital out: Clear bit */
944         flush_dcr(ctlr);
945
946         port = PORTX(ctlr, chan);
947
948         len = bp->b_bcount;
949         data = (u_char *)bp->b_data;
950
951         for (i = 0; i < len; i++)
952         {
953                 loutb(port, *data++);
954         }
955
956         bp->b_resid = 0;
957
958         bp_done(bp, 0);
959 }
960
961 /* digital_in_strategy: Digital input
962  */
963 static void
964 digital_in_strategy(struct buf *bp, struct ctlr *ctlr)
965 {
966         int len;
967         u_char *data;
968         int port;
969         int i;
970         int chan = CHAN(bp->b_dev);
971
972         ctlr->dcr_val |= set_input[chan];       /* Digital in: Set bit */
973         flush_dcr(ctlr);
974         port = PORTX(ctlr, chan);
975
976         len = bp->b_bcount;
977         data = (u_char *)bp->b_data;
978
979         for (i = 0; i < len; i++)
980         {
981                 *data++ = inb(port);
982         }
983
984         bp->b_resid = 0;
985
986         bp_done(bp, 0);
987 }
988
989
990 static  void
991 labpcstrategy(struct buf *bp)
992 {
993         struct ctlr *ctlr = labpcs[UNIT(bp->b_dev)];
994
995         if (DIGITAL(bp->b_dev)) {
996                 if (bp->b_flags & B_READ) {
997                         ctlr->starter = null_start;
998                         ctlr->stop = all_stop;
999                         ctlr->intr = null_intr;
1000                         digital_in_strategy(bp, ctlr);
1001                 }
1002                 else
1003                 {
1004                         ctlr->starter = null_start;
1005                         ctlr->stop = all_stop;
1006                         ctlr->intr = null_intr;
1007                         digital_out_strategy(bp, ctlr);
1008                 }
1009         }
1010         else {
1011                 if (bp->b_flags & B_READ) {
1012
1013                         ctlr->starter = INTERVAL(ctlr->dev) ? ad_interval_start : ad_start;
1014                         ctlr->stop = all_stop;
1015                         ctlr->intr = ad_intr;
1016                         ad_strategy(bp, ctlr);
1017                 }
1018                 else
1019                 {
1020                         ctlr->starter = null_start;
1021                         ctlr->stop = all_stop;
1022                         ctlr->intr = null_intr;
1023                         da_strategy(bp, ctlr);
1024                 }
1025         }
1026 }
1027
1028 static  int
1029 labpcioctl(dev_t dev, u_long cmd, caddr_t arg, int mode, struct proc *p)
1030 {
1031         struct ctlr *ctlr = labpcs[UNIT(dev)];
1032
1033         switch(cmd)
1034         {
1035                 case AD_MICRO_PERIOD_SET:
1036                 {
1037                         /* XXX I'm only supporting what I have to, which is
1038                          * no slow periods.  You can't get any slower than 15 Hz
1039                          * with the current setup.  To go slower you'll need to
1040                          * support TCINTEN in CR3.
1041                          */
1042
1043                         long sample_us = *(long *)arg;
1044
1045                         if (sample_us > 65535)
1046                                 return EIO;
1047
1048                         ctlr->sample_us = sample_us;
1049                         return 0;
1050                 }
1051
1052                 case AD_MICRO_PERIOD_GET:
1053                         *(long *)arg = ctlr->sample_us;
1054                         return 0;
1055
1056                 case AD_NGAINS_GET:
1057                         *(int *)arg = 8;
1058                         return 0;
1059
1060                 case AD_NCHANS_GET:
1061                         *(int *)arg = 8;
1062                         return 0;
1063
1064                 case AD_SUPPORTED_GAINS:
1065                 {
1066                         static double gains[] = {1., 1.25, 2., 5., 10., 20., 50., 100.};
1067                         copyout(gains, *(caddr_t *)arg, sizeof(gains));
1068
1069                         return 0;
1070                 }
1071
1072                 case AD_GAINS_SET:
1073                 {
1074                         copyin(*(caddr_t *)arg, ctlr->gains, sizeof(ctlr->gains));
1075                         return 0;
1076                 }
1077
1078                 case AD_GAINS_GET:
1079                 {
1080                         copyout(ctlr->gains, *(caddr_t *)arg, sizeof(ctlr->gains));
1081                         return 0;
1082                 }
1083
1084                 default:
1085                         return ENOTTY;
1086         }
1087 }