V4L/DVB (8082): cx18: convert to video_ioctl2()
[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-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
33 static 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
48 static const u32 *ctrl_classes[] = {
49         user_ctrls,
50         cx2341x_mpeg_ctrls,
51         NULL
52 };
53
54 int cx18_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
55 {
56         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
57         const char *name;
58
59         CX18_DEBUG_IOCTL("VIDIOC_QUERYCTRL(%08x)\n", qctrl->id);
60
61         qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
62         if (qctrl->id == 0)
63                 return -EINVAL;
64
65         switch (qctrl->id) {
66         /* Standard V4L2 controls */
67         case V4L2_CID_BRIGHTNESS:
68         case V4L2_CID_HUE:
69         case V4L2_CID_SATURATION:
70         case V4L2_CID_CONTRAST:
71                 if (cx18_av_cmd(cx, VIDIOC_QUERYCTRL, qctrl))
72                         qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
73                 return 0;
74
75         case V4L2_CID_AUDIO_VOLUME:
76         case V4L2_CID_AUDIO_MUTE:
77         case V4L2_CID_AUDIO_BALANCE:
78         case V4L2_CID_AUDIO_BASS:
79         case V4L2_CID_AUDIO_TREBLE:
80         case V4L2_CID_AUDIO_LOUDNESS:
81                 if (cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_QUERYCTRL, qctrl))
82                         qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
83                 return 0;
84
85         default:
86                 if (cx2341x_ctrl_query(&cx->params, qctrl))
87                         qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
88                 return 0;
89         }
90         strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
91         qctrl->name[sizeof(qctrl->name) - 1] = 0;
92         return 0;
93 }
94
95 int cx18_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
96 {
97         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
98         struct v4l2_queryctrl qctrl;
99
100         CX18_DEBUG_IOCTL("VIDIOC_QUERYMENU\n");
101         qctrl.id = qmenu->id;
102         cx18_queryctrl(file, fh, &qctrl);
103         return v4l2_ctrl_query_menu(qmenu, &qctrl, cx2341x_ctrl_get_menu(qmenu->id));
104 }
105
106 int cx18_s_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
107 {
108         struct cx18_open_id *id = fh;
109         struct cx18 *cx = id->cx;
110         int ret;
111         s32 v = vctrl->value;
112
113         ret = v4l2_prio_check(&cx->prio, &id->prio);
114         if (ret)
115                 return ret;
116
117         CX18_DEBUG_IOCTL("VIDIOC_S_CTRL(%08x, %x)\n", vctrl->id, v);
118
119         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 cx18_av_cmd(cx, 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 cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_S_CTRL, vctrl);
134
135         default:
136                 CX18_DEBUG_IOCTL("invalid control %x\n", vctrl->id);
137                 return -EINVAL;
138         }
139         return 0;
140 }
141
142 int cx18_g_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
143 {
144         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
145
146         CX18_DEBUG_IOCTL("VIDIOC_G_CTRL(%08x)\n", vctrl->id);
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 cx18_av_cmd(cx, VIDIOC_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 cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_G_CTRL, vctrl);
163         default:
164                 CX18_DEBUG_IOCTL("invalid control %x\n", vctrl->id);
165                 return -EINVAL;
166         }
167         return 0;
168 }
169
170 static int cx18_setup_vbi_fmt(struct cx18 *cx, enum v4l2_mpeg_stream_vbi_fmt fmt)
171 {
172         if (!(cx->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
173                 return -EINVAL;
174         if (atomic_read(&cx->ana_capturing) > 0)
175                 return -EBUSY;
176
177         /* First try to allocate sliced VBI buffers if needed. */
178         if (fmt && cx->vbi.sliced_mpeg_data[0] == NULL) {
179                 int i;
180
181                 for (i = 0; i < CX18_VBI_FRAMES; i++) {
182                         /* Yuck, hardcoded. Needs to be a define */
183                         cx->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
184                         if (cx->vbi.sliced_mpeg_data[i] == NULL) {
185                                 while (--i >= 0) {
186                                         kfree(cx->vbi.sliced_mpeg_data[i]);
187                                         cx->vbi.sliced_mpeg_data[i] = NULL;
188                                 }
189                                 return -ENOMEM;
190                         }
191                 }
192         }
193
194         cx->vbi.insert_mpeg = fmt;
195
196         if (cx->vbi.insert_mpeg == 0)
197                 return 0;
198         /* Need sliced data for mpeg insertion */
199         if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
200                 if (cx->is_60hz)
201                         cx->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
202                 else
203                         cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
204                 cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
205         }
206         return 0;
207 }
208
209 int cx18_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
210 {
211         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
212         struct v4l2_control ctrl;
213
214         if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
215                 int i;
216                 int err = 0;
217
218                 for (i = 0; i < c->count; i++) {
219                         ctrl.id = c->controls[i].id;
220                         ctrl.value = c->controls[i].value;
221                         err = cx18_g_ctrl(file, fh, &ctrl);
222                         c->controls[i].value = ctrl.value;
223                         if (err) {
224                                 c->error_idx = i;
225                                 break;
226                         }
227                 }
228                 return err;
229         }
230         CX18_DEBUG_IOCTL("VIDIOC_G_EXT_CTRLS\n");
231         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 }
235
236 int 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;
242
243         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;
254                         err = cx18_s_ctrl(file, fh, &ctrl);
255                         c->controls[i].value = ctrl.value;
256                         if (err) {
257                                 c->error_idx = i;
258                                 break;
259                         }
260                 }
261                 return err;
262         }
263         CX18_DEBUG_IOCTL("VIDIOC_S_EXT_CTRLS\n");
264         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
265                 struct cx2341x_mpeg_params p = cx->params;
266                 int err = cx2341x_ext_ctrls(&p, atomic_read(&cx->ana_capturing),
267                                                 c, VIDIOC_S_EXT_CTRLS);
268
269                 if (err)
270                         return err;
271
272                 if (p.video_encoding != cx->params.video_encoding) {
273                         int is_mpeg1 = p.video_encoding ==
274                                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
275                         struct v4l2_format fmt;
276
277                         /* fix videodecoder resolution */
278                         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
279                         fmt.fmt.pix.width = cx->params.width
280                                                 / (is_mpeg1 ? 2 : 1);
281                         fmt.fmt.pix.height = cx->params.height;
282                         cx18_av_cmd(cx, VIDIOC_S_FMT, &fmt);
283                 }
284                 err = cx2341x_update(cx, cx18_api_func, &cx->params, &p);
285                 if (!err && cx->params.stream_vbi_fmt != p.stream_vbi_fmt)
286                         err = cx18_setup_vbi_fmt(cx, p.stream_vbi_fmt);
287                 cx->params = p;
288                 cx->dualwatch_stereo_mode = p.audio_properties & 0x0300;
289                 cx18_audio_set_audio_clock_freq(cx, p.audio_properties & 0x03);
290                 return err;
291         }
292         return -EINVAL;
293 }
294
295 int cx18_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
296 {
297         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
298
299         CX18_DEBUG_IOCTL("VIDIOC_TRY_EXT_CTRLS\n");
300         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
301                 return cx2341x_ext_ctrls(&cx->params,
302                                                 atomic_read(&cx->ana_capturing),
303                                                 c, VIDIOC_TRY_EXT_CTRLS);
304         return -EINVAL;
305 }