18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  cx18 ioctl control functions
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Derived from ivtv-controls.c
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "cx18-driver.h"
138c2ecf20Sopenharmony_ci#include "cx18-cards.h"
148c2ecf20Sopenharmony_ci#include "cx18-ioctl.h"
158c2ecf20Sopenharmony_ci#include "cx18-audio.h"
168c2ecf20Sopenharmony_ci#include "cx18-mailbox.h"
178c2ecf20Sopenharmony_ci#include "cx18-controls.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic int cx18_s_stream_vbi_fmt(struct cx2341x_handler *cxhdl, u32 fmt)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
228c2ecf20Sopenharmony_ci	int type = cxhdl->stream_type->val;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	if (atomic_read(&cx->ana_capturing) > 0)
258c2ecf20Sopenharmony_ci		return -EBUSY;
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	if (fmt != V4L2_MPEG_STREAM_VBI_FMT_IVTV ||
288c2ecf20Sopenharmony_ci	    !(type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS ||
298c2ecf20Sopenharmony_ci	      type == V4L2_MPEG_STREAM_TYPE_MPEG2_DVD ||
308c2ecf20Sopenharmony_ci	      type == V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD)) {
318c2ecf20Sopenharmony_ci		/* Only IVTV fmt VBI insertion & only MPEG-2 PS type streams */
328c2ecf20Sopenharmony_ci		cx->vbi.insert_mpeg = V4L2_MPEG_STREAM_VBI_FMT_NONE;
338c2ecf20Sopenharmony_ci		CX18_DEBUG_INFO("disabled insertion of sliced VBI data into the MPEG stream\n");
348c2ecf20Sopenharmony_ci		return 0;
358c2ecf20Sopenharmony_ci	}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	/* Allocate sliced VBI buffers if needed. */
388c2ecf20Sopenharmony_ci	if (cx->vbi.sliced_mpeg_data[0] == NULL) {
398c2ecf20Sopenharmony_ci		int i;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci		for (i = 0; i < CX18_VBI_FRAMES; i++) {
428c2ecf20Sopenharmony_ci			cx->vbi.sliced_mpeg_data[i] =
438c2ecf20Sopenharmony_ci			       kmalloc(CX18_SLICED_MPEG_DATA_BUFSZ, GFP_KERNEL);
448c2ecf20Sopenharmony_ci			if (cx->vbi.sliced_mpeg_data[i] == NULL) {
458c2ecf20Sopenharmony_ci				while (--i >= 0) {
468c2ecf20Sopenharmony_ci					kfree(cx->vbi.sliced_mpeg_data[i]);
478c2ecf20Sopenharmony_ci					cx->vbi.sliced_mpeg_data[i] = NULL;
488c2ecf20Sopenharmony_ci				}
498c2ecf20Sopenharmony_ci				cx->vbi.insert_mpeg =
508c2ecf20Sopenharmony_ci						  V4L2_MPEG_STREAM_VBI_FMT_NONE;
518c2ecf20Sopenharmony_ci				CX18_WARN("Unable to allocate buffers for sliced VBI data insertion\n");
528c2ecf20Sopenharmony_ci				return -ENOMEM;
538c2ecf20Sopenharmony_ci			}
548c2ecf20Sopenharmony_ci		}
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	cx->vbi.insert_mpeg = fmt;
588c2ecf20Sopenharmony_ci	CX18_DEBUG_INFO("enabled insertion of sliced VBI data into the MPEG PS,when sliced VBI is enabled\n");
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	/*
618c2ecf20Sopenharmony_ci	 * If our current settings have no lines set for capture, store a valid,
628c2ecf20Sopenharmony_ci	 * default set of service lines to capture, in our current settings.
638c2ecf20Sopenharmony_ci	 */
648c2ecf20Sopenharmony_ci	if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
658c2ecf20Sopenharmony_ci		if (cx->is_60hz)
668c2ecf20Sopenharmony_ci			cx->vbi.sliced_in->service_set =
678c2ecf20Sopenharmony_ci							V4L2_SLICED_CAPTION_525;
688c2ecf20Sopenharmony_ci		else
698c2ecf20Sopenharmony_ci			cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
708c2ecf20Sopenharmony_ci		cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci	return 0;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic int cx18_s_video_encoding(struct cx2341x_handler *cxhdl, u32 val)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
788c2ecf20Sopenharmony_ci	int is_mpeg1 = val == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
798c2ecf20Sopenharmony_ci	struct v4l2_subdev_format format = {
808c2ecf20Sopenharmony_ci		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
818c2ecf20Sopenharmony_ci	};
828c2ecf20Sopenharmony_ci	struct v4l2_mbus_framefmt *fmt = &format.format;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	/* fix videodecoder resolution */
858c2ecf20Sopenharmony_ci	fmt->width = cxhdl->width / (is_mpeg1 ? 2 : 1);
868c2ecf20Sopenharmony_ci	fmt->height = cxhdl->height;
878c2ecf20Sopenharmony_ci	fmt->code = MEDIA_BUS_FMT_FIXED;
888c2ecf20Sopenharmony_ci	v4l2_subdev_call(cx->sd_av, pad, set_fmt, NULL, &format);
898c2ecf20Sopenharmony_ci	return 0;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic int cx18_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	static const u32 freqs[3] = { 44100, 48000, 32000 };
958c2ecf20Sopenharmony_ci	struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/* The audio clock of the digitizer must match the codec sample
988c2ecf20Sopenharmony_ci	   rate otherwise you get some very strange effects. */
998c2ecf20Sopenharmony_ci	if (idx < ARRAY_SIZE(freqs))
1008c2ecf20Sopenharmony_ci		cx18_call_all(cx, audio, s_clock_freq, freqs[idx]);
1018c2ecf20Sopenharmony_ci	return 0;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic int cx18_s_audio_mode(struct cx2341x_handler *cxhdl, u32 val)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	cx->dualwatch_stereo_mode = val;
1098c2ecf20Sopenharmony_ci	return 0;
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ciconst struct cx2341x_handler_ops cx18_cxhdl_ops = {
1138c2ecf20Sopenharmony_ci	.s_audio_mode = cx18_s_audio_mode,
1148c2ecf20Sopenharmony_ci	.s_audio_sampling_freq = cx18_s_audio_sampling_freq,
1158c2ecf20Sopenharmony_ci	.s_video_encoding = cx18_s_video_encoding,
1168c2ecf20Sopenharmony_ci	.s_stream_vbi_fmt = cx18_s_stream_vbi_fmt,
1178c2ecf20Sopenharmony_ci};
118