blob: 93f0dae01350316d9c13110504df129baa4f5ecb [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
2 * cx18 ioctl control functions
3 *
4 * Derived from ivtv-controls.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
7 *
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.
12 *
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.
17 *
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
21 * 02111-1307 USA
22 */
Andy Walls225aeb12009-07-23 21:51:29 -030023#include <linux/kernel.h>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030024
25#include "cx18-driver.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030026#include "cx18-cards.h"
27#include "cx18-ioctl.h"
28#include "cx18-audio.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030029#include "cx18-mailbox.h"
30#include "cx18-controls.h"
31
Hans Verkuil2ba58892009-02-13 10:57:48 -030032/* Must be sorted from low to high control ID! */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030033static const u32 user_ctrls[] = {
34 V4L2_CID_USER_CLASS,
35 V4L2_CID_BRIGHTNESS,
36 V4L2_CID_CONTRAST,
37 V4L2_CID_SATURATION,
38 V4L2_CID_HUE,
39 V4L2_CID_AUDIO_VOLUME,
40 V4L2_CID_AUDIO_BALANCE,
41 V4L2_CID_AUDIO_BASS,
42 V4L2_CID_AUDIO_TREBLE,
43 V4L2_CID_AUDIO_MUTE,
44 V4L2_CID_AUDIO_LOUDNESS,
45 0
46};
47
48static const u32 *ctrl_classes[] = {
49 user_ctrls,
50 cx2341x_mpeg_ctrls,
51 NULL
52};
53
Andy Walls3b6fe582008-06-21 08:36:31 -030054int cx18_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030055{
Andy Walls3b6fe582008-06-21 08:36:31 -030056 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030057 const char *name;
58
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030059 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
60 if (qctrl->id == 0)
61 return -EINVAL;
62
63 switch (qctrl->id) {
64 /* Standard V4L2 controls */
Hans Verkuilc6711c32009-06-14 05:20:21 -030065 case V4L2_CID_USER_CLASS:
66 return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030067 case V4L2_CID_BRIGHTNESS:
68 case V4L2_CID_HUE:
69 case V4L2_CID_SATURATION:
70 case V4L2_CID_CONTRAST:
Andy Wallsfa3e7032009-02-16 02:23:25 -030071 if (v4l2_subdev_call(cx->sd_av, core, queryctrl, qctrl))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030072 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
73 return 0;
74
75 case V4L2_CID_AUDIO_VOLUME:
76 case V4L2_CID_AUDIO_MUTE:
77 case V4L2_CID_AUDIO_BALANCE:
78 case V4L2_CID_AUDIO_BASS:
79 case V4L2_CID_AUDIO_TREBLE:
80 case V4L2_CID_AUDIO_LOUDNESS:
Andy Wallsff2a2002009-02-20 23:52:13 -030081 if (v4l2_subdev_call(cx->sd_av, core, queryctrl, qctrl))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030082 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
83 return 0;
84
85 default:
86 if (cx2341x_ctrl_query(&cx->params, qctrl))
87 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
88 return 0;
89 }
90 strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
91 qctrl->name[sizeof(qctrl->name) - 1] = 0;
92 return 0;
93}
94
Andy Walls3b6fe582008-06-21 08:36:31 -030095int cx18_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030096{
Hans Verkuile0e31cd2008-06-22 12:03:28 -030097 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030098 struct v4l2_queryctrl qctrl;
99
100 qctrl.id = qmenu->id;
Andy Walls3b6fe582008-06-21 08:36:31 -0300101 cx18_queryctrl(file, fh, &qctrl);
Hans Verkuile0e31cd2008-06-22 12:03:28 -0300102 return v4l2_ctrl_query_menu(qmenu, &qctrl,
103 cx2341x_ctrl_get_menu(&cx->params, qmenu->id));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300104}
105
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300106static int cx18_try_ctrl(struct file *file, void *fh,
107 struct v4l2_ext_control *vctrl)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300108{
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300109 struct v4l2_queryctrl qctrl;
110 const char **menu_items = NULL;
111 int err;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300112
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300113 qctrl.id = vctrl->id;
114 err = cx18_queryctrl(file, fh, &qctrl);
115 if (err)
116 return err;
117 if (qctrl.type == V4L2_CTRL_TYPE_MENU)
118 menu_items = v4l2_ctrl_get_menu(qctrl.id);
119 return v4l2_ctrl_check(vctrl, &qctrl, menu_items);
120}
Andy Walls3b6fe582008-06-21 08:36:31 -0300121
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300122static int cx18_s_ctrl(struct cx18 *cx, struct v4l2_control *vctrl)
123{
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300124 switch (vctrl->id) {
125 /* Standard V4L2 controls */
126 case V4L2_CID_BRIGHTNESS:
127 case V4L2_CID_HUE:
128 case V4L2_CID_SATURATION:
129 case V4L2_CID_CONTRAST:
Andy Wallsfa3e7032009-02-16 02:23:25 -0300130 return v4l2_subdev_call(cx->sd_av, core, s_ctrl, vctrl);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300131
132 case V4L2_CID_AUDIO_VOLUME:
133 case V4L2_CID_AUDIO_MUTE:
134 case V4L2_CID_AUDIO_BALANCE:
135 case V4L2_CID_AUDIO_BASS:
136 case V4L2_CID_AUDIO_TREBLE:
137 case V4L2_CID_AUDIO_LOUDNESS:
Andy Wallsff2a2002009-02-20 23:52:13 -0300138 return v4l2_subdev_call(cx->sd_av, core, s_ctrl, vctrl);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300139
140 default:
Hans Verkuil37f89f92008-06-22 11:57:31 -0300141 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300142 return -EINVAL;
143 }
144 return 0;
145}
146
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300147static int cx18_g_ctrl(struct cx18 *cx, struct v4l2_control *vctrl)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300148{
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300149 switch (vctrl->id) {
150 /* Standard V4L2 controls */
151 case V4L2_CID_BRIGHTNESS:
152 case V4L2_CID_HUE:
153 case V4L2_CID_SATURATION:
154 case V4L2_CID_CONTRAST:
Andy Wallsfa3e7032009-02-16 02:23:25 -0300155 return v4l2_subdev_call(cx->sd_av, core, g_ctrl, vctrl);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300156
157 case V4L2_CID_AUDIO_VOLUME:
158 case V4L2_CID_AUDIO_MUTE:
159 case V4L2_CID_AUDIO_BALANCE:
160 case V4L2_CID_AUDIO_BASS:
161 case V4L2_CID_AUDIO_TREBLE:
162 case V4L2_CID_AUDIO_LOUDNESS:
Andy Wallsff2a2002009-02-20 23:52:13 -0300163 return v4l2_subdev_call(cx->sd_av, core, g_ctrl, vctrl);
164
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300165 default:
Hans Verkuil37f89f92008-06-22 11:57:31 -0300166 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300167 return -EINVAL;
168 }
169 return 0;
170}
171
Andy Walls49a53752009-03-01 23:10:07 -0300172static int cx18_setup_vbi_fmt(struct cx18 *cx,
173 enum v4l2_mpeg_stream_vbi_fmt fmt,
174 enum v4l2_mpeg_stream_type type)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300175{
176 if (!(cx->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
177 return -EINVAL;
Hans Verkuil31554ae2008-05-25 11:21:27 -0300178 if (atomic_read(&cx->ana_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300179 return -EBUSY;
180
Andy Walls49a53752009-03-01 23:10:07 -0300181 if (fmt != V4L2_MPEG_STREAM_VBI_FMT_IVTV ||
Andy Walls0c629252009-04-26 16:34:36 -0300182 !(type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS ||
183 type == V4L2_MPEG_STREAM_TYPE_MPEG2_DVD ||
184 type == V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD)) {
185 /* Only IVTV fmt VBI insertion & only MPEG-2 PS type streams */
Andy Walls49a53752009-03-01 23:10:07 -0300186 cx->vbi.insert_mpeg = V4L2_MPEG_STREAM_VBI_FMT_NONE;
187 CX18_DEBUG_INFO("disabled insertion of sliced VBI data into "
188 "the MPEG stream\n");
189 return 0;
190 }
191
192 /* Allocate sliced VBI buffers if needed. */
193 if (cx->vbi.sliced_mpeg_data[0] == NULL) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300194 int i;
195
196 for (i = 0; i < CX18_VBI_FRAMES; i++) {
Andy Walls302df972009-01-31 00:33:02 -0300197 cx->vbi.sliced_mpeg_data[i] =
198 kmalloc(CX18_SLICED_MPEG_DATA_BUFSZ, GFP_KERNEL);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300199 if (cx->vbi.sliced_mpeg_data[i] == NULL) {
200 while (--i >= 0) {
201 kfree(cx->vbi.sliced_mpeg_data[i]);
202 cx->vbi.sliced_mpeg_data[i] = NULL;
203 }
Andy Walls49a53752009-03-01 23:10:07 -0300204 cx->vbi.insert_mpeg =
205 V4L2_MPEG_STREAM_VBI_FMT_NONE;
206 CX18_WARN("Unable to allocate buffers for "
207 "sliced VBI data insertion\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300208 return -ENOMEM;
209 }
210 }
211 }
212
213 cx->vbi.insert_mpeg = fmt;
Andy Walls49a53752009-03-01 23:10:07 -0300214 CX18_DEBUG_INFO("enabled insertion of sliced VBI data into the MPEG PS,"
215 "when sliced VBI is enabled\n");
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300216
Andy Walls49a53752009-03-01 23:10:07 -0300217 /*
218 * If our current settings have no lines set for capture, store a valid,
219 * default set of service lines to capture, in our current settings.
220 */
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300221 if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300222 if (cx->is_60hz)
Andy Walls49a53752009-03-01 23:10:07 -0300223 cx->vbi.sliced_in->service_set =
224 V4L2_SLICED_CAPTION_525;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300225 else
226 cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300227 cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300228 }
229 return 0;
230}
231
Andy Walls3b6fe582008-06-21 08:36:31 -0300232int cx18_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300233{
Andy Walls3b6fe582008-06-21 08:36:31 -0300234 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300235 struct v4l2_control ctrl;
236
Andy Walls3b6fe582008-06-21 08:36:31 -0300237 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
238 int i;
239 int err = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300240
Andy Walls3b6fe582008-06-21 08:36:31 -0300241 for (i = 0; i < c->count; i++) {
242 ctrl.id = c->controls[i].id;
243 ctrl.value = c->controls[i].value;
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300244 err = cx18_g_ctrl(cx, &ctrl);
Andy Walls3b6fe582008-06-21 08:36:31 -0300245 c->controls[i].value = ctrl.value;
246 if (err) {
247 c->error_idx = i;
248 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300249 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300250 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300251 return err;
252 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300253 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
254 return cx2341x_ext_ctrls(&cx->params, 0, c, VIDIOC_G_EXT_CTRLS);
255 return -EINVAL;
256}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300257
Andy Walls3b6fe582008-06-21 08:36:31 -0300258int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
259{
260 struct cx18_open_id *id = fh;
261 struct cx18 *cx = id->cx;
262 int ret;
263 struct v4l2_control ctrl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300264
Andy Walls3b6fe582008-06-21 08:36:31 -0300265 ret = v4l2_prio_check(&cx->prio, &id->prio);
266 if (ret)
267 return ret;
268
269 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
270 int i;
271 int err = 0;
272
273 for (i = 0; i < c->count; i++) {
274 ctrl.id = c->controls[i].id;
275 ctrl.value = c->controls[i].value;
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300276 err = cx18_s_ctrl(cx, &ctrl);
Andy Walls3b6fe582008-06-21 08:36:31 -0300277 c->controls[i].value = ctrl.value;
278 if (err) {
279 c->error_idx = i;
280 break;
281 }
282 }
283 return err;
284 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300285 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
Andy Wallsff2a2002009-02-20 23:52:13 -0300286 static u32 freqs[3] = { 44100, 48000, 32000 };
Andy Walls50b86ba2008-11-23 19:16:44 -0300287 struct cx18_api_func_private priv;
Andy Walls3b6fe582008-06-21 08:36:31 -0300288 struct cx2341x_mpeg_params p = cx->params;
289 int err = cx2341x_ext_ctrls(&p, atomic_read(&cx->ana_capturing),
290 c, VIDIOC_S_EXT_CTRLS);
Andy Wallsff2a2002009-02-20 23:52:13 -0300291 unsigned int idx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300292
293 if (err)
294 return err;
295
296 if (p.video_encoding != cx->params.video_encoding) {
297 int is_mpeg1 = p.video_encoding ==
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300298 V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
Andy Walls3b6fe582008-06-21 08:36:31 -0300299 struct v4l2_format fmt;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300300
Andy Walls3b6fe582008-06-21 08:36:31 -0300301 /* fix videodecoder resolution */
302 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
303 fmt.fmt.pix.width = cx->params.width
304 / (is_mpeg1 ? 2 : 1);
305 fmt.fmt.pix.height = cx->params.height;
Andy Wallsfa3e7032009-02-16 02:23:25 -0300306 v4l2_subdev_call(cx->sd_av, video, s_fmt, &fmt);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300307 }
Andy Walls50b86ba2008-11-23 19:16:44 -0300308 priv.cx = cx;
309 priv.s = &cx->streams[id->type];
310 err = cx2341x_update(&priv, cx18_api_func, &cx->params, &p);
Andy Walls49a53752009-03-01 23:10:07 -0300311 if (!err &&
312 (cx->params.stream_vbi_fmt != p.stream_vbi_fmt ||
313 cx->params.stream_type != p.stream_type))
314 err = cx18_setup_vbi_fmt(cx, p.stream_vbi_fmt,
315 p.stream_type);
Andy Walls3b6fe582008-06-21 08:36:31 -0300316 cx->params = p;
317 cx->dualwatch_stereo_mode = p.audio_properties & 0x0300;
Andy Wallsff2a2002009-02-20 23:52:13 -0300318 idx = p.audio_properties & 0x03;
319 /* The audio clock of the digitizer must match the codec sample
320 rate otherwise you get some very strange effects. */
Andy Walls225aeb12009-07-23 21:51:29 -0300321 if (idx < ARRAY_SIZE(freqs))
Andy Wallsff2a2002009-02-20 23:52:13 -0300322 cx18_call_all(cx, audio, s_clock_freq, freqs[idx]);
Andy Walls3b6fe582008-06-21 08:36:31 -0300323 return err;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300324 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300325 return -EINVAL;
326}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300327
Andy Walls3b6fe582008-06-21 08:36:31 -0300328int cx18_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
329{
330 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300331
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300332 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
333 int i;
334 int err = 0;
335
336 for (i = 0; i < c->count; i++) {
337 err = cx18_try_ctrl(file, fh, &c->controls[i]);
338 if (err) {
339 c->error_idx = i;
340 break;
341 }
342 }
343 return err;
344 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300345 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
346 return cx2341x_ext_ctrls(&cx->params,
347 atomic_read(&cx->ana_capturing),
348 c, VIDIOC_TRY_EXT_CTRLS);
349 return -EINVAL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300350}