saa7164-encoder.c 38.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  Driver for the NXP SAA7164 PCIe bridge
 *
 *  Copyright (c) 2010 Steven Toth <stoth@kernellabs.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.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "saa7164.h"

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#define ENCODER_MAX_BITRATE 6500000
#define ENCODER_MIN_BITRATE 1000000
#define ENCODER_DEF_BITRATE 5000000

static struct saa7164_tvnorm saa7164_tvnorms[] = {
	{
		.name      = "NTSC-M",
		.id        = V4L2_STD_NTSC_M,
	}, {
		.name      = "NTSC-JP",
		.id        = V4L2_STD_NTSC_M_JP,
	}
};

static const u32 saa7164_v4l2_ctrls[] = {
	V4L2_CID_BRIGHTNESS,
	V4L2_CID_CONTRAST,
	V4L2_CID_SATURATION,
	V4L2_CID_HUE,
	V4L2_CID_AUDIO_VOLUME,
	V4L2_CID_SHARPNESS,
45
	V4L2_CID_MPEG_STREAM_TYPE,
46
	V4L2_CID_MPEG_VIDEO_ASPECT,
47
	V4L2_CID_MPEG_VIDEO_B_FRAMES,
48
	V4L2_CID_MPEG_VIDEO_GOP_SIZE,
49
	V4L2_CID_MPEG_AUDIO_MUTE,
50
	V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
51
	V4L2_CID_MPEG_VIDEO_BITRATE,
52
	V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	0
};

/* Take the encoder configuration form the port struct and
 * flush it to the hardware.
 */
static void saa7164_encoder_configure(struct saa7164_port *port)
{
	struct saa7164_dev *dev = port->dev;
	dprintk(DBGLVL_ENC, "%s()\n", __func__);

	port->encoder_params.width = port->width;
	port->encoder_params.height = port->height;
	port->encoder_params.is_50hz =
		(port->encodernorm.id & V4L2_STD_625_50) != 0;

	/* Set up the DIF (enable it) for analog mode by default */
	saa7164_api_initialize_dif(port);

	/* Configure the correct video standard */
	saa7164_api_configure_dif(port, port->encodernorm.id);

	/* Ensure the audio decoder is correct configured */
	saa7164_api_set_audio_std(port);
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static int saa7164_encoder_buffers_dealloc(struct saa7164_port *port)
{
	struct list_head *c, *n, *p, *q, *l, *v;
	struct saa7164_dev *dev = port->dev;
	struct saa7164_buffer *buf;
	struct saa7164_user_buffer *ubuf;

	/* Remove any allocated buffers */
	mutex_lock(&port->dmaqueue_lock);

	dprintk(DBGLVL_ENC, "%s(port=%d) dmaqueue\n", __func__, port->nr);
	list_for_each_safe(c, n, &port->dmaqueue.list) {
		buf = list_entry(c, struct saa7164_buffer, list);
		list_del(c);
		saa7164_buffer_dealloc(buf);
	}

	dprintk(DBGLVL_ENC, "%s(port=%d) used\n", __func__, port->nr);
	list_for_each_safe(p, q, &port->list_buf_used.list) {
		ubuf = list_entry(p, struct saa7164_user_buffer, list);
		list_del(p);
		saa7164_buffer_dealloc_user(ubuf);
	}

	dprintk(DBGLVL_ENC, "%s(port=%d) free\n", __func__, port->nr);
	list_for_each_safe(l, v, &port->list_buf_free.list) {
		ubuf = list_entry(l, struct saa7164_user_buffer, list);
		list_del(l);
		saa7164_buffer_dealloc_user(ubuf);
	}

	mutex_unlock(&port->dmaqueue_lock);
	dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);

	return 0;
}

/* Dynamic buffer switch at encoder start time */
static int saa7164_encoder_buffers_alloc(struct saa7164_port *port)
118 119
{
	struct saa7164_dev *dev = port->dev;
120 121
	struct saa7164_buffer *buf;
	struct saa7164_user_buffer *ubuf;
122
	struct tmHWStreamParameters *params = &port->hw_streamingparams;
123 124
	int result = -ENODEV, i;
	int len = 0;
125 126 127

	dprintk(DBGLVL_ENC, "%s()\n", __func__);

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) {
		dprintk(DBGLVL_ENC, "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_PS\n", __func__);
		params->samplesperline = 128;
		params->numberoflines = 256;
		params->pitch = 128;
		params->numpagetables = 2 +
			((SAA7164_PS_NUMBER_OF_LINES * 128) / PAGE_SIZE);
	} else
	if (port->encoder_params.stream_type == V4L2_MPEG_STREAM_TYPE_MPEG2_TS) {
		dprintk(DBGLVL_ENC, "%s() type=V4L2_MPEG_STREAM_TYPE_MPEG2_TS\n", __func__);
		params->samplesperline = 188;
		params->numberoflines = 312;
		params->pitch = 188;
		params->numpagetables = 2 +
			((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
	} else
		BUG();

	/* Init and establish defaults */
	params->bitspersample = 8;
	params->linethreshold = 0;
	params->pagetablelistvirt = 0;
	params->pagetablelistphys = 0;
	params->numpagetableentries = port->hwcfg.buffercount;

	/* Allocate the PCI resources, buffers (hard) */
	for (i = 0; i < port->hwcfg.buffercount; i++) {
		buf = saa7164_buffer_alloc(port,
			params->numberoflines *
			params->pitch);

		if (!buf) {
			printk(KERN_ERR "%s() failed "
			       "(errno = %d), unable to allocate buffer\n",
				__func__, result);
			result = -ENOMEM;
			goto failed;
		} else {

			mutex_lock(&port->dmaqueue_lock);
			list_add_tail(&buf->list, &port->dmaqueue.list);
			mutex_unlock(&port->dmaqueue_lock);

		}
	}

	/* Allocate some kenrel kernel buffers for copying
	 * to userpsace.
	 */
	len = params->numberoflines * params->pitch;

	if (encoder_buffers < 16)
		encoder_buffers = 16;
	if (encoder_buffers > 512)
		encoder_buffers = 512;

	for (i = 0; i < encoder_buffers; i++) {

		ubuf = saa7164_buffer_alloc_user(dev, len);
		if (ubuf) {
			mutex_lock(&port->dmaqueue_lock);
			list_add_tail(&ubuf->list, &port->list_buf_free.list);
			mutex_unlock(&port->dmaqueue_lock);
		}

	}

	result = 0;
196

197 198 199 200 201 202 203
failed:
	return result;
}

static int saa7164_encoder_initialize(struct saa7164_port *port)
{
	saa7164_encoder_configure(port);
204 205 206 207 208 209
	return 0;
}

/* -- V4L2 --------------------------------------------------------- */
static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
{
210
	struct saa7164_encoder_fh *fh = file->private_data;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;
	unsigned int i;

	dprintk(DBGLVL_ENC, "%s(id=0x%x)\n", __func__, (u32)*id);

	for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
		if (*id & saa7164_tvnorms[i].id)
			break;
	}
	if (i == ARRAY_SIZE(saa7164_tvnorms))
		return -EINVAL;

	port->encodernorm = saa7164_tvnorms[i];

	/* Update the audio decoder while is not running in
	 * auto detect mode.
	 */
	saa7164_api_set_audio_std(port);

	dprintk(DBGLVL_ENC, "%s(id=0x%x) OK\n", __func__, (u32)*id);

	return 0;
}

