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