media: cec-func-poll.rst/func-poll.rst: update EINVAL description
[linux-block.git] / drivers / media / platform / vicodec / vicodec-core.c
CommitLineData
256bf813
HV
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * A virtual codec example device.
4 *
5 * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 *
7 * This is a virtual codec device driver for testing the codec framework.
8 * It simulates a device that uses memory buffers for both source and
9 * destination and encodes or decodes the data.
10 */
11
12#include <linux/module.h>
13#include <linux/delay.h>
14#include <linux/fs.h>
15#include <linux/sched.h>
16#include <linux/slab.h>
17
18#include <linux/platform_device.h>
19#include <media/v4l2-mem2mem.h>
20#include <media/v4l2-device.h>
21#include <media/v4l2-ioctl.h>
22#include <media/v4l2-ctrls.h>
23#include <media/v4l2-event.h>
24#include <media/videobuf2-vmalloc.h>
25
cd12b401 26#include "codec-v4l2-fwht.h"
256bf813
HV
27
28MODULE_DESCRIPTION("Virtual codec device");
29MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
30MODULE_LICENSE("GPL v2");
31
32static bool multiplanar;
33module_param(multiplanar, bool, 0444);
34MODULE_PARM_DESC(multiplanar,
35 " use multi-planar API instead of single-planar API");
36
37static unsigned int debug;
38module_param(debug, uint, 0644);
39MODULE_PARM_DESC(debug, " activates debug info");
40
41#define VICODEC_NAME "vicodec"
42#define MAX_WIDTH 4096U
43#define MIN_WIDTH 640U
44#define MAX_HEIGHT 2160U
45#define MIN_HEIGHT 480U
46
47#define dprintk(dev, fmt, arg...) \
48 v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
49
50
29a7a5e9
HV
51struct pixfmt_info {
52 u32 id;
53 unsigned int bytesperline_mult;
54 unsigned int sizeimage_mult;
55 unsigned int sizeimage_div;
56 unsigned int luma_step;
57 unsigned int chroma_step;
58 /* Chroma plane subsampling */
59 unsigned int width_div;
60 unsigned int height_div;
61};
62
cd12b401 63static const struct v4l2_fwht_pixfmt_info pixfmt_fwht = {
29a7a5e9
HV
64 V4L2_PIX_FMT_FWHT, 0, 3, 1, 1, 1, 1, 1
65};
66
256bf813
HV
67static void vicodec_dev_release(struct device *dev)
68{
69}
70
71static struct platform_device vicodec_pdev = {
72 .name = VICODEC_NAME,
73 .dev.release = vicodec_dev_release,
74};
75
76/* Per-queue, driver-specific private data */
77struct vicodec_q_data {
78 unsigned int width;
79 unsigned int height;
256bf813
HV
80 unsigned int sizeimage;
81 unsigned int sequence;
cd12b401 82 const struct v4l2_fwht_pixfmt_info *info;
256bf813
HV
83};
84
85enum {
86 V4L2_M2M_SRC = 0,
87 V4L2_M2M_DST = 1,
88};
89
90struct vicodec_dev {
91 struct v4l2_device v4l2_dev;
92 struct video_device enc_vfd;
93 struct video_device dec_vfd;
94#ifdef CONFIG_MEDIA_CONTROLLER
95 struct media_device mdev;
96#endif
97
98 struct mutex enc_mutex;
99 struct mutex dec_mutex;
100 spinlock_t enc_lock;
101 spinlock_t dec_lock;
102
103 struct v4l2_m2m_dev *enc_dev;
104 struct v4l2_m2m_dev *dec_dev;
105};
106
107struct vicodec_ctx {
108 struct v4l2_fh fh;
109 struct vicodec_dev *dev;
110 bool is_enc;
111 spinlock_t *lock;
112
113 struct v4l2_ctrl_handler hdl;
256bf813
HV
114
115 /* Abort requested by m2m */
116 int aborting;
117 struct vb2_v4l2_buffer *last_src_buf;
118 struct vb2_v4l2_buffer *last_dst_buf;
119
256bf813
HV
120 /* Source and destination queue data */
121 struct vicodec_q_data q_data[2];
cd12b401
HV
122 struct v4l2_fwht_state state;
123
256bf813
HV
124 u32 cur_buf_offset;
125 u32 comp_max_size;
126 u32 comp_size;
127 u32 comp_magic_cnt;
128 u32 comp_frame_size;
129 bool comp_has_frame;
130 bool comp_has_next_frame;
131};
132
256bf813
HV
133static inline struct vicodec_ctx *file2ctx(struct file *file)
134{
135 return container_of(file->private_data, struct vicodec_ctx, fh);
136}
137
138static struct vicodec_q_data *get_q_data(struct vicodec_ctx *ctx,
139 enum v4l2_buf_type type)
140{
141 switch (type) {
142 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
143 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
144 return &ctx->q_data[V4L2_M2M_SRC];
145 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
146 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
147 return &ctx->q_data[V4L2_M2M_DST];
148 default:
149 WARN_ON(1);
150 break;
151 }
152 return NULL;
153}
154
256bf813
HV
155static int device_process(struct vicodec_ctx *ctx,
156 struct vb2_v4l2_buffer *in_vb,
157 struct vb2_v4l2_buffer *out_vb)
158{
159 struct vicodec_dev *dev = ctx->dev;
160 struct vicodec_q_data *q_out, *q_cap;
cd12b401 161 struct v4l2_fwht_state *state = &ctx->state;
256bf813
HV
162 u8 *p_in, *p_out;
163 int ret;
164
165 q_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
166 q_cap = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
167 if (ctx->is_enc)
168 p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
169 else
cd12b401 170 p_in = state->compressed_frame;
256bf813
HV
171 p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
172 if (!p_in || !p_out) {
173 v4l2_err(&dev->v4l2_dev,
174 "Acquiring kernel pointers to buffers failed\n");
175 return -EFAULT;
176 }
177
178 if (ctx->is_enc) {
cd12b401 179 unsigned int size = v4l2_fwht_encode(state, p_in, p_out);
256bf813 180
cd12b401 181 vb2_set_plane_payload(&out_vb->vb2_buf, 0, size);
256bf813 182 } else {
cd12b401 183 ret = v4l2_fwht_decode(state, p_in, p_out);
256bf813
HV
184 if (ret)
185 return ret;
29a7a5e9 186 vb2_set_plane_payload(&out_vb->vb2_buf, 0, q_cap->sizeimage);
256bf813
HV
187 }
188
189 out_vb->sequence = q_cap->sequence++;
190 out_vb->vb2_buf.timestamp = in_vb->vb2_buf.timestamp;
191
192 if (in_vb->flags & V4L2_BUF_FLAG_TIMECODE)
193 out_vb->timecode = in_vb->timecode;
194 out_vb->field = in_vb->field;
195 out_vb->flags &= ~V4L2_BUF_FLAG_LAST;
196 out_vb->flags |= in_vb->flags &
197 (V4L2_BUF_FLAG_TIMECODE |
198 V4L2_BUF_FLAG_KEYFRAME |
199 V4L2_BUF_FLAG_PFRAME |
200 V4L2_BUF_FLAG_BFRAME |
201 V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
202
203 return 0;
204}
205
206/*
207 * mem2mem callbacks
208 */
209
210/* device_run() - prepares and starts the device */
211static void device_run(void *priv)
212{
213 static const struct v4l2_event eos_event = {
214 .type = V4L2_EVENT_EOS
215 };
216 struct vicodec_ctx *ctx = priv;
217 struct vicodec_dev *dev = ctx->dev;
218 struct vb2_v4l2_buffer *src_buf, *dst_buf;
219 struct vicodec_q_data *q_out;
220 u32 state;
221
222 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
223 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
224 q_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
225
226 state = VB2_BUF_STATE_DONE;
227 if (device_process(ctx, src_buf, dst_buf))
228 state = VB2_BUF_STATE_ERROR;
229 ctx->last_dst_buf = dst_buf;
230
231 spin_lock(ctx->lock);
232 if (!ctx->comp_has_next_frame && src_buf == ctx->last_src_buf) {
233 dst_buf->flags |= V4L2_BUF_FLAG_LAST;
234 v4l2_event_queue_fh(&ctx->fh, &eos_event);
235 }
236 if (ctx->is_enc) {
237 src_buf->sequence = q_out->sequence++;
238 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
239 v4l2_m2m_buf_done(src_buf, state);
240 } else if (vb2_get_plane_payload(&src_buf->vb2_buf, 0) == ctx->cur_buf_offset) {
241 src_buf->sequence = q_out->sequence++;
242 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
243 v4l2_m2m_buf_done(src_buf, state);
244 ctx->cur_buf_offset = 0;
245 ctx->comp_has_next_frame = false;
246 }
247 v4l2_m2m_buf_done(dst_buf, state);
248 ctx->comp_size = 0;
249 ctx->comp_magic_cnt = 0;
250 ctx->comp_has_frame = false;
251 spin_unlock(ctx->lock);
252
253 if (ctx->is_enc)
254 v4l2_m2m_job_finish(dev->enc_dev, ctx->fh.m2m_ctx);
255 else
256 v4l2_m2m_job_finish(dev->dec_dev, ctx->fh.m2m_ctx);
257}
258
259static void job_remove_out_buf(struct vicodec_ctx *ctx, u32 state)
260{
261 struct vb2_v4l2_buffer *src_buf;
262 struct vicodec_q_data *q_out;
263
264 q_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
265 spin_lock(ctx->lock);
266 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
267 src_buf->sequence = q_out->sequence++;
268 v4l2_m2m_buf_done(src_buf, state);
269 ctx->cur_buf_offset = 0;
270 spin_unlock(ctx->lock);
271}
272
273static int job_ready(void *priv)
274{
275 static const u8 magic[] = {
276 0x4f, 0x4f, 0x4f, 0x4f, 0xff, 0xff, 0xff, 0xff
277 };
278 struct vicodec_ctx *ctx = priv;
279 struct vb2_v4l2_buffer *src_buf;
280 u8 *p_out;
281 u8 *p;
282 u32 sz;
283 u32 state;
284
285 if (ctx->is_enc || ctx->comp_has_frame)
286 return 1;
287
288restart:
289 ctx->comp_has_next_frame = false;
290 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
291 if (!src_buf)
292 return 0;
293 p_out = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
294 sz = vb2_get_plane_payload(&src_buf->vb2_buf, 0);
295 p = p_out + ctx->cur_buf_offset;
296
297 state = VB2_BUF_STATE_DONE;
298
299 if (!ctx->comp_size) {
300 state = VB2_BUF_STATE_ERROR;
301 for (; p < p_out + sz; p++) {
302 u32 copy;
303
304 p = memchr(p, magic[ctx->comp_magic_cnt], sz);
305 if (!p) {
306 ctx->comp_magic_cnt = 0;
307 break;
308 }
309 copy = sizeof(magic) - ctx->comp_magic_cnt;
310 if (p_out + sz - p < copy)
311 copy = p_out + sz - p;
cd12b401 312 memcpy(ctx->state.compressed_frame + ctx->comp_magic_cnt,
256bf813
HV
313 p, copy);
314 ctx->comp_magic_cnt += copy;
cd12b401
HV
315 if (!memcmp(ctx->state.compressed_frame, magic,
316 ctx->comp_magic_cnt)) {
256bf813
HV
317 p += copy;
318 state = VB2_BUF_STATE_DONE;
319 break;
320 }
321 ctx->comp_magic_cnt = 0;
322 }
323 if (ctx->comp_magic_cnt < sizeof(magic)) {
324 job_remove_out_buf(ctx, state);
325 goto restart;
326 }
327 ctx->comp_size = sizeof(magic);
328 }
21abebf0
HV
329 if (ctx->comp_size < sizeof(struct fwht_cframe_hdr)) {
330 struct fwht_cframe_hdr *p_hdr =
cd12b401 331 (struct fwht_cframe_hdr *)ctx->state.compressed_frame;
21abebf0 332 u32 copy = sizeof(struct fwht_cframe_hdr) - ctx->comp_size;
256bf813
HV
333
334 if (copy > p_out + sz - p)
335 copy = p_out + sz - p;
cd12b401 336 memcpy(ctx->state.compressed_frame + ctx->comp_size,
256bf813
HV
337 p, copy);
338 p += copy;
339 ctx->comp_size += copy;
21abebf0 340 if (ctx->comp_size < sizeof(struct fwht_cframe_hdr)) {
256bf813
HV
341 job_remove_out_buf(ctx, state);
342 goto restart;
343 }
344 ctx->comp_frame_size = ntohl(p_hdr->size) + sizeof(*p_hdr);
345 if (ctx->comp_frame_size > ctx->comp_max_size)
346 ctx->comp_frame_size = ctx->comp_max_size;
347 }
348 if (ctx->comp_size < ctx->comp_frame_size) {
349 u32 copy = ctx->comp_frame_size - ctx->comp_size;
350
351 if (copy > p_out + sz - p)
352 copy = p_out + sz - p;
cd12b401 353 memcpy(ctx->state.compressed_frame + ctx->comp_size,
256bf813
HV
354 p, copy);
355 p += copy;
356 ctx->comp_size += copy;
357 if (ctx->comp_size < ctx->comp_frame_size) {
358 job_remove_out_buf(ctx, state);
359 goto restart;
360 }
361 }
362 ctx->cur_buf_offset = p - p_out;
363 ctx->comp_has_frame = true;
364 ctx->comp_has_next_frame = false;
21abebf0
HV
365 if (sz - ctx->cur_buf_offset >= sizeof(struct fwht_cframe_hdr)) {
366 struct fwht_cframe_hdr *p_hdr = (struct fwht_cframe_hdr *)p;
256bf813
HV
367 u32 frame_size = ntohl(p_hdr->size);
368 u32 remaining = sz - ctx->cur_buf_offset - sizeof(*p_hdr);
369
370 if (!memcmp(p, magic, sizeof(magic)))
371 ctx->comp_has_next_frame = remaining >= frame_size;
372 }
373 return 1;
374}
375
376static void job_abort(void *priv)
377{
378 struct vicodec_ctx *ctx = priv;
379
380 /* Will cancel the transaction in the next interrupt handler */
381 ctx->aborting = 1;
382}
383
384/*
385 * video ioctls
386 */
387
cd12b401 388static const struct v4l2_fwht_pixfmt_info *find_fmt(u32 fmt)
256bf813 389{
cd12b401
HV
390 const struct v4l2_fwht_pixfmt_info *info =
391 v4l2_fwht_find_pixfmt(fmt);
256bf813 392
cd12b401
HV
393 if (!info)
394 info = v4l2_fwht_get_pixfmt(0);
395 return info;
256bf813
HV
396}
397
398static int vidioc_querycap(struct file *file, void *priv,
399 struct v4l2_capability *cap)
400{
401 strncpy(cap->driver, VICODEC_NAME, sizeof(cap->driver) - 1);
402 strncpy(cap->card, VICODEC_NAME, sizeof(cap->card) - 1);
403 snprintf(cap->bus_info, sizeof(cap->bus_info),
404 "platform:%s", VICODEC_NAME);
405 cap->device_caps = V4L2_CAP_STREAMING |
406 (multiplanar ?
407 V4L2_CAP_VIDEO_M2M_MPLANE :
408 V4L2_CAP_VIDEO_M2M);
409 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
410 return 0;
411}
412
413static int enum_fmt(struct v4l2_fmtdesc *f, bool is_enc, bool is_out)
414{
29a7a5e9 415 bool is_uncomp = (is_enc && is_out) || (!is_enc && !is_out);
256bf813
HV
416
417 if (V4L2_TYPE_IS_MULTIPLANAR(f->type) && !multiplanar)
418 return -EINVAL;
419 if (!V4L2_TYPE_IS_MULTIPLANAR(f->type) && multiplanar)
420 return -EINVAL;
256bf813 421
cd12b401
HV
422 if (is_uncomp) {
423 const struct v4l2_fwht_pixfmt_info *info =
424 v4l2_fwht_get_pixfmt(f->index);
425
426 if (!info)
427 return -EINVAL;
428 f->pixelformat = info->id;
429 } else {
430 if (f->index)
431 return -EINVAL;
256bf813 432 f->pixelformat = V4L2_PIX_FMT_FWHT;
cd12b401 433 }
256bf813
HV
434 return 0;
435}
436
437static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
438 struct v4l2_fmtdesc *f)
439{
440 struct vicodec_ctx *ctx = file2ctx(file);
441
442 return enum_fmt(f, ctx->is_enc, false);
443}
444
445static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
446 struct v4l2_fmtdesc *f)
447{
448 struct vicodec_ctx *ctx = file2ctx(file);
449
450 return enum_fmt(f, ctx->is_enc, true);
451}
452
453static int vidioc_g_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f)
454{
455 struct vb2_queue *vq;
456 struct vicodec_q_data *q_data;
457 struct v4l2_pix_format_mplane *pix_mp;
458 struct v4l2_pix_format *pix;
cd12b401 459 const struct v4l2_fwht_pixfmt_info *info;
256bf813
HV
460
461 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
462 if (!vq)
463 return -EINVAL;
464
465 q_data = get_q_data(ctx, f->type);
29a7a5e9 466 info = q_data->info;
256bf813
HV
467
468 switch (f->type) {
469 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
470 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
471 if (multiplanar)
472 return -EINVAL;
473 pix = &f->fmt.pix;
474 pix->width = q_data->width;
475 pix->height = q_data->height;
476 pix->field = V4L2_FIELD_NONE;
29a7a5e9
HV
477 pix->pixelformat = info->id;
478 pix->bytesperline = q_data->width * info->bytesperline_mult;
256bf813 479 pix->sizeimage = q_data->sizeimage;
cd12b401
HV
480 pix->colorspace = ctx->state.colorspace;
481 pix->xfer_func = ctx->state.xfer_func;
482 pix->ycbcr_enc = ctx->state.ycbcr_enc;
483 pix->quantization = ctx->state.quantization;
256bf813
HV
484 break;
485
486 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
487 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
488 if (!multiplanar)
489 return -EINVAL;
490 pix_mp = &f->fmt.pix_mp;
491 pix_mp->width = q_data->width;
492 pix_mp->height = q_data->height;
493 pix_mp->field = V4L2_FIELD_NONE;
29a7a5e9 494 pix_mp->pixelformat = info->id;
256bf813 495 pix_mp->num_planes = 1;
29a7a5e9
HV
496 pix_mp->plane_fmt[0].bytesperline =
497 q_data->width * info->bytesperline_mult;
256bf813 498 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage;
cd12b401
HV
499 pix_mp->colorspace = ctx->state.colorspace;
500 pix_mp->xfer_func = ctx->state.xfer_func;
501 pix_mp->ycbcr_enc = ctx->state.ycbcr_enc;
502 pix_mp->quantization = ctx->state.quantization;
256bf813
HV
503 memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
504 memset(pix_mp->plane_fmt[0].reserved, 0,
505 sizeof(pix_mp->plane_fmt[0].reserved));
506 break;
507 default:
508 return -EINVAL;
509 }
510 return 0;
511}
512
513static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
514 struct v4l2_format *f)
515{
516 return vidioc_g_fmt(file2ctx(file), f);
517}
518
519static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
520 struct v4l2_format *f)
521{
522 return vidioc_g_fmt(file2ctx(file), f);
523}
524
525static int vidioc_try_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f)
526{
527 struct v4l2_pix_format_mplane *pix_mp;
528 struct v4l2_pix_format *pix;
29a7a5e9 529 struct v4l2_plane_pix_format *plane;
cd12b401 530 const struct v4l2_fwht_pixfmt_info *info = &pixfmt_fwht;
256bf813
HV
531
532 switch (f->type) {
533 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
534 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
535 pix = &f->fmt.pix;
29a7a5e9
HV
536 if (pix->pixelformat != V4L2_PIX_FMT_FWHT)
537 info = find_fmt(pix->pixelformat);
256bf813
HV
538 pix->width = clamp(pix->width, MIN_WIDTH, MAX_WIDTH) & ~7;
539 pix->height = clamp(pix->height, MIN_HEIGHT, MAX_HEIGHT) & ~7;
256bf813 540 pix->field = V4L2_FIELD_NONE;
29a7a5e9
HV
541 pix->bytesperline =
542 pix->width * info->bytesperline_mult;
543 pix->sizeimage = pix->width * pix->height *
544 info->sizeimage_mult / info->sizeimage_div;
545 if (pix->pixelformat == V4L2_PIX_FMT_FWHT)
21abebf0 546 pix->sizeimage += sizeof(struct fwht_cframe_hdr);
256bf813
HV
547 break;
548 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
549 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
550 pix_mp = &f->fmt.pix_mp;
29a7a5e9
HV
551 plane = pix_mp->plane_fmt;
552 if (pix_mp->pixelformat != V4L2_PIX_FMT_FWHT)
553 info = find_fmt(pix_mp->pixelformat);
554 pix_mp->num_planes = 1;
256bf813
HV
555 pix_mp->width = clamp(pix_mp->width, MIN_WIDTH, MAX_WIDTH) & ~7;
556 pix_mp->height =
557 clamp(pix_mp->height, MIN_HEIGHT, MAX_HEIGHT) & ~7;
256bf813 558 pix_mp->field = V4L2_FIELD_NONE;
29a7a5e9
HV
559 plane->bytesperline =
560 pix_mp->width * info->bytesperline_mult;
561 plane->sizeimage = pix_mp->width * pix_mp->height *
562 info->sizeimage_mult / info->sizeimage_div;
563 if (pix_mp->pixelformat == V4L2_PIX_FMT_FWHT)
21abebf0 564 plane->sizeimage += sizeof(struct fwht_cframe_hdr);
256bf813 565 memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
29a7a5e9 566 memset(plane->reserved, 0, sizeof(plane->reserved));
256bf813
HV
567 break;
568 default:
569 return -EINVAL;
570 }
571
572 return 0;
573}
574
575static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
576 struct v4l2_format *f)
577{
578 struct vicodec_ctx *ctx = file2ctx(file);
579 struct v4l2_pix_format_mplane *pix_mp;
580 struct v4l2_pix_format *pix;
581
582 switch (f->type) {
583 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
584 if (multiplanar)
585 return -EINVAL;
586 pix = &f->fmt.pix;
587 pix->pixelformat = ctx->is_enc ? V4L2_PIX_FMT_FWHT :
29a7a5e9 588 find_fmt(f->fmt.pix.pixelformat)->id;
cd12b401
HV
589 pix->colorspace = ctx->state.colorspace;
590 pix->xfer_func = ctx->state.xfer_func;
591 pix->ycbcr_enc = ctx->state.ycbcr_enc;
592 pix->quantization = ctx->state.quantization;
256bf813
HV
593 break;
594 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
595 if (!multiplanar)
596 return -EINVAL;
597 pix_mp = &f->fmt.pix_mp;
598 pix_mp->pixelformat = ctx->is_enc ? V4L2_PIX_FMT_FWHT :
29a7a5e9 599 find_fmt(pix_mp->pixelformat)->id;
cd12b401
HV
600 pix_mp->colorspace = ctx->state.colorspace;
601 pix_mp->xfer_func = ctx->state.xfer_func;
602 pix_mp->ycbcr_enc = ctx->state.ycbcr_enc;
603 pix_mp->quantization = ctx->state.quantization;
256bf813
HV
604 break;
605 default:
606 return -EINVAL;
607 }
608
609 return vidioc_try_fmt(ctx, f);
610}
611
612static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
613 struct v4l2_format *f)
614{
615 struct vicodec_ctx *ctx = file2ctx(file);
616 struct v4l2_pix_format_mplane *pix_mp;
617 struct v4l2_pix_format *pix;
618
619 switch (f->type) {
620 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
621 if (multiplanar)
622 return -EINVAL;
623 pix = &f->fmt.pix;
624 pix->pixelformat = !ctx->is_enc ? V4L2_PIX_FMT_FWHT :
29a7a5e9 625 find_fmt(pix->pixelformat)->id;
256bf813
HV
626 if (!pix->colorspace)
627 pix->colorspace = V4L2_COLORSPACE_REC709;
628 break;
629 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
630 if (!multiplanar)
631 return -EINVAL;
632 pix_mp = &f->fmt.pix_mp;
633 pix_mp->pixelformat = !ctx->is_enc ? V4L2_PIX_FMT_FWHT :
29a7a5e9 634 find_fmt(pix_mp->pixelformat)->id;
256bf813
HV
635 if (!pix_mp->colorspace)
636 pix_mp->colorspace = V4L2_COLORSPACE_REC709;
637 break;
638 default:
639 return -EINVAL;
640 }
641
642 return vidioc_try_fmt(ctx, f);
643}
644
645static int vidioc_s_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f)
646{
647 struct vicodec_q_data *q_data;
648 struct vb2_queue *vq;
649 bool fmt_changed = true;
650 struct v4l2_pix_format_mplane *pix_mp;
651 struct v4l2_pix_format *pix;
652
653 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
654 if (!vq)
655 return -EINVAL;
656
657 q_data = get_q_data(ctx, f->type);
658 if (!q_data)
659 return -EINVAL;
660
661 switch (f->type) {
662 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
663 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
664 pix = &f->fmt.pix;
665 if (ctx->is_enc && V4L2_TYPE_IS_OUTPUT(f->type))
666 fmt_changed =
29a7a5e9 667 q_data->info->id != pix->pixelformat ||
256bf813
HV
668 q_data->width != pix->width ||
669 q_data->height != pix->height;
670
671 if (vb2_is_busy(vq) && fmt_changed)
672 return -EBUSY;
673
29a7a5e9
HV
674 if (pix->pixelformat == V4L2_PIX_FMT_FWHT)
675 q_data->info = &pixfmt_fwht;
676 else
677 q_data->info = find_fmt(pix->pixelformat);
256bf813
HV
678 q_data->width = pix->width;
679 q_data->height = pix->height;
680 q_data->sizeimage = pix->sizeimage;
681 break;
682 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
683 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
684 pix_mp = &f->fmt.pix_mp;
685 if (ctx->is_enc && V4L2_TYPE_IS_OUTPUT(f->type))
686 fmt_changed =
29a7a5e9 687 q_data->info->id != pix_mp->pixelformat ||
256bf813
HV
688 q_data->width != pix_mp->width ||
689 q_data->height != pix_mp->height;
690
691 if (vb2_is_busy(vq) && fmt_changed)
692 return -EBUSY;
693
29a7a5e9
HV
694 if (pix_mp->pixelformat == V4L2_PIX_FMT_FWHT)
695 q_data->info = &pixfmt_fwht;
696 else
697 q_data->info = find_fmt(pix_mp->pixelformat);
256bf813
HV
698 q_data->width = pix_mp->width;
699 q_data->height = pix_mp->height;
700 q_data->sizeimage = pix_mp->plane_fmt[0].sizeimage;
701 break;
702 default:
703 return -EINVAL;
704 }
705
706 dprintk(ctx->dev,
707 "Setting format for type %d, wxh: %dx%d, fourcc: %08x\n",
29a7a5e9 708 f->type, q_data->width, q_data->height, q_data->info->id);
256bf813
HV
709
710 return 0;
711}
712
713static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
714 struct v4l2_format *f)
715{
716 int ret;
717
718 ret = vidioc_try_fmt_vid_cap(file, priv, f);
719 if (ret)
720 return ret;
721
722 return vidioc_s_fmt(file2ctx(file), f);
723}
724
725static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
726 struct v4l2_format *f)
727{
728 struct vicodec_ctx *ctx = file2ctx(file);
729 struct v4l2_pix_format_mplane *pix_mp;
730 struct v4l2_pix_format *pix;
731 int ret;
732
733 ret = vidioc_try_fmt_vid_out(file, priv, f);
734 if (ret)
735 return ret;
736
737 ret = vidioc_s_fmt(file2ctx(file), f);
738 if (!ret) {
739 switch (f->type) {
740 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
741 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
742 pix = &f->fmt.pix;
cd12b401
HV
743 ctx->state.colorspace = pix->colorspace;
744 ctx->state.xfer_func = pix->xfer_func;
745 ctx->state.ycbcr_enc = pix->ycbcr_enc;
746 ctx->state.quantization = pix->quantization;
256bf813
HV
747 break;
748 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
749 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
750 pix_mp = &f->fmt.pix_mp;
cd12b401
HV
751 ctx->state.colorspace = pix_mp->colorspace;
752 ctx->state.xfer_func = pix_mp->xfer_func;
753 ctx->state.ycbcr_enc = pix_mp->ycbcr_enc;
754 ctx->state.quantization = pix_mp->quantization;
256bf813
HV
755 break;
756 default:
757 break;
758 }
759 }
760 return ret;
761}
762
763static void vicodec_mark_last_buf(struct vicodec_ctx *ctx)
764{
765 static const struct v4l2_event eos_event = {
766 .type = V4L2_EVENT_EOS
767 };
768
769 spin_lock(ctx->lock);
770 ctx->last_src_buf = v4l2_m2m_last_src_buf(ctx->fh.m2m_ctx);
771 if (!ctx->last_src_buf && ctx->last_dst_buf) {
772 ctx->last_dst_buf->flags |= V4L2_BUF_FLAG_LAST;
773 v4l2_event_queue_fh(&ctx->fh, &eos_event);
774 }
775 spin_unlock(ctx->lock);
776}
777
778static int vicodec_try_encoder_cmd(struct file *file, void *fh,
779 struct v4l2_encoder_cmd *ec)
780{
781 if (ec->cmd != V4L2_ENC_CMD_STOP)
782 return -EINVAL;
783
784 if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END)
785 return -EINVAL;
786
787 return 0;
788}
789
790static int vicodec_encoder_cmd(struct file *file, void *fh,
791 struct v4l2_encoder_cmd *ec)
792{
793 struct vicodec_ctx *ctx = file2ctx(file);
794 int ret;
795
796 ret = vicodec_try_encoder_cmd(file, fh, ec);
797 if (ret < 0)
798 return ret;
799
800 vicodec_mark_last_buf(ctx);
801 return 0;
802}
803
804static int vicodec_try_decoder_cmd(struct file *file, void *fh,
805 struct v4l2_decoder_cmd *dc)
806{
807 if (dc->cmd != V4L2_DEC_CMD_STOP)
808 return -EINVAL;
809
810 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK)
811 return -EINVAL;
812
813 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0))
814 return -EINVAL;
815
816 return 0;
817}
818
819static int vicodec_decoder_cmd(struct file *file, void *fh,
820 struct v4l2_decoder_cmd *dc)
821{
822 struct vicodec_ctx *ctx = file2ctx(file);
823 int ret;
824
825 ret = vicodec_try_decoder_cmd(file, fh, dc);
826 if (ret < 0)
827 return ret;
828
829 vicodec_mark_last_buf(ctx);
830 return 0;
831}
832
833static int vicodec_enum_framesizes(struct file *file, void *fh,
834 struct v4l2_frmsizeenum *fsize)
835{
836 switch (fsize->pixel_format) {
837 case V4L2_PIX_FMT_FWHT:
838 break;
839 default:
29a7a5e9 840 if (find_fmt(fsize->pixel_format)->id == fsize->pixel_format)
256bf813
HV
841 break;
842 return -EINVAL;
843 }
844
845 if (fsize->index)
846 return -EINVAL;
847
848 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
849
850 fsize->stepwise.min_width = MIN_WIDTH;
851 fsize->stepwise.max_width = MAX_WIDTH;
852 fsize->stepwise.step_width = 8;
853 fsize->stepwise.min_height = MIN_HEIGHT;
854 fsize->stepwise.max_height = MAX_HEIGHT;
855 fsize->stepwise.step_height = 8;
856
857 return 0;
858}
859
860static int vicodec_subscribe_event(struct v4l2_fh *fh,
861 const struct v4l2_event_subscription *sub)
862{
863 switch (sub->type) {
864 case V4L2_EVENT_EOS:
865 return v4l2_event_subscribe(fh, sub, 0, NULL);
866 default:
867 return v4l2_ctrl_subscribe_event(fh, sub);
868 }
869}
870
871static const struct v4l2_ioctl_ops vicodec_ioctl_ops = {
872 .vidioc_querycap = vidioc_querycap,
873
874 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
875 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
876 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
877 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
878
879 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap,
880 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_vid_cap,
881 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap,
882 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_vid_cap,
883
884 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
885 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
886 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
887 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
888
889 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out,
890 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_vid_out,
891 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out,
892 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_vid_out,
893
894 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
895 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
896 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
897 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
898 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
899 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
900 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
901
902 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
903 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
904
905 .vidioc_try_encoder_cmd = vicodec_try_encoder_cmd,
906 .vidioc_encoder_cmd = vicodec_encoder_cmd,
907 .vidioc_try_decoder_cmd = vicodec_try_decoder_cmd,
908 .vidioc_decoder_cmd = vicodec_decoder_cmd,
909 .vidioc_enum_framesizes = vicodec_enum_framesizes,
910
911 .vidioc_subscribe_event = vicodec_subscribe_event,
912 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
913};
914
915
916/*
917 * Queue operations
918 */
919
920static int vicodec_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
921 unsigned int *nplanes, unsigned int sizes[],
922 struct device *alloc_devs[])
923{
924 struct vicodec_ctx *ctx = vb2_get_drv_priv(vq);
925 struct vicodec_q_data *q_data = get_q_data(ctx, vq->type);
926 unsigned int size = q_data->sizeimage;
927
928 if (*nplanes)
929 return sizes[0] < size ? -EINVAL : 0;
930
931 *nplanes = 1;
932 sizes[0] = size;
933 return 0;
934}
935
936static int vicodec_buf_prepare(struct vb2_buffer *vb)
937{
938 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
939 struct vicodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
940 struct vicodec_q_data *q_data;
941
942 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
943
944 q_data = get_q_data(ctx, vb->vb2_queue->type);
945 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
946 if (vbuf->field == V4L2_FIELD_ANY)
947 vbuf->field = V4L2_FIELD_NONE;
948 if (vbuf->field != V4L2_FIELD_NONE) {
949 dprintk(ctx->dev, "%s field isn't supported\n",
950 __func__);
951 return -EINVAL;
952 }
953 }
954
955 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
956 dprintk(ctx->dev,
957 "%s data will not fit into plane (%lu < %lu)\n",
958 __func__, vb2_plane_size(vb, 0),
959 (long)q_data->sizeimage);
960 return -EINVAL;
961 }
962
963 return 0;
964}
965
966static void vicodec_buf_queue(struct vb2_buffer *vb)
967{
968 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
969 struct vicodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
970
971 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
972}
973
974static void vicodec_return_bufs(struct vb2_queue *q, u32 state)
975{
976 struct vicodec_ctx *ctx = vb2_get_drv_priv(q);
977 struct vb2_v4l2_buffer *vbuf;
978
979 for (;;) {
980 if (V4L2_TYPE_IS_OUTPUT(q->type))
981 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
982 else
983 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
984 if (vbuf == NULL)
985 return;
986 spin_lock(ctx->lock);
987 v4l2_m2m_buf_done(vbuf, state);
988 spin_unlock(ctx->lock);
989 }
990}
991
992static int vicodec_start_streaming(struct vb2_queue *q,
993 unsigned int count)
994{
995 struct vicodec_ctx *ctx = vb2_get_drv_priv(q);
996 struct vicodec_q_data *q_data = get_q_data(ctx, q->type);
cd12b401 997 struct v4l2_fwht_state *state = &ctx->state;
256bf813 998 unsigned int size = q_data->width * q_data->height;
cd12b401 999 const struct v4l2_fwht_pixfmt_info *info = q_data->info;
29a7a5e9 1000 unsigned int chroma_div = info->width_div * info->height_div;
256bf813
HV
1001
1002 q_data->sequence = 0;
1003
1004 if (!V4L2_TYPE_IS_OUTPUT(q->type))
1005 return 0;
1006
cd12b401
HV
1007 state->width = q_data->width;
1008 state->height = q_data->height;
1009 state->ref_frame.width = state->ref_frame.height = 0;
1010 state->ref_frame.luma = kvmalloc(size + 2 * size / chroma_div,
1011 GFP_KERNEL);
29a7a5e9 1012 ctx->comp_max_size = size + 2 * size / chroma_div +
21abebf0 1013 sizeof(struct fwht_cframe_hdr);
cd12b401
HV
1014 state->compressed_frame = kvmalloc(ctx->comp_max_size, GFP_KERNEL);
1015 if (!state->ref_frame.luma || !state->compressed_frame) {
1016 kvfree(state->ref_frame.luma);
1017 kvfree(state->compressed_frame);
256bf813
HV
1018 vicodec_return_bufs(q, VB2_BUF_STATE_QUEUED);
1019 return -ENOMEM;
1020 }
cd12b401
HV
1021 state->ref_frame.cb = state->ref_frame.luma + size;
1022 state->ref_frame.cr = state->ref_frame.cb + size / chroma_div;
256bf813
HV
1023 ctx->last_src_buf = NULL;
1024 ctx->last_dst_buf = NULL;
cd12b401 1025 state->gop_cnt = 0;
256bf813
HV
1026 ctx->cur_buf_offset = 0;
1027 ctx->comp_size = 0;
1028 ctx->comp_magic_cnt = 0;
1029 ctx->comp_has_frame = false;
1030
1031 return 0;
1032}
1033
1034static void vicodec_stop_streaming(struct vb2_queue *q)
1035{
1036 struct vicodec_ctx *ctx = vb2_get_drv_priv(q);
1037
1038 vicodec_return_bufs(q, VB2_BUF_STATE_ERROR);
1039
1040 if (!V4L2_TYPE_IS_OUTPUT(q->type))
1041 return;
1042
cd12b401
HV
1043 kvfree(ctx->state.ref_frame.luma);
1044 kvfree(ctx->state.compressed_frame);
256bf813
HV
1045}
1046
1047static const struct vb2_ops vicodec_qops = {
1048 .queue_setup = vicodec_queue_setup,
1049 .buf_prepare = vicodec_buf_prepare,
1050 .buf_queue = vicodec_buf_queue,
1051 .start_streaming = vicodec_start_streaming,
1052 .stop_streaming = vicodec_stop_streaming,
1053 .wait_prepare = vb2_ops_wait_prepare,
1054 .wait_finish = vb2_ops_wait_finish,
1055};
1056
1057static int queue_init(void *priv, struct vb2_queue *src_vq,
1058 struct vb2_queue *dst_vq)
1059{
1060 struct vicodec_ctx *ctx = priv;
1061 int ret;
1062
1063 src_vq->type = (multiplanar ?
1064 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1065 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1066 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1067 src_vq->drv_priv = ctx;
1068 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1069 src_vq->ops = &vicodec_qops;
1070 src_vq->mem_ops = &vb2_vmalloc_memops;
1071 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1072 src_vq->lock = ctx->is_enc ? &ctx->dev->enc_mutex :
1073 &ctx->dev->dec_mutex;
1074
1075 ret = vb2_queue_init(src_vq);
1076 if (ret)
1077 return ret;
1078
1079 dst_vq->type = (multiplanar ?
1080 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1081 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1082 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1083 dst_vq->drv_priv = ctx;
1084 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1085 dst_vq->ops = &vicodec_qops;
1086 dst_vq->mem_ops = &vb2_vmalloc_memops;
1087 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1088 dst_vq->lock = src_vq->lock;
1089
1090 return vb2_queue_init(dst_vq);
1091}
1092
48568b0c
HV
1093#define VICODEC_CID_CUSTOM_BASE (V4L2_CID_MPEG_BASE | 0xf000)
1094#define VICODEC_CID_I_FRAME_QP (VICODEC_CID_CUSTOM_BASE + 0)
1095#define VICODEC_CID_P_FRAME_QP (VICODEC_CID_CUSTOM_BASE + 1)
1096
1097static int vicodec_s_ctrl(struct v4l2_ctrl *ctrl)
1098{
1099 struct vicodec_ctx *ctx = container_of(ctrl->handler,
1100 struct vicodec_ctx, hdl);
1101
1102 switch (ctrl->id) {
1103 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
cd12b401 1104 ctx->state.gop_size = ctrl->val;
48568b0c
HV
1105 return 0;
1106 case VICODEC_CID_I_FRAME_QP:
cd12b401 1107 ctx->state.i_frame_qp = ctrl->val;
48568b0c
HV
1108 return 0;
1109 case VICODEC_CID_P_FRAME_QP:
cd12b401 1110 ctx->state.p_frame_qp = ctrl->val;
48568b0c
HV
1111 return 0;
1112 }
1113 return -EINVAL;
1114}
1115
cd12b401 1116static struct v4l2_ctrl_ops vicodec_ctrl_ops = {
48568b0c
HV
1117 .s_ctrl = vicodec_s_ctrl,
1118};
1119
1120static const struct v4l2_ctrl_config vicodec_ctrl_i_frame = {
1121 .ops = &vicodec_ctrl_ops,
1122 .id = VICODEC_CID_I_FRAME_QP,
1123 .name = "FWHT I-Frame QP Value",
1124 .type = V4L2_CTRL_TYPE_INTEGER,
1125 .min = 1,
1126 .max = 31,
1127 .def = 20,
1128 .step = 1,
1129};
1130
1131static const struct v4l2_ctrl_config vicodec_ctrl_p_frame = {
1132 .ops = &vicodec_ctrl_ops,
1133 .id = VICODEC_CID_P_FRAME_QP,
1134 .name = "FWHT P-Frame QP Value",
1135 .type = V4L2_CTRL_TYPE_INTEGER,
1136 .min = 1,
1137 .max = 31,
1138 .def = 20,
1139 .step = 1,
1140};
1141
256bf813
HV
1142/*
1143 * File operations
1144 */
1145static int vicodec_open(struct file *file)
1146{
1147 struct video_device *vfd = video_devdata(file);
1148 struct vicodec_dev *dev = video_drvdata(file);
1149 struct vicodec_ctx *ctx = NULL;
1150 struct v4l2_ctrl_handler *hdl;
1151 unsigned int size;
1152 int rc = 0;
1153
1154 if (mutex_lock_interruptible(vfd->lock))
1155 return -ERESTARTSYS;
1156 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1157 if (!ctx) {
1158 rc = -ENOMEM;
1159 goto open_unlock;
1160 }
1161
1162 if (vfd == &dev->enc_vfd)
1163 ctx->is_enc = true;
1164
1165 v4l2_fh_init(&ctx->fh, video_devdata(file));
1166 file->private_data = &ctx->fh;
1167 ctx->dev = dev;
1168 hdl = &ctx->hdl;
1169 v4l2_ctrl_handler_init(hdl, 4);
48568b0c
HV
1170 v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
1171 1, 16, 1, 10);
1172 v4l2_ctrl_new_custom(hdl, &vicodec_ctrl_i_frame, NULL);
1173 v4l2_ctrl_new_custom(hdl, &vicodec_ctrl_p_frame, NULL);
256bf813
HV
1174 if (hdl->error) {
1175 rc = hdl->error;
1176 v4l2_ctrl_handler_free(hdl);
1177 kfree(ctx);
1178 goto open_unlock;
1179 }
1180 ctx->fh.ctrl_handler = hdl;
1181 v4l2_ctrl_handler_setup(hdl);
1182
29a7a5e9 1183 ctx->q_data[V4L2_M2M_SRC].info =
cd12b401 1184 ctx->is_enc ? v4l2_fwht_get_pixfmt(0) : &pixfmt_fwht;
256bf813
HV
1185 ctx->q_data[V4L2_M2M_SRC].width = 1280;
1186 ctx->q_data[V4L2_M2M_SRC].height = 720;
29a7a5e9
HV
1187 size = 1280 * 720 * ctx->q_data[V4L2_M2M_SRC].info->sizeimage_mult /
1188 ctx->q_data[V4L2_M2M_SRC].info->sizeimage_div;
256bf813
HV
1189 ctx->q_data[V4L2_M2M_SRC].sizeimage = size;
1190 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
29a7a5e9 1191 ctx->q_data[V4L2_M2M_DST].info =
cd12b401 1192 ctx->is_enc ? &pixfmt_fwht : v4l2_fwht_get_pixfmt(0);
29a7a5e9
HV
1193 size = 1280 * 720 * ctx->q_data[V4L2_M2M_DST].info->sizeimage_mult /
1194 ctx->q_data[V4L2_M2M_DST].info->sizeimage_div;
1195 ctx->q_data[V4L2_M2M_DST].sizeimage = size;
cd12b401 1196 ctx->state.colorspace = V4L2_COLORSPACE_REC709;
256bf813 1197
21abebf0 1198 size += sizeof(struct fwht_cframe_hdr);
256bf813
HV
1199 if (ctx->is_enc) {
1200 ctx->q_data[V4L2_M2M_DST].sizeimage = size;
1201 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->enc_dev, ctx,
1202 &queue_init);
1203 ctx->lock = &dev->enc_lock;
1204 } else {
1205 ctx->q_data[V4L2_M2M_SRC].sizeimage = size;
1206 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->dec_dev, ctx,
1207 &queue_init);
1208 ctx->lock = &dev->dec_lock;
1209 }
1210
1211 if (IS_ERR(ctx->fh.m2m_ctx)) {
1212 rc = PTR_ERR(ctx->fh.m2m_ctx);
1213
1214 v4l2_ctrl_handler_free(hdl);
1215 v4l2_fh_exit(&ctx->fh);
1216 kfree(ctx);
1217 goto open_unlock;
1218 }
1219
1220 v4l2_fh_add(&ctx->fh);
1221
1222open_unlock:
1223 mutex_unlock(vfd->lock);
1224 return rc;
1225}
1226
1227static int vicodec_release(struct file *file)
1228{
1229 struct video_device *vfd = video_devdata(file);
1230 struct vicodec_ctx *ctx = file2ctx(file);
1231
1232 v4l2_fh_del(&ctx->fh);
1233 v4l2_fh_exit(&ctx->fh);
1234 v4l2_ctrl_handler_free(&ctx->hdl);
1235 mutex_lock(vfd->lock);
1236 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1237 mutex_unlock(vfd->lock);
1238 kfree(ctx);
1239
1240 return 0;
1241}
1242
1243static const struct v4l2_file_operations vicodec_fops = {
1244 .owner = THIS_MODULE,
1245 .open = vicodec_open,
1246 .release = vicodec_release,
1247 .poll = v4l2_m2m_fop_poll,
1248 .unlocked_ioctl = video_ioctl2,
1249 .mmap = v4l2_m2m_fop_mmap,
1250};
1251
1252static const struct video_device vicodec_videodev = {
1253 .name = VICODEC_NAME,
1254 .vfl_dir = VFL_DIR_M2M,
1255 .fops = &vicodec_fops,
1256 .ioctl_ops = &vicodec_ioctl_ops,
1257 .minor = -1,
1258 .release = video_device_release_empty,
1259};
1260
1261static const struct v4l2_m2m_ops m2m_ops = {
1262 .device_run = device_run,
1263 .job_abort = job_abort,
1264 .job_ready = job_ready,
1265};
1266
1267static int vicodec_probe(struct platform_device *pdev)
1268{
1269 struct vicodec_dev *dev;
1270 struct video_device *vfd;
1271 int ret;
1272
1273 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
1274 if (!dev)
1275 return -ENOMEM;
1276
1277 spin_lock_init(&dev->enc_lock);
1278 spin_lock_init(&dev->dec_lock);
1279
1280 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1281 if (ret)
1282 return ret;
1283
1284#ifdef CONFIG_MEDIA_CONTROLLER
1285 dev->mdev.dev = &pdev->dev;
1286 strlcpy(dev->mdev.model, "vicodec", sizeof(dev->mdev.model));
1287 media_device_init(&dev->mdev);
1288 dev->v4l2_dev.mdev = &dev->mdev;
1289#endif
1290
1291 mutex_init(&dev->enc_mutex);
1292 mutex_init(&dev->dec_mutex);
1293
1294 platform_set_drvdata(pdev, dev);
1295
1296 dev->enc_dev = v4l2_m2m_init(&m2m_ops);
1297 if (IS_ERR(dev->enc_dev)) {
1298 v4l2_err(&dev->v4l2_dev, "Failed to init vicodec device\n");
1299 ret = PTR_ERR(dev->enc_dev);
1300 goto unreg_dev;
1301 }
1302
1303 dev->dec_dev = v4l2_m2m_init(&m2m_ops);
1304 if (IS_ERR(dev->dec_dev)) {
1305 v4l2_err(&dev->v4l2_dev, "Failed to init vicodec device\n");
1306 ret = PTR_ERR(dev->dec_dev);
1307 goto err_enc_m2m;
1308 }
1309
1310 dev->enc_vfd = vicodec_videodev;
1311 vfd = &dev->enc_vfd;
1312 vfd->lock = &dev->enc_mutex;
1313 vfd->v4l2_dev = &dev->v4l2_dev;
1314 strlcpy(vfd->name, "vicodec-enc", sizeof(vfd->name));
1315 v4l2_disable_ioctl(vfd, VIDIOC_DECODER_CMD);
1316 v4l2_disable_ioctl(vfd, VIDIOC_TRY_DECODER_CMD);
1317 video_set_drvdata(vfd, dev);
1318
1319 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1320 if (ret) {
1321 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1322 goto err_dec_m2m;
1323 }
1324 v4l2_info(&dev->v4l2_dev,
1325 "Device registered as /dev/video%d\n", vfd->num);
1326
1327 dev->dec_vfd = vicodec_videodev;
1328 vfd = &dev->dec_vfd;
1329 vfd->lock = &dev->dec_mutex;
1330 vfd->v4l2_dev = &dev->v4l2_dev;
1331 strlcpy(vfd->name, "vicodec-dec", sizeof(vfd->name));
1332 v4l2_disable_ioctl(vfd, VIDIOC_ENCODER_CMD);
1333 v4l2_disable_ioctl(vfd, VIDIOC_TRY_ENCODER_CMD);
1334 video_set_drvdata(vfd, dev);
1335
1336 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1337 if (ret) {
1338 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1339 goto unreg_enc;
1340 }
1341 v4l2_info(&dev->v4l2_dev,
1342 "Device registered as /dev/video%d\n", vfd->num);
1343
1344#ifdef CONFIG_MEDIA_CONTROLLER
1345 ret = v4l2_m2m_register_media_controller(dev->enc_dev,
1346 &dev->enc_vfd, MEDIA_ENT_F_PROC_VIDEO_ENCODER);
1347 if (ret) {
1348 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
1349 goto unreg_m2m;
1350 }
1351
1352 ret = v4l2_m2m_register_media_controller(dev->dec_dev,
1353 &dev->dec_vfd, MEDIA_ENT_F_PROC_VIDEO_DECODER);
1354 if (ret) {
1355 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
1356 goto unreg_m2m_enc_mc;
1357 }
1358
1359 ret = media_device_register(&dev->mdev);
1360 if (ret) {
1361 v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
1362 goto unreg_m2m_dec_mc;
1363 }
1364#endif
1365 return 0;
1366
1367#ifdef CONFIG_MEDIA_CONTROLLER
1368unreg_m2m_dec_mc:
1369 v4l2_m2m_unregister_media_controller(dev->dec_dev);
1370unreg_m2m_enc_mc:
1371 v4l2_m2m_unregister_media_controller(dev->enc_dev);
1372unreg_m2m:
1373 video_unregister_device(&dev->dec_vfd);
1374#endif
1375unreg_enc:
1376 video_unregister_device(&dev->enc_vfd);
1377err_dec_m2m:
1378 v4l2_m2m_release(dev->dec_dev);
1379err_enc_m2m:
1380 v4l2_m2m_release(dev->enc_dev);
1381unreg_dev:
1382 v4l2_device_unregister(&dev->v4l2_dev);
1383
1384 return ret;
1385}
1386
1387static int vicodec_remove(struct platform_device *pdev)
1388{
1389 struct vicodec_dev *dev = platform_get_drvdata(pdev);
1390
1391 v4l2_info(&dev->v4l2_dev, "Removing " VICODEC_NAME);
1392
1393#ifdef CONFIG_MEDIA_CONTROLLER
1394 media_device_unregister(&dev->mdev);
1395 v4l2_m2m_unregister_media_controller(dev->enc_dev);
1396 v4l2_m2m_unregister_media_controller(dev->dec_dev);
1397 media_device_cleanup(&dev->mdev);
1398#endif
1399
1400 v4l2_m2m_release(dev->enc_dev);
1401 v4l2_m2m_release(dev->dec_dev);
1402 video_unregister_device(&dev->enc_vfd);
1403 video_unregister_device(&dev->dec_vfd);
1404 v4l2_device_unregister(&dev->v4l2_dev);
1405
1406 return 0;
1407}
1408
1409static struct platform_driver vicodec_pdrv = {
1410 .probe = vicodec_probe,
1411 .remove = vicodec_remove,
1412 .driver = {
1413 .name = VICODEC_NAME,
1414 },
1415};
1416
1417static void __exit vicodec_exit(void)
1418{
1419 platform_driver_unregister(&vicodec_pdrv);
1420 platform_device_unregister(&vicodec_pdev);
1421}
1422
1423static int __init vicodec_init(void)
1424{
1425 int ret;
1426
1427 ret = platform_device_register(&vicodec_pdev);
1428 if (ret)
1429 return ret;
1430
1431 ret = platform_driver_register(&vicodec_pdrv);
1432 if (ret)
1433 platform_device_unregister(&vicodec_pdev);
1434
1435 return ret;
1436}
1437
1438module_init(vicodec_init);
1439module_exit(vicodec_exit);