]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/media/hantro/hantro_v4l2.c
Merge tag 'v5.3-rc4' into patchwork
[linux.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         case V4L2_PIX_FMT_VP8_FRAME:
348                 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true;
349                 break;
350         default:
351                 break;
352         }
353 }
354
355 static int
356 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
357 {
358         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
359         struct hantro_ctx *ctx = fh_to_ctx(priv);
360         const struct hantro_fmt *formats;
361         unsigned int num_fmts;
362         struct vb2_queue *vq;
363         int ret;
364
365         /* Change not allowed if queue is busy. */
366         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
367         if (vb2_is_busy(vq))
368                 return -EBUSY;
369
370         if (!hantro_is_encoder_ctx(ctx)) {
371                 struct vb2_queue *peer_vq;
372
373                 /*
374                  * Since format change on the OUTPUT queue will reset
375                  * the CAPTURE queue, we can't allow doing so
376                  * when the CAPTURE queue has buffers allocated.
377                  */
378                 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
379                                           V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
380                 if (vb2_is_busy(peer_vq))
381                         return -EBUSY;
382         }
383
384         ret = vidioc_try_fmt_out_mplane(file, priv, f);
385         if (ret)
386                 return ret;
387
388         formats = hantro_get_formats(ctx, &num_fmts);
389         ctx->vpu_src_fmt = hantro_find_format(formats, num_fmts,
390                                               pix_mp->pixelformat);
391         ctx->src_fmt = *pix_mp;
392
393         /*
394          * Current raw format might have become invalid with newly
395          * selected codec, so reset it to default just to be safe and
396          * keep internal driver state sane. User is mandated to set
397          * the raw format again after we return, so we don't need
398          * anything smarter.
399          * Note that hantro_reset_raw_fmt() also propagates size
400          * changes to the raw format.
401          */
402         if (!hantro_is_encoder_ctx(ctx))
403                 hantro_reset_raw_fmt(ctx);
404
405         /* Colorimetry information are always propagated. */
406         ctx->dst_fmt.colorspace = pix_mp->colorspace;
407         ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
408         ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
409         ctx->dst_fmt.quantization = pix_mp->quantization;
410
411         hantro_update_requires_request(ctx, pix_mp->pixelformat);
412
413         vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
414         vpu_debug(0, "fmt - w: %d, h: %d\n",
415                   pix_mp->width, pix_mp->height);
416         return 0;
417 }
418
419 static int vidioc_s_fmt_cap_mplane(struct file *file, void *priv,
420                                    struct v4l2_format *f)
421 {
422         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
423         struct hantro_ctx *ctx = fh_to_ctx(priv);
424         const struct hantro_fmt *formats;
425         struct vb2_queue *vq;
426         unsigned int num_fmts;
427         int ret;
428
429         /* Change not allowed if queue is busy. */
430         vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
431         if (vb2_is_busy(vq))
432                 return -EBUSY;
433
434         if (hantro_is_encoder_ctx(ctx)) {
435                 struct vb2_queue *peer_vq;
436
437                 /*
438                  * Since format change on the CAPTURE queue will reset
439                  * the OUTPUT queue, we can't allow doing so
440                  * when the OUTPUT queue has buffers allocated.
441                  */
442                 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
443                                           V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
444                 if (vb2_is_busy(peer_vq) &&
445                     (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
446                      pix_mp->height != ctx->dst_fmt.height ||
447                      pix_mp->width != ctx->dst_fmt.width))
448                         return -EBUSY;
449         }
450
451         ret = vidioc_try_fmt_cap_mplane(file, priv, f);
452         if (ret)
453                 return ret;
454
455         formats = hantro_get_formats(ctx, &num_fmts);
456         ctx->vpu_dst_fmt = hantro_find_format(formats, num_fmts,
457                                               pix_mp->pixelformat);
458         ctx->dst_fmt = *pix_mp;
459
460         /*
461          * Current raw format might have become invalid with newly
462          * selected codec, so reset it to default just to be safe and
463          * keep internal driver state sane. User is mandated to set
464          * the raw format again after we return, so we don't need
465          * anything smarter.
466          * Note that hantro_reset_raw_fmt() also propagates size
467          * changes to the raw format.
468          */
469         if (hantro_is_encoder_ctx(ctx))
470                 hantro_reset_raw_fmt(ctx);
471
472         /* Colorimetry information are always propagated. */
473         ctx->src_fmt.colorspace = pix_mp->colorspace;
474         ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
475         ctx->src_fmt.xfer_func = pix_mp->xfer_func;
476         ctx->src_fmt.quantization = pix_mp->quantization;
477
478         vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
479         vpu_debug(0, "fmt - w: %d, h: %d\n",
480                   pix_mp->width, pix_mp->height);
481
482         hantro_update_requires_request(ctx, pix_mp->pixelformat);
483
484         return 0;
485 }
486
487 const struct v4l2_ioctl_ops hantro_ioctl_ops = {
488         .vidioc_querycap = vidioc_querycap,
489         .vidioc_enum_framesizes = vidioc_enum_framesizes,
490
491         .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
492         .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
493         .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
494         .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
495         .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
496         .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
497         .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
498         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
499
500         .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
501         .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
502         .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
503         .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
504         .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
505         .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
506         .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
507
508         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
509         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
510
511         .vidioc_streamon = v4l2_m2m_ioctl_streamon,
512         .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
513 };
514
515 static int
516 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers,
517                    unsigned int *num_planes, unsigned int sizes[],
518                    struct device *alloc_devs[])
519 {
520         struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
521         struct v4l2_pix_format_mplane *pixfmt;
522         int i;
523
524         switch (vq->type) {
525         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
526                 pixfmt = &ctx->dst_fmt;
527                 break;
528         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
529                 pixfmt = &ctx->src_fmt;
530                 break;
531         default:
532                 vpu_err("invalid queue type: %d\n", vq->type);
533                 return -EINVAL;
534         }
535
536         if (*num_planes) {
537                 if (*num_planes != pixfmt->num_planes)
538                         return -EINVAL;
539                 for (i = 0; i < pixfmt->num_planes; ++i)
540                         if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
541                                 return -EINVAL;
542                 return 0;
543         }
544
545         *num_planes = pixfmt->num_planes;
546         for (i = 0; i < pixfmt->num_planes; ++i)
547                 sizes[i] = pixfmt->plane_fmt[i].sizeimage;
548         return 0;
549 }
550
551 static int
552 hantro_buf_plane_check(struct vb2_buffer *vb, const struct hantro_fmt *vpu_fmt,
553                        struct v4l2_pix_format_mplane *pixfmt)
554 {
555         unsigned int sz;
556         int i;
557
558         for (i = 0; i < pixfmt->num_planes; ++i) {
559                 sz = pixfmt->plane_fmt[i].sizeimage;
560                 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
561                           i, vb2_plane_size(vb, i), sz);
562                 if (vb2_plane_size(vb, i) < sz) {
563                         vpu_err("plane %d is too small for output\n", i);
564                         return -EINVAL;
565                 }
566         }
567         return 0;
568 }
569
570 static int hantro_buf_prepare(struct vb2_buffer *vb)
571 {
572         struct vb2_queue *vq = vb->vb2_queue;
573         struct hantro_ctx *ctx = vb2_get_drv_priv(vq);
574
575         if (V4L2_TYPE_IS_OUTPUT(vq->type))
576                 return hantro_buf_plane_check(vb, ctx->vpu_src_fmt,
577                                                   &ctx->src_fmt);
578
579         return hantro_buf_plane_check(vb, ctx->vpu_dst_fmt, &ctx->dst_fmt);
580 }
581
582 static void hantro_buf_queue(struct vb2_buffer *vb)
583 {
584         struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
585         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
586
587         v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
588 }
589
590 static bool hantro_vq_is_coded(struct vb2_queue *q)
591 {
592         struct hantro_ctx *ctx = vb2_get_drv_priv(q);
593
594         return hantro_is_encoder_ctx(ctx) != V4L2_TYPE_IS_OUTPUT(q->type);
595 }
596
597 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count)
598 {
599         struct hantro_ctx *ctx = vb2_get_drv_priv(q);
600         int ret = 0;
601
602         if (V4L2_TYPE_IS_OUTPUT(q->type))
603                 ctx->sequence_out = 0;
604         else
605                 ctx->sequence_cap = 0;
606
607         if (hantro_vq_is_coded(q)) {
608                 enum hantro_codec_mode codec_mode;
609
610                 if (V4L2_TYPE_IS_OUTPUT(q->type))
611                         codec_mode = ctx->vpu_src_fmt->codec_mode;
612                 else
613                         codec_mode = ctx->vpu_dst_fmt->codec_mode;
614
615                 vpu_debug(4, "Codec mode = %d\n", codec_mode);
616                 ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
617                 if (ctx->codec_ops->init)
618                         ret = ctx->codec_ops->init(ctx);
619         }
620
621         return ret;
622 }
623
624 static void
625 hantro_return_bufs(struct vb2_queue *q,
626                    struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *))
627 {
628         struct hantro_ctx *ctx = vb2_get_drv_priv(q);
629
630         for (;;) {
631                 struct vb2_v4l2_buffer *vbuf;
632
633                 vbuf = buf_remove(ctx->fh.m2m_ctx);
634                 if (!vbuf)
635                         break;
636                 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
637                                            &ctx->ctrl_handler);
638                 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
639         }
640 }
641
642 static void hantro_stop_streaming(struct vb2_queue *q)
643 {
644         struct hantro_ctx *ctx = vb2_get_drv_priv(q);
645
646         if (hantro_vq_is_coded(q)) {
647                 if (ctx->codec_ops && ctx->codec_ops->exit)
648                         ctx->codec_ops->exit(ctx);
649         }
650
651         /*
652          * The mem2mem framework calls v4l2_m2m_cancel_job before
653          * .stop_streaming, so there isn't any job running and
654          * it is safe to return all the buffers.
655          */
656         if (V4L2_TYPE_IS_OUTPUT(q->type))
657                 hantro_return_bufs(q, v4l2_m2m_src_buf_remove);
658         else
659                 hantro_return_bufs(q, v4l2_m2m_dst_buf_remove);
660 }
661
662 static void hantro_buf_request_complete(struct vb2_buffer *vb)
663 {
664         struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
665
666         v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
667 }
668
669 static int hantro_buf_out_validate(struct vb2_buffer *vb)
670 {
671         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
672
673         vbuf->field = V4L2_FIELD_NONE;
674         return 0;
675 }
676
677 const struct vb2_ops hantro_queue_ops = {
678         .queue_setup = hantro_queue_setup,
679         .buf_prepare = hantro_buf_prepare,
680         .buf_queue = hantro_buf_queue,
681         .buf_out_validate = hantro_buf_out_validate,
682         .buf_request_complete = hantro_buf_request_complete,
683         .start_streaming = hantro_start_streaming,
684         .stop_streaming = hantro_stop_streaming,
685         .wait_prepare = vb2_ops_wait_prepare,
686         .wait_finish = vb2_ops_wait_finish,
687 };