mixer_video.c 29.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Samsung TV Mixer driver
 *
 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
 *
 * Tomasz Stanislawski, <t.stanislaws@samsung.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation. either version 2 of the License,
 * or (at your option) any later version
 */

14 15
#define pr_fmt(fmt) "s5p-tv (mixer): " fmt

16 17 18 19 20
#include "mixer.h"

#include <media/v4l2-ioctl.h>
#include <linux/videodev2.h>
#include <linux/mm.h>
21
#include <linux/module.h>
22
#include <linux/platform_device.h>
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#include <linux/timer.h>
#include <media/videobuf2-dma-contig.h>

static int find_reg_callback(struct device *dev, void *p)
{
	struct v4l2_subdev **sd = p;

	*sd = dev_get_drvdata(dev);
	/* non-zero value stops iteration */
	return 1;
}

static struct v4l2_subdev *find_and_register_subdev(
	struct mxr_device *mdev, char *module_name)
{
	struct device_driver *drv;
	struct v4l2_subdev *sd = NULL;
	int ret;

	/* TODO: add waiting until probe is finished */
	drv = driver_find(module_name, &platform_bus_type);
	if (!drv) {
		mxr_warn(mdev, "module %s is missing\n", module_name);
		return NULL;
	}
	/* driver refcnt is increased, it is safe to iterate over devices */
	ret = driver_for_each_device(drv, NULL, &sd, find_reg_callback);
	/* ret == 0 means that find_reg_callback was never executed */
	if (sd == NULL) {
		mxr_warn(mdev, "module %s provides no subdev!\n", module_name);
		goto done;
	}
	/* v4l2_device_register_subdev detects if sd is NULL */
	ret = v4l2_device_register_subdev(&mdev->v4l2_dev, sd);
	if (ret) {
		mxr_warn(mdev, "failed to register subdev %s\n", sd->name);
		sd = NULL;
	}

done:
	return sd;
}

66 67
int mxr_acquire_video(struct mxr_device *mdev,
		      struct mxr_output_conf *output_conf, int output_count)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
{
	struct device *dev = mdev->dev;
	struct v4l2_device *v4l2_dev = &mdev->v4l2_dev;
	int i;
	int ret = 0;
	struct v4l2_subdev *sd;

	strlcpy(v4l2_dev->name, dev_name(mdev->dev), sizeof(v4l2_dev->name));
	/* prepare context for V4L2 device */
	ret = v4l2_device_register(dev, v4l2_dev);
	if (ret) {
		mxr_err(mdev, "could not register v4l2 device.\n");
		goto fail;
	}

	mdev->alloc_ctx = vb2_dma_contig_init_ctx(mdev->dev);
84
	if (IS_ERR(mdev->alloc_ctx)) {
85
		mxr_err(mdev, "could not acquire vb2 allocator\n");
86
		ret = PTR_ERR(mdev->alloc_ctx);
87 88 89 90 91 92 93 94 95 96 97 98 99
		goto fail_v4l2_dev;
	}

	/* registering outputs */
	mdev->output_cnt = 0;
	for (i = 0; i < output_count; ++i) {
		struct mxr_output_conf *conf = &output_conf[i];
		struct mxr_output *out;

		sd = find_and_register_subdev(mdev, conf->module_name);
		/* trying to register next output */
		if (sd == NULL)
			continue;
100
		out = kzalloc(sizeof(*out), GFP_KERNEL);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		if (out == NULL) {
			mxr_err(mdev, "no memory for '%s'\n",
				conf->output_name);
			ret = -ENOMEM;
			/* registered subdevs are removed in fail_v4l2_dev */
			goto fail_output;
		}
		strlcpy(out->name, conf->output_name, sizeof(out->name));
		out->sd = sd;
		out->cookie = conf->cookie;
		mdev->output[mdev->output_cnt++] = out;
		mxr_info(mdev, "added output '%s' from module '%s'\n",
			conf->output_name, conf->module_name);
		/* checking if maximal number of outputs is reached */
		if (mdev->output_cnt >= MXR_MAX_OUTPUTS)
			break;
	}

	if (mdev->output_cnt == 0) {
		mxr_err(mdev, "failed to register any output\n");
		ret = -ENODEV;
		/* skipping fail_output because there is nothing to free */
		goto fail_vb2_allocator;
	}

	return 0;

fail_output:
	/* kfree is NULL-safe */
	for (i = 0; i < mdev->output_cnt; ++i)
		kfree(mdev->output[i]);
132
	memset(mdev->output, 0, sizeof(mdev->output));
133 134 135 136 137 138 139 140 141 142 143 144 145

fail_vb2_allocator:
	/* freeing allocator context */
	vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx);

fail_v4l2_dev:
	/* NOTE: automatically unregister all subdevs */
	v4l2_device_unregister(v4l2_dev);

fail:
	return ret;
}

