Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 1 | /* |
| 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 | cx2584x, in kernels 2.6.16 or newer. |
| 27 | |
| 28 | */ |
| 29 | |
| 30 | #include "pvrusb2-cx2584x-v4l.h" |
| 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 <media/cx25840.h> |
| 38 | #include <linux/videodev2.h> |
| 39 | #include <media/v4l2-common.h> |
| 40 | #include <linux/errno.h> |
| 41 | #include <linux/slab.h> |
| 42 | |
| 43 | struct pvr2_v4l_cx2584x { |
| 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 Isely | f5174af | 2007-11-26 02:07:26 -0300 | [diff] [blame] | 52 | struct routing_scheme_item { |
| 53 | int vid; |
| 54 | int aud; |
| 55 | }; |
| 56 | |
| 57 | struct routing_scheme { |
| 58 | const struct routing_scheme_item *def; |
| 59 | unsigned int cnt; |
| 60 | }; |
| 61 | |
| 62 | static const struct routing_scheme_item routing_scheme0[] = { |
| 63 | [PVR2_CVAL_INPUT_TV] = { |
| 64 | .vid = CX25840_COMPOSITE7, |
| 65 | .aud = CX25840_AUDIO8, |
| 66 | }, |
| 67 | [PVR2_CVAL_INPUT_RADIO] = { /* Treat the same as composite */ |
| 68 | .vid = CX25840_COMPOSITE3, |
| 69 | .aud = CX25840_AUDIO_SERIAL, |
| 70 | }, |
| 71 | [PVR2_CVAL_INPUT_COMPOSITE] = { |
| 72 | .vid = CX25840_COMPOSITE3, |
| 73 | .aud = CX25840_AUDIO_SERIAL, |
| 74 | }, |
| 75 | [PVR2_CVAL_INPUT_SVIDEO] = { |
| 76 | .vid = CX25840_SVIDEO1, |
| 77 | .aud = CX25840_AUDIO_SERIAL, |
| 78 | }, |
| 79 | }; |
| 80 | |
| 81 | static const struct routing_scheme routing_schemes[] = { |
| 82 | [PVR2_ROUTING_SCHEME_HAUPPAUGE] = { |
| 83 | .def = routing_scheme0, |
| 84 | .cnt = ARRAY_SIZE(routing_scheme0), |
| 85 | }, |
| 86 | }; |
| 87 | |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 88 | static void set_input(struct pvr2_v4l_cx2584x *ctxt) |
| 89 | { |
| 90 | struct pvr2_hdw *hdw = ctxt->hdw; |
| 91 | struct v4l2_routing route; |
| 92 | enum cx25840_video_input vid_input; |
| 93 | enum cx25840_audio_input aud_input; |
Mike Isely | f5174af | 2007-11-26 02:07:26 -0300 | [diff] [blame] | 94 | const struct routing_scheme *sp; |
| 95 | unsigned int sid = hdw->hdw_desc->signal_routing_scheme; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 96 | |
| 97 | memset(&route,0,sizeof(route)); |
| 98 | |
Mike Isely | f5174af | 2007-11-26 02:07:26 -0300 | [diff] [blame] | 99 | if ((sid < ARRAY_SIZE(routing_schemes)) && |
| 100 | ((sp = routing_schemes + sid) != 0) && |
| 101 | (hdw->input_val >= 0) && |
| 102 | (hdw->input_val < sp->cnt)) { |
| 103 | vid_input = sp->def[hdw->input_val].vid; |
| 104 | aud_input = sp->def[hdw->input_val].aud; |
| 105 | } else { |
| 106 | pvr2_trace(PVR2_TRACE_ERROR_LEGS, |
| 107 | "*** WARNING *** i2c cx2584x set_input:" |
| 108 | " Invalid routing scheme (%u) and/or input (%d)", |
| 109 | sid,hdw->input_val); |
| 110 | return; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_input vid=0x%x aud=0x%x", |
| 114 | vid_input,aud_input); |
| 115 | route.input = (u32)vid_input; |
| 116 | pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route); |
| 117 | route.input = (u32)aud_input; |
| 118 | pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_AUDIO_ROUTING,&route); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static int check_input(struct pvr2_v4l_cx2584x *ctxt) |
| 123 | { |
| 124 | struct pvr2_hdw *hdw = ctxt->hdw; |
| 125 | return hdw->input_dirty != 0; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | static void set_audio(struct pvr2_v4l_cx2584x *ctxt) |
| 130 | { |
| 131 | u32 val; |
| 132 | struct pvr2_hdw *hdw = ctxt->hdw; |
| 133 | |
| 134 | pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx2584x set_audio %d", |
| 135 | hdw->srate_val); |
| 136 | switch (hdw->srate_val) { |
| 137 | default: |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 138 | case V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000: |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 139 | val = 48000; |
| 140 | break; |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 141 | case V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100: |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 142 | val = 44100; |
| 143 | break; |
Mike Isely | b30d244 | 2006-06-25 20:05:01 -0300 | [diff] [blame] | 144 | case V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000: |
| 145 | val = 32000; |
| 146 | break; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 147 | } |
| 148 | pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | static int check_audio(struct pvr2_v4l_cx2584x *ctxt) |
| 153 | { |
| 154 | struct pvr2_hdw *hdw = ctxt->hdw; |
| 155 | return hdw->srate_dirty != 0; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | struct pvr2_v4l_cx2584x_ops { |
| 160 | void (*update)(struct pvr2_v4l_cx2584x *); |
| 161 | int (*check)(struct pvr2_v4l_cx2584x *); |
| 162 | }; |
| 163 | |
| 164 | |
| 165 | static const struct pvr2_v4l_cx2584x_ops decoder_ops[] = { |
| 166 | { .update = set_input, .check = check_input}, |
| 167 | { .update = set_audio, .check = check_audio}, |
| 168 | }; |
| 169 | |
| 170 | |
| 171 | static void decoder_detach(struct pvr2_v4l_cx2584x *ctxt) |
| 172 | { |
Mike Isely | a0fd1cb | 2006-06-30 11:35:28 -0300 | [diff] [blame] | 173 | ctxt->client->handler = NULL; |
Mike Isely | 681c739 | 2007-11-26 01:48:52 -0300 | [diff] [blame] | 174 | pvr2_hdw_set_decoder(ctxt->hdw,NULL); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 175 | kfree(ctxt); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | static int decoder_check(struct pvr2_v4l_cx2584x *ctxt) |
| 180 | { |
| 181 | unsigned long msk; |
| 182 | unsigned int idx; |
| 183 | |
Mike Isely | 27c7b71 | 2007-01-20 00:39:17 -0300 | [diff] [blame] | 184 | for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) { |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 185 | msk = 1 << idx; |
| 186 | if (ctxt->stale_mask & msk) continue; |
| 187 | if (decoder_ops[idx].check(ctxt)) { |
| 188 | ctxt->stale_mask |= msk; |
| 189 | } |
| 190 | } |
| 191 | return ctxt->stale_mask != 0; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | static void decoder_update(struct pvr2_v4l_cx2584x *ctxt) |
| 196 | { |
| 197 | unsigned long msk; |
| 198 | unsigned int idx; |
| 199 | |
Mike Isely | 27c7b71 | 2007-01-20 00:39:17 -0300 | [diff] [blame] | 200 | for (idx = 0; idx < ARRAY_SIZE(decoder_ops); idx++) { |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 201 | msk = 1 << idx; |
| 202 | if (!(ctxt->stale_mask & msk)) continue; |
| 203 | ctxt->stale_mask &= ~msk; |
| 204 | decoder_ops[idx].update(ctxt); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | |
| 209 | static void decoder_enable(struct pvr2_v4l_cx2584x *ctxt,int fl) |
| 210 | { |
| 211 | pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx25840 decoder_enable(%d)",fl); |
| 212 | pvr2_v4l2_cmd_stream(ctxt->client,fl); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | static int decoder_detect(struct pvr2_i2c_client *cp) |
| 217 | { |
| 218 | int ret; |
| 219 | /* Attempt to query the decoder - let's see if it will answer */ |
| 220 | struct v4l2_queryctrl qc; |
| 221 | |
| 222 | memset(&qc,0,sizeof(qc)); |
| 223 | |
| 224 | qc.id = V4L2_CID_BRIGHTNESS; |
| 225 | |
| 226 | ret = pvr2_i2c_client_cmd(cp,VIDIOC_QUERYCTRL,&qc); |
| 227 | return ret == 0; /* Return true if it answered */ |
| 228 | } |
| 229 | |
| 230 | |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 231 | static unsigned int decoder_describe(struct pvr2_v4l_cx2584x *ctxt, |
| 232 | char *buf,unsigned int cnt) |
| 233 | { |
| 234 | return scnprintf(buf,cnt,"handler: pvrusb2-cx2584x-v4l"); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | static void decoder_reset(struct pvr2_v4l_cx2584x *ctxt) |
| 239 | { |
| 240 | int ret; |
Randy Dunlap | c2625bf | 2006-10-29 11:12:27 -0300 | [diff] [blame] | 241 | ret = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_RESET,NULL); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 242 | pvr2_trace(PVR2_TRACE_CHIPS,"i2c cx25840 decoder_reset (ret=%d)",ret); |
| 243 | } |
| 244 | |
| 245 | |
Tobias Klauser | c5a69d5 | 2007-02-17 20:11:19 +0100 | [diff] [blame] | 246 | static const struct pvr2_i2c_handler_functions hfuncs = { |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 247 | .detach = (void (*)(void *))decoder_detach, |
| 248 | .check = (int (*)(void *))decoder_check, |
| 249 | .update = (void (*)(void *))decoder_update, |
| 250 | .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe, |
| 251 | }; |
| 252 | |
| 253 | |
| 254 | int pvr2_i2c_cx2584x_v4l_setup(struct pvr2_hdw *hdw, |
| 255 | struct pvr2_i2c_client *cp) |
| 256 | { |
| 257 | struct pvr2_v4l_cx2584x *ctxt; |
| 258 | |
| 259 | if (hdw->decoder_ctrl) return 0; |
| 260 | if (cp->handler) return 0; |
| 261 | if (!decoder_detect(cp)) return 0; |
| 262 | |
Mike Isely | ca545f7 | 2007-01-20 00:37:11 -0300 | [diff] [blame] | 263 | ctxt = kzalloc(sizeof(*ctxt),GFP_KERNEL); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 264 | if (!ctxt) return 0; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 265 | |
| 266 | ctxt->handler.func_data = ctxt; |
| 267 | ctxt->handler.func_table = &hfuncs; |
| 268 | ctxt->ctrl.ctxt = ctxt; |
| 269 | ctxt->ctrl.detach = (void (*)(void *))decoder_detach; |
| 270 | ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable; |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 271 | ctxt->ctrl.force_reset = (void (*)(void*))decoder_reset; |
| 272 | ctxt->client = cp; |
| 273 | ctxt->hdw = hdw; |
Mike Isely | 27c7b71 | 2007-01-20 00:39:17 -0300 | [diff] [blame] | 274 | ctxt->stale_mask = (1 << ARRAY_SIZE(decoder_ops)) - 1; |
Mike Isely | 681c739 | 2007-11-26 01:48:52 -0300 | [diff] [blame] | 275 | pvr2_hdw_set_decoder(hdw,&ctxt->ctrl); |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 276 | cp->handler = &ctxt->handler; |
Mike Isely | ff67c61 | 2006-11-19 20:50:31 -0300 | [diff] [blame] | 277 | { |
| 278 | /* |
| 279 | Mike Isely <isely@pobox.com> 19-Nov-2006 - This bit |
| 280 | of nuttiness for cx25840 causes that module to |
| 281 | correctly set up its video scaling. This is really |
| 282 | a problem in the cx25840 module itself, but we work |
| 283 | around it here. The problem has not been seen in |
| 284 | ivtv because there VBI is supported and set up. We |
| 285 | don't do VBI here (at least not yet) and thus we |
| 286 | never attempted to even set it up. |
| 287 | */ |
| 288 | struct v4l2_format fmt; |
| 289 | memset(&fmt,0,sizeof(fmt)); |
| 290 | fmt.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE; |
| 291 | pvr2_i2c_client_cmd(ctxt->client,VIDIOC_S_FMT,&fmt); |
| 292 | } |
Mike Isely | d855497 | 2006-06-26 20:58:46 -0300 | [diff] [blame] | 293 | pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x cx2584x V4L2 handler set up", |
| 294 | cp->client->addr); |
| 295 | return !0; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | /* |
| 302 | Stuff for Emacs to see, in order to encourage consistent editing style: |
| 303 | *** Local Variables: *** |
| 304 | *** mode: c *** |
| 305 | *** fill-column: 70 *** |
| 306 | *** tab-width: 8 *** |
| 307 | *** c-basic-offset: 8 *** |
| 308 | *** End: *** |
| 309 | */ |