static int vidioc_enum_input(struct file *file, void *priv,
	struct v4l2_input *i)
{
	int n;

	char *inputs[] = { "tuner", "composite", "svideo", "aux",
242
		"composite 2", "svideo 2", "aux 2" };
243 244 245 246

	if (i->index >= 7)
		return -EINVAL;

247
	strcpy(i->name, inputs[i->index]);
248 249 250 251 252 253 254 255 256 257 258 259 260 261

	if (i->index == 0)
		i->type = V4L2_INPUT_TYPE_TUNER;
	else
		i->type  = V4L2_INPUT_TYPE_CAMERA;

	for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
		i->std |= saa7164_tvnorms[n].id;

	return 0;
}

static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
{
262
	struct saa7164_encoder_fh *fh = file->private_data;
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;

	if (saa7164_api_get_videomux(port) != SAA_OK)
		return -EIO;

	*i = (port->mux_input - 1);

	dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, *i);

	return 0;
}

static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
{
278
	struct saa7164_encoder_fh *fh = file->private_data;
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;

	dprintk(DBGLVL_ENC, "%s() input=%d\n", __func__, i);

	if (i >= 7)
		return -EINVAL;

	port->mux_input = i + 1;

	if (saa7164_api_set_videomux(port) != SAA_OK)
		return -EIO;

	return 0;
}

static int vidioc_g_tuner(struct file *file, void *priv,
	struct v4l2_tuner *t)
{
298
	struct saa7164_encoder_fh *fh = file->private_data;
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;

	if (0 != t->index)
		return -EINVAL;

	strcpy(t->name, "tuner");
	t->type = V4L2_TUNER_ANALOG_TV;
	t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;

	dprintk(DBGLVL_ENC, "VIDIOC_G_TUNER: tuner type %d\n", t->type);

	return 0;
}

static int vidioc_s_tuner(struct file *file, void *priv,
	struct v4l2_tuner *t)
{
	/* Update the A/V core */
	return 0;
}

static int vidioc_g_frequency(struct file *file, void *priv,
	struct v4l2_frequency *f)
{
324
	struct saa7164_encoder_fh *fh = file->private_data;
325 326 327 328 329 330 331 332 333 334 335
	struct saa7164_port *port = fh->port;

	f->type = V4L2_TUNER_ANALOG_TV;
	f->frequency = port->freq;

	return 0;
}

static int vidioc_s_frequency(struct file *file, void *priv,
	struct v4l2_frequency *f)
{
336
	struct saa7164_encoder_fh *fh = file->private_data;
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;
	struct saa7164_port *tsport;
	struct dvb_frontend *fe;

	/* TODO: Pull this for the std */
	struct analog_parameters params = {
		.mode      = V4L2_TUNER_ANALOG_TV,
		.audmode   = V4L2_TUNER_MODE_STEREO,
		.std       = port->encodernorm.id,
		.frequency = f->frequency
	};

	/* Stop the encoder */
	dprintk(DBGLVL_ENC, "%s() frequency=%d tuner=%d\n", __func__,
		f->frequency, f->tuner);

	if (f->tuner != 0)
		return -EINVAL;

	if (f->type != V4L2_TUNER_ANALOG_TV)
		return -EINVAL;

	port->freq = f->frequency;

	/* Update the hardware */
	if (port->nr == SAA7164_PORT_ENC1)
364
		tsport = &dev->ports[SAA7164_PORT_TS1];
365 366
	else
	if (port->nr == SAA7164_PORT_ENC2)
367
		tsport = &dev->ports[SAA7164_PORT_TS2];
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	else
		BUG();

	fe = tsport->dvb.frontend;

	if (fe && fe->ops.tuner_ops.set_analog_params)
		fe->ops.tuner_ops.set_analog_params(fe, &params);
	else
		printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);

	saa7164_encoder_initialize(port);

	return 0;
}

static int vidioc_g_ctrl(struct file *file, void *priv,
	struct v4l2_control *ctl)
{
386
	struct saa7164_encoder_fh *fh = file->private_data;
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;

	dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
		ctl->id, ctl->value);

	switch (ctl->id) {
	case V4L2_CID_BRIGHTNESS:
		ctl->value = port->ctl_brightness;
		break;
	case V4L2_CID_CONTRAST:
		ctl->value = port->ctl_contrast;
		break;
	case V4L2_CID_SATURATION:
		ctl->value = port->ctl_saturation;
		break;
	case V4L2_CID_HUE:
		ctl->value = port->ctl_hue;
		break;
	case V4L2_CID_SHARPNESS:
		ctl->value = port->ctl_sharpness;
		break;
	case V4L2_CID_AUDIO_VOLUME:
		ctl->value = port->ctl_volume;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int vidioc_s_ctrl(struct file *file, void *priv,
	struct v4l2_control *ctl)
{
422
	struct saa7164_encoder_fh *fh = file->private_data;
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 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 497 498 499
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;
	int ret = 0;

	dprintk(DBGLVL_ENC, "%s(id=%d, value=%d)\n", __func__,
		ctl->id, ctl->value);

	switch (ctl->id) {
	case V4L2_CID_BRIGHTNESS:
		if ((ctl->value >= 0) && (ctl->value <= 255)) {
			port->ctl_brightness = ctl->value;
			saa7164_api_set_usercontrol(port,
				PU_BRIGHTNESS_CONTROL);
		} else
			ret = -EINVAL;
		break;
	case V4L2_CID_CONTRAST:
		if ((ctl->value >= 0) && (ctl->value <= 255)) {
			port->ctl_contrast = ctl->value;
			saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
		} else
			ret = -EINVAL;
		break;
	case V4L2_CID_SATURATION:
		if ((ctl->value >= 0) && (ctl->value <= 255)) {
			port->ctl_saturation = ctl->value;
			saa7164_api_set_usercontrol(port,
				PU_SATURATION_CONTROL);
		} else
			ret = -EINVAL;
		break;
	case V4L2_CID_HUE:
		if ((ctl->value >= 0) && (ctl->value <= 255)) {
			port->ctl_hue = ctl->value;
			saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
		} else
			ret = -EINVAL;
		break;
	case V4L2_CID_SHARPNESS:
		if ((ctl->value >= 0) && (ctl->value <= 255)) {
			port->ctl_sharpness = ctl->value;
			saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
		} else
			ret = -EINVAL;
		break;
	case V4L2_CID_AUDIO_VOLUME:
		if ((ctl->value >= -83) && (ctl->value <= 24)) {
			port->ctl_volume = ctl->value;
			saa7164_api_set_audio_volume(port, port->ctl_volume);
		} else
			ret = -EINVAL;
		break;
	default:
		ret = -EINVAL;
	}

	return ret;
}

static int saa7164_get_ctrl(struct saa7164_port *port,
	struct v4l2_ext_control *ctrl)
{
	struct saa7164_encoder_params *params = &port->encoder_params;

	switch (ctrl->id) {
	case V4L2_CID_MPEG_VIDEO_BITRATE:
		ctrl->value = params->bitrate;
		break;
	case V4L2_CID_MPEG_STREAM_TYPE:
		ctrl->value = params->stream_type;
		break;
	case V4L2_CID_MPEG_AUDIO_MUTE:
		ctrl->value = params->ctl_mute;
		break;
	case V4L2_CID_MPEG_VIDEO_ASPECT:
		ctrl->value = params->ctl_aspect;
		break;
500 501 502
	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
		ctrl->value = params->bitrate_mode;
		break;
503 504 505
	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
		ctrl->value = params->refdist;
		break;
506 507 508
	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
		ctrl->value = params->bitrate_peak;
		break;
509 510 511
	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
		ctrl->value = params->gop_size;
		break;
512 513 514 515 516 517 518 519 520
	default:
		return -EINVAL;
	}
	return 0;
}

static int vidioc_g_ext_ctrls(struct file *file, void *priv,
	struct v4l2_ext_controls *ctrls)
{
521
	struct saa7164_encoder_fh *fh = file->private_data;
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
	struct saa7164_port *port = fh->port;
	int i, err = 0;

	if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
		for (i = 0; i < ctrls->count; i++) {
			struct v4l2_ext_control *ctrl = ctrls->controls + i;

			err = saa7164_get_ctrl(port, ctrl);
			if (err) {
				ctrls->error_idx = i;
				break;
			}
		}
		return err;

	}

	return -EINVAL;
}

static int saa7164_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
{
	int ret = -EINVAL;

	switch (ctrl->id) {
	case V4L2_CID_MPEG_VIDEO_BITRATE:
		if ((ctrl->value >= ENCODER_MIN_BITRATE) &&
			(ctrl->value <= ENCODER_MAX_BITRATE))
			ret = 0;
		break;
	case V4L2_CID_MPEG_STREAM_TYPE:
553 554
		if ((ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) ||
			(ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS))
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
			ret = 0;
		break;
	case V4L2_CID_MPEG_AUDIO_MUTE:
		if ((ctrl->value >= 0) &&
			(ctrl->value <= 1))
			ret = 0;
		break;
	case V4L2_CID_MPEG_VIDEO_ASPECT:
		if ((ctrl->value >= V4L2_MPEG_VIDEO_ASPECT_1x1) &&
			(ctrl->value <= V4L2_MPEG_VIDEO_ASPECT_221x100))
			ret = 0;
		break;
	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
		if ((ctrl->value >= 0) &&
			(ctrl->value <= 255))
			ret = 0;
		break;
572 573 574 575 576
	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
		if ((ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) ||
			(ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR))
			ret = 0;
		break;
577 578 579 580 581
	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
		if ((ctrl->value >= 1) &&
			(ctrl->value <= 3))
			ret = 0;
		break;
582 583 584 585 586
	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
		if ((ctrl->value >= ENCODER_MIN_BITRATE) &&
			(ctrl->value <= ENCODER_MAX_BITRATE))
			ret = 0;
		break;
587 588 589 590 591 592 593 594 595 596 597 598 599 600 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 635 636 637 638 639 640 641 642 643 644 645
	default:
		ret = -EINVAL;
	}

	return ret;
}

