1*c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2269c11fbSAndy Walls /*
3269c11fbSAndy Walls * ALSA interface to ivtv PCM capture streams
4269c11fbSAndy Walls *
5269c11fbSAndy Walls * Copyright (C) 2009,2012 Andy Walls <awalls@md.metrocast.net>
6269c11fbSAndy Walls * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
7269c11fbSAndy Walls *
8269c11fbSAndy Walls * Portions of this work were sponsored by ONELAN Limited for the cx18 driver
9269c11fbSAndy Walls */
10269c11fbSAndy Walls
11269c11fbSAndy Walls #include "ivtv-driver.h"
12269c11fbSAndy Walls #include "ivtv-version.h"
13269c11fbSAndy Walls #include "ivtv-alsa.h"
14269c11fbSAndy Walls #include "ivtv-alsa-pcm.h"
15269c11fbSAndy Walls
16bbdba43fSMauro Carvalho Chehab #include <sound/core.h>
17bbdba43fSMauro Carvalho Chehab #include <sound/initval.h>
18bbdba43fSMauro Carvalho Chehab
19269c11fbSAndy Walls int ivtv_alsa_debug;
2027315059SNicolas Sugino static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
21269c11fbSAndy Walls
22f57e9618SMauro Carvalho Chehab #define IVTV_DEBUG_ALSA_INFO(__fmt, __arg...) \
23269c11fbSAndy Walls do { \
24269c11fbSAndy Walls if (ivtv_alsa_debug & 2) \
25f57e9618SMauro Carvalho Chehab printk(KERN_INFO pr_fmt("%s: alsa:" __fmt), \
26f57e9618SMauro Carvalho Chehab __func__, ##__arg); \
27269c11fbSAndy Walls } while (0)
28269c11fbSAndy Walls
29269c11fbSAndy Walls module_param_named(debug, ivtv_alsa_debug, int, 0644);
30269c11fbSAndy Walls MODULE_PARM_DESC(debug,
31269c11fbSAndy Walls "Debug level (bitmask). Default: 0\n"
32269c11fbSAndy Walls "\t\t\t 1/0x0001: warning\n"
33269c11fbSAndy Walls "\t\t\t 2/0x0002: info\n");
34269c11fbSAndy Walls
3527315059SNicolas Sugino module_param_array(index, int, NULL, 0444);
3627315059SNicolas Sugino MODULE_PARM_DESC(index,
3727315059SNicolas Sugino "Index value for IVTV ALSA capture interface(s).\n");
3827315059SNicolas Sugino
39269c11fbSAndy Walls MODULE_AUTHOR("Andy Walls");
40269c11fbSAndy Walls MODULE_DESCRIPTION("CX23415/CX23416 ALSA Interface");
41269c11fbSAndy Walls MODULE_LICENSE("GPL");
42269c11fbSAndy Walls
43269c11fbSAndy Walls MODULE_VERSION(IVTV_VERSION);
44269c11fbSAndy Walls
45269c11fbSAndy Walls static inline
to_snd_ivtv_card(struct v4l2_device * v4l2_dev)46269c11fbSAndy Walls struct snd_ivtv_card *to_snd_ivtv_card(struct v4l2_device *v4l2_dev)
47269c11fbSAndy Walls {
48269c11fbSAndy Walls return to_ivtv(v4l2_dev)->alsa;
49269c11fbSAndy Walls }
50269c11fbSAndy Walls
snd_ivtv_card_free(struct snd_ivtv_card * itvsc)51269c11fbSAndy Walls static void snd_ivtv_card_free(struct snd_ivtv_card *itvsc)
52269c11fbSAndy Walls {
53269c11fbSAndy Walls if (itvsc == NULL)
54269c11fbSAndy Walls return;
55269c11fbSAndy Walls
56269c11fbSAndy Walls if (itvsc->v4l2_dev != NULL)
57269c11fbSAndy Walls to_ivtv(itvsc->v4l2_dev)->alsa = NULL;
58269c11fbSAndy Walls
59269c11fbSAndy Walls /* FIXME - take any other stopping actions needed */
60269c11fbSAndy Walls
61269c11fbSAndy Walls kfree(itvsc);
62269c11fbSAndy Walls }
63269c11fbSAndy Walls
snd_ivtv_card_private_free(struct snd_card * sc)64269c11fbSAndy Walls static void snd_ivtv_card_private_free(struct snd_card *sc)
65269c11fbSAndy Walls {
66269c11fbSAndy Walls if (sc == NULL)
67269c11fbSAndy Walls return;
68269c11fbSAndy Walls snd_ivtv_card_free(sc->private_data);
69269c11fbSAndy Walls sc->private_data = NULL;
70269c11fbSAndy Walls sc->private_free = NULL;
71269c11fbSAndy Walls }
72269c11fbSAndy Walls
snd_ivtv_card_create(struct v4l2_device * v4l2_dev,struct snd_card * sc,struct snd_ivtv_card ** itvsc)73269c11fbSAndy Walls static int snd_ivtv_card_create(struct v4l2_device *v4l2_dev,
74269c11fbSAndy Walls struct snd_card *sc,
75269c11fbSAndy Walls struct snd_ivtv_card **itvsc)
76269c11fbSAndy Walls {
77269c11fbSAndy Walls *itvsc = kzalloc(sizeof(struct snd_ivtv_card), GFP_KERNEL);
78269c11fbSAndy Walls if (*itvsc == NULL)
79269c11fbSAndy Walls return -ENOMEM;
80269c11fbSAndy Walls
81269c11fbSAndy Walls (*itvsc)->v4l2_dev = v4l2_dev;
82269c11fbSAndy Walls (*itvsc)->sc = sc;
83269c11fbSAndy Walls
84269c11fbSAndy Walls sc->private_data = *itvsc;
85269c11fbSAndy Walls sc->private_free = snd_ivtv_card_private_free;
86269c11fbSAndy Walls
87269c11fbSAndy Walls return 0;
88269c11fbSAndy Walls }
89269c11fbSAndy Walls
snd_ivtv_card_set_names(struct snd_ivtv_card * itvsc)90269c11fbSAndy Walls static int snd_ivtv_card_set_names(struct snd_ivtv_card *itvsc)
91269c11fbSAndy Walls {
92269c11fbSAndy Walls struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
93269c11fbSAndy Walls struct snd_card *sc = itvsc->sc;
94269c11fbSAndy Walls
95269c11fbSAndy Walls /* sc->driver is used by alsa-lib's configurator: simple, unique */
96c0decac1SMauro Carvalho Chehab strscpy(sc->driver, "CX2341[56]", sizeof(sc->driver));
97269c11fbSAndy Walls
98269c11fbSAndy Walls /* sc->shortname is a symlink in /proc/asound: IVTV-M -> cardN */
99269c11fbSAndy Walls snprintf(sc->shortname, sizeof(sc->shortname), "IVTV-%d",
100269c11fbSAndy Walls itv->instance);
101269c11fbSAndy Walls
102269c11fbSAndy Walls /* sc->longname is read from /proc/asound/cards */
103269c11fbSAndy Walls snprintf(sc->longname, sizeof(sc->longname),
104269c11fbSAndy Walls "CX2341[56] #%d %s TV/FM Radio/Line-In Capture",
105269c11fbSAndy Walls itv->instance, itv->card_name);
106269c11fbSAndy Walls
107269c11fbSAndy Walls return 0;
108269c11fbSAndy Walls }
109269c11fbSAndy Walls
snd_ivtv_init(struct v4l2_device * v4l2_dev)110269c11fbSAndy Walls static int snd_ivtv_init(struct v4l2_device *v4l2_dev)
111269c11fbSAndy Walls {
112269c11fbSAndy Walls struct ivtv *itv = to_ivtv(v4l2_dev);
113269c11fbSAndy Walls struct snd_card *sc = NULL;
114269c11fbSAndy Walls struct snd_ivtv_card *itvsc;
11527315059SNicolas Sugino int ret, idx;
116269c11fbSAndy Walls
117269c11fbSAndy Walls /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
118269c11fbSAndy Walls
119269c11fbSAndy Walls /* (1) Check and increment the device index */
120269c11fbSAndy Walls /* This is a no-op for us. We'll use the itv->instance */
121269c11fbSAndy Walls
122269c11fbSAndy Walls /* (2) Create a card instance */
12327315059SNicolas Sugino /* use first available id if not specified otherwise*/
12427315059SNicolas Sugino idx = index[itv->instance] == -1 ? SNDRV_DEFAULT_IDX1 : index[itv->instance];
125e7356888STakashi Iwai ret = snd_card_new(&itv->pdev->dev,
12627315059SNicolas Sugino idx,
127269c11fbSAndy Walls SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
128269c11fbSAndy Walls THIS_MODULE, 0, &sc);
129269c11fbSAndy Walls if (ret) {
130e7356888STakashi Iwai IVTV_ALSA_ERR("%s: snd_card_new() failed with err %d\n",
131269c11fbSAndy Walls __func__, ret);
132269c11fbSAndy Walls goto err_exit;
133269c11fbSAndy Walls }
134269c11fbSAndy Walls
135269c11fbSAndy Walls /* (3) Create a main component */
136269c11fbSAndy Walls ret = snd_ivtv_card_create(v4l2_dev, sc, &itvsc);
137269c11fbSAndy Walls if (ret) {
138269c11fbSAndy Walls IVTV_ALSA_ERR("%s: snd_ivtv_card_create() failed with err %d\n",
139269c11fbSAndy Walls __func__, ret);
140269c11fbSAndy Walls goto err_exit_free;
141269c11fbSAndy Walls }
142269c11fbSAndy Walls
143269c11fbSAndy Walls /* (4) Set the driver ID and name strings */
144269c11fbSAndy Walls snd_ivtv_card_set_names(itvsc);
145269c11fbSAndy Walls
14647b93487SCorentin Labbe /* (5) Create other components: PCM, & proc files */
147269c11fbSAndy Walls ret = snd_ivtv_pcm_create(itvsc);
148269c11fbSAndy Walls if (ret) {
149269c11fbSAndy Walls IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
150269c11fbSAndy Walls __func__, ret);
151269c11fbSAndy Walls goto err_exit_free;
152269c11fbSAndy Walls }
153269c11fbSAndy Walls /* FIXME - proc files */
154269c11fbSAndy Walls
155269c11fbSAndy Walls /* (7) Set the driver data and return 0 */
156269c11fbSAndy Walls /* We do this out of normal order for PCI drivers to avoid races */
157269c11fbSAndy Walls itv->alsa = itvsc;
158269c11fbSAndy Walls
159269c11fbSAndy Walls /* (6) Register the card instance */
160269c11fbSAndy Walls ret = snd_card_register(sc);
161269c11fbSAndy Walls if (ret) {
162269c11fbSAndy Walls itv->alsa = NULL;
163269c11fbSAndy Walls IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
164269c11fbSAndy Walls __func__, ret);
165269c11fbSAndy Walls goto err_exit_free;
166269c11fbSAndy Walls }
167269c11fbSAndy Walls
16827315059SNicolas Sugino IVTV_ALSA_INFO("%s: Instance %d registered as ALSA card %d\n",
16927315059SNicolas Sugino __func__, itv->instance, sc->number);
17027315059SNicolas Sugino
171269c11fbSAndy Walls return 0;
172269c11fbSAndy Walls
173269c11fbSAndy Walls err_exit_free:
174269c11fbSAndy Walls if (sc != NULL)
175269c11fbSAndy Walls snd_card_free(sc);
176269c11fbSAndy Walls kfree(itvsc);
177269c11fbSAndy Walls err_exit:
178269c11fbSAndy Walls return ret;
179269c11fbSAndy Walls }
180269c11fbSAndy Walls
ivtv_alsa_load(struct ivtv * itv)181cfb046cbSHans Verkuil static int ivtv_alsa_load(struct ivtv *itv)
182269c11fbSAndy Walls {
183269c11fbSAndy Walls struct v4l2_device *v4l2_dev = &itv->v4l2_dev;
184269c11fbSAndy Walls struct ivtv_stream *s;
185269c11fbSAndy Walls
186269c11fbSAndy Walls if (v4l2_dev == NULL) {
187269c11fbSAndy Walls pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
188269c11fbSAndy Walls __func__);
189269c11fbSAndy Walls return 0;
190269c11fbSAndy Walls }
191269c11fbSAndy Walls
192269c11fbSAndy Walls itv = to_ivtv(v4l2_dev);
193269c11fbSAndy Walls if (itv == NULL) {
194269c11fbSAndy Walls pr_err("ivtv-alsa itv is NULL\n");
195269c11fbSAndy Walls return 0;
196269c11fbSAndy Walls }
197269c11fbSAndy Walls
198269c11fbSAndy Walls s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
199635d62f0SHans Verkuil if (s->vdev.v4l2_dev == NULL) {
200f57e9618SMauro Carvalho Chehab IVTV_DEBUG_ALSA_INFO("PCM stream for card is disabled - skipping\n");
201269c11fbSAndy Walls return 0;
202269c11fbSAndy Walls }
203269c11fbSAndy Walls
204269c11fbSAndy Walls if (itv->alsa != NULL) {
205269c11fbSAndy Walls IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n",
206269c11fbSAndy Walls __func__);
207269c11fbSAndy Walls return 0;
208269c11fbSAndy Walls }
209269c11fbSAndy Walls
210269c11fbSAndy Walls if (snd_ivtv_init(v4l2_dev)) {
211269c11fbSAndy Walls IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n",
212269c11fbSAndy Walls __func__);
213269c11fbSAndy Walls } else {
214f57e9618SMauro Carvalho Chehab IVTV_DEBUG_ALSA_INFO("created ivtv ALSA interface instance\n");
215269c11fbSAndy Walls }
216269c11fbSAndy Walls return 0;
217269c11fbSAndy Walls }
218269c11fbSAndy Walls
ivtv_alsa_init(void)219269c11fbSAndy Walls static int __init ivtv_alsa_init(void)
220269c11fbSAndy Walls {
221269c11fbSAndy Walls pr_info("ivtv-alsa: module loading...\n");
222269c11fbSAndy Walls ivtv_ext_init = &ivtv_alsa_load;
223269c11fbSAndy Walls return 0;
224269c11fbSAndy Walls }
225269c11fbSAndy Walls
snd_ivtv_exit(struct snd_ivtv_card * itvsc)226269c11fbSAndy Walls static void __exit snd_ivtv_exit(struct snd_ivtv_card *itvsc)
227269c11fbSAndy Walls {
228269c11fbSAndy Walls struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
229269c11fbSAndy Walls
230269c11fbSAndy Walls /* FIXME - pointer checks & shutdown itvsc */
231269c11fbSAndy Walls
232269c11fbSAndy Walls snd_card_free(itvsc->sc);
233269c11fbSAndy Walls itv->alsa = NULL;
234269c11fbSAndy Walls }
235269c11fbSAndy Walls
ivtv_alsa_exit_callback(struct device * dev,void * data)236269c11fbSAndy Walls static int __exit ivtv_alsa_exit_callback(struct device *dev, void *data)
237269c11fbSAndy Walls {
238269c11fbSAndy Walls struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
239269c11fbSAndy Walls struct snd_ivtv_card *itvsc;
240269c11fbSAndy Walls
241269c11fbSAndy Walls if (v4l2_dev == NULL) {
242269c11fbSAndy Walls pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
243269c11fbSAndy Walls __func__);
244269c11fbSAndy Walls return 0;
245269c11fbSAndy Walls }
246269c11fbSAndy Walls
247269c11fbSAndy Walls itvsc = to_snd_ivtv_card(v4l2_dev);
248269c11fbSAndy Walls if (itvsc == NULL) {
249269c11fbSAndy Walls IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n",
250269c11fbSAndy Walls __func__);
251269c11fbSAndy Walls return 0;
252269c11fbSAndy Walls }
253269c11fbSAndy Walls
254269c11fbSAndy Walls snd_ivtv_exit(itvsc);
255269c11fbSAndy Walls return 0;
256269c11fbSAndy Walls }
257269c11fbSAndy Walls
ivtv_alsa_exit(void)258269c11fbSAndy Walls static void __exit ivtv_alsa_exit(void)
259269c11fbSAndy Walls {
260269c11fbSAndy Walls struct device_driver *drv;
261269c11fbSAndy Walls int ret;
262269c11fbSAndy Walls
263269c11fbSAndy Walls pr_info("ivtv-alsa: module unloading...\n");
264269c11fbSAndy Walls
265269c11fbSAndy Walls drv = driver_find("ivtv", &pci_bus_type);
266269c11fbSAndy Walls ret = driver_for_each_device(drv, NULL, NULL, ivtv_alsa_exit_callback);
267269c11fbSAndy Walls (void)ret; /* suppress compiler warning */
268269c11fbSAndy Walls
269269c11fbSAndy Walls ivtv_ext_init = NULL;
270269c11fbSAndy Walls pr_info("ivtv-alsa: module unload complete\n");
271269c11fbSAndy Walls }
272269c11fbSAndy Walls
273269c11fbSAndy Walls module_init(ivtv_alsa_init);
274269c11fbSAndy Walls module_exit(ivtv_alsa_exit);
275