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"
25 #include "ivtv-queue.h"
26 #include "ivtv-fileops.h"
28 #include "ivtv-routing.h"
29 #include "ivtv-streams.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)
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;
51 return IVTV_SLICED_TYPE_VPS;
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);
68 set = set & valid_set;
69 if (set == 0 || !valid_service_line(field, line, is_pal)) {
73 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
74 return V4L2_SLICED_CAPTION_525;
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;
84 for (i = 0; i < 32; i++) {
91 void ivtv_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
93 u16 set = fmt->service_set;
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)
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];
118 u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt)
123 for (f = 0; f < 2; f++) {
124 for (l = 0; l < 24; l++) {
125 set |= fmt->service_lines[f][l];
131 static const struct {
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},
156 static const struct v4l2_standard ivtv_std_50hz =
158 .frameperiod = {.numerator = 1, .denominator = 25},
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);
176 if (speed == 0) speed = 1000;
179 if (speed == itv->speed && !single_step)
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);
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;
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) {
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);
224 finish_wait(&itv->dma_waitq, &wait);
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]);
234 speed = (speed < 0) ? -1 : 1;
235 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
241 static int ivtv_validate_speed(int cur_speed, int new_speed)
243 int fact = new_speed < 0 ? -1 : 1;
249 new_speed = -new_speed;
251 cur_speed = -cur_speed;
253 if (cur_speed <= new_speed) {
254 if (new_speed > 1500)
256 if (new_speed > 1000)
260 if (new_speed >= 2000)
262 if (new_speed >= 1500)
264 if (new_speed >= 1000)
269 if (new_speed == 1 || new_speed == 1000)
270 return fact * 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))
289 case VIDEO_CMD_PLAY: {
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;
296 if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
298 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
299 /* forces ivtv_set_speed to be called */
302 return ivtv_start_decoding(id, vc->play.speed);
306 vc->flags &= VIDEO_CMD_STOP_IMMEDIATELY|VIDEO_CMD_STOP_TO_BLACK;
307 if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
310 if (atomic_read(&itv->decoding) == 0)
312 if (itv->output_mode != OUT_MPG)
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;
321 if (itv->output_mode != OUT_MPG)
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);
330 case VIDEO_CMD_CONTINUE:
333 if (itv->output_mode != OUT_MPG)
335 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
336 int speed = itv->speed;
338 return ivtv_start_decoding(id, speed);
348 static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
350 struct v4l2_register *regs = arg;
352 volatile u8 __iomem *reg_start;
354 if (!capable(CAP_SYS_ADMIN))
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;
366 spin_lock_irqsave(&ivtv_cards_lock, flags);
367 if (cmd == VIDIOC_DBG_G_REGISTER) {
368 regs->val = readl(regs->reg + reg_start);
370 writel(regs->val, regs->reg + reg_start);
372 spin_unlock_irqrestore(&ivtv_cards_lock, flags);
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 vbifmt->reserved[0] = 0;
382 vbifmt->reserved[1] = 0;
383 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
385 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
387 vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
388 vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
390 vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
391 vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
393 vbifmt->service_set = ivtv_get_service_set(vbifmt);
397 static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
399 struct ivtv_open_id *id = fh;
400 struct ivtv *itv = id->itv;
401 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
403 pixfmt->width = itv->params.width;
404 pixfmt->height = itv->params.height;
405 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
406 pixfmt->field = V4L2_FIELD_INTERLACED;
408 if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
409 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
410 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
412 pixfmt->height * pixfmt->width +
413 pixfmt->height * (pixfmt->width / 2);
414 pixfmt->bytesperline = 720;
416 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
417 pixfmt->sizeimage = 128 * 1024;
418 pixfmt->bytesperline = 0;
423 static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
425 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
426 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
428 vbifmt->sampling_rate = 27000000;
429 vbifmt->offset = 248;
430 vbifmt->samples_per_line = itv->vbi.raw_decoder_line_size - 4;
431 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
432 vbifmt->start[0] = itv->vbi.start[0];
433 vbifmt->start[1] = itv->vbi.start[1];
434 vbifmt->count[0] = vbifmt->count[1] = itv->vbi.count;
436 vbifmt->reserved[0] = 0;
437 vbifmt->reserved[1] = 0;
441 static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
443 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
444 struct ivtv_open_id *id = fh;
445 struct ivtv *itv = id->itv;
447 vbifmt->reserved[0] = 0;
448 vbifmt->reserved[1] = 0;
449 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
451 if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
452 vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
454 ivtv_expand_service_set(vbifmt, itv->is_50hz);
458 itv->video_dec_func(itv, VIDIOC_G_FMT, fmt);
459 vbifmt->service_set = ivtv_get_service_set(vbifmt);
463 static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
465 struct ivtv_open_id *id = fh;
466 struct ivtv *itv = id->itv;
467 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
469 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
471 pixfmt->width = itv->main_rect.width;
472 pixfmt->height = itv->main_rect.height;
473 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
474 pixfmt->field = V4L2_FIELD_INTERLACED;
476 if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
477 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
478 case IVTV_YUV_MODE_INTERLACED:
479 pixfmt->field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
480 V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
482 case IVTV_YUV_MODE_PROGRESSIVE:
483 pixfmt->field = V4L2_FIELD_NONE;
486 pixfmt->field = V4L2_FIELD_ANY;
489 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
490 pixfmt->bytesperline = 720;
491 pixfmt->width = itv->yuv_info.v4l2_src_w;
492 pixfmt->height = itv->yuv_info.v4l2_src_h;
493 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
495 1080 * ((pixfmt->height + 31) & ~31);
497 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
498 pixfmt->sizeimage = 128 * 1024;
499 pixfmt->bytesperline = 0;
504 static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
506 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
507 struct v4l2_window *winfmt = &fmt->fmt.win;
509 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
511 winfmt->chromakey = itv->osd_chroma_key;
512 winfmt->global_alpha = itv->osd_global_alpha;
513 winfmt->field = V4L2_FIELD_INTERLACED;
514 winfmt->clips = NULL;
515 winfmt->clipcount = 0;
516 winfmt->bitmap = NULL;
517 winfmt->w.top = winfmt->w.left = 0;
518 winfmt->w.width = itv->osd_rect.width;
519 winfmt->w.height = itv->osd_rect.height;
523 static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
525 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
528 static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
530 struct ivtv_open_id *id = fh;
531 struct ivtv *itv = id->itv;
532 int w = fmt->fmt.pix.width;
533 int h = fmt->fmt.pix.height;
537 h = min(h, itv->is_50hz ? 576 : 480);
539 ivtv_g_fmt_vid_cap(file, fh, fmt);
540 fmt->fmt.pix.width = w;
541 fmt->fmt.pix.height = h;
545 static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
547 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
550 static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
552 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
553 struct ivtv_open_id *id = fh;
554 struct ivtv *itv = id->itv;
556 if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
557 return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
559 /* set sliced VBI capture format */
560 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
561 vbifmt->reserved[0] = 0;
562 vbifmt->reserved[1] = 0;
564 if (vbifmt->service_set)
565 ivtv_expand_service_set(vbifmt, itv->is_50hz);
566 check_service_set(vbifmt, itv->is_50hz);
567 vbifmt->service_set = ivtv_get_service_set(vbifmt);
571 static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
573 struct ivtv_open_id *id = fh;
578 w = fmt->fmt.pix.width;
579 h = fmt->fmt.pix.height;
580 field = fmt->fmt.pix.field;
581 ret = ivtv_g_fmt_vid_out(file, fh, fmt);
582 fmt->fmt.pix.width = w;
583 fmt->fmt.pix.height = h;
584 if (!ret && id->type == IVTV_DEC_STREAM_TYPE_YUV) {
585 fmt->fmt.pix.field = field;
586 if (fmt->fmt.pix.width < 2)
587 fmt->fmt.pix.width = 2;
588 if (fmt->fmt.pix.width > 720)
589 fmt->fmt.pix.width = 720;
590 if (fmt->fmt.pix.height < 2)
591 fmt->fmt.pix.height = 2;
592 if (fmt->fmt.pix.height > 576)
593 fmt->fmt.pix.height = 576;
598 static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
600 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
601 u32 chromakey = fmt->fmt.win.chromakey;
602 u8 global_alpha = fmt->fmt.win.global_alpha;
604 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
606 ivtv_g_fmt_vid_out_overlay(file, fh, fmt);
607 fmt->fmt.win.chromakey = chromakey;
608 fmt->fmt.win.global_alpha = global_alpha;
612 static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
614 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
617 static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
619 struct ivtv_open_id *id = fh;
620 struct ivtv *itv = id->itv;
621 struct cx2341x_mpeg_params *p = &itv->params;
622 int w = fmt->fmt.pix.width;
623 int h = fmt->fmt.pix.height;
624 int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
629 if (p->width == w && p->height == h)
632 if (atomic_read(&itv->capturing) > 0)
637 if (w != 720 || h != (itv->is_50hz ? 576 : 480))
638 p->video_temporal_filter = 0;
640 p->video_temporal_filter = 8;
641 if (p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
642 fmt->fmt.pix.width /= 2;
643 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
644 return ivtv_g_fmt_vid_cap(file, fh, fmt);
647 static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
649 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
651 itv->vbi.sliced_in->service_set = 0;
652 itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
653 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
656 static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
658 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
659 struct ivtv_open_id *id = fh;
660 struct ivtv *itv = id->itv;
661 int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
663 if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
666 if (check_service_set(vbifmt, itv->is_50hz) == 0)
668 if (atomic_read(&itv->capturing) > 0)
670 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
671 memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
675 static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
677 struct ivtv_open_id *id = fh;
678 struct ivtv *itv = id->itv;
679 struct yuv_playback_info *yi = &itv->yuv_info;
680 int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
685 if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
688 /* Return now if we already have some frame data */
692 yi->v4l2_src_w = fmt->fmt.pix.width;
693 yi->v4l2_src_h = fmt->fmt.pix.height;
695 switch (fmt->fmt.pix.field) {
696 case V4L2_FIELD_NONE:
697 yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
700 yi->lace_mode = IVTV_YUV_MODE_AUTO;
702 case V4L2_FIELD_INTERLACED_BT:
704 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
706 case V4L2_FIELD_INTERLACED_TB:
708 yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
711 yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
713 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
714 itv->dma_data_req_size =
715 1080 * ((yi->v4l2_src_h + 31) & ~31);
717 /* Force update of yuv registers */
718 yi->yuv_forced_update = 1;
722 static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
724 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
725 int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
728 itv->osd_chroma_key = fmt->fmt.win.chromakey;
729 itv->osd_global_alpha = fmt->fmt.win.global_alpha;
730 ivtv_set_osd_alpha(itv);
735 static int ivtv_g_chip_ident(struct file *file, void *fh, struct v4l2_chip_ident *chip)
737 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
739 chip->ident = V4L2_IDENT_NONE;
741 if (chip->match_type == V4L2_CHIP_MATCH_HOST) {
742 if (v4l2_chip_match_host(chip->match_type, chip->match_chip))
743 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
746 if (chip->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
747 return ivtv_i2c_id(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
748 if (chip->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
749 return ivtv_call_i2c_client(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
753 static int ivtv_g_register(struct file *file, void *fh, struct v4l2_register *reg)
755 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
757 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
758 return ivtv_itvc(itv, VIDIOC_DBG_G_REGISTER, reg);
759 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
760 return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
761 return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
764 static int ivtv_s_register(struct file *file, void *fh, struct v4l2_register *reg)
766 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
768 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
769 return ivtv_itvc(itv, VIDIOC_DBG_S_REGISTER, reg);
770 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
771 return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
772 return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
775 static int ivtv_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
777 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
779 *p = v4l2_prio_max(&itv->prio);
784 static int ivtv_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
786 struct ivtv_open_id *id = fh;
787 struct ivtv *itv = id->itv;
789 return v4l2_prio_change(&itv->prio, &id->prio, prio);
792 static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
794 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
796 memset(vcap, 0, sizeof(*vcap));
797 strlcpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
798 strlcpy(vcap->card, itv->card_name, sizeof(vcap->card));
799 strlcpy(vcap->bus_info, pci_name(itv->dev), sizeof(vcap->bus_info));
800 vcap->version = IVTV_DRIVER_VERSION; /* version */
801 vcap->capabilities = itv->v4l2_cap; /* capabilities */
802 /* reserved.. must set to 0! */
803 vcap->reserved[0] = vcap->reserved[1] =
804 vcap->reserved[2] = vcap->reserved[3] = 0;
808 static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
810 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
812 return ivtv_get_audio_input(itv, vin->index, vin);
815 static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
817 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
819 vin->index = itv->audio_input;
820 return ivtv_get_audio_input(itv, vin->index, vin);
823 static int ivtv_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
825 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
827 if (vout->index >= itv->nof_audio_inputs)
830 itv->audio_input = vout->index;
831 ivtv_audio_set_io(itv);
836 static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
838 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
840 /* set it to defaults from our table */
841 return ivtv_get_audio_output(itv, vin->index, vin);
844 static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
846 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
849 return ivtv_get_audio_output(itv, vin->index, vin);
852 static int ivtv_s_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
854 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
856 return ivtv_get_audio_output(itv, vout->index, vout);
859 static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
861 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
863 /* set it to defaults from our table */
864 return ivtv_get_input(itv, vin->index, vin);
867 static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
869 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
871 return ivtv_get_output(itv, vout->index, vout);
874 static int ivtv_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
876 struct ivtv_open_id *id = fh;
877 struct ivtv *itv = id->itv;
878 struct yuv_playback_info *yi = &itv->yuv_info;
881 streamtype = id->type;
883 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
885 cropcap->bounds.top = cropcap->bounds.left = 0;
886 cropcap->bounds.width = 720;
887 if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
888 cropcap->bounds.height = itv->is_50hz ? 576 : 480;
889 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
890 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
891 } else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
893 cropcap->bounds.width = yi->osd_full_w;
894 cropcap->bounds.height = yi->osd_full_h;
896 cropcap->bounds.width = 720;
897 cropcap->bounds.height =
898 itv->is_out_50hz ? 576 : 480;
900 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
901 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
903 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
904 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
905 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
907 cropcap->defrect = cropcap->bounds;
911 static int ivtv_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
913 struct ivtv_open_id *id = fh;
914 struct ivtv *itv = id->itv;
915 struct yuv_playback_info *yi = &itv->yuv_info;
918 streamtype = id->type;
920 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
921 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
922 /* Should be replaced */
923 /* v4l_printk_ioctl(VIDIOC_S_CROP); */
926 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
927 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
928 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
929 yi->main_rect = crop->c;
932 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
933 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
934 itv->main_rect = crop->c;
943 static int ivtv_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
945 struct ivtv_open_id *id = fh;
946 struct ivtv *itv = id->itv;
947 struct yuv_playback_info *yi = &itv->yuv_info;
950 streamtype = id->type;
952 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
953 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
954 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
955 crop->c = yi->main_rect;
957 crop->c = itv->main_rect;
963 static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
965 static struct v4l2_fmtdesc formats[] = {
967 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
970 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
971 "MPEG", V4L2_PIX_FMT_MPEG,
975 enum v4l2_buf_type type = fmt->type;
980 *fmt = formats[fmt->index];
985 static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
987 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
989 static struct v4l2_fmtdesc formats[] = {
991 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
994 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
995 "MPEG", V4L2_PIX_FMT_MPEG,
999 enum v4l2_buf_type type = fmt->type;
1001 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1007 *fmt = formats[fmt->index];
1013 static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
1015 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1017 *i = itv->active_input;
1022 int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
1024 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1026 if (inp < 0 || inp >= itv->nof_inputs)
1029 if (inp == itv->active_input) {
1030 IVTV_DEBUG_INFO("Input unchanged\n");
1034 if (atomic_read(&itv->capturing) > 0) {
1038 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
1039 itv->active_input, inp);
1041 itv->active_input = inp;
1042 /* Set the audio input to whatever is appropriate for the
1044 itv->audio_input = itv->card->video_inputs[inp].audio_index;
1046 /* prevent others from messing with the streams until
1047 we're finished changing inputs. */
1049 ivtv_video_set_io(itv);
1050 ivtv_audio_set_io(itv);
1056 static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1058 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1060 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1063 *i = itv->active_output;
1068 static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1070 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1071 struct v4l2_routing route;
1073 if (outp >= itv->card->nof_outputs)
1076 if (outp == itv->active_output) {
1077 IVTV_DEBUG_INFO("Output unchanged\n");
1080 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1081 itv->active_output, outp);
1083 itv->active_output = outp;
1084 route.input = SAA7127_INPUT_TYPE_NORMAL;
1085 route.output = itv->card->video_outputs[outp].video_output;
1086 ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1091 static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1093 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1098 ivtv_call_i2c_clients(itv, VIDIOC_G_FREQUENCY, vf);
1102 int ivtv_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1104 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1110 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1111 ivtv_call_i2c_clients(itv, VIDIOC_S_FREQUENCY, vf);
1116 static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1118 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1124 int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
1126 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1127 struct yuv_playback_info *yi = &itv->yuv_info;
1129 if ((*std & V4L2_STD_ALL) == 0)
1132 if (*std == itv->std)
1135 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1136 atomic_read(&itv->capturing) > 0 ||
1137 atomic_read(&itv->decoding) > 0) {
1138 /* Switching standard would turn off the radio or mess
1139 with already running streams, prevent that by
1145 itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
1146 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
1147 itv->params.width = 720;
1148 itv->params.height = itv->is_50hz ? 576 : 480;
1149 itv->vbi.count = itv->is_50hz ? 18 : 12;
1150 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1151 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1153 if (itv->hw_flags & IVTV_HW_CX25840)
1154 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1156 IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
1159 ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
1161 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1162 /* set display standard */
1163 itv->std_out = *std;
1164 itv->is_out_60hz = itv->is_60hz;
1165 itv->is_out_50hz = itv->is_50hz;
1166 ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
1167 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1168 itv->main_rect.left = itv->main_rect.top = 0;
1169 itv->main_rect.width = 720;
1170 itv->main_rect.height = itv->params.height;
1171 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1172 720, itv->main_rect.height, 0, 0);
1173 yi->main_rect = itv->main_rect;
1174 if (!itv->osd_info) {
1175 yi->osd_full_w = 720;
1176 yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1182 static int ivtv_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1184 struct ivtv_open_id *id = fh;
1185 struct ivtv *itv = id->itv;
1190 ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
1195 static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1197 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1202 memset(vt, 0, sizeof(*vt));
1203 ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
1205 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1206 strlcpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1207 vt->type = V4L2_TUNER_RADIO;
1209 strlcpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1210 vt->type = V4L2_TUNER_ANALOG_TV;
1216 static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1218 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1219 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1221 enum v4l2_buf_type type = cap->type;
1223 memset(cap, 0, sizeof(*cap));
1225 if (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1226 for (f = 0; f < 2; f++) {
1227 for (l = 0; l < 24; l++) {
1228 if (valid_service_line(f, l, itv->is_50hz))
1229 cap->service_lines[f][l] = set;
1234 if (type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1235 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1238 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1239 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1241 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1242 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1249 static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1251 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1252 struct v4l2_enc_idx_entry *e = idx->entry;
1256 entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1258 if (entries > V4L2_ENC_IDX_ENTRIES)
1259 entries = V4L2_ENC_IDX_ENTRIES;
1261 for (i = 0; i < entries; i++) {
1262 *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1263 if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1268 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1272 static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1274 struct ivtv_open_id *id = fh;
1275 struct ivtv *itv = id->itv;
1277 memset(&enc->raw, 0, sizeof(enc->raw));
1280 case V4L2_ENC_CMD_START:
1281 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1283 return ivtv_start_capture(id);
1285 case V4L2_ENC_CMD_STOP:
1286 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1287 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1288 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1291 case V4L2_ENC_CMD_PAUSE:
1292 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1295 if (!atomic_read(&itv->capturing))
1297 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1301 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1304 case V4L2_ENC_CMD_RESUME:
1305 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1308 if (!atomic_read(&itv->capturing))
1311 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1314 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1318 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1325 static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1327 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1329 memset(&enc->raw, 0, sizeof(enc->raw));
1332 case V4L2_ENC_CMD_START:
1333 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1337 case V4L2_ENC_CMD_STOP:
1338 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1339 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1342 case V4L2_ENC_CMD_PAUSE:
1343 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1347 case V4L2_ENC_CMD_RESUME:
1348 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1352 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1357 static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1359 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1360 u32 data[CX2341X_MBOX_MAX_DATA];
1361 struct yuv_playback_info *yi = &itv->yuv_info;
1364 static u32 pixel_format[16] = {
1365 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1366 V4L2_PIX_FMT_RGB565,
1367 V4L2_PIX_FMT_RGB555,
1368 V4L2_PIX_FMT_RGB444,
1373 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1374 V4L2_PIX_FMT_YUV565,
1375 V4L2_PIX_FMT_YUV555,
1376 V4L2_PIX_FMT_YUV444,
1383 memset(fb, 0, sizeof(*fb));
1385 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1388 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1389 V4L2_FBUF_CAP_GLOBAL_ALPHA;
1391 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1392 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1393 pixfmt = (data[0] >> 3) & 0xf;
1395 fb->fmt.pixelformat = pixel_format[pixfmt];
1396 fb->fmt.width = itv->osd_rect.width;
1397 fb->fmt.height = itv->osd_rect.height;
1398 fb->base = (void *)itv->osd_video_pbase;
1400 if (itv->osd_chroma_key_state)
1401 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1403 if (itv->osd_global_alpha_state)
1404 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1408 /* no local alpha for RGB565 or unknown formats */
1409 if (pixfmt == 1 || pixfmt > 4)
1412 /* 16-bit formats have inverted local alpha */
1413 if (pixfmt == 2 || pixfmt == 3)
1414 fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1416 fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1418 if (itv->osd_local_alpha_state) {
1419 /* 16-bit formats have inverted local alpha */
1420 if (pixfmt == 2 || pixfmt == 3)
1421 fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1423 fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1426 fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1431 static int ivtv_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1433 struct ivtv_open_id *id = fh;
1434 struct ivtv *itv = id->itv;
1435 struct yuv_playback_info *yi = &itv->yuv_info;
1437 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1440 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1441 itv->osd_local_alpha_state =
1442 (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1443 itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1444 ivtv_set_osd_alpha(itv);
1445 yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1450 static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1452 struct ivtv_open_id *id = fh;
1453 struct ivtv *itv = id->itv;
1455 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1458 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1463 static int ivtv_log_status(struct file *file, void *fh)
1465 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1466 u32 data[CX2341X_MBOX_MAX_DATA];
1468 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1469 struct v4l2_input vidin;
1470 struct v4l2_audio audin;
1473 IVTV_INFO("================= START STATUS CARD #%d =================\n", itv->num);
1474 IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1475 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1478 ivtv_read_eeprom(itv, &tv);
1480 ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
1481 ivtv_get_input(itv, itv->active_input, &vidin);
1482 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1483 IVTV_INFO("Video Input: %s\n", vidin.name);
1484 IVTV_INFO("Audio Input: %s%s\n", audin.name,
1485 (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
1487 struct v4l2_output vidout;
1488 struct v4l2_audioout audout;
1489 int mode = itv->output_mode;
1490 static const char * const output_modes[5] = {
1497 static const char * const audio_modes[5] = {
1504 static const char * const alpha_mode[4] = {
1510 static const char * const pixel_format[16] = {
1529 ivtv_get_output(itv, itv->active_output, &vidout);
1530 ivtv_get_audio_output(itv, 0, &audout);
1531 IVTV_INFO("Video Output: %s\n", vidout.name);
1532 IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
1533 audio_modes[itv->audio_stereo_mode],
1534 audio_modes[itv->audio_bilingual_mode]);
1535 if (mode < 0 || mode > OUT_PASSTHROUGH)
1537 IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1538 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1539 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1540 IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
1541 data[0] & 1 ? "On" : "Off",
1542 alpha_mode[(data[0] >> 1) & 0x3],
1543 pixel_format[(data[0] >> 3) & 0xf]);
1545 IVTV_INFO("Tuner: %s\n",
1546 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1547 cx2341x_log_status(&itv->params, itv->name);
1548 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1549 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1550 struct ivtv_stream *s = &itv->streams[i];
1552 if (s->v4l2dev == NULL || s->buffers == 0)
1554 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1555 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1556 (s->buffers * s->buf_size) / 1024, s->buffers);
1559 IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n", (long long)itv->mpg_data_received, (long long)itv->vbi_data_inserted);
1560 IVTV_INFO("================== END STATUS CARD #%d ==================\n", itv->num);
1565 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1567 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1568 struct ivtv *itv = id->itv;
1569 int nonblocking = filp->f_flags & O_NONBLOCK;
1570 struct ivtv_stream *s = &itv->streams[id->type];
1573 case IVTV_IOC_DMA_FRAME: {
1574 struct ivtv_dma_frame *args = arg;
1576 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1577 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1579 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1581 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1583 if (ivtv_claim_stream(id, id->type)) {
1586 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1587 ivtv_release_stream(s);
1590 /* Mark that this file handle started the UDMA_YUV mode */
1592 if (args->y_source == NULL)
1594 return ivtv_yuv_prep_frame(itv, args);
1597 case VIDEO_GET_PTS: {
1598 u32 data[CX2341X_MBOX_MAX_DATA];
1601 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1602 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1606 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1609 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1610 *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1611 (u64)itv->last_dec_timing[1];
1615 if (atomic_read(&itv->decoding)) {
1616 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1617 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1620 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1621 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1622 *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1623 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1628 case VIDEO_GET_FRAME_COUNT: {
1629 u32 data[CX2341X_MBOX_MAX_DATA];
1632 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1633 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1637 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1640 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1641 *frame = itv->last_dec_timing[0];
1645 if (atomic_read(&itv->decoding)) {
1646 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1647 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1650 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1651 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1658 struct video_command vc;
1660 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1661 memset(&vc, 0, sizeof(vc));
1662 vc.cmd = VIDEO_CMD_PLAY;
1663 return ivtv_video_command(itv, id, &vc, 0);
1667 struct video_command vc;
1669 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1670 memset(&vc, 0, sizeof(vc));
1671 vc.cmd = VIDEO_CMD_STOP;
1672 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1673 return ivtv_video_command(itv, id, &vc, 0);
1676 case VIDEO_FREEZE: {
1677 struct video_command vc;
1679 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1680 memset(&vc, 0, sizeof(vc));
1681 vc.cmd = VIDEO_CMD_FREEZE;
1682 return ivtv_video_command(itv, id, &vc, 0);
1685 case VIDEO_CONTINUE: {
1686 struct video_command vc;
1688 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1689 memset(&vc, 0, sizeof(vc));
1690 vc.cmd = VIDEO_CMD_CONTINUE;
1691 return ivtv_video_command(itv, id, &vc, 0);
1695 case VIDEO_TRY_COMMAND: {
1696 struct video_command *vc = arg;
1697 int try = (cmd == VIDEO_TRY_COMMAND);
1700 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
1702 IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
1703 return ivtv_video_command(itv, id, vc, try);
1706 case VIDEO_GET_EVENT: {
1707 struct video_event *ev = arg;
1710 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1711 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1713 memset(ev, 0, sizeof(*ev));
1714 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1717 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1718 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1719 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1720 ev->type = VIDEO_EVENT_VSYNC;
1721 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1722 VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1723 if (itv->output_mode == OUT_UDMA_YUV &&
1724 (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1725 IVTV_YUV_MODE_PROGRESSIVE) {
1726 ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1733 /* Wait for event. Note that serialize_lock is locked,
1734 so to allow other processes to access the driver while
1735 we are waiting unlock first and later lock again. */
1736 mutex_unlock(&itv->serialize_lock);
1737 prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1738 if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
1740 finish_wait(&itv->event_waitq, &wait);
1741 mutex_lock(&itv->serialize_lock);
1742 if (signal_pending(current)) {
1743 /* return if a signal was received */
1744 IVTV_DEBUG_INFO("User stopped wait for event\n");
1757 static int ivtv_default(struct file *file, void *fh, int cmd, void *arg)
1759 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1762 case VIDIOC_INT_S_AUDIO_ROUTING: {
1763 struct v4l2_routing *route = arg;
1765 ivtv_i2c_hw(itv, itv->card->hw_audio, VIDIOC_INT_S_AUDIO_ROUTING, route);
1769 case VIDIOC_INT_RESET: {
1770 u32 val = *(u32 *)arg;
1772 if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1773 ivtv_reset_ir_gpio(itv);
1775 itv->video_dec_func(itv, cmd, NULL);
1785 static int ivtv_serialized_ioctl(struct ivtv *itv, struct inode *inode, struct file *filp,
1786 unsigned int cmd, unsigned long arg)
1788 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1791 /* Filter dvb ioctls that cannot be handled by the v4l ioctl framework */
1793 case VIDEO_SELECT_SOURCE:
1794 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1795 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1797 return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
1799 case AUDIO_SET_MUTE:
1800 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1801 itv->speed_mute_audio = arg;
1804 case AUDIO_CHANNEL_SELECT:
1805 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1806 if (arg > AUDIO_STEREO_SWAPPED)
1808 itv->audio_stereo_mode = arg;
1809 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1812 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1813 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1814 if (arg > AUDIO_STEREO_SWAPPED)
1816 itv->audio_bilingual_mode = arg;
1817 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1820 case IVTV_IOC_DMA_FRAME:
1822 case VIDEO_GET_FRAME_COUNT:
1823 case VIDEO_GET_EVENT:
1827 case VIDEO_CONTINUE:
1829 case VIDEO_TRY_COMMAND:
1830 return ivtv_decoder_ioctls(filp, cmd, (void *)arg);
1836 /* check priority */
1840 case VIDIOC_S_INPUT:
1841 case VIDIOC_S_OUTPUT:
1842 case VIDIOC_S_TUNER:
1843 case VIDIOC_S_FREQUENCY:
1846 case VIDIOC_S_AUDIO:
1847 case VIDIOC_S_AUDOUT:
1848 case VIDIOC_S_EXT_CTRLS:
1850 case VIDIOC_OVERLAY:
1851 ret = v4l2_prio_check(&itv->prio, &id->prio);
1856 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1857 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1858 v4l_printk_ioctl(cmd);
1862 return video_ioctl2(inode, filp, cmd, arg);
1865 int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1868 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1869 struct ivtv *itv = id->itv;
1872 mutex_lock(&itv->serialize_lock);
1873 res = ivtv_serialized_ioctl(itv, inode, filp, cmd, arg);
1874 mutex_unlock(&itv->serialize_lock);
1878 void ivtv_set_funcs(struct video_device *vdev)
1880 vdev->vidioc_querycap = ivtv_querycap;
1881 vdev->vidioc_g_priority = ivtv_g_priority;
1882 vdev->vidioc_s_priority = ivtv_s_priority;
1883 vdev->vidioc_s_audio = ivtv_s_audio;
1884 vdev->vidioc_g_audio = ivtv_g_audio;
1885 vdev->vidioc_enumaudio = ivtv_enumaudio;
1886 vdev->vidioc_s_audout = ivtv_s_audout;
1887 vdev->vidioc_g_audout = ivtv_g_audout;
1888 vdev->vidioc_enum_input = ivtv_enum_input;
1889 vdev->vidioc_enum_output = ivtv_enum_output;
1890 vdev->vidioc_enumaudout = ivtv_enumaudout;
1891 vdev->vidioc_cropcap = ivtv_cropcap;
1892 vdev->vidioc_s_crop = ivtv_s_crop;
1893 vdev->vidioc_g_crop = ivtv_g_crop;
1894 vdev->vidioc_g_input = ivtv_g_input;
1895 vdev->vidioc_s_input = ivtv_s_input;
1896 vdev->vidioc_g_output = ivtv_g_output;
1897 vdev->vidioc_s_output = ivtv_s_output;
1898 vdev->vidioc_g_frequency = ivtv_g_frequency;
1899 vdev->vidioc_s_frequency = ivtv_s_frequency;
1900 vdev->vidioc_s_tuner = ivtv_s_tuner;
1901 vdev->vidioc_g_tuner = ivtv_g_tuner;
1902 vdev->vidioc_g_enc_index = ivtv_g_enc_index;
1903 vdev->vidioc_g_fbuf = ivtv_g_fbuf;
1904 vdev->vidioc_s_fbuf = ivtv_s_fbuf;
1905 vdev->vidioc_g_std = ivtv_g_std;
1906 vdev->vidioc_s_std = ivtv_s_std;
1907 vdev->vidioc_overlay = ivtv_overlay;
1908 vdev->vidioc_log_status = ivtv_log_status;
1909 vdev->vidioc_enum_fmt_vid_cap = ivtv_enum_fmt_vid_cap;
1910 vdev->vidioc_encoder_cmd = ivtv_encoder_cmd;
1911 vdev->vidioc_try_encoder_cmd = ivtv_try_encoder_cmd;
1912 vdev->vidioc_enum_fmt_vid_out = ivtv_enum_fmt_vid_out;
1913 vdev->vidioc_g_fmt_vid_cap = ivtv_g_fmt_vid_cap;
1914 vdev->vidioc_g_fmt_vbi_cap = ivtv_g_fmt_vbi_cap;
1915 vdev->vidioc_g_fmt_sliced_vbi_cap = ivtv_g_fmt_sliced_vbi_cap;
1916 vdev->vidioc_g_fmt_vid_out = ivtv_g_fmt_vid_out;
1917 vdev->vidioc_g_fmt_vid_out_overlay = ivtv_g_fmt_vid_out_overlay;
1918 vdev->vidioc_g_fmt_sliced_vbi_out = ivtv_g_fmt_sliced_vbi_out;
1919 vdev->vidioc_s_fmt_vid_cap = ivtv_s_fmt_vid_cap;
1920 vdev->vidioc_s_fmt_vbi_cap = ivtv_s_fmt_vbi_cap;
1921 vdev->vidioc_s_fmt_sliced_vbi_cap = ivtv_s_fmt_sliced_vbi_cap;
1922 vdev->vidioc_s_fmt_vid_out = ivtv_s_fmt_vid_out;
1923 vdev->vidioc_s_fmt_vid_out_overlay = ivtv_s_fmt_vid_out_overlay;
1924 vdev->vidioc_s_fmt_sliced_vbi_out = ivtv_s_fmt_sliced_vbi_out;
1925 vdev->vidioc_try_fmt_vid_cap = ivtv_try_fmt_vid_cap;
1926 vdev->vidioc_try_fmt_vbi_cap = ivtv_try_fmt_vbi_cap;
1927 vdev->vidioc_try_fmt_sliced_vbi_cap = ivtv_try_fmt_sliced_vbi_cap;
1928 vdev->vidioc_try_fmt_vid_out = ivtv_try_fmt_vid_out;
1929 vdev->vidioc_try_fmt_vid_out_overlay = ivtv_try_fmt_vid_out_overlay;
1930 vdev->vidioc_try_fmt_sliced_vbi_out = ivtv_try_fmt_sliced_vbi_out;
1931 vdev->vidioc_g_sliced_vbi_cap = ivtv_g_sliced_vbi_cap;
1932 vdev->vidioc_g_chip_ident = ivtv_g_chip_ident;
1933 vdev->vidioc_g_register = ivtv_g_register;
1934 vdev->vidioc_s_register = ivtv_s_register;
1935 vdev->vidioc_default = ivtv_default;
1936 vdev->vidioc_queryctrl = ivtv_queryctrl;
1937 vdev->vidioc_querymenu = ivtv_querymenu;
1938 vdev->vidioc_g_ctrl = ivtv_g_ctrl;
1939 vdev->vidioc_s_ctrl = ivtv_s_ctrl;
1940 vdev->vidioc_g_ext_ctrls = ivtv_g_ext_ctrls;
1941 vdev->vidioc_s_ext_ctrls = ivtv_s_ext_ctrls;
1942 vdev->vidioc_try_ext_ctrls = ivtv_try_ext_ctrls;