1 // SPDX-License-Identifier: GPL-2.0+
2 /* Driver for virtio video device.
4 * Copyright 2020 OpenSynergy GmbH.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <linux/version.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/videobuf2-dma-sg.h>
25 #include "virtio_video.h"
27 enum video_stream_state virtio_video_state(struct virtio_video_stream *stream)
29 return atomic_read(&stream->state);
32 void virtio_video_state_reset(struct virtio_video_stream *stream)
34 atomic_set(&stream->state, STREAM_STATE_IDLE);
37 void virtio_video_state_update(struct virtio_video_stream *stream,
38 enum video_stream_state new_state)
40 enum video_stream_state prev_state;
43 prev_state = atomic_read(&stream->state);
44 if (prev_state == STREAM_STATE_ERROR)
46 } while (atomic_cmpxchg(&stream->state, prev_state, new_state) !=
50 int virtio_video_pending_buf_list_empty(struct virtio_video_device *vvd)
54 if (vvd->is_m2m_dev) {
55 v4l2_err(&vvd->v4l2_dev, "Unexpected call for m2m device!\n");
59 spin_lock(&vvd->pending_buf_list_lock);
60 if (list_empty(&vvd->pending_buf_list))
62 spin_unlock(&vvd->pending_buf_list_lock);
67 int virtio_video_pending_buf_list_pop(struct virtio_video_device *vvd,
68 struct virtio_video_buffer **virtio_vb)
70 struct virtio_video_buffer *retbuf;
72 if (vvd->is_m2m_dev) {
73 v4l2_err(&vvd->v4l2_dev, "Unexpected call for m2m device!\n");
77 spin_lock(&vvd->pending_buf_list_lock);
78 if (list_empty(&vvd->pending_buf_list)) {
79 spin_unlock(&vvd->pending_buf_list_lock);
83 retbuf = list_first_entry(&vvd->pending_buf_list,
84 struct virtio_video_buffer, list);
85 spin_unlock(&vvd->pending_buf_list_lock);
91 int virtio_video_pending_buf_list_add(struct virtio_video_device *vvd,
92 struct virtio_video_buffer *virtio_vb)
94 if (vvd->is_m2m_dev) {
95 v4l2_err(&vvd->v4l2_dev, "Unexpected call for m2m device!\n");
99 spin_lock(&vvd->pending_buf_list_lock);
100 list_add_tail(&virtio_vb->list, &vvd->pending_buf_list);
101 spin_unlock(&vvd->pending_buf_list_lock);
106 int virtio_video_pending_buf_list_del(struct virtio_video_device *vvd,
107 struct virtio_video_buffer *virtio_vb)
109 struct virtio_video_buffer *vb, *vb_tmp;
112 if (vvd->is_m2m_dev) {
113 v4l2_err(&vvd->v4l2_dev, "Unexpected call for m2m device!\n");
117 spin_lock(&vvd->pending_buf_list_lock);
118 if (list_empty(&vvd->pending_buf_list)) {
119 spin_unlock(&vvd->pending_buf_list_lock);
123 list_for_each_entry_safe(vb, vb_tmp, &vvd->pending_buf_list, list) {
124 if (vb->resource_id == virtio_vb->resource_id) {
130 spin_unlock(&vvd->pending_buf_list_lock);
135 int virtio_video_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
136 unsigned int *num_planes, unsigned int sizes[],
137 struct device *alloc_devs[])
140 struct virtio_video_stream *stream = vb2_get_drv_priv(vq);
141 struct video_format_info *p_info;
143 if (virtio_video_state(stream) == STREAM_STATE_ERROR)
149 if (V4L2_TYPE_IS_OUTPUT(vq->type))
150 p_info = &stream->in_info;
152 p_info = &stream->out_info;
154 *num_planes = p_info->num_planes;
156 for (i = 0; i < p_info->num_planes; i++)
157 sizes[i] = p_info->plane_format[i].plane_size;
163 build_virtio_video_sglist_contig(struct virtio_video_resource_sg_list *sgl,
164 struct vb2_buffer *vb, unsigned int plane)
166 sgl->entries[0].addr = cpu_to_le64(vb2_dma_contig_plane_dma_addr(vb, plane));
167 sgl->entries[0].length = cpu_to_le32(vb->planes[plane].length);
169 sgl->num_entries = 1;
171 return VIRTIO_VIDEO_RESOURCE_SG_SIZE(1);
175 build_virtio_video_sglist(struct virtio_video_resource_sg_list *sgl,
176 struct vb2_buffer *vb, unsigned int plane,
180 struct scatterlist *sg;
181 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, plane);
183 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
184 sgl->entries[i].addr = cpu_to_le64(has_iommu
187 sgl->entries[i].length = cpu_to_le32(sg->length);
190 sgl->num_entries = sgt->nents;
192 return VIRTIO_VIDEO_RESOURCE_SG_SIZE(sgt->nents);
195 int virtio_video_buf_init(struct vb2_buffer *vb)
200 struct virtio_video_resource_sg_list *sg_list;
201 unsigned int i, offset = 0, resource_id, nents = 0;
202 struct vb2_queue *vq = vb->vb2_queue;
203 enum v4l2_buf_type queue_type = vq->type;
204 struct virtio_video_stream *stream = vb2_get_drv_priv(vq);
205 struct virtio_video_buffer *virtio_vb = to_virtio_vb(vb);
206 struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
208 if (vvd->supp_non_contig) {
209 for (i = 0; i < vb->num_planes; i++) {
210 nents = vb2_dma_sg_plane_desc(vb, i)->nents;
211 buf_size += VIRTIO_VIDEO_RESOURCE_SG_SIZE(nents);
214 buf = kcalloc(1, buf_size, GFP_KERNEL);
218 for (i = 0; i < vb->num_planes; i++) {
219 sg_list = buf + offset;
220 offset += build_virtio_video_sglist(sg_list, vb, i,
224 buf_size = vb->num_planes * VIRTIO_VIDEO_RESOURCE_SG_SIZE(nents);
226 buf = kcalloc(1, buf_size, GFP_KERNEL);
230 for (i = 0; i < vb->num_planes; i++) {
231 sg_list = buf + offset;
232 offset += build_virtio_video_sglist_contig(sg_list,
237 virtio_video_resource_id_get(vvd, &resource_id);
239 ret = virtio_video_cmd_resource_attach(vvd, stream->stream_id,
241 to_virtio_queue_type(queue_type),
244 virtio_video_resource_id_put(vvd, resource_id);
249 virtio_vb->queued = false;
250 virtio_vb->resource_id = resource_id;
255 void virtio_video_buf_cleanup(struct vb2_buffer *vb)
257 struct virtio_video_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
258 struct virtio_video_buffer *virtio_vb = to_virtio_vb(vb);
259 struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
261 virtio_video_resource_id_put(vvd, virtio_vb->resource_id);
264 void virtio_video_buf_queue(struct vb2_buffer *vb)
267 struct virtio_video_buffer *virtio_vb;
268 uint32_t data_size[VB2_MAX_PLANES] = {0};
269 struct virtio_video_stream *stream = vb2_get_drv_priv(vb->vb2_queue);
270 struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
272 for (i = 0; i < vb->num_planes; ++i)
273 data_size[i] = vb->planes[i].bytesused;
275 virtio_vb = to_virtio_vb(vb);
277 if (!vvd->is_m2m_dev)
278 virtio_video_pending_buf_list_add(vvd, virtio_vb);
280 ret = virtio_video_cmd_resource_queue(vvd, stream->stream_id,
281 virtio_vb, data_size,
283 to_virtio_queue_type(vb->type));
285 v4l2_err(&vvd->v4l2_dev, "failed to queue buffer\n");
289 virtio_vb->queued = true;
292 int virtio_video_qbuf(struct file *file, void *priv,
293 struct v4l2_buffer *buf)
295 struct virtio_video_stream *stream = file2stream(file);
296 struct virtio_video_device *vvd = video_drvdata(file);
298 if (virtio_video_state(stream) == STREAM_STATE_ERROR)
302 return v4l2_m2m_ioctl_qbuf(file, priv, buf);
304 return vb2_ioctl_qbuf(file, priv, buf);
307 int virtio_video_dqbuf(struct file *file, void *priv,
308 struct v4l2_buffer *buf)
310 struct virtio_video_stream *stream = file2stream(file);
311 struct virtio_video_device *vvd = video_drvdata(file);
313 if (virtio_video_state(stream) == STREAM_STATE_ERROR)
317 return v4l2_m2m_ioctl_dqbuf(file, priv, buf);
319 return vb2_ioctl_dqbuf(file, priv, buf);
322 int virtio_video_querycap(struct file *file, void *fh,
323 struct v4l2_capability *cap)
325 struct video_device *video_dev = video_devdata(file);
326 struct virtio_video_device *vvd = video_drvdata(file);
328 if (strscpy(cap->driver, DRIVER_NAME, sizeof(cap->driver)) < 0)
329 v4l2_err(&vvd->v4l2_dev, "failed to copy driver name\n");
330 if (strscpy(cap->card, video_dev->name, sizeof(cap->card)) < 0)
331 v4l2_err(&vvd->v4l2_dev, "failed to copy card name\n");
333 snprintf(cap->bus_info, sizeof(cap->bus_info), "virtio:%s",
336 cap->device_caps = video_dev->device_caps;
340 int virtio_video_enum_framesizes(struct file *file, void *fh,
341 struct v4l2_frmsizeenum *f)
343 struct virtio_video_stream *stream = file2stream(file);
344 struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
345 struct video_format *fmt;
346 struct video_format_frame *frm;
347 struct virtio_video_format_frame *frame;
350 fmt = virtio_video_find_video_format(&vvd->input_fmt_list,
353 fmt = virtio_video_find_video_format(&vvd->output_fmt_list,
358 if (idx >= fmt->desc.num_frames)
361 frm = &fmt->frames[idx];
364 if (frame->width.min == frame->width.max &&
365 frame->height.min == frame->height.max) {
366 f->type = V4L2_FRMSIZE_TYPE_DISCRETE;
367 f->discrete.width = frame->width.min;
368 f->discrete.height = frame->height.min;
372 f->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
373 f->stepwise.min_width = frame->width.min;
374 f->stepwise.max_width = frame->width.max;
375 f->stepwise.min_height = frame->height.min;
376 f->stepwise.max_height = frame->height.max;
377 f->stepwise.step_width = frame->width.step;
378 f->stepwise.step_height = frame->height.step;
382 static bool in_stepped_interval(struct virtio_video_format_range range,
385 if (point < range.min || point > range.max)
388 if (range.step == 0 && range.min == range.max && range.min == point)
391 if (range.step != 0 && (point - range.min) % range.step == 0)
397 int virtio_video_enum_framemintervals(struct file *file, void *fh,
398 struct v4l2_frmivalenum *f)
400 struct virtio_video_stream *stream = file2stream(file);
401 struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
402 struct video_format *fmt;
403 struct video_format_frame *frm;
404 struct virtio_video_format_frame *frame;
405 struct virtio_video_format_range *frate;
409 fmt = virtio_video_find_video_format(&vvd->input_fmt_list,
412 fmt = virtio_video_find_video_format(&vvd->output_fmt_list,
417 for (f_idx = 0; f_idx <= fmt->desc.num_frames; f_idx++) {
418 frm = &fmt->frames[f_idx];
420 if (in_stepped_interval(frame->width, f->width) &&
421 in_stepped_interval(frame->height, f->height))
425 if (frame == NULL || f->index >= frame->num_rates)
428 frate = &frm->frame_rates[idx];
429 if (frate->max == frate->min) {
430 f->type = V4L2_FRMIVAL_TYPE_DISCRETE;
431 f->discrete.numerator = 1;
432 f->discrete.denominator = frate->max;
434 f->stepwise.min.numerator = 1;
435 f->stepwise.min.denominator = frate->max;
436 f->stepwise.max.numerator = 1;
437 f->stepwise.max.denominator = frate->min;
438 f->stepwise.step.numerator = 1;
439 f->stepwise.step.denominator = frate->step;
440 if (frate->step == 1)
441 f->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
443 f->type = V4L2_FRMIVAL_TYPE_STEPWISE;
448 int virtio_video_stream_get_params(struct virtio_video_device *vvd,
449 struct virtio_video_stream *stream)
453 if (vvd->is_m2m_dev) {
454 ret = virtio_video_cmd_get_params(vvd, stream,
455 VIRTIO_VIDEO_QUEUE_TYPE_INPUT);
457 v4l2_err(&vvd->v4l2_dev,
458 "failed to get stream in params\n");
463 ret = virtio_video_cmd_get_params(vvd, stream,
464 VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT);
466 v4l2_err(&vvd->v4l2_dev, "failed to get stream out params\n");
472 int virtio_video_stream_get_controls(struct virtio_video_device *vvd,
473 struct virtio_video_stream *stream)
477 ret = virtio_video_cmd_get_control(vvd, stream,
478 VIRTIO_VIDEO_CONTROL_PROFILE);
480 v4l2_err(&vvd->v4l2_dev, "failed to get stream profile\n");
484 ret = virtio_video_cmd_get_control(vvd, stream,
485 VIRTIO_VIDEO_CONTROL_LEVEL);
487 v4l2_err(&vvd->v4l2_dev, "failed to get stream level\n");
491 ret = virtio_video_cmd_get_control(vvd, stream,
492 VIRTIO_VIDEO_CONTROL_BITRATE);
494 v4l2_err(&vvd->v4l2_dev, "failed to get stream bitrate\n");
500 int virtio_video_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
502 struct video_format_info *info;
503 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
504 struct virtio_video_stream *stream = file2stream(file);
506 if (!V4L2_TYPE_IS_OUTPUT(f->type))
507 info = &stream->out_info;
509 info = &stream->in_info;
511 virtio_video_format_from_info(info, pix_mp);
516 int virtio_video_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
519 struct virtio_video_stream *stream = file2stream(file);
520 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
521 struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
522 struct video_format_info info;
523 struct video_format_info *p_info;
526 ret = virtio_video_try_fmt(stream, f);
530 if (V4L2_TYPE_IS_OUTPUT(f->type)) {
531 virtio_video_format_fill_default_info(&info, &stream->in_info);
532 queue = VIRTIO_VIDEO_QUEUE_TYPE_INPUT;
534 virtio_video_format_fill_default_info(&info, &stream->out_info);
535 queue = VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT;
538 info.frame_width = pix_mp->width;
539 info.frame_height = pix_mp->height;
540 info.num_planes = pix_mp->num_planes;
541 info.fourcc_format = pix_mp->pixelformat;
543 for (i = 0; i < info.num_planes; i++) {
544 info.plane_format[i].stride =
545 pix_mp->plane_fmt[i].bytesperline;
546 info.plane_format[i].plane_size =
547 pix_mp->plane_fmt[i].sizeimage;
550 virtio_video_cmd_set_params(vvd, stream, &info, queue);
551 virtio_video_stream_get_params(vvd, stream);
553 if (V4L2_TYPE_IS_OUTPUT(f->type))
554 p_info = &stream->in_info;
556 p_info = &stream->out_info;
558 virtio_video_format_from_info(p_info, pix_mp);
563 int virtio_video_g_selection(struct file *file, void *fh,
564 struct v4l2_selection *sel)
566 struct video_format_info *info;
567 struct virtio_video_stream *stream = file2stream(file);
568 struct virtio_video_device *vvd = video_drvdata(file);
571 case VIRTIO_VIDEO_DEVICE_ENCODER:
572 if (!V4L2_TYPE_IS_OUTPUT(sel->type))
574 info = &stream->in_info;
576 case VIRTIO_VIDEO_DEVICE_DECODER:
577 case VIRTIO_VIDEO_DEVICE_CAMERA:
578 if (V4L2_TYPE_IS_OUTPUT(sel->type))
580 info = &stream->out_info;
583 v4l2_err(&vvd->v4l2_dev, "unsupported device type\n");
587 switch (sel->target) {
588 case V4L2_SEL_TGT_COMPOSE:
589 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
590 case V4L2_SEL_TGT_COMPOSE_PADDED:
591 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
592 sel->r.width = info->frame_width;
593 sel->r.height = info->frame_height;
595 case V4L2_SEL_TGT_CROP_BOUNDS:
596 case V4L2_SEL_TGT_CROP_DEFAULT:
597 case V4L2_SEL_TGT_CROP:
598 sel->r.top = info->crop.top;
599 sel->r.left = info->crop.left;
600 sel->r.width = info->frame_width;
601 sel->r.height = info->frame_height;
604 v4l2_dbg(1, vvd->debug, &vvd->v4l2_dev,
605 "unsupported/invalid selection target: %d\n",
613 int virtio_video_try_fmt(struct virtio_video_stream *stream,
614 struct v4l2_format *f)
617 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
618 struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
619 struct video_format *fmt;
621 struct video_format_frame *frm;
622 struct virtio_video_format_frame *frame;
624 if (virtio_video_state(stream) == STREAM_STATE_ERROR)
627 if (V4L2_TYPE_IS_OUTPUT(f->type))
628 fmt = virtio_video_find_video_format(&vvd->input_fmt_list,
629 pix_mp->pixelformat);
631 fmt = virtio_video_find_video_format(&vvd->output_fmt_list,
632 pix_mp->pixelformat);
635 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
636 virtio_video_format_from_info(&stream->out_info,
638 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
639 virtio_video_format_from_info(&stream->in_info,
646 for (i = 0; i < fmt->desc.num_frames && !found; i++) {
647 frm = &fmt->frames[i];
649 if (!within_range(frame->width.min, pix_mp->width,
653 if (!within_range(frame->height.min, pix_mp->height,
658 * Try to find a more suitable frame size. Go with the current
661 if (needs_alignment(pix_mp->width, frame->width.step))
664 if (needs_alignment(pix_mp->height, frame->height.step))
667 stream->current_frame = frm;
672 frm = &fmt->frames[idx];
677 pix_mp->width = clamp(pix_mp->width, frame->width.min,
679 if (frame->width.step != 0)
680 pix_mp->width = ALIGN(pix_mp->width, frame->width.step);
682 pix_mp->height = clamp(pix_mp->height, frame->height.min,
684 if (frame->height.step != 0)
685 pix_mp->height = ALIGN(pix_mp->height,
687 stream->current_frame = frm;
693 static int virtio_video_queue_free(struct virtio_video_device *vvd,
694 struct virtio_video_stream *stream,
695 enum v4l2_buf_type type)
698 enum virtio_video_queue_type queue_type = to_virtio_queue_type(type);
700 ret = virtio_video_cmd_queue_detach_resources(vvd, stream, queue_type);
702 v4l2_warn(&vvd->v4l2_dev,
703 "failed to destroy resources\n");
710 int virtio_video_reqbufs(struct file *file, void *priv,
711 struct v4l2_requestbuffers *rb)
714 struct virtio_video_stream *stream = file2stream(file);
715 struct v4l2_m2m_ctx *m2m_ctx = stream->fh.m2m_ctx;
716 struct virtio_video_device *vvd = video_drvdata(file);
717 struct video_device *vdev = video_devdata(file);
718 struct vb2_queue *vq;
721 vq = v4l2_m2m_get_vq(m2m_ctx, rb->type);
725 if (rb->count == 0) {
726 ret = virtio_video_queue_free(vvd, stream, vq->type);
732 return v4l2_m2m_reqbufs(file, m2m_ctx, rb);
734 return vb2_ioctl_reqbufs(file, priv, rb);
737 int virtio_video_subscribe_event(struct v4l2_fh *fh,
738 const struct v4l2_event_subscription *sub)
741 case V4L2_EVENT_SOURCE_CHANGE:
742 return v4l2_src_change_event_subscribe(fh, sub);
744 return v4l2_ctrl_subscribe_event(fh, sub);
748 void virtio_video_queue_eos_event(struct virtio_video_stream *stream)
750 static const struct v4l2_event eos_event = {
751 .type = V4L2_EVENT_EOS
754 v4l2_event_queue_fh(&stream->fh, &eos_event);
757 void virtio_video_queue_res_chg_event(struct virtio_video_stream *stream)
759 static const struct v4l2_event ev_src_ch = {
760 .type = V4L2_EVENT_SOURCE_CHANGE,
761 .u.src_change.changes =
762 V4L2_EVENT_SRC_CH_RESOLUTION,
765 v4l2_event_queue_fh(&stream->fh, &ev_src_ch);
768 void virtio_video_handle_error(struct virtio_video_stream *stream)
770 struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
773 virtio_video_queue_release_buffers
774 (stream, VIRTIO_VIDEO_QUEUE_TYPE_INPUT);
776 virtio_video_queue_release_buffers
777 (stream, VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT);
780 int virtio_video_queue_release_buffers(struct virtio_video_stream *stream,
781 enum virtio_video_queue_type queue_type)
784 struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
785 struct vb2_v4l2_buffer *v4l2_vb;
786 struct virtio_video_buffer *vvb;
788 ret = virtio_video_cmd_queue_clear(vvd, stream, queue_type);
790 v4l2_err(&vvd->v4l2_dev, "failed to clear queue\n");
794 if (!vvd->is_m2m_dev) {
795 while (!virtio_video_pending_buf_list_pop(vvd, &vvb) && vvb) {
796 v4l2_vb = &vvb->v4l2_m2m_vb.vb;
797 v4l2_m2m_buf_done(v4l2_vb, VB2_BUF_STATE_ERROR);
803 if (queue_type == VIRTIO_VIDEO_QUEUE_TYPE_INPUT)
804 v4l2_vb = v4l2_m2m_src_buf_remove(stream->fh.m2m_ctx);
806 v4l2_vb = v4l2_m2m_dst_buf_remove(stream->fh.m2m_ctx);
809 v4l2_m2m_buf_done(v4l2_vb, VB2_BUF_STATE_ERROR);
815 void virtio_video_buf_done(struct virtio_video_buffer *virtio_vb,
816 uint32_t flags, uint64_t timestamp,
817 uint32_t data_sizes[])
820 enum vb2_buffer_state done_state = VB2_BUF_STATE_DONE;
821 struct vb2_v4l2_buffer *v4l2_vb = &virtio_vb->v4l2_m2m_vb.vb;
822 struct vb2_buffer *vb = &v4l2_vb->vb2_buf;
823 struct vb2_queue *vb2_queue = vb->vb2_queue;
824 struct virtio_video_stream *stream = vb2_get_drv_priv(vb2_queue);
825 struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
826 struct video_format_info *p_info;
828 virtio_vb->queued = false;
830 if (flags & VIRTIO_VIDEO_DEQUEUE_FLAG_ERR)
831 done_state = VB2_BUF_STATE_ERROR;
833 if (flags & VIRTIO_VIDEO_DEQUEUE_FLAG_KEY_FRAME)
834 v4l2_vb->flags |= V4L2_BUF_FLAG_KEYFRAME;
836 if (flags & VIRTIO_VIDEO_DEQUEUE_FLAG_BFRAME)
837 v4l2_vb->flags |= V4L2_BUF_FLAG_BFRAME;
839 if (flags & VIRTIO_VIDEO_DEQUEUE_FLAG_PFRAME)
840 v4l2_vb->flags |= V4L2_BUF_FLAG_PFRAME;
842 if (flags & VIRTIO_VIDEO_DEQUEUE_FLAG_EOS) {
843 v4l2_vb->flags |= V4L2_BUF_FLAG_LAST;
844 virtio_video_state_update(stream, STREAM_STATE_STOPPED);
845 virtio_video_queue_eos_event(stream);
848 if ((flags & VIRTIO_VIDEO_DEQUEUE_FLAG_ERR) ||
849 (flags & VIRTIO_VIDEO_DEQUEUE_FLAG_EOS)) {
850 vb->planes[0].bytesused = 0;
852 if (!vvd->is_m2m_dev)
853 virtio_video_pending_buf_list_del(vvd, virtio_vb);
855 v4l2_m2m_buf_done(v4l2_vb, done_state);
859 if (!V4L2_TYPE_IS_OUTPUT(vb2_queue->type)) {
861 case VIRTIO_VIDEO_DEVICE_ENCODER:
862 for (i = 0; i < vb->num_planes; i++)
863 vb->planes[i].bytesused =
864 le32_to_cpu(data_sizes[i]);
866 case VIRTIO_VIDEO_DEVICE_CAMERA:
867 case VIRTIO_VIDEO_DEVICE_DECODER:
868 p_info = &stream->out_info;
869 for (i = 0; i < p_info->num_planes; i++)
870 vb->planes[i].bytesused =
871 p_info->plane_format[i].plane_size;
875 vb->timestamp = timestamp;
878 if (!vvd->is_m2m_dev)
879 virtio_video_pending_buf_list_del(vvd, virtio_vb);
881 v4l2_m2m_buf_done(v4l2_vb, done_state);
884 static int virtio_video_set_device_busy(struct virtio_video_device *vvd)
886 struct video_device *vd = &vvd->video_dev;
889 /* Multiple open is allowed for m2m device */
893 mutex_lock(vd->lock);
895 if (vvd->device_busy)
898 vvd->device_busy = true;
900 mutex_unlock(vd->lock);
905 static void virtio_video_clear_device_busy(struct virtio_video_device *vvd,
908 /* Nothing to do for m2m device */
915 vvd->device_busy = false;
921 static int virtio_video_device_open(struct file *file)
925 char name[TASK_COMM_LEN];
926 struct virtio_video_stream *stream;
927 struct video_format *default_fmt;
928 enum virtio_video_format format;
929 struct video_device *video_dev = video_devdata(file);
930 struct virtio_video_device *vvd = video_drvdata(file);
932 ret = virtio_video_set_device_busy(vvd);
934 v4l2_err(&vvd->v4l2_dev, "device already in use.\n");
938 default_fmt = list_first_entry_or_null(vvd->ops->get_fmt_list(vvd),
942 v4l2_err(&vvd->v4l2_dev, "device failed to start\n");
947 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
953 get_task_comm(name, current);
954 format = virtio_video_v4l2_format_to_virtio(default_fmt->desc.format);
955 virtio_video_stream_id_get(vvd, stream, &stream_id);
956 ret = virtio_video_cmd_stream_create(vvd, stream_id, format, name);
958 v4l2_err(&vvd->v4l2_dev, "failed to create stream\n");
959 goto err_stream_create;
962 stream->video_dev = video_dev;
963 stream->stream_id = stream_id;
965 virtio_video_state_reset(stream);
967 ret = virtio_video_stream_get_params(vvd, stream);
969 goto err_stream_create;
971 if (format >= VIRTIO_VIDEO_FORMAT_CODED_MIN &&
972 format <= VIRTIO_VIDEO_FORMAT_CODED_MAX) {
973 ret = virtio_video_stream_get_controls(vvd, stream);
975 goto err_stream_create;
978 mutex_init(&stream->vq_mutex);
979 v4l2_fh_init(&stream->fh, video_dev);
980 stream->fh.ctrl_handler = &stream->ctrl_handler;
982 if (vvd->is_m2m_dev) {
983 stream->fh.m2m_ctx = v4l2_m2m_ctx_init(vvd->m2m_dev, stream,
984 vvd->ops->init_queues);
985 if (IS_ERR(stream->fh.m2m_ctx)) {
986 ret = PTR_ERR(stream->fh.m2m_ctx);
990 v4l2_m2m_set_src_buffered(stream->fh.m2m_ctx, true);
991 v4l2_m2m_set_dst_buffered(stream->fh.m2m_ctx, true);
993 vvd->ops->init_queues(stream, NULL, &vvd->vb2_output_queue);
994 /* Video dev queue is required for vb2 ioctl wrappers */
995 video_dev->queue = &vvd->vb2_output_queue;
998 file->private_data = &stream->fh;
999 v4l2_fh_add(&stream->fh);
1001 if (vvd->ops->init_ctrls) {
1002 ret = vvd->ops->init_ctrls(stream);
1004 v4l2_err(&vvd->v4l2_dev, "failed to init controls\n");
1005 goto err_init_ctrls;
1011 v4l2_fh_del(&stream->fh);
1012 mutex_lock(video_dev->lock);
1013 if (vvd->is_m2m_dev)
1014 v4l2_m2m_ctx_release(stream->fh.m2m_ctx);
1015 mutex_unlock(video_dev->lock);
1017 v4l2_fh_exit(&stream->fh);
1019 virtio_video_stream_id_put(vvd, stream_id);
1022 virtio_video_clear_device_busy(vvd, video_dev->lock);
1026 static int virtio_video_device_release(struct file *file)
1028 struct virtio_video_stream *stream = file2stream(file);
1029 struct video_device *video_dev = video_devdata(file);
1030 struct virtio_video_device *vvd = video_drvdata(file);
1032 mutex_lock(video_dev->lock);
1034 v4l2_fh_del(&stream->fh);
1035 if (vvd->is_m2m_dev) {
1036 v4l2_m2m_ctx_release(stream->fh.m2m_ctx);
1037 } else if (file->private_data == video_dev->queue->owner) {
1038 vb2_queue_release(&vvd->vb2_output_queue);
1039 video_dev->queue->owner = NULL;
1042 v4l2_fh_exit(&stream->fh);
1044 virtio_video_cmd_stream_destroy(vvd, stream->stream_id);
1045 virtio_video_stream_id_put(vvd, stream->stream_id);
1049 /* Mutex already locked here, passing NULL */
1050 virtio_video_clear_device_busy(vvd, NULL);
1052 mutex_unlock(video_dev->lock);
1057 static const struct v4l2_file_operations virtio_video_device_m2m_fops = {
1058 .owner = THIS_MODULE,
1059 .open = virtio_video_device_open,
1060 .release = virtio_video_device_release,
1061 .poll = v4l2_m2m_fop_poll,
1062 .unlocked_ioctl = video_ioctl2,
1063 .mmap = v4l2_m2m_fop_mmap,
1066 static const struct v4l2_file_operations virtio_video_device_fops = {
1067 .owner = THIS_MODULE,
1068 .open = virtio_video_device_open,
1069 .release = virtio_video_device_release,
1070 .poll = vb2_fop_poll,
1071 .unlocked_ioctl = video_ioctl2,
1072 .mmap = vb2_fop_mmap,
1075 static void virtio_video_device_run(void *priv)
1080 static void virtio_video_device_job_abort(void *priv)
1082 struct virtio_video_stream *stream = priv;
1083 struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
1085 v4l2_m2m_job_finish(vvd->m2m_dev, stream->fh.m2m_ctx);
1088 static const struct v4l2_m2m_ops virtio_video_device_m2m_ops = {
1089 .device_run = virtio_video_device_run,
1090 .job_abort = virtio_video_device_job_abort,
1093 static int virtio_video_device_register(struct virtio_video_device *vvd)
1096 struct video_device *vd;
1098 vd = &vvd->video_dev;
1100 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,0)
1101 ret = video_register_device(vd, VFL_TYPE_VIDEO, vvd->vid_dev_nr);
1103 ret = video_register_device(vd, VFL_TYPE_GRABBER, vvd->vid_dev_nr);
1106 v4l2_err(&vvd->v4l2_dev, "failed to register video device\n");
1110 v4l2_info(&vvd->v4l2_dev, "Device '%s' registered as /dev/video%d\n",
1116 static void virtio_video_device_unregister(struct virtio_video_device *vvd)
1118 video_unregister_device(&vvd->video_dev);
1122 virtio_video_query_capability(struct virtio_video_device *vvd,
1124 enum virtio_video_queue_type queue_type)
1127 int resp_size = vvd->max_caps_len;
1129 ret = virtio_video_cmd_query_capability(vvd, resp_buf, resp_size,
1132 v4l2_err(&vvd->v4l2_dev, "failed to query capability\n");
1137 int virtio_video_device_init(struct virtio_video_device *vvd)
1142 struct video_device *vd;
1143 struct v4l2_m2m_dev *m2m_dev;
1144 const struct v4l2_file_operations *fops;
1145 void *input_resp_buf, *output_resp_buf;
1147 output_resp_buf = kzalloc(vvd->max_caps_len, GFP_KERNEL);
1148 if (!output_resp_buf)
1151 ret = virtio_video_query_capability(vvd, output_resp_buf,
1152 VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT);
1154 v4l2_err(&vvd->v4l2_dev, "failed to get output caps\n");
1155 goto err_output_cap;
1158 if (vvd->is_m2m_dev) {
1159 input_resp_buf = kzalloc(vvd->max_caps_len, GFP_KERNEL);
1160 if (!input_resp_buf) {
1165 ret = virtio_video_query_capability(vvd, input_resp_buf,
1166 VIRTIO_VIDEO_QUEUE_TYPE_INPUT);
1168 v4l2_err(&vvd->v4l2_dev, "failed to get input caps\n");
1172 m2m_dev = v4l2_m2m_init(&virtio_video_device_m2m_ops);
1173 if (IS_ERR(m2m_dev)) {
1174 v4l2_err(&vvd->v4l2_dev, "failed to init m2m device\n");
1175 ret = PTR_ERR(m2m_dev);
1178 vfl_dir = VFL_DIR_M2M;
1179 fops = &virtio_video_device_m2m_fops;
1180 dev_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
1182 input_resp_buf = NULL;
1184 vfl_dir = VFL_DIR_RX;
1185 fops = &virtio_video_device_fops;
1186 dev_caps = V4L2_CAP_STREAMING;
1187 if (vvd->is_mplane_cam)
1188 dev_caps |= V4L2_CAP_VIDEO_CAPTURE_MPLANE;
1190 dev_caps |= V4L2_CAP_VIDEO_CAPTURE;
1193 vvd->m2m_dev = m2m_dev;
1194 mutex_init(&vvd->video_dev_mutex);
1195 vd = &vvd->video_dev;
1196 vd->lock = &vvd->video_dev_mutex;
1197 vd->v4l2_dev = &vvd->v4l2_dev;
1198 vd->vfl_dir = vfl_dir;
1199 vd->ioctl_ops = NULL;
1201 vd->device_caps = dev_caps;
1202 vd->release = video_device_release_empty;
1204 /* Use the selection API instead */
1205 v4l2_disable_ioctl(vd, VIDIOC_CROPCAP);
1206 v4l2_disable_ioctl(vd, VIDIOC_G_CROP);
1208 video_set_drvdata(vd, vvd);
1210 INIT_LIST_HEAD(&vvd->input_fmt_list);
1211 INIT_LIST_HEAD(&vvd->output_fmt_list);
1212 INIT_LIST_HEAD(&vvd->controls_fmt_list);
1213 INIT_LIST_HEAD(&vvd->pending_buf_list);
1215 vvd->num_output_fmts = 0;
1216 vvd->num_input_fmts = 0;
1218 switch (vvd->type) {
1219 case VIRTIO_VIDEO_DEVICE_CAMERA:
1220 virtio_video_cam_init(vvd);
1222 case VIRTIO_VIDEO_DEVICE_ENCODER:
1223 virtio_video_enc_init(vvd);
1225 case VIRTIO_VIDEO_DEVICE_DECODER:
1227 virtio_video_dec_init(vvd);
1231 ret = virtio_video_parse_virtio_capabilities(vvd, input_resp_buf,
1234 v4l2_err(&vvd->v4l2_dev, "failed to parse a function\n");
1238 ret = virtio_video_parse_virtio_control(vvd);
1240 v4l2_err(&vvd->v4l2_dev, "failed to query controls\n");
1241 goto parse_ctrl_err;
1244 ret = virtio_video_device_register(vvd);
1246 v4l2_err(&vvd->v4l2_dev,
1247 "failed to init virtio video device\n");
1254 virtio_video_clean_control(vvd);
1256 virtio_video_clean_capability(vvd);
1258 if (vvd->is_m2m_dev)
1259 v4l2_m2m_release(vvd->m2m_dev);
1263 if (vvd->is_m2m_dev)
1264 kfree(input_resp_buf);
1267 kfree(output_resp_buf);
1272 void virtio_video_device_deinit(struct virtio_video_device *vvd)
1274 vvd->commandq.ready = false;
1275 vvd->eventq.ready = false;
1277 virtio_video_device_unregister(vvd);
1278 if (vvd->is_m2m_dev)
1279 v4l2_m2m_release(vvd->m2m_dev);
1280 virtio_video_clean_control(vvd);
1281 virtio_video_clean_capability(vvd);