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