V4L/DVB (8079): ivtv: Convert to video_ioctl2.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / ivtv / ivtv-ioctl.c
blob49b297e788dbe3e7b51b7e7179a68e94da5ea835
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>
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.
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.
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
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>
41 u16 ivtv_service2vbi(int type)
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;
57 static int valid_service_line(int field, int line, int is_pal)
59 return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
60 (!is_pal && line >= 10 && line < 22);
63 static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
65 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
66 int i;
68 set = set & valid_set;
69 if (set == 0 || !valid_service_line(field, line, is_pal)) {
70 return 0;
72 if (!is_pal) {
73 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
74 return V4L2_SLICED_CAPTION_525;
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;
84 for (i = 0; i < 32; i++) {
85 if ((1 << i) & set)
86 return 1 << i;
88 return 0;
91 void ivtv_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
93 u16 set = fmt->service_set;
94 int f, l;
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);
104 static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
106 int f, l;
107 u16 set = 0;
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];
115 return set != 0;
118 u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt)
120 int f, l;
121 u16 set = 0;
123 for (f = 0; f < 2; f++) {
124 for (l = 0; l < 24; l++) {
125 set |= fmt->service_lines[f][l];
128 return set;
131 static const struct {
132 v4l2_std_id std;
133 char *name;
134 } enum_stds[] = {
135 { V4L2_STD_PAL_BG | V4L2_STD_PAL_H, "PAL-BGH" },
136 { V4L2_STD_PAL_DK, "PAL-DK" },
137 { V4L2_STD_PAL_I, "PAL-I" },
138 { V4L2_STD_PAL_M, "PAL-M" },
139 { V4L2_STD_PAL_N, "PAL-N" },
140 { V4L2_STD_PAL_Nc, "PAL-Nc" },
141 { V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H, "SECAM-BGH" },
142 { V4L2_STD_SECAM_DK, "SECAM-DK" },
143 { V4L2_STD_SECAM_L, "SECAM-L" },
144 { V4L2_STD_SECAM_LC, "SECAM-L'" },
145 { V4L2_STD_NTSC_M, "NTSC-M" },
146 { V4L2_STD_NTSC_M_JP, "NTSC-J" },
147 { V4L2_STD_NTSC_M_KR, "NTSC-K" },
150 static const struct v4l2_standard ivtv_std_60hz =
152 .frameperiod = {.numerator = 1001, .denominator = 30000},
153 .framelines = 525,
156 static const struct v4l2_standard ivtv_std_50hz =
158 .frameperiod = {.numerator = 1, .denominator = 25},
159 .framelines = 625,
162 void ivtv_set_osd_alpha(struct ivtv *itv)
164 ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
165 itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
166 ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
169 int ivtv_set_speed(struct ivtv *itv, int speed)
171 u32 data[CX2341X_MBOX_MAX_DATA];
172 struct ivtv_stream *s;
173 int single_step = (speed == 1 || speed == -1);
174 DEFINE_WAIT(wait);
176 if (speed == 0) speed = 1000;
178 /* No change? */
179 if (speed == itv->speed && !single_step)
180 return 0;
182 s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
184 if (single_step && (speed < 0) == (itv->speed < 0)) {
185 /* Single step video and no need to change direction */
186 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
187 itv->speed = speed;
188 return 0;
190 if (single_step)
191 /* Need to change direction */
192 speed = speed < 0 ? -1000 : 1000;
194 data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
195 data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
196 data[1] = (speed < 0);
197 data[2] = speed < 0 ? 3 : 7;
198 data[3] = itv->params.video_b_frames;
199 data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
200 data[5] = 0;
201 data[6] = 0;
203 if (speed == 1500 || speed == -1500) data[0] |= 1;
204 else if (speed == 2000 || speed == -2000) data[0] |= 2;
205 else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
206 else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
208 /* If not decoding, just change speed setting */
209 if (atomic_read(&itv->decoding) > 0) {
210 int got_sig = 0;
212 /* Stop all DMA and decoding activity */
213 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
215 /* Wait for any DMA to finish */
216 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
217 while (itv->i_flags & IVTV_F_I_DMA) {
218 got_sig = signal_pending(current);
219 if (got_sig)
220 break;
221 got_sig = 0;
222 schedule();
224 finish_wait(&itv->dma_waitq, &wait);
225 if (got_sig)
226 return -EINTR;
228 /* Change Speed safely */
229 ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
230 IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
231 data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
233 if (single_step) {
234 speed = (speed < 0) ? -1 : 1;
235 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
237 itv->speed = speed;
238 return 0;
241 static int ivtv_validate_speed(int cur_speed, int new_speed)
243 int fact = new_speed < 0 ? -1 : 1;
244 int s;
246 if (cur_speed == 0)
247 cur_speed = 1000;
248 if (new_speed < 0)
249 new_speed = -new_speed;
250 if (cur_speed < 0)
251 cur_speed = -cur_speed;
253 if (cur_speed <= new_speed) {
254 if (new_speed > 1500)
255 return fact * 2000;
256 if (new_speed > 1000)
257 return fact * 1500;
259 else {
260 if (new_speed >= 2000)
261 return fact * 2000;
262 if (new_speed >= 1500)
263 return fact * 1500;
264 if (new_speed >= 1000)
265 return fact * 1000;
267 if (new_speed == 0)
268 return 1000;
269 if (new_speed == 1 || new_speed == 1000)
270 return fact * new_speed;
272 s = new_speed;
273 new_speed = 1000 / new_speed;
274 if (1000 / cur_speed == new_speed)
275 new_speed += (cur_speed < s) ? -1 : 1;
276 if (new_speed > 60) return 1000 / (fact * 60);
277 return 1000 / (fact * new_speed);
280 static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
281 struct video_command *vc, int try)
283 struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
285 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
286 return -EINVAL;
288 switch (vc->cmd) {
289 case VIDEO_CMD_PLAY: {
290 vc->flags = 0;
291 vc->play.speed = ivtv_validate_speed(itv->speed, vc->play.speed);
292 if (vc->play.speed < 0)
293 vc->play.format = VIDEO_PLAY_FMT_GOP;
294 if (try) break;
296 if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
297 return -EBUSY;
298 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
299 /* forces ivtv_set_speed to be called */
300 itv->speed = 0;
302 return ivtv_start_decoding(id, vc->play.speed);
305 case VIDEO_CMD_STOP:
306 vc->flags &= VIDEO_CMD_STOP_IMMEDIATELY|VIDEO_CMD_STOP_TO_BLACK;
307 if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
308 vc->stop.pts = 0;
309 if (try) break;
310 if (atomic_read(&itv->decoding) == 0)
311 return 0;
312 if (itv->output_mode != OUT_MPG)
313 return -EBUSY;
315 itv->output_mode = OUT_NONE;
316 return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);
318 case VIDEO_CMD_FREEZE:
319 vc->flags &= VIDEO_CMD_FREEZE_TO_BLACK;
320 if (try) break;
321 if (itv->output_mode != OUT_MPG)
322 return -EBUSY;
323 if (atomic_read(&itv->decoding) > 0) {
324 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
325 (vc->flags & VIDEO_CMD_FREEZE_TO_BLACK) ? 1 : 0);
326 set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
328 break;
330 case VIDEO_CMD_CONTINUE:
331 vc->flags = 0;
332 if (try) break;
333 if (itv->output_mode != OUT_MPG)
334 return -EBUSY;
335 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
336 int speed = itv->speed;
337 itv->speed = 0;
338 return ivtv_start_decoding(id, speed);
340 break;
342 default:
343 return -EINVAL;
345 return 0;
348 static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
350 struct v4l2_register *regs = arg;
351 unsigned long flags;
352 volatile u8 __iomem *reg_start;
354 if (!capable(CAP_SYS_ADMIN))
355 return -EPERM;
356 if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
357 reg_start = itv->reg_mem - IVTV_REG_OFFSET;
358 else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
359 regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
360 reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
361 else if (regs->reg >= 0 && regs->reg < IVTV_ENCODER_SIZE)
362 reg_start = itv->enc_mem;
363 else
364 return -EINVAL;
366 spin_lock_irqsave(&ivtv_cards_lock, flags);
367 if (cmd == VIDIOC_DBG_G_REGISTER) {
368 regs->val = readl(regs->reg + reg_start);
369 } else {
370 writel(regs->val, regs->reg + reg_start);
372 spin_unlock_irqrestore(&ivtv_cards_lock, flags);
373 return 0;
376 static int ivtv_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
378 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
379 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
381 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
382 return -EINVAL;
383 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
384 if (itv->is_60hz) {
385 vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
386 vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
387 } else {
388 vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
389 vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
391 vbifmt->service_set = ivtv_get_service_set(vbifmt);
392 return 0;
395 static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
397 struct ivtv_open_id *id = fh;
398 struct ivtv *itv = id->itv;
400 fmt->fmt.pix.width = itv->params.width;
401 fmt->fmt.pix.height = itv->params.height;
402 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
403 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
404 if (id->type == IVTV_ENC_STREAM_TYPE_YUV ||
405 id->type == IVTV_DEC_STREAM_TYPE_YUV) {
406 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
407 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
408 fmt->fmt.pix.sizeimage =
409 fmt->fmt.pix.height * fmt->fmt.pix.width +
410 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
411 } else {
412 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
413 fmt->fmt.pix.sizeimage = 128 * 1024;
415 return 0;
418 static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
420 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
422 fmt->fmt.vbi.sampling_rate = 27000000;
423 fmt->fmt.vbi.offset = 248;
424 fmt->fmt.vbi.samples_per_line = itv->vbi.raw_decoder_line_size - 4;
425 fmt->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
426 fmt->fmt.vbi.start[0] = itv->vbi.start[0];
427 fmt->fmt.vbi.start[1] = itv->vbi.start[1];
428 fmt->fmt.vbi.count[0] = fmt->fmt.vbi.count[1] = itv->vbi.count;
429 return 0;
432 static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
434 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
435 struct ivtv_open_id *id = fh;
436 struct ivtv *itv = id->itv;
438 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
440 if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
441 vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
442 V4L2_SLICED_VBI_525;
443 ivtv_expand_service_set(vbifmt, itv->is_50hz);
444 return 0;
447 itv->video_dec_func(itv, VIDIOC_G_FMT, fmt);
448 vbifmt->service_set = ivtv_get_service_set(vbifmt);
449 return 0;
452 static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
454 struct ivtv_open_id *id = fh;
455 struct ivtv *itv = id->itv;
457 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
458 return -EINVAL;
459 fmt->fmt.pix.width = itv->main_rect.width;
460 fmt->fmt.pix.height = itv->main_rect.height;
461 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
462 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
463 if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
464 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
465 case IVTV_YUV_MODE_INTERLACED:
466 fmt->fmt.pix.field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
467 V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
468 break;
469 case IVTV_YUV_MODE_PROGRESSIVE:
470 fmt->fmt.pix.field = V4L2_FIELD_NONE;
471 break;
472 default:
473 fmt->fmt.pix.field = V4L2_FIELD_ANY;
474 break;
476 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
477 fmt->fmt.pix.bytesperline = 720;
478 fmt->fmt.pix.width = itv->yuv_info.v4l2_src_w;
479 fmt->fmt.pix.height = itv->yuv_info.v4l2_src_h;
480 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
481 fmt->fmt.pix.sizeimage =
482 1080 * ((fmt->fmt.pix.height + 31) & ~31);
483 } else if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
484 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
485 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
486 fmt->fmt.pix.sizeimage =
487 fmt->fmt.pix.height * fmt->fmt.pix.width +
488 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
489 } else {
490 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
491 fmt->fmt.pix.sizeimage = 128 * 1024;
493 return 0;
496 static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
498 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
500 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
501 return -EINVAL;
502 fmt->fmt.win.chromakey = itv->osd_chroma_key;
503 fmt->fmt.win.global_alpha = itv->osd_global_alpha;
504 return 0;
507 static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
509 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
512 static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
514 struct ivtv_open_id *id = fh;
515 struct ivtv *itv = id->itv;
516 int w = fmt->fmt.pix.width;
517 int h = fmt->fmt.pix.height;
519 w = min(w, 720);
520 w = max(w, 1);
521 h = min(h, itv->is_50hz ? 576 : 480);
522 h = max(h, 2);
523 ivtv_g_fmt_vid_cap(file, fh, fmt);
524 fmt->fmt.pix.width = w;
525 fmt->fmt.pix.height = h;
526 return 0;
529 static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
531 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
534 static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
536 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
537 struct ivtv_open_id *id = fh;
538 struct ivtv *itv = id->itv;
540 if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
541 return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
543 /* set sliced VBI capture format */
544 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
545 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
547 if (vbifmt->service_set)
548 ivtv_expand_service_set(vbifmt, itv->is_50hz);
549 check_service_set(vbifmt, itv->is_50hz);
550 vbifmt->service_set = ivtv_get_service_set(vbifmt);
551 return 0;
554 static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
556 struct ivtv_open_id *id = fh;
557 s32 w, h;
558 int field;
559 int ret;
561 w = fmt->fmt.pix.width;
562 h = fmt->fmt.pix.height;
563 field = fmt->fmt.pix.field;
564 ret = ivtv_g_fmt_vid_out(file, fh, fmt);
565 fmt->fmt.pix.width = w;
566 fmt->fmt.pix.height = h;
567 if (!ret && id->type == IVTV_DEC_STREAM_TYPE_YUV) {
568 fmt->fmt.pix.field = field;
569 if (fmt->fmt.pix.width < 2)
570 fmt->fmt.pix.width = 2;
571 if (fmt->fmt.pix.width > 720)
572 fmt->fmt.pix.width = 720;
573 if (fmt->fmt.pix.height < 2)
574 fmt->fmt.pix.height = 2;
575 if (fmt->fmt.pix.height > 576)
576 fmt->fmt.pix.height = 576;
578 return ret;
581 static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
583 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
585 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
586 return -EINVAL;
587 return 0;
590 static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
592 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
595 static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
597 struct ivtv_open_id *id = fh;
598 struct ivtv *itv = id->itv;
599 struct cx2341x_mpeg_params *p = &itv->params;
600 int w = fmt->fmt.pix.width;
601 int h = fmt->fmt.pix.height;
602 int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
604 if (ret)
605 return ret;
607 if (p->width == w && p->height == h)
608 return 0;
610 if (atomic_read(&itv->capturing) > 0)
611 return -EBUSY;
613 p->width = w;
614 p->height = h;
615 if (w != 720 || h != (itv->is_50hz ? 576 : 480))
616 p->video_temporal_filter = 0;
617 else
618 p->video_temporal_filter = 8;
619 if (p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
620 fmt->fmt.pix.width /= 2;
621 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
622 return ivtv_g_fmt_vid_cap(file, fh, fmt);
625 static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
627 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
629 itv->vbi.sliced_in->service_set = 0;
630 itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
631 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
634 static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
636 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
637 struct ivtv_open_id *id = fh;
638 struct ivtv *itv = id->itv;
639 int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
641 if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
642 return ret;
644 if (check_service_set(vbifmt, itv->is_50hz) == 0)
645 return -EINVAL;
646 if (atomic_read(&itv->capturing) > 0)
647 return -EBUSY;
648 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
649 memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
650 return 0;
653 static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
655 struct ivtv_open_id *id = fh;
656 struct ivtv *itv = id->itv;
657 struct yuv_playback_info *yi = &itv->yuv_info;
658 int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
660 if (ret)
661 return ret;
663 if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
664 return 0;
666 /* Return now if we already have some frame data */
667 if (yi->stream_size)
668 return -EBUSY;
670 yi->v4l2_src_w = fmt->fmt.pix.width;
671 yi->v4l2_src_h = fmt->fmt.pix.height;
673 switch (fmt->fmt.pix.field) {
674 case V4L2_FIELD_NONE:
675 yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
676 break;
677 case V4L2_FIELD_ANY:
678 yi->lace_mode = IVTV_YUV_MODE_AUTO;
679 break;
680 case V4L2_FIELD_INTERLACED_BT:
681 yi->lace_mode =
682 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
683 break;
684 case V4L2_FIELD_INTERLACED_TB:
685 default:
686 yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
687 break;
689 yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
691 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
692 itv->dma_data_req_size =
693 1080 * ((yi->v4l2_src_h + 31) & ~31);
695 /* Force update of yuv registers */
696 yi->yuv_forced_update = 1;
697 return 0;
700 static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
702 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
703 int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
705 if (ret == 0) {
706 itv->osd_chroma_key = fmt->fmt.win.chromakey;
707 itv->osd_global_alpha = fmt->fmt.win.global_alpha;
708 ivtv_set_osd_alpha(itv);
710 return ret;
713 static int ivtv_g_chip_ident(struct file *file, void *fh, struct v4l2_chip_ident *chip)
715 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
717 chip->ident = V4L2_IDENT_NONE;
718 chip->revision = 0;
719 if (chip->match_type == V4L2_CHIP_MATCH_HOST) {
720 if (v4l2_chip_match_host(chip->match_type, chip->match_chip))
721 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
722 return 0;
724 if (chip->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
725 return ivtv_i2c_id(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
726 if (chip->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
727 return ivtv_call_i2c_client(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
728 return -EINVAL;
731 static int ivtv_g_register(struct file *file, void *fh, struct v4l2_register *reg)
733 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
735 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
736 return ivtv_itvc(itv, VIDIOC_DBG_G_REGISTER, reg);
737 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
738 return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
739 return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
742 static int ivtv_s_register(struct file *file, void *fh, struct v4l2_register *reg)
744 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
746 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
747 return ivtv_itvc(itv, VIDIOC_DBG_S_REGISTER, reg);
748 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
749 return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
750 return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
753 static int ivtv_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
755 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
757 *p = v4l2_prio_max(&itv->prio);
759 return 0;
762 static int ivtv_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
764 struct ivtv_open_id *id = fh;
765 struct ivtv *itv = id->itv;
767 return v4l2_prio_change(&itv->prio, &id->prio, prio);
770 static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
772 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
774 memset(vcap, 0, sizeof(*vcap));
775 strlcpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
776 strlcpy(vcap->card, itv->card_name, sizeof(vcap->card));
777 strlcpy(vcap->bus_info, pci_name(itv->dev), sizeof(vcap->bus_info));
778 vcap->version = IVTV_DRIVER_VERSION; /* version */
779 vcap->capabilities = itv->v4l2_cap; /* capabilities */
780 /* reserved.. must set to 0! */
781 vcap->reserved[0] = vcap->reserved[1] =
782 vcap->reserved[2] = vcap->reserved[3] = 0;
783 return 0;
786 static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
788 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
790 return ivtv_get_audio_input(itv, vin->index, vin);
793 static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
795 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
797 vin->index = itv->audio_input;
798 return ivtv_get_audio_input(itv, vin->index, vin);
801 static int ivtv_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
803 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
805 if (vout->index >= itv->nof_audio_inputs)
806 return -EINVAL;
808 itv->audio_input = vout->index;
809 ivtv_audio_set_io(itv);
811 return 0;
814 static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
816 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
818 /* set it to defaults from our table */
819 return ivtv_get_audio_output(itv, vin->index, vin);
822 static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
824 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
826 vin->index = 0;
827 return ivtv_get_audio_output(itv, vin->index, vin);
830 static int ivtv_s_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
832 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
834 return ivtv_get_audio_output(itv, vout->index, vout);
837 static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
839 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
841 /* set it to defaults from our table */
842 return ivtv_get_input(itv, vin->index, vin);
845 static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
847 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
849 return ivtv_get_output(itv, vout->index, vout);
852 static int ivtv_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
854 struct ivtv_open_id *id = fh;
855 struct ivtv *itv = id->itv;
856 struct yuv_playback_info *yi = &itv->yuv_info;
857 int streamtype;
859 streamtype = id->type;
861 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
862 return -EINVAL;
863 cropcap->bounds.top = cropcap->bounds.left = 0;
864 cropcap->bounds.width = 720;
865 if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
866 cropcap->bounds.height = itv->is_50hz ? 576 : 480;
867 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
868 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
869 } else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
870 if (yi->track_osd) {
871 cropcap->bounds.width = yi->osd_full_w;
872 cropcap->bounds.height = yi->osd_full_h;
873 } else {
874 cropcap->bounds.width = 720;
875 cropcap->bounds.height =
876 itv->is_out_50hz ? 576 : 480;
878 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
879 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
880 } else {
881 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
882 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
883 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
885 cropcap->defrect = cropcap->bounds;
886 return 0;
889 static int ivtv_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
891 struct ivtv_open_id *id = fh;
892 struct ivtv *itv = id->itv;
893 struct yuv_playback_info *yi = &itv->yuv_info;
894 int streamtype;
896 streamtype = id->type;
898 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
899 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
900 /* Should be replaced */
901 /* v4l_printk_ioctl(VIDIOC_S_CROP); */
904 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
905 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
906 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
907 yi->main_rect = crop->c;
908 return 0;
909 } else {
910 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
911 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
912 itv->main_rect = crop->c;
913 return 0;
916 return -EINVAL;
918 return -EINVAL;
921 static int ivtv_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
923 struct ivtv_open_id *id = fh;
924 struct ivtv *itv = id->itv;
925 struct yuv_playback_info *yi = &itv->yuv_info;
926 int streamtype;
928 streamtype = id->type;
930 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
931 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
932 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
933 crop->c = yi->main_rect;
934 else
935 crop->c = itv->main_rect;
936 return 0;
938 return -EINVAL;
941 static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
943 static struct v4l2_fmtdesc formats[] = {
944 { 0, 0, 0,
945 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
946 { 0, 0, 0, 0 }
948 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
949 "MPEG", V4L2_PIX_FMT_MPEG,
950 { 0, 0, 0, 0 }
953 enum v4l2_buf_type type = fmt->type;
955 if (fmt->index > 1)
956 return -EINVAL;
958 *fmt = formats[fmt->index];
959 fmt->type = type;
960 return 0;
963 static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
965 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
967 static struct v4l2_fmtdesc formats[] = {
968 { 0, 0, 0,
969 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
970 { 0, 0, 0, 0 }
972 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
973 "MPEG", V4L2_PIX_FMT_MPEG,
974 { 0, 0, 0, 0 }
977 enum v4l2_buf_type type = fmt->type;
979 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
980 return -EINVAL;
982 if (fmt->index > 1)
983 return -EINVAL;
985 *fmt = formats[fmt->index];
986 fmt->type = type;
988 return 0;
991 static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
993 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
995 *i = itv->active_input;
997 return 0;
1000 int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
1002 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1004 if (inp < 0 || inp >= itv->nof_inputs)
1005 return -EINVAL;
1007 if (inp == itv->active_input) {
1008 IVTV_DEBUG_INFO("Input unchanged\n");
1009 return 0;
1012 if (atomic_read(&itv->capturing) > 0) {
1013 return -EBUSY;
1016 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
1017 itv->active_input, inp);
1019 itv->active_input = inp;
1020 /* Set the audio input to whatever is appropriate for the
1021 input type. */
1022 itv->audio_input = itv->card->video_inputs[inp].audio_index;
1024 /* prevent others from messing with the streams until
1025 we're finished changing inputs. */
1026 ivtv_mute(itv);
1027 ivtv_video_set_io(itv);
1028 ivtv_audio_set_io(itv);
1029 ivtv_unmute(itv);
1031 return 0;
1034 static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1036 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1038 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1039 return -EINVAL;
1041 *i = itv->active_output;
1043 return 0;
1046 static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1048 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1049 struct v4l2_routing route;
1051 if (outp >= itv->card->nof_outputs)
1052 return -EINVAL;
1054 if (outp == itv->active_output) {
1055 IVTV_DEBUG_INFO("Output unchanged\n");
1056 return 0;
1058 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1059 itv->active_output, outp);
1061 itv->active_output = outp;
1062 route.input = SAA7127_INPUT_TYPE_NORMAL;
1063 route.output = itv->card->video_outputs[outp].video_output;
1064 ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1066 return 0;
1069 static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1071 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1073 if (vf->tuner != 0)
1074 return -EINVAL;
1076 ivtv_call_i2c_clients(itv, VIDIOC_G_FREQUENCY, vf);
1077 return 0;
1080 int ivtv_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1082 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1084 if (vf->tuner != 0)
1085 return -EINVAL;
1087 ivtv_mute(itv);
1088 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1089 ivtv_call_i2c_clients(itv, VIDIOC_S_FREQUENCY, vf);
1090 ivtv_unmute(itv);
1091 return 0;
1094 static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1096 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1098 *std = itv->std;
1099 return 0;
1102 int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
1104 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1105 struct yuv_playback_info *yi = &itv->yuv_info;
1107 if ((*std & V4L2_STD_ALL) == 0)
1108 return -EINVAL;
1110 if (*std == itv->std)
1111 return 0;
1113 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1114 atomic_read(&itv->capturing) > 0 ||
1115 atomic_read(&itv->decoding) > 0) {
1116 /* Switching standard would turn off the radio or mess
1117 with already running streams, prevent that by
1118 returning EBUSY. */
1119 return -EBUSY;
1122 itv->std = *std;
1123 itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
1124 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
1125 itv->params.width = 720;
1126 itv->params.height = itv->is_50hz ? 576 : 480;
1127 itv->vbi.count = itv->is_50hz ? 18 : 12;
1128 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1129 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1131 if (itv->hw_flags & IVTV_HW_CX25840)
1132 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1134 IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
1136 /* Tuner */
1137 ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
1139 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1140 /* set display standard */
1141 itv->std_out = *std;
1142 itv->is_out_60hz = itv->is_60hz;
1143 itv->is_out_50hz = itv->is_50hz;
1144 ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
1145 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1146 itv->main_rect.left = itv->main_rect.top = 0;
1147 itv->main_rect.width = 720;
1148 itv->main_rect.height = itv->params.height;
1149 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1150 720, itv->main_rect.height, 0, 0);
1151 yi->main_rect = itv->main_rect;
1152 if (!itv->osd_info) {
1153 yi->osd_full_w = 720;
1154 yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1157 return 0;
1160 static int ivtv_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1162 struct ivtv_open_id *id = fh;
1163 struct ivtv *itv = id->itv;
1165 if (vt->index != 0)
1166 return -EINVAL;
1168 ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
1170 return 0;
1173 static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1175 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1177 if (vt->index != 0)
1178 return -EINVAL;
1180 memset(vt, 0, sizeof(*vt));
1181 ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
1183 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1184 strlcpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1185 vt->type = V4L2_TUNER_RADIO;
1186 } else {
1187 strlcpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1188 vt->type = V4L2_TUNER_ANALOG_TV;
1191 return 0;
1194 static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1196 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1197 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1198 int f, l;
1199 enum v4l2_buf_type type = cap->type;
1201 memset(cap, 0, sizeof(*cap));
1202 cap->type = type;
1203 if (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1204 for (f = 0; f < 2; f++) {
1205 for (l = 0; l < 24; l++) {
1206 if (valid_service_line(f, l, itv->is_50hz))
1207 cap->service_lines[f][l] = set;
1210 return 0;
1212 if (type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1213 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1214 return -EINVAL;
1215 if (itv->is_60hz) {
1216 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1217 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1218 } else {
1219 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1220 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1222 return 0;
1224 return -EINVAL;
1227 static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1229 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1230 struct v4l2_enc_idx_entry *e = idx->entry;
1231 int entries;
1232 int i;
1234 entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1235 IVTV_MAX_PGM_INDEX;
1236 if (entries > V4L2_ENC_IDX_ENTRIES)
1237 entries = V4L2_ENC_IDX_ENTRIES;
1238 idx->entries = 0;
1239 for (i = 0; i < entries; i++) {
1240 *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1241 if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1242 idx->entries++;
1243 e++;
1246 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1247 return 0;
1250 static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1252 struct ivtv_open_id *id = fh;
1253 struct ivtv *itv = id->itv;
1255 memset(&enc->raw, 0, sizeof(enc->raw));
1257 switch (enc->cmd) {
1258 case V4L2_ENC_CMD_START:
1259 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1260 enc->flags = 0;
1261 return ivtv_start_capture(id);
1263 case V4L2_ENC_CMD_STOP:
1264 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1265 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1266 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1267 return 0;
1269 case V4L2_ENC_CMD_PAUSE:
1270 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1271 enc->flags = 0;
1273 if (!atomic_read(&itv->capturing))
1274 return -EPERM;
1275 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1276 return 0;
1278 ivtv_mute(itv);
1279 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1280 break;
1282 case V4L2_ENC_CMD_RESUME:
1283 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1284 enc->flags = 0;
1286 if (!atomic_read(&itv->capturing))
1287 return -EPERM;
1289 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1290 return 0;
1292 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1293 ivtv_unmute(itv);
1294 break;
1295 default:
1296 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1297 return -EINVAL;
1300 return 0;
1303 static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1305 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1307 memset(&enc->raw, 0, sizeof(enc->raw));
1309 switch (enc->cmd) {
1310 case V4L2_ENC_CMD_START:
1311 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1312 enc->flags = 0;
1313 return 0;
1315 case V4L2_ENC_CMD_STOP:
1316 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1317 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1318 return 0;
1320 case V4L2_ENC_CMD_PAUSE:
1321 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1322 enc->flags = 0;
1323 return 0;
1325 case V4L2_ENC_CMD_RESUME:
1326 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1327 enc->flags = 0;
1328 return 0;
1329 default:
1330 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1331 return -EINVAL;
1335 static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1337 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1338 u32 data[CX2341X_MBOX_MAX_DATA];
1339 struct yuv_playback_info *yi = &itv->yuv_info;
1341 int pixfmt;
1342 static u32 pixel_format[16] = {
1343 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1344 V4L2_PIX_FMT_RGB565,
1345 V4L2_PIX_FMT_RGB555,
1346 V4L2_PIX_FMT_RGB444,
1347 V4L2_PIX_FMT_RGB32,
1351 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1352 V4L2_PIX_FMT_YUV565,
1353 V4L2_PIX_FMT_YUV555,
1354 V4L2_PIX_FMT_YUV444,
1355 V4L2_PIX_FMT_YUV32,
1361 memset(fb, 0, sizeof(*fb));
1363 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1364 return -EINVAL;
1366 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1367 V4L2_FBUF_CAP_GLOBAL_ALPHA;
1369 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1370 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1371 pixfmt = (data[0] >> 3) & 0xf;
1373 fb->fmt.pixelformat = pixel_format[pixfmt];
1374 fb->fmt.width = itv->osd_rect.width;
1375 fb->fmt.height = itv->osd_rect.height;
1376 fb->base = (void *)itv->osd_video_pbase;
1378 if (itv->osd_chroma_key_state)
1379 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1381 if (itv->osd_global_alpha_state)
1382 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1384 pixfmt &= 7;
1386 /* no local alpha for RGB565 or unknown formats */
1387 if (pixfmt == 1 || pixfmt > 4)
1388 return 0;
1390 /* 16-bit formats have inverted local alpha */
1391 if (pixfmt == 2 || pixfmt == 3)
1392 fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1393 else
1394 fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1396 if (itv->osd_local_alpha_state) {
1397 /* 16-bit formats have inverted local alpha */
1398 if (pixfmt == 2 || pixfmt == 3)
1399 fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1400 else
1401 fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1403 if (yi->track_osd)
1404 fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1406 return 0;
1409 static int ivtv_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1411 struct ivtv_open_id *id = fh;
1412 struct ivtv *itv = id->itv;
1413 struct yuv_playback_info *yi = &itv->yuv_info;
1415 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1416 return -EINVAL;
1418 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1419 itv->osd_local_alpha_state =
1420 (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1421 itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1422 ivtv_set_osd_alpha(itv);
1423 yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1425 return 0;
1428 static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1430 struct ivtv_open_id *id = fh;
1431 struct ivtv *itv = id->itv;
1433 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1434 return -EINVAL;
1436 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1438 return 0;
1441 static int ivtv_log_status(struct file *file, void *fh)
1443 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1444 u32 data[CX2341X_MBOX_MAX_DATA];
1446 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1447 struct v4l2_input vidin;
1448 struct v4l2_audio audin;
1449 int i;
1451 IVTV_INFO("================= START STATUS CARD #%d =================\n", itv->num);
1452 IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1453 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1454 struct tveeprom tv;
1456 ivtv_read_eeprom(itv, &tv);
1458 ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
1459 ivtv_get_input(itv, itv->active_input, &vidin);
1460 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1461 IVTV_INFO("Video Input: %s\n", vidin.name);
1462 IVTV_INFO("Audio Input: %s%s\n", audin.name,
1463 (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
1464 if (has_output) {
1465 struct v4l2_output vidout;
1466 struct v4l2_audioout audout;
1467 int mode = itv->output_mode;
1468 static const char * const output_modes[5] = {
1469 "None",
1470 "MPEG Streaming",
1471 "YUV Streaming",
1472 "YUV Frames",
1473 "Passthrough",
1475 static const char * const audio_modes[5] = {
1476 "Stereo",
1477 "Left",
1478 "Right",
1479 "Mono",
1480 "Swapped"
1482 static const char * const alpha_mode[4] = {
1483 "None",
1484 "Global",
1485 "Local",
1486 "Global and Local"
1488 static const char * const pixel_format[16] = {
1489 "ARGB Indexed",
1490 "RGB 5:6:5",
1491 "ARGB 1:5:5:5",
1492 "ARGB 1:4:4:4",
1493 "ARGB 8:8:8:8",
1494 "5",
1495 "6",
1496 "7",
1497 "AYUV Indexed",
1498 "YUV 5:6:5",
1499 "AYUV 1:5:5:5",
1500 "AYUV 1:4:4:4",
1501 "AYUV 8:8:8:8",
1502 "13",
1503 "14",
1504 "15",
1507 ivtv_get_output(itv, itv->active_output, &vidout);
1508 ivtv_get_audio_output(itv, 0, &audout);
1509 IVTV_INFO("Video Output: %s\n", vidout.name);
1510 IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
1511 audio_modes[itv->audio_stereo_mode],
1512 audio_modes[itv->audio_bilingual_mode]);
1513 if (mode < 0 || mode > OUT_PASSTHROUGH)
1514 mode = OUT_NONE;
1515 IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1516 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1517 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1518 IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
1519 data[0] & 1 ? "On" : "Off",
1520 alpha_mode[(data[0] >> 1) & 0x3],
1521 pixel_format[(data[0] >> 3) & 0xf]);
1523 IVTV_INFO("Tuner: %s\n",
1524 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1525 cx2341x_log_status(&itv->params, itv->name);
1526 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1527 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1528 struct ivtv_stream *s = &itv->streams[i];
1530 if (s->v4l2dev == NULL || s->buffers == 0)
1531 continue;
1532 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1533 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1534 (s->buffers * s->buf_size) / 1024, s->buffers);
1537 IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n", (long long)itv->mpg_data_received, (long long)itv->vbi_data_inserted);
1538 IVTV_INFO("================== END STATUS CARD #%d ==================\n", itv->num);
1540 return 0;
1543 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1545 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1546 struct ivtv *itv = id->itv;
1547 int nonblocking = filp->f_flags & O_NONBLOCK;
1548 struct ivtv_stream *s = &itv->streams[id->type];
1550 switch (cmd) {
1551 case IVTV_IOC_DMA_FRAME: {
1552 struct ivtv_dma_frame *args = arg;
1554 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1555 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1556 return -EINVAL;
1557 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1558 return -EINVAL;
1559 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1560 return 0;
1561 if (ivtv_claim_stream(id, id->type)) {
1562 return -EBUSY;
1564 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1565 ivtv_release_stream(s);
1566 return -EBUSY;
1568 /* Mark that this file handle started the UDMA_YUV mode */
1569 id->yuv_frames = 1;
1570 if (args->y_source == NULL)
1571 return 0;
1572 return ivtv_yuv_prep_frame(itv, args);
1575 case VIDEO_GET_PTS: {
1576 u32 data[CX2341X_MBOX_MAX_DATA];
1577 u64 *pts = arg;
1579 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1580 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1581 *pts = s->dma_pts;
1582 break;
1584 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1585 return -EINVAL;
1587 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1588 *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1589 (u64)itv->last_dec_timing[1];
1590 break;
1592 *pts = 0;
1593 if (atomic_read(&itv->decoding)) {
1594 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1595 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1596 return -EIO;
1598 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1599 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1600 *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1601 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1603 break;
1606 case VIDEO_GET_FRAME_COUNT: {
1607 u32 data[CX2341X_MBOX_MAX_DATA];
1608 u64 *frame = arg;
1610 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1611 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1612 *frame = 0;
1613 break;
1615 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1616 return -EINVAL;
1618 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1619 *frame = itv->last_dec_timing[0];
1620 break;
1622 *frame = 0;
1623 if (atomic_read(&itv->decoding)) {
1624 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1625 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1626 return -EIO;
1628 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1629 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1630 *frame = data[0];
1632 break;
1635 case VIDEO_PLAY: {
1636 struct video_command vc;
1638 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1639 memset(&vc, 0, sizeof(vc));
1640 vc.cmd = VIDEO_CMD_PLAY;
1641 return ivtv_video_command(itv, id, &vc, 0);
1644 case VIDEO_STOP: {
1645 struct video_command vc;
1647 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1648 memset(&vc, 0, sizeof(vc));
1649 vc.cmd = VIDEO_CMD_STOP;
1650 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1651 return ivtv_video_command(itv, id, &vc, 0);
1654 case VIDEO_FREEZE: {
1655 struct video_command vc;
1657 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1658 memset(&vc, 0, sizeof(vc));
1659 vc.cmd = VIDEO_CMD_FREEZE;
1660 return ivtv_video_command(itv, id, &vc, 0);
1663 case VIDEO_CONTINUE: {
1664 struct video_command vc;
1666 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1667 memset(&vc, 0, sizeof(vc));
1668 vc.cmd = VIDEO_CMD_CONTINUE;
1669 return ivtv_video_command(itv, id, &vc, 0);
1672 case VIDEO_COMMAND:
1673 case VIDEO_TRY_COMMAND: {
1674 struct video_command *vc = arg;
1675 int try = (cmd == VIDEO_TRY_COMMAND);
1677 if (try)
1678 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
1679 else
1680 IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
1681 return ivtv_video_command(itv, id, vc, try);
1684 case VIDEO_GET_EVENT: {
1685 struct video_event *ev = arg;
1686 DEFINE_WAIT(wait);
1688 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1689 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1690 return -EINVAL;
1691 memset(ev, 0, sizeof(*ev));
1692 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1694 while (1) {
1695 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1696 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1697 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1698 ev->type = VIDEO_EVENT_VSYNC;
1699 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1700 VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1701 if (itv->output_mode == OUT_UDMA_YUV &&
1702 (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1703 IVTV_YUV_MODE_PROGRESSIVE) {
1704 ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1707 if (ev->type)
1708 return 0;
1709 if (nonblocking)
1710 return -EAGAIN;
1711 /* Wait for event. Note that serialize_lock is locked,
1712 so to allow other processes to access the driver while
1713 we are waiting unlock first and later lock again. */
1714 mutex_unlock(&itv->serialize_lock);
1715 prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1716 if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
1717 schedule();
1718 finish_wait(&itv->event_waitq, &wait);
1719 mutex_lock(&itv->serialize_lock);
1720 if (signal_pending(current)) {
1721 /* return if a signal was received */
1722 IVTV_DEBUG_INFO("User stopped wait for event\n");
1723 return -EINTR;
1726 break;
1729 default:
1730 return -EINVAL;
1732 return 0;
1735 static int ivtv_default(struct file *file, void *fh, int cmd, void *arg)
1737 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1739 switch (cmd) {
1740 case VIDIOC_INT_S_AUDIO_ROUTING: {
1741 struct v4l2_routing *route = arg;
1743 ivtv_i2c_hw(itv, itv->card->hw_audio, VIDIOC_INT_S_AUDIO_ROUTING, route);
1744 break;
1747 case VIDIOC_INT_RESET: {
1748 u32 val = *(u32 *)arg;
1750 if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1751 ivtv_reset_ir_gpio(itv);
1752 if (val & 0x02)
1753 itv->video_dec_func(itv, cmd, NULL);
1754 break;
1757 default:
1758 return -EINVAL;
1760 return 0;
1763 static int ivtv_serialized_ioctl(struct ivtv *itv, struct inode *inode, struct file *filp,
1764 unsigned int cmd, unsigned long arg)
1766 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1767 int ret;
1769 /* Filter dvb ioctls that cannot be handled by the v4l ioctl framework */
1770 switch (cmd) {
1771 case VIDEO_SELECT_SOURCE:
1772 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1773 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1774 return -EINVAL;
1775 return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
1777 case AUDIO_SET_MUTE:
1778 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1779 itv->speed_mute_audio = arg;
1780 return 0;
1782 case AUDIO_CHANNEL_SELECT:
1783 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1784 if (arg > AUDIO_STEREO_SWAPPED)
1785 return -EINVAL;
1786 itv->audio_stereo_mode = arg;
1787 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1788 return 0;
1790 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1791 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1792 if (arg > AUDIO_STEREO_SWAPPED)
1793 return -EINVAL;
1794 itv->audio_bilingual_mode = arg;
1795 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1796 return 0;
1798 case IVTV_IOC_DMA_FRAME:
1799 case VIDEO_GET_PTS:
1800 case VIDEO_GET_FRAME_COUNT:
1801 case VIDEO_GET_EVENT:
1802 case VIDEO_PLAY:
1803 case VIDEO_STOP:
1804 case VIDEO_FREEZE:
1805 case VIDEO_CONTINUE:
1806 case VIDEO_COMMAND:
1807 case VIDEO_TRY_COMMAND:
1808 return ivtv_decoder_ioctls(filp, cmd, (void *)arg);
1810 default:
1811 break;
1814 /* check priority */
1815 switch (cmd) {
1816 case VIDIOC_S_CTRL:
1817 case VIDIOC_S_STD:
1818 case VIDIOC_S_INPUT:
1819 case VIDIOC_S_OUTPUT:
1820 case VIDIOC_S_TUNER:
1821 case VIDIOC_S_FREQUENCY:
1822 case VIDIOC_S_FMT:
1823 case VIDIOC_S_CROP:
1824 case VIDIOC_S_AUDIO:
1825 case VIDIOC_S_AUDOUT:
1826 case VIDIOC_S_EXT_CTRLS:
1827 case VIDIOC_S_FBUF:
1828 case VIDIOC_OVERLAY:
1829 ret = v4l2_prio_check(&itv->prio, &id->prio);
1830 if (ret)
1831 return ret;
1834 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1835 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1836 v4l_printk_ioctl(cmd);
1837 printk("\n");
1840 return video_ioctl2(inode, filp, cmd, arg);
1843 int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1844 unsigned long arg)
1846 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1847 struct ivtv *itv = id->itv;
1848 int res;
1850 mutex_lock(&itv->serialize_lock);
1851 res = ivtv_serialized_ioctl(itv, inode, filp, cmd, arg);
1852 mutex_unlock(&itv->serialize_lock);
1853 return res;
1856 void ivtv_set_funcs(struct video_device *vdev)
1858 vdev->vidioc_querycap = ivtv_querycap;
1859 vdev->vidioc_g_priority = ivtv_g_priority;
1860 vdev->vidioc_s_priority = ivtv_s_priority;
1861 vdev->vidioc_s_audio = ivtv_s_audio;
1862 vdev->vidioc_g_audio = ivtv_g_audio;
1863 vdev->vidioc_enumaudio = ivtv_enumaudio;
1864 vdev->vidioc_s_audout = ivtv_s_audout;
1865 vdev->vidioc_g_audout = ivtv_g_audout;
1866 vdev->vidioc_enum_input = ivtv_enum_input;
1867 vdev->vidioc_enum_output = ivtv_enum_output;
1868 vdev->vidioc_enumaudout = ivtv_enumaudout;
1869 vdev->vidioc_cropcap = ivtv_cropcap;
1870 vdev->vidioc_s_crop = ivtv_s_crop;
1871 vdev->vidioc_g_crop = ivtv_g_crop;
1872 vdev->vidioc_g_input = ivtv_g_input;
1873 vdev->vidioc_s_input = ivtv_s_input;
1874 vdev->vidioc_g_output = ivtv_g_output;
1875 vdev->vidioc_s_output = ivtv_s_output;
1876 vdev->vidioc_g_frequency = ivtv_g_frequency;
1877 vdev->vidioc_s_frequency = ivtv_s_frequency;
1878 vdev->vidioc_s_tuner = ivtv_s_tuner;
1879 vdev->vidioc_g_tuner = ivtv_g_tuner;
1880 vdev->vidioc_g_enc_index = ivtv_g_enc_index;
1881 vdev->vidioc_g_fbuf = ivtv_g_fbuf;
1882 vdev->vidioc_s_fbuf = ivtv_s_fbuf;
1883 vdev->vidioc_g_std = ivtv_g_std;
1884 vdev->vidioc_s_std = ivtv_s_std;
1885 vdev->vidioc_overlay = ivtv_overlay;
1886 vdev->vidioc_log_status = ivtv_log_status;
1887 vdev->vidioc_enum_fmt_vid_cap = ivtv_enum_fmt_vid_cap;
1888 vdev->vidioc_encoder_cmd = ivtv_encoder_cmd;
1889 vdev->vidioc_try_encoder_cmd = ivtv_try_encoder_cmd;
1890 vdev->vidioc_enum_fmt_vid_out = ivtv_enum_fmt_vid_out;
1891 vdev->vidioc_g_fmt_vid_cap = ivtv_g_fmt_vid_cap;
1892 vdev->vidioc_g_fmt_vbi_cap = ivtv_g_fmt_vbi_cap;
1893 vdev->vidioc_g_fmt_sliced_vbi_cap = ivtv_g_fmt_sliced_vbi_cap;
1894 vdev->vidioc_g_fmt_vid_out = ivtv_g_fmt_vid_out;
1895 vdev->vidioc_g_fmt_vid_out_overlay = ivtv_g_fmt_vid_out_overlay;
1896 vdev->vidioc_g_fmt_sliced_vbi_out = ivtv_g_fmt_sliced_vbi_out;
1897 vdev->vidioc_s_fmt_vid_cap = ivtv_s_fmt_vid_cap;
1898 vdev->vidioc_s_fmt_vbi_cap = ivtv_s_fmt_vbi_cap;
1899 vdev->vidioc_s_fmt_sliced_vbi_cap = ivtv_s_fmt_sliced_vbi_cap;
1900 vdev->vidioc_s_fmt_vid_out = ivtv_s_fmt_vid_out;
1901 vdev->vidioc_s_fmt_vid_out_overlay = ivtv_s_fmt_vid_out_overlay;
1902 vdev->vidioc_s_fmt_sliced_vbi_out = ivtv_s_fmt_sliced_vbi_out;
1903 vdev->vidioc_try_fmt_vid_cap = ivtv_try_fmt_vid_cap;
1904 vdev->vidioc_try_fmt_vbi_cap = ivtv_try_fmt_vbi_cap;
1905 vdev->vidioc_try_fmt_sliced_vbi_cap = ivtv_try_fmt_sliced_vbi_cap;
1906 vdev->vidioc_try_fmt_vid_out = ivtv_try_fmt_vid_out;
1907 vdev->vidioc_try_fmt_vid_out_overlay = ivtv_try_fmt_vid_out_overlay;
1908 vdev->vidioc_try_fmt_sliced_vbi_out = ivtv_try_fmt_sliced_vbi_out;
1909 vdev->vidioc_g_sliced_vbi_cap = ivtv_g_sliced_vbi_cap;
1910 vdev->vidioc_g_chip_ident = ivtv_g_chip_ident;
1911 vdev->vidioc_g_register = ivtv_g_register;
1912 vdev->vidioc_s_register = ivtv_s_register;
1913 vdev->vidioc_default = ivtv_default;
1914 vdev->vidioc_queryctrl = ivtv_queryctrl;
1915 vdev->vidioc_querymenu = ivtv_querymenu;
1916 vdev->vidioc_g_ctrl = ivtv_g_ctrl;
1917 vdev->vidioc_s_ctrl = ivtv_s_ctrl;
1918 vdev->vidioc_g_ext_ctrls = ivtv_g_ext_ctrls;
1919 vdev->vidioc_s_ext_ctrls = ivtv_s_ext_ctrls;
1920 vdev->vidioc_try_ext_ctrls = ivtv_try_ext_ctrls;