static int vidioc_try_ext_ctrls(struct file *file, void *priv,
	struct v4l2_ext_controls *ctrls)
{
	int i, err = 0;

	if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
		for (i = 0; i < ctrls->count; i++) {
			struct v4l2_ext_control *ctrl = ctrls->controls + i;

			err = saa7164_try_ctrl(ctrl, 0);
			if (err) {
				ctrls->error_idx = i;
				break;
			}
		}
		return err;
	}

	return -EINVAL;
}

static int saa7164_set_ctrl(struct saa7164_port *port,
	struct v4l2_ext_control *ctrl)
{
	struct saa7164_encoder_params *params = &port->encoder_params;
	int ret = 0;

	switch (ctrl->id) {
	case V4L2_CID_MPEG_VIDEO_BITRATE:
		params->bitrate = ctrl->value;
		break;
	case V4L2_CID_MPEG_STREAM_TYPE:
		params->stream_type = ctrl->value;
		break;
	case V4L2_CID_MPEG_AUDIO_MUTE:
		params->ctl_mute = ctrl->value;
		ret = saa7164_api_audio_mute(port, params->ctl_mute);
		if (ret != SAA_OK) {
			printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
				ret);
			ret = -EIO;
		}
		break;
	case V4L2_CID_MPEG_VIDEO_ASPECT:
		params->ctl_aspect = ctrl->value;
		ret = saa7164_api_set_aspect_ratio(port);
		if (ret != SAA_OK) {
			printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
				ret);
			ret = -EIO;
		}
		break;
646 647 648
	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
		params->bitrate_mode = ctrl->value;
		break;
649 650 651
	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
		params->refdist = ctrl->value;
		break;
652 653 654
	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
		params->bitrate_peak = ctrl->value;
		break;
655 656 657
	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
		params->gop_size = ctrl->value;
		break;
658 659 660 661 662 663 664 665 666 667 668 669
	default:
		return -EINVAL;
	}

	/* TODO: Update the hardware */

	return ret;
}

static int vidioc_s_ext_ctrls(struct file *file, void *priv,
	struct v4l2_ext_controls *ctrls)
{
670
	struct saa7164_encoder_fh *fh = file->private_data;
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
	struct saa7164_port *port = fh->port;
	int i, err = 0;

	if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
		for (i = 0; i < ctrls->count; i++) {
			struct v4l2_ext_control *ctrl = ctrls->controls + i;

			err = saa7164_try_ctrl(ctrl, 0);
			if (err) {
				ctrls->error_idx = i;
				break;
			}
			err = saa7164_set_ctrl(port, ctrl);
			if (err) {
				ctrls->error_idx = i;
				break;
			}
		}
		return err;

	}

	return -EINVAL;
}

static int vidioc_querycap(struct file *file, void  *priv,
	struct v4l2_capability *cap)
{
699
	struct saa7164_encoder_fh *fh = file->private_data;
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;

	strcpy(cap->driver, dev->name);
	strlcpy(cap->card, saa7164_boards[dev->board].name,
		sizeof(cap->card));
	sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));

	cap->capabilities =
		V4L2_CAP_VIDEO_CAPTURE |
		V4L2_CAP_READWRITE     |
		0;

	cap->capabilities |= V4L2_CAP_TUNER;
	cap->version = 0;

	return 0;
}

static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
	struct v4l2_fmtdesc *f)
{
	if (f->index != 0)
		return -EINVAL;

	strlcpy(f->description, "MPEG", sizeof(f->description));
	f->pixelformat = V4L2_PIX_FMT_MPEG;

	return 0;
}

static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
				struct v4l2_format *f)
{
734
	struct saa7164_encoder_fh *fh = file->private_data;
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;

	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
	f->fmt.pix.bytesperline = 0;
	f->fmt.pix.sizeimage    =
		port->ts_packet_size * port->ts_packet_count;
	f->fmt.pix.colorspace   = 0;
	f->fmt.pix.width        = port->width;
	f->fmt.pix.height       = port->height;

	dprintk(DBGLVL_ENC, "VIDIOC_G_FMT: w: %d, h: %d\n",
		port->width, port->height);

	return 0;
}

static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
				struct v4l2_format *f)
{
755
	struct saa7164_encoder_fh *fh = file->private_data;
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;

	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
	f->fmt.pix.bytesperline = 0;
	f->fmt.pix.sizeimage    =
		port->ts_packet_size * port->ts_packet_count;
	f->fmt.pix.colorspace   = 0;
	dprintk(DBGLVL_ENC, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
		port->width, port->height);
	return 0;
}

static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
				struct v4l2_format *f)
{
772
	struct saa7164_encoder_fh *fh = file->private_data;
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;

	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
	f->fmt.pix.bytesperline = 0;
	f->fmt.pix.sizeimage    =
		port->ts_packet_size * port->ts_packet_count;
	f->fmt.pix.colorspace   = 0;

	dprintk(DBGLVL_ENC, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
		f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);

	return 0;
}

static int vidioc_log_status(struct file *file, void *priv)
{
	return 0;
}