146
void mxr_release_video(struct mxr_device *mdev)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
{
	int i;

	/* kfree is NULL-safe */
	for (i = 0; i < mdev->output_cnt; ++i)
		kfree(mdev->output[i]);

	vb2_dma_contig_cleanup_ctx(mdev->alloc_ctx);
	v4l2_device_unregister(&mdev->v4l2_dev);
}

static int mxr_querycap(struct file *file, void *priv,
	struct v4l2_capability *cap)
{
	struct mxr_layer *layer = video_drvdata(file);

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);

165 166
	strlcpy(cap->driver, MXR_DRIVER_NAME, sizeof(cap->driver));
	strlcpy(cap->card, layer->vfd.name, sizeof(cap->card));
167
	sprintf(cap->bus_info, "%d", layer->idx);
168 169
	cap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
170 171 172 173

	return 0;
}

174
static void mxr_geometry_dump(struct mxr_device *mdev, struct mxr_geometry *geo)
175
{
176 177 178 179 180 181 182 183 184 185 186 187 188 189
	mxr_dbg(mdev, "src.full_size = (%u, %u)\n",
		geo->src.full_width, geo->src.full_height);
	mxr_dbg(mdev, "src.size = (%u, %u)\n",
		geo->src.width, geo->src.height);
	mxr_dbg(mdev, "src.offset = (%u, %u)\n",
		geo->src.x_offset, geo->src.y_offset);
	mxr_dbg(mdev, "dst.full_size = (%u, %u)\n",
		geo->dst.full_width, geo->dst.full_height);
	mxr_dbg(mdev, "dst.size = (%u, %u)\n",
		geo->dst.width, geo->dst.height);
	mxr_dbg(mdev, "dst.offset = (%u, %u)\n",
		geo->dst.x_offset, geo->dst.y_offset);
	mxr_dbg(mdev, "ratio = (%u, %u)\n",
		geo->x_ratio, geo->y_ratio);
190 191 192 193 194 195 196
}

static void mxr_layer_default_geo(struct mxr_layer *layer)
{
	struct mxr_device *mdev = layer->mdev;
	struct v4l2_mbus_framefmt mbus_fmt;

197
	memset(&layer->geo, 0, sizeof(layer->geo));
198 199 200 201 202 203 204 205 206 207 208 209 210 211

	mxr_get_mbus_fmt(mdev, &mbus_fmt);

	layer->geo.dst.full_width = mbus_fmt.width;
	layer->geo.dst.full_height = mbus_fmt.height;
	layer->geo.dst.width = layer->geo.dst.full_width;
	layer->geo.dst.height = layer->geo.dst.full_height;
	layer->geo.dst.field = mbus_fmt.field;

	layer->geo.src.full_width = mbus_fmt.width;
	layer->geo.src.full_height = mbus_fmt.height;
	layer->geo.src.width = layer->geo.src.full_width;
	layer->geo.src.height = layer->geo.src.full_height;

212 213 214
	mxr_geometry_dump(mdev, &layer->geo);
	layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);
	mxr_geometry_dump(mdev, &layer->geo);
215 216
}

217
static void mxr_layer_update_output(struct mxr_layer *layer)
218
{
219 220 221 222 223 224 225 226
	struct mxr_device *mdev = layer->mdev;
	struct v4l2_mbus_framefmt mbus_fmt;

	mxr_get_mbus_fmt(mdev, &mbus_fmt);
	/* checking if update is needed */
	if (layer->geo.dst.full_width == mbus_fmt.width &&
		layer->geo.dst.full_height == mbus_fmt.width)
		return;
227

228 229 230 231 232 233 234
	layer->geo.dst.full_width = mbus_fmt.width;
	layer->geo.dst.full_height = mbus_fmt.height;
	layer->geo.dst.field = mbus_fmt.field;
	layer->ops.fix_geometry(layer, MXR_GEOMETRY_SINK, 0);

	mxr_geometry_dump(mdev, &layer->geo);
}
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

static const struct mxr_format *find_format_by_fourcc(
	struct mxr_layer *layer, unsigned long fourcc);
static const struct mxr_format *find_format_by_index(
	struct mxr_layer *layer, unsigned long index);

static int mxr_enum_fmt(struct file *file, void  *priv,
	struct v4l2_fmtdesc *f)
{
	struct mxr_layer *layer = video_drvdata(file);
	struct mxr_device *mdev = layer->mdev;
	const struct mxr_format *fmt;

	mxr_dbg(mdev, "%s\n", __func__);
	fmt = find_format_by_index(layer, f->index);
	if (fmt == NULL)
		return -EINVAL;

	strlcpy(f->description, fmt->name, sizeof(f->description));
	f->pixelformat = fmt->fourcc;

	return 0;
}

static unsigned int divup(unsigned int divident, unsigned int divisor)
{
	return (divident + divisor - 1) / divisor;
}

unsigned long mxr_get_plane_size(const struct mxr_block *blk,
	unsigned int width, unsigned int height)
{
	unsigned int bl_width = divup(width, blk->width);
	unsigned int bl_height = divup(height, blk->height);

	return bl_width * bl_height * blk->size;
}

