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