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 cx2584x, in kernels 2.6.16 or newer.
30 #include "pvrusb2-cx2584x-v4l.h"
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 <media/cx25840.h>
38 #include <linux/videodev2.h>
39 #include <media/v4l2-common.h>
40 #include <linux/errno.h>
41 #include <linux/slab.h>
43 struct pvr2_v4l_cx2584x {
44 struct pvr2_i2c_handler handler;
45 struct pvr2_decoder_ctrl ctrl;
46 struct pvr2_i2c_client *client;
48 unsigned long stale_mask;
52 struct routing_scheme_item {
57 struct routing_scheme {
58 const struct routing_scheme_item *def;
62 static const struct routing_scheme_item routing_scheme0[] = {
63 [PVR2_CVAL_INPUT_TV] = {
64 .vid = CX25840_COMPOSITE7,
65 .aud = CX25840_AUDIO8,
67 [PVR2_CVAL_INPUT_RADIO] = { /* Treat the same as composite */
68 .vid = CX25840_COMPOSITE3,
69 .aud = CX25840_AUDIO_SERIAL,
71 [PVR2_CVAL_INPUT_COMPOSITE] = {
72 .vid = CX25840_COMPOSITE3,
73 .aud = CX25840_AUDIO_SERIAL,
75 [PVR2_CVAL_INPUT_SVIDEO] = {
76 .vid = CX25840_SVIDEO1,
77 .aud = CX25840_AUDIO_SERIAL,
81 /* Specific to gotview device */
82 static const struct routing_scheme_item routing_schemegv[] = {
83 [PVR2_CVAL_INPUT_TV] = {
84 .vid = CX25840_COMPOSITE2,
85 .aud = CX25840_AUDIO5,
87 [PVR2_CVAL_INPUT_RADIO] = {
88 /* line-in is used for radio and composite. A GPIO is
89 used to switch between the two choices. */
90 .vid = CX25840_COMPOSITE1,
91 .aud = CX25840_AUDIO_SERIAL,
93 [PVR2_CVAL_INPUT_COMPOSITE] = {
94 .vid = CX25840_COMPOSITE1,
95 .aud = CX25840_AUDIO_SERIAL,
97 [PVR2_CVAL_INPUT_SVIDEO] = {
98 .vid = (CX25840_SVIDEO_LUMA3|CX25840_SVIDEO_CHROMA4),
99 .aud = CX25840_AUDIO_SERIAL,
103 static const struct routing_scheme routing_schemes[] = {
104 [PVR2_ROUTING_SCHEME_HAUPPAUGE] = {
105 .def = routing_scheme0,
106 .cnt = ARRAY_SIZE(routing_scheme0),
108 [PVR2_ROUTING_SCHEME_GOTVIEW] = {
109 .def = routing_schemegv,
110 .cnt = ARRAY_SIZE(routing_schemegv),
114 static void set_input(struct pvr2_v4l_cx2584x *ctxt)
116 struct pvr2_hdw *hdw = ctxt->hdw;
117 struct v4l2_routing route;
118 enum cx25840_video_input vid_input;
119 enum cx25840_audio_input aud_input;
120 const struct routing_scheme *sp;
121 unsigned int sid = hdw->hdw_desc->signal_routing_scheme;
123 memset(&route,0,sizeof(route));
125 if ((sid < ARRAY_SIZE(routing_schemes)) &&
126 ((sp = routing_schemes + sid) != NULL) &&
127 (hdw->input_val >= 0) &&
128 (hdw->input_val < sp->cnt)) {
129 vid_input = sp->def[hdw->input_val].vid;
130 aud_input = sp->def[hdw->input_val].aud;
132 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
133 "*** WARNING *** i2c cx2584x set_input:"
134 " Invalid routing scheme (%u) and/or input (%d)",
139 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_input vid=0x%x aud=0x%x",
140 vid_input,aud_input);
141 route.input = (u32)vid_input;
142 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route);
143 route.input = (u32)aud_input;
144 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_AUDIO_ROUTING,&route);
148 static int check_input(struct pvr2_v4l_cx2584x *ctxt)
150 struct pvr2_hdw *hdw = ctxt->hdw;
151 return hdw->input_dirty != 0;
155 static void set_audio(struct pvr2_v4l_cx2584x *ctxt)
158 struct pvr2_hdw *hdw = ctxt->hdw;
160 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_audio %d",
162 switch (hdw->srate_val) {
164 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000:
167 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100:
170 case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000:
174 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val);
178 static int check_audio(struct pvr2_v4l_cx2584x *ctxt)
180 struct pvr2_hdw *hdw = ctxt->hdw;
181 return hdw->srate_dirty != 0;
185 struct pvr2_v4l_cx2584x_ops {
186 void (*update)(struct pvr2_v4l_cx2584x *);
187 int (*check)(struct pvr2_v4l_cx2584x *);
191 static const struct pvr2_v4l_cx2584x_ops decoder_ops[] = {
192 { .update = set_input, .check = check_input},
193 { .update = set_audio, .check = check_audio},
197 static void decoder_detach(struct pvr2_v4l_cx2584x *ctxt)
199 ctxt->client->handler = NULL;
200 pvr2_hdw_set_decoder(ctxt->hdw,NULL);
205 static int decoder_check(struct pvr2_v4l_cx2584x *ctxt)
210 for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
212 if (ctxt->stale_mask & msk) continue;
213 if (decoder_ops[idx].check(ctxt)) {
214 ctxt->stale_mask |= msk;
217 return ctxt->stale_mask != 0;
221 static void decoder_update(struct pvr2_v4l_cx2584x *ctxt)
226 for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) {
228 if (!(ctxt->stale_mask & msk)) continue;
229 ctxt->stale_mask &= ~msk;
230 decoder_ops[idx].update(ctxt);
235 static void decoder_enable(struct pvr2_v4l_cx2584x *ctxt,int fl)
237 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx25840 decoder_enable(%d)",fl);
238 pvr2_v4l2_cmd_stream(ctxt->client,fl);
242 static int decoder_detect(struct pvr2_i2c_client *cp)
245 /* Attempt to query the decoder - let's see if it will answer */
246 struct v4l2_queryctrl qc;
248 memset(&qc,0,sizeof(qc));
250 qc.id = V4L2_CID_BRIGHTNESS;
252 ret = pvr2_i2c_client_cmd(cp,VIDIOC_QUERYCTRL,&qc);
253 return ret == 0; /* Return true if it answered */
257 static unsigned int decoder_describe(struct pvr2_v4l_cx2584x *ctxt,
258 char *buf,unsigned int cnt)
260 return scnprintf(buf,cnt,"handler: pvrusb2-cx2584x-v4l");
264 static void decoder_reset(struct pvr2_v4l_cx2584x *ctxt)
267 ret = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_RESET,NULL);
268 pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx25840 decoder_reset (ret=%d)",ret);
272 static const struct pvr2_i2c_handler_functions hfuncs = {
273 .detach = (void (*)(void *))decoder_detach,
274 .check = (int (*)(void *))decoder_check,
275 .update = (void (*)(void *))decoder_update,
276 .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe,
280 int pvr2_i2c_cx2584x_v4l_setup(struct pvr2_hdw *hdw,
281 struct pvr2_i2c_client *cp)
283 struct pvr2_v4l_cx2584x *ctxt;
285 if (hdw->decoder_ctrl) return 0;
286 if (cp->handler) return 0;
287 if (!decoder_detect(cp)) return 0;
289 ctxt = kzalloc(sizeof(*ctxt),GFP_KERNEL);
292 ctxt->handler.func_data = ctxt;
293 ctxt->handler.func_table = &hfuncs;
294 ctxt->ctrl.ctxt = ctxt;
295 ctxt->ctrl.detach = (void (*)(void *))decoder_detach;
296 ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable;
297 ctxt->ctrl.force_reset = (void (*)(void*))decoder_reset;
300 ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1;
301 pvr2_hdw_set_decoder(hdw,&ctxt->ctrl);
302 cp->handler = &ctxt->handler;
305 Mike Isely <isely@pobox.com> 19-Nov-2006 - This bit
306 of nuttiness for cx25840 causes that module to
307 correctly set up its video scaling. This is really
308 a problem in the cx25840 module itself, but we work
309 around it here. The problem has not been seen in
310 ivtv because there VBI is supported and set up. We
311 don't do VBI here (at least not yet) and thus we
312 never attempted to even set it up.
314 struct v4l2_format fmt;
315 memset(&fmt,0,sizeof(fmt));
316 fmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
317 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_S_FMT,&fmt);
319 pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x cx2584x V4L2 handler set up",
328 Stuff for Emacs to see, in order to encourage consistent editing style:
329 *** Local Variables: ***
331 *** fill-column: 70 ***
333 *** c-basic-offset: 8 ***