blob: e2901184db0982aaace00c3b48d103c6e0a35c27 [file] [log] [blame]
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001/*
2 ioctl control functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
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.
10
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.
15
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
19 */
20
21#include "ivtv-driver.h"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030022#include "ivtv-ioctl.h"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030023#include "ivtv-controls.h"
Hans Verkuildebf8002011-12-15 10:32:53 -030024#include "ivtv-mailbox.h"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030025
Hans Verkuilf7b80e62010-06-27 06:07:26 -030026static int ivtv_s_stream_vbi_fmt(struct cx2341x_handler *cxhdl, u32 fmt)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030027{
Hans Verkuilf7b80e62010-06-27 06:07:26 -030028 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030029
30 /* First try to allocate sliced VBI buffers if needed. */
31 if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
32 int i;
33
34 for (i = 0; i < IVTV_VBI_FRAMES; i++) {
35 /* Yuck, hardcoded. Needs to be a define */
36 itv->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
37 if (itv->vbi.sliced_mpeg_data[i] == NULL) {
38 while (--i >= 0) {
39 kfree(itv->vbi.sliced_mpeg_data[i]);
40 itv->vbi.sliced_mpeg_data[i] = NULL;
41 }
42 return -ENOMEM;
43 }
44 }
45 }
46
47 itv->vbi.insert_mpeg = fmt;
48
49 if (itv->vbi.insert_mpeg == 0) {
50 return 0;
51 }
52 /* Need sliced data for mpeg insertion */
Hans Verkuilfeb5bce2008-05-01 09:22:13 -030053 if (ivtv_get_service_set(itv->vbi.sliced_in) == 0) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030054 if (itv->is_60hz)
55 itv->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
56 else
57 itv->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
Hans Verkuilfeb5bce2008-05-01 09:22:13 -030058 ivtv_expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030059 }
60 return 0;
61}
62
Hans Verkuilf7b80e62010-06-27 06:07:26 -030063static int ivtv_s_video_encoding(struct cx2341x_handler *cxhdl, u32 val)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030064{
Hans Verkuilf7b80e62010-06-27 06:07:26 -030065 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
66 int is_mpeg1 = val == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
67 struct v4l2_mbus_framefmt fmt;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030068
Hans Verkuilf7b80e62010-06-27 06:07:26 -030069 /* fix videodecoder resolution */
70 fmt.width = cxhdl->width / (is_mpeg1 ? 2 : 1);
71 fmt.height = cxhdl->height;
72 fmt.code = V4L2_MBUS_FMT_FIXED;
73 v4l2_subdev_call(itv->sd_video, video, s_mbus_fmt, &fmt);
74 return 0;
Hans Verkuil3f038d82008-05-29 16:43:54 -030075}
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030076
Hans Verkuilf7b80e62010-06-27 06:07:26 -030077static int ivtv_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
Hans Verkuil3f038d82008-05-29 16:43:54 -030078{
Hans Verkuilf7b80e62010-06-27 06:07:26 -030079 static const u32 freqs[3] = { 44100, 48000, 32000 };
80 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030081
Hans Verkuilf7b80e62010-06-27 06:07:26 -030082 /* The audio clock of the digitizer must match the codec sample
83 rate otherwise you get some very strange effects. */
84 if (idx < ARRAY_SIZE(freqs))
85 ivtv_call_all(itv, audio, s_clock_freq, freqs[idx]);
86 return 0;
Hans Verkuil3f038d82008-05-29 16:43:54 -030087}
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030088
Hans Verkuilf7b80e62010-06-27 06:07:26 -030089static int ivtv_s_audio_mode(struct cx2341x_handler *cxhdl, u32 val)
Hans Verkuil3f038d82008-05-29 16:43:54 -030090{
Hans Verkuilf7b80e62010-06-27 06:07:26 -030091 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030092
Hans Verkuilf7b80e62010-06-27 06:07:26 -030093 itv->dualwatch_stereo_mode = val;
94 return 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030095}
Hans Verkuilf7b80e62010-06-27 06:07:26 -030096
97struct cx2341x_handler_ops ivtv_cxhdl_ops = {
98 .s_audio_mode = ivtv_s_audio_mode,
99 .s_audio_sampling_freq = ivtv_s_audio_sampling_freq,
100 .s_video_encoding = ivtv_s_video_encoding,
101 .s_stream_vbi_fmt = ivtv_s_stream_vbi_fmt,
102};
Hans Verkuildebf8002011-12-15 10:32:53 -0300103
104int ivtv_g_pts_frame(struct ivtv *itv, s64 *pts, s64 *frame)
105{
106 u32 data[CX2341X_MBOX_MAX_DATA];
107
108 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
109 *pts = (s64)((u64)itv->last_dec_timing[2] << 32) |
110 (u64)itv->last_dec_timing[1];
111 *frame = itv->last_dec_timing[0];
112 return 0;
113 }
114 *pts = 0;
115 *frame = 0;
116 if (atomic_read(&itv->decoding)) {
117 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
118 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
119 return -EIO;
120 }
121 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
122 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
123 *pts = (s64)((u64) data[2] << 32) | (u64) data[1];
124 *frame = data[0];
125 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
126 }
127 return 0;
128}
129
130static int ivtv_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
131{
132 struct ivtv *itv = container_of(ctrl->handler, struct ivtv, hdl_out);
133
134 switch (ctrl->id) {
135 /* V4L2_CID_MPEG_VIDEO_DEC_PTS and V4L2_CID_MPEG_VIDEO_DEC_FRAME
136 control cluster */
137 case V4L2_CID_MPEG_VIDEO_DEC_PTS:
138 return ivtv_g_pts_frame(itv, &itv->ctrl_pts->val64,
139 &itv->ctrl_frame->val64);
140 }
141 return 0;
142}
143
144static int ivtv_s_ctrl(struct v4l2_ctrl *ctrl)
145{
146 struct ivtv *itv = container_of(ctrl->handler, struct ivtv, hdl_out);
147
148 switch (ctrl->id) {
149 /* V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK and MULTILINGUAL_PLAYBACK
150 control cluster */
151 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
152 itv->audio_stereo_mode = itv->ctrl_audio_playback->val - 1;
153 itv->audio_bilingual_mode = itv->ctrl_audio_multilingual_playback->val - 1;
154 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
155 break;
156 }
157 return 0;
158}
159
160const struct v4l2_ctrl_ops ivtv_hdl_out_ops = {
161 .s_ctrl = ivtv_s_ctrl,
162 .g_volatile_ctrl = ivtv_g_volatile_ctrl,
163};