xref: /openbmc/linux/drivers/media/pci/ivtv/ivtv-alsa-main.c (revision 47b934875b00fefe16a2b8a9daa88eb82be169a8)
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-pcm.h"
24269c11fbSAndy Walls 
25bbdba43fSMauro Carvalho Chehab #include <sound/core.h>
26bbdba43fSMauro Carvalho Chehab #include <sound/initval.h>
27bbdba43fSMauro Carvalho Chehab 
28269c11fbSAndy Walls int ivtv_alsa_debug;
2927315059SNicolas Sugino static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
30269c11fbSAndy Walls 
31f57e9618SMauro Carvalho Chehab #define IVTV_DEBUG_ALSA_INFO(__fmt, __arg...) \
32269c11fbSAndy Walls 	do { \
33269c11fbSAndy Walls 		if (ivtv_alsa_debug & 2) \
34f57e9618SMauro Carvalho Chehab 			printk(KERN_INFO pr_fmt("%s: alsa:" __fmt),	\
35f57e9618SMauro Carvalho Chehab 			       __func__, ##__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 
162*47b93487SCorentin Labbe 	/* (5) Create other components: PCM, & proc files */
163269c11fbSAndy Walls 	ret = snd_ivtv_pcm_create(itvsc);
164269c11fbSAndy Walls 	if (ret) {
165269c11fbSAndy Walls 		IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
166269c11fbSAndy Walls 			      __func__, ret);
167269c11fbSAndy Walls 		goto err_exit_free;
168269c11fbSAndy Walls 	}
169269c11fbSAndy Walls 	/* FIXME - proc files */
170269c11fbSAndy Walls 
171269c11fbSAndy Walls 	/* (7) Set the driver data and return 0 */
172269c11fbSAndy Walls 	/* We do this out of normal order for PCI drivers to avoid races */
173269c11fbSAndy Walls 	itv->alsa = itvsc;
174269c11fbSAndy Walls 
175269c11fbSAndy Walls 	/* (6) Register the card instance */
176269c11fbSAndy Walls 	ret = snd_card_register(sc);
177269c11fbSAndy Walls 	if (ret) {
178269c11fbSAndy Walls 		itv->alsa = NULL;
179269c11fbSAndy Walls 		IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
180269c11fbSAndy Walls 			      __func__, ret);
181269c11fbSAndy Walls 		goto err_exit_free;
182269c11fbSAndy Walls 	}
183269c11fbSAndy Walls 
18427315059SNicolas Sugino 	IVTV_ALSA_INFO("%s: Instance %d registered as ALSA card %d\n",
18527315059SNicolas Sugino 			 __func__, itv->instance, sc->number);
18627315059SNicolas Sugino 
187269c11fbSAndy Walls 	return 0;
188269c11fbSAndy Walls 
189269c11fbSAndy Walls err_exit_free:
190269c11fbSAndy Walls 	if (sc != NULL)
191269c11fbSAndy Walls 		snd_card_free(sc);
192269c11fbSAndy Walls 	kfree(itvsc);
193269c11fbSAndy Walls err_exit:
194269c11fbSAndy Walls 	return ret;
195269c11fbSAndy Walls }
196269c11fbSAndy Walls 
197cfb046cbSHans Verkuil static int ivtv_alsa_load(struct ivtv *itv)
198269c11fbSAndy Walls {
199269c11fbSAndy Walls 	struct v4l2_device *v4l2_dev = &itv->v4l2_dev;
200269c11fbSAndy Walls 	struct ivtv_stream *s;
201269c11fbSAndy Walls 
202269c11fbSAndy Walls 	if (v4l2_dev == NULL) {
203269c11fbSAndy Walls 		pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
204269c11fbSAndy Walls 		       __func__);
205269c11fbSAndy Walls 		return 0;
206269c11fbSAndy Walls 	}
207269c11fbSAndy Walls 
208269c11fbSAndy Walls 	itv = to_ivtv(v4l2_dev);
209269c11fbSAndy Walls 	if (itv == NULL) {
210269c11fbSAndy Walls 		pr_err("ivtv-alsa itv is NULL\n");
211269c11fbSAndy Walls 		return 0;
212269c11fbSAndy Walls 	}
213269c11fbSAndy Walls 
214269c11fbSAndy Walls 	s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
215635d62f0SHans Verkuil 	if (s->vdev.v4l2_dev == NULL) {
216f57e9618SMauro Carvalho Chehab 		IVTV_DEBUG_ALSA_INFO("PCM stream for card is disabled - skipping\n");
217269c11fbSAndy Walls 		return 0;
218269c11fbSAndy Walls 	}
219269c11fbSAndy Walls 
220269c11fbSAndy Walls 	if (itv->alsa != NULL) {
221269c11fbSAndy Walls 		IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n",
222269c11fbSAndy Walls 			      __func__);
223269c11fbSAndy Walls 		return 0;
224269c11fbSAndy Walls 	}
225269c11fbSAndy Walls 
226269c11fbSAndy Walls 	if (snd_ivtv_init(v4l2_dev)) {
227269c11fbSAndy Walls 		IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n",
228269c11fbSAndy Walls 			      __func__);
229269c11fbSAndy Walls 	} else {
230f57e9618SMauro Carvalho Chehab 		IVTV_DEBUG_ALSA_INFO("created ivtv ALSA interface instance\n");
231269c11fbSAndy Walls 	}
232269c11fbSAndy Walls 	return 0;
233269c11fbSAndy Walls }
234269c11fbSAndy Walls 
235269c11fbSAndy Walls static int __init ivtv_alsa_init(void)
236269c11fbSAndy Walls {
237269c11fbSAndy Walls 	pr_info("ivtv-alsa: module loading...\n");
238269c11fbSAndy Walls 	ivtv_ext_init = &ivtv_alsa_load;
239269c11fbSAndy Walls 	return 0;
240269c11fbSAndy Walls }
241269c11fbSAndy Walls 
242269c11fbSAndy Walls static void __exit snd_ivtv_exit(struct snd_ivtv_card *itvsc)
243269c11fbSAndy Walls {
244269c11fbSAndy Walls 	struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
245269c11fbSAndy Walls 
246269c11fbSAndy Walls 	/* FIXME - pointer checks & shutdown itvsc */
247269c11fbSAndy Walls 
248269c11fbSAndy Walls 	snd_card_free(itvsc->sc);
249269c11fbSAndy Walls 	itv->alsa = NULL;
250269c11fbSAndy Walls }
251269c11fbSAndy Walls 
252269c11fbSAndy Walls static int __exit ivtv_alsa_exit_callback(struct device *dev, void *data)
253269c11fbSAndy Walls {
254269c11fbSAndy Walls 	struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
255269c11fbSAndy Walls 	struct snd_ivtv_card *itvsc;
256269c11fbSAndy Walls 
257269c11fbSAndy Walls 	if (v4l2_dev == NULL) {
258269c11fbSAndy Walls 		pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
259269c11fbSAndy Walls 		       __func__);
260269c11fbSAndy Walls 		return 0;
261269c11fbSAndy Walls 	}
262269c11fbSAndy Walls 
263269c11fbSAndy Walls 	itvsc = to_snd_ivtv_card(v4l2_dev);
264269c11fbSAndy Walls 	if (itvsc == NULL) {
265269c11fbSAndy Walls 		IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n",
266269c11fbSAndy Walls 			       __func__);
267269c11fbSAndy Walls 		return 0;
268269c11fbSAndy Walls 	}
269269c11fbSAndy Walls 
270269c11fbSAndy Walls 	snd_ivtv_exit(itvsc);
271269c11fbSAndy Walls 	return 0;
272269c11fbSAndy Walls }
273269c11fbSAndy Walls 
274269c11fbSAndy Walls static void __exit ivtv_alsa_exit(void)
275269c11fbSAndy Walls {
276269c11fbSAndy Walls 	struct device_driver *drv;
277269c11fbSAndy Walls 	int ret;
278269c11fbSAndy Walls 
279269c11fbSAndy Walls 	pr_info("ivtv-alsa: module unloading...\n");
280269c11fbSAndy Walls 
281269c11fbSAndy Walls 	drv = driver_find("ivtv", &pci_bus_type);
282269c11fbSAndy Walls 	ret = driver_for_each_device(drv, NULL, NULL, ivtv_alsa_exit_callback);
283269c11fbSAndy Walls 	(void)ret;	/* suppress compiler warning */
284269c11fbSAndy Walls 
285269c11fbSAndy Walls 	ivtv_ext_init = NULL;
286269c11fbSAndy Walls 	pr_info("ivtv-alsa: module unload complete\n");
287269c11fbSAndy Walls }
288269c11fbSAndy Walls 
289269c11fbSAndy Walls module_init(ivtv_alsa_init);
290269c11fbSAndy Walls module_exit(ivtv_alsa_exit);
291