ivtv-ioctl.c 53.5 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 24 25 26 27
/*
    ioctl system call
    Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
    Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>

    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "ivtv-driver.h"
#include "ivtv-version.h"
#include "ivtv-mailbox.h"
#include "ivtv-i2c.h"
#include "ivtv-queue.h"
#include "ivtv-fileops.h"
#include "ivtv-vbi.h"
28
#include "ivtv-routing.h"
29 30 31 32 33 34 35 36 37
#include "ivtv-streams.h"
#include "ivtv-yuv.h"
#include "ivtv-ioctl.h"
#include "ivtv-gpio.h"
#include "ivtv-controls.h"
#include "ivtv-cards.h"
#include <media/saa7127.h>
#include <media/tveeprom.h>
#include <media/v4l2-chip-ident.h>
38
#include <media/v4l2-event.h>
39 40 41
#include <linux/dvb/audio.h>
#include <linux/i2c-id.h>

42
u16 ivtv_service2vbi(int type)
43 44 45 46 47 48 49 50 51 52 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 79 80 81 82 83 84 85 86 87 88 89 90 91
{
	switch (type) {
		case V4L2_SLICED_TELETEXT_B:
			return IVTV_SLICED_TYPE_TELETEXT_B;
		case V4L2_SLICED_CAPTION_525:
			return IVTV_SLICED_TYPE_CAPTION_525;
		case V4L2_SLICED_WSS_625:
			return IVTV_SLICED_TYPE_WSS_625;
		case V4L2_SLICED_VPS:
			return IVTV_SLICED_TYPE_VPS;
		default:
			return 0;
	}
}

static int valid_service_line(int field, int line, int is_pal)
{
	return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
	       (!is_pal && line >= 10 && line < 22);
}

static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
{
	u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
	int i;

	set = set & valid_set;
	if (set == 0 || !valid_service_line(field, line, is_pal)) {
		return 0;
	}
	if (!is_pal) {
		if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
			return V4L2_SLICED_CAPTION_525;
	}
	else {
		if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
			return V4L2_SLICED_VPS;
		if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
			return V4L2_SLICED_WSS_625;
		if (line == 23)
			return 0;
	}
	for (i = 0; i < 32; i++) {
		if ((1 << i) & set)
			return 1 << i;
	}
	return 0;
}

92
void ivtv_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
93 94 95 96 97 98 99 100 101 102 103 104
{
	u16 set = fmt->service_set;
	int f, l;

	fmt->service_set = 0;
	for (f = 0; f < 2; f++) {
		for (l = 0; l < 24; l++) {
			fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
		}
	}
}

105
static void check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
106 107 108 109 110 111 112 113 114 115
{
	int f, l;

	for (f = 0; f < 2; f++) {
		for (l = 0; l < 24; l++) {
			fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
		}
	}
}

116
u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
{
	int f, l;
	u16 set = 0;

	for (f = 0; f < 2; f++) {
		for (l = 0; l < 24; l++) {
			set |= fmt->service_lines[f][l];
		}
	}
	return set;
}

void ivtv_set_osd_alpha(struct ivtv *itv)
{
	ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
		itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
133
	ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
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
}

int ivtv_set_speed(struct ivtv *itv, int speed)
{
	u32 data[CX2341X_MBOX_MAX_DATA];
	struct ivtv_stream *s;
	int single_step = (speed == 1 || speed == -1);
	DEFINE_WAIT(wait);

	if (speed == 0) speed = 1000;

	/* No change? */
	if (speed == itv->speed && !single_step)
		return 0;

	s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];

	if (single_step && (speed < 0) == (itv->speed < 0)) {
		/* Single step video and no need to change direction */
		ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
		itv->speed = speed;
		return 0;
	}
	if (single_step)
		/* Need to change direction */
		speed = speed < 0 ? -1000 : 1000;

	data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
	data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
	data[1] = (speed < 0);
	data[2] = speed < 0 ? 3 : 7;
	data[3] = itv->params.video_b_frames;
	data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
	data[5] = 0;
	data[6] = 0;

	if (speed == 1500 || speed == -1500) data[0] |= 1;
	else if (speed == 2000 || speed == -2000) data[0] |= 2;
	else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
	else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);

	/* If not decoding, just change speed setting */
	if (atomic_read(&itv->decoding) > 0) {
		int got_sig = 0;

		/* Stop all DMA and decoding activity */
		ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);

		/* Wait for any DMA to finish */
		prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
184
		while (test_bit(IVTV_F_I_DMA, &itv->i_flags)) {
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
			got_sig = signal_pending(current);
			if (got_sig)
				break;
			got_sig = 0;
			schedule();
		}
		finish_wait(&itv->dma_waitq, &wait);
		if (got_sig)
			return -EINTR;

		/* Change Speed safely */
		ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
		IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
				data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
	}
	if (single_step) {
		speed = (speed < 0) ? -1 : 1;
		ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
	}
	itv->speed = speed;
	return 0;
}

static int ivtv_validate_speed(int cur_speed, int new_speed)
{
	int fact = new_speed < 0 ? -1 : 1;
	int s;

213 214 215 216 217 218
	if (cur_speed == 0)
		cur_speed = 1000;
	if (new_speed < 0)
		new_speed = -new_speed;
	if (cur_speed < 0)
		cur_speed = -cur_speed;
219 220

	if (cur_speed <= new_speed) {
221 222 223 224
		if (new_speed > 1500)
			return fact * 2000;
		if (new_speed > 1000)
			return fact * 1500;
225 226
	}
	else {
227 228 229 230 231 232
		if (new_speed >= 2000)
			return fact * 2000;
		if (new_speed >= 1500)
			return fact * 1500;
		if (new_speed >= 1000)
			return fact * 1000;
233
	}
234 235 236 237
	if (new_speed == 0)
		return 1000;
	if (new_speed == 1 || new_speed == 1000)
		return fact * new_speed;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

	s = new_speed;
	new_speed = 1000 / new_speed;
	if (1000 / cur_speed == new_speed)
		new_speed += (cur_speed < s) ? -1 : 1;
	if (new_speed > 60) return 1000 / (fact * 60);
	return 1000 / (fact * new_speed);
}

static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
		struct video_command *vc, int try)
{
	struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];

	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
		return -EINVAL;

	switch (vc->cmd) {
	case VIDEO_CMD_PLAY: {
257
		vc->flags = 0;
258 259 260 261 262 263 264
		vc->play.speed = ivtv_validate_speed(itv->speed, vc->play.speed);
		if (vc->play.speed < 0)
			vc->play.format = VIDEO_PLAY_FMT_GOP;
		if (try) break;

		if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
			return -EBUSY;
265 266 267 268
		if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
			/* forces ivtv_set_speed to be called */
			itv->speed = 0;
		}
269 270 271 272
		return ivtv_start_decoding(id, vc->play.speed);
	}

	case VIDEO_CMD_STOP:
273
		vc->flags &= VIDEO_CMD_STOP_IMMEDIATELY|VIDEO_CMD_STOP_TO_BLACK;
