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