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