static int fill_queryctrl(struct saa7164_encoder_params *params,
	struct v4l2_queryctrl *c)
{
	switch (c->id) {
	case V4L2_CID_BRIGHTNESS:
		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
	case V4L2_CID_CONTRAST:
		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
	case V4L2_CID_SATURATION:
		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
	case V4L2_CID_HUE:
		return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
	case V4L2_CID_SHARPNESS:
		return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
	case V4L2_CID_MPEG_AUDIO_MUTE:
		return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
	case V4L2_CID_AUDIO_VOLUME:
		return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
	case V4L2_CID_MPEG_VIDEO_BITRATE:
		return v4l2_ctrl_query_fill(c,
			ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
			100000, ENCODER_DEF_BITRATE);
	case V4L2_CID_MPEG_STREAM_TYPE:
		return v4l2_ctrl_query_fill(c,
			V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
818 819
			V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
			1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
820 821 822 823 824 825 826
	case V4L2_CID_MPEG_VIDEO_ASPECT:
		return v4l2_ctrl_query_fill(c,
			V4L2_MPEG_VIDEO_ASPECT_1x1,
			V4L2_MPEG_VIDEO_ASPECT_221x100,
			1, V4L2_MPEG_VIDEO_ASPECT_4x3);
	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
		return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
827 828 829 830
	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
		return v4l2_ctrl_query_fill(c,
			V4L2_MPEG_VIDEO_BITRATE_MODE_VBR, V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
			1, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR);
831 832 833
	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
		return v4l2_ctrl_query_fill(c,
			1, 3, 1, 1);
834 835 836 837
	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
		return v4l2_ctrl_query_fill(c,
			ENCODER_MIN_BITRATE, ENCODER_MAX_BITRATE,
			100000, ENCODER_DEF_BITRATE);
838 839 840 841 842 843 844 845
	default:
		return -EINVAL;
	}
}

static int vidioc_queryctrl(struct file *file, void *priv,
	struct v4l2_queryctrl *c)
{
846
	struct saa7164_encoder_fh *fh = priv;
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 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 941 942 943 944 945 946 947 948 949 950
	struct saa7164_port *port = fh->port;
	int i, next;
	u32 id = c->id;

	memset(c, 0, sizeof(*c));

	next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
	c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;

	for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
		if (next) {
			if (c->id < saa7164_v4l2_ctrls[i])
				c->id = saa7164_v4l2_ctrls[i];
			else
				continue;
		}

		if (c->id == saa7164_v4l2_ctrls[i])
			return fill_queryctrl(&port->encoder_params, c);

		if (c->id < saa7164_v4l2_ctrls[i])
			break;
	}

	return -EINVAL;
}

static int saa7164_encoder_stop_port(struct saa7164_port *port)
{
	struct saa7164_dev *dev = port->dev;
	int ret;

	ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
		printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
			__func__, ret);
		ret = -EIO;
	} else {
		dprintk(DBGLVL_ENC, "%s()    Stopped\n", __func__);
		ret = 0;
	}

	return ret;
}

static int saa7164_encoder_acquire_port(struct saa7164_port *port)
{
	struct saa7164_dev *dev = port->dev;
	int ret;

	ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
		printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
			__func__, ret);
		ret = -EIO;
	} else {
		dprintk(DBGLVL_ENC, "%s() Acquired\n", __func__);
		ret = 0;
	}

	return ret;
}

static int saa7164_encoder_pause_port(struct saa7164_port *port)
{
	struct saa7164_dev *dev = port->dev;
	int ret;

	ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
		printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
			__func__, ret);
		ret = -EIO;
	} else {
		dprintk(DBGLVL_ENC, "%s()   Paused\n", __func__);
		ret = 0;
	}

	return ret;
}

/* Firmware is very windows centric, meaning you have to transition
 * the part through AVStream / KS Windows stages, forwards or backwards.
 * States are: stopped, acquired (h/w), paused, started.
 * We have to leave here will all of the soft buffers on the free list,
 * else the cfg_post() func won't have soft buffers to correctly configure.
 */
static int saa7164_encoder_stop_streaming(struct saa7164_port *port)
{
	struct saa7164_dev *dev = port->dev;
	struct saa7164_buffer *buf;
	struct saa7164_user_buffer *ubuf;
	struct list_head *c, *n;
	int ret;

	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);

	ret = saa7164_encoder_pause_port(port);
	ret = saa7164_encoder_acquire_port(port);
	ret = saa7164_encoder_stop_port(port);

	dprintk(DBGLVL_ENC, "%s(port=%d) Hardware stopped\n", __func__,
		port->nr);

951
	/* Reset the state of any allocated buffer resources */
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
	mutex_lock(&port->dmaqueue_lock);

	/* Reset the hard and soft buffer state */
	list_for_each_safe(c, n, &port->dmaqueue.list) {
		buf = list_entry(c, struct saa7164_buffer, list);
		buf->flags = SAA7164_BUFFER_FREE;
		buf->pos = 0;
	}

	list_for_each_safe(c, n, &port->list_buf_used.list) {
		ubuf = list_entry(c, struct saa7164_user_buffer, list);
		ubuf->pos = 0;
		list_move_tail(&ubuf->list, &port->list_buf_free.list);
	}

	mutex_unlock(&port->dmaqueue_lock);
968 969 970 971

	/* Free any allocated resources */
	saa7164_encoder_buffers_dealloc(port);

972 973 974 975 976 977 978 979 980 981 982 983
	dprintk(DBGLVL_ENC, "%s(port=%d) Released\n", __func__, port->nr);

	return ret;
}

static int saa7164_encoder_start_streaming(struct saa7164_port *port)
{
	struct saa7164_dev *dev = port->dev;
	int result, ret = 0;

	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);

984 985 986 987 988 989 990 991
	port->done_first_interrupt = 0;

	/* allocate all of the PCIe DMA buffer resources on the fly,
	 * allowing switching between TS and PS payloads without
	 * requiring a complete driver reload.
	 */
	saa7164_encoder_buffers_alloc(port);

