18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci    ioctl control functions
48c2ecf20Sopenharmony_ci    Copyright (C) 2003-2004  Kevin Thayer <nufan_wfk at yahoo.com>
58c2ecf20Sopenharmony_ci    Copyright (C) 2005-2007  Hans Verkuil <hverkuil@xs4all.nl>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include "ivtv-driver.h"
108c2ecf20Sopenharmony_ci#include "ivtv-ioctl.h"
118c2ecf20Sopenharmony_ci#include "ivtv-controls.h"
128c2ecf20Sopenharmony_ci#include "ivtv-mailbox.h"
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic int ivtv_s_stream_vbi_fmt(struct cx2341x_handler *cxhdl, u32 fmt)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci	/* First try to allocate sliced VBI buffers if needed. */
198c2ecf20Sopenharmony_ci	if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
208c2ecf20Sopenharmony_ci		int i;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci		for (i = 0; i < IVTV_VBI_FRAMES; i++) {
238c2ecf20Sopenharmony_ci			/* Yuck, hardcoded. Needs to be a define */
248c2ecf20Sopenharmony_ci			itv->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
258c2ecf20Sopenharmony_ci			if (itv->vbi.sliced_mpeg_data[i] == NULL) {
268c2ecf20Sopenharmony_ci				while (--i >= 0) {
278c2ecf20Sopenharmony_ci					kfree(itv->vbi.sliced_mpeg_data[i]);
288c2ecf20Sopenharmony_ci					itv->vbi.sliced_mpeg_data[i] = NULL;
298c2ecf20Sopenharmony_ci				}
308c2ecf20Sopenharmony_ci				return -ENOMEM;
318c2ecf20Sopenharmony_ci			}
328c2ecf20Sopenharmony_ci		}
338c2ecf20Sopenharmony_ci	}
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	itv->vbi.insert_mpeg = fmt;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (itv->vbi.insert_mpeg == 0) {
388c2ecf20Sopenharmony_ci		return 0;
398c2ecf20Sopenharmony_ci	}
408c2ecf20Sopenharmony_ci	/* Need sliced data for mpeg insertion */
418c2ecf20Sopenharmony_ci	if (ivtv_get_service_set(itv->vbi.sliced_in) == 0) {
428c2ecf20Sopenharmony_ci		if (itv->is_60hz)
438c2ecf20Sopenharmony_ci			itv->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
448c2ecf20Sopenharmony_ci		else
458c2ecf20Sopenharmony_ci			itv->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
468c2ecf20Sopenharmony_ci		ivtv_expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
478c2ecf20Sopenharmony_ci	}
488c2ecf20Sopenharmony_ci	return 0;
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int ivtv_s_video_encoding(struct cx2341x_handler *cxhdl, u32 val)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
548c2ecf20Sopenharmony_ci	int is_mpeg1 = val == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
558c2ecf20Sopenharmony_ci	struct v4l2_subdev_format format = {
568c2ecf20Sopenharmony_ci		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
578c2ecf20Sopenharmony_ci	};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	/* fix videodecoder resolution */
608c2ecf20Sopenharmony_ci	format.format.width = cxhdl->width / (is_mpeg1 ? 2 : 1);
618c2ecf20Sopenharmony_ci	format.format.height = cxhdl->height;
628c2ecf20Sopenharmony_ci	format.format.code = MEDIA_BUS_FMT_FIXED;
638c2ecf20Sopenharmony_ci	v4l2_subdev_call(itv->sd_video, pad, set_fmt, NULL, &format);
648c2ecf20Sopenharmony_ci	return 0;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int ivtv_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	static const u32 freqs[3] = { 44100, 48000, 32000 };
708c2ecf20Sopenharmony_ci	struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/* The audio clock of the digitizer must match the codec sample
738c2ecf20Sopenharmony_ci	   rate otherwise you get some very strange effects. */
748c2ecf20Sopenharmony_ci	if (idx < ARRAY_SIZE(freqs))
758c2ecf20Sopenharmony_ci		ivtv_call_all(itv, audio, s_clock_freq, freqs[idx]);
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic int ivtv_s_audio_mode(struct cx2341x_handler *cxhdl, u32 val)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct ivtv *itv = container_of(cxhdl, struct ivtv, cxhdl);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	itv->dualwatch_stereo_mode = val;
848c2ecf20Sopenharmony_ci	return 0;
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciconst struct cx2341x_handler_ops ivtv_cxhdl_ops = {
888c2ecf20Sopenharmony_ci	.s_audio_mode = ivtv_s_audio_mode,
898c2ecf20Sopenharmony_ci	.s_audio_sampling_freq = ivtv_s_audio_sampling_freq,
908c2ecf20Sopenharmony_ci	.s_video_encoding = ivtv_s_video_encoding,
918c2ecf20Sopenharmony_ci	.s_stream_vbi_fmt = ivtv_s_stream_vbi_fmt,
928c2ecf20Sopenharmony_ci};
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciint ivtv_g_pts_frame(struct ivtv *itv, s64 *pts, s64 *frame)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	u32 data[CX2341X_MBOX_MAX_DATA];
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (test_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags)) {
998c2ecf20Sopenharmony_ci		*pts = (s64)((u64)itv->last_dec_timing[2] << 32) |
1008c2ecf20Sopenharmony_ci			(u64)itv->last_dec_timing[1];
1018c2ecf20Sopenharmony_ci		*frame = itv->last_dec_timing[0];
1028c2ecf20Sopenharmony_ci		return 0;
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci	*pts = 0;
1058c2ecf20Sopenharmony_ci	*frame = 0;
1068c2ecf20Sopenharmony_ci	if (atomic_read(&itv->decoding)) {
1078c2ecf20Sopenharmony_ci		if (ivtv_api(itv, CX2341X_DEC_GET_TIMING_INFO, 5, data)) {
1088c2ecf20Sopenharmony_ci			IVTV_DEBUG_WARN("GET_TIMING: couldn't read clock\n");
1098c2ecf20Sopenharmony_ci			return -EIO;
1108c2ecf20Sopenharmony_ci		}
1118c2ecf20Sopenharmony_ci		memcpy(itv->last_dec_timing, data, sizeof(itv->last_dec_timing));
1128c2ecf20Sopenharmony_ci		set_bit(IVTV_F_I_VALID_DEC_TIMINGS, &itv->i_flags);
1138c2ecf20Sopenharmony_ci		*pts = (s64)((u64) data[2] << 32) | (u64) data[1];
1148c2ecf20Sopenharmony_ci		*frame = data[0];
1158c2ecf20Sopenharmony_ci		/*timing->scr = (u64) (((u64) data[4] << 32) | (u64) (data[3]));*/
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci	return 0;
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic int ivtv_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	struct ivtv *itv = container_of(ctrl->handler, struct ivtv, cxhdl.hdl);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	switch (ctrl->id) {
1258c2ecf20Sopenharmony_ci	/* V4L2_CID_MPEG_VIDEO_DEC_PTS and V4L2_CID_MPEG_VIDEO_DEC_FRAME
1268c2ecf20Sopenharmony_ci	   control cluster */
1278c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_VIDEO_DEC_PTS:
1288c2ecf20Sopenharmony_ci		return ivtv_g_pts_frame(itv, itv->ctrl_pts->p_new.p_s64,
1298c2ecf20Sopenharmony_ci					     itv->ctrl_frame->p_new.p_s64);
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci	return 0;
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic int ivtv_s_ctrl(struct v4l2_ctrl *ctrl)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	struct ivtv *itv = container_of(ctrl->handler, struct ivtv, cxhdl.hdl);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	switch (ctrl->id) {
1398c2ecf20Sopenharmony_ci	/* V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK and MULTILINGUAL_PLAYBACK
1408c2ecf20Sopenharmony_ci	   control cluster */
1418c2ecf20Sopenharmony_ci	case V4L2_CID_MPEG_AUDIO_DEC_PLAYBACK:
1428c2ecf20Sopenharmony_ci		itv->audio_stereo_mode = itv->ctrl_audio_playback->val - 1;
1438c2ecf20Sopenharmony_ci		itv->audio_bilingual_mode = itv->ctrl_audio_multilingual_playback->val - 1;
1448c2ecf20Sopenharmony_ci		ivtv_vapi(itv, CX2341X_DEC_SET_AUDIO_MODE, 2, itv->audio_bilingual_mode, itv->audio_stereo_mode);
1458c2ecf20Sopenharmony_ci		break;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci	return 0;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciconst struct v4l2_ctrl_ops ivtv_hdl_out_ops = {
1518c2ecf20Sopenharmony_ci	.s_ctrl = ivtv_s_ctrl,
1528c2ecf20Sopenharmony_ci	.g_volatile_ctrl = ivtv_g_volatile_ctrl,
1538c2ecf20Sopenharmony_ci};
154