]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/pvrusb2/pvrusb2-audio.c
V4L/DVB (5092): Pvrusb2: Use ARRAY_SIZE wherever possible
[linux-2.6-omap-h63xx.git] / drivers / media / video / pvrusb2 / pvrusb2-audio.c
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 #include "pvrusb2-audio.h"
24 #include "pvrusb2-hdw-internal.h"
25 #include "pvrusb2-debug.h"
26 #include <linux/videodev2.h>
27 #include <media/msp3400.h>
28 #include <media/v4l2-common.h>
29
30 struct pvr2_msp3400_handler {
31         struct pvr2_hdw *hdw;
32         struct pvr2_i2c_client *client;
33         struct pvr2_i2c_handler i2c_handler;
34         unsigned long stale_mask;
35 };
36
37
38 /* This function selects the correct audio input source */
39 static void set_stereo(struct pvr2_msp3400_handler *ctxt)
40 {
41         struct pvr2_hdw *hdw = ctxt->hdw;
42         struct v4l2_routing route;
43
44         pvr2_trace(PVR2_TRACE_CHIPS,"i2c msp3400 v4l2 set_stereo");
45
46         if (hdw->input_val == PVR2_CVAL_INPUT_TV) {
47                 struct v4l2_tuner vt;
48                 memset(&vt,0,sizeof(vt));
49                 vt.audmode = hdw->audiomode_val;
50                 pvr2_i2c_client_cmd(ctxt->client,VIDIOC_S_TUNER,&vt);
51         }
52
53         route.input = MSP_INPUT_DEFAULT;
54         route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
55         switch (hdw->input_val) {
56         case PVR2_CVAL_INPUT_TV:
57                 break;
58         case PVR2_CVAL_INPUT_RADIO:
59                 /* Assume that msp34xx also handle FM decoding, in which case
60                    we're still using the tuner. */
61                 /* HV: actually it is more likely to be the SCART2 input if
62                    the ivtv experience is any indication. */
63                 route.input = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
64                                     MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
65                 break;
66         case PVR2_CVAL_INPUT_SVIDEO:
67         case PVR2_CVAL_INPUT_COMPOSITE:
68                 /* SCART 1 input */
69                 route.input = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
70                                     MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
71                 break;
72         }
73         pvr2_i2c_client_cmd(ctxt->client,VIDIOC_INT_S_AUDIO_ROUTING,&route);
74 }
75
76
77 static int check_stereo(struct pvr2_msp3400_handler *ctxt)
78 {
79         struct pvr2_hdw *hdw = ctxt->hdw;
80         return (hdw->input_dirty ||
81                 hdw->audiomode_dirty);
82 }
83
84
85 struct pvr2_msp3400_ops {
86         void (*update)(struct pvr2_msp3400_handler *);
87         int (*check)(struct pvr2_msp3400_handler *);
88 };
89
90
91 static const struct pvr2_msp3400_ops msp3400_ops[] = {
92         { .update = set_stereo, .check = check_stereo},
93 };
94
95
96 static int msp3400_check(struct pvr2_msp3400_handler *ctxt)
97 {
98         unsigned long msk;
99         unsigned int idx;
100
101         for (idx = 0; idx < ARRAY_SIZE(msp3400_ops); idx++) {
102                 msk = 1 << idx;
103                 if (ctxt->stale_mask & msk) continue;
104                 if (msp3400_ops[idx].check(ctxt)) {
105                         ctxt->stale_mask |= msk;
106                 }
107         }
108         return ctxt->stale_mask != 0;
109 }
110
111
112 static void msp3400_update(struct pvr2_msp3400_handler *ctxt)
113 {
114         unsigned long msk;
115         unsigned int idx;
116
117         for (idx = 0; idx < ARRAY_SIZE(msp3400_ops); idx++) {
118                 msk = 1 << idx;
119                 if (!(ctxt->stale_mask & msk)) continue;
120                 ctxt->stale_mask &= ~msk;
121                 msp3400_ops[idx].update(ctxt);
122         }
123 }
124
125
126 static void pvr2_msp3400_detach(struct pvr2_msp3400_handler *ctxt)
127 {
128         ctxt->client->handler = NULL;
129         kfree(ctxt);
130 }
131
132
133 static unsigned int pvr2_msp3400_describe(struct pvr2_msp3400_handler *ctxt,
134                                           char *buf,unsigned int cnt)
135 {
136         return scnprintf(buf,cnt,"handler: pvrusb2-audio v4l2");
137 }
138
139
140 static const struct pvr2_i2c_handler_functions msp3400_funcs = {
141         .detach = (void (*)(void *))pvr2_msp3400_detach,
142         .check = (int (*)(void *))msp3400_check,
143         .update = (void (*)(void *))msp3400_update,
144         .describe = (unsigned int (*)(void *,char *,unsigned int))pvr2_msp3400_describe,
145 };
146
147
148 int pvr2_i2c_msp3400_setup(struct pvr2_hdw *hdw,struct pvr2_i2c_client *cp)
149 {
150         struct pvr2_msp3400_handler *ctxt;
151         if (cp->handler) return 0;
152
153         ctxt = kzalloc(sizeof(*ctxt),GFP_KERNEL);
154         if (!ctxt) return 0;
155
156         ctxt->i2c_handler.func_data = ctxt;
157         ctxt->i2c_handler.func_table = &msp3400_funcs;
158         ctxt->client = cp;
159         ctxt->hdw = hdw;
160         ctxt->stale_mask = (1 << ARRAY_SIZE(msp3400_ops)) - 1;
161         cp->handler = &ctxt->i2c_handler;
162         pvr2_trace(PVR2_TRACE_CHIPS,"i2c 0x%x msp3400 V4L2 handler set up",
163                    cp->client->addr);
164         return !0;
165 }
166
167
168 /*
169   Stuff for Emacs to see, in order to encourage consistent editing style:
170   *** Local Variables: ***
171   *** mode: c ***
172   *** fill-column: 70 ***
173   *** tab-width: 8 ***
174   *** c-basic-offset: 8 ***
175   *** End: ***
176   */