992 993
	/* Configure the encoder with any cache values */
	saa7164_api_set_encoder(port);
994
	saa7164_api_get_encoder(port);
995

996
	/* Place the empty buffers on the hardware */
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
	saa7164_buffer_cfg_port(port);

	/* Acquire the hardware */
	result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
		printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
			__func__, result);

		/* Stop the hardware, regardless */
		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
			printk(KERN_ERR "%s() acquire/forced stop transition "
				"failed, res = 0x%x\n", __func__, result);
		}
		ret = -EIO;
		goto out;
	} else
		dprintk(DBGLVL_ENC, "%s()   Acquired\n", __func__);

	/* Pause the hardware */
	result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
		printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
				__func__, result);

		/* Stop the hardware, regardless */
		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
			printk(KERN_ERR "%s() pause/forced stop transition "
				"failed, res = 0x%x\n", __func__, result);
		}

		ret = -EIO;
		goto out;
	} else
		dprintk(DBGLVL_ENC, "%s()   Paused\n", __func__);

	/* Start the hardware */
	result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
		printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
				__func__, result);

		/* Stop the hardware, regardless */
		result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
		if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
			printk(KERN_ERR "%s() run/forced stop transition "
				"failed, res = 0x%x\n", __func__, result);
		}

		ret = -EIO;
	} else
		dprintk(DBGLVL_ENC, "%s()   Running\n", __func__);

out:
	return ret;
}

static int fops_open(struct file *file)
{
	struct saa7164_dev *h, *dev = NULL;
	struct saa7164_port *port = NULL;
	struct saa7164_port *portc = NULL;
	struct saa7164_port *portd = NULL;
1061
	struct saa7164_encoder_fh *fh;
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
	struct list_head *list;
	int minor = video_devdata(file)->minor;

	dprintk(DBGLVL_ENC, "%s()\n", __func__);

	/* TODO: Really, the BKL? - remove this */
	lock_kernel();
	list_for_each(list, &saa7164_devlist) {
		h = list_entry(list, struct saa7164_dev, devlist);

1072 1073
		portc = &h->ports[SAA7164_PORT_ENC1];
		portd = &h->ports[SAA7164_PORT_ENC2];
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

		if (portc->v4l_device &&
		    portc->v4l_device->minor == minor) {
			dev = h;
			port = portc;
			break;
		}

		if (portd->v4l_device &&
		    portd->v4l_device->minor == minor) {
			dev = h;
			port = portd;
			break;
		}

	}

	if (port == NULL) {
		unlock_kernel();
		return -ENODEV;
	}

	/* allocate + initialize per filehandle data */
	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
	if (NULL == fh) {
		unlock_kernel();
		return -ENOMEM;
	}

	file->private_data = fh;
	fh->port = port;

	unlock_kernel();

	return 0;
}

static int fops_release(struct file *file)
{
1113
	struct saa7164_encoder_fh *fh = file->private_data;
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
	struct saa7164_port *port = fh->port;
	struct saa7164_dev *dev = port->dev;

	dprintk(DBGLVL_ENC, "%s()\n", __func__);

	/* Shut device down on last close */
	if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
		if (atomic_dec_return(&port->v4l_reader_count) == 0) {
			/* stop mpeg capture then cancel buffers */
			saa7164_encoder_stop_streaming(port);
		}
	}

	file->private_data = NULL;
	kfree(fh);

	return 0;
}

struct saa7164_user_buffer *saa7164_enc_next_buf(struct saa7164_port *port)
{
1135
	struct saa7164_user_buffer *ubuf = 0;
1136
	struct saa7164_dev *dev = port->dev;
1137
	u32 crc;
1138 1139 1140

	mutex_lock(&port->dmaqueue_lock);
	if (!list_empty(&port->list_buf_used.list)) {
1141
		ubuf = list_first_entry(&port->list_buf_used.list,
1142
			struct saa7164_user_buffer, list);
1143

1144 1145 1146 1147 1148 1149
		if (crc_checking) {
			crc = crc32(0, ubuf->data, ubuf->actual_size);
			if (crc != ubuf->crc) {
				printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n", __func__,
					ubuf, ubuf->crc, crc);
			}
1150 1151
		}

1152 1153 1154
	}
	mutex_unlock(&port->dmaqueue_lock);

1155
	dprintk(DBGLVL_ENC, "%s() returns %p\n", __func__, ubuf);
1156

1157
	return ubuf;
1158 1159 1160 1161 1162
}

