agl-test-framework: bump SRCREV
[AGL/meta-agl-devel.git] / meta-egvirt / recipes-kernel / kernel-module-virtio-video / files / virtio_video_device.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Driver for virtio video device.
3  *
4  * Copyright 2020 OpenSynergy GmbH.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <linux/version.h>
21 #include <media/v4l2-event.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/videobuf2-dma-sg.h>
24
25 #include "virtio_video.h"
26
27 enum video_stream_state virtio_video_state(struct virtio_video_stream *stream)
28 {
29         return atomic_read(&stream->state);
30 }
31
32 void virtio_video_state_reset(struct virtio_video_stream *stream)
33 {
34         atomic_set(&stream->state, STREAM_STATE_IDLE);
35 }
36
37 void virtio_video_state_update(struct virtio_video_stream *stream,
38                                enum video_stream_state new_state)
39 {
40         enum video_stream_state prev_state;
41
42         do {
43             prev_state = atomic_read(&stream->state);
44             if (prev_state == STREAM_STATE_ERROR)
45                     return;
46         } while (atomic_cmpxchg(&stream->state, prev_state, new_state) !=
47                  prev_state);
48 }
49
50 int virtio_video_pending_buf_list_empty(struct virtio_video_device *vvd)
51 {
52         int ret = 0;
53
54         if (vvd->is_m2m_dev) {
55                 v4l2_err(&vvd->v4l2_dev, "Unexpected call for m2m device!\n");
56                 return -EPERM;
57         }
58
59         spin_lock(&vvd->pending_buf_list_lock);
60         if (list_empty(&vvd->pending_buf_list))
61                 ret = 1;
62         spin_unlock(&vvd->pending_buf_list_lock);
63
64         return ret;
65 }
66
67 int virtio_video_pending_buf_list_pop(struct virtio_video_device *vvd,
68                                       struct virtio_video_buffer **virtio_vb)
69 {
70         struct virtio_video_buffer *retbuf;
71
72         if (vvd->is_m2m_dev) {
73                 v4l2_err(&vvd->v4l2_dev, "Unexpected call for m2m device!\n");
74                 return -EPERM;
75         }
76
77         spin_lock(&vvd->pending_buf_list_lock);
78         if (list_empty(&vvd->pending_buf_list)) {
79                 spin_unlock(&vvd->pending_buf_list_lock);
80                 return -EAGAIN;
81         }
82
83         retbuf = list_first_entry(&vvd->pending_buf_list,
84                                   struct virtio_video_buffer, list);
85         spin_unlock(&vvd->pending_buf_list_lock);
86
87         *virtio_vb = retbuf;
88         return 0;
89 }
90
91 int virtio_video_pending_buf_list_add(struct virtio_video_device *vvd,
92                                       struct virtio_video_buffer *virtio_vb)
93 {
94         if (vvd->is_m2m_dev) {
95                 v4l2_err(&vvd->v4l2_dev, "Unexpected call for m2m device!\n");
96                 return -EPERM;
97         }
98
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);
102
103         return 0;
104 }
105
106 int virtio_video_pending_buf_list_del(struct virtio_video_device *vvd,
107                                       struct virtio_video_buffer *virtio_vb)
108 {
109         struct virtio_video_buffer *vb, *vb_tmp;
110         int ret = -EINVAL;
111
112         if (vvd->is_m2m_dev) {
113                 v4l2_err(&vvd->v4l2_dev, "Unexpected call for m2m device!\n");
114                 return -EPERM;
115         }
116
117         spin_lock(&vvd->pending_buf_list_lock);
118         if (list_empty(&vvd->pending_buf_list)) {
119                 spin_unlock(&vvd->pending_buf_list_lock);
120                 return -EAGAIN;
121         }
122
123         list_for_each_entry_safe(vb, vb_tmp, &vvd->pending_buf_list, list) {
124                 if (vb->resource_id == virtio_vb->resource_id) {
125                         list_del(&vb->list);
126                         ret = 0;
127                         break;
128                 }
129         }
130         spin_unlock(&vvd->pending_buf_list_lock);
131
132         return ret;
133 }
134
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[])
138 {
139         int i;
140         struct virtio_video_stream *stream = vb2_get_drv_priv(vq);
141         struct video_format_info *p_info;
142
143         if (virtio_video_state(stream) == STREAM_STATE_ERROR)
144                 return -EIO;
145
146         if (*num_planes)
147                 return 0;
148
149         if (V4L2_TYPE_IS_OUTPUT(vq->type))
150                 p_info = &stream->in_info;
151         else
152                 p_info = &stream->out_info;
153
154         *num_planes = p_info->num_planes;
155
156         for (i = 0; i < p_info->num_planes; i++)
157                 sizes[i] = p_info->plane_format[i].plane_size;
158
159         return 0;
160 }
161
162 static unsigned int
163 build_virtio_video_sglist_contig(struct virtio_video_resource_sg_list *sgl,
164                                  struct vb2_buffer *vb, unsigned int plane)
165 {
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);
168
169         sgl->num_entries = 1;
170
171         return VIRTIO_VIDEO_RESOURCE_SG_SIZE(1);
172 }
173
174 static unsigned int
175 build_virtio_video_sglist(struct virtio_video_resource_sg_list *sgl,
176                           struct vb2_buffer *vb, unsigned int plane,
177                           bool has_iommu)
178 {
179         int i;
180         struct scatterlist *sg;
181         struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, plane);
182
183         for_each_sg(sgt->sgl, sg, sgt->nents, i) {
184                 sgl->entries[i].addr = cpu_to_le64(has_iommu
185                                                         ? sg_dma_address(sg)
186                                                         : sg_phys(sg));
187                 sgl->entries[i].length = cpu_to_le32(sg->length);
188         }
189
190         sgl->num_entries = sgt->nents;
191
192         return VIRTIO_VIDEO_RESOURCE_SG_SIZE(sgt->nents);
193 }
194
195 int virtio_video_buf_init(struct vb2_buffer *vb)
196 {
197         int ret = 0;
198         void *buf;
199         size_t buf_size = 0;
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);
207
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);
212                 }
213
214                 buf = kcalloc(1, buf_size, GFP_KERNEL);
215                 if (!buf)
216                         return -ENOMEM;
217
218                 for (i = 0; i < vb->num_planes; i++) {
219                         sg_list = buf + offset;
220                         offset += build_virtio_video_sglist(sg_list, vb, i,
221                                                             vvd->has_iommu);
222                 }
223         } else {
224                 buf_size = vb->num_planes * VIRTIO_VIDEO_RESOURCE_SG_SIZE(nents);
225
226                 buf = kcalloc(1, buf_size, GFP_KERNEL);
227                 if (!buf)
228                         return -ENOMEM;
229
230                 for (i = 0; i < vb->num_planes; i++) {
231                         sg_list = buf + offset;
232                         offset += build_virtio_video_sglist_contig(sg_list,
233                                                                    vb, i);
234                 }
235         }
236
237         virtio_video_resource_id_get(vvd, &resource_id);
238
239         ret = virtio_video_cmd_resource_attach(vvd, stream->stream_id,
240                                                resource_id,
241                                                to_virtio_queue_type(queue_type),
242                                                buf, buf_size);
243         if (ret) {
244                 virtio_video_resource_id_put(vvd, resource_id);
245                 kfree(buf);
246                 return ret;
247         }
248
249         virtio_vb->queued = false;
250         virtio_vb->resource_id = resource_id;
251
252         return 0;
253 }
254
255 void virtio_video_buf_cleanup(struct vb2_buffer *vb)
256 {
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);
260
261         virtio_video_resource_id_put(vvd, virtio_vb->resource_id);
262 }
263
264 void virtio_video_buf_queue(struct vb2_buffer *vb)
265 {
266         int i, ret;
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);
271
272         for (i = 0; i < vb->num_planes; ++i)
273                 data_size[i] = vb->planes[i].bytesused;
274
275         virtio_vb = to_virtio_vb(vb);
276
277         if (!vvd->is_m2m_dev)
278                 virtio_video_pending_buf_list_add(vvd, virtio_vb);
279
280         ret = virtio_video_cmd_resource_queue(vvd, stream->stream_id,
281                                               virtio_vb, data_size,
282                                               vb->num_planes,
283                                               to_virtio_queue_type(vb->type));
284         if (ret) {
285                 v4l2_err(&vvd->v4l2_dev, "failed to queue buffer\n");
286                 return;
287         }
288
289         virtio_vb->queued = true;
290 }
291
292 int virtio_video_qbuf(struct file *file, void *priv,
293                       struct v4l2_buffer *buf)
294 {
295         struct virtio_video_stream *stream = file2stream(file);
296         struct virtio_video_device *vvd = video_drvdata(file);
297
298         if (virtio_video_state(stream) == STREAM_STATE_ERROR)
299                 return -EIO;
300
301         if (vvd->is_m2m_dev)
302                 return v4l2_m2m_ioctl_qbuf(file, priv, buf);
303
304         return vb2_ioctl_qbuf(file, priv, buf);
305 }
306
307 int virtio_video_dqbuf(struct file *file, void *priv,
308                        struct v4l2_buffer *buf)
309 {
310         struct virtio_video_stream *stream = file2stream(file);
311         struct virtio_video_device *vvd = video_drvdata(file);
312
313         if (virtio_video_state(stream) == STREAM_STATE_ERROR)
314                 return -EIO;
315
316         if (vvd->is_m2m_dev)
317                 return v4l2_m2m_ioctl_dqbuf(file, priv, buf);
318
319         return vb2_ioctl_dqbuf(file, priv, buf);
320 }
321
322 int virtio_video_querycap(struct file *file, void *fh,
323                           struct v4l2_capability *cap)
324 {
325         struct video_device *video_dev = video_devdata(file);
326         struct virtio_video_device *vvd = video_drvdata(file);
327
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");
332
333         snprintf(cap->bus_info, sizeof(cap->bus_info), "virtio:%s",
334                  video_dev->name);
335
336         cap->device_caps = video_dev->device_caps;
337         return 0;
338 }
339
340 int virtio_video_enum_framesizes(struct file *file, void *fh,
341                                  struct v4l2_frmsizeenum *f)
342 {
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;
348         int idx = f->index;
349
350         fmt = virtio_video_find_video_format(&vvd->input_fmt_list,
351                                              f->pixel_format);
352         if (fmt == NULL)
353                 fmt = virtio_video_find_video_format(&vvd->output_fmt_list,
354                                                      f->pixel_format);
355         if (fmt == NULL)
356                 return -EINVAL;
357
358         if (idx >= fmt->desc.num_frames)
359                 return -EINVAL;
360
361         frm = &fmt->frames[idx];
362         frame = &frm->frame;
363
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;
369                 return 0;
370         }
371
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;
379         return 0;
380 }
381
382 static bool in_stepped_interval(struct virtio_video_format_range range,
383                                 uint32_t point)
384 {
385         if (point < range.min || point > range.max)
386                 return false;
387
388         if (range.step == 0 && range.min == range.max && range.min == point)
389                 return true;
390
391         if (range.step != 0 && (point - range.min) % range.step == 0)
392                 return true;
393
394         return false;
395 }
396
397 int virtio_video_enum_framemintervals(struct file *file, void *fh,
398                                       struct v4l2_frmivalenum *f)
399 {
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;
406         int idx = f->index;
407         int f_idx;
408
409         fmt = virtio_video_find_video_format(&vvd->input_fmt_list,
410                                              f->pixel_format);
411         if (fmt == NULL)
412                 fmt = virtio_video_find_video_format(&vvd->output_fmt_list,
413                                                      f->pixel_format);
414         if (fmt == NULL)
415                 return -EINVAL;
416
417         for (f_idx = 0; f_idx <= fmt->desc.num_frames; f_idx++) {
418                 frm = &fmt->frames[f_idx];
419                 frame = &frm->frame;
420                 if (in_stepped_interval(frame->width, f->width) &&
421                     in_stepped_interval(frame->height, f->height))
422                         break;
423         }
424
425         if (frame == NULL || f->index >= frame->num_rates)
426                 return -EINVAL;
427
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;
433         } else {
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;
442                 else
443                         f->type = V4L2_FRMIVAL_TYPE_STEPWISE;
444         }
445         return 0;
446 }
447
448 int virtio_video_stream_get_params(struct virtio_video_device *vvd,
449                                    struct virtio_video_stream *stream)
450 {
451         int ret;
452
453         if (vvd->is_m2m_dev) {
454                 ret = virtio_video_cmd_get_params(vvd, stream,
455                                                 VIRTIO_VIDEO_QUEUE_TYPE_INPUT);
456                 if (ret) {
457                         v4l2_err(&vvd->v4l2_dev,
458                                  "failed to get stream in params\n");
459                         goto err_get_parms;
460                 }
461         }
462
463         ret = virtio_video_cmd_get_params(vvd, stream,
464                                           VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT);
465         if (ret)
466                 v4l2_err(&vvd->v4l2_dev, "failed to get stream out params\n");
467
468 err_get_parms:
469         return ret;
470 }
471
472 int virtio_video_stream_get_controls(struct virtio_video_device *vvd,
473                                      struct virtio_video_stream *stream)
474 {
475         int ret;
476
477         ret = virtio_video_cmd_get_control(vvd, stream,
478                                            VIRTIO_VIDEO_CONTROL_PROFILE);
479         if (ret) {
480                 v4l2_err(&vvd->v4l2_dev, "failed to get stream profile\n");
481                 goto err_get_ctrl;
482         }
483
484         ret = virtio_video_cmd_get_control(vvd, stream,
485                                            VIRTIO_VIDEO_CONTROL_LEVEL);
486         if (ret) {
487                 v4l2_err(&vvd->v4l2_dev, "failed to get stream level\n");
488                 goto err_get_ctrl;
489         }
490
491         ret = virtio_video_cmd_get_control(vvd, stream,
492                                            VIRTIO_VIDEO_CONTROL_BITRATE);
493         if (ret)
494                 v4l2_err(&vvd->v4l2_dev, "failed to get stream bitrate\n");
495
496 err_get_ctrl:
497         return ret;
498 }
499
500 int virtio_video_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
501 {
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);
505
506         if (!V4L2_TYPE_IS_OUTPUT(f->type))
507                 info = &stream->out_info;
508         else
509                 info = &stream->in_info;
510
511         virtio_video_format_from_info(info, pix_mp);
512
513         return 0;
514 }
515
516 int virtio_video_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
517 {
518         int i, ret;
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;
524         uint32_t queue;
525
526         ret = virtio_video_try_fmt(stream, f);
527         if (ret)
528                 return ret;
529
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;
533         } else {
534                 virtio_video_format_fill_default_info(&info, &stream->out_info);
535                 queue = VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT;
536         }
537
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;
542
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;
548         }
549
550         virtio_video_cmd_set_params(vvd, stream, &info, queue);
551         virtio_video_stream_get_params(vvd, stream);
552
553         if (V4L2_TYPE_IS_OUTPUT(f->type))
554                 p_info = &stream->in_info;
555         else
556                 p_info = &stream->out_info;
557
558         virtio_video_format_from_info(p_info, pix_mp);
559
560         return 0;
561 }
562
563 int virtio_video_g_selection(struct file *file, void *fh,
564                          struct v4l2_selection *sel)
565 {
566         struct video_format_info *info;
567         struct virtio_video_stream *stream = file2stream(file);
568         struct virtio_video_device *vvd = video_drvdata(file);
569
570         switch (vvd->type) {
571         case VIRTIO_VIDEO_DEVICE_ENCODER:
572                 if (!V4L2_TYPE_IS_OUTPUT(sel->type))
573                         return -EINVAL;
574                 info = &stream->in_info;
575                 break;
576         case VIRTIO_VIDEO_DEVICE_DECODER:
577         case VIRTIO_VIDEO_DEVICE_CAMERA:
578                 if (V4L2_TYPE_IS_OUTPUT(sel->type))
579                         return -EINVAL;
580                 info = &stream->out_info;
581                 break;
582         default:
583                 v4l2_err(&vvd->v4l2_dev, "unsupported device type\n");
584                 return -EINVAL;
585         }
586
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;
594                 break;
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;
602                 break;
603         default:
604                 v4l2_dbg(1, vvd->debug, &vvd->v4l2_dev,
605                          "unsupported/invalid selection target: %d\n",
606                          sel->target);
607                 return -EINVAL;
608         }
609
610         return 0;
611 }
612
613 int virtio_video_try_fmt(struct virtio_video_stream *stream,
614                          struct v4l2_format *f)
615 {
616         int i, idx = 0;
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;
620         bool found = false;
621         struct video_format_frame *frm;
622         struct virtio_video_format_frame *frame;
623
624         if (virtio_video_state(stream) == STREAM_STATE_ERROR)
625                 return -EIO;
626
627         if (V4L2_TYPE_IS_OUTPUT(f->type))
628                 fmt = virtio_video_find_video_format(&vvd->input_fmt_list,
629                                                      pix_mp->pixelformat);
630         else
631                 fmt = virtio_video_find_video_format(&vvd->output_fmt_list,
632                                                      pix_mp->pixelformat);
633
634         if (!fmt) {
635                 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
636                         virtio_video_format_from_info(&stream->out_info,
637                                                       pix_mp);
638                 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
639                         virtio_video_format_from_info(&stream->in_info,
640                                                       pix_mp);
641                 else
642                         return -EINVAL;
643                 return 0;
644         }
645
646         for (i = 0; i < fmt->desc.num_frames && !found; i++) {
647                 frm = &fmt->frames[i];
648                 frame = &frm->frame;
649                 if (!within_range(frame->width.min, pix_mp->width,
650                                   frame->width.max))
651                         continue;
652
653                 if (!within_range(frame->height.min, pix_mp->height,
654                                   frame->height.max))
655                         continue;
656                 idx = i;
657                 /*
658                  * Try to find a more suitable frame size. Go with the current
659                  * one otherwise.
660                  */
661                 if (needs_alignment(pix_mp->width, frame->width.step))
662                         continue;
663
664                 if (needs_alignment(pix_mp->height, frame->height.step))
665                         continue;
666
667                 stream->current_frame = frm;
668                 found = true;
669         }
670
671         if (!found) {
672                 frm = &fmt->frames[idx];
673                 if (!frm)
674                         return -EINVAL;
675
676                 frame = &frm->frame;
677                 pix_mp->width = clamp(pix_mp->width, frame->width.min,
678                                       frame->width.max);
679                 if (frame->width.step != 0)
680                         pix_mp->width = ALIGN(pix_mp->width, frame->width.step);
681
682                 pix_mp->height = clamp(pix_mp->height, frame->height.min,
683                                        frame->height.max);
684                 if (frame->height.step != 0)
685                         pix_mp->height = ALIGN(pix_mp->height,
686                                                frame->height.step);
687                 stream->current_frame = frm;
688         }
689
690         return 0;
691 }
692
693 static int virtio_video_queue_free(struct virtio_video_device *vvd,
694                                    struct virtio_video_stream *stream,
695                                    enum v4l2_buf_type type)
696 {
697         int ret;
698         enum virtio_video_queue_type queue_type = to_virtio_queue_type(type);
699
700         ret = virtio_video_cmd_queue_detach_resources(vvd, stream, queue_type);
701         if (ret) {
702                 v4l2_warn(&vvd->v4l2_dev,
703                           "failed to destroy resources\n");
704                 return ret;
705         }
706
707         return 0;
708 }
709
710 int virtio_video_reqbufs(struct file *file, void *priv,
711                          struct v4l2_requestbuffers *rb)
712 {
713         int ret;
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;
719
720         if (vvd->is_m2m_dev)
721                 vq = v4l2_m2m_get_vq(m2m_ctx, rb->type);
722         else
723                 vq = vdev->queue;
724
725         if (rb->count == 0) {
726                 ret = virtio_video_queue_free(vvd, stream, vq->type);
727                 if (ret < 0)
728                         return ret;
729         }
730
731         if (vvd->is_m2m_dev)
732                 return v4l2_m2m_reqbufs(file, m2m_ctx, rb);
733         else
734                 return vb2_ioctl_reqbufs(file, priv, rb);
735 }
736
737 int virtio_video_subscribe_event(struct v4l2_fh *fh,
738                                  const struct v4l2_event_subscription *sub)
739 {
740         switch (sub->type) {
741         case V4L2_EVENT_SOURCE_CHANGE:
742                 return v4l2_src_change_event_subscribe(fh, sub);
743         default:
744                 return v4l2_ctrl_subscribe_event(fh, sub);
745         }
746 }
747
748 void virtio_video_queue_eos_event(struct virtio_video_stream *stream)
749 {
750         static const struct v4l2_event eos_event = {
751                 .type = V4L2_EVENT_EOS
752         };
753
754         v4l2_event_queue_fh(&stream->fh, &eos_event);
755 }
756
757 void virtio_video_queue_res_chg_event(struct virtio_video_stream *stream)
758 {
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,
763         };
764
765         v4l2_event_queue_fh(&stream->fh, &ev_src_ch);
766 }
767
768 void virtio_video_handle_error(struct virtio_video_stream *stream)
769 {
770         struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
771
772         if (vvd->is_m2m_dev)
773                 virtio_video_queue_release_buffers
774                         (stream, VIRTIO_VIDEO_QUEUE_TYPE_INPUT);
775
776         virtio_video_queue_release_buffers
777                 (stream, VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT);
778 }
779
780 int virtio_video_queue_release_buffers(struct virtio_video_stream *stream,
781                                        enum virtio_video_queue_type queue_type)
782 {
783         int ret;
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;
787
788         ret = virtio_video_cmd_queue_clear(vvd, stream, queue_type);
789         if (ret) {
790                 v4l2_err(&vvd->v4l2_dev, "failed to clear queue\n");
791                 return ret;
792         }
793
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);
798                 }
799                 return 0;
800         }
801
802         for (;;) {
803                 if (queue_type == VIRTIO_VIDEO_QUEUE_TYPE_INPUT)
804                         v4l2_vb = v4l2_m2m_src_buf_remove(stream->fh.m2m_ctx);
805                 else
806                         v4l2_vb = v4l2_m2m_dst_buf_remove(stream->fh.m2m_ctx);
807                 if (!v4l2_vb)
808                         break;
809                 v4l2_m2m_buf_done(v4l2_vb, VB2_BUF_STATE_ERROR);
810         }
811
812         return 0;
813 }
814
815 void virtio_video_buf_done(struct virtio_video_buffer *virtio_vb,
816                            uint32_t flags, uint64_t timestamp,
817                            uint32_t data_sizes[])
818 {
819         int i;
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;
827
828         virtio_vb->queued = false;
829
830         if (flags & VIRTIO_VIDEO_DEQUEUE_FLAG_ERR)
831                 done_state = VB2_BUF_STATE_ERROR;
832
833         if (flags & VIRTIO_VIDEO_DEQUEUE_FLAG_KEY_FRAME)
834                 v4l2_vb->flags |= V4L2_BUF_FLAG_KEYFRAME;
835
836         if (flags & VIRTIO_VIDEO_DEQUEUE_FLAG_BFRAME)
837                 v4l2_vb->flags |= V4L2_BUF_FLAG_BFRAME;
838
839         if (flags & VIRTIO_VIDEO_DEQUEUE_FLAG_PFRAME)
840                 v4l2_vb->flags |= V4L2_BUF_FLAG_PFRAME;
841
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);
846         }
847
848         if ((flags & VIRTIO_VIDEO_DEQUEUE_FLAG_ERR) ||
849             (flags & VIRTIO_VIDEO_DEQUEUE_FLAG_EOS)) {
850                 vb->planes[0].bytesused = 0;
851
852                 if (!vvd->is_m2m_dev)
853                         virtio_video_pending_buf_list_del(vvd, virtio_vb);
854
855                 v4l2_m2m_buf_done(v4l2_vb, done_state);
856                 return;
857         }
858
859         if (!V4L2_TYPE_IS_OUTPUT(vb2_queue->type)) {
860                 switch (vvd->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]);
865                         break;
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;
872                         break;
873                 }
874
875                 vb->timestamp = timestamp;
876         }
877
878         if (!vvd->is_m2m_dev)
879                 virtio_video_pending_buf_list_del(vvd, virtio_vb);
880
881         v4l2_m2m_buf_done(v4l2_vb, done_state);
882 }
883
884 static int virtio_video_set_device_busy(struct virtio_video_device *vvd)
885 {
886         struct video_device *vd = &vvd->video_dev;
887         int ret = 0;
888
889         /* Multiple open is allowed for m2m device */
890         if (vvd->is_m2m_dev)
891                 return 0;
892
893         mutex_lock(vd->lock);
894
895         if (vvd->device_busy)
896                 ret = -EBUSY;
897         else
898                 vvd->device_busy = true;
899
900         mutex_unlock(vd->lock);
901
902         return ret;
903 }
904
905 static void virtio_video_clear_device_busy(struct virtio_video_device *vvd,
906                                            struct mutex *lock)
907 {
908         /* Nothing to do for m2m device */
909         if (vvd->is_m2m_dev)
910                 return;
911
912         if (lock)
913                 mutex_lock(lock);
914
915         vvd->device_busy = false;
916
917         if (lock)
918                 mutex_unlock(lock);
919 }
920
921 static int virtio_video_device_open(struct file *file)
922 {
923         int ret;
924         uint32_t stream_id;
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);
931
932         ret = virtio_video_set_device_busy(vvd);
933         if (ret) {
934                 v4l2_err(&vvd->v4l2_dev, "device already in use.\n");
935                 return ret;
936         }
937
938         default_fmt = list_first_entry_or_null(vvd->ops->get_fmt_list(vvd),
939                                                struct video_format,
940                                                formats_list_entry);
941         if (!default_fmt) {
942                 v4l2_err(&vvd->v4l2_dev, "device failed to start\n");
943                 ret = -EIO;
944                 goto err;
945         }
946
947         stream = kzalloc(sizeof(*stream), GFP_KERNEL);
948         if (!stream) {
949                 ret = -ENOMEM;
950                 goto err;
951         }
952
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);
957         if (ret) {
958                 v4l2_err(&vvd->v4l2_dev, "failed to create stream\n");
959                 goto err_stream_create;
960         }
961
962         stream->video_dev = video_dev;
963         stream->stream_id = stream_id;
964
965         virtio_video_state_reset(stream);
966
967         ret = virtio_video_stream_get_params(vvd, stream);
968         if (ret)
969                 goto err_stream_create;
970
971         if (format >= VIRTIO_VIDEO_FORMAT_CODED_MIN &&
972             format <= VIRTIO_VIDEO_FORMAT_CODED_MAX) {
973                 ret = virtio_video_stream_get_controls(vvd, stream);
974                 if (ret)
975                         goto err_stream_create;
976         }
977
978         mutex_init(&stream->vq_mutex);
979         v4l2_fh_init(&stream->fh, video_dev);
980         stream->fh.ctrl_handler = &stream->ctrl_handler;
981
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);
987                         goto err_init_ctx;
988                 }
989
990                 v4l2_m2m_set_src_buffered(stream->fh.m2m_ctx, true);
991                 v4l2_m2m_set_dst_buffered(stream->fh.m2m_ctx, true);
992         } else {
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;
996         }
997
998         file->private_data = &stream->fh;
999         v4l2_fh_add(&stream->fh);
1000
1001         if (vvd->ops->init_ctrls) {
1002                 ret = vvd->ops->init_ctrls(stream);
1003                 if (ret) {
1004                         v4l2_err(&vvd->v4l2_dev, "failed to init controls\n");
1005                         goto err_init_ctrls;
1006                 }
1007         }
1008         return 0;
1009
1010 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);
1016 err_init_ctx:
1017         v4l2_fh_exit(&stream->fh);
1018 err_stream_create:
1019         virtio_video_stream_id_put(vvd, stream_id);
1020         kfree(stream);
1021 err:
1022         virtio_video_clear_device_busy(vvd, video_dev->lock);
1023         return ret;
1024 }
1025
1026 static int virtio_video_device_release(struct file *file)
1027 {
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);
1031
1032         mutex_lock(video_dev->lock);
1033
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;
1040         }
1041
1042         v4l2_fh_exit(&stream->fh);
1043
1044         virtio_video_cmd_stream_destroy(vvd, stream->stream_id);
1045         virtio_video_stream_id_put(vvd, stream->stream_id);
1046
1047         kfree(stream);
1048
1049         /* Mutex already locked here, passing NULL */
1050         virtio_video_clear_device_busy(vvd, NULL);
1051
1052         mutex_unlock(video_dev->lock);
1053
1054         return 0;
1055 }
1056
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,
1064 };
1065
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,
1073 };
1074
1075 static void virtio_video_device_run(void *priv)
1076 {
1077
1078 }
1079
1080 static void virtio_video_device_job_abort(void *priv)
1081 {
1082         struct virtio_video_stream *stream = priv;
1083         struct virtio_video_device *vvd = to_virtio_vd(stream->video_dev);
1084
1085         v4l2_m2m_job_finish(vvd->m2m_dev, stream->fh.m2m_ctx);
1086 }
1087
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,
1091 };
1092
1093 static int virtio_video_device_register(struct virtio_video_device *vvd)
1094 {
1095         int ret;
1096         struct video_device *vd;
1097
1098         vd = &vvd->video_dev;
1099
1100 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5,7,0)
1101         ret = video_register_device(vd, VFL_TYPE_VIDEO, vvd->vid_dev_nr);
1102 #else
1103         ret = video_register_device(vd, VFL_TYPE_GRABBER, vvd->vid_dev_nr);
1104 #endif
1105         if (ret) {
1106                 v4l2_err(&vvd->v4l2_dev, "failed to register video device\n");
1107                 return ret;
1108         }
1109
1110         v4l2_info(&vvd->v4l2_dev, "Device '%s' registered as /dev/video%d\n",
1111                   vd->name, vd->num);
1112
1113         return 0;
1114 }
1115
1116 static void virtio_video_device_unregister(struct virtio_video_device *vvd)
1117 {
1118         video_unregister_device(&vvd->video_dev);
1119 }
1120
1121 static int
1122 virtio_video_query_capability(struct virtio_video_device *vvd,
1123                               void *resp_buf,
1124                               enum virtio_video_queue_type queue_type)
1125 {
1126         int ret;
1127         int resp_size = vvd->max_caps_len;
1128
1129         ret = virtio_video_cmd_query_capability(vvd, resp_buf, resp_size,
1130                                                 queue_type);
1131         if (ret)
1132                 v4l2_err(&vvd->v4l2_dev, "failed to query capability\n");
1133
1134         return ret;
1135 }
1136
1137 int virtio_video_device_init(struct virtio_video_device *vvd)
1138 {
1139         int ret;
1140         int vfl_dir;
1141         u32 dev_caps;
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;
1146
1147         output_resp_buf = kzalloc(vvd->max_caps_len, GFP_KERNEL);
1148         if (!output_resp_buf)
1149                 return -ENOMEM;
1150
1151         ret = virtio_video_query_capability(vvd, output_resp_buf,
1152                                             VIRTIO_VIDEO_QUEUE_TYPE_OUTPUT);
1153         if (ret) {
1154                 v4l2_err(&vvd->v4l2_dev, "failed to get output caps\n");
1155                 goto err_output_cap;
1156         }
1157
1158         if (vvd->is_m2m_dev) {
1159                 input_resp_buf = kzalloc(vvd->max_caps_len, GFP_KERNEL);
1160                 if (!input_resp_buf) {
1161                         ret = -ENOMEM;
1162                         goto err_input_buf;
1163                 }
1164
1165                 ret = virtio_video_query_capability(vvd, input_resp_buf,
1166                                                 VIRTIO_VIDEO_QUEUE_TYPE_INPUT);
1167                 if (ret) {
1168                         v4l2_err(&vvd->v4l2_dev, "failed to get input caps\n");
1169                         goto err_input_cap;
1170                 }
1171
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);
1176                         goto err_m2m_dev;
1177                 }
1178                 vfl_dir = VFL_DIR_M2M;
1179                 fops = &virtio_video_device_m2m_fops;
1180                 dev_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
1181         } else {
1182                 input_resp_buf = NULL;
1183                 m2m_dev = 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;
1189                 else
1190                         dev_caps |= V4L2_CAP_VIDEO_CAPTURE;
1191         }
1192
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;
1200         vd->fops = fops;
1201         vd->device_caps = dev_caps;
1202         vd->release = video_device_release_empty;
1203
1204         /* Use the selection API instead */
1205         v4l2_disable_ioctl(vd, VIDIOC_CROPCAP);
1206         v4l2_disable_ioctl(vd, VIDIOC_G_CROP);
1207
1208         video_set_drvdata(vd, vvd);
1209
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);
1214
1215         vvd->num_output_fmts = 0;
1216         vvd->num_input_fmts = 0;
1217
1218         switch (vvd->type) {
1219         case VIRTIO_VIDEO_DEVICE_CAMERA:
1220                 virtio_video_cam_init(vvd);
1221                 break;
1222         case VIRTIO_VIDEO_DEVICE_ENCODER:
1223                 virtio_video_enc_init(vvd);
1224                 break;
1225         case VIRTIO_VIDEO_DEVICE_DECODER:
1226         default:
1227                 virtio_video_dec_init(vvd);
1228                 break;
1229         }
1230
1231         ret = virtio_video_parse_virtio_capabilities(vvd, input_resp_buf,
1232                                                      output_resp_buf);
1233         if (ret) {
1234                 v4l2_err(&vvd->v4l2_dev, "failed to parse a function\n");
1235                 goto parse_cap_err;
1236         }
1237
1238         ret = virtio_video_parse_virtio_control(vvd);
1239         if (ret) {
1240                 v4l2_err(&vvd->v4l2_dev, "failed to query controls\n");
1241                 goto parse_ctrl_err;
1242         }
1243
1244         ret = virtio_video_device_register(vvd);
1245         if (ret) {
1246                 v4l2_err(&vvd->v4l2_dev,
1247                          "failed to init virtio video device\n");
1248                 goto register_err;
1249         }
1250
1251         goto out_cleanup;
1252
1253 register_err:
1254         virtio_video_clean_control(vvd);
1255 parse_ctrl_err:
1256         virtio_video_clean_capability(vvd);
1257 parse_cap_err:
1258         if (vvd->is_m2m_dev)
1259                 v4l2_m2m_release(vvd->m2m_dev);
1260 err_m2m_dev:
1261 err_input_cap:
1262 out_cleanup:
1263         if (vvd->is_m2m_dev)
1264                 kfree(input_resp_buf);
1265 err_input_buf:
1266 err_output_cap:
1267         kfree(output_resp_buf);
1268
1269         return ret;
1270 }
1271
1272 void virtio_video_device_deinit(struct virtio_video_device *vvd)
1273 {
1274         vvd->commandq.ready = false;
1275         vvd->eventq.ready = false;
1276
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);
1282 }