staging: comedi: pcl818: fix pcl818_handle_dma() for short DMA transaction
authorH Hartley Sweeten <hsweeten@visionengravers.com>
Mon, 12 Jan 2015 17:55:59 +0000 (10:55 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 17 Jan 2015 22:25:20 +0000 (14:25 -0800)
Currently this function always transfers the full DMA buffer to the comedi
async buffer. When the cmd->stop_src == TRIG_COUNT the last DMA transfer
might be smaller than the buffer size. This results in invalid data being
added to the asunc buffer.

Add a 'size' member to the DMA descriptor and initialize it with the
actual size of the DMA transfer. Use that in pcl818_handle_dma() to
return the proper number of samples. Use the comedi_bytes_to_samples()
helper to convert the byte size to comedi samples. Remove the unnecessary
'bufptr' local variable.

Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Reviewed-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/comedi/drivers/pcl818.c

index 3656ecf108136ac68eb75072d52908f41aba2b34..19f6b595a80e8086efaf58b87a5fe11fd325b7ea 100644 (file)
@@ -305,6 +305,7 @@ static const struct pcl818_board boardtypes[] = {
 struct pcl818_dma_desc {
        void *virt_addr;        /* virtual address of DMA buffer */
        dma_addr_t hw_addr;     /* hardware (bus) address of DMA buffer */
+       unsigned int size;      /* transfer size (in bytes) */
 };
 
 struct pcl818_private {
@@ -349,17 +350,17 @@ static void pcl818_ai_setup_dma(struct comedi_device *dev,
        struct pcl818_dma_desc *dma = &devpriv->dma_desc[0];
        struct comedi_cmd *cmd = &s->async->cmd;
        unsigned int flags;
-       unsigned int bytes;
 
        disable_dma(devpriv->dma);      /*  disable dma */
-       bytes = devpriv->hwdmasize;
        if (cmd->stop_src == TRIG_COUNT) {
-               bytes = cmd->stop_arg * comedi_bytes_per_scan(s);
-               devpriv->dma_runs_to_end = bytes / devpriv->hwdmasize;
-               devpriv->last_dma_run = bytes % devpriv->hwdmasize;
+               dma->size = cmd->stop_arg * comedi_bytes_per_scan(s);
+               devpriv->dma_runs_to_end = dma->size / devpriv->hwdmasize;
+               devpriv->last_dma_run = dma->size % devpriv->hwdmasize;
                devpriv->dma_runs_to_end--;
                if (devpriv->dma_runs_to_end >= 0)
-                       bytes = devpriv->hwdmasize;
+                       dma->size = devpriv->hwdmasize;
+       } else {
+               dma->size = devpriv->hwdmasize;
        }
 
        devpriv->cur_dma = 0;
@@ -367,7 +368,7 @@ static void pcl818_ai_setup_dma(struct comedi_device *dev,
        flags = claim_dma_lock();
        clear_dma_ff(devpriv->dma);
        set_dma_addr(devpriv->dma, dma->hw_addr);
-       set_dma_count(devpriv->dma, bytes);
+       set_dma_count(devpriv->dma, dma->size);
        release_dma_lock(flags);
        enable_dma(devpriv->dma);
 }
@@ -385,13 +386,14 @@ static void pcl818_ai_setup_next_dma(struct comedi_device *dev,
        if (devpriv->dma_runs_to_end > -1 || cmd->stop_src == TRIG_NONE) {
                /* switch dma bufs */
                dma = &devpriv->dma_desc[devpriv->cur_dma];
+               if (devpriv->dma_runs_to_end || cmd->stop_src == TRIG_NONE)
+                       dma->size = devpriv->hwdmasize;
+               else
+                       dma->size = devpriv->last_dma_run;
                set_dma_mode(devpriv->dma, DMA_MODE_READ);
                flags = claim_dma_lock();
                set_dma_addr(devpriv->dma, dma->hw_addr);
-               if (devpriv->dma_runs_to_end || cmd->stop_src == TRIG_NONE)
-                       set_dma_count(devpriv->dma, devpriv->hwdmasize);
-               else
-                       set_dma_count(devpriv->dma, devpriv->last_dma_run);
+               set_dma_count(devpriv->dma, dma->size);
                release_dma_lock(flags);
                enable_dma(devpriv->dma);
        }
@@ -565,17 +567,15 @@ static void pcl818_handle_dma(struct comedi_device *dev,
        struct pcl818_private *devpriv = dev->private;
        struct pcl818_dma_desc *dma = &devpriv->dma_desc[devpriv->cur_dma];
        unsigned short *ptr = dma->virt_addr;
+       unsigned int nsamples = comedi_bytes_to_samples(s, dma->size);
        unsigned int chan;
        unsigned int val;
-       int i, len, bufptr;
+       int i;
 
        pcl818_ai_setup_next_dma(dev, s);
 
-       len = devpriv->hwdmasize >> 1;
-       bufptr = 0;
-
-       for (i = 0; i < len; i++) {
-               val = ptr[bufptr++];
+       for (i = 0; i < nsamples; i++) {
+               val = ptr[i];
                chan = val & 0xf;
                val = (val >> 4) & s->maxdata;