]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/media/platform/exynos-gsc/gsc-m2m.c
[media] media:gspca:dtcs033 Clean sparse check warnings on endianess
[mirror_ubuntu-eoan-kernel.git] / drivers / media / platform / exynos-gsc / gsc-m2m.c
CommitLineData
5d718338
SK
1/*
2 * Copyright (c) 2011 - 2012 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
4 *
5 * Samsung EXYNOS5 SoC series G-Scaler driver
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 2 of the License,
10 * or (at your option) any later version.
11 */
12
13#include <linux/module.h>
14#include <linux/kernel.h>
5d718338
SK
15#include <linux/types.h>
16#include <linux/errno.h>
17#include <linux/bug.h>
18#include <linux/interrupt.h>
19#include <linux/workqueue.h>
20#include <linux/device.h>
21#include <linux/platform_device.h>
22#include <linux/list.h>
23#include <linux/io.h>
24#include <linux/slab.h>
25#include <linux/clk.h>
26
27#include <media/v4l2-ioctl.h>
28
29#include "gsc-core.h"
30
31static int gsc_m2m_ctx_stop_req(struct gsc_ctx *ctx)
32{
33 struct gsc_ctx *curr_ctx;
34 struct gsc_dev *gsc = ctx->gsc_dev;
35 int ret;
36
37 curr_ctx = v4l2_m2m_get_curr_priv(gsc->m2m.m2m_dev);
38 if (!gsc_m2m_pending(gsc) || (curr_ctx != ctx))
39 return 0;
40
41 gsc_ctx_state_lock_set(GSC_CTX_STOP_REQ, ctx);
42 ret = wait_event_timeout(gsc->irq_queue,
43 !gsc_ctx_state_is_set(GSC_CTX_STOP_REQ, ctx),
44 GSC_SHUTDOWN_TIMEOUT);
45
46 return ret == 0 ? -ETIMEDOUT : ret;
47}
48
d9315160
SAB
49static void __gsc_m2m_job_abort(struct gsc_ctx *ctx)
50{
51 int ret;
52
53 ret = gsc_m2m_ctx_stop_req(ctx);
54 if ((ret == -ETIMEDOUT) || (ctx->state & GSC_CTX_ABORT)) {
55 gsc_ctx_state_lock_clear(GSC_CTX_STOP_REQ | GSC_CTX_ABORT, ctx);
56 gsc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
57 }
58}
59
5d718338
SK
60static int gsc_m2m_start_streaming(struct vb2_queue *q, unsigned int count)
61{
62 struct gsc_ctx *ctx = q->drv_priv;
63 int ret;
64
65 ret = pm_runtime_get_sync(&ctx->gsc_dev->pdev->dev);
66 return ret > 0 ? 0 : ret;
67}
68
69static int gsc_m2m_stop_streaming(struct vb2_queue *q)
70{
71 struct gsc_ctx *ctx = q->drv_priv;
5d718338 72
d9315160 73 __gsc_m2m_job_abort(ctx);
5d718338
SK
74
75 pm_runtime_put(&ctx->gsc_dev->pdev->dev);
76
77 return 0;
78}
79
80void gsc_m2m_job_finish(struct gsc_ctx *ctx, int vb_state)
81{
82 struct vb2_buffer *src_vb, *dst_vb;
83
84 if (!ctx || !ctx->m2m_ctx)
85 return;
86
87 src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
88 dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
89
90 if (src_vb && dst_vb) {
599b0892
SA
91 dst_vb->v4l2_buf.timestamp = src_vb->v4l2_buf.timestamp;
92 dst_vb->v4l2_buf.timecode = src_vb->v4l2_buf.timecode;
309f4d62
SA
93 dst_vb->v4l2_buf.flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
94 dst_vb->v4l2_buf.flags |=
95 src_vb->v4l2_buf.flags
96 & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
9c303ec6 97
5d718338
SK
98 v4l2_m2m_buf_done(src_vb, vb_state);
99 v4l2_m2m_buf_done(dst_vb, vb_state);
100
101 v4l2_m2m_job_finish(ctx->gsc_dev->m2m.m2m_dev,
102 ctx->m2m_ctx);
103 }
104}
105
5d718338
SK
106static void gsc_m2m_job_abort(void *priv)
107{
d9315160 108 __gsc_m2m_job_abort((struct gsc_ctx *)priv);
5d718338
SK
109}
110
f60e160e 111static int gsc_get_bufs(struct gsc_ctx *ctx)
5d718338
SK
112{
113 struct gsc_frame *s_frame, *d_frame;
f60e160e 114 struct vb2_buffer *src_vb, *dst_vb;
5d718338
SK
115 int ret;
116
117 s_frame = &ctx->s_frame;
118 d_frame = &ctx->d_frame;
119
f60e160e
SAB
120 src_vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
121 ret = gsc_prepare_addr(ctx, src_vb, s_frame, &s_frame->addr);
122 if (ret)
123 return ret;
124
125 dst_vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
126 ret = gsc_prepare_addr(ctx, dst_vb, d_frame, &d_frame->addr);
5d718338
SK
127 if (ret)
128 return ret;
129
f60e160e
SAB
130 dst_vb->v4l2_buf.timestamp = src_vb->v4l2_buf.timestamp;
131
132 return 0;
5d718338
SK
133}
134
135static void gsc_m2m_device_run(void *priv)
136{
137 struct gsc_ctx *ctx = priv;
138 struct gsc_dev *gsc;
139 unsigned long flags;
4bd0e030 140 int ret;
5d718338
SK
141 bool is_set = false;
142
143 if (WARN(!ctx, "null hardware context\n"))
144 return;
145
146 gsc = ctx->gsc_dev;
147 spin_lock_irqsave(&gsc->slock, flags);
148
149 set_bit(ST_M2M_PEND, &gsc->state);
150
151 /* Reconfigure hardware if the context has changed. */
152 if (gsc->m2m.ctx != ctx) {
153 pr_debug("gsc->m2m.ctx = 0x%p, current_ctx = 0x%p",
154 gsc->m2m.ctx, ctx);
155 ctx->state |= GSC_PARAMS;
156 gsc->m2m.ctx = ctx;
157 }
158
d9315160 159 is_set = ctx->state & GSC_CTX_STOP_REQ;
5d718338 160 if (is_set) {
d9315160
SAB
161 ctx->state &= ~GSC_CTX_STOP_REQ;
162 ctx->state |= GSC_CTX_ABORT;
5d718338
SK
163 wake_up(&gsc->irq_queue);
164 goto put_device;
165 }
166
f60e160e 167 ret = gsc_get_bufs(ctx);
5d718338
SK
168 if (ret) {
169 pr_err("Wrong address");
170 goto put_device;
171 }
172
173 gsc_set_prefbuf(gsc, &ctx->s_frame);
174 gsc_hw_set_input_addr(gsc, &ctx->s_frame.addr, GSC_M2M_BUF_NUM);
175 gsc_hw_set_output_addr(gsc, &ctx->d_frame.addr, GSC_M2M_BUF_NUM);
176
177 if (ctx->state & GSC_PARAMS) {
178 gsc_hw_set_input_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
179 gsc_hw_set_output_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
180 gsc_hw_set_frm_done_irq_mask(gsc, false);
181 gsc_hw_set_gsc_irq_enable(gsc, true);
182
183 if (gsc_set_scaler_info(ctx)) {
184 pr_err("Scaler setup error");
185 goto put_device;
186 }
187
188 gsc_hw_set_input_path(ctx);
189 gsc_hw_set_in_size(ctx);
190 gsc_hw_set_in_image_format(ctx);
191
192 gsc_hw_set_output_path(ctx);
193 gsc_hw_set_out_size(ctx);
194 gsc_hw_set_out_image_format(ctx);
195
196 gsc_hw_set_prescaler(ctx);
197 gsc_hw_set_mainscaler(ctx);
198 gsc_hw_set_rotation(ctx);
199 gsc_hw_set_global_alpha(ctx);
200 }
201
202 /* update shadow registers */
203 gsc_hw_set_sfr_update(ctx);
204
205 ctx->state &= ~GSC_PARAMS;
206 gsc_hw_enable_control(gsc, true);
207
208 spin_unlock_irqrestore(&gsc->slock, flags);
209 return;
210
211put_device:
212 ctx->state &= ~GSC_PARAMS;
213 spin_unlock_irqrestore(&gsc->slock, flags);
214}
215
216static int gsc_m2m_queue_setup(struct vb2_queue *vq,
217 const struct v4l2_format *fmt,
218 unsigned int *num_buffers, unsigned int *num_planes,
219 unsigned int sizes[], void *allocators[])
220{
221 struct gsc_ctx *ctx = vb2_get_drv_priv(vq);
222 struct gsc_frame *frame;
223 int i;
224
225 frame = ctx_get_frame(ctx, vq->type);
226 if (IS_ERR(frame))
227 return PTR_ERR(frame);
228
229 if (!frame->fmt)
230 return -EINVAL;
231
232 *num_planes = frame->fmt->num_planes;
233 for (i = 0; i < frame->fmt->num_planes; i++) {
234 sizes[i] = frame->payload[i];
235 allocators[i] = ctx->gsc_dev->alloc_ctx;
236 }
237 return 0;
238}
239
240static int gsc_m2m_buf_prepare(struct vb2_buffer *vb)
241{
242 struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
243 struct gsc_frame *frame;
244 int i;
245
246 frame = ctx_get_frame(ctx, vb->vb2_queue->type);
247 if (IS_ERR(frame))
248 return PTR_ERR(frame);
249
250 if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
251 for (i = 0; i < frame->fmt->num_planes; i++)
252 vb2_set_plane_payload(vb, i, frame->payload[i]);
253 }
254
255 return 0;
256}
257
258static void gsc_m2m_buf_queue(struct vb2_buffer *vb)
259{
260 struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
261
262 pr_debug("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
263
264 if (ctx->m2m_ctx)
265 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
266}
267
d2331c99 268static struct vb2_ops gsc_m2m_qops = {
5d718338
SK
269 .queue_setup = gsc_m2m_queue_setup,
270 .buf_prepare = gsc_m2m_buf_prepare,
271 .buf_queue = gsc_m2m_buf_queue,
272 .wait_prepare = gsc_unlock,
273 .wait_finish = gsc_lock,
274 .stop_streaming = gsc_m2m_stop_streaming,
275 .start_streaming = gsc_m2m_start_streaming,
276};
277
278static int gsc_m2m_querycap(struct file *file, void *fh,
279 struct v4l2_capability *cap)
280{
281 struct gsc_ctx *ctx = fh_to_ctx(fh);
282 struct gsc_dev *gsc = ctx->gsc_dev;
283
284 strlcpy(cap->driver, gsc->pdev->name, sizeof(cap->driver));
285 strlcpy(cap->card, gsc->pdev->name, sizeof(cap->card));
286 strlcpy(cap->bus_info, "platform", sizeof(cap->bus_info));
287 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE |
288 V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
289
290 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
291 return 0;
292}
293
294static int gsc_m2m_enum_fmt_mplane(struct file *file, void *priv,
295 struct v4l2_fmtdesc *f)
296{
297 return gsc_enum_fmt_mplane(f);
298}
299
300static int gsc_m2m_g_fmt_mplane(struct file *file, void *fh,
301 struct v4l2_format *f)
302{
303 struct gsc_ctx *ctx = fh_to_ctx(fh);
304
305 return gsc_g_fmt_mplane(ctx, f);
306}
307
308static int gsc_m2m_try_fmt_mplane(struct file *file, void *fh,
309 struct v4l2_format *f)
310{
311 struct gsc_ctx *ctx = fh_to_ctx(fh);
312
313 return gsc_try_fmt_mplane(ctx, f);
314}
315
316static int gsc_m2m_s_fmt_mplane(struct file *file, void *fh,
317 struct v4l2_format *f)
318{
319 struct gsc_ctx *ctx = fh_to_ctx(fh);
320 struct vb2_queue *vq;
321 struct gsc_frame *frame;
322 struct v4l2_pix_format_mplane *pix;
323 int i, ret = 0;
324
325 ret = gsc_m2m_try_fmt_mplane(file, fh, f);
326 if (ret)
327 return ret;
328
329 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
330
331 if (vb2_is_streaming(vq)) {
332 pr_err("queue (%d) busy", f->type);
333 return -EBUSY;
334 }
335
336 if (V4L2_TYPE_IS_OUTPUT(f->type))
337 frame = &ctx->s_frame;
338 else
339 frame = &ctx->d_frame;
340
341 pix = &f->fmt.pix_mp;
342 frame->fmt = find_fmt(&pix->pixelformat, NULL, 0);
343 frame->colorspace = pix->colorspace;
344 if (!frame->fmt)
345 return -EINVAL;
346
347 for (i = 0; i < frame->fmt->num_planes; i++)
348 frame->payload[i] = pix->plane_fmt[i].sizeimage;
349
350 gsc_set_frame_size(frame, pix->width, pix->height);
351
352 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
353 gsc_ctx_state_lock_set(GSC_PARAMS | GSC_DST_FMT, ctx);
354 else
355 gsc_ctx_state_lock_set(GSC_PARAMS | GSC_SRC_FMT, ctx);
356
357 pr_debug("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
358
359 return 0;
360}
361
362static int gsc_m2m_reqbufs(struct file *file, void *fh,
363 struct v4l2_requestbuffers *reqbufs)
364{
365 struct gsc_ctx *ctx = fh_to_ctx(fh);
366 struct gsc_dev *gsc = ctx->gsc_dev;
367 struct gsc_frame *frame;
368 u32 max_cnt;
369
370 max_cnt = (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
371 gsc->variant->in_buf_cnt : gsc->variant->out_buf_cnt;
372 if (reqbufs->count > max_cnt) {
373 return -EINVAL;
374 } else if (reqbufs->count == 0) {
375 if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
376 gsc_ctx_state_lock_clear(GSC_SRC_FMT, ctx);
377 else
378 gsc_ctx_state_lock_clear(GSC_DST_FMT, ctx);
379 }
380
381 frame = ctx_get_frame(ctx, reqbufs->type);
382
383 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
384}
385
371a664e
SAB
386static int gsc_m2m_expbuf(struct file *file, void *fh,
387 struct v4l2_exportbuffer *eb)
388{
389 struct gsc_ctx *ctx = fh_to_ctx(fh);
390 return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
391}
392
5d718338
SK
393static int gsc_m2m_querybuf(struct file *file, void *fh,
394 struct v4l2_buffer *buf)
395{
396 struct gsc_ctx *ctx = fh_to_ctx(fh);
397 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
398}
399
400static int gsc_m2m_qbuf(struct file *file, void *fh,
401 struct v4l2_buffer *buf)
402{
403 struct gsc_ctx *ctx = fh_to_ctx(fh);
404 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
405}
406
407static int gsc_m2m_dqbuf(struct file *file, void *fh,
408 struct v4l2_buffer *buf)
409{
410 struct gsc_ctx *ctx = fh_to_ctx(fh);
411 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
412}
413
414static int gsc_m2m_streamon(struct file *file, void *fh,
415 enum v4l2_buf_type type)
416{
417 struct gsc_ctx *ctx = fh_to_ctx(fh);
418
419 /* The source and target color format need to be set */
420 if (V4L2_TYPE_IS_OUTPUT(type)) {
421 if (!gsc_ctx_state_is_set(GSC_SRC_FMT, ctx))
422 return -EINVAL;
423 } else if (!gsc_ctx_state_is_set(GSC_DST_FMT, ctx)) {
424 return -EINVAL;
425 }
426
427 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
428}
429
430static int gsc_m2m_streamoff(struct file *file, void *fh,
431 enum v4l2_buf_type type)
432{
433 struct gsc_ctx *ctx = fh_to_ctx(fh);
434 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
435}
436
437/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
438static int is_rectangle_enclosed(struct v4l2_rect *a, struct v4l2_rect *b)
439{
440 if (a->left < b->left || a->top < b->top)
441 return 0;
442
443 if (a->left + a->width > b->left + b->width)
444 return 0;
445
446 if (a->top + a->height > b->top + b->height)
447 return 0;
448
449 return 1;
450}
451
452static int gsc_m2m_g_selection(struct file *file, void *fh,
453 struct v4l2_selection *s)
454{
455 struct gsc_frame *frame;
456 struct gsc_ctx *ctx = fh_to_ctx(fh);
457
458 if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
459 (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
460 return -EINVAL;
461
462 frame = ctx_get_frame(ctx, s->type);
463 if (IS_ERR(frame))
464 return PTR_ERR(frame);
465
466 switch (s->target) {
467 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
468 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
469 case V4L2_SEL_TGT_CROP_BOUNDS:
470 case V4L2_SEL_TGT_CROP_DEFAULT:
471 s->r.left = 0;
472 s->r.top = 0;
473 s->r.width = frame->f_width;
474 s->r.height = frame->f_height;
475 return 0;
476
477 case V4L2_SEL_TGT_COMPOSE:
478 case V4L2_SEL_TGT_CROP:
479 s->r.left = frame->crop.left;
480 s->r.top = frame->crop.top;
481 s->r.width = frame->crop.width;
482 s->r.height = frame->crop.height;
483 return 0;
484 }
485
486 return -EINVAL;
487}
488
489static int gsc_m2m_s_selection(struct file *file, void *fh,
490 struct v4l2_selection *s)
491{
492 struct gsc_frame *frame;
493 struct gsc_ctx *ctx = fh_to_ctx(fh);
494 struct v4l2_crop cr;
495 struct gsc_variant *variant = ctx->gsc_dev->variant;
496 int ret;
497
498 cr.type = s->type;
499 cr.c = s->r;
500
501 if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
502 (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE))
503 return -EINVAL;
504
505 ret = gsc_try_crop(ctx, &cr);
506 if (ret)
507 return ret;
508
509 if (s->flags & V4L2_SEL_FLAG_LE &&
510 !is_rectangle_enclosed(&cr.c, &s->r))
511 return -ERANGE;
512
513 if (s->flags & V4L2_SEL_FLAG_GE &&
514 !is_rectangle_enclosed(&s->r, &cr.c))
515 return -ERANGE;
516
517 s->r = cr.c;
518
519 switch (s->target) {
520 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
521 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
522 case V4L2_SEL_TGT_COMPOSE:
523 frame = &ctx->s_frame;
524 break;
525
526 case V4L2_SEL_TGT_CROP_BOUNDS:
527 case V4L2_SEL_TGT_CROP:
528 case V4L2_SEL_TGT_CROP_DEFAULT:
529 frame = &ctx->d_frame;
530 break;
531
532 default:
533 return -EINVAL;
534 }
535
536 /* Check to see if scaling ratio is within supported range */
537 if (gsc_ctx_state_is_set(GSC_DST_FMT | GSC_SRC_FMT, ctx)) {
538 if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
539 ret = gsc_check_scaler_ratio(variant, cr.c.width,
540 cr.c.height, ctx->d_frame.crop.width,
541 ctx->d_frame.crop.height,
542 ctx->gsc_ctrls.rotate->val, ctx->out_path);
543 } else {
544 ret = gsc_check_scaler_ratio(variant,
545 ctx->s_frame.crop.width,
546 ctx->s_frame.crop.height, cr.c.width,
547 cr.c.height, ctx->gsc_ctrls.rotate->val,
548 ctx->out_path);
549 }
550
551 if (ret) {
552 pr_err("Out of scaler range");
553 return -EINVAL;
554 }
555 }
556
557 frame->crop = cr.c;
558
559 gsc_ctx_state_lock_set(GSC_PARAMS, ctx);
560 return 0;
561}
562
563static const struct v4l2_ioctl_ops gsc_m2m_ioctl_ops = {
564 .vidioc_querycap = gsc_m2m_querycap,
565 .vidioc_enum_fmt_vid_cap_mplane = gsc_m2m_enum_fmt_mplane,
566 .vidioc_enum_fmt_vid_out_mplane = gsc_m2m_enum_fmt_mplane,
567 .vidioc_g_fmt_vid_cap_mplane = gsc_m2m_g_fmt_mplane,
568 .vidioc_g_fmt_vid_out_mplane = gsc_m2m_g_fmt_mplane,
569 .vidioc_try_fmt_vid_cap_mplane = gsc_m2m_try_fmt_mplane,
570 .vidioc_try_fmt_vid_out_mplane = gsc_m2m_try_fmt_mplane,
571 .vidioc_s_fmt_vid_cap_mplane = gsc_m2m_s_fmt_mplane,
572 .vidioc_s_fmt_vid_out_mplane = gsc_m2m_s_fmt_mplane,
573 .vidioc_reqbufs = gsc_m2m_reqbufs,
371a664e 574 .vidioc_expbuf = gsc_m2m_expbuf,
5d718338
SK
575 .vidioc_querybuf = gsc_m2m_querybuf,
576 .vidioc_qbuf = gsc_m2m_qbuf,
577 .vidioc_dqbuf = gsc_m2m_dqbuf,
578 .vidioc_streamon = gsc_m2m_streamon,
579 .vidioc_streamoff = gsc_m2m_streamoff,
580 .vidioc_g_selection = gsc_m2m_g_selection,
581 .vidioc_s_selection = gsc_m2m_s_selection
582};
583
584static int queue_init(void *priv, struct vb2_queue *src_vq,
585 struct vb2_queue *dst_vq)
586{
587 struct gsc_ctx *ctx = priv;
588 int ret;
589
590 memset(src_vq, 0, sizeof(*src_vq));
591 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
371a664e 592 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
5d718338
SK
593 src_vq->drv_priv = ctx;
594 src_vq->ops = &gsc_m2m_qops;
595 src_vq->mem_ops = &vb2_dma_contig_memops;
596 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
ade48681 597 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
5d718338
SK
598
599 ret = vb2_queue_init(src_vq);
600 if (ret)
601 return ret;
602
603 memset(dst_vq, 0, sizeof(*dst_vq));
604 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
371a664e 605 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
5d718338
SK
606 dst_vq->drv_priv = ctx;
607 dst_vq->ops = &gsc_m2m_qops;
608 dst_vq->mem_ops = &vb2_dma_contig_memops;
609 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
ade48681 610 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
5d718338
SK
611
612 return vb2_queue_init(dst_vq);
613}
614
615static int gsc_m2m_open(struct file *file)
616{
617 struct gsc_dev *gsc = video_drvdata(file);
618 struct gsc_ctx *ctx = NULL;
619 int ret;
620
621 pr_debug("pid: %d, state: 0x%lx", task_pid_nr(current), gsc->state);
622
623 if (mutex_lock_interruptible(&gsc->lock))
624 return -ERESTARTSYS;
625
2c8cc13f 626 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
5d718338
SK
627 if (!ctx) {
628 ret = -ENOMEM;
629 goto unlock;
630 }
631
632 v4l2_fh_init(&ctx->fh, gsc->m2m.vfd);
633 ret = gsc_ctrls_create(ctx);
634 if (ret)
635 goto error_fh;
636
637 /* Use separate control handler per file handle */
638 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
639 file->private_data = &ctx->fh;
640 v4l2_fh_add(&ctx->fh);
641
642 ctx->gsc_dev = gsc;
643 /* Default color format */
644 ctx->s_frame.fmt = get_format(0);
645 ctx->d_frame.fmt = get_format(0);
646 /* Setup the device context for mem2mem mode. */
647 ctx->state = GSC_CTX_M2M;
648 ctx->flags = 0;
649 ctx->in_path = GSC_DMA;
650 ctx->out_path = GSC_DMA;
651
652 ctx->m2m_ctx = v4l2_m2m_ctx_init(gsc->m2m.m2m_dev, ctx, queue_init);
653 if (IS_ERR(ctx->m2m_ctx)) {
654 pr_err("Failed to initialize m2m context");
655 ret = PTR_ERR(ctx->m2m_ctx);
656 goto error_ctrls;
657 }
658
659 if (gsc->m2m.refcnt++ == 0)
660 set_bit(ST_M2M_OPEN, &gsc->state);
661
662 pr_debug("gsc m2m driver is opened, ctx(0x%p)", ctx);
663
664 mutex_unlock(&gsc->lock);
665 return 0;
666
667error_ctrls:
668 gsc_ctrls_delete(ctx);
669error_fh:
670 v4l2_fh_del(&ctx->fh);
671 v4l2_fh_exit(&ctx->fh);
672 kfree(ctx);
673unlock:
674 mutex_unlock(&gsc->lock);
675 return ret;
676}
677
678static int gsc_m2m_release(struct file *file)
679{
680 struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
681 struct gsc_dev *gsc = ctx->gsc_dev;
682
683 pr_debug("pid: %d, state: 0x%lx, refcnt= %d",
684 task_pid_nr(current), gsc->state, gsc->m2m.refcnt);
685
98680180 686 mutex_lock(&gsc->lock);
5d718338
SK
687
688 v4l2_m2m_ctx_release(ctx->m2m_ctx);
689 gsc_ctrls_delete(ctx);
690 v4l2_fh_del(&ctx->fh);
691 v4l2_fh_exit(&ctx->fh);
692
693 if (--gsc->m2m.refcnt <= 0)
694 clear_bit(ST_M2M_OPEN, &gsc->state);
695 kfree(ctx);
696
697 mutex_unlock(&gsc->lock);
698 return 0;
699}
700
701static unsigned int gsc_m2m_poll(struct file *file,
702 struct poll_table_struct *wait)
703{
704 struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
705 struct gsc_dev *gsc = ctx->gsc_dev;
706 int ret;
707
708 if (mutex_lock_interruptible(&gsc->lock))
709 return -ERESTARTSYS;
710
711 ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
712 mutex_unlock(&gsc->lock);
713
714 return ret;
715}
716
717static int gsc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
718{
719 struct gsc_ctx *ctx = fh_to_ctx(file->private_data);
720 struct gsc_dev *gsc = ctx->gsc_dev;
721 int ret;
722
723 if (mutex_lock_interruptible(&gsc->lock))
724 return -ERESTARTSYS;
725
726 ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
727 mutex_unlock(&gsc->lock);
728
729 return ret;
730}
731
732static const struct v4l2_file_operations gsc_m2m_fops = {
733 .owner = THIS_MODULE,
734 .open = gsc_m2m_open,
735 .release = gsc_m2m_release,
736 .poll = gsc_m2m_poll,
737 .unlocked_ioctl = video_ioctl2,
738 .mmap = gsc_m2m_mmap,
739};
740
741static struct v4l2_m2m_ops gsc_m2m_ops = {
742 .device_run = gsc_m2m_device_run,
743 .job_abort = gsc_m2m_job_abort,
744};
745
746int gsc_register_m2m_device(struct gsc_dev *gsc)
747{
748 struct platform_device *pdev;
749 int ret;
750
751 if (!gsc)
752 return -ENODEV;
753
754 pdev = gsc->pdev;
755
756 gsc->vdev.fops = &gsc_m2m_fops;
757 gsc->vdev.ioctl_ops = &gsc_m2m_ioctl_ops;
758 gsc->vdev.release = video_device_release_empty;
759 gsc->vdev.lock = &gsc->lock;
24fc681a 760 gsc->vdev.vfl_dir = VFL_DIR_M2M;
d0b1c313 761 gsc->vdev.v4l2_dev = &gsc->v4l2_dev;
5d718338
SK
762 snprintf(gsc->vdev.name, sizeof(gsc->vdev.name), "%s.%d:m2m",
763 GSC_MODULE_NAME, gsc->id);
764
765 video_set_drvdata(&gsc->vdev, gsc);
766
767 gsc->m2m.vfd = &gsc->vdev;
768 gsc->m2m.m2m_dev = v4l2_m2m_init(&gsc_m2m_ops);
769 if (IS_ERR(gsc->m2m.m2m_dev)) {
770 dev_err(&pdev->dev, "failed to initialize v4l2-m2m device\n");
771 ret = PTR_ERR(gsc->m2m.m2m_dev);
772 goto err_m2m_r1;
773 }
774
775 ret = video_register_device(&gsc->vdev, VFL_TYPE_GRABBER, -1);
776 if (ret) {
777 dev_err(&pdev->dev,
778 "%s(): failed to register video device\n", __func__);
779 goto err_m2m_r2;
780 }
781
782 pr_debug("gsc m2m driver registered as /dev/video%d", gsc->vdev.num);
783 return 0;
784
785err_m2m_r2:
786 v4l2_m2m_release(gsc->m2m.m2m_dev);
787err_m2m_r1:
788 video_device_release(gsc->m2m.vfd);
789
790 return ret;
791}
792
793void gsc_unregister_m2m_device(struct gsc_dev *gsc)
794{
795 if (gsc)
796 v4l2_m2m_release(gsc->m2m.m2m_dev);
797}