genirq: Wake up irq thread after action has been installed
[pandora-kernel.git] / 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_USER_CLASS:
64                 return v4l2_ctrl_query_fill(qctrl, 0, 0, 0, 0);
65         case V4L2_CID_BRIGHTNESS:
66         case V4L2_CID_HUE:
67         case V4L2_CID_SATURATION:
68         case V4L2_CID_CONTRAST:
69                 if (v4l2_subdev_call(itv->sd_video, core, 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 (v4l2_subdev_call(itv->sd_audio, core, queryctrl, qctrl))
80                         qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
81                 return 0;
82
83         default:
84                 if (cx2341x_ctrl_query(&itv->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 ivtv_querymenu(struct file *file, void *fh, struct v4l2_querymenu *qmenu)
94 {
95         struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
96         struct v4l2_queryctrl qctrl;
97
98         qctrl.id = qmenu->id;
99         ivtv_queryctrl(file, fh, &qctrl);
100         return v4l2_ctrl_query_menu(qmenu, &qctrl,
101                         cx2341x_ctrl_get_menu(&itv->params, qmenu->id));
102 }
103
104 static int ivtv_try_ctrl(struct file *file, void *fh,
105                                         struct v4l2_ext_control *vctrl)
106 {
107         struct v4l2_queryctrl qctrl;
108         const char **menu_items = NULL;
109         int err;
110
111         qctrl.id = vctrl->id;
112         err = ivtv_queryctrl(file, fh, &qctrl);
113         if (err)
114                 return err;
115         if (qctrl.type == V4L2_CTRL_TYPE_MENU)
116                 menu_items = v4l2_ctrl_get_menu(qctrl.id);
117         return v4l2_ctrl_check(vctrl, &qctrl, menu_items);
118 }
119
120 static int ivtv_s_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
121 {
122         switch (vctrl->id) {
123                 /* Standard V4L2 controls */
124         case V4L2_CID_BRIGHTNESS:
125         case V4L2_CID_HUE:
126         case V4L2_CID_SATURATION:
127         case V4L2_CID_CONTRAST:
128                 return v4l2_subdev_call(itv->sd_video, core, s_ctrl, vctrl);
129
130         case V4L2_CID_AUDIO_VOLUME:
131         case V4L2_CID_AUDIO_MUTE:
132         case V4L2_CID_AUDIO_BALANCE:
133         case V4L2_CID_AUDIO_BASS:
134         case V4L2_CID_AUDIO_TREBLE:
135         case V4L2_CID_AUDIO_LOUDNESS:
136                 return v4l2_subdev_call(itv->sd_audio, core, s_ctrl, vctrl);
137
138         default:
139                 IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
140                 return -EINVAL;
141         }
142         return 0;
143 }
144
145 static int ivtv_g_ctrl(struct ivtv *itv, struct v4l2_control *vctrl)
146 {
147         switch (vctrl->id) {
148                 /* Standard V4L2 controls */
149         case V4L2_CID_BRIGHTNESS:
150         case V4L2_CID_HUE:
151         case V4L2_CID_SATURATION:
152         case V4L2_CID_CONTRAST:
153                 return v4l2_subdev_call(itv->sd_video, core, g_ctrl, vctrl);
154
155         case V4L2_CID_AUDIO_VOLUME:
156         case V4L2_CID_AUDIO_MUTE:
157         case V4L2_CID_AUDIO_BALANCE:
158         case V4L2_CID_AUDIO_BASS:
159         case V4L2_CID_AUDIO_TREBLE:
160         case V4L2_CID_AUDIO_LOUDNESS:
161                 return v4l2_subdev_call(itv->sd_audio, core, g_ctrl, vctrl);
162         default:
163                 IVTV_DEBUG_IOCTL("invalid control 0x%x\n", vctrl->id);
164                 return -EINVAL;
165         }
166         return 0;
167 }
168
169 static int ivtv_setup_vbi_fmt(struct ivtv *itv, enum v4l2_mpeg_stream_vbi_fmt fmt)
170 {
171         if (!(itv->v4l2_cap & V4L2_CAP_SLICED_VBI_CAPTURE))
172                 return -EINVAL;
173         if (atomic_read(&itv->capturing) > 0)
174                 return -EBUSY;
175
176         /* First try to allocate sliced VBI buffers if needed. */
177         if (fmt && itv->vbi.sliced_mpeg_data[0] == NULL) {
178                 int i;
179
180                 for (i = 0; i < IVTV_VBI_FRAMES; i++) {
181                         /* Yuck, hardcoded. Needs to be a define */
182                         itv->vbi.sliced_mpeg_data[i] = kmalloc(2049, GFP_KERNEL);
183                         if (itv->vbi.sliced_mpeg_data[i] == NULL) {
184                                 while (--i >= 0) {
185                                         kfree(itv->vbi.sliced_mpeg_data[i]);
186                                         itv->vbi.sliced_mpeg_data[i] = NULL;
187                                 }
188                                 return -ENOMEM;
189                         }
190                 }
191         }
192
193         itv->vbi.insert_mpeg = fmt;
194
195         if (itv->vbi.insert_mpeg == 0) {
196                 return 0;
197         }
198         /* Need sliced data for mpeg insertion */
199         if (ivtv_get_service_set(itv->vbi.sliced_in) == 0) {
200                 if (itv->is_60hz)
201                         itv->vbi.sliced_in->service_set = V4L2_SLICED_CAPTION_525;
202                 else
203                         itv->vbi.sliced_in->service_set = V4L2_SLICED_WSS_625;
204                 ivtv_expand_service_set(itv->vbi.sliced_in, itv->is_50hz);
205         }
206         return 0;
207 }
208
209 int ivtv_g_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
210 {
211         struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
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 = ivtv_g_ctrl(itv, &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         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
231                 return cx2341x_ext_ctrls(&itv->params, 0, c, VIDIOC_G_EXT_CTRLS);
232         return -EINVAL;
233 }
234
235 int ivtv_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
236 {
237         struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
238         struct v4l2_control ctrl;
239
240         if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
241                 int i;
242                 int err = 0;
243
244                 for (i = 0; i < c->count; i++) {
245                         ctrl.id = c->controls[i].id;
246                         ctrl.value = c->controls[i].value;
247                         err = ivtv_s_ctrl(itv, &ctrl);
248                         c->controls[i].value = ctrl.value;
249                         if (err) {
250                                 c->error_idx = i;
251                                 break;
252                         }
253                 }
254                 return err;
255         }
256         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
257                 static u32 freqs[3] = { 44100, 48000, 32000 };
258                 struct cx2341x_mpeg_params p = itv->params;
259                 int err = cx2341x_ext_ctrls(&p, atomic_read(&itv->capturing), c, VIDIOC_S_EXT_CTRLS);
260                 unsigned idx;
261
262                 if (err)
263                         return err;
264
265                 if (p.video_encoding != itv->params.video_encoding) {
266                         int is_mpeg1 = p.video_encoding ==
267                                 V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
268                         struct v4l2_format fmt;
269
270                         /* fix videodecoder resolution */
271                         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
272                         fmt.fmt.pix.width = itv->params.width / (is_mpeg1 ? 2 : 1);
273                         fmt.fmt.pix.height = itv->params.height;
274                         v4l2_subdev_call(itv->sd_video, video, s_fmt, &fmt);
275                 }
276                 err = cx2341x_update(itv, ivtv_api_func, &itv->params, &p);
277                 if (!err && itv->params.stream_vbi_fmt != p.stream_vbi_fmt)
278                         err = ivtv_setup_vbi_fmt(itv, p.stream_vbi_fmt);
279                 itv->params = p;
280                 itv->dualwatch_stereo_mode = p.audio_properties & 0x0300;
281                 idx = p.audio_properties & 0x03;
282                 /* The audio clock of the digitizer must match the codec sample
283                    rate otherwise you get some very strange effects. */
284                 if (idx < sizeof(freqs))
285                         ivtv_call_all(itv, audio, s_clock_freq, freqs[idx]);
286                 return err;
287         }
288         return -EINVAL;
289 }
290
291 int ivtv_try_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
292 {
293         struct ivtv *itv = ((struct ivtv_open_id *)fh)->itv;
294
295         if (c->ctrl_class == V4L2_CTRL_CLASS_USER) {
296                 int i;
297                 int err = 0;
298
299                 for (i = 0; i < c->count; i++) {
300                         err = ivtv_try_ctrl(file, fh, &c->controls[i]);
301                         if (err) {
302                                 c->error_idx = i;
303                                 break;
304                         }
305                 }
306                 return err;
307         }
308         if (c->ctrl_class == V4L2_CTRL_CLASS_MPEG)
309                 return cx2341x_ext_ctrls(&itv->params, atomic_read(&itv->capturing), c, VIDIOC_TRY_EXT_CTRLS);
310         return -EINVAL;
311 }