xref: /openbmc/linux/drivers/media/pci/ivtv/ivtv-alsa-main.c (revision bbdba43f202bcd724063524aef1436459af0e5ec)
1269c11fbSAndy Walls /*
2269c11fbSAndy Walls  *  ALSA interface to ivtv PCM capture streams
3269c11fbSAndy Walls  *
4269c11fbSAndy Walls  *  Copyright (C) 2009,2012  Andy Walls <awalls@md.metrocast.net>
5269c11fbSAndy Walls  *  Copyright (C) 2009  Devin Heitmueller <dheitmueller@kernellabs.com>
6269c11fbSAndy Walls  *
7269c11fbSAndy Walls  *  Portions of this work were sponsored by ONELAN Limited for the cx18 driver
8269c11fbSAndy Walls  *
9269c11fbSAndy Walls  *  This program is free software; you can redistribute it and/or modify
10269c11fbSAndy Walls  *  it under the terms of the GNU General Public License as published by
11269c11fbSAndy Walls  *  the Free Software Foundation; either version 2 of the License, or
12269c11fbSAndy Walls  *  (at your option) any later version.
13269c11fbSAndy Walls  *
14269c11fbSAndy Walls  *  This program is distributed in the hope that it will be useful,
15269c11fbSAndy Walls  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16269c11fbSAndy Walls  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17269c11fbSAndy Walls  *  GNU General Public License for more details.
18269c11fbSAndy Walls  */
19269c11fbSAndy Walls 
20269c11fbSAndy Walls #include "ivtv-driver.h"
21269c11fbSAndy Walls #include "ivtv-version.h"
22269c11fbSAndy Walls #include "ivtv-alsa.h"
23269c11fbSAndy Walls #include "ivtv-alsa-mixer.h"
24269c11fbSAndy Walls #include "ivtv-alsa-pcm.h"
25269c11fbSAndy Walls 
26*bbdba43fSMauro Carvalho Chehab #include <sound/core.h>
27*bbdba43fSMauro Carvalho Chehab #include <sound/initval.h>
28*bbdba43fSMauro Carvalho Chehab 
29269c11fbSAndy Walls int ivtv_alsa_debug;
3027315059SNicolas Sugino static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
31269c11fbSAndy Walls 
32269c11fbSAndy Walls #define IVTV_DEBUG_ALSA_INFO(fmt, arg...) \
33269c11fbSAndy Walls 	do { \
34269c11fbSAndy Walls 		if (ivtv_alsa_debug & 2) \
35269c11fbSAndy Walls 			pr_info("%s: " fmt, "ivtv-alsa", ## arg); \
36269c11fbSAndy Walls 	} while (0)
37269c11fbSAndy Walls 
38269c11fbSAndy Walls module_param_named(debug, ivtv_alsa_debug, int, 0644);
39269c11fbSAndy Walls MODULE_PARM_DESC(debug,
40269c11fbSAndy Walls 		 "Debug level (bitmask). Default: 0\n"
41269c11fbSAndy Walls 		 "\t\t\t  1/0x0001: warning\n"
42269c11fbSAndy Walls 		 "\t\t\t  2/0x0002: info\n");
43269c11fbSAndy Walls 
4427315059SNicolas Sugino module_param_array(index, int, NULL, 0444);
4527315059SNicolas Sugino MODULE_PARM_DESC(index,
4627315059SNicolas Sugino 		 "Index value for IVTV ALSA capture interface(s).\n");
4727315059SNicolas Sugino 
48269c11fbSAndy Walls MODULE_AUTHOR("Andy Walls");
49269c11fbSAndy Walls MODULE_DESCRIPTION("CX23415/CX23416 ALSA Interface");
50269c11fbSAndy Walls MODULE_SUPPORTED_DEVICE("CX23415/CX23416 MPEG2 encoder");
51269c11fbSAndy Walls MODULE_LICENSE("GPL");
52269c11fbSAndy Walls 
53269c11fbSAndy Walls MODULE_VERSION(IVTV_VERSION);
54269c11fbSAndy Walls 
55269c11fbSAndy Walls static inline
56269c11fbSAndy Walls struct snd_ivtv_card *to_snd_ivtv_card(struct v4l2_device *v4l2_dev)
57269c11fbSAndy Walls {
58269c11fbSAndy Walls 	return to_ivtv(v4l2_dev)->alsa;
59269c11fbSAndy Walls }
60269c11fbSAndy Walls 
61269c11fbSAndy Walls static inline
62269c11fbSAndy Walls struct snd_ivtv_card *p_to_snd_ivtv_card(struct v4l2_device **v4l2_dev)
63269c11fbSAndy Walls {
64269c11fbSAndy Walls 	return container_of(v4l2_dev, struct snd_ivtv_card, v4l2_dev);
65269c11fbSAndy Walls }
66269c11fbSAndy Walls 
67269c11fbSAndy Walls static void snd_ivtv_card_free(struct snd_ivtv_card *itvsc)
68269c11fbSAndy Walls {
69269c11fbSAndy Walls 	if (itvsc == NULL)
70269c11fbSAndy Walls 		return;
71269c11fbSAndy Walls 
72269c11fbSAndy Walls 	if (itvsc->v4l2_dev != NULL)
73269c11fbSAndy Walls 		to_ivtv(itvsc->v4l2_dev)->alsa = NULL;
74269c11fbSAndy Walls 
75269c11fbSAndy Walls 	/* FIXME - take any other stopping actions needed */
76269c11fbSAndy Walls 
77269c11fbSAndy Walls 	kfree(itvsc);
78269c11fbSAndy Walls }
79269c11fbSAndy Walls 
80269c11fbSAndy Walls static void snd_ivtv_card_private_free(struct snd_card *sc)
81269c11fbSAndy Walls {
82269c11fbSAndy Walls 	if (sc == NULL)
83269c11fbSAndy Walls 		return;
84269c11fbSAndy Walls 	snd_ivtv_card_free(sc->private_data);
85269c11fbSAndy Walls 	sc->private_data = NULL;
86269c11fbSAndy Walls 	sc->private_free = NULL;
87269c11fbSAndy Walls }
88269c11fbSAndy Walls 
89269c11fbSAndy Walls static int snd_ivtv_card_create(struct v4l2_device *v4l2_dev,
90269c11fbSAndy Walls 				       struct snd_card *sc,
91269c11fbSAndy Walls 				       struct snd_ivtv_card **itvsc)
92269c11fbSAndy Walls {
93269c11fbSAndy Walls 	*itvsc = kzalloc(sizeof(struct snd_ivtv_card), GFP_KERNEL);
94269c11fbSAndy Walls 	if (*itvsc == NULL)
95269c11fbSAndy Walls 		return -ENOMEM;
96269c11fbSAndy Walls 
97269c11fbSAndy Walls 	(*itvsc)->v4l2_dev = v4l2_dev;
98269c11fbSAndy Walls 	(*itvsc)->sc = sc;
99269c11fbSAndy Walls 
100269c11fbSAndy Walls 	sc->private_data = *itvsc;
101269c11fbSAndy Walls 	sc->private_free = snd_ivtv_card_private_free;
102269c11fbSAndy Walls 
103269c11fbSAndy Walls 	return 0;
104269c11fbSAndy Walls }
105269c11fbSAndy Walls 
106269c11fbSAndy Walls static int snd_ivtv_card_set_names(struct snd_ivtv_card *itvsc)
107269c11fbSAndy Walls {
108269c11fbSAndy Walls 	struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
109269c11fbSAndy Walls 	struct snd_card *sc = itvsc->sc;
110269c11fbSAndy Walls 
111269c11fbSAndy Walls 	/* sc->driver is used by alsa-lib's configurator: simple, unique */
112269c11fbSAndy Walls 	strlcpy(sc->driver, "CX2341[56]", sizeof(sc->driver));
113269c11fbSAndy Walls 
114269c11fbSAndy Walls 	/* sc->shortname is a symlink in /proc/asound: IVTV-M -> cardN */
115269c11fbSAndy Walls 	snprintf(sc->shortname,  sizeof(sc->shortname), "IVTV-%d",
116269c11fbSAndy Walls 		 itv->instance);
117269c11fbSAndy Walls 
118269c11fbSAndy Walls 	/* sc->longname is read from /proc/asound/cards */
119269c11fbSAndy Walls 	snprintf(sc->longname, sizeof(sc->longname),
120269c11fbSAndy Walls 		 "CX2341[56] #%d %s TV/FM Radio/Line-In Capture",
121269c11fbSAndy Walls 		 itv->instance, itv->card_name);
122269c11fbSAndy Walls 
123269c11fbSAndy Walls 	return 0;
124269c11fbSAndy Walls }
125269c11fbSAndy Walls 
126269c11fbSAndy Walls static int snd_ivtv_init(struct v4l2_device *v4l2_dev)
127269c11fbSAndy Walls {
128269c11fbSAndy Walls 	struct ivtv *itv = to_ivtv(v4l2_dev);
129269c11fbSAndy Walls 	struct snd_card *sc = NULL;
130269c11fbSAndy Walls 	struct snd_ivtv_card *itvsc;
13127315059SNicolas Sugino 	int ret, idx;
132269c11fbSAndy Walls 
133269c11fbSAndy Walls 	/* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
134269c11fbSAndy Walls 
135269c11fbSAndy Walls 	/* (1) Check and increment the device index */
136269c11fbSAndy Walls 	/* This is a no-op for us.  We'll use the itv->instance */
137269c11fbSAndy Walls 
138269c11fbSAndy Walls 	/* (2) Create a card instance */
13927315059SNicolas Sugino 	/* use first available id if not specified otherwise*/
14027315059SNicolas Sugino 	idx = index[itv->instance] == -1 ? SNDRV_DEFAULT_IDX1 : index[itv->instance];
141e7356888STakashi Iwai 	ret = snd_card_new(&itv->pdev->dev,
14227315059SNicolas Sugino 			   idx,
143269c11fbSAndy Walls 			   SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
144269c11fbSAndy Walls 			   THIS_MODULE, 0, &sc);
145269c11fbSAndy Walls 	if (ret) {
146e7356888STakashi Iwai 		IVTV_ALSA_ERR("%s: snd_card_new() failed with err %d\n",
147269c11fbSAndy Walls 			      __func__, ret);
148269c11fbSAndy Walls 		goto err_exit;
149269c11fbSAndy Walls 	}
150269c11fbSAndy Walls 
151269c11fbSAndy Walls 	/* (3) Create a main component */
152269c11fbSAndy Walls 	ret = snd_ivtv_card_create(v4l2_dev, sc, &itvsc);
153269c11fbSAndy Walls 	if (ret) {
154269c11fbSAndy Walls 		IVTV_ALSA_ERR("%s: snd_ivtv_card_create() failed with err %d\n",
155269c11fbSAndy Walls 			      __func__, ret);
156269c11fbSAndy Walls 		goto err_exit_free;
157269c11fbSAndy Walls 	}
158269c11fbSAndy Walls 
159269c11fbSAndy Walls 	/* (4) Set the driver ID and name strings */
160269c11fbSAndy Walls 	snd_ivtv_card_set_names(itvsc);
161269c11fbSAndy Walls 
162269c11fbSAndy Walls 	/* (5) Create other components: mixer, PCM, & proc files */
163269c11fbSAndy Walls #if 0
164269c11fbSAndy Walls 	ret = snd_ivtv_mixer_create(itvsc);
165269c11fbSAndy Walls 	if (ret) {
16667ccf860SMauro Carvalho Chehab 		IVTV_ALSA_WARN("%s: snd_ivtv_mixer_create() failed with err %d: proceeding anyway\n",
16767ccf860SMauro Carvalho Chehab 			       __func__, ret);
168269c11fbSAndy Walls 	}
169269c11fbSAndy Walls #endif
170269c11fbSAndy Walls 
171269c11fbSAndy Walls 	ret = snd_ivtv_pcm_create(itvsc);
172269c11fbSAndy Walls 	if (ret) {
173269c11fbSAndy Walls 		IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
174269c11fbSAndy Walls 			      __func__, ret);
175269c11fbSAndy Walls 		goto err_exit_free;
176269c11fbSAndy Walls 	}
177269c11fbSAndy Walls 	/* FIXME - proc files */
178269c11fbSAndy Walls 
179269c11fbSAndy Walls 	/* (7) Set the driver data and return 0 */
180269c11fbSAndy Walls 	/* We do this out of normal order for PCI drivers to avoid races */
181269c11fbSAndy Walls 	itv->alsa = itvsc;
182269c11fbSAndy Walls 
183269c11fbSAndy Walls 	/* (6) Register the card instance */
184269c11fbSAndy Walls 	ret = snd_card_register(sc);
185269c11fbSAndy Walls 	if (ret) {
186269c11fbSAndy Walls 		itv->alsa = NULL;
187269c11fbSAndy Walls 		IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
188269c11fbSAndy Walls 			      __func__, ret);
189269c11fbSAndy Walls 		goto err_exit_free;
190269c11fbSAndy Walls 	}
191269c11fbSAndy Walls 
19227315059SNicolas Sugino 	IVTV_ALSA_INFO("%s: Instance %d registered as ALSA card %d\n",
19327315059SNicolas Sugino 			 __func__, itv->instance, sc->number);
19427315059SNicolas Sugino 
195269c11fbSAndy Walls 	return 0;
196269c11fbSAndy Walls 
197269c11fbSAndy Walls err_exit_free:
198269c11fbSAndy Walls 	if (sc != NULL)
199269c11fbSAndy Walls 		snd_card_free(sc);
200269c11fbSAndy Walls 	kfree(itvsc);
201269c11fbSAndy Walls err_exit:
202269c11fbSAndy Walls 	return ret;
203269c11fbSAndy Walls }
204269c11fbSAndy Walls 
205cfb046cbSHans Verkuil static int ivtv_alsa_load(struct ivtv *itv)
206269c11fbSAndy Walls {
207269c11fbSAndy Walls 	struct v4l2_device *v4l2_dev = &itv->v4l2_dev;
208269c11fbSAndy Walls 	struct ivtv_stream *s;
209269c11fbSAndy Walls 
210269c11fbSAndy Walls 	if (v4l2_dev == NULL) {
211269c11fbSAndy Walls 		pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
212269c11fbSAndy Walls 		       __func__);
213269c11fbSAndy Walls 		return 0;
214269c11fbSAndy Walls 	}
215269c11fbSAndy Walls 
216269c11fbSAndy Walls 	itv = to_ivtv(v4l2_dev);
217269c11fbSAndy Walls 	if (itv == NULL) {
218269c11fbSAndy Walls 		pr_err("ivtv-alsa itv is NULL\n");
219269c11fbSAndy Walls 		return 0;
220269c11fbSAndy Walls 	}
221269c11fbSAndy Walls 
222269c11fbSAndy Walls 	s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
223635d62f0SHans Verkuil 	if (s->vdev.v4l2_dev == NULL) {
22467ccf860SMauro Carvalho Chehab 		IVTV_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - skipping\n",
22567ccf860SMauro Carvalho Chehab 				     __func__);
226269c11fbSAndy Walls 		return 0;
227269c11fbSAndy Walls 	}
228269c11fbSAndy Walls 
229269c11fbSAndy Walls 	if (itv->alsa != NULL) {
230269c11fbSAndy Walls 		IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n",
231269c11fbSAndy Walls 			      __func__);
232269c11fbSAndy Walls 		return 0;
233269c11fbSAndy Walls 	}
234269c11fbSAndy Walls 
235269c11fbSAndy Walls 	if (snd_ivtv_init(v4l2_dev)) {
236269c11fbSAndy Walls 		IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n",
237269c11fbSAndy Walls 			      __func__);
238269c11fbSAndy Walls 	} else {
23967ccf860SMauro Carvalho Chehab 		IVTV_DEBUG_ALSA_INFO("%s: created ivtv ALSA interface instance \n",
24067ccf860SMauro Carvalho Chehab 				     __func__);
241269c11fbSAndy Walls 	}
242269c11fbSAndy Walls 	return 0;
243269c11fbSAndy Walls }
244269c11fbSAndy Walls 
245269c11fbSAndy Walls static int __init ivtv_alsa_init(void)
246269c11fbSAndy Walls {
247269c11fbSAndy Walls 	pr_info("ivtv-alsa: module loading...\n");
248269c11fbSAndy Walls 	ivtv_ext_init = &ivtv_alsa_load;
249269c11fbSAndy Walls 	return 0;
250269c11fbSAndy Walls }
251269c11fbSAndy Walls 
252269c11fbSAndy Walls static void __exit snd_ivtv_exit(struct snd_ivtv_card *itvsc)
253269c11fbSAndy Walls {
254269c11fbSAndy Walls 	struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
255269c11fbSAndy Walls 
256269c11fbSAndy Walls 	/* FIXME - pointer checks & shutdown itvsc */
257269c11fbSAndy Walls 
258269c11fbSAndy Walls 	snd_card_free(itvsc->sc);
259269c11fbSAndy Walls 	itv->alsa = NULL;
260269c11fbSAndy Walls }
261269c11fbSAndy Walls 
262269c11fbSAndy Walls static int __exit ivtv_alsa_exit_callback(struct device *dev, void *data)
263269c11fbSAndy Walls {
264269c11fbSAndy Walls 	struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
265269c11fbSAndy Walls 	struct snd_ivtv_card *itvsc;
266269c11fbSAndy Walls 
267269c11fbSAndy Walls 	if (v4l2_dev == NULL) {
268269c11fbSAndy Walls 		pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
269269c11fbSAndy Walls 		       __func__);
270269c11fbSAndy Walls 		return 0;
271269c11fbSAndy Walls 	}
272269c11fbSAndy Walls 
273269c11fbSAndy Walls 	itvsc = to_snd_ivtv_card(v4l2_dev);
274269c11fbSAndy Walls 	if (itvsc == NULL) {
275269c11fbSAndy Walls 		IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n",
276269c11fbSAndy Walls 			       __func__);
277269c11fbSAndy Walls 		return 0;
278269c11fbSAndy Walls 	}
279269c11fbSAndy Walls 
280269c11fbSAndy Walls 	snd_ivtv_exit(itvsc);
281269c11fbSAndy Walls 	return 0;
282269c11fbSAndy Walls }
283269c11fbSAndy Walls 
284269c11fbSAndy Walls static void __exit ivtv_alsa_exit(void)
285269c11fbSAndy Walls {
286269c11fbSAndy Walls 	struct device_driver *drv;
287269c11fbSAndy Walls 	int ret;
288269c11fbSAndy Walls 
289269c11fbSAndy Walls 	pr_info("ivtv-alsa: module unloading...\n");
290269c11fbSAndy Walls 
291269c11fbSAndy Walls 	drv = driver_find("ivtv", &pci_bus_type);
292269c11fbSAndy Walls 	ret = driver_for_each_device(drv, NULL, NULL, ivtv_alsa_exit_callback);
293269c11fbSAndy Walls 	(void)ret;	/* suppress compiler warning */
294269c11fbSAndy Walls 
295269c11fbSAndy Walls 	ivtv_ext_init = NULL;
296269c11fbSAndy Walls 	pr_info("ivtv-alsa: module unload complete\n");
297269c11fbSAndy Walls }
298269c11fbSAndy Walls 
299269c11fbSAndy Walls module_init(ivtv_alsa_init);
300269c11fbSAndy Walls module_exit(ivtv_alsa_exit);
301