static void mxr_mplane_fill(struct v4l2_plane_pix_format *planes,
	const struct mxr_format *fmt, u32 width, u32 height)
{
	int i;

278 279 280 281
	/* checking if nothing to fill */
	if (!planes)
		return;

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	memset(planes, 0, sizeof(*planes) * fmt->num_subframes);
	for (i = 0; i < fmt->num_planes; ++i) {
		struct v4l2_plane_pix_format *plane = planes
			+ fmt->plane2subframe[i];
		const struct mxr_block *blk = &fmt->plane[i];
		u32 bl_width = divup(width, blk->width);
		u32 bl_height = divup(height, blk->height);
		u32 sizeimage = bl_width * bl_height * blk->size;
		u16 bytesperline = bl_width * blk->size / blk->height;

		plane->sizeimage += sizeimage;
		plane->bytesperline = max(plane->bytesperline, bytesperline);
	}
}

static int mxr_g_fmt(struct file *file, void *priv,
			     struct v4l2_format *f)
{
	struct mxr_layer *layer = video_drvdata(file);
	struct v4l2_pix_format_mplane *pix = &f->fmt.pix_mp;

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);

	pix->width = layer->geo.src.full_width;
	pix->height = layer->geo.src.full_height;
	pix->field = V4L2_FIELD_NONE;
	pix->pixelformat = layer->fmt->fourcc;
	pix->colorspace = layer->fmt->colorspace;
	mxr_mplane_fill(pix->plane_fmt, layer->fmt, pix->width, pix->height);

	return 0;
}

315 316
static int mxr_s_fmt(struct file *file, void *priv,
	struct v4l2_format *f)
317 318
{
	struct mxr_layer *layer = video_drvdata(file);
319 320 321 322
	const struct mxr_format *fmt;
	struct v4l2_pix_format_mplane *pix;
	struct mxr_device *mdev = layer->mdev;
	struct mxr_geometry *geo = &layer->geo;
323

324 325 326 327 328 329 330
	mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);

	pix = &f->fmt.pix_mp;
	fmt = find_format_by_fourcc(layer, pix->pixelformat);
	if (fmt == NULL) {
		mxr_warn(mdev, "not recognized fourcc: %08x\n",
			pix->pixelformat);
331
		return -EINVAL;
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	}
	layer->fmt = fmt;
	/* set source size to highest accepted value */
	geo->src.full_width = max(geo->dst.full_width, pix->width);
	geo->src.full_height = max(geo->dst.full_height, pix->height);
	layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
	mxr_geometry_dump(mdev, &layer->geo);
	/* set cropping to total visible screen */
	geo->src.width = pix->width;
	geo->src.height = pix->height;
	geo->src.x_offset = 0;
	geo->src.y_offset = 0;
	/* assure consistency of geometry */
	layer->ops.fix_geometry(layer, MXR_GEOMETRY_CROP, MXR_NO_OFFSET);
	mxr_geometry_dump(mdev, &layer->geo);
	/* set full size to lowest possible value */
	geo->src.full_width = 0;
	geo->src.full_height = 0;
	layer->ops.fix_geometry(layer, MXR_GEOMETRY_SOURCE, 0);
	mxr_geometry_dump(mdev, &layer->geo);

	/* returning results */
	mxr_g_fmt(file, priv, f);

356 357 358
	return 0;
}

359 360
static int mxr_g_selection(struct file *file, void *fh,
	struct v4l2_selection *s)
361 362
{
	struct mxr_layer *layer = video_drvdata(file);
363
	struct mxr_geometry *geo = &layer->geo;
364 365

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
366 367 368 369 370 371

	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
		s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
		return -EINVAL;

	switch (s->target) {
372
	case V4L2_SEL_TGT_CROP:
373 374 375 376 377 378 379 380 381 382 383 384
		s->r.left = geo->src.x_offset;
		s->r.top = geo->src.y_offset;
		s->r.width = geo->src.width;
		s->r.height = geo->src.height;
		break;
	case V4L2_SEL_TGT_CROP_DEFAULT:
	case V4L2_SEL_TGT_CROP_BOUNDS:
		s->r.left = 0;
		s->r.top = 0;
		s->r.width = geo->src.full_width;
		s->r.height = geo->src.full_height;
		break;
385
	case V4L2_SEL_TGT_COMPOSE:
386 387 388 389 390 391 392 393 394 395 396 397 398 399
	case V4L2_SEL_TGT_COMPOSE_PADDED:
		s->r.left = geo->dst.x_offset;
		s->r.top = geo->dst.y_offset;
		s->r.width = geo->dst.width;
		s->r.height = geo->dst.height;
		break;
	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
		s->r.left = 0;
		s->r.top = 0;
		s->r.width = geo->dst.full_width;
		s->r.height = geo->dst.full_height;
		break;
	default:
400
		return -EINVAL;
401 402
	}

403 404 405
	return 0;
}

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
/* returns 1 if rectangle 'a' is inside 'b' */
static int mxr_is_rect_inside(struct v4l2_rect *a, struct v4l2_rect *b)
{
	if (a->left < b->left)
		return 0;
	if (a->top < b->top)
		return 0;
	if (a->left + a->width > b->left + b->width)
		return 0;
	if (a->top + a->height > b->top + b->height)
		return 0;
	return 1;
}

