V4L/DVB (12104): ivtv/cx18: fix regression: class controls are no longer seen
[linux-2.6] / drivers / media / video / cx18 / cx18-controls.c
1 /*
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-cards.h"
26 #include "cx18-ioctl.h"
27 #include "cx18-audio.h"
28 #include "cx18-mailbox.h"
29 #include "cx18-controls.h"
30
31 /* Must be sorted from low to high control ID! */
32 static const u32 user_ctrls[] = {
33         V4L2_CID_USER_CLASS,
34         V4L2_CID_BRIGHTNESS,
35         V4L2_CID_CONTRAST,
36         V4L2_CID_SATURATION,
37         V4L2_CID_HUE,
38         V4L2_CID_AUDIO_VOLUME,
39         V4L2_CID_AUDIO_BALANCE,
40         V4L2_CID_AUDIO_BASS,
41         V4L2_CID_AUDIO_TREBLE,
42         V4L2_CID_AUDIO_MUTE,
43         V4L2_CID_AUDIO_LOUDNESS,
44         0
45 };
46
47 static const u32 *ctrl_classes[] = {
48         user_ctrls,
49         cx2341x_mpeg_ctrls,
50         NULL
51 };
52
53 int cx18_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
54 {
55         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
56         const char *name;
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_USER_CLASS:
65                 return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
66         case V4L2_CID_BRIGHTNESS:
67         case V4L2_CID_HUE:
68         case V4L2_CID_SATURATION:
69         case V4L2_CID_CONTRAST:
70                 if (v4l2_subdev_call(cx->sd_av, core, queryctrl, qctrl))
71                         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:
80                 if (v4l2_subdev_call(cx->sd_av, core, queryctrl, qctrl))
81                         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
94 int cx18_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
95 {
96         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
97         struct v4l2_queryctrl qctrl;
98
99         qctrl.id = qmenu->id;
100         cx18_queryctrl(file, fh, &qctrl);
101         return v4l2_ctrl_query_menu(qmenu, &qctrl,
102                         cx2341x_ctrl_get_menu(&cx->params, qmenu->id));
103 }
104
105 static int cx18_try_ctrl(struct file *file, void *fh,
106                                         struct v4l2_ext_control *vctrl)
107 {
108         struct v4l2_queryctrl qctrl;
109         const char **menu_items = NULL;
110         int err;
111
112         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 }
120
121 static int cx18_s_ctrl(struct cx18 *cx, struct v4l2_control *vctrl)
122 {
123         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:
129                 return v4l2_subdev_call(cx->sd_av, core, s_ctrl, vctrl);
130
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:
137                 return v4l2_subdev_call(cx->sd_av, core, s_ctrl, vctrl);
138
139         default:
140                 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
141                 return -EINVAL;
142         }
143         return 0;
144 }
145
146 static int cx18_g_ctrl(struct cx18 *cx, struct v4l2_control *vctrl)
147 {
148         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:
154                 return v4l2_subdev_call(cx->sd_av, core, g_ctrl, vctrl);
155
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:
162                 return v4l2_subdev_call(cx->sd_av, core, g_ctrl, vctrl);
163
164         default:
165                 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
166                 return -EINVAL;
167         }
168         return 0;
169 }
170
171 static int cx18_setup_vbi_fmt(struct cx18 *cx,
172                               enum v4l2_mpeg_stream_vbi_fmt fmt,
173                               enum v4l2_mpeg_stream_type type)
174 {
175         if (!(cx->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
176                 return -EINVAL;
177         if (atomic_read(&cx->ana_capturing) > 0)
178                 return -EBUSY;
179
180         if (fmt != V4L2_MPEG_STREAM_VBI_FMT_IVTV ||
181             !(type == V4L2_MPEG_STREAM_TYPE_MPEG2_PS ||
182               type == V4L2_MPEG_STREAM_TYPE_MPEG2_DVD ||
183               type == V4L2_MPEG_STREAM_TYPE_MPEG2_SVCD)) {
184                 /* Only IVTV fmt VBI insertion & only MPEG-2 PS type streams */
185                 cx->vbi.insert_mpeg = V4L2_MPEG_STREAM_VBI_FMT_NONE;
186                 CX18_DEBUG_INFO("disabled insertion of sliced VBI data into "
187                                 "the MPEG stream\n");
188                 return 0;
189         }
190
191         /* Allocate sliced VBI buffers if needed. */
192         if (cx->vbi.sliced_mpeg_data[0] == NULL) {
193                 int i;
194
195                 for (i = 0; i < CX18_VBI_FRAMES; i++) {
196                         cx->vbi.sliced_mpeg_data[i] =
197                                kmalloc(CX18_SLICED_MPEG_DATA_BUFSZ, GFP_KERNEL);
198                         if (cx->vbi.sliced_mpeg_data[i] == NULL) {
199                                 while (--i >= 0) {
200                                         kfree(cx->vbi.sliced_mpeg_data[i]);
201                                         cx->vbi.sliced_mpeg_data[i] = NULL;
202                                 }
203                                 cx->vbi.insert_mpeg =
204                                                   V4L2_MPEG_STREAM_VBI_FMT_NONE;
205                                 CX18_WARN("Unable to allocate buffers for "
206                                           "sliced VBI data insertion\n");
207                                 return -ENOMEM;
208                         }
209                 }
210         }
211
212         cx->vbi.insert_mpeg = fmt;
213         CX18_DEBUG_INFO("enabled insertion of sliced VBI data into the MPEG PS,"
214                         "when sliced VBI is enabled\n");
215
216         /*
217          * If our current settings have no lines set for capture, store a valid,
218          * default set of service lines to capture, in our current settings.
219          */
220         if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
221                 if (cx->is_60hz)
222                         cx->vbi.sliced_in->service_set =
223                                                         V4L2_SLICED_CAPTION_525;
224                 else
225                         cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
226                 cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
227         }
228         return 0;
229 }
230
231 int cx18_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
232 {
233         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
234         struct v4l2_control ctrl;
235
236         if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
237                 int i;
238                 int err = 0;
239
240                 for (i = 0; i < c->count; i++) {
241                         ctrl.id = c->controls[i].id;
242                         ctrl.value = c->controls[i].value;
243                         err = cx18_g_ctrl(cx, &ctrl);
244                         c->controls[i].value = ctrl.value;
245                         if (err) {
246                                 c->error_idx = i;
247                                 break;
248                         }
249                 }
250                 return err;
251         }
252         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
253                 return cx2341x_ext_ctrls(&cx->params, 0, c, VIDIOC_G_EXT_CTRLS);
254         return -EINVAL;
255 }
256
257 int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
258 {
259         struct cx18_open_id *id = fh;
260         struct cx18 *cx = id->cx;
261         int ret;
262         struct v4l2_control ctrl;
263
264         ret = v4l2_prio_check(&cx->prio, &id->prio);
265         if (ret)
266                 return ret;
267
268         if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
269                 int i;
270                 int err = 0;
271
272                 for (i = 0; i < c->count; i++) {
273                         ctrl.id = c->controls[i].id;
274                         ctrl.value = c->controls[i].value;
275                         err = cx18_s_ctrl(cx, &ctrl);
276                         c->controls[i].value = ctrl.value;
277                         if (err) {
278                                 c->error_idx = i;
279                                 break;
280                         }
281                 }
282                 return err;
283         }
284         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
285                 static u32 freqs[3] = { 44100, 48000, 32000 };
286                 struct cx18_api_func_private priv;
287                 struct cx2341x_mpeg_params p = cx->params;
288                 int err = cx2341x_ext_ctrls(&p, atomic_read(&cx->ana_capturing),
289                                                 c, VIDIOC_S_EXT_CTRLS);
290                 unsigned int idx;
291
292                 if (err)
293                         return err;
294
295                 if (p.video_encoding != cx->params.video_encoding) {
296                         int is_mpeg1 = p.video_encoding ==
297                                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
298                         struct v4l2_format fmt;
299
300                         /* fix videodecoder resolution */
301                         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
302                         fmt.fmt.pix.width = cx->params.width
303                                                 / (is_mpeg1 ? 2 : 1);
304                         fmt.fmt.pix.height = cx->params.height;
305                         v4l2_subdev_call(cx->sd_av, video, s_fmt, &fmt);
306                 }
307                 priv.cx = cx;
308                 priv.s = &cx->streams[id->type];
309                 err = cx2341x_update(&priv, cx18_api_func, &cx->params, &p);
310                 if (!err &&
311                     (cx->params.stream_vbi_fmt != p.stream_vbi_fmt ||
312                      cx->params.stream_type != p.stream_type))
313                         err = cx18_setup_vbi_fmt(cx, p.stream_vbi_fmt,
314                                                  p.stream_type);
315                 cx->params = p;
316                 cx->dualwatch_stereo_mode = p.audio_properties & 0x0300;
317                 idx = p.audio_properties & 0x03;
318                 /* The audio clock of the digitizer must match the codec sample
319                    rate otherwise you get some very strange effects. */
320                 if (idx < sizeof(freqs))
321                         cx18_call_all(cx, audio, s_clock_freq, freqs[idx]);
322                 return err;
323         }
324         return -EINVAL;
325 }
326
327 int cx18_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
328 {
329         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
330
331         if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
332                 int i;
333                 int err = 0;
334
335                 for (i = 0; i < c->count; i++) {
336                         err = cx18_try_ctrl(file, fh, &c->controls[i]);
337                         if (err) {
338                                 c->error_idx = i;
339                                 break;
340                         }
341                 }
342                 return err;
343         }
344         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
345                 return cx2341x_ext_ctrls(&cx->params,
346                                                 atomic_read(&cx->ana_capturing),
347                                                 c, VIDIOC_TRY_EXT_CTRLS);
348         return -EINVAL;
349 }