media: exynos: fix permissions on files
[cascardo/linux.git] / drivers / media / video / s5p-mfc / s5p_mfc_dec.c
1 /*
2  * linux/drivers/media/video/s5p-mfc/s5p_mfc_dec.c
3  *
4  * Copyright (C) 2011 Samsung Electronics Co., Ltd.
5  *              http://www.samsung.com/
6  * Kamil Debski, <k.debski@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  */
13
14 #include <linux/clk.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/version.h>
22 #include <linux/videodev2.h>
23 #include <linux/workqueue.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/videobuf2-core.h>
26 #include "s5p_mfc_common.h"
27 #include "s5p_mfc_debug.h"
28 #include "s5p_mfc_dec.h"
29 #include "s5p_mfc_intr.h"
30 #include "s5p_mfc_pm.h"
31
32 #define DEF_SRC_FMT     4
33 #define DEF_DST_FMT     0
34
35 static struct s5p_mfc_fmt formats[] = {
36         {
37                 .name           = "4:2:0 2 Planes 16x16 Tiles",
38                 .fourcc         = V4L2_PIX_FMT_NV12MT_16X16,
39                 .codec_mode     = S5P_FIMV_CODEC_NONE,
40                 .type           = MFC_FMT_RAW,
41                 .num_planes     = 2,
42         },
43         {
44                 .name           = "4:2:0 2 Planes 64x32 Tiles",
45                 .fourcc         = V4L2_PIX_FMT_NV12MT,
46                 .codec_mode     = S5P_FIMV_CODEC_NONE,
47                 .type           = MFC_FMT_RAW,
48                 .num_planes     = 2,
49         },
50         {
51                 .name           = "4:2:0 2 Planes Y/CbCr",
52                 .fourcc         = V4L2_PIX_FMT_NV12M,
53                 .codec_mode     = S5P_FIMV_CODEC_NONE,
54                 .type           = MFC_FMT_RAW,
55                 .num_planes     = 2,
56         },
57         {
58                 .name           = "4:2:0 2 Planes Y/CrCb",
59                 .fourcc         = V4L2_PIX_FMT_NV21M,
60                 .codec_mode     = S5P_FIMV_CODEC_NONE,
61                 .type           = MFC_FMT_RAW,
62                 .num_planes     = 2,
63         },
64         {
65                 .name           = "H264 Encoded Stream",
66                 .fourcc         = V4L2_PIX_FMT_H264,
67                 .codec_mode     = S5P_FIMV_CODEC_H264_DEC,
68                 .type           = MFC_FMT_DEC,
69                 .num_planes     = 1,
70         },
71         {
72                 .name           = "H264/MVC Encoded Stream",
73                 .fourcc         = V4L2_PIX_FMT_H264_MVC,
74                 .codec_mode     = S5P_FIMV_CODEC_H264_MVC_DEC,
75                 .type           = MFC_FMT_DEC,
76                 .num_planes     = 1,
77         },
78         {
79                 .name           = "H263 Encoded Stream",
80                 .fourcc         = V4L2_PIX_FMT_H263,
81                 .codec_mode     = S5P_FIMV_CODEC_H263_DEC,
82                 .type           = MFC_FMT_DEC,
83                 .num_planes     = 1,
84         },
85         {
86                 .name           = "MPEG1 Encoded Stream",
87                 .fourcc         = V4L2_PIX_FMT_MPEG1,
88                 .codec_mode     = S5P_FIMV_CODEC_MPEG2_DEC,
89                 .type           = MFC_FMT_DEC,
90                 .num_planes     = 1,
91         },
92         {
93                 .name           = "MPEG2 Encoded Stream",
94                 .fourcc         = V4L2_PIX_FMT_MPEG2,
95                 .codec_mode     = S5P_FIMV_CODEC_MPEG2_DEC,
96                 .type           = MFC_FMT_DEC,
97                 .num_planes     = 1,
98         },
99         {
100                 .name           = "MPEG4 Encoded Stream",
101                 .fourcc         = V4L2_PIX_FMT_MPEG4,
102                 .codec_mode     = S5P_FIMV_CODEC_MPEG4_DEC,
103                 .type           = MFC_FMT_DEC,
104                 .num_planes     = 1,
105         },
106         {
107                 .name           = "XviD Encoded Stream",
108                 .fourcc         = V4L2_PIX_FMT_XVID,
109                 .codec_mode     = S5P_FIMV_CODEC_MPEG4_DEC,
110                 .type           = MFC_FMT_DEC,
111                 .num_planes     = 1,
112         },
113         {
114                 .name           = "VC1 Encoded Stream",
115                 .fourcc         = V4L2_PIX_FMT_VC1_ANNEX_G,
116                 .codec_mode     = S5P_FIMV_CODEC_VC1_DEC,
117                 .type           = MFC_FMT_DEC,
118                 .num_planes     = 1,
119         },
120         {
121                 .name           = "VC1 RCV Encoded Stream",
122                 .fourcc         = V4L2_PIX_FMT_VC1_ANNEX_L,
123                 .codec_mode     = S5P_FIMV_CODEC_VC1RCV_DEC,
124                 .type           = MFC_FMT_DEC,
125                 .num_planes     = 1,
126         },
127         {
128                 .name           = "VC8 Encoded Stream",
129                 .fourcc         = V4L2_PIX_FMT_VP8,
130                 .codec_mode     = S5P_FIMV_CODEC_VP8_DEC,
131                 .type           = MFC_FMT_DEC,
132                 .num_planes     = 1,
133         },
134 };
135
136 #define NUM_FORMATS ARRAY_SIZE(formats)
137
138 /* Find selected format description */
139 static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
140 {
141         unsigned int i;
142
143         for (i = 0; i < NUM_FORMATS; i++) {
144                 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
145                     formats[i].type == t)
146                         return &formats[i];
147         }
148         return NULL;
149 }
150
151 static struct mfc_control controls[] = {
152         {
153                 .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY,
154                 .type = V4L2_CTRL_TYPE_INTEGER,
155                 .name = "H264 Display Delay",
156                 .minimum = 0,
157                 .maximum = 16383,
158                 .step = 1,
159                 .default_value = 0,
160         },
161         {
162                 .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE,
163                 .type = V4L2_CTRL_TYPE_BOOLEAN,
164                 .name = "H264 Display Delay Enable",
165                 .minimum = 0,
166                 .maximum = 1,
167                 .step = 1,
168                 .default_value = 0,
169         },
170         {
171                 .id = V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER,
172                 .type = V4L2_CTRL_TYPE_BOOLEAN,
173                 .name = "Mpeg4 Loop Filter Enable",
174                 .minimum = 0,
175                 .maximum = 1,
176                 .step = 1,
177                 .default_value = 0,
178         },
179         {
180                 .id = V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE,
181                 .type = V4L2_CTRL_TYPE_BOOLEAN,
182                 .name = "Slice Interface Enable",
183                 .minimum = 0,
184                 .maximum = 1,
185                 .step = 1,
186                 .default_value = 0,
187         },
188         {
189                 .id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
190                 .type = V4L2_CTRL_TYPE_INTEGER,
191                 .name = "Minimum number of cap bufs",
192                 .minimum = 1,
193                 .maximum = 32,
194                 .step = 1,
195                 .default_value = 1,
196                 .is_volatile = 1,
197         },
198         {
199                 .id = V4L2_CID_CODEC_DISPLAY_STATUS,
200                 .type = V4L2_CTRL_TYPE_INTEGER,
201                 .name = "Display Status",
202                 .minimum = 0,
203                 .maximum = 3,
204                 .step = 1,
205                 .default_value = 0,
206                 .is_volatile = 1,
207         },
208 };
209
210 #define NUM_CTRLS ARRAY_SIZE(controls)
211
212 /* Check whether a context should be run on hardware */
213 static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
214 {
215         /* Context is to parse header */
216         if (ctx->src_queue_cnt >= 1 && ctx->state == MFCINST_GOT_INST)
217                 return 1;
218         /* Context is to decode a frame */
219         if (ctx->src_queue_cnt >= 1 &&
220             ctx->state == MFCINST_RUNNING &&
221             ctx->dst_queue_cnt >= ctx->dpb_count)
222                 return 1;
223         /* Context is to return last frame */
224         if (ctx->state == MFCINST_FINISHING &&
225             ctx->dst_queue_cnt >= ctx->dpb_count)
226                 return 1;
227         /* Context is to set buffers */
228         if (ctx->src_queue_cnt >= 1 &&
229             ctx->state == MFCINST_HEAD_PARSED &&
230             ctx->capture_state == QUEUE_BUFS_MMAPED)
231                 return 1;
232         /* Resolution change */
233         if ((ctx->state == MFCINST_RES_CHANGE_INIT ||
234                 ctx->state == MFCINST_RES_CHANGE_FLUSH) &&
235                 ctx->dst_queue_cnt >= ctx->dpb_count)
236                 return 1;
237         if (ctx->state == MFCINST_RES_CHANGE_END &&
238                 ctx->src_queue_cnt >= 1)
239                 return 1;
240         mfc_debug(2, "ctx is not ready\n");
241         return 0;
242 }
243
244 static struct s5p_mfc_codec_ops decoder_codec_ops = {
245         .pre_seq_start          = NULL,
246         .post_seq_start         = NULL,
247         .pre_frame_start        = NULL,
248         .post_frame_start       = NULL,
249 };
250
251 /* Query capabilities of the device */
252 static int vidioc_querycap(struct file *file, void *priv,
253                            struct v4l2_capability *cap)
254 {
255         struct s5p_mfc_dev *dev = video_drvdata(file);
256
257         strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
258         strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
259         cap->bus_info[0] = 0;
260         cap->version = KERNEL_VERSION(1, 0, 0);
261         cap->capabilities = V4L2_CAP_VIDEO_CAPTURE_MPLANE |
262                         V4L2_CAP_VIDEO_OUTPUT_MPLANE | V4L2_CAP_STREAMING;
263         return 0;
264 }
265
266 /* Enumerate format */
267 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
268 {
269         struct s5p_mfc_fmt *fmt;
270         int i, j = 0;
271
272         for (i = 0; i < ARRAY_SIZE(formats); ++i) {
273                 if (mplane && formats[i].num_planes == 1)
274                         continue;
275                 else if (!mplane && formats[i].num_planes > 1)
276                         continue;
277                 if (out && formats[i].type != MFC_FMT_DEC)
278                         continue;
279                 else if (!out && formats[i].type != MFC_FMT_RAW)
280                         continue;
281
282                 if (j == f->index)
283                         break;
284                 ++j;
285         }
286         if (i == ARRAY_SIZE(formats))
287                 return -EINVAL;
288         fmt = &formats[i];
289         strlcpy(f->description, fmt->name, sizeof(f->description));
290         f->pixelformat = fmt->fourcc;
291         return 0;
292 }
293
294 static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
295                                                         struct v4l2_fmtdesc *f)
296 {
297         return vidioc_enum_fmt(f, false, false);
298 }
299
300 static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
301                                                         struct v4l2_fmtdesc *f)
302 {
303         return vidioc_enum_fmt(f, true, false);
304 }
305
306 static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
307                                                         struct v4l2_fmtdesc *f)
308 {
309         return vidioc_enum_fmt(f, false, true);
310 }
311
312 static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
313                                                         struct v4l2_fmtdesc *f)
314 {
315         return vidioc_enum_fmt(f, true, true);
316 }
317
318 /* Get format */
319 static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
320 {
321         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
322         struct v4l2_pix_format_mplane *pix_mp;
323
324         mfc_debug_enter();
325         pix_mp = &f->fmt.pix_mp;
326         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
327             (ctx->state == MFCINST_GOT_INST || ctx->state ==
328                                                 MFCINST_RES_CHANGE_END)) {
329                 /* If the MFC is parsing the header,
330                  * so wait until it is finished */
331                 s5p_mfc_clean_ctx_int_flags(ctx);
332                 s5p_mfc_wait_for_done_ctx(ctx, S5P_FIMV_R2H_CMD_SEQ_DONE_RET,
333                                                                         0);
334         }
335         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
336             ctx->state >= MFCINST_HEAD_PARSED &&
337             ctx->state < MFCINST_ABORT) {
338                 /* This is run on CAPTURE (decode output) */
339                 /* Width and height are set to the dimensions
340                    of the movie, the buffer is bigger and
341                    further processing stages should crop to this
342                    rectangle. */
343                 pix_mp->width = ctx->buf_width;
344                 pix_mp->height = ctx->buf_height;
345                 pix_mp->field = V4L2_FIELD_NONE;
346                 pix_mp->num_planes = 2;
347                 /* Set pixelformat to the format in which MFC
348                    outputs the decoded frame */
349                 pix_mp->pixelformat = V4L2_PIX_FMT_NV12MT_16X16;
350                 pix_mp->plane_fmt[0].bytesperline = ctx->buf_width;
351                 pix_mp->plane_fmt[0].sizeimage = ctx->luma_size;
352                 pix_mp->plane_fmt[1].bytesperline = ctx->buf_width;
353                 pix_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
354         } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
355                 /* This is run on OUTPUT
356                    The buffer contains compressed image
357                    so width and height have no meaning */
358                 pix_mp->width = 0;
359                 pix_mp->height = 0;
360                 pix_mp->field = V4L2_FIELD_NONE;
361                 pix_mp->plane_fmt[0].bytesperline = ctx->dec_src_buf_size;
362                 pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size;
363                 pix_mp->pixelformat = ctx->src_fmt->fourcc;
364                 pix_mp->num_planes = ctx->src_fmt->num_planes;
365         } else {
366                 mfc_err("Format could not be read\n");
367                 mfc_debug(2, "%s-- with error\n", __func__);
368                 return -EINVAL;
369         }
370         mfc_debug_leave();
371         return 0;
372 }
373
374 /* Try format */
375 static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
376 {
377         struct s5p_mfc_dev *dev = video_drvdata(file);
378         struct s5p_mfc_fmt *fmt;
379
380         mfc_debug(2, "Type is %d\n", f->type);
381         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
382                 fmt = find_format(f, MFC_FMT_DEC);
383                 if (!fmt) {
384                         mfc_err("Unsupported format for source.\n");
385                         return -EINVAL;
386                 }
387                 if (!IS_MFCV6(dev)) {
388                         if (fmt->fourcc == V4L2_PIX_FMT_VP8) {
389                                 mfc_err("Not supported format.\n");
390                                 return -EINVAL;
391                         }
392                 }
393         } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
394                 fmt = find_format(f, MFC_FMT_RAW);
395                 if (!fmt) {
396                         mfc_err("Unsupported format for destination.\n");
397                         return -EINVAL;
398                 }
399                 if (IS_MFCV6(dev)) {
400                         if (fmt->fourcc == V4L2_PIX_FMT_NV12MT) {
401                                 mfc_err("Not supported format.\n");
402                                 return -EINVAL;
403                         }
404                 } else {
405                         if (fmt->fourcc != V4L2_PIX_FMT_NV12MT) {
406                                 mfc_err("Not supported format.\n");
407                                 return -EINVAL;
408                         }
409                 }
410         }
411
412         return 0;
413 }
414
415 /* Set format */
416 static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
417 {
418         struct s5p_mfc_dev *dev = video_drvdata(file);
419         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
420         int ret = 0;
421         struct s5p_mfc_fmt *fmt;
422         struct v4l2_pix_format_mplane *pix_mp;
423
424         mfc_debug_enter();
425         ret = vidioc_try_fmt(file, priv, f);
426         pix_mp = &f->fmt.pix_mp;
427         if (ret)
428                 return ret;
429         if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
430                 v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
431                 ret = -EBUSY;
432                 goto out;
433         }
434         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
435                 fmt = find_format(f, MFC_FMT_RAW);
436                 if (!fmt) {
437                         mfc_err("Unsupported format for source.\n");
438                         return -EINVAL;
439                 }
440                 if (!IS_MFCV6(dev)) {
441                         if (fmt->fourcc != V4L2_PIX_FMT_NV12MT) {
442                                 mfc_err("Not supported format.\n");
443                                 return -EINVAL;
444                         }
445                 } else if (IS_MFCV6(dev)) {
446                         if (fmt->fourcc == V4L2_PIX_FMT_NV12MT) {
447                                 mfc_err("Not supported format.\n");
448                                 return -EINVAL;
449                         }
450                 }
451                 ctx->dst_fmt = fmt;
452                 mfc_debug_leave();
453                 return ret;
454         } else if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
455                 mfc_err("Wrong type error for S_FMT : %d", f->type);
456                 return -EINVAL;
457         }
458         fmt = find_format(f, MFC_FMT_DEC);
459         if (!fmt || fmt->codec_mode == S5P_FIMV_CODEC_NONE) {
460                 mfc_err("Unknown codec\n");
461                 ret = -EINVAL;
462                 goto out;
463         }
464         if (fmt->type != MFC_FMT_DEC) {
465                 mfc_err("Wrong format selected, you should choose "
466                                         "format for decoding\n");
467                 ret = -EINVAL;
468                 goto out;
469         }
470         if (!IS_MFCV6(dev)) {
471                 if (fmt->fourcc == V4L2_PIX_FMT_VP8) {
472                         mfc_err("Not supported format.\n");
473                         return -EINVAL;
474                 }
475         }
476         ctx->src_fmt = fmt;
477         ctx->codec_mode = fmt->codec_mode;
478         mfc_debug(2, "The codec number is: %d\n", ctx->codec_mode);
479         pix_mp->height = 0;
480         pix_mp->width = 0;
481         if (pix_mp->plane_fmt[0].sizeimage)
482                 ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage;
483         else
484                 pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size =
485                                                                 DEF_CPB_SIZE;
486         pix_mp->plane_fmt[0].bytesperline = 0;
487         ctx->state = MFCINST_INIT;
488 out:
489         mfc_debug_leave();
490         return ret;
491 }
492
493 /* Reqeust buffers */
494 static int vidioc_reqbufs(struct file *file, void *priv,
495                                           struct v4l2_requestbuffers *reqbufs)
496 {
497         struct s5p_mfc_dev *dev = video_drvdata(file);
498         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
499         int ret = 0;
500         unsigned long flags;
501
502         if (reqbufs->memory != V4L2_MEMORY_MMAP) {
503                 mfc_err("Only V4L2_MEMORY_MAP is supported\n");
504                 return -EINVAL;
505         }
506         if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
507                 /* Can only request buffers after an instance has been opened.*/
508                 if (ctx->state == MFCINST_INIT) {
509                         ctx->src_bufs_cnt = 0;
510                         if (reqbufs->count == 0) {
511                                 mfc_debug(2, "Freeing buffers\n");
512                                 s5p_mfc_clock_on();
513                                 ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
514                                 s5p_mfc_clock_off();
515                                 return ret;
516                         }
517                         /* Decoding */
518                         if (ctx->output_state != QUEUE_FREE) {
519                                 mfc_err("Bufs have already been requested\n");
520                                 return -EINVAL;
521                         }
522                         s5p_mfc_clock_on();
523                         ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
524                         s5p_mfc_clock_off();
525                         if (ret) {
526                                 mfc_err("vb2_reqbufs on output failed\n");
527                                 return ret;
528                         }
529                         mfc_debug(2, "vb2_reqbufs: %d\n", ret);
530                         ctx->output_state = QUEUE_BUFS_REQUESTED;
531                 }
532         } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
533                 ctx->dst_bufs_cnt = 0;
534                 if (reqbufs->count == 0) {
535                         mfc_debug(2, "Freeing buffers\n");
536                         s5p_mfc_clock_on();
537                         ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
538                         s5p_mfc_clock_off();
539                         return ret;
540                 }
541                 if (ctx->capture_state != QUEUE_FREE) {
542                         mfc_err("Bufs have already been requested\n");
543                         return -EINVAL;
544                 }
545                 ctx->capture_state = QUEUE_BUFS_REQUESTED;
546                 s5p_mfc_clock_on();
547                 ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
548                 s5p_mfc_clock_off();
549                 if (ret) {
550                         mfc_err("vb2_reqbufs on capture failed\n");
551                         return ret;
552                 }
553                 if (reqbufs->count < ctx->dpb_count) {
554                         mfc_err("Not enough buffers allocated\n");
555                         reqbufs->count = 0;
556                         s5p_mfc_clock_on();
557                         ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
558                         s5p_mfc_clock_off();
559                         return -ENOMEM;
560                 }
561                 ctx->total_dpb_count = reqbufs->count;
562                 ret = s5p_mfc_alloc_codec_buffers(ctx);
563                 if (ret) {
564                         mfc_err("Failed to allocate decoding buffers\n");
565                         reqbufs->count = 0;
566                         s5p_mfc_clock_on();
567                         ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
568                         s5p_mfc_clock_off();
569                         return -ENOMEM;
570                 }
571                 if (ctx->dst_bufs_cnt == ctx->total_dpb_count) {
572                         ctx->capture_state = QUEUE_BUFS_MMAPED;
573                 } else {
574                         mfc_err("Not all buffers passed to buf_init\n");
575                         reqbufs->count = 0;
576                         s5p_mfc_clock_on();
577                         ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
578                         s5p_mfc_release_codec_buffers(ctx);
579                         s5p_mfc_clock_off();
580                         return -ENOMEM;
581                 }
582                 if (s5p_mfc_ctx_ready(ctx)) {
583                         spin_lock_irqsave(&dev->condlock, flags);
584                         set_bit(ctx->num, &dev->ctx_work_bits);
585                         spin_unlock_irqrestore(&dev->condlock, flags);
586                 }
587                 s5p_mfc_try_run(dev);
588                 s5p_mfc_wait_for_done_ctx(ctx,
589                                         S5P_FIMV_R2H_CMD_INIT_BUFFERS_RET, 0);
590         }
591         return ret;
592 }
593
594 /* Query buffer */
595 static int vidioc_querybuf(struct file *file, void *priv,
596                                                    struct v4l2_buffer *buf)
597 {
598         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
599         int ret;
600         int i;
601
602         if (buf->memory != V4L2_MEMORY_MMAP) {
603                 mfc_err("Only mmaped buffers can be used\n");
604                 return -EINVAL;
605         }
606         mfc_debug(2, "State: %d, buf->type: %d\n", ctx->state, buf->type);
607         if (ctx->state == MFCINST_INIT &&
608                         buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
609                 ret = vb2_querybuf(&ctx->vq_src, buf);
610         } else if (ctx->state == MFCINST_RUNNING &&
611                         buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
612                 ret = vb2_querybuf(&ctx->vq_dst, buf);
613                 for (i = 0; i < buf->length; i++)
614                         buf->m.planes[i].m.mem_offset += DST_QUEUE_OFF_BASE;
615         } else {
616                 mfc_err("vidioc_querybuf called in an inappropriate state\n");
617                 ret = -EINVAL;
618         }
619         mfc_debug_leave();
620         return ret;
621 }
622
623 /* Queue a buffer */
624 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
625 {
626         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
627
628         if (ctx->state == MFCINST_ERROR) {
629                 mfc_err("Call on QBUF after unrecoverable error\n");
630                 return -EIO;
631         }
632         if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
633                 return vb2_qbuf(&ctx->vq_src, buf);
634         else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
635                 return vb2_qbuf(&ctx->vq_dst, buf);
636         return -EINVAL;
637 }
638
639 /* Dequeue a buffer */
640 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
641 {
642         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
643
644         if (ctx->state == MFCINST_ERROR) {
645                 mfc_err("Call on DQBUF after unrecoverable error\n");
646                 return -EIO;
647         }
648         if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
649                 return vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
650         else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
651                 return vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
652         return -EINVAL;
653 }
654
655 /* Export DMA buffer */
656 static int vidioc_expbuf(struct file *file, void *priv,
657         struct v4l2_exportbuffer *eb)
658 {
659         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
660         int ret;
661
662         if (eb->mem_offset < DST_QUEUE_OFF_BASE)
663                 return vb2_expbuf(&ctx->vq_src, eb);
664
665         eb->mem_offset -= DST_QUEUE_OFF_BASE;
666         ret = vb2_expbuf(&ctx->vq_dst, eb);
667         eb->mem_offset += DST_QUEUE_OFF_BASE;
668
669         return ret;
670 }
671
672 /* Stream on */
673 static int vidioc_streamon(struct file *file, void *priv,
674                            enum v4l2_buf_type type)
675 {
676         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
677         struct s5p_mfc_dev *dev = ctx->dev;
678         unsigned long flags;
679         int ret = -EINVAL;
680
681         mfc_debug_enter();
682         if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
683
684                 if (ctx->state == MFCINST_INIT) {
685                         ctx->dst_bufs_cnt = 0;
686                         ctx->src_bufs_cnt = 0;
687                         ctx->capture_state = QUEUE_FREE;
688                         ctx->output_state = QUEUE_FREE;
689                         s5p_mfc_alloc_instance_buffer(ctx);
690                         s5p_mfc_alloc_dec_temp_buffers(ctx);
691                         spin_lock_irqsave(&dev->condlock, flags);
692                         set_bit(ctx->num, &dev->ctx_work_bits);
693                         spin_unlock_irqrestore(&dev->condlock, flags);
694                         s5p_mfc_clean_ctx_int_flags(ctx);
695                         s5p_mfc_try_run(dev);
696
697                         if (s5p_mfc_wait_for_done_ctx(ctx,
698                                 S5P_FIMV_R2H_CMD_OPEN_INSTANCE_RET, 0)) {
699                                 /* Error or timeout */
700                                 mfc_err("Error getting instance from hardware\n");
701                                 s5p_mfc_release_instance_buffer(ctx);
702                                 s5p_mfc_release_dec_desc_buffer(ctx);
703                                 return -EIO;
704                         }
705                         mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
706                 }
707                 ret = vb2_streamon(&ctx->vq_src, type);
708                 }
709         else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
710                 ret = vb2_streamon(&ctx->vq_dst, type);
711         mfc_debug_leave();
712         return ret;
713 }
714
715 /* Stream off, which equals to a pause */
716 static int vidioc_streamoff(struct file *file, void *priv,
717                             enum v4l2_buf_type type)
718 {
719         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
720
721         if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
722                 return vb2_streamoff(&ctx->vq_src, type);
723         else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
724                 return vb2_streamoff(&ctx->vq_dst, type);
725         return -EINVAL;
726 }
727
728 /* Set controls - v4l2 control framework */
729 static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl *ctrl)
730 {
731         struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
732
733         switch (ctrl->id) {
734         case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY:
735                 ctx->loop_filter_mpeg4 = ctrl->val;
736                 break;
737         case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE:
738                 ctx->display_delay_enable = ctrl->val;
739                 break;
740         case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
741                 ctx->display_delay = ctrl->val;
742                 break;
743         case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
744                 ctx->slice_interface = ctrl->val;
745                 break;
746         default:
747                 mfc_err("Invalid control 0x%08x\n", ctrl->id);
748                 return -EINVAL;
749         }
750         return 0;
751 }
752
753 static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl *ctrl)
754 {
755         struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
756         struct s5p_mfc_dev *dev = ctx->dev;
757
758         switch (ctrl->id) {
759         case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
760                 if (ctx->state >= MFCINST_HEAD_PARSED &&
761                     ctx->state < MFCINST_ABORT) {
762                         ctrl->val = ctx->dpb_count;
763                         break;
764                 } else if (ctx->state != MFCINST_INIT) {
765                         v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
766                         return -EINVAL;
767                 }
768                 /* Should wait for the header to be parsed */
769                 s5p_mfc_clean_ctx_int_flags(ctx);
770                 s5p_mfc_wait_for_done_ctx(ctx,
771                                 S5P_FIMV_R2H_CMD_SEQ_DONE_RET, 0);
772                 if (ctx->state >= MFCINST_HEAD_PARSED &&
773                     ctx->state < MFCINST_ABORT) {
774                         ctrl->val = ctx->dpb_count;
775                 } else {
776                         v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
777                         return -EINVAL;
778                 }
779                 break;
780         case V4L2_CID_CODEC_DISPLAY_STATUS:
781                 ctrl->val = s5p_mfc_get_dspl_status()
782                                 & S5P_FIMV_DEC_STATUS_DECODING_STATUS_MASK;
783                 break;
784         }
785         return 0;
786 }
787
788
789 static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops = {
790         .s_ctrl = s5p_mfc_dec_s_ctrl,
791         .g_volatile_ctrl = s5p_mfc_dec_g_v_ctrl,
792 };
793
794 /* Get cropping information */
795 static int vidioc_g_crop(struct file *file, void *priv,
796                 struct v4l2_crop *cr)
797 {
798         struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
799         u32 left, right, top, bottom;
800
801         if (ctx->state != MFCINST_HEAD_PARSED &&
802         ctx->state != MFCINST_RUNNING && ctx->state != MFCINST_FINISHING
803                                         && ctx->state != MFCINST_FINISHED) {
804                         mfc_err("Cannont set crop\n");
805                         return -EINVAL;
806                 }
807         if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_H264) {
808                 left = s5p_mfc_read_info(ctx, CROP_INFO_H);
809                 right = left >> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT;
810                 left = left & S5P_FIMV_SHARED_CROP_LEFT_MASK;
811                 top = s5p_mfc_read_info(ctx, CROP_INFO_V);
812                 bottom = top >> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT;
813                 top = top & S5P_FIMV_SHARED_CROP_TOP_MASK;
814                 cr->c.left = left;
815                 cr->c.top = top;
816                 cr->c.width = ctx->img_width - left - right;
817                 cr->c.height = ctx->img_height - top - bottom;
818                 mfc_debug(2, "Cropping info [h264]: l=%d t=%d "
819                         "w=%d h=%d (r=%d b=%d fw=%d fh=%d\n", left, top,
820                         cr->c.width, cr->c.height, right, bottom,
821                         ctx->buf_width, ctx->buf_height);
822         } else {
823                 cr->c.left = 0;
824                 cr->c.top = 0;
825                 cr->c.width = ctx->img_width;
826                 cr->c.height = ctx->img_height;
827                 mfc_debug(2, "Cropping info: w=%d h=%d fw=%d "
828                         "fh=%d\n", cr->c.width, cr->c.height, ctx->buf_width,
829                                                         ctx->buf_height);
830         }
831         return 0;
832 }
833
834 /* v4l2_ioctl_ops */
835 static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
836         .vidioc_querycap = vidioc_querycap,
837         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
838         .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
839         .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
840         .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
841         .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
842         .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
843         .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
844         .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
845         .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
846         .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
847         .vidioc_reqbufs = vidioc_reqbufs,
848         .vidioc_querybuf = vidioc_querybuf,
849         .vidioc_qbuf = vidioc_qbuf,
850         .vidioc_dqbuf = vidioc_dqbuf,
851         .vidioc_expbuf = vidioc_expbuf,
852         .vidioc_streamon = vidioc_streamon,
853         .vidioc_streamoff = vidioc_streamoff,
854         .vidioc_g_crop = vidioc_g_crop,
855 };
856
857 static int s5p_mfc_queue_setup(struct vb2_queue *vq,
858                         const struct v4l2_format *fmt, unsigned int *buf_count,
859                         unsigned int *plane_count, unsigned int psize[],
860                         void *allocators[])
861 {
862         struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
863         struct s5p_mfc_dev *dev = ctx->dev;
864
865         /* Video output for decoding (source)
866          * this can be set after getting an instance */
867         if (ctx->state == MFCINST_INIT &&
868             vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
869                 /* A single plane is required for input */
870                 *plane_count = 1;
871                 if (*buf_count < 1)
872                         *buf_count = 1;
873                 if (*buf_count > MFC_MAX_BUFFERS)
874                         *buf_count = MFC_MAX_BUFFERS;
875         /* Video capture for decoding (destination)
876          * this can be set after the header was parsed */
877         } else if (ctx->state == MFCINST_HEAD_PARSED &&
878                    vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
879                 /* Output plane count is 2 - one for Y and one for CbCr */
880                 *plane_count = 2;
881                 /* Setup buffer count */
882                 if (*buf_count < ctx->dpb_count)
883                         *buf_count = ctx->dpb_count;
884                 if (*buf_count > ctx->dpb_count + MFC_MAX_EXTRA_DPB)
885                         *buf_count = ctx->dpb_count + MFC_MAX_EXTRA_DPB;
886                 if (*buf_count > MFC_MAX_BUFFERS)
887                         *buf_count = MFC_MAX_BUFFERS;
888         } else {
889                 mfc_err("State seems invalid. State = %d, vq->type = %d\n",
890                                                         ctx->state, vq->type);
891                 return -EINVAL;
892         }
893         mfc_debug(2, "Buffer count=%d, plane count=%d\n",
894                                                 *buf_count, *plane_count);
895         if (ctx->state == MFCINST_HEAD_PARSED &&
896             vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
897                 psize[0] = ctx->luma_size;
898                 psize[1] = ctx->chroma_size;
899
900                 if (IS_MFCV6(dev))
901                         allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
902                 else
903                         allocators[0] = ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
904                 allocators[1] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
905         } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
906                    ctx->state == MFCINST_INIT) {
907                 psize[0] = ctx->dec_src_buf_size;
908                 allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
909         } else {
910                 mfc_err("This video node is dedicated to decoding. Decoding not initalised\n");
911                 return -EINVAL;
912         }
913         return 0;
914 }
915
916 static void s5p_mfc_unlock(struct vb2_queue *q)
917 {
918         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
919         struct s5p_mfc_dev *dev = ctx->dev;
920
921         mutex_unlock(&dev->mfc_mutex);
922 }
923
924 static void s5p_mfc_lock(struct vb2_queue *q)
925 {
926         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
927         struct s5p_mfc_dev *dev = ctx->dev;
928
929         mutex_lock(&dev->mfc_mutex);
930 }
931
932 static int s5p_mfc_buf_init(struct vb2_buffer *vb)
933 {
934         struct vb2_queue *vq = vb->vb2_queue;
935         struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
936         unsigned int i;
937
938         if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
939                 if (ctx->capture_state == QUEUE_BUFS_MMAPED)
940                         return 0;
941                 for (i = 0; i <= ctx->src_fmt->num_planes; i++) {
942                         if (IS_ERR_OR_NULL(ERR_PTR(
943                                         vb2_dma_contig_plane_dma_addr(vb, i)))) {
944                                 mfc_err("Plane mem not allocated\n");
945                                 return -EINVAL;
946                         }
947                 }
948                 if (vb2_plane_size(vb, 0) < ctx->luma_size ||
949                         vb2_plane_size(vb, 1) < ctx->chroma_size) {
950                         mfc_err("Plane buffer (CAPTURE) is too small\n");
951                         return -EINVAL;
952                 }
953                 i = vb->v4l2_buf.index;
954                 ctx->dst_bufs[i].b = vb;
955                 ctx->dst_bufs[i].cookie.raw.luma =
956                                         vb2_dma_contig_plane_dma_addr(vb, 0);
957                 ctx->dst_bufs[i].cookie.raw.chroma =
958                                         vb2_dma_contig_plane_dma_addr(vb, 1);
959                 ctx->dst_bufs_cnt++;
960         } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
961                 if (IS_ERR_OR_NULL(ERR_PTR(
962                                         vb2_dma_contig_plane_dma_addr(vb, 0)))) {
963                         mfc_err("Plane memory not allocated\n");
964                         return -EINVAL;
965                 }
966                 if (vb2_plane_size(vb, 0) < ctx->dec_src_buf_size) {
967                         mfc_err("Plane buffer (OUTPUT) is too small\n");
968                         return -EINVAL;
969                 }
970
971                 i = vb->v4l2_buf.index;
972                 ctx->src_bufs[i].b = vb;
973                 ctx->src_bufs[i].cookie.stream =
974                                         vb2_dma_contig_plane_dma_addr(vb, 0);
975                 ctx->src_bufs_cnt++;
976         } else {
977                 mfc_err("s5p_mfc_buf_init: unknown queue type\n");
978                 return -EINVAL;
979         }
980         return 0;
981 }
982
983 static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
984 {
985         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
986         struct s5p_mfc_dev *dev = ctx->dev;
987         unsigned long flags;
988
989         v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
990         if (ctx->state == MFCINST_FINISHING ||
991                 ctx->state == MFCINST_FINISHED)
992                 ctx->state = MFCINST_RUNNING;
993         /* If context is ready then dev = work->data;schedule it to run */
994         if (s5p_mfc_ctx_ready(ctx)) {
995                 spin_lock_irqsave(&dev->condlock, flags);
996                 set_bit(ctx->num, &dev->ctx_work_bits);
997                 spin_unlock_irqrestore(&dev->condlock, flags);
998         }
999         s5p_mfc_try_run(dev);
1000         return 0;
1001 }
1002
1003 static int s5p_mfc_stop_streaming(struct vb2_queue *q)
1004 {
1005         unsigned long flags;
1006         struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1007         struct s5p_mfc_dev *dev = ctx->dev;
1008         int aborted = 0;
1009
1010         if ((ctx->state == MFCINST_FINISHING ||
1011                 ctx->state ==  MFCINST_RUNNING) &&
1012                 dev->curr_ctx == ctx->num && dev->hw_lock) {
1013                 ctx->state = MFCINST_ABORT;
1014                 s5p_mfc_wait_for_done_ctx(ctx,
1015                                         S5P_FIMV_R2H_CMD_FRAME_DONE_RET, 0);
1016                 aborted = 1;
1017         }
1018         spin_lock_irqsave(&dev->irqlock, flags);
1019         if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1020                 s5p_mfc_cleanup_queue(&ctx->dst_queue, &ctx->vq_dst);
1021                 INIT_LIST_HEAD(&ctx->dst_queue);
1022                 ctx->dst_queue_cnt = 0;
1023                 ctx->dpb_flush = 1;
1024                 ctx->dec_dst_flag = 0;
1025         }
1026         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1027                 s5p_mfc_cleanup_queue(&ctx->src_queue, &ctx->vq_src);
1028                 INIT_LIST_HEAD(&ctx->src_queue);
1029                 ctx->src_queue_cnt = 0;
1030         }
1031         if (aborted)
1032                 ctx->state = MFCINST_RUNNING;
1033         spin_unlock_irqrestore(&dev->irqlock, flags);
1034         return 0;
1035 }
1036
1037
1038 static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1039 {
1040         struct vb2_queue *vq = vb->vb2_queue;
1041         struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1042         struct s5p_mfc_dev *dev = ctx->dev;
1043         unsigned long flags;
1044         struct s5p_mfc_buf *mfc_buf;
1045
1046         if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1047                 mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
1048                 mfc_buf->used = 0;
1049                 spin_lock_irqsave(&dev->irqlock, flags);
1050                 list_add_tail(&mfc_buf->list, &ctx->src_queue);
1051                 ctx->src_queue_cnt++;
1052                 spin_unlock_irqrestore(&dev->irqlock, flags);
1053         } else if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1054                 mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
1055                 mfc_buf->used = 0;
1056                 /* Mark destination as available for use by MFC */
1057                 spin_lock_irqsave(&dev->irqlock, flags);
1058                 set_bit(vb->v4l2_buf.index, &ctx->dec_dst_flag);
1059                 list_add_tail(&mfc_buf->list, &ctx->dst_queue);
1060                 ctx->dst_queue_cnt++;
1061                 spin_unlock_irqrestore(&dev->irqlock, flags);
1062         } else {
1063                 mfc_err("Unsupported buffer type (%d)\n", vq->type);
1064         }
1065         if (s5p_mfc_ctx_ready(ctx)) {
1066                 spin_lock_irqsave(&dev->condlock, flags);
1067                 set_bit(ctx->num, &dev->ctx_work_bits);
1068                 spin_unlock_irqrestore(&dev->condlock, flags);
1069         }
1070         s5p_mfc_try_run(dev);
1071 }
1072
1073 static struct vb2_ops s5p_mfc_dec_qops = {
1074         .queue_setup            = s5p_mfc_queue_setup,
1075         .wait_prepare           = s5p_mfc_unlock,
1076         .wait_finish            = s5p_mfc_lock,
1077         .buf_init               = s5p_mfc_buf_init,
1078         .start_streaming        = s5p_mfc_start_streaming,
1079         .stop_streaming         = s5p_mfc_stop_streaming,
1080         .buf_queue              = s5p_mfc_buf_queue,
1081 };
1082
1083 struct s5p_mfc_codec_ops *get_dec_codec_ops(void)
1084 {
1085         return &decoder_codec_ops;
1086 }
1087
1088 struct vb2_ops *get_dec_queue_ops(void)
1089 {
1090         return &s5p_mfc_dec_qops;
1091 }
1092
1093 const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
1094 {
1095         return &s5p_mfc_dec_ioctl_ops;
1096 }
1097
1098 #define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1099                                                 && V4L2_CTRL_DRIVER_PRIV(x))
1100
1101 int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx *ctx)
1102 {
1103         struct v4l2_ctrl_config cfg;
1104         int i;
1105
1106         v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
1107         if (ctx->ctrl_handler.error) {
1108                 mfc_err("v4l2_ctrl_handler_init failed\n");
1109                 return ctx->ctrl_handler.error;
1110         }
1111
1112         for (i = 0; i < NUM_CTRLS; i++) {
1113                 if (IS_MFC51_PRIV(controls[i].id)) {
1114                         cfg.ops = &s5p_mfc_dec_ctrl_ops;
1115                         cfg.id = controls[i].id;
1116                         cfg.min = controls[i].minimum;
1117                         cfg.max = controls[i].maximum;
1118                         cfg.def = controls[i].default_value;
1119                         cfg.name = controls[i].name;
1120                         cfg.type = controls[i].type;
1121
1122                         cfg.step = controls[i].step;
1123                         cfg.menu_skip_mask = 0;
1124
1125                         ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
1126                                         &cfg, NULL);
1127                 } else {
1128                         ctx->ctrls[i] = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1129                                         &s5p_mfc_dec_ctrl_ops,
1130                                         controls[i].id, controls[i].minimum,
1131                                         controls[i].maximum, controls[i].step,
1132                                         controls[i].default_value);
1133                 }
1134                 if (ctx->ctrl_handler.error) {
1135                         mfc_err("Adding control (%d) failed\n", i);
1136                         return ctx->ctrl_handler.error;
1137                 }
1138                 if (controls[i].is_volatile && ctx->ctrls[i])
1139                         ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
1140         }
1141         return 0;
1142 }
1143
1144 void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx *ctx)
1145 {
1146         int i;
1147
1148         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1149         for (i = 0; i < NUM_CTRLS; i++)
1150                 ctx->ctrls[i] = NULL;
1151 }
1152
1153 void s5p_mfc_dec_init(struct s5p_mfc_ctx *ctx)
1154 {
1155         ctx->src_fmt = &formats[DEF_SRC_FMT];
1156         ctx->dst_fmt = &formats[DEF_DST_FMT];
1157 }