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