blob: e5604c2328ad66f6200208bc3ec37fe6aee9aaa8 [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 */
23
24#include "cx18-driver.h"
25#include "cx18-av-core.h"
26#include "cx18-cards.h"
27#include "cx18-ioctl.h"
28#include "cx18-audio.h"
29#include "cx18-i2c.h"
30#include "cx18-mailbox.h"
31#include "cx18-controls.h"
32
Hans Verkuil2ba58892009-02-13 10:57:48 -030033/* Must be sorted from low to high control ID! */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030034static const u32 user_ctrls[] = {
35 V4L2_CID_USER_CLASS,
36 V4L2_CID_BRIGHTNESS,
37 V4L2_CID_CONTRAST,
38 V4L2_CID_SATURATION,
39 V4L2_CID_HUE,
40 V4L2_CID_AUDIO_VOLUME,
41 V4L2_CID_AUDIO_BALANCE,
42 V4L2_CID_AUDIO_BASS,
43 V4L2_CID_AUDIO_TREBLE,
44 V4L2_CID_AUDIO_MUTE,
45 V4L2_CID_AUDIO_LOUDNESS,
46 0
47};
48
49static const u32 *ctrl_classes[] = {
50 user_ctrls,
51 cx2341x_mpeg_ctrls,
52 NULL
53};
54
Andy Walls3b6fe582008-06-21 08:36:31 -030055int cx18_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030056{
Andy Walls3b6fe582008-06-21 08:36:31 -030057 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030058 const char *name;
59
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030060 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
61 if (qctrl->id == 0)
62 return -EINVAL;
63
64 switch (qctrl->id) {
65 /* Standard V4L2 controls */
66 case V4L2_CID_BRIGHTNESS:
67 case V4L2_CID_HUE:
68 case V4L2_CID_SATURATION:
69 case V4L2_CID_CONTRAST:
Andy Wallsfa3e7032009-02-16 02:23:25 -030070 if (v4l2_subdev_call(cx->sd_av, core, queryctrl, qctrl))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030071 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
72 return 0;
73
74 case V4L2_CID_AUDIO_VOLUME:
75 case V4L2_CID_AUDIO_MUTE:
76 case V4L2_CID_AUDIO_BALANCE:
77 case V4L2_CID_AUDIO_BASS:
78 case V4L2_CID_AUDIO_TREBLE:
79 case V4L2_CID_AUDIO_LOUDNESS:
Andy Wallsff2a2002009-02-20 23:52:13 -030080 if (v4l2_subdev_call(cx->sd_av, core, queryctrl, qctrl))
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030081 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
82 return 0;
83
84 default:
85 if (cx2341x_ctrl_query(&cx->params, qctrl))
86 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
87 return 0;
88 }
89 strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
90 qctrl->name[sizeof(qctrl->name) - 1] = 0;
91 return 0;
92}
93
Andy Walls3b6fe582008-06-21 08:36:31 -030094int cx18_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030095{
Hans Verkuile0e31cd2008-06-22 12:03:28 -030096 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030097 struct v4l2_queryctrl qctrl;
98
99 qctrl.id = qmenu->id;
Andy Walls3b6fe582008-06-21 08:36:31 -0300100 cx18_queryctrl(file, fh, &qctrl);
Hans Verkuile0e31cd2008-06-22 12:03:28 -0300101 return v4l2_ctrl_query_menu(qmenu, &qctrl,
102 cx2341x_ctrl_get_menu(&cx->params, qmenu->id));
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300103}
104
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300105static int cx18_try_ctrl(struct file *file, void *fh,
106 struct v4l2_ext_control *vctrl)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300107{
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300108 struct v4l2_queryctrl qctrl;
109 const char **menu_items = NULL;
110 int err;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300111
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300112 qctrl.id = vctrl->id;
113 err = cx18_queryctrl(file, fh, &qctrl);
114 if (err)
115 return err;
116 if (qctrl.type == V4L2_CTRL_TYPE_MENU)
117 menu_items = v4l2_ctrl_get_menu(qctrl.id);
118 return v4l2_ctrl_check(vctrl, &qctrl, menu_items);
119}
Andy Walls3b6fe582008-06-21 08:36:31 -0300120
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300121static int cx18_s_ctrl(struct cx18 *cx, struct v4l2_control *vctrl)
122{
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300123 switch (vctrl->id) {
124 /* Standard V4L2 controls */
125 case V4L2_CID_BRIGHTNESS:
126 case V4L2_CID_HUE:
127 case V4L2_CID_SATURATION:
128 case V4L2_CID_CONTRAST:
Andy Wallsfa3e7032009-02-16 02:23:25 -0300129 return v4l2_subdev_call(cx->sd_av, core, s_ctrl, vctrl);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300130
131 case V4L2_CID_AUDIO_VOLUME:
132 case V4L2_CID_AUDIO_MUTE:
133 case V4L2_CID_AUDIO_BALANCE:
134 case V4L2_CID_AUDIO_BASS:
135 case V4L2_CID_AUDIO_TREBLE:
136 case V4L2_CID_AUDIO_LOUDNESS:
Andy Wallsff2a2002009-02-20 23:52:13 -0300137 return v4l2_subdev_call(cx->sd_av, core, s_ctrl, vctrl);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300138
139 default:
Hans Verkuil37f89f92008-06-22 11:57:31 -0300140 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300141 return -EINVAL;
142 }
143 return 0;
144}
145
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300146static int cx18_g_ctrl(struct cx18 *cx, struct v4l2_control *vctrl)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300147{
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300148 switch (vctrl->id) {
149 /* Standard V4L2 controls */
150 case V4L2_CID_BRIGHTNESS:
151 case V4L2_CID_HUE:
152 case V4L2_CID_SATURATION:
153 case V4L2_CID_CONTRAST:
Andy Wallsfa3e7032009-02-16 02:23:25 -0300154 return v4l2_subdev_call(cx->sd_av, core, g_ctrl, vctrl);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300155
156 case V4L2_CID_AUDIO_VOLUME:
157 case V4L2_CID_AUDIO_MUTE:
158 case V4L2_CID_AUDIO_BALANCE:
159 case V4L2_CID_AUDIO_BASS:
160 case V4L2_CID_AUDIO_TREBLE:
161 case V4L2_CID_AUDIO_LOUDNESS:
Andy Wallsff2a2002009-02-20 23:52:13 -0300162 return v4l2_subdev_call(cx->sd_av, core, g_ctrl, vctrl);
163
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300164 default:
Hans Verkuil37f89f92008-06-22 11:57:31 -0300165 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300166 return -EINVAL;
167 }
168 return 0;
169}
170
171static int cx18_setup_vbi_fmt(struct cx18 *cx, enum v4l2_mpeg_stream_vbi_fmt fmt)
172{
173 if (!(cx->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
174 return -EINVAL;
Hans Verkuil31554ae2008-05-25 11:21:27 -0300175 if (atomic_read(&cx->ana_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300176 return -EBUSY;
177
178 /* First try to allocate sliced VBI buffers if needed. */
179 if (fmt && cx->vbi.sliced_mpeg_data[0] == NULL) {
180 int i;
181
182 for (i = 0; i < CX18_VBI_FRAMES; i++) {
Andy Walls302df972009-01-31 00:33:02 -0300183 cx->vbi.sliced_mpeg_data[i] =
184 kmalloc(CX18_SLICED_MPEG_DATA_BUFSZ, GFP_KERNEL);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300185 if (cx->vbi.sliced_mpeg_data[i] == NULL) {
186 while (--i >= 0) {
187 kfree(cx->vbi.sliced_mpeg_data[i]);
188 cx->vbi.sliced_mpeg_data[i] = NULL;
189 }
190 return -ENOMEM;
191 }
192 }
193 }
194
195 cx->vbi.insert_mpeg = fmt;
196
197 if (cx->vbi.insert_mpeg == 0)
198 return 0;
199 /* Need sliced data for mpeg insertion */
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300200 if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300201 if (cx->is_60hz)
202 cx->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
203 else
204 cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300205 cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300206 }
207 return 0;
208}
209
Andy Walls3b6fe582008-06-21 08:36:31 -0300210int cx18_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300211{
Andy Walls3b6fe582008-06-21 08:36:31 -0300212 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300213 struct v4l2_control ctrl;
214
Andy Walls3b6fe582008-06-21 08:36:31 -0300215 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
216 int i;
217 int err = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300218
Andy Walls3b6fe582008-06-21 08:36:31 -0300219 for (i = 0; i < c->count; i++) {
220 ctrl.id = c->controls[i].id;
221 ctrl.value = c->controls[i].value;
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300222 err = cx18_g_ctrl(cx, &ctrl);
Andy Walls3b6fe582008-06-21 08:36:31 -0300223 c->controls[i].value = ctrl.value;
224 if (err) {
225 c->error_idx = i;
226 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300227 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300228 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300229 return err;
230 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300231 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
232 return cx2341x_ext_ctrls(&cx->params, 0, c, VIDIOC_G_EXT_CTRLS);
233 return -EINVAL;
234}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300235
Andy Walls3b6fe582008-06-21 08:36:31 -0300236int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
237{
238 struct cx18_open_id *id = fh;
239 struct cx18 *cx = id->cx;
240 int ret;
241 struct v4l2_control ctrl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300242
Andy Walls3b6fe582008-06-21 08:36:31 -0300243 ret = v4l2_prio_check(&cx->prio, &id->prio);
244 if (ret)
245 return ret;
246
247 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
248 int i;
249 int err = 0;
250
251 for (i = 0; i < c->count; i++) {
252 ctrl.id = c->controls[i].id;
253 ctrl.value = c->controls[i].value;
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300254 err = cx18_s_ctrl(cx, &ctrl);
Andy Walls3b6fe582008-06-21 08:36:31 -0300255 c->controls[i].value = ctrl.value;
256 if (err) {
257 c->error_idx = i;
258 break;
259 }
260 }
261 return err;
262 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300263 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
Andy Wallsff2a2002009-02-20 23:52:13 -0300264 static u32 freqs[3] = { 44100, 48000, 32000 };
Andy Walls50b86ba2008-11-23 19:16:44 -0300265 struct cx18_api_func_private priv;
Andy Walls3b6fe582008-06-21 08:36:31 -0300266 struct cx2341x_mpeg_params p = cx->params;
267 int err = cx2341x_ext_ctrls(&p, atomic_read(&cx->ana_capturing),
268 c, VIDIOC_S_EXT_CTRLS);
Andy Wallsff2a2002009-02-20 23:52:13 -0300269 unsigned int idx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300270
271 if (err)
272 return err;
273
274 if (p.video_encoding != cx->params.video_encoding) {
275 int is_mpeg1 = p.video_encoding ==
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300276 V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
Andy Walls3b6fe582008-06-21 08:36:31 -0300277 struct v4l2_format fmt;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300278
Andy Walls3b6fe582008-06-21 08:36:31 -0300279 /* fix videodecoder resolution */
280 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
281 fmt.fmt.pix.width = cx->params.width
282 / (is_mpeg1 ? 2 : 1);
283 fmt.fmt.pix.height = cx->params.height;
Andy Wallsfa3e7032009-02-16 02:23:25 -0300284 v4l2_subdev_call(cx->sd_av, video, s_fmt, &fmt);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300285 }
Andy Walls50b86ba2008-11-23 19:16:44 -0300286 priv.cx = cx;
287 priv.s = &cx->streams[id->type];
288 err = cx2341x_update(&priv, cx18_api_func, &cx->params, &p);
Andy Walls3b6fe582008-06-21 08:36:31 -0300289 if (!err && cx->params.stream_vbi_fmt != p.stream_vbi_fmt)
290 err = cx18_setup_vbi_fmt(cx, p.stream_vbi_fmt);
291 cx->params = p;
292 cx->dualwatch_stereo_mode = p.audio_properties & 0x0300;
Andy Wallsff2a2002009-02-20 23:52:13 -0300293 idx = p.audio_properties & 0x03;
294 /* The audio clock of the digitizer must match the codec sample
295 rate otherwise you get some very strange effects. */
296 if (idx < sizeof(freqs))
297 cx18_call_all(cx, audio, s_clock_freq, freqs[idx]);
Andy Walls3b6fe582008-06-21 08:36:31 -0300298 return err;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300299 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300300 return -EINVAL;
301}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300302
Andy Walls3b6fe582008-06-21 08:36:31 -0300303int cx18_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
304{
305 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300306
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300307 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
308 int i;
309 int err = 0;
310
311 for (i = 0; i < c->count; i++) {
312 err = cx18_try_ctrl(file, fh, &c->controls[i]);
313 if (err) {
314 c->error_idx = i;
315 break;
316 }
317 }
318 return err;
319 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300320 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
321 return cx2341x_ext_ctrls(&cx->params,
322 atomic_read(&cx->ana_capturing),
323 c, VIDIOC_TRY_EXT_CTRLS);
324 return -EINVAL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300325}