2 * Copyright (c) 1998 Scottibox
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Industrial Computer Source model AIO8-P
30 * 8 channel, moderate speed analog to digital converter board with
31 * 128 channel MUX capability via daisy chained AT-16P units
32 * alog.c, character device driver, last revised January 6 1998
33 * See http://www.scottibox.com
34 * http://www.indcompsrc.com/products/data/html/aio8g-p.html
35 * http://www.indcompsrc.com/products/data/html/at16-p.html
37 * Written by: Jamil J. Weatherbee <jamil@scottibox.com>
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/fcntl.h>
53 #include <sys/malloc.h>
56 #include <sys/vnode.h>
57 #include <sys/filio.h>
58 #include <machine/clock.h>
59 #include <i386/isa/isa.h>
60 #include <i386/isa/isa_device.h>
61 #include <sys/alogio.h>
62 #include <sys/dataacq.h>
64 #include "opt_devfs.h"
66 #include <sys/devfsext.h>
71 /* Tests have shown that increasing the fifo size
72 * beyond 64 entries for this particular piece of hardware is
76 #define FIFOSIZE ALOG_FIFOSIZE
81 #ifdef ALOG_FIFO_TRIGGER
82 #define DEFAULT_FIFO_TRIGGER ALOG_FIFO_TRIGGER
84 #define DEFAULT_FIFO_TRIGGER 1
88 #define NUMCHANNELS ALOG_CHANNELS
90 #define NUMCHANNELS 128
94 #define READTIMO ALOG_TIMO
96 #define READTIMO (MAX_MICRO_PERIOD*NUMCHANNELS/500000*hz)
110 #define CNTRCNTRL 0x7
112 #define DEVFORMAT "alog%d%c%d"
113 #define CLOCK2FREQ 4.165
114 #define MIN_MICRO_PERIOD 25
115 #define MAX_MICRO_PERIOD (65535/CLOCK2FREQ*PRIMARY_STATES)
116 #define DEFAULT_MICRO_PERIOD MAX_MICRO_PERIOD
117 #define READMAXTRIG 0.75*FIFOSIZE
118 #define ALOGPRI PRIBIO
119 #define ALOGMSG "alogio"
121 #define PRIMARY_STATES 2 /* Setup and conversion are clock tick consuming */
122 #define STATE_SETUP 0
123 #define STATE_CONVERT 1
126 /* Notes on interrupt driven A/D conversion:
127 * On the AIO8-P, interrupt driven conversion (the only type supported by this
128 * driver) is facilitated through 8253 timer #2. In order for interrrupts to
129 * be generated you must connect line 6 to line 24 (counter 2 output to
130 * interrupt input) and line 23 to line 29 (counter 2 gate to +5VDC).
131 * Due to the design of the AIO8-P this precludes the use of programmable
135 /* mode bits for the status register */
139 #define IMUXMASK 0x07
140 #define EMUXMASK 0xf0
142 /* mode bits for counter controller */
144 #define LD2MODE4 0xb8
146 /* Minor allocations:
148 * U: board unit (0-1)
149 * CCCC: external multiplexer channel (0-15) (on AT-16P units)
150 * MMM: internal multiplexer channel (0-7) (on AIO8-P card)
153 #define UNIT(dev) ((minor(dev) & 0x80) >> 7)
154 #define CHANNEL(dev) (minor(dev) & 0x7f)
155 #define EMUX(chan) ((chan & 0x78) >> 3)
156 #define EMUXMAKE(chan) ((chan & 0x78) << 1)
157 #define IMUX(chan) (chan & 0x07)
158 #define LMINOR(unit, chan) ((unit << 7)+chan)
162 #define STATUS_UNUSED 0
163 #define STATUS_INUSE 1
164 #define STATUS_STOPPED 2
165 #define STATUS_INIT 3
167 /* Type definitions */
171 short status; /* the status of this chan */
172 struct selinfo readpoll; /* the poll() info */
173 u_short fifo[FIFOSIZE]; /* fifo for this chan */
174 int fifostart, fifoend; /* the ptrs showing where info is stored in fifo */
175 int fifosize, fifotrig; /* the current and trigger size of the fifo */
176 void *devfs_token; /* the devfs token for this chan */
182 struct isa_device *isaunit; /* ptr to isa device information */
183 talog_chan chan[NUMCHANNELS]; /* the device nodes */
184 int curchan; /* the current chan being intr handled */
185 int firstchan; /* the first chan to go to in list */
186 int state; /* is the node in setup or convert mode */
187 long microperiod; /* current microsecond period setting */
188 u_char perlo, perhi; /* current values to send to clock 2 after every intr */
192 /* Function Prototypes */
194 static int alog_probe (struct isa_device *idp); /* Check for alog board */
195 static int alog_attach (struct isa_device *idp); /* Take alog board */
196 static int sync_clock2 (int unit, long period); /* setup clock 2 period */
197 static int putfifo (talog_chan *pchan, u_short fifoent);
198 static int alog_open (dev_t dev, int oflags, int devtype, struct proc *p);
199 static int alog_close (dev_t dev, int fflag, int devtype, struct proc *p);
200 static int alog_ioctl (dev_t dev, int cmd, caddr_t data,
201 int fflag, struct proc *p);
202 static int alog_read (dev_t dev, struct uio *uio, int ioflag);
203 static int alog_poll (dev_t dev, int events, struct proc *p);
207 static int alog_devsw_installed = 0; /* Protect against reinit multiunit */
208 static talog_unit *alog_unit[NALOG]; /* data structs for each unit */
210 /* Character device switching structure */
211 static struct cdevsw alog_cdevsw = { alog_open, alog_close, alog_read,
212 nowrite, alog_ioctl, nostop, noreset,
213 nodevtotty, alog_poll, nommap,
214 nostrategy, "alog", NULL, -1 };
216 /* Structure expected to tell how to probe and attach the driver
217 * Must be published externally (cannot be static) */
218 struct isa_driver alogdriver = { alog_probe, alog_attach, "alog", 0 };
221 /* handle the ioctls */
222 static int alog_ioctl (dev_t dev, int cmd, caddr_t data,
223 int fflag, struct proc *p)
225 int unit = UNIT(dev);
226 int chan = CHANNEL(dev);
227 talog_unit *info = alog_unit[unit];
232 case FIONBIO: return 0; /* this allows for non-blocking ioctls */
234 case AD_NCHANS_GET: *(int *)data = NUMCHANNELS;
236 case AD_FIFOSIZE_GET: *(int *)data = FIFOSIZE;
239 case AD_FIFO_TRIGGER_GET: s = spltty();
240 *(int *)data = info->chan[chan].fifotrig;
244 case AD_FIFO_TRIGGER_SET:
246 if ((*(int *)data < 1) || (*(int *)data > FIFOSIZE))
251 info->chan[chan].fifotrig = *(int *)data;
255 case AD_STOP: s = spltty();
256 info->chan[chan].status = STATUS_STOPPED;
260 case AD_START: s = spltty();
261 info->chan[chan].status = STATUS_INUSE;
265 case AD_MICRO_PERIOD_SET:
267 if (sync_clock2 (unit, *(long *) data))
275 case AD_MICRO_PERIOD_GET: s = spltty();
276 *(long *)data = info->microperiod;
286 /* handle poll() based read polling */
287 static int alog_poll (dev_t dev, int events, struct proc *p)
289 int unit = UNIT(dev);
290 int chan = CHANNEL(dev);
291 talog_unit *info = alog_unit[unit];
295 if (events & (POLLIN | POLLRDNORM)) /* if polling for any/normal data */
296 if (info->chan[chan].fifosize >= info->chan[chan].fifotrig)
300 return events & (POLLIN | POLLRDNORM); /* ready for any/read */
304 /* record this request */
305 selrecord (p, &(info->chan[chan].readpoll));
307 return 0; /* not ready, yet */
311 return 0; /* not ready (any I never will be) */
315 /* how to read from the board */
316 static int alog_read (dev_t dev, struct uio *uio, int ioflag)
318 int unit = UNIT(dev);
319 int chan = CHANNEL(dev);
320 talog_unit *info = alog_unit[unit];
321 int s, oldtrig, toread, err = 0;
325 oldtrig = info->chan[chan].fifotrig; /* save official trigger value */
326 while (uio->uio_resid >= sizeof(u_short)) /* while uio has space */
328 if (!info->chan[chan].fifosize) /* if we have an empty fifo */
330 if (ioflag & IO_NDELAY) /* exit if we are non-blocking */
334 /* Start filling fifo on first blocking read */
335 if (info->chan[chan].status == STATUS_INIT)
336 info->chan[chan].status = STATUS_INUSE;
337 /* temporarily adjust the fifo trigger to be optimal size */
338 info->chan[chan].fifotrig =
339 min (READMAXTRIG, uio->uio_resid / sizeof(u_short));
340 /* lets sleep until we have some io available or timeout */
341 err = tsleep (&(info->chan[chan].fifo), ALOGPRI | PCATCH, ALOGMSG,
342 info->chan[chan].fifotrig*READTIMO);
343 if (err == EWOULDBLOCK)
344 { printf (DEVFORMAT ": read timeout\n", unit,
345 'a'+EMUX(chan), IMUX(chan));
347 if (err == ERESTART) err = EINTR; /* don't know how to restart */
348 if (err) break; /* exit if any kind of error or signal */
351 /* ok, now if we got here there is something to read from the fifo */
353 /* calculate how many entries we can read out from the fifostart
355 toread = min (uio->uio_resid / sizeof(u_short),
356 min (info->chan[chan].fifosize,
357 FIFOSIZE - info->chan[chan].fifostart));
358 /* perform the move, if there is an error then exit */
359 if (err = uiomove((caddr_t)
360 &(info->chan[chan].fifo[info->chan[chan].fifostart]),
361 toread * sizeof(u_short), uio)) break;
362 info->chan[chan].fifosize -= toread; /* fifo this much smaller */
363 info->chan[chan].fifostart += toread; /* we got this many more */
364 if (info->chan[chan].fifostart == FIFOSIZE)
365 info->chan[chan].fifostart = 0; /* wrap around fifostart */
368 info->chan[chan].fifotrig = oldtrig; /* restore trigger changes */
375 static int alog_open (dev_t dev, int oflags, int devtype, struct proc *p)
377 int unit = UNIT(dev); /* get unit no */
378 int chan = CHANNEL(dev); /* get channel no */
380 int s; /* priority */
383 if ((unit >= NALOG) || (unit >= MAXUNITS) || (chan >= NUMCHANNELS))
384 return ENXIO; /* unit and channel no ok ? */
385 if (!alog_unit[unit]) return ENXIO; /* unit attached */
386 info = alog_unit[unit]; /* ok, this is valid now */
388 if (info->chan[chan].status) return EBUSY; /* channel busy */
392 info->chan[chan].status = STATUS_INIT; /* channel open, read waiting */
393 info->chan[chan].fifostart = info->chan[chan].fifoend =
394 info->chan[chan].fifosize = 0;/* fifo empty */
395 info->chan[chan].fifotrig = DEFAULT_FIFO_TRIGGER;
396 if (info->firstchan < 0) /* if empty chain */
398 info->firstchan = info->curchan = chan; /* rev up the list */
399 info->chan[chan].nextchan = -1; /* end of the list */
401 else /* non empty list must insert */
403 if (chan < info->firstchan) /* this one must become first in list */
405 info->chan[chan].nextchan = info->firstchan;
406 info->firstchan = chan;
408 else /* insert this one as second - last in chan list */
410 cur = info->firstchan;
412 /* traverse list as long as cur is less than chan and cur is
413 * not last in list */
414 while ((info->chan[cur].nextchan < chan) &&
415 (info->chan[cur].nextchan >= 0))
416 cur = info->chan[cur].nextchan;
418 /* now cur should point to the entry right before yours */
419 info->chan[chan].nextchan = info->chan[cur].nextchan;
420 info->chan[cur].nextchan = chan; /* insert yours in */
424 return 0; /* open successful */
426 return EPERM; /* this is a read only device */
430 /* close a channel */
431 static int alog_close (dev_t dev, int fflag, int devtype, struct proc *p)
433 int unit = UNIT(dev);
434 int chan = CHANNEL(dev);
435 talog_unit *info = alog_unit[unit];
440 info->chan[chan].status = STATUS_UNUSED;
442 /* what if we are in the middle of a conversion ?
443 * then smoothly get us out of it: */
444 if (info->curchan == chan)
445 { /* if we are last in list set curchan to first in list */
446 if ((info->curchan = info->chan[chan].nextchan) < 0)
447 info->curchan = info->firstchan;
449 info->state = STATE_SETUP;
452 /* if this is the first channel, then make the second channel the first
453 * channel (note that if this is also the only channel firstchan becomes
454 * -1 and so the list is marked as empty */
456 if (chan == info->firstchan)
457 info->firstchan = info->chan[chan].nextchan;
458 else /* ok, so there must be at least 2 channels (and it is not the first) */
460 cur = info->firstchan;
462 /* find the entry before it (which must exist if you are closing) */
463 while (info->chan[cur].nextchan < chan)
464 cur = info->chan[cur].nextchan;
465 /* at this point we must have the entry before ours */
466 info->chan[cur].nextchan = info->chan[chan].nextchan; /* give our link */
472 return 0; /* close always successful */
476 /* The probing routine - returns number of bytes needed */
477 static int alog_probe (struct isa_device *idp)
479 int unit = idp->id_unit; /* this device unit number */
480 int iobase = idp->id_iobase; /* the base address of the unit */
483 if ((unit < 0) || (unit >= NALOG) || (unit >= MAXUNITS))
485 printf ("alog: invalid unit number (%d)\n", unit);
489 /* the unit number is ok, lets check if used */
492 printf ("alog: unit (%d) already attached\n", unit);
496 if (inb (iobase+STATUS) & EOC) return 0; /* End of conv bit should be 0 */
497 for (addr=0; addr<NUMIMUXES; addr++)
499 outb (iobase+STATUS, EMUXMASK|addr);/* output ones to upper nibbl+addr */
500 /* get back a zero in MSB and the addr where you put it */
501 if ((inb (iobase+STATUS) & (EOC|IMUXMASK)) != addr) return 0;
504 return NUMPORTS; /* this device needs this many ports */
508 /* setup the info structure correctly for reloading clock 2 after interrupt */
509 static int sync_clock2 (int unit, long period)
512 talog_unit *info = alog_unit[unit];
514 if ((period > MAX_MICRO_PERIOD) || (period < MIN_MICRO_PERIOD))
515 return -1; /* error period too long */
516 info->microperiod = period; /* record the period */
517 clockper = (CLOCK2FREQ * period) / PRIMARY_STATES;
518 info->perlo = clockper & 0xff; /* least sig byte of clock period */
519 info->perhi = ((clockper & 0xff00) >> 8); /* most sig byte of clock period */
524 /* The attachment routine - returns true on success */
525 static int alog_attach (struct isa_device *idp)
527 int unit = idp->id_unit; /* this device unit number */
528 int iobase = idp->id_iobase; /* the base address of the unit */
529 talog_unit *info; /* pointer to driver specific info for unit */
530 int chan; /* the channel used for creating devfs nodes */
532 if (!(info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT)))
534 printf ("alog%d: cannot allocate driver storage\n", unit);
537 alog_unit[unit] = info; /* make sure to save the pointer */
538 bzero (info, sizeof(*info)); /* clear info structure to all false */
539 info->isaunit = idp; /* store ptr to isa device information */
540 sync_clock2 (unit, DEFAULT_MICRO_PERIOD); /* setup perlo and perhi */
541 info->firstchan = -1; /* channel lists are empty */
543 /* insert devfs nodes */
546 for (chan=0; chan<NUMCHANNELS; chan++)
547 info->chan[chan].devfs_token =
548 devfs_add_devswf(&alog_cdevsw, LMINOR(unit, chan), DV_CHR,
549 UID_ROOT, GID_WHEEL, 0400, DEVFORMAT,
550 unit, 'a'+EMUX(chan), IMUX(chan));
553 printf ("alog%d: %d channels, %d bytes/FIFO, %d entry trigger\n",
554 unit, NUMCHANNELS, FIFOSIZE*sizeof(u_short),
555 DEFAULT_FIFO_TRIGGER);
556 alogintr (unit); /* start the periodic interrupting process */
557 return 1; /* obviously successful */
561 /* Unit interrupt handling routine (interrupts generated by clock 2) */
562 void alogintr (int unit)
564 talog_unit *info = alog_unit[unit];
565 int iobase = info->isaunit->id_iobase;
569 if (info->firstchan >= 0) /* ? is there even a chan list to traverse */
573 if (info->chan[info->curchan].status == STATUS_INUSE)
575 if (inb (iobase+STATUS) & EOC) /* check that conversion finished */
576 printf (DEVFORMAT ": incomplete conversion\n", unit,
577 'a'+EMUX(info->curchan), IMUX(info->curchan));
578 else /* conversion is finished (should always be) */
580 fifoent = (inb (iobase+ADHIGH) << 8) +
582 if (putfifo(&(info->chan[info->curchan]), fifoent))
584 printf (DEVFORMAT ": fifo overflow\n", unit,
585 'a'+EMUX(info->curchan), IMUX(info->curchan));
587 if (info->chan[info->curchan].fifosize >=
588 info->chan[info->curchan].fifotrig)
590 /* if we've reached trigger levels */
591 selwakeup (&(info->chan[info->curchan].readpoll));
592 wakeup (&(info->chan[info->curchan].fifo));
596 /* goto setup state for next channel on list */
597 if ((info->curchan = info->chan[info->curchan].nextchan) < 0)
598 info->curchan = info->firstchan;
599 /* notice lack of break here this implys a STATE_SETUP */
600 case STATE_SETUP: /* set the muxes and let them settle */
601 #if NUMCHANNELS > NUMIMUXES /* only do this if using external muxes */
603 EMUXMAKE(info->curchan) | IMUX(info->curchan) | IEN);
604 info->state = STATE_CONVERT;
609 EMUXMAKE(info->curchan) | IMUX(info->curchan) | IEN);
610 outb (iobase+ADHIGH, 0); /* start the conversion */
611 info->state = STATE_READ;
614 else /* this is kind of like an idle mode */
616 outb (iobase+STATUS, IEN); /* no list keep getting interrupts though */
617 /* since we have no open channels spin clock rate down to
618 * minimum to save interrupt overhead */
619 outb (iobase+CNTRCNTRL, LD2MODE4); /* counter 2 to mode 4 strobe */
620 outb (iobase+CNTR2, 0xff); /* longest period we can generate */
621 outb (iobase+CNTR2, 0xff);
624 outb (iobase+CNTRCNTRL, LD2MODE4); /* counter 2 to mode 4 strobe */
625 outb (iobase+CNTR2, info->perlo); /* low part of the period count */
626 outb (iobase+CNTR2, info->perhi); /* high part of the period count */
630 /* this will put an entry in fifo, returns 1 if the first item in
631 * fifo was wiped (overflow) or 0 if everything went fine */
632 static int putfifo (talog_chan *pchan, u_short fifoent)
634 pchan->fifo[pchan->fifoend] = fifoent; /* insert the entry in */
635 pchan->fifoend++; /* one more in fifo */
636 if (pchan->fifoend == FIFOSIZE) pchan->fifoend = 0; /* wrap around */
637 /* note: I did intend to write over the oldest entry on overflow */
638 if (pchan->fifosize == FIFOSIZE) /* overflowing state already */
641 if (pchan->fifostart == FIFOSIZE) pchan->fifostart = 0;
642 return 1; /* we overflowed */
644 pchan->fifosize++; /* actually one bigger, else same size */
645 return 0; /* went in just fine */
649 /* Driver initialization */
650 static void alog_drvinit (void *unused)
652 dev_t dev; /* Type for holding device major/minor numbers (int) */
654 if (!alog_devsw_installed)
656 dev = makedev (CDEV_MAJOR, 0); /* description of device major */
657 cdevsw_add (&dev, &alog_cdevsw, NULL); /* put driver in cdev table */
658 alog_devsw_installed=1;
662 /* System initialization call instance */
664 SYSINIT (alogdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR,