Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6] / drivers / media / video / ivtv / ivtv-controls.c
1 /*
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"
24 #include "ivtv-routing.h"
25 #include "ivtv-i2c.h"
26 #include "ivtv-mailbox.h"
27 #include "ivtv-controls.h"
28
29 /* Must be sorted from low to high control ID! */
30 static const u32 user_ctrls[] = {
31         V4L2_CID_USER_CLASS,
32         V4L2_CID_BRIGHTNESS,
33         V4L2_CID_CONTRAST,
34         V4L2_CID_SATURATION,
35         V4L2_CID_HUE,
36         V4L2_CID_AUDIO_VOLUME,
37         V4L2_CID_AUDIO_BALANCE,
38         V4L2_CID_AUDIO_BASS,
39         V4L2_CID_AUDIO_TREBLE,
40         V4L2_CID_AUDIO_MUTE,
41         V4L2_CID_AUDIO_LOUDNESS,
42         0
43 };
44
45 static const u32 *ctrl_classes[] = {
46         user_ctrls,
47         cx2341x_mpeg_ctrls,
48         NULL
49 };
50
51
52 int ivtv_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *qctrl)
53 {
54         struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
55         const char *name;
56
57         qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
58         if (qctrl->id == 0)
59                 return -EINVAL;
60
61         switch (qctrl->id) {
62         /* Standard V4L2 controls */
63         case V4L2_CID_BRIGHTNESS:
64         case V4L2_CID_HUE:
65         case V4L2_CID_SATURATION:
66         case V4L2_CID_CONTRAST:
67                 if (v4l2_subdev_call(itv->sd_video, core, queryctrl, qctrl))
68                         qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
69                 return 0;
70
71         case V4L2_CID_AUDIO_VOLUME:
72         case V4L2_CID_AUDIO_MUTE:
73         case V4L2_CID_AUDIO_BALANCE:
74         case V4L2_CID_AUDIO_BASS:
75         case V4L2_CID_AUDIO_TREBLE:
76         case V4L2_CID_AUDIO_LOUDNESS:
77                 if (v4l2_subdev_call(itv->sd_audio, core, queryctrl, qctrl))
78                         qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
79                 return 0;
80
81         default:
82                 if (cx2341x_ctrl_query(&itv->params, qctrl))
83                         qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
84                 return 0;
85         }
86         strncpy(qctrl->name, name, sizeof(qctrl->name) - 1);
87         qctrl->name[sizeof(qctrl->name) - 1] = 0;
88         return 0;
89 }
90
91 int ivtv_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
92 {
93         struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
94         struct v4l2_queryctrl qctrl;
95
96         qctrl.id = qmenu->id;
97         ivtv_queryctrl(file, fh, &qctrl);
98         return v4l2_ctrl_query_menu(qmenu, &qctrl,
99                         cx2341x_ctrl_get_menu(&itv->params, qmenu->id));
100 }
101
102 static int ivtv_try_ctrl(struct file *file, void *fh,
103                                         struct v4l2_ext_control *vctrl)
104 {
105         struct v4l2_queryctrl qctrl;
106         const char **menu_items = NULL;
107         int err;
108
109         qctrl.id = vctrl->id;
110         err = ivtv_queryctrl(file, fh, &qctrl);
111         if (err)
112                 return err;
113         if (qctrl.type == V4L2_CTRL_TYPE_MENU)
114                 menu_items = v4l2_ctrl_get_menu(qctrl.id);
115         return v4l2_ctrl_check(vctrl, &qctrl, menu_items);
116 }
117
118 static int ivtv_s_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
119 {
120         switch (vctrl->id) {
121                 /* Standard V4L2 controls */
122         case V4L2_CID_BRIGHTNESS:
123         case V4L2_CID_HUE:
124         case V4L2_CID_SATURATION:
125         case V4L2_CID_CONTRAST:
126                 return v4l2_subdev_call(itv->sd_video, core, s_ctrl, vctrl);
127
128         case V4L2_CID_AUDIO_VOLUME:
129         case V4L2_CID_AUDIO_MUTE:
130         case V4L2_CID_AUDIO_BALANCE:
131         case V4L2_CID_AUDIO_BASS:
132         case V4L2_CID_AUDIO_TREBLE:
133         case V4L2_CID_AUDIO_LOUDNESS:
134                 return v4l2_subdev_call(itv->sd_audio, core, s_ctrl, vctrl);
135
136         default:
137                 IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
138                 return -EINVAL;
139         }
140         return 0;
141 }
142
143 static int ivtv_g_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
144 {
145         switch (vctrl->id) {
146                 /* Standard V4L2 controls */
147         case V4L2_CID_BRIGHTNESS:
148         case V4L2_CID_HUE:
149         case V4L2_CID_SATURATION:
150         case V4L2_CID_CONTRAST:
151                 return v4l2_subdev_call(itv->sd_video, core, g_ctrl, vctrl);
152
153         case V4L2_CID_AUDIO_VOLUME:
154         case V4L2_CID_AUDIO_MUTE:
155         case V4L2_CID_AUDIO_BALANCE:
156         case V4L2_CID_AUDIO_BASS:
157         case V4L2_CID_AUDIO_TREBLE:
158         case V4L2_CID_AUDIO_LOUDNESS:
159                 return v4l2_subdev_call(itv->sd_audio, core, g_ctrl, vctrl);
160         default:
161                 IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
162                 return -EINVAL;
163         }
164         return 0;
165 }
166
167 static int ivtv_setup_vbi_fmt(struct ivtv *itv, enum v4l2_mpeg_stream_vbi_fmt fmt)
168 {
169         if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
170                 return -EINVAL;
171         if (atomic_read(&itv->capturing) > 0)
172                 return -EBUSY;
173
174         /* First try to allocate sliced VBI buffers if needed. */
175         if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
176                 int i;
177
178                 for (i = 0; i < IVTV_VBI_FRAMES; i++) {
179                         /* Yuck, hardcoded. Needs to be a define */
180                         itv->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
181                         if (itv->vbi.sliced_mpeg_data[i] == NULL) {
182                                 while (--i >= 0) {
183                                         kfree(itv->vbi.sliced_mpeg_data[i]);
184                                         itv->vbi.sliced_mpeg_data[i] = NULL;
185                                 }
186                                 return -ENOMEM;
187                         }
188                 }
189         }
190
191         itv->vbi.insert_mpeg = fmt;
192
193         if (itv->vbi.insert_mpeg == 0) {
194                 return 0;
195         }
196         /* Need sliced data for mpeg insertion */
197         if (ivtv_get_service_set(itv->vbi.sliced_in) == 0) {
198                 if (itv->is_60hz)
199                         itv->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
200                 else
201                         itv->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
202                 ivtv_expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
203         }
204         return 0;
205 }
206
207 int ivtv_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
208 {
209         struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
210         struct v4l2_control ctrl;
211
212         if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
213                 int i;
214                 int err = 0;
215
216                 for (i = 0; i < c->count; i++) {
217                         ctrl.id = c->controls[i].id;
218                         ctrl.value = c->controls[i].value;
219                         err = ivtv_g_ctrl(itv, &ctrl);
220                         c->controls[i].value = ctrl.value;
221                         if (err) {
222                                 c->error_idx = i;
223                                 break;
224                         }
225                 }
226                 return err;
227         }
228         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
229                 return cx2341x_ext_ctrls(&itv->params, 0, c, VIDIOC_G_EXT_CTRLS);
230         return -EINVAL;
231 }
232
233 int ivtv_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
234 {
235         struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
236         struct v4l2_control ctrl;
237
238         if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
239                 int i;
240                 int err = 0;
241
242                 for (i = 0; i < c->count; i++) {
243                         ctrl.id = c->controls[i].id;
244                         ctrl.value = c->controls[i].value;
245                         err = ivtv_s_ctrl(itv, &ctrl);
246                         c->controls[i].value = ctrl.value;
247                         if (err) {
248                                 c->error_idx = i;
249                                 break;
250                         }
251                 }
252                 return err;
253         }
254         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
255                 static u32 freqs[3] = { 44100, 48000, 32000 };
256                 struct cx2341x_mpeg_params p = itv->params;
257                 int err = cx2341x_ext_ctrls(&p, atomic_read(&itv->capturing), c, VIDIOC_S_EXT_CTRLS);
258                 unsigned idx;
259
260                 if (err)
261                         return err;
262
263                 if (p.video_encoding != itv->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 = itv->params.width / (is_mpeg1 ? 2 : 1);
271                         fmt.fmt.pix.height = itv->params.height;
272                         v4l2_subdev_call(itv->sd_video, video, s_fmt, &fmt);
273                 }
274                 err = cx2341x_update(itv, ivtv_api_func, &itv->params, &p);
275                 if (!err && itv->params.stream_vbi_fmt != p.stream_vbi_fmt)
276                         err = ivtv_setup_vbi_fmt(itv, p.stream_vbi_fmt);
277                 itv->params = p;
278                 itv->dualwatch_stereo_mode = p.audio_properties & 0x0300;
279                 idx = p.audio_properties & 0x03;
280                 /* The audio clock of the digitizer must match the codec sample
281                    rate otherwise you get some very strange effects. */
282                 if (idx < sizeof(freqs))
283                         ivtv_call_all(itv, audio, s_clock_freq, freqs[idx]);
284                 return err;
285         }
286         return -EINVAL;
287 }
288
289 int ivtv_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
290 {
291         struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
292
293         if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
294                 int i;
295                 int err = 0;
296
297                 for (i = 0; i < c->count; i++) {
298                         err = ivtv_try_ctrl(file, fh, &c->controls[i]);
299                         if (err) {
300                                 c->error_idx = i;
301                                 break;
302                         }
303                 }
304                 return err;
305         }
306         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
307                 return cx2341x_ext_ctrls(&itv->params, atomic_read(&itv->capturing), c, VIDIOC_TRY_EXT_CTRLS);
308         return -EINVAL;
309 }