static ssize_t fops_read(struct file *file, char __user *buffer,
	size_t count, loff_t *pos)
{
1163
	struct saa7164_encoder_fh *fh = file->private_data;
1164 1165 1166
	struct saa7164_port *port = fh->port;
	struct saa7164_user_buffer *ubuf = NULL;
	struct saa7164_dev *dev = port->dev;
1167
	int ret = 0;
1168 1169 1170
	int rem, cnt;
	u8 *p;

1171 1172 1173 1174 1175 1176 1177 1178
	port->last_read_msecs_diff = port->last_read_msecs;
	port->last_read_msecs = jiffies_to_msecs(jiffies);
	port->last_read_msecs_diff = port->last_read_msecs -
		port->last_read_msecs_diff;

	saa7164_histogram_update(&port->read_interval,
		port->last_read_msecs_diff);

1179 1180
	if (*pos) {
		printk(KERN_ERR "%s() ESPIPE\n", __func__);
1181
		return -ESPIPE;
1182
	}
1183 1184 1185 1186

	if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
		if (atomic_inc_return(&port->v4l_reader_count) == 1) {

1187 1188
			if (saa7164_encoder_initialize(port) < 0) {
				printk(KERN_ERR "%s() EINVAL\n", __func__);
1189
				return -EINVAL;
1190
			}
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200

			saa7164_encoder_start_streaming(port);
			msleep(200);
		}
	}

	/* blocking wait for buffer */
	if ((file->f_flags & O_NONBLOCK) == 0) {
		if (wait_event_interruptible(port->wait_read,
			saa7164_enc_next_buf(port))) {
1201
				printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
				return -ERESTARTSYS;
		}
	}

	/* Pull the first buffer from the used list */
	ubuf = saa7164_enc_next_buf(port);

	while ((count > 0) && ubuf) {

		/* set remaining bytes to copy */
		rem = ubuf->actual_size - ubuf->pos;
		cnt = rem > count ? count : rem;

		p = ubuf->data + ubuf->pos;

		dprintk(DBGLVL_ENC,
			"%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
			__func__, (int)count, cnt, rem, ubuf, ubuf->pos);

		if (copy_to_user(buffer, p, cnt)) {
			printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
1223 1224
			if (!ret) {
				printk(KERN_ERR "%s() EFAULT\n", __func__);
1225
				ret = -EFAULT;
1226
			}
1227 1228 1229 1230 1231 1232 1233 1234
			goto err;
		}

		ubuf->pos += cnt;
		count -= cnt;
		buffer += cnt;
		ret += cnt;

1235 1236 1237 1238
		if (ubuf->pos > ubuf->actual_size) {
			printk(KERN_ERR "read() pos > actual, huh?\n");
		}

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
		if (ubuf->pos == ubuf->actual_size) {

			/* finished with current buffer, take next buffer */

			/* Requeue the buffer on the free list */
			ubuf->pos = 0;

			mutex_lock(&port->dmaqueue_lock);
			list_move_tail(&ubuf->list, &port->list_buf_free.list);
			mutex_unlock(&port->dmaqueue_lock);

			/* Dequeue next */
			if ((file->f_flags & O_NONBLOCK) == 0) {
				if (wait_event_interruptible(port->wait_read,
					saa7164_enc_next_buf(port))) {
						break;
				}
			}
			ubuf = saa7164_enc_next_buf(port);
		}
	}
err:
1261
	if (!ret && !ubuf) {
1262
		ret = -EAGAIN;
1263
	}
1264 1265 1266 1267 1268 1269

	return ret;
}

static unsigned int fops_poll(struct file *file, poll_table *wait)
{
1270
	struct saa7164_encoder_fh *fh = (struct saa7164_encoder_fh *)file->private_data;
1271 1272 1273 1274
	struct saa7164_port *port = fh->port;
	struct saa7164_user_buffer *ubuf;
	unsigned int mask = 0;

1275 1276 1277 1278 1279 1280 1281 1282
	port->last_poll_msecs_diff = port->last_poll_msecs;
	port->last_poll_msecs = jiffies_to_msecs(jiffies);
	port->last_poll_msecs_diff = port->last_poll_msecs -
		port->last_poll_msecs_diff;

	saa7164_histogram_update(&port->poll_interval,
		port->last_poll_msecs_diff);

1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
	if (!video_is_registered(port->v4l_device)) {
		return -EIO;
	}

	if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
		if (atomic_inc_return(&port->v4l_reader_count) == 1) {
			if (saa7164_encoder_initialize(port) < 0)
				return -EINVAL;
			saa7164_encoder_start_streaming(port);
			msleep(200);
		}
	}

	/* blocking wait for buffer */
	if ((file->f_flags & O_NONBLOCK) == 0) {
		if (wait_event_interruptible(port->wait_read,
			saa7164_enc_next_buf(port))) {
				return -ERESTARTSYS;
		}
	}

	/* Pull the first buffer from the used list */
	ubuf = list_first_entry(&port->list_buf_used.list,
		struct saa7164_user_buffer, list);

	if (ubuf)
		mask |= POLLIN | POLLRDNORM;

	return mask;
}

static const struct v4l2_file_operations mpeg_fops = {
	.owner		= THIS_MODULE,
	.open		= fops_open,
	.release	= fops_release,
	.read		= fops_read,
	.poll		= fops_poll,
	.unlocked_ioctl	= video_ioctl2,
};

int saa7164_g_chip_ident(struct file *file, void *fh,
	struct v4l2_dbg_chip_ident *chip)
{
1326
	struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
1327 1328 1329 1330 1331 1332 1333 1334 1335
	struct saa7164_dev *dev = port->dev;
	dprintk(DBGLVL_ENC, "%s()\n", __func__);

	return 0;
}

int saa7164_g_register(struct file *file, void *fh,
	struct v4l2_dbg_register *reg)
{
1336
	struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
	struct saa7164_dev *dev = port->dev;
	dprintk(DBGLVL_ENC, "%s()\n", __func__);

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	return 0;
}

int saa7164_s_register(struct file *file, void *fh,
	struct v4l2_dbg_register *reg)
{
1349
	struct saa7164_port *port = ((struct saa7164_encoder_fh *)fh)->port;
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
	struct saa7164_dev *dev = port->dev;
	dprintk(DBGLVL_ENC, "%s()\n", __func__);

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	return 0;
}

