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_SUPPORTED_DEVICE("CX23415/CX23416 MPEG2 encoder");
42 MODULE_LICENSE("GPL");
44 MODULE_VERSION(IVTV_VERSION
);
47 struct snd_ivtv_card
*to_snd_ivtv_card(struct v4l2_device
*v4l2_dev
)
49 return to_ivtv(v4l2_dev
)->alsa
;
53 struct snd_ivtv_card
*p_to_snd_ivtv_card(struct v4l2_device
**v4l2_dev
)
55 return container_of(v4l2_dev
, struct snd_ivtv_card
, v4l2_dev
);
58 static void snd_ivtv_card_free(struct snd_ivtv_card
*itvsc
)
63 if (itvsc
->v4l2_dev
!= NULL
)
64 to_ivtv(itvsc
->v4l2_dev
)->alsa
= NULL
;
66 /* FIXME - take any other stopping actions needed */
71 static void snd_ivtv_card_private_free(struct snd_card
*sc
)
75 snd_ivtv_card_free(sc
->private_data
);
76 sc
->private_data
= NULL
;
77 sc
->private_free
= NULL
;
80 static int snd_ivtv_card_create(struct v4l2_device
*v4l2_dev
,
82 struct snd_ivtv_card
**itvsc
)
84 *itvsc
= kzalloc(sizeof(struct snd_ivtv_card
), GFP_KERNEL
);
88 (*itvsc
)->v4l2_dev
= v4l2_dev
;
91 sc
->private_data
= *itvsc
;
92 sc
->private_free
= snd_ivtv_card_private_free
;
97 static int snd_ivtv_card_set_names(struct snd_ivtv_card
*itvsc
)
99 struct ivtv
*itv
= to_ivtv(itvsc
->v4l2_dev
);
100 struct snd_card
*sc
= itvsc
->sc
;
102 /* sc->driver is used by alsa-lib's configurator: simple, unique */
103 strscpy(sc
->driver
, "CX2341[56]", sizeof(sc
->driver
));
105 /* sc->shortname is a symlink in /proc/asound: IVTV-M -> cardN */
106 snprintf(sc
->shortname
, sizeof(sc
->shortname
), "IVTV-%d",
109 /* sc->longname is read from /proc/asound/cards */
110 snprintf(sc
->longname
, sizeof(sc
->longname
),
111 "CX2341[56] #%d %s TV/FM Radio/Line-In Capture",
112 itv
->instance
, itv
->card_name
);
117 static int snd_ivtv_init(struct v4l2_device
*v4l2_dev
)
119 struct ivtv
*itv
= to_ivtv(v4l2_dev
);
120 struct snd_card
*sc
= NULL
;
121 struct snd_ivtv_card
*itvsc
;
124 /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
126 /* (1) Check and increment the device index */
127 /* This is a no-op for us. We'll use the itv->instance */
129 /* (2) Create a card instance */
130 /* use first available id if not specified otherwise*/
131 idx
= index
[itv
->instance
] == -1 ? SNDRV_DEFAULT_IDX1
: index
[itv
->instance
];
132 ret
= snd_card_new(&itv
->pdev
->dev
,
134 SNDRV_DEFAULT_STR1
, /* xid from end of shortname*/
135 THIS_MODULE
, 0, &sc
);
137 IVTV_ALSA_ERR("%s: snd_card_new() failed with err %d\n",
142 /* (3) Create a main component */
143 ret
= snd_ivtv_card_create(v4l2_dev
, sc
, &itvsc
);
145 IVTV_ALSA_ERR("%s: snd_ivtv_card_create() failed with err %d\n",
150 /* (4) Set the driver ID and name strings */
151 snd_ivtv_card_set_names(itvsc
);
153 /* (5) Create other components: PCM, & proc files */
154 ret
= snd_ivtv_pcm_create(itvsc
);
156 IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
160 /* FIXME - proc files */
162 /* (7) Set the driver data and return 0 */
163 /* We do this out of normal order for PCI drivers to avoid races */
166 /* (6) Register the card instance */
167 ret
= snd_card_register(sc
);
170 IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
175 IVTV_ALSA_INFO("%s: Instance %d registered as ALSA card %d\n",
176 __func__
, itv
->instance
, sc
->number
);
188 static int ivtv_alsa_load(struct ivtv
*itv
)
190 struct v4l2_device
*v4l2_dev
= &itv
->v4l2_dev
;
191 struct ivtv_stream
*s
;
193 if (v4l2_dev
== NULL
) {
194 pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
199 itv
= to_ivtv(v4l2_dev
);
201 pr_err("ivtv-alsa itv is NULL\n");
205 s
= &itv
->streams
[IVTV_ENC_STREAM_TYPE_PCM
];
206 if (s
->vdev
.v4l2_dev
== NULL
) {
207 IVTV_DEBUG_ALSA_INFO("PCM stream for card is disabled - skipping\n");
211 if (itv
->alsa
!= NULL
) {
212 IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n",
217 if (snd_ivtv_init(v4l2_dev
)) {
218 IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n",
221 IVTV_DEBUG_ALSA_INFO("created ivtv ALSA interface instance\n");
226 static int __init
ivtv_alsa_init(void)
228 pr_info("ivtv-alsa: module loading...\n");
229 ivtv_ext_init
= &ivtv_alsa_load
;
233 static void __exit
snd_ivtv_exit(struct snd_ivtv_card
*itvsc
)
235 struct ivtv
*itv
= to_ivtv(itvsc
->v4l2_dev
);
237 /* FIXME - pointer checks & shutdown itvsc */
239 snd_card_free(itvsc
->sc
);
243 static int __exit
ivtv_alsa_exit_callback(struct device
*dev
, void *data
)
245 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev
);
246 struct snd_ivtv_card
*itvsc
;
248 if (v4l2_dev
== NULL
) {
249 pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
254 itvsc
= to_snd_ivtv_card(v4l2_dev
);
256 IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n",
261 snd_ivtv_exit(itvsc
);
265 static void __exit
ivtv_alsa_exit(void)
267 struct device_driver
*drv
;
270 pr_info("ivtv-alsa: module unloading...\n");
272 drv
= driver_find("ivtv", &pci_bus_type
);
273 ret
= driver_for_each_device(drv
, NULL
, NULL
, ivtv_alsa_exit_callback
);
274 (void)ret
; /* suppress compiler warning */
276 ivtv_ext_init
= NULL
;
277 pr_info("ivtv-alsa: module unload complete\n");
280 module_init(ivtv_alsa_init
);
281 module_exit(ivtv_alsa_exit
);