274 275 276 277 278 279 280 281 282 283 284 285
		if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
			vc->stop.pts = 0;
		if (try) break;
		if (atomic_read(&itv->decoding) == 0)
			return 0;
		if (itv->output_mode != OUT_MPG)
			return -EBUSY;

		itv->output_mode = OUT_NONE;
		return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);

	case VIDEO_CMD_FREEZE:
286
		vc->flags &= VIDEO_CMD_FREEZE_TO_BLACK;
287 288 289 290 291 292
		if (try) break;
		if (itv->output_mode != OUT_MPG)
			return -EBUSY;
		if (atomic_read(&itv->decoding) > 0) {
			ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
				(vc->flags & VIDEO_CMD_FREEZE_TO_BLACK) ? 1 : 0);
293
			set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
294 295 296 297
		}
		break;

	case VIDEO_CMD_CONTINUE:
298
		vc->flags = 0;
299 300 301
		if (try) break;
		if (itv->output_mode != OUT_MPG)
			return -EBUSY;
302 303 304 305
		if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
			int speed = itv->speed;
			itv->speed = 0;
			return ivtv_start_decoding(id, speed);
306 307 308 309 310 311 312 313 314
		}
		break;

	default:
		return -EINVAL;
	}
	return 0;
}

315
static int ivtv_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
316
{
317 318
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
319

320 321
	vbifmt->reserved[0] = 0;
	vbifmt->reserved[1] = 0;
322 323 324 325 326 327 328 329 330
	if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
		return -EINVAL;
	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
	if (itv->is_60hz) {
		vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
		vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
	} else {
		vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
		vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
331
	}
332 333 334
	vbifmt->service_set = ivtv_get_service_set(vbifmt);
	return 0;
}
335

336 337 338 339
static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
340 341 342 343 344 345 346 347 348
	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;

	pixfmt->width = itv->params.width;
	pixfmt->height = itv->params.height;
	pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
	pixfmt->field = V4L2_FIELD_INTERLACED;
	pixfmt->priv = 0;
	if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
		pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
349 350
		/* YUV size is (Y=(h*720) + UV=(h*(720/2))) */
		pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
351
		pixfmt->bytesperline = 720;
352
	} else {
353 354 355
		pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
		pixfmt->sizeimage = 128 * 1024;
		pixfmt->bytesperline = 0;
356 357 358 359
	}
	return 0;
}

360
static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
361
{
362
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
363 364 365 366 367 368 369 370 371 372 373 374
	struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;

	vbifmt->sampling_rate = 27000000;
	vbifmt->offset = 248;
	vbifmt->samples_per_line = itv->vbi.raw_decoder_line_size - 4;
	vbifmt->sample_format = V4L2_PIX_FMT_GREY;
	vbifmt->start[0] = itv->vbi.start[0];
	vbifmt->start[1] = itv->vbi.start[1];
	vbifmt->count[0] = vbifmt->count[1] = itv->vbi.count;
	vbifmt->flags = 0;
	vbifmt->reserved[0] = 0;
	vbifmt->reserved[1] = 0;
375 376
	return 0;
}
377

378 379 380 381 382
static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
383

384 385
	vbifmt->reserved[0] = 0;
	vbifmt->reserved[1] = 0;
386
	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
387

388 389 390 391 392 393
	if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
		vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
			V4L2_SLICED_VBI_525;
		ivtv_expand_service_set(vbifmt, itv->is_50hz);
		return 0;
	}
394

395
	v4l2_subdev_call(itv->sd_video, vbi, g_sliced_fmt, vbifmt);
396 397 398
	vbifmt->service_set = ivtv_get_service_set(vbifmt);
	return 0;
}
399

400 401 402 403
static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
404
	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
405

406 407
	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
		return -EINVAL;
408 409 410 411 412
	pixfmt->width = itv->main_rect.width;
	pixfmt->height = itv->main_rect.height;
	pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
	pixfmt->field = V4L2_FIELD_INTERLACED;
	pixfmt->priv = 0;
413 414 415
	if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
		switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
		case IVTV_YUV_MODE_INTERLACED:
416
			pixfmt->field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
417 418 419
				V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
			break;
		case IVTV_YUV_MODE_PROGRESSIVE:
420
			pixfmt->field = V4L2_FIELD_NONE;
421 422
			break;
		default:
423
			pixfmt->field = V4L2_FIELD_ANY;
424
			break;
425
		}
426 427 428 429
		pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
		pixfmt->bytesperline = 720;
		pixfmt->width = itv->yuv_info.v4l2_src_w;
		pixfmt->height = itv->yuv_info.v4l2_src_h;
430
		/* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
431 432
		pixfmt->sizeimage =
			1080 * ((pixfmt->height + 31) & ~31);
433
	} else {
434 435 436
		pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
		pixfmt->sizeimage = 128 * 1024;
		pixfmt->bytesperline = 0;
437
	}
438 439
	return 0;
}
440

441 442 443
static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
444
	struct v4l2_window *winfmt = &fmt->fmt.win;
445

446 447
	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
		return -EINVAL;
448 449 450 451 452 453 454 455 456
	winfmt->chromakey = itv->osd_chroma_key;
	winfmt->global_alpha = itv->osd_global_alpha;
	winfmt->field = V4L2_FIELD_INTERLACED;
	winfmt->clips = NULL;
	winfmt->clipcount = 0;
	winfmt->bitmap = NULL;
	winfmt->w.top = winfmt->w.left = 0;
	winfmt->w.width = itv->osd_rect.width;
	winfmt->w.height = itv->osd_rect.height;
457 458
	return 0;
}
459

460 461 462 463
static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
{
	return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
}
464

465 466 467 468 469 470
static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
	int w = fmt->fmt.pix.width;
	int h = fmt->fmt.pix.height;
471
	int min_h = 2;
472 473

	w = min(w, 720);
474
	w = max(w, 2);
475 476 477 478 479
	if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
		/* YUV height must be a multiple of 32 */
		h &= ~0x1f;
		min_h = 32;
	}
480
	h = min(h, itv->is_50hz ? 576 : 480);
481
	h = max(h, min_h);
482 483 484 485 486
	ivtv_g_fmt_vid_cap(file, fh, fmt);
	fmt->fmt.pix.width = w;
	fmt->fmt.pix.height = h;
	return 0;
}
487

488 489 490 491
static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
{
	return ivtv_g_fmt_vbi_cap(file, fh, fmt);
}
492

493 494 495 496 497
static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
498

499 500
	if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
		return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
501 502 503

	/* set sliced VBI capture format */
	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
504 505
	vbifmt->reserved[0] = 0;
	vbifmt->reserved[1] = 0;
506 507

	if (vbifmt->service_set)
508
		ivtv_expand_service_set(vbifmt, itv->is_50hz);
509
	check_service_set(vbifmt, itv->is_50hz);
510
	vbifmt->service_set = ivtv_get_service_set(vbifmt);
511 512 513 514 515 516
	return 0;
}

