Constified ranges, board structures, and miscellaneous data.
[comedi.git] / comedi / drivers / pcl711.c
index 06ea6442b7a9e1df506d88a4c61c1a33f01fbd39..70c0961a38b3c8b2e9899935260dd87171a02e5d 100644 (file)
@@ -4,7 +4,7 @@
    and compatibles
 
    COMEDI - Linux Control and Measurement Device Interface
-   Copyright (C) 1998 David A. Schleef <ds@stm.lbl.gov>
+   Copyright (C) 1998 David A. Schleef <ds@schleef.org>
    Janne Jalkanen <jalkanen@cs.hut.fi>
    Eric Bunn <ebu@cs.hut.fi>
 
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  */
+/*
+Driver: pcl711.o
+Description: Advantech PCL-711 and 711b, ADLink ACL-8112
+Author: ds, Janne Jalkanen <jalkanen@cs.hut.fi>, Eric Bunn <ebu@cs.hut.fi>
+Status: mostly complete
+Devices: [Advantech] PCL-711 (pcl711), PCL-711B (pcl711b),
+  [AdLink] ACL-8112HG (acl8112hg), ACL-8112DG (acl8112dg)
+
+Since these boards do not have DMA or FIFOs, only immediate mode is
+supported.
+
+*/
 
 /*
    Dave Andruczyk <dave@tech.buffalostate.edu> also wrote a
 
  */
 
-#include <linux/kernel.h>
-#include <linux/module.h>
-#include <linux/sched.h>
-#include <linux/mm.h>
-#include <linux/malloc.h>
-#include <linux/errno.h>
+#include <linux/comedidev.h>
+
 #include <linux/ioport.h>
 #include <linux/delay.h>
-#include <linux/interrupt.h>
-#include <linux/timex.h>
-#include <linux/timer.h>
-#include <asm/io.h>
-#include <linux/comedidev.h>
+
 #include "8253.h"
 
 
 #define PCL711_DO_LO 13
 #define PCL711_DO_HI 14
 
