Merge branch 'linus' into x86/microcode, to pick up merge window changes
[cascardo/linux.git] / drivers / staging / comedi / drivers / das16m1.c
index 1a8cf92..bb8d6ec 100644 (file)
 #include "8255.h"
 #include "comedi_8254.h"
 
-#define DAS16M1_SIZE2 8
-
-#define FIFO_SIZE 1024         /*  1024 sample fifo */
-
 /*
  * Register map (dev->iobase)
  */
 #define DAS16M1_Q_REG                  0x07
 #define DAS16M1_Q_CHAN(x)              (((x) & 0x7) << 0)
 #define DAS16M1_Q_RANGE(x)             (((x) & 0xf) << 4)
-#define DAS16M1_8254_FIRST             0x8
-#define DAS16M1_8254_SECOND            0xc
-#define DAS16M1_82C55                  0x400
-#define DAS16M1_8254_THIRD             0x404
+#define DAS16M1_8254_IOBASE1           0x08
+#define DAS16M1_8254_IOBASE2           0x0c
+#define DAS16M1_8255_IOBASE            0x400
+#define DAS16M1_8254_IOBASE3           0x404
+
+#define DAS16M1_SIZE2                  0x08
+
+#define DAS16M1_AI_FIFO_SZ             1024    /* # samples */
 
 static const struct comedi_lrange range_das16m1 = {
        9, {
@@ -103,20 +103,45 @@ static const struct comedi_lrange range_das16m1 = {
        }
 };
 
-struct das16m1_private_struct {
+struct das16m1_private {
        struct comedi_8254 *counter;
        unsigned int intr_ctrl;
        unsigned int adc_count;
        u16 initial_hw_count;
-       unsigned short ai_buffer[FIFO_SIZE];
+       unsigned short ai_buffer[DAS16M1_AI_FIFO_SZ];
        unsigned long extra_iobase;
 };
 
-static void munge_sample_array(unsigned short *array, unsigned int num_elements)
+static void das16m1_ai_set_queue(struct comedi_device *dev,
+                                unsigned int *chanspec, unsigned int len)
+{
+       unsigned int i;
+
+       for (i = 0; i < len; i++) {
+               unsigned int chan = CR_CHAN(chanspec[i]);
+               unsigned int range = CR_RANGE(chanspec[i]);
+
+               outb(i, dev->iobase + DAS16M1_Q_ADDR_REG);
+               outb(DAS16M1_Q_CHAN(chan) | DAS16M1_Q_RANGE(range),
+                    dev->iobase + DAS16M1_Q_REG);
+       }
+}
+
+static void das16m1_ai_munge(struct comedi_device *dev,
+                            struct comedi_subdevice *s,
+                            void *data, unsigned int num_bytes,
+                            unsigned int start_chan_index)
 {
+       unsigned short *array = data;
+       unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
        unsigned int i;
 
-       for (i = 0; i < num_elements; i++)
+       /*
+        * The fifo values have the channel number in the lower 4-bits and
+        * the sample in the upper 12-bits. This just shifts the values
+        * to remove the channel numbers.
+        */
+       for (i = 0; i < nsamples; i++)
                array[i] = DAS16M1_AI_TO_SAMPLE(array[i]);
 }
 
@@ -148,8 +173,9 @@ static int das16m1_ai_check_chanlist(struct comedi_device *dev,
        return 0;
 }
 
-static int das16m1_cmd_test(struct comedi_device *dev,
-                           struct comedi_subdevice *s, struct comedi_cmd *cmd)
+static int das16m1_ai_cmdtest(struct comedi_device *dev,
+                             struct comedi_subdevice *s,
+                             struct comedi_cmd *cmd)
 {
        int err = 0;
 
@@ -219,13 +245,13 @@ static int das16m1_cmd_test(struct comedi_device *dev,
        return 0;
 }
 
-static int das16m1_cmd_exec(struct comedi_device *dev,
-                           struct comedi_subdevice *s)
+static int das16m1_ai_cmd(struct comedi_device *dev,
+                         struct comedi_subdevice *s)
 {
-       struct das16m1_private_struct *devpriv = dev->private;
+       struct das16m1_private *devpriv = dev->private;
        struct comedi_async *async = s->async;
        struct comedi_cmd *cmd = &async->cmd;
-       unsigned int byte, i;
+       unsigned int byte;
 
        /*  set software count */
        devpriv->adc_count = 0;
@@ -244,14 +270,7 @@ static int das16m1_cmd_exec(struct comedi_device *dev,
         */
        devpriv->initial_hw_count = comedi_8254_read(devpriv->counter, 1);
 
-       /* setup channel/gain queue */
-       for (i = 0; i < cmd->chanlist_len; i++) {
-               outb(i, dev->iobase + DAS16M1_Q_ADDR_REG);
-               byte =
-                   DAS16M1_Q_CHAN(CR_CHAN(cmd->chanlist[i])) |
-                   DAS16M1_Q_RANGE(CR_RANGE(cmd->chanlist[i]));
-               outb(byte, dev->iobase + DAS16M1_Q_REG);
-       }
+       das16m1_ai_set_queue(dev, cmd->chanlist, cmd->chanlist_len);
 
        /* enable interrupts and set internal pacer counter mode and counts */
        devpriv->intr_ctrl &= ~DAS16M1_INTR_CTRL_PACER_MASK;
@@ -283,9 +302,10 @@ static int das16m1_cmd_exec(struct comedi_device *dev,
        return 0;
 }
 
-static int das16m1_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
+static int das16m1_ai_cancel(struct comedi_device *dev,
+                            struct comedi_subdevice *s)
 {
-       struct das16m1_private_struct *devpriv = dev->private;
+       struct das16m1_private *devpriv = dev->private;
 
        /* disable interrupts and pacer */
        devpriv->intr_ctrl &= ~(DAS16M1_INTR_CTRL_INTE |
@@ -308,21 +328,17 @@ static int das16m1_ai_eoc(struct comedi_device *dev,
        return -EBUSY;
 }
 
-static int das16m1_ai_rinsn(struct comedi_device *dev,
-                           struct comedi_subdevice *s,
-                           struct comedi_insn *insn, unsigned int *data)
+static int das16m1_ai_insn_read(struct comedi_device *dev,
+                               struct comedi_subdevice *s,
+                               struct comedi_insn *insn,
+                               unsigned int *data)
 {
        int ret;
-       int n;
-       int byte;
+       int i;
 
-       /* setup channel/gain queue */
-       outb(0, dev->iobase + DAS16M1_Q_ADDR_REG);
-       byte = DAS16M1_Q_CHAN(CR_CHAN(insn->chanspec)) |
-              DAS16M1_Q_RANGE(CR_RANGE(insn->chanspec));
-       outb(byte, dev->iobase + DAS16M1_Q_REG);
+       das16m1_ai_set_queue(dev, &insn->chanspec, 1);
 
-       for (n = 0; n < insn->n; n++) {
+       for (i = 0; i < insn->n; i++) {
                unsigned short val;
 
                /* clear interrupt */
@@ -335,29 +351,26 @@ static int das16m1_ai_rinsn(struct comedi_device *dev,
                        return ret;
 
                val = inw(dev->iobase + DAS16M1_AI_REG);
-               data[n] = DAS16M1_AI_TO_SAMPLE(val);
+               data[i] = DAS16M1_AI_TO_SAMPLE(val);
        }
 
-       return n;
+       return insn->n;
 }
 
-static int das16m1_di_rbits(struct comedi_device *dev,
-                           struct comedi_subdevice *s,
-                           struct comedi_insn *insn, unsigned int *data)
+static int das16m1_di_insn_bits(struct comedi_device *dev,
+                               struct comedi_subdevice *s,
+                               struct comedi_insn *insn,
+                               unsigned int *data)
 {
-       unsigned int bits;
-
-       bits = inb(dev->iobase + DAS16M1_DI_REG) & 0xf;
-       data[1] = bits;
-       data[0] = 0;
+       data[1] = inb(dev->iobase + DAS16M1_DI_REG) & 0xf;
 
        return insn->n;
 }
 
-static int das16m1_do_wbits(struct comedi_device *dev,
-                           struct comedi_subdevice *s,
-                           struct comedi_insn *insn,
-                           unsigned int *data)
+static int das16m1_do_insn_bits(struct comedi_device *dev,
+                               struct comedi_subdevice *s,
+                               struct comedi_insn *insn,
+                               unsigned int *data)
 {
        if (comedi_dio_update_state(s, data))
                outb(s->state, dev->iobase + DAS16M1_DO_REG);
@@ -369,17 +382,13 @@ static int das16m1_do_wbits(struct comedi_device *dev,
 
 static void das16m1_handler(struct comedi_device *dev, unsigned int status)
 {
-       struct das16m1_private_struct *devpriv = dev->private;
-       struct comedi_subdevice *s;
-       struct comedi_async *async;
-       struct comedi_cmd *cmd;
+       struct das16m1_private *devpriv = dev->private;
+       struct comedi_subdevice *s = dev->read_subdev;
+       struct comedi_async *async = s->async;
+       struct comedi_cmd *cmd = &async->cmd;
        u16 num_samples;
        u16 hw_counter;
 
-       s = dev->read_subdev;
-       async = s->async;
-       cmd = &async->cmd;
-
        /* figure out how many samples are in fifo */
        hw_counter = comedi_8254_read(devpriv->counter, 1);
        /*
@@ -408,10 +417,9 @@ static void das16m1_handler(struct comedi_device *dev, unsigned int status)
                        num_samples = cmd->stop_arg * cmd->chanlist_len;
        }
        /*  make sure we dont try to get too many points if fifo has overrun */
-       if (num_samples > FIFO_SIZE)
-               num_samples = FIFO_SIZE;
+       if (num_samples > DAS16M1_AI_FIFO_SZ)
+               num_samples = DAS16M1_AI_FIFO_SZ;
        insw(dev->iobase, devpriv->ai_buffer, num_samples);
-       munge_sample_array(devpriv->ai_buffer, num_samples);
        comedi_buf_write_samples(s, devpriv->ai_buffer, num_samples);
        devpriv->adc_count += num_samples;
 
@@ -434,7 +442,8 @@ static void das16m1_handler(struct comedi_device *dev, unsigned int status)
        comedi_handle_events(dev, s);
 }
 
-static int das16m1_poll(struct comedi_device *dev, struct comedi_subdevice *s)
+static int das16m1_ai_poll(struct comedi_device *dev,
+                          struct comedi_subdevice *s)
 {
        unsigned long flags;
        unsigned int status;
@@ -501,15 +510,10 @@ static int das16m1_irq_bits(unsigned int irq)
        }
 }
 
-/*
- * Options list:
- *   0  I/O base
- *   1  IRQ
- */
 static int das16m1_attach(struct comedi_device *dev,
                          struct comedi_devconfig *it)
 {
-       struct das16m1_private_struct *devpriv;
+       struct das16m1_private *devpriv;
        struct comedi_subdevice *s;
        int ret;
 
@@ -520,12 +524,12 @@ static int das16m1_attach(struct comedi_device *dev,
        ret = comedi_request_region(dev, it->options[0], 0x10);
        if (ret)
                return ret;
-       /* Request an additional region for the 8255 */
-       ret = __comedi_request_region(dev, dev->iobase + DAS16M1_82C55,
+       /* Request an additional region for the 8255 and 3rd 8254 */
+       ret = __comedi_request_region(dev, dev->iobase + DAS16M1_8255_IOBASE,
                                      DAS16M1_SIZE2);
        if (ret)
                return ret;
-       devpriv->extra_iobase = dev->iobase + DAS16M1_82C55;
+       devpriv->extra_iobase = dev->iobase + DAS16M1_8255_IOBASE;
 
        /* only irqs 2, 3, 4, 5, 6, 7, 10, 11, 12, 14, and 15 are valid */
        if ((1 << it->options[1]) & 0xdcfc) {
@@ -535,12 +539,12 @@ static int das16m1_attach(struct comedi_device *dev,
                        dev->irq = it->options[1];
        }
 
-       dev->pacer = comedi_8254_init(dev->iobase + DAS16M1_8254_SECOND,
+       dev->pacer = comedi_8254_init(dev->iobase + DAS16M1_8254_IOBASE2,
                                      I8254_OSC_BASE_10MHZ, I8254_IO8, 0);
        if (!dev->pacer)
                return -ENOMEM;
 
-       devpriv->counter = comedi_8254_init(dev->iobase + DAS16M1_8254_FIRST,
+       devpriv->counter = comedi_8254_init(dev->iobase + DAS16M1_8254_IOBASE1,
                                            0, I8254_IO8, 0);
        if (!devpriv->counter)
                return -ENOMEM;
@@ -549,45 +553,46 @@ static int das16m1_attach(struct comedi_device *dev,
        if (ret)
                return ret;
 
+       /* Analog Input subdevice */
        s = &dev->subdevices[0];
-       /* ai */
-       s->type = COMEDI_SUBD_AI;
-       s->subdev_flags = SDF_READABLE | SDF_DIFF;
-       s->n_chan = 8;
-       s->maxdata = (1 << 12) - 1;
-       s->range_table = &range_das16m1;
-       s->insn_read = das16m1_ai_rinsn;
+       s->type         = COMEDI_SUBD_AI;
+       s->subdev_flags = SDF_READABLE | SDF_DIFF;
+       s->n_chan       = 8;
+       s->maxdata      = 0x0fff;
+       s->range_table  = &range_das16m1;
+       s->insn_read    = das16m1_ai_insn_read;
        if (dev->irq) {
                dev->read_subdev = s;
-               s->subdev_flags |= SDF_CMD_READ;
-               s->len_chanlist = 256;
-               s->do_cmdtest = das16m1_cmd_test;
-               s->do_cmd = das16m1_cmd_exec;
-               s->cancel = das16m1_cancel;
-               s->poll = das16m1_poll;
+               s->subdev_flags |= SDF_CMD_READ;
+               s->len_chanlist = 256;
+               s->do_cmdtest   = das16m1_ai_cmdtest;
+               s->do_cmd       = das16m1_ai_cmd;
+               s->cancel       = das16m1_ai_cancel;
+               s->poll         = das16m1_ai_poll;
+               s->munge        = das16m1_ai_munge;
        }
 
+       /* Digital Input subdevice */
        s = &dev->subdevices[1];
-       /* di */
-       s->type = COMEDI_SUBD_DI;
-       s->subdev_flags = SDF_READABLE;
-       s->n_chan = 4;
-       s->maxdata = 1;
-       s->range_table = &range_digital;
-       s->insn_bits = das16m1_di_rbits;
-
+       s->type         = COMEDI_SUBD_DI;
+       s->subdev_flags = SDF_READABLE;
+       s->n_chan       = 4;
+       s->maxdata      = 1;
+       s->range_table  = &range_digital;
+       s->insn_bits    = das16m1_di_insn_bits;
+
+       /* Digital Output subdevice */
        s = &dev->subdevices[2];
-       /* do */
-       s->type = COMEDI_SUBD_DO;
-       s->subdev_flags = SDF_WRITABLE;
-       s->n_chan = 4;
-       s->maxdata = 1;
-       s->range_table = &range_digital;
-       s->insn_bits = das16m1_do_wbits;
-
+       s->type         = COMEDI_SUBD_DO;
+       s->subdev_flags = SDF_WRITABLE;
+       s->n_chan       = 4;
+       s->maxdata      = 1;
+       s->range_table  = &range_digital;
+       s->insn_bits    = das16m1_do_insn_bits;
+
+       /* Digital I/O subdevice (8255) */
        s = &dev->subdevices[3];
-       /* 8255 */
-       ret = subdev_8255_init(dev, s, NULL, DAS16M1_82C55);
+       ret = subdev_8255_init(dev, s, NULL, DAS16M1_8255_IOBASE);
        if (ret)
                return ret;
 
@@ -603,7 +608,7 @@ static int das16m1_attach(struct comedi_device *dev,
 
 static void das16m1_detach(struct comedi_device *dev)
 {
-       struct das16m1_private_struct *devpriv = dev->private;
+       struct das16m1_private *devpriv = dev->private;
 
        if (devpriv) {
                if (devpriv->extra_iobase)
@@ -622,5 +627,5 @@ static struct comedi_driver das16m1_driver = {
 module_comedi_driver(das16m1_driver);
 
 MODULE_AUTHOR("Comedi http://www.comedi.org");
-MODULE_DESCRIPTION("Comedi low-level driver");
+MODULE_DESCRIPTION("Comedi driver for CIO-DAS16/M1 ISA cards");
 MODULE_LICENSE("GPL");