static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct ivtv_open_id *id = fh;
517 518 519 520 521
	s32 w = fmt->fmt.pix.width;
	s32 h = fmt->fmt.pix.height;
	int field = fmt->fmt.pix.field;
	int ret = ivtv_g_fmt_vid_out(file, fh, fmt);

522 523
	w = min(w, 720);
	w = max(w, 2);
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	/* Why can the height be 576 even when the output is NTSC?

	   Internally the buffers of the PVR350 are always set to 720x576. The
	   decoded video frame will always be placed in the top left corner of
	   this buffer. For any video which is not 720x576, the buffer will
	   then be cropped to remove the unused right and lower areas, with
	   the remaining image being scaled by the hardware to fit the display
	   area. The video can be scaled both up and down, so a 720x480 video
	   can be displayed full-screen on PAL and a 720x576 video can be
	   displayed without cropping on NTSC.

	   Note that the scaling only occurs on the video stream, the osd
	   resolution is locked to the broadcast standard and not scaled.

	   Thanks to Ian Armstrong for this explanation. */
	h = min(h, 576);
540 541
	h = max(h, 2);
	if (id->type == IVTV_DEC_STREAM_TYPE_YUV)
542
		fmt->fmt.pix.field = field;
543 544
	fmt->fmt.pix.width = w;
	fmt->fmt.pix.height = h;
545 546 547 548 549 550
	return ret;
}

static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
551 552
	u32 chromakey = fmt->fmt.win.chromakey;
	u8 global_alpha = fmt->fmt.win.global_alpha;
553

554 555
	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
		return -EINVAL;
556 557 558
	ivtv_g_fmt_vid_out_overlay(file, fh, fmt);
	fmt->fmt.win.chromakey = chromakey;
	fmt->fmt.win.global_alpha = global_alpha;
559 560 561 562 563 564 565 566 567 568 569 570 571
	return 0;
}

static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
{
	return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
}

static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
	struct cx2341x_mpeg_params *p = &itv->params;
572
	int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
573 574 575 576 577 578 579
	int w = fmt->fmt.pix.width;
	int h = fmt->fmt.pix.height;

	if (ret)
		return ret;

	if (p->width == w && p->height == h)
580
		return 0;
581 582 583 584 585 586 587 588

	if (atomic_read(&itv->capturing) > 0)
		return -EBUSY;

	p->width = w;
	p->height = h;
	if (p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
		fmt->fmt.pix.width /= 2;
589
	v4l2_subdev_call(itv->sd_video, video, s_fmt, fmt);
590 591 592 593 594 595 596
	return ivtv_g_fmt_vid_cap(file, fh, fmt);
}

static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;

597 598
	if (!ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
		return -EBUSY;
599
	itv->vbi.sliced_in->service_set = 0;
600
	itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
601
	v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &fmt->fmt.vbi);
602 603 604 605 606 607 608 609 610 611 612 613 614
	return ivtv_g_fmt_vbi_cap(file, fh, fmt);
}

static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
{
	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
	int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);

	if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
		return ret;

615
	check_service_set(vbifmt, itv->is_50hz);
616
	if (ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
617
		return -EBUSY;
618
	itv->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
619
	v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, vbifmt);
620 621 622 623
	memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
	return 0;
}

624
static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
625
{
626
	struct ivtv_open_id *id = fh;
627
	struct ivtv *itv = id->itv;
628 629
	struct yuv_playback_info *yi = &itv->yuv_info;
	int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
630

631 632
	if (ret)
		return ret;
633

634 635
	if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
		return 0;
636

637 638 639
	/* Return now if we already have some frame data */
	if (yi->stream_size)
		return -EBUSY;
640

641 642
	yi->v4l2_src_w = fmt->fmt.pix.width;
	yi->v4l2_src_h = fmt->fmt.pix.height;
643

644 645 646
	switch (fmt->fmt.pix.field) {
	case V4L2_FIELD_NONE:
		yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
647
		break;
648 649 650 651 652 653 654 655
	case V4L2_FIELD_ANY:
		yi->lace_mode = IVTV_YUV_MODE_AUTO;
		break;
	case V4L2_FIELD_INTERLACED_BT:
		yi->lace_mode =
			IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
		break;
	case V4L2_FIELD_INTERLACED_TB:
656
	default:
657 658
		yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
		break;
659
	}
660 661 662 663 664 665
	yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;

	if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
		itv->dma_data_req_size =
			1080 * ((yi->v4l2_src_h + 31) & ~31);

666 667 668
	return 0;
}

669
static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
670
{
671 672
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
	int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
673

674 675 676 677
	if (ret == 0) {
		itv->osd_chroma_key = fmt->fmt.win.chromakey;
		itv->osd_global_alpha = fmt->fmt.win.global_alpha;
		ivtv_set_osd_alpha(itv);
678
	}
679 680
	return ret;
}
681

682
static int ivtv_g_chip_ident(struct file *file, void *fh, struct v4l2_dbg_chip_ident *chip)
683 684
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
685

686 687
	chip->ident = V4L2_IDENT_NONE;
	chip->revision = 0;
688 689
	if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
		if (v4l2_chip_match_host(&chip->match))
690 691
			chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
		return 0;
692
	}
693 694
	if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
	    chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
695 696 697
		return -EINVAL;
	/* TODO: is this correct? */
	return ivtv_call_all_err(itv, core, g_chip_ident, chip);
698
}
699

700 701 702
#ifdef CONFIG_VIDEO_ADV_DEBUG
static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
{
703
	struct v4l2_dbg_register *regs = arg;
704
	volatile u8 __iomem *reg_start;
705 706 707 708 709 710 711 712

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;
	if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
		reg_start = itv->reg_mem - IVTV_REG_OFFSET;
	else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
			regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
		reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
713
	else if (regs->reg < IVTV_ENCODER_SIZE)
714 715 716 717
		reg_start = itv->enc_mem;
	else
		return -EINVAL;

718
	regs->size = 4;
719 720 721 722 723 724 725
	if (cmd == VIDIOC_DBG_G_REGISTER)
		regs->val = readl(regs->reg + reg_start);
	else
		writel(regs->val, regs->reg + reg_start);
	return 0;
}

726
static int ivtv_g_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
727 728
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
729

730
	if (v4l2_chip_match_host(&reg->match))
731
		return ivtv_itvc(itv, VIDIOC_DBG_G_REGISTER, reg);
732 733 734 735
	/* TODO: subdev errors should not be ignored, this should become a
	   subdev helper function. */
	ivtv_call_all(itv, core, g_register, reg);
	return 0;
736 737
}

738
static int ivtv_s_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
739 740
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
741

742
	if (v4l2_chip_match_host(&reg->match))
743
		return ivtv_itvc(itv, VIDIOC_DBG_S_REGISTER, reg);
744 745 746 747
	/* TODO: subdev errors should not be ignored, this should become a
	   subdev helper function. */
	ivtv_call_all(itv, core, s_register, reg);
	return 0;
748
}
749
#endif
750

751 752 753
static int ivtv_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
754

755
	*p = v4l2_prio_max(&itv->prio);
756

757 758
	return 0;
}
759

760 761 762 763
static int ivtv_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
764

