Ported allocation of mite dma rings to modern kernel dma api.
authorFrank Mori Hess <fmhess@speakeasy.net>
Tue, 7 Aug 2007 19:04:01 +0000 (19:04 +0000)
committerFrank Mori Hess <fmhess@speakeasy.net>
Tue, 7 Aug 2007 19:04:01 +0000 (19:04 +0000)
comedi/drivers/mite.c
comedi/drivers/mite.h
comedi/drivers/ni_pcidio.c
comedi/drivers/ni_pcimio.c
include/linux/comedidev.h

index 4f2332a5d4600ec84b119547060f5a457bdb5df3..451f6d11e8efed42c2eb4b5e81e09645cee9b392 100644 (file)
@@ -316,21 +316,22 @@ int mite_buf_change(struct mite_dma_descriptor_ring *ring, comedi_async *async)
        int i;
 
        if(ring->descriptors){
-               kfree(ring->descriptors);
+               dma_free_coherent(ring->hw_dev, ring->n_links * sizeof(struct mite_dma_descriptor),
+                       ring->descriptors, ring->descriptors_dma_addr);
        }
        ring->descriptors = NULL;
+       ring->descriptors_dma_addr = 0;
        ring->n_links = 0;
 
        if(async->prealloc_bufsz==0){
                return 0;
        }
-
        n_links = async->prealloc_bufsz >> PAGE_SHIFT;
 
-       MDPRINTK("buf=%p buf(bus)=%08lx bufsz=0x%08x n_links=0x%04x\n",
-               async->prealloc_buf, virt_to_bus(async->prealloc_buf), async->prealloc_bufsz, n_links);
+       MDPRINTK("ring->hw_dev=%p, n_links=0x%04x\n", ring->hw_dev, n_links);
 
