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