xref: /linux/drivers/media/pci/ivtv/ivtv-ioctl.c (revision 5d579ece43f4ed2d88bd5e14d6de682dbc532954)
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@kernel.org>
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 = file2id(file)->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 = file2id(file);
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 = file2id(file)->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 = file2id(file);
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 = file2id(file);
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 = file2id(file)->itv;
438 	struct ivtv_stream *s = &itv->streams[file2id(file)->type];
439 	struct v4l2_window *winfmt = &fmt->fmt.win;
440 
441 	if (!(s->vdev.device_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 = file2id(file);
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 = clamp(w, 2, 720);
471 	if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
472 		/* YUV height must be a multiple of 32 */
473 		h &= ~0x1f;
474 		min_h = 32;
475 	}
476 	h = clamp(h, min_h, itv->is_50hz ? 576 : 480);
477 	ivtv_g_fmt_vid_cap(file, fh, fmt);
478 	fmt->fmt.pix.width = w;
479 	fmt->fmt.pix.height = h;
480 	return 0;
481 }
482 
483 static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
484 {
485 	return ivtv_g_fmt_vbi_cap(file, fh, fmt);
486 }
487 
488 static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
489 {
490 	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
491 	struct ivtv_open_id *id = file2id(file);
492 	struct ivtv *itv = id->itv;
493 
494 	if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
495 		return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
496 
497 	/* set sliced VBI capture format */
498 	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
499 	vbifmt->reserved[0] = 0;
500 	vbifmt->reserved[1] = 0;
501 
502 	if (vbifmt->service_set)
503 		ivtv_expand_service_set(vbifmt, itv->is_50hz);
504 	check_service_set(vbifmt, itv->is_50hz);
505 	vbifmt->service_set = ivtv_get_service_set(vbifmt);
506 	return 0;
507 }
508 
509 static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
510 {
511 	struct ivtv_open_id *id = file2id(file);
512 	s32 w = fmt->fmt.pix.width;
513 	s32 h = fmt->fmt.pix.height;
514 	int field = fmt->fmt.pix.field;
515 	int ret = ivtv_g_fmt_vid_out(file, fh, fmt);
516 
517 	w = clamp(w, 2, 720);
518 	/* Why can the height be 576 even when the output is NTSC?
519 
520 	   Internally the buffers of the PVR350 are always set to 720x576. The
521 	   decoded video frame will always be placed in the top left corner of
522 	   this buffer. For any video which is not 720x576, the buffer will
523 	   then be cropped to remove the unused right and lower areas, with
524 	   the remaining image being scaled by the hardware to fit the display
525 	   area. The video can be scaled both up and down, so a 720x480 video
526 	   can be displayed full-screen on PAL and a 720x576 video can be
527 	   displayed without cropping on NTSC.
528 
529 	   Note that the scaling only occurs on the video stream, the osd
530 	   resolution is locked to the broadcast standard and not scaled.
531 
532 	   Thanks to Ian Armstrong for this explanation. */
533 	h = clamp(h, 2, 576);
534 	if (id->type == IVTV_DEC_STREAM_TYPE_YUV)
535 		fmt->fmt.pix.field = field;
536 	fmt->fmt.pix.width = w;
537 	fmt->fmt.pix.height = h;
538 	return ret;
539 }
540 
541 static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
542 {
543 	struct ivtv *itv = file2id(file)->itv;
544 	struct ivtv_stream *s = &itv->streams[file2id(file)->type];
545 	u32 chromakey = fmt->fmt.win.chromakey;
546 	u8 global_alpha = fmt->fmt.win.global_alpha;
547 
548 	if (!(s->vdev.device_caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
549 		return -EINVAL;
550 	if (!itv->osd_video_pbase)
551 		return -EINVAL;
552 	ivtv_g_fmt_vid_out_overlay(file, fh, fmt);
553 	fmt->fmt.win.chromakey = chromakey;
554 	fmt->fmt.win.global_alpha = global_alpha;
555 	return 0;
556 }
557 
558 static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
559 {
560 	return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
561 }
562 
563 static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
564 {
565 	struct ivtv_open_id *id = file2id(file);
566 	struct ivtv *itv = id->itv;
567 	struct v4l2_subdev_format format = {
568 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
569 	};
570 	int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
571 	int w = fmt->fmt.pix.width;
572 	int h = fmt->fmt.pix.height;
573 
574 	if (ret)
575 		return ret;
576 
577 	if (itv->cxhdl.width == w && itv->cxhdl.height == h)
578 		return 0;
579 
580 	if (atomic_read(&itv->capturing) > 0)
581 		return -EBUSY;
582 
583 	itv->cxhdl.width = w;
584 	itv->cxhdl.height = h;
585 	if (v4l2_ctrl_g_ctrl(itv->cxhdl.video_encoding) == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
586 		fmt->fmt.pix.width /= 2;
587 	format.format.width = fmt->fmt.pix.width;
588 	format.format.height = h;
589 	format.format.code = MEDIA_BUS_FMT_FIXED;
590 	v4l2_subdev_call(itv->sd_video, pad, set_fmt, NULL, &format);
591 	return ivtv_g_fmt_vid_cap(file, fh, fmt);
592 }
593 
594 static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
595 {
596 	struct ivtv *itv = file2id(file)->itv;
597 
598 	if (!ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
599 		return -EBUSY;
600 	itv->vbi.sliced_in->service_set = 0;
601 	itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
602 	v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &fmt->fmt.vbi);
603 	return ivtv_g_fmt_vbi_cap(file, fh, fmt);
604 }
605 
606 static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
607 {
608 	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
609 	struct ivtv_open_id *id = file2id(file);
610 	struct ivtv *itv = id->itv;
611 	int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
612 
613 	if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
614 		return ret;
615 
616 	check_service_set(vbifmt, itv->is_50hz);
617 	if (ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
618 		return -EBUSY;
619 	itv->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
620 	v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, vbifmt);
621 	memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
622 	return 0;
623 }
624 
625 static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
626 {
627 	struct ivtv_open_id *id = file2id(file);
628 	struct ivtv *itv = id->itv;
629 	struct yuv_playback_info *yi = &itv->yuv_info;
630 	int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
631 
632 	if (ret)
633 		return ret;
634 
635 	if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
636 		return 0;
637 
638 	/* Return now if we already have some frame data */
639 	if (yi->stream_size)
640 		return -EBUSY;
641 
642 	yi->v4l2_src_w = fmt->fmt.pix.width;
643 	yi->v4l2_src_h = fmt->fmt.pix.height;
644 
645 	switch (fmt->fmt.pix.field) {
646 	case V4L2_FIELD_NONE:
647 		yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
648 		break;
649 	case V4L2_FIELD_ANY:
650 		yi->lace_mode = IVTV_YUV_MODE_AUTO;
651 		break;
652 	case V4L2_FIELD_INTERLACED_BT:
653 		yi->lace_mode =
654 			IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
655 		break;
656 	case V4L2_FIELD_INTERLACED_TB:
657 	default:
658 		yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
659 		break;
660 	}
661 	yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
662 
663 	if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
664 		itv->dma_data_req_size =
665 			1080 * ((yi->v4l2_src_h + 31) & ~31);
666 
667 	return 0;
668 }
669 
670 static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
671 {
672 	struct ivtv *itv = file2id(file)->itv;
673 	int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
674 
675 	if (ret == 0) {
676 		itv->osd_chroma_key = fmt->fmt.win.chromakey;
677 		itv->osd_global_alpha = fmt->fmt.win.global_alpha;
678 		ivtv_set_osd_alpha(itv);
679 	}
680 	return ret;
681 }
682 
683 #ifdef CONFIG_VIDEO_ADV_DEBUG
684 static int ivtv_itvc(struct ivtv *itv, bool get, u64 reg, u64 *val)
685 {
686 	volatile u8 __iomem *reg_start;
687 
688 	if (reg & 0x3)
689 		return -EINVAL;
690 	if (reg >= IVTV_REG_OFFSET && reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
691 		reg_start = itv->reg_mem - IVTV_REG_OFFSET;
692 	else if (itv->has_cx23415 && reg >= IVTV_DECODER_OFFSET &&
693 			reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
694 		reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
695 	else if (reg < IVTV_ENCODER_SIZE)
696 		reg_start = itv->enc_mem;
697 	else
698 		return -EINVAL;
699 
700 	if (get)
701 		*val = readl(reg + reg_start);
702 	else
703 		writel(*val, reg + reg_start);
704 	return 0;
705 }
706 
707 static int ivtv_g_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
708 {
709 	struct ivtv *itv = file2id(file)->itv;
710 
711 	reg->size = 4;
712 	return ivtv_itvc(itv, true, reg->reg, &reg->val);
713 }
714 
715 static int ivtv_s_register(struct file *file, void *fh, const struct v4l2_dbg_register *reg)
716 {
717 	struct ivtv *itv = file2id(file)->itv;
718 	u64 val = reg->val;
719 
720 	return ivtv_itvc(itv, false, reg->reg, &val);
721 }
722 #endif
723 
724 static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
725 {
726 	struct ivtv_open_id *id = file2id(file);
727 	struct ivtv *itv = id->itv;
728 
729 	strscpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
730 	strscpy(vcap->card, itv->card_name, sizeof(vcap->card));
731 	vcap->capabilities = itv->v4l2_cap | V4L2_CAP_DEVICE_CAPS;
732 	return 0;
733 }
734 
735 static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
736 {
737 	struct ivtv *itv = file2id(file)->itv;
738 
739 	return ivtv_get_audio_input(itv, vin->index, vin);
740 }
741 
742 static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
743 {
744 	struct ivtv *itv = file2id(file)->itv;
745 
746 	vin->index = itv->audio_input;
747 	return ivtv_get_audio_input(itv, vin->index, vin);
748 }
749 
750 static int ivtv_s_audio(struct file *file, void *fh, const struct v4l2_audio *vout)
751 {
752 	struct ivtv *itv = file2id(file)->itv;
753 
754 	if (vout->index >= itv->nof_audio_inputs)
755 		return -EINVAL;
756 
757 	itv->audio_input = vout->index;
758 	ivtv_audio_set_io(itv);
759 
760 	return 0;
761 }
762 
763 static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
764 {
765 	struct ivtv *itv = file2id(file)->itv;
766 
767 	/* set it to defaults from our table */
768 	return ivtv_get_audio_output(itv, vin->index, vin);
769 }
770 
771 static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
772 {
773 	struct ivtv *itv = file2id(file)->itv;
774 
775 	vin->index = 0;
776 	return ivtv_get_audio_output(itv, vin->index, vin);
777 }
778 
779 static int ivtv_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
780 {
781 	struct ivtv *itv = file2id(file)->itv;
782 
783 	if (itv->card->video_outputs == NULL || vout->index != 0)
784 		return -EINVAL;
785 	return 0;
786 }
787 
788 static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
789 {
790 	struct ivtv *itv = file2id(file)->itv;
791 
792 	/* set it to defaults from our table */
793 	return ivtv_get_input(itv, vin->index, vin);
794 }
795 
796 static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
797 {
798 	struct ivtv *itv = file2id(file)->itv;
799 
800 	return ivtv_get_output(itv, vout->index, vout);
801 }
802 
803 static int ivtv_g_pixelaspect(struct file *file, void *fh,
804 			      int type, struct v4l2_fract *f)
805 {
806 	struct ivtv_open_id *id = file2id(file);
807 	struct ivtv *itv = id->itv;
808 
809 	if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
810 		f->numerator = itv->is_50hz ? 54 : 11;
811 		f->denominator = itv->is_50hz ? 59 : 10;
812 	} else if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
813 		f->numerator = itv->is_out_50hz ? 54 : 11;
814 		f->denominator = itv->is_out_50hz ? 59 : 10;
815 	} else {
816 		return -EINVAL;
817 	}
818 	return 0;
819 }
820 
821 static int ivtv_s_selection(struct file *file, void *fh,
822 			    struct v4l2_selection *sel)
823 {
824 	struct ivtv_open_id *id = file2id(file);
825 	struct ivtv *itv = id->itv;
826 	struct yuv_playback_info *yi = &itv->yuv_info;
827 	struct v4l2_rect r = { 0, 0, 720, 0 };
828 	int streamtype = id->type;
829 
830 	if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
831 	    !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
832 		return -EINVAL;
833 
834 	if (sel->target != V4L2_SEL_TGT_COMPOSE)
835 		return -EINVAL;
836 
837 
838 	if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
839 	    !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
840 		return -EINVAL;
841 
842 	r.height = itv->is_out_50hz ? 576 : 480;
843 	if (streamtype == IVTV_DEC_STREAM_TYPE_YUV && yi->track_osd) {
844 		r.width = yi->osd_full_w;
845 		r.height = yi->osd_full_h;
846 	}
847 	sel->r.width = clamp(sel->r.width, 16U, r.width);
848 	sel->r.height = clamp(sel->r.height, 16U, r.height);
849 	sel->r.left = clamp_t(unsigned, sel->r.left, 0, r.width - sel->r.width);
850 	sel->r.top = clamp_t(unsigned, sel->r.top, 0, r.height - sel->r.height);
851 
852 	if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
853 		yi->main_rect = sel->r;
854 		return 0;
855 	}
856 	if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
857 			sel->r.width, sel->r.height, sel->r.left, sel->r.top)) {
858 		itv->main_rect = sel->r;
859 		return 0;
860 	}
861 	return -EINVAL;
862 }
863 
864 static int ivtv_g_selection(struct file *file, void *fh,
865 			    struct v4l2_selection *sel)
866 {
867 	struct ivtv_open_id *id = file2id(file);
868 	struct ivtv *itv = id->itv;
869 	struct yuv_playback_info *yi = &itv->yuv_info;
870 	struct v4l2_rect r = { 0, 0, 720, 0 };
871 	int streamtype = id->type;
872 
873 	if (sel->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
874 		switch (sel->target) {
875 		case V4L2_SEL_TGT_CROP_DEFAULT:
876 		case V4L2_SEL_TGT_CROP_BOUNDS:
877 			sel->r.top = sel->r.left = 0;
878 			sel->r.width = 720;
879 			sel->r.height = itv->is_50hz ? 576 : 480;
880 			return 0;
881 		default:
882 			return -EINVAL;
883 		}
884 	}
885 
886 	if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
887 	    !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
888 		return -EINVAL;
889 
890 	switch (sel->target) {
891 	case V4L2_SEL_TGT_COMPOSE:
892 		if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
893 			sel->r = yi->main_rect;
894 		else
895 			sel->r = itv->main_rect;
896 		return 0;
897 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
898 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
899 		r.height = itv->is_out_50hz ? 576 : 480;
900 		if (streamtype == IVTV_DEC_STREAM_TYPE_YUV && yi->track_osd) {
901 			r.width = yi->osd_full_w;
902 			r.height = yi->osd_full_h;
903 		}
904 		sel->r = r;
905 		return 0;
906 	}
907 	return -EINVAL;
908 }
909 
910 static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
911 {
912 	static const struct v4l2_fmtdesc hm12 = {
913 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
914 		.description = "HM12 (YUV 4:2:0)",
915 		.pixelformat = V4L2_PIX_FMT_NV12_16L16,
916 	};
917 	static const struct v4l2_fmtdesc mpeg = {
918 		.type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
919 		.flags = V4L2_FMT_FLAG_COMPRESSED,
920 		.description = "MPEG",
921 		.pixelformat = V4L2_PIX_FMT_MPEG,
922 	};
923 	struct ivtv *itv = file2id(file)->itv;
924 	struct ivtv_stream *s = &itv->streams[file2id(file)->type];
925 
926 	if (fmt->index)
927 		return -EINVAL;
928 	if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
929 		*fmt = mpeg;
930 	else if (s->type == IVTV_ENC_STREAM_TYPE_YUV)
931 		*fmt = hm12;
932 	else
933 		return -EINVAL;
934 	return 0;
935 }
936 
937 static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
938 {
939 	static const struct v4l2_fmtdesc hm12 = {
940 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
941 		.description = "HM12 (YUV 4:2:0)",
942 		.pixelformat = V4L2_PIX_FMT_NV12_16L16,
943 	};
944 	static const struct v4l2_fmtdesc mpeg = {
945 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT,
946 		.flags = V4L2_FMT_FLAG_COMPRESSED,
947 		.description = "MPEG",
948 		.pixelformat = V4L2_PIX_FMT_MPEG,
949 	};
950 	struct ivtv *itv = file2id(file)->itv;
951 	struct ivtv_stream *s = &itv->streams[file2id(file)->type];
952 
953 	if (fmt->index)
954 		return -EINVAL;
955 	if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
956 		*fmt = mpeg;
957 	else if (s->type == IVTV_DEC_STREAM_TYPE_YUV)
958 		*fmt = hm12;
959 	else
960 		return -EINVAL;
961 	return 0;
962 }
963 
964 static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
965 {
966 	struct ivtv *itv = file2id(file)->itv;
967 
968 	*i = itv->active_input;
969 
970 	return 0;
971 }
972 
973 int ivtv_do_s_input(struct ivtv *itv, unsigned int inp)
974 {
975 	v4l2_std_id std;
976 	int i;
977 
978 	if (inp >= itv->nof_inputs)
979 		return -EINVAL;
980 
981 	if (inp == itv->active_input) {
982 		IVTV_DEBUG_INFO("Input unchanged\n");
983 		return 0;
984 	}
985 
986 	if (atomic_read(&itv->capturing) > 0) {
987 		return -EBUSY;
988 	}
989 
990 	IVTV_DEBUG_INFO("Changing input from %d to %d\n",
991 			itv->active_input, inp);
992 
993 	itv->active_input = inp;
994 	/* Set the audio input to whatever is appropriate for the
995 	   input type. */
996 	itv->audio_input = itv->card->video_inputs[inp].audio_index;
997 
998 	if (itv->card->video_inputs[inp].video_type == IVTV_CARD_INPUT_VID_TUNER)
999 		std = itv->tuner_std;
1000 	else
1001 		std = V4L2_STD_ALL;
1002 	for (i = 0; i <= IVTV_ENC_STREAM_TYPE_VBI; i++)
1003 		itv->streams[i].vdev.tvnorms = std;
1004 
1005 	/* prevent others from messing with the streams until
1006 	   we're finished changing inputs. */
1007 	ivtv_mute(itv);
1008 	ivtv_video_set_io(itv);
1009 	ivtv_audio_set_io(itv);
1010 	ivtv_unmute(itv);
1011 
1012 	return 0;
1013 }
1014 
1015 static int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
1016 {
1017 	return ivtv_do_s_input(file2id(file)->itv, inp);
1018 }
1019 
1020 static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1021 {
1022 	struct ivtv *itv = file2id(file)->itv;
1023 
1024 	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1025 		return -EINVAL;
1026 
1027 	*i = itv->active_output;
1028 
1029 	return 0;
1030 }
1031 
1032 static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1033 {
1034 	struct ivtv *itv = file2id(file)->itv;
1035 
1036 	if (outp >= itv->card->nof_outputs)
1037 		return -EINVAL;
1038 
1039 	if (outp == itv->active_output) {
1040 		IVTV_DEBUG_INFO("Output unchanged\n");
1041 		return 0;
1042 	}
1043 	IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1044 		   itv->active_output, outp);
1045 
1046 	itv->active_output = outp;
1047 	ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
1048 			SAA7127_INPUT_TYPE_NORMAL,
1049 			itv->card->video_outputs[outp].video_output, 0);
1050 
1051 	return 0;
1052 }
1053 
1054 static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1055 {
1056 	struct ivtv *itv = file2id(file)->itv;
1057 	struct ivtv_stream *s = &itv->streams[file2id(file)->type];
1058 
1059 	if (s->vdev.vfl_dir)
1060 		return -ENOTTY;
1061 	if (vf->tuner != 0)
1062 		return -EINVAL;
1063 
1064 	ivtv_call_all(itv, tuner, g_frequency, vf);
1065 	return 0;
1066 }
1067 
1068 int ivtv_do_s_frequency(struct ivtv_stream *s, const struct v4l2_frequency *vf)
1069 {
1070 	struct ivtv *itv = s->itv;
1071 
1072 	if (s->vdev.vfl_dir)
1073 		return -ENOTTY;
1074 	if (vf->tuner != 0)
1075 		return -EINVAL;
1076 
1077 	ivtv_mute(itv);
1078 	IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1079 	ivtv_call_all(itv, tuner, s_frequency, vf);
1080 	ivtv_unmute(itv);
1081 	return 0;
1082 }
1083 
1084 static int ivtv_s_frequency(struct file *file, void *fh,
1085 			    const struct v4l2_frequency *vf)
1086 {
1087 	struct ivtv_open_id *id = file2id(file);
1088 	struct ivtv *itv = id->itv;
1089 
1090 	return ivtv_do_s_frequency(&itv->streams[id->type], vf);
1091 }
1092 
1093 static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1094 {
1095 	struct ivtv *itv = file2id(file)->itv;
1096 
1097 	*std = itv->std;
1098 	return 0;
1099 }
1100 
1101 void ivtv_s_std_enc(struct ivtv *itv, v4l2_std_id std)
1102 {
1103 	itv->std = std;
1104 	itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1105 	itv->is_50hz = !itv->is_60hz;
1106 	cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1107 	itv->cxhdl.width = 720;
1108 	itv->cxhdl.height = itv->is_50hz ? 576 : 480;
1109 	itv->vbi.count = itv->is_50hz ? 18 : 12;
1110 	itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1111 	itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1112 
1113 	if (itv->hw_flags & IVTV_HW_CX25840)
1114 		itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1115 
1116 	/* Tuner */
1117 	ivtv_call_all(itv, video, s_std, itv->std);
1118 }
1119 
1120 void ivtv_s_std_dec(struct ivtv *itv, v4l2_std_id std)
1121 {
1122 	struct yuv_playback_info *yi = &itv->yuv_info;
1123 	DEFINE_WAIT(wait);
1124 	int f;
1125 
1126 	/* set display standard */
1127 	itv->std_out = std;
1128 	itv->is_out_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1129 	itv->is_out_50hz = !itv->is_out_60hz;
1130 	ivtv_call_all(itv, video, s_std_output, itv->std_out);
1131 
1132 	/*
1133 	 * The next firmware call is time sensitive. Time it to
1134 	 * avoid risk of a hard lock, by trying to ensure the call
1135 	 * happens within the first 100 lines of the top field.
1136 	 * Make 4 attempts to sync to the decoder before giving up.
1137 	 */
1138 	mutex_unlock(&itv->serialize_lock);
1139 	for (f = 0; f < 4; f++) {
1140 		prepare_to_wait(&itv->vsync_waitq, &wait,
1141 				TASK_UNINTERRUPTIBLE);
1142 		if ((read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16) < 100)
1143 			break;
1144 		schedule_timeout(msecs_to_jiffies(25));
1145 	}
1146 	finish_wait(&itv->vsync_waitq, &wait);
1147 	mutex_lock(&itv->serialize_lock);
1148 
1149 	if (f == 4)
1150 		IVTV_WARN("Mode change failed to sync to decoder\n");
1151 
1152 	ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1153 	itv->main_rect.left = 0;
1154 	itv->main_rect.top = 0;
1155 	itv->main_rect.width = 720;
1156 	itv->main_rect.height = itv->is_out_50hz ? 576 : 480;
1157 	ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1158 		720, itv->main_rect.height, 0, 0);
1159 	yi->main_rect = itv->main_rect;
1160 	if (!itv->osd_info) {
1161 		yi->osd_full_w = 720;
1162 		yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1163 	}
1164 }
1165 
1166 static int ivtv_s_std(struct file *file, void *fh, v4l2_std_id std)
1167 {
1168 	struct ivtv *itv = file2id(file)->itv;
1169 
1170 	if ((std & V4L2_STD_ALL) == 0)
1171 		return -EINVAL;
1172 
1173 	if (std == itv->std)
1174 		return 0;
1175 
1176 	if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1177 	    atomic_read(&itv->capturing) > 0 ||
1178 	    atomic_read(&itv->decoding) > 0) {
1179 		/* Switching standard would mess with already running
1180 		   streams, prevent that by returning EBUSY. */
1181 		return -EBUSY;
1182 	}
1183 
1184 	IVTV_DEBUG_INFO("Switching standard to %llx.\n",
1185 		(unsigned long long)itv->std);
1186 
1187 	ivtv_s_std_enc(itv, std);
1188 	if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
1189 		ivtv_s_std_dec(itv, std);
1190 
1191 	return 0;
1192 }
1193 
1194 static int ivtv_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1195 {
1196 	struct ivtv_open_id *id = file2id(file);
1197 	struct ivtv *itv = id->itv;
1198 
1199 	if (vt->index != 0)
1200 		return -EINVAL;
1201 
1202 	ivtv_call_all(itv, tuner, s_tuner, vt);
1203 
1204 	return 0;
1205 }
1206 
1207 static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1208 {
1209 	struct ivtv *itv = file2id(file)->itv;
1210 
1211 	if (vt->index != 0)
1212 		return -EINVAL;
1213 
1214 	ivtv_call_all(itv, tuner, g_tuner, vt);
1215 
1216 	if (vt->type == V4L2_TUNER_RADIO)
1217 		strscpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1218 	else
1219 		strscpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1220 	return 0;
1221 }
1222 
1223 static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1224 {
1225 	struct ivtv *itv = file2id(file)->itv;
1226 	int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1227 	int f, l;
1228 
1229 	if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1230 		for (f = 0; f < 2; f++) {
1231 			for (l = 0; l < 24; l++) {
1232 				if (valid_service_line(f, l, itv->is_50hz))
1233 					cap->service_lines[f][l] = set;
1234 			}
1235 		}
1236 	} else if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1237 		if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1238 			return -EINVAL;
1239 		if (itv->is_60hz) {
1240 			cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1241 			cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1242 		} else {
1243 			cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1244 			cap->service_lines[0][16] = V4L2_SLICED_VPS;
1245 		}
1246 	} else {
1247 		return -EINVAL;
1248 	}
1249 
1250 	set = 0;
1251 	for (f = 0; f < 2; f++)
1252 		for (l = 0; l < 24; l++)
1253 			set |= cap->service_lines[f][l];
1254 	cap->service_set = set;
1255 	return 0;
1256 }
1257 
1258 static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1259 {
1260 	struct ivtv *itv = file2id(file)->itv;
1261 	struct v4l2_enc_idx_entry *e = idx->entry;
1262 	int entries;
1263 	int i;
1264 
1265 	entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1266 				IVTV_MAX_PGM_INDEX;
1267 	if (entries > V4L2_ENC_IDX_ENTRIES)
1268 		entries = V4L2_ENC_IDX_ENTRIES;
1269 	idx->entries = 0;
1270 	idx->entries_cap = IVTV_MAX_PGM_INDEX;
1271 	if (!atomic_read(&itv->capturing))
1272 		return 0;
1273 	for (i = 0; i < entries; i++) {
1274 		*e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1275 		if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1276 			idx->entries++;
1277 			e++;
1278 		}
1279 	}
1280 	itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1281 	return 0;
1282 }
1283 
1284 static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1285 {
1286 	struct ivtv_open_id *id = file2id(file);
1287 	struct ivtv *itv = id->itv;
1288 
1289 
1290 	switch (enc->cmd) {
1291 	case V4L2_ENC_CMD_START:
1292 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1293 		enc->flags = 0;
1294 		return ivtv_start_capture(id);
1295 
1296 	case V4L2_ENC_CMD_STOP:
1297 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1298 		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1299 		ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1300 		return 0;
1301 
1302 	case V4L2_ENC_CMD_PAUSE:
1303 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1304 		enc->flags = 0;
1305 
1306 		if (!atomic_read(&itv->capturing))
1307 			return -EPERM;
1308 		if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1309 			return 0;
1310 
1311 		ivtv_mute(itv);
1312 		ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1313 		break;
1314 
1315 	case V4L2_ENC_CMD_RESUME:
1316 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1317 		enc->flags = 0;
1318 
1319 		if (!atomic_read(&itv->capturing))
1320 			return -EPERM;
1321 
1322 		if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1323 			return 0;
1324 
1325 		ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1326 		ivtv_unmute(itv);
1327 		break;
1328 	default:
1329 		IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1330 		return -EINVAL;
1331 	}
1332 
1333 	return 0;
1334 }
1335 
1336 static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1337 {
1338 	struct ivtv *itv = file2id(file)->itv;
1339 
1340 	switch (enc->cmd) {
1341 	case V4L2_ENC_CMD_START:
1342 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1343 		enc->flags = 0;
1344 		return 0;
1345 
1346 	case V4L2_ENC_CMD_STOP:
1347 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1348 		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1349 		return 0;
1350 
1351 	case V4L2_ENC_CMD_PAUSE:
1352 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1353 		enc->flags = 0;
1354 		return 0;
1355 
1356 	case V4L2_ENC_CMD_RESUME:
1357 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1358 		enc->flags = 0;
1359 		return 0;
1360 	default:
1361 		IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1362 		return -EINVAL;
1363 	}
1364 }
1365 
1366 static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1367 {
1368 	struct ivtv *itv = file2id(file)->itv;
1369 	struct ivtv_stream *s = &itv->streams[file2id(file)->type];
1370 	u32 data[CX2341X_MBOX_MAX_DATA];
1371 	struct yuv_playback_info *yi = &itv->yuv_info;
1372 
1373 	int pixfmt;
1374 	static u32 pixel_format[16] = {
1375 		V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1376 		V4L2_PIX_FMT_RGB565,
1377 		V4L2_PIX_FMT_RGB555,
1378 		V4L2_PIX_FMT_RGB444,
1379 		V4L2_PIX_FMT_RGB32,
1380 		0,
1381 		0,
1382 		0,
1383 		V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1384 		V4L2_PIX_FMT_YUV565,
1385 		V4L2_PIX_FMT_YUV555,
1386 		V4L2_PIX_FMT_YUV444,
1387 		V4L2_PIX_FMT_YUV32,
1388 		0,
1389 		0,
1390 		0,
1391 	};
1392 
1393 	if (!(s->vdev.device_caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1394 		return -ENOTTY;
1395 	if (!itv->osd_video_pbase)
1396 		return -ENOTTY;
1397 
1398 	fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1399 		V4L2_FBUF_CAP_GLOBAL_ALPHA;
1400 
1401 	ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1402 	data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1403 	pixfmt = (data[0] >> 3) & 0xf;
1404 
1405 	fb->fmt.pixelformat = pixel_format[pixfmt];
1406 	fb->fmt.width = itv->osd_rect.width;
1407 	fb->fmt.height = itv->osd_rect.height;
1408 	fb->fmt.field = V4L2_FIELD_INTERLACED;
1409 	fb->fmt.bytesperline = fb->fmt.width;
1410 	fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
1411 	fb->fmt.field = V4L2_FIELD_INTERLACED;
1412 	if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
1413 		fb->fmt.bytesperline *= 2;
1414 	if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
1415 	    fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
1416 		fb->fmt.bytesperline *= 2;
1417 	fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
1418 	fb->base = (void *)itv->osd_video_pbase;
1419 	fb->flags = 0;
1420 
1421 	if (itv->osd_chroma_key_state)
1422 		fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1423 
1424 	if (itv->osd_global_alpha_state)
1425 		fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1426 
1427 	if (yi->track_osd)
1428 		fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1429 
1430 	pixfmt &= 7;
1431 
1432 	/* no local alpha for RGB565 or unknown formats */
1433 	if (pixfmt == 1 || pixfmt > 4)
1434 		return 0;
1435 
1436 	/* 16-bit formats have inverted local alpha */
1437 	if (pixfmt == 2 || pixfmt == 3)
1438 		fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1439 	else
1440 		fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1441 
1442 	if (itv->osd_local_alpha_state) {
1443 		/* 16-bit formats have inverted local alpha */
1444 		if (pixfmt == 2 || pixfmt == 3)
1445 			fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1446 		else
1447 			fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1448 	}
1449 
1450 	return 0;
1451 }
1452 
1453 static int ivtv_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *fb)
1454 {
1455 	struct ivtv_open_id *id = file2id(file);
1456 	struct ivtv *itv = id->itv;
1457 	struct ivtv_stream *s = &itv->streams[file2id(file)->type];
1458 	struct yuv_playback_info *yi = &itv->yuv_info;
1459 
1460 	if (!(s->vdev.device_caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1461 		return -ENOTTY;
1462 	if (!itv->osd_video_pbase)
1463 		return -ENOTTY;
1464 
1465 	itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1466 	itv->osd_local_alpha_state =
1467 		(fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1468 	itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1469 	ivtv_set_osd_alpha(itv);
1470 	yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1471 	return 0;
1472 }
1473 
1474 static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1475 {
1476 	struct ivtv_open_id *id = file2id(file);
1477 	struct ivtv *itv = id->itv;
1478 	struct ivtv_stream *s = &itv->streams[file2id(file)->type];
1479 
1480 	if (!(s->vdev.device_caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1481 		return -ENOTTY;
1482 	if (!itv->osd_video_pbase)
1483 		return -ENOTTY;
1484 
1485 	ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1486 
1487 	return 0;
1488 }
1489 
1490 static int ivtv_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
1491 {
1492 	switch (sub->type) {
1493 	case V4L2_EVENT_VSYNC:
1494 	case V4L2_EVENT_EOS:
1495 		return v4l2_event_subscribe(fh, sub, 0, NULL);
1496 	default:
1497 		return v4l2_ctrl_subscribe_event(fh, sub);
1498 	}
1499 }
1500 
1501 static int ivtv_log_status(struct file *file, void *fh)
1502 {
1503 	struct ivtv *itv = file2id(file)->itv;
1504 	u32 data[CX2341X_MBOX_MAX_DATA];
1505 
1506 	int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1507 	struct v4l2_input vidin;
1508 	struct v4l2_audio audin;
1509 	int i;
1510 
1511 	IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1512 	if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1513 		struct tveeprom tv;
1514 
1515 		ivtv_read_eeprom(itv, &tv);
1516 	}
1517 	ivtv_call_all(itv, core, log_status);
1518 	ivtv_get_input(itv, itv->active_input, &vidin);
1519 	ivtv_get_audio_input(itv, itv->audio_input, &audin);
1520 	IVTV_INFO("Video Input:  %s\n", vidin.name);
1521 	IVTV_INFO("Audio Input:  %s%s\n", audin.name,
1522 		itv->dualwatch_stereo_mode == V4L2_MPEG_AUDIO_MODE_DUAL ?
1523 			" (Bilingual)" : "");
1524 	if (has_output) {
1525 		struct v4l2_output vidout;
1526 		struct v4l2_audioout audout;
1527 		int mode = itv->output_mode;
1528 		static const char * const output_modes[5] = {
1529 			"None",
1530 			"MPEG Streaming",
1531 			"YUV Streaming",
1532 			"YUV Frames",
1533 			"Passthrough",
1534 		};
1535 		static const char * const alpha_mode[4] = {
1536 			"None",
1537 			"Global",
1538 			"Local",
1539 			"Global and Local"
1540 		};
1541 		static const char * const pixel_format[16] = {
1542 			"ARGB Indexed",
1543 			"RGB 5:6:5",
1544 			"ARGB 1:5:5:5",
1545 			"ARGB 1:4:4:4",
1546 			"ARGB 8:8:8:8",
1547 			"5",
1548 			"6",
1549 			"7",
1550 			"AYUV Indexed",
1551 			"YUV 5:6:5",
1552 			"AYUV 1:5:5:5",
1553 			"AYUV 1:4:4:4",
1554 			"AYUV 8:8:8:8",
1555 			"13",
1556 			"14",
1557 			"15",
1558 		};
1559 
1560 		ivtv_get_output(itv, itv->active_output, &vidout);
1561 		ivtv_get_audio_output(itv, 0, &audout);
1562 		IVTV_INFO("Video Output: %s\n", vidout.name);
1563 		if (mode < 0 || mode > OUT_PASSTHROUGH)
1564 			mode = OUT_NONE;
1565 		IVTV_INFO("Output Mode:  %s\n", output_modes[mode]);
1566 		ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1567 		data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1568 		IVTV_INFO("Overlay:      %s, Alpha: %s, Pixel Format: %s\n",
1569 			data[0] & 1 ? "On" : "Off",
1570 			alpha_mode[(data[0] >> 1) & 0x3],
1571 			pixel_format[(data[0] >> 3) & 0xf]);
1572 	}
1573 	IVTV_INFO("Tuner:  %s\n",
1574 		test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1575 	v4l2_ctrl_handler_log_status(&itv->cxhdl.hdl, itv->v4l2_dev.name);
1576 	IVTV_INFO("Status flags:    0x%08lx\n", itv->i_flags);
1577 	for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1578 		struct ivtv_stream *s = &itv->streams[i];
1579 
1580 		if (s->vdev.v4l2_dev == NULL || s->buffers == 0)
1581 			continue;
1582 		IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1583 				(s->buffers - s->q_free.buffers) * 100 / s->buffers,
1584 				(s->buffers * s->buf_size) / 1024, s->buffers);
1585 	}
1586 
1587 	IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n",
1588 			(long long)itv->mpg_data_received,
1589 			(long long)itv->vbi_data_inserted);
1590 	return 0;
1591 }
1592 
1593 static int ivtv_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1594 {
1595 	struct ivtv_open_id *id = file2id(file);
1596 	struct ivtv *itv = id->itv;
1597 
1598 	IVTV_DEBUG_IOCTL("VIDIOC_DECODER_CMD %d\n", dec->cmd);
1599 	return ivtv_video_command(itv, id, dec, false);
1600 }
1601 
1602 static int ivtv_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1603 {
1604 	struct ivtv_open_id *id = file2id(file);
1605 	struct ivtv *itv = id->itv;
1606 
1607 	IVTV_DEBUG_IOCTL("VIDIOC_TRY_DECODER_CMD %d\n", dec->cmd);
1608 	return ivtv_video_command(itv, id, dec, true);
1609 }
1610 
1611 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1612 {
1613 	struct ivtv_open_id *id = file2id(filp);
1614 	struct ivtv *itv = id->itv;
1615 	struct ivtv_stream *s = &itv->streams[id->type];
1616 
1617 	switch (cmd) {
1618 	case IVTV_IOC_DMA_FRAME: {
1619 		struct ivtv_dma_frame *args = arg;
1620 
1621 		IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1622 		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1623 			return -EINVAL;
1624 		if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1625 			return -EINVAL;
1626 		if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1627 			return 0;
1628 		if (ivtv_start_decoding(id, id->type)) {
1629 			return -EBUSY;
1630 		}
1631 		if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1632 			ivtv_release_stream(s);
1633 			return -EBUSY;
1634 		}
1635 		/* Mark that this file handle started the UDMA_YUV mode */
1636 		id->yuv_frames = 1;
1637 		if (args->y_source == NULL)
1638 			return 0;
1639 		return ivtv_yuv_prep_frame(itv, args);
1640 	}
1641 
1642 	case IVTV_IOC_PASSTHROUGH_MODE:
1643 		IVTV_DEBUG_IOCTL("IVTV_IOC_PASSTHROUGH_MODE\n");
1644 		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1645 			return -EINVAL;
1646 		return ivtv_passthrough_mode(itv, *(int *)arg != 0);
1647 	default:
1648 		return -EINVAL;
1649 	}
1650 	return 0;
1651 }
1652 
1653 static long ivtv_default(struct file *file, void *fh, bool valid_prio,
1654 			 unsigned int cmd, void *arg)
1655 {
1656 	struct ivtv *itv = file2id(file)->itv;
1657 
1658 	if (!valid_prio) {
1659 		switch (cmd) {
1660 		case IVTV_IOC_PASSTHROUGH_MODE:
1661 			return -EBUSY;
1662 		}
1663 	}
1664 
1665 	switch (cmd) {
1666 	case VIDIOC_INT_RESET: {
1667 		u32 val = *(u32 *)arg;
1668 
1669 		if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1670 			ivtv_reset_ir_gpio(itv);
1671 		if (val & 0x02)
1672 			v4l2_subdev_call(itv->sd_video, core, reset, 0);
1673 		break;
1674 	}
1675 
1676 	case IVTV_IOC_DMA_FRAME:
1677 	case IVTV_IOC_PASSTHROUGH_MODE:
1678 		return ivtv_decoder_ioctls(file, cmd, (void *)arg);
1679 
1680 	default:
1681 		return -ENOTTY;
1682 	}
1683 	return 0;
1684 }
1685 
1686 static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
1687 	.vidioc_querycap		    = ivtv_querycap,
1688 	.vidioc_s_audio			    = ivtv_s_audio,
1689 	.vidioc_g_audio			    = ivtv_g_audio,
1690 	.vidioc_enumaudio		    = ivtv_enumaudio,
1691 	.vidioc_s_audout		    = ivtv_s_audout,
1692 	.vidioc_g_audout		    = ivtv_g_audout,
1693 	.vidioc_enum_input		    = ivtv_enum_input,
1694 	.vidioc_enum_output		    = ivtv_enum_output,
1695 	.vidioc_enumaudout		    = ivtv_enumaudout,
1696 	.vidioc_g_pixelaspect		    = ivtv_g_pixelaspect,
1697 	.vidioc_s_selection		    = ivtv_s_selection,
1698 	.vidioc_g_selection		    = ivtv_g_selection,
1699 	.vidioc_g_input			    = ivtv_g_input,
1700 	.vidioc_s_input			    = ivtv_s_input,
1701 	.vidioc_g_output		    = ivtv_g_output,
1702 	.vidioc_s_output		    = ivtv_s_output,
1703 	.vidioc_g_frequency		    = ivtv_g_frequency,
1704 	.vidioc_s_frequency		    = ivtv_s_frequency,
1705 	.vidioc_s_tuner			    = ivtv_s_tuner,
1706 	.vidioc_g_tuner			    = ivtv_g_tuner,
1707 	.vidioc_g_enc_index		    = ivtv_g_enc_index,
1708 	.vidioc_g_fbuf			    = ivtv_g_fbuf,
1709 	.vidioc_s_fbuf			    = ivtv_s_fbuf,
1710 	.vidioc_g_std			    = ivtv_g_std,
1711 	.vidioc_s_std			    = ivtv_s_std,
1712 	.vidioc_overlay			    = ivtv_overlay,
1713 	.vidioc_log_status		    = ivtv_log_status,
1714 	.vidioc_enum_fmt_vid_cap	    = ivtv_enum_fmt_vid_cap,
1715 	.vidioc_encoder_cmd		    = ivtv_encoder_cmd,
1716 	.vidioc_try_encoder_cmd		    = ivtv_try_encoder_cmd,
1717 	.vidioc_decoder_cmd		    = ivtv_decoder_cmd,
1718 	.vidioc_try_decoder_cmd		    = ivtv_try_decoder_cmd,
1719 	.vidioc_enum_fmt_vid_out	    = ivtv_enum_fmt_vid_out,
1720 	.vidioc_g_fmt_vid_cap		    = ivtv_g_fmt_vid_cap,
1721 	.vidioc_g_fmt_vbi_cap		    = ivtv_g_fmt_vbi_cap,
1722 	.vidioc_g_fmt_sliced_vbi_cap        = ivtv_g_fmt_sliced_vbi_cap,
1723 	.vidioc_g_fmt_vid_out               = ivtv_g_fmt_vid_out,
1724 	.vidioc_g_fmt_vid_out_overlay       = ivtv_g_fmt_vid_out_overlay,
1725 	.vidioc_g_fmt_sliced_vbi_out        = ivtv_g_fmt_sliced_vbi_out,
1726 	.vidioc_s_fmt_vid_cap		    = ivtv_s_fmt_vid_cap,
1727 	.vidioc_s_fmt_vbi_cap		    = ivtv_s_fmt_vbi_cap,
1728 	.vidioc_s_fmt_sliced_vbi_cap        = ivtv_s_fmt_sliced_vbi_cap,
1729 	.vidioc_s_fmt_vid_out               = ivtv_s_fmt_vid_out,
1730 	.vidioc_s_fmt_vid_out_overlay       = ivtv_s_fmt_vid_out_overlay,
1731 	.vidioc_s_fmt_sliced_vbi_out        = ivtv_s_fmt_sliced_vbi_out,
1732 	.vidioc_try_fmt_vid_cap		    = ivtv_try_fmt_vid_cap,
1733 	.vidioc_try_fmt_vbi_cap		    = ivtv_try_fmt_vbi_cap,
1734 	.vidioc_try_fmt_sliced_vbi_cap      = ivtv_try_fmt_sliced_vbi_cap,
1735 	.vidioc_try_fmt_vid_out		    = ivtv_try_fmt_vid_out,
1736 	.vidioc_try_fmt_vid_out_overlay     = ivtv_try_fmt_vid_out_overlay,
1737 	.vidioc_try_fmt_sliced_vbi_out	    = ivtv_try_fmt_sliced_vbi_out,
1738 	.vidioc_g_sliced_vbi_cap	    = ivtv_g_sliced_vbi_cap,
1739 #ifdef CONFIG_VIDEO_ADV_DEBUG
1740 	.vidioc_g_register		    = ivtv_g_register,
1741 	.vidioc_s_register		    = ivtv_s_register,
1742 #endif
1743 	.vidioc_default			    = ivtv_default,
1744 	.vidioc_subscribe_event		    = ivtv_subscribe_event,
1745 	.vidioc_unsubscribe_event	    = v4l2_event_unsubscribe,
1746 };
1747 
1748 void ivtv_set_funcs(struct video_device *vdev)
1749 {
1750 	vdev->ioctl_ops = &ivtv_ioctl_ops;
1751 }
1752