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