765 766
	return v4l2_prio_change(&itv->prio, &id->prio, prio);
}
767

768 769 770 771 772 773
static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;

	strlcpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
	strlcpy(vcap->card, itv->card_name, sizeof(vcap->card));
774
	snprintf(vcap->bus_info, sizeof(vcap->bus_info), "PCI:%s", pci_name(itv->pdev));
775 776 777 778
	vcap->version = IVTV_DRIVER_VERSION; 	    /* version */
	vcap->capabilities = itv->v4l2_cap; 	    /* capabilities */
	return 0;
}
779

780 781 782
static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
783

784 785 786 787 788 789
	return ivtv_get_audio_input(itv, vin->index, vin);
}

static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
790

791 792 793
	vin->index = itv->audio_input;
	return ivtv_get_audio_input(itv, vin->index, vin);
}
794

795 796 797
static int ivtv_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
798

799 800
	if (vout->index >= itv->nof_audio_inputs)
		return -EINVAL;
801

802 803
	itv->audio_input = vout->index;
	ivtv_audio_set_io(itv);
804

805 806
	return 0;
}
807

808 809 810
static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
811

812 813 814
	/* set it to defaults from our table */
	return ivtv_get_audio_output(itv, vin->index, vin);
}
815

816 817 818
static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
819

820 821 822
	vin->index = 0;
	return ivtv_get_audio_output(itv, vin->index, vin);
}
823

824 825 826
static int ivtv_s_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
827

828 829
	return ivtv_get_audio_output(itv, vout->index, vout);
}
830

831 832 833
static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
834

835 836 837
	/* set it to defaults from our table */
	return ivtv_get_input(itv, vin->index, vin);
}
838

839 840 841
static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
842

843 844
	return ivtv_get_output(itv, vout->index, vout);
}
845

846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
static int ivtv_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
	struct yuv_playback_info *yi = &itv->yuv_info;
	int streamtype;

	streamtype = id->type;

	if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
		return -EINVAL;
	cropcap->bounds.top = cropcap->bounds.left = 0;
	cropcap->bounds.width = 720;
	if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
		cropcap->bounds.height = itv->is_50hz ? 576 : 480;
		cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
		cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
	} else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
		if (yi->track_osd) {
			cropcap->bounds.width = yi->osd_full_w;
			cropcap->bounds.height = yi->osd_full_h;
867
		} else {
868 869 870
			cropcap->bounds.width = 720;
			cropcap->bounds.height =
					itv->is_out_50hz ? 576 : 480;
871
		}
872 873 874 875 876 877
		cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
		cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
	} else {
		cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
		cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
		cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
878
	}
879 880 881 882 883 884 885 886 887 888
	cropcap->defrect = cropcap->bounds;
	return 0;
}

static int ivtv_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
	struct yuv_playback_info *yi = &itv->yuv_info;
	int streamtype;
889

890
	streamtype = id->type;
891

892 893 894 895 896 897 898 899 900
	if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
	    (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
		if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
			yi->main_rect = crop->c;
			return 0;
		} else {
			if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
				crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
				itv->main_rect = crop->c;
901 902 903
				return 0;
			}
		}
904
		return -EINVAL;
905
	}
906 907
	return -EINVAL;
}
908

909 910 911 912 913 914
static int ivtv_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
	struct yuv_playback_info *yi = &itv->yuv_info;
	int streamtype;
915

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
	streamtype = id->type;

	if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
	    (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
		if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
			crop->c = yi->main_rect;
		else
			crop->c = itv->main_rect;
		return 0;
	}
	return -EINVAL;
}

static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
{
	static struct v4l2_fmtdesc formats[] = {
		{ 0, 0, 0,
		  "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
		  { 0, 0, 0, 0 }
		},
		{ 1, 0, V4L2_FMT_FLAG_COMPRESSED,
		  "MPEG", V4L2_PIX_FMT_MPEG,
		  { 0, 0, 0, 0 }
939
		}
940 941 942 943
	};
	enum v4l2_buf_type type = fmt->type;

	if (fmt->index > 1)
944
		return -EINVAL;
945

946 947 948 949
	*fmt = formats[fmt->index];
	fmt->type = type;
	return 0;
}
950

951 952 953 954 955 956 957 958 959 960 961 962
static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;

	static struct v4l2_fmtdesc formats[] = {
		{ 0, 0, 0,
		  "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
		  { 0, 0, 0, 0 }
		},
		{ 1, 0, V4L2_FMT_FLAG_COMPRESSED,
		  "MPEG", V4L2_PIX_FMT_MPEG,
		  { 0, 0, 0, 0 }
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
	};
	enum v4l2_buf_type type = fmt->type;

	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
		return -EINVAL;

	if (fmt->index > 1)
		return -EINVAL;

	*fmt = formats[fmt->index];
	fmt->type = type;

	return 0;
}

static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;

	*i = itv->active_input;

	return 0;
}

int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;

	if (inp < 0 || inp >= itv->nof_inputs)
		return -EINVAL;

	if (inp == itv->active_input) {
		IVTV_DEBUG_INFO("Input unchanged\n");
997 998 999
		return 0;
	}

1000 1001
	if (atomic_read(&itv->capturing) > 0) {
		return -EBUSY;
1002 1003
	}

1004 1005
	IVTV_DEBUG_INFO("Changing input from %d to %d\n",
			itv->active_input, inp);
1006

1007 1008 1009 1010
	itv->active_input = inp;
	/* Set the audio input to whatever is appropriate for the
	   input type. */
	itv->audio_input = itv->card->video_inputs[inp].audio_index;
1011

1012 1013 1014 1015 1016 1017 1018 1019 1020
	/* prevent others from messing with the streams until
	   we're finished changing inputs. */
	ivtv_mute(itv);
	ivtv_video_set_io(itv);
	ivtv_audio_set_io(itv);
	ivtv_unmute(itv);

	return 0;
}
1021

1022 1023 1024
static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1025

1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
		return -EINVAL;

	*i = itv->active_output;

	return 0;
}

static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;

	if (outp >= itv->card->nof_outputs)
		return -EINVAL;

	if (outp == itv->active_output) {
		IVTV_DEBUG_INFO("Output unchanged\n");
		return 0;
1044
	}
1045 1046
	IVTV_DEBUG_INFO("Changing output from %d to %d\n",
		   itv->active_output, outp);
1047

1048
	itv->active_output = outp;
1049 1050 1051
	ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
			SAA7127_INPUT_TYPE_NORMAL,
			itv->card->video_outputs[outp].video_output, 0);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062

	return 0;
}

static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;

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

1063
	ivtv_call_all(itv, tuner, g_frequency, vf);
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	return 0;
}

int ivtv_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;

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

	ivtv_mute(itv);
	IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1076
	ivtv_call_all(itv, tuner, s_frequency, vf);
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
	ivtv_unmute(itv);
	return 0;
}

static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;

	*std = itv->std;
	return 0;
}

