blob: 7c47345501b610ea988f349f935de29666415193 [file] [log] [blame]
Mike Iselyd8554972006-06-26 20:58:46 -03001/*
2 *
3 * $Id$
4 *
5 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
6 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7 *
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
11 *
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.
16 *
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
20 *
21 */
22
23/*
24
25 This source file is specifically designed to interface with the
26 saa711x support that is available in the v4l available starting
27 with linux 2.6.15.
28
29*/
30
31#include "pvrusb2-video-v4l.h"
32#include "pvrusb2-i2c-cmd-v4l2.h"
33
34
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>
42
43struct pvr2_v4l_decoder {
44 struct pvr2_i2c_handler handler;
45 struct pvr2_decoder_ctrl ctrl;
46 struct pvr2_i2c_client *client;
47 struct pvr2_hdw *hdw;
48 unsigned long stale_mask;
49};
50
51
Mike Iselyf5174af2007-11-26 02:07:26 -030052struct routing_scheme {
53 const int *def;
54 unsigned int cnt;
55};
56
57
58static const int routing_scheme0[] = {
59 [PVR2_CVAL_INPUT_TV] = SAA7115_COMPOSITE4,
60 /* In radio mode, we mute the video, but point at one
61 spot just to stay consistent */
62 [PVR2_CVAL_INPUT_RADIO] = SAA7115_COMPOSITE5,
63 [PVR2_CVAL_INPUT_COMPOSITE] = SAA7115_COMPOSITE5,
64 [PVR2_CVAL_INPUT_SVIDEO] = SAA7115_SVIDEO2,
65};
66
67static const struct routing_scheme routing_schemes[] = {
68 [PVR2_ROUTING_SCHEME_HAUPPAUGE] = {
69 .def = routing_scheme0,
70 .cnt = ARRAY_SIZE(routing_scheme0),
71 },
72};
73
Mike Iselyd8554972006-06-26 20:58:46 -030074static void set_input(struct pvr2_v4l_decoder *ctxt)
75{
76 struct pvr2_hdw *hdw = ctxt->hdw;
77 struct v4l2_routing route;
Mike Iselyf5174af2007-11-26 02:07:26 -030078 const struct routing_scheme *sp;
79 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
Mike Iselyd8554972006-06-26 20:58:46 -030080
81 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_input(%d)",hdw->input_val);
Mike Iselyf5174af2007-11-26 02:07:26 -030082
83 if ((sid < ARRAY_SIZE(routing_schemes)) &&
84 ((sp = routing_schemes + sid) != 0) &&
85 (hdw->input_val >= 0) &&
86 (hdw->input_val < sp->cnt)) {
87 route.input = sp->def[hdw->input_val];
88 } else {
89 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
90 "*** WARNING *** i2c v4l2 set_input:"
91 " Invalid routing scheme (%u) and/or input (%d)",
92 sid,hdw->input_val);
Mike Iselyd8554972006-06-26 20:58:46 -030093 return;
94 }
Mike Iselyf5174af2007-11-26 02:07:26 -030095
Mike Iselyd8554972006-06-26 20:58:46 -030096 route.output = 0;
97 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
98}
99
100
101static int check_input(struct pvr2_v4l_decoder *ctxt)
102{
103 struct pvr2_hdw *hdw = ctxt->hdw;
104 return hdw->input_dirty != 0;
105}
106
107
108static void set_audio(struct pvr2_v4l_decoder *ctxt)
109{
110 u32 val;
111 struct pvr2_hdw *hdw = ctxt->hdw;
112
113 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_audio %d",
114 hdw->srate_val);
115 switch (hdw->srate_val) {
116 default:
Mike Iselyb30d2442006-06-25 20:05:01 -0300117 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
Mike Iselyd8554972006-06-26 20:58:46 -0300118 val = 48000;
119 break;
Mike Iselyb30d2442006-06-25 20:05:01 -0300120 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
Mike Iselyd8554972006-06-26 20:58:46 -0300121 val = 44100;
122 break;
Mike Iselyb30d2442006-06-25 20:05:01 -0300123 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
124 val = 32000;
125 break;
Mike Iselyd8554972006-06-26 20:58:46 -0300126 }
127 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
128}
129
130
131static int check_audio(struct pvr2_v4l_decoder *ctxt)
132{
133 struct pvr2_hdw *hdw = ctxt->hdw;
134 return hdw->srate_dirty != 0;
135}
136
137
138struct pvr2_v4l_decoder_ops {
139 void (*update)(struct pvr2_v4l_decoder *);
140 int (*check)(struct pvr2_v4l_decoder *);
141};
142
143
144static const struct pvr2_v4l_decoder_ops decoder_ops[] = {
145 { .update = set_input, .check = check_input},
146 { .update = set_audio, .check = check_audio},
147};
148
149
150static void decoder_detach(struct pvr2_v4l_decoder *ctxt)
151{
Mike Iselya0fd1cb2006-06-30 11:35:28 -0300152 ctxt->client->handler = NULL;
Mike Isely681c7392007-11-26 01:48:52 -0300153 pvr2_hdw_set_decoder(ctxt->hdw,NULL);
Mike Iselyd8554972006-06-26 20:58:46 -0300154 kfree(ctxt);
155}
156
157
158static int decoder_check(struct pvr2_v4l_decoder *ctxt)
159{
160 unsigned long msk;
161 unsigned int idx;
162
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300163 for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300164 msk = 1 << idx;
165 if (ctxt->stale_mask & msk) continue;
166 if (decoder_ops[idx].check(ctxt)) {
167 ctxt->stale_mask |= msk;
168 }
169 }
170 return ctxt->stale_mask != 0;
171}
172
173
174static void decoder_update(struct pvr2_v4l_decoder *ctxt)
175{
176 unsigned long msk;
177 unsigned int idx;
178
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300179 for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
Mike Iselyd8554972006-06-26 20:58:46 -0300180 msk = 1 << idx;
181 if (!(ctxt->stale_mask & msk)) continue;
182 ctxt->stale_mask &= ~msk;
183 decoder_ops[idx].update(ctxt);
184 }
185}
186
187
188static int decoder_detect(struct pvr2_i2c_client *cp)
189{
190 /* Attempt to query the decoder - let's see if it will answer */
191 struct v4l2_tuner vt;
192 int ret;
193
194 memset(&vt,0,sizeof(vt));
195 ret = pvr2_i2c_client_cmd(cp,VIDIOC_G_TUNER,&vt);
196 return ret == 0; /* Return true if it answered */
197}
198
199
200static void decoder_enable(struct pvr2_v4l_decoder *ctxt,int fl)
201{
202 pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 decoder_enable(%d)",fl);
203 pvr2_v4l2_cmd_stream(ctxt->client,fl);
204}
205
206
Mike Iselyd8554972006-06-26 20:58:46 -0300207static unsigned int decoder_describe(struct pvr2_v4l_decoder *ctxt,char *buf,unsigned int cnt)
208{
209 return scnprintf(buf,cnt,"handler: pvrusb2-video-v4l");
210}
211
212
Tobias Klauserc5a69d52007-02-17 20:11:19 +0100213static const struct pvr2_i2c_handler_functions hfuncs = {
Mike Iselyd8554972006-06-26 20:58:46 -0300214 .detach = (void (*)(void *))decoder_detach,
215 .check = (int (*)(void *))decoder_check,
216 .update = (void (*)(void *))decoder_update,
217 .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
218};
219
220
221int pvr2_i2c_decoder_v4l_setup(struct pvr2_hdw *hdw,
222 struct pvr2_i2c_client *cp)
223{
224 struct pvr2_v4l_decoder *ctxt;
225
226 if (hdw->decoder_ctrl) return 0;
227 if (cp->handler) return 0;
228 if (!decoder_detect(cp)) return 0;
229
Mike Iselyca545f72007-01-20 00:37:11 -0300230 ctxt = kzalloc(sizeof(*ctxt),GFP_KERNEL);
Mike Iselyd8554972006-06-26 20:58:46 -0300231 if (!ctxt) return 0;
Mike Iselyd8554972006-06-26 20:58:46 -0300232
233 ctxt->handler.func_data = ctxt;
234 ctxt->handler.func_table = &hfuncs;
235 ctxt->ctrl.ctxt = ctxt;
236 ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
237 ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
Mike Iselyd8554972006-06-26 20:58:46 -0300238 ctxt->client = cp;
239 ctxt->hdw = hdw;
Ahmed S. Darwisheca8ebf2007-01-20 00:35:03 -0300240 ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1;
Mike Isely681c7392007-11-26 01:48:52 -0300241 pvr2_hdw_set_decoder(hdw,&ctxt->ctrl);
Mike Iselyd8554972006-06-26 20:58:46 -0300242 cp->handler = &ctxt->handler;
243 pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x saa711x V4L2 handler set up",
244 cp->client->addr);
245 return !0;
246}
247
248
249
250
251/*
252 Stuff for Emacs to see, in order to encourage consistent editing style:
253 *** Local Variables: ***
254 *** mode: c ***
255 *** fill-column: 70 ***
256 *** tab-width: 8 ***
257 *** c-basic-offset: 8 ***
258 *** End: ***
259 */