-static comedi_lrange range_pcl711b_ai = { 5, {
+static const comedi_lrange range_pcl711b_ai = { 5, {
         BIP_RANGE( 5 ),
         BIP_RANGE( 2.5 ),
         BIP_RANGE( 1.25 ),
         BIP_RANGE( 0.625 ),
         BIP_RANGE( 0.3125 )
 }};
-static comedi_lrange range_acl8112hg_ai = { 12, {
+static const comedi_lrange range_acl8112hg_ai = { 12, {
        BIP_RANGE( 5 ),
        BIP_RANGE( 0.5 ),
        BIP_RANGE( 0.05 ),
@@ -106,7 +110,7 @@ static comedi_lrange range_acl8112hg_ai = { 12, {
        BIP_RANGE( 0.1 ),
        BIP_RANGE( 0.01 )
 }};
-static comedi_lrange range_acl8112dg_ai = { 9, {
+static const comedi_lrange range_acl8112dg_ai = { 9, {
         BIP_RANGE( 5 ),
         BIP_RANGE( 2.5 ),
         BIP_RANGE( 1.25 ),
@@ -118,8 +122,6 @@ static comedi_lrange range_acl8112dg_ai = { 9, {
        BIP_RANGE( 10 )
 }};
 
-typedef int bool;
-
 /*
  * flags
  */
@@ -127,10 +129,10 @@ typedef int bool;
 #define PCL711_TIMEOUT 100
 #define PCL711_DRDY 0x10
 
-int i8253_osc_base = 500;      /* 2 Mhz */
+static const int i8253_osc_base = 500; /* 2 Mhz */
 
 typedef struct {
-       char *name;
+       const char *name;
        int is_pcl711b;
        int is_8112;
        int is_dg;
@@ -138,10 +140,10 @@ typedef struct {
        int n_aichan;
        int n_aochan;
        int maxirq;
-       comedi_lrange * ai_range_type;
+       const comedi_lrange * ai_range_type;
 } boardtype;
 
-static boardtype boardtypes[] =
+static const boardtype boardtypes[] =
 {
        {"pcl711", 0, 0, 0, 5, 8, 1, 0, &range_bipolar5},
        {"pcl711b", 1, 0, 0, 5, 8, 1, 7, &range_pcl711b_ai},
@@ -149,16 +151,16 @@ static boardtype boardtypes[] =
        {"acl8112dg", 0, 1, 1, 9, 16, 2, 15, &range_acl8112dg_ai},
 };
 #define n_boardtypes (sizeof(boardtypes)/sizeof(boardtype))
-#define this_board ((boardtype *)dev->board_ptr)
+#define this_board ((const boardtype *)dev->board_ptr)
 
 static int pcl711_attach(comedi_device *dev,comedi_devconfig *it);
 static int pcl711_detach(comedi_device *dev);
-comedi_driver driver_pcl711={
+static comedi_driver driver_pcl711={
        driver_name:    "pcl711",
        module:         THIS_MODULE,
        attach:         pcl711_attach,
        detach:         pcl711_detach,
-       board_name:     boardtypes,
+       board_name:     &boardtypes[0].name,
        num_names:      n_boardtypes,
        offset:         sizeof(boardtype),
 };
@@ -171,11 +173,13 @@ typedef struct {
        int aip[8];
        int mode;
        lsampl_t ao_readback[2];
+       unsigned int divisor1;
+       unsigned int divisor2;
 } pcl711_private;
 
 #define devpriv ((pcl711_private *)dev->private)
 
-static void pcl711_interrupt(int irq, void *d, struct pt_regs *regs)
+static irqreturn_t pcl711_interrupt(int irq, void *d PT_REGS_ARG)
 {
        int lo, hi;
        int data;
@@ -195,8 +199,10 @@ static void pcl711_interrupt(int irq, void *d, struct pt_regs *regs)
                        outb(0, dev->iobase + PCL711_MODE);
                }
 
-               comedi_done(dev,s);
+               s->async->events |= COMEDI_CB_EOA;
        }
+       comedi_event(dev, s, s->async->events);
+       return IRQ_HANDLED;
 }
 
 static void pcl711_set_changain(comedi_device * dev, int chan)
@@ -204,7 +210,7 @@ static void pcl711_set_changain(comedi_device * dev, int chan)
        int chan_register;
 
        outb(CR_RANGE(chan), dev->iobase + PCL711_GAIN);
-       
+
        chan_register=CR_CHAN(chan);
 
        if (this_board->is_8112) {
@@ -234,12 +240,6 @@ static int pcl711_ai_insn(comedi_device *dev,comedi_subdevice *s,
 
        pcl711_set_changain(dev,insn->chanspec);
 
-       /*
-          a sensible precaution to wait for the mux to
-          settle here.  is 10us enough?
-       */
-       udelay(10);
-
        for(n=0;n<insn->n;n++){
                /*
                 *  Write the correct mode (software polling) and start polling by writing
@@ -257,10 +257,11 @@ static int pcl711_ai_insn(comedi_device *dev,comedi_subdevice *s,
                        hi = inb(dev->iobase + PCL711_AD_HI);
                        if (!(hi & PCL711_DRDY))
                                goto ok;
+                       comedi_udelay(1);
                }
                rt_printk("comedi%d: pcl711: A/D timeout\n", dev->minor);
                return -ETIME;
-       
+
 ok:
                lo = inb(dev->iobase + PCL711_AD_LO);
 
@@ -270,64 +271,140 @@ ok:
        return n;
 }
 
-#ifdef CONFIG_COMEDI_TRIG
-static int pcl711_ai_mode4(comedi_device * dev, comedi_subdevice * s, comedi_trig * it)
+static int pcl711_ai_cmdtest(comedi_device *dev, comedi_subdevice *s,
+       comedi_cmd *cmd)
 {
-       if (!this_board->is_pcl711b || dev->irq == 0)
-               return -EINVAL;
+       int tmp;
+       int err = 0;
 
-       pcl711_set_changain(dev,it->chanlist[0]);
+       /* step 1 */
+       tmp=cmd->start_src;
+       cmd->start_src &= TRIG_NOW;
+       if(!cmd->start_src || tmp!=cmd->start_src)err++;
 
-       /*
-        *  Set mode to "no internal trigger"
-        */
-       outb(devpriv->mode | 3, dev->iobase + PCL711_MODE);
+       tmp=cmd->scan_begin_src;
+       cmd->scan_begin_src &= TRIG_TIMER|TRIG_EXT;
+       if(!cmd->scan_begin_src || tmp!=cmd->scan_begin_src)err++;
+
+       tmp=cmd->convert_src;
+       cmd->convert_src &= TRIG_NOW;
+       if(!cmd->convert_src || tmp!=cmd->convert_src)err++;
+
+       tmp=cmd->scan_end_src;
+       cmd->scan_end_src &= TRIG_COUNT;
+       if(!cmd->scan_end_src || tmp!=cmd->scan_end_src)err++;
+
+       tmp=cmd->stop_src;
+       cmd->stop_src &= TRIG_COUNT|TRIG_NONE;
+       if(!cmd->stop_src || tmp!=cmd->stop_src)err++;
+
+       if(err)return 1;
+
+       /* step 2 */
+
+       if(cmd->scan_begin_src!=TRIG_TIMER &&
+          cmd->scan_begin_src!=TRIG_EXT)err++;
+       if(cmd->stop_src!=TRIG_COUNT &&
+          cmd->stop_src!=TRIG_NONE)err++;
+
+       if(err)return 2;
+
+       /* step 3 */
+
+       if(cmd->start_arg!=0){
+               cmd->start_arg=0;
+               err++;
+       }
+       if(cmd->scan_begin_src==TRIG_EXT){
+               if(cmd->scan_begin_arg!=0){
+                       cmd->scan_begin_arg=0;
+                       err++;
+               }
+       }else{
+#define MAX_SPEED 1000
+#define TIMER_BASE 100
+               if(cmd->scan_begin_arg<MAX_SPEED){
+                       cmd->scan_begin_arg=MAX_SPEED;
+                       err++;
+               }
+       }
+       if(cmd->convert_arg!=0){
+               cmd->convert_arg=0;
+               err++;
+       }
+       if(cmd->scan_end_arg!=cmd->chanlist_len){
+               cmd->scan_end_arg=cmd->chanlist_len;
+               err++;
+       }
+       if(cmd->stop_src==TRIG_NONE){
+               if(cmd->stop_arg!=0){
+                       cmd->stop_arg=0;
+                       err++;
+               }
+       }else{
+               /* ignore */
+       }
+
+       if(err)return 3;
+
+       /* step 4 */
+
+       if(cmd->scan_begin_src==TRIG_TIMER){
+               tmp = cmd->scan_begin_arg;
+               i8253_cascade_ns_to_timer_2div(TIMER_BASE,
+                       &devpriv->divisor1,&devpriv->divisor2,
+                       &cmd->scan_begin_arg, cmd->flags&TRIG_ROUND_MASK);
+               if(tmp!=cmd->scan_begin_arg)
+                       err++;
+       }
+
+       if(err)return 4;
 
        return 0;
 }
-#endif
-
 
-#ifdef CONFIG_COMEDI_TRIG
-static int pcl711_ai_mode1(comedi_device * dev, comedi_subdevice * s, comedi_trig * it)
+static int pcl711_ai_cmd(comedi_device *dev, comedi_subdevice *s)
 {
        int timer1,timer2;
+       comedi_cmd *cmd = &s->async->cmd;
 
-       if (!dev->irq)
-               return -EINVAL;
-
-       pcl711_set_changain(dev,it->chanlist[0]);
+       pcl711_set_changain(dev,cmd->chanlist[0]);
 
-       /*
-        *  Set timers
-        *      timer chip is an 8253, with timers 1 and 2
-        *      cascaded
-        *  0x74 = Select Counter 1 | LSB/MSB | Mode=2 | Binary
-        *        Mode 2 = Rate generator
-        *
-        *  0xb4 = Select Counter 2 | LSB/MSB | Mode=2 | Binary
-        */
+       if(cmd->scan_begin_src==TRIG_TIMER){
+               /*
+                *  Set timers
+                *      timer chip is an 8253, with timers 1 and 2
+                *      cascaded
+                *  0x74 = Select Counter 1 | LSB/MSB | Mode=2 | Binary
+                *        Mode 2 = Rate generator
+                *
+                *  0xb4 = Select Counter 2 | LSB/MSB | Mode=2 | Binary
+                */
 
-       i8253_cascade_ns_to_timer(i8253_osc_base,&timer1,&timer2,&it->trigvar,TRIG_ROUND_NEAREST);
+               i8253_cascade_ns_to_timer(i8253_osc_base,&timer1,&timer2,
+                       &cmd->scan_begin_arg,TRIG_ROUND_NEAREST);
 
-       outb(0x74, dev->iobase + PCL711_CTRCTL);
-       outb(timer1 & 0xff, dev->iobase + PCL711_CTR1);
-       outb((timer1 >> 8) & 0xff, dev->iobase + PCL711_CTR1);
-       outb(0xb4, dev->iobase + PCL711_CTRCTL);
-       outb(timer2 & 0xff, dev->iobase + PCL711_CTR2);
-       outb((timer2 >> 8) & 0xff, dev->iobase + PCL711_CTR2);
+               outb(0x74, dev->iobase + PCL711_CTRCTL);
+               outb(timer1 & 0xff, dev->iobase + PCL711_CTR1);
+               outb((timer1 >> 8) & 0xff, dev->iobase + PCL711_CTR1);
+               outb(0xb4, dev->iobase + PCL711_CTRCTL);
+               outb(timer2 & 0xff, dev->iobase + PCL711_CTR2);
+               outb((timer2 >> 8) & 0xff, dev->iobase + PCL711_CTR2);
 
-       /* clear pending interrupts (just in case) */
-       outb(0, dev->iobase + PCL711_CLRINTR);
+               /* clear pending interrupts (just in case) */
+               outb(0, dev->iobase + PCL711_CLRINTR);
 
-       /*
-        *  Set mode to IRQ transfer
-        */
-       outb(devpriv->mode | 6, dev->iobase + PCL711_MODE);
+               /*
+                *  Set mode to IRQ transfer
+                */
+               outb(devpriv->mode | 6, dev->iobase + PCL711_MODE);
+       }else{
+               /* external trigger */
+               outb(devpriv->mode | 3, dev->iobase + PCL711_MODE);
+       }
 
        return 0;
 }
-#endif
 
 /*
    analog output
@@ -400,7 +477,7 @@ static int pcl711_detach(comedi_device * dev)
        printk("comedi%d: pcl711: remove\n", dev->minor);
 
        if (dev->irq)
-               free_irq(dev->irq, dev);
+               comedi_free_irq(dev->irq, dev);
 
        if (dev->iobase)
                release_region(dev->iobase, PCL711_SIZE);
@@ -412,19 +489,18 @@ static int pcl711_detach(comedi_device * dev)
 static int pcl711_attach(comedi_device * dev, comedi_devconfig * it)
 {
        int ret;
-       int iobase;
-       int irq;
+       unsigned long iobase;
+       unsigned int irq;
        comedi_subdevice *s;
 
        /* claim our I/O space */
 
        iobase = it->options[0];
-       printk("comedi%d: pcl711: 0x%04x ", dev->minor, iobase);
-       if (check_region(iobase, PCL711_SIZE) < 0) {
+       printk("comedi%d: pcl711: 0x%04lx ", dev->minor, iobase);
+       if (!request_region(iobase, PCL711_SIZE, "pcl711")) {
                printk("I/O port conflict\n");
                return -EIO;
        }
-       request_region(iobase, PCL711_SIZE, "pcl711");
        dev->iobase = iobase;
 
        /* there should be a sanity check here */
@@ -434,22 +510,21 @@ static int pcl711_attach(comedi_device * dev, comedi_devconfig * it)
 
        /* grab our IRQ */
        irq = it->options[1];
-       if (irq < 0 || irq > this_board->maxirq) {
+       if (irq > this_board->maxirq) {
                printk("irq out of range\n");
                return -EINVAL;
        }
        if (irq) {
                if (comedi_request_irq(irq, pcl711_interrupt, 0, "pcl711", dev)) {
-                       printk("unable to allocate irq %d\n", irq);
+                       printk("unable to allocate irq %u\n", irq);
                        return -EINVAL;
                } else {
-                       printk("( irq = %d )\n", irq);
+                       printk("( irq = %u )\n", irq);
                }
        }
        dev->irq = irq;
 
-       dev->n_subdevices = 4;
-       if((ret=alloc_subdevices(dev))<0)
+       if((ret=alloc_subdevices(dev, 4))<0)
                return ret;
        if((ret=alloc_private(dev,sizeof(pcl711_private)))<0)
                return ret;
@@ -457,21 +532,23 @@ static int pcl711_attach(comedi_device * dev, comedi_devconfig * it)
        s = dev->subdevices + 0;
        /* AI subdevice */
        s->type = COMEDI_SUBD_AI;
-       s->subdev_flags = SDF_READABLE;
+       s->subdev_flags = SDF_READABLE | SDF_GROUND;
        s->n_chan = this_board->n_aichan;
        s->maxdata = 0xfff;
        s->len_chanlist = 1;
        s->range_table = this_board->ai_range_type;
        s->insn_read = pcl711_ai_insn;
-#ifdef CONFIG_COMEDI_TRIG
-       s->trig[1] = pcl711_ai_mode1;
-       s->trig[4] = pcl711_ai_mode4;
-#endif
+       if(irq){
+               dev->read_subdev = s;
+               s->subdev_flags |= SDF_CMD_READ;
+               s->do_cmdtest = pcl711_ai_cmdtest;
+               s->do_cmd = pcl711_ai_cmd;
+       }
 
        s++;
        /* AO subdevice */
        s->type = COMEDI_SUBD_AO;
-       s->subdev_flags = SDF_WRITEABLE;
+       s->subdev_flags = SDF_WRITABLE;
        s->n_chan = this_board->n_aochan;
        s->maxdata = 0xfff;
        s->len_chanlist = 1;
@@ -492,7 +569,7 @@ static int pcl711_attach(comedi_device * dev, comedi_devconfig * it)
        s++;
        /* 16-bit digital out */
        s->type = COMEDI_SUBD_DO;
-       s->subdev_flags = SDF_WRITEABLE;
+       s->subdev_flags = SDF_WRITABLE;
        s->n_chan = 16;
        s->maxdata = 1;
        s->len_chanlist = 16;