-       ring->descriptors = kmalloc(n_links * sizeof(struct mite_dma_descriptor), GFP_KERNEL);
+       ring->descriptors = dma_alloc_coherent(ring->hw_dev, n_links * sizeof(struct mite_dma_descriptor),
+               &ring->descriptors_dma_addr, GFP_KERNEL);
        if(!ring->descriptors){
                printk("mite: ring buffer allocation failed\n");
                return -ENOMEM;
@@ -340,12 +341,9 @@ int mite_buf_change(struct mite_dma_descriptor_ring *ring, comedi_async *async)
        for(i = 0; i < n_links; i++){
                ring->descriptors[i].count = cpu_to_le32(PAGE_SIZE);
                ring->descriptors[i].addr = cpu_to_le32(async->buf_page_list[i].dma_addr);
-               // FIXME: virt_to_bus is deprecated
-               ring->descriptors[i].next = cpu_to_le32(virt_to_bus(
-                       ring->descriptors + i + 1));
+               ring->descriptors[i].next = cpu_to_le32(ring->descriptors_dma_addr + (i + 1) * sizeof(struct mite_dma_descriptor));
        }
-       ring->descriptors[n_links - 1].next = cpu_to_le32(virt_to_bus(
-               ring->descriptors));
+       ring->descriptors[n_links - 1].next = cpu_to_le32(ring->descriptors_dma_addr);
        /* barrier is meant to insure that all the writes to the dma descriptors
        have completed before the dma controller is commanded to read them */
        smp_wmb();
@@ -433,7 +431,7 @@ void mite_prep_dma(struct mite_channel *mite_chan,
        writel(lkcr, mite->mite_io_addr + MITE_LKCR(mite_chan->channel));
 
        /* starting address for link chaining */
-       writel(virt_to_bus(mite_chan->ring->descriptors),
+       writel(mite_chan->ring->descriptors_dma_addr,
                mite->mite_io_addr + MITE_LKAR(mite_chan->channel));
 
        MDPRINTK("exit mite_prep_dma\n");
index 90085f84584d49a0083e62e24931ebd0b57f592c..1f9e4d3747f8746f41a1db0a934ee66764f7a976 100644 (file)
@@ -50,26 +50,10 @@ struct mite_dma_descriptor{
 
 struct mite_dma_descriptor_ring
 {
+       struct device *hw_dev;
        unsigned int n_links;
        struct mite_dma_descriptor *descriptors;
-};
-
-static inline struct mite_dma_descriptor_ring* mite_alloc_ring(void)
-{
-       struct mite_dma_descriptor_ring *ring = kmalloc(sizeof(struct mite_dma_descriptor_ring), GFP_KERNEL);
-       if(ring == NULL) return ring;
-       ring->n_links = 0;
-       ring->descriptors = NULL;
-       return ring;
-};
-
-static inline void mite_free_ring(struct mite_dma_descriptor_ring *ring)
-{
-       if(ring)
-       {
-               if(ring->descriptors) kfree(ring->descriptors);
-               kfree(ring);
-       }
+       dma_addr_t descriptors_dma_addr;
 };
 
 struct mite_channel{
@@ -96,6 +80,32 @@ struct mite_struct{
        spinlock_t lock;
 };
 
+static inline struct mite_dma_descriptor_ring* mite_alloc_ring(struct mite_struct *mite)
+{
+       struct mite_dma_descriptor_ring *ring = kmalloc(sizeof(struct mite_dma_descriptor_ring), GFP_KERNEL);
+       if(ring == NULL) return ring;
+       ring->hw_dev = get_device(&mite->pcidev->dev);
+       if(ring->hw_dev == NULL)
+       {
+               kfree(ring);
+               return NULL;
+       }
+       ring->n_links = 0;
+       ring->descriptors = NULL;
+       ring->descriptors_dma_addr = 0;
+       return ring;
+};
+
+static inline void mite_free_ring(struct mite_dma_descriptor_ring *ring)
+{
+       if(ring)
+       {
+               if(ring->descriptors) kfree(ring->descriptors);
+               if(ring->hw_dev) put_device(ring->hw_dev);
+               kfree(ring);
+       }
+};
+
 extern struct mite_struct *mite_devices;
 
 static inline unsigned int mite_irq(struct mite_struct *mite)
index 47647b1547378e68074053c208b392fa931f83a2..ab57a78283b66b5cc134dab9bd78eecb34106943 100644 (file)
@@ -1105,8 +1105,6 @@ static int nidio_attach(comedi_device *dev,comedi_devconfig *it)
 
        if((ret=alloc_private(dev,sizeof(nidio96_private)))<0)
                return ret;
-       devpriv->di_mite_ring = mite_alloc_ring();
-       if(devpriv->di_mite_ring == NULL) return -ENOMEM;
        spin_lock_init(&devpriv->mite_channel_lock);
 
        ret=nidio_find_device(dev,it->options[0],it->options[1]);
@@ -1119,6 +1117,8 @@ static int nidio_attach(comedi_device *dev,comedi_devconfig *it)
                return ret;
        }
        comedi_set_hw_dev(dev, &devpriv->mite->pcidev->dev);
+       devpriv->di_mite_ring = mite_alloc_ring(devpriv->mite);
+       if(devpriv->di_mite_ring == NULL) return -ENOMEM;
 
        dev->board_name=this_board->name;
        irq=mite_irq(devpriv->mite);
index 52463766093e6fc5b1887848d8316db9ebccb618..1d3d731bd6f733a68aefc39f9a6f53d6f310df96 100644 (file)
@@ -1533,14 +1533,6 @@ static int pcimio_attach(comedi_device *dev,comedi_devconfig *it)
 
        ret=ni_alloc_private(dev);
        if(ret<0)return ret;
-       devpriv->ai_mite_ring = mite_alloc_ring();
-       if(devpriv->ai_mite_ring == NULL) return -ENOMEM;
-       devpriv->ao_mite_ring = mite_alloc_ring();
-       if(devpriv->ao_mite_ring == NULL) return -ENOMEM;
-       devpriv->gpct_mite_ring[0] = mite_alloc_ring();
-       if(devpriv->gpct_mite_ring[0] == NULL) return -ENOMEM;
-       devpriv->gpct_mite_ring[1] = mite_alloc_ring();
-       if(devpriv->gpct_mite_ring[1] == NULL) return -ENOMEM;
 
        ret=pcimio_find_device(dev,it->options[0],it->options[1]);
        if(ret<0)return ret;
@@ -1569,6 +1561,14 @@ static int pcimio_attach(comedi_device *dev,comedi_devconfig *it)
                return ret;
        }
        comedi_set_hw_dev(dev, &devpriv->mite->pcidev->dev);
+       devpriv->ai_mite_ring = mite_alloc_ring(devpriv->mite);
+       if(devpriv->ai_mite_ring == NULL) return -ENOMEM;
+       devpriv->ao_mite_ring = mite_alloc_ring(devpriv->mite);
+       if(devpriv->ao_mite_ring == NULL) return -ENOMEM;
+       devpriv->gpct_mite_ring[0] = mite_alloc_ring(devpriv->mite);
+       if(devpriv->gpct_mite_ring[0] == NULL) return -ENOMEM;
+       devpriv->gpct_mite_ring[1] = mite_alloc_ring(devpriv->mite);
+       if(devpriv->gpct_mite_ring[1] == NULL) return -ENOMEM;
 
        if(boardtype.reg_type & ni_reg_m_series_mask)
                m_series_init_eeprom_buffer(dev);
index a85266bb224d8aafb8bb0556467c387924147823..10cf384daaa0fccb4746b767aff11c52b3d78479 100644 (file)
@@ -460,7 +460,8 @@ static inline void comedi_set_hw_dev(comedi_device *dev, struct device *hw_dev)
        dev->hw_dev = hw_dev;
        if(dev->hw_dev)
        {
-               get_device(dev->hw_dev);
+               dev->hw_dev = get_device(dev->hw_dev);
+               BUG_ON(dev->hw_dev == NULL);
        }
 }