static int mxr_s_selection(struct file *file, void *fh,
	struct v4l2_selection *s)
422 423
{
	struct mxr_layer *layer = video_drvdata(file);
424 425 426 427 428
	struct mxr_geometry *geo = &layer->geo;
	struct mxr_crop *target = NULL;
	enum mxr_geometry_stage stage;
	struct mxr_geometry tmp;
	struct v4l2_rect res;
429

430
	memset(&res, 0, sizeof(res));
431 432 433 434 435 436

	mxr_dbg(layer->mdev, "%s: rect: %dx%d@%d,%d\n", __func__,
		s->r.width, s->r.height, s->r.left, s->r.top);

	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
		s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
437
		return -EINVAL;
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

	switch (s->target) {
	/* ignore read-only targets */
	case V4L2_SEL_TGT_CROP_DEFAULT:
	case V4L2_SEL_TGT_CROP_BOUNDS:
		res.width = geo->src.full_width;
		res.height = geo->src.full_height;
		break;

	/* ignore read-only targets */
	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
		res.width = geo->dst.full_width;
		res.height = geo->dst.full_height;
		break;

454
	case V4L2_SEL_TGT_CROP:
455 456 457
		target = &geo->src;
		stage = MXR_GEOMETRY_CROP;
		break;
458
	case V4L2_SEL_TGT_COMPOSE:
459 460 461 462 463 464 465 466 467 468
	case V4L2_SEL_TGT_COMPOSE_PADDED:
		target = &geo->dst;
		stage = MXR_GEOMETRY_COMPOSE;
		break;
	default:
		return -EINVAL;
	}
	/* apply change and update geometry if needed */
	if (target) {
		/* backup current geometry if setup fails */
469
		memcpy(&tmp, geo, sizeof(tmp));
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496

		/* apply requested selection */
		target->x_offset = s->r.left;
		target->y_offset = s->r.top;
		target->width = s->r.width;
		target->height = s->r.height;

		layer->ops.fix_geometry(layer, stage, s->flags);

		/* retrieve update selection rectangle */
		res.left = target->x_offset;
		res.top = target->y_offset;
		res.width = target->width;
		res.height = target->height;

		mxr_geometry_dump(layer->mdev, &layer->geo);
	}

	/* checking if the rectangle satisfies constraints */
	if ((s->flags & V4L2_SEL_FLAG_LE) && !mxr_is_rect_inside(&res, &s->r))
		goto fail;
	if ((s->flags & V4L2_SEL_FLAG_GE) && !mxr_is_rect_inside(&s->r, &res))
		goto fail;

	/* return result rectangle */
	s->r = res;

497
	return 0;
498 499 500
fail:
	/* restore old geometry, which is not touched if target is NULL */
	if (target)
501
		memcpy(geo, &tmp, sizeof(tmp));
502
	return -ERANGE;
503 504
}

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
static int mxr_enum_dv_timings(struct file *file, void *fh,
	struct v4l2_enum_dv_timings *timings)
{
	struct mxr_layer *layer = video_drvdata(file);
	struct mxr_device *mdev = layer->mdev;
	int ret;

	/* lock protects from changing sd_out */
	mutex_lock(&mdev->mutex);
	ret = v4l2_subdev_call(to_outsd(mdev), video, enum_dv_timings, timings);
	mutex_unlock(&mdev->mutex);

	return ret ? -EINVAL : 0;
}

static int mxr_s_dv_timings(struct file *file, void *fh,
	struct v4l2_dv_timings *timings)
{
	struct mxr_layer *layer = video_drvdata(file);
	struct mxr_device *mdev = layer->mdev;
	int ret;

	/* lock protects from changing sd_out */
	mutex_lock(&mdev->mutex);

	/* timings change cannot be done while there is an entity
531
	 * dependent on output configuration
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
	 */
	if (mdev->n_output > 0) {
		mutex_unlock(&mdev->mutex);
		return -EBUSY;
	}

	ret = v4l2_subdev_call(to_outsd(mdev), video, s_dv_timings, timings);

	mutex_unlock(&mdev->mutex);

	mxr_layer_update_output(layer);

	/* any failure should return EINVAL according to V4L2 doc */
	return ret ? -EINVAL : 0;
}

static int mxr_g_dv_timings(struct file *file, void *fh,
	struct v4l2_dv_timings *timings)
{
	struct mxr_layer *layer = video_drvdata(file);
	struct mxr_device *mdev = layer->mdev;
	int ret;

	/* lock protects from changing sd_out */
	mutex_lock(&mdev->mutex);
	ret = v4l2_subdev_call(to_outsd(mdev), video, g_dv_timings, timings);
	mutex_unlock(&mdev->mutex);

	return ret ? -EINVAL : 0;
}

