1/*
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"
22#include "ivtv-ioctl.h"
23#include "ivtv-controls.h"
24#include "ivtv-mailbox.h"
25
26static int ivtv_s_stream_vbi_fmt(struct cx2341x_handler *cxhdl, u32 fmt)
27{
28	struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
29
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 */
53	if (ivtv_get_service_set(itv->vbi.sliced_in) == 0) {
54		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;
58		ivtv_expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
59	}
60	return 0;
61}
62
63static int ivtv_s_video_encoding(struct cx2341x_handler *cxhdl, u32 val)
64{
65	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;
68
69	/* fix videodecoder resolution */
70	fmt.width = cxhdl->width / (is_mpeg1 ? 2 : 1);
71	fmt.height = cxhdl->height;
72	fmt.code = MEDIA_BUS_FMT_FIXED;
73	v4l2_subdev_call(itv->sd_video, video, s_mbus_fmt, &fmt);
74	return 0;
75}
76
77static int ivtv_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
78{
79	static const u32 freqs[3] = { 44100, 48000, 32000 };
80	struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
81
82	/* 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;
87}
88
89static int ivtv_s_audio_mode(struct cx2341x_handler *cxhdl, u32 val)
90{
91	struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
92
93	itv->dualwatch_stereo_mode = val;
94	return 0;
95}
96
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};
103
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, cxhdl.hdl);
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->p_new.p_s64,
139					     itv->ctrl_frame->p_new.p_s64);
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, cxhdl.hdl);
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};
164