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 <media/v4l2-event.h>
39 #include <linux/dvb/audio.h>
40 #include <linux/i2c-id.h>
42 u16 ivtv_service2vbi(int type)
45 case V4L2_SLICED_TELETEXT_B:
46 return IVTV_SLICED_TYPE_TELETEXT_B;
47 case V4L2_SLICED_CAPTION_525:
48 return IVTV_SLICED_TYPE_CAPTION_525;
49 case V4L2_SLICED_WSS_625:
50 return IVTV_SLICED_TYPE_WSS_625;
52 return IVTV_SLICED_TYPE_VPS;
58 static int valid_service_line(int field, int line, int is_pal)
60 return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
61 (!is_pal && line >= 10 && line < 22);
64 static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
66 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
69 set = set & valid_set;
70 if (set == 0 || !valid_service_line(field, line, is_pal)) {
74 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
75 return V4L2_SLICED_CAPTION_525;
78 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
79 return V4L2_SLICED_VPS;
80 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
81 return V4L2_SLICED_WSS_625;
85 for (i = 0; i < 32; i++) {
92 void ivtv_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
94 u16 set = fmt->service_set;
98 for (f = 0; f < 2; f++) {
99 for (l = 0; l < 24; l++) {
100 fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
105 static void 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);
116 u16 ivtv_get_service_set(struct v4l2_sliced_vbi_format *fmt)
121 for (f = 0; f < 2; f++) {
122 for (l = 0; l < 24; l++) {
123 set |= fmt->service_lines[f][l];
129 void ivtv_set_osd_alpha(struct ivtv *itv)
131 ivtv_vapi(itv, CX2341X_OSD_SET_GLOBAL_ALPHA, 3,
132 itv->osd_global_alpha_state, itv->osd_global_alpha, !itv->osd_local_alpha_state);
133 ivtv_vapi(itv, CX2341X_OSD_SET_CHROMA_KEY, 2, itv->osd_chroma_key_state, itv->osd_chroma_key);
136 int ivtv_set_speed(struct ivtv *itv, int speed)
138 u32 data[CX2341X_MBOX_MAX_DATA];
139 struct ivtv_stream *s;
140 int single_step = (speed == 1 || speed == -1);
143 if (speed == 0) speed = 1000;
146 if (speed == itv->speed && !single_step)
149 s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
151 if (single_step && (speed < 0) == (itv->speed < 0)) {
152 /* Single step video and no need to change direction */
153 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
158 /* Need to change direction */
159 speed = speed < 0 ? -1000 : 1000;
161 data[0] = (speed > 1000 || speed < -1000) ? 0x80000000 : 0;
162 data[0] |= (speed > 1000 || speed < -1500) ? 0x40000000 : 0;
163 data[1] = (speed < 0);
164 data[2] = speed < 0 ? 3 : 7;
165 data[3] = v4l2_ctrl_g_ctrl(itv->cxhdl.video_b_frames);
166 data[4] = (speed == 1500 || speed == 500) ? itv->speed_mute_audio : 0;
170 if (speed == 1500 || speed == -1500) data[0] |= 1;
171 else if (speed == 2000 || speed == -2000) data[0] |= 2;
172 else if (speed > -1000 && speed < 0) data[0] |= (-1000 / speed);
173 else if (speed < 1000 && speed > 0) data[0] |= (1000 / speed);
175 /* If not decoding, just change speed setting */
176 if (atomic_read(&itv->decoding) > 0) {
179 /* Stop all DMA and decoding activity */
180 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1, 0);
182 /* Wait for any DMA to finish */
183 prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
184 while (test_bit(IVTV_F_I_DMA, &itv->i_flags)) {
185 got_sig = signal_pending(current);
191 finish_wait(&itv->dma_waitq, &wait);
195 /* Change Speed safely */
196 ivtv_api(itv, CX2341X_DEC_SET_PLAYBACK_SPEED, 7, data);
197 IVTV_DEBUG_INFO("Setting Speed to 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
198 data[0], data[1], data[2], data[3], data[4], data[5], data[6]);
201 speed = (speed < 0) ? -1 : 1;
202 ivtv_vapi(itv, CX2341X_DEC_STEP_VIDEO, 1, 0);
208 static int ivtv_validate_speed(int cur_speed, int new_speed)
210 int fact = new_speed < 0 ? -1 : 1;
216 new_speed = -new_speed;
218 cur_speed = -cur_speed;
220 if (cur_speed <= new_speed) {
221 if (new_speed > 1500)
223 if (new_speed > 1000)
227 if (new_speed >= 2000)
229 if (new_speed >= 1500)
231 if (new_speed >= 1000)
236 if (new_speed == 1 || new_speed == 1000)
237 return fact * new_speed;
240 new_speed = 1000 / new_speed;
241 if (1000 / cur_speed == new_speed)
242 new_speed += (cur_speed < s) ? -1 : 1;
243 if (new_speed > 60) return 1000 / (fact * 60);
244 return 1000 / (fact * new_speed);
247 static int ivtv_video_command(struct ivtv *itv, struct ivtv_open_id *id,
248 struct video_command *vc, int try)
250 struct ivtv_stream *s = &itv->streams[IVTV_DEC_STREAM_TYPE_MPG];
252 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
256 case VIDEO_CMD_PLAY: {
258 vc->play.speed = ivtv_validate_speed(itv->speed, vc->play.speed);
259 if (vc->play.speed < 0)
260 vc->play.format = VIDEO_PLAY_FMT_GOP;
263 if (ivtv_set_output_mode(itv, OUT_MPG) != OUT_MPG)
265 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
266 /* forces ivtv_set_speed to be called */
269 return ivtv_start_decoding(id, vc->play.speed);
273 vc->flags &= VIDEO_CMD_STOP_IMMEDIATELY|VIDEO_CMD_STOP_TO_BLACK;
274 if (vc->flags & VIDEO_CMD_STOP_IMMEDIATELY)
277 if (atomic_read(&itv->decoding) == 0)
279 if (itv->output_mode != OUT_MPG)
282 itv->output_mode = OUT_NONE;
283 return ivtv_stop_v4l2_decode_stream(s, vc->flags, vc->stop.pts);
285 case VIDEO_CMD_FREEZE:
286 vc->flags &= VIDEO_CMD_FREEZE_TO_BLACK;
288 if (itv->output_mode != OUT_MPG)
290 if (atomic_read(&itv->decoding) > 0) {
291 ivtv_vapi(itv, CX2341X_DEC_PAUSE_PLAYBACK, 1,
292 (vc->flags & VIDEO_CMD_FREEZE_TO_BLACK) ? 1 : 0);
293 set_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags);
297 case VIDEO_CMD_CONTINUE:
300 if (itv->output_mode != OUT_MPG)
302 if (test_and_clear_bit(IVTV_F_I_DEC_PAUSED, &itv->i_flags)) {
303 int speed = itv->speed;
305 return ivtv_start_decoding(id, speed);
315 static int ivtv_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
317 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
318 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
320 vbifmt->reserved[0] = 0;
321 vbifmt->reserved[1] = 0;
322 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
324 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
326 vbifmt->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
327 vbifmt->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
329 vbifmt->service_lines[0][23] = V4L2_SLICED_WSS_625;
330 vbifmt->service_lines[0][16] = V4L2_SLICED_VPS;
332 vbifmt->service_set = ivtv_get_service_set(vbifmt);
336 static int ivtv_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
338 struct ivtv_open_id *id = fh;
339 struct ivtv *itv = id->itv;
340 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
342 pixfmt->width = itv->cxhdl.width;
343 pixfmt->height = itv->cxhdl.height;
344 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
345 pixfmt->field = V4L2_FIELD_INTERLACED;
347 if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
348 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
349 /* YUV size is (Y=(h*720) + UV=(h*(720/2))) */
350 pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
351 pixfmt->bytesperline = 720;
353 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
354 pixfmt->sizeimage = 128 * 1024;
355 pixfmt->bytesperline = 0;
360 static int ivtv_g_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
362 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
363 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
365 vbifmt->sampling_rate = 27000000;
366 vbifmt->offset = 248;
367 vbifmt->samples_per_line = itv->vbi.raw_decoder_line_size - 4;
368 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
369 vbifmt->start[0] = itv->vbi.start[0];
370 vbifmt->start[1] = itv->vbi.start[1];
371 vbifmt->count[0] = vbifmt->count[1] = itv->vbi.count;
373 vbifmt->reserved[0] = 0;
374 vbifmt->reserved[1] = 0;
378 static int ivtv_g_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
380 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
381 struct ivtv_open_id *id = fh;
382 struct ivtv *itv = id->itv;
384 vbifmt->reserved[0] = 0;
385 vbifmt->reserved[1] = 0;
386 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
388 if (id->type == IVTV_DEC_STREAM_TYPE_VBI) {
389 vbifmt->service_set = itv->is_50hz ? V4L2_SLICED_VBI_625 :
391 ivtv_expand_service_set(vbifmt, itv->is_50hz);
395 v4l2_subdev_call(itv->sd_video, vbi, g_sliced_fmt, vbifmt);
396 vbifmt->service_set = ivtv_get_service_set(vbifmt);
400 static int ivtv_g_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
402 struct ivtv_open_id *id = fh;
403 struct ivtv *itv = id->itv;
404 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
406 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
408 pixfmt->width = itv->main_rect.width;
409 pixfmt->height = itv->main_rect.height;
410 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
411 pixfmt->field = V4L2_FIELD_INTERLACED;
413 if (id->type == IVTV_DEC_STREAM_TYPE_YUV) {
414 switch (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) {
415 case IVTV_YUV_MODE_INTERLACED:
416 pixfmt->field = (itv->yuv_info.lace_mode & IVTV_YUV_SYNC_MASK) ?
417 V4L2_FIELD_INTERLACED_BT : V4L2_FIELD_INTERLACED_TB;
419 case IVTV_YUV_MODE_PROGRESSIVE:
420 pixfmt->field = V4L2_FIELD_NONE;
423 pixfmt->field = V4L2_FIELD_ANY;
426 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
427 pixfmt->bytesperline = 720;
428 pixfmt->width = itv->yuv_info.v4l2_src_w;
429 pixfmt->height = itv->yuv_info.v4l2_src_h;
430 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
432 1080 * ((pixfmt->height + 31) & ~31);
434 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
435 pixfmt->sizeimage = 128 * 1024;
436 pixfmt->bytesperline = 0;
441 static int ivtv_g_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
443 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
444 struct v4l2_window *winfmt = &fmt->fmt.win;
446 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
448 winfmt->chromakey = itv->osd_chroma_key;
449 winfmt->global_alpha = itv->osd_global_alpha;
450 winfmt->field = V4L2_FIELD_INTERLACED;
451 winfmt->clips = NULL;
452 winfmt->clipcount = 0;
453 winfmt->bitmap = NULL;
454 winfmt->w.top = winfmt->w.left = 0;
455 winfmt->w.width = itv->osd_rect.width;
456 winfmt->w.height = itv->osd_rect.height;
460 static int ivtv_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
462 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
465 static int ivtv_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
467 struct ivtv_open_id *id = fh;
468 struct ivtv *itv = id->itv;
469 int w = fmt->fmt.pix.width;
470 int h = fmt->fmt.pix.height;
475 if (id->type == IVTV_ENC_STREAM_TYPE_YUV) {
476 /* YUV height must be a multiple of 32 */
480 h = min(h, itv->is_50hz ? 576 : 480);
482 ivtv_g_fmt_vid_cap(file, fh, fmt);
483 fmt->fmt.pix.width = w;
484 fmt->fmt.pix.height = h;
488 static int ivtv_try_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
490 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
493 static int ivtv_try_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
495 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
496 struct ivtv_open_id *id = fh;
497 struct ivtv *itv = id->itv;
499 if (id->type == IVTV_DEC_STREAM_TYPE_VBI)
500 return ivtv_g_fmt_sliced_vbi_cap(file, fh, fmt);
502 /* set sliced VBI capture format */
503 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
504 vbifmt->reserved[0] = 0;
505 vbifmt->reserved[1] = 0;
507 if (vbifmt->service_set)
508 ivtv_expand_service_set(vbifmt, itv->is_50hz);
509 check_service_set(vbifmt, itv->is_50hz);
510 vbifmt->service_set = ivtv_get_service_set(vbifmt);
514 static int ivtv_try_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
516 struct ivtv_open_id *id = fh;
517 s32 w = fmt->fmt.pix.width;
518 s32 h = fmt->fmt.pix.height;
519 int field = fmt->fmt.pix.field;
520 int ret = ivtv_g_fmt_vid_out(file, fh, fmt);
524 /* Why can the height be 576 even when the output is NTSC?
526 Internally the buffers of the PVR350 are always set to 720x576. The
527 decoded video frame will always be placed in the top left corner of
528 this buffer. For any video which is not 720x576, the buffer will
529 then be cropped to remove the unused right and lower areas, with
530 the remaining image being scaled by the hardware to fit the display
531 area. The video can be scaled both up and down, so a 720x480 video
532 can be displayed full-screen on PAL and a 720x576 video can be
533 displayed without cropping on NTSC.
535 Note that the scaling only occurs on the video stream, the osd
536 resolution is locked to the broadcast standard and not scaled.
538 Thanks to Ian Armstrong for this explanation. */
541 if (id->type == IVTV_DEC_STREAM_TYPE_YUV)
542 fmt->fmt.pix.field = field;
543 fmt->fmt.pix.width = w;
544 fmt->fmt.pix.height = h;
548 static int ivtv_try_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
550 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
551 u32 chromakey = fmt->fmt.win.chromakey;
552 u8 global_alpha = fmt->fmt.win.global_alpha;
554 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
556 ivtv_g_fmt_vid_out_overlay(file, fh, fmt);
557 fmt->fmt.win.chromakey = chromakey;
558 fmt->fmt.win.global_alpha = global_alpha;
562 static int ivtv_s_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
564 return ivtv_g_fmt_sliced_vbi_out(file, fh, fmt);
567 static int ivtv_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
569 struct ivtv_open_id *id = fh;
570 struct ivtv *itv = id->itv;
571 struct v4l2_mbus_framefmt mbus_fmt;
572 int ret = ivtv_try_fmt_vid_cap(file, fh, fmt);
573 int w = fmt->fmt.pix.width;
574 int h = fmt->fmt.pix.height;
579 if (itv->cxhdl.width == w && itv->cxhdl.height == h)
582 if (atomic_read(&itv->capturing) > 0)
585 itv->cxhdl.width = w;
586 itv->cxhdl.height = h;
587 if (v4l2_ctrl_g_ctrl(itv->cxhdl.video_encoding) == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
588 fmt->fmt.pix.width /= 2;
589 mbus_fmt.width = fmt->fmt.pix.width;
591 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
592 v4l2_subdev_call(itv->sd_video, video, s_mbus_fmt, &mbus_fmt);
593 return ivtv_g_fmt_vid_cap(file, fh, fmt);
596 static int ivtv_s_fmt_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
598 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
600 if (!ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
602 itv->vbi.sliced_in->service_set = 0;
603 itv->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
604 v4l2_subdev_call(itv->sd_video, vbi, s_raw_fmt, &fmt->fmt.vbi);
605 return ivtv_g_fmt_vbi_cap(file, fh, fmt);
608 static int ivtv_s_fmt_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_format *fmt)
610 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
611 struct ivtv_open_id *id = fh;
612 struct ivtv *itv = id->itv;
613 int ret = ivtv_try_fmt_sliced_vbi_cap(file, fh, fmt);
615 if (ret || id->type == IVTV_DEC_STREAM_TYPE_VBI)
618 check_service_set(vbifmt, itv->is_50hz);
619 if (ivtv_raw_vbi(itv) && atomic_read(&itv->capturing) > 0)
621 itv->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
622 v4l2_subdev_call(itv->sd_video, vbi, s_sliced_fmt, vbifmt);
623 memcpy(itv->vbi.sliced_in, vbifmt, sizeof(*itv->vbi.sliced_in));
627 static int ivtv_s_fmt_vid_out(struct file *file, void *fh, struct v4l2_format *fmt)
629 struct ivtv_open_id *id = fh;
630 struct ivtv *itv = id->itv;
631 struct yuv_playback_info *yi = &itv->yuv_info;
632 int ret = ivtv_try_fmt_vid_out(file, fh, fmt);
637 if (id->type != IVTV_DEC_STREAM_TYPE_YUV)
640 /* Return now if we already have some frame data */
644 yi->v4l2_src_w = fmt->fmt.pix.width;
645 yi->v4l2_src_h = fmt->fmt.pix.height;
647 switch (fmt->fmt.pix.field) {
648 case V4L2_FIELD_NONE:
649 yi->lace_mode = IVTV_YUV_MODE_PROGRESSIVE;
652 yi->lace_mode = IVTV_YUV_MODE_AUTO;
654 case V4L2_FIELD_INTERLACED_BT:
656 IVTV_YUV_MODE_INTERLACED|IVTV_YUV_SYNC_ODD;
658 case V4L2_FIELD_INTERLACED_TB:
660 yi->lace_mode = IVTV_YUV_MODE_INTERLACED;
663 yi->lace_sync_field = (yi->lace_mode & IVTV_YUV_SYNC_MASK) == IVTV_YUV_SYNC_EVEN ? 0 : 1;
665 if (test_bit(IVTV_F_I_DEC_YUV, &itv->i_flags))
666 itv->dma_data_req_size =
667 1080 * ((yi->v4l2_src_h + 31) & ~31);
672 static int ivtv_s_fmt_vid_out_overlay(struct file *file, void *fh, struct v4l2_format *fmt)
674 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
675 int ret = ivtv_try_fmt_vid_out_overlay(file, fh, fmt);
678 itv->osd_chroma_key = fmt->fmt.win.chromakey;
679 itv->osd_global_alpha = fmt->fmt.win.global_alpha;
680 ivtv_set_osd_alpha(itv);
685 static int ivtv_g_chip_ident(struct file *file, void *fh, struct v4l2_dbg_chip_ident *chip)
687 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
689 chip->ident = V4L2_IDENT_NONE;
691 if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
692 if (v4l2_chip_match_host(&chip->match))
693 chip->ident = itv->has_cx23415 ? V4L2_IDENT_CX23415 : V4L2_IDENT_CX23416;
696 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
697 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
699 /* TODO: is this correct? */
700 return ivtv_call_all_err(itv, core, g_chip_ident, chip);
703 #ifdef CONFIG_VIDEO_ADV_DEBUG
704 static int ivtv_itvc(struct ivtv *itv, unsigned int cmd, void *arg)
706 struct v4l2_dbg_register *regs = arg;
707 volatile u8 __iomem *reg_start;
709 if (!capable(CAP_SYS_ADMIN))
711 if (regs->reg >= IVTV_REG_OFFSET && regs->reg < IVTV_REG_OFFSET + IVTV_REG_SIZE)
712 reg_start = itv->reg_mem - IVTV_REG_OFFSET;
713 else if (itv->has_cx23415 && regs->reg >= IVTV_DECODER_OFFSET &&
714 regs->reg < IVTV_DECODER_OFFSET + IVTV_DECODER_SIZE)
715 reg_start = itv->dec_mem - IVTV_DECODER_OFFSET;
716 else if (regs->reg < IVTV_ENCODER_SIZE)
717 reg_start = itv->enc_mem;
722 if (cmd == VIDIOC_DBG_G_REGISTER)
723 regs->val = readl(regs->reg + reg_start);
725 writel(regs->val, regs->reg + reg_start);
729 static int ivtv_g_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
731 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
733 if (v4l2_chip_match_host(®->match))
734 return ivtv_itvc(itv, VIDIOC_DBG_G_REGISTER, reg);
735 /* TODO: subdev errors should not be ignored, this should become a
736 subdev helper function. */
737 ivtv_call_all(itv, core, g_register, reg);
741 static int ivtv_s_register(struct file *file, void *fh, struct v4l2_dbg_register *reg)
743 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
745 if (v4l2_chip_match_host(®->match))
746 return ivtv_itvc(itv, VIDIOC_DBG_S_REGISTER, reg);
747 /* TODO: subdev errors should not be ignored, this should become a
748 subdev helper function. */
749 ivtv_call_all(itv, core, s_register, reg);
754 static int ivtv_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
756 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
758 *p = v4l2_prio_max(&itv->prio);
763 static int ivtv_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
765 struct ivtv_open_id *id = fh;
766 struct ivtv *itv = id->itv;
768 return v4l2_prio_change(&itv->prio, &id->prio, prio);
771 static int ivtv_querycap(struct file *file, void *fh, struct v4l2_capability *vcap)
773 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
775 strlcpy(vcap->driver, IVTV_DRIVER_NAME, sizeof(vcap->driver));
776 strlcpy(vcap->card, itv->card_name, sizeof(vcap->card));
777 snprintf(vcap->bus_info, sizeof(vcap->bus_info), "PCI:%s", pci_name(itv->pdev));
778 vcap->version = IVTV_DRIVER_VERSION; /* version */
779 vcap->capabilities = itv->v4l2_cap; /* capabilities */
783 static int ivtv_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
785 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
787 return ivtv_get_audio_input(itv, vin->index, vin);
790 static int ivtv_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
792 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
794 vin->index = itv->audio_input;
795 return ivtv_get_audio_input(itv, vin->index, vin);
798 static int ivtv_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
800 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
802 if (vout->index >= itv->nof_audio_inputs)
805 itv->audio_input = vout->index;
806 ivtv_audio_set_io(itv);
811 static int ivtv_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vin)
813 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
815 /* set it to defaults from our table */
816 return ivtv_get_audio_output(itv, vin->index, vin);
819 static int ivtv_g_audout(struct file *file, void *fh, struct v4l2_audioout *vin)
821 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
824 return ivtv_get_audio_output(itv, vin->index, vin);
827 static int ivtv_s_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
829 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
831 return ivtv_get_audio_output(itv, vout->index, vout);
834 static int ivtv_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
836 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
838 /* set it to defaults from our table */
839 return ivtv_get_input(itv, vin->index, vin);
842 static int ivtv_enum_output(struct file *file, void *fh, struct v4l2_output *vout)
844 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
846 return ivtv_get_output(itv, vout->index, vout);
849 static int ivtv_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cropcap)
851 struct ivtv_open_id *id = fh;
852 struct ivtv *itv = id->itv;
853 struct yuv_playback_info *yi = &itv->yuv_info;
856 streamtype = id->type;
858 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
860 cropcap->bounds.top = cropcap->bounds.left = 0;
861 cropcap->bounds.width = 720;
862 if (cropcap->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
863 cropcap->bounds.height = itv->is_50hz ? 576 : 480;
864 cropcap->pixelaspect.numerator = itv->is_50hz ? 59 : 10;
865 cropcap->pixelaspect.denominator = itv->is_50hz ? 54 : 11;
866 } else if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
868 cropcap->bounds.width = yi->osd_full_w;
869 cropcap->bounds.height = yi->osd_full_h;
871 cropcap->bounds.width = 720;
872 cropcap->bounds.height =
873 itv->is_out_50hz ? 576 : 480;
875 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
876 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
878 cropcap->bounds.height = itv->is_out_50hz ? 576 : 480;
879 cropcap->pixelaspect.numerator = itv->is_out_50hz ? 59 : 10;
880 cropcap->pixelaspect.denominator = itv->is_out_50hz ? 54 : 11;
882 cropcap->defrect = cropcap->bounds;
886 static int ivtv_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
888 struct ivtv_open_id *id = fh;
889 struct ivtv *itv = id->itv;
890 struct yuv_playback_info *yi = &itv->yuv_info;
893 streamtype = id->type;
895 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
896 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
897 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV) {
898 yi->main_rect = crop->c;
901 if (!ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
902 crop->c.width, crop->c.height, crop->c.left, crop->c.top)) {
903 itv->main_rect = crop->c;
912 static int ivtv_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
914 struct ivtv_open_id *id = fh;
915 struct ivtv *itv = id->itv;
916 struct yuv_playback_info *yi = &itv->yuv_info;
919 streamtype = id->type;
921 if (crop->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
922 (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT)) {
923 if (streamtype == IVTV_DEC_STREAM_TYPE_YUV)
924 crop->c = yi->main_rect;
926 crop->c = itv->main_rect;
932 static int ivtv_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
934 static struct v4l2_fmtdesc formats[] = {
936 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
939 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
940 "MPEG", V4L2_PIX_FMT_MPEG,
944 enum v4l2_buf_type type = fmt->type;
949 *fmt = formats[fmt->index];
954 static int ivtv_enum_fmt_vid_out(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
956 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
958 static struct v4l2_fmtdesc formats[] = {
960 "HM12 (YUV 4:2:0)", V4L2_PIX_FMT_HM12,
963 { 1, 0, V4L2_FMT_FLAG_COMPRESSED,
964 "MPEG", V4L2_PIX_FMT_MPEG,
968 enum v4l2_buf_type type = fmt->type;
970 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
976 *fmt = formats[fmt->index];
982 static int ivtv_g_input(struct file *file, void *fh, unsigned int *i)
984 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
986 *i = itv->active_input;
991 int ivtv_s_input(struct file *file, void *fh, unsigned int inp)
993 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
995 if (inp < 0 || inp >= itv->nof_inputs)
998 if (inp == itv->active_input) {
999 IVTV_DEBUG_INFO("Input unchanged\n");
1003 if (atomic_read(&itv->capturing) > 0) {
1007 IVTV_DEBUG_INFO("Changing input from %d to %d\n",
1008 itv->active_input, inp);
1010 itv->active_input = inp;
1011 /* Set the audio input to whatever is appropriate for the
1013 itv->audio_input = itv->card->video_inputs[inp].audio_index;
1015 /* prevent others from messing with the streams until
1016 we're finished changing inputs. */
1018 ivtv_video_set_io(itv);
1019 ivtv_audio_set_io(itv);
1025 static int ivtv_g_output(struct file *file, void *fh, unsigned int *i)
1027 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1029 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1032 *i = itv->active_output;
1037 static int ivtv_s_output(struct file *file, void *fh, unsigned int outp)
1039 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1041 if (outp >= itv->card->nof_outputs)
1044 if (outp == itv->active_output) {
1045 IVTV_DEBUG_INFO("Output unchanged\n");
1048 IVTV_DEBUG_INFO("Changing output from %d to %d\n",
1049 itv->active_output, outp);
1051 itv->active_output = outp;
1052 ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_routing,
1053 SAA7127_INPUT_TYPE_NORMAL,
1054 itv->card->video_outputs[outp].video_output, 0);
1059 static int ivtv_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1061 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1066 ivtv_call_all(itv, tuner, g_frequency, vf);
1070 int ivtv_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1072 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1078 IVTV_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
1079 ivtv_call_all(itv, tuner, s_frequency, vf);
1084 static int ivtv_g_std(struct file *file, void *fh, v4l2_std_id *std)
1086 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1092 int ivtv_s_std(struct file *file, void *fh, v4l2_std_id *std)
1095 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1096 struct yuv_playback_info *yi = &itv->yuv_info;
1099 if ((*std & V4L2_STD_ALL) == 0)
1102 if (*std == itv->std)
1105 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ||
1106 atomic_read(&itv->capturing) > 0 ||
1107 atomic_read(&itv->decoding) > 0) {
1108 /* Switching standard would turn off the radio or mess
1109 with already running streams, prevent that by
1115 itv->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
1116 itv->is_50hz = !itv->is_60hz;
1117 cx2341x_handler_set_50hz(&itv->cxhdl, itv->is_50hz);
1118 itv->cxhdl.width = 720;
1119 itv->cxhdl.height = itv->is_50hz ? 576 : 480;
1120 itv->vbi.count = itv->is_50hz ? 18 : 12;
1121 itv->vbi.start[0] = itv->is_50hz ? 6 : 10;
1122 itv->vbi.start[1] = itv->is_50hz ? 318 : 273;
1124 if (itv->hw_flags & IVTV_HW_CX25840)
1125 itv->vbi.sliced_decoder_line_size = itv->is_60hz ? 272 : 284;
1127 IVTV_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)itv->std);
1130 ivtv_call_all(itv, core, s_std, itv->std);
1132 if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
1133 /* set display standard */
1134 itv->std_out = *std;
1135 itv->is_out_60hz = itv->is_60hz;
1136 itv->is_out_50hz = itv->is_50hz;
1137 ivtv_call_all(itv, video, s_std_output, itv->std_out);
1140 * The next firmware call is time sensitive. Time it to
1141 * avoid risk of a hard lock, by trying to ensure the call
1142 * happens within the first 100 lines of the top field.
1143 * Make 4 attempts to sync to the decoder before giving up.
1145 for (f = 0; f < 4; f++) {
1146 prepare_to_wait(&itv->vsync_waitq, &wait,
1147 TASK_UNINTERRUPTIBLE);
1148 if ((read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16) < 100)
1150 schedule_timeout(msecs_to_jiffies(25));
1152 finish_wait(&itv->vsync_waitq, &wait);
1155 IVTV_WARN("Mode change failed to sync to decoder\n");
1157 ivtv_vapi(itv, CX2341X_DEC_SET_STANDARD, 1, itv->is_out_50hz);
1158 itv->main_rect.left = itv->main_rect.top = 0;
1159 itv->main_rect.width = 720;
1160 itv->main_rect.height = itv->cxhdl.height;
1161 ivtv_vapi(itv, CX2341X_OSD_SET_FRAMEBUFFER_WINDOW, 4,
1162 720, itv->main_rect.height, 0, 0);
1163 yi->main_rect = itv->main_rect;
1164 if (!itv->osd_info) {
1165 yi->osd_full_w = 720;
1166 yi->osd_full_h = itv->is_out_50hz ? 576 : 480;
1172 static int ivtv_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1174 struct ivtv_open_id *id = fh;
1175 struct ivtv *itv = id->itv;
1180 ivtv_call_all(itv, tuner, s_tuner, vt);
1185 static int ivtv_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1187 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1192 ivtv_call_all(itv, tuner, g_tuner, vt);
1194 if (test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags)) {
1195 strlcpy(vt->name, "ivtv Radio Tuner", sizeof(vt->name));
1196 vt->type = V4L2_TUNER_RADIO;
1198 strlcpy(vt->name, "ivtv TV Tuner", sizeof(vt->name));
1199 vt->type = V4L2_TUNER_ANALOG_TV;
1205 static int ivtv_g_sliced_vbi_cap(struct file *file, void *fh, struct v4l2_sliced_vbi_cap *cap)
1207 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1208 int set = itv->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
1211 if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
1212 for (f = 0; f < 2; f++) {
1213 for (l = 0; l < 24; l++) {
1214 if (valid_service_line(f, l, itv->is_50hz))
1215 cap->service_lines[f][l] = set;
1220 if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT) {
1221 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_OUTPUT))
1224 cap->service_lines[0][21] = V4L2_SLICED_CAPTION_525;
1225 cap->service_lines[1][21] = V4L2_SLICED_CAPTION_525;
1227 cap->service_lines[0][23] = V4L2_SLICED_WSS_625;
1228 cap->service_lines[0][16] = V4L2_SLICED_VPS;
1235 static int ivtv_g_enc_index(struct file *file, void *fh, struct v4l2_enc_idx *idx)
1237 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1238 struct v4l2_enc_idx_entry *e = idx->entry;
1242 entries = (itv->pgm_info_write_idx + IVTV_MAX_PGM_INDEX - itv->pgm_info_read_idx) %
1244 if (entries > V4L2_ENC_IDX_ENTRIES)
1245 entries = V4L2_ENC_IDX_ENTRIES;
1247 for (i = 0; i < entries; i++) {
1248 *e = itv->pgm_info[(itv->pgm_info_read_idx + i) % IVTV_MAX_PGM_INDEX];
1249 if ((e->flags & V4L2_ENC_IDX_FRAME_MASK) <= V4L2_ENC_IDX_FRAME_B) {
1254 itv->pgm_info_read_idx = (itv->pgm_info_read_idx + idx->entries) % IVTV_MAX_PGM_INDEX;
1258 static int ivtv_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1260 struct ivtv_open_id *id = fh;
1261 struct ivtv *itv = id->itv;
1265 case V4L2_ENC_CMD_START:
1266 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1268 return ivtv_start_capture(id);
1270 case V4L2_ENC_CMD_STOP:
1271 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1272 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1273 ivtv_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1276 case V4L2_ENC_CMD_PAUSE:
1277 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1280 if (!atomic_read(&itv->capturing))
1282 if (test_and_set_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1286 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 0);
1289 case V4L2_ENC_CMD_RESUME:
1290 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1293 if (!atomic_read(&itv->capturing))
1296 if (!test_and_clear_bit(IVTV_F_I_ENC_PAUSED, &itv->i_flags))
1299 ivtv_vapi(itv, CX2341X_ENC_PAUSE_ENCODER, 1, 1);
1303 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1310 static int ivtv_try_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *enc)
1312 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1315 case V4L2_ENC_CMD_START:
1316 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1320 case V4L2_ENC_CMD_STOP:
1321 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1322 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1325 case V4L2_ENC_CMD_PAUSE:
1326 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1330 case V4L2_ENC_CMD_RESUME:
1331 IVTV_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1335 IVTV_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1340 static int ivtv_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1342 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1343 u32 data[CX2341X_MBOX_MAX_DATA];
1344 struct yuv_playback_info *yi = &itv->yuv_info;
1347 static u32 pixel_format[16] = {
1348 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry RGB colormap */
1349 V4L2_PIX_FMT_RGB565,
1350 V4L2_PIX_FMT_RGB555,
1351 V4L2_PIX_FMT_RGB444,
1356 V4L2_PIX_FMT_PAL8, /* Uses a 256-entry YUV colormap */
1357 V4L2_PIX_FMT_YUV565,
1358 V4L2_PIX_FMT_YUV555,
1359 V4L2_PIX_FMT_YUV444,
1366 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1368 if (!itv->osd_video_pbase)
1371 fb->capability = V4L2_FBUF_CAP_EXTERNOVERLAY | V4L2_FBUF_CAP_CHROMAKEY |
1372 V4L2_FBUF_CAP_GLOBAL_ALPHA;
1374 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1375 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1376 pixfmt = (data[0] >> 3) & 0xf;
1378 fb->fmt.pixelformat = pixel_format[pixfmt];
1379 fb->fmt.width = itv->osd_rect.width;
1380 fb->fmt.height = itv->osd_rect.height;
1381 fb->fmt.field = V4L2_FIELD_INTERLACED;
1382 fb->fmt.bytesperline = fb->fmt.width;
1383 fb->fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
1384 fb->fmt.field = V4L2_FIELD_INTERLACED;
1386 if (fb->fmt.pixelformat != V4L2_PIX_FMT_PAL8)
1387 fb->fmt.bytesperline *= 2;
1388 if (fb->fmt.pixelformat == V4L2_PIX_FMT_RGB32 ||
1389 fb->fmt.pixelformat == V4L2_PIX_FMT_YUV32)
1390 fb->fmt.bytesperline *= 2;
1391 fb->fmt.sizeimage = fb->fmt.bytesperline * fb->fmt.height;
1392 fb->base = (void *)itv->osd_video_pbase;
1395 if (itv->osd_chroma_key_state)
1396 fb->flags |= V4L2_FBUF_FLAG_CHROMAKEY;
1398 if (itv->osd_global_alpha_state)
1399 fb->flags |= V4L2_FBUF_FLAG_GLOBAL_ALPHA;
1402 fb->flags |= V4L2_FBUF_FLAG_OVERLAY;
1406 /* no local alpha for RGB565 or unknown formats */
1407 if (pixfmt == 1 || pixfmt > 4)
1410 /* 16-bit formats have inverted local alpha */
1411 if (pixfmt == 2 || pixfmt == 3)
1412 fb->capability |= V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
1414 fb->capability |= V4L2_FBUF_CAP_LOCAL_ALPHA;
1416 if (itv->osd_local_alpha_state) {
1417 /* 16-bit formats have inverted local alpha */
1418 if (pixfmt == 2 || pixfmt == 3)
1419 fb->flags |= V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1421 fb->flags |= V4L2_FBUF_FLAG_LOCAL_ALPHA;
1427 static int ivtv_s_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *fb)
1429 struct ivtv_open_id *id = fh;
1430 struct ivtv *itv = id->itv;
1431 struct yuv_playback_info *yi = &itv->yuv_info;
1433 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1435 if (!itv->osd_video_pbase)
1438 itv->osd_global_alpha_state = (fb->flags & V4L2_FBUF_FLAG_GLOBAL_ALPHA) != 0;
1439 itv->osd_local_alpha_state =
1440 (fb->flags & (V4L2_FBUF_FLAG_LOCAL_ALPHA|V4L2_FBUF_FLAG_LOCAL_INV_ALPHA)) != 0;
1441 itv->osd_chroma_key_state = (fb->flags & V4L2_FBUF_FLAG_CHROMAKEY) != 0;
1442 ivtv_set_osd_alpha(itv);
1443 yi->track_osd = (fb->flags & V4L2_FBUF_FLAG_OVERLAY) != 0;
1444 return ivtv_g_fbuf(file, fh, fb);
1447 static int ivtv_overlay(struct file *file, void *fh, unsigned int on)
1449 struct ivtv_open_id *id = fh;
1450 struct ivtv *itv = id->itv;
1452 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT_OVERLAY))
1455 ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, on != 0);
1460 static int ivtv_subscribe_event(struct v4l2_fh *fh, struct v4l2_event_subscription *sub)
1462 switch (sub->type) {
1463 case V4L2_EVENT_VSYNC:
1464 case V4L2_EVENT_EOS:
1469 return v4l2_event_subscribe(fh, sub);
1472 static int ivtv_log_status(struct file *file, void *fh)
1474 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1475 u32 data[CX2341X_MBOX_MAX_DATA];
1477 int has_output = itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT;
1478 struct v4l2_input vidin;
1479 struct v4l2_audio audin;
1482 IVTV_INFO("================= START STATUS CARD #%d =================\n",
1484 IVTV_INFO("Version: %s Card: %s\n", IVTV_VERSION, itv->card_name);
1485 if (itv->hw_flags & IVTV_HW_TVEEPROM) {
1488 ivtv_read_eeprom(itv, &tv);
1490 ivtv_call_all(itv, core, log_status);
1491 ivtv_get_input(itv, itv->active_input, &vidin);
1492 ivtv_get_audio_input(itv, itv->audio_input, &audin);
1493 IVTV_INFO("Video Input: %s\n", vidin.name);
1494 IVTV_INFO("Audio Input: %s%s\n", audin.name,
1495 (itv->dualwatch_stereo_mode & ~0x300) == 0x200 ? " (Bilingual)" : "");
1497 struct v4l2_output vidout;
1498 struct v4l2_audioout audout;
1499 int mode = itv->output_mode;
1500 static const char * const output_modes[5] = {
1507 static const char * const audio_modes[5] = {
1514 static const char * const alpha_mode[4] = {
1520 static const char * const pixel_format[16] = {
1539 ivtv_get_output(itv, itv->active_output, &vidout);
1540 ivtv_get_audio_output(itv, 0, &audout);
1541 IVTV_INFO("Video Output: %s\n", vidout.name);
1542 IVTV_INFO("Audio Output: %s (Stereo/Bilingual: %s/%s)\n", audout.name,
1543 audio_modes[itv->audio_stereo_mode],
1544 audio_modes[itv->audio_bilingual_mode]);
1545 if (mode < 0 || mode > OUT_PASSTHROUGH)
1547 IVTV_INFO("Output Mode: %s\n", output_modes[mode]);
1548 ivtv_vapi_result(itv, data, CX2341X_OSD_GET_STATE, 0);
1549 data[0] |= (read_reg(0x2a00) >> 7) & 0x40;
1550 IVTV_INFO("Overlay: %s, Alpha: %s, Pixel Format: %s\n",
1551 data[0] & 1 ? "On" : "Off",
1552 alpha_mode[(data[0] >> 1) & 0x3],
1553 pixel_format[(data[0] >> 3) & 0xf]);
1555 IVTV_INFO("Tuner: %s\n",
1556 test_bit(IVTV_F_I_RADIO_USER, &itv->i_flags) ? "Radio" : "TV");
1557 v4l2_ctrl_handler_log_status(&itv->cxhdl.hdl, itv->v4l2_dev.name);
1558 IVTV_INFO("Status flags: 0x%08lx\n", itv->i_flags);
1559 for (i = 0; i < IVTV_MAX_STREAMS; i++) {
1560 struct ivtv_stream *s = &itv->streams[i];
1562 if (s->vdev == NULL || s->buffers == 0)
1564 IVTV_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n", s->name, s->s_flags,
1565 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
1566 (s->buffers * s->buf_size) / 1024, s->buffers);
1569 IVTV_INFO("Read MPG/VBI: %lld/%lld bytes\n",
1570 (long long)itv->mpg_data_received,
1571 (long long)itv->vbi_data_inserted);
1572 IVTV_INFO("================== END STATUS CARD #%d ==================\n",
1578 static int ivtv_decoder_ioctls(struct file *filp, unsigned int cmd, void *arg)
1580 struct ivtv_open_id *id = fh2id(filp->private_data);
1581 struct ivtv *itv = id->itv;
1582 int nonblocking = filp->f_flags & O_NONBLOCK;
1583 struct ivtv_stream *s = &itv->streams[id->type];
1584 unsigned long iarg = (unsigned long)arg;
1587 case IVTV_IOC_DMA_FRAME: {
1588 struct ivtv_dma_frame *args = arg;
1590 IVTV_DEBUG_IOCTL("IVTV_IOC_DMA_FRAME\n");
1591 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1593 if (args->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1595 if (itv->output_mode == OUT_UDMA_YUV && args->y_source == NULL)
1597 if (ivtv_start_decoding(id, id->type)) {
1600 if (ivtv_set_output_mode(itv, OUT_UDMA_YUV) != OUT_UDMA_YUV) {
1601 ivtv_release_stream(s);
1604 /* Mark that this file handle started the UDMA_YUV mode */
1606 if (args->y_source == NULL)
1608 return ivtv_yuv_prep_frame(itv, args);
1611 case VIDEO_GET_PTS: {
1612 u32 data[CX2341X_MBOX_MAX_DATA];
1615 IVTV_DEBUG_IOCTL("VIDEO_GET_PTS\n");
1616 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1620 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1623 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1624 *pts = (u64) ((u64)itv->last_dec_timing[2] << 32) |
1625 (u64)itv->last_dec_timing[1];
1629 if (atomic_read(&itv->decoding)) {
1630 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1631 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1634 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1635 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1636 *pts = (u64) ((u64) data[2] << 32) | (u64) data[1];
1637 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1642 case VIDEO_GET_FRAME_COUNT: {
1643 u32 data[CX2341X_MBOX_MAX_DATA];
1646 IVTV_DEBUG_IOCTL("VIDEO_GET_FRAME_COUNT\n");
1647 if (s->type < IVTV_DEC_STREAM_TYPE_MPG) {
1651 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1654 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
1655 *frame = itv->last_dec_timing[0];
1659 if (atomic_read(&itv->decoding)) {
1660 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1661 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1664 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1665 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1672 struct video_command vc;
1674 IVTV_DEBUG_IOCTL("VIDEO_PLAY\n");
1675 memset(&vc, 0, sizeof(vc));
1676 vc.cmd = VIDEO_CMD_PLAY;
1677 return ivtv_video_command(itv, id, &vc, 0);
1681 struct video_command vc;
1683 IVTV_DEBUG_IOCTL("VIDEO_STOP\n");
1684 memset(&vc, 0, sizeof(vc));
1685 vc.cmd = VIDEO_CMD_STOP;
1686 vc.flags = VIDEO_CMD_STOP_TO_BLACK | VIDEO_CMD_STOP_IMMEDIATELY;
1687 return ivtv_video_command(itv, id, &vc, 0);
1690 case VIDEO_FREEZE: {
1691 struct video_command vc;
1693 IVTV_DEBUG_IOCTL("VIDEO_FREEZE\n");
1694 memset(&vc, 0, sizeof(vc));
1695 vc.cmd = VIDEO_CMD_FREEZE;
1696 return ivtv_video_command(itv, id, &vc, 0);
1699 case VIDEO_CONTINUE: {
1700 struct video_command vc;
1702 IVTV_DEBUG_IOCTL("VIDEO_CONTINUE\n");
1703 memset(&vc, 0, sizeof(vc));
1704 vc.cmd = VIDEO_CMD_CONTINUE;
1705 return ivtv_video_command(itv, id, &vc, 0);
1709 case VIDEO_TRY_COMMAND: {
1710 struct video_command *vc = arg;
1711 int try = (cmd == VIDEO_TRY_COMMAND);
1714 IVTV_DEBUG_IOCTL("VIDEO_TRY_COMMAND %d\n", vc->cmd);
1716 IVTV_DEBUG_IOCTL("VIDEO_COMMAND %d\n", vc->cmd);
1717 return ivtv_video_command(itv, id, vc, try);
1720 case VIDEO_GET_EVENT: {
1721 struct video_event *ev = arg;
1724 IVTV_DEBUG_IOCTL("VIDEO_GET_EVENT\n");
1725 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1727 memset(ev, 0, sizeof(*ev));
1728 set_bit(IVTV_F_I_EV_VSYNC_ENABLED, &itv->i_flags);
1731 if (test_and_clear_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags))
1732 ev->type = VIDEO_EVENT_DECODER_STOPPED;
1733 else if (test_and_clear_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags)) {
1734 ev->type = VIDEO_EVENT_VSYNC;
1735 ev->u.vsync_field = test_bit(IVTV_F_I_EV_VSYNC_FIELD, &itv->i_flags) ?
1736 VIDEO_VSYNC_FIELD_ODD : VIDEO_VSYNC_FIELD_EVEN;
1737 if (itv->output_mode == OUT_UDMA_YUV &&
1738 (itv->yuv_info.lace_mode & IVTV_YUV_MODE_MASK) ==
1739 IVTV_YUV_MODE_PROGRESSIVE) {
1740 ev->u.vsync_field = VIDEO_VSYNC_FIELD_PROGRESSIVE;
1747 /* Wait for event. Note that serialize_lock is locked,
1748 so to allow other processes to access the driver while
1749 we are waiting unlock first and later lock again. */
1750 mutex_unlock(&itv->serialize_lock);
1751 prepare_to_wait(&itv->event_waitq, &wait, TASK_INTERRUPTIBLE);
1752 if (!test_bit(IVTV_F_I_EV_DEC_STOPPED, &itv->i_flags) &&
1753 !test_bit(IVTV_F_I_EV_VSYNC, &itv->i_flags))
1755 finish_wait(&itv->event_waitq, &wait);
1756 mutex_lock(&itv->serialize_lock);
1757 if (signal_pending(current)) {
1758 /* return if a signal was received */
1759 IVTV_DEBUG_INFO("User stopped wait for event\n");
1766 case VIDEO_SELECT_SOURCE:
1767 IVTV_DEBUG_IOCTL("VIDEO_SELECT_SOURCE\n");
1768 if (!(itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT))
1770 return ivtv_passthrough_mode(itv, iarg == VIDEO_SOURCE_DEMUX);
1772 case AUDIO_SET_MUTE:
1773 IVTV_DEBUG_IOCTL("AUDIO_SET_MUTE\n");
1774 itv->speed_mute_audio = iarg;
1777 case AUDIO_CHANNEL_SELECT:
1778 IVTV_DEBUG_IOCTL("AUDIO_CHANNEL_SELECT\n");
1779 if (iarg > AUDIO_STEREO_SWAPPED)
1781 itv->audio_stereo_mode = iarg;
1782 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1785 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1786 IVTV_DEBUG_IOCTL("AUDIO_BILINGUAL_CHANNEL_SELECT\n");
1787 if (iarg > AUDIO_STEREO_SWAPPED)
1789 itv->audio_bilingual_mode = iarg;
1790 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1799 static long ivtv_default(struct file *file, void *fh, int cmd, void *arg)
1801 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
1804 case VIDIOC_INT_RESET: {
1805 u32 val = *(u32 *)arg;
1807 if ((val == 0 && itv->options.newi2c) || (val & 0x01))
1808 ivtv_reset_ir_gpio(itv);
1810 v4l2_subdev_call(itv->sd_video, core, reset, 0);
1814 case IVTV_IOC_DMA_FRAME:
1816 case VIDEO_GET_FRAME_COUNT:
1817 case VIDEO_GET_EVENT:
1821 case VIDEO_CONTINUE:
1823 case VIDEO_TRY_COMMAND:
1824 case VIDEO_SELECT_SOURCE:
1825 case AUDIO_SET_MUTE:
1826 case AUDIO_CHANNEL_SELECT:
1827 case AUDIO_BILINGUAL_CHANNEL_SELECT:
1828 return ivtv_decoder_ioctls(file, cmd, (void *)arg);
1836 static long ivtv_serialized_ioctl(struct ivtv *itv, struct file *filp,
1837 unsigned int cmd, unsigned long arg)
1839 struct video_device *vfd = video_devdata(filp);
1840 struct ivtv_open_id *id = fh2id(filp->private_data);
1843 /* check priority */
1847 case VIDIOC_S_INPUT:
1848 case VIDIOC_S_OUTPUT:
1849 case VIDIOC_S_TUNER:
1850 case VIDIOC_S_FREQUENCY:
1853 case VIDIOC_S_AUDIO:
1854 case VIDIOC_S_AUDOUT:
1855 case VIDIOC_S_EXT_CTRLS:
1857 case VIDIOC_S_PRIORITY:
1858 case VIDIOC_OVERLAY:
1859 ret = v4l2_prio_check(&itv->prio, id->prio);
1864 if (ivtv_debug & IVTV_DBGFLG_IOCTL)
1865 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
1866 ret = video_ioctl2(filp, cmd, arg);
1871 long ivtv_v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1873 struct ivtv_open_id *id = fh2id(filp->private_data);
1874 struct ivtv *itv = id->itv;
1877 /* DQEVENT can block, so this should not run with the serialize lock */
1878 if (cmd == VIDIOC_DQEVENT)
1879 return ivtv_serialized_ioctl(itv, filp, cmd, arg);
1880 mutex_lock(&itv->serialize_lock);
1881 res = ivtv_serialized_ioctl(itv, filp, cmd, arg);
1882 mutex_unlock(&itv->serialize_lock);
1886 static const struct v4l2_ioctl_ops ivtv_ioctl_ops = {
1887 .vidioc_querycap = ivtv_querycap,
1888 .vidioc_g_priority = ivtv_g_priority,
1889 .vidioc_s_priority = ivtv_s_priority,
1890 .vidioc_s_audio = ivtv_s_audio,
1891 .vidioc_g_audio = ivtv_g_audio,
1892 .vidioc_enumaudio = ivtv_enumaudio,
1893 .vidioc_s_audout = ivtv_s_audout,
1894 .vidioc_g_audout = ivtv_g_audout,
1895 .vidioc_enum_input = ivtv_enum_input,
1896 .vidioc_enum_output = ivtv_enum_output,
1897 .vidioc_enumaudout = ivtv_enumaudout,
1898 .vidioc_cropcap = ivtv_cropcap,
1899 .vidioc_s_crop = ivtv_s_crop,
1900 .vidioc_g_crop = ivtv_g_crop,
1901 .vidioc_g_input = ivtv_g_input,
1902 .vidioc_s_input = ivtv_s_input,
1903 .vidioc_g_output = ivtv_g_output,
1904 .vidioc_s_output = ivtv_s_output,
1905 .vidioc_g_frequency = ivtv_g_frequency,
1906 .vidioc_s_frequency = ivtv_s_frequency,
1907 .vidioc_s_tuner = ivtv_s_tuner,
1908 .vidioc_g_tuner = ivtv_g_tuner,
1909 .vidioc_g_enc_index = ivtv_g_enc_index,
1910 .vidioc_g_fbuf = ivtv_g_fbuf,
1911 .vidioc_s_fbuf = ivtv_s_fbuf,
1912 .vidioc_g_std = ivtv_g_std,
1913 .vidioc_s_std = ivtv_s_std,
1914 .vidioc_overlay = ivtv_overlay,
1915 .vidioc_log_status = ivtv_log_status,
1916 .vidioc_enum_fmt_vid_cap = ivtv_enum_fmt_vid_cap,
1917 .vidioc_encoder_cmd = ivtv_encoder_cmd,
1918 .vidioc_try_encoder_cmd = ivtv_try_encoder_cmd,
1919 .vidioc_enum_fmt_vid_out = ivtv_enum_fmt_vid_out,
1920 .vidioc_g_fmt_vid_cap = ivtv_g_fmt_vid_cap,
1921 .vidioc_g_fmt_vbi_cap = ivtv_g_fmt_vbi_cap,
1922 .vidioc_g_fmt_sliced_vbi_cap = ivtv_g_fmt_sliced_vbi_cap,
1923 .vidioc_g_fmt_vid_out = ivtv_g_fmt_vid_out,
1924 .vidioc_g_fmt_vid_out_overlay = ivtv_g_fmt_vid_out_overlay,
1925 .vidioc_g_fmt_sliced_vbi_out = ivtv_g_fmt_sliced_vbi_out,
1926 .vidioc_s_fmt_vid_cap = ivtv_s_fmt_vid_cap,
1927 .vidioc_s_fmt_vbi_cap = ivtv_s_fmt_vbi_cap,
1928 .vidioc_s_fmt_sliced_vbi_cap = ivtv_s_fmt_sliced_vbi_cap,
1929 .vidioc_s_fmt_vid_out = ivtv_s_fmt_vid_out,
1930 .vidioc_s_fmt_vid_out_overlay = ivtv_s_fmt_vid_out_overlay,
1931 .vidioc_s_fmt_sliced_vbi_out = ivtv_s_fmt_sliced_vbi_out,
1932 .vidioc_try_fmt_vid_cap = ivtv_try_fmt_vid_cap,
1933 .vidioc_try_fmt_vbi_cap = ivtv_try_fmt_vbi_cap,
1934 .vidioc_try_fmt_sliced_vbi_cap = ivtv_try_fmt_sliced_vbi_cap,
1935 .vidioc_try_fmt_vid_out = ivtv_try_fmt_vid_out,
1936 .vidioc_try_fmt_vid_out_overlay = ivtv_try_fmt_vid_out_overlay,
1937 .vidioc_try_fmt_sliced_vbi_out = ivtv_try_fmt_sliced_vbi_out,
1938 .vidioc_g_sliced_vbi_cap = ivtv_g_sliced_vbi_cap,
1939 .vidioc_g_chip_ident = ivtv_g_chip_ident,
1940 #ifdef CONFIG_VIDEO_ADV_DEBUG
1941 .vidioc_g_register = ivtv_g_register,
1942 .vidioc_s_register = ivtv_s_register,
1944 .vidioc_default = ivtv_default,
1945 .vidioc_subscribe_event = ivtv_subscribe_event,
1946 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1949 void ivtv_set_funcs(struct video_device *vdev)
1951 vdev->ioctl_ops = &ivtv_ioctl_ops;