Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6] / drivers / media / video / pvrusb2 / pvrusb2-i2c-cmd-v4l2.c
1 /*
2  *
3  *
4  *  Copyright (C) 2005 Mike Isely <isely@pobox.com>
5  *  Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License
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
22 #include "pvrusb2-i2c-cmd-v4l2.h"
23 #include "pvrusb2-hdw-internal.h"
24 #include "pvrusb2-debug.h"
25 #include <linux/videodev2.h>
26 #include <media/v4l2-common.h>
27
28 static void set_standard(struct pvr2_hdw *hdw)
29 {
30         pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_standard");
31
32         if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
33                 pvr2_i2c_core_cmd(hdw,AUDC_SET_RADIO,NULL);
34         } else {
35                 v4l2_std_id vs;
36                 vs = hdw->std_mask_cur;
37                 pvr2_i2c_core_cmd(hdw,VIDIOC_S_STD,&vs);
38         }
39         hdw->tuner_signal_stale = !0;
40 }
41
42
43 static int check_standard(struct pvr2_hdw *hdw)
44 {
45         return (hdw->input_dirty != 0) || (hdw->std_dirty != 0);
46 }
47
48
49 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_standard = {
50         .check = check_standard,
51         .update = set_standard,
52         .name = "v4l2_standard",
53 };
54
55
56 static void set_bcsh(struct pvr2_hdw *hdw)
57 {
58         struct v4l2_control ctrl;
59         pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_bcsh"
60                    " b=%d c=%d s=%d h=%d",
61                    hdw->brightness_val,hdw->contrast_val,
62                    hdw->saturation_val,hdw->hue_val);
63         memset(&ctrl,0,sizeof(ctrl));
64         ctrl.id = V4L2_CID_BRIGHTNESS;
65         ctrl.value = hdw->brightness_val;
66         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
67         ctrl.id = V4L2_CID_CONTRAST;
68         ctrl.value = hdw->contrast_val;
69         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
70         ctrl.id = V4L2_CID_SATURATION;
71         ctrl.value = hdw->saturation_val;
72         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
73         ctrl.id = V4L2_CID_HUE;
74         ctrl.value = hdw->hue_val;
75         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
76 }
77
78
79 static int check_bcsh(struct pvr2_hdw *hdw)
80 {
81         return (hdw->brightness_dirty ||
82                 hdw->contrast_dirty ||
83                 hdw->saturation_dirty ||
84                 hdw->hue_dirty);
85 }
86
87
88 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_bcsh = {
89         .check = check_bcsh,
90         .update = set_bcsh,
91         .name = "v4l2_bcsh",
92 };
93
94
95 static void set_volume(struct pvr2_hdw *hdw)
96 {
97         struct v4l2_control ctrl;
98         pvr2_trace(PVR2_TRACE_CHIPS,
99                    "i2c v4l2 set_volume"
100                    "(vol=%d bal=%d bas=%d treb=%d mute=%d)",
101                    hdw->volume_val,
102                    hdw->balance_val,
103                    hdw->bass_val,
104                    hdw->treble_val,
105                    hdw->mute_val);
106         memset(&ctrl,0,sizeof(ctrl));
107         ctrl.id = V4L2_CID_AUDIO_MUTE;
108         ctrl.value = hdw->mute_val ? 1 : 0;
109         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
110         ctrl.id = V4L2_CID_AUDIO_VOLUME;
111         ctrl.value = hdw->volume_val;
112         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
113         ctrl.id = V4L2_CID_AUDIO_BALANCE;
114         ctrl.value = hdw->balance_val;
115         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
116         ctrl.id = V4L2_CID_AUDIO_BASS;
117         ctrl.value = hdw->bass_val;
118         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
119         ctrl.id = V4L2_CID_AUDIO_TREBLE;
120         ctrl.value = hdw->treble_val;
121         pvr2_i2c_core_cmd(hdw,VIDIOC_S_CTRL,&ctrl);
122 }
123
124
125 static int check_volume(struct pvr2_hdw *hdw)
126 {
127         return (hdw->volume_dirty ||
128                 hdw->balance_dirty ||
129                 hdw->bass_dirty ||
130                 hdw->treble_dirty ||
131                 hdw->mute_dirty);
132 }
133
134
135 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_volume = {
136         .check = check_volume,
137         .update = set_volume,
138         .name = "v4l2_volume",
139 };
140
141
142 static void set_audiomode(struct pvr2_hdw *hdw)
143 {
144         struct v4l2_tuner vt;
145         memset(&vt,0,sizeof(vt));
146         vt.audmode = hdw->audiomode_val;
147         pvr2_i2c_core_cmd(hdw,VIDIOC_S_TUNER,&vt);
148 }
149
150
151 static int check_audiomode(struct pvr2_hdw *hdw)
152 {
153         return (hdw->input_dirty ||
154                 hdw->audiomode_dirty);
155 }
156
157
158 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_audiomode = {
159         .check = check_audiomode,
160         .update = set_audiomode,
161         .name = "v4l2_audiomode",
162 };
163
164
165 static void set_frequency(struct pvr2_hdw *hdw)
166 {
167         unsigned long fv;
168         struct v4l2_frequency freq;
169         fv = pvr2_hdw_get_cur_freq(hdw);
170         pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_freq(%lu)",fv);
171         if (hdw->tuner_signal_stale) {
172                 pvr2_i2c_core_status_poll(hdw);
173         }
174         memset(&freq,0,sizeof(freq));
175         if (hdw->tuner_signal_info.capability & V4L2_TUNER_CAP_LOW) {
176                 // ((fv * 1000) / 62500)
177                 freq.frequency = (fv * 2) / 125;
178         } else {
179                 freq.frequency = fv / 62500;
180         }
181         /* tuner-core currently doesn't seem to care about this, but
182            let's set it anyway for completeness. */
183         if (hdw->input_val == PVR2_CVAL_INPUT_RADIO) {
184                 freq.type = V4L2_TUNER_RADIO;
185         } else {
186                 freq.type = V4L2_TUNER_ANALOG_TV;
187         }
188         freq.tuner = 0;
189         pvr2_i2c_core_cmd(hdw,VIDIOC_S_FREQUENCY,&freq);
190 }
191
192
193 static int check_frequency(struct pvr2_hdw *hdw)
194 {
195         return hdw->freqDirty != 0;
196 }
197
198
199 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_frequency = {
200         .check = check_frequency,
201         .update = set_frequency,
202         .name = "v4l2_freq",
203 };
204
205
206 static void set_size(struct pvr2_hdw *hdw)
207 {
208         struct v4l2_format fmt;
209
210         memset(&fmt,0,sizeof(fmt));
211
212         fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
213         fmt.fmt.pix.width = hdw->res_hor_val;
214         fmt.fmt.pix.height = hdw->res_ver_val;
215
216         pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_size(%dx%d)",
217                            fmt.fmt.pix.width,fmt.fmt.pix.height);
218
219         pvr2_i2c_core_cmd(hdw,VIDIOC_S_FMT,&fmt);
220 }
221
222
223 static int check_size(struct pvr2_hdw *hdw)
224 {
225         return (hdw->res_hor_dirty || hdw->res_ver_dirty);
226 }
227
228
229 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_size = {
230         .check = check_size,
231         .update = set_size,
232         .name = "v4l2_size",
233 };
234
235
236 static void do_log(struct pvr2_hdw *hdw)
237 {
238         pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 do_log()");
239         pvr2_i2c_core_cmd(hdw,VIDIOC_LOG_STATUS,NULL);
240
241 }
242
243
244 static int check_log(struct pvr2_hdw *hdw)
245 {
246         return hdw->log_requested != 0;
247 }
248
249
250 const struct pvr2_i2c_op pvr2_i2c_op_v4l2_log = {
251         .check = check_log,
252         .update = do_log,
253         .name = "v4l2_log",
254 };
255
256
257 void pvr2_v4l2_cmd_stream(struct pvr2_i2c_client *cp,int fl)
258 {
259         pvr2_i2c_client_cmd(cp,
260                             (fl ? VIDIOC_STREAMON : VIDIOC_STREAMOFF),NULL);
261 }
262
263
264 void pvr2_v4l2_cmd_status_poll(struct pvr2_i2c_client *cp)
265 {
266         pvr2_i2c_client_cmd(cp,VIDIOC_G_TUNER,&cp->hdw->tuner_signal_info);
267 }
268
269
270 /*
271   Stuff for Emacs to see, in order to encourage consistent editing style:
272   *** Local Variables: ***
273   *** mode: c ***
274   *** fill-column: 70 ***
275   *** tab-width: 8 ***
276   *** c-basic-offset: 8 ***
277   *** End: ***
278   */