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 | 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 | |
| 43 | struct 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 | |
| 52 | static void set_input(struct pvr2_v4l_decoder *ctxt) |
| 53 | { |
| 54 | struct pvr2_hdw *hdw = ctxt->hdw; |
| 55 | struct v4l2_routing route; |
| 56 | |
| 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; |
| 61 | break; |
| 62 | case PVR2_CVAL_INPUT_COMPOSITE: |
| 63 | route.input = SAA7115_COMPOSITE5; |
| 64 | break; |
| 65 | case PVR2_CVAL_INPUT_SVIDEO: |
| 66 | route.input = SAA7115_SVIDEO2; |
| 67 | break; |
| 68 | case PVR2_CVAL_INPUT_RADIO: |
| 69 | // ????? No idea yet what to do here |
| 70 | default: |
| 71 | return; |
| 72 | } |
| 73 | route.output = 0; |
| 74 | pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_VIDEO_ROUTING,&route); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | static int check_input(struct pvr2_v4l_decoder *ctxt) |
| 79 | { |
| 80 | struct pvr2_hdw *hdw = ctxt->hdw; |
| 81 | return hdw->input_dirty != 0; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static void set_audio(struct pvr2_v4l_decoder *ctxt) |
| 86 | { |
| 87 | u32 val; |
| 88 | struct pvr2_hdw *hdw = ctxt->hdw; |
| 89 | |
| 90 | pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 set_audio %d", |
| 91 | hdw->srate_val); |
| 92 | switch (hdw->srate_val) { |
| 93 | default: |
| 94 | case PVR2_CVAL_SRATE_48: |
| 95 | val = 48000; |
| 96 | break; |
| 97 | case PVR2_CVAL_SRATE_44_1: |
| 98 | val = 44100; |
| 99 | break; |
| 100 | } |
| 101 | pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_AUDIO_CLOCK_FREQ,&val); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | static int check_audio(struct pvr2_v4l_decoder *ctxt) |
| 106 | { |
| 107 | struct pvr2_hdw *hdw = ctxt->hdw; |
| 108 | return hdw->srate_dirty != 0; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | struct pvr2_v4l_decoder_ops { |
| 113 | void (*update)(struct pvr2_v4l_decoder *); |
| 114 | int (*check)(struct pvr2_v4l_decoder *); |
| 115 | }; |
| 116 | |
| 117 | |
| 118 | static const struct pvr2_v4l_decoder_ops decoder_ops[] = { |
| 119 | { .update = set_input, .check = check_input}, |
| 120 | { .update = set_audio, .check = check_audio}, |
| 121 | }; |
| 122 | |
| 123 | |
| 124 | static void decoder_detach(struct pvr2_v4l_decoder *ctxt) |
| 125 | { |
| 126 | ctxt->client->handler = 0; |
| 127 | ctxt->hdw->decoder_ctrl = 0; |
| 128 | kfree(ctxt); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | static int decoder_check(struct pvr2_v4l_decoder *ctxt) |
| 133 | { |
| 134 | unsigned long msk; |
| 135 | unsigned int idx; |
| 136 | |
| 137 | for (idx = 0; idx < sizeof(decoder_ops)/sizeof(decoder_ops[0]); |
| 138 | idx++) { |
| 139 | msk = 1 << idx; |
| 140 | if (ctxt->stale_mask & msk) continue; |
| 141 | if (decoder_ops[idx].check(ctxt)) { |
| 142 | ctxt->stale_mask |= msk; |
| 143 | } |
| 144 | } |
| 145 | return ctxt->stale_mask != 0; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | static void decoder_update(struct pvr2_v4l_decoder *ctxt) |
| 150 | { |
| 151 | unsigned long msk; |
| 152 | unsigned int idx; |
| 153 | |
| 154 | for (idx = 0; idx < sizeof(decoder_ops)/sizeof(decoder_ops[0]); |
| 155 | idx++) { |
| 156 | msk = 1 << idx; |
| 157 | if (!(ctxt->stale_mask & msk)) continue; |
| 158 | ctxt->stale_mask &= ~msk; |
| 159 | decoder_ops[idx].update(ctxt); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |
| 164 | static int decoder_detect(struct pvr2_i2c_client *cp) |
| 165 | { |
| 166 | /* Attempt to query the decoder - let's see if it will answer */ |
| 167 | struct v4l2_tuner vt; |
| 168 | int ret; |
| 169 | |
| 170 | memset(&vt,0,sizeof(vt)); |
| 171 | ret = pvr2_i2c_client_cmd(cp,VIDIOC_G_TUNER,&vt); |
| 172 | return ret == 0; /* Return true if it answered */ |
| 173 | } |
| 174 | |
| 175 | |
| 176 | static void decoder_enable(struct pvr2_v4l_decoder *ctxt,int fl) |
| 177 | { |
| 178 | pvr2_trace(PVR2_TRACE_CHIPS,"i2c v4l2 decoder_enable(%d)",fl); |
| 179 | pvr2_v4l2_cmd_stream(ctxt->client,fl); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | static int decoder_is_tuned(struct pvr2_v4l_decoder *ctxt) |
| 184 | { |
| 185 | struct v4l2_tuner vt; |
| 186 | int ret; |
| 187 | |
| 188 | memset(&vt,0,sizeof(vt)); |
| 189 | ret = pvr2_i2c_client_cmd(ctxt->client,VIDIOC_G_TUNER,&vt); |
| 190 | if (ret < 0) return -EINVAL; |
| 191 | return vt.signal ? 1 : 0; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | static unsigned int decoder_describe(struct pvr2_v4l_decoder *ctxt,char *buf,unsigned int cnt) |
| 196 | { |
| 197 | return scnprintf(buf,cnt,"handler: pvrusb2-video-v4l"); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | const static struct pvr2_i2c_handler_functions hfuncs = { |
| 202 | .detach = (void (*)(void *))decoder_detach, |
| 203 | .check = (int (*)(void *))decoder_check, |
| 204 | .update = (void (*)(void *))decoder_update, |
| 205 | .describe = (unsigned int (*)(void *,char *,unsigned int))decoder_describe, |
| 206 | }; |
| 207 | |
| 208 | |
| 209 | int pvr2_i2c_decoder_v4l_setup(struct pvr2_hdw *hdw, |
| 210 | struct pvr2_i2c_client *cp) |
| 211 | { |
| 212 | struct pvr2_v4l_decoder *ctxt; |
| 213 | |
| 214 | if (hdw->decoder_ctrl) return 0; |
| 215 | if (cp->handler) return 0; |
| 216 | if (!decoder_detect(cp)) return 0; |
| 217 | |
| 218 | ctxt = kmalloc(sizeof(*ctxt),GFP_KERNEL); |
| 219 | if (!ctxt) return 0; |
| 220 | memset(ctxt,0,sizeof(*ctxt)); |
| 221 | |
| 222 | ctxt->handler.func_data = ctxt; |
| 223 | ctxt->handler.func_table = &hfuncs; |
| 224 | ctxt->ctrl.ctxt = ctxt; |
| 225 | ctxt->ctrl.detach = (void (*)(void *))decoder_detach; |
| 226 | ctxt->ctrl.enable = (void (*)(void *,int))decoder_enable; |
| 227 | ctxt->ctrl.tuned = (int (*)(void *))decoder_is_tuned; |
| 228 | ctxt->client = cp; |
| 229 | ctxt->hdw = hdw; |
| 230 | ctxt->stale_mask = (1 << (sizeof(decoder_ops)/ |
| 231 | sizeof(decoder_ops[0]))) - 1; |
| 232 | hdw->decoder_ctrl = &ctxt->ctrl; |
| 233 | cp->handler = &ctxt->handler; |
| 234 | pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x saa711x V4L2 handler set up", |
| 235 | cp->client->addr); |
| 236 | return !0; |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | /* |
| 243 | Stuff for Emacs to see, in order to encourage consistent editing style: |
| 244 | *** Local Variables: *** |
| 245 | *** mode: c *** |
| 246 | *** fill-column: 70 *** |
| 247 | *** tab-width: 8 *** |
| 248 | *** c-basic-offset: 8 *** |
| 249 | *** End: *** |
| 250 | */ |