blob: 06723bac99c59b2cf6972955d7cecc20dc96a33e [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
56 IVTV_DEBUG_IOCTL("VIDIOC_QUERYCTRL(%08x)\n", qctrl->id);
57
58 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
59 if (qctrl->id == 0)
60 return -EINVAL;
61
62 switch (qctrl->id) {
63 /* Standard V4L2 controls */
64 case V4L2_CID_BRIGHTNESS:
65 case V4L2_CID_HUE:
66 case V4L2_CID_SATURATION:
67 case V4L2_CID_CONTRAST:
68 if (itv->video_dec_func(itv, VIDIOC_QUERYCTRL, qctrl))
69 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
70 return 0;
71
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 (ivtv_i2c_hw(itv, itv->card->hw_audio_ctrl, VIDIOC_QUERYCTRL, qctrl))
79 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
80 return 0;
81
82 default:
83 if (cx2341x_ctrl_query(&itv->params, qctrl))
84 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
85 return 0;
86 }
87 strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
88 qctrl->name[sizeof(qctrl->name) - 1] = 0;
89 return 0;
90}
91
Hans Verkuil3f038d82008-05-29 16:43:54 -030092int ivtv_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030093{
Hans Verkuil3f038d82008-05-29 16:43:54 -030094 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030095 struct v4l2_queryctrl qctrl;
96
Hans Verkuil3f038d82008-05-29 16:43:54 -030097 IVTV_DEBUG_IOCTL("VIDIOC_QUERYMENU\n");
Hans Verkuil1a0adaf2007-04-27 12:31:25 -030098 qctrl.id = qmenu->id;
Hans Verkuil3f038d82008-05-29 16:43:54 -030099 ivtv_queryctrl(file, fh, &qctrl);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300100 return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
101}
102
Hans Verkuil3f038d82008-05-29 16:43:54 -0300103int ivtv_s_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300104{
Hans Verkuil3f038d82008-05-29 16:43:54 -0300105 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300106 s32 v = vctrl->value;
107
108 IVTV_DEBUG_IOCTL("VIDIOC_S_CTRL(%08x, %x)\n", vctrl->id, v);
109
110 switch (vctrl->id) {
111 /* Standard V4L2 controls */
112 case V4L2_CID_BRIGHTNESS:
113 case V4L2_CID_HUE:
114 case V4L2_CID_SATURATION:
115 case V4L2_CID_CONTRAST:
116 return itv->video_dec_func(itv, VIDIOC_S_CTRL, vctrl);
117
118 case V4L2_CID_AUDIO_VOLUME:
119 case V4L2_CID_AUDIO_MUTE:
120 case V4L2_CID_AUDIO_BALANCE:
121 case V4L2_CID_AUDIO_BASS:
122 case V4L2_CID_AUDIO_TREBLE:
123 case V4L2_CID_AUDIO_LOUDNESS:
124 return ivtv_i2c_hw(itv, itv->card->hw_audio_ctrl, VIDIOC_S_CTRL, vctrl);
125
126 default:
127 IVTV_DEBUG_IOCTL("invalid control %x\n", vctrl->id);
128 return -EINVAL;
129 }
130 return 0;
131}
132
Hans Verkuil3f038d82008-05-29 16:43:54 -0300133int ivtv_g_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300134{
Hans Verkuil3f038d82008-05-29 16:43:54 -0300135 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
136
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300137 IVTV_DEBUG_IOCTL("VIDIOC_G_CTRL(%08x)\n", vctrl->id);
138
139 switch (vctrl->id) {
140 /* Standard V4L2 controls */
141 case V4L2_CID_BRIGHTNESS:
142 case V4L2_CID_HUE:
143 case V4L2_CID_SATURATION:
144 case V4L2_CID_CONTRAST:
145 return itv->video_dec_func(itv, VIDIOC_G_CTRL, vctrl);
146
147 case V4L2_CID_AUDIO_VOLUME:
148 case V4L2_CID_AUDIO_MUTE:
149 case V4L2_CID_AUDIO_BALANCE:
150 case V4L2_CID_AUDIO_BASS:
151 case V4L2_CID_AUDIO_TREBLE:
152 case V4L2_CID_AUDIO_LOUDNESS:
153 return ivtv_i2c_hw(itv, itv->card->hw_audio_ctrl, VIDIOC_G_CTRL, vctrl);
154 default:
155 IVTV_DEBUG_IOCTL("invalid control %x\n", vctrl->id);
156 return -EINVAL;
157 }
158 return 0;
159}
160
161static int ivtv_setup_vbi_fmt(struct ivtv *itv, enum v4l2_mpeg_stream_vbi_fmt fmt)
162{
163 if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
164 return -EINVAL;
165 if (atomic_read(&itv->capturing) > 0)
166 return -EBUSY;
167
168 /* First try to allocate sliced VBI buffers if needed. */
169 if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
170 int i;
171
172 for (i = 0; i < IVTV_VBI_FRAMES; i++) {
173 /* Yuck, hardcoded. Needs to be a define */
174 itv->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
175 if (itv->vbi.sliced_mpeg_data[i] == NULL) {
176 while (--i >= 0) {
177 kfree(itv->vbi.sliced_mpeg_data[i]);
178 itv->vbi.sliced_mpeg_data[i] = NULL;
179 }
180 return -ENOMEM;
181 }
182 }
183 }
184
185 itv->vbi.insert_mpeg = fmt;
186
187 if (itv->vbi.insert_mpeg == 0) {
188 return 0;
189 }
190 /* Need sliced data for mpeg insertion */
Hans Verkuilfeb5bce2008-05-01 09:22:13 -0300191 if (ivtv_get_service_set(itv->vbi.sliced_in) == 0) {
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300192 if (itv->is_60hz)
193 itv->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
194 else
195 itv->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
Hans Verkuilfeb5bce2008-05-01 09:22:13 -0300196 ivtv_expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300197 }
198 return 0;
199}
200
Hans Verkuil3f038d82008-05-29 16:43:54 -0300201int ivtv_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300202{
Hans Verkuil3f038d82008-05-29 16:43:54 -0300203 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300204 struct v4l2_control ctrl;
205
Hans Verkuil3f038d82008-05-29 16:43:54 -0300206 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
207 int i;
208 int err = 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300209
Hans Verkuil3f038d82008-05-29 16:43:54 -0300210 for (i = 0; i < c->count; i++) {
211 ctrl.id = c->controls[i].id;
212 ctrl.value = c->controls[i].value;
213 err = ivtv_g_ctrl(file, fh, &ctrl);
214 c->controls[i].value = ctrl.value;
215 if (err) {
216 c->error_idx = i;
217 break;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300218 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300219 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300220 return err;
221 }
222 IVTV_DEBUG_IOCTL("VIDIOC_G_EXT_CTRLS\n");
223 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
224 return cx2341x_ext_ctrls(&itv->params, 0, c, VIDIOC_G_EXT_CTRLS);
225 return -EINVAL;
226}
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300227
Hans Verkuil3f038d82008-05-29 16:43:54 -0300228int ivtv_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
229{
230 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
231 struct v4l2_control ctrl;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300232
Hans Verkuil3f038d82008-05-29 16:43:54 -0300233 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
234 int i;
235 int err = 0;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300236
Hans Verkuil3f038d82008-05-29 16:43:54 -0300237 for (i = 0; i < c->count; i++) {
238 ctrl.id = c->controls[i].id;
239 ctrl.value = c->controls[i].value;
240 err = ivtv_s_ctrl(file, fh, &ctrl);
241 c->controls[i].value = ctrl.value;
242 if (err) {
243 c->error_idx = i;
244 break;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300245 }
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300246 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300247 return err;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300248 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300249 IVTV_DEBUG_IOCTL("VIDIOC_S_EXT_CTRLS\n");
250 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
251 static u32 freqs[3] = { 44100, 48000, 32000 };
252 struct cx2341x_mpeg_params p = itv->params;
253 int err = cx2341x_ext_ctrls(&p, atomic_read(&itv->capturing), c, VIDIOC_S_EXT_CTRLS);
254 unsigned idx;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300255
Hans Verkuil3f038d82008-05-29 16:43:54 -0300256 if (err)
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300257 return err;
Hans Verkuil3f038d82008-05-29 16:43:54 -0300258
259 if (p.video_encoding != itv->params.video_encoding) {
260 int is_mpeg1 = p.video_encoding ==
261 V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
262 struct v4l2_format fmt;
263
264 /* fix videodecoder resolution */
265 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
266 fmt.fmt.pix.width = itv->params.width / (is_mpeg1 ? 2 : 1);
267 fmt.fmt.pix.height = itv->params.height;
268 itv->video_dec_func(itv, VIDIOC_S_FMT, &fmt);
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300269 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300270 err = cx2341x_update(itv, ivtv_api_func, &itv->params, &p);
271 if (!err && itv->params.stream_vbi_fmt != p.stream_vbi_fmt)
272 err = ivtv_setup_vbi_fmt(itv, p.stream_vbi_fmt);
273 itv->params = p;
274 itv->dualwatch_stereo_mode = p.audio_properties & 0x0300;
275 idx = p.audio_properties & 0x03;
276 /* The audio clock of the digitizer must match the codec sample
277 rate otherwise you get some very strange effects. */
278 if (idx < sizeof(freqs))
279 ivtv_call_i2c_clients(itv, VIDIOC_INT_AUDIO_CLOCK_FREQ, &freqs[idx]);
280 return err;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300281 }
Hans Verkuil3f038d82008-05-29 16:43:54 -0300282 return -EINVAL;
283}
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300284
Hans Verkuil3f038d82008-05-29 16:43:54 -0300285int ivtv_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
286{
287 struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300288
Hans Verkuil3f038d82008-05-29 16:43:54 -0300289 IVTV_DEBUG_IOCTL("VIDIOC_TRY_EXT_CTRLS\n");
290 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
291 return cx2341x_ext_ctrls(&itv->params, atomic_read(&itv->capturing), c, VIDIOC_TRY_EXT_CTRLS);
292 return -EINVAL;
Hans Verkuil1a0adaf2007-04-27 12:31:25 -0300293}