staging: comedi: pcl818: tidy up dma buffer allocation
authorH Hartley Sweeten <hsweeten@visionengravers.com>
Mon, 17 Feb 2014 21:27:30 +0000 (14:27 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 18 Feb 2014 19:55:32 +0000 (11:55 -0800)
This driver uses 2 buffers for DMA. Refactor the buffer allocation to
use a for loop to remove code duplication. Remove the dev_err() messages
when __get_dma_pages() fails and change the errno returned from -EBUSY
to -ENOMEM.

Both buffers are the same size so replace the 'dmapages' and 'hwdmasize'
arrays in the private data with variables to save a bit of space.

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 dcc397cf45bb2abea9a0e59265dfd2ac954bf04c..e2629087d097e2047894c498d6e946c32a68d2af 100644 (file)
@@ -320,12 +320,11 @@ static const struct pcl818_board boardtypes[] = {
 };
 
 struct pcl818_private {
-
        unsigned int dma;       /*  used DMA, 0=don't use DMA */
+       unsigned int dmapages;
+       unsigned int hwdmasize;
        unsigned long dmabuf[2];        /*  pointers to begin of DMA buffers */
-       unsigned int dmapages[2];       /*  len of DMA buffers in PAGE_SIZEs */
        unsigned int hwdmaptr[2];       /*  hardware address of DMA buffers */
-       unsigned int hwdmasize[2];      /*  len of DMA buffers in Bytes */
        int next_dma_buf;       /*  which DMA buffer will be used next round */
        long dma_runs_to_end;   /*  how many we must permorm DMA transfer to end of record */
        unsigned long last_dma_run;     /*  how many bytes we must transfer on last DMA page */
@@ -584,13 +583,10 @@ static irqreturn_t interrupt_pcl818_ai_mode13_dma(int irq, void *d)
                flags = claim_dma_lock();
                set_dma_addr(devpriv->dma,
                             devpriv->hwdmaptr[devpriv->next_dma_buf]);
-               if (devpriv->dma_runs_to_end || devpriv->neverending_ai) {
-                       set_dma_count(devpriv->dma,
-                                     devpriv->hwdmasize[devpriv->
-                                                        next_dma_buf]);
-               } else {
+               if (devpriv->dma_runs_to_end || devpriv->neverending_ai)
+                       set_dma_count(devpriv->dma, devpriv->hwdmasize);
+               else
                        set_dma_count(devpriv->dma, devpriv->last_dma_run);
-               }
                release_dma_lock(flags);
                enable_dma(devpriv->dma);
        }
@@ -599,7 +595,7 @@ static irqreturn_t interrupt_pcl818_ai_mode13_dma(int irq, void *d)
        outb(0, dev->iobase + PCL818_CLRINT);   /* clear INT request */
        ptr = (unsigned short *)devpriv->dmabuf[1 - devpriv->next_dma_buf];
 
-       len = devpriv->hwdmasize[0] >> 1;
+       len = devpriv->hwdmasize >> 1;
        bufptr = 0;
 
        for (i = 0; i < len; i++) {
@@ -795,14 +791,14 @@ static void pcl818_ai_mode13dma_int(int mode, struct comedi_device *dev,
        unsigned int bytes;
 
        disable_dma(devpriv->dma);      /*  disable dma */
-       bytes = devpriv->hwdmasize[0];
+       bytes = devpriv->hwdmasize;
        if (!devpriv->neverending_ai) {
-               bytes = cmd->chanlist_len * cmd->stop_arg * sizeof(short);      /*  how many */
-               devpriv->dma_runs_to_end = bytes / devpriv->hwdmasize[0];       /*  how many DMA pages we must fiil */
-               devpriv->last_dma_run = bytes % devpriv->hwdmasize[0];  /* on last dma transfer must be moved */
+               bytes = cmd->chanlist_len * cmd->stop_arg * sizeof(short);
+               devpriv->dma_runs_to_end = bytes / devpriv->hwdmasize;
+               devpriv->last_dma_run = bytes % devpriv->hwdmasize;
                devpriv->dma_runs_to_end--;
                if (devpriv->dma_runs_to_end >= 0)
-                       bytes = devpriv->hwdmasize[0];
+                       bytes = devpriv->hwdmasize;
        }
 
        devpriv->next_dma_buf = 0;
@@ -1308,9 +1304,9 @@ static int pcl818_attach(struct comedi_device *dev, struct comedi_devconfig *it)
 {
        const struct pcl818_board *board = comedi_board(dev);
        struct pcl818_private *devpriv;
-       int ret;
-       unsigned long pages;
        struct comedi_subdevice *s;
+       int ret;
+       int i;
 
        devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
        if (!devpriv)
@@ -1353,20 +1349,19 @@ static int pcl818_attach(struct comedi_device *dev, struct comedi_devconfig *it)
                }
                devpriv->dma = it->options[2];
 
-               pages = 2;      /* we need 16KB */
-               devpriv->dmabuf[0] = __get_dma_pages(GFP_KERNEL, pages);
-               if (!devpriv->dmabuf[0])
-                       /* maybe experiment with try_to_free_pages() will help .... */
-                       return -EBUSY;  /* no buffer :-( */
-               devpriv->dmapages[0] = pages;
-               devpriv->hwdmaptr[0] = virt_to_bus((void *)devpriv->dmabuf[0]);
-               devpriv->hwdmasize[0] = (1 << pages) * PAGE_SIZE;
-               devpriv->dmabuf[1] = __get_dma_pages(GFP_KERNEL, pages);
-               if (!devpriv->dmabuf[1])
-                       return -EBUSY;
-               devpriv->dmapages[1] = pages;
-               devpriv->hwdmaptr[1] = virt_to_bus((void *)devpriv->dmabuf[1]);
-               devpriv->hwdmasize[1] = (1 << pages) * PAGE_SIZE;
+               devpriv->dmapages = 2;  /* we need 16KB */
+               devpriv->hwdmasize = (1 << devpriv->dmapages) * PAGE_SIZE;
+
+               for (i = 0; i < 2; i++) {
+                       unsigned long dmabuf;
+
+                       dmabuf = __get_dma_pages(GFP_KERNEL, devpriv->dmapages);
+                       if (!dmabuf)
+                               return -ENOMEM;
+
+                       devpriv->dmabuf[i] = dmabuf;
+                       devpriv->hwdmaptr[i] = virt_to_bus((void *)dmabuf);
+               }
        }
 
        ret = comedi_alloc_subdevices(dev, 4);
@@ -1468,9 +1463,9 @@ static void pcl818_detach(struct comedi_device *dev)
                if (devpriv->dma)
                        free_dma(devpriv->dma);
                if (devpriv->dmabuf[0])
-                       free_pages(devpriv->dmabuf[0], devpriv->dmapages[0]);
+                       free_pages(devpriv->dmabuf[0], devpriv->dmapages);
                if (devpriv->dmabuf[1])
-                       free_pages(devpriv->dmabuf[1], devpriv->dmapages[1]);
+                       free_pages(devpriv->dmabuf[1], devpriv->dmapages);
        }
        comedi_legacy_detach(dev);
 }