blob: 8a55ccb8f0c97e2595b3b253736628d6e50f4001 [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;
Hans Verkuilebf984b2015-04-09 04:05:59 -030067 struct v4l2_subdev_format format = {
68 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
69 };
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030070
Hans Verkuilf7b80e62010-06-27 06:07:26 -030071 /* fix videodecoder resolution */
Hans Verkuilebf984b2015-04-09 04:05:59 -030072 format.format.width = cxhdl->width / (is_mpeg1 ? 2 : 1);
73 format.format.height = cxhdl->height;
74 format.format.code = MEDIA_BUS_FMT_FIXED;
75 v4l2_subdev_call(itv->sd_video, pad, set_fmt, NULL, &format);
Hans Verkuilf7b80e62010-06-27 06:07:26 -030076 return 0;
Hans Verkuil3f038d82008-05-29 16:43:54 -030077}
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030078
Hans Verkuilf7b80e62010-06-27 06:07:26 -030079static int ivtv_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
Hans Verkuil3f038d82008-05-29 16:43:54 -030080{
Hans Verkuilf7b80e62010-06-27 06:07:26 -030081 static const u32 freqs[3] = { 44100, 48000, 32000 };
82 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030083
Hans Verkuilf7b80e62010-06-27 06:07:26 -030084 /* The audio clock of the digitizer must match the codec sample
85 rate otherwise you get some very strange effects. */
86 if (idx < ARRAY_SIZE(freqs))
87 ivtv_call_all(itv, audio, s_clock_freq, freqs[idx]);
88 return 0;
Hans Verkuil3f038d82008-05-29 16:43:54 -030089}
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030090
Hans Verkuilf7b80e62010-06-27 06:07:26 -030091static int ivtv_s_audio_mode(struct cx2341x_handler *cxhdl, u32 val)
Hans Verkuil3f038d82008-05-29 16:43:54 -030092{
Hans Verkuilf7b80e62010-06-27 06:07:26 -030093 struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030094
Hans Verkuilf7b80e62010-06-27 06:07:26 -030095 itv->dualwatch_stereo_mode = val;
96 return 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030097}
Hans Verkuilf7b80e62010-06-27 06:07:26 -030098
99struct cx2341x_handler_ops ivtv_cxhdl_ops = {
100 .s_audio_mode = ivtv_s_audio_mode,
101 .s_audio_sampling_freq = ivtv_s_audio_sampling_freq,
102 .s_video_encoding = ivtv_s_video_encoding,
103 .s_stream_vbi_fmt = ivtv_s_stream_vbi_fmt,
104};
Hans Verkuildebf8002011-12-15 10:32:53 -0300105
106int ivtv_g_pts_frame(struct ivtv *itv, s64 *pts, s64 *frame)
107{
108 u32 data[CX2341X_MBOX_MAX_DATA];
109
110 if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
111 *pts = (s64)((u64)itv->last_dec_timing[2] << 32) |
112 (u64)itv->last_dec_timing[1];
113 *frame = itv->last_dec_timing[0];
114 return 0;
115 }
116 *pts = 0;
117 *frame = 0;
118 if (atomic_read(&itv->decoding)) {
119 if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
120 IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
121 return -EIO;
122 }
123 memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
124 set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
125 *pts = (s64)((u64) data[2] << 32) | (u64) data[1];
126 *frame = data[0];
127 /*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
128 }
129 return 0;
130}
131
132static int ivtv_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
133{
Hans Verkuileb2ba852012-03-02 13:02:11 -0300134 struct ivtv *itv = container_of(ctrl->handler, struct ivtv, cxhdl.hdl);
Hans Verkuildebf8002011-12-15 10:32:53 -0300135
136 switch (ctrl->id) {
137 /* V4L2_CID_MPEG_VIDEO_DEC_PTS and V4L2_CID_MPEG_VIDEO_DEC_FRAME
138 control cluster */
139 case V4L2_CID_MPEG_VIDEO_DEC_PTS:
Hans Verkuil2a9ec372014-04-27 03:38:13 -0300140 return ivtv_g_pts_frame(itv, itv->ctrl_pts->p_new.p_s64,
141 itv->ctrl_frame->p_new.p_s64);
Hans Verkuildebf8002011-12-15 10:32:53 -0300142 }
143 return 0;
144}
145
146static int ivtv_s_ctrl(struct v4l2_ctrl *ctrl)
147{
Hans Verkuileb2ba852012-03-02 13:02:11 -0300148 struct ivtv *itv = container_of(ctrl->handler, struct ivtv, cxhdl.hdl);
Hans Verkuildebf8002011-12-15 10:32:53 -0300149
150 switch (ctrl->id) {
151 /* V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK and MULTILINGUAL_PLAYBACK
152 control cluster */
153 case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
154 itv->audio_stereo_mode = itv->ctrl_audio_playback->val - 1;
155 itv->audio_bilingual_mode = itv->ctrl_audio_multilingual_playback->val - 1;
156 ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
157 break;
158 }
159 return 0;
160}
161
162const struct v4l2_ctrl_ops ivtv_hdl_out_ops = {
163 .s_ctrl = ivtv_s_ctrl,
164 .g_volatile_ctrl = ivtv_g_volatile_ctrl,
165};