static int mxr_dv_timings_cap(struct file *file, void *fh,
	struct v4l2_dv_timings_cap *cap)
{
	struct mxr_layer *layer = video_drvdata(file);
	struct mxr_device *mdev = layer->mdev;
	int ret;

	/* lock protects from changing sd_out */
	mutex_lock(&mdev->mutex);
	ret = v4l2_subdev_call(to_outsd(mdev), video, dv_timings_cap, cap);
	mutex_unlock(&mdev->mutex);

	return ret ? -EINVAL : 0;
}

578
static int mxr_s_std(struct file *file, void *fh, v4l2_std_id norm)
579 580 581 582 583 584 585 586 587
{
	struct mxr_layer *layer = video_drvdata(file);
	struct mxr_device *mdev = layer->mdev;
	int ret;

	/* lock protects from changing sd_out */
	mutex_lock(&mdev->mutex);

	/* standard change cannot be done while there is an entity
588
	 * dependent on output configuration
589 590 591 592 593 594
	 */
	if (mdev->n_output > 0) {
		mutex_unlock(&mdev->mutex);
		return -EBUSY;
	}

595
	ret = v4l2_subdev_call(to_outsd(mdev), video, s_std_output, norm);
596 597 598

	mutex_unlock(&mdev->mutex);

599 600
	mxr_layer_update_output(layer);

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	return ret ? -EINVAL : 0;
}

static int mxr_g_std(struct file *file, void *fh, v4l2_std_id *norm)
{
	struct mxr_layer *layer = video_drvdata(file);
	struct mxr_device *mdev = layer->mdev;
	int ret;

	/* lock protects from changing sd_out */
	mutex_lock(&mdev->mutex);
	ret = v4l2_subdev_call(to_outsd(mdev), video, g_std_output, norm);
	mutex_unlock(&mdev->mutex);

	return ret ? -EINVAL : 0;
}

static int mxr_enum_output(struct file *file, void *fh, struct v4l2_output *a)
{
	struct mxr_layer *layer = video_drvdata(file);
	struct mxr_device *mdev = layer->mdev;
	struct mxr_output *out;
	struct v4l2_subdev *sd;

	if (a->index >= mdev->output_cnt)
		return -EINVAL;
	out = mdev->output[a->index];
	BUG_ON(out == NULL);
	sd = out->sd;
	strlcpy(a->name, out->name, sizeof(a->name));

	/* try to obtain supported tv norms */
	v4l2_subdev_call(sd, video, g_tvnorms_output, &a->std);
	a->capabilities = 0;
635 636
	if (sd->ops->video && sd->ops->video->s_dv_timings)
		a->capabilities |= V4L2_OUT_CAP_DV_TIMINGS;
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
	if (sd->ops->video && sd->ops->video->s_std_output)
		a->capabilities |= V4L2_OUT_CAP_STD;
	a->type = V4L2_OUTPUT_TYPE_ANALOG;

	return 0;
}

static int mxr_s_output(struct file *file, void *fh, unsigned int i)
{
	struct video_device *vfd = video_devdata(file);
	struct mxr_layer *layer = video_drvdata(file);
	struct mxr_device *mdev = layer->mdev;

	if (i >= mdev->output_cnt || mdev->output[i] == NULL)
		return -EINVAL;

	mutex_lock(&mdev->mutex);
	if (mdev->n_output > 0) {
655 656
		mutex_unlock(&mdev->mutex);
		return -EBUSY;
657 658 659 660 661
	}
	mdev->current_output = i;
	vfd->tvnorms = 0;
	v4l2_subdev_call(to_outsd(mdev), video, g_tvnorms_output,
		&vfd->tvnorms);
662 663 664 665 666
	mutex_unlock(&mdev->mutex);

	/* update layers geometry */
	mxr_layer_update_output(layer);

667 668
	mxr_dbg(mdev, "tvnorms = %08llx\n", vfd->tvnorms);

669
	return 0;
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
}

static int mxr_g_output(struct file *file, void *fh, unsigned int *p)
{
	struct mxr_layer *layer = video_drvdata(file);
	struct mxr_device *mdev = layer->mdev;

	mutex_lock(&mdev->mutex);
	*p = mdev->current_output;
	mutex_unlock(&mdev->mutex);

	return 0;
}

static int mxr_reqbufs(struct file *file, void *priv,
			  struct v4l2_requestbuffers *p)
{
	struct mxr_layer *layer = video_drvdata(file);

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
	return vb2_reqbufs(&layer->vb_queue, p);
}

static int mxr_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
{
	struct mxr_layer *layer = video_drvdata(file);

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
	return vb2_querybuf(&layer->vb_queue, p);
}

static int mxr_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
{
	struct mxr_layer *layer = video_drvdata(file);

	mxr_dbg(layer->mdev, "%s:%d(%d)\n", __func__, __LINE__, p->index);
	return vb2_qbuf(&layer->vb_queue, p);
}

static int mxr_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
{
	struct mxr_layer *layer = video_drvdata(file);

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
	return vb2_dqbuf(&layer->vb_queue, p, file->f_flags & O_NONBLOCK);
}

