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 void ivtv_set_osd_alpha(struct ivtv *itv)
133 ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
134 itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
135 ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
138 int ivtv_set_speed(struct ivtv *itv, int speed)
140 u32 data[CX2341X_MBOX_MAX_DATA];
141 struct ivtv_stream *s;
142 int single_step = (speed == 1 || speed == -1);
145 if (speed == 0) speed = 1000;
148 if (speed == itv->speed && !single_step)
151 s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
153 if (single_step && (speed < 0) == (itv->speed < 0)) {
154 /* Single step video and no need to change direction */
155 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
160 /* Need to change direction */
161 speed = speed < 0 ? -1000 : 1000;
163 data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
164 data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
165 data[1] = (speed < 0);
166 data[2] = speed < 0 ? 3 : 7;
167 data[3] = itv->params.video_b_frames;
168 data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
172 if (speed == 1500 || speed == -1500) data[0] |= 1;
173 else if (speed == 2000 || speed == -2000) data[0] |= 2;
174 else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
175 else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
177 /* If not decoding, just change speed setting */
178 if (atomic_read(&itv->decoding) > 0) {
181 /* Stop all DMA and decoding activity */
182 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
184 /* Wait for any DMA to finish */
185 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
186 while (itv->i_flags & IVTV_F_I_DMA) {
187 got_sig = signal_pending(current);
193 finish_wait(&itv->dma_waitq, &wait);
197 /* Change Speed safely */
198 ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
199 IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
200 data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
203 speed = (speed < 0) ? -1 : 1;
204 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
210 static int ivtv_validate_speed(int cur_speed, int new_speed)
212 int fact = new_speed < 0 ? -1 : 1;
218 new_speed = -new_speed;
220 cur_speed = -cur_speed;
222 if (cur_speed <= new_speed) {
223 if (new_speed > 1500)
225 if (new_speed > 1000)
229 if (new_speed >= 2000)
231 if (new_speed >= 1500)
233 if (new_speed >= 1000)
238 if (new_speed == 1 || new_speed == 1000)
239 return fact * new_speed;
242 new_speed = 1000 / new_speed;
243 if (1000 / cur_speed == new_speed)
244 new_speed += (cur_speed < s) ? -1 : 1;
245 if (new_speed > 60) return 1000 / (fact * 60);
246 return 1000 / (fact * new_speed);
249 static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
250 struct video_command *vc, int try)
252 struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
254 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
258 case VIDEO_CMD_PLAY: {
260 vc->play.speed = ivtv_validate_speed(itv->speed, vc->play.speed);
261 if (vc->play.speed < 0)
262 vc->play.format = VIDEO_PLAY_FMT_GOP;
265 if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
267 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
268 /* forces ivtv_set_speed to be called */
271 return ivtv_start_decoding(id, vc->play.speed);
275 vc->flags &= VIDEO_CMD_STOP_IMMEDIATELY|VIDEO_CMD_STOP_TO_BLACK;
276 if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
279 if (atomic_read(&itv->decoding) == 0)
281 if (itv->output_mode != OUT_MPG)
284 itv->output_mode = OUT_NONE;
285 return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);
287 case VIDEO_CMD_FREEZE:
288 vc->flags &= VIDEO_CMD_FREEZE_TO_BLACK;
290 if (itv->output_mode != OUT_MPG)
292 if (atomic_read(&itv->decoding) > 0) {
293 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
294 (vc->flags & VIDEO_CMD_FREEZE_TO_BLACK) ? 1 : 0);
295 set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
299 case VIDEO_CMD_CONTINUE:
302 if (itv->output_mode != OUT_MPG)
304 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
305 int speed = itv->speed;
307 return ivtv_start_decoding(id, speed);
317 static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
319 struct v4l2_register *regs = arg;
321 volatile u8 __iomem *reg_start;
323 if (!capable(CAP_SYS_ADMIN))
325 if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
326 reg_start = itv->reg_mem - IVTV_REG_OFFSET;
327 else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
328 regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
329 reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
330 else if (regs->reg >= 0 && regs->reg < IVTV_ENCODER_SIZE)
331 reg_start = itv->enc_mem;
335 spin_lock_irqsave(&ivtv_cards_lock, flags);
336 if (cmd == VIDIOC_DBG_G_REGISTER) {
337 regs->val = readl(regs->reg + reg_start);
339 writel(regs->val, regs->reg + reg_start);
341 spin_unlock_irqrestore(&ivtv_cards_lock, flags);
345 static int ivtv_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
347 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
348 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
350 vbifmt->reserved[0] = 0;
351 vbifmt->reserved[1] = 0;
352 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
354 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
356 vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
357 vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
359 vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
360 vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
362 vbifmt->service_set = ivtv_get_service_set(vbifmt);
366 static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
368 struct ivtv_open_id *id = fh;
369 struct ivtv *itv = id->itv;
370 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
372 pixfmt->width = itv->params.width;
373 pixfmt->height = itv->params.height;
374 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
375 pixfmt->field = V4L2_FIELD_INTERLACED;
377 if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
378 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
379 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
381 pixfmt->height * pixfmt->width +
382 pixfmt->height * (pixfmt->width / 2);
383 pixfmt->bytesperline = 720;
385 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
386 pixfmt->sizeimage = 128 * 1024;
387 pixfmt->bytesperline = 0;
392 static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
394 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
395 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
397 vbifmt->sampling_rate = 27000000;
398 vbifmt->offset = 248;
399 vbifmt->samples_per_line = itv->vbi.raw_decoder_line_size - 4;
400 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
401 vbifmt->start[0] = itv->vbi.start[0];
402 vbifmt->start[1] = itv->vbi.start[1];
403 vbifmt->count[0] = vbifmt->count[1] = itv->vbi.count;
405 vbifmt->reserved[0] = 0;
406 vbifmt->reserved[1] = 0;
410 static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
412 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
413 struct ivtv_open_id *id = fh;
414 struct ivtv *itv = id->itv;
416 vbifmt->reserved[0] = 0;
417 vbifmt->reserved[1] = 0;
418 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
420 if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
421 vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
423 ivtv_expand_service_set(vbifmt, itv->is_50hz);
427 itv->video_dec_func(itv, VIDIOC_G_FMT, fmt);
428 vbifmt->service_set = ivtv_get_service_set(vbifmt);
432 static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
434 struct ivtv_open_id *id = fh;
435 struct ivtv *itv = id->itv;
436 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
438 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
440 pixfmt->width = itv->main_rect.width;
441 pixfmt->height = itv->main_rect.height;
442 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
443 pixfmt->field = V4L2_FIELD_INTERLACED;
445 if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
446 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
447 case IVTV_YUV_MODE_INTERLACED:
448 pixfmt->field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
449 V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
451 case IVTV_YUV_MODE_PROGRESSIVE:
452 pixfmt->field = V4L2_FIELD_NONE;
455 pixfmt->field = V4L2_FIELD_ANY;
458 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
459 pixfmt->bytesperline = 720;
460 pixfmt->width = itv->yuv_info.v4l2_src_w;
461 pixfmt->height = itv->yuv_info.v4l2_src_h;
462 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
464 1080 * ((pixfmt->height + 31) & ~31);
466 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
467 pixfmt->sizeimage = 128 * 1024;
468 pixfmt->bytesperline = 0;
473 static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
475 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
476 struct v4l2_window *winfmt = &fmt->fmt.win;
478 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
480 winfmt->chromakey = itv->osd_chroma_key;
481 winfmt->global_alpha = itv->osd_global_alpha;
482 winfmt->field = V4L2_FIELD_INTERLACED;
483 winfmt->clips = NULL;
484 winfmt->clipcount = 0;
485 winfmt->bitmap = NULL;
486 winfmt->w.top = winfmt->w.left = 0;
487 winfmt->w.width = itv->osd_rect.width;
488 winfmt->w.height = itv->osd_rect.height;
492 static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
494 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
497 static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
499 struct ivtv_open_id *id = fh;
500 struct ivtv *itv = id->itv;
501 int w = fmt->fmt.pix.width;
502 int h = fmt->fmt.pix.height;
506 h = min(h, itv->is_50hz ? 576 : 480);
508 ivtv_g_fmt_vid_cap(file, fh, fmt);
509 fmt->fmt.pix.width = w;
510 fmt->fmt.pix.height = h;
514 static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
516 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
519 static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
521 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
522 struct ivtv_open_id *id = fh;
523 struct ivtv *itv = id->itv;
525 if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
526 return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
528 /* set sliced VBI capture format */
529 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
530 vbifmt->reserved[0] = 0;
531 vbifmt->reserved[1] = 0;
533 if (vbifmt->service_set)
534 ivtv_expand_service_set(vbifmt, itv->is_50hz);
535 check_service_set(vbifmt, itv->is_50hz);
536 vbifmt->service_set = ivtv_get_service_set(vbifmt);
540 static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
542 struct ivtv_open_id *id = fh;
547 w = fmt->fmt.pix.width;
548 h = fmt->fmt.pix.height;
549 field = fmt->fmt.pix.field;
550 ret = ivtv_g_fmt_vid_out(file, fh, fmt);
551 fmt->fmt.pix.width = w;
552 fmt->fmt.pix.height = h;
553 if (!ret && id->type == IVTV_DEC_STREAM_TYPE_YUV) {
554 fmt->fmt.pix.field = field;
555 if (fmt->fmt.pix.width < 2)
556 fmt->fmt.pix.width = 2;
557 if (fmt->fmt.pix.width > 720)
558 fmt->fmt.pix.width = 720;
559 if (fmt->fmt.pix.height < 2)
560 fmt->fmt.pix.height = 2;
561 if (fmt->fmt.pix.height > 576)
562 fmt->fmt.pix.height = 576;
567 static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
569 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
570 u32 chromakey = fmt->fmt.win.chromakey;
571 u8 global_alpha = fmt->fmt.win.global_alpha;
573 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
575 ivtv_g_fmt_vid_out_overlay(file, fh, fmt);
576 fmt->fmt.win.chromakey = chromakey;
577 fmt->fmt.win.global_alpha = global_alpha;
581 static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
583 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
586 static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
588 struct ivtv_open_id *id = fh;
589 struct ivtv *itv = id->itv;
590 struct cx2341x_mpeg_params *p = &itv->params;
591 int w = fmt->fmt.pix.width;
592 int h = fmt->fmt.pix.height;
593 int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
598 if (p->width == w && p->height == h)
601 if (atomic_read(&itv->capturing) > 0)
606 if (p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
607 fmt->fmt.pix.width /= 2;
608 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
609 return ivtv_g_fmt_vid_cap(file, fh, fmt);
612 static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
614 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
616 itv->vbi.sliced_in->service_set = 0;
617 itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
618 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
621 static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
623 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
624 struct ivtv_open_id *id = fh;
625 struct ivtv *itv = id->itv;
626 int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
628 if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
631 if (check_service_set(vbifmt, itv->is_50hz) == 0)
633 if (atomic_read(&itv->capturing) > 0)
635 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
636 memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
640 static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
642 struct ivtv_open_id *id = fh;
643 struct ivtv *itv = id->itv;
644 struct yuv_playback_info *yi = &itv->yuv_info;
645 int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
650 if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
653 /* Return now if we already have some frame data */
657 yi->v4l2_src_w = fmt->fmt.pix.width;
658 yi->v4l2_src_h = fmt->fmt.pix.height;
660 switch (fmt->fmt.pix.field) {
661 case V4L2_FIELD_NONE:
662 yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
665 yi->lace_mode = IVTV_YUV_MODE_AUTO;
667 case V4L2_FIELD_INTERLACED_BT:
669 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
671 case V4L2_FIELD_INTERLACED_TB:
673 yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
676 yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
678 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
679 itv->dma_data_req_size =
680 1080 * ((yi->v4l2_src_h + 31) & ~31);
682 /* Force update of yuv registers */
683 yi->yuv_forced_update = 1;
687 static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
689 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
690 int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
693 itv->osd_chroma_key = fmt->fmt.win.chromakey;
694 itv->osd_global_alpha = fmt->fmt.win.global_alpha;
695 ivtv_set_osd_alpha(itv);
700 static int ivtv_g_chip_ident(struct file *file, void *fh, struct v4l2_chip_ident *chip)
702 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
704 chip->ident = V4L2_IDENT_NONE;
706 if (chip->match_type == V4L2_CHIP_MATCH_HOST) {
707 if (v4l2_chip_match_host(chip->match_type, chip->match_chip))
708 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
711 if (chip->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
712 return ivtv_i2c_id(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
713 if (chip->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
714 return ivtv_call_i2c_client(itv, chip->match_chip, VIDIOC_G_CHIP_IDENT, chip);
718 static int ivtv_g_register(struct file *file, void *fh, struct v4l2_register *reg)
720 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
722 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
723 return ivtv_itvc(itv, VIDIOC_DBG_G_REGISTER, reg);
724 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
725 return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
726 return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_G_REGISTER, reg);
729 static int ivtv_s_register(struct file *file, void *fh, struct v4l2_register *reg)
731 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
733 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
734 return ivtv_itvc(itv, VIDIOC_DBG_S_REGISTER, reg);
735 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
736 return ivtv_i2c_id(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
737 return ivtv_call_i2c_client(itv, reg->match_chip, VIDIOC_DBG_S_REGISTER, reg);
740 static int ivtv_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
742 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
744 *p = v4l2_prio_max(&itv->prio);
749 static int ivtv_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
751 struct ivtv_open_id *id = fh;
752 struct ivtv *itv = id->itv;
754 return v4l2_prio_change(&itv->prio, &id->prio, prio);
757 static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
759 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
761 strlcpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
762 strlcpy(vcap->card, itv->card_name, sizeof(vcap->card));
763 strlcpy(vcap->bus_info, pci_name(itv->dev), sizeof(vcap->bus_info));
764 vcap->version = IVTV_DRIVER_VERSION; /* version */
765 vcap->capabilities = itv->v4l2_cap; /* capabilities */
769 static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
771 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
773 return ivtv_get_audio_input(itv, vin->index, vin);
776 static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
778 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
780 vin->index = itv->audio_input;
781 return ivtv_get_audio_input(itv, vin->index, vin);
784 static int ivtv_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
786 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
788 if (vout->index >= itv->nof_audio_inputs)
791 itv->audio_input = vout->index;
792 ivtv_audio_set_io(itv);
797 static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
799 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
801 /* set it to defaults from our table */
802 return ivtv_get_audio_output(itv, vin->index, vin);
805 static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
807 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
810 return ivtv_get_audio_output(itv, vin->index, vin);
813 static int ivtv_s_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
815 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
817 return ivtv_get_audio_output(itv, vout->index, vout);
820 static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
822 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
824 /* set it to defaults from our table */
825 return ivtv_get_input(itv, vin->index, vin);
828 static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
830 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
832 return ivtv_get_output(itv, vout->index, vout);
835 static int ivtv_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
837 struct ivtv_open_id *id = fh;
838 struct ivtv *itv = id->itv;
839 struct yuv_playback_info *yi = &itv->yuv_info;
842 streamtype = id->type;
844 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
846 cropcap->bounds.top = cropcap->bounds.left = 0;
847 cropcap->bounds.width = 720;
848 if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
849 cropcap->bounds.height = itv->is_50hz ? 576 : 480;
850 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
851 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
852 } else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
854 cropcap->bounds.width = yi->osd_full_w;
855 cropcap->bounds.height = yi->osd_full_h;
857 cropcap->bounds.width = 720;
858 cropcap->bounds.height =
859 itv->is_out_50hz ? 576 : 480;
861 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
862 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
864 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
865 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
866 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
868 cropcap->defrect = cropcap->bounds;
872 static int ivtv_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
874 struct ivtv_open_id *id = fh;
875 struct ivtv *itv = id->itv;
876 struct yuv_playback_info *yi = &itv->yuv_info;
879 streamtype = id->type;
881 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
882 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
883 /* Should be replaced */
884 /* v4l_printk_ioctl(VIDIOC_S_CROP); */
887 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
888 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
889 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
890 yi->main_rect = crop->c;
893 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
894 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
895 itv->main_rect = crop->c;
904 static int ivtv_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
906 struct ivtv_open_id *id = fh;
907 struct ivtv *itv = id->itv;
908 struct yuv_playback_info *yi = &itv->yuv_info;
911 streamtype = id->type;
913 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
914 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
915 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
916 crop->c = yi->main_rect;
918 crop->c = itv->main_rect;
924 static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
926 static struct v4l2_fmtdesc formats[] = {
928 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
931 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
932 "MPEG", V4L2_PIX_FMT_MPEG,
936 enum v4l2_buf_type type = fmt->type;
941 *fmt = formats[fmt->index];
946 static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
948 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
950 static struct v4l2_fmtdesc formats[] = {
952 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
955 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
956 "MPEG", V4L2_PIX_FMT_MPEG,
960 enum v4l2_buf_type type = fmt->type;
962 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
968 *fmt = formats[fmt->index];
974 static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
976 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
978 *i = itv->active_input;
983 int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
985 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
987 if (inp < 0 || inp >= itv->nof_inputs)
990 if (inp == itv->active_input) {
991 IVTV_DEBUG_INFO("Input unchanged\n");
995 if (atomic_read(&itv->capturing) > 0) {
999 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
1000 itv->active_input, inp);
1002 itv->active_input = inp;
1003 /* Set the audio input to whatever is appropriate for the
1005 itv->audio_input = itv->card->video_inputs[inp].audio_index;
1007 /* prevent others from messing with the streams until
1008 we're finished changing inputs. */
1010 ivtv_video_set_io(itv);
1011 ivtv_audio_set_io(itv);
1017 static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1019 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1021 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1024 *i = itv->active_output;
1029 static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1031 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1032 struct v4l2_routing route;
1034 if (outp >= itv->card->nof_outputs)
1037 if (outp == itv->active_output) {
1038 IVTV_DEBUG_INFO("Output unchanged\n");
1041 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1042 itv->active_output, outp);
1044 itv->active_output = outp;
1045 route.input = SAA7127_INPUT_TYPE_NORMAL;
1046 route.output = itv->card->video_outputs[outp].video_output;
1047 ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
1052 static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1054 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1059 ivtv_call_i2c_clients(itv, VIDIOC_G_FREQUENCY, vf);
1063 int ivtv_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1065 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1071 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1072 ivtv_call_i2c_clients(itv, VIDIOC_S_FREQUENCY, vf);
1077 static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1079 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1085 int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
1087 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1088 struct yuv_playback_info *yi = &itv->yuv_info;
1090 if ((*std & V4L2_STD_ALL) == 0)
1093 if (*std == itv->std)
1096 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1097 atomic_read(&itv->capturing) > 0 ||
1098 atomic_read(&itv->decoding) > 0) {
1099 /* Switching standard would turn off the radio or mess
1100 with already running streams, prevent that by
1106 itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
1107 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
1108 itv->params.width = 720;
1109 itv->params.height = itv->is_50hz ? 576 : 480;
1110 itv->vbi.count = itv->is_50hz ? 18 : 12;
1111 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1112 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1114 if (itv->hw_flags & IVTV_HW_CX25840)
1115 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1117 IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
1120 ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
1122 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1123 /* set display standard */
1124 itv->std_out = *std;
1125 itv->is_out_60hz = itv->is_60hz;
1126 itv->is_out_50hz = itv->is_50hz;
1127 ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
1128 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1129 itv->main_rect.left = itv->main_rect.top = 0;
1130 itv->main_rect.width = 720;
1131 itv->main_rect.height = itv->params.height;
1132 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1133 720, itv->main_rect.height, 0, 0);
1134 yi->main_rect = itv->main_rect;
1135 if (!itv->osd_info) {
1136 yi->osd_full_w = 720;
1137 yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1143 static int ivtv_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1145 struct ivtv_open_id *id = fh;
1146 struct ivtv *itv = id->itv;
1151 ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
1156 static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1158 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1163 ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
1165 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1166 strlcpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1167 vt->type = V4L2_TUNER_RADIO;
1169 strlcpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1170 vt->type = V4L2_TUNER_ANALOG_TV;
1176 static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1178 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1179 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1182 if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1183 for (f = 0; f < 2; f++) {
1184 for (l = 0; l < 24; l++) {
1185 if (valid_service_line(f, l, itv->is_50hz))
1186 cap->service_lines[f][l] = set;
1191 if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1192 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1195 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1196 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1198 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1199 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1206 static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1208 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1209 struct v4l2_enc_idx_entry *e = idx->entry;
1213 entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1215 if (entries > V4L2_ENC_IDX_ENTRIES)
1216 entries = V4L2_ENC_IDX_ENTRIES;
1218 for (i = 0; i < entries; i++) {
1219 *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1220 if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1225 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1229 static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1231 struct ivtv_open_id *id = fh;
1232 struct ivtv *itv = id->itv;
1236 case V4L2_ENC_CMD_START:
1237 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1239 return ivtv_start_capture(id);
1241 case V4L2_ENC_CMD_STOP:
1242 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1243 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1244 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1247 case V4L2_ENC_CMD_PAUSE:
1248 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1251 if (!atomic_read(&itv->capturing))
1253 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1257 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1260 case V4L2_ENC_CMD_RESUME:
1261 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1264 if (!atomic_read(&itv->capturing))
1267 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1270 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1274 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1281 static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1283 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1286 case V4L2_ENC_CMD_START:
1287 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1291 case V4L2_ENC_CMD_STOP:
1292 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1293 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1296 case V4L2_ENC_CMD_PAUSE:
1297 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1301 case V4L2_ENC_CMD_RESUME:
1302 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1306 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1311 static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1313 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1314 u32 data[CX2341X_MBOX_MAX_DATA];
1315 struct yuv_playback_info *yi = &itv->yuv_info;
1318 static u32 pixel_format[16] = {
1319 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1320 V4L2_PIX_FMT_RGB565,
1321 V4L2_PIX_FMT_RGB555,
1322 V4L2_PIX_FMT_RGB444,
1327 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1328 V4L2_PIX_FMT_YUV565,
1329 V4L2_PIX_FMT_YUV555,
1330 V4L2_PIX_FMT_YUV444,
1337 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1339 if (!itv->osd_video_pbase)
1342 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1343 V4L2_FBUF_CAP_GLOBAL_ALPHA;
1345 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1346 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1347 pixfmt = (data[0] >> 3) & 0xf;
1349 fb->fmt.pixelformat = pixel_format[pixfmt];
1350 fb->fmt.width = itv->osd_rect.width;
1351 fb->fmt.height = itv->osd_rect.height;
1352 fb->fmt.field = V4L2_FIELD_INTERLACED;
1353 fb->fmt.bytesperline = fb->fmt.width;
1354 fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
1355 fb->fmt.field = V4L2_FIELD_INTERLACED;
1357 if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
1358 fb->fmt.bytesperline *= 2;
1359 if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
1360 fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
1361 fb->fmt.bytesperline *= 2;
1362 fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
1363 fb->base = (void *)itv->osd_video_pbase;
1366 if (itv->osd_chroma_key_state)
1367 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1369 if (itv->osd_global_alpha_state)
1370 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1374 /* no local alpha for RGB565 or unknown formats */
1375 if (pixfmt == 1 || pixfmt > 4)
1378 /* 16-bit formats have inverted local alpha */
1379 if (pixfmt == 2 || pixfmt == 3)
1380 fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1382 fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1384 if (itv->osd_local_alpha_state) {
1385 /* 16-bit formats have inverted local alpha */
1386 if (pixfmt == 2 || pixfmt == 3)
1387 fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1389 fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1392 fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1397 static int ivtv_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1399 struct ivtv_open_id *id = fh;
1400 struct ivtv *itv = id->itv;
1401 struct yuv_playback_info *yi = &itv->yuv_info;
1403 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1405 if (!itv->osd_video_pbase)
1408 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1409 itv->osd_local_alpha_state =
1410 (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1411 itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1412 ivtv_set_osd_alpha(itv);
1413 yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1414 return ivtv_g_fbuf(file, fh, fb);
1417 static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1419 struct ivtv_open_id *id = fh;
1420 struct ivtv *itv = id->itv;
1422 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1425 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1430 static int ivtv_log_status(struct file *file, void *fh)
1432 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1433 u32 data[CX2341X_MBOX_MAX_DATA];
1435 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1436 struct v4l2_input vidin;
1437 struct v4l2_audio audin;
1440 IVTV_INFO("================= START STATUS CARD #%d =================\n", itv->num);
1441 IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1442 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1445 ivtv_read_eeprom(itv, &tv);
1447 ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
1448 ivtv_get_input(itv, itv->active_input, &vidin);
1449 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1450 IVTV_INFO("Video Input: %s\n", vidin.name);
1451 IVTV_INFO("Audio Input: %s%s\n", audin.name,
1452 (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
1454 struct v4l2_output vidout;
1455 struct v4l2_audioout audout;
1456 int mode = itv->output_mode;
1457 static const char * const output_modes[5] = {
1464 static const char * const audio_modes[5] = {
1471 static const char * const alpha_mode[4] = {
1477 static const char * const pixel_format[16] = {
1496 ivtv_get_output(itv, itv->active_output, &vidout);
1497 ivtv_get_audio_output(itv, 0, &audout);
1498 IVTV_INFO("Video Output: %s\n", vidout.name);
1499 IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
1500 audio_modes[itv->audio_stereo_mode],
1501 audio_modes[itv->audio_bilingual_mode]);
1502 if (mode < 0 || mode > OUT_PASSTHROUGH)
1504 IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1505 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1506 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1507 IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
1508 data[0] & 1 ? "On" : "Off",
1509 alpha_mode[(data[0] >> 1) & 0x3],
1510 pixel_format[(data[0] >> 3) & 0xf]);
1512 IVTV_INFO("Tuner: %s\n",
1513 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1514 cx2341x_log_status(&itv->params, itv->name);
1515 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1516 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1517 struct ivtv_stream *s = &itv->streams[i];
1519 if (s->v4l2dev == NULL || s->buffers == 0)
1521 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1522 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1523 (s->buffers * s->buf_size) / 1024, s->buffers);
1526 IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n", (long long)itv->mpg_data_received, (long long)itv->vbi_data_inserted);
1527 IVTV_INFO("================== END STATUS CARD #%d ==================\n", itv->num);
1532 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1534 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1535 struct ivtv *itv = id->itv;
1536 int nonblocking = filp->f_flags & O_NONBLOCK;
1537 struct ivtv_stream *s = &itv->streams[id->type];
1540 case IVTV_IOC_DMA_FRAME: {
1541 struct ivtv_dma_frame *args = arg;
1543 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1544 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1546 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1548 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1550 if (ivtv_start_decoding(id, id->type)) {
1553 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1554 ivtv_release_stream(s);
1557 /* Mark that this file handle started the UDMA_YUV mode */
1559 if (args->y_source == NULL)
1561 return ivtv_yuv_prep_frame(itv, args);
1564 case VIDEO_GET_PTS: {
1565 u32 data[CX2341X_MBOX_MAX_DATA];
1568 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1569 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1573 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1576 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1577 *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1578 (u64)itv->last_dec_timing[1];
1582 if (atomic_read(&itv->decoding)) {
1583 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1584 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1587 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1588 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1589 *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1590 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1595 case VIDEO_GET_FRAME_COUNT: {
1596 u32 data[CX2341X_MBOX_MAX_DATA];
1599 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1600 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1604 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1607 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1608 *frame = itv->last_dec_timing[0];
1612 if (atomic_read(&itv->decoding)) {
1613 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1614 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1617 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1618 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1625 struct video_command vc;
1627 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1628 memset(&vc, 0, sizeof(vc));
1629 vc.cmd = VIDEO_CMD_PLAY;
1630 return ivtv_video_command(itv, id, &vc, 0);
1634 struct video_command vc;
1636 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1637 memset(&vc, 0, sizeof(vc));
1638 vc.cmd = VIDEO_CMD_STOP;
1639 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1640 return ivtv_video_command(itv, id, &vc, 0);
1643 case VIDEO_FREEZE: {
1644 struct video_command vc;
1646 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1647 memset(&vc, 0, sizeof(vc));
1648 vc.cmd = VIDEO_CMD_FREEZE;
1649 return ivtv_video_command(itv, id, &vc, 0);
1652 case VIDEO_CONTINUE: {
1653 struct video_command vc;
1655 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1656 memset(&vc, 0, sizeof(vc));
1657 vc.cmd = VIDEO_CMD_CONTINUE;
1658 return ivtv_video_command(itv, id, &vc, 0);
1662 case VIDEO_TRY_COMMAND: {
1663 struct video_command *vc = arg;
1664 int try = (cmd == VIDEO_TRY_COMMAND);
1667 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
1669 IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
1670 return ivtv_video_command(itv, id, vc, try);
1673 case VIDEO_GET_EVENT: {
1674 struct video_event *ev = arg;
1677 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1678 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1680 memset(ev, 0, sizeof(*ev));
1681 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1684 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1685 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1686 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1687 ev->type = VIDEO_EVENT_VSYNC;
1688 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1689 VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1690 if (itv->output_mode == OUT_UDMA_YUV &&
1691 (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1692 IVTV_YUV_MODE_PROGRESSIVE) {
1693 ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1700 /* Wait for event. Note that serialize_lock is locked,
1701 so to allow other processes to access the driver while
1702 we are waiting unlock first and later lock again. */
1703 mutex_unlock(&itv->serialize_lock);
1704 prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1705 if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
1707 finish_wait(&itv->event_waitq, &wait);
1708 mutex_lock(&itv->serialize_lock);
1709 if (signal_pending(current)) {
1710 /* return if a signal was received */
1711 IVTV_DEBUG_INFO("User stopped wait for event\n");
1724 static int ivtv_default(struct file *file, void *fh, int cmd, void *arg)
1726 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1729 case VIDIOC_INT_S_AUDIO_ROUTING: {
1730 struct v4l2_routing *route = arg;
1732 ivtv_i2c_hw(itv, itv->card->hw_audio, VIDIOC_INT_S_AUDIO_ROUTING, route);
1736 case VIDIOC_INT_RESET: {
1737 u32 val = *(u32 *)arg;
1739 if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1740 ivtv_reset_ir_gpio(itv);
1742 itv->video_dec_func(itv, cmd, NULL);
1752 static int ivtv_serialized_ioctl(struct ivtv *itv, struct inode *inode, struct file *filp,
1753 unsigned int cmd, unsigned long arg)
1755 struct video_device *vfd = video_devdata(filp);
1756 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1759 /* Filter dvb ioctls that cannot be handled by the v4l ioctl framework */
1761 case VIDEO_SELECT_SOURCE:
1762 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1763 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1765 return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
1767 case AUDIO_SET_MUTE:
1768 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1769 itv->speed_mute_audio = arg;
1772 case AUDIO_CHANNEL_SELECT:
1773 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1774 if (arg > AUDIO_STEREO_SWAPPED)
1776 itv->audio_stereo_mode = arg;
1777 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1780 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1781 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1782 if (arg > AUDIO_STEREO_SWAPPED)
1784 itv->audio_bilingual_mode = arg;
1785 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1788 case IVTV_IOC_DMA_FRAME:
1790 case VIDEO_GET_FRAME_COUNT:
1791 case VIDEO_GET_EVENT:
1795 case VIDEO_CONTINUE:
1797 case VIDEO_TRY_COMMAND:
1798 return ivtv_decoder_ioctls(filp, cmd, (void *)arg);
1804 /* check priority */
1808 case VIDIOC_S_INPUT:
1809 case VIDIOC_S_OUTPUT:
1810 case VIDIOC_S_TUNER:
1811 case VIDIOC_S_FREQUENCY:
1814 case VIDIOC_S_AUDIO:
1815 case VIDIOC_S_AUDOUT:
1816 case VIDIOC_S_EXT_CTRLS:
1818 case VIDIOC_OVERLAY:
1819 ret = v4l2_prio_check(&itv->prio, &id->prio);
1824 if (ivtv_debug & IVTV_DBGFLG_IOCTL)
1825 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
1826 ret = video_ioctl2(inode, filp, cmd, arg);
1831 int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1834 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1835 struct ivtv *itv = id->itv;
1838 mutex_lock(&itv->serialize_lock);
1839 res = ivtv_serialized_ioctl(itv, inode, filp, cmd, arg);
1840 mutex_unlock(&itv->serialize_lock);
1844 void ivtv_set_funcs(struct video_device *vdev)
1846 vdev->vidioc_querycap = ivtv_querycap;
1847 vdev->vidioc_g_priority = ivtv_g_priority;
1848 vdev->vidioc_s_priority = ivtv_s_priority;
1849 vdev->vidioc_s_audio = ivtv_s_audio;
1850 vdev->vidioc_g_audio = ivtv_g_audio;
1851 vdev->vidioc_enumaudio = ivtv_enumaudio;
1852 vdev->vidioc_s_audout = ivtv_s_audout;
1853 vdev->vidioc_g_audout = ivtv_g_audout;
1854 vdev->vidioc_enum_input = ivtv_enum_input;
1855 vdev->vidioc_enum_output = ivtv_enum_output;
1856 vdev->vidioc_enumaudout = ivtv_enumaudout;
1857 vdev->vidioc_cropcap = ivtv_cropcap;
1858 vdev->vidioc_s_crop = ivtv_s_crop;
1859 vdev->vidioc_g_crop = ivtv_g_crop;
1860 vdev->vidioc_g_input = ivtv_g_input;
1861 vdev->vidioc_s_input = ivtv_s_input;
1862 vdev->vidioc_g_output = ivtv_g_output;
1863 vdev->vidioc_s_output = ivtv_s_output;
1864 vdev->vidioc_g_frequency = ivtv_g_frequency;
1865 vdev->vidioc_s_frequency = ivtv_s_frequency;
1866 vdev->vidioc_s_tuner = ivtv_s_tuner;
1867 vdev->vidioc_g_tuner = ivtv_g_tuner;
1868 vdev->vidioc_g_enc_index = ivtv_g_enc_index;
1869 vdev->vidioc_g_fbuf = ivtv_g_fbuf;
1870 vdev->vidioc_s_fbuf = ivtv_s_fbuf;
1871 vdev->vidioc_g_std = ivtv_g_std;
1872 vdev->vidioc_s_std = ivtv_s_std;
1873 vdev->vidioc_overlay = ivtv_overlay;
1874 vdev->vidioc_log_status = ivtv_log_status;
1875 vdev->vidioc_enum_fmt_vid_cap = ivtv_enum_fmt_vid_cap;
1876 vdev->vidioc_encoder_cmd = ivtv_encoder_cmd;
1877 vdev->vidioc_try_encoder_cmd = ivtv_try_encoder_cmd;
1878 vdev->vidioc_enum_fmt_vid_out = ivtv_enum_fmt_vid_out;
1879 vdev->vidioc_g_fmt_vid_cap = ivtv_g_fmt_vid_cap;
1880 vdev->vidioc_g_fmt_vbi_cap = ivtv_g_fmt_vbi_cap;
1881 vdev->vidioc_g_fmt_sliced_vbi_cap = ivtv_g_fmt_sliced_vbi_cap;
1882 vdev->vidioc_g_fmt_vid_out = ivtv_g_fmt_vid_out;
1883 vdev->vidioc_g_fmt_vid_out_overlay = ivtv_g_fmt_vid_out_overlay;
1884 vdev->vidioc_g_fmt_sliced_vbi_out = ivtv_g_fmt_sliced_vbi_out;
1885 vdev->vidioc_s_fmt_vid_cap = ivtv_s_fmt_vid_cap;
1886 vdev->vidioc_s_fmt_vbi_cap = ivtv_s_fmt_vbi_cap;
1887 vdev->vidioc_s_fmt_sliced_vbi_cap = ivtv_s_fmt_sliced_vbi_cap;
1888 vdev->vidioc_s_fmt_vid_out = ivtv_s_fmt_vid_out;
1889 vdev->vidioc_s_fmt_vid_out_overlay = ivtv_s_fmt_vid_out_overlay;
1890 vdev->vidioc_s_fmt_sliced_vbi_out = ivtv_s_fmt_sliced_vbi_out;
1891 vdev->vidioc_try_fmt_vid_cap = ivtv_try_fmt_vid_cap;
1892 vdev->vidioc_try_fmt_vbi_cap = ivtv_try_fmt_vbi_cap;
1893 vdev->vidioc_try_fmt_sliced_vbi_cap = ivtv_try_fmt_sliced_vbi_cap;
1894 vdev->vidioc_try_fmt_vid_out = ivtv_try_fmt_vid_out;
1895 vdev->vidioc_try_fmt_vid_out_overlay = ivtv_try_fmt_vid_out_overlay;
1896 vdev->vidioc_try_fmt_sliced_vbi_out = ivtv_try_fmt_sliced_vbi_out;
1897 vdev->vidioc_g_sliced_vbi_cap = ivtv_g_sliced_vbi_cap;
1898 vdev->vidioc_g_chip_ident = ivtv_g_chip_ident;
1899 vdev->vidioc_g_register = ivtv_g_register;
1900 vdev->vidioc_s_register = ivtv_s_register;
1901 vdev->vidioc_default = ivtv_default;
1902 vdev->vidioc_queryctrl = ivtv_queryctrl;
1903 vdev->vidioc_querymenu = ivtv_querymenu;
1904 vdev->vidioc_g_ctrl = ivtv_g_ctrl;
1905 vdev->vidioc_s_ctrl = ivtv_s_ctrl;
1906 vdev->vidioc_g_ext_ctrls = ivtv_g_ext_ctrls;
1907 vdev->vidioc_s_ext_ctrls = ivtv_s_ext_ctrls;
1908 vdev->vidioc_try_ext_ctrls = ivtv_try_ext_ctrls;