Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild
[linux-2.6] / 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-audio.h"
29 #include "ivtv-video.h"
30 #include "ivtv-streams.h"
31 #include "ivtv-yuv.h"
32 #include "ivtv-ioctl.h"
33 #include "ivtv-gpio.h"
34 #include "ivtv-controls.h"
35 #include "ivtv-cards.h"
36 #include <media/saa7127.h>
37 #include <media/tveeprom.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <linux/dvb/audio.h>
40 #include <linux/i2c-id.h>
41
42 u16 service2vbi(int type)
43 {
44         switch (type) {
45                 case V4L2_SLICED_TELETEXT_B:
46                         return IVTV_SLICED_TYPE_TELETEXT_B;
47                 case V4L2_SLICED_CAPTION_525:
48                         return IVTV_SLICED_TYPE_CAPTION_525;
49                 case V4L2_SLICED_WSS_625:
50                         return IVTV_SLICED_TYPE_WSS_625;
51                 case V4L2_SLICED_VPS:
52                         return IVTV_SLICED_TYPE_VPS;
53                 default:
54                         return 0;
55         }
56 }
57
58 static int valid_service_line(int field, int line, int is_pal)
59 {
60         return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
61                (!is_pal && line >= 10 && line < 22);
62 }
63
64 static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
65 {
66         u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
67         int i;
68
69         set = set & valid_set;
70         if (set == 0 || !valid_service_line(field, line, is_pal)) {
71                 return 0;
72         }
73         if (!is_pal) {
74                 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
75                         return V4L2_SLICED_CAPTION_525;
76         }
77         else {
78                 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
79                         return V4L2_SLICED_VPS;
80                 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
81                         return V4L2_SLICED_WSS_625;
82                 if (line == 23)
83                         return 0;
84         }
85         for (i = 0; i < 32; i++) {
86                 if ((1 << i) & set)
87                         return 1 << i;
88         }
89         return 0;
90 }
91
92 void expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
93 {
94         u16 set = fmt->service_set;
95         int f, l;
96
97         fmt->service_set = 0;
98         for (f = 0; f < 2; f++) {
99                 for (l = 0; l < 24; l++) {
100                         fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
101                 }
102         }
103 }
104
105 static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
106 {
107         int f, l;
108         u16 set = 0;
109
110         for (f = 0; f < 2; f++) {
111                 for (l = 0; l < 24; l++) {
112                         fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
113                         set |= fmt->service_lines[f][l];
114                 }
115         }
116         return set != 0;
117 }
118
119 u16 get_service_set(struct v4l2_sliced_vbi_format *fmt)
120 {
121         int f, l;
122         u16 set = 0;
123
124         for (f = 0; f < 2; f++) {
125                 for (l = 0; l < 24; l++) {
126                         set |= fmt->service_lines[f][l];
127                 }
128         }
129         return set;
130 }
131
132 static const struct {
133         v4l2_std_id  std;
134         char        *name;
135 } enum_stds[] = {
136         { V4L2_STD_PAL_BG | V4L2_STD_PAL_H, "PAL-BGH" },
137         { V4L2_STD_PAL_DK,    "PAL-DK"    },
138         { V4L2_STD_PAL_I,     "PAL-I"     },
139         { V4L2_STD_PAL_M,     "PAL-M"     },
140         { V4L2_STD_PAL_N,     "PAL-N"     },
141         { V4L2_STD_PAL_Nc,    "PAL-Nc"    },
142         { V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H, "SECAM-BGH" },
143         { V4L2_STD_SECAM_DK,  "SECAM-DK"  },
144         { V4L2_STD_SECAM_L,   "SECAM-L"   },
145         { V4L2_STD_SECAM_LC,  "SECAM-L'"  },
146         { V4L2_STD_NTSC_M,    "NTSC-M"    },
147         { V4L2_STD_NTSC_M_JP, "NTSC-J"    },
148         { V4L2_STD_NTSC_M_KR, "NTSC-K"    },
149 };
150
151 static const struct v4l2_standard ivtv_std_60hz =
152 {
153         .frameperiod = {.numerator = 1001, .denominator = 30000},
154         .framelines = 525,
155 };
156
157 static const struct v4l2_standard ivtv_std_50hz =
158 {
159         .frameperiod = {.numerator = 1, .denominator = 25},
160         .framelines = 625,
161 };
162
163 void ivtv_set_osd_alpha(struct ivtv *itv)
164 {
165         ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
166                 itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
167         ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_color_key_state, itv->osd_color_key);
168 }
169
170 int ivtv_set_speed(struct ivtv *itv, int speed)
171 {
172         u32 data[CX2341X_MBOX_MAX_DATA];
173         struct ivtv_stream *s;
174         int single_step = (speed == 1 || speed == -1);
175         DEFINE_WAIT(wait);
176
177         if (speed == 0) speed = 1000;
178
179         /* No change? */
180         if (speed == itv->speed && !single_step)
181                 return 0;
182
183         s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
184
185         if (single_step && (speed < 0) == (itv->speed < 0)) {
186                 /* Single step video and no need to change direction */
187                 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
188                 itv->speed = speed;
189                 return 0;
190         }
191         if (single_step)
192                 /* Need to change direction */
193                 speed = speed < 0 ? -1000 : 1000;
194
195         data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
196         data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
197         data[1] = (speed < 0);
198         data[2] = speed < 0 ? 3 : 7;
199         data[3] = itv->params.video_b_frames;
200         data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
201         data[5] = 0;
202         data[6] = 0;
203
204         if (speed == 1500 || speed == -1500) data[0] |= 1;
205         else if (speed == 2000 || speed == -2000) data[0] |= 2;
206         else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
207         else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
208
209         /* If not decoding, just change speed setting */
210         if (atomic_read(&itv->decoding) > 0) {
211                 int got_sig = 0;
212
213                 /* Stop all DMA and decoding activity */
214                 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
215
216                 /* Wait for any DMA to finish */
217                 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
218                 while (itv->i_flags & IVTV_F_I_DMA) {
219                         got_sig = signal_pending(current);
220                         if (got_sig)
221                                 break;
222                         got_sig = 0;
223                         schedule();
224                 }
225                 finish_wait(&itv->dma_waitq, &wait);
226                 if (got_sig)
227                         return -EINTR;
228
229                 /* Change Speed safely */
230                 ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
231                 IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
232                                 data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
233         }
234         if (single_step) {
235                 speed = (speed < 0) ? -1 : 1;
236                 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
237         }
238         itv->speed = speed;
239         return 0;
240 }
241
242 static int ivtv_validate_speed(int cur_speed, int new_speed)
243 {
244         int fact = new_speed < 0 ? -1 : 1;
245         int s;
246
247         if (new_speed < 0) new_speed = -new_speed;
248         if (cur_speed < 0) cur_speed = -cur_speed;
249
250         if (cur_speed <= new_speed) {
251                 if (new_speed > 1500) return fact * 2000;
252                 if (new_speed > 1000) return fact * 1500;
253         }
254         else {
255                 if (new_speed >= 2000) return fact * 2000;
256                 if (new_speed >= 1500) return fact * 1500;
257                 if (new_speed >= 1000) return fact * 1000;
258         }
259         if (new_speed == 0) return 1000;
260         if (new_speed == 1 || new_speed == 1000) return fact * new_speed;
261
262         s = new_speed;
263         new_speed = 1000 / new_speed;
264         if (1000 / cur_speed == new_speed)
265                 new_speed += (cur_speed < s) ? -1 : 1;
266         if (new_speed > 60) return 1000 / (fact * 60);
267         return 1000 / (fact * new_speed);
268 }
269
270 static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
271                 struct video_command *vc, int try)
272 {
273         struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
274
275         if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
276                 return -EINVAL;
277
278         switch (vc->cmd) {
279         case VIDEO_CMD_PLAY: {
280                 vc->flags = 0;
281                 vc->play.speed = ivtv_validate_speed(itv->speed, vc->play.speed);
282                 if (vc->play.speed < 0)
283                         vc->play.format = VIDEO_PLAY_FMT_GOP;
284                 if (try) break;
285
286                 if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
287                         return -EBUSY;
288                 return ivtv_start_decoding(id, vc->play.speed);
289         }
290
291         case VIDEO_CMD_STOP:
292                 vc->flags &= VIDEO_CMD_STOP_IMMEDIATELY|VIDEO_CMD_STOP_TO_BLACK;
293                 if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
294                         vc->stop.pts = 0;
295                 if (try) break;
296                 if (atomic_read(&itv->decoding) == 0)
297                         return 0;
298                 if (itv->output_mode != OUT_MPG)
299                         return -EBUSY;
300
301                 itv->output_mode = OUT_NONE;
302                 return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);
303
304         case VIDEO_CMD_FREEZE:
305                 vc->flags &= VIDEO_CMD_FREEZE_TO_BLACK;
306                 if (try) break;
307                 if (itv->output_mode != OUT_MPG)
308                         return -EBUSY;
309                 if (atomic_read(&itv->decoding) > 0) {
310                         ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
311                                 (vc->flags & VIDEO_CMD_FREEZE_TO_BLACK) ? 1 : 0);
312                 }
313                 break;
314
315         case VIDEO_CMD_CONTINUE:
316                 vc->flags = 0;
317                 if (try) break;
318                 if (itv->output_mode != OUT_MPG)
319                         return -EBUSY;
320                 if (atomic_read(&itv->decoding) > 0) {
321                         ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 0);
322                 }
323                 break;
324
325         default:
326                 return -EINVAL;
327         }
328         return 0;
329 }
330
331 static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
332 {
333         struct v4l2_register *regs = arg;
334         unsigned long flags;
335         volatile u8 __iomem *reg_start;
336
337         if (!capable(CAP_SYS_ADMIN))
338                 return -EPERM;
339         if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
340                 reg_start = itv->reg_mem - IVTV_REG_OFFSET;
341         else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
342                         regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
343                 reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
344         else if (regs->reg >= 0 && regs->reg < IVTV_ENCODER_SIZE)
345                 reg_start = itv->enc_mem;
346         else
347                 return -EINVAL;
348
349         spin_lock_irqsave(&ivtv_cards_lock, flags);
350         if (cmd == VIDIOC_DBG_G_REGISTER) {
351                 regs->val = readl(regs->reg + reg_start);
352         } else {
353                 writel(regs->val, regs->reg + reg_start);
354         }
355         spin_unlock_irqrestore(&ivtv_cards_lock, flags);
356         return 0;
357 }
358
359 static int ivtv_get_fmt(struct ivtv *itv, int streamtype, struct v4l2_format *fmt)
360 {
361         switch (fmt->type) {
362         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
363                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
364                         return -EINVAL;
365                 fmt->fmt.pix.width = itv->main_rect.width;
366                 fmt->fmt.pix.height = itv->main_rect.height;
367                 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
368                 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
369                 if (itv->output_mode == OUT_UDMA_YUV) {
370                         switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
371                         case IVTV_YUV_MODE_INTERLACED:
372                                 fmt->fmt.pix.field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
373                                         V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
374                                 break;
375                         case IVTV_YUV_MODE_PROGRESSIVE:
376                                 fmt->fmt.pix.field = V4L2_FIELD_NONE;
377                                 break;
378                         default:
379                                 fmt->fmt.pix.field = V4L2_FIELD_ANY;
380                                 break;
381                         }
382                         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
383                         /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
384                         fmt->fmt.pix.sizeimage =
385                                 fmt->fmt.pix.height * fmt->fmt.pix.width +
386                                 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
387                 }
388                 else if (itv->output_mode == OUT_YUV ||
389                                 streamtype == IVTV_ENC_STREAM_TYPE_YUV ||
390                                 streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
391                         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
392                         /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
393                         fmt->fmt.pix.sizeimage =
394                                 fmt->fmt.pix.height * fmt->fmt.pix.width +
395                                 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
396                 } else {
397                         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
398                         fmt->fmt.pix.sizeimage = 128 * 1024;
399                 }
400                 break;
401
402         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
403                 fmt->fmt.pix.width = itv->params.width;
404                 fmt->fmt.pix.height = itv->params.height;
405                 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
406                 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
407                 if (streamtype == IVTV_ENC_STREAM_TYPE_YUV ||
408                                 streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
409                         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
410                         /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
411                         fmt->fmt.pix.sizeimage =
412                                 fmt->fmt.pix.height * fmt->fmt.pix.width +
413                                 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
414                 } else {
415                         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
416                         fmt->fmt.pix.sizeimage = 128 * 1024;
417                 }
418                 break;
419
420         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
421                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
422                         return -EINVAL;
423                 fmt->fmt.win.chromakey = itv->osd_color_key;
424                 fmt->fmt.win.global_alpha = itv->osd_global_alpha;
425                 break;
426
427         case V4L2_BUF_TYPE_VBI_CAPTURE:
428                 fmt->fmt.vbi.sampling_rate = 27000000;
429                 fmt->fmt.vbi.offset = 248;
430                 fmt->fmt.vbi.samples_per_line = itv->vbi.raw_decoder_line_size - 4;
431                 fmt->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
432                 fmt->fmt.vbi.start[0] = itv->vbi.start[0];
433                 fmt->fmt.vbi.start[1] = itv->vbi.start[1];
434                 fmt->fmt.vbi.count[0] = fmt->fmt.vbi.count[1] = itv->vbi.count;
435                 break;
436
437         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
438         {
439                 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
440
441                 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
442                         return -EINVAL;
443                 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
444                 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
445                 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
446                 if (itv->is_60hz) {
447                         vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
448                         vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
449                 } else {
450                         vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
451                         vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
452                 }
453                 vbifmt->service_set = get_service_set(vbifmt);
454                 break;
455         }
456
457         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
458         {
459                 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
460
461                 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
462                 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
463                 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
464
465                 if (streamtype == IVTV_DEC_STREAM_TYPE_VBI) {
466                         vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
467                                                  V4L2_SLICED_VBI_525;
468                         expand_service_set(vbifmt, itv->is_50hz);
469                         break;
470                 }
471
472                 itv->video_dec_func(itv, VIDIOC_G_FMT, fmt);
473                 vbifmt->service_set = get_service_set(vbifmt);
474                 break;
475         }
476         case V4L2_BUF_TYPE_VBI_OUTPUT:
477         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
478         default:
479                 return -EINVAL;
480         }
481         return 0;
482 }
483
484 static int ivtv_try_or_set_fmt(struct ivtv *itv, int streamtype,
485                 struct v4l2_format *fmt, int set_fmt)
486 {
487         struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
488         u16 set;
489
490         if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
491                 struct v4l2_rect r;
492                 int field;
493
494                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
495                         return -EINVAL;
496                 field = fmt->fmt.pix.field;
497                 r.top = 0;
498                 r.left = 0;
499                 r.width = fmt->fmt.pix.width;
500                 r.height = fmt->fmt.pix.height;
501                 ivtv_get_fmt(itv, streamtype, fmt);
502                 if (itv->output_mode != OUT_UDMA_YUV) {
503                         /* TODO: would setting the rect also be valid for this mode? */
504                         fmt->fmt.pix.width = r.width;
505                         fmt->fmt.pix.height = r.height;
506                 }
507                 if (itv->output_mode == OUT_UDMA_YUV) {
508                         /* TODO: add checks for validity */
509                         fmt->fmt.pix.field = field;
510                 }
511                 if (set_fmt) {
512                         if (itv->output_mode == OUT_UDMA_YUV) {
513                                 switch (field) {
514                                 case V4L2_FIELD_NONE:
515                                         itv->yuv_info.lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
516                                         break;
517                                 case V4L2_FIELD_ANY:
518                                         itv->yuv_info.lace_mode = IVTV_YUV_MODE_AUTO;
519                                         break;
520                                 case V4L2_FIELD_INTERLACED_BT:
521                                         itv->yuv_info.lace_mode =
522                                                 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
523                                         break;
524                                 case V4L2_FIELD_INTERLACED_TB:
525                                 default:
526                                         itv->yuv_info.lace_mode = IVTV_YUV_MODE_INTERLACED;
527                                         break;
528                                 }
529                                 itv->yuv_info.lace_sync_field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
530
531                                 /* Force update of yuv registers */
532                                 itv->yuv_info.yuv_forced_update = 1;
533                                 return 0;
534                         }
535                 }
536                 return 0;
537         }
538
539         if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY) {
540                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
541                         return -EINVAL;
542                 if (set_fmt) {
543                         itv->osd_color_key = fmt->fmt.win.chromakey;
544                         itv->osd_global_alpha = fmt->fmt.win.global_alpha;
545                         ivtv_set_osd_alpha(itv);
546                 }
547                 return 0;
548         }
549
550         /* set window size */
551         if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
552                 int w = fmt->fmt.pix.width;
553                 int h = fmt->fmt.pix.height;
554
555                 if (w > 720) w = 720;
556                 else if (w < 1) w = 1;
557                 if (h > (itv->is_50hz ? 576 : 480)) h = (itv->is_50hz ? 576 : 480);
558                 else if (h < 2) h = 2;
559                 ivtv_get_fmt(itv, streamtype, fmt);
560                 fmt->fmt.pix.width = w;
561                 fmt->fmt.pix.height = h;
562
563                 if (!set_fmt || (itv->params.width == w && itv->params.height == h))
564                         return 0;
565                 if (atomic_read(&itv->capturing) > 0)
566                         return -EBUSY;
567
568                 itv->params.width = w;
569                 itv->params.height = h;
570                 if (w != 720 || h != (itv->is_50hz ? 576 : 480))
571                         itv->params.video_temporal_filter = 0;
572                 else
573                         itv->params.video_temporal_filter = 8;
574                 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
575                 return ivtv_get_fmt(itv, streamtype, fmt);
576         }
577
578         /* set raw VBI format */
579         if (fmt->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
580                 if (set_fmt && streamtype == IVTV_ENC_STREAM_TYPE_VBI &&
581                     itv->vbi.sliced_in->service_set &&
582                     atomic_read(&itv->capturing) > 0) {
583                         return -EBUSY;
584                 }
585                 if (set_fmt) {
586                         itv->vbi.sliced_in->service_set = 0;
587                         itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
588                 }
589                 return ivtv_get_fmt(itv, streamtype, fmt);
590         }
591
592         /* set sliced VBI output
593            In principle the user could request that only certain
594            VBI types are output and that the others are ignored.
595            I.e., suppress CC in the even fields or only output
596            WSS and no VPS. Currently though there is no choice. */
597         if (fmt->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
598                 return ivtv_get_fmt(itv, streamtype, fmt);
599
600         /* any else but sliced VBI capture is an error */
601         if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
602                 return -EINVAL;
603
604         if (streamtype == IVTV_DEC_STREAM_TYPE_VBI)
605                 return ivtv_get_fmt(itv, streamtype, fmt);
606
607         /* set sliced VBI capture format */
608         vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
609         memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
610
611         if (vbifmt->service_set)
612                 expand_service_set(vbifmt, itv->is_50hz);
613         set = check_service_set(vbifmt, itv->is_50hz);
614         vbifmt->service_set = get_service_set(vbifmt);
615
616         if (!set_fmt)
617                 return 0;
618         if (set == 0)
619                 return -EINVAL;
620         if (atomic_read(&itv->capturing) > 0 && itv->vbi.sliced_in->service_set == 0) {
621                 return -EBUSY;
622         }
623         itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
624         memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
625         return 0;
626 }
627
628 static int ivtv_debug_ioctls(struct file *filp, unsigned int cmd, void *arg)
629 {
630         struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
631         struct ivtv *itv = id->itv;
632         struct v4l2_register *reg = arg;
633
634         switch (cmd) {
635         /* ioctls to allow direct access to the encoder registers for testing */
636         case VIDIOC_DBG_G_REGISTER:
637                 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
638                         return ivtv_itvc(itv, cmd, arg);
639                 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
640                         return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
641                 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
642
643         case VIDIOC_DBG_S_REGISTER:
644                 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
645                         return ivtv_itvc(itv, cmd, arg);
646                 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
647                         return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
648                 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
649
650         case VIDIOC_G_CHIP_IDENT: {
651                 struct v4l2_chip_ident *chip = arg;
652
653                 chip->ident = V4L2_IDENT_NONE;
654                 chip->revision = 0;
655                 if (reg->match_type == V4L2_CHIP_MATCH_HOST) {
656                         if (v4l2_chip_match_host(reg->match_type, reg->match_chip)) {
657                                 struct v4l2_chip_ident *chip = arg;
658
659                                 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
660                         }
661                         return 0;
662                 }
663                 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
664                         return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
665                 if (reg->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
666                         return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
667                 return -EINVAL;
668         }
669
670         case VIDIOC_INT_S_AUDIO_ROUTING: {
671                 struct v4l2_routing *route = arg;
672
673                 ivtv_audio_set_route(itv, route);
674                 break;
675         }
676
677         case VIDIOC_INT_RESET:
678                 ivtv_reset_ir_gpio(itv);
679                 break;
680
681         default:
682                 return -EINVAL;
683         }
684         return 0;
685 }
686
687 int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void *arg)
688 {
689         struct ivtv_open_id *id = NULL;
690
691         if (filp) id = (struct ivtv_open_id *)filp->private_data;
692
693         switch (cmd) {
694         case VIDIOC_G_PRIORITY:
695         {
696                 enum v4l2_priority *p = arg;
697
698                 *p = v4l2_prio_max(&itv->prio);
699                 break;
700         }
701
702         case VIDIOC_S_PRIORITY:
703         {
704                 enum v4l2_priority *prio = arg;
705
706                 return v4l2_prio_change(&itv->prio, &id->prio, *prio);
707         }
708
709         case VIDIOC_QUERYCAP:{
710                 struct v4l2_capability *vcap = arg;
711
712                 memset(vcap, 0, sizeof(*vcap));
713                 strcpy(vcap->driver, IVTV_DRIVER_NAME);     /* driver name */
714                 strcpy(vcap->card, itv->card_name);         /* card type */
715                 strcpy(vcap->bus_info, pci_name(itv->dev)); /* bus info... */
716                 vcap->version = IVTV_DRIVER_VERSION;        /* version */
717                 vcap->capabilities = itv->v4l2_cap;         /* capabilities */
718
719                 /* reserved.. must set to 0! */
720                 vcap->reserved[0] = vcap->reserved[1] =
721                         vcap->reserved[2] = vcap->reserved[3] = 0;
722                 break;
723         }
724
725         case VIDIOC_ENUMAUDIO:{
726                 struct v4l2_audio *vin = arg;
727
728                 return ivtv_get_audio_input(itv, vin->index, vin);
729         }
730
731         case VIDIOC_G_AUDIO:{
732                 struct v4l2_audio *vin = arg;
733
734                 vin->index = itv->audio_input;
735                 return ivtv_get_audio_input(itv, vin->index, vin);
736         }
737
738         case VIDIOC_S_AUDIO:{
739                 struct v4l2_audio *vout = arg;
740
741                 if (vout->index >= itv->nof_audio_inputs)
742                         return -EINVAL;
743                 itv->audio_input = vout->index;
744                 ivtv_audio_set_io(itv);
745                 break;
746         }
747
748         case VIDIOC_ENUMAUDOUT:{
749                 struct v4l2_audioout *vin = arg;
750
751                 /* set it to defaults from our table */
752                 return ivtv_get_audio_output(itv, vin->index, vin);
753         }
754
755         case VIDIOC_G_AUDOUT:{
756                 struct v4l2_audioout *vin = arg;
757
758                 vin->index = 0;
759                 return ivtv_get_audio_output(itv, vin->index, vin);
760         }
761
762         case VIDIOC_S_AUDOUT:{
763                 struct v4l2_audioout *vout = arg;
764
765                 return ivtv_get_audio_output(itv, vout->index, vout);
766         }
767
768         case VIDIOC_ENUMINPUT:{
769                 struct v4l2_input *vin = arg;
770
771                 /* set it to defaults from our table */
772                 return ivtv_get_input(itv, vin->index, vin);
773         }
774
775         case VIDIOC_ENUMOUTPUT:{
776                 struct v4l2_output *vout = arg;
777
778                 return ivtv_get_output(itv, vout->index, vout);
779         }
780
781         case VIDIOC_TRY_FMT:
782         case VIDIOC_S_FMT: {
783                 struct v4l2_format *fmt = arg;
784
785                 return ivtv_try_or_set_fmt(itv, id->type, fmt, cmd == VIDIOC_S_FMT);
786         }
787
788         case VIDIOC_G_FMT: {
789                 struct v4l2_format *fmt = arg;
790                 int type = fmt->type;
791
792                 memset(fmt, 0, sizeof(*fmt));
793                 fmt->type = type;
794                 return ivtv_get_fmt(itv, id->type, fmt);
795         }
796
797         case VIDIOC_CROPCAP: {
798                 struct v4l2_cropcap *cropcap = arg;
799
800                 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
801                     cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
802                         return -EINVAL;
803                 cropcap->bounds.top = cropcap->bounds.left = 0;
804                 cropcap->bounds.width = 720;
805                 if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
806                         cropcap->bounds.height = itv->is_50hz ? 576 : 480;
807                         cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
808                         cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
809                 } else {
810                         cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
811                         cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
812                         cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
813                 }
814                 cropcap->defrect = cropcap->bounds;
815                 return 0;
816         }
817
818         case VIDIOC_S_CROP: {
819                 struct v4l2_crop *crop = arg;
820
821                 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
822                     (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
823                         if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
824                                  crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
825                                 itv->main_rect = crop->c;
826                                 return 0;
827                         }
828                         return -EINVAL;
829                 }
830                 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
831                         return -EINVAL;
832                 return itv->video_dec_func(itv, VIDIOC_S_CROP, arg);
833         }
834
835         case VIDIOC_G_CROP: {
836                 struct v4l2_crop *crop = arg;
837
838                 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
839                     (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
840                         crop->c = itv->main_rect;
841                         return 0;
842                 }
843                 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
844                         return -EINVAL;
845                 return itv->video_dec_func(itv, VIDIOC_G_CROP, arg);
846         }
847
848         case VIDIOC_ENUM_FMT: {
849                 static struct v4l2_fmtdesc formats[] = {
850                         { 0, 0, 0,
851                           "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12,
852                           { 0, 0, 0, 0 }
853                         },
854                         { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
855                           "MPEG", V4L2_PIX_FMT_MPEG,
856                           { 0, 0, 0, 0 }
857                         }
858                 };
859                 struct v4l2_fmtdesc *fmt = arg;
860                 enum v4l2_buf_type type = fmt->type;
861
862                 switch (type) {
863                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
864                         break;
865                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
866                         if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
867                                 return -EINVAL;
868                         break;
869                 default:
870                         return -EINVAL;
871                 }
872                 if (fmt->index > 1)
873                         return -EINVAL;
874                 *fmt = formats[fmt->index];
875                 fmt->type = type;
876                 return 0;
877         }
878
879         case VIDIOC_G_INPUT:{
880                 *(int *)arg = itv->active_input;
881                 break;
882         }
883
884         case VIDIOC_S_INPUT:{
885                 int inp = *(int *)arg;
886
887                 if (inp < 0 || inp >= itv->nof_inputs)
888                         return -EINVAL;
889
890                 if (inp == itv->active_input) {
891                         IVTV_DEBUG_INFO("Input unchanged\n");
892                         break;
893                 }
894                 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
895                                 itv->active_input, inp);
896
897                 itv->active_input = inp;
898                 /* Set the audio input to whatever is appropriate for the
899                    input type. */
900                 itv->audio_input = itv->card->video_inputs[inp].audio_index;
901
902                 /* prevent others from messing with the streams until
903                    we're finished changing inputs. */
904                 ivtv_mute(itv);
905                 ivtv_video_set_io(itv);
906                 ivtv_audio_set_io(itv);
907                 ivtv_unmute(itv);
908                 break;
909         }
910
911         case VIDIOC_G_OUTPUT:{
912                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
913                         return -EINVAL;
914                 *(int *)arg = itv->active_output;
915                 break;
916         }
917
918         case VIDIOC_S_OUTPUT:{
919                 int outp = *(int *)arg;
920                 struct v4l2_routing route;
921
922                 if (outp >= itv->card->nof_outputs)
923                         return -EINVAL;
924
925                 if (outp == itv->active_output) {
926                         IVTV_DEBUG_INFO("Output unchanged\n");
927                         break;
928                 }
929                 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
930                            itv->active_output, outp);
931
932                 itv->active_output = outp;
933                 route.input = SAA7127_INPUT_TYPE_NORMAL;
934                 route.output = itv->card->video_outputs[outp].video_output;
935                 ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
936                 break;
937         }
938
939         case VIDIOC_G_FREQUENCY:{
940                 struct v4l2_frequency *vf = arg;
941
942                 if (vf->tuner != 0)
943                         return -EINVAL;
944                 ivtv_call_i2c_clients(itv, cmd, arg);
945                 break;
946         }
947
948         case VIDIOC_S_FREQUENCY:{
949                 struct v4l2_frequency vf = *(struct v4l2_frequency *)arg;
950
951                 if (vf.tuner != 0)
952                         return -EINVAL;
953
954                 ivtv_mute(itv);
955                 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf.frequency);
956                 ivtv_call_i2c_clients(itv, cmd, &vf);
957                 ivtv_unmute(itv);
958                 break;
959         }
960
961         case VIDIOC_ENUMSTD:{
962                 struct v4l2_standard *vs = arg;
963                 int idx = vs->index;
964
965                 if (idx < 0 || idx >= ARRAY_SIZE(enum_stds))
966                         return -EINVAL;
967
968                 *vs = (enum_stds[idx].std & V4L2_STD_525_60) ?
969                                 ivtv_std_60hz : ivtv_std_50hz;
970                 vs->index = idx;
971                 vs->id = enum_stds[idx].std;
972                 strcpy(vs->name, enum_stds[idx].name);
973                 break;
974         }
975
976         case VIDIOC_G_STD:{
977                 *(v4l2_std_id *) arg = itv->std;
978                 break;
979         }
980
981         case VIDIOC_S_STD: {
982                 v4l2_std_id std = *(v4l2_std_id *) arg;
983
984                 if ((std & V4L2_STD_ALL) == 0)
985                         return -EINVAL;
986
987                 if (std == itv->std)
988                         break;
989
990                 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
991                     atomic_read(&itv->capturing) > 0 ||
992                     atomic_read(&itv->decoding) > 0) {
993                         /* Switching standard would turn off the radio or mess
994                            with already running streams, prevent that by
995                            returning EBUSY. */
996                         return -EBUSY;
997                 }
998
999                 itv->std = std;
1000                 itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1001                 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
1002                 itv->params.width = 720;
1003                 itv->params.height = itv->is_50hz ? 576 : 480;
1004                 itv->vbi.count = itv->is_50hz ? 18 : 12;
1005                 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1006                 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1007                 if (itv->hw_flags & IVTV_HW_CX25840) {
1008                         itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1009                 }
1010                 IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
1011
1012                 /* Tuner */
1013                 ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
1014
1015                 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1016                         /* set display standard */
1017                         itv->std_out = std;
1018                         itv->is_out_60hz = itv->is_60hz;
1019                         itv->is_out_50hz = itv->is_50hz;
1020                         ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
1021                         ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1022                         itv->main_rect.left = itv->main_rect.top = 0;
1023                         itv->main_rect.width = 720;
1024                         itv->main_rect.height = itv->params.height;
1025                         ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1026                                 720, itv->main_rect.height, 0, 0);
1027                 }
1028                 break;
1029         }
1030
1031         case VIDIOC_S_TUNER: {  /* Setting tuner can only set audio mode */
1032                 struct v4l2_tuner *vt = arg;
1033
1034                 if (vt->index != 0)
1035                         return -EINVAL;
1036
1037                 ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
1038                 break;
1039         }
1040
1041         case VIDIOC_G_TUNER: {
1042                 struct v4l2_tuner *vt = arg;
1043
1044                 if (vt->index != 0)
1045                         return -EINVAL;
1046
1047                 memset(vt, 0, sizeof(*vt));
1048                 ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
1049
1050                 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1051                         strcpy(vt->name, "ivtv Radio Tuner");
1052                         vt->type = V4L2_TUNER_RADIO;
1053                 } else {
1054                         strcpy(vt->name, "ivtv TV Tuner");
1055                         vt->type = V4L2_TUNER_ANALOG_TV;
1056                 }
1057                 break;
1058         }
1059
1060         case VIDIOC_G_SLICED_VBI_CAP: {
1061                 struct v4l2_sliced_vbi_cap *cap = arg;
1062                 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1063                 int f, l;
1064                 enum v4l2_buf_type type = cap->type;
1065
1066                 memset(cap, 0, sizeof(*cap));
1067                 cap->type = type;
1068                 if (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1069                         for (f = 0; f < 2; f++) {
1070                                 for (l = 0; l < 24; l++) {
1071                                         if (valid_service_line(f, l, itv->is_50hz)) {
1072                                                 cap->service_lines[f][l] = set;
1073                                         }
1074                                 }
1075                         }
1076                         return 0;
1077                 }
1078                 if (type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1079                         if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1080                                 return -EINVAL;
1081                         if (itv->is_60hz) {
1082                                 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1083                                 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1084                         } else {
1085                                 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1086                                 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1087                         }
1088                         return 0;
1089                 }
1090                 return -EINVAL;
1091         }
1092
1093         case VIDIOC_G_ENC_INDEX: {
1094                 struct v4l2_enc_idx *idx = arg;
1095                 int i;
1096
1097                 idx->entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1098                                         IVTV_MAX_PGM_INDEX;
1099                 if (idx->entries > V4L2_ENC_IDX_ENTRIES)
1100                         idx->entries = V4L2_ENC_IDX_ENTRIES;
1101                 for (i = 0; i < idx->entries; i++) {
1102                         idx->entry[i] = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1103                 }
1104                 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1105                 break;
1106         }
1107
1108         case VIDIOC_ENCODER_CMD:
1109         case VIDIOC_TRY_ENCODER_CMD: {
1110                 struct v4l2_encoder_cmd *enc = arg;
1111                 int try = cmd == VIDIOC_TRY_ENCODER_CMD;
1112
1113                 memset(&enc->raw, 0, sizeof(enc->raw));
1114                 switch (enc->cmd) {
1115                 case V4L2_ENC_CMD_START:
1116                         enc->flags = 0;
1117                         if (try)
1118                                 return 0;
1119                         return ivtv_start_capture(id);
1120
1121                 case V4L2_ENC_CMD_STOP:
1122                         enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1123                         if (try)
1124                                 return 0;
1125                         ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1126                         return 0;
1127
1128                 case V4L2_ENC_CMD_PAUSE:
1129                         enc->flags = 0;
1130                         if (try)
1131                                 return 0;
1132                         if (!atomic_read(&itv->capturing))
1133                                 return -EPERM;
1134                         if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1135                                 return 0;
1136                         ivtv_mute(itv);
1137                         ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1138                         break;
1139
1140                 case V4L2_ENC_CMD_RESUME:
1141                         enc->flags = 0;
1142                         if (try)
1143                                 return 0;
1144                         if (!atomic_read(&itv->capturing))
1145                                 return -EPERM;
1146                         if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1147                                 return 0;
1148                         ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1149                         ivtv_unmute(itv);
1150                         break;
1151                 default:
1152                         return -EINVAL;
1153                 }
1154                 break;
1155         }
1156
1157         case VIDIOC_G_FBUF: {
1158                 struct v4l2_framebuffer *fb = arg;
1159
1160                 memset(fb, 0, sizeof(*fb));
1161                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1162                         return -EINVAL;
1163                 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1164                         V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_GLOBAL_ALPHA;
1165                 fb->fmt.pixelformat = itv->osd_pixelformat;
1166                 fb->fmt.width = itv->osd_rect.width;
1167                 fb->fmt.height = itv->osd_rect.height;
1168                 fb->base = (void *)itv->osd_video_pbase;
1169                 if (itv->osd_global_alpha_state)
1170                         fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1171                 if (itv->osd_local_alpha_state)
1172                         fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1173                 if (itv->osd_color_key_state)
1174                         fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1175                 break;
1176         }
1177
1178         case VIDIOC_S_FBUF: {
1179                 struct v4l2_framebuffer *fb = arg;
1180
1181                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1182                         return -EINVAL;
1183                 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1184                 itv->osd_local_alpha_state = (fb->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) != 0;
1185                 itv->osd_color_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1186                 break;
1187         }
1188
1189         case VIDIOC_LOG_STATUS:
1190         {
1191                 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1192                 struct v4l2_input vidin;
1193                 struct v4l2_audio audin;
1194                 int i;
1195
1196                 IVTV_INFO("=================  START STATUS CARD #%d  =================\n", itv->num);
1197                 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1198                         struct tveeprom tv;
1199
1200                         ivtv_read_eeprom(itv, &tv);
1201                 }
1202                 ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
1203                 ivtv_get_input(itv, itv->active_input, &vidin);
1204                 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1205                 IVTV_INFO("Video Input: %s\n", vidin.name);
1206                 IVTV_INFO("Audio Input: %s\n", audin.name);
1207                 if (has_output) {
1208                         struct v4l2_output vidout;
1209                         struct v4l2_audioout audout;
1210                         int mode = itv->output_mode;
1211                         static const char * const output_modes[] = {
1212                                 "None",
1213                                 "MPEG Streaming",
1214                                 "YUV Streaming",
1215                                 "YUV Frames",
1216                                 "Passthrough",
1217                         };
1218
1219                         ivtv_get_output(itv, itv->active_output, &vidout);
1220                         ivtv_get_audio_output(itv, 0, &audout);
1221                         IVTV_INFO("Video Output: %s\n", vidout.name);
1222                         IVTV_INFO("Audio Output: %s\n", audout.name);
1223                         if (mode < 0 || mode > OUT_PASSTHROUGH)
1224                                 mode = OUT_NONE;
1225                         IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1226                 }
1227                 IVTV_INFO("Tuner: %s\n",
1228                         test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1229                 cx2341x_log_status(&itv->params, itv->name);
1230                 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1231                 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1232                         struct ivtv_stream *s = &itv->streams[i];
1233
1234                         if (s->v4l2dev == NULL || s->buffers == 0)
1235                                 continue;
1236                         IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1237                                         (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1238                                         (s->buffers * s->buf_size) / 1024, s->buffers);
1239                 }
1240                 IVTV_INFO("Read MPEG/VBI: %lld/%lld bytes\n", (long long)itv->mpg_data_received, (long long)itv->vbi_data_inserted);
1241                 IVTV_INFO("==================  END STATUS CARD #%d  ==================\n", itv->num);
1242                 break;
1243         }
1244
1245         default:
1246                 return -EINVAL;
1247         }
1248         return 0;
1249 }
1250
1251 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1252 {
1253         struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1254         struct ivtv *itv = id->itv;
1255         int nonblocking = filp->f_flags & O_NONBLOCK;
1256         struct ivtv_stream *s = &itv->streams[id->type];
1257
1258         switch (cmd) {
1259         case IVTV_IOC_DMA_FRAME: {
1260                 struct ivtv_dma_frame *args = arg;
1261
1262                 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1263                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1264                         return -EINVAL;
1265                 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1266                         return -EINVAL;
1267                 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1268                         return 0;
1269                 if (ivtv_claim_stream(id, id->type)) {
1270                         return -EBUSY;
1271                 }
1272                 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1273                         ivtv_release_stream(s);
1274                         return -EBUSY;
1275                 }
1276                 if (args->y_source == NULL)
1277                         return 0;
1278                 return ivtv_yuv_prep_frame(itv, args);
1279         }
1280
1281         case VIDEO_GET_PTS: {
1282                 u32 data[CX2341X_MBOX_MAX_DATA];
1283                 u64 *pts = arg;
1284
1285                 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1286                 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1287                         *pts = s->dma_pts;
1288                         break;
1289                 }
1290                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1291                         return -EINVAL;
1292
1293                 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1294                         *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1295                                         (u64)itv->last_dec_timing[1];
1296                         break;
1297                 }
1298                 *pts = 0;
1299                 if (atomic_read(&itv->decoding)) {
1300                         if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1301                                 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1302                                 return -EIO;
1303                         }
1304                         memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1305                         set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1306                         *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1307                         /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1308                 }
1309                 break;
1310         }
1311
1312         case VIDEO_GET_FRAME_COUNT: {
1313                 u32 data[CX2341X_MBOX_MAX_DATA];
1314                 u64 *frame = arg;
1315
1316                 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1317                 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1318                         *frame = 0;
1319                         break;
1320                 }
1321                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1322                         return -EINVAL;
1323
1324                 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1325                         *frame = itv->last_dec_timing[0];
1326                         break;
1327                 }
1328                 *frame = 0;
1329                 if (atomic_read(&itv->decoding)) {
1330                         if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1331                                 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1332                                 return -EIO;
1333                         }
1334                         memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1335                         set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1336                         *frame = data[0];
1337                 }
1338                 break;
1339         }
1340
1341         case VIDEO_PLAY: {
1342                 struct video_command vc;
1343
1344                 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1345                 memset(&vc, 0, sizeof(vc));
1346                 vc.cmd = VIDEO_CMD_PLAY;
1347                 return ivtv_video_command(itv, id, &vc, 0);
1348         }
1349
1350         case VIDEO_STOP: {
1351                 struct video_command vc;
1352
1353                 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1354                 memset(&vc, 0, sizeof(vc));
1355                 vc.cmd = VIDEO_CMD_STOP;
1356                 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1357                 return ivtv_video_command(itv, id, &vc, 0);
1358         }
1359
1360         case VIDEO_FREEZE: {
1361                 struct video_command vc;
1362
1363                 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1364                 memset(&vc, 0, sizeof(vc));
1365                 vc.cmd = VIDEO_CMD_FREEZE;
1366                 return ivtv_video_command(itv, id, &vc, 0);
1367         }
1368
1369         case VIDEO_CONTINUE: {
1370                 struct video_command vc;
1371
1372                 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1373                 memset(&vc, 0, sizeof(vc));
1374                 vc.cmd = VIDEO_CMD_CONTINUE;
1375                 return ivtv_video_command(itv, id, &vc, 0);
1376         }
1377
1378         case VIDEO_COMMAND:
1379         case VIDEO_TRY_COMMAND: {
1380                 struct video_command *vc = arg;
1381                 int try = (cmd == VIDEO_TRY_COMMAND);
1382
1383                 if (try)
1384                         IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND\n");
1385                 else
1386                         IVTV_DEBUG_IOCTL("VIDEO_COMMAND\n");
1387                 return ivtv_video_command(itv, id, vc, try);
1388         }
1389
1390         case VIDEO_GET_EVENT: {
1391                 struct video_event *ev = arg;
1392                 DEFINE_WAIT(wait);
1393
1394                 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1395                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1396                         return -EINVAL;
1397                 memset(ev, 0, sizeof(*ev));
1398                 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1399
1400                 while (1) {
1401                         if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1402                                 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1403                         else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1404                                 ev->type = VIDEO_EVENT_VSYNC;
1405                                 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1406                                         VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1407                                 if (itv->output_mode == OUT_UDMA_YUV &&
1408                                         (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1409                                                                 IVTV_YUV_MODE_PROGRESSIVE) {
1410                                         ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1411                                 }
1412                         }
1413                         if (ev->type)
1414                                 return 0;
1415                         if (nonblocking)
1416                                 return -EAGAIN;
1417                         /* wait for event */
1418                         prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1419                         if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
1420                                 schedule();
1421                         finish_wait(&itv->event_waitq, &wait);
1422                         if (signal_pending(current)) {
1423                                 /* return if a signal was received */
1424                                 IVTV_DEBUG_INFO("User stopped wait for event\n");
1425                                 return -EINTR;
1426                         }
1427                 }
1428                 break;
1429         }
1430
1431         default:
1432                 return -EINVAL;
1433         }
1434         return 0;
1435 }
1436
1437 static int ivtv_v4l2_do_ioctl(struct inode *inode, struct file *filp,
1438                               unsigned int cmd, void *arg)
1439 {
1440         struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1441         struct ivtv *itv = id->itv;
1442         int ret;
1443
1444         /* check priority */
1445         switch (cmd) {
1446         case VIDIOC_S_CTRL:
1447         case VIDIOC_S_STD:
1448         case VIDIOC_S_INPUT:
1449         case VIDIOC_S_OUTPUT:
1450         case VIDIOC_S_TUNER:
1451         case VIDIOC_S_FREQUENCY:
1452         case VIDIOC_S_FMT:
1453         case VIDIOC_S_CROP:
1454         case VIDIOC_S_AUDIO:
1455         case VIDIOC_S_AUDOUT:
1456         case VIDIOC_S_EXT_CTRLS:
1457         case VIDIOC_S_FBUF:
1458                 ret = v4l2_prio_check(&itv->prio, &id->prio);
1459                 if (ret)
1460                         return ret;
1461         }
1462
1463         switch (cmd) {
1464         case VIDIOC_DBG_G_REGISTER:
1465         case VIDIOC_DBG_S_REGISTER:
1466         case VIDIOC_G_CHIP_IDENT:
1467         case VIDIOC_INT_S_AUDIO_ROUTING:
1468         case VIDIOC_INT_RESET:
1469                 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1470                         printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1471                         v4l_printk_ioctl(cmd);
1472                 }
1473                 return ivtv_debug_ioctls(filp, cmd, arg);
1474
1475         case VIDIOC_G_PRIORITY:
1476         case VIDIOC_S_PRIORITY:
1477         case VIDIOC_QUERYCAP:
1478         case VIDIOC_ENUMINPUT:
1479         case VIDIOC_G_INPUT:
1480         case VIDIOC_S_INPUT:
1481         case VIDIOC_ENUMOUTPUT:
1482         case VIDIOC_G_OUTPUT:
1483         case VIDIOC_S_OUTPUT:
1484         case VIDIOC_G_FMT:
1485         case VIDIOC_S_FMT:
1486         case VIDIOC_TRY_FMT:
1487         case VIDIOC_ENUM_FMT:
1488         case VIDIOC_CROPCAP:
1489         case VIDIOC_G_CROP:
1490         case VIDIOC_S_CROP:
1491         case VIDIOC_G_FREQUENCY:
1492         case VIDIOC_S_FREQUENCY:
1493         case VIDIOC_ENUMSTD:
1494         case VIDIOC_G_STD:
1495         case VIDIOC_S_STD:
1496         case VIDIOC_S_TUNER:
1497         case VIDIOC_G_TUNER:
1498         case VIDIOC_ENUMAUDIO:
1499         case VIDIOC_S_AUDIO:
1500         case VIDIOC_G_AUDIO:
1501         case VIDIOC_ENUMAUDOUT:
1502         case VIDIOC_S_AUDOUT:
1503         case VIDIOC_G_AUDOUT:
1504         case VIDIOC_G_SLICED_VBI_CAP:
1505         case VIDIOC_LOG_STATUS:
1506         case VIDIOC_G_ENC_INDEX:
1507         case VIDIOC_ENCODER_CMD:
1508         case VIDIOC_TRY_ENCODER_CMD:
1509         case VIDIOC_G_FBUF:
1510         case VIDIOC_S_FBUF:
1511                 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1512                         printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1513                         v4l_printk_ioctl(cmd);
1514                 }
1515                 return ivtv_v4l2_ioctls(itv, filp, cmd, arg);
1516
1517         case VIDIOC_QUERYMENU:
1518         case VIDIOC_QUERYCTRL:
1519         case VIDIOC_S_CTRL:
1520         case VIDIOC_G_CTRL:
1521         case VIDIOC_S_EXT_CTRLS:
1522         case VIDIOC_G_EXT_CTRLS:
1523         case VIDIOC_TRY_EXT_CTRLS:
1524                 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1525                         printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1526                         v4l_printk_ioctl(cmd);
1527                 }
1528                 return ivtv_control_ioctls(itv, cmd, arg);
1529
1530         case IVTV_IOC_DMA_FRAME:
1531         case VIDEO_GET_PTS:
1532         case VIDEO_GET_FRAME_COUNT:
1533         case VIDEO_GET_EVENT:
1534         case VIDEO_PLAY:
1535         case VIDEO_STOP:
1536         case VIDEO_FREEZE:
1537         case VIDEO_CONTINUE:
1538         case VIDEO_COMMAND:
1539         case VIDEO_TRY_COMMAND:
1540                 return ivtv_decoder_ioctls(filp, cmd, arg);
1541
1542         case 0x00005401:        /* Handle isatty() calls */
1543                 return -EINVAL;
1544         default:
1545                 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1546                                                    ivtv_v4l2_do_ioctl);
1547         }
1548         return 0;
1549 }
1550
1551 int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1552                     unsigned long arg)
1553 {
1554         struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1555         struct ivtv *itv = id->itv;
1556
1557         /* Filter dvb ioctls that cannot be handled by video_usercopy */
1558         switch (cmd) {
1559         case VIDEO_SELECT_SOURCE:
1560                 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1561                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1562                         return -EINVAL;
1563                 return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
1564
1565         case AUDIO_SET_MUTE:
1566                 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1567                 itv->speed_mute_audio = arg;
1568                 return 0;
1569
1570         case AUDIO_CHANNEL_SELECT:
1571                 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1572                 if (arg > AUDIO_STEREO_SWAPPED)
1573                         return -EINVAL;
1574                 itv->audio_stereo_mode = arg;
1575                 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1576                 return 0;
1577
1578         case AUDIO_BILINGUAL_CHANNEL_SELECT:
1579                 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1580                 if (arg > AUDIO_STEREO_SWAPPED)
1581                         return -EINVAL;
1582                 itv->audio_bilingual_mode = arg;
1583                 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1584                 return 0;
1585
1586         default:
1587                 break;
1588         }
1589         return video_usercopy(inode, filp, cmd, arg, ivtv_v4l2_do_ioctl);
1590 }