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