]> err.no Git - linux-2.6/blob - drivers/media/video/cx18/cx18-controls.c
855313359370b1da889ae0c4d20e06d21b961f22
[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         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
93 int cx18_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
94 {
95         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
96         struct v4l2_queryctrl qctrl;
97
98         qctrl.id = qmenu->id;
99         cx18_queryctrl(file, fh, &qctrl);
100         return v4l2_ctrl_query_menu(qmenu, &qctrl,
101                         cx2341x_ctrl_get_menu(&cx->params, qmenu->id));
102 }
103
104 int cx18_s_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
105 {
106         struct cx18_open_id *id = fh;
107         struct cx18 *cx = id->cx;
108         int ret;
109
110         ret = v4l2_prio_check(&cx->prio, &id->prio);
111         if (ret)
112                 return ret;
113
114         switch (vctrl->id) {
115                 /* Standard V4L2 controls */
116         case V4L2_CID_BRIGHTNESS:
117         case V4L2_CID_HUE:
118         case V4L2_CID_SATURATION:
119         case V4L2_CID_CONTRAST:
120                 return cx18_av_cmd(cx, VIDIOC_S_CTRL, vctrl);
121
122         case V4L2_CID_AUDIO_VOLUME:
123         case V4L2_CID_AUDIO_MUTE:
124         case V4L2_CID_AUDIO_BALANCE:
125         case V4L2_CID_AUDIO_BASS:
126         case V4L2_CID_AUDIO_TREBLE:
127         case V4L2_CID_AUDIO_LOUDNESS:
128                 return cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_S_CTRL, vctrl);
129
130         default:
131                 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
132                 return -EINVAL;
133         }
134         return 0;
135 }
136
137 int cx18_g_ctrl(struct file *file, void *fh, struct v4l2_control *vctrl)
138 {
139         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
140
141         switch (vctrl->id) {
142                 /* Standard V4L2 controls */
143         case V4L2_CID_BRIGHTNESS:
144         case V4L2_CID_HUE:
145         case V4L2_CID_SATURATION:
146         case V4L2_CID_CONTRAST:
147                 return cx18_av_cmd(cx, VIDIOC_G_CTRL, vctrl);
148
149         case V4L2_CID_AUDIO_VOLUME:
150         case V4L2_CID_AUDIO_MUTE:
151         case V4L2_CID_AUDIO_BALANCE:
152         case V4L2_CID_AUDIO_BASS:
153         case V4L2_CID_AUDIO_TREBLE:
154         case V4L2_CID_AUDIO_LOUDNESS:
155                 return cx18_i2c_hw(cx, cx->card->hw_audio_ctrl, VIDIOC_G_CTRL, vctrl);
156         default:
157                 CX18_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
158                 return -EINVAL;
159         }
160         return 0;
161 }
162
163 static int cx18_setup_vbi_fmt(struct cx18 *cx, enum v4l2_mpeg_stream_vbi_fmt fmt)
164 {
165         if (!(cx->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
166                 return -EINVAL;
167         if (atomic_read(&cx->ana_capturing) > 0)
168                 return -EBUSY;
169
170         /* First try to allocate sliced VBI buffers if needed. */
171         if (fmt && cx->vbi.sliced_mpeg_data[0] == NULL) {
172                 int i;
173
174                 for (i = 0; i < CX18_VBI_FRAMES; i++) {
175                         /* Yuck, hardcoded. Needs to be a define */
176                         cx->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
177                         if (cx->vbi.sliced_mpeg_data[i] == NULL) {
178                                 while (--i >= 0) {
179                                         kfree(cx->vbi.sliced_mpeg_data[i]);
180                                         cx->vbi.sliced_mpeg_data[i] = NULL;
181                                 }
182                                 return -ENOMEM;
183                         }
184                 }
185         }
186
187         cx->vbi.insert_mpeg = fmt;
188
189         if (cx->vbi.insert_mpeg == 0)
190                 return 0;
191         /* Need sliced data for mpeg insertion */
192         if (cx18_get_service_set(cx->vbi.sliced_in) == 0) {
193                 if (cx->is_60hz)
194                         cx->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
195                 else
196                         cx->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
197                 cx18_expand_service_set(cx->vbi.sliced_in, cx->is_50hz);
198         }
199         return 0;
200 }
201
202 int cx18_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
203 {
204         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
205         struct v4l2_control ctrl;
206
207         if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
208                 int i;
209                 int err = 0;
210
211                 for (i = 0; i < c->count; i++) {
212                         ctrl.id = c->controls[i].id;
213                         ctrl.value = c->controls[i].value;
214                         err = cx18_g_ctrl(file, fh, &ctrl);
215                         c->controls[i].value = ctrl.value;
216                         if (err) {
217                                 c->error_idx = i;
218                                 break;
219                         }
220                 }
221                 return err;
222         }
223         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
224                 return cx2341x_ext_ctrls(&cx->params, 0, c, VIDIOC_G_EXT_CTRLS);
225         return -EINVAL;
226 }
227
228 int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
229 {
230         struct cx18_open_id *id = fh;
231         struct cx18 *cx = id->cx;
232         int ret;
233         struct v4l2_control ctrl;
234
235         ret = v4l2_prio_check(&cx->prio, &id->prio);
236         if (ret)
237                 return ret;
238
239         if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
240                 int i;
241                 int err = 0;
242
243                 for (i = 0; i < c->count; i++) {
244                         ctrl.id = c->controls[i].id;
245                         ctrl.value = c->controls[i].value;
246                         err = cx18_s_ctrl(file, fh, &ctrl);
247                         c->controls[i].value = ctrl.value;
248                         if (err) {
249                                 c->error_idx = i;
250                                 break;
251                         }
252                 }
253                 return err;
254         }
255         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
256                 struct cx2341x_mpeg_params p = cx->params;
257                 int err = cx2341x_ext_ctrls(&p, atomic_read(&cx->ana_capturing),
258                                                 c, VIDIOC_S_EXT_CTRLS);
259
260                 if (err)
261                         return err;
262
263                 if (p.video_encoding != cx->params.video_encoding) {
264                         int is_mpeg1 = p.video_encoding ==
265                                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
266                         struct v4l2_format fmt;
267
268                         /* fix videodecoder resolution */
269                         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
270                         fmt.fmt.pix.width = cx->params.width
271                                                 / (is_mpeg1 ? 2 : 1);
272                         fmt.fmt.pix.height = cx->params.height;
273                         cx18_av_cmd(cx, VIDIOC_S_FMT, &fmt);
274                 }
275                 err = cx2341x_update(cx, cx18_api_func, &cx->params, &p);
276                 if (!err && cx->params.stream_vbi_fmt != p.stream_vbi_fmt)
277                         err = cx18_setup_vbi_fmt(cx, p.stream_vbi_fmt);
278                 cx->params = p;
279                 cx->dualwatch_stereo_mode = p.audio_properties & 0x0300;
280                 cx18_audio_set_audio_clock_freq(cx, p.audio_properties & 0x03);
281                 return err;
282         }
283         return -EINVAL;
284 }
285
286 int cx18_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
287 {
288         struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
289
290         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
291                 return cx2341x_ext_ctrls(&cx->params,
292                                                 atomic_read(&cx->ana_capturing),
293                                                 c, VIDIOC_TRY_EXT_CTRLS);
294         return -EINVAL;
295 }