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_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
833 {
834 	struct ivtv_open_id *id = fh2id(fh);
835 	struct ivtv *itv = id->itv;
836 
837 	if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
838 		cropcap->pixelaspect.numerator = itv->is_50hz ? 54 : 11;
839 		cropcap->pixelaspect.denominator = itv->is_50hz ? 59 : 10;
840 	} else if (cropcap->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
841 		cropcap->pixelaspect.numerator = itv->is_out_50hz ? 54 : 11;
842 		cropcap->pixelaspect.denominator = itv->is_out_50hz ? 59 : 10;
843 	} else {
844 		return -EINVAL;
845 	}
846 	return 0;
847 }
848 
849 static int ivtv_s_selection(struct file *file, void *fh,
850 			    struct v4l2_selection *sel)
851 {
852 	struct ivtv_open_id *id = fh2id(fh);
853 	struct ivtv *itv = id->itv;
854 	struct yuv_playback_info *yi = &itv->yuv_info;
855 	struct v4l2_rect r = { 0, 0, 720, 0 };
856 	int streamtype = id->type;
857 
858 	if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
859 	    !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
860 		return -EINVAL;
861 
862 	if (sel->target != V4L2_SEL_TGT_COMPOSE)
863 		return -EINVAL;
864 
865 
866 	if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
867 	    !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
868 		return -EINVAL;
869 
870 	r.height = itv->is_out_50hz ? 576 : 480;
871 	if (streamtype == IVTV_DEC_STREAM_TYPE_YUV && yi->track_osd) {
872 		r.width = yi->osd_full_w;
873 		r.height = yi->osd_full_h;
874 	}
875 	sel->r.width = clamp(sel->r.width, 16U, r.width);
876 	sel->r.height = clamp(sel->r.height, 16U, r.height);
877 	sel->r.left = clamp_t(unsigned, sel->r.left, 0, r.width - sel->r.width);
878 	sel->r.top = clamp_t(unsigned, sel->r.top, 0, r.height - sel->r.height);
879 
880 	if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
881 		yi->main_rect = sel->r;
882 		return 0;
883 	}
884 	if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
885 			sel->r.width, sel->r.height, sel->r.left, sel->r.top)) {
886 		itv->main_rect = sel->r;
887 		return 0;
888 	}
889 	return -EINVAL;
890 }
891 
892 static int ivtv_g_selection(struct file *file, void *fh,
893 			    struct v4l2_selection *sel)
894 {
895 	struct ivtv_open_id *id = fh2id(fh);
896 	struct ivtv *itv = id->itv;
897 	struct yuv_playback_info *yi = &itv->yuv_info;
898 	struct v4l2_rect r = { 0, 0, 720, 0 };
899 	int streamtype = id->type;
900 
901 	if (sel->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
902 		switch (sel->target) {
903 		case V4L2_SEL_TGT_CROP_DEFAULT:
904 		case V4L2_SEL_TGT_CROP_BOUNDS:
905 			sel->r.top = sel->r.left = 0;
906 			sel->r.width = 720;
907 			sel->r.height = itv->is_50hz ? 576 : 480;
908 			return 0;
909 		default:
910 			return -EINVAL;
911 		}
912 	}
913 
914 	if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
915 	    !(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
916 		return -EINVAL;
917 
918 	switch (sel->target) {
919 	case V4L2_SEL_TGT_COMPOSE:
920 		if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
921 			sel->r = yi->main_rect;
922 		else
923 			sel->r = itv->main_rect;
924 		return 0;
925 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
926 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
927 		r.height = itv->is_out_50hz ? 576 : 480;
928 		if (streamtype == IVTV_DEC_STREAM_TYPE_YUV && yi->track_osd) {
929 			r.width = yi->osd_full_w;
930 			r.height = yi->osd_full_h;
931 		}
932 		sel->r = r;
933 		return 0;
934 	}
935 	return -EINVAL;
936 }
937 
938 static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
939 {
940 	static const struct v4l2_fmtdesc hm12 = {
941 		0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
942 		"HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
943 		{ 0, 0, 0, 0 }
944 	};
945 	static const struct v4l2_fmtdesc mpeg = {
946 		0, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
947 		"MPEG", V4L2_PIX_FMT_MPEG,
948 		{ 0, 0, 0, 0 }
949 	};
950 	struct ivtv *itv = fh2id(fh)->itv;
951 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
952 
953 	if (fmt->index)
954 		return -EINVAL;
955 	if (s->type == IVTV_ENC_STREAM_TYPE_MPG)
956 		*fmt = mpeg;
957 	else if (s->type == IVTV_ENC_STREAM_TYPE_YUV)
958 		*fmt = hm12;
959 	else
960 		return -EINVAL;
961 	return 0;
962 }
963 
964 static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
965 {
966 	static const struct v4l2_fmtdesc hm12 = {
967 		0, V4L2_BUF_TYPE_VIDEO_OUTPUT, 0,
968 		"HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
969 		{ 0, 0, 0, 0 }
970 	};
971 	static const struct v4l2_fmtdesc mpeg = {
972 		0, V4L2_BUF_TYPE_VIDEO_OUTPUT, V4L2_FMT_FLAG_COMPRESSED,
973 		"MPEG", V4L2_PIX_FMT_MPEG,
974 		{ 0, 0, 0, 0 }
975 	};
976 	struct ivtv *itv = fh2id(fh)->itv;
977 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
978 
979 	if (fmt->index)
980 		return -EINVAL;
981 	if (s->type == IVTV_DEC_STREAM_TYPE_MPG)
982 		*fmt = mpeg;
983 	else if (s->type == IVTV_DEC_STREAM_TYPE_YUV)
984 		*fmt = hm12;
985 	else
986 		return -EINVAL;
987 	return 0;
988 }
989 
990 static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
991 {
992 	struct ivtv *itv = fh2id(fh)->itv;
993 
994 	*i = itv->active_input;
995 
996 	return 0;
997 }
998 
999 int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
1000 {
1001 	struct ivtv *itv = fh2id(fh)->itv;
1002 	v4l2_std_id std;
1003 	int i;
1004 
1005 	if (inp >= itv->nof_inputs)
1006 		return -EINVAL;
1007 
1008 	if (inp == itv->active_input) {
1009 		IVTV_DEBUG_INFO("Input unchanged\n");
1010 		return 0;
1011 	}
1012 
1013 	if (atomic_read(&itv->capturing) > 0) {
1014 		return -EBUSY;
1015 	}
1016 
1017 	IVTV_DEBUG_INFO("Changing input from %d to %d\n",
1018 			itv->active_input, inp);
1019 
1020 	itv->active_input = inp;
1021 	/* Set the audio input to whatever is appropriate for the
1022 	   input type. */
1023 	itv->audio_input = itv->card->video_inputs[inp].audio_index;
1024 
1025 	if (itv->card->video_inputs[inp].video_type == IVTV_CARD_INPUT_VID_TUNER)
1026 		std = itv->tuner_std;
1027 	else
1028 		std = V4L2_STD_ALL;
1029 	for (i = 0; i <= IVTV_ENC_STREAM_TYPE_VBI; i++)
1030 		itv->streams[i].vdev.tvnorms = std;
1031 
1032 	/* prevent others from messing with the streams until
1033 	   we're finished changing inputs. */
1034 	ivtv_mute(itv);
1035 	ivtv_video_set_io(itv);
1036 	ivtv_audio_set_io(itv);
1037 	ivtv_unmute(itv);
1038 
1039 	return 0;
1040 }
1041 
1042 static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1043 {
1044 	struct ivtv *itv = fh2id(fh)->itv;
1045 
1046 	if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1047 		return -EINVAL;
1048 
1049 	*i = itv->active_output;
1050 
1051 	return 0;
1052 }
1053 
1054 static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1055 {
1056 	struct ivtv *itv = fh2id(fh)->itv;
1057 
1058 	if (outp >= itv->card->nof_outputs)
1059 		return -EINVAL;
1060 
1061 	if (outp == itv->active_output) {
1062 		IVTV_DEBUG_INFO("Output unchanged\n");
1063 		return 0;
1064 	}
1065 	IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1066 		   itv->active_output, outp);
1067 
1068 	itv->active_output = outp;
1069 	ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
1070 			SAA7127_INPUT_TYPE_NORMAL,
1071 			itv->card->video_outputs[outp].video_output, 0);
1072 
1073 	return 0;
1074 }
1075 
1076 static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1077 {
1078 	struct ivtv *itv = fh2id(fh)->itv;
1079 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1080 
1081 	if (s->vdev.vfl_dir)
1082 		return -ENOTTY;
1083 	if (vf->tuner != 0)
1084 		return -EINVAL;
1085 
1086 	ivtv_call_all(itv, tuner, g_frequency, vf);
1087 	return 0;
1088 }
1089 
1090 int ivtv_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1091 {
1092 	struct ivtv *itv = fh2id(fh)->itv;
1093 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1094 
1095 	if (s->vdev.vfl_dir)
1096 		return -ENOTTY;
1097 	if (vf->tuner != 0)
1098 		return -EINVAL;
1099 
1100 	ivtv_mute(itv);
1101 	IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1102 	ivtv_call_all(itv, tuner, s_frequency, vf);
1103 	ivtv_unmute(itv);
1104 	return 0;
1105 }
1106 
1107 static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1108 {
1109 	struct ivtv *itv = fh2id(fh)->itv;
1110 
1111 	*std = itv->std;
1112 	return 0;
1113 }
1114 
1115 void ivtv_s_std_enc(struct ivtv *itv, v4l2_std_id std)
1116 {
1117 	itv->std = std;
1118 	itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1119 	itv->is_50hz = !itv->is_60hz;
1120 	cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1121 	itv->cxhdl.width = 720;
1122 	itv->cxhdl.height = itv->is_50hz ? 576 : 480;
1123 	itv->vbi.count = itv->is_50hz ? 18 : 12;
1124 	itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1125 	itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1126 
1127 	if (itv->hw_flags & IVTV_HW_CX25840)
1128 		itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1129 
1130 	/* Tuner */
1131 	ivtv_call_all(itv, video, s_std, itv->std);
1132 }
1133 
1134 void ivtv_s_std_dec(struct ivtv *itv, v4l2_std_id std)
1135 {
1136 	struct yuv_playback_info *yi = &itv->yuv_info;
1137 	DEFINE_WAIT(wait);
1138 	int f;
1139 
1140 	/* set display standard */
1141 	itv->std_out = std;
1142 	itv->is_out_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1143 	itv->is_out_50hz = !itv->is_out_60hz;
1144 	ivtv_call_all(itv, video, s_std_output, itv->std_out);
1145 
1146 	/*
1147 	 * The next firmware call is time sensitive. Time it to
1148 	 * avoid risk of a hard lock, by trying to ensure the call
1149 	 * happens within the first 100 lines of the top field.
1150 	 * Make 4 attempts to sync to the decoder before giving up.
1151 	 */
1152 	mutex_unlock(&itv->serialize_lock);
1153 	for (f = 0; f < 4; f++) {
1154 		prepare_to_wait(&itv->vsync_waitq, &wait,
1155 				TASK_UNINTERRUPTIBLE);
1156 		if ((read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16) < 100)
1157 			break;
1158 		schedule_timeout(msecs_to_jiffies(25));
1159 	}
1160 	finish_wait(&itv->vsync_waitq, &wait);
1161 	mutex_lock(&itv->serialize_lock);
1162 
1163 	if (f == 4)
1164 		IVTV_WARN("Mode change failed to sync to decoder\n");
1165 
1166 	ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1167 	itv->main_rect.left = 0;
1168 	itv->main_rect.top = 0;
1169 	itv->main_rect.width = 720;
1170 	itv->main_rect.height = itv->is_out_50hz ? 576 : 480;
1171 	ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1172 		720, itv->main_rect.height, 0, 0);
1173 	yi->main_rect = itv->main_rect;
1174 	if (!itv->osd_info) {
1175 		yi->osd_full_w = 720;
1176 		yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1177 	}
1178 }
1179 
1180 static int ivtv_s_std(struct file *file, void *fh, v4l2_std_id std)
1181 {
1182 	struct ivtv *itv = fh2id(fh)->itv;
1183 
1184 	if ((std & V4L2_STD_ALL) == 0)
1185 		return -EINVAL;
1186 
1187 	if (std == itv->std)
1188 		return 0;
1189 
1190 	if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1191 	    atomic_read(&itv->capturing) > 0 ||
1192 	    atomic_read(&itv->decoding) > 0) {
1193 		/* Switching standard would mess with already running
1194 		   streams, prevent that by returning EBUSY. */
1195 		return -EBUSY;
1196 	}
1197 
1198 	IVTV_DEBUG_INFO("Switching standard to %llx.\n",
1199 		(unsigned long long)itv->std);
1200 
1201 	ivtv_s_std_enc(itv, std);
1202 	if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)
1203 		ivtv_s_std_dec(itv, std);
1204 
1205 	return 0;
1206 }
1207 
1208 static int ivtv_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1209 {
1210 	struct ivtv_open_id *id = fh2id(fh);
1211 	struct ivtv *itv = id->itv;
1212 
1213 	if (vt->index != 0)
1214 		return -EINVAL;
1215 
1216 	ivtv_call_all(itv, tuner, s_tuner, vt);
1217 
1218 	return 0;
1219 }
1220 
1221 static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1222 {
1223 	struct ivtv *itv = fh2id(fh)->itv;
1224 
1225 	if (vt->index != 0)
1226 		return -EINVAL;
1227 
1228 	ivtv_call_all(itv, tuner, g_tuner, vt);
1229 
1230 	if (vt->type == V4L2_TUNER_RADIO)
1231 		strscpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1232 	else
1233 		strscpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1234 	return 0;
1235 }
1236 
1237 static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1238 {
1239 	struct ivtv *itv = fh2id(fh)->itv;
1240 	int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1241 	int f, l;
1242 
1243 	if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1244 		for (f = 0; f < 2; f++) {
1245 			for (l = 0; l < 24; l++) {
1246 				if (valid_service_line(f, l, itv->is_50hz))
1247 					cap->service_lines[f][l] = set;
1248 			}
1249 		}
1250 	} else if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1251 		if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1252 			return -EINVAL;
1253 		if (itv->is_60hz) {
1254 			cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1255 			cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1256 		} else {
1257 			cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1258 			cap->service_lines[0][16] = V4L2_SLICED_VPS;
1259 		}
1260 	} else {
1261 		return -EINVAL;
1262 	}
1263 
1264 	set = 0;
1265 	for (f = 0; f < 2; f++)
1266 		for (l = 0; l < 24; l++)
1267 			set |= cap->service_lines[f][l];
1268 	cap->service_set = set;
1269 	return 0;
1270 }
1271 
1272 static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1273 {
1274 	struct ivtv *itv = fh2id(fh)->itv;
1275 	struct v4l2_enc_idx_entry *e = idx->entry;
1276 	int entries;
1277 	int i;
1278 
1279 	entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1280 				IVTV_MAX_PGM_INDEX;
1281 	if (entries > V4L2_ENC_IDX_ENTRIES)
1282 		entries = V4L2_ENC_IDX_ENTRIES;
1283 	idx->entries = 0;
1284 	idx->entries_cap = IVTV_MAX_PGM_INDEX;
1285 	if (!atomic_read(&itv->capturing))
1286 		return 0;
1287 	for (i = 0; i < entries; i++) {
1288 		*e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1289 		if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1290 			idx->entries++;
1291 			e++;
1292 		}
1293 	}
1294 	itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1295 	return 0;
1296 }
1297 
1298 static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1299 {
1300 	struct ivtv_open_id *id = fh2id(fh);
1301 	struct ivtv *itv = id->itv;
1302 
1303 
1304 	switch (enc->cmd) {
1305 	case V4L2_ENC_CMD_START:
1306 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1307 		enc->flags = 0;
1308 		return ivtv_start_capture(id);
1309 
1310 	case V4L2_ENC_CMD_STOP:
1311 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1312 		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1313 		ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1314 		return 0;
1315 
1316 	case V4L2_ENC_CMD_PAUSE:
1317 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1318 		enc->flags = 0;
1319 
1320 		if (!atomic_read(&itv->capturing))
1321 			return -EPERM;
1322 		if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1323 			return 0;
1324 
1325 		ivtv_mute(itv);
1326 		ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1327 		break;
1328 
1329 	case V4L2_ENC_CMD_RESUME:
1330 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1331 		enc->flags = 0;
1332 
1333 		if (!atomic_read(&itv->capturing))
1334 			return -EPERM;
1335 
1336 		if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1337 			return 0;
1338 
1339 		ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1340 		ivtv_unmute(itv);
1341 		break;
1342 	default:
1343 		IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1344 		return -EINVAL;
1345 	}
1346 
1347 	return 0;
1348 }
1349 
1350 static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1351 {
1352 	struct ivtv *itv = fh2id(fh)->itv;
1353 
1354 	switch (enc->cmd) {
1355 	case V4L2_ENC_CMD_START:
1356 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1357 		enc->flags = 0;
1358 		return 0;
1359 
1360 	case V4L2_ENC_CMD_STOP:
1361 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1362 		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1363 		return 0;
1364 
1365 	case V4L2_ENC_CMD_PAUSE:
1366 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1367 		enc->flags = 0;
1368 		return 0;
1369 
1370 	case V4L2_ENC_CMD_RESUME:
1371 		IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1372 		enc->flags = 0;
1373 		return 0;
1374 	default:
1375 		IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1376 		return -EINVAL;
1377 	}
1378 }
1379 
1380 static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1381 {
1382 	struct ivtv *itv = fh2id(fh)->itv;
1383 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1384 	u32 data[CX2341X_MBOX_MAX_DATA];
1385 	struct yuv_playback_info *yi = &itv->yuv_info;
1386 
1387 	int pixfmt;
1388 	static u32 pixel_format[16] = {
1389 		V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1390 		V4L2_PIX_FMT_RGB565,
1391 		V4L2_PIX_FMT_RGB555,
1392 		V4L2_PIX_FMT_RGB444,
1393 		V4L2_PIX_FMT_RGB32,
1394 		0,
1395 		0,
1396 		0,
1397 		V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1398 		V4L2_PIX_FMT_YUV565,
1399 		V4L2_PIX_FMT_YUV555,
1400 		V4L2_PIX_FMT_YUV444,
1401 		V4L2_PIX_FMT_YUV32,
1402 		0,
1403 		0,
1404 		0,
1405 	};
1406 
1407 	if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1408 		return -ENOTTY;
1409 	if (!itv->osd_video_pbase)
1410 		return -ENOTTY;
1411 
1412 	fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1413 		V4L2_FBUF_CAP_GLOBAL_ALPHA;
1414 
1415 	ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1416 	data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1417 	pixfmt = (data[0] >> 3) & 0xf;
1418 
1419 	fb->fmt.pixelformat = pixel_format[pixfmt];
1420 	fb->fmt.width = itv->osd_rect.width;
1421 	fb->fmt.height = itv->osd_rect.height;
1422 	fb->fmt.field = V4L2_FIELD_INTERLACED;
1423 	fb->fmt.bytesperline = fb->fmt.width;
1424 	fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
1425 	fb->fmt.field = V4L2_FIELD_INTERLACED;
1426 	if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
1427 		fb->fmt.bytesperline *= 2;
1428 	if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
1429 	    fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
1430 		fb->fmt.bytesperline *= 2;
1431 	fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
1432 	fb->base = (void *)itv->osd_video_pbase;
1433 	fb->flags = 0;
1434 
1435 	if (itv->osd_chroma_key_state)
1436 		fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1437 
1438 	if (itv->osd_global_alpha_state)
1439 		fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1440 
1441 	if (yi->track_osd)
1442 		fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1443 
1444 	pixfmt &= 7;
1445 
1446 	/* no local alpha for RGB565 or unknown formats */
1447 	if (pixfmt == 1 || pixfmt > 4)
1448 		return 0;
1449 
1450 	/* 16-bit formats have inverted local alpha */
1451 	if (pixfmt == 2 || pixfmt == 3)
1452 		fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1453 	else
1454 		fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1455 
1456 	if (itv->osd_local_alpha_state) {
1457 		/* 16-bit formats have inverted local alpha */
1458 		if (pixfmt == 2 || pixfmt == 3)
1459 			fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1460 		else
1461 			fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1462 	}
1463 
1464 	return 0;
1465 }
1466 
1467 static int ivtv_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *fb)
1468 {
1469 	struct ivtv_open_id *id = fh2id(fh);
1470 	struct ivtv *itv = id->itv;
1471 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1472 	struct yuv_playback_info *yi = &itv->yuv_info;
1473 
1474 	if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1475 		return -ENOTTY;
1476 	if (!itv->osd_video_pbase)
1477 		return -ENOTTY;
1478 
1479 	itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1480 	itv->osd_local_alpha_state =
1481 		(fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1482 	itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1483 	ivtv_set_osd_alpha(itv);
1484 	yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1485 	return 0;
1486 }
1487 
1488 static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1489 {
1490 	struct ivtv_open_id *id = fh2id(fh);
1491 	struct ivtv *itv = id->itv;
1492 	struct ivtv_stream *s = &itv->streams[fh2id(fh)->type];
1493 
1494 	if (!(s->caps & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1495 		return -ENOTTY;
1496 	if (!itv->osd_video_pbase)
1497 		return -ENOTTY;
1498 
1499 	ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1500 
1501 	return 0;
1502 }
1503 
1504 static int ivtv_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
1505 {
1506 	switch (sub->type) {
1507 	case V4L2_EVENT_VSYNC:
1508 	case V4L2_EVENT_EOS:
1509 		return v4l2_event_subscribe(fh, sub, 0, NULL);
1510 	default:
1511 		return v4l2_ctrl_subscribe_event(fh, sub);
1512 	}
1513 }
1514 
1515 static int ivtv_log_status(struct file *file, void *fh)
1516 {
1517 	struct ivtv *itv = fh2id(fh)->itv;
1518 	u32 data[CX2341X_MBOX_MAX_DATA];
1519 
1520 	int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1521 	struct v4l2_input vidin;
1522 	struct v4l2_audio audin;
1523 	int i;
1524 
1525 	IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1526 	if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1527 		struct tveeprom tv;
1528 
1529 		ivtv_read_eeprom(itv, &tv);
1530 	}
1531 	ivtv_call_all(itv, core, log_status);
1532 	ivtv_get_input(itv, itv->active_input, &vidin);
1533 	ivtv_get_audio_input(itv, itv->audio_input, &audin);
1534 	IVTV_INFO("Video Input:  %s\n", vidin.name);
1535 	IVTV_INFO("Audio Input:  %s%s\n", audin.name,
1536 		itv->dualwatch_stereo_mode == V4L2_MPEG_AUDIO_MODE_DUAL ?
1537 			" (Bilingual)" : "");
1538 	if (has_output) {
1539 		struct v4l2_output vidout;
1540 		struct v4l2_audioout audout;
1541 		int mode = itv->output_mode;
1542 		static const char * const output_modes[5] = {
1543 			"None",
1544 			"MPEG Streaming",
1545 			"YUV Streaming",
1546 			"YUV Frames",
1547 			"Passthrough",
1548 		};
1549 		static const char * const alpha_mode[4] = {
1550 			"None",
1551 			"Global",
1552 			"Local",
1553 			"Global and Local"
1554 		};
1555 		static const char * const pixel_format[16] = {
1556 			"ARGB Indexed",
1557 			"RGB 5:6:5",
1558 			"ARGB 1:5:5:5",
1559 			"ARGB 1:4:4:4",
1560 			"ARGB 8:8:8:8",
1561 			"5",
1562 			"6",
1563 			"7",
1564 			"AYUV Indexed",
1565 			"YUV 5:6:5",
1566 			"AYUV 1:5:5:5",
1567 			"AYUV 1:4:4:4",
1568 			"AYUV 8:8:8:8",
1569 			"13",
1570 			"14",
1571 			"15",
1572 		};
1573 
1574 		ivtv_get_output(itv, itv->active_output, &vidout);
1575 		ivtv_get_audio_output(itv, 0, &audout);
1576 		IVTV_INFO("Video Output: %s\n", vidout.name);
1577 		if (mode < 0 || mode > OUT_PASSTHROUGH)
1578 			mode = OUT_NONE;
1579 		IVTV_INFO("Output Mode:  %s\n", output_modes[mode]);
1580 		ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1581 		data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1582 		IVTV_INFO("Overlay:      %s, Alpha: %s, Pixel Format: %s\n",
1583 			data[0] & 1 ? "On" : "Off",
1584 			alpha_mode[(data[0] >> 1) & 0x3],
1585 			pixel_format[(data[0] >> 3) & 0xf]);
1586 	}
1587 	IVTV_INFO("Tuner:  %s\n",
1588 		test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1589 	v4l2_ctrl_handler_log_status(&itv->cxhdl.hdl, itv->v4l2_dev.name);
1590 	IVTV_INFO("Status flags:    0x%08lx\n", itv->i_flags);
1591 	for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1592 		struct ivtv_stream *s = &itv->streams[i];
1593 
1594 		if (s->vdev.v4l2_dev == NULL || s->buffers == 0)
1595 			continue;
1596 		IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1597 				(s->buffers - s->q_free.buffers) * 100 / s->buffers,
1598 				(s->buffers * s->buf_size) / 1024, s->buffers);
1599 	}
1600 
1601 	IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n",
1602 			(long long)itv->mpg_data_received,
1603 			(long long)itv->vbi_data_inserted);
1604 	return 0;
1605 }
1606 
1607 static int ivtv_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1608 {
1609 	struct ivtv_open_id *id = fh2id(file->private_data);
1610 	struct ivtv *itv = id->itv;
1611 
1612 	IVTV_DEBUG_IOCTL("VIDIOC_DECODER_CMD %d\n", dec->cmd);
1613 	return ivtv_video_command(itv, id, dec, false);
1614 }
1615 
1616 static int ivtv_try_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *dec)
1617 {
1618 	struct ivtv_open_id *id = fh2id(file->private_data);
1619 	struct ivtv *itv = id->itv;
1620 
1621 	IVTV_DEBUG_IOCTL("VIDIOC_TRY_DECODER_CMD %d\n", dec->cmd);
1622 	return ivtv_video_command(itv, id, dec, true);
1623 }
1624 
1625 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1626 static __inline__ void warn_deprecated_ioctl(const char *name)
1627 {
1628 	pr_warn_once("warning: the %s ioctl is deprecated. Don't use it, as it will be removed soon\n",
1629 		     name);
1630 }
1631 
1632 #ifdef CONFIG_COMPAT
1633 struct compat_video_event {
1634 	__s32 type;
1635 	/* unused, make sure to use atomic time for y2038 if it ever gets used */
1636 	compat_long_t timestamp;
1637 	union {
1638 		video_size_t size;
1639 		unsigned int frame_rate;        /* in frames per 1000sec */
1640 		unsigned char vsync_field;      /* unknown/odd/even/progressive */
1641 	} u;
1642 };
1643 #define VIDEO_GET_EVENT32 _IOR('o', 28, struct compat_video_event)
1644 #endif
1645 
1646 #endif
1647 
1648 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1649 {
1650 	struct ivtv_open_id *id = fh2id(filp->private_data);
1651 	struct ivtv *itv = id->itv;
1652 	struct ivtv_stream *s = &itv->streams[id->type];
1653 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1654 	int nonblocking = filp->f_flags & O_NONBLOCK;
1655 	unsigned long iarg = (unsigned long)arg;
1656 #endif
1657 
1658 	switch (cmd) {
1659 	case IVTV_IOC_DMA_FRAME: {
1660 		struct ivtv_dma_frame *args = arg;
1661 
1662 		IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1663 		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1664 			return -EINVAL;
1665 		if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1666 			return -EINVAL;
1667 		if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1668 			return 0;
1669 		if (ivtv_start_decoding(id, id->type)) {
1670 			return -EBUSY;
1671 		}
1672 		if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1673 			ivtv_release_stream(s);
1674 			return -EBUSY;
1675 		}
1676 		/* Mark that this file handle started the UDMA_YUV mode */
1677 		id->yuv_frames = 1;
1678 		if (args->y_source == NULL)
1679 			return 0;
1680 		return ivtv_yuv_prep_frame(itv, args);
1681 	}
1682 
1683 	case IVTV_IOC_PASSTHROUGH_MODE:
1684 		IVTV_DEBUG_IOCTL("IVTV_IOC_PASSTHROUGH_MODE\n");
1685 		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1686 			return -EINVAL;
1687 		return ivtv_passthrough_mode(itv, *(int *)arg != 0);
1688 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1689 	case VIDEO_GET_PTS: {
1690 		s64 *pts = arg;
1691 		s64 frame;
1692 
1693 		warn_deprecated_ioctl("VIDEO_GET_PTS");
1694 		if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1695 			*pts = s->dma_pts;
1696 			break;
1697 		}
1698 		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1699 			return -EINVAL;
1700 		return ivtv_g_pts_frame(itv, pts, &frame);
1701 	}
1702 
1703 	case VIDEO_GET_FRAME_COUNT: {
1704 		s64 *frame = arg;
1705 		s64 pts;
1706 
1707 		warn_deprecated_ioctl("VIDEO_GET_FRAME_COUNT");
1708 		if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1709 			*frame = 0;
1710 			break;
1711 		}
1712 		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1713 			return -EINVAL;
1714 		return ivtv_g_pts_frame(itv, &pts, frame);
1715 	}
1716 
1717 	case VIDEO_PLAY: {
1718 		struct v4l2_decoder_cmd dc;
1719 
1720 		warn_deprecated_ioctl("VIDEO_PLAY");
1721 		memset(&dc, 0, sizeof(dc));
1722 		dc.cmd = V4L2_DEC_CMD_START;
1723 		return ivtv_video_command(itv, id, &dc, 0);
1724 	}
1725 
1726 	case VIDEO_STOP: {
1727 		struct v4l2_decoder_cmd dc;
1728 
1729 		warn_deprecated_ioctl("VIDEO_STOP");
1730 		memset(&dc, 0, sizeof(dc));
1731 		dc.cmd = V4L2_DEC_CMD_STOP;
1732 		dc.flags = V4L2_DEC_CMD_STOP_TO_BLACK | V4L2_DEC_CMD_STOP_IMMEDIATELY;
1733 		return ivtv_video_command(itv, id, &dc, 0);
1734 	}
1735 
1736 	case VIDEO_FREEZE: {
1737 		struct v4l2_decoder_cmd dc;
1738 
1739 		warn_deprecated_ioctl("VIDEO_FREEZE");
1740 		memset(&dc, 0, sizeof(dc));
1741 		dc.cmd = V4L2_DEC_CMD_PAUSE;
1742 		return ivtv_video_command(itv, id, &dc, 0);
1743 	}
1744 
1745 	case VIDEO_CONTINUE: {
1746 		struct v4l2_decoder_cmd dc;
1747 
1748 		warn_deprecated_ioctl("VIDEO_CONTINUE");
1749 		memset(&dc, 0, sizeof(dc));
1750 		dc.cmd = V4L2_DEC_CMD_RESUME;
1751 		return ivtv_video_command(itv, id, &dc, 0);
1752 	}
1753 
1754 	case VIDEO_COMMAND:
1755 	case VIDEO_TRY_COMMAND: {
1756 		/* Note: struct v4l2_decoder_cmd has the same layout as
1757 		   struct video_command */
1758 		struct v4l2_decoder_cmd *dc = arg;
1759 		int try = (cmd == VIDEO_TRY_COMMAND);
1760 
1761 		if (try)
1762 			warn_deprecated_ioctl("VIDEO_TRY_COMMAND");
1763 		else
1764 			warn_deprecated_ioctl("VIDEO_COMMAND");
1765 		return ivtv_video_command(itv, id, dc, try);
1766 	}
1767 
1768 #ifdef CONFIG_COMPAT
1769 	case VIDEO_GET_EVENT32:
1770 #endif
1771 	case VIDEO_GET_EVENT: {
1772 #ifdef CONFIG_COMPAT
1773 		struct compat_video_event *ev32 = arg;
1774 #endif
1775 		struct video_event *ev = arg;
1776 		DEFINE_WAIT(wait);
1777 
1778 		warn_deprecated_ioctl("VIDEO_GET_EVENT");
1779 		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1780 			return -EINVAL;
1781 		memset(ev, 0, sizeof(*ev));
1782 		set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1783 
1784 		while (1) {
1785 			if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1786 				ev->type = VIDEO_EVENT_DECODER_STOPPED;
1787 			else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1788 				unsigned char vsync_field;
1789 
1790 				ev->type = VIDEO_EVENT_VSYNC;
1791 				vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1792 					VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1793 				if (itv->output_mode == OUT_UDMA_YUV &&
1794 					(itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1795 								IVTV_YUV_MODE_PROGRESSIVE) {
1796 					vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1797 				}
1798 #ifdef CONFIG_COMPAT
1799 				if (cmd == VIDEO_GET_EVENT32)
1800 					ev32->u.vsync_field = vsync_field;
1801 				else
1802 #endif
1803 					ev->u.vsync_field = vsync_field;
1804 			}
1805 			if (ev->type)
1806 				return 0;
1807 			if (nonblocking)
1808 				return -EAGAIN;
1809 			/* Wait for event. Note that serialize_lock is locked,
1810 			   so to allow other processes to access the driver while
1811 			   we are waiting unlock first and later lock again. */
1812 			mutex_unlock(&itv->serialize_lock);
1813 			prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1814 			if (!test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags) &&
1815 			    !test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags))
1816 				schedule();
1817 			finish_wait(&itv->event_waitq, &wait);
1818 			mutex_lock(&itv->serialize_lock);
1819 			if (signal_pending(current)) {
1820 				/* return if a signal was received */
1821 				IVTV_DEBUG_INFO("User stopped wait for event\n");
1822 				return -EINTR;
1823 			}
1824 		}
1825 		break;
1826 	}
1827 
1828 	case VIDEO_SELECT_SOURCE:
1829 		warn_deprecated_ioctl("VIDEO_SELECT_SOURCE");
1830 		if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1831 			return -EINVAL;
1832 		return ivtv_passthrough_mode(itv, iarg == VIDEO_SOURCE_DEMUX);
1833 
1834 	case AUDIO_SET_MUTE:
1835 		warn_deprecated_ioctl("AUDIO_SET_MUTE");
1836 		itv->speed_mute_audio = iarg;
1837 		return 0;
1838 
1839 	case AUDIO_CHANNEL_SELECT:
1840 		warn_deprecated_ioctl("AUDIO_CHANNEL_SELECT");
1841 		if (iarg > AUDIO_STEREO_SWAPPED)
1842 			return -EINVAL;
1843 		return v4l2_ctrl_s_ctrl(itv->ctrl_audio_playback, iarg + 1);
1844 
1845 	case AUDIO_BILINGUAL_CHANNEL_SELECT:
1846 		warn_deprecated_ioctl("AUDIO_BILINGUAL_CHANNEL_SELECT");
1847 		if (iarg > AUDIO_STEREO_SWAPPED)
1848 			return -EINVAL;
1849 		return v4l2_ctrl_s_ctrl(itv->ctrl_audio_multilingual_playback, iarg + 1);
1850 #endif
1851 	default:
1852 		return -EINVAL;
1853 	}
1854 	return 0;
1855 }
1856 
1857 static long ivtv_default(struct file *file, void *fh, bool valid_prio,
1858 			 unsigned int cmd, void *arg)
1859 {
1860 	struct ivtv *itv = fh2id(fh)->itv;
1861 
1862 	if (!valid_prio) {
1863 		switch (cmd) {
1864 		case IVTV_IOC_PASSTHROUGH_MODE:
1865 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1866 		case VIDEO_PLAY:
1867 		case VIDEO_STOP:
1868 		case VIDEO_FREEZE:
1869 		case VIDEO_CONTINUE:
1870 		case VIDEO_COMMAND:
1871 		case VIDEO_SELECT_SOURCE:
1872 		case AUDIO_SET_MUTE:
1873 		case AUDIO_CHANNEL_SELECT:
1874 		case AUDIO_BILINGUAL_CHANNEL_SELECT:
1875 #endif
1876 			return -EBUSY;
1877 		}
1878 	}
1879 
1880 	switch (cmd) {
1881 	case VIDIOC_INT_RESET: {
1882 		u32 val = *(u32 *)arg;
1883 
1884 		if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1885 			ivtv_reset_ir_gpio(itv);
1886 		if (val & 0x02)
1887 			v4l2_subdev_call(itv->sd_video, core, reset, 0);
1888 		break;
1889 	}
1890 
1891 	case IVTV_IOC_DMA_FRAME:
1892 	case IVTV_IOC_PASSTHROUGH_MODE:
1893 #ifdef CONFIG_VIDEO_IVTV_DEPRECATED_IOCTLS
1894 	case VIDEO_GET_PTS:
1895 	case VIDEO_GET_FRAME_COUNT:
1896 	case VIDEO_GET_EVENT:
1897 	case VIDEO_PLAY:
1898 	case VIDEO_STOP:
1899 	case VIDEO_FREEZE:
1900 	case VIDEO_CONTINUE:
1901 	case VIDEO_COMMAND:
1902 	case VIDEO_TRY_COMMAND:
1903 	case VIDEO_SELECT_SOURCE:
1904 	case AUDIO_SET_MUTE:
1905 	case AUDIO_CHANNEL_SELECT:
1906 	case AUDIO_BILINGUAL_CHANNEL_SELECT:
1907 #endif
1908 		return ivtv_decoder_ioctls(file, cmd, (void *)arg);
1909 
1910 	default:
1911 		return -ENOTTY;
1912 	}
1913 	return 0;
1914 }
1915 
1916 static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
1917 	.vidioc_querycap		    = ivtv_querycap,
1918 	.vidioc_s_audio			    = ivtv_s_audio,
1919 	.vidioc_g_audio			    = ivtv_g_audio,
1920 	.vidioc_enumaudio		    = ivtv_enumaudio,
1921 	.vidioc_s_audout		    = ivtv_s_audout,
1922 	.vidioc_g_audout		    = ivtv_g_audout,
1923 	.vidioc_enum_input		    = ivtv_enum_input,
1924 	.vidioc_enum_output		    = ivtv_enum_output,
1925 	.vidioc_enumaudout		    = ivtv_enumaudout,
1926 	.vidioc_cropcap			    = ivtv_cropcap,
1927 	.vidioc_s_selection		    = ivtv_s_selection,
1928 	.vidioc_g_selection		    = ivtv_g_selection,
1929 	.vidioc_g_input			    = ivtv_g_input,
1930 	.vidioc_s_input			    = ivtv_s_input,
1931 	.vidioc_g_output		    = ivtv_g_output,
1932 	.vidioc_s_output		    = ivtv_s_output,
1933 	.vidioc_g_frequency		    = ivtv_g_frequency,
1934 	.vidioc_s_frequency		    = ivtv_s_frequency,
1935 	.vidioc_s_tuner			    = ivtv_s_tuner,
1936 	.vidioc_g_tuner			    = ivtv_g_tuner,
1937 	.vidioc_g_enc_index		    = ivtv_g_enc_index,
1938 	.vidioc_g_fbuf			    = ivtv_g_fbuf,
1939 	.vidioc_s_fbuf			    = ivtv_s_fbuf,
1940 	.vidioc_g_std			    = ivtv_g_std,
1941 	.vidioc_s_std			    = ivtv_s_std,
1942 	.vidioc_overlay			    = ivtv_overlay,
1943 	.vidioc_log_status		    = ivtv_log_status,
1944 	.vidioc_enum_fmt_vid_cap	    = ivtv_enum_fmt_vid_cap,
1945 	.vidioc_encoder_cmd		    = ivtv_encoder_cmd,
1946 	.vidioc_try_encoder_cmd		    = ivtv_try_encoder_cmd,
1947 	.vidioc_decoder_cmd		    = ivtv_decoder_cmd,
1948 	.vidioc_try_decoder_cmd		    = ivtv_try_decoder_cmd,
1949 	.vidioc_enum_fmt_vid_out	    = ivtv_enum_fmt_vid_out,
1950 	.vidioc_g_fmt_vid_cap		    = ivtv_g_fmt_vid_cap,
1951 	.vidioc_g_fmt_vbi_cap		    = ivtv_g_fmt_vbi_cap,
1952 	.vidioc_g_fmt_sliced_vbi_cap        = ivtv_g_fmt_sliced_vbi_cap,
1953 	.vidioc_g_fmt_vid_out               = ivtv_g_fmt_vid_out,
1954 	.vidioc_g_fmt_vid_out_overlay       = ivtv_g_fmt_vid_out_overlay,
1955 	.vidioc_g_fmt_sliced_vbi_out        = ivtv_g_fmt_sliced_vbi_out,
1956 	.vidioc_s_fmt_vid_cap		    = ivtv_s_fmt_vid_cap,
1957 	.vidioc_s_fmt_vbi_cap		    = ivtv_s_fmt_vbi_cap,
1958 	.vidioc_s_fmt_sliced_vbi_cap        = ivtv_s_fmt_sliced_vbi_cap,
1959 	.vidioc_s_fmt_vid_out               = ivtv_s_fmt_vid_out,
1960 	.vidioc_s_fmt_vid_out_overlay       = ivtv_s_fmt_vid_out_overlay,
1961 	.vidioc_s_fmt_sliced_vbi_out        = ivtv_s_fmt_sliced_vbi_out,
1962 	.vidioc_try_fmt_vid_cap		    = ivtv_try_fmt_vid_cap,
1963 	.vidioc_try_fmt_vbi_cap		    = ivtv_try_fmt_vbi_cap,
1964 	.vidioc_try_fmt_sliced_vbi_cap      = ivtv_try_fmt_sliced_vbi_cap,
1965 	.vidioc_try_fmt_vid_out		    = ivtv_try_fmt_vid_out,
1966 	.vidioc_try_fmt_vid_out_overlay     = ivtv_try_fmt_vid_out_overlay,
1967 	.vidioc_try_fmt_sliced_vbi_out	    = ivtv_try_fmt_sliced_vbi_out,
1968 	.vidioc_g_sliced_vbi_cap	    = ivtv_g_sliced_vbi_cap,
1969 #ifdef CONFIG_VIDEO_ADV_DEBUG
1970 	.vidioc_g_register		    = ivtv_g_register,
1971 	.vidioc_s_register		    = ivtv_s_register,
1972 #endif
1973 	.vidioc_default			    = ivtv_default,
1974 	.vidioc_subscribe_event		    = ivtv_subscribe_event,
1975 	.vidioc_unsubscribe_event	    = v4l2_event_unsubscribe,
1976 };
1977 
1978 void ivtv_set_funcs(struct video_device *vdev)
1979 {
1980 	vdev->ioctl_ops = &ivtv_ioctl_ops;
1981 }
1982