blob: 4059648c70563341844c90697a79f03782b37ec2 [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
2 *
Mike Iselyd8554972006-06-26 20:58:46 -03003 *
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/*
23
24 This source file is specifically designed to interface with the
25 saa711x support that is available in the v4l available starting
26 with linux 2.6.15.
27
28*/
29
30#include "pvrusb2-video-v4l.h"
31#include "pvrusb2-i2c-cmd-v4l2.h"
32
33
34#include "pvrusb2-hdw-internal.h"
35#include "pvrusb2-debug.h"
36#include <linux/videodev2.h>
37#include <media/v4l2-common.h>
38#include <media/saa7115.h>
39#include <linux/errno.h>
40#include <linux/slab.h>
41
42struct pvr2_v4l_decoder {
43 struct pvr2_i2c_handler handler;
44 struct pvr2_decoder_ctrl ctrl;
45 struct pvr2_i2c_client *client;
46 struct pvr2_hdw *hdw;
47 unsigned long stale_mask;
48};
49
50
Mike Iselyf5174af2007-11-26 02:07:26 -030051struct routing_scheme {
52 const int *def;
53 unsigned int cnt;
54};
55
56
57static const int routing_scheme0[] = {
58 [PVR2_CVAL_INPUT_TV] = SAA7115_COMPOSITE4,
59 /* In radio mode, we mute the video, but point at one
60 spot just to stay consistent */
61 [PVR2_CVAL_INPUT_RADIO] = SAA7115_COMPOSITE5,
62 [PVR2_CVAL_INPUT_COMPOSITE] = SAA7115_COMPOSITE5,
63 [PVR2_CVAL_INPUT_SVIDEO] = SAA7115_SVIDEO2,
64};
65
66static const struct routing_scheme routing_schemes[] = {
67 [PVR2_ROUTING_SCHEME_HAUPPAUGE] = {
68 .def = routing_scheme0,
69 .cnt = ARRAY_SIZE(routing_scheme0),
70 },
71};
72
Mike Iselyd8554972006-06-26 20:58:46 -030073static void set_input(struct pvr2_v4l_decoder *ctxt)
74{
75 struct pvr2_hdw *hdw = ctxt->hdw;
76 struct v4l2_routing route;
Mike Iselyf5174af2007-11-26 02:07:26 -030077 const struct routing_scheme *sp;
78 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
Mike Iselyd8554972006-06-26 20:58:46 -030079
80 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_input(%d)",hdw->input_val);
Mike Iselyf5174af2007-11-26 02:07:26 -030081
82 if ((sid < ARRAY_SIZE(routing_schemes)) &&
Harvey Harrisona6a3a172008-04-28 16:50:03 -070083 ((sp = routing_schemes + sid) != NULL) &&
Mike Iselyf5174af2007-11-26 02:07:26 -030084 (hdw->input_val >= 0) &&
85 (hdw->input_val < sp->cnt)) {
86 route.input = sp->def[hdw->input_val];
87 } else {
88 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
89 "*** WARNING *** i2c v4l2 set_input:"
90 " Invalid routing scheme (%u) and/or input (%d)",
91 sid,hdw->input_val);
Mike Iselyd8554972006-06-26 20:58:46 -030092 return;
93 }
Mike Iselyf5174af2007-11-26 02:07:26 -030094
Mike Iselyd8554972006-06-26 20:58:46 -030095 route.output = 0;
96 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
97}
98
99
100static int check_input(struct pvr2_v4l_decoder *ctxt)
101{
102 struct pvr2_hdw *hdw = ctxt->hdw;
103 return hdw->input_dirty != 0;
104}
105
106
107static void set_audio(struct pvr2_v4l_decoder *ctxt)
108{
109 u32 val;
110 struct pvr2_hdw *hdw = ctxt->hdw;
111
112 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_audio %d",
113 hdw->srate_val);
114 switch (hdw->srate_val) {
115 default:
Mike Iselyb30d2442006-06-25 20:05:01 -0300116 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
Mike Iselyd8554972006-06-26 20:58:46 -0300117 val = 48000;
118 break;
Mike Iselyb30d2442006-06-25 20:05:01 -0300119 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
Mike Iselyd8554972006-06-26 20:58:46 -0300120 val = 44100;
121 break;
Mike Iselyb30d2442006-06-25 20:05:01 -0300122 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
123 val = 32000;
124 break;
Mike Iselyd8554972006-06-26 20:58:46 -0300125 }
126 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
127}
128
129
130static int check_audio(struct pvr2_v4l_decoder *ctxt)
131{
132 struct pvr2_hdw *hdw = ctxt->hdw;
133 return hdw->srate_dirty != 0;
134}
135
136
137struct pvr2_v4l_decoder_ops {
138 void (*update)(struct pvr2_v4l_decoder *);
139 int (*check)(struct pvr2_v4l_decoder *);
140};
141
142
143static const struct pvr2_v4l_decoder_ops decoder_ops[] = {
144 { .update = set_input, .check = check_input},
145 { .update = set_audio, .check = check_audio},
146};
147
148
149static void decoder_detach(struct pvr2_v4l_decoder *ctxt)
150{
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300151 ctxt->client->handler = NULL;
Mike Isely681c7392007-11-26 01:48:52 -0300152 pvr2_hdw_set_decoder(ctxt->hdw,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300153 kfree(ctxt);
154}
155
156
157static int decoder_check(struct pvr2_v4l_decoder *ctxt)
158{
159 unsigned long msk;
160 unsigned int idx;
161
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300162 for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300163 msk = 1 << idx;
164 if (ctxt->stale_mask & msk) continue;
165 if (decoder_ops[idx].check(ctxt)) {
166 ctxt->stale_mask |= msk;
167 }
168 }
169 return ctxt->stale_mask != 0;
170}
171
172
173static void decoder_update(struct pvr2_v4l_decoder *ctxt)
174{
175 unsigned long msk;
176 unsigned int idx;
177
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300178 for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300179 msk = 1 << idx;
180 if (!(ctxt->stale_mask & msk)) continue;
181 ctxt->stale_mask &= ~msk;
182 decoder_ops[idx].update(ctxt);
183 }
184}
185
186
187static int decoder_detect(struct pvr2_i2c_client *cp)
188{
189 /* Attempt to query the decoder - let's see if it will answer */
190 struct v4l2_tuner vt;
191 int ret;
192
193 memset(&vt,0,sizeof(vt));
194 ret = pvr2_i2c_client_cmd(cp,VIDIOC_G_TUNER,&vt);
195 return ret == 0; /* Return true if it answered */
196}
197
198
199static void decoder_enable(struct pvr2_v4l_decoder *ctxt,int fl)
200{
201 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 decoder_enable(%d)",fl);
202 pvr2_v4l2_cmd_stream(ctxt->client,fl);
203}
204
205
Mike Iselyd8554972006-06-26 20:58:46 -0300206static unsigned int decoder_describe(struct pvr2_v4l_decoder *ctxt,char *buf,unsigned int cnt)
207{
208 return scnprintf(buf,cnt,"handler: pvrusb2-video-v4l");
209}
210
211
Tobias Klauserc5a69d52007-02-17 20:11:19 +0100212static const struct pvr2_i2c_handler_functions hfuncs = {
Mike Iselyd8554972006-06-26 20:58:46 -0300213 .detach = (void (*)(void *))decoder_detach,
214 .check = (int (*)(void *))decoder_check,
215 .update = (void (*)(void *))decoder_update,
216 .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
217};
218
219
220int pvr2_i2c_decoder_v4l_setup(struct pvr2_hdw *hdw,
221 struct pvr2_i2c_client *cp)
222{
223 struct pvr2_v4l_decoder *ctxt;
224
225 if (hdw->decoder_ctrl) return 0;
226 if (cp->handler) return 0;
227 if (!decoder_detect(cp)) return 0;
228
Mike Iselyca545f72007-01-20 00:37:11 -0300229 ctxt = kzalloc(sizeof(*ctxt),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300230 if (!ctxt) return 0;
Mike Iselyd8554972006-06-26 20:58:46 -0300231
232 ctxt->handler.func_data = ctxt;
233 ctxt->handler.func_table = &hfuncs;
234 ctxt->ctrl.ctxt = ctxt;
235 ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
236 ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
Mike Iselyd8554972006-06-26 20:58:46 -0300237 ctxt->client = cp;
238 ctxt->hdw = hdw;
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300239 ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1;
Mike Isely681c7392007-11-26 01:48:52 -0300240 pvr2_hdw_set_decoder(hdw,&ctxt->ctrl);
Mike Iselyd8554972006-06-26 20:58:46 -0300241 cp->handler = &ctxt->handler;
242 pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x saa711x V4L2 handler set up",
243 cp->client->addr);
244 return !0;
245}
246
247
248
249
250/*
251 Stuff for Emacs to see, in order to encourage consistent editing style:
252 *** Local Variables: ***
253 *** mode: c ***
254 *** fill-column: 70 ***
255 *** tab-width: 8 ***
256 *** c-basic-offset: 8 ***
257 *** End: ***
258 */