1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/device.h>
30#include <linux/spinlock.h>
31
32#include <media/v4l2-device.h>
33
34#include <sound/core.h>
35#include <sound/initval.h>
36
37#include "cx18-driver.h"
38#include "cx18-version.h"
39#include "cx18-alsa.h"
40#include "cx18-alsa-mixer.h"
41#include "cx18-alsa-pcm.h"
42
43int cx18_alsa_debug;
44
45#define CX18_DEBUG_ALSA_INFO(fmt, arg...) \
46 do { \
47 if (cx18_alsa_debug & 2) \
48 printk(KERN_INFO "%s: " fmt, "cx18-alsa", ## arg); \
49 } while (0);
50
51module_param_named(debug, cx18_alsa_debug, int, 0644);
52MODULE_PARM_DESC(debug,
53 "Debug level (bitmask). Default: 0\n"
54 "\t\t\t 1/0x0001: warning\n"
55 "\t\t\t 2/0x0002: info\n");
56
57MODULE_AUTHOR("Andy Walls");
58MODULE_DESCRIPTION("CX23418 ALSA Interface");
59MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
60MODULE_LICENSE("GPL");
61
62MODULE_VERSION(CX18_VERSION);
63
64static inline
65struct snd_cx18_card *to_snd_cx18_card(struct v4l2_device *v4l2_dev)
66{
67 return to_cx18(v4l2_dev)->alsa;
68}
69
70static inline
71struct snd_cx18_card *p_to_snd_cx18_card(struct v4l2_device **v4l2_dev)
72{
73 return container_of(v4l2_dev, struct snd_cx18_card, v4l2_dev);
74}
75
76static void snd_cx18_card_free(struct snd_cx18_card *cxsc)
77{
78 if (cxsc == NULL)
79 return;
80
81 if (cxsc->v4l2_dev != NULL)
82 to_cx18(cxsc->v4l2_dev)->alsa = NULL;
83
84
85
86 kfree(cxsc);
87}
88
89static void snd_cx18_card_private_free(struct snd_card *sc)
90{
91 if (sc == NULL)
92 return;
93 snd_cx18_card_free(sc->private_data);
94 sc->private_data = NULL;
95 sc->private_free = NULL;
96}
97
98static int snd_cx18_card_create(struct v4l2_device *v4l2_dev,
99 struct snd_card *sc,
100 struct snd_cx18_card **cxsc)
101{
102 *cxsc = kzalloc(sizeof(struct snd_cx18_card), GFP_KERNEL);
103 if (*cxsc == NULL)
104 return -ENOMEM;
105
106 (*cxsc)->v4l2_dev = v4l2_dev;
107 (*cxsc)->sc = sc;
108
109 sc->private_data = *cxsc;
110 sc->private_free = snd_cx18_card_private_free;
111
112 return 0;
113}
114
115static int snd_cx18_card_set_names(struct snd_cx18_card *cxsc)
116{
117 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
118 struct snd_card *sc = cxsc->sc;
119
120
121 strlcpy(sc->driver, "CX23418", sizeof(sc->driver));
122
123
124 snprintf(sc->shortname, sizeof(sc->shortname), "CX18-%d",
125 cx->instance);
126
127
128 snprintf(sc->longname, sizeof(sc->longname),
129 "CX23418 #%d %s TV/FM Radio/Line-In Capture",
130 cx->instance, cx->card_name);
131
132 return 0;
133}
134
135static int snd_cx18_init(struct v4l2_device *v4l2_dev)
136{
137 struct cx18 *cx = to_cx18(v4l2_dev);
138 struct snd_card *sc = NULL;
139 struct snd_cx18_card *cxsc;
140 int ret;
141
142
143
144
145
146
147
148 ret = snd_card_new(&cx->pci_dev->dev,
149 SNDRV_DEFAULT_IDX1,
150 SNDRV_DEFAULT_STR1,
151 THIS_MODULE, 0, &sc);
152 if (ret) {
153 CX18_ALSA_ERR("%s: snd_card_new() failed with err %d\n",
154 __func__, ret);
155 goto err_exit;
156 }
157
158
159 ret = snd_cx18_card_create(v4l2_dev, sc, &cxsc);
160 if (ret) {
161 CX18_ALSA_ERR("%s: snd_cx18_card_create() failed with err %d\n",
162 __func__, ret);
163 goto err_exit_free;
164 }
165
166
167 snd_cx18_card_set_names(cxsc);
168
169
170 ret = snd_cx18_pcm_create(cxsc);
171 if (ret) {
172 CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
173 __func__, ret);
174 goto err_exit_free;
175 }
176
177
178
179
180 cx->alsa = cxsc;
181
182
183 ret = snd_card_register(sc);
184 if (ret) {
185 cx->alsa = NULL;
186 CX18_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
187 __func__, ret);
188 goto err_exit_free;
189 }
190
191 return 0;
192
193err_exit_free:
194 if (sc != NULL)
195 snd_card_free(sc);
196 kfree(cxsc);
197err_exit:
198 return ret;
199}
200
201static int cx18_alsa_load(struct cx18 *cx)
202{
203 struct v4l2_device *v4l2_dev = &cx->v4l2_dev;
204 struct cx18_stream *s;
205
206 if (v4l2_dev == NULL) {
207 printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
208 __func__);
209 return 0;
210 }
211
212 cx = to_cx18(v4l2_dev);
213 if (cx == NULL) {
214 printk(KERN_ERR "cx18-alsa cx is NULL\n");
215 return 0;
216 }
217
218 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
219 if (s->video_dev.v4l2_dev == NULL) {
220 CX18_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - "
221 "skipping\n", __func__);
222 return 0;
223 }
224
225 if (cx->alsa != NULL) {
226 CX18_ALSA_ERR("%s: struct snd_cx18_card * already exists\n",
227 __func__);
228 return 0;
229 }
230
231 if (snd_cx18_init(v4l2_dev)) {
232 CX18_ALSA_ERR("%s: failed to create struct snd_cx18_card\n",
233 __func__);
234 } else {
235 CX18_DEBUG_ALSA_INFO("%s: created cx18 ALSA interface instance "
236 "\n", __func__);
237 }
238 return 0;
239}
240
241static int __init cx18_alsa_init(void)
242{
243 printk(KERN_INFO "cx18-alsa: module loading...\n");
244 cx18_ext_init = &cx18_alsa_load;
245 return 0;
246}
247
248static void __exit snd_cx18_exit(struct snd_cx18_card *cxsc)
249{
250 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
251
252
253
254 snd_card_free(cxsc->sc);
255 cx->alsa = NULL;
256}
257
258static int __exit cx18_alsa_exit_callback(struct device *dev, void *data)
259{
260 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
261 struct snd_cx18_card *cxsc;
262
263 if (v4l2_dev == NULL) {
264 printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
265 __func__);
266 return 0;
267 }
268
269 cxsc = to_snd_cx18_card(v4l2_dev);
270 if (cxsc == NULL) {
271 CX18_ALSA_WARN("%s: struct snd_cx18_card * is NULL\n",
272 __func__);
273 return 0;
274 }
275
276 snd_cx18_exit(cxsc);
277 return 0;
278}
279
280static void __exit cx18_alsa_exit(void)
281{
282 struct device_driver *drv;
283 int ret;
284
285 printk(KERN_INFO "cx18-alsa: module unloading...\n");
286
287 drv = driver_find("cx18", &pci_bus_type);
288 ret = driver_for_each_device(drv, NULL, NULL, cx18_alsa_exit_callback);
289 (void)ret;
290
291 cx18_ext_init = NULL;
292 printk(KERN_INFO "cx18-alsa: module unload complete\n");
293}
294
295module_init(cx18_alsa_init);
296module_exit(cx18_alsa_exit);
297