717 718 719 720 721 722 723 724 725
static int mxr_expbuf(struct file *file, void *priv,
	struct v4l2_exportbuffer *eb)
{
	struct mxr_layer *layer = video_drvdata(file);

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
	return vb2_expbuf(&layer->vb_queue, eb);
}

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
static int mxr_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
{
	struct mxr_layer *layer = video_drvdata(file);

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
	return vb2_streamon(&layer->vb_queue, i);
}

static int mxr_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
{
	struct mxr_layer *layer = video_drvdata(file);

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
	return vb2_streamoff(&layer->vb_queue, i);
}

static const struct v4l2_ioctl_ops mxr_ioctl_ops = {
	.vidioc_querycap = mxr_querycap,
	/* format handling */
745
	.vidioc_enum_fmt_vid_out_mplane = mxr_enum_fmt,
746 747 748 749 750 751 752
	.vidioc_s_fmt_vid_out_mplane = mxr_s_fmt,
	.vidioc_g_fmt_vid_out_mplane = mxr_g_fmt,
	/* buffer control */
	.vidioc_reqbufs = mxr_reqbufs,
	.vidioc_querybuf = mxr_querybuf,
	.vidioc_qbuf = mxr_qbuf,
	.vidioc_dqbuf = mxr_dqbuf,
753
	.vidioc_expbuf = mxr_expbuf,
754 755 756
	/* Streaming control */
	.vidioc_streamon = mxr_streamon,
	.vidioc_streamoff = mxr_streamoff,
757 758 759 760 761
	/* DV Timings functions */
	.vidioc_enum_dv_timings = mxr_enum_dv_timings,
	.vidioc_s_dv_timings = mxr_s_dv_timings,
	.vidioc_g_dv_timings = mxr_g_dv_timings,
	.vidioc_dv_timings_cap = mxr_dv_timings_cap,
762 763 764 765 766 767 768
	/* analog TV standard functions */
	.vidioc_s_std = mxr_s_std,
	.vidioc_g_std = mxr_g_std,
	/* Output handling */
	.vidioc_enum_output = mxr_enum_output,
	.vidioc_s_output = mxr_s_output,
	.vidioc_g_output = mxr_g_output,
769 770 771
	/* selection ioctls */
	.vidioc_g_selection = mxr_g_selection,
	.vidioc_s_selection = mxr_s_selection,
772 773 774 775 776 777 778 779 780
};

static int mxr_video_open(struct file *file)
{
	struct mxr_layer *layer = video_drvdata(file);
	struct mxr_device *mdev = layer->mdev;
	int ret = 0;

	mxr_dbg(mdev, "%s:%d\n", __func__, __LINE__);
781 782
	if (mutex_lock_interruptible(&layer->mutex))
		return -ERESTARTSYS;
783 784 785 786 787 788
	/* assure device probe is finished */
	wait_for_device_probe();
	/* creating context for file descriptor */
	ret = v4l2_fh_open(file);
	if (ret) {
		mxr_err(mdev, "v4l2_fh_open failed\n");
789
		goto unlock;
790 791 792 793
	}

	/* leaving if layer is already initialized */
	if (!v4l2_fh_is_singular_file(file))
794
		goto unlock;
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811

	/* FIXME: should power be enabled on open? */
	ret = mxr_power_get(mdev);
	if (ret) {
		mxr_err(mdev, "power on failed\n");
		goto fail_fh_open;
	}

	ret = vb2_queue_init(&layer->vb_queue);
	if (ret != 0) {
		mxr_err(mdev, "failed to initialize vb2 queue\n");
		goto fail_power;
	}
	/* set default format, first on the list */
	layer->fmt = layer->fmt_array[0];
	/* setup default geometry */
	mxr_layer_default_geo(layer);
812
	mutex_unlock(&layer->mutex);
813 814 815 816 817 818 819 820 821

	return 0;

fail_power:
	mxr_power_put(mdev);

fail_fh_open:
	v4l2_fh_release(file);

822 823 824
unlock:
	mutex_unlock(&layer->mutex);

825 826 827 828 829 830 831
	return ret;
}

static unsigned int
mxr_video_poll(struct file *file, struct poll_table_struct *wait)
{
	struct mxr_layer *layer = video_drvdata(file);
832
	unsigned int res;
833 834 835

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);

836 837 838 839
	mutex_lock(&layer->mutex);
	res = vb2_poll(&layer->vb_queue, file, wait);
	mutex_unlock(&layer->mutex);
	return res;
840 841 842 843 844
}

static int mxr_video_mmap(struct file *file, struct vm_area_struct *vma)
{
	struct mxr_layer *layer = video_drvdata(file);
845
	int ret;
846 847 848

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);

849 850 851 852 853
	if (mutex_lock_interruptible(&layer->mutex))
		return -ERESTARTSYS;
	ret = vb2_mmap(&layer->vb_queue, vma);
	mutex_unlock(&layer->mutex);
	return ret;
854 855 856 857 858 859 860
}

