5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
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
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 This source file is specifically designed to interface with the
26 saa711x support that is available in the v4l available starting
31 #include "pvrusb2-video-v4l.h"
32 #include "pvrusb2-i2c-cmd-v4l2.h"
35 #include "pvrusb2-hdw-internal.h"
36 #include "pvrusb2-debug.h"
37 #include <linux/videodev2.h>
38 #include <media/v4l2-common.h>
39 #include <media/saa7115.h>
40 #include <linux/errno.h>
41 #include <linux/slab.h>
43 struct pvr2_v4l_decoder {
44 struct pvr2_i2c_handler handler;
45 struct pvr2_decoder_ctrl ctrl;
46 struct pvr2_i2c_client *client;
48 unsigned long stale_mask;
52 static void set_input(struct pvr2_v4l_decoder *ctxt)
54 struct pvr2_hdw *hdw = ctxt->hdw;
55 struct v4l2_routing route;
57 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_input(%d)",hdw->input_val);
58 switch(hdw->input_val) {
59 case PVR2_CVAL_INPUT_TV:
60 route.input = SAA7115_COMPOSITE4;
62 case PVR2_CVAL_INPUT_COMPOSITE:
63 route.input = SAA7115_COMPOSITE5;
65 case PVR2_CVAL_INPUT_SVIDEO:
66 route.input = SAA7115_SVIDEO2;
68 case PVR2_CVAL_INPUT_RADIO:
69 // ????? No idea yet what to do here
74 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
78 static int check_input(struct pvr2_v4l_decoder *ctxt)
80 struct pvr2_hdw *hdw = ctxt->hdw;
81 return hdw->input_dirty != 0;
85 static void set_audio(struct pvr2_v4l_decoder *ctxt)
88 struct pvr2_hdw *hdw = ctxt->hdw;
90 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_audio %d",
92 switch (hdw->srate_val) {
94 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
97 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
100 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
104 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
108 static int check_audio(struct pvr2_v4l_decoder *ctxt)
110 struct pvr2_hdw *hdw = ctxt->hdw;
111 return hdw->srate_dirty != 0;
115 struct pvr2_v4l_decoder_ops {
116 void (*update)(struct pvr2_v4l_decoder *);
117 int (*check)(struct pvr2_v4l_decoder *);
121 static const struct pvr2_v4l_decoder_ops decoder_ops[] = {
122 { .update = set_input, .check = check_input},
123 { .update = set_audio, .check = check_audio},
127 static void decoder_detach(struct pvr2_v4l_decoder *ctxt)
129 ctxt->client->handler = 0;
130 ctxt->hdw->decoder_ctrl = 0;
135 static int decoder_check(struct pvr2_v4l_decoder *ctxt)
140 for (idx = 0; idx < sizeof(decoder_ops)/sizeof(decoder_ops[0]);
143 if (ctxt->stale_mask & msk) continue;
144 if (decoder_ops[idx].check(ctxt)) {
145 ctxt->stale_mask |= msk;
148 return ctxt->stale_mask != 0;
152 static void decoder_update(struct pvr2_v4l_decoder *ctxt)
157 for (idx = 0; idx < sizeof(decoder_ops)/sizeof(decoder_ops[0]);
160 if (!(ctxt->stale_mask & msk)) continue;
161 ctxt->stale_mask &= ~msk;
162 decoder_ops[idx].update(ctxt);
167 static int decoder_detect(struct pvr2_i2c_client *cp)
169 /* Attempt to query the decoder - let's see if it will answer */
170 struct v4l2_tuner vt;
173 memset(&vt,0,sizeof(vt));
174 ret = pvr2_i2c_client_cmd(cp,VIDIOC_G_TUNER,&vt);
175 return ret == 0; /* Return true if it answered */
179 static void decoder_enable(struct pvr2_v4l_decoder *ctxt,int fl)
181 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 decoder_enable(%d)",fl);
182 pvr2_v4l2_cmd_stream(ctxt->client,fl);
186 static int decoder_is_tuned(struct pvr2_v4l_decoder *ctxt)
188 struct v4l2_tuner vt;
191 memset(&vt,0,sizeof(vt));
192 ret = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_G_TUNER,&vt);
193 if (ret < 0) return -EINVAL;
194 return vt.signal ? 1 : 0;
198 static unsigned int decoder_describe(struct pvr2_v4l_decoder *ctxt,char *buf,unsigned int cnt)
200 return scnprintf(buf,cnt,"handler: pvrusb2-video-v4l");
204 const static struct pvr2_i2c_handler_functions hfuncs = {
205 .detach = (void (*)(void *))decoder_detach,
206 .check = (int (*)(void *))decoder_check,
207 .update = (void (*)(void *))decoder_update,
208 .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
212 int pvr2_i2c_decoder_v4l_setup(struct pvr2_hdw *hdw,
213 struct pvr2_i2c_client *cp)
215 struct pvr2_v4l_decoder *ctxt;
217 if (hdw->decoder_ctrl) return 0;
218 if (cp->handler) return 0;
219 if (!decoder_detect(cp)) return 0;
221 ctxt = kmalloc(sizeof(*ctxt),GFP_KERNEL);
223 memset(ctxt,0,sizeof(*ctxt));
225 ctxt->handler.func_data = ctxt;
226 ctxt->handler.func_table = &hfuncs;
227 ctxt->ctrl.ctxt = ctxt;
228 ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
229 ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
230 ctxt->ctrl.tuned = (int (*)(void *))decoder_is_tuned;
233 ctxt->stale_mask = (1 << (sizeof(decoder_ops)/
234 sizeof(decoder_ops[0]))) - 1;
235 hdw->decoder_ctrl = &ctxt->ctrl;
236 cp->handler = &ctxt->handler;
237 pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x saa711x V4L2 handler set up",
246 Stuff for Emacs to see, in order to encourage consistent editing style:
247 *** Local Variables: ***
249 *** fill-column: 70 ***
251 *** c-basic-offset: 8 ***