1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3     ioctl system call
4     Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
5     Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
6 
7  */
8 
9 #include "ivtv-driver.h"
10 #include "ivtv-version.h"
11 #include "ivtv-mailbox.h"
12 #include "ivtv-i2c.h"
13 #include "ivtv-queue.h"
14 #include "ivtv-fileops.h"
15 #include "ivtv-vbi.h"
16 #include "ivtv-routing.h"
17 #include "ivtv-streams.h"
18 #include "ivtv-yuv.h"
19 #include "ivtv-ioctl.h"
20 #include "ivtv-gpio.h"
21 #include "ivtv-controls.h"
22 #include "ivtv-cards.h"
23 #include <media/i2c/saa7127.h>
24 #include <media/tveeprom.h>
25 #include <media/v4l2-event.h>
26 
27 u16 ivtv_service2vbi(int type)
28 {
29 	switch (type) {
30 		case V4L2_SLICED_TELETEXT_B:
31 			return IVTV_SLICED_TYPE_TELETEXT_B;
32 		case V4L2_SLICED_CAPTION_525:
33 			return IVTV_SLICED_TYPE_CAPTION_525;
34 		case V4L2_SLICED_WSS_625:
35 			return IVTV_SLICED_TYPE_WSS_625;
36 		case V4L2_SLICED_VPS:
37 			return IVTV_SLICED_TYPE_VPS;
38 		default:
39 			return 0;
40 	}
41 }
42 
43 static int valid_service_line(int field, int line, int is_pal)
44 {
45 	return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
46 	       (!is_pal && line >= 10 && line < 22);
47 }
48 
49 static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
50 {
51 	u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
52 	int i;
53 
54 	set = set & valid_set;
55 	if (set == 0 || !valid_service_line(field, line, is_pal)) {
56 		return 0;
57 	}
58 	if (!is_pal) {
59 		if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
60 			return V4L2_SLICED_CAPTION_525;
61 	}
62 	else {
63 		if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
64 			return V4L2_SLICED_VPS;
65 		if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
66 			return V4L2_SLICED_WSS_625;
67 		if (line == 23)
68 			return 0;
69 	}
70 	for (i = 0; i < 32; i++) {
71 		if (BIT(i) & set)
72 			return BIT(i);
73 	}
74 	return 0;
75 }
76 
77 void ivtv_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
78 {
79 	u16 set = fmt->service_set;
80 	int f, l;
81 
82 	fmt->service_set = 0;
83 	for (f = 0; f < 2; f++) {
84 		for (l = 0; l < 24; l++) {
85 			fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
86 		}
87 	}
88 }
89 
90 static void check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
91 {
92 	int f, l;
93 
94 	for (f = 0; f < 2; f++) {
95 		for (l = 0; l < 24; l++) {
96 			fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
97 		}
98 	}
99 }
100 
101 u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt)
102 {
103 	int f, l;
104 	u16 set = 0;
105 
106 	for (f = 0; f < 2; f++) {
107 		for (l = 0; l < 24; l++) {
108 			set |= fmt->service_lines[f][l];
109 		}
110 	}
111 	return set;
112 }
113 
114 void ivtv_set_osd_alpha(struct ivtv *itv)
115 {
116 	ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
117 		itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
118 	ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
119 }
120 
121 int ivtv_set_speed(struct ivtv *itv, int speed)
122 {
123 	u32 data[CX2341X_MBOX_MAX_DATA];
124 	int single_step = (speed == 1 || speed == -1);
125 	DEFINE_WAIT(wait);
126 
127 	if (speed == 0) speed = 1000;
128 
129 	/* No change? */
130 	if (speed == itv->speed && !single_step)
131 		return 0;
132 
133 	if (single_step && (speed < 0) == (itv->speed < 0)) {
134 		/* Single step video and no need to change direction */
135 		ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
136 		itv->speed = speed;
137 		return 0;
138 	}
139 	if (single_step)
140 		/* Need to change direction */
141 		speed = speed < 0 ? -1000 : 1000;
142 
143 	data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
144 	data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
145 	data[1] = (speed < 0);
146 	data[2] = speed < 0 ? 3 : 7;
147 	data[3] = v4l2_ctrl_g_ctrl(itv->cxhdl.video_b_frames);
148 	data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
149 	data[5] = 0;
150 	data[6] = 0;
151 
152 	if (speed == 1500 || speed == -1500) data[0] |= 1;
153 	else if (speed == 2000 || speed == -2000) data[0] |= 2;
154 	else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
155 	else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
156 
157 	/* If not decoding, just change speed setting */
158 	if (atomic_read(&itv->decoding) > 0) {
159 		int got_sig = 0;
160 
161 		/* Stop all DMA and decoding activity */
162 		ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
163 
164 		/* Wait for any DMA to finish */
165 		mutex_unlock(&itv->serialize_lock);
166 		prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
167 		while (test_bit(IVTV_F_I_DMA, &itv->i_flags)) {
168 			got_sig = signal_pending(current);
169 			if (got_sig)
170 				break;
171 			got_sig = 0;
172 			schedule();
173 		}
174 		finish_wait(&itv->dma_waitq, &wait);
175 		mutex_lock(&itv->serialize_lock);
176 		if (got_sig)
177 			return -EINTR;
178 
179 		/* Change Speed safely */
180 		ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
181 		IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
182 				data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
183 	}
184 	if (single_step) {
185 		speed = (speed < 0) ? -1 : 1;
186 		ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
187 	}
188 	itv->speed = speed;
189 	return 0;
190 }
191 
192 static int ivtv_validate_speed(int cur_speed, int new_speed)
193 {
194 	int fact = new_speed < 0 ? -1 : 1;
195 	int s;
196 
197 	if (cur_speed == 0)
198 		cur_speed = 1000;
199 	if (new_speed < 0)
200 		new_speed = -new_speed;
201 	if (cur_speed < 0)
202 		cur_speed = -cur_speed;
203 
204 	if (cur_speed <= new_speed) {
205 		if (new_speed > 1500)
206 			return fact * 2000;
207 		if (new_speed > 1000)
208 			return fact * 1500;
209 	}
210 	else {
211 		if (new_speed >= 2000)
212 			return fact * 2000;
213 		if (new_speed >= 1500)
214 			return fact * 1500;
215 		if (new_speed >= 1000)
216 			return fact * 1000;
217 	}
218 	if (new_speed == 0)
219 		return 1000;
220 	if (new_speed == 1 || new_speed == 1000)
221 		return fact * new_speed;
222 
223 	s = new_speed;
224 	new_speed = 1000 / new_speed;
225 	if (1000 / cur_speed == new_speed)
226 		new_speed += (cur_speed < s) ? -1 : 1;
227 	if (new_speed > 60) return 1000 / (fact * 60);
228 	return 1000 / (fact * new_speed);
229 }
230 
231 static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
232 		struct v4l2_decoder_cmd *dc, int try)
233 {
234 	struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
235 
236 	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
237 		return -EINVAL;
238 
239 	switch (dc->cmd) {
240 	case V4L2_DEC_CMD_START: {
241 		dc->flags &= V4L2_DEC_CMD_START_MUTE_AUDIO;
242 		dc->start.speed = ivtv_validate_speed(itv->speed, dc->start.speed);
243 		if (dc->start.speed < 0)
244 			dc->start.format = V4L2_DEC_START_FMT_GOP;
245 		else
246 			dc->start.format = V4L2_DEC_START_FMT_NONE;
247 		if (dc->start.speed != 500 && dc->start.speed != 1500)
248 			dc->flags = dc->start.speed == 1000 ? 0 :
249 					V4L2_DEC_CMD_START_MUTE_AUDIO;
250 		if (try) break;
251 
252 		itv->speed_mute_audio = dc->flags & V4L2_DEC_CMD_START_MUTE_AUDIO;
253 		if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
254 			return -EBUSY;
255 		if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
256 			/* forces ivtv_set_speed to be called */
257 			itv->speed = 0;
258 		}
259 		return ivtv_start_decoding(id, dc->start.speed);
260 	}
261 
262 	case V4L2_DEC_CMD_STOP:
263 		dc->flags &= V4L2_DEC_CMD_STOP_IMMEDIATELY | V4L2_DEC_CMD_STOP_TO_BLACK;
264 		if (dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY)
265 			dc->stop.pts = 0;
266 		if (try) break;
267 		if (atomic_read(&itv->decoding) == 0)
268 			return 0;
269 		if (itv->output_mode != OUT_MPG)
270 			return -EBUSY;
271 
272 		itv->output_mode = OUT_NONE;
273 		return ivtv_stop_v4l2_decode_stream(s, dc->flags, dc->stop.pts);
274 
275 	case V4L2_DEC_CMD_PAUSE:
276 		dc->flags &= V4L2_DEC_CMD_PAUSE_TO_BLACK;
277 		if (try) break;
278 		if (!atomic_read(&itv->decoding))
279 			return -EPERM;
280 		if (itv->output_mode != OUT_MPG)
281 			return -EBUSY;
282 		if (atomic_read(&itv->decoding) > 0) {
283 			ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
284 				(dc->flags & V4L2_DEC_CMD_PAUSE_TO_BLACK) ? 1 : 0);
285 			set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
286 		}
287 		break;
288 
289 	case V4L2_DEC_CMD_RESUME:
290 		dc->flags = 0;
291 		if (try) break;
292 		if (!atomic_read(&itv->decoding))
293 			return -EPERM;
294 		if (itv->output_mode != OUT_MPG)
295 			return -EBUSY;
296 		if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
297 			int speed = itv->speed;
298 			itv->speed = 0;
299 			return ivtv_start_decoding(id, speed);
300 		}
301 		break;
302 
303 	default:
304 		return -EINVAL;
305 	}
306 	return 0;
307 }
308 
309 static int ivtv_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
310 {
311 	struct ivtv *itv = fh2id(fh)->itv;
312 	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
313 
314 	vbifmt->reserved[0] = 0;
315 	vbifmt->reserved[1] = 0;
316 	if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
317 		return -EINVAL;
318 	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
319 	memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
320 	if (itv->is_60hz) {
321 		vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
322 		vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
323 	} else {
324 		vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
325 		vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
326 	}
327 	vbifmt->service_set = ivtv_get_service_set(vbifmt);
328 	return 0;
329 }
330 
331 static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
332 {
333 	struct ivtv_open_id *id = fh2id(fh);
334 	struct ivtv *itv = id->itv;
335 	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
336 
337 	pixfmt->width = itv->cxhdl.width;
338 	pixfmt->height = itv->cxhdl.height;
339 	pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
340 	pixfmt->field = V4L2_FIELD_INTERLACED;
341 	if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
342 		pixfmt->pixelformat = V4L2_PIX_FMT_NV12_16L16;
343 		/* YUV size is (Y=(h*720) + UV=(h*(720/2))) */
344 		pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
345 		pixfmt->bytesperline = 720;
346 	} else {
347 		pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
348 		pixfmt->sizeimage = 128 * 1024;
349 		pixfmt->bytesperline = 0;
350 	}
351 	return 0;
352 }
353 
354 static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
355 {
356 	struct ivtv *itv = fh2id(fh)->itv;
357 	struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
358 
359 	vbifmt->sampling_rate = 27000000;
360 	vbifmt->offset = 248;
361 	vbifmt->samples_per_line = itv->vbi.raw_decoder_line_size - 4;
362 	vbifmt->sample_format = V4L2_PIX_FMT_GREY;
363 	vbifmt->start[0] = itv->vbi.start[0];
364 	vbifmt->start[1] = itv->vbi.start[1];
365 	vbifmt->count[0] = vbifmt->count[1] = itv->vbi.count;
366 	vbifmt->flags = 0;
367 	vbifmt->reserved[0] = 0;
368 	vbifmt->reserved[1] = 0;
369 	return 0;
370 }
371 
372 static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
373 {
374 	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
375 	struct ivtv_open_id *id = fh2id(fh);
376 	struct ivtv *itv = id->itv;
377 
378 	vbifmt->reserved[0] = 0;
379 	vbifmt->reserved[1] = 0;
380 	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
381 
382 	if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
383 		vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
384 			V4L2_SLICED_VBI_525;
385 		ivtv_expand_service_set(vbifmt, itv->is_50hz);
386 		vbifmt->service_set = ivtv_get_service_set(vbifmt);
387 		return 0;
388 	}
389 
390 	v4l2_subdev_call(itv->sd_video, vbi, g_sliced_fmt, vbifmt);
391 	vbifmt->service_set = ivtv_get_service_set(vbifmt);
392 	return 0;
393 }
394 
395 static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
396 {
397 	struct ivtv_open_id *id = fh2id(fh);
398 	struct ivtv *itv = id->itv;
399 	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
400 
401 	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
402 		return -EINVAL;
403 	pixfmt->width = itv->main_rect.width;
404 	pixfmt->height = itv->main_rect.height;
405 	pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
406 	pixfmt->field = V4L2_FIELD_INTERLACED;
407 	if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
408 		switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
409 		case IVTV_YUV_MODE_INTERLACED:
410 			pixfmt->field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
411 				V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
412 			break;
413 		case IVTV_YUV_MODE_PROGRESSIVE:
414 			pixfmt->field = V4L2_FIELD_NONE;
415 			break;
416 		default:
417 			pixfmt->field = V4L2_FIELD_ANY;
418 			break;
419 		}
420 		pixfmt->pixelformat = V4L2_PIX_FMT_NV12_16L16;
421 		pixfmt->bytesperline = 720;
422 		pixfmt->width = itv->yuv_info.v4l2_src_w;
423 		pixfmt->height = itv->yuv_info.v4l2_src_h;
424 		/* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
425 		pixfmt->sizeimage =
426 			1080 * ((pixfmt->height + 31) & ~31);
427 	} else {
428 		pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
429 		pixfmt->sizeimage = 128 * 1024;
430 		pixfmt->bytesperline = 0;
431 	}
432 	return 0;
433 }
434 
435 static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
436 {
437 	struct ivtv *itv = fh2id(fh)->itv;
438 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
439 	struct v4l2_window *winfmt = &fmt->fmt.win;
440 
441 	if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
442 		return -EINVAL;
443 	if (!itv->osd_video_pbase)
444 		return -EINVAL;
445 	winfmt->chromakey = itv->osd_chroma_key;
446 	winfmt->global_alpha = itv->osd_global_alpha;
447 	winfmt->field = V4L2_FIELD_INTERLACED;
448 	winfmt->clips = NULL;
449 	winfmt->clipcount = 0;
450 	winfmt->bitmap = NULL;
451 	winfmt->w.top = winfmt->w.left = 0;
452 	winfmt->w.width = itv->osd_rect.width;
453 	winfmt->w.height = itv->osd_rect.height;
454 	return 0;
455 }
456 
457 static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
458 {
459 	return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
460 }
461 
462 static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
463 {
464 	struct ivtv_open_id *id = fh2id(fh);
465 	struct ivtv *itv = id->itv;
466 	int w = fmt->fmt.pix.width;
467 	int h = fmt->fmt.pix.height;
468 	int min_h = 2;
469 
470 	w = min(w, 720);
471 	w = max(w, 2);
472 	if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
473 		/* YUV height must be a multiple of 32 */
474 		h &= ~0x1f;
475 		min_h = 32;
476 	}
477 	h = min(h, itv->is_50hz ? 576 : 480);
478 	h = max(h, min_h);
479 	ivtv_g_fmt_vid_cap(file, fh, fmt);
480 	fmt->fmt.pix.width = w;
481 	fmt->fmt.pix.height = h;
482 	return 0;
483 }
484 
485 static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
486 {
487 	return ivtv_g_fmt_vbi_cap(file, fh, fmt);
488 }
489 
490 static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
491 {
492 	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
493 	struct ivtv_open_id *id = fh2id(fh);
494 	struct ivtv *itv = id->itv;
495 
496 	if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
497 		return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
498 
499 	/* set sliced VBI capture format */
500 	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
501 	vbifmt->reserved[0] = 0;
502 	vbifmt->reserved[1] = 0;
503 
504 	if (vbifmt->service_set)
505 		ivtv_expand_service_set(vbifmt, itv->is_50hz);
506 	check_service_set(vbifmt, itv->is_50hz);
507 	vbifmt->service_set = ivtv_get_service_set(vbifmt);
508 	return 0;
509 }
510 
511 static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
512 {
513 	struct ivtv_open_id *id = fh2id(fh);
514 	s32 w = fmt->fmt.pix.width;
515 	s32 h = fmt->fmt.pix.height;
516 	int field = fmt->fmt.pix.field;
517 	int ret = ivtv_g_fmt_vid_out(file, fh, fmt);
518 
519 	w = min(w, 720);
520 	w = max(w, 2);
521 	/* Why can the height be 576 even when the output is NTSC?
522 
523 	   Internally the buffers of the PVR350 are always set to 720x576. The
524 	   decoded video frame will always be placed in the top left corner of
525 	   this buffer. For any video which is not 720x576, the buffer will
526 	   then be cropped to remove the unused right and lower areas, with
527 	   the remaining image being scaled by the hardware to fit the display
528 	   area. The video can be scaled both up and down, so a 720x480 video
529 	   can be displayed full-screen on PAL and a 720x576 video can be
530 	   displayed without cropping on NTSC.
531 
532 	   Note that the scaling only occurs on the video stream, the osd
533 	   resolution is locked to the broadcast standard and not scaled.
534 
535 	   Thanks to Ian Armstrong for this explanation. */
536 	h = min(h, 576);
537 	h = max(h, 2);
538 	if (id->type == IVTV_DEC_STREAM_TYPE_YUV)
539 		fmt->fmt.pix.field = field;
540 	fmt->fmt.pix.width = w;
541 	fmt->fmt.pix.height = h;
542 	return ret;
543 }
544 
545 static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
546 {
547 	struct ivtv *itv = fh2id(fh)->itv;
548 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
549 	u32 chromakey = fmt->fmt.win.chromakey;
550 	u8 global_alpha = fmt->fmt.win.global_alpha;
551 
552 	if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
553 		return -EINVAL;
554 	if (!itv->osd_video_pbase)
555 		return -EINVAL;
556 	ivtv_g_fmt_vid_out_overlay(file, fh, fmt);
557 	fmt->fmt.win.chromakey = chromakey;
558 	fmt->fmt.win.global_alpha = global_alpha;
559 	return 0;
560 }
561 
562 static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
563 {
564 	return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
565 }
566 
567 static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
568 {
569 	struct ivtv_open_id *id = fh2id(fh);
570 	struct ivtv *itv = id->itv;
571 	struct v4l2_subdev_format format = {
572 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
573 	};
574 	int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
575 	int w = fmt->fmt.pix.width;
576 	int h = fmt->fmt.pix.height;
577 
578 	if (ret)
579 		return ret;
580 
581 	if (itv->cxhdl.width == w && itv->cxhdl.height == h)
582 		return 0;
583 
584 	if (atomic_read(&itv->capturing) > 0)
585 		return -EBUSY;
586 
587 	itv->cxhdl.width = w;
588 	itv->cxhdl.height = h;
589 	if (v4l2_ctrl_g_ctrl(itv->cxhdl.video_encoding) == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
590 		fmt->fmt.pix.width /= 2;
591 	format.format.width = fmt->fmt.pix.width;
592 	format.format.height = h;
593 	format.format.code = MEDIA_BUS_FMT_FIXED;
594 	v4l2_subdev_call(itv->sd_video, pad, set_fmt, NULL, &format);
595 	return ivtv_g_fmt_vid_cap(file, fh, fmt);
596 }
597 
598 static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
599 {
600 	struct ivtv *itv = fh2id(fh)->itv;
601 
602 	if (!ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
603 		return -EBUSY;
604 	itv->vbi.sliced_in->service_set = 0;
605 	itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
606 	v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &fmt->fmt.vbi);
607 	return ivtv_g_fmt_vbi_cap(file, fh, fmt);
608 }
609 
610 static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
611 {
612 	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
613 	struct ivtv_open_id *id = fh2id(fh);
614 	struct ivtv *itv = id->itv;
615 	int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
616 
617 	if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
618 		return ret;
619 
620 	check_service_set(vbifmt, itv->is_50hz);
621 	if (ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
622 		return -EBUSY;
623 	itv->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
624 	v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, vbifmt);
625 	memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
626 	return 0;
627 }
628 
629 static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
630 {
631 	struct ivtv_open_id *id = fh2id(fh);
632 	struct ivtv *itv = id->itv;
633 	struct yuv_playback_info *yi = &itv->yuv_info;
634 	int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
635 
636 	if (ret)
637 		return ret;
638 
639 	if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
640 		return 0;
641 
642 	/* Return now if we already have some frame data */
643 	if (yi->stream_size)
644 		return -EBUSY;
645 
646 	yi->v4l2_src_w = fmt->fmt.pix.width;
647 	yi->v4l2_src_h = fmt->fmt.pix.height;
648 
649 	switch (fmt->fmt.pix.field) {
650 	case V4L2_FIELD_NONE:
651 		yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
652 		break;
653 	case V4L2_FIELD_ANY:
654 		yi->lace_mode = IVTV_YUV_MODE_AUTO;
655 		break;
656 	case V4L2_FIELD_INTERLACED_BT:
657 		yi->lace_mode =
658 			IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
659 		break;
660 	case V4L2_FIELD_INTERLACED_TB:
661 	default:
662 		yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
663 		break;
664 	}
665 	yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
666 
667 	if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
668 		itv->dma_data_req_size =
669 			1080 * ((yi->v4l2_src_h + 31) & ~31);
670 
671 	return 0;
672 }
673 
674 static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
675 {
676 	struct ivtv *itv = fh2id(fh)->itv;
677 	int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
678 
679 	if (ret == 0) {
680 		itv->osd_chroma_key = fmt->fmt.win.chromakey;
681 		itv->osd_global_alpha = fmt->fmt.win.global_alpha;
682 		ivtv_set_osd_alpha(itv);
683 	}
684 	return ret;
685 }
686 
687 #ifdef CONFIG_VIDEO_ADV_DEBUG
688 static int ivtv_itvc(struct ivtv *itv, bool get, u64 reg, u64 *val)
689 {
690 	volatile u8 __iomem *reg_start;
691 
692 	if (reg & 0x3)
693 		return -EINVAL;
694 	if (reg >= IVTV_REG_OFFSET && reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
695 		reg_start = itv->reg_mem - IVTV_REG_OFFSET;
696 	else if (itv->has_cx23415 && reg >= IVTV_DECODER_OFFSET &&
697 			reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
698 		reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
699 	else if (reg < IVTV_ENCODER_SIZE)
700 		reg_start = itv->enc_mem;
701 	else
702 		return -EINVAL;
703 
704 	if (get)
705 		*val = readl(reg + reg_start);
706 	else
707 		writel(*val, reg + reg_start);
708 	return 0;
709 }
710 
711 static int ivtv_g_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
712 {
713 	struct ivtv *itv = fh2id(fh)->itv;
714 
715 	reg->size = 4;
716 	return ivtv_itvc(itv, true, reg->reg, &reg->val);
717 }
718 
719 static int ivtv_s_register(struct file *file, void *fh, const struct v4l2_dbg_register *reg)
720 {
721 	struct ivtv *itv = fh2id(fh)->itv;
722 	u64 val = reg->val;
723 
724 	return ivtv_itvc(itv, false, reg->reg, &val);
725 }
726 #endif
727 
728 static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
729 {
730 	struct ivtv_open_id *id = fh2id(file->private_data);
731 	struct ivtv *itv = id->itv;
732 
733 	strscpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
734 	strscpy(vcap->card, itv->card_name, sizeof(vcap->card));
735 	snprintf(vcap->bus_info, sizeof(vcap->bus_info), "PCI:%s", pci_name(itv->pdev));
736 	vcap->capabilities = itv->v4l2_cap | V4L2_CAP_DEVICE_CAPS;
737 	return 0;
738 }
739 
740 static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
741 {
742 	struct ivtv *itv = fh2id(fh)->itv;
743 
744 	return ivtv_get_audio_input(itv, vin->index, vin);
745 }
746 
747 static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
748 {
749 	struct ivtv *itv = fh2id(fh)->itv;
750 
751 	vin->index = itv->audio_input;
752 	return ivtv_get_audio_input(itv, vin->index, vin);
753 }
754 
755 static int ivtv_s_audio(struct file *file, void *fh, const struct v4l2_audio *vout)
756 {
757 	struct ivtv *itv = fh2id(fh)->itv;
758 
759 	if (vout->index >= itv->nof_audio_inputs)
760 		return -EINVAL;
761 
762 	itv->audio_input = vout->index;
763 	ivtv_audio_set_io(itv);
764 
765 	return 0;
766 }
767 
768 static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
769 {
770 	struct ivtv *itv = fh2id(fh)->itv;
771 
772 	/* set it to defaults from our table */
773 	return ivtv_get_audio_output(itv, vin->index, vin);
774 }
775 
776 static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
777 {
778 	struct ivtv *itv = fh2id(fh)->itv;
779 
780 	vin->index = 0;
781 	return ivtv_get_audio_output(itv, vin->index, vin);
782 }
783 
784 static int ivtv_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
785 {
786 	struct ivtv *itv = fh2id(fh)->itv;
787 
788 	if (itv->card->video_outputs == NULL || vout->index != 0)
789 		return -EINVAL;
790 	return 0;
791 }
792 
793 static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
794 {
795 	struct ivtv *itv = fh2id(fh)->itv;
796 
797 	/* set it to defaults from our table */
798 	return ivtv_get_input(itv, vin->index, vin);
799 }
800 
801 static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
802 {
803 	struct ivtv *itv = fh2id(fh)->itv;
804 
805 	return ivtv_get_output(itv, vout->index, vout);
806 }
807 
808 static int ivtv_g_pixelaspect(struct file *file, void *fh,
809 			      int type, struct v4l2_fract *f)
810 {
811 	struct ivtv_open_id *id = fh2id(fh);
812 	struct ivtv *itv = id->itv;
813 
814 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
815 		f->numerator = itv->is_50hz ? 54 : 11;
816 		f->denominator = itv->is_50hz ? 59 : 10;
817 	} else if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
818 		f->numerator = itv->is_out_50hz ? 54 : 11;
819 		f->denominator = itv->is_out_50hz ? 59 : 10;
820 	} else {
821 		return -EINVAL;
822 	}
823 	return 0;
824 }
825 
826 static int ivtv_s_selection(struct file *file, void *fh,
827 			    struct v4l2_selection *sel)
828 {
829 	struct ivtv_open_id *id = fh2id(fh);
830 	struct ivtv *itv = id->itv;
831 	struct yuv_playback_info *yi = &itv->yuv_info;
832 	struct v4l2_rect r = { 0, 0, 720, 0 };
833 	int streamtype = id->type;
834 
835 	if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
836 	    !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
837 		return -EINVAL;
838 
839 	if (sel->target != V4L2_SEL_TGT_COMPOSE)
840 		return -EINVAL;
841 
842 
843 	if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
844 	    !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
845 		return -EINVAL;
846 
847 	r.height = itv->is_out_50hz ? 576 : 480;
848 	if (streamtype == IVTV_DEC_STREAM_TYPE_YUV && yi->track_osd) {
849 		r.width = yi->osd_full_w;
850 		r.height = yi->osd_full_h;
851 	}
852 	sel->r.width = clamp(sel->r.width, 16U, r.width);
853 	sel->r.height = clamp(sel->r.height, 16U, r.height);
854 	sel->r.left = clamp_t(unsigned, sel->r.left, 0, r.width - sel->r.width);
855 	sel->r.top = clamp_t(unsigned, sel->r.top, 0, r.height - sel->r.height);
856 
857 	if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
858 		yi->main_rect = sel->r;
859 		return 0;
860 	}
861 	if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
862 			sel->r.width, sel->r.height, sel->r.left, sel->r.top)) {
863 		itv->main_rect = sel->r;
864 		return 0;
865 	}
866 	return -EINVAL;
867 }
868 
869 static int ivtv_g_selection(struct file *file, void *fh,
870 			    struct v4l2_selection *sel)
871 {
872 	struct ivtv_open_id *id = fh2id(fh);
873 	struct ivtv *itv = id->itv;
874 	struct yuv_playback_info *yi = &itv->yuv_info;
875 	struct v4l2_rect r = { 0, 0, 720, 0 };
876 	int streamtype = id->type;
877 
878 	if (sel->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
879 		switch (sel->target) {
880 		case V4L2_SEL_TGT_CROP_DEFAULT:
881 		case V4L2_SEL_TGT_CROP_BOUNDS:
882 			sel->r.top = sel->r.left = 0;
883 			sel->r.width = 720;
884 			sel->r.height = itv->is_50hz ? 576 : 480;
885 			return 0;
886 		default:
887 			return -EINVAL;
888 		}
889 	}
890 
891 	if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
892 	    !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
893 		return -EINVAL;
894 
895 	switch (sel->target) {
896 	case V4L2_SEL_TGT_COMPOSE:
897 		if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
898 			sel->r = yi->main_rect;
899 		else
900 			sel->r = itv->main_rect;
901 		return 0;
902 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
903 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
904 		r.height = itv->is_out_50hz ? 576 : 480;
905 		if (streamtype == IVTV_DEC_STREAM_TYPE_YUV && yi->track_osd) {
906 			r.width = yi->osd_full_w;
907 			r.height = yi->osd_full_h;
908 		}
909 		sel->r = r;
910 		return 0;
911 	}
912 	return -EINVAL;
913 }
914 
915 static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
916 {
917 	static const struct v4l2_fmtdesc hm12 = {
918 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
919 		.description = "HM12 (YUV 4:2:0)",
920 		.pixelformat = V4L2_PIX_FMT_NV12_16L16,
921 	};
922 	static const struct v4l2_fmtdesc mpeg = {
923 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
924 		.flags = V4L2_FMT_FLAG_COMPRESSED,
925 		.description = "MPEG",
926 		.pixelformat = V4L2_PIX_FMT_MPEG,
927 	};
928 	struct ivtv *itv = fh2id(fh)->itv;
929 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
930 
931 	if (fmt->index)
932 		return -EINVAL;
933 	if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
934 		*fmt = mpeg;
935 	else if (s->type == IVTV_ENC_STREAM_TYPE_YUV)
936 		*fmt = hm12;
937 	else
938 		return -EINVAL;
939 	return 0;
940 }
941 
942 static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
943 {
944 	static const struct v4l2_fmtdesc hm12 = {
945 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
946 		.description = "HM12 (YUV 4:2:0)",
947 		.pixelformat = V4L2_PIX_FMT_NV12_16L16,
948 	};
949 	static const struct v4l2_fmtdesc mpeg = {
950 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
951 		.flags = V4L2_FMT_FLAG_COMPRESSED,
952 		.description = "MPEG",
953 		.pixelformat = V4L2_PIX_FMT_MPEG,
954 	};
955 	struct ivtv *itv = fh2id(fh)->itv;
956 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
957 
958 	if (fmt->index)
959 		return -EINVAL;
960 	if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
961 		*fmt = mpeg;
962 	else if (s->type == IVTV_DEC_STREAM_TYPE_YUV)
963 		*fmt = hm12;
964 	else
965 		return -EINVAL;
966 	return 0;
967 }
968 
969 static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
970 {
971 	struct ivtv *itv = fh2id(fh)->itv;
972 
973 	*i = itv->active_input;
974 
975 	return 0;
976 }
977 
978 int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
979 {
980 	struct ivtv *itv = fh2id(fh)->itv;
981 	v4l2_std_id std;
982 	int i;
983 
984 	if (inp >= itv->nof_inputs)
985 		return -EINVAL;
986 
987 	if (inp == itv->active_input) {
988 		IVTV_DEBUG_INFO("Input unchanged\n");
989 		return 0;
990 	}
991 
992 	if (atomic_read(&itv->capturing) > 0) {
993 		return -EBUSY;
994 	}
995 
996 	IVTV_DEBUG_INFO("Changing input from %d to %d\n",
997 			itv->active_input, inp);
998 
999 	itv->active_input = inp;
1000 	/* Set the audio input to whatever is appropriate for the
1001 	   input type. */
1002 	itv->audio_input = itv->card->video_inputs[inp].audio_index;
1003 
1004 	if (itv->card->video_inputs[inp].video_type == IVTV_CARD_INPUT_VID_TUNER)
1005 		std = itv->tuner_std;
1006 	else
1007 		std = V4L2_STD_ALL;
1008 	for (i = 0; i <= IVTV_ENC_STREAM_TYPE_VBI; i++)
1009 		itv->streams[i].vdev.tvnorms = std;
1010 
1011 	/* prevent others from messing with the streams until
1012 	   we're finished changing inputs. */
1013 	ivtv_mute(itv);
1014 	ivtv_video_set_io(itv);
1015 	ivtv_audio_set_io(itv);
1016 	ivtv_unmute(itv);
1017 
1018 	return 0;
1019 }
1020 
1021 static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1022 {
1023 	struct ivtv *itv = fh2id(fh)->itv;
1024 
1025 	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1026 		return -EINVAL;
1027 
1028 	*i = itv->active_output;
1029 
1030 	return 0;
1031 }
1032 
1033 static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1034 {
1035 	struct ivtv *itv = fh2id(fh)->itv;
1036 
1037 	if (outp >= itv->card->nof_outputs)
1038 		return -EINVAL;
1039 
1040 	if (outp == itv->active_output) {
1041 		IVTV_DEBUG_INFO("Output unchanged\n");
1042 		return 0;
1043 	}
1044 	IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1045 		   itv->active_output, outp);
1046 
1047 	itv->active_output = outp;
1048 	ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
1049 			SAA7127_INPUT_TYPE_NORMAL,
1050 			itv->card->video_outputs[outp].video_output, 0);
1051 
1052 	return 0;
1053 }
1054 
1055 static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1056 {
1057 	struct ivtv *itv = fh2id(fh)->itv;
1058 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1059 
1060 	if (s->vdev.vfl_dir)
1061 		return -ENOTTY;
1062 	if (vf->tuner != 0)
1063 		return -EINVAL;
1064 
1065 	ivtv_call_all(itv, tuner, g_frequency, vf);
1066 	return 0;
1067 }
1068 
1069 int ivtv_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1070 {
1071 	struct ivtv *itv = fh2id(fh)->itv;
1072 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1073 
1074 	if (s->vdev.vfl_dir)
1075 		return -ENOTTY;
1076 	if (vf->tuner != 0)
1077 		return -EINVAL;
1078 
1079 	ivtv_mute(itv);
1080 	IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1081 	ivtv_call_all(itv, tuner, s_frequency, vf);
1082 	ivtv_unmute(itv);
1083 	return 0;
1084 }
1085 
1086 static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1087 {
1088 	struct ivtv *itv = fh2id(fh)->itv;
1089 
1090 	*std = itv->std;
1091 	return 0;
1092 }
1093 
1094 void ivtv_s_std_enc(struct ivtv *itv, v4l2_std_id std)
1095 {
1096 	itv->std = std;
1097 	itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1098 	itv->is_50hz = !itv->is_60hz;
1099 	cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1100 	itv->cxhdl.width = 720;
1101 	itv->cxhdl.height = itv->is_50hz ? 576 : 480;
1102 	itv->vbi.count = itv->is_50hz ? 18 : 12;
1103 	itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1104 	itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1105 
1106 	if (itv->hw_flags & IVTV_HW_CX25840)
1107 		itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1108 
1109 	/* Tuner */
1110 	ivtv_call_all(itv, video, s_std, itv->std);
1111 }
1112 
1113 void ivtv_s_std_dec(struct ivtv *itv, v4l2_std_id std)
1114 {
1115 	struct yuv_playback_info *yi = &itv->yuv_info;
1116 	DEFINE_WAIT(wait);
1117 	int f;
1118 
1119 	/* set display standard */
1120 	itv->std_out = std;
1121 	itv->is_out_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1122 	itv->is_out_50hz = !itv->is_out_60hz;
1123 	ivtv_call_all(itv, video, s_std_output, itv->std_out);
1124 
1125 	/*
1126 	 * The next firmware call is time sensitive. Time it to
1127 	 * avoid risk of a hard lock, by trying to ensure the call
1128 	 * happens within the first 100 lines of the top field.
1129 	 * Make 4 attempts to sync to the decoder before giving up.
1130 	 */
1131 	mutex_unlock(&itv->serialize_lock);
1132 	for (f = 0; f < 4; f++) {
1133 		prepare_to_wait(&itv->vsync_waitq, &wait,
1134 				TASK_UNINTERRUPTIBLE);
1135 		if ((read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16) < 100)
1136 			break;
1137 		schedule_timeout(msecs_to_jiffies(25));
1138 	}
1139 	finish_wait(&itv->vsync_waitq, &wait);
1140 	mutex_lock(&itv->serialize_lock);
1141 
1142 	if (f == 4)
1143 		IVTV_WARN("Mode change failed to sync to decoder\n");
1144 
1145 	ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1146 	itv->main_rect.left = 0;
1147 	itv->main_rect.top = 0;
1148 	itv->main_rect.width = 720;
1149 	itv->main_rect.height = itv->is_out_50hz ? 576 : 480;
1150 	ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1151 		720, itv->main_rect.height, 0, 0);
1152 	yi->main_rect = itv->main_rect;
1153 	if (!itv->osd_info) {
1154 		yi->osd_full_w = 720;
1155 		yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1156 	}
1157 }
1158 
1159 static int ivtv_s_std(struct file *file, void *fh, v4l2_std_id std)
1160 {
1161 	struct ivtv *itv = fh2id(fh)->itv;
1162 
1163 	if ((std & V4L2_STD_ALL) == 0)
1164 		return -EINVAL;
1165 
1166 	if (std == itv->std)
1167 		return 0;
1168 
1169 	if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1170 	    atomic_read(&itv->capturing) > 0 ||
1171 	    atomic_read(&itv->decoding) > 0) {
1172 		/* Switching standard would mess with already running
1173 		   streams, prevent that by returning EBUSY. */
1174 		return -EBUSY;
1175 	}
1176 
1177 	IVTV_DEBUG_INFO("Switching standard to %llx.\n",
1178 		(unsigned long long)itv->std);
1179 
1180 	ivtv_s_std_enc(itv, std);
1181 	if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
1182 		ivtv_s_std_dec(itv, std);
1183 
1184 	return 0;
1185 }
1186 
1187 static int ivtv_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1188 {
1189 	struct ivtv_open_id *id = fh2id(fh);
1190 	struct ivtv *itv = id->itv;
1191 
1192 	if (vt->index != 0)
1193 		return -EINVAL;
1194 
1195 	ivtv_call_all(itv, tuner, s_tuner, vt);
1196 
1197 	return 0;
1198 }
1199 
1200 static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1201 {
1202 	struct ivtv *itv = fh2id(fh)->itv;
1203 
1204 	if (vt->index != 0)
1205 		return -EINVAL;
1206 
1207 	ivtv_call_all(itv, tuner, g_tuner, vt);
1208 
1209 	if (vt->type == V4L2_TUNER_RADIO)
1210 		strscpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1211 	else
1212 		strscpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1213 	return 0;
1214 }
1215 
1216 static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1217 {
1218 	struct ivtv *itv = fh2id(fh)->itv;
1219 	int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1220 	int f, l;
1221 
1222 	if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1223 		for (f = 0; f < 2; f++) {
1224 			for (l = 0; l < 24; l++) {
1225 				if (valid_service_line(f, l, itv->is_50hz))
1226 					cap->service_lines[f][l] = set;
1227 			}
1228 		}
1229 	} else if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1230 		if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1231 			return -EINVAL;
1232 		if (itv->is_60hz) {
1233 			cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1234 			cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1235 		} else {
1236 			cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1237 			cap->service_lines[0][16] = V4L2_SLICED_VPS;
1238 		}
1239 	} else {
1240 		return -EINVAL;
1241 	}
1242 
1243 	set = 0;
1244 	for (f = 0; f < 2; f++)
1245 		for (l = 0; l < 24; l++)
1246 			set |= cap->service_lines[f][l];
1247 	cap->service_set = set;
1248 	return 0;
1249 }
1250 
1251 static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1252 {
1253 	struct ivtv *itv = fh2id(fh)->itv;
1254 	struct v4l2_enc_idx_entry *e = idx->entry;
1255 	int entries;
1256 	int i;
1257 
1258 	entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1259 				IVTV_MAX_PGM_INDEX;
1260 	if (entries > V4L2_ENC_IDX_ENTRIES)
1261 		entries = V4L2_ENC_IDX_ENTRIES;
1262 	idx->entries = 0;
1263 	idx->entries_cap = IVTV_MAX_PGM_INDEX;
1264 	if (!atomic_read(&itv->capturing))
1265 		return 0;
1266 	for (i = 0; i < entries; i++) {
1267 		*e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1268 		if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1269 			idx->entries++;
1270 			e++;
1271 		}
1272 	}
1273 	itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1274 	return 0;
1275 }
1276 
1277 static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1278 {
1279 	struct ivtv_open_id *id = fh2id(fh);
1280 	struct ivtv *itv = id->itv;
1281 
1282 
1283 	switch (enc->cmd) {
1284 	case V4L2_ENC_CMD_START:
1285 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1286 		enc->flags = 0;
1287 		return ivtv_start_capture(id);
1288 
1289 	case V4L2_ENC_CMD_STOP:
1290 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1291 		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1292 		ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1293 		return 0;
1294 
1295 	case V4L2_ENC_CMD_PAUSE:
1296 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1297 		enc->flags = 0;
1298 
1299 		if (!atomic_read(&itv->capturing))
1300 			return -EPERM;
1301 		if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1302 			return 0;
1303 
1304 		ivtv_mute(itv);
1305 		ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1306 		break;
1307 
1308 	case V4L2_ENC_CMD_RESUME:
1309 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1310 		enc->flags = 0;
1311 
1312 		if (!atomic_read(&itv->capturing))
1313 			return -EPERM;
1314 
1315 		if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1316 			return 0;
1317 
1318 		ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1319 		ivtv_unmute(itv);
1320 		break;
1321 	default:
1322 		IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1323 		return -EINVAL;
1324 	}
1325 
1326 	return 0;
1327 }
1328 
1329 static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1330 {
1331 	struct ivtv *itv = fh2id(fh)->itv;
1332 
1333 	switch (enc->cmd) {
1334 	case V4L2_ENC_CMD_START:
1335 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1336 		enc->flags = 0;
1337 		return 0;
1338 
1339 	case V4L2_ENC_CMD_STOP:
1340 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1341 		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1342 		return 0;
1343 
1344 	case V4L2_ENC_CMD_PAUSE:
1345 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1346 		enc->flags = 0;
1347 		return 0;
1348 
1349 	case V4L2_ENC_CMD_RESUME:
1350 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1351 		enc->flags = 0;
1352 		return 0;
1353 	default:
1354 		IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1355 		return -EINVAL;
1356 	}
1357 }
1358 
1359 static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1360 {
1361 	struct ivtv *itv = fh2id(fh)->itv;
1362 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1363 	u32 data[CX2341X_MBOX_MAX_DATA];
1364 	struct yuv_playback_info *yi = &itv->yuv_info;
1365 
1366 	int pixfmt;
1367 	static u32 pixel_format[16] = {
1368 		V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1369 		V4L2_PIX_FMT_RGB565,
1370 		V4L2_PIX_FMT_RGB555,
1371 		V4L2_PIX_FMT_RGB444,
1372 		V4L2_PIX_FMT_RGB32,
1373 		0,
1374 		0,
1375 		0,
1376 		V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1377 		V4L2_PIX_FMT_YUV565,
1378 		V4L2_PIX_FMT_YUV555,
1379 		V4L2_PIX_FMT_YUV444,
1380 		V4L2_PIX_FMT_YUV32,
1381 		0,
1382 		0,
1383 		0,
1384 	};
1385 
1386 	if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1387 		return -ENOTTY;
1388 	if (!itv->osd_video_pbase)
1389 		return -ENOTTY;
1390 
1391 	fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1392 		V4L2_FBUF_CAP_GLOBAL_ALPHA;
1393 
1394 	ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1395 	data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1396 	pixfmt = (data[0] >> 3) & 0xf;
1397 
1398 	fb->fmt.pixelformat = pixel_format[pixfmt];
1399 	fb->fmt.width = itv->osd_rect.width;
1400 	fb->fmt.height = itv->osd_rect.height;
1401 	fb->fmt.field = V4L2_FIELD_INTERLACED;
1402 	fb->fmt.bytesperline = fb->fmt.width;
1403 	fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
1404 	fb->fmt.field = V4L2_FIELD_INTERLACED;
1405 	if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
1406 		fb->fmt.bytesperline *= 2;
1407 	if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
1408 	    fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
1409 		fb->fmt.bytesperline *= 2;
1410 	fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
1411 	fb->base = (void *)itv->osd_video_pbase;
1412 	fb->flags = 0;
1413 
1414 	if (itv->osd_chroma_key_state)
1415 		fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1416 
1417 	if (itv->osd_global_alpha_state)
1418 		fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1419 
1420 	if (yi->track_osd)
1421 		fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1422 
1423 	pixfmt &= 7;
1424 
1425 	/* no local alpha for RGB565 or unknown formats */
1426 	if (pixfmt == 1 || pixfmt > 4)
1427 		return 0;
1428 
1429 	/* 16-bit formats have inverted local alpha */
1430 	if (pixfmt == 2 || pixfmt == 3)
1431 		fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1432 	else
1433 		fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1434 
1435 	if (itv->osd_local_alpha_state) {
1436 		/* 16-bit formats have inverted local alpha */
1437 		if (pixfmt == 2 || pixfmt == 3)
1438 			fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1439 		else
1440 			fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1441 	}
1442 
1443 	return 0;
1444 }
1445 
1446 static int ivtv_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *fb)
1447 {
1448 	struct ivtv_open_id *id = fh2id(fh);
1449 	struct ivtv *itv = id->itv;
1450 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1451 	struct yuv_playback_info *yi = &itv->yuv_info;
1452 
1453 	if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1454 		return -ENOTTY;
1455 	if (!itv->osd_video_pbase)
1456 		return -ENOTTY;
1457 
1458 	itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1459 	itv->osd_local_alpha_state =
1460 		(fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1461 	itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1462 	ivtv_set_osd_alpha(itv);
1463 	yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1464 	return 0;
1465 }
1466 
1467 static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1468 {
1469 	struct ivtv_open_id *id = fh2id(fh);
1470 	struct ivtv *itv = id->itv;
1471 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1472 
1473 	if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1474 		return -ENOTTY;
1475 	if (!itv->osd_video_pbase)
1476 		return -ENOTTY;
1477 
1478 	ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1479 
1480 	return 0;
1481 }
1482 
1483 static int ivtv_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
1484 {
1485 	switch (sub->type) {
1486 	case V4L2_EVENT_VSYNC:
1487 	case V4L2_EVENT_EOS:
1488 		return v4l2_event_subscribe(fh, sub, 0, NULL);
1489 	default:
1490 		return v4l2_ctrl_subscribe_event(fh, sub);
1491 	}
1492 }
1493 
1494 static int ivtv_log_status(struct file *file, void *fh)
1495 {
1496 	struct ivtv *itv = fh2id(fh)->itv;
1497 	u32 data[CX2341X_MBOX_MAX_DATA];
1498 
1499 	int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1500 	struct v4l2_input vidin;
1501 	struct v4l2_audio audin;
1502 	int i;
1503 
1504 	IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1505 	if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1506 		struct tveeprom tv;
1507 
1508 		ivtv_read_eeprom(itv, &tv);
1509 	}
1510 	ivtv_call_all(itv, core, log_status);
1511 	ivtv_get_input(itv, itv->active_input, &vidin);
1512 	ivtv_get_audio_input(itv, itv->audio_input, &audin);
1513 	IVTV_INFO("Video Input:  %s\n", vidin.name);
1514 	IVTV_INFO("Audio Input:  %s%s\n", audin.name,
1515 		itv->dualwatch_stereo_mode == V4L2_MPEG_AUDIO_MODE_DUAL ?
1516 			" (Bilingual)" : "");
1517 	if (has_output) {
1518 		struct v4l2_output vidout;
1519 		struct v4l2_audioout audout;
1520 		int mode = itv->output_mode;
1521 		static const char * const output_modes[5] = {
1522 			"None",
1523 			"MPEG Streaming",
1524 			"YUV Streaming",
1525 			"YUV Frames",
1526 			"Passthrough",
1527 		};
1528 		static const char * const alpha_mode[4] = {
1529 			"None",
1530 			"Global",
1531 			"Local",
1532 			"Global and Local"
1533 		};
1534 		static const char * const pixel_format[16] = {
1535 			"ARGB Indexed",
1536 			"RGB 5:6:5",
1537 			"ARGB 1:5:5:5",
1538 			"ARGB 1:4:4:4",
1539 			"ARGB 8:8:8:8",
1540 			"5",
1541 			"6",
1542 			"7",
1543 			"AYUV Indexed",
1544 			"YUV 5:6:5",
1545 			"AYUV 1:5:5:5",
1546 			"AYUV 1:4:4:4",
1547 			"AYUV 8:8:8:8",
1548 			"13",
1549 			"14",
1550 			"15",
1551 		};
1552 
1553 		ivtv_get_output(itv, itv->active_output, &vidout);
1554 		ivtv_get_audio_output(itv, 0, &audout);
1555 		IVTV_INFO("Video Output: %s\n", vidout.name);
1556 		if (mode < 0 || mode > OUT_PASSTHROUGH)
1557 			mode = OUT_NONE;
1558 		IVTV_INFO("Output Mode:  %s\n", output_modes[mode]);
1559 		ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1560 		data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1561 		IVTV_INFO("Overlay:      %s, Alpha: %s, Pixel Format: %s\n",
1562 			data[0] & 1 ? "On" : "Off",
1563 			alpha_mode[(data[0] >> 1) & 0x3],
1564 			pixel_format[(data[0] >> 3) & 0xf]);
1565 	}
1566 	IVTV_INFO("Tuner:  %s\n",
1567 		test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1568 	v4l2_ctrl_handler_log_status(&itv->cxhdl.hdl, itv->v4l2_dev.name);
1569 	IVTV_INFO("Status flags:    0x%08lx\n", itv->i_flags);
1570 	for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1571 		struct ivtv_stream *s = &itv->streams[i];
1572 
1573 		if (s->vdev.v4l2_dev == NULL || s->buffers == 0)
1574 			continue;
1575 		IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1576 				(s->buffers - s->q_free.buffers) * 100 / s->buffers,
1577 				(s->buffers * s->buf_size) / 1024, s->buffers);
1578 	}
1579 
1580 	IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n",
1581 			(long long)itv->mpg_data_received,
1582 			(long long)itv->vbi_data_inserted);
1583 	return 0;
1584 }
1585 
1586 static int ivtv_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1587 {
1588 	struct ivtv_open_id *id = fh2id(file->private_data);
1589 	struct ivtv *itv = id->itv;
1590 
1591 	IVTV_DEBUG_IOCTL("VIDIOC_DECODER_CMD %d\n", dec->cmd);
1592 	return ivtv_video_command(itv, id, dec, false);
1593 }
1594 
1595 static int ivtv_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1596 {
1597 	struct ivtv_open_id *id = fh2id(file->private_data);
1598 	struct ivtv *itv = id->itv;
1599 
1600 	IVTV_DEBUG_IOCTL("VIDIOC_TRY_DECODER_CMD %d\n", dec->cmd);
1601 	return ivtv_video_command(itv, id, dec, true);
1602 }
1603 
1604 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1605 {
1606 	struct ivtv_open_id *id = fh2id(filp->private_data);
1607 	struct ivtv *itv = id->itv;
1608 	struct ivtv_stream *s = &itv->streams[id->type];
1609 
1610 	switch (cmd) {
1611 	case IVTV_IOC_DMA_FRAME: {
1612 		struct ivtv_dma_frame *args = arg;
1613 
1614 		IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1615 		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1616 			return -EINVAL;
1617 		if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1618 			return -EINVAL;
1619 		if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1620 			return 0;
1621 		if (ivtv_start_decoding(id, id->type)) {
1622 			return -EBUSY;
1623 		}
1624 		if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1625 			ivtv_release_stream(s);
1626 			return -EBUSY;
1627 		}
1628 		/* Mark that this file handle started the UDMA_YUV mode */
1629 		id->yuv_frames = 1;
1630 		if (args->y_source == NULL)
1631 			return 0;
1632 		return ivtv_yuv_prep_frame(itv, args);
1633 	}
1634 
1635 	case IVTV_IOC_PASSTHROUGH_MODE:
1636 		IVTV_DEBUG_IOCTL("IVTV_IOC_PASSTHROUGH_MODE\n");
1637 		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1638 			return -EINVAL;
1639 		return ivtv_passthrough_mode(itv, *(int *)arg != 0);
1640 	default:
1641 		return -EINVAL;
1642 	}
1643 	return 0;
1644 }
1645 
1646 static long ivtv_default(struct file *file, void *fh, bool valid_prio,
1647 			 unsigned int cmd, void *arg)
1648 {
1649 	struct ivtv *itv = fh2id(fh)->itv;
1650 
1651 	if (!valid_prio) {
1652 		switch (cmd) {
1653 		case IVTV_IOC_PASSTHROUGH_MODE:
1654 			return -EBUSY;
1655 		}
1656 	}
1657 
1658 	switch (cmd) {
1659 	case VIDIOC_INT_RESET: {
1660 		u32 val = *(u32 *)arg;
1661 
1662 		if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1663 			ivtv_reset_ir_gpio(itv);
1664 		if (val & 0x02)
1665 			v4l2_subdev_call(itv->sd_video, core, reset, 0);
1666 		break;
1667 	}
1668 
1669 	case IVTV_IOC_DMA_FRAME:
1670 	case IVTV_IOC_PASSTHROUGH_MODE:
1671 		return ivtv_decoder_ioctls(file, cmd, (void *)arg);
1672 
1673 	default:
1674 		return -ENOTTY;
1675 	}
1676 	return 0;
1677 }
1678 
1679 static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
1680 	.vidioc_querycap		    = ivtv_querycap,
1681 	.vidioc_s_audio			    = ivtv_s_audio,
1682 	.vidioc_g_audio			    = ivtv_g_audio,
1683 	.vidioc_enumaudio		    = ivtv_enumaudio,
1684 	.vidioc_s_audout		    = ivtv_s_audout,
1685 	.vidioc_g_audout		    = ivtv_g_audout,
1686 	.vidioc_enum_input		    = ivtv_enum_input,
1687 	.vidioc_enum_output		    = ivtv_enum_output,
1688 	.vidioc_enumaudout		    = ivtv_enumaudout,
1689 	.vidioc_g_pixelaspect		    = ivtv_g_pixelaspect,
1690 	.vidioc_s_selection		    = ivtv_s_selection,
1691 	.vidioc_g_selection		    = ivtv_g_selection,
1692 	.vidioc_g_input			    = ivtv_g_input,
1693 	.vidioc_s_input			    = ivtv_s_input,
1694 	.vidioc_g_output		    = ivtv_g_output,
1695 	.vidioc_s_output		    = ivtv_s_output,
1696 	.vidioc_g_frequency		    = ivtv_g_frequency,
1697 	.vidioc_s_frequency		    = ivtv_s_frequency,
1698 	.vidioc_s_tuner			    = ivtv_s_tuner,
1699 	.vidioc_g_tuner			    = ivtv_g_tuner,
1700 	.vidioc_g_enc_index		    = ivtv_g_enc_index,
1701 	.vidioc_g_fbuf			    = ivtv_g_fbuf,
1702 	.vidioc_s_fbuf			    = ivtv_s_fbuf,
1703 	.vidioc_g_std			    = ivtv_g_std,
1704 	.vidioc_s_std			    = ivtv_s_std,
1705 	.vidioc_overlay			    = ivtv_overlay,
1706 	.vidioc_log_status		    = ivtv_log_status,
1707 	.vidioc_enum_fmt_vid_cap	    = ivtv_enum_fmt_vid_cap,
1708 	.vidioc_encoder_cmd		    = ivtv_encoder_cmd,
1709 	.vidioc_try_encoder_cmd		    = ivtv_try_encoder_cmd,
1710 	.vidioc_decoder_cmd		    = ivtv_decoder_cmd,
1711 	.vidioc_try_decoder_cmd		    = ivtv_try_decoder_cmd,
1712 	.vidioc_enum_fmt_vid_out	    = ivtv_enum_fmt_vid_out,
1713 	.vidioc_g_fmt_vid_cap		    = ivtv_g_fmt_vid_cap,
1714 	.vidioc_g_fmt_vbi_cap		    = ivtv_g_fmt_vbi_cap,
1715 	.vidioc_g_fmt_sliced_vbi_cap        = ivtv_g_fmt_sliced_vbi_cap,
1716 	.vidioc_g_fmt_vid_out               = ivtv_g_fmt_vid_out,
1717 	.vidioc_g_fmt_vid_out_overlay       = ivtv_g_fmt_vid_out_overlay,
1718 	.vidioc_g_fmt_sliced_vbi_out        = ivtv_g_fmt_sliced_vbi_out,
1719 	.vidioc_s_fmt_vid_cap		    = ivtv_s_fmt_vid_cap,
1720 	.vidioc_s_fmt_vbi_cap		    = ivtv_s_fmt_vbi_cap,
1721 	.vidioc_s_fmt_sliced_vbi_cap        = ivtv_s_fmt_sliced_vbi_cap,
1722 	.vidioc_s_fmt_vid_out               = ivtv_s_fmt_vid_out,
1723 	.vidioc_s_fmt_vid_out_overlay       = ivtv_s_fmt_vid_out_overlay,
1724 	.vidioc_s_fmt_sliced_vbi_out        = ivtv_s_fmt_sliced_vbi_out,
1725 	.vidioc_try_fmt_vid_cap		    = ivtv_try_fmt_vid_cap,
1726 	.vidioc_try_fmt_vbi_cap		    = ivtv_try_fmt_vbi_cap,
1727 	.vidioc_try_fmt_sliced_vbi_cap      = ivtv_try_fmt_sliced_vbi_cap,
1728 	.vidioc_try_fmt_vid_out		    = ivtv_try_fmt_vid_out,
1729 	.vidioc_try_fmt_vid_out_overlay     = ivtv_try_fmt_vid_out_overlay,
1730 	.vidioc_try_fmt_sliced_vbi_out	    = ivtv_try_fmt_sliced_vbi_out,
1731 	.vidioc_g_sliced_vbi_cap	    = ivtv_g_sliced_vbi_cap,
1732 #ifdef CONFIG_VIDEO_ADV_DEBUG
1733 	.vidioc_g_register		    = ivtv_g_register,
1734 	.vidioc_s_register		    = ivtv_s_register,
1735 #endif
1736 	.vidioc_default			    = ivtv_default,
1737 	.vidioc_subscribe_event		    = ivtv_subscribe_event,
1738 	.vidioc_unsubscribe_event	    = v4l2_event_unsubscribe,
1739 };
1740 
1741 void ivtv_set_funcs(struct video_device *vdev)
1742 {
1743 	vdev->ioctl_ops = &ivtv_ioctl_ops;
1744 }
1745