static int mxr_video_release(struct file *file)
{
	struct mxr_layer *layer = video_drvdata(file);

	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
861
	mutex_lock(&layer->mutex);
862 863 864 865 866
	if (v4l2_fh_is_singular_file(file)) {
		vb2_queue_release(&layer->vb_queue);
		mxr_power_put(layer->mdev);
	}
	v4l2_fh_release(file);
867
	mutex_unlock(&layer->mutex);
868 869 870 871 872 873 874 875 876 877 878 879
	return 0;
}

static const struct v4l2_file_operations mxr_fops = {
	.owner = THIS_MODULE,
	.open = mxr_video_open,
	.poll = mxr_video_poll,
	.mmap = mxr_video_mmap,
	.release = mxr_video_release,
	.unlocked_ioctl = video_ioctl2,
};

880 881
static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *pfmt,
	unsigned int *nbuffers, unsigned int *nplanes, unsigned int sizes[],
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
	void *alloc_ctxs[])
{
	struct mxr_layer *layer = vb2_get_drv_priv(vq);
	const struct mxr_format *fmt = layer->fmt;
	int i;
	struct mxr_device *mdev = layer->mdev;
	struct v4l2_plane_pix_format planes[3];

	mxr_dbg(mdev, "%s\n", __func__);
	/* checking if format was configured */
	if (fmt == NULL)
		return -EINVAL;
	mxr_dbg(mdev, "fmt = %s\n", fmt->name);
	mxr_mplane_fill(planes, fmt, layer->geo.src.full_width,
		layer->geo.src.full_height);

	*nplanes = fmt->num_subframes;
	for (i = 0; i < fmt->num_subframes; ++i) {
		alloc_ctxs[i] = layer->mdev->alloc_ctx;
901
		sizes[i] = planes[i].sizeimage;
902
		mxr_dbg(mdev, "size[%d] = %08x\n", i, sizes[i]);
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	}

	if (*nbuffers == 0)
		*nbuffers = 1;

	return 0;
}

static void buf_queue(struct vb2_buffer *vb)
{
	struct mxr_buffer *buffer = container_of(vb, struct mxr_buffer, vb);
	struct mxr_layer *layer = vb2_get_drv_priv(vb->vb2_queue);
	struct mxr_device *mdev = layer->mdev;
	unsigned long flags;

	spin_lock_irqsave(&layer->enq_slock, flags);
	list_add_tail(&buffer->list, &layer->enq_list);
	spin_unlock_irqrestore(&layer->enq_slock, flags);

	mxr_dbg(mdev, "queuing buffer\n");
}

static void wait_lock(struct vb2_queue *vq)
{
	struct mxr_layer *layer = vb2_get_drv_priv(vq);

	mxr_dbg(layer->mdev, "%s\n", __func__);
	mutex_lock(&layer->mutex);
}

static void wait_unlock(struct vb2_queue *vq)
{
	struct mxr_layer *layer = vb2_get_drv_priv(vq);

	mxr_dbg(layer->mdev, "%s\n", __func__);
	mutex_unlock(&layer->mutex);
}

941
static int start_streaming(struct vb2_queue *vq, unsigned int count)
942 943 944 945 946 947
{
	struct mxr_layer *layer = vb2_get_drv_priv(vq);
	struct mxr_device *mdev = layer->mdev;
	unsigned long flags;

	mxr_dbg(mdev, "%s\n", __func__);
948

949 950 951
	/* block any changes in output configuration */
	mxr_output_get(mdev);

952
	mxr_layer_update_output(layer);
953 954 955
	layer->ops.format_set(layer);
	/* enabling layer in hardware */
	spin_lock_irqsave(&layer->enq_slock, flags);
956
	layer->state = MXR_LAYER_STREAMING;
957 958
	spin_unlock_irqrestore(&layer->enq_slock, flags);

959 960 961
	layer->ops.stream_set(layer, MXR_ENABLE);
	mxr_streamer_get(mdev);

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
	return 0;
}

static void mxr_watchdog(unsigned long arg)
{
	struct mxr_layer *layer = (struct mxr_layer *) arg;
	struct mxr_device *mdev = layer->mdev;
	unsigned long flags;

	mxr_err(mdev, "watchdog fired for layer %s\n", layer->vfd.name);

	spin_lock_irqsave(&layer->enq_slock, flags);

	if (layer->update_buf == layer->shadow_buf)
		layer->update_buf = NULL;
	if (layer->update_buf) {
		vb2_buffer_done(&layer->update_buf->vb, VB2_BUF_STATE_ERROR);
		layer->update_buf = NULL;
	}
	if (layer->shadow_buf) {
		vb2_buffer_done(&layer->shadow_buf->vb, VB2_BUF_STATE_ERROR);
		layer->shadow_buf = NULL;
	}
	spin_unlock_irqrestore(&layer->enq_slock, flags);
}

