Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / drivers / staging / media / hantro / hantro_v4l2.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Hantro VPU codec driver
4  *
5  * Copyright (C) 2018 Collabora, Ltd.
6  * Copyright (C) 2018 Rockchip Electronics Co., Ltd.
7  *      Alpha Lin <Alpha.Lin@rock-chips.com>
8  *      Jeffy Chen <jeffy.chen@rock-chips.com>
9  *
10  * Copyright 2018 Google LLC.
11  *      Tomasz Figa <tfiga@chromium.org>
12  *
13  * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
14  * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
15  */
16
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/videodev2.h>
22 #include <linux/workqueue.h>
23 #include <media/v4l2-ctrls.h>
24 #include <media/v4l2-event.h>
25 #include <media/v4l2-mem2mem.h>
26 #include <media/videobuf2-core.h>
27 #include <media/videobuf2-dma-sg.h>
28
29 #include "hantro.h"
30 #include "hantro_hw.h"
31 #include "hantro_v4l2.h"
32
33 static const struct hantro_fmt *
34 hantro_get_formats(const struct hantro_ctx *ctx, unsigned int *num_fmts)
35 {
36         const struct hantro_fmt *formats;
37
38         if (hantro_is_encoder_ctx(ctx)) {
39                 formats = ctx->dev->variant->enc_fmts;
40                 *num_fmts = ctx->dev->variant->num_enc_fmts;
41         } else {
42                 formats = ctx->dev->variant->dec_fmts;
43                 *num_fmts = ctx->dev->variant->num_dec_fmts;
44         }
45
46         return formats;
47 }
48
49 static const struct hantro_fmt *
50 hantro_find_format(const struct hantro_fmt *formats, unsigned int num_fmts,
51                    u32 fourcc)
52 {
53         unsigned int i;
54
55         for (i = 0; i < num_fmts; i++)
56                 if (formats[i].fourcc == fourcc)
57                         return &formats[i];
58         return NULL;
59 }
60
61 static const struct hantro_fmt *
62 hantro_get_default_fmt(const struct hantro_fmt *formats, unsigned int num_fmts,
63                        bool bitstream)
64 {
65         unsigned int i;
66
67         for (i = 0; i < num_fmts; i++) {
68                 if (bitstream == (formats[i].codec_mode !=
69                                   HANTRO_MODE_NONE))
70                         return &formats[i];
71         }
72         return NULL;
73 }
74
75 static int vidioc_querycap(struct file *file, void *priv,
76                            struct v4l2_capability *cap)
77 {
78         struct hantro_dev *vpu = video_drvdata(file);
79         struct video_device *vdev = video_devdata(file);
80
81         strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver));
82         strscpy(cap->card, vdev->name, sizeof(cap->card));
83         snprintf(cap->bus_info, sizeof(cap->bus_info), "platform: %s",
84                  vpu->dev->driver->name);
85         return 0;
86 }
87
88 static int vidioc_enum_framesizes(struct file *file, void *priv,
89                                   struct v4l2_frmsizeenum *fsize)
90 {
91         struct hantro_ctx *ctx = fh_to_ctx(priv);
92         const struct hantro_fmt *formats, *fmt;
93         unsigned int num_fmts;
94
95         if (fsize->index != 0) {
96                 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
97                           fsize->index);
98                 return -EINVAL;
99         }
100
101         formats = hantro_get_formats(ctx, &num_fmts);
102         fmt = hantro_find_format(formats, num_fmts, fsize->pixel_format);
103         if (!fmt) {
104                 vpu_debug(0, "unsupported bitstream format (%08x)\n",
105                           fsize->pixel_format);
106                 return -EINVAL;
107         }
108
109         /* This only makes sense for coded formats */
110         if (fmt->codec_mode == HANTRO_MODE_NONE)
111                 return -EINVAL;
112
113         fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
114         fsize->stepwise = fmt->frmsize;
115
116         return 0;
117 }
118
119 static int vidioc_enum_fmt(struct file *file, void *priv,
120                            struct v4l2_fmtdesc *f, bool capture)
121
122 {
123         struct hantro_ctx *ctx = fh_to_ctx(priv);
124         const struct hantro_fmt *fmt, *formats;
125         unsigned int num_fmts, i, j = 0;
126         bool skip_mode_none;
127
128         /*
129          * When dealing with an encoder:
130          *  - on the capture side we want to filter out all MODE_NONE formats.
131          *  - on the output side we want to filter out all formats that are
132          *    not MODE_NONE.
133          * When dealing with a decoder:
134          *  - on the capture side we want to filter out all formats that are
135          *    not MODE_NONE.
136          *  - on the output side we want to filter out all MODE_NONE formats.
137          */
138         skip_mode_none = capture == hantro_is_encoder_ctx(ctx);
139
140         formats = hantro_get_formats(ctx, &num_fmts);
141         for (i = 0; i < num_fmts; i++) {
142                 bool mode_none = formats[i].codec_mode == HANTRO_MODE_NONE;
143
144                 if (skip_mode_none == mode_none)
145                         continue;
146                 if (j == f->index) {
147                         fmt = &formats[i];
148                         f->pixelformat = fmt->fourcc;
149                         return 0;
150                 }
151                 ++j;
152         }
153         return -EINVAL;
154 }
155
156 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
157                                    struct v4l2_fmtdesc *f)
158 {
159         return vidioc_enum_fmt(file, priv, f, true);
160 }
161
162 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
163                                    struct v4l2_fmtdesc *f)
164 {
165         return vidioc_enum_fmt(file, priv, f, false);
166 }
167
168 static int vidioc_g_fmt_out_mplane(struct file *file, void *priv,
169                                    struct v4l2_format *f)
170 {
171         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
172         struct hantro_ctx *ctx = fh_to_ctx(priv);
173
174         vpu_debug(4, "f->type = %d\n", f->type);
175
176         *pix_mp = ctx->src_fmt;
177
178         return 0;
179 }
180
181 static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv,
182                                    struct v4l2_format *f)
183 {
184         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
185         struct hantro_ctx *ctx = fh_to_ctx(priv);
186
187         vpu_debug(4, "f->type = %d\n", f->type);
188
189         *pix_mp = ctx->dst_fmt;
190
191         return 0;
192 }
193
194 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f,
195                           bool capture)
196 {
197         struct hantro_ctx *ctx = fh_to_ctx(priv);
198         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
199         const struct hantro_fmt *formats, *fmt, *vpu_fmt;
200         unsigned int num_fmts;
201         bool coded;
202
203         coded = capture == hantro_is_encoder_ctx(ctx);
204
205         vpu_debug(4, "trying format %c%c%c%c\n",
206                   (pix_mp->pixelformat & 0x7f),
207                   (pix_mp->pixelformat >> 8) & 0x7f,
208                   (pix_mp->pixelformat >> 16) & 0x7f,
209                   (pix_mp->pixelformat >> 24) & 0x7f);
210
211         formats = hantro_get_formats(ctx, &num_fmts);
212         fmt = hantro_find_format(formats, num_fmts, pix_mp->pixelformat);
213         if (!fmt) {
214                 fmt = hantro_get_default_fmt(formats, num_fmts, coded);
215                 f->fmt.pix_mp.pixelformat = fmt->fourcc;
216         }
217
218         if (coded) {
219                 pix_mp->num_planes = 1;
220                 vpu_fmt = fmt;
221         } else if (hantro_is_encoder_ctx(ctx)) {
222                 vpu_fmt = ctx->vpu_dst_fmt;
223         } else {
224                 vpu_fmt = ctx->vpu_src_fmt;
225                 /*
226                  * Width/height on the CAPTURE end of a decoder are ignored and
227                  * replaced by the OUTPUT ones.
228                  */
229                 pix_mp->width = ctx->src_fmt.width;
230                 pix_mp->height = ctx->src_fmt.height;
231         }
232
233         pix_mp->field = V4L2_FIELD_NONE;
234
235         v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
236                                        &vpu_fmt->frmsize);
237
238         if (!coded) {
239                 /* Fill remaining fields */
240                 v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
241                                     pix_mp->height);
242         } else if (!pix_mp->plane_fmt[0].sizeimage) {
243                 /*
244                  * For coded formats the application can specify
245                  * sizeimage. If the application passes a zero sizeimage,
246                  * let's default to the maximum frame size.
247                  */
248                 pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
249                         pix_mp->width * pix_mp->height * fmt->max_depth;
250         }
251
252         return 0;
253 }
254
255 static int vidioc_try_fmt_cap_mplane(struct file *file, void *priv,
256                                      struct v4l2_format *f)
257 {
258         return vidioc_try_fmt(file, priv, f, true);
259 }
260
261 static int vidioc_try_fmt_out_mplane(struct file *file, void *priv,
262                                      struct v4l2_format *f)
263 {
264         return vidioc_try_fmt(file, priv, f, false);
265 }
266
267 static void
268 hantro_reset_fmt(struct v4l2_pix_format_mplane *fmt,
269                  const struct hantro_fmt *vpu_fmt)
270 {
271         memset(fmt, 0, sizeof(*fmt));
272
273         fmt->pixelformat = vpu_fmt->fourcc;
274         fmt->field = V4L2_FIELD_NONE;
275         fmt->colorspace = V4L2_COLORSPACE_JPEG,
276         fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
277         fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
278         fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
279 }
280
281 static void
282 hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
283 {
284         const struct hantro_fmt *vpu_fmt, *formats;
285         struct v4l2_pix_format_mplane *fmt;
286         unsigned int num_fmts;
287
288         formats = hantro_get_formats(ctx, &num_fmts);
289         vpu_fmt = hantro_get_default_fmt(formats, num_fmts, true);
290
291         if (hantro_is_encoder_ctx(ctx)) {
292                 ctx->vpu_dst_fmt = vpu_fmt;
293                 fmt = &ctx->dst_fmt;
294         } else {
295                 ctx->vpu_src_fmt = vpu_fmt;
296                 fmt = &ctx->src_fmt;
297         }
298
299         hantro_reset_fmt(fmt, vpu_fmt);
300         fmt->num_planes = 1;
301         fmt->width = vpu_fmt->frmsize.min_width;
302         fmt->height = vpu_fmt->frmsize.min_height;
303         fmt->plane_fmt[0].sizeimage = vpu_fmt->header_size +
304                                 fmt->width * fmt->height * vpu_fmt->max_depth;
305 }
306
307 static void
308 hantro_reset_raw_fmt(struct hantro_ctx *ctx)
309 {
310         const struct hantro_fmt *raw_vpu_fmt, *formats;
311         struct v4l2_pix_format_mplane *raw_fmt, *encoded_fmt;
312         unsigned int num_fmts;
313
314         formats = hantro_get_formats(ctx, &num_fmts);
315         raw_vpu_fmt = hantro_get_default_fmt(formats, num_fmts, false);
316
317         if (hantro_is_encoder_ctx(ctx)) {
318                 ctx->vpu_src_fmt = raw_vpu_fmt;
319                 raw_fmt = &ctx->src_fmt;
320                 encoded_fmt = &ctx->dst_fmt;
321         } else {
322                 ctx->vpu_dst_fmt = raw_vpu_fmt;
323                 raw_fmt = &ctx->dst_fmt;
324                 encoded_fmt = &ctx->src_fmt;
325         }
326
327         hantro_reset_fmt(raw_fmt, raw_vpu_fmt);
328         v4l2_fill_pixfmt_mp(raw_fmt, raw_vpu_fmt->fourcc,
329                             encoded_fmt->width,
330                             encoded_fmt->height);
331 }
332
333 void hantro_reset_fmts(struct hantro_ctx *ctx)
334 {
335         hantro_reset_encoded_fmt(ctx);
336         hantro_reset_raw_fmt(ctx);
337 }
338
339 static void
340 hantro_update_requires_request(struct hantro_ctx *ctx, u32 fourcc)
341 {
342         switch (fourcc) {
343         case V4L2_PIX_FMT_JPEG:
344                 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = false;
345                 break;
346         case V4L2_PIX_FMT_MPEG2_SLICE:
347                 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true;
348                 break;
349         default:
350                 break;
351         }
352 }
353
354 static int
355 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
356 {
357         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
358         struct hantro_ctx *ctx = fh_to_ctx(priv);
359         const struct hantro_fmt *formats;
360         unsigned int num_fmts;
361         struct vb2_queue *vq;
362         int ret;
363
364         /* Change not allowed if queue is busy. */
365         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
366         if (vb2_is_busy(vq))
367                 return -EBUSY;
368
369         if (!hantro_is_encoder_ctx(ctx)) {
370                 struct vb2_queue *peer_vq;
371
372                 /*
373                  * Since format change on the OUTPUT queue will reset
374                  * the CAPTURE queue, we can't allow doing so
375                  * when the CAPTURE queue has buffers allocated.
376                  */
377                 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
378                                           V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
379                 if (vb2_is_busy(peer_vq))
380                         return -EBUSY;
381         }
382
383         ret = vidioc_try_fmt_out_mplane(file, priv, f);
384         if (ret)
385                 return ret;
386
387         formats = hantro_get_formats(ctx, &num_fmts);
388         ctx->vpu_src_fmt = hantro_find_format(formats, num_fmts,
389                                               pix_mp->pixelformat);
390         ctx->src_fmt = *pix_mp;
391
392         /*
393          * Current raw format might have become invalid with newly
394          * selected codec, so reset it to default just to be safe and
395          * keep internal driver state sane. User is mandated to set
396          * the raw format again after we return, so we don't need
397          * anything smarter.
398          * Note that hantro_reset_raw_fmt() also propagates size
399          * changes to the raw format.
400          */
401         if (!hantro_is_encoder_ctx(ctx))
402                 hantro_reset_raw_fmt(ctx);
403
404         /* Colorimetry information are always propagated. */
405         ctx->dst_fmt.colorspace = pix_mp->colorspace;
406         ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
407         ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
408         ctx->dst_fmt.quantization = pix_mp->quantization;
409
410         hantro_update_requires_request(ctx, pix_mp->pixelformat);
411
412         vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
413         vpu_debug(0, "fmt - w: %d, h: %d\n",
414                   pix_mp->width, pix_mp->height);
415         return 0;
416 }
417
418 static int vidioc_s_fmt_cap_mplane(struct file *file, void *priv,
419                                    struct v4l2_format *f)
420 {
421         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
422         struct hantro_ctx *ctx = fh_to_ctx(priv);
423         const struct hantro_fmt *formats;
424         struct vb2_queue *vq;
425         unsigned int num_fmts;
426         int ret;
427
428         /* Change not allowed if queue is busy. */
429         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
430         if (vb2_is_busy(vq))
431                 return -EBUSY;
432
433         if (hantro_is_encoder_ctx(ctx)) {
434                 struct vb2_queue *peer_vq;
435
436                 /*
437                  * Since format change on the CAPTURE queue will reset
438                  * the OUTPUT queue, we can't allow doing so
439                  * when the OUTPUT queue has buffers allocated.
440                  */
441                 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
442                                           V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
443                 if (vb2_is_busy(peer_vq) &&
444                     (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
445                      pix_mp->height != ctx->dst_fmt.height ||
446                      pix_mp->width != ctx->dst_fmt.width))
447                         return -EBUSY;
448         }
449
450         ret = vidioc_try_fmt_cap_mplane(file, priv, f);
451         if (ret)
452                 return ret;
453
454         formats = hantro_get_formats(ctx, &num_fmts);
455         ctx->vpu_dst_fmt = hantro_find_format(formats, num_fmts,
456                                               pix_mp->pixelformat);
457         ctx->dst_fmt = *pix_mp;
458
459         /*
460          * Current raw format might have become invalid with newly
461          * selected codec, so reset it to default just to be safe and
462          * keep internal driver state sane. User is mandated to set
463          * the raw format again after we return, so we don't need
464          * anything smarter.
465          * Note that hantro_reset_raw_fmt() also propagates size
466          * changes to the raw format.
467          */
468         if (hantro_is_encoder_ctx(ctx))
469                 hantro_reset_raw_fmt(ctx);
470
471         /* Colorimetry information are always propagated. */
472         ctx->src_fmt.colorspace = pix_mp->colorspace;
473         ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
474         ctx->src_fmt.xfer_func = pix_mp->xfer_func;
475         ctx->src_fmt.quantization = pix_mp->quantization;
476
477         vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
478         vpu_debug(0, "fmt - w: %d, h: %d\n",
479                   pix_mp->width, pix_mp->height);
480
481         hantro_update_requires_request(ctx, pix_mp->pixelformat);
482
483         return 0;
484 }
485
486 const struct v4l2_ioctl_ops hantro_ioctl_ops = {
487         .vidioc_querycap = vidioc_querycap,
488         .vidioc_enum_framesizes = vidioc_enum_framesizes,
489
490         .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
491         .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
492         .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
493         .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
494         .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
495         .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
496         .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
497         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
498
499         .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
500         .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
501         .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
502         .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
503         .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
504         .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
505         .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
506
507         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
508         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
509
510         .vidioc_streamon = v4l2_m2m_ioctl_streamon,
511         .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
512 };
513
514 static int
515 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
516                    unsigned int *num_planes, unsigned int sizes[],
517                    struct device *alloc_devs[])
518 {
519         struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
520         struct v4l2_pix_format_mplane *pixfmt;
521         int i;
522
523         switch (vq->type) {
524         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
525                 pixfmt = &ctx->dst_fmt;
526                 break;
527         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
528                 pixfmt = &ctx->src_fmt;
529                 break;
530         default:
531                 vpu_err("invalid queue type: %d\n", vq->type);
532                 return -EINVAL;
533         }
534
535         if (*num_planes) {
536                 if (*num_planes != pixfmt->num_planes)
537                         return -EINVAL;
538                 for (i = 0; i < pixfmt->num_planes; ++i)
539                         if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
540                                 return -EINVAL;
541                 return 0;
542         }
543
544         *num_planes = pixfmt->num_planes;
545         for (i = 0; i < pixfmt->num_planes; ++i)
546                 sizes[i] = pixfmt->plane_fmt[i].sizeimage;
547         return 0;
548 }
549
550 static int
551 hantro_buf_plane_check(struct vb2_buffer *vb, const struct hantro_fmt *vpu_fmt,
552                        struct v4l2_pix_format_mplane *pixfmt)
553 {
554         unsigned int sz;
555         int i;
556
557         for (i = 0; i < pixfmt->num_planes; ++i) {
558                 sz = pixfmt->plane_fmt[i].sizeimage;
559                 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
560                           i, vb2_plane_size(vb, i), sz);
561                 if (vb2_plane_size(vb, i) < sz) {
562                         vpu_err("plane %d is too small for output\n", i);
563                         return -EINVAL;
564                 }
565         }
566         return 0;
567 }
568
569 static int hantro_buf_prepare(struct vb2_buffer *vb)
570 {
571         struct vb2_queue *vq = vb->vb2_queue;
572         struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
573
574         if (V4L2_TYPE_IS_OUTPUT(vq->type))
575                 return hantro_buf_plane_check(vb, ctx->vpu_src_fmt,
576                                                   &ctx->src_fmt);
577
578         return hantro_buf_plane_check(vb, ctx->vpu_dst_fmt, &ctx->dst_fmt);
579 }
580
581 static void hantro_buf_queue(struct vb2_buffer *vb)
582 {
583         struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
584         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
585
586         v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
587 }
588
589 static bool hantro_vq_is_coded(struct vb2_queue *q)
590 {
591         struct hantro_ctx *ctx = vb2_get_drv_priv(q);
592
593         return hantro_is_encoder_ctx(ctx) != V4L2_TYPE_IS_OUTPUT(q->type);
594 }
595
596 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
597 {
598         struct hantro_ctx *ctx = vb2_get_drv_priv(q);
599         int ret = 0;
600
601         if (V4L2_TYPE_IS_OUTPUT(q->type))
602                 ctx->sequence_out = 0;
603         else
604                 ctx->sequence_cap = 0;
605
606         if (hantro_vq_is_coded(q)) {
607                 enum hantro_codec_mode codec_mode;
608
609                 if (V4L2_TYPE_IS_OUTPUT(q->type))
610                         codec_mode = ctx->vpu_src_fmt->codec_mode;
611                 else
612                         codec_mode = ctx->vpu_dst_fmt->codec_mode;
613
614                 vpu_debug(4, "Codec mode = %d\n", codec_mode);
615                 ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
616                 if (ctx->codec_ops->init)
617                         ret = ctx->codec_ops->init(ctx);
618         }
619
620         return ret;
621 }
622
623 static void
624 hantro_return_bufs(struct vb2_queue *q,
625                    struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
626 {
627         struct hantro_ctx *ctx = vb2_get_drv_priv(q);
628
629         for (;;) {
630                 struct vb2_v4l2_buffer *vbuf;
631
632                 vbuf = buf_remove(ctx->fh.m2m_ctx);
633                 if (!vbuf)
634                         break;
635                 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
636                                            &ctx->ctrl_handler);
637                 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
638         }
639 }
640
641 static void hantro_stop_streaming(struct vb2_queue *q)
642 {
643         struct hantro_ctx *ctx = vb2_get_drv_priv(q);
644
645         if (hantro_vq_is_coded(q)) {
646                 if (ctx->codec_ops && ctx->codec_ops->exit)
647                         ctx->codec_ops->exit(ctx);
648         }
649
650         /*
651          * The mem2mem framework calls v4l2_m2m_cancel_job before
652          * .stop_streaming, so there isn't any job running and
653          * it is safe to return all the buffers.
654          */
655         if (V4L2_TYPE_IS_OUTPUT(q->type))
656                 hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
657         else
658                 hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
659 }
660
661 static void hantro_buf_request_complete(struct vb2_buffer *vb)
662 {
663         struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
664
665         v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
666 }
667
668 static int hantro_buf_out_validate(struct vb2_buffer *vb)
669 {
670         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
671
672         vbuf->field = V4L2_FIELD_NONE;
673         return 0;
674 }
675
676 const struct vb2_ops hantro_queue_ops = {
677         .queue_setup = hantro_queue_setup,
678         .buf_prepare = hantro_buf_prepare,
679         .buf_queue = hantro_buf_queue,
680         .buf_out_validate = hantro_buf_out_validate,
681         .buf_request_complete = hantro_buf_request_complete,
682         .start_streaming = hantro_start_streaming,
683         .stop_streaming = hantro_stop_streaming,
684         .wait_prepare = vb2_ops_wait_prepare,
685         .wait_finish = vb2_ops_wait_finish,
686 };