Commit 0ff657b0 authored by Hans Verkuil's avatar Hans Verkuil Committed by Mauro Carvalho Chehab

[media] vb2: don't return NULL for alloc and get_userptr ops

Always return an ERR_PTR() instead of NULL.

This makes the behavior of alloc, get_userptr and attach_dmabuf the
same.

Update the documentation in videobuf2-core.h as well.
Signed-off-by: default avatarHans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@s-opensource.com>
parent 82019205
...@@ -198,6 +198,7 @@ static int __vb2_buf_mem_alloc(struct vb2_buffer *vb) ...@@ -198,6 +198,7 @@ static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE; q->is_output ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
void *mem_priv; void *mem_priv;
int plane; int plane;
int ret = -ENOMEM;
/* /*
* Allocate memory for all planes in this buffer * Allocate memory for all planes in this buffer
...@@ -209,8 +210,11 @@ static int __vb2_buf_mem_alloc(struct vb2_buffer *vb) ...@@ -209,8 +210,11 @@ static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
mem_priv = call_ptr_memop(vb, alloc, mem_priv = call_ptr_memop(vb, alloc,
q->alloc_devs[plane] ? : q->dev, q->alloc_devs[plane] ? : q->dev,
q->dma_attrs, size, dma_dir, q->gfp_flags); q->dma_attrs, size, dma_dir, q->gfp_flags);
if (IS_ERR_OR_NULL(mem_priv)) if (IS_ERR(mem_priv)) {
if (mem_priv)
ret = PTR_ERR(mem_priv);
goto free; goto free;
}
/* Associate allocator private data with this plane */ /* Associate allocator private data with this plane */
vb->planes[plane].mem_priv = mem_priv; vb->planes[plane].mem_priv = mem_priv;
...@@ -224,7 +228,7 @@ static int __vb2_buf_mem_alloc(struct vb2_buffer *vb) ...@@ -224,7 +228,7 @@ static int __vb2_buf_mem_alloc(struct vb2_buffer *vb)
vb->planes[plane - 1].mem_priv = NULL; vb->planes[plane - 1].mem_priv = NULL;
} }
return -ENOMEM; return ret;
} }
/** /**
...@@ -1136,10 +1140,10 @@ static int __qbuf_userptr(struct vb2_buffer *vb, const void *pb) ...@@ -1136,10 +1140,10 @@ static int __qbuf_userptr(struct vb2_buffer *vb, const void *pb)
q->alloc_devs[plane] ? : q->dev, q->alloc_devs[plane] ? : q->dev,
planes[plane].m.userptr, planes[plane].m.userptr,
planes[plane].length, dma_dir); planes[plane].length, dma_dir);
if (IS_ERR_OR_NULL(mem_priv)) { if (IS_ERR(mem_priv)) {
dprintk(1, "failed acquiring userspace " dprintk(1, "failed acquiring userspace "
"memory for plane %d\n", plane); "memory for plane %d\n", plane);
ret = mem_priv ? PTR_ERR(mem_priv) : -EINVAL; ret = PTR_ERR(mem_priv);
goto err; goto err;
} }
vb->planes[plane].mem_priv = mem_priv; vb->planes[plane].mem_priv = mem_priv;
......
...@@ -104,11 +104,12 @@ static void *vb2_dma_sg_alloc(struct device *dev, unsigned long dma_attrs, ...@@ -104,11 +104,12 @@ static void *vb2_dma_sg_alloc(struct device *dev, unsigned long dma_attrs,
int ret; int ret;
int num_pages; int num_pages;
if (WARN_ON(dev == NULL)) if (WARN_ON(!dev))
return NULL; return ERR_PTR(-EINVAL);
buf = kzalloc(sizeof *buf, GFP_KERNEL); buf = kzalloc(sizeof *buf, GFP_KERNEL);
if (!buf) if (!buf)
return NULL; return ERR_PTR(-ENOMEM);
buf->vaddr = NULL; buf->vaddr = NULL;
buf->dma_dir = dma_dir; buf->dma_dir = dma_dir;
...@@ -166,7 +167,7 @@ static void *vb2_dma_sg_alloc(struct device *dev, unsigned long dma_attrs, ...@@ -166,7 +167,7 @@ static void *vb2_dma_sg_alloc(struct device *dev, unsigned long dma_attrs,
kfree(buf->pages); kfree(buf->pages);
fail_pages_array_alloc: fail_pages_array_alloc:
kfree(buf); kfree(buf);
return NULL; return ERR_PTR(-ENOMEM);
} }
static void vb2_dma_sg_put(void *buf_priv) static void vb2_dma_sg_put(void *buf_priv)
...@@ -226,7 +227,7 @@ static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr, ...@@ -226,7 +227,7 @@ static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
buf = kzalloc(sizeof *buf, GFP_KERNEL); buf = kzalloc(sizeof *buf, GFP_KERNEL);
if (!buf) if (!buf)
return NULL; return ERR_PTR(-ENOMEM);
buf->vaddr = NULL; buf->vaddr = NULL;
buf->dev = dev; buf->dev = dev;
...@@ -266,7 +267,7 @@ static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr, ...@@ -266,7 +267,7 @@ static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
vb2_destroy_framevec(vec); vb2_destroy_framevec(vec);
userptr_fail_pfnvec: userptr_fail_pfnvec:
kfree(buf); kfree(buf);
return NULL; return ERR_PTR(-ENOMEM);
} }
/* /*
......
...@@ -41,7 +41,7 @@ static void *vb2_vmalloc_alloc(struct device *dev, unsigned long attrs, ...@@ -41,7 +41,7 @@ static void *vb2_vmalloc_alloc(struct device *dev, unsigned long attrs,
buf = kzalloc(sizeof(*buf), GFP_KERNEL | gfp_flags); buf = kzalloc(sizeof(*buf), GFP_KERNEL | gfp_flags);
if (!buf) if (!buf)
return NULL; return ERR_PTR(-ENOMEM);
buf->size = size; buf->size = size;
buf->vaddr = vmalloc_user(buf->size); buf->vaddr = vmalloc_user(buf->size);
...@@ -53,7 +53,7 @@ static void *vb2_vmalloc_alloc(struct device *dev, unsigned long attrs, ...@@ -53,7 +53,7 @@ static void *vb2_vmalloc_alloc(struct device *dev, unsigned long attrs,
if (!buf->vaddr) { if (!buf->vaddr) {
pr_debug("vmalloc of size %ld failed\n", buf->size); pr_debug("vmalloc of size %ld failed\n", buf->size);
kfree(buf); kfree(buf);
return NULL; return ERR_PTR(-ENOMEM);
} }
atomic_inc(&buf->refcount); atomic_inc(&buf->refcount);
...@@ -77,17 +77,20 @@ static void *vb2_vmalloc_get_userptr(struct device *dev, unsigned long vaddr, ...@@ -77,17 +77,20 @@ static void *vb2_vmalloc_get_userptr(struct device *dev, unsigned long vaddr,
struct vb2_vmalloc_buf *buf; struct vb2_vmalloc_buf *buf;
struct frame_vector *vec; struct frame_vector *vec;
int n_pages, offset, i; int n_pages, offset, i;
int ret = -ENOMEM;
buf = kzalloc(sizeof(*buf), GFP_KERNEL); buf = kzalloc(sizeof(*buf), GFP_KERNEL);
if (!buf) if (!buf)
return NULL; return ERR_PTR(-ENOMEM);
buf->dma_dir = dma_dir; buf->dma_dir = dma_dir;
offset = vaddr & ~PAGE_MASK; offset = vaddr & ~PAGE_MASK;
buf->size = size; buf->size = size;
vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE); vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE);
if (IS_ERR(vec)) if (IS_ERR(vec)) {
ret = PTR_ERR(vec);
goto fail_pfnvec_create; goto fail_pfnvec_create;
}
buf->vec = vec; buf->vec = vec;
n_pages = frame_vector_count(vec); n_pages = frame_vector_count(vec);
if (frame_vector_to_pages(vec) < 0) { if (frame_vector_to_pages(vec) < 0) {
...@@ -117,7 +120,7 @@ static void *vb2_vmalloc_get_userptr(struct device *dev, unsigned long vaddr, ...@@ -117,7 +120,7 @@ static void *vb2_vmalloc_get_userptr(struct device *dev, unsigned long vaddr,
fail_pfnvec_create: fail_pfnvec_create:
kfree(buf); kfree(buf);
return NULL; return ERR_PTR(ret);
} }
static void vb2_vmalloc_put_userptr(void *buf_priv) static void vb2_vmalloc_put_userptr(void *buf_priv)
......
...@@ -33,7 +33,7 @@ struct vb2_threadio_data; ...@@ -33,7 +33,7 @@ struct vb2_threadio_data;
/** /**
* struct vb2_mem_ops - memory handling/memory allocator operations * struct vb2_mem_ops - memory handling/memory allocator operations
* @alloc: allocate video memory and, optionally, allocator private data, * @alloc: allocate video memory and, optionally, allocator private data,
* return NULL on failure or a pointer to allocator private, * return ERR_PTR() on failure or a pointer to allocator private,
* per-buffer data on success; the returned private structure * per-buffer data on success; the returned private structure
* will then be passed as buf_priv argument to other ops in this * will then be passed as buf_priv argument to other ops in this
* structure. Additional gfp_flags to use when allocating the * structure. Additional gfp_flags to use when allocating the
...@@ -50,14 +50,14 @@ struct vb2_threadio_data; ...@@ -50,14 +50,14 @@ struct vb2_threadio_data;
* USERPTR memory types; vaddr is the address passed to the * USERPTR memory types; vaddr is the address passed to the
* videobuf layer when queuing a video buffer of USERPTR type; * videobuf layer when queuing a video buffer of USERPTR type;
* should return an allocator private per-buffer structure * should return an allocator private per-buffer structure
* associated with the buffer on success, NULL on failure; * associated with the buffer on success, ERR_PTR() on failure;
* the returned private structure will then be passed as buf_priv * the returned private structure will then be passed as buf_priv
* argument to other ops in this structure. * argument to other ops in this structure.
* @put_userptr: inform the allocator that a USERPTR buffer will no longer * @put_userptr: inform the allocator that a USERPTR buffer will no longer
* be used. * be used.
* @attach_dmabuf: attach a shared struct dma_buf for a hardware operation; * @attach_dmabuf: attach a shared struct dma_buf for a hardware operation;
* used for DMABUF memory types; dev is the alloc device * used for DMABUF memory types; dev is the alloc device
* dbuf is the shared dma_buf; returns NULL on failure; * dbuf is the shared dma_buf; returns ERR_PTR() on failure;
* allocator private per-buffer structure on success; * allocator private per-buffer structure on success;
* this needs to be used for further accesses to the buffer. * this needs to be used for further accesses to the buffer.
* @detach_dmabuf: inform the exporter of the buffer that the current DMABUF * @detach_dmabuf: inform the exporter of the buffer that the current DMABUF
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment