]> err.no Git - linux-2.6/blob - drivers/media/video/ivtv/ivtv-ioctl.c
6f80941e49ec176768096f09ebacbbe48bdaa848
[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->play.speed = ivtv_validate_speed(itv->speed, vc->play.speed);
281                 if (vc->play.speed < 0)
282                         vc->play.format = VIDEO_PLAY_FMT_GOP;
283                 if (try) break;
284
285                 if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
286                         return -EBUSY;
287                 return ivtv_start_decoding(id, vc->play.speed);
288         }
289
290         case VIDEO_CMD_STOP:
291                 if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
292                         vc->stop.pts = 0;
293                 if (try) break;
294                 if (atomic_read(&itv->decoding) == 0)
295                         return 0;
296                 if (itv->output_mode != OUT_MPG)
297                         return -EBUSY;
298
299                 itv->output_mode = OUT_NONE;
300                 return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);
301
302         case VIDEO_CMD_FREEZE:
303                 if (try) break;
304                 if (itv->output_mode != OUT_MPG)
305                         return -EBUSY;
306                 if (atomic_read(&itv->decoding) > 0) {
307                         ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
308                                 (vc->flags & VIDEO_CMD_FREEZE_TO_BLACK) ? 1 : 0);
309                 }
310                 break;
311
312         case VIDEO_CMD_CONTINUE:
313                 if (try) break;
314                 if (itv->output_mode != OUT_MPG)
315                         return -EBUSY;
316                 if (atomic_read(&itv->decoding) > 0) {
317                         ivtv_vapi(itv, CX2341X_DEC_START_PLAYBACK, 2, 0, 0);
318                 }
319                 break;
320
321         default:
322                 return -EINVAL;
323         }
324         return 0;
325 }
326
327 static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
328 {
329         struct v4l2_register *regs = arg;
330         unsigned long flags;
331         volatile u8 __iomem *reg_start;
332
333         if (!capable(CAP_SYS_ADMIN))
334                 return -EPERM;
335         if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
336                 reg_start = itv->reg_mem - IVTV_REG_OFFSET;
337         else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
338                         regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
339                 reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
340         else if (regs->reg >= 0 && regs->reg < IVTV_ENCODER_SIZE)
341                 reg_start = itv->enc_mem;
342         else
343                 return -EINVAL;
344
345         spin_lock_irqsave(&ivtv_cards_lock, flags);
346         if (cmd == VIDIOC_DBG_G_REGISTER) {
347                 regs->val = readl(regs->reg + reg_start);
348         } else {
349                 writel(regs->val, regs->reg + reg_start);
350         }
351         spin_unlock_irqrestore(&ivtv_cards_lock, flags);
352         return 0;
353 }
354
355 static int ivtv_get_fmt(struct ivtv *itv, int streamtype, struct v4l2_format *fmt)
356 {
357         switch (fmt->type) {
358         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
359                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
360                         return -EINVAL;
361                 fmt->fmt.pix.left = itv->main_rect.left;
362                 fmt->fmt.pix.top = itv->main_rect.top;
363                 fmt->fmt.pix.width = itv->main_rect.width;
364                 fmt->fmt.pix.height = itv->main_rect.height;
365                 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
366                 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
367                 if (itv->output_mode == OUT_UDMA_YUV) {
368                         switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
369                         case IVTV_YUV_MODE_INTERLACED:
370                                 fmt->fmt.pix.field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
371                                         V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
372                                 break;
373                         case IVTV_YUV_MODE_PROGRESSIVE:
374                                 fmt->fmt.pix.field = V4L2_FIELD_NONE;
375                                 break;
376                         default:
377                                 fmt->fmt.pix.field = V4L2_FIELD_ANY;
378                                 break;
379                         }
380                         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
381                         /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
382                         fmt->fmt.pix.sizeimage =
383                                 fmt->fmt.pix.height * fmt->fmt.pix.width +
384                                 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
385                 }
386                 else if (itv->output_mode == OUT_YUV ||
387                                 streamtype == IVTV_ENC_STREAM_TYPE_YUV ||
388                                 streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
389                         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
390                         /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
391                         fmt->fmt.pix.sizeimage =
392                                 fmt->fmt.pix.height * fmt->fmt.pix.width +
393                                 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
394                 } else {
395                         fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
396                         fmt->fmt.pix.sizeimage = 128 * 1024;
397                 }
398                 break;
399
400         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
401                 fmt->fmt.pix.left = 0;
402                 fmt->fmt.pix.top = 0;
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 = fmt->fmt.pix.top;
498                 r.left = fmt->fmt.pix.left;
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.top = r.top;
505                         fmt->fmt.pix.left = r.left;
506                         fmt->fmt.pix.width = r.width;
507                         fmt->fmt.pix.height = r.height;
508                 }
509                 if (itv->output_mode == OUT_UDMA_YUV) {
510                         /* TODO: add checks for validity */
511                         fmt->fmt.pix.field = field;
512                 }
513                 if (set_fmt) {
514                         if (itv->output_mode == OUT_UDMA_YUV) {
515                                 switch (field) {
516                                 case V4L2_FIELD_NONE:
517                                         itv->yuv_info.lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
518                                         break;
519                                 case V4L2_FIELD_ANY:
520                                         itv->yuv_info.lace_mode = IVTV_YUV_MODE_AUTO;
521                                         break;
522                                 case V4L2_FIELD_INTERLACED_BT:
523                                         itv->yuv_info.lace_mode =
524                                                 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
525                                         break;
526                                 case V4L2_FIELD_INTERLACED_TB:
527                                 default:
528                                         itv->yuv_info.lace_mode = IVTV_YUV_MODE_INTERLACED;
529                                         break;
530                                 }
531                                 itv->yuv_info.lace_sync_field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
532
533                                 /* Force update of yuv registers */
534                                 itv->yuv_info.yuv_forced_update = 1;
535                                 return 0;
536                         }
537                         if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
538                                  r.width, r.height, r.left, r.top))
539                                 itv->main_rect = r;
540                         else
541                                 return -EINVAL;
542                 }
543                 return 0;
544         }
545
546         if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY) {
547                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
548                         return -EINVAL;
549                 if (set_fmt) {
550                         itv->osd_color_key = fmt->fmt.win.chromakey;
551                         itv->osd_global_alpha = fmt->fmt.win.global_alpha;
552                         ivtv_set_osd_alpha(itv);
553                 }
554                 return 0;
555         }
556
557         /* set window size */
558         if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
559                 int w = fmt->fmt.pix.width;
560                 int h = fmt->fmt.pix.height;
561
562                 if (w > 720) w = 720;
563                 else if (w < 1) w = 1;
564                 if (h > (itv->is_50hz ? 576 : 480)) h = (itv->is_50hz ? 576 : 480);
565                 else if (h < 2) h = 2;
566                 ivtv_get_fmt(itv, streamtype, fmt);
567                 fmt->fmt.pix.width = w;
568                 fmt->fmt.pix.height = h;
569
570                 if (!set_fmt || (itv->params.width == w && itv->params.height == h))
571                         return 0;
572                 if (atomic_read(&itv->capturing) > 0)
573                         return -EBUSY;
574
575                 itv->params.width = w;
576                 itv->params.height = h;
577                 if (w != 720 || h != (itv->is_50hz ? 576 : 480))
578                         itv->params.video_temporal_filter = 0;
579                 else
580                         itv->params.video_temporal_filter = 8;
581                 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
582                 return ivtv_get_fmt(itv, streamtype, fmt);
583         }
584
585         /* set raw VBI format */
586         if (fmt->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
587                 if (set_fmt && streamtype == IVTV_ENC_STREAM_TYPE_VBI &&
588                     itv->vbi.sliced_in->service_set &&
589                     atomic_read(&itv->capturing) > 0) {
590                         return -EBUSY;
591                 }
592                 if (set_fmt) {
593                         itv->vbi.sliced_in->service_set = 0;
594                         itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
595                 }
596                 return ivtv_get_fmt(itv, streamtype, fmt);
597         }
598
599         /* set sliced VBI output
600            In principle the user could request that only certain
601            VBI types are output and that the others are ignored.
602            I.e., suppress CC in the even fields or only output
603            WSS and no VPS. Currently though there is no choice. */
604         if (fmt->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
605                 return ivtv_get_fmt(itv, streamtype, fmt);
606
607         /* any else but sliced VBI capture is an error */
608         if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
609                 return -EINVAL;
610
611         if (streamtype == IVTV_DEC_STREAM_TYPE_VBI)
612                 return ivtv_get_fmt(itv, streamtype, fmt);
613
614         /* set sliced VBI capture format */
615         vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
616         memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
617
618         if (vbifmt->service_set)
619                 expand_service_set(vbifmt, itv->is_50hz);
620         set = check_service_set(vbifmt, itv->is_50hz);
621         vbifmt->service_set = get_service_set(vbifmt);
622
623         if (!set_fmt)
624                 return 0;
625         if (set == 0)
626                 return -EINVAL;
627         if (atomic_read(&itv->capturing) > 0 && itv->vbi.sliced_in->service_set == 0) {
628                 return -EBUSY;
629         }
630         itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
631         memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
632         return 0;
633 }
634
635 static int ivtv_internal_ioctls(struct file *filp, unsigned int cmd, void *arg)
636 {
637         struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
638         struct ivtv *itv = id->itv;
639         struct v4l2_register *reg = arg;
640
641         switch (cmd) {
642         /* ioctls to allow direct access to the encoder registers for testing */
643         case VIDIOC_DBG_G_REGISTER:
644                 IVTV_DEBUG_IOCTL("VIDIOC_DBG_G_REGISTER\n");
645                 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
646                         return ivtv_itvc(itv, cmd, arg);
647                 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
648                         return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
649                 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
650
651         case VIDIOC_DBG_S_REGISTER:
652                 IVTV_DEBUG_IOCTL("VIDIOC_DBG_S_REGISTER\n");
653                 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
654                         return ivtv_itvc(itv, cmd, arg);
655                 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
656                         return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
657                 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
658
659         case VIDIOC_G_CHIP_IDENT: {
660                 struct v4l2_chip_ident *chip = arg;
661
662                 IVTV_DEBUG_IOCTL("VIDIOC_G_CHIP_IDENT\n");
663                 chip->ident = V4L2_IDENT_NONE;
664                 chip->revision = 0;
665                 if (reg->match_type == V4L2_CHIP_MATCH_HOST) {
666                         if (v4l2_chip_match_host(reg->match_type, reg->match_chip)) {
667                                 struct v4l2_chip_ident *chip = arg;
668
669                                 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
670                         }
671                         return 0;
672                 }
673                 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
674                         return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
675                 if (reg->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
676                         return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
677                 return -EINVAL;
678         }
679
680         case VIDIOC_INT_S_AUDIO_ROUTING: {
681                 struct v4l2_routing *route = arg;
682
683                 IVTV_DEBUG_IOCTL("VIDIOC_INT_S_AUDIO_ROUTING\n");
684                 ivtv_audio_set_route(itv, route);
685                 break;
686         }
687
688         case VIDIOC_INT_RESET:
689                 IVTV_DEBUG_IOCTL("VIDIOC_INT_RESET\n");
690                 ivtv_reset_ir_gpio(itv);
691                 break;
692
693         default:
694                 return -EINVAL;
695         }
696         return 0;
697 }
698
699 int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void *arg)
700 {
701         struct ivtv_open_id *id = NULL;
702
703         if (filp) id = (struct ivtv_open_id *)filp->private_data;
704
705         switch (cmd) {
706         case VIDIOC_G_PRIORITY:
707         {
708                 enum v4l2_priority *p = arg;
709
710                 *p = v4l2_prio_max(&itv->prio);
711                 break;
712         }
713
714         case VIDIOC_S_PRIORITY:
715         {
716                 enum v4l2_priority *prio = arg;
717
718                 return v4l2_prio_change(&itv->prio, &id->prio, *prio);
719         }
720
721         case VIDIOC_QUERYCAP:{
722                 struct v4l2_capability *vcap = arg;
723
724                 IVTV_DEBUG_IOCTL("VIDIOC_QUERYCAP\n");
725
726                 memset(vcap, 0, sizeof(*vcap));
727                 strcpy(vcap->driver, IVTV_DRIVER_NAME);     /* driver name */
728                 strcpy(vcap->card, itv->card_name);         /* card type */
729                 strcpy(vcap->bus_info, pci_name(itv->dev)); /* bus info... */
730                 vcap->version = IVTV_DRIVER_VERSION;        /* version */
731                 vcap->capabilities = itv->v4l2_cap;         /* capabilities */
732
733                 /* reserved.. must set to 0! */
734                 vcap->reserved[0] = vcap->reserved[1] =
735                         vcap->reserved[2] = vcap->reserved[3] = 0;
736                 break;
737         }
738
739         case VIDIOC_ENUMAUDIO:{
740                 struct v4l2_audio *vin = arg;
741
742                 IVTV_DEBUG_IOCTL("VIDIOC_ENUMAUDIO\n");
743
744                 return ivtv_get_audio_input(itv, vin->index, vin);
745         }
746
747         case VIDIOC_G_AUDIO:{
748                 struct v4l2_audio *vin = arg;
749
750                 IVTV_DEBUG_IOCTL("VIDIOC_G_AUDIO\n");
751                 vin->index = itv->audio_input;
752                 return ivtv_get_audio_input(itv, vin->index, vin);
753         }
754
755         case VIDIOC_S_AUDIO:{
756                 struct v4l2_audio *vout = arg;
757
758                 IVTV_DEBUG_IOCTL("VIDIOC_S_AUDIO\n");
759
760                 if (vout->index >= itv->nof_audio_inputs)
761                         return -EINVAL;
762                 itv->audio_input = vout->index;
763                 ivtv_audio_set_io(itv);
764                 break;
765         }
766
767         case VIDIOC_ENUMAUDOUT:{
768                 struct v4l2_audioout *vin = arg;
769
770                 IVTV_DEBUG_IOCTL("VIDIOC_ENUMAUDOUT\n");
771
772                 /* set it to defaults from our table */
773                 return ivtv_get_audio_output(itv, vin->index, vin);
774         }
775
776         case VIDIOC_G_AUDOUT:{
777                 struct v4l2_audioout *vin = arg;
778
779                 IVTV_DEBUG_IOCTL("VIDIOC_G_AUDOUT\n");
780                 vin->index = 0;
781                 return ivtv_get_audio_output(itv, vin->index, vin);
782         }
783
784         case VIDIOC_S_AUDOUT:{
785                 struct v4l2_audioout *vout = arg;
786
787                 IVTV_DEBUG_IOCTL("VIDIOC_S_AUDOUT\n");
788
789                 return ivtv_get_audio_output(itv, vout->index, vout);
790         }
791
792         case VIDIOC_ENUMINPUT:{
793                 struct v4l2_input *vin = arg;
794
795                 IVTV_DEBUG_IOCTL("VIDIOC_ENUMINPUT\n");
796
797                 /* set it to defaults from our table */
798                 return ivtv_get_input(itv, vin->index, vin);
799         }
800
801         case VIDIOC_ENUMOUTPUT:{
802                 struct v4l2_output *vout = arg;
803
804                 IVTV_DEBUG_IOCTL("VIDIOC_ENUMOUTPUT\n");
805
806                 return ivtv_get_output(itv, vout->index, vout);
807         }
808
809         case VIDIOC_TRY_FMT:
810         case VIDIOC_S_FMT: {
811                 struct v4l2_format *fmt = arg;
812
813                 if (cmd == VIDIOC_S_FMT) {
814                         IVTV_DEBUG_IOCTL("VIDIOC_S_FMT\n");
815                 } else {
816                         IVTV_DEBUG_IOCTL("VIDIOC_TRY_FMT\n");
817                 }
818                 return ivtv_try_or_set_fmt(itv, id->type, fmt, cmd == VIDIOC_S_FMT);
819         }
820
821         case VIDIOC_G_FMT: {
822                 struct v4l2_format *fmt = arg;
823                 int type = fmt->type;
824
825                 IVTV_DEBUG_IOCTL("VIDIOC_G_FMT\n");
826                 memset(fmt, 0, sizeof(*fmt));
827                 fmt->type = type;
828                 return ivtv_get_fmt(itv, id->type, fmt);
829         }
830
831         case VIDIOC_S_CROP: {
832                 struct v4l2_crop *crop = arg;
833
834                 IVTV_DEBUG_IOCTL("VIDIOC_S_CROP\n");
835                 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
836                         return -EINVAL;
837                 return itv->video_dec_func(itv, VIDIOC_S_CROP, arg);
838         }
839
840         case VIDIOC_G_CROP: {
841                 struct v4l2_crop *crop = arg;
842
843                 IVTV_DEBUG_IOCTL("VIDIOC_G_CROP\n");
844                 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
845                         return -EINVAL;
846                 return itv->video_dec_func(itv, VIDIOC_G_CROP, arg);
847         }
848
849         case VIDIOC_ENUM_FMT: {
850                 static struct v4l2_fmtdesc formats[] = {
851                         { 0, 0, 0,
852                           "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12,
853                           { 0, 0, 0, 0 }
854                         },
855                         { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
856                           "MPEG", V4L2_PIX_FMT_MPEG,
857                           { 0, 0, 0, 0 }
858                         }
859                 };
860                 struct v4l2_fmtdesc *fmt = arg;
861                 enum v4l2_buf_type type = fmt->type;
862
863                 switch (type) {
864                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
865                         break;
866                 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
867                         if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
868                                 return -EINVAL;
869                         break;
870                 default:
871                         return -EINVAL;
872                 }
873                 if (fmt->index > 1)
874                         return -EINVAL;
875                 *fmt = formats[fmt->index];
876                 fmt->type = type;
877                 return 0;
878         }
879
880         case VIDIOC_G_INPUT:{
881                 IVTV_DEBUG_IOCTL("VIDIOC_G_INPUT\n");
882
883                 *(int *)arg = itv->active_input;
884                 break;
885         }
886
887         case VIDIOC_S_INPUT:{
888                 int inp = *(int *)arg;
889
890                 IVTV_DEBUG_IOCTL("VIDIOC_S_INPUT\n");
891
892                 if (inp < 0 || inp >= itv->nof_inputs)
893                         return -EINVAL;
894
895                 if (inp == itv->active_input) {
896                         IVTV_DEBUG_INFO("Input unchanged\n");
897                         break;
898                 }
899                 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
900                                 itv->active_input, inp);
901
902                 itv->active_input = inp;
903                 /* Set the audio input to whatever is appropriate for the
904                    input type. */
905                 itv->audio_input = itv->card->video_inputs[inp].audio_index;
906
907                 /* prevent others from messing with the streams until
908                    we're finished changing inputs. */
909                 ivtv_mute(itv);
910                 ivtv_video_set_io(itv);
911                 ivtv_audio_set_io(itv);
912                 ivtv_unmute(itv);
913                 break;
914         }
915
916         case VIDIOC_G_OUTPUT:{
917                 IVTV_DEBUG_IOCTL("VIDIOC_G_OUTPUT\n");
918
919                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
920                         return -EINVAL;
921                 *(int *)arg = itv->active_output;
922                 break;
923         }
924
925         case VIDIOC_S_OUTPUT:{
926                 int outp = *(int *)arg;
927                 struct v4l2_routing route;
928
929                 IVTV_DEBUG_IOCTL("VIDIOC_S_OUTPUT\n");
930
931                 if (outp >= itv->card->nof_outputs)
932                         return -EINVAL;
933
934                 if (outp == itv->active_output) {
935                         IVTV_DEBUG_INFO("Output unchanged\n");
936                         break;
937                 }
938                 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
939                            itv->active_output, outp);
940
941                 itv->active_output = outp;
942                 route.input = SAA7127_INPUT_TYPE_NORMAL;
943                 route.output = itv->card->video_outputs[outp].video_output;
944                 ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
945                 break;
946         }
947
948         case VIDIOC_G_FREQUENCY:{
949                 struct v4l2_frequency *vf = arg;
950
951                 IVTV_DEBUG_IOCTL("VIDIOC_G_FREQUENCY\n");
952
953                 if (vf->tuner != 0)
954                         return -EINVAL;
955                 ivtv_call_i2c_clients(itv, cmd, arg);
956                 break;
957         }
958
959         case VIDIOC_S_FREQUENCY:{
960                 struct v4l2_frequency vf = *(struct v4l2_frequency *)arg;
961
962                 IVTV_DEBUG_IOCTL("VIDIOC_S_FREQUENCY\n");
963
964                 if (vf.tuner != 0)
965                         return -EINVAL;
966
967                 ivtv_mute(itv);
968                 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf.frequency);
969                 ivtv_call_i2c_clients(itv, cmd, &vf);
970                 ivtv_unmute(itv);
971                 break;
972         }
973
974         case VIDIOC_ENUMSTD:{
975                 struct v4l2_standard *vs = arg;
976                 int idx = vs->index;
977
978                 IVTV_DEBUG_IOCTL("VIDIOC_ENUMSTD\n");
979
980                 if (idx < 0 || idx >= ARRAY_SIZE(enum_stds))
981                         return -EINVAL;
982
983                 *vs = (enum_stds[idx].std & V4L2_STD_525_60) ?
984                                 ivtv_std_60hz : ivtv_std_50hz;
985                 vs->index = idx;
986                 vs->id = enum_stds[idx].std;
987                 strcpy(vs->name, enum_stds[idx].name);
988                 break;
989         }
990
991         case VIDIOC_G_STD:{
992                 IVTV_DEBUG_IOCTL("VIDIOC_G_STD\n");
993                 *(v4l2_std_id *) arg = itv->std;
994                 break;
995         }
996
997         case VIDIOC_S_STD: {
998                 v4l2_std_id std = *(v4l2_std_id *) arg;
999
1000                 IVTV_DEBUG_IOCTL("VIDIOC_S_STD\n");
1001
1002                 if ((std & V4L2_STD_ALL) == 0)
1003                         return -EINVAL;
1004
1005                 if (std == itv->std)
1006                         break;
1007
1008                 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1009                     atomic_read(&itv->capturing) > 0 ||
1010                     atomic_read(&itv->decoding) > 0) {
1011                         /* Switching standard would turn off the radio or mess
1012                            with already running streams, prevent that by
1013                            returning EBUSY. */
1014                         return -EBUSY;
1015                 }
1016
1017                 itv->std = std;
1018                 itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1019                 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
1020                 itv->params.width = 720;
1021                 itv->params.height = itv->is_50hz ? 576 : 480;
1022                 itv->vbi.count = itv->is_50hz ? 18 : 12;
1023                 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1024                 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1025                 if (itv->hw_flags & IVTV_HW_CX25840) {
1026                         itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1027                 }
1028                 IVTV_DEBUG_INFO("Switching standard to %llx.\n", itv->std);
1029
1030                 /* Tuner */
1031                 ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
1032
1033                 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1034                         /* set display standard */
1035                         itv->std_out = std;
1036                         itv->is_out_60hz = itv->is_60hz;
1037                         itv->is_out_50hz = itv->is_50hz;
1038                         ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
1039                         ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1040                         itv->main_rect.left = itv->main_rect.top = 0;
1041                         itv->main_rect.width = 720;
1042                         itv->main_rect.height = itv->params.height;
1043                         ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1044                                 720, itv->main_rect.height, 0, 0);
1045                 }
1046                 break;
1047         }
1048
1049         case VIDIOC_S_TUNER: {  /* Setting tuner can only set audio mode */
1050                 struct v4l2_tuner *vt = arg;
1051
1052                 IVTV_DEBUG_IOCTL("VIDIOC_S_TUNER\n");
1053
1054                 if (vt->index != 0)
1055                         return -EINVAL;
1056
1057                 ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
1058                 break;
1059         }
1060
1061         case VIDIOC_G_TUNER: {
1062                 struct v4l2_tuner *vt = arg;
1063
1064                 IVTV_DEBUG_IOCTL("VIDIOC_G_TUNER\n");
1065
1066                 if (vt->index != 0)
1067                         return -EINVAL;
1068
1069                 memset(vt, 0, sizeof(*vt));
1070                 ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
1071
1072                 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1073                         strcpy(vt->name, "ivtv Radio Tuner");
1074                         vt->type = V4L2_TUNER_RADIO;
1075                 } else {
1076                         strcpy(vt->name, "ivtv TV Tuner");
1077                         vt->type = V4L2_TUNER_ANALOG_TV;
1078                 }
1079                 break;
1080         }
1081
1082         case VIDIOC_G_SLICED_VBI_CAP: {
1083                 struct v4l2_sliced_vbi_cap *cap = arg;
1084                 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1085                 int f, l;
1086                 enum v4l2_buf_type type = cap->type;
1087
1088                 IVTV_DEBUG_IOCTL("VIDIOC_G_SLICED_VBI_CAP\n");
1089                 memset(cap, 0, sizeof(*cap));
1090                 cap->type = type;
1091                 if (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1092                         for (f = 0; f < 2; f++) {
1093                                 for (l = 0; l < 24; l++) {
1094                                         if (valid_service_line(f, l, itv->is_50hz)) {
1095                                                 cap->service_lines[f][l] = set;
1096                                         }
1097                                 }
1098                         }
1099                         return 0;
1100                 }
1101                 if (type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1102                         if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1103                                 return -EINVAL;
1104                         if (itv->is_60hz) {
1105                                 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1106                                 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1107                         } else {
1108                                 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1109                                 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1110                         }
1111                         return 0;
1112                 }
1113                 return -EINVAL;
1114         }
1115
1116         case VIDIOC_LOG_STATUS:
1117         {
1118                 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1119                 struct v4l2_input vidin;
1120                 struct v4l2_audio audin;
1121                 int i;
1122
1123                 IVTV_INFO("=================  START STATUS CARD #%d  =================\n", itv->num);
1124                 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1125                         struct tveeprom tv;
1126
1127                         ivtv_read_eeprom(itv, &tv);
1128                 }
1129                 ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
1130                 ivtv_get_input(itv, itv->active_input, &vidin);
1131                 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1132                 IVTV_INFO("Video Input: %s\n", vidin.name);
1133                 IVTV_INFO("Audio Input: %s\n", audin.name);
1134                 if (has_output) {
1135                         struct v4l2_output vidout;
1136                         struct v4l2_audioout audout;
1137                         int mode = itv->output_mode;
1138                         static const char * const output_modes[] = {
1139                                 "None",
1140                                 "MPEG Streaming",
1141                                 "YUV Streaming",
1142                                 "YUV Frames",
1143                                 "Passthrough",
1144                         };
1145
1146                         ivtv_get_output(itv, itv->active_output, &vidout);
1147                         ivtv_get_audio_output(itv, 0, &audout);
1148                         IVTV_INFO("Video Output: %s\n", vidout.name);
1149                         IVTV_INFO("Audio Output: %s\n", audout.name);
1150                         if (mode < 0 || mode > OUT_PASSTHROUGH)
1151                                 mode = OUT_NONE;
1152                         IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1153                 }
1154                 IVTV_INFO("Tuner: %s\n",
1155                         test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1156                 cx2341x_log_status(&itv->params, itv->name);
1157                 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1158                 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1159                         struct ivtv_stream *s = &itv->streams[i];
1160
1161                         if (s->v4l2dev == NULL || s->buffers == 0)
1162                                 continue;
1163                         IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1164                                         (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1165                                         (s->buffers * s->buf_size) / 1024, s->buffers);
1166                 }
1167                 IVTV_INFO("Read MPEG/VBI: %lld/%lld bytes\n", itv->mpg_data_received, itv->vbi_data_inserted);
1168                 IVTV_INFO("==================  END STATUS CARD #%d  ==================\n", itv->num);
1169                 break;
1170         }
1171
1172         default:
1173                 return -EINVAL;
1174         }
1175         return 0;
1176 }
1177
1178 static int ivtv_ivtv_ioctls(struct file *filp, unsigned int cmd, void *arg)
1179 {
1180         struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1181         struct ivtv *itv = id->itv;
1182         int nonblocking = filp->f_flags & O_NONBLOCK;
1183         struct ivtv_stream *s = &itv->streams[id->type];
1184
1185         switch (cmd) {
1186         case IVTV_IOC_DMA_FRAME: {
1187                 struct ivtv_dma_frame *args = arg;
1188
1189                 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1190                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1191                         return -EINVAL;
1192                 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1193                         return -EINVAL;
1194                 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1195                         return 0;
1196                 if (ivtv_claim_stream(id, id->type)) {
1197                         return -EBUSY;
1198                 }
1199                 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1200                         ivtv_release_stream(s);
1201                         return -EBUSY;
1202                 }
1203                 if (args->y_source == NULL)
1204                         return 0;
1205                 return ivtv_yuv_prep_frame(itv, args);
1206         }
1207
1208         case VIDEO_GET_PTS: {
1209                 u32 data[CX2341X_MBOX_MAX_DATA];
1210                 u64 *pts = arg;
1211
1212                 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1213                 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1214                         *pts = s->dma_pts;
1215                         break;
1216                 }
1217                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1218                         return -EINVAL;
1219
1220                 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1221                         *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1222                                         (u64)itv->last_dec_timing[1];
1223                         break;
1224                 }
1225                 *pts = 0;
1226                 if (atomic_read(&itv->decoding)) {
1227                         if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1228                                 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1229                                 return -EIO;
1230                         }
1231                         memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1232                         set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1233                         *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1234                         /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1235                 }
1236                 break;
1237         }
1238
1239         case VIDEO_GET_FRAME_COUNT: {
1240                 u32 data[CX2341X_MBOX_MAX_DATA];
1241                 u64 *frame = arg;
1242
1243                 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1244                 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1245                         *frame = 0;
1246                         break;
1247                 }
1248                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1249                         return -EINVAL;
1250
1251                 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1252                         *frame = itv->last_dec_timing[0];
1253                         break;
1254                 }
1255                 *frame = 0;
1256                 if (atomic_read(&itv->decoding)) {
1257                         if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1258                                 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1259                                 return -EIO;
1260                         }
1261                         memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1262                         set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1263                         *frame = data[0];
1264                 }
1265                 break;
1266         }
1267
1268         case VIDEO_PLAY: {
1269                 struct video_command vc;
1270
1271                 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1272                 memset(&vc, 0, sizeof(vc));
1273                 vc.cmd = VIDEO_CMD_PLAY;
1274                 return ivtv_video_command(itv, id, &vc, 0);
1275         }
1276
1277         case VIDEO_STOP: {
1278                 struct video_command vc;
1279
1280                 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1281                 memset(&vc, 0, sizeof(vc));
1282                 vc.cmd = VIDEO_CMD_STOP;
1283                 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1284                 return ivtv_video_command(itv, id, &vc, 0);
1285         }
1286
1287         case VIDEO_FREEZE: {
1288                 struct video_command vc;
1289
1290                 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1291                 memset(&vc, 0, sizeof(vc));
1292                 vc.cmd = VIDEO_CMD_FREEZE;
1293                 return ivtv_video_command(itv, id, &vc, 0);
1294         }
1295
1296         case VIDEO_CONTINUE: {
1297                 struct video_command vc;
1298
1299                 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1300                 memset(&vc, 0, sizeof(vc));
1301                 vc.cmd = VIDEO_CMD_CONTINUE;
1302                 return ivtv_video_command(itv, id, &vc, 0);
1303         }
1304
1305         case VIDEO_COMMAND:
1306         case VIDEO_TRY_COMMAND: {
1307                 struct video_command *vc = arg;
1308                 int try = (cmd == VIDEO_TRY_COMMAND);
1309
1310                 if (try)
1311                         IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND\n");
1312                 else
1313                         IVTV_DEBUG_IOCTL("VIDEO_COMMAND\n");
1314                 return ivtv_video_command(itv, id, vc, try);
1315         }
1316
1317         case VIDEO_GET_EVENT: {
1318                 struct video_event *ev = arg;
1319                 DEFINE_WAIT(wait);
1320
1321                 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1322                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1323                         return -EINVAL;
1324                 memset(ev, 0, sizeof(*ev));
1325                 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1326
1327                 while (1) {
1328                         if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1329                                 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1330                         else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1331                                 ev->type = VIDEO_EVENT_VSYNC;
1332                                 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1333                                         VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1334                                 if (itv->output_mode == OUT_UDMA_YUV &&
1335                                         (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1336                                                                 IVTV_YUV_MODE_PROGRESSIVE) {
1337                                         ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1338                                 }
1339                         }
1340                         if (ev->type)
1341                                 return 0;
1342                         if (nonblocking)
1343                                 return -EAGAIN;
1344                         /* wait for event */
1345                         prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1346                         if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
1347                                 schedule();
1348                         finish_wait(&itv->event_waitq, &wait);
1349                         if (signal_pending(current)) {
1350                                 /* return if a signal was received */
1351                                 IVTV_DEBUG_INFO("User stopped wait for event\n");
1352                                 return -EINTR;
1353                         }
1354                 }
1355                 break;
1356         }
1357
1358         case VIDIOC_G_ENC_INDEX: {
1359                 struct v4l2_enc_idx *idx = arg;
1360                 int i;
1361
1362                 IVTV_DEBUG_IOCTL("VIDIOC_G_ENC_INDEX\n");
1363                 idx->entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1364                                         IVTV_MAX_PGM_INDEX;
1365                 if (idx->entries > V4L2_ENC_IDX_ENTRIES)
1366                         idx->entries = V4L2_ENC_IDX_ENTRIES;
1367                 for (i = 0; i < idx->entries; i++) {
1368                         idx->entry[i] = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1369                 }
1370                 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1371                 break;
1372         }
1373
1374         case VIDIOC_ENCODER_CMD:
1375         case VIDIOC_TRY_ENCODER_CMD: {
1376                 struct v4l2_encoder_cmd *enc = arg;
1377                 int try = cmd == VIDIOC_TRY_ENCODER_CMD;
1378
1379                 if (try)
1380                         IVTV_DEBUG_IOCTL("VIDIOC_TRY_ENCODER_CMD\n");
1381                 else
1382                         IVTV_DEBUG_IOCTL("VIDIOC_ENCODER_CMD\n");
1383                 switch (enc->cmd) {
1384                 case V4L2_ENC_CMD_START:
1385                         return ivtv_start_capture(id);
1386
1387                 case V4L2_ENC_CMD_STOP:
1388                         ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1389                         return 0;
1390
1391                 case V4L2_ENC_CMD_PAUSE:
1392                         if (!atomic_read(&itv->capturing))
1393                                 return -EPERM;
1394                         if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1395                                 return 0;
1396                         ivtv_mute(itv);
1397                         ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1398                         break;
1399
1400                 case V4L2_ENC_CMD_RESUME:
1401                         if (!atomic_read(&itv->capturing))
1402                                 return -EPERM;
1403                         if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1404                                 return 0;
1405                         ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1406                         ivtv_unmute(itv);
1407                         break;
1408                 }
1409                 break;
1410         }
1411
1412         case VIDIOC_G_FBUF: {
1413                 struct v4l2_framebuffer *fb = arg;
1414
1415                 IVTV_DEBUG_IOCTL("VIDIOC_G_FBUF\n");
1416                 memset(fb, 0, sizeof(*fb));
1417                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1418                         break;
1419                 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1420                         V4L2_FBUF_CAP_LOCAL_ALPHA | V4L2_FBUF_CAP_GLOBAL_ALPHA;
1421                 fb->fmt.pixelformat = itv->osd_pixelformat;
1422                 fb->fmt.width = itv->osd_rect.width;
1423                 fb->fmt.height = itv->osd_rect.height;
1424                 fb->fmt.left = itv->osd_rect.left;
1425                 fb->fmt.top = itv->osd_rect.top;
1426                 fb->base = (void *)itv->osd_video_pbase;
1427                 if (itv->osd_global_alpha_state)
1428                         fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1429                 if (itv->osd_local_alpha_state)
1430                         fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1431                 if (itv->osd_color_key_state)
1432                         fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1433                 break;
1434         }
1435
1436         case VIDIOC_S_FBUF: {
1437                 struct v4l2_framebuffer *fb = arg;
1438
1439                 IVTV_DEBUG_IOCTL("VIDIOC_S_FBUF\n");
1440                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1441                         break;
1442                 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1443                 itv->osd_local_alpha_state = (fb->flags & V4L2_FBUF_FLAG_LOCAL_ALPHA) != 0;
1444                 itv->osd_color_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1445                 break;
1446         }
1447
1448         default:
1449                 return -EINVAL;
1450         }
1451         return 0;
1452 }
1453
1454 static int ivtv_v4l2_do_ioctl(struct inode *inode, struct file *filp,
1455                               unsigned int cmd, void *arg)
1456 {
1457         struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1458         struct ivtv *itv = id->itv;
1459         int ret;
1460
1461         IVTV_DEBUG_IOCTL("v4l2 ioctl 0x%08x\n", cmd);
1462
1463         /* check priority */
1464         switch (cmd) {
1465         case VIDIOC_S_CTRL:
1466         case VIDIOC_S_STD:
1467         case VIDIOC_S_INPUT:
1468         case VIDIOC_S_OUTPUT:
1469         case VIDIOC_S_TUNER:
1470         case VIDIOC_S_FREQUENCY:
1471         case VIDIOC_S_FMT:
1472         case VIDIOC_S_CROP:
1473         case VIDIOC_S_AUDIO:
1474         case VIDIOC_S_AUDOUT:
1475         case VIDIOC_S_EXT_CTRLS:
1476         case VIDIOC_S_FBUF:
1477                 ret = v4l2_prio_check(&itv->prio, &id->prio);
1478                 if (ret)
1479                         return ret;
1480         }
1481
1482         switch (cmd) {
1483         case VIDIOC_DBG_G_REGISTER:
1484         case VIDIOC_DBG_S_REGISTER:
1485         case VIDIOC_G_CHIP_IDENT:
1486         case VIDIOC_INT_S_AUDIO_ROUTING:
1487         case VIDIOC_INT_RESET:
1488                 return ivtv_internal_ioctls(filp, cmd, arg);
1489
1490         case VIDIOC_G_PRIORITY:
1491         case VIDIOC_S_PRIORITY:
1492         case VIDIOC_QUERYCAP:
1493         case VIDIOC_ENUMINPUT:
1494         case VIDIOC_G_INPUT:
1495         case VIDIOC_S_INPUT:
1496         case VIDIOC_ENUMOUTPUT:
1497         case VIDIOC_G_OUTPUT:
1498         case VIDIOC_S_OUTPUT:
1499         case VIDIOC_G_FMT:
1500         case VIDIOC_S_FMT:
1501         case VIDIOC_TRY_FMT:
1502         case VIDIOC_ENUM_FMT:
1503         case VIDIOC_G_CROP:
1504         case VIDIOC_S_CROP:
1505         case VIDIOC_G_FREQUENCY:
1506         case VIDIOC_S_FREQUENCY:
1507         case VIDIOC_ENUMSTD:
1508         case VIDIOC_G_STD:
1509         case VIDIOC_S_STD:
1510         case VIDIOC_S_TUNER:
1511         case VIDIOC_G_TUNER:
1512         case VIDIOC_ENUMAUDIO:
1513         case VIDIOC_S_AUDIO:
1514         case VIDIOC_G_AUDIO:
1515         case VIDIOC_ENUMAUDOUT:
1516         case VIDIOC_S_AUDOUT:
1517         case VIDIOC_G_AUDOUT:
1518         case VIDIOC_G_SLICED_VBI_CAP:
1519         case VIDIOC_LOG_STATUS:
1520                 return ivtv_v4l2_ioctls(itv, filp, cmd, arg);
1521
1522         case VIDIOC_QUERYMENU:
1523         case VIDIOC_QUERYCTRL:
1524         case VIDIOC_S_CTRL:
1525         case VIDIOC_G_CTRL:
1526         case VIDIOC_S_EXT_CTRLS:
1527         case VIDIOC_G_EXT_CTRLS:
1528         case VIDIOC_TRY_EXT_CTRLS:
1529                 return ivtv_control_ioctls(itv, cmd, arg);
1530
1531         case IVTV_IOC_DMA_FRAME:
1532         case VIDEO_GET_PTS:
1533         case VIDEO_GET_FRAME_COUNT:
1534         case VIDEO_GET_EVENT:
1535         case VIDEO_PLAY:
1536         case VIDEO_STOP:
1537         case VIDEO_FREEZE:
1538         case VIDEO_CONTINUE:
1539         case VIDEO_COMMAND:
1540         case VIDEO_TRY_COMMAND:
1541         case VIDIOC_G_ENC_INDEX:
1542         case VIDIOC_ENCODER_CMD:
1543         case VIDIOC_TRY_ENCODER_CMD:
1544         case VIDIOC_G_FBUF:
1545         case VIDIOC_S_FBUF:
1546                 return ivtv_ivtv_ioctls(filp, cmd, arg);
1547
1548         case 0x00005401:        /* Handle isatty() calls */
1549                 return -EINVAL;
1550         default:
1551                 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1552                                                    ivtv_v4l2_do_ioctl);
1553         }
1554         return 0;
1555 }
1556
1557 int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1558                     unsigned long arg)
1559 {
1560         struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1561         struct ivtv *itv = id->itv;
1562
1563         /* Filter dvb ioctls that cannot be handled by video_usercopy */
1564         switch (cmd) {
1565         case VIDEO_SELECT_SOURCE:
1566                 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1567                 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1568                         return -EINVAL;
1569                 return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
1570
1571         case AUDIO_SET_MUTE:
1572                 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1573                 itv->speed_mute_audio = arg;
1574                 return 0;
1575
1576         case AUDIO_CHANNEL_SELECT:
1577                 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1578                 if (arg > AUDIO_STEREO_SWAPPED)
1579                         return -EINVAL;
1580                 itv->audio_stereo_mode = arg;
1581                 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1582                 return 0;
1583
1584         case AUDIO_BILINGUAL_CHANNEL_SELECT:
1585                 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1586                 if (arg > AUDIO_STEREO_SWAPPED)
1587                         return -EINVAL;
1588                 itv->audio_bilingual_mode = arg;
1589                 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1590                 return 0;
1591
1592         default:
1593                 break;
1594         }
1595         return video_usercopy(inode, filp, cmd, arg, ivtv_v4l2_do_ioctl);
1596 }