blob: 48e103be718340fe0db30394d5b2b989912c8697 [file] [log] [blame]
Hans Verkuil1a0adaf2007-04-27 12:31:25 -03001/*
2 ioctl control functions
3 Copyright (C) 2003-2004 Kevin Thayer <nufan_wfk at yahoo.com>
4 Copyright (C) 2005-2007 Hans Verkuil <hverkuil@xs4all.nl>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "ivtv-driver.h"
22#include "ivtv-cards.h"
23#include "ivtv-ioctl.h"
Hans Verkuil33c0fca2007-08-23 06:32:46 -030024#include "ivtv-routing.h"
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030025#include "ivtv-i2c.h"
26#include "ivtv-mailbox.h"
27#include "ivtv-controls.h"
28
29static const u32 user_ctrls[] = {
30 V4L2_CID_USER_CLASS,
31 V4L2_CID_BRIGHTNESS,
32 V4L2_CID_CONTRAST,
33 V4L2_CID_SATURATION,
34 V4L2_CID_HUE,
35 V4L2_CID_AUDIO_VOLUME,
36 V4L2_CID_AUDIO_BALANCE,
37 V4L2_CID_AUDIO_BASS,
38 V4L2_CID_AUDIO_TREBLE,
39 V4L2_CID_AUDIO_MUTE,
40 V4L2_CID_AUDIO_LOUDNESS,
41 0
42};
43
44static const u32 *ctrl_classes[] = {
45 user_ctrls,
46 cx2341x_mpeg_ctrls,
47 NULL
48};
49
Hans Verkuil3f038d82008-05-29 16:43:54 -030050
51int ivtv_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030052{
Hans Verkuil3f038d82008-05-29 16:43:54 -030053 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030054 const char *name;
55
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030056 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
57 if (qctrl->id == 0)
58 return -EINVAL;
59
60 switch (qctrl->id) {
61 /* Standard V4L2 controls */
62 case V4L2_CID_BRIGHTNESS:
63 case V4L2_CID_HUE:
64 case V4L2_CID_SATURATION:
65 case V4L2_CID_CONTRAST:
66 if (itv->video_dec_func(itv, VIDIOC_QUERYCTRL, qctrl))
67 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
68 return 0;
69
70 case V4L2_CID_AUDIO_VOLUME:
71 case V4L2_CID_AUDIO_MUTE:
72 case V4L2_CID_AUDIO_BALANCE:
73 case V4L2_CID_AUDIO_BASS:
74 case V4L2_CID_AUDIO_TREBLE:
75 case V4L2_CID_AUDIO_LOUDNESS:
76 if (ivtv_i2c_hw(itv, itv->card->hw_audio_ctrl, VIDIOC_QUERYCTRL, qctrl))
77 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
78 return 0;
79
80 default:
81 if (cx2341x_ctrl_query(&itv->params, qctrl))
82 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
83 return 0;
84 }
85 strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
86 qctrl->name[sizeof(qctrl->name) - 1] = 0;
87 return 0;
88}
89
Hans Verkuil3f038d82008-05-29 16:43:54 -030090int ivtv_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030091{
Hans Verkuile0e31cd2008-06-22 12:03:28 -030092 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030093 struct v4l2_queryctrl qctrl;
94
95 qctrl.id = qmenu->id;
Hans Verkuil3f038d82008-05-29 16:43:54 -030096 ivtv_queryctrl(file, fh, &qctrl);
Hans Verkuile0e31cd2008-06-22 12:03:28 -030097 return v4l2_ctrl_query_menu(qmenu, &qctrl,
98 cx2341x_ctrl_get_menu(&itv->params, qmenu->id));
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030099}
100
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300101static int ivtv_try_ctrl(struct file *file, void *fh,
102 struct v4l2_ext_control *vctrl)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300103{
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300104 struct v4l2_queryctrl qctrl;
105 const char **menu_items = NULL;
106 int err;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300107
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300108 qctrl.id = vctrl->id;
109 err = ivtv_queryctrl(file, fh, &qctrl);
110 if (err)
111 return err;
112 if (qctrl.type == V4L2_CTRL_TYPE_MENU)
113 menu_items = v4l2_ctrl_get_menu(qctrl.id);
114 return v4l2_ctrl_check(vctrl, &qctrl, menu_items);
115}
116
117static int ivtv_s_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
118{
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300119 switch (vctrl->id) {
120 /* Standard V4L2 controls */
121 case V4L2_CID_BRIGHTNESS:
122 case V4L2_CID_HUE:
123 case V4L2_CID_SATURATION:
124 case V4L2_CID_CONTRAST:
125 return itv->video_dec_func(itv, VIDIOC_S_CTRL, vctrl);
126
127 case V4L2_CID_AUDIO_VOLUME:
128 case V4L2_CID_AUDIO_MUTE:
129 case V4L2_CID_AUDIO_BALANCE:
130 case V4L2_CID_AUDIO_BASS:
131 case V4L2_CID_AUDIO_TREBLE:
132 case V4L2_CID_AUDIO_LOUDNESS:
133 return ivtv_i2c_hw(itv, itv->card->hw_audio_ctrl, VIDIOC_S_CTRL, vctrl);
134
135 default:
Hans Verkuil37f89f92008-06-22 11:57:31 -0300136 IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300137 return -EINVAL;
138 }
139 return 0;
140}
141
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300142static int ivtv_g_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300143{
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300144 switch (vctrl->id) {
145 /* Standard V4L2 controls */
146 case V4L2_CID_BRIGHTNESS:
147 case V4L2_CID_HUE:
148 case V4L2_CID_SATURATION:
149 case V4L2_CID_CONTRAST:
150 return itv->video_dec_func(itv, VIDIOC_G_CTRL, vctrl);
151
152 case V4L2_CID_AUDIO_VOLUME:
153 case V4L2_CID_AUDIO_MUTE:
154 case V4L2_CID_AUDIO_BALANCE:
155 case V4L2_CID_AUDIO_BASS:
156 case V4L2_CID_AUDIO_TREBLE:
157 case V4L2_CID_AUDIO_LOUDNESS:
158 return ivtv_i2c_hw(itv, itv->card->hw_audio_ctrl, VIDIOC_G_CTRL, vctrl);
159 default:
Hans Verkuil37f89f92008-06-22 11:57:31 -0300160 IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300161 return -EINVAL;
162 }
163 return 0;
164}
165
166static int ivtv_setup_vbi_fmt(struct ivtv *itv, enum v4l2_mpeg_stream_vbi_fmt fmt)
167{
168 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
169 return -EINVAL;
170 if (atomic_read(&itv->capturing) > 0)
171 return -EBUSY;
172
173 /* First try to allocate sliced VBI buffers if needed. */
174 if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
175 int i;
176
177 for (i = 0; i < IVTV_VBI_FRAMES; i++) {
178 /* Yuck, hardcoded. Needs to be a define */
179 itv->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
180 if (itv->vbi.sliced_mpeg_data[i] == NULL) {
181 while (--i >= 0) {
182 kfree(itv->vbi.sliced_mpeg_data[i]);
183 itv->vbi.sliced_mpeg_data[i] = NULL;
184 }
185 return -ENOMEM;
186 }
187 }
188 }
189
190 itv->vbi.insert_mpeg = fmt;
191
192 if (itv->vbi.insert_mpeg == 0) {
193 return 0;
194 }
195 /* Need sliced data for mpeg insertion */
Hans Verkuilfeb5bce2008-05-01 09:22:13 -0300196 if (ivtv_get_service_set(itv->vbi.sliced_in) == 0) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300197 if (itv->is_60hz)
198 itv->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
199 else
200 itv->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
Hans Verkuilfeb5bce2008-05-01 09:22:13 -0300201 ivtv_expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300202 }
203 return 0;
204}
205
Hans Verkuil3f038d82008-05-29 16:43:54 -0300206int ivtv_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300207{
Hans Verkuil3f038d82008-05-29 16:43:54 -0300208 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300209 struct v4l2_control ctrl;
210
Hans Verkuil3f038d82008-05-29 16:43:54 -0300211 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
212 int i;
213 int err = 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300214
Hans Verkuil3f038d82008-05-29 16:43:54 -0300215 for (i = 0; i < c->count; i++) {
216 ctrl.id = c->controls[i].id;
217 ctrl.value = c->controls[i].value;
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300218 err = ivtv_g_ctrl(itv, &ctrl);
Hans Verkuil3f038d82008-05-29 16:43:54 -0300219 c->controls[i].value = ctrl.value;
220 if (err) {
221 c->error_idx = i;
222 break;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300223 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300224 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300225 return err;
226 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300227 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
228 return cx2341x_ext_ctrls(&itv->params, 0, c, VIDIOC_G_EXT_CTRLS);
229 return -EINVAL;
230}
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300231
Hans Verkuil3f038d82008-05-29 16:43:54 -0300232int ivtv_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
233{
234 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
235 struct v4l2_control ctrl;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300236
Hans Verkuil3f038d82008-05-29 16:43:54 -0300237 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
238 int i;
239 int err = 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300240
Hans Verkuil3f038d82008-05-29 16:43:54 -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 = ivtv_s_ctrl(itv, &ctrl);
Hans Verkuil3f038d82008-05-29 16:43:54 -0300245 c->controls[i].value = ctrl.value;
246 if (err) {
247 c->error_idx = i;
248 break;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300249 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300250 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300251 return err;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300252 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300253 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
254 static u32 freqs[3] = { 44100, 48000, 32000 };
255 struct cx2341x_mpeg_params p = itv->params;
256 int err = cx2341x_ext_ctrls(&p, atomic_read(&itv->capturing), c, VIDIOC_S_EXT_CTRLS);
257 unsigned idx;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300258
Hans Verkuil3f038d82008-05-29 16:43:54 -0300259 if (err)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300260 return err;
Hans Verkuil3f038d82008-05-29 16:43:54 -0300261
262 if (p.video_encoding != itv->params.video_encoding) {
263 int is_mpeg1 = p.video_encoding ==
264 V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
265 struct v4l2_format fmt;
266
267 /* fix videodecoder resolution */
268 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
269 fmt.fmt.pix.width = itv->params.width / (is_mpeg1 ? 2 : 1);
270 fmt.fmt.pix.height = itv->params.height;
271 itv->video_dec_func(itv, VIDIOC_S_FMT, &fmt);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300272 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300273 err = cx2341x_update(itv, ivtv_api_func, &itv->params, &p);
274 if (!err && itv->params.stream_vbi_fmt != p.stream_vbi_fmt)
275 err = ivtv_setup_vbi_fmt(itv, p.stream_vbi_fmt);
276 itv->params = p;
277 itv->dualwatch_stereo_mode = p.audio_properties & 0x0300;
278 idx = p.audio_properties & 0x03;
279 /* The audio clock of the digitizer must match the codec sample
280 rate otherwise you get some very strange effects. */
281 if (idx < sizeof(freqs))
282 ivtv_call_i2c_clients(itv, VIDIOC_INT_AUDIO_CLOCK_FREQ, &freqs[idx]);
283 return err;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300284 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300285 return -EINVAL;
286}
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300287
Hans Verkuil3f038d82008-05-29 16:43:54 -0300288int ivtv_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
289{
290 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300291
Hans Verkuiladb65bc2008-06-25 06:32:44 -0300292 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
293 int i;
294 int err = 0;
295
296 for (i = 0; i < c->count; i++) {
297 err = ivtv_try_ctrl(file, fh, &c->controls[i]);
298 if (err) {
299 c->error_idx = i;
300 break;
301 }
302 }
303 return err;
304 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300305 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
306 return cx2341x_ext_ctrls(&itv->params, atomic_read(&itv->capturing), c, VIDIOC_TRY_EXT_CTRLS);
307 return -EINVAL;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300308}