blob: 6c4d1b91f3255d0ea1f7dae27d4c3e4c3b3b2ccd [file] [log] [blame]
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001/*
2 * Samsung TV Mixer driver
3 *
4 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
5 *
6 * Tomasz Stanislawski, <t.stanislaws@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation. either version 2 of the License,
11 * or (at your option) any later version
12 */
13
Sachin Kamatf5c99032012-06-11 06:13:52 -030014#define pr_fmt(fmt) "s5p-tv (mixer): " fmt
15
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -030016#include "mixer.h"
17
18#include <media/v4l2-ioctl.h>
19#include <linux/videodev2.h>
20#include <linux/mm.h>
Marek Szyprowskifc8ac772011-11-21 05:02:38 -030021#include <linux/module.h>
Sachin Kamat3e58ac12013-02-07 02:55:55 -030022#include <linux/platform_device.h>
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -030023#include <linux/timer.h>
24#include <media/videobuf2-dma-contig.h>
25
26static int find_reg_callback(struct device *dev, void *p)
27{
28 struct v4l2_subdev **sd = p;
29
30 *sd = dev_get_drvdata(dev);
31 /* non-zero value stops iteration */
32 return 1;
33}
34
35static struct v4l2_subdev *find_and_register_subdev(
36 struct mxr_device *mdev, char *module_name)
37{
38 struct device_driver *drv;
39 struct v4l2_subdev *sd = NULL;
40 int ret;
41
42 /* TODO: add waiting until probe is finished */
43 drv = driver_find(module_name, &platform_bus_type);
44 if (!drv) {
45 mxr_warn(mdev, "module %s is missing\n", module_name);
46 return NULL;
47 }
48 /* driver refcnt is increased, it is safe to iterate over devices */
49 ret = driver_for_each_device(drv, NULL, &sd, find_reg_callback);
50 /* ret == 0 means that find_reg_callback was never executed */
51 if (sd == NULL) {
52 mxr_warn(mdev, "module %s provides no subdev!\n", module_name);
53 goto done;
54 }
55 /* v4l2_device_register_subdev detects if sd is NULL */
56 ret = v4l2_device_register_subdev(&mdev->v4l2_dev, sd);
57 if (ret) {
58 mxr_warn(mdev, "failed to register subdev %s\n", sd->name);
59 sd = NULL;
60 }
61
62done:
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -030063 return sd;
64}
65
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -080066int mxr_acquire_video(struct mxr_device *mdev,
67 struct mxr_output_conf *output_conf, int output_count)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -030068{
69 struct device *dev = mdev->dev;
70 struct v4l2_device *v4l2_dev = &mdev->v4l2_dev;
71 int i;
72 int ret = 0;
73 struct v4l2_subdev *sd;
74
75 strlcpy(v4l2_dev->name, dev_name(mdev->dev), sizeof(v4l2_dev->name));
76 /* prepare context for V4L2 device */
77 ret = v4l2_device_register(dev, v4l2_dev);
78 if (ret) {
79 mxr_err(mdev, "could not register v4l2 device.\n");
80 goto fail;
81 }
82
Marek Szyprowski712b6172016-05-24 09:16:07 +020083 vb2_dma_contig_set_max_seg_size(mdev->dev, DMA_BIT_MASK(32));
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -030084
85 /* registering outputs */
86 mdev->output_cnt = 0;
87 for (i = 0; i < output_count; ++i) {
88 struct mxr_output_conf *conf = &output_conf[i];
89 struct mxr_output *out;
90
91 sd = find_and_register_subdev(mdev, conf->module_name);
92 /* trying to register next output */
93 if (sd == NULL)
94 continue;
Sachin Kamatc0d51202012-11-26 01:49:01 -030095 out = kzalloc(sizeof(*out), GFP_KERNEL);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -030096 if (out == NULL) {
97 mxr_err(mdev, "no memory for '%s'\n",
98 conf->output_name);
99 ret = -ENOMEM;
100 /* registered subdevs are removed in fail_v4l2_dev */
101 goto fail_output;
102 }
103 strlcpy(out->name, conf->output_name, sizeof(out->name));
104 out->sd = sd;
105 out->cookie = conf->cookie;
106 mdev->output[mdev->output_cnt++] = out;
107 mxr_info(mdev, "added output '%s' from module '%s'\n",
108 conf->output_name, conf->module_name);
109 /* checking if maximal number of outputs is reached */
110 if (mdev->output_cnt >= MXR_MAX_OUTPUTS)
111 break;
112 }
113
114 if (mdev->output_cnt == 0) {
115 mxr_err(mdev, "failed to register any output\n");
116 ret = -ENODEV;
117 /* skipping fail_output because there is nothing to free */
Hans Verkuilc781e4a2016-02-15 14:25:09 -0200118 goto fail_v4l2_dev;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300119 }
120
121 return 0;
122
123fail_output:
124 /* kfree is NULL-safe */
125 for (i = 0; i < mdev->output_cnt; ++i)
126 kfree(mdev->output[i]);
Sachin Kamatc0d51202012-11-26 01:49:01 -0300127 memset(mdev->output, 0, sizeof(mdev->output));
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300128
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300129fail_v4l2_dev:
130 /* NOTE: automatically unregister all subdevs */
131 v4l2_device_unregister(v4l2_dev);
132
133fail:
134 return ret;
135}
136
Sachin Kamate6364a92012-03-12 03:13:34 -0300137void mxr_release_video(struct mxr_device *mdev)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300138{
139 int i;
140
141 /* kfree is NULL-safe */
142 for (i = 0; i < mdev->output_cnt; ++i)
143 kfree(mdev->output[i]);
144
Marek Szyprowski712b6172016-05-24 09:16:07 +0200145 vb2_dma_contig_clear_max_seg_size(mdev->dev);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300146 v4l2_device_unregister(&mdev->v4l2_dev);
147}
148
149static int mxr_querycap(struct file *file, void *priv,
150 struct v4l2_capability *cap)
151{
152 struct mxr_layer *layer = video_drvdata(file);
153
154 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
155
Sachin Kamatc0d51202012-11-26 01:49:01 -0300156 strlcpy(cap->driver, MXR_DRIVER_NAME, sizeof(cap->driver));
157 strlcpy(cap->card, layer->vfd.name, sizeof(cap->card));
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300158 sprintf(cap->bus_info, "%d", layer->idx);
Sylwester Nawrocki72085072012-09-24 06:03:04 -0300159 cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
160 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300161
162 return 0;
163}
164
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300165static void mxr_geometry_dump(struct mxr_device *mdev, struct mxr_geometry *geo)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300166{
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300167 mxr_dbg(mdev, "src.full_size = (%u, %u)\n",
168 geo->src.full_width, geo->src.full_height);
169 mxr_dbg(mdev, "src.size = (%u, %u)\n",
170 geo->src.width, geo->src.height);
171 mxr_dbg(mdev, "src.offset = (%u, %u)\n",
172 geo->src.x_offset, geo->src.y_offset);
173 mxr_dbg(mdev, "dst.full_size = (%u, %u)\n",
174 geo->dst.full_width, geo->dst.full_height);
175 mxr_dbg(mdev, "dst.size = (%u, %u)\n",
176 geo->dst.width, geo->dst.height);
177 mxr_dbg(mdev, "dst.offset = (%u, %u)\n",
178 geo->dst.x_offset, geo->dst.y_offset);
179 mxr_dbg(mdev, "ratio = (%u, %u)\n",
180 geo->x_ratio, geo->y_ratio);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300181}
182
183static void mxr_layer_default_geo(struct mxr_layer *layer)
184{
185 struct mxr_device *mdev = layer->mdev;
186 struct v4l2_mbus_framefmt mbus_fmt;
187
Sachin Kamatc0d51202012-11-26 01:49:01 -0300188 memset(&layer->geo, 0, sizeof(layer->geo));
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300189
190 mxr_get_mbus_fmt(mdev, &mbus_fmt);
191
192 layer->geo.dst.full_width = mbus_fmt.width;
193 layer->geo.dst.full_height = mbus_fmt.height;
194 layer->geo.dst.width = layer->geo.dst.full_width;
195 layer->geo.dst.height = layer->geo.dst.full_height;
196 layer->geo.dst.field = mbus_fmt.field;
197
198 layer->geo.src.full_width = mbus_fmt.width;
199 layer->geo.src.full_height = mbus_fmt.height;
200 layer->geo.src.width = layer->geo.src.full_width;
201 layer->geo.src.height = layer->geo.src.full_height;
202
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300203 mxr_geometry_dump(mdev, &layer->geo);
204 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);
205 mxr_geometry_dump(mdev, &layer->geo);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300206}
207
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300208static void mxr_layer_update_output(struct mxr_layer *layer)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300209{
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300210 struct mxr_device *mdev = layer->mdev;
211 struct v4l2_mbus_framefmt mbus_fmt;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300212
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300213 mxr_get_mbus_fmt(mdev, &mbus_fmt);
214 /* checking if update is needed */
215 if (layer->geo.dst.full_width == mbus_fmt.width &&
216 layer->geo.dst.full_height == mbus_fmt.width)
217 return;
218
219 layer->geo.dst.full_width = mbus_fmt.width;
220 layer->geo.dst.full_height = mbus_fmt.height;
221 layer->geo.dst.field = mbus_fmt.field;
222 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);
223
224 mxr_geometry_dump(mdev, &layer->geo);
225}
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300226
227static const struct mxr_format *find_format_by_fourcc(
228 struct mxr_layer *layer, unsigned long fourcc);
229static const struct mxr_format *find_format_by_index(
230 struct mxr_layer *layer, unsigned long index);
231
232static int mxr_enum_fmt(struct file *file, void *priv,
233 struct v4l2_fmtdesc *f)
234{
235 struct mxr_layer *layer = video_drvdata(file);
236 struct mxr_device *mdev = layer->mdev;
237 const struct mxr_format *fmt;
238
239 mxr_dbg(mdev, "%s\n", __func__);
240 fmt = find_format_by_index(layer, f->index);
241 if (fmt == NULL)
242 return -EINVAL;
243
244 strlcpy(f->description, fmt->name, sizeof(f->description));
245 f->pixelformat = fmt->fourcc;
246
247 return 0;
248}
249
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300250static unsigned int divup(unsigned int divident, unsigned int divisor)
251{
252 return (divident + divisor - 1) / divisor;
253}
254
255unsigned long mxr_get_plane_size(const struct mxr_block *blk,
256 unsigned int width, unsigned int height)
257{
258 unsigned int bl_width = divup(width, blk->width);
259 unsigned int bl_height = divup(height, blk->height);
260
261 return bl_width * bl_height * blk->size;
262}
263
264static void mxr_mplane_fill(struct v4l2_plane_pix_format *planes,
265 const struct mxr_format *fmt, u32 width, u32 height)
266{
267 int i;
268
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300269 /* checking if nothing to fill */
270 if (!planes)
271 return;
272
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300273 memset(planes, 0, sizeof(*planes) * fmt->num_subframes);
274 for (i = 0; i < fmt->num_planes; ++i) {
275 struct v4l2_plane_pix_format *plane = planes
276 + fmt->plane2subframe[i];
277 const struct mxr_block *blk = &fmt->plane[i];
278 u32 bl_width = divup(width, blk->width);
279 u32 bl_height = divup(height, blk->height);
280 u32 sizeimage = bl_width * bl_height * blk->size;
Hans Verkuilcc7d2df2015-03-15 14:30:25 -0300281 u32 bytesperline = bl_width * blk->size / blk->height;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300282
283 plane->sizeimage += sizeimage;
284 plane->bytesperline = max(plane->bytesperline, bytesperline);
285 }
286}
287
288static int mxr_g_fmt(struct file *file, void *priv,
289 struct v4l2_format *f)
290{
291 struct mxr_layer *layer = video_drvdata(file);
292 struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;
293
294 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
295
296 pix->width = layer->geo.src.full_width;
297 pix->height = layer->geo.src.full_height;
298 pix->field = V4L2_FIELD_NONE;
299 pix->pixelformat = layer->fmt->fourcc;
300 pix->colorspace = layer->fmt->colorspace;
301 mxr_mplane_fill(pix->plane_fmt, layer->fmt, pix->width, pix->height);
302
303 return 0;
304}
305
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300306static int mxr_s_fmt(struct file *file, void *priv,
307 struct v4l2_format *f)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300308{
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300309 struct mxr_layer *layer = video_drvdata(file);
310 const struct mxr_format *fmt;
311 struct v4l2_pix_format_mplane *pix;
312 struct mxr_device *mdev = layer->mdev;
313 struct mxr_geometry *geo = &layer->geo;
314
315 mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);
316
317 pix = &f->fmt.pix_mp;
318 fmt = find_format_by_fourcc(layer, pix->pixelformat);
319 if (fmt == NULL) {
320 mxr_warn(mdev, "not recognized fourcc: %08x\n",
321 pix->pixelformat);
322 return -EINVAL;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300323 }
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300324 layer->fmt = fmt;
325 /* set source size to highest accepted value */
326 geo->src.full_width = max(geo->dst.full_width, pix->width);
327 geo->src.full_height = max(geo->dst.full_height, pix->height);
328 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
329 mxr_geometry_dump(mdev, &layer->geo);
330 /* set cropping to total visible screen */
331 geo->src.width = pix->width;
332 geo->src.height = pix->height;
333 geo->src.x_offset = 0;
334 geo->src.y_offset = 0;
335 /* assure consistency of geometry */
336 layer->ops.fix_geometry(layer, MXR_GEOMETRY_CROP, MXR_NO_OFFSET);
337 mxr_geometry_dump(mdev, &layer->geo);
338 /* set full size to lowest possible value */
339 geo->src.full_width = 0;
340 geo->src.full_height = 0;
341 layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
342 mxr_geometry_dump(mdev, &layer->geo);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300343
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300344 /* returning results */
345 mxr_g_fmt(file, priv, f);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300346
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300347 return 0;
348}
349
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300350static int mxr_g_selection(struct file *file, void *fh,
351 struct v4l2_selection *s)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300352{
353 struct mxr_layer *layer = video_drvdata(file);
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300354 struct mxr_geometry *geo = &layer->geo;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300355
356 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300357
358 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
359 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300360 return -EINVAL;
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300361
362 switch (s->target) {
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300363 case V4L2_SEL_TGT_CROP:
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300364 s->r.left = geo->src.x_offset;
365 s->r.top = geo->src.y_offset;
366 s->r.width = geo->src.width;
367 s->r.height = geo->src.height;
368 break;
369 case V4L2_SEL_TGT_CROP_DEFAULT:
370 case V4L2_SEL_TGT_CROP_BOUNDS:
371 s->r.left = 0;
372 s->r.top = 0;
373 s->r.width = geo->src.full_width;
374 s->r.height = geo->src.full_height;
375 break;
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300376 case V4L2_SEL_TGT_COMPOSE:
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300377 case V4L2_SEL_TGT_COMPOSE_PADDED:
378 s->r.left = geo->dst.x_offset;
379 s->r.top = geo->dst.y_offset;
380 s->r.width = geo->dst.width;
381 s->r.height = geo->dst.height;
382 break;
383 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
384 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
385 s->r.left = 0;
386 s->r.top = 0;
387 s->r.width = geo->dst.full_width;
388 s->r.height = geo->dst.full_height;
389 break;
390 default:
391 return -EINVAL;
392 }
393
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300394 return 0;
395}
396
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300397/* returns 1 if rectangle 'a' is inside 'b' */
398static int mxr_is_rect_inside(struct v4l2_rect *a, struct v4l2_rect *b)
399{
400 if (a->left < b->left)
401 return 0;
402 if (a->top < b->top)
403 return 0;
404 if (a->left + a->width > b->left + b->width)
405 return 0;
406 if (a->top + a->height > b->top + b->height)
407 return 0;
408 return 1;
409}
410
411static int mxr_s_selection(struct file *file, void *fh,
412 struct v4l2_selection *s)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300413{
414 struct mxr_layer *layer = video_drvdata(file);
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300415 struct mxr_geometry *geo = &layer->geo;
416 struct mxr_crop *target = NULL;
417 enum mxr_geometry_stage stage;
418 struct mxr_geometry tmp;
419 struct v4l2_rect res;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300420
Sachin Kamatc0d51202012-11-26 01:49:01 -0300421 memset(&res, 0, sizeof(res));
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300422
423 mxr_dbg(layer->mdev, "%s: rect: %dx%d@%d,%d\n", __func__,
424 s->r.width, s->r.height, s->r.left, s->r.top);
425
426 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
427 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300428 return -EINVAL;
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300429
430 switch (s->target) {
431 /* ignore read-only targets */
432 case V4L2_SEL_TGT_CROP_DEFAULT:
433 case V4L2_SEL_TGT_CROP_BOUNDS:
434 res.width = geo->src.full_width;
435 res.height = geo->src.full_height;
436 break;
437
438 /* ignore read-only targets */
439 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
440 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
441 res.width = geo->dst.full_width;
442 res.height = geo->dst.full_height;
443 break;
444
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300445 case V4L2_SEL_TGT_CROP:
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300446 target = &geo->src;
447 stage = MXR_GEOMETRY_CROP;
448 break;
Sylwester Nawrockic1334822012-05-20 11:17:12 -0300449 case V4L2_SEL_TGT_COMPOSE:
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300450 case V4L2_SEL_TGT_COMPOSE_PADDED:
451 target = &geo->dst;
452 stage = MXR_GEOMETRY_COMPOSE;
453 break;
454 default:
455 return -EINVAL;
456 }
457 /* apply change and update geometry if needed */
458 if (target) {
459 /* backup current geometry if setup fails */
Sachin Kamatc0d51202012-11-26 01:49:01 -0300460 memcpy(&tmp, geo, sizeof(tmp));
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300461
462 /* apply requested selection */
463 target->x_offset = s->r.left;
464 target->y_offset = s->r.top;
465 target->width = s->r.width;
466 target->height = s->r.height;
467
468 layer->ops.fix_geometry(layer, stage, s->flags);
469
470 /* retrieve update selection rectangle */
471 res.left = target->x_offset;
472 res.top = target->y_offset;
473 res.width = target->width;
474 res.height = target->height;
475
476 mxr_geometry_dump(layer->mdev, &layer->geo);
477 }
478
479 /* checking if the rectangle satisfies constraints */
480 if ((s->flags & V4L2_SEL_FLAG_LE) && !mxr_is_rect_inside(&res, &s->r))
481 goto fail;
482 if ((s->flags & V4L2_SEL_FLAG_GE) && !mxr_is_rect_inside(&s->r, &res))
483 goto fail;
484
485 /* return result rectangle */
486 s->r = res;
487
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300488 return 0;
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300489fail:
490 /* restore old geometry, which is not touched if target is NULL */
491 if (target)
Sachin Kamatc0d51202012-11-26 01:49:01 -0300492 memcpy(geo, &tmp, sizeof(tmp));
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300493 return -ERANGE;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300494}
495
Hans Verkuil8e42bf02013-03-04 09:30:48 -0300496static int mxr_enum_dv_timings(struct file *file, void *fh,
497 struct v4l2_enum_dv_timings *timings)
498{
499 struct mxr_layer *layer = video_drvdata(file);
500 struct mxr_device *mdev = layer->mdev;
501 int ret;
502
Laurent Pinchart78dc47b2014-01-31 09:04:19 -0300503 timings->pad = 0;
504
Hans Verkuil8e42bf02013-03-04 09:30:48 -0300505 /* lock protects from changing sd_out */
506 mutex_lock(&mdev->mutex);
Laurent Pinchart78dc47b2014-01-31 09:04:19 -0300507 ret = v4l2_subdev_call(to_outsd(mdev), pad, enum_dv_timings, timings);
Hans Verkuil8e42bf02013-03-04 09:30:48 -0300508 mutex_unlock(&mdev->mutex);
509
510 return ret ? -EINVAL : 0;
511}
512
513static int mxr_s_dv_timings(struct file *file, void *fh,
514 struct v4l2_dv_timings *timings)
515{
516 struct mxr_layer *layer = video_drvdata(file);
517 struct mxr_device *mdev = layer->mdev;
518 int ret;
519
520 /* lock protects from changing sd_out */
521 mutex_lock(&mdev->mutex);
522
523 /* timings change cannot be done while there is an entity
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300524 * dependent on output configuration
Hans Verkuil8e42bf02013-03-04 09:30:48 -0300525 */
526 if (mdev->n_output > 0) {
527 mutex_unlock(&mdev->mutex);
528 return -EBUSY;
529 }
530
531 ret = v4l2_subdev_call(to_outsd(mdev), video, s_dv_timings, timings);
532
533 mutex_unlock(&mdev->mutex);
534
535 mxr_layer_update_output(layer);
536
537 /* any failure should return EINVAL according to V4L2 doc */
538 return ret ? -EINVAL : 0;
539}
540
541static int mxr_g_dv_timings(struct file *file, void *fh,
542 struct v4l2_dv_timings *timings)
543{
544 struct mxr_layer *layer = video_drvdata(file);
545 struct mxr_device *mdev = layer->mdev;
546 int ret;
547
548 /* lock protects from changing sd_out */
549 mutex_lock(&mdev->mutex);
550 ret = v4l2_subdev_call(to_outsd(mdev), video, g_dv_timings, timings);
551 mutex_unlock(&mdev->mutex);
552
553 return ret ? -EINVAL : 0;
554}
555
556static int mxr_dv_timings_cap(struct file *file, void *fh,
557 struct v4l2_dv_timings_cap *cap)
558{
559 struct mxr_layer *layer = video_drvdata(file);
560 struct mxr_device *mdev = layer->mdev;
561 int ret;
562
Laurent Pinchart78dc47b2014-01-31 09:04:19 -0300563 cap->pad = 0;
564
Hans Verkuil8e42bf02013-03-04 09:30:48 -0300565 /* lock protects from changing sd_out */
566 mutex_lock(&mdev->mutex);
Laurent Pinchart78dc47b2014-01-31 09:04:19 -0300567 ret = v4l2_subdev_call(to_outsd(mdev), pad, dv_timings_cap, cap);
Hans Verkuil8e42bf02013-03-04 09:30:48 -0300568 mutex_unlock(&mdev->mutex);
569
570 return ret ? -EINVAL : 0;
571}
572
Hans Verkuil314527a2013-03-15 06:10:40 -0300573static int mxr_s_std(struct file *file, void *fh, v4l2_std_id norm)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300574{
575 struct mxr_layer *layer = video_drvdata(file);
576 struct mxr_device *mdev = layer->mdev;
577 int ret;
578
579 /* lock protects from changing sd_out */
580 mutex_lock(&mdev->mutex);
581
582 /* standard change cannot be done while there is an entity
Jonathan McCrohan39c1cb22013-10-20 21:34:01 -0300583 * dependent on output configuration
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300584 */
585 if (mdev->n_output > 0) {
586 mutex_unlock(&mdev->mutex);
587 return -EBUSY;
588 }
589
Hans Verkuil314527a2013-03-15 06:10:40 -0300590 ret = v4l2_subdev_call(to_outsd(mdev), video, s_std_output, norm);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300591
592 mutex_unlock(&mdev->mutex);
593
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300594 mxr_layer_update_output(layer);
595
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300596 return ret ? -EINVAL : 0;
597}
598
599static int mxr_g_std(struct file *file, void *fh, v4l2_std_id *norm)
600{
601 struct mxr_layer *layer = video_drvdata(file);
602 struct mxr_device *mdev = layer->mdev;
603 int ret;
604
605 /* lock protects from changing sd_out */
606 mutex_lock(&mdev->mutex);
607 ret = v4l2_subdev_call(to_outsd(mdev), video, g_std_output, norm);
608 mutex_unlock(&mdev->mutex);
609
610 return ret ? -EINVAL : 0;
611}
612
613static int mxr_enum_output(struct file *file, void *fh, struct v4l2_output *a)
614{
615 struct mxr_layer *layer = video_drvdata(file);
616 struct mxr_device *mdev = layer->mdev;
617 struct mxr_output *out;
618 struct v4l2_subdev *sd;
619
620 if (a->index >= mdev->output_cnt)
621 return -EINVAL;
622 out = mdev->output[a->index];
623 BUG_ON(out == NULL);
624 sd = out->sd;
625 strlcpy(a->name, out->name, sizeof(a->name));
626
627 /* try to obtain supported tv norms */
628 v4l2_subdev_call(sd, video, g_tvnorms_output, &a->std);
629 a->capabilities = 0;
Hans Verkuil8e42bf02013-03-04 09:30:48 -0300630 if (sd->ops->video && sd->ops->video->s_dv_timings)
631 a->capabilities |= V4L2_OUT_CAP_DV_TIMINGS;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300632 if (sd->ops->video && sd->ops->video->s_std_output)
633 a->capabilities |= V4L2_OUT_CAP_STD;
634 a->type = V4L2_OUTPUT_TYPE_ANALOG;
635
636 return 0;
637}
638
639static int mxr_s_output(struct file *file, void *fh, unsigned int i)
640{
641 struct video_device *vfd = video_devdata(file);
642 struct mxr_layer *layer = video_drvdata(file);
643 struct mxr_device *mdev = layer->mdev;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300644
645 if (i >= mdev->output_cnt || mdev->output[i] == NULL)
646 return -EINVAL;
647
648 mutex_lock(&mdev->mutex);
649 if (mdev->n_output > 0) {
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300650 mutex_unlock(&mdev->mutex);
651 return -EBUSY;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300652 }
653 mdev->current_output = i;
654 vfd->tvnorms = 0;
655 v4l2_subdev_call(to_outsd(mdev), video, g_tvnorms_output,
656 &vfd->tvnorms);
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300657 mutex_unlock(&mdev->mutex);
658
659 /* update layers geometry */
660 mxr_layer_update_output(layer);
661
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300662 mxr_dbg(mdev, "tvnorms = %08llx\n", vfd->tvnorms);
663
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300664 return 0;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300665}
666
667static int mxr_g_output(struct file *file, void *fh, unsigned int *p)
668{
669 struct mxr_layer *layer = video_drvdata(file);
670 struct mxr_device *mdev = layer->mdev;
671
672 mutex_lock(&mdev->mutex);
673 *p = mdev->current_output;
674 mutex_unlock(&mdev->mutex);
675
676 return 0;
677}
678
679static int mxr_reqbufs(struct file *file, void *priv,
680 struct v4l2_requestbuffers *p)
681{
682 struct mxr_layer *layer = video_drvdata(file);
683
684 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
685 return vb2_reqbufs(&layer->vb_queue, p);
686}
687
688static int mxr_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
689{
690 struct mxr_layer *layer = video_drvdata(file);
691
692 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
693 return vb2_querybuf(&layer->vb_queue, p);
694}
695
696static int mxr_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
697{
698 struct mxr_layer *layer = video_drvdata(file);
699
700 mxr_dbg(layer->mdev, "%s:%d(%d)\n", __func__, __LINE__, p->index);
701 return vb2_qbuf(&layer->vb_queue, p);
702}
703
704static int mxr_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
705{
706 struct mxr_layer *layer = video_drvdata(file);
707
708 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
709 return vb2_dqbuf(&layer->vb_queue, p, file->f_flags & O_NONBLOCK);
710}
711
Tomasz Stanislawski3c44efd2012-06-14 11:32:27 -0300712static int mxr_expbuf(struct file *file, void *priv,
713 struct v4l2_exportbuffer *eb)
714{
715 struct mxr_layer *layer = video_drvdata(file);
716
717 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
718 return vb2_expbuf(&layer->vb_queue, eb);
719}
720
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300721static int mxr_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
722{
723 struct mxr_layer *layer = video_drvdata(file);
724
725 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
726 return vb2_streamon(&layer->vb_queue, i);
727}
728
729static int mxr_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
730{
731 struct mxr_layer *layer = video_drvdata(file);
732
733 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
734 return vb2_streamoff(&layer->vb_queue, i);
735}
736
737static const struct v4l2_ioctl_ops mxr_ioctl_ops = {
738 .vidioc_querycap = mxr_querycap,
739 /* format handling */
Sylwester Nawrocki72085072012-09-24 06:03:04 -0300740 .vidioc_enum_fmt_vid_out_mplane = mxr_enum_fmt,
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300741 .vidioc_s_fmt_vid_out_mplane = mxr_s_fmt,
742 .vidioc_g_fmt_vid_out_mplane = mxr_g_fmt,
743 /* buffer control */
744 .vidioc_reqbufs = mxr_reqbufs,
745 .vidioc_querybuf = mxr_querybuf,
746 .vidioc_qbuf = mxr_qbuf,
747 .vidioc_dqbuf = mxr_dqbuf,
Tomasz Stanislawski3c44efd2012-06-14 11:32:27 -0300748 .vidioc_expbuf = mxr_expbuf,
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300749 /* Streaming control */
750 .vidioc_streamon = mxr_streamon,
751 .vidioc_streamoff = mxr_streamoff,
Hans Verkuil8e42bf02013-03-04 09:30:48 -0300752 /* DV Timings functions */
753 .vidioc_enum_dv_timings = mxr_enum_dv_timings,
754 .vidioc_s_dv_timings = mxr_s_dv_timings,
755 .vidioc_g_dv_timings = mxr_g_dv_timings,
756 .vidioc_dv_timings_cap = mxr_dv_timings_cap,
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300757 /* analog TV standard functions */
758 .vidioc_s_std = mxr_s_std,
759 .vidioc_g_std = mxr_g_std,
760 /* Output handling */
761 .vidioc_enum_output = mxr_enum_output,
762 .vidioc_s_output = mxr_s_output,
763 .vidioc_g_output = mxr_g_output,
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300764 /* selection ioctls */
765 .vidioc_g_selection = mxr_g_selection,
766 .vidioc_s_selection = mxr_s_selection,
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300767};
768
769static int mxr_video_open(struct file *file)
770{
771 struct mxr_layer *layer = video_drvdata(file);
772 struct mxr_device *mdev = layer->mdev;
773 int ret = 0;
774
775 mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);
Hans Verkuil00ccdc32012-06-24 07:02:01 -0300776 if (mutex_lock_interruptible(&layer->mutex))
777 return -ERESTARTSYS;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300778 /* assure device probe is finished */
779 wait_for_device_probe();
780 /* creating context for file descriptor */
781 ret = v4l2_fh_open(file);
782 if (ret) {
783 mxr_err(mdev, "v4l2_fh_open failed\n");
Hans Verkuil00ccdc32012-06-24 07:02:01 -0300784 goto unlock;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300785 }
786
787 /* leaving if layer is already initialized */
788 if (!v4l2_fh_is_singular_file(file))
Hans Verkuil00ccdc32012-06-24 07:02:01 -0300789 goto unlock;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300790
791 /* FIXME: should power be enabled on open? */
792 ret = mxr_power_get(mdev);
793 if (ret) {
794 mxr_err(mdev, "power on failed\n");
795 goto fail_fh_open;
796 }
797
798 ret = vb2_queue_init(&layer->vb_queue);
799 if (ret != 0) {
800 mxr_err(mdev, "failed to initialize vb2 queue\n");
801 goto fail_power;
802 }
803 /* set default format, first on the list */
804 layer->fmt = layer->fmt_array[0];
805 /* setup default geometry */
806 mxr_layer_default_geo(layer);
Hans Verkuil00ccdc32012-06-24 07:02:01 -0300807 mutex_unlock(&layer->mutex);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300808
809 return 0;
810
811fail_power:
812 mxr_power_put(mdev);
813
814fail_fh_open:
815 v4l2_fh_release(file);
816
Hans Verkuil00ccdc32012-06-24 07:02:01 -0300817unlock:
818 mutex_unlock(&layer->mutex);
819
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300820 return ret;
821}
822
823static unsigned int
824mxr_video_poll(struct file *file, struct poll_table_struct *wait)
825{
826 struct mxr_layer *layer = video_drvdata(file);
Hans Verkuil00ccdc32012-06-24 07:02:01 -0300827 unsigned int res;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300828
829 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
830
Hans Verkuil00ccdc32012-06-24 07:02:01 -0300831 mutex_lock(&layer->mutex);
832 res = vb2_poll(&layer->vb_queue, file, wait);
833 mutex_unlock(&layer->mutex);
834 return res;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300835}
836
837static int mxr_video_mmap(struct file *file, struct vm_area_struct *vma)
838{
839 struct mxr_layer *layer = video_drvdata(file);
Hans Verkuil00ccdc32012-06-24 07:02:01 -0300840 int ret;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300841
842 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
843
Hans Verkuil00ccdc32012-06-24 07:02:01 -0300844 if (mutex_lock_interruptible(&layer->mutex))
845 return -ERESTARTSYS;
846 ret = vb2_mmap(&layer->vb_queue, vma);
847 mutex_unlock(&layer->mutex);
848 return ret;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300849}
850
851static int mxr_video_release(struct file *file)
852{
853 struct mxr_layer *layer = video_drvdata(file);
854
855 mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
Hans Verkuil00ccdc32012-06-24 07:02:01 -0300856 mutex_lock(&layer->mutex);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300857 if (v4l2_fh_is_singular_file(file)) {
858 vb2_queue_release(&layer->vb_queue);
859 mxr_power_put(layer->mdev);
860 }
861 v4l2_fh_release(file);
Hans Verkuil00ccdc32012-06-24 07:02:01 -0300862 mutex_unlock(&layer->mutex);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300863 return 0;
864}
865
866static const struct v4l2_file_operations mxr_fops = {
867 .owner = THIS_MODULE,
868 .open = mxr_video_open,
869 .poll = mxr_video_poll,
870 .mmap = mxr_video_mmap,
871 .release = mxr_video_release,
872 .unlocked_ioctl = video_ioctl2,
873};
874
Hans Verkuildf9ecb02015-10-28 00:50:37 -0200875static int queue_setup(struct vb2_queue *vq,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300876 unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[],
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300877 void *alloc_ctxs[])
878{
879 struct mxr_layer *layer = vb2_get_drv_priv(vq);
880 const struct mxr_format *fmt = layer->fmt;
881 int i;
882 struct mxr_device *mdev = layer->mdev;
883 struct v4l2_plane_pix_format planes[3];
884
885 mxr_dbg(mdev, "%s\n", __func__);
886 /* checking if format was configured */
887 if (fmt == NULL)
888 return -EINVAL;
889 mxr_dbg(mdev, "fmt = %s\n", fmt->name);
890 mxr_mplane_fill(planes, fmt, layer->geo.src.full_width,
891 layer->geo.src.full_height);
892
893 *nplanes = fmt->num_subframes;
894 for (i = 0; i < fmt->num_subframes; ++i) {
Marek Szyprowskic1bf9c62011-10-21 04:56:12 -0300895 sizes[i] = planes[i].sizeimage;
Tomasz Stanislawskic31e3c42012-03-09 07:07:28 -0300896 mxr_dbg(mdev, "size[%d] = %08x\n", i, sizes[i]);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300897 }
898
899 if (*nbuffers == 0)
900 *nbuffers = 1;
901
902 return 0;
903}
904
905static void buf_queue(struct vb2_buffer *vb)
906{
Junghak Sung2d700712015-09-22 10:30:30 -0300907 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
908 struct mxr_buffer *buffer = container_of(vbuf, struct mxr_buffer, vb);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300909 struct mxr_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
910 struct mxr_device *mdev = layer->mdev;
911 unsigned long flags;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300912
913 spin_lock_irqsave(&layer->enq_slock, flags);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300914 list_add_tail(&buffer->list, &layer->enq_list);
915 spin_unlock_irqrestore(&layer->enq_slock, flags);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300916
917 mxr_dbg(mdev, "queuing buffer\n");
918}
919
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300920static int start_streaming(struct vb2_queue *vq, unsigned int count)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300921{
922 struct mxr_layer *layer = vb2_get_drv_priv(vq);
923 struct mxr_device *mdev = layer->mdev;
924 unsigned long flags;
925
926 mxr_dbg(mdev, "%s\n", __func__);
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300927
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300928 /* block any changes in output configuration */
929 mxr_output_get(mdev);
930
Tomasz Stanislawski0d066d32011-08-25 07:14:26 -0300931 mxr_layer_update_output(layer);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300932 layer->ops.format_set(layer);
933 /* enabling layer in hardware */
934 spin_lock_irqsave(&layer->enq_slock, flags);
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300935 layer->state = MXR_LAYER_STREAMING;
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300936 spin_unlock_irqrestore(&layer->enq_slock, flags);
937
Marek Szyprowskibd323e22011-08-29 08:51:49 -0300938 layer->ops.stream_set(layer, MXR_ENABLE);
939 mxr_streamer_get(mdev);
940
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300941 return 0;
942}
943
944static void mxr_watchdog(unsigned long arg)
945{
946 struct mxr_layer *layer = (struct mxr_layer *) arg;
947 struct mxr_device *mdev = layer->mdev;
948 unsigned long flags;
949
950 mxr_err(mdev, "watchdog fired for layer %s\n", layer->vfd.name);
951
952 spin_lock_irqsave(&layer->enq_slock, flags);
953
954 if (layer->update_buf == layer->shadow_buf)
955 layer->update_buf = NULL;
956 if (layer->update_buf) {
Junghak Sung2d700712015-09-22 10:30:30 -0300957 vb2_buffer_done(&layer->update_buf->vb.vb2_buf,
958 VB2_BUF_STATE_ERROR);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300959 layer->update_buf = NULL;
960 }
961 if (layer->shadow_buf) {
Junghak Sung2d700712015-09-22 10:30:30 -0300962 vb2_buffer_done(&layer->shadow_buf->vb.vb2_buf,
963 VB2_BUF_STATE_ERROR);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300964 layer->shadow_buf = NULL;
965 }
966 spin_unlock_irqrestore(&layer->enq_slock, flags);
967}
968
Hans Verkuile37559b2014-04-17 02:47:21 -0300969static void stop_streaming(struct vb2_queue *vq)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300970{
971 struct mxr_layer *layer = vb2_get_drv_priv(vq);
972 struct mxr_device *mdev = layer->mdev;
973 unsigned long flags;
974 struct timer_list watchdog;
975 struct mxr_buffer *buf, *buf_tmp;
976
977 mxr_dbg(mdev, "%s\n", __func__);
978
979 spin_lock_irqsave(&layer->enq_slock, flags);
980
981 /* reset list */
982 layer->state = MXR_LAYER_STREAMING_FINISH;
983
984 /* set all buffer to be done */
985 list_for_each_entry_safe(buf, buf_tmp, &layer->enq_list, list) {
986 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300987 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -0300988 }
989
990 spin_unlock_irqrestore(&layer->enq_slock, flags);
991
992 /* give 1 seconds to complete to complete last buffers */
993 setup_timer_on_stack(&watchdog, mxr_watchdog,
994 (unsigned long)layer);
995 mod_timer(&watchdog, jiffies + msecs_to_jiffies(1000));
996
997 /* wait until all buffers are goes to done state */
998 vb2_wait_for_all_buffers(vq);
999
1000 /* stop timer if all synchronization is done */
1001 del_timer_sync(&watchdog);
1002 destroy_timer_on_stack(&watchdog);
1003
1004 /* stopping hardware */
1005 spin_lock_irqsave(&layer->enq_slock, flags);
1006 layer->state = MXR_LAYER_IDLE;
1007 spin_unlock_irqrestore(&layer->enq_slock, flags);
1008
1009 /* disabling layer in hardware */
1010 layer->ops.stream_set(layer, MXR_DISABLE);
1011 /* remove one streamer */
1012 mxr_streamer_put(mdev);
1013 /* allow changes in output configuration */
1014 mxr_output_put(mdev);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001015}
1016
1017static struct vb2_ops mxr_video_qops = {
1018 .queue_setup = queue_setup,
1019 .buf_queue = buf_queue,
Prabhakar Ladc7474042014-11-26 19:42:30 -03001020 .wait_prepare = vb2_ops_wait_prepare,
1021 .wait_finish = vb2_ops_wait_finish,
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001022 .start_streaming = start_streaming,
1023 .stop_streaming = stop_streaming,
1024};
1025
1026/* FIXME: try to put this functions to mxr_base_layer_create */
1027int mxr_base_layer_register(struct mxr_layer *layer)
1028{
1029 struct mxr_device *mdev = layer->mdev;
1030 int ret;
1031
1032 ret = video_register_device(&layer->vfd, VFL_TYPE_GRABBER, -1);
1033 if (ret)
1034 mxr_err(mdev, "failed to register video device\n");
1035 else
1036 mxr_info(mdev, "registered layer %s as /dev/video%d\n",
1037 layer->vfd.name, layer->vfd.num);
1038 return ret;
1039}
1040
1041void mxr_base_layer_unregister(struct mxr_layer *layer)
1042{
1043 video_unregister_device(&layer->vfd);
1044}
1045
1046void mxr_layer_release(struct mxr_layer *layer)
1047{
1048 if (layer->ops.release)
1049 layer->ops.release(layer);
1050}
1051
1052void mxr_base_layer_release(struct mxr_layer *layer)
1053{
1054 kfree(layer);
1055}
1056
1057static void mxr_vfd_release(struct video_device *vdev)
1058{
Sachin Kamatf5c99032012-06-11 06:13:52 -03001059 pr_info("video device release\n");
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001060}
1061
1062struct mxr_layer *mxr_base_layer_create(struct mxr_device *mdev,
Julia Lawalladbedc22015-11-15 18:08:23 -02001063 int idx, char *name, const struct mxr_layer_ops *ops)
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001064{
1065 struct mxr_layer *layer;
1066
Sachin Kamatc0d51202012-11-26 01:49:01 -03001067 layer = kzalloc(sizeof(*layer), GFP_KERNEL);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001068 if (layer == NULL) {
1069 mxr_err(mdev, "not enough memory for layer.\n");
1070 goto fail;
1071 }
1072
1073 layer->mdev = mdev;
1074 layer->idx = idx;
1075 layer->ops = *ops;
1076
1077 spin_lock_init(&layer->enq_slock);
1078 INIT_LIST_HEAD(&layer->enq_list);
1079 mutex_init(&layer->mutex);
1080
1081 layer->vfd = (struct video_device) {
1082 .minor = -1,
1083 .release = mxr_vfd_release,
1084 .fops = &mxr_fops,
Hans Verkuil954f3402012-09-05 06:05:50 -03001085 .vfl_dir = VFL_DIR_TX,
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001086 .ioctl_ops = &mxr_ioctl_ops,
1087 };
1088 strlcpy(layer->vfd.name, name, sizeof(layer->vfd.name));
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001089
1090 video_set_drvdata(&layer->vfd, layer);
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001091 layer->vfd.lock = &layer->mutex;
1092 layer->vfd.v4l2_dev = &mdev->v4l2_dev;
1093
1094 layer->vb_queue = (struct vb2_queue) {
1095 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
Tomasz Stanislawskifa775212012-06-14 10:37:48 -03001096 .io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF,
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001097 .drv_priv = layer,
1098 .buf_struct_size = sizeof(struct mxr_buffer),
1099 .ops = &mxr_video_qops,
Hans Verkuilb3379c62014-02-24 13:51:03 -03001100 .min_buffers_needed = 1,
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001101 .mem_ops = &vb2_dma_contig_memops,
Prabhakar Ladc7474042014-11-26 19:42:30 -03001102 .lock = &layer->mutex,
Hans Verkuilc781e4a2016-02-15 14:25:09 -02001103 .dev = mdev->dev,
Tomasz Stanislawskifef1c8d2011-02-02 05:40:08 -03001104 };
1105
1106 return layer;
1107
1108fail:
1109 return NULL;
1110}
1111
1112static const struct mxr_format *find_format_by_fourcc(
1113 struct mxr_layer *layer, unsigned long fourcc)
1114{
1115 int i;
1116
1117 for (i = 0; i < layer->fmt_array_size; ++i)
1118 if (layer->fmt_array[i]->fourcc == fourcc)
1119 return layer->fmt_array[i];
1120 return NULL;
1121}
1122
1123static const struct mxr_format *find_format_by_index(
1124 struct mxr_layer *layer, unsigned long index)
1125{
1126 if (index >= layer->fmt_array_size)
1127 return NULL;
1128 return layer->fmt_array[index];
1129}
1130