blob: 01ba9ca37d09d7b3e3e82824865fcb457011015c [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
33static 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 */
65 case V4L2_CID_BRIGHTNESS:
66 case V4L2_CID_HUE:
67 case V4L2_CID_SATURATION:
68 case V4L2_CID_CONTRAST:
69 if (cx18_av_cmd(cx, VIDIOC_QUERYCTRL, qctrl))
70 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
71 return 0;
72
73 case V4L2_CID_AUDIO_VOLUME:
74 case V4L2_CID_AUDIO_MUTE:
75 case V4L2_CID_AUDIO_BALANCE:
76 case V4L2_CID_AUDIO_BASS:
77 case V4L2_CID_AUDIO_TREBLE:
78 case V4L2_CID_AUDIO_LOUDNESS:
79 if (cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_QUERYCTRL, qctrl))
80 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
81 return 0;
82
83 default:
84 if (cx2341x_ctrl_query(&cx->params, qctrl))
85 qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
86 return 0;
87 }
88 strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
89 qctrl->name[sizeof(qctrl->name) - 1] = 0;
90 return 0;
91}
92
Andy Walls3b6fe582008-06-21 08:36:31 -030093int cx18_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030094{
95 struct v4l2_queryctrl qctrl;
96
97 qctrl.id = qmenu->id;
Andy Walls3b6fe582008-06-21 08:36:31 -030098 cx18_queryctrl(file, fh, &qctrl);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030099 return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
100}
101
Andy Walls3b6fe582008-06-21 08:36:31 -0300102int cx18_s_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300103{
Andy Walls3b6fe582008-06-21 08:36:31 -0300104 struct cx18_open_id *id = fh;
105 struct cx18 *cx = id->cx;
106 int ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300107
Andy Walls3b6fe582008-06-21 08:36:31 -0300108 ret = v4l2_prio_check(&cx->prio, &id->prio);
109 if (ret)
110 return ret;
111
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300112 switch (vctrl->id) {
113 /* Standard V4L2 controls */
114 case V4L2_CID_BRIGHTNESS:
115 case V4L2_CID_HUE:
116 case V4L2_CID_SATURATION:
117 case V4L2_CID_CONTRAST:
118 return cx18_av_cmd(cx, VIDIOC_S_CTRL, vctrl);
119
120 case V4L2_CID_AUDIO_VOLUME:
121 case V4L2_CID_AUDIO_MUTE:
122 case V4L2_CID_AUDIO_BALANCE:
123 case V4L2_CID_AUDIO_BASS:
124 case V4L2_CID_AUDIO_TREBLE:
125 case V4L2_CID_AUDIO_LOUDNESS:
126 return cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_S_CTRL, vctrl);
127
128 default:
Hans Verkuil37f89f92008-06-22 11:57:31 -0300129 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300130 return -EINVAL;
131 }
132 return 0;
133}
134
Andy Walls3b6fe582008-06-21 08:36:31 -0300135int cx18_g_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300136{
Andy Walls3b6fe582008-06-21 08:36:31 -0300137 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
138
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300139 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 cx18_av_cmd(cx, 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 cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_G_CTRL, vctrl);
154 default:
Hans Verkuil37f89f92008-06-22 11:57:31 -0300155 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300156 return -EINVAL;
157 }
158 return 0;
159}
160
161static int cx18_setup_vbi_fmt(struct cx18 *cx, enum v4l2_mpeg_stream_vbi_fmt fmt)
162{
163 if (!(cx->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
164 return -EINVAL;
Hans Verkuil31554ae2008-05-25 11:21:27 -0300165 if (atomic_read(&cx->ana_capturing) > 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300166 return -EBUSY;
167
168 /* First try to allocate sliced VBI buffers if needed. */
169 if (fmt && cx->vbi.sliced_mpeg_data[0] == NULL) {
170 int i;
171
172 for (i = 0; i < CX18_VBI_FRAMES; i++) {
173 /* Yuck, hardcoded. Needs to be a define */
174 cx->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
175 if (cx->vbi.sliced_mpeg_data[i] == NULL) {
176 while (--i >= 0) {
177 kfree(cx->vbi.sliced_mpeg_data[i]);
178 cx->vbi.sliced_mpeg_data[i] = NULL;
179 }
180 return -ENOMEM;
181 }
182 }
183 }
184
185 cx->vbi.insert_mpeg = fmt;
186
187 if (cx->vbi.insert_mpeg == 0)
188 return 0;
189 /* Need sliced data for mpeg insertion */
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300190 if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300191 if (cx->is_60hz)
192 cx->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
193 else
194 cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300195 cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300196 }
197 return 0;
198}
199
Andy Walls3b6fe582008-06-21 08:36:31 -0300200int cx18_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300201{
Andy Walls3b6fe582008-06-21 08:36:31 -0300202 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300203 struct v4l2_control ctrl;
204
Andy Walls3b6fe582008-06-21 08:36:31 -0300205 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
206 int i;
207 int err = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300208
Andy Walls3b6fe582008-06-21 08:36:31 -0300209 for (i = 0; i < c->count; i++) {
210 ctrl.id = c->controls[i].id;
211 ctrl.value = c->controls[i].value;
212 err = cx18_g_ctrl(file, fh, &ctrl);
213 c->controls[i].value = ctrl.value;
214 if (err) {
215 c->error_idx = i;
216 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300217 }
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300218 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300219 return err;
220 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300221 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
222 return cx2341x_ext_ctrls(&cx->params, 0, c, VIDIOC_G_EXT_CTRLS);
223 return -EINVAL;
224}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300225
Andy Walls3b6fe582008-06-21 08:36:31 -0300226int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
227{
228 struct cx18_open_id *id = fh;
229 struct cx18 *cx = id->cx;
230 int ret;
231 struct v4l2_control ctrl;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300232
Andy Walls3b6fe582008-06-21 08:36:31 -0300233 ret = v4l2_prio_check(&cx->prio, &id->prio);
234 if (ret)
235 return ret;
236
237 if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
238 int i;
239 int err = 0;
240
241 for (i = 0; i < c->count; i++) {
242 ctrl.id = c->controls[i].id;
243 ctrl.value = c->controls[i].value;
244 err = cx18_s_ctrl(file, fh, &ctrl);
245 c->controls[i].value = ctrl.value;
246 if (err) {
247 c->error_idx = i;
248 break;
249 }
250 }
251 return err;
252 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300253 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
254 struct cx2341x_mpeg_params p = cx->params;
255 int err = cx2341x_ext_ctrls(&p, atomic_read(&cx->ana_capturing),
256 c, VIDIOC_S_EXT_CTRLS);
257
258 if (err)
259 return err;
260
261 if (p.video_encoding != cx->params.video_encoding) {
262 int is_mpeg1 = p.video_encoding ==
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300263 V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
Andy Walls3b6fe582008-06-21 08:36:31 -0300264 struct v4l2_format fmt;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300265
Andy Walls3b6fe582008-06-21 08:36:31 -0300266 /* fix videodecoder resolution */
267 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
268 fmt.fmt.pix.width = cx->params.width
269 / (is_mpeg1 ? 2 : 1);
270 fmt.fmt.pix.height = cx->params.height;
271 cx18_av_cmd(cx, VIDIOC_S_FMT, &fmt);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300272 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300273 err = cx2341x_update(cx, cx18_api_func, &cx->params, &p);
274 if (!err && cx->params.stream_vbi_fmt != p.stream_vbi_fmt)
275 err = cx18_setup_vbi_fmt(cx, p.stream_vbi_fmt);
276 cx->params = p;
277 cx->dualwatch_stereo_mode = p.audio_properties & 0x0300;
278 cx18_audio_set_audio_clock_freq(cx, p.audio_properties & 0x03);
279 return err;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300280 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300281 return -EINVAL;
282}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300283
Andy Walls3b6fe582008-06-21 08:36:31 -0300284int cx18_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
285{
286 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300287
Andy Walls3b6fe582008-06-21 08:36:31 -0300288 if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
289 return cx2341x_ext_ctrls(&cx->params,
290 atomic_read(&cx->ana_capturing),
291 c, VIDIOC_TRY_EXT_CTRLS);
292 return -EINVAL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300293}