xref: /linux/drivers/media/pci/ivtv/ivtv-alsa-main.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
1 /*
2  *  ALSA interface to ivtv PCM capture streams
3  *
4  *  Copyright (C) 2009,2012  Andy Walls <awalls@md.metrocast.net>
5  *  Copyright (C) 2009  Devin Heitmueller <dheitmueller@kernellabs.com>
6  *
7  *  Portions of this work were sponsored by ONELAN Limited for the cx18 driver
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  */
19 
20 #include "ivtv-driver.h"
21 #include "ivtv-version.h"
22 #include "ivtv-alsa.h"
23 #include "ivtv-alsa-mixer.h"
24 #include "ivtv-alsa-pcm.h"
25 
26 #include <sound/core.h>
27 #include <sound/initval.h>
28 
29 int ivtv_alsa_debug;
30 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
31 
32 #define IVTV_DEBUG_ALSA_INFO(__fmt, __arg...) \
33 	do { \
34 		if (ivtv_alsa_debug & 2) \
35 			printk(KERN_INFO pr_fmt("%s: alsa:" __fmt),	\
36 			       __func__, ##__arg);			\
37 	} while (0)
38 
39 module_param_named(debug, ivtv_alsa_debug, int, 0644);
40 MODULE_PARM_DESC(debug,
41 		 "Debug level (bitmask). Default: 0\n"
42 		 "\t\t\t  1/0x0001: warning\n"
43 		 "\t\t\t  2/0x0002: info\n");
44 
45 module_param_array(index, int, NULL, 0444);
46 MODULE_PARM_DESC(index,
47 		 "Index value for IVTV ALSA capture interface(s).\n");
48 
49 MODULE_AUTHOR("Andy Walls");
50 MODULE_DESCRIPTION("CX23415/CX23416 ALSA Interface");
51 MODULE_SUPPORTED_DEVICE("CX23415/CX23416 MPEG2 encoder");
52 MODULE_LICENSE("GPL");
53 
54 MODULE_VERSION(IVTV_VERSION);
55 
56 static inline
57 struct snd_ivtv_card *to_snd_ivtv_card(struct v4l2_device *v4l2_dev)
58 {
59 	return to_ivtv(v4l2_dev)->alsa;
60 }
61 
62 static inline
63 struct snd_ivtv_card *p_to_snd_ivtv_card(struct v4l2_device **v4l2_dev)
64 {
65 	return container_of(v4l2_dev, struct snd_ivtv_card, v4l2_dev);
66 }
67 
68 static void snd_ivtv_card_free(struct snd_ivtv_card *itvsc)
69 {
70 	if (itvsc == NULL)
71 		return;
72 
73 	if (itvsc->v4l2_dev != NULL)
74 		to_ivtv(itvsc->v4l2_dev)->alsa = NULL;
75 
76 	/* FIXME - take any other stopping actions needed */
77 
78 	kfree(itvsc);
79 }
80 
81 static void snd_ivtv_card_private_free(struct snd_card *sc)
82 {
83 	if (sc == NULL)
84 		return;
85 	snd_ivtv_card_free(sc->private_data);
86 	sc->private_data = NULL;
87 	sc->private_free = NULL;
88 }
89 
90 static int snd_ivtv_card_create(struct v4l2_device *v4l2_dev,
91 				       struct snd_card *sc,
92 				       struct snd_ivtv_card **itvsc)
93 {
94 	*itvsc = kzalloc(sizeof(struct snd_ivtv_card), GFP_KERNEL);
95 	if (*itvsc == NULL)
96 		return -ENOMEM;
97 
98 	(*itvsc)->v4l2_dev = v4l2_dev;
99 	(*itvsc)->sc = sc;
100 
101 	sc->private_data = *itvsc;
102 	sc->private_free = snd_ivtv_card_private_free;
103 
104 	return 0;
105 }
106 
107 static int snd_ivtv_card_set_names(struct snd_ivtv_card *itvsc)
108 {
109 	struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
110 	struct snd_card *sc = itvsc->sc;
111 
112 	/* sc->driver is used by alsa-lib's configurator: simple, unique */
113 	strlcpy(sc->driver, "CX2341[56]", sizeof(sc->driver));
114 
115 	/* sc->shortname is a symlink in /proc/asound: IVTV-M -> cardN */
116 	snprintf(sc->shortname,  sizeof(sc->shortname), "IVTV-%d",
117 		 itv->instance);
118 
119 	/* sc->longname is read from /proc/asound/cards */
120 	snprintf(sc->longname, sizeof(sc->longname),
121 		 "CX2341[56] #%d %s TV/FM Radio/Line-In Capture",
122 		 itv->instance, itv->card_name);
123 
124 	return 0;
125 }
126 
127 static int snd_ivtv_init(struct v4l2_device *v4l2_dev)
128 {
129 	struct ivtv *itv = to_ivtv(v4l2_dev);
130 	struct snd_card *sc = NULL;
131 	struct snd_ivtv_card *itvsc;
132 	int ret, idx;
133 
134 	/* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
135 
136 	/* (1) Check and increment the device index */
137 	/* This is a no-op for us.  We'll use the itv->instance */
138 
139 	/* (2) Create a card instance */
140 	/* use first available id if not specified otherwise*/
141 	idx = index[itv->instance] == -1 ? SNDRV_DEFAULT_IDX1 : index[itv->instance];
142 	ret = snd_card_new(&itv->pdev->dev,
143 			   idx,
144 			   SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
145 			   THIS_MODULE, 0, &sc);
146 	if (ret) {
147 		IVTV_ALSA_ERR("%s: snd_card_new() failed with err %d\n",
148 			      __func__, ret);
149 		goto err_exit;
150 	}
151 
152 	/* (3) Create a main component */
153 	ret = snd_ivtv_card_create(v4l2_dev, sc, &itvsc);
154 	if (ret) {
155 		IVTV_ALSA_ERR("%s: snd_ivtv_card_create() failed with err %d\n",
156 			      __func__, ret);
157 		goto err_exit_free;
158 	}
159 
160 	/* (4) Set the driver ID and name strings */
161 	snd_ivtv_card_set_names(itvsc);
162 
163 	/* (5) Create other components: mixer, PCM, & proc files */
164 #if 0
165 	ret = snd_ivtv_mixer_create(itvsc);
166 	if (ret) {
167 		IVTV_ALSA_WARN("%s: snd_ivtv_mixer_create() failed with err %d: proceeding anyway\n",
168 			       __func__, ret);
169 	}
170 #endif
171 
172 	ret = snd_ivtv_pcm_create(itvsc);
173 	if (ret) {
174 		IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
175 			      __func__, ret);
176 		goto err_exit_free;
177 	}
178 	/* FIXME - proc files */
179 
180 	/* (7) Set the driver data and return 0 */
181 	/* We do this out of normal order for PCI drivers to avoid races */
182 	itv->alsa = itvsc;
183 
184 	/* (6) Register the card instance */
185 	ret = snd_card_register(sc);
186 	if (ret) {
187 		itv->alsa = NULL;
188 		IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
189 			      __func__, ret);
190 		goto err_exit_free;
191 	}
192 
193 	IVTV_ALSA_INFO("%s: Instance %d registered as ALSA card %d\n",
194 			 __func__, itv->instance, sc->number);
195 
196 	return 0;
197 
198 err_exit_free:
199 	if (sc != NULL)
200 		snd_card_free(sc);
201 	kfree(itvsc);
202 err_exit:
203 	return ret;
204 }
205 
206 static int ivtv_alsa_load(struct ivtv *itv)
207 {
208 	struct v4l2_device *v4l2_dev = &itv->v4l2_dev;
209 	struct ivtv_stream *s;
210 
211 	if (v4l2_dev == NULL) {
212 		pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
213 		       __func__);
214 		return 0;
215 	}
216 
217 	itv = to_ivtv(v4l2_dev);
218 	if (itv == NULL) {
219 		pr_err("ivtv-alsa itv is NULL\n");
220 		return 0;
221 	}
222 
223 	s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
224 	if (s->vdev.v4l2_dev == NULL) {
225 		IVTV_DEBUG_ALSA_INFO("PCM stream for card is disabled - skipping\n");
226 		return 0;
227 	}
228 
229 	if (itv->alsa != NULL) {
230 		IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n",
231 			      __func__);
232 		return 0;
233 	}
234 
235 	if (snd_ivtv_init(v4l2_dev)) {
236 		IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n",
237 			      __func__);
238 	} else {
239 		IVTV_DEBUG_ALSA_INFO("created ivtv ALSA interface instance\n");
240 	}
241 	return 0;
242 }
243 
244 static int __init ivtv_alsa_init(void)
245 {
246 	pr_info("ivtv-alsa: module loading...\n");
247 	ivtv_ext_init = &ivtv_alsa_load;
248 	return 0;
249 }
250 
251 static void __exit snd_ivtv_exit(struct snd_ivtv_card *itvsc)
252 {
253 	struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
254 
255 	/* FIXME - pointer checks & shutdown itvsc */
256 
257 	snd_card_free(itvsc->sc);
258 	itv->alsa = NULL;
259 }
260 
261 static int __exit ivtv_alsa_exit_callback(struct device *dev, void *data)
262 {
263 	struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
264 	struct snd_ivtv_card *itvsc;
265 
266 	if (v4l2_dev == NULL) {
267 		pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
268 		       __func__);
269 		return 0;
270 	}
271 
272 	itvsc = to_snd_ivtv_card(v4l2_dev);
273 	if (itvsc == NULL) {
274 		IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n",
275 			       __func__);
276 		return 0;
277 	}
278 
279 	snd_ivtv_exit(itvsc);
280 	return 0;
281 }
282 
283 static void __exit ivtv_alsa_exit(void)
284 {
285 	struct device_driver *drv;
286 	int ret;
287 
288 	pr_info("ivtv-alsa: module unloading...\n");
289 
290 	drv = driver_find("ivtv", &pci_bus_type);
291 	ret = driver_for_each_device(drv, NULL, NULL, ivtv_alsa_exit_callback);
292 	(void)ret;	/* suppress compiler warning */
293 
294 	ivtv_ext_init = NULL;
295 	pr_info("ivtv-alsa: module unload complete\n");
296 }
297 
298 module_init(ivtv_alsa_init);
299 module_exit(ivtv_alsa_exit);
300