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 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 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 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_get_fmt(struct ivtv *itv, int streamtype, struct v4l2_format *fmt)
379 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
380 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
382 fmt->fmt.pix.width = itv->main_rect.width;
383 fmt->fmt.pix.height = itv->main_rect.height;
384 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
385 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
386 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
387 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
388 case IVTV_YUV_MODE_INTERLACED:
389 fmt->fmt.pix.field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
390 V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
392 case IVTV_YUV_MODE_PROGRESSIVE:
393 fmt->fmt.pix.field = V4L2_FIELD_NONE;
396 fmt->fmt.pix.field = V4L2_FIELD_ANY;
399 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
400 fmt->fmt.pix.bytesperline = 720;
401 fmt->fmt.pix.width = itv->yuv_info.v4l2_src_w;
402 fmt->fmt.pix.height = itv->yuv_info.v4l2_src_h;
403 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
404 fmt->fmt.pix.sizeimage =
405 1080 * ((fmt->fmt.pix.height + 31) & ~31);
406 } else if (streamtype == IVTV_ENC_STREAM_TYPE_YUV) {
407 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
408 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
409 fmt->fmt.pix.sizeimage =
410 fmt->fmt.pix.height * fmt->fmt.pix.width +
411 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
413 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
414 fmt->fmt.pix.sizeimage = 128 * 1024;
418 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
419 fmt->fmt.pix.width = itv->params.width;
420 fmt->fmt.pix.height = itv->params.height;
421 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
422 fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
423 if (streamtype == IVTV_ENC_STREAM_TYPE_YUV ||
424 streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
425 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
426 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
427 fmt->fmt.pix.sizeimage =
428 fmt->fmt.pix.height * fmt->fmt.pix.width +
429 fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
431 fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
432 fmt->fmt.pix.sizeimage = 128 * 1024;
436 case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
437 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
439 fmt->fmt.win.chromakey = itv->osd_chroma_key;
440 fmt->fmt.win.global_alpha = itv->osd_global_alpha;
443 case V4L2_BUF_TYPE_VBI_CAPTURE:
444 fmt->fmt.vbi.sampling_rate = 27000000;
445 fmt->fmt.vbi.offset = 248;
446 fmt->fmt.vbi.samples_per_line = itv->vbi.raw_decoder_line_size - 4;
447 fmt->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
448 fmt->fmt.vbi.start[0] = itv->vbi.start[0];
449 fmt->fmt.vbi.start[1] = itv->vbi.start[1];
450 fmt->fmt.vbi.count[0] = fmt->fmt.vbi.count[1] = itv->vbi.count;
453 case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
455 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
457 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
459 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
460 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
461 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
463 vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
464 vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
466 vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
467 vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
469 vbifmt->service_set = get_service_set(vbifmt);
473 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
475 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
477 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
478 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
479 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
481 if (streamtype == IVTV_DEC_STREAM_TYPE_VBI) {
482 vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
484 expand_service_set(vbifmt, itv->is_50hz);
488 itv->video_dec_func(itv, VIDIOC_G_FMT, fmt);
489 vbifmt->service_set = get_service_set(vbifmt);
492 case V4L2_BUF_TYPE_VBI_OUTPUT:
493 case V4L2_BUF_TYPE_VIDEO_OVERLAY:
500 static int ivtv_try_or_set_fmt(struct ivtv *itv, int streamtype,
501 struct v4l2_format *fmt, int set_fmt)
503 struct yuv_playback_info *yi = &itv->yuv_info;
504 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
507 if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
511 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
513 field = fmt->fmt.pix.field;
516 r.width = fmt->fmt.pix.width;
517 r.height = fmt->fmt.pix.height;
518 ivtv_get_fmt(itv, streamtype, fmt);
519 fmt->fmt.pix.width = r.width;
520 fmt->fmt.pix.height = r.height;
521 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
522 fmt->fmt.pix.field = field;
523 if (fmt->fmt.pix.width < 2)
524 fmt->fmt.pix.width = 2;
525 if (fmt->fmt.pix.width > 720)
526 fmt->fmt.pix.width = 720;
527 if (fmt->fmt.pix.height < 2)
528 fmt->fmt.pix.height = 2;
529 if (fmt->fmt.pix.height > 576)
530 fmt->fmt.pix.height = 576;
532 if (set_fmt && streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
533 /* Return now if we already have some frame data */
537 yi->v4l2_src_w = r.width;
538 yi->v4l2_src_h = r.height;
541 case V4L2_FIELD_NONE:
542 yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
545 yi->lace_mode = IVTV_YUV_MODE_AUTO;
547 case V4L2_FIELD_INTERLACED_BT:
549 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
551 case V4L2_FIELD_INTERLACED_TB:
553 yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
556 yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
558 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
559 itv->dma_data_req_size =
560 1080 * ((yi->v4l2_src_h + 31) & ~31);
562 /* Force update of yuv registers */
563 yi->yuv_forced_update = 1;
569 if (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY) {
570 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
573 itv->osd_chroma_key = fmt->fmt.win.chromakey;
574 itv->osd_global_alpha = fmt->fmt.win.global_alpha;
575 ivtv_set_osd_alpha(itv);
580 /* set window size */
581 if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
582 struct cx2341x_mpeg_params *p = &itv->params;
583 int w = fmt->fmt.pix.width;
584 int h = fmt->fmt.pix.height;
586 if (w > 720) w = 720;
587 else if (w < 1) w = 1;
588 if (h > (itv->is_50hz ? 576 : 480)) h = (itv->is_50hz ? 576 : 480);
589 else if (h < 2) h = 2;
590 ivtv_get_fmt(itv, streamtype, fmt);
591 fmt->fmt.pix.width = w;
592 fmt->fmt.pix.height = h;
594 if (!set_fmt || (p->width == w && p->height == h))
596 if (atomic_read(&itv->capturing) > 0)
601 if (w != 720 || h != (itv->is_50hz ? 576 : 480))
602 p->video_temporal_filter = 0;
604 p->video_temporal_filter = 8;
605 if (p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
606 fmt->fmt.pix.width /= 2;
607 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
608 return ivtv_get_fmt(itv, streamtype, fmt);
611 /* set raw VBI format */
612 if (fmt->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
613 if (set_fmt && atomic_read(&itv->capturing) > 0) {
617 itv->vbi.sliced_in->service_set = 0;
618 itv->video_dec_func(itv, VIDIOC_S_FMT, &itv->vbi.in);
620 return ivtv_get_fmt(itv, streamtype, fmt);
623 /* set sliced VBI output
624 In principle the user could request that only certain
625 VBI types are output and that the others are ignored.
626 I.e., suppress CC in the even fields or only output
627 WSS and no VPS. Currently though there is no choice. */
628 if (fmt->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT)
629 return ivtv_get_fmt(itv, streamtype, fmt);
631 /* any else but sliced VBI capture is an error */
632 if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
635 if (streamtype == IVTV_DEC_STREAM_TYPE_VBI)
636 return ivtv_get_fmt(itv, streamtype, fmt);
638 /* set sliced VBI capture format */
639 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
640 memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
642 if (vbifmt->service_set)
643 expand_service_set(vbifmt, itv->is_50hz);
644 set = check_service_set(vbifmt, itv->is_50hz);
645 vbifmt->service_set = get_service_set(vbifmt);
651 if (atomic_read(&itv->capturing) > 0) {
654 itv->video_dec_func(itv, VIDIOC_S_FMT, fmt);
655 memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
659 static int ivtv_debug_ioctls(struct file *filp, unsigned int cmd, void *arg)
661 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
662 struct ivtv *itv = id->itv;
663 struct v4l2_register *reg = arg;
666 /* ioctls to allow direct access to the encoder registers for testing */
667 case VIDIOC_DBG_G_REGISTER:
668 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
669 return ivtv_itvc(itv, cmd, arg);
670 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
671 return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
672 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
674 case VIDIOC_DBG_S_REGISTER:
675 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
676 return ivtv_itvc(itv, cmd, arg);
677 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
678 return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
679 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
681 case VIDIOC_G_CHIP_IDENT: {
682 struct v4l2_chip_ident *chip = arg;
684 chip->ident = V4L2_IDENT_NONE;
686 if (reg->match_type == V4L2_CHIP_MATCH_HOST) {
687 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
688 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
691 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
692 return ivtv_i2c_id(itv, reg->match_chip, cmd, arg);
693 if (reg->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
694 return ivtv_call_i2c_client(itv, reg->match_chip, cmd, arg);
698 case VIDIOC_INT_S_AUDIO_ROUTING: {
699 struct v4l2_routing *route = arg;
701 ivtv_i2c_hw(itv, itv->card->hw_audio, VIDIOC_INT_S_AUDIO_ROUTING, route);
705 case VIDIOC_INT_RESET: {
706 u32 val = *(u32 *)arg;
708 if ((val == 0 && itv->options.newi2c) || (val & 0x01)) {
709 ivtv_reset_ir_gpio(itv);
712 itv->video_dec_func(itv, cmd, NULL);
723 int ivtv_v4l2_ioctls(struct ivtv *itv, struct file *filp, unsigned int cmd, void *arg)
725 struct ivtv_open_id *id = NULL;
726 struct yuv_playback_info *yi = &itv->yuv_info;
727 u32 data[CX2341X_MBOX_MAX_DATA];
731 id = (struct ivtv_open_id *)filp->private_data;
732 streamtype = id->type;
736 case VIDIOC_G_PRIORITY:
738 enum v4l2_priority *p = arg;
740 *p = v4l2_prio_max(&itv->prio);
744 case VIDIOC_S_PRIORITY:
746 enum v4l2_priority *prio = arg;
748 return v4l2_prio_change(&itv->prio, &id->prio, *prio);
751 case VIDIOC_QUERYCAP:{
752 struct v4l2_capability *vcap = arg;
754 memset(vcap, 0, sizeof(*vcap));
755 strlcpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
756 strlcpy(vcap->card, itv->card_name, sizeof(vcap->card));
757 strlcpy(vcap->bus_info, pci_name(itv->dev), sizeof(vcap->bus_info));
758 vcap->version = IVTV_DRIVER_VERSION; /* version */
759 vcap->capabilities = itv->v4l2_cap; /* capabilities */
761 /* reserved.. must set to 0! */
762 vcap->reserved[0] = vcap->reserved[1] =
763 vcap->reserved[2] = vcap->reserved[3] = 0;
767 case VIDIOC_ENUMAUDIO:{
768 struct v4l2_audio *vin = arg;
770 return ivtv_get_audio_input(itv, vin->index, vin);
773 case VIDIOC_G_AUDIO:{
774 struct v4l2_audio *vin = arg;
776 vin->index = itv->audio_input;
777 return ivtv_get_audio_input(itv, vin->index, vin);
780 case VIDIOC_S_AUDIO:{
781 struct v4l2_audio *vout = arg;
783 if (vout->index >= itv->nof_audio_inputs)
785 itv->audio_input = vout->index;
786 ivtv_audio_set_io(itv);
790 case VIDIOC_ENUMAUDOUT:{
791 struct v4l2_audioout *vin = arg;
793 /* set it to defaults from our table */
794 return ivtv_get_audio_output(itv, vin->index, vin);
797 case VIDIOC_G_AUDOUT:{
798 struct v4l2_audioout *vin = arg;
801 return ivtv_get_audio_output(itv, vin->index, vin);
804 case VIDIOC_S_AUDOUT:{
805 struct v4l2_audioout *vout = arg;
807 return ivtv_get_audio_output(itv, vout->index, vout);
810 case VIDIOC_ENUMINPUT:{
811 struct v4l2_input *vin = arg;
813 /* set it to defaults from our table */
814 return ivtv_get_input(itv, vin->index, vin);
817 case VIDIOC_ENUMOUTPUT:{
818 struct v4l2_output *vout = arg;
820 return ivtv_get_output(itv, vout->index, vout);
825 struct v4l2_format *fmt = arg;
827 return ivtv_try_or_set_fmt(itv, id->type, fmt, cmd == VIDIOC_S_FMT);
831 struct v4l2_format *fmt = arg;
832 int type = fmt->type;
834 memset(fmt, 0, sizeof(*fmt));
836 return ivtv_get_fmt(itv, id->type, fmt);
839 case VIDIOC_CROPCAP: {
840 struct v4l2_cropcap *cropcap = arg;
842 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
844 cropcap->bounds.top = cropcap->bounds.left = 0;
845 cropcap->bounds.width = 720;
846 if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
847 cropcap->bounds.height = itv->is_50hz ? 576 : 480;
848 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
849 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
850 } else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
852 cropcap->bounds.width = yi->osd_full_w;
853 cropcap->bounds.height = yi->osd_full_h;
855 cropcap->bounds.width = 720;
856 cropcap->bounds.height =
857 itv->is_out_50hz ? 576 : 480;
859 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
860 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
862 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
863 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
864 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
866 cropcap->defrect = cropcap->bounds;
870 case VIDIOC_S_CROP: {
871 struct v4l2_crop *crop = arg;
873 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
874 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
875 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
876 yi->main_rect = crop->c;
879 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
880 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
881 itv->main_rect = crop->c;
890 case VIDIOC_G_CROP: {
891 struct v4l2_crop *crop = arg;
893 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
894 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
895 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
896 crop->c = yi->main_rect;
898 crop->c = itv->main_rect;
904 case VIDIOC_ENUM_FMT: {
905 static struct v4l2_fmtdesc formats[] = {
907 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
910 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
911 "MPEG", V4L2_PIX_FMT_MPEG,
915 struct v4l2_fmtdesc *fmt = arg;
916 enum v4l2_buf_type type = fmt->type;
919 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
921 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
922 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
930 *fmt = formats[fmt->index];
935 case VIDIOC_G_INPUT:{
936 *(int *)arg = itv->active_input;
940 case VIDIOC_S_INPUT:{
941 int inp = *(int *)arg;
943 if (inp < 0 || inp >= itv->nof_inputs)
946 if (inp == itv->active_input) {
947 IVTV_DEBUG_INFO("Input unchanged\n");
950 if (atomic_read(&itv->capturing) > 0) {
953 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
954 itv->active_input, inp);
956 itv->active_input = inp;
957 /* Set the audio input to whatever is appropriate for the
959 itv->audio_input = itv->card->video_inputs[inp].audio_index;
961 /* prevent others from messing with the streams until
962 we're finished changing inputs. */
964 ivtv_video_set_io(itv);
965 ivtv_audio_set_io(itv);
970 case VIDIOC_G_OUTPUT:{
971 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
973 *(int *)arg = itv->active_output;
977 case VIDIOC_S_OUTPUT:{
978 int outp = *(int *)arg;
979 struct v4l2_routing route;
981 if (outp >= itv->card->nof_outputs)
984 if (outp == itv->active_output) {
985 IVTV_DEBUG_INFO("Output unchanged\n");
988 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
989 itv->active_output, outp);
991 itv->active_output = outp;
992 route.input = SAA7127_INPUT_TYPE_NORMAL;
993 route.output = itv->card->video_outputs[outp].video_output;
994 ivtv_saa7127(itv, VIDIOC_INT_S_VIDEO_ROUTING, &route);
998 case VIDIOC_G_FREQUENCY:{
999 struct v4l2_frequency *vf = arg;
1003 ivtv_call_i2c_clients(itv, cmd, arg);
1007 case VIDIOC_S_FREQUENCY:{
1008 struct v4l2_frequency vf = *(struct v4l2_frequency *)arg;
1014 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf.frequency);
1015 ivtv_call_i2c_clients(itv, cmd, &vf);
1020 case VIDIOC_ENUMSTD:{
1021 struct v4l2_standard *vs = arg;
1022 int idx = vs->index;
1024 if (idx < 0 || idx >= ARRAY_SIZE(enum_stds))
1027 *vs = (enum_stds[idx].std & V4L2_STD_525_60) ?
1028 ivtv_std_60hz : ivtv_std_50hz;
1030 vs->id = enum_stds[idx].std;
1031 strlcpy(vs->name, enum_stds[idx].name, sizeof(vs->name));
1036 *(v4l2_std_id *) arg = itv->std;
1040 case VIDIOC_S_STD: {
1041 v4l2_std_id std = *(v4l2_std_id *) arg;
1043 if ((std & V4L2_STD_ALL) == 0)
1046 if (std == itv->std)
1049 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1050 atomic_read(&itv->capturing) > 0 ||
1051 atomic_read(&itv->decoding) > 0) {
1052 /* Switching standard would turn off the radio or mess
1053 with already running streams, prevent that by
1059 itv->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
1060 itv->params.is_50hz = itv->is_50hz = !itv->is_60hz;
1061 itv->params.width = 720;
1062 itv->params.height = itv->is_50hz ? 576 : 480;
1063 itv->vbi.count = itv->is_50hz ? 18 : 12;
1064 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1065 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1066 if (itv->hw_flags & IVTV_HW_CX25840) {
1067 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1069 IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
1072 ivtv_call_i2c_clients(itv, VIDIOC_S_STD, &itv->std);
1074 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1075 /* set display standard */
1077 itv->is_out_60hz = itv->is_60hz;
1078 itv->is_out_50hz = itv->is_50hz;
1079 ivtv_call_i2c_clients(itv, VIDIOC_INT_S_STD_OUTPUT, &itv->std_out);
1080 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1081 itv->main_rect.left = itv->main_rect.top = 0;
1082 itv->main_rect.width = 720;
1083 itv->main_rect.height = itv->params.height;
1084 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1085 720, itv->main_rect.height, 0, 0);
1086 yi->main_rect = itv->main_rect;
1087 if (!itv->osd_info) {
1088 yi->osd_full_w = 720;
1089 yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1095 case VIDIOC_S_TUNER: { /* Setting tuner can only set audio mode */
1096 struct v4l2_tuner *vt = arg;
1101 ivtv_call_i2c_clients(itv, VIDIOC_S_TUNER, vt);
1105 case VIDIOC_G_TUNER: {
1106 struct v4l2_tuner *vt = arg;
1111 memset(vt, 0, sizeof(*vt));
1112 ivtv_call_i2c_clients(itv, VIDIOC_G_TUNER, vt);
1114 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1115 strlcpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1116 vt->type = V4L2_TUNER_RADIO;
1118 strlcpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1119 vt->type = V4L2_TUNER_ANALOG_TV;
1124 case VIDIOC_G_SLICED_VBI_CAP: {
1125 struct v4l2_sliced_vbi_cap *cap = arg;
1126 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1128 enum v4l2_buf_type type = cap->type;
1130 memset(cap, 0, sizeof(*cap));
1132 if (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1133 for (f = 0; f < 2; f++) {
1134 for (l = 0; l < 24; l++) {
1135 if (valid_service_line(f, l, itv->is_50hz)) {
1136 cap->service_lines[f][l] = set;
1142 if (type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1143 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1146 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1147 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1149 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1150 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1157 case VIDIOC_G_ENC_INDEX: {
1158 struct v4l2_enc_idx *idx = arg;
1159 struct v4l2_enc_idx_entry *e = idx->entry;
1163 entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1165 if (entries > V4L2_ENC_IDX_ENTRIES)
1166 entries = V4L2_ENC_IDX_ENTRIES;
1168 for (i = 0; i < entries; i++) {
1169 *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1170 if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1175 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1179 case VIDIOC_ENCODER_CMD:
1180 case VIDIOC_TRY_ENCODER_CMD: {
1181 struct v4l2_encoder_cmd *enc = arg;
1182 int try = cmd == VIDIOC_TRY_ENCODER_CMD;
1184 memset(&enc->raw, 0, sizeof(enc->raw));
1186 case V4L2_ENC_CMD_START:
1187 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1191 return ivtv_start_capture(id);
1193 case V4L2_ENC_CMD_STOP:
1194 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1195 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1198 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1201 case V4L2_ENC_CMD_PAUSE:
1202 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1206 if (!atomic_read(&itv->capturing))
1208 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1211 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1214 case V4L2_ENC_CMD_RESUME:
1215 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1219 if (!atomic_read(&itv->capturing))
1221 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1223 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1227 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1233 case VIDIOC_G_FBUF: {
1234 struct v4l2_framebuffer *fb = arg;
1236 static u32 pixel_format[16] = {
1237 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1238 V4L2_PIX_FMT_RGB565,
1239 V4L2_PIX_FMT_RGB555,
1240 V4L2_PIX_FMT_RGB444,
1245 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1246 V4L2_PIX_FMT_YUV565,
1247 V4L2_PIX_FMT_YUV555,
1248 V4L2_PIX_FMT_YUV444,
1255 memset(fb, 0, sizeof(*fb));
1256 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1258 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1259 V4L2_FBUF_CAP_GLOBAL_ALPHA;
1260 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1261 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1262 pixfmt = (data[0] >> 3) & 0xf;
1263 fb->fmt.pixelformat = pixel_format[pixfmt];
1264 fb->fmt.width = itv->osd_rect.width;
1265 fb->fmt.height = itv->osd_rect.height;
1266 fb->base = (void *)itv->osd_video_pbase;
1267 if (itv->osd_chroma_key_state)
1268 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1269 if (itv->osd_global_alpha_state)
1270 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1272 /* no local alpha for RGB565 or unknown formats */
1273 if (pixfmt == 1 || pixfmt > 4)
1275 /* 16-bit formats have inverted local alpha */
1276 if (pixfmt == 2 || pixfmt == 3)
1277 fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1279 fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1280 if (itv->osd_local_alpha_state) {
1281 /* 16-bit formats have inverted local alpha */
1282 if (pixfmt == 2 || pixfmt == 3)
1283 fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1285 fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1288 fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1292 case VIDIOC_S_FBUF: {
1293 struct v4l2_framebuffer *fb = arg;
1295 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1297 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1298 itv->osd_local_alpha_state =
1299 (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1300 itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1301 ivtv_set_osd_alpha(itv);
1302 yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1306 case VIDIOC_OVERLAY: {
1309 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1311 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, *on != 0);
1315 case VIDIOC_LOG_STATUS:
1317 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1318 struct v4l2_input vidin;
1319 struct v4l2_audio audin;
1322 IVTV_INFO("================= START STATUS CARD #%d =================\n", itv->num);
1323 IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1324 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1327 ivtv_read_eeprom(itv, &tv);
1329 ivtv_call_i2c_clients(itv, VIDIOC_LOG_STATUS, NULL);
1330 ivtv_get_input(itv, itv->active_input, &vidin);
1331 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1332 IVTV_INFO("Video Input: %s\n", vidin.name);
1333 IVTV_INFO("Audio Input: %s%s\n", audin.name,
1334 (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
1336 struct v4l2_output vidout;
1337 struct v4l2_audioout audout;
1338 int mode = itv->output_mode;
1339 static const char * const output_modes[5] = {
1346 static const char * const audio_modes[5] = {
1353 static const char * const alpha_mode[4] = {
1359 static const char * const pixel_format[16] = {
1378 ivtv_get_output(itv, itv->active_output, &vidout);
1379 ivtv_get_audio_output(itv, 0, &audout);
1380 IVTV_INFO("Video Output: %s\n", vidout.name);
1381 IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
1382 audio_modes[itv->audio_stereo_mode],
1383 audio_modes[itv->audio_bilingual_mode]);
1384 if (mode < 0 || mode > OUT_PASSTHROUGH)
1386 IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1387 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1388 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1389 IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
1390 data[0] & 1 ? "On" : "Off",
1391 alpha_mode[(data[0] >> 1) & 0x3],
1392 pixel_format[(data[0] >> 3) & 0xf]);
1394 IVTV_INFO("Tuner: %s\n",
1395 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1396 cx2341x_log_status(&itv->params, itv->name);
1397 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1398 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1399 struct ivtv_stream *s = &itv->streams[i];
1401 if (s->v4l2dev == NULL || s->buffers == 0)
1403 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1404 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1405 (s->buffers * s->buf_size) / 1024, s->buffers);
1407 IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n", (long long)itv->mpg_data_received, (long long)itv->vbi_data_inserted);
1408 IVTV_INFO("================== END STATUS CARD #%d ==================\n", itv->num);
1418 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1420 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1421 struct ivtv *itv = id->itv;
1422 int nonblocking = filp->f_flags & O_NONBLOCK;
1423 struct ivtv_stream *s = &itv->streams[id->type];
1426 case IVTV_IOC_DMA_FRAME: {
1427 struct ivtv_dma_frame *args = arg;
1429 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1430 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1432 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1434 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1436 if (ivtv_claim_stream(id, id->type)) {
1439 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1440 ivtv_release_stream(s);
1443 /* Mark that this file handle started the UDMA_YUV mode */
1445 if (args->y_source == NULL)
1447 return ivtv_yuv_prep_frame(itv, args);
1450 case VIDEO_GET_PTS: {
1451 u32 data[CX2341X_MBOX_MAX_DATA];
1454 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1455 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1459 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1462 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1463 *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1464 (u64)itv->last_dec_timing[1];
1468 if (atomic_read(&itv->decoding)) {
1469 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1470 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1473 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1474 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1475 *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1476 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1481 case VIDEO_GET_FRAME_COUNT: {
1482 u32 data[CX2341X_MBOX_MAX_DATA];
1485 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1486 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1490 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1493 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1494 *frame = itv->last_dec_timing[0];
1498 if (atomic_read(&itv->decoding)) {
1499 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1500 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1503 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1504 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1511 struct video_command vc;
1513 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1514 memset(&vc, 0, sizeof(vc));
1515 vc.cmd = VIDEO_CMD_PLAY;
1516 return ivtv_video_command(itv, id, &vc, 0);
1520 struct video_command vc;
1522 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1523 memset(&vc, 0, sizeof(vc));
1524 vc.cmd = VIDEO_CMD_STOP;
1525 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1526 return ivtv_video_command(itv, id, &vc, 0);
1529 case VIDEO_FREEZE: {
1530 struct video_command vc;
1532 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1533 memset(&vc, 0, sizeof(vc));
1534 vc.cmd = VIDEO_CMD_FREEZE;
1535 return ivtv_video_command(itv, id, &vc, 0);
1538 case VIDEO_CONTINUE: {
1539 struct video_command vc;
1541 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1542 memset(&vc, 0, sizeof(vc));
1543 vc.cmd = VIDEO_CMD_CONTINUE;
1544 return ivtv_video_command(itv, id, &vc, 0);
1548 case VIDEO_TRY_COMMAND: {
1549 struct video_command *vc = arg;
1550 int try = (cmd == VIDEO_TRY_COMMAND);
1553 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
1555 IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
1556 return ivtv_video_command(itv, id, vc, try);
1559 case VIDEO_GET_EVENT: {
1560 struct video_event *ev = arg;
1563 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1564 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1566 memset(ev, 0, sizeof(*ev));
1567 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1570 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1571 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1572 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1573 ev->type = VIDEO_EVENT_VSYNC;
1574 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1575 VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1576 if (itv->output_mode == OUT_UDMA_YUV &&
1577 (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1578 IVTV_YUV_MODE_PROGRESSIVE) {
1579 ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1586 /* Wait for event. Note that serialize_lock is locked,
1587 so to allow other processes to access the driver while
1588 we are waiting unlock first and later lock again. */
1589 mutex_unlock(&itv->serialize_lock);
1590 prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1591 if ((itv->i_flags & (IVTV_F_I_EV_DEC_STOPPED|IVTV_F_I_EV_VSYNC)) == 0)
1593 finish_wait(&itv->event_waitq, &wait);
1594 mutex_lock(&itv->serialize_lock);
1595 if (signal_pending(current)) {
1596 /* return if a signal was received */
1597 IVTV_DEBUG_INFO("User stopped wait for event\n");
1610 static int ivtv_v4l2_do_ioctl(struct inode *inode, struct file *filp,
1611 unsigned int cmd, void *arg)
1613 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1614 struct ivtv *itv = id->itv;
1617 /* check priority */
1621 case VIDIOC_S_INPUT:
1622 case VIDIOC_S_OUTPUT:
1623 case VIDIOC_S_TUNER:
1624 case VIDIOC_S_FREQUENCY:
1627 case VIDIOC_S_AUDIO:
1628 case VIDIOC_S_AUDOUT:
1629 case VIDIOC_S_EXT_CTRLS:
1631 case VIDIOC_OVERLAY:
1632 ret = v4l2_prio_check(&itv->prio, &id->prio);
1638 case VIDIOC_DBG_G_REGISTER:
1639 case VIDIOC_DBG_S_REGISTER:
1640 case VIDIOC_G_CHIP_IDENT:
1641 case VIDIOC_INT_S_AUDIO_ROUTING:
1642 case VIDIOC_INT_RESET:
1643 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1644 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1645 v4l_printk_ioctl(cmd);
1648 return ivtv_debug_ioctls(filp, cmd, arg);
1650 case VIDIOC_G_PRIORITY:
1651 case VIDIOC_S_PRIORITY:
1652 case VIDIOC_QUERYCAP:
1653 case VIDIOC_ENUMINPUT:
1654 case VIDIOC_G_INPUT:
1655 case VIDIOC_S_INPUT:
1656 case VIDIOC_ENUMOUTPUT:
1657 case VIDIOC_G_OUTPUT:
1658 case VIDIOC_S_OUTPUT:
1661 case VIDIOC_TRY_FMT:
1662 case VIDIOC_ENUM_FMT:
1663 case VIDIOC_CROPCAP:
1666 case VIDIOC_G_FREQUENCY:
1667 case VIDIOC_S_FREQUENCY:
1668 case VIDIOC_ENUMSTD:
1671 case VIDIOC_S_TUNER:
1672 case VIDIOC_G_TUNER:
1673 case VIDIOC_ENUMAUDIO:
1674 case VIDIOC_S_AUDIO:
1675 case VIDIOC_G_AUDIO:
1676 case VIDIOC_ENUMAUDOUT:
1677 case VIDIOC_S_AUDOUT:
1678 case VIDIOC_G_AUDOUT:
1679 case VIDIOC_G_SLICED_VBI_CAP:
1680 case VIDIOC_LOG_STATUS:
1681 case VIDIOC_G_ENC_INDEX:
1682 case VIDIOC_ENCODER_CMD:
1683 case VIDIOC_TRY_ENCODER_CMD:
1686 case VIDIOC_OVERLAY:
1687 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1688 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1689 v4l_printk_ioctl(cmd);
1692 return ivtv_v4l2_ioctls(itv, filp, cmd, arg);
1694 case VIDIOC_QUERYMENU:
1695 case VIDIOC_QUERYCTRL:
1698 case VIDIOC_S_EXT_CTRLS:
1699 case VIDIOC_G_EXT_CTRLS:
1700 case VIDIOC_TRY_EXT_CTRLS:
1701 if (ivtv_debug & IVTV_DBGFLG_IOCTL) {
1702 printk(KERN_INFO "ivtv%d ioctl: ", itv->num);
1703 v4l_printk_ioctl(cmd);
1706 return ivtv_control_ioctls(itv, cmd, arg);
1708 case IVTV_IOC_DMA_FRAME:
1710 case VIDEO_GET_FRAME_COUNT:
1711 case VIDEO_GET_EVENT:
1715 case VIDEO_CONTINUE:
1717 case VIDEO_TRY_COMMAND:
1718 return ivtv_decoder_ioctls(filp, cmd, arg);
1720 case 0x00005401: /* Handle isatty() calls */
1723 return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
1724 ivtv_v4l2_do_ioctl);
1729 static int ivtv_serialized_ioctl(struct ivtv *itv, struct inode *inode, struct file *filp,
1730 unsigned int cmd, unsigned long arg)
1732 /* Filter dvb ioctls that cannot be handled by video_usercopy */
1734 case VIDEO_SELECT_SOURCE:
1735 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1736 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1738 return ivtv_passthrough_mode(itv, arg == VIDEO_SOURCE_DEMUX);
1740 case AUDIO_SET_MUTE:
1741 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1742 itv->speed_mute_audio = arg;
1745 case AUDIO_CHANNEL_SELECT:
1746 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1747 if (arg > AUDIO_STEREO_SWAPPED)
1749 itv->audio_stereo_mode = arg;
1750 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1753 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1754 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1755 if (arg > AUDIO_STEREO_SWAPPED)
1757 itv->audio_bilingual_mode = arg;
1758 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1764 return video_usercopy(inode, filp, cmd, arg, ivtv_v4l2_do_ioctl);
1767 int ivtv_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
1770 struct ivtv_open_id *id = (struct ivtv_open_id *)filp->private_data;
1771 struct ivtv *itv = id->itv;
1774 mutex_lock(&itv->serialize_lock);
1775 res = ivtv_serialized_ioctl(itv, inode, filp, cmd, arg);
1776 mutex_unlock(&itv->serialize_lock);