int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
{
1091
	DEFINE_WAIT(wait);
1092 1093
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
	struct yuv_playback_info *yi = &itv->yuv_info;
1094
	int f;
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125

	if ((*std & V4L2_STD_ALL) == 0)
		return -EINVAL;

	if (*std == itv->std)
		return 0;

	if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
	    atomic_read(&itv->capturing) > 0 ||
	    atomic_read(&itv->decoding) > 0) {
		/* Switching standard would turn off the radio or mess
		   with already running streams, prevent that by
		   returning EBUSY. */
		return -EBUSY;
	}

	itv->std = *std;
	itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
	itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
	itv->params.width = 720;
	itv->params.height = itv->is_50hz ? 576 : 480;
	itv->vbi.count = itv->is_50hz ? 18 : 12;
	itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
	itv->vbi.start[1] = itv->is_50hz ? 318 : 273;

	if (itv->hw_flags & IVTV_HW_CX25840)
		itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;

	IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);

	/* Tuner */
1126
	ivtv_call_all(itv, core, s_std, itv->std);
1127 1128 1129 1130 1131 1132

	if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
		/* set display standard */
		itv->std_out = *std;
		itv->is_out_60hz = itv->is_60hz;
		itv->is_out_50hz = itv->is_50hz;
1133
		ivtv_call_all(itv, video, s_std_output, itv->std_out);
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

		/*
		 * The next firmware call is time sensitive. Time it to
		 * avoid risk of a hard lock, by trying to ensure the call
		 * happens within the first 100 lines of the top field.
		 * Make 4 attempts to sync to the decoder before giving up.
		 */
		for (f = 0; f < 4; f++) {
			prepare_to_wait(&itv->vsync_waitq, &wait,
					TASK_UNINTERRUPTIBLE);
1144
			if ((read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16) < 100)
1145 1146 1147 1148 1149 1150 1151 1152
				break;
			schedule_timeout(msecs_to_jiffies(25));
		}
		finish_wait(&itv->vsync_waitq, &wait);

		if (f == 4)
			IVTV_WARN("Mode change failed to sync to decoder\n");

1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
		ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
		itv->main_rect.left = itv->main_rect.top = 0;
		itv->main_rect.width = 720;
		itv->main_rect.height = itv->params.height;
		ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
			720, itv->main_rect.height, 0, 0);
		yi->main_rect = itv->main_rect;
		if (!itv->osd_info) {
			yi->osd_full_w = 720;
			yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
		}
	}
	return 0;
}

static int ivtv_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;

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

1176
	ivtv_call_all(itv, tuner, s_tuner, vt);
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187

	return 0;
}

static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;

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

1188
	ivtv_call_all(itv, tuner, g_tuner, vt);
1189 1190 1191 1192 1193 1194 1195

	if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
		strlcpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
		vt->type = V4L2_TUNER_RADIO;
	} else {
		strlcpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
		vt->type = V4L2_TUNER_ANALOG_TV;
1196 1197
	}

1198 1199
	return 0;
}
1200

1201 1202 1203 1204 1205 1206
static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
	int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
	int f, l;

1207
	if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1208 1209 1210 1211 1212 1213 1214 1215
		for (f = 0; f < 2; f++) {
			for (l = 0; l < 24; l++) {
				if (valid_service_line(f, l, itv->is_50hz))
					cap->service_lines[f][l] = set;
			}
		}
		return 0;
	}
1216
	if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1217
		if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1218
			return -EINVAL;
1219 1220 1221 1222 1223 1224
		if (itv->is_60hz) {
			cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
			cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
		} else {
			cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
			cap->service_lines[0][16] = V4L2_SLICED_VPS;
1225
		}
1226
		return 0;
1227
	}
1228 1229
	return -EINVAL;
}
1230

1231 1232 1233 1234 1235 1236
static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
	struct v4l2_enc_idx_entry *e = idx->entry;
	int entries;
	int i;
1237

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
				IVTV_MAX_PGM_INDEX;
	if (entries > V4L2_ENC_IDX_ENTRIES)
		entries = V4L2_ENC_IDX_ENTRIES;
	idx->entries = 0;
	for (i = 0; i < entries; i++) {
		*e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
		if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
			idx->entries++;
			e++;
		}
1249
	}
1250 1251 1252
	itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
	return 0;
}
1253

1254 1255 1256 1257
static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
1258 1259


1260 1261 1262 1263 1264
	switch (enc->cmd) {
	case V4L2_ENC_CMD_START:
		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
		enc->flags = 0;
		return ivtv_start_capture(id);
1265

1266 1267 1268 1269 1270
	case V4L2_ENC_CMD_STOP:
		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
		ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
		return 0;
1271

1272 1273 1274
	case V4L2_ENC_CMD_PAUSE:
		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
		enc->flags = 0;
1275

1276 1277 1278 1279
		if (!atomic_read(&itv->capturing))
			return -EPERM;
		if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
			return 0;
1280

1281 1282
		ivtv_mute(itv);
		ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1283 1284
		break;

1285 1286 1287
	case V4L2_ENC_CMD_RESUME:
		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
		enc->flags = 0;
1288

1289 1290
		if (!atomic_read(&itv->capturing))
			return -EPERM;
1291

1292 1293
		if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
			return 0;
1294

1295 1296
		ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
		ivtv_unmute(itv);
1297
		break;
1298 1299 1300
	default:
		IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
		return -EINVAL;
1301 1302
	}

1303 1304
	return 0;
}
1305

1306 1307 1308
static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1309

1310 1311 1312 1313 1314
	switch (enc->cmd) {
	case V4L2_ENC_CMD_START:
		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
		enc->flags = 0;
		return 0;
1315

1316 1317 1318 1319
	case V4L2_ENC_CMD_STOP:
		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
		return 0;
1320

1321 1322 1323 1324
	case V4L2_ENC_CMD_PAUSE:
		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
		enc->flags = 0;
		return 0;
1325

1326 1327 1328 1329 1330 1331 1332
	case V4L2_ENC_CMD_RESUME:
		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
		enc->flags = 0;
		return 0;
	default:
		IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
		return -EINVAL;
1333
	}
1334
}
1335

1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
	u32 data[CX2341X_MBOX_MAX_DATA];
	struct yuv_playback_info *yi = &itv->yuv_info;

	int pixfmt;
	static u32 pixel_format[16] = {
		V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
		V4L2_PIX_FMT_RGB565,
		V4L2_PIX_FMT_RGB555,
		V4L2_PIX_FMT_RGB444,
		V4L2_PIX_FMT_RGB32,
		0,
		0,
		0,
		V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
		V4L2_PIX_FMT_YUV565,
		V4L2_PIX_FMT_YUV555,
		V4L2_PIX_FMT_YUV444,
		V4L2_PIX_FMT_YUV32,
		0,
		0,
		0,
	};

	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1363
		return -EINVAL;
1364 1365
	if (!itv->osd_video_pbase)
		return -EINVAL;
1366

1367 1368
	fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
		V4L2_FBUF_CAP_GLOBAL_ALPHA;
1369

1370 1371 1372
	ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
	data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
	pixfmt = (data[0] >> 3) & 0xf;
1373