static int stop_streaming(struct vb2_queue *vq)
{
	struct mxr_layer *layer = vb2_get_drv_priv(vq);
	struct mxr_device *mdev = layer->mdev;
	unsigned long flags;
	struct timer_list watchdog;
	struct mxr_buffer *buf, *buf_tmp;

	mxr_dbg(mdev, "%s\n", __func__);

	spin_lock_irqsave(&layer->enq_slock, flags);

	/* reset list */
	layer->state = MXR_LAYER_STREAMING_FINISH;

	/* set all buffer to be done */
	list_for_each_entry_safe(buf, buf_tmp, &layer->enq_list, list) {
		list_del(&buf->list);
		vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
	}

	spin_unlock_irqrestore(&layer->enq_slock, flags);

	/* give 1 seconds to complete to complete last buffers */
	setup_timer_on_stack(&watchdog, mxr_watchdog,
		(unsigned long)layer);
	mod_timer(&watchdog, jiffies + msecs_to_jiffies(1000));

	/* wait until all buffers are goes to done state */
	vb2_wait_for_all_buffers(vq);

	/* stop timer if all synchronization is done */
	del_timer_sync(&watchdog);
	destroy_timer_on_stack(&watchdog);

	/* stopping hardware */
	spin_lock_irqsave(&layer->enq_slock, flags);
	layer->state = MXR_LAYER_IDLE;
	spin_unlock_irqrestore(&layer->enq_slock, flags);

	/* disabling layer in hardware */
	layer->ops.stream_set(layer, MXR_DISABLE);
	/* remove one streamer */
	mxr_streamer_put(mdev);
	/* allow changes in output configuration */
	mxr_output_put(mdev);
	return 0;
}

static struct vb2_ops mxr_video_qops = {
	.queue_setup = queue_setup,
	.buf_queue = buf_queue,
	.wait_prepare = wait_unlock,
	.wait_finish = wait_lock,
	.start_streaming = start_streaming,
	.stop_streaming = stop_streaming,
};

/* FIXME: try to put this functions to mxr_base_layer_create */
int mxr_base_layer_register(struct mxr_layer *layer)
{
	struct mxr_device *mdev = layer->mdev;
	int ret;

	ret = video_register_device(&layer->vfd, VFL_TYPE_GRABBER, -1);
	if (ret)
		mxr_err(mdev, "failed to register video device\n");
	else
		mxr_info(mdev, "registered layer %s as /dev/video%d\n",
			layer->vfd.name, layer->vfd.num);
	return ret;
}

void mxr_base_layer_unregister(struct mxr_layer *layer)
{
	video_unregister_device(&layer->vfd);
}

void mxr_layer_release(struct mxr_layer *layer)
{
	if (layer->ops.release)
		layer->ops.release(layer);
}

void mxr_base_layer_release(struct mxr_layer *layer)
{
	kfree(layer);
}

static void mxr_vfd_release(struct video_device *vdev)
{
1079
	pr_info("video device release\n");
1080 1081 1082 1083 1084 1085 1086
}

struct mxr_layer *mxr_base_layer_create(struct mxr_device *mdev,
	int idx, char *name, struct mxr_layer_ops *ops)
{
	struct mxr_layer *layer;

1087
	layer = kzalloc(sizeof(*layer), GFP_KERNEL);
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	if (layer == NULL) {
		mxr_err(mdev, "not enough memory for layer.\n");
		goto fail;
	}

	layer->mdev = mdev;
	layer->idx = idx;
	layer->ops = *ops;

	spin_lock_init(&layer->enq_slock);
	INIT_LIST_HEAD(&layer->enq_list);
	mutex_init(&layer->mutex);

	layer->vfd = (struct video_device) {
		.minor = -1,
		.release = mxr_vfd_release,
		.fops = &mxr_fops,
1105
		.vfl_dir = VFL_DIR_TX,
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
		.ioctl_ops = &mxr_ioctl_ops,
	};
	strlcpy(layer->vfd.name, name, sizeof(layer->vfd.name));
	/* let framework control PRIORITY */
	set_bit(V4L2_FL_USE_FH_PRIO, &layer->vfd.flags);

	video_set_drvdata(&layer->vfd, layer);
	layer->vfd.lock = &layer->mutex;
	layer->vfd.v4l2_dev = &mdev->v4l2_dev;

	layer->vb_queue = (struct vb2_queue) {
		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
1118
		.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF,
1119 1120 1121
		.drv_priv = layer,
		.buf_struct_size = sizeof(struct mxr_buffer),
		.ops = &mxr_video_qops,
1122
		.min_buffers_needed = 1,
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
		.mem_ops = &vb2_dma_contig_memops,
	};

	return layer;

fail:
	return NULL;
}

static const struct mxr_format *find_format_by_fourcc(
	struct mxr_layer *layer, unsigned long fourcc)
{
	int i;

	for (i = 0; i < layer->fmt_array_size; ++i)
		if (layer->fmt_array[i]->fourcc == fourcc)
			return layer->fmt_array[i];
	return NULL;
}

static const struct mxr_format *find_format_by_index(
	struct mxr_layer *layer, unsigned long index)
{
	if (index >= layer->fmt_array_size)
		return NULL;
	return layer->fmt_array[index];
}