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