1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ALSA interface to ivtv PCM capture streams
5 * Copyright (C) 2009,2012 Andy Walls <awalls@md.metrocast.net>
6 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
8 * Portions of this work were sponsored by ONELAN Limited for the cx18 driver
11 #include "ivtv-driver.h"
12 #include "ivtv-version.h"
13 #include "ivtv-alsa.h"
14 #include "ivtv-alsa-pcm.h"
16 #include <sound/core.h>
17 #include <sound/initval.h>
20 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
22 #define IVTV_DEBUG_ALSA_INFO(__fmt, __arg...) \
24 if (ivtv_alsa_debug & 2) \
25 printk(KERN_INFO pr_fmt("%s: alsa:" __fmt), \
29 module_param_named(debug
, ivtv_alsa_debug
, int, 0644);
30 MODULE_PARM_DESC(debug
,
31 "Debug level (bitmask). Default: 0\n"
32 "\t\t\t 1/0x0001: warning\n"
33 "\t\t\t 2/0x0002: info\n");
35 module_param_array(index
, int, NULL
, 0444);
36 MODULE_PARM_DESC(index
,
37 "Index value for IVTV ALSA capture interface(s).\n");
39 MODULE_AUTHOR("Andy Walls");
40 MODULE_DESCRIPTION("CX23415/CX23416 ALSA Interface");
41 MODULE_LICENSE("GPL");
43 MODULE_VERSION(IVTV_VERSION
);
46 struct snd_ivtv_card
*to_snd_ivtv_card(struct v4l2_device
*v4l2_dev
)
48 return to_ivtv(v4l2_dev
)->alsa
;
51 static void snd_ivtv_card_free(struct snd_ivtv_card
*itvsc
)
56 if (itvsc
->v4l2_dev
!= NULL
)
57 to_ivtv(itvsc
->v4l2_dev
)->alsa
= NULL
;
59 /* FIXME - take any other stopping actions needed */
64 static void snd_ivtv_card_private_free(struct snd_card
*sc
)
68 snd_ivtv_card_free(sc
->private_data
);
69 sc
->private_data
= NULL
;
70 sc
->private_free
= NULL
;
73 static int snd_ivtv_card_create(struct v4l2_device
*v4l2_dev
,
75 struct snd_ivtv_card
**itvsc
)
77 *itvsc
= kzalloc(sizeof(struct snd_ivtv_card
), GFP_KERNEL
);
81 (*itvsc
)->v4l2_dev
= v4l2_dev
;
84 sc
->private_data
= *itvsc
;
85 sc
->private_free
= snd_ivtv_card_private_free
;
90 static int snd_ivtv_card_set_names(struct snd_ivtv_card
*itvsc
)
92 struct ivtv
*itv
= to_ivtv(itvsc
->v4l2_dev
);
93 struct snd_card
*sc
= itvsc
->sc
;
95 /* sc->driver is used by alsa-lib's configurator: simple, unique */
96 strscpy(sc
->driver
, "CX2341[56]", sizeof(sc
->driver
));
98 /* sc->shortname is a symlink in /proc/asound: IVTV-M -> cardN */
99 snprintf(sc
->shortname
, sizeof(sc
->shortname
), "IVTV-%d",
102 /* sc->longname is read from /proc/asound/cards */
103 snprintf(sc
->longname
, sizeof(sc
->longname
),
104 "CX2341[56] #%d %s TV/FM Radio/Line-In Capture",
105 itv
->instance
, itv
->card_name
);
110 static int snd_ivtv_init(struct v4l2_device
*v4l2_dev
)
112 struct ivtv
*itv
= to_ivtv(v4l2_dev
);
113 struct snd_card
*sc
= NULL
;
114 struct snd_ivtv_card
*itvsc
;
117 /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
119 /* (1) Check and increment the device index */
120 /* This is a no-op for us. We'll use the itv->instance */
122 /* (2) Create a card instance */
123 /* use first available id if not specified otherwise*/
124 idx
= index
[itv
->instance
] == -1 ? SNDRV_DEFAULT_IDX1
: index
[itv
->instance
];
125 ret
= snd_card_new(&itv
->pdev
->dev
,
127 SNDRV_DEFAULT_STR1
, /* xid from end of shortname*/
128 THIS_MODULE
, 0, &sc
);
130 IVTV_ALSA_ERR("%s: snd_card_new() failed with err %d\n",
135 /* (3) Create a main component */
136 ret
= snd_ivtv_card_create(v4l2_dev
, sc
, &itvsc
);
138 IVTV_ALSA_ERR("%s: snd_ivtv_card_create() failed with err %d\n",
143 /* (4) Set the driver ID and name strings */
144 snd_ivtv_card_set_names(itvsc
);
146 /* (5) Create other components: PCM, & proc files */
147 ret
= snd_ivtv_pcm_create(itvsc
);
149 IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
153 /* FIXME - proc files */
155 /* (7) Set the driver data and return 0 */
156 /* We do this out of normal order for PCI drivers to avoid races */
159 /* (6) Register the card instance */
160 ret
= snd_card_register(sc
);
163 IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
168 IVTV_ALSA_INFO("%s: Instance %d registered as ALSA card %d\n",
169 __func__
, itv
->instance
, sc
->number
);
181 static int ivtv_alsa_load(struct ivtv
*itv
)
183 struct v4l2_device
*v4l2_dev
= &itv
->v4l2_dev
;
184 struct ivtv_stream
*s
;
186 if (v4l2_dev
== NULL
) {
187 pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
192 itv
= to_ivtv(v4l2_dev
);
194 pr_err("ivtv-alsa itv is NULL\n");
198 s
= &itv
->streams
[IVTV_ENC_STREAM_TYPE_PCM
];
199 if (s
->vdev
.v4l2_dev
== NULL
) {
200 IVTV_DEBUG_ALSA_INFO("PCM stream for card is disabled - skipping\n");
204 if (itv
->alsa
!= NULL
) {
205 IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n",
210 if (snd_ivtv_init(v4l2_dev
)) {
211 IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n",
214 IVTV_DEBUG_ALSA_INFO("created ivtv ALSA interface instance\n");
219 static int __init
ivtv_alsa_init(void)
221 pr_info("ivtv-alsa: module loading...\n");
222 ivtv_ext_init
= &ivtv_alsa_load
;
226 static void __exit
snd_ivtv_exit(struct snd_ivtv_card
*itvsc
)
228 struct ivtv
*itv
= to_ivtv(itvsc
->v4l2_dev
);
230 /* FIXME - pointer checks & shutdown itvsc */
232 snd_card_free(itvsc
->sc
);
236 static int __exit
ivtv_alsa_exit_callback(struct device
*dev
, void *data
)
238 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev
);
239 struct snd_ivtv_card
*itvsc
;
241 if (v4l2_dev
== NULL
) {
242 pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
247 itvsc
= to_snd_ivtv_card(v4l2_dev
);
249 IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n",
254 snd_ivtv_exit(itvsc
);
258 static void __exit
ivtv_alsa_exit(void)
260 struct device_driver
*drv
;
263 pr_info("ivtv-alsa: module unloading...\n");
265 drv
= driver_find("ivtv", &pci_bus_type
);
266 ret
= driver_for_each_device(drv
, NULL
, NULL
, ivtv_alsa_exit_callback
);
267 (void)ret
; /* suppress compiler warning */
269 ivtv_ext_init
= NULL
;
270 pr_info("ivtv-alsa: module unload complete\n");
273 module_init(ivtv_alsa_init
);
274 module_exit(ivtv_alsa_exit
);