1374 1375 1376
	fb->fmt.pixelformat = pixel_format[pixfmt];
	fb->fmt.width = itv->osd_rect.width;
	fb->fmt.height = itv->osd_rect.height;
1377 1378
	fb->fmt.field = V4L2_FIELD_INTERLACED;
	fb->fmt.bytesperline = fb->fmt.width;
1379 1380 1381
	fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
	fb->fmt.field = V4L2_FIELD_INTERLACED;
	fb->fmt.priv = 0;
1382 1383 1384 1385 1386
	if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
		fb->fmt.bytesperline *= 2;
	if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
	    fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
		fb->fmt.bytesperline *= 2;
1387
	fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
1388
	fb->base = (void *)itv->osd_video_pbase;
1389
	fb->flags = 0;
1390

1391 1392
	if (itv->osd_chroma_key_state)
		fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1393

1394 1395
	if (itv->osd_global_alpha_state)
		fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1396

1397 1398 1399
	if (yi->track_osd)
		fb->flags |= V4L2_FBUF_FLAG_OVERLAY;

1400
	pixfmt &= 7;
1401

1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
	/* no local alpha for RGB565 or unknown formats */
	if (pixfmt == 1 || pixfmt > 4)
		return 0;

	/* 16-bit formats have inverted local alpha */
	if (pixfmt == 2 || pixfmt == 3)
		fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
	else
		fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;

	if (itv->osd_local_alpha_state) {
1413 1414
		/* 16-bit formats have inverted local alpha */
		if (pixfmt == 2 || pixfmt == 3)
1415
			fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1416
		else
1417
			fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1418
	}
1419 1420 1421

	return 0;
}
1422

1423 1424 1425 1426 1427
static int ivtv_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
	struct yuv_playback_info *yi = &itv->yuv_info;
1428

1429 1430
	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
		return -EINVAL;
1431 1432
	if (!itv->osd_video_pbase)
		return -EINVAL;
1433

1434 1435 1436 1437 1438 1439
	itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
	itv->osd_local_alpha_state =
		(fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
	itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
	ivtv_set_osd_alpha(itv);
	yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1440
	return ivtv_g_fbuf(file, fh, fb);
1441 1442 1443 1444 1445 1446
}

static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
{
	struct ivtv_open_id *id = fh;
	struct ivtv *itv = id->itv;
1447

1448 1449
	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
		return -EINVAL;
1450

1451
	ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1452

1453 1454 1455
	return 0;
}

1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
static int ivtv_subscribe_event(struct v4l2_fh *fh, struct v4l2_event_subscription *sub)
{
	switch (sub->type) {
	case V4L2_EVENT_VSYNC:
	case V4L2_EVENT_EOS:
		break;
	default:
		return -EINVAL;
	}
	return v4l2_event_subscribe(fh, sub);
}

1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
static int ivtv_log_status(struct file *file, void *fh)
{
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
	u32 data[CX2341X_MBOX_MAX_DATA];

	int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
	struct v4l2_input vidin;
	struct v4l2_audio audin;
	int i;

1478 1479
	IVTV_INFO("=================  START STATUS CARD #%d  =================\n",
		       itv->instance);
1480 1481 1482 1483 1484 1485
	IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
	if (itv->hw_flags & IVTV_HW_TVEEPROM) {
		struct tveeprom tv;

		ivtv_read_eeprom(itv, &tv);
	}
1486
	ivtv_call_all(itv, core, log_status);
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 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
	ivtv_get_input(itv, itv->active_input, &vidin);
	ivtv_get_audio_input(itv, itv->audio_input, &audin);
	IVTV_INFO("Video Input:  %s\n", vidin.name);
	IVTV_INFO("Audio Input:  %s%s\n", audin.name,
		(itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
	if (has_output) {
		struct v4l2_output vidout;
		struct v4l2_audioout audout;
		int mode = itv->output_mode;
		static const char * const output_modes[5] = {
			"None",
			"MPEG Streaming",
			"YUV Streaming",
			"YUV Frames",
			"Passthrough",
		};
		static const char * const audio_modes[5] = {
			"Stereo",
			"Left",
			"Right",
			"Mono",
			"Swapped"
		};
		static const char * const alpha_mode[4] = {
			"None",
			"Global",
			"Local",
			"Global and Local"
		};
		static const char * const pixel_format[16] = {
			"ARGB Indexed",
			"RGB 5:6:5",
			"ARGB 1:5:5:5",
			"ARGB 1:4:4:4",
			"ARGB 8:8:8:8",
			"5",
			"6",
			"7",
			"AYUV Indexed",
			"YUV 5:6:5",
			"AYUV 1:5:5:5",
			"AYUV 1:4:4:4",
			"AYUV 8:8:8:8",
			"13",
			"14",
			"15",
		};

		ivtv_get_output(itv, itv->active_output, &vidout);
		ivtv_get_audio_output(itv, 0, &audout);
		IVTV_INFO("Video Output: %s\n", vidout.name);
		IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
			audio_modes[itv->audio_stereo_mode],
			audio_modes[itv->audio_bilingual_mode]);
		if (mode < 0 || mode > OUT_PASSTHROUGH)
			mode = OUT_NONE;
		IVTV_INFO("Output Mode:  %s\n", output_modes[mode]);
		ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
		data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
		IVTV_INFO("Overlay:      %s, Alpha: %s, Pixel Format: %s\n",
			data[0] & 1 ? "On" : "Off",
			alpha_mode[(data[0] >> 1) & 0x3],
			pixel_format[(data[0] >> 3) & 0xf]);
1550
	}
1551 1552
	IVTV_INFO("Tuner:  %s\n",
		test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1553
	cx2341x_log_status(&itv->params, itv->v4l2_dev.name);
1554 1555 1556
	IVTV_INFO("Status flags:    0x%08lx\n", itv->i_flags);
	for (i = 0; i < IVTV_MAX_STREAMS; i++) {
		struct ivtv_stream *s = &itv->streams[i];
1557

1558
		if (s->vdev == NULL || s->buffers == 0)
1559 1560 1561 1562
			continue;
		IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
				(s->buffers - s->q_free.buffers) * 100 / s->buffers,
				(s->buffers * s->buf_size) / 1024, s->buffers);
1563
	}
1564

1565 1566 1567 1568 1569
	IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n",
			(long long)itv->mpg_data_received,
			(long long)itv->vbi_data_inserted);
	IVTV_INFO("==================  END STATUS CARD #%d  ==================\n",
			itv->instance);
1570

1571 1572 1573
	return 0;
}

