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