2 * cx18 ioctl control functions
4 * Derived from ivtv-controls.c
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include "cx18-driver.h"
25 #include "cx18-cards.h"
26 #include "cx18-ioctl.h"
27 #include "cx18-audio.h"
28 #include "cx18-mailbox.h"
29 #include "cx18-controls.h"
31 /* Must be sorted from low to high control ID! */
32 static const u32 user_ctrls[] = {
38 V4L2_CID_AUDIO_VOLUME,
39 V4L2_CID_AUDIO_BALANCE,
41 V4L2_CID_AUDIO_TREBLE,
43 V4L2_CID_AUDIO_LOUDNESS,
47 static const u32 *ctrl_classes[] = {
53 int cx18_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
55 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
58 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
63 /* Standard V4L2 controls */
64 case V4L2_CID_BRIGHTNESS:
66 case V4L2_CID_SATURATION:
67 case V4L2_CID_CONTRAST:
68 if (v4l2_subdev_call(cx->sd_av, core, queryctrl, qctrl))
69 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
72 case V4L2_CID_AUDIO_VOLUME:
73 case V4L2_CID_AUDIO_MUTE:
74 case V4L2_CID_AUDIO_BALANCE:
75 case V4L2_CID_AUDIO_BASS:
76 case V4L2_CID_AUDIO_TREBLE:
77 case V4L2_CID_AUDIO_LOUDNESS:
78 if (v4l2_subdev_call(cx->sd_av, core, queryctrl, qctrl))
79 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
83 if (cx2341x_ctrl_query(&cx->params, qctrl))
84 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
87 strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
88 qctrl->name[sizeof(qctrl->name) - 1] = 0;
92 int cx18_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
94 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
95 struct v4l2_queryctrl qctrl;
98 cx18_queryctrl(file, fh, &qctrl);
99 return v4l2_ctrl_query_menu(qmenu, &qctrl,
100 cx2341x_ctrl_get_menu(&cx->params, qmenu->id));
103 static int cx18_try_ctrl(struct file *file, void *fh,
104 struct v4l2_ext_control *vctrl)
106 struct v4l2_queryctrl qctrl;
107 const char **menu_items = NULL;
110 qctrl.id = vctrl->id;
111 err = cx18_queryctrl(file, fh, &qctrl);
114 if (qctrl.type == V4L2_CTRL_TYPE_MENU)
115 menu_items = v4l2_ctrl_get_menu(qctrl.id);
116 return v4l2_ctrl_check(vctrl, &qctrl, menu_items);
119 static int cx18_s_ctrl(struct cx18 *cx, struct v4l2_control *vctrl)
122 /* Standard V4L2 controls */
123 case V4L2_CID_BRIGHTNESS:
125 case V4L2_CID_SATURATION:
126 case V4L2_CID_CONTRAST:
127 return v4l2_subdev_call(cx->sd_av, core, s_ctrl, vctrl);
129 case V4L2_CID_AUDIO_VOLUME:
130 case V4L2_CID_AUDIO_MUTE:
131 case V4L2_CID_AUDIO_BALANCE:
132 case V4L2_CID_AUDIO_BASS:
133 case V4L2_CID_AUDIO_TREBLE:
134 case V4L2_CID_AUDIO_LOUDNESS:
135 return v4l2_subdev_call(cx->sd_av, core, s_ctrl, vctrl);
138 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
144 static int cx18_g_ctrl(struct cx18 *cx, struct v4l2_control *vctrl)
147 /* Standard V4L2 controls */
148 case V4L2_CID_BRIGHTNESS:
150 case V4L2_CID_SATURATION:
151 case V4L2_CID_CONTRAST:
152 return v4l2_subdev_call(cx->sd_av, core, g_ctrl, vctrl);
154 case V4L2_CID_AUDIO_VOLUME:
155 case V4L2_CID_AUDIO_MUTE:
156 case V4L2_CID_AUDIO_BALANCE:
157 case V4L2_CID_AUDIO_BASS:
158 case V4L2_CID_AUDIO_TREBLE:
159 case V4L2_CID_AUDIO_LOUDNESS:
160 return v4l2_subdev_call(cx->sd_av, core, g_ctrl, vctrl);
163 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
169 static int cx18_setup_vbi_fmt(struct cx18 *cx,
170 enum v4l2_mpeg_stream_vbi_fmt fmt,
171 enum v4l2_mpeg_stream_type type)
173 if (!(cx->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
175 if (atomic_read(&cx->ana_capturing) > 0)
178 if (fmt != V4L2_MPEG_STREAM_VBI_FMT_IVTV ||
179 type != V4L2_MPEG_STREAM_TYPE_MPEG2_PS) {
180 /* We don't do VBI insertion aside from IVTV format in a PS */
181 cx->vbi.insert_mpeg = V4L2_MPEG_STREAM_VBI_FMT_NONE;
182 CX18_DEBUG_INFO("disabled insertion of sliced VBI data into "
183 "the MPEG stream\n");
187 /* Allocate sliced VBI buffers if needed. */
188 if (cx->vbi.sliced_mpeg_data[0] == NULL) {
191 for (i = 0; i < CX18_VBI_FRAMES; i++) {
192 cx->vbi.sliced_mpeg_data[i] =
193 kmalloc(CX18_SLICED_MPEG_DATA_BUFSZ, GFP_KERNEL);
194 if (cx->vbi.sliced_mpeg_data[i] == NULL) {
196 kfree(cx->vbi.sliced_mpeg_data[i]);
197 cx->vbi.sliced_mpeg_data[i] = NULL;
199 cx->vbi.insert_mpeg =
200 V4L2_MPEG_STREAM_VBI_FMT_NONE;
201 CX18_WARN("Unable to allocate buffers for "
202 "sliced VBI data insertion\n");
208 cx->vbi.insert_mpeg = fmt;
209 CX18_DEBUG_INFO("enabled insertion of sliced VBI data into the MPEG PS,"
210 "when sliced VBI is enabled\n");
213 * If our current settings have no lines set for capture, store a valid,
214 * default set of service lines to capture, in our current settings.
216 if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
218 cx->vbi.sliced_in->service_set =
219 V4L2_SLICED_CAPTION_525;
221 cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
222 cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
227 int cx18_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
229 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
230 struct v4l2_control ctrl;
232 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
236 for (i = 0; i < c->count; i++) {
237 ctrl.id = c->controls[i].id;
238 ctrl.value = c->controls[i].value;
239 err = cx18_g_ctrl(cx, &ctrl);
240 c->controls[i].value = ctrl.value;
248 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
249 return cx2341x_ext_ctrls(&cx->params, 0, c, VIDIOC_G_EXT_CTRLS);
253 int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
255 struct cx18_open_id *id = fh;
256 struct cx18 *cx = id->cx;
258 struct v4l2_control ctrl;
260 ret = v4l2_prio_check(&cx->prio, &id->prio);
264 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
268 for (i = 0; i < c->count; i++) {
269 ctrl.id = c->controls[i].id;
270 ctrl.value = c->controls[i].value;
271 err = cx18_s_ctrl(cx, &ctrl);
272 c->controls[i].value = ctrl.value;
280 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
281 static u32 freqs[3] = { 44100, 48000, 32000 };
282 struct cx18_api_func_private priv;
283 struct cx2341x_mpeg_params p = cx->params;
284 int err = cx2341x_ext_ctrls(&p, atomic_read(&cx->ana_capturing),
285 c, VIDIOC_S_EXT_CTRLS);
291 if (p.video_encoding != cx->params.video_encoding) {
292 int is_mpeg1 = p.video_encoding ==
293 V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
294 struct v4l2_format fmt;
296 /* fix videodecoder resolution */
297 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
298 fmt.fmt.pix.width = cx->params.width
299 / (is_mpeg1 ? 2 : 1);
300 fmt.fmt.pix.height = cx->params.height;
301 v4l2_subdev_call(cx->sd_av, video, s_fmt, &fmt);
304 priv.s = &cx->streams[id->type];
305 err = cx2341x_update(&priv, cx18_api_func, &cx->params, &p);
307 (cx->params.stream_vbi_fmt != p.stream_vbi_fmt ||
308 cx->params.stream_type != p.stream_type))
309 err = cx18_setup_vbi_fmt(cx, p.stream_vbi_fmt,
312 cx->dualwatch_stereo_mode = p.audio_properties & 0x0300;
313 idx = p.audio_properties & 0x03;
314 /* The audio clock of the digitizer must match the codec sample
315 rate otherwise you get some very strange effects. */
316 if (idx < sizeof(freqs))
317 cx18_call_all(cx, audio, s_clock_freq, freqs[idx]);
323 int cx18_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
325 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
327 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
331 for (i = 0; i < c->count; i++) {
332 err = cx18_try_ctrl(file, fh, &c->controls[i]);
340 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
341 return cx2341x_ext_ctrls(&cx->params,
342 atomic_read(&cx->ana_capturing),
343 c, VIDIOC_TRY_EXT_CTRLS);