1574
static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1575
{
1576
	struct ivtv_open_id *id = fh2id(filp->private_data);
1577 1578 1579
	struct ivtv *itv = id->itv;
	int nonblocking = filp->f_flags & O_NONBLOCK;
	struct ivtv_stream *s = &itv->streams[id->type];
1580
	unsigned long iarg = (unsigned long)arg;
1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592

	switch (cmd) {
	case IVTV_IOC_DMA_FRAME: {
		struct ivtv_dma_frame *args = arg;

		IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
			return -EINVAL;
		if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
			return -EINVAL;
		if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
			return 0;
1593
		if (ivtv_start_decoding(id, id->type)) {
1594 1595 1596 1597 1598 1599
			return -EBUSY;
		}
		if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
			ivtv_release_stream(s);
			return -EBUSY;
		}
1600 1601
		/* Mark that this file handle started the UDMA_YUV mode */
		id->yuv_frames = 1;
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
		if (args->y_source == NULL)
			return 0;
		return ivtv_yuv_prep_frame(itv, args);
	}

	case VIDEO_GET_PTS: {
		u32 data[CX2341X_MBOX_MAX_DATA];
		u64 *pts = arg;

		IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
		if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
			*pts = s->dma_pts;
			break;
		}
		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
			return -EINVAL;

		if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
			*pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
					(u64)itv->last_dec_timing[1];
			break;
		}
		*pts = 0;
		if (atomic_read(&itv->decoding)) {
			if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
				IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
				return -EIO;
			}
			memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
			set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
			*pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
			/*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
		}
		break;
	}

	case VIDEO_GET_FRAME_COUNT: {
		u32 data[CX2341X_MBOX_MAX_DATA];
		u64 *frame = arg;

		IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
		if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
			*frame = 0;
			break;
		}
		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
			return -EINVAL;

		if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
			*frame = itv->last_dec_timing[0];
			break;
		}
		*frame = 0;
		if (atomic_read(&itv->decoding)) {
			if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
				IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
				return -EIO;
			}
			memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
			set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
			*frame = data[0];
		}
		break;
	}

	case VIDEO_PLAY: {
		struct video_command vc;

		IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
		memset(&vc, 0, sizeof(vc));
		vc.cmd = VIDEO_CMD_PLAY;
		return ivtv_video_command(itv, id, &vc, 0);
	}

	case VIDEO_STOP: {
		struct video_command vc;

		IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
		memset(&vc, 0, sizeof(vc));
		vc.cmd = VIDEO_CMD_STOP;
		vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
		return ivtv_video_command(itv, id, &vc, 0);
	}

	case VIDEO_FREEZE: {
		struct video_command vc;

		IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
		memset(&vc, 0, sizeof(vc));
		vc.cmd = VIDEO_CMD_FREEZE;
		return ivtv_video_command(itv, id, &vc, 0);
	}

	case VIDEO_CONTINUE: {
		struct video_command vc;

		IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
		memset(&vc, 0, sizeof(vc));
		vc.cmd = VIDEO_CMD_CONTINUE;
		return ivtv_video_command(itv, id, &vc, 0);
	}

	case VIDEO_COMMAND:
	case VIDEO_TRY_COMMAND: {
		struct video_command *vc = arg;
		int try = (cmd == VIDEO_TRY_COMMAND);

		if (try)
1710
			IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
1711
		else
1712
			IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
		return ivtv_video_command(itv, id, vc, try);
	}

	case VIDEO_GET_EVENT: {
		struct video_event *ev = arg;
		DEFINE_WAIT(wait);

		IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
			return -EINVAL;
		memset(ev, 0, sizeof(*ev));
		set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);

		while (1) {
			if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
				ev->type = VIDEO_EVENT_DECODER_STOPPED;
			else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
				ev->type = VIDEO_EVENT_VSYNC;
1731 1732 1733 1734 1735 1736 1737
				ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
					VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
				if (itv->output_mode == OUT_UDMA_YUV &&
					(itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
								IVTV_YUV_MODE_PROGRESSIVE) {
					ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
				}
1738 1739 1740 1741 1742
			}
			if (ev->type)
				return 0;
			if (nonblocking)
				return -EAGAIN;
1743 1744 1745 1746
			/* Wait for event. Note that serialize_lock is locked,
			   so to allow other processes to access the driver while
			   we are waiting unlock first and later lock again. */
			mutex_unlock(&itv->serialize_lock);
1747
			prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1748 1749
			if (!test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags) &&
			    !test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags))
1750 1751
				schedule();
			finish_wait(&itv->event_waitq, &wait);
1752
			mutex_lock(&itv->serialize_lock);
1753 1754 1755 1756 1757 1758 1759 1760 1761
			if (signal_pending(current)) {
				/* return if a signal was received */
				IVTV_DEBUG_INFO("User stopped wait for event\n");
				return -EINTR;
			}
		}
		break;
	}

1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
	case VIDEO_SELECT_SOURCE:
		IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
			return -EINVAL;
		return ivtv_passthrough_mode(itv, iarg == VIDEO_SOURCE_DEMUX);

	case AUDIO_SET_MUTE:
		IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
		itv->speed_mute_audio = iarg;
		return 0;

	case AUDIO_CHANNEL_SELECT:
		IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
		if (iarg > AUDIO_STEREO_SWAPPED)
			return -EINVAL;
		itv->audio_stereo_mode = iarg;
		ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
		return 0;

	case AUDIO_BILINGUAL_CHANNEL_SELECT:
		IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
		if (iarg > AUDIO_STEREO_SWAPPED)
			return -EINVAL;
		itv->audio_bilingual_mode = iarg;
		ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
		return 0;

1789 1790 1791 1792 1793 1794
	default:
		return -EINVAL;
	}
	return 0;
}

1795
static long ivtv_default(struct file *file, void *fh, int cmd, void *arg)
1796
{
1797
	struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1798

1799
	switch (cmd) {
1800 1801
	case VIDIOC_INT_RESET: {
		u32 val = *(u32 *)arg;
1802

1803 1804 1805
		if ((val == 0 && itv->options.newi2c) || (val & 0x01))
			ivtv_reset_ir_gpio(itv);
		if (val & 0x02)
1806
			v4l2_subdev_call(itv->sd_video, core, reset, 0);
1807 1808
		break;
	}
1809

1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
	case IVTV_IOC_DMA_FRAME:
	case VIDEO_GET_PTS:
	case VIDEO_GET_FRAME_COUNT:
	case VIDEO_GET_EVENT:
	case VIDEO_PLAY:
	case VIDEO_STOP:
	case VIDEO_FREEZE:
	case VIDEO_CONTINUE:
	case VIDEO_COMMAND:
	case VIDEO_TRY_COMMAND:
1820 1821 1822 1823
	case VIDEO_SELECT_SOURCE:
	case AUDIO_SET_MUTE:
	case AUDIO_CHANNEL_SELECT:
	case AUDIO_BILINGUAL_CHANNEL_SELECT:
1824 1825
		return ivtv_decoder_ioctls(file, cmd, (void *)arg);

1826
	default:
1827
		return -EINVAL;
1828 1829 1830 1831
	}
	return 0;
}

1832
static long ivtv_serialized_ioctl(struct ivtv *itv, struct file *filp,
1833
		unsigned int cmd, unsigned long arg)
