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