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>
49 #include <sys/kernel.h>
51 #include <sys/fcntl.h>
52 #include <sys/malloc.h>
54 #include <sys/vnode.h>
55 #include <sys/filio.h>
56 #include <i386/isa/isa_device.h>
57 #include <sys/alogio.h>
58 #include <sys/dataacq.h>
60 #include "opt_devfs.h"
62 #include <sys/devfsext.h>
67 /* Tests have shown that increasing the fifo size
68 * beyond 64 entries for this particular piece of hardware is
72 #define FIFOSIZE ALOG_FIFOSIZE
77 #ifdef ALOG_FIFO_TRIGGER
78 #define DEFAULT_FIFO_TRIGGER ALOG_FIFO_TRIGGER
80 #define DEFAULT_FIFO_TRIGGER 1
84 #define NUMCHANNELS ALOG_CHANNELS
86 #define NUMCHANNELS 128
90 #define READTIMO ALOG_TIMO
92 #define READTIMO (MAX_MICRO_PERIOD*NUMCHANNELS/500000*hz)
106 #define CNTRCNTRL 0x7
108 #define DEVFORMAT "alog%d%c%d"
109 #define CLOCK2FREQ 4.165
110 #define MIN_MICRO_PERIOD 25
111 #define MAX_MICRO_PERIOD (65535/CLOCK2FREQ*PRIMARY_STATES)
112 #define DEFAULT_MICRO_PERIOD MAX_MICRO_PERIOD
113 #define READMAXTRIG 0.75*FIFOSIZE
114 #define ALOGPRI PRIBIO
115 #define ALOGMSG "alogio"
117 #define PRIMARY_STATES 2 /* Setup and conversion are clock tick consuming */
118 #define STATE_SETUP 0
119 #define STATE_CONVERT 1
122 /* Notes on interrupt driven A/D conversion:
123 * On the AIO8-P, interrupt driven conversion (the only type supported by this
124 * driver) is facilitated through 8253 timer #2. In order for interrrupts to
125 * be generated you must connect line 6 to line 24 (counter 2 output to
126 * interrupt input) and line 23 to line 29 (counter 2 gate to +5VDC).
127 * Due to the design of the AIO8-P this precludes the use of programmable
131 /* mode bits for the status register */
135 #define IMUXMASK 0x07
136 #define EMUXMASK 0xf0
138 /* mode bits for counter controller */
140 #define LD2MODE4 0xb8
142 /* Minor allocations:
144 * U: board unit (0-1)
145 * CCCC: external multiplexer channel (0-15) (on AT-16P units)
146 * MMM: internal multiplexer channel (0-7) (on AIO8-P card)
149 #define UNIT(dev) ((minor(dev) & 0x80) >> 7)
150 #define CHANNEL(dev) (minor(dev) & 0x7f)
151 #define EMUX(chan) ((chan & 0x78) >> 3)
152 #define EMUXMAKE(chan) ((chan & 0x78) << 1)
153 #define IMUX(chan) (chan & 0x07)
154 #define LMINOR(unit, chan) ((unit << 7)+chan)
158 #define STATUS_UNUSED 0
159 #define STATUS_INUSE 1
160 #define STATUS_STOPPED 2
161 #define STATUS_INIT 3
163 /* Type definitions */
167 short status; /* the status of this chan */
168 struct selinfo readpoll; /* the poll() info */
169 u_short fifo[FIFOSIZE]; /* fifo for this chan */
170 int fifostart, fifoend; /* the ptrs showing where info is stored in fifo */
171 int fifosize, fifotrig; /* the current and trigger size of the fifo */
172 void *devfs_token; /* the devfs token for this chan */
178 struct isa_device *isaunit; /* ptr to isa device information */
179 talog_chan chan[NUMCHANNELS]; /* the device nodes */
180 int curchan; /* the current chan being intr handled */
181 int firstchan; /* the first chan to go to in list */
182 int state; /* is the node in setup or convert mode */
183 long microperiod; /* current microsecond period setting */
184 u_char perlo, perhi; /* current values to send to clock 2 after every intr */
188 /* Function Prototypes */
190 static int alog_probe (struct isa_device *idp); /* Check for alog board */
191 static int alog_attach (struct isa_device *idp); /* Take alog board */
192 static int sync_clock2 (int unit, long period); /* setup clock 2 period */
193 static int putfifo (talog_chan *pchan, u_short fifoent);
194 static int alog_open (dev_t dev, int oflags, int devtype, struct proc *p);
195 static int alog_close (dev_t dev, int fflag, int devtype, struct proc *p);
196 static int alog_ioctl (dev_t dev, int cmd, caddr_t data,
197 int fflag, struct proc *p);
198 static int alog_read (dev_t dev, struct uio *uio, int ioflag);
199 static int alog_poll (dev_t dev, int events, struct proc *p);
203 static int alog_devsw_installed = 0; /* Protect against reinit multiunit */
204 static talog_unit *alog_unit[NALOG]; /* data structs for each unit */
206 /* Character device switching structure */
207 static struct cdevsw alog_cdevsw = { alog_open, alog_close, alog_read,
208 nowrite, alog_ioctl, nostop, noreset,
209 nodevtotty, alog_poll, nommap,
210 nostrategy, "alog", NULL, -1 };
212 /* Structure expected to tell how to probe and attach the driver
213 * Must be published externally (cannot be static) */
214 struct isa_driver alogdriver = { alog_probe, alog_attach, "alog", 0 };
217 /* handle the ioctls */
218 static int alog_ioctl (dev_t dev, int cmd, caddr_t data,
219 int fflag, struct proc *p)
221 int unit = UNIT(dev);
222 int chan = CHANNEL(dev);
223 talog_unit *info = alog_unit[unit];
228 case FIONBIO: return 0; /* this allows for non-blocking ioctls */
230 case AD_NCHANS_GET: *(int *)data = NUMCHANNELS;
232 case AD_FIFOSIZE_GET: *(int *)data = FIFOSIZE;
235 case AD_FIFO_TRIGGER_GET: s = spltty();
236 *(int *)data = info->chan[chan].fifotrig;
240 case AD_FIFO_TRIGGER_SET:
242 if ((*(int *)data < 1) || (*(int *)data > FIFOSIZE))
247 info->chan[chan].fifotrig = *(int *)data;
251 case AD_STOP: s = spltty();
252 info->chan[chan].status = STATUS_STOPPED;
256 case AD_START: s = spltty();
257 info->chan[chan].status = STATUS_INUSE;
261 case AD_MICRO_PERIOD_SET:
263 if (sync_clock2 (unit, *(long *) data))
271 case AD_MICRO_PERIOD_GET: s = spltty();
272 *(long *)data = info->microperiod;
282 /* handle poll() based read polling */
283 static int alog_poll (dev_t dev, int events, struct proc *p)
285 int unit = UNIT(dev);
286 int chan = CHANNEL(dev);
287 talog_unit *info = alog_unit[unit];
291 if (events & (POLLIN | POLLRDNORM)) /* if polling for any/normal data */
292 if (info->chan[chan].fifosize >= info->chan[chan].fifotrig)
296 return events & (POLLIN | POLLRDNORM); /* ready for any/read */
300 /* record this request */
301 selrecord (p, &(info->chan[chan].readpoll));
303 return 0; /* not ready, yet */
307 return 0; /* not ready (any I never will be) */
311 /* how to read from the board */
312 static int alog_read (dev_t dev, struct uio *uio, int ioflag)
314 int unit = UNIT(dev);
315 int chan = CHANNEL(dev);
316 talog_unit *info = alog_unit[unit];
317 int s, oldtrig, toread, err = 0;
321 oldtrig = info->chan[chan].fifotrig; /* save official trigger value */
322 while (uio->uio_resid >= sizeof(u_short)) /* while uio has space */
324 if (!info->chan[chan].fifosize) /* if we have an empty fifo */
326 if (ioflag & IO_NDELAY) /* exit if we are non-blocking */
330 /* Start filling fifo on first blocking read */
331 if (info->chan[chan].status == STATUS_INIT)
332 info->chan[chan].status = STATUS_INUSE;
333 /* temporarily adjust the fifo trigger to be optimal size */
334 info->chan[chan].fifotrig =
335 min (READMAXTRIG, uio->uio_resid / sizeof(u_short));
336 /* lets sleep until we have some io available or timeout */
337 err = tsleep (&(info->chan[chan].fifo), ALOGPRI | PCATCH, ALOGMSG,
338 info->chan[chan].fifotrig*READTIMO);
339 if (err == EWOULDBLOCK)
340 { printf (DEVFORMAT ": read timeout\n", unit,
341 'a'+EMUX(chan), IMUX(chan));
343 if (err == ERESTART) err = EINTR; /* don't know how to restart */
344 if (err) break; /* exit if any kind of error or signal */
347 /* ok, now if we got here there is something to read from the fifo */
349 /* calculate how many entries we can read out from the fifostart
351 toread = min (uio->uio_resid / sizeof(u_short),
352 min (info->chan[chan].fifosize,
353 FIFOSIZE - info->chan[chan].fifostart));
354 /* perform the move, if there is an error then exit */
355 if (err = uiomove((caddr_t)
356 &(info->chan[chan].fifo[info->chan[chan].fifostart]),
357 toread * sizeof(u_short), uio)) break;
358 info->chan[chan].fifosize -= toread; /* fifo this much smaller */
359 info->chan[chan].fifostart += toread; /* we got this many more */
360 if (info->chan[chan].fifostart == FIFOSIZE)
361 info->chan[chan].fifostart = 0; /* wrap around fifostart */
364 info->chan[chan].fifotrig = oldtrig; /* restore trigger changes */
371 static int alog_open (dev_t dev, int oflags, int devtype, struct proc *p)
373 int unit = UNIT(dev); /* get unit no */
374 int chan = CHANNEL(dev); /* get channel no */
376 int s; /* priority */
379 if ((unit >= NALOG) || (unit >= MAXUNITS) || (chan >= NUMCHANNELS))
380 return ENXIO; /* unit and channel no ok ? */
381 if (!alog_unit[unit]) return ENXIO; /* unit attached */
382 info = alog_unit[unit]; /* ok, this is valid now */
384 if (info->chan[chan].status) return EBUSY; /* channel busy */
388 info->chan[chan].status = STATUS_INIT; /* channel open, read waiting */
389 info->chan[chan].fifostart = info->chan[chan].fifoend =
390 info->chan[chan].fifosize = 0;/* fifo empty */
391 info->chan[chan].fifotrig = DEFAULT_FIFO_TRIGGER;
392 if (info->firstchan < 0) /* if empty chain */
394 info->firstchan = info->curchan = chan; /* rev up the list */
395 info->chan[chan].nextchan = -1; /* end of the list */
397 else /* non empty list must insert */
399 if (chan < info->firstchan) /* this one must become first in list */
401 info->chan[chan].nextchan = info->firstchan;
402 info->firstchan = chan;
404 else /* insert this one as second - last in chan list */
406 cur = info->firstchan;
408 /* traverse list as long as cur is less than chan and cur is
409 * not last in list */
410 while ((info->chan[cur].nextchan < chan) &&
411 (info->chan[cur].nextchan >= 0))
412 cur = info->chan[cur].nextchan;
414 /* now cur should point to the entry right before yours */
415 info->chan[chan].nextchan = info->chan[cur].nextchan;
416 info->chan[cur].nextchan = chan; /* insert yours in */
420 return 0; /* open successful */
422 return EPERM; /* this is a read only device */
426 /* close a channel */
427 static int alog_close (dev_t dev, int fflag, int devtype, struct proc *p)
429 int unit = UNIT(dev);
430 int chan = CHANNEL(dev);
431 talog_unit *info = alog_unit[unit];
436 info->chan[chan].status = STATUS_UNUSED;
438 /* what if we are in the middle of a conversion ?
439 * then smoothly get us out of it: */
440 if (info->curchan == chan)
441 { /* if we are last in list set curchan to first in list */
442 if ((info->curchan = info->chan[chan].nextchan) < 0)
443 info->curchan = info->firstchan;
445 info->state = STATE_SETUP;
448 /* if this is the first channel, then make the second channel the first
449 * channel (note that if this is also the only channel firstchan becomes
450 * -1 and so the list is marked as empty */
452 if (chan == info->firstchan)
453 info->firstchan = info->chan[chan].nextchan;
454 else /* ok, so there must be at least 2 channels (and it is not the first) */
456 cur = info->firstchan;
458 /* find the entry before it (which must exist if you are closing) */
459 while (info->chan[cur].nextchan < chan)
460 cur = info->chan[cur].nextchan;
461 /* at this point we must have the entry before ours */
462 info->chan[cur].nextchan = info->chan[chan].nextchan; /* give our link */
468 return 0; /* close always successful */
472 /* The probing routine - returns number of bytes needed */
473 static int alog_probe (struct isa_device *idp)
475 int unit = idp->id_unit; /* this device unit number */
476 int iobase = idp->id_iobase; /* the base address of the unit */
479 if ((unit < 0) || (unit >= NALOG) || (unit >= MAXUNITS))
481 printf ("alog: invalid unit number (%d)\n", unit);
485 /* the unit number is ok, lets check if used */
488 printf ("alog: unit (%d) already attached\n", unit);
492 if (inb (iobase+STATUS) & EOC) return 0; /* End of conv bit should be 0 */
493 for (addr=0; addr<NUMIMUXES; addr++)
495 outb (iobase+STATUS, EMUXMASK|addr);/* output ones to upper nibbl+addr */
496 /* get back a zero in MSB and the addr where you put it */
497 if ((inb (iobase+STATUS) & (EOC|IMUXMASK)) != addr) return 0;
500 return NUMPORTS; /* this device needs this many ports */
504 /* setup the info structure correctly for reloading clock 2 after interrupt */
505 static int sync_clock2 (int unit, long period)
508 talog_unit *info = alog_unit[unit];
510 if ((period > MAX_MICRO_PERIOD) || (period < MIN_MICRO_PERIOD))
511 return -1; /* error period too long */
512 info->microperiod = period; /* record the period */
513 clockper = (CLOCK2FREQ * period) / PRIMARY_STATES;
514 info->perlo = clockper & 0xff; /* least sig byte of clock period */
515 info->perhi = ((clockper & 0xff00) >> 8); /* most sig byte of clock period */
520 /* The attachment routine - returns true on success */
521 static int alog_attach (struct isa_device *idp)
523 int unit = idp->id_unit; /* this device unit number */
524 int iobase = idp->id_iobase; /* the base address of the unit */
525 talog_unit *info; /* pointer to driver specific info for unit */
526 int chan; /* the channel used for creating devfs nodes */
528 if (!(info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT)))
530 printf ("alog%d: cannot allocate driver storage\n", unit);
533 alog_unit[unit] = info; /* make sure to save the pointer */
534 bzero (info, sizeof(*info)); /* clear info structure to all false */
535 info->isaunit = idp; /* store ptr to isa device information */
536 sync_clock2 (unit, DEFAULT_MICRO_PERIOD); /* setup perlo and perhi */
537 info->firstchan = -1; /* channel lists are empty */
539 /* insert devfs nodes */
542 for (chan=0; chan<NUMCHANNELS; chan++)
543 info->chan[chan].devfs_token =
544 devfs_add_devswf(&alog_cdevsw, LMINOR(unit, chan), DV_CHR,
545 UID_ROOT, GID_WHEEL, 0400, DEVFORMAT,
546 unit, 'a'+EMUX(chan), IMUX(chan));
549 printf ("alog%d: %d channels, %d bytes/FIFO, %d entry trigger\n",
550 unit, NUMCHANNELS, FIFOSIZE*sizeof(u_short),
551 DEFAULT_FIFO_TRIGGER);
552 alogintr (unit); /* start the periodic interrupting process */
553 return 1; /* obviously successful */
557 /* Unit interrupt handling routine (interrupts generated by clock 2) */
558 void alogintr (int unit)
560 talog_unit *info = alog_unit[unit];
561 int iobase = info->isaunit->id_iobase;
565 if (info->firstchan >= 0) /* ? is there even a chan list to traverse */
569 if (info->chan[info->curchan].status == STATUS_INUSE)
571 if (inb (iobase+STATUS) & EOC) /* check that conversion finished */
572 printf (DEVFORMAT ": incomplete conversion\n", unit,
573 'a'+EMUX(info->curchan), IMUX(info->curchan));
574 else /* conversion is finished (should always be) */
576 fifoent = (inb (iobase+ADHIGH) << 8) +
578 if (putfifo(&(info->chan[info->curchan]), fifoent))
580 printf (DEVFORMAT ": fifo overflow\n", unit,
581 'a'+EMUX(info->curchan), IMUX(info->curchan));
583 if (info->chan[info->curchan].fifosize >=
584 info->chan[info->curchan].fifotrig)
586 /* if we've reached trigger levels */
587 selwakeup (&(info->chan[info->curchan].readpoll));
588 wakeup (&(info->chan[info->curchan].fifo));
592 /* goto setup state for next channel on list */
593 if ((info->curchan = info->chan[info->curchan].nextchan) < 0)
594 info->curchan = info->firstchan;
595 /* notice lack of break here this implys a STATE_SETUP */
596 case STATE_SETUP: /* set the muxes and let them settle */
597 #if NUMCHANNELS > NUMIMUXES /* only do this if using external muxes */
599 EMUXMAKE(info->curchan) | IMUX(info->curchan) | IEN);
600 info->state = STATE_CONVERT;
605 EMUXMAKE(info->curchan) | IMUX(info->curchan) | IEN);
606 outb (iobase+ADHIGH, 0); /* start the conversion */
607 info->state = STATE_READ;
610 else /* this is kind of like an idle mode */
612 outb (iobase+STATUS, IEN); /* no list keep getting interrupts though */
613 /* since we have no open channels spin clock rate down to
614 * minimum to save interrupt overhead */
615 outb (iobase+CNTRCNTRL, LD2MODE4); /* counter 2 to mode 4 strobe */
616 outb (iobase+CNTR2, 0xff); /* longest period we can generate */
617 outb (iobase+CNTR2, 0xff);
620 outb (iobase+CNTRCNTRL, LD2MODE4); /* counter 2 to mode 4 strobe */
621 outb (iobase+CNTR2, info->perlo); /* low part of the period count */
622 outb (iobase+CNTR2, info->perhi); /* high part of the period count */
626 /* this will put an entry in fifo, returns 1 if the first item in
627 * fifo was wiped (overflow) or 0 if everything went fine */
628 static int putfifo (talog_chan *pchan, u_short fifoent)
630 pchan->fifo[pchan->fifoend] = fifoent; /* insert the entry in */
631 pchan->fifoend++; /* one more in fifo */
632 if (pchan->fifoend == FIFOSIZE) pchan->fifoend = 0; /* wrap around */
633 /* note: I did intend to write over the oldest entry on overflow */
634 if (pchan->fifosize == FIFOSIZE) /* overflowing state already */
637 if (pchan->fifostart == FIFOSIZE) pchan->fifostart = 0;
638 return 1; /* we overflowed */
640 pchan->fifosize++; /* actually one bigger, else same size */
641 return 0; /* went in just fine */
645 /* Driver initialization */
646 static void alog_drvinit (void *unused)
648 dev_t dev; /* Type for holding device major/minor numbers (int) */
650 if (!alog_devsw_installed)
652 dev = makedev (CDEV_MAJOR, 0); /* description of device major */
653 cdevsw_add (&dev, &alog_cdevsw, NULL); /* put driver in cdev table */
654 alog_devsw_installed=1;
658 /* System initialization call instance */
660 SYSINIT (alogdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE+CDEV_MAJOR,