1834
{
1835
	struct video_device *vfd = video_devdata(filp);
1836
	struct ivtv_open_id *id = fh2id(filp->private_data);
1837
	long ret;
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852

	/* check priority */
	switch (cmd) {
	case VIDIOC_S_CTRL:
	case VIDIOC_S_STD:
	case VIDIOC_S_INPUT:
	case VIDIOC_S_OUTPUT:
	case VIDIOC_S_TUNER:
	case VIDIOC_S_FREQUENCY:
	case VIDIOC_S_FMT:
	case VIDIOC_S_CROP:
	case VIDIOC_S_AUDIO:
	case VIDIOC_S_AUDOUT:
	case VIDIOC_S_EXT_CTRLS:
	case VIDIOC_S_FBUF:
1853
	case VIDIOC_S_PRIORITY:
1854
	case VIDIOC_OVERLAY:
1855
		ret = v4l2_prio_check(&itv->prio, id->prio);
1856 1857 1858 1859
		if (ret)
			return ret;
	}

1860 1861
	if (ivtv_debug & IVTV_DBGFLG_IOCTL)
		vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
1862
	ret = video_ioctl2(filp, cmd, arg);
1863 1864
	vfd->debug = 0;
	return ret;
1865
}
1866

1867
long ivtv_v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1868
{
1869
	struct ivtv_open_id *id = fh2id(filp->private_data);
1870
	struct ivtv *itv = id->itv;
1871
	long res;
1872

1873 1874 1875
	/* DQEVENT can block, so this should not run with the serialize lock */
	if (cmd == VIDIOC_DQEVENT)
		return ivtv_serialized_ioctl(itv, filp, cmd, arg);
1876
	mutex_lock(&itv->serialize_lock);
1877
	res = ivtv_serialized_ioctl(itv, filp, cmd, arg);
1878 1879 1880
	mutex_unlock(&itv->serialize_lock);
	return res;
}
1881

1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
	.vidioc_querycap    		    = ivtv_querycap,
	.vidioc_g_priority  		    = ivtv_g_priority,
	.vidioc_s_priority  		    = ivtv_s_priority,
	.vidioc_s_audio     		    = ivtv_s_audio,
	.vidioc_g_audio     		    = ivtv_g_audio,
	.vidioc_enumaudio   		    = ivtv_enumaudio,
	.vidioc_s_audout     		    = ivtv_s_audout,
	.vidioc_g_audout     		    = ivtv_g_audout,
	.vidioc_enum_input   		    = ivtv_enum_input,
	.vidioc_enum_output   		    = ivtv_enum_output,
	.vidioc_enumaudout   		    = ivtv_enumaudout,
	.vidioc_cropcap       		    = ivtv_cropcap,
	.vidioc_s_crop       		    = ivtv_s_crop,
	.vidioc_g_crop       		    = ivtv_g_crop,
	.vidioc_g_input      		    = ivtv_g_input,
	.vidioc_s_input      		    = ivtv_s_input,
	.vidioc_g_output     		    = ivtv_g_output,
	.vidioc_s_output     		    = ivtv_s_output,
	.vidioc_g_frequency 		    = ivtv_g_frequency,
	.vidioc_s_frequency  		    = ivtv_s_frequency,
	.vidioc_s_tuner      		    = ivtv_s_tuner,
	.vidioc_g_tuner      		    = ivtv_g_tuner,
	.vidioc_g_enc_index 		    = ivtv_g_enc_index,
	.vidioc_g_fbuf			    = ivtv_g_fbuf,
	.vidioc_s_fbuf			    = ivtv_s_fbuf,
	.vidioc_g_std 			    = ivtv_g_std,
	.vidioc_s_std 			    = ivtv_s_std,
	.vidioc_overlay			    = ivtv_overlay,
	.vidioc_log_status		    = ivtv_log_status,
	.vidioc_enum_fmt_vid_cap 	    = ivtv_enum_fmt_vid_cap,
	.vidioc_encoder_cmd  		    = ivtv_encoder_cmd,
	.vidioc_try_encoder_cmd 	    = ivtv_try_encoder_cmd,
	.vidioc_enum_fmt_vid_out 	    = ivtv_enum_fmt_vid_out,
	.vidioc_g_fmt_vid_cap 		    = ivtv_g_fmt_vid_cap,
	.vidioc_g_fmt_vbi_cap		    = ivtv_g_fmt_vbi_cap,
	.vidioc_g_fmt_sliced_vbi_cap        = ivtv_g_fmt_sliced_vbi_cap,
	.vidioc_g_fmt_vid_out               = ivtv_g_fmt_vid_out,
	.vidioc_g_fmt_vid_out_overlay       = ivtv_g_fmt_vid_out_overlay,
	.vidioc_g_fmt_sliced_vbi_out        = ivtv_g_fmt_sliced_vbi_out,
	.vidioc_s_fmt_vid_cap  		    = ivtv_s_fmt_vid_cap,
	.vidioc_s_fmt_vbi_cap 		    = ivtv_s_fmt_vbi_cap,
	.vidioc_s_fmt_sliced_vbi_cap        = ivtv_s_fmt_sliced_vbi_cap,
	.vidioc_s_fmt_vid_out               = ivtv_s_fmt_vid_out,
	.vidioc_s_fmt_vid_out_overlay       = ivtv_s_fmt_vid_out_overlay,
	.vidioc_s_fmt_sliced_vbi_out        = ivtv_s_fmt_sliced_vbi_out,
	.vidioc_try_fmt_vid_cap  	    = ivtv_try_fmt_vid_cap,
	.vidioc_try_fmt_vbi_cap		    = ivtv_try_fmt_vbi_cap,
	.vidioc_try_fmt_sliced_vbi_cap      = ivtv_try_fmt_sliced_vbi_cap,
	.vidioc_try_fmt_vid_out 	    = ivtv_try_fmt_vid_out,
	.vidioc_try_fmt_vid_out_overlay     = ivtv_try_fmt_vid_out_overlay,
	.vidioc_try_fmt_sliced_vbi_out 	    = ivtv_try_fmt_sliced_vbi_out,
	.vidioc_g_sliced_vbi_cap 	    = ivtv_g_sliced_vbi_cap,
	.vidioc_g_chip_ident 		    = ivtv_g_chip_ident,
1936
#ifdef CONFIG_VIDEO_ADV_DEBUG
1937 1938
	.vidioc_g_register 		    = ivtv_g_register,
	.vidioc_s_register 		    = ivtv_s_register,
1939
#endif
1940 1941 1942 1943 1944 1945
	.vidioc_default 		    = ivtv_default,
	.vidioc_queryctrl 		    = ivtv_queryctrl,
	.vidioc_querymenu 		    = ivtv_querymenu,
	.vidioc_g_ext_ctrls 		    = ivtv_g_ext_ctrls,
	.vidioc_s_ext_ctrls 		    = ivtv_s_ext_ctrls,
	.vidioc_try_ext_ctrls    	    = ivtv_try_ext_ctrls,
1946 1947
	.vidioc_subscribe_event 	    = ivtv_subscribe_event,
	.vidioc_unsubscribe_event 	    = v4l2_event_unsubscribe,
1948 1949 1950 1951 1952
};

void ivtv_set_funcs(struct video_device *vdev)
{
	vdev->ioctl_ops = &ivtv_ioctl_ops;
1953
}