1*c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2b285192aSMauro Carvalho Chehab /*
3b285192aSMauro Carvalho Chehab * cx18 ioctl control functions
4b285192aSMauro Carvalho Chehab *
5b285192aSMauro Carvalho Chehab * Derived from ivtv-controls.c
6b285192aSMauro Carvalho Chehab *
7b285192aSMauro Carvalho Chehab * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
8b285192aSMauro Carvalho Chehab */
9b285192aSMauro Carvalho Chehab #include <linux/kernel.h>
10b285192aSMauro Carvalho Chehab #include <linux/slab.h>
11b285192aSMauro Carvalho Chehab
12b285192aSMauro Carvalho Chehab #include "cx18-driver.h"
13b285192aSMauro Carvalho Chehab #include "cx18-cards.h"
14b285192aSMauro Carvalho Chehab #include "cx18-ioctl.h"
15b285192aSMauro Carvalho Chehab #include "cx18-audio.h"
16b285192aSMauro Carvalho Chehab #include "cx18-mailbox.h"
17b285192aSMauro Carvalho Chehab #include "cx18-controls.h"
18b285192aSMauro Carvalho Chehab
cx18_s_stream_vbi_fmt(struct cx2341x_handler * cxhdl,u32 fmt)19b285192aSMauro Carvalho Chehab static int cx18_s_stream_vbi_fmt(struct cx2341x_handler *cxhdl, u32 fmt)
20b285192aSMauro Carvalho Chehab {
21b285192aSMauro Carvalho Chehab struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
22b285192aSMauro Carvalho Chehab int type = cxhdl->stream_type->val;
23b285192aSMauro Carvalho Chehab
24b285192aSMauro Carvalho Chehab if (atomic_read(&cx->ana_capturing) > 0)
25b285192aSMauro Carvalho Chehab return -EBUSY;
26b285192aSMauro Carvalho Chehab
27b285192aSMauro Carvalho Chehab if (fmt != V4L2_MPEG_STREAM_VBI_FMT_IVTV ||
28b285192aSMauro Carvalho Chehab !(type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS ||
29b285192aSMauro Carvalho Chehab type == V4L2_MPEG_STREAM_TYPE_MPEG2_DVD ||
30b285192aSMauro Carvalho Chehab type == V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD)) {
31b285192aSMauro Carvalho Chehab /* Only IVTV fmt VBI insertion & only MPEG-2 PS type streams */
32b285192aSMauro Carvalho Chehab cx->vbi.insert_mpeg = V4L2_MPEG_STREAM_VBI_FMT_NONE;
336beb1388SMauro Carvalho Chehab CX18_DEBUG_INFO("disabled insertion of sliced VBI data into the MPEG stream\n");
34b285192aSMauro Carvalho Chehab return 0;
35b285192aSMauro Carvalho Chehab }
36b285192aSMauro Carvalho Chehab
37b285192aSMauro Carvalho Chehab /* Allocate sliced VBI buffers if needed. */
38b285192aSMauro Carvalho Chehab if (cx->vbi.sliced_mpeg_data[0] == NULL) {
39b285192aSMauro Carvalho Chehab int i;
40b285192aSMauro Carvalho Chehab
41b285192aSMauro Carvalho Chehab for (i = 0; i < CX18_VBI_FRAMES; i++) {
42b285192aSMauro Carvalho Chehab cx->vbi.sliced_mpeg_data[i] =
43b285192aSMauro Carvalho Chehab kmalloc(CX18_SLICED_MPEG_DATA_BUFSZ, GFP_KERNEL);
44b285192aSMauro Carvalho Chehab if (cx->vbi.sliced_mpeg_data[i] == NULL) {
45b285192aSMauro Carvalho Chehab while (--i >= 0) {
46b285192aSMauro Carvalho Chehab kfree(cx->vbi.sliced_mpeg_data[i]);
47b285192aSMauro Carvalho Chehab cx->vbi.sliced_mpeg_data[i] = NULL;
48b285192aSMauro Carvalho Chehab }
49b285192aSMauro Carvalho Chehab cx->vbi.insert_mpeg =
50b285192aSMauro Carvalho Chehab V4L2_MPEG_STREAM_VBI_FMT_NONE;
516beb1388SMauro Carvalho Chehab CX18_WARN("Unable to allocate buffers for sliced VBI data insertion\n");
52b285192aSMauro Carvalho Chehab return -ENOMEM;
53b285192aSMauro Carvalho Chehab }
54b285192aSMauro Carvalho Chehab }
55b285192aSMauro Carvalho Chehab }
56b285192aSMauro Carvalho Chehab
57b285192aSMauro Carvalho Chehab cx->vbi.insert_mpeg = fmt;
586beb1388SMauro Carvalho Chehab CX18_DEBUG_INFO("enabled insertion of sliced VBI data into the MPEG PS,when sliced VBI is enabled\n");
59b285192aSMauro Carvalho Chehab
60b285192aSMauro Carvalho Chehab /*
61b285192aSMauro Carvalho Chehab * If our current settings have no lines set for capture, store a valid,
62b285192aSMauro Carvalho Chehab * default set of service lines to capture, in our current settings.
63b285192aSMauro Carvalho Chehab */
64b285192aSMauro Carvalho Chehab if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
65b285192aSMauro Carvalho Chehab if (cx->is_60hz)
66b285192aSMauro Carvalho Chehab cx->vbi.sliced_in->service_set =
67b285192aSMauro Carvalho Chehab V4L2_SLICED_CAPTION_525;
68b285192aSMauro Carvalho Chehab else
69b285192aSMauro Carvalho Chehab cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
70b285192aSMauro Carvalho Chehab cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
71b285192aSMauro Carvalho Chehab }
72b285192aSMauro Carvalho Chehab return 0;
73b285192aSMauro Carvalho Chehab }
74b285192aSMauro Carvalho Chehab
cx18_s_video_encoding(struct cx2341x_handler * cxhdl,u32 val)75b285192aSMauro Carvalho Chehab static int cx18_s_video_encoding(struct cx2341x_handler *cxhdl, u32 val)
76b285192aSMauro Carvalho Chehab {
77b285192aSMauro Carvalho Chehab struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
78b285192aSMauro Carvalho Chehab int is_mpeg1 = val == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
79ebf984bbSHans Verkuil struct v4l2_subdev_format format = {
80ebf984bbSHans Verkuil .which = V4L2_SUBDEV_FORMAT_ACTIVE,
81ebf984bbSHans Verkuil };
82ebf984bbSHans Verkuil struct v4l2_mbus_framefmt *fmt = &format.format;
83b285192aSMauro Carvalho Chehab
84b285192aSMauro Carvalho Chehab /* fix videodecoder resolution */
85ebf984bbSHans Verkuil fmt->width = cxhdl->width / (is_mpeg1 ? 2 : 1);
86ebf984bbSHans Verkuil fmt->height = cxhdl->height;
87ebf984bbSHans Verkuil fmt->code = MEDIA_BUS_FMT_FIXED;
88ebf984bbSHans Verkuil v4l2_subdev_call(cx->sd_av, pad, set_fmt, NULL, &format);
89b285192aSMauro Carvalho Chehab return 0;
90b285192aSMauro Carvalho Chehab }
91b285192aSMauro Carvalho Chehab
cx18_s_audio_sampling_freq(struct cx2341x_handler * cxhdl,u32 idx)92b285192aSMauro Carvalho Chehab static int cx18_s_audio_sampling_freq(struct cx2341x_handler *cxhdl, u32 idx)
93b285192aSMauro Carvalho Chehab {
94b285192aSMauro Carvalho Chehab static const u32 freqs[3] = { 44100, 48000, 32000 };
95b285192aSMauro Carvalho Chehab struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
96b285192aSMauro Carvalho Chehab
97b285192aSMauro Carvalho Chehab /* The audio clock of the digitizer must match the codec sample
98b285192aSMauro Carvalho Chehab rate otherwise you get some very strange effects. */
99b285192aSMauro Carvalho Chehab if (idx < ARRAY_SIZE(freqs))
100b285192aSMauro Carvalho Chehab cx18_call_all(cx, audio, s_clock_freq, freqs[idx]);
101b285192aSMauro Carvalho Chehab return 0;
102b285192aSMauro Carvalho Chehab }
103b285192aSMauro Carvalho Chehab
cx18_s_audio_mode(struct cx2341x_handler * cxhdl,u32 val)104b285192aSMauro Carvalho Chehab static int cx18_s_audio_mode(struct cx2341x_handler *cxhdl, u32 val)
105b285192aSMauro Carvalho Chehab {
106b285192aSMauro Carvalho Chehab struct cx18 *cx = container_of(cxhdl, struct cx18, cxhdl);
107b285192aSMauro Carvalho Chehab
108b285192aSMauro Carvalho Chehab cx->dualwatch_stereo_mode = val;
109b285192aSMauro Carvalho Chehab return 0;
110b285192aSMauro Carvalho Chehab }
111b285192aSMauro Carvalho Chehab
112083206fcSJulia Lawall const struct cx2341x_handler_ops cx18_cxhdl_ops = {
113b285192aSMauro Carvalho Chehab .s_audio_mode = cx18_s_audio_mode,
114b285192aSMauro Carvalho Chehab .s_audio_sampling_freq = cx18_s_audio_sampling_freq,
115b285192aSMauro Carvalho Chehab .s_video_encoding = cx18_s_video_encoding,
116b285192aSMauro Carvalho Chehab .s_stream_vbi_fmt = cx18_s_stream_vbi_fmt,
117b285192aSMauro Carvalho Chehab };
118