From 1dc6e72903f6dbefd79ea2e9ce9c9e536e4e695b Mon Sep 17 00:00:00 2001 From: H Hartley Sweeten Date: Mon, 26 Jan 2015 16:17:20 -0700 Subject: [PATCH] staging: comedi: das16: fix DMA size for short transfers Currently this driver alwasy programs the DMA transfer to be the allocated size of the DMA buffer. When the async command 'stop_src' is TRIG_COUNT its possible (likely) for the last transfer to be less than the buffer size. Introduce a helper function, das16_ai_setup_dma(), to calculate the actual size of the DMA transfer based on the number of samples remaining and the number of unread samples. Signed-off-by: H Hartley Sweeten Reviewed-by: Ian Abbott Signed-off-by: Greg Kroah-Hartman --- drivers/staging/comedi/drivers/das16.c | 34 +++++++++++++++++++------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/drivers/staging/comedi/drivers/das16.c b/drivers/staging/comedi/drivers/das16.c index 3a2a6ccffe3f..86e90d549de7 100644 --- a/drivers/staging/comedi/drivers/das16.c +++ b/drivers/staging/comedi/drivers/das16.c @@ -448,6 +448,28 @@ struct das16_private_struct { unsigned int timer_running:1; }; +static void das16_ai_setup_dma(struct comedi_device *dev, + struct comedi_subdevice *s, + unsigned int unread_samples) +{ + struct das16_private_struct *devpriv = dev->private; + struct comedi_isadma *dma = devpriv->dma; + struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma]; + unsigned int max_samples = comedi_bytes_to_samples(s, desc->maxsize); + unsigned int nsamples; + + /* + * Determine dma size based on the buffer size plus the number of + * unread samples and the number of samples remaining in the command. + */ + nsamples = comedi_nsamples_left(s, max_samples + unread_samples); + if (nsamples > unread_samples) { + nsamples -= unread_samples; + desc->size = comedi_samples_to_bytes(s, nsamples); + comedi_isadma_program(desc); + } +} + static void das16_interrupt(struct comedi_device *dev) { struct das16_private_struct *devpriv = dev->private; @@ -486,13 +508,9 @@ static void das16_interrupt(struct comedi_device *dev) nsamples = comedi_bytes_to_samples(s, nbytes); /* restart DMA if more samples are needed */ - if (nsamples && nsamples < comedi_nsamples_left(s, nsamples + 1)) { - struct comedi_isadma_desc *nxt_desc; - + if (nsamples) { dma->cur_dma = 1 - dma->cur_dma; - nxt_desc = &dma->desc[dma->cur_dma]; - nxt_desc->size = nxt_desc->maxsize; - comedi_isadma_program(nxt_desc); + das16_ai_setup_dma(dev, s, nsamples); } spin_unlock_irqrestore(&dev->spinlock, spin_flags); @@ -672,7 +690,6 @@ static int das16_cmd_exec(struct comedi_device *dev, struct comedi_subdevice *s) const struct das16_board *board = dev->board_ptr; struct das16_private_struct *devpriv = dev->private; struct comedi_isadma *dma = devpriv->dma; - struct comedi_isadma_desc *desc = &dma->desc[0]; struct comedi_async *async = s->async; struct comedi_cmd *cmd = &async->cmd; unsigned int byte; @@ -721,8 +738,7 @@ static int das16_cmd_exec(struct comedi_device *dev, struct comedi_subdevice *s) /* set up dma transfer */ dma->cur_dma = 0; - desc->size = desc->maxsize; - comedi_isadma_program(desc); + das16_ai_setup_dma(dev, s, 0); /* set up timer */ spin_lock_irqsave(&dev->spinlock, flags); -- 2.34.1