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