[media] media/videobuf2-dma-sg: Fix handling of sg_table structure
authorRicardo Ribalda <ricardo.ribalda@gmail.com>
Wed, 29 Apr 2015 12:00:45 +0000 (09:00 -0300)
committerMauro Carvalho Chehab <mchehab@osg.samsung.com>
Sat, 30 May 2015 14:28:29 +0000 (11:28 -0300)
When sg_alloc_table_from_pages() does not fail it returns a sg_table
structure with nents and nents_orig initialized to the same value.

dma_map_sg returns the number of areas mapped by the hardware,
which could be different than the areas given as an input.
The output must be saved to nent.

The output of dma_map, should be used to transverse the scatter list.

dma_unmap_sg needs the value passed to dma_map_sg (nents_orig).

sg_free_tables uses also orig_nent.

This patch fix the file to follow this paradigm.

Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
Reviewed-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
drivers/media/v4l2-core/videobuf2-dma-sg.c

index afd4b514affcaa6594f82bce2ad3dd0fdacdafa0..d7bcb05c705884c24df86c25420f4584bc6cd536 100644 (file)
@@ -147,8 +147,9 @@ static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size,
         * No need to sync to the device, this will happen later when the
         * prepare() memop is called.
         */
-       if (dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
-                            buf->dma_dir, &attrs) == 0)
+       sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
+                                     buf->dma_dir, &attrs);
+       if (!sgt->nents)
                goto fail_map;
 
        buf->handler.refcount = &buf->refcount;
@@ -187,7 +188,7 @@ static void vb2_dma_sg_put(void *buf_priv)
                dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
                dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
                        buf->num_pages);
-               dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
+               dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
                                   buf->dma_dir, &attrs);
                if (buf->vaddr)
                        vm_unmap_ram(buf->vaddr, buf->num_pages);
@@ -316,9 +317,11 @@ static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
         * No need to sync to the device, this will happen later when the
         * prepare() memop is called.
         */
-       if (dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
-                            buf->dma_dir, &attrs) == 0)
+       sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
+                                     buf->dma_dir, &attrs);
+       if (!sgt->nents)
                goto userptr_fail_map;
+
        return buf;
 
 userptr_fail_map:
@@ -355,7 +358,8 @@ static void vb2_dma_sg_put_userptr(void *buf_priv)
 
        dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
               __func__, buf->num_pages);
-       dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir, &attrs);
+       dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir,
+                          &attrs);
        if (buf->vaddr)
                vm_unmap_ram(buf->vaddr, buf->num_pages);
        sg_free_table(buf->dma_sgt);
@@ -508,7 +512,6 @@ static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
        /* stealing dmabuf mutex to serialize map/unmap operations */
        struct mutex *lock = &db_attach->dmabuf->lock;
        struct sg_table *sgt;
-       int ret;
 
        mutex_lock(lock);
 
@@ -527,8 +530,9 @@ static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
        }
 
        /* mapping to the client with new direction */
-       ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dma_dir);
-       if (ret <= 0) {
+       sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
+                               dma_dir);
+       if (!sgt->nents) {
                pr_err("failed to map scatterlist\n");
                mutex_unlock(lock);
                return ERR_PTR(-EIO);