Skip to content

Commit 8310ca9

Browse files
wensmchehab
authored andcommitted
media: v4l2-mem2mem: Apply DST_QUEUE_OFF_BASE on MMAP buffers across ioctls
DST_QUEUE_OFF_BASE is applied to offset/mem_offset on MMAP capture buffers only for the VIDIOC_QUERYBUF ioctl, while the userspace fields (including offset/mem_offset) are filled in for VIDIOC_{QUERY,PREPARE,Q,DQ}BUF ioctls. This leads to differences in the values presented to userspace. If userspace attempts to mmap the capture buffer directly using values from DQBUF, it will fail. Move the code that applies the magic offset into a helper, and call that helper from all four ioctl entry points. [hverkuil: drop unnecessary '= 0' in v4l2_m2m_querybuf() for ret] Fixes: 7f98639 ("V4L/DVB: add memory-to-memory device helper framework for videobuf") Fixes: 908a0d7 ("[media] v4l: mem2mem: port to videobuf2") Signed-off-by: Chen-Yu Tsai <wenst@chromium.org> Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl> Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
1 parent 2092f0d commit 8310ca9

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

drivers/media/v4l2-core/v4l2-mem2mem.c

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -585,28 +585,38 @@ int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
585585
}
586586
EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs);
587587

588-
int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
589-
struct v4l2_buffer *buf)
588+
static void v4l2_m2m_adjust_mem_offset(struct vb2_queue *vq,
589+
struct v4l2_buffer *buf)
590590
{
591-
struct vb2_queue *vq;
592-
int ret = 0;
593-
unsigned int i;
594-
595-
vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
596-
ret = vb2_querybuf(vq, buf);
597-
598591
/* Adjust MMAP memory offsets for the CAPTURE queue */
599592
if (buf->memory == V4L2_MEMORY_MMAP && V4L2_TYPE_IS_CAPTURE(vq->type)) {
600593
if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
594+
unsigned int i;
595+
601596
for (i = 0; i < buf->length; ++i)
602597
buf->m.planes[i].m.mem_offset
603598
+= DST_QUEUE_OFF_BASE;
604599
} else {
605600
buf->m.offset += DST_QUEUE_OFF_BASE;
606601
}
607602
}
603+
}
608604

609-
return ret;
605+
int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
606+
struct v4l2_buffer *buf)
607+
{
608+
struct vb2_queue *vq;
609+
int ret;
610+
611+
vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
612+
ret = vb2_querybuf(vq, buf);
613+
if (ret)
614+
return ret;
615+
616+
/* Adjust MMAP memory offsets for the CAPTURE queue */
617+
v4l2_m2m_adjust_mem_offset(vq, buf);
618+
619+
return 0;
610620
}
611621
EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf);
612622

@@ -763,6 +773,9 @@ int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
763773
if (ret)
764774
return ret;
765775

776+
/* Adjust MMAP memory offsets for the CAPTURE queue */
777+
v4l2_m2m_adjust_mem_offset(vq, buf);
778+
766779
/*
767780
* If the capture queue is streaming, but streaming hasn't started
768781
* on the device, but was asked to stop, mark the previously queued
@@ -784,9 +797,17 @@ int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
784797
struct v4l2_buffer *buf)
785798
{
786799
struct vb2_queue *vq;
800+
int ret;
787801

788802
vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
789-
return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
803+
ret = vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
804+
if (ret)
805+
return ret;
806+
807+
/* Adjust MMAP memory offsets for the CAPTURE queue */
808+
v4l2_m2m_adjust_mem_offset(vq, buf);
809+
810+
return 0;
790811
}
791812
EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
792813

@@ -795,9 +816,17 @@ int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
795816
{
796817
struct video_device *vdev = video_devdata(file);
797818
struct vb2_queue *vq;
819+
int ret;
798820

799821
vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
800-
return vb2_prepare_buf(vq, vdev->v4l2_dev->mdev, buf);
822+
ret = vb2_prepare_buf(vq, vdev->v4l2_dev->mdev, buf);
823+
if (ret)
824+
return ret;
825+
826+
/* Adjust MMAP memory offsets for the CAPTURE queue */
827+
v4l2_m2m_adjust_mem_offset(vq, buf);
828+
829+
return 0;
801830
}
802831
EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf);
803832

0 commit comments

Comments
 (0)