static const struct v4l2_ioctl_ops mpeg_ioctl_ops = {
	.vidioc_s_std		 = vidioc_s_std,
	.vidioc_enum_input	 = vidioc_enum_input,
	.vidioc_g_input		 = vidioc_g_input,
	.vidioc_s_input		 = vidioc_s_input,
	.vidioc_g_tuner		 = vidioc_g_tuner,
	.vidioc_s_tuner		 = vidioc_s_tuner,
	.vidioc_g_frequency	 = vidioc_g_frequency,
	.vidioc_s_frequency	 = vidioc_s_frequency,
	.vidioc_s_ctrl		 = vidioc_s_ctrl,
	.vidioc_g_ctrl		 = vidioc_g_ctrl,
	.vidioc_querycap	 = vidioc_querycap,
	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap	 = vidioc_g_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap	 = vidioc_try_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap	 = vidioc_s_fmt_vid_cap,
	.vidioc_g_ext_ctrls	 = vidioc_g_ext_ctrls,
	.vidioc_s_ext_ctrls	 = vidioc_s_ext_ctrls,
	.vidioc_try_ext_ctrls	 = vidioc_try_ext_ctrls,
	.vidioc_log_status	 = vidioc_log_status,
	.vidioc_queryctrl	 = vidioc_queryctrl,
	.vidioc_g_chip_ident	 = saa7164_g_chip_ident,
#ifdef CONFIG_VIDEO_ADV_DEBUG
	.vidioc_g_register	 = saa7164_g_register,
	.vidioc_s_register	 = saa7164_s_register,
#endif
};

static struct video_device saa7164_mpeg_template = {
	.name          = "saa7164",
	.fops          = &mpeg_fops,
	.ioctl_ops     = &mpeg_ioctl_ops,
	.minor         = -1,
	.tvnorms       = SAA7164_NORMS,
	.current_norm  = V4L2_STD_NTSC_M,
};

static struct video_device *saa7164_encoder_alloc(
	struct saa7164_port *port,
	struct pci_dev *pci,
	struct video_device *template,
	char *type)
{
	struct video_device *vfd;
	struct saa7164_dev *dev = port->dev;

	dprintk(DBGLVL_ENC, "%s()\n", __func__);

	vfd = video_device_alloc();
	if (NULL == vfd)
		return NULL;

	*vfd = *template;
	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
		type, saa7164_boards[dev->board].name);

	vfd->parent  = &pci->dev;
	vfd->release = video_device_release;
	return vfd;
}

int saa7164_encoder_register(struct saa7164_port *port)
{
	struct saa7164_dev *dev = port->dev;
1423
	int result = -ENODEV;
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452

	dprintk(DBGLVL_ENC, "%s()\n", __func__);

	if (port->type != SAA7164_MPEG_ENCODER)
		BUG();

	/* Sanity check that the PCI configuration space is active */
	if (port->hwcfg.BARLocation == 0) {
		printk(KERN_ERR "%s() failed "
		       "(errno = %d), NO PCI configuration\n",
			__func__, result);
		result = -ENOMEM;
		goto failed;
	}

	/* Establish encoder defaults here */
	/* Set default TV standard */
	port->encodernorm = saa7164_tvnorms[0];
	port->width = 720;
	port->mux_input = 1; /* Composite */
	port->video_format = EU_VIDEO_FORMAT_MPEG_2;
	port->audio_format = 0;
	port->video_resolution = 0;
	port->ctl_brightness = 127;
	port->ctl_contrast = 66;
	port->ctl_hue = 128;
	port->ctl_saturation = 62;
	port->ctl_sharpness = 8;
	port->encoder_params.bitrate = ENCODER_DEF_BITRATE;
1453
	port->encoder_params.bitrate_peak = ENCODER_DEF_BITRATE;
1454
	port->encoder_params.bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
1455 1456 1457
	port->encoder_params.stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS;
	port->encoder_params.ctl_mute = 0;
	port->encoder_params.ctl_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3;
1458
	port->encoder_params.refdist = 1;
1459
	port->encoder_params.gop_size = SAA7164_ENCODER_DEFAULT_GOP_SIZE;
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533

	if (port->encodernorm.id & V4L2_STD_525_60)
		port->height = 480;
	else
		port->height = 576;

	/* Allocate and register the video device node */
	port->v4l_device = saa7164_encoder_alloc(port,
		dev->pci, &saa7164_mpeg_template, "mpeg");

	if (port->v4l_device == NULL) {
		printk(KERN_INFO "%s: can't allocate mpeg device\n",
			dev->name);
		result = -ENOMEM;
		goto failed;
	}

	result = video_register_device(port->v4l_device,
		VFL_TYPE_GRABBER, -1);
	if (result < 0) {
		printk(KERN_INFO "%s: can't register mpeg device\n",
			dev->name);
		/* TODO: We're going to leak here if we don't dealloc
		 The buffers above. The unreg function can't deal wit it.
		*/
		goto failed;
	}

	printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
		dev->name, port->v4l_device->num);

	/* Configure the hardware defaults */
	saa7164_api_set_videomux(port);
	saa7164_api_set_usercontrol(port, PU_BRIGHTNESS_CONTROL);
	saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
	saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
	saa7164_api_set_usercontrol(port, PU_SATURATION_CONTROL);
	saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
	saa7164_api_audio_mute(port, 0);
	saa7164_api_set_audio_volume(port, 20);
	saa7164_api_set_aspect_ratio(port);

	/* Disable audio standard detection, it's buggy */
	saa7164_api_set_audio_detection(port, 0);

	saa7164_api_set_encoder(port);
	saa7164_api_get_encoder(port);

	result = 0;
failed:
	return result;
}

void saa7164_encoder_unregister(struct saa7164_port *port)
{
	struct saa7164_dev *dev = port->dev;

	dprintk(DBGLVL_ENC, "%s(port=%d)\n", __func__, port->nr);

	if (port->type != SAA7164_MPEG_ENCODER)
		BUG();

	if (port->v4l_device) {
		if (port->v4l_device->minor != -1)
			video_unregister_device(port->v4l_device);
		else
			video_device_release(port->v4l_device);

		port->v4l_device = NULL;
	}

	dprintk(DBGLVL_ENC, "%s(port=%d) done\n", __func__, port->nr);
}