Linux 4.16.11
[linux/fpc-iii.git] / drivers / media / pci / cx18 / cx18-alsa-main.c
blob2531e4b81b60ad9718cd8934fb726926bc8921e4
1 /*
2 * ALSA interface to cx18 PCM capture streams
4 * Copyright (C) 2009 Andy Walls <awalls@md.metrocast.net>
5 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
7 * Portions of this work were sponsored by ONELAN Limited.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/init.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/device.h>
25 #include <linux/spinlock.h>
27 #include <media/v4l2-device.h>
29 #include <sound/core.h>
30 #include <sound/initval.h>
32 #include "cx18-driver.h"
33 #include "cx18-version.h"
34 #include "cx18-alsa.h"
35 #include "cx18-alsa-mixer.h"
36 #include "cx18-alsa-pcm.h"
38 int cx18_alsa_debug;
40 #define CX18_DEBUG_ALSA_INFO(fmt, arg...) \
41 do { \
42 if (cx18_alsa_debug & 2) \
43 printk(KERN_INFO "%s: " fmt, "cx18-alsa", ## arg); \
44 } while (0);
46 module_param_named(debug, cx18_alsa_debug, int, 0644);
47 MODULE_PARM_DESC(debug,
48 "Debug level (bitmask). Default: 0\n"
49 "\t\t\t 1/0x0001: warning\n"
50 "\t\t\t 2/0x0002: info\n");
52 MODULE_AUTHOR("Andy Walls");
53 MODULE_DESCRIPTION("CX23418 ALSA Interface");
54 MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
55 MODULE_LICENSE("GPL");
57 MODULE_VERSION(CX18_VERSION);
59 static inline
60 struct snd_cx18_card *to_snd_cx18_card(struct v4l2_device *v4l2_dev)
62 return to_cx18(v4l2_dev)->alsa;
65 static inline
66 struct snd_cx18_card *p_to_snd_cx18_card(struct v4l2_device **v4l2_dev)
68 return container_of(v4l2_dev, struct snd_cx18_card, v4l2_dev);
71 static void snd_cx18_card_free(struct snd_cx18_card *cxsc)
73 if (cxsc == NULL)
74 return;
76 if (cxsc->v4l2_dev != NULL)
77 to_cx18(cxsc->v4l2_dev)->alsa = NULL;
79 /* FIXME - take any other stopping actions needed */
81 kfree(cxsc);
84 static void snd_cx18_card_private_free(struct snd_card *sc)
86 if (sc == NULL)
87 return;
88 snd_cx18_card_free(sc->private_data);
89 sc->private_data = NULL;
90 sc->private_free = NULL;
93 static int snd_cx18_card_create(struct v4l2_device *v4l2_dev,
94 struct snd_card *sc,
95 struct snd_cx18_card **cxsc)
97 *cxsc = kzalloc(sizeof(struct snd_cx18_card), GFP_KERNEL);
98 if (*cxsc == NULL)
99 return -ENOMEM;
101 (*cxsc)->v4l2_dev = v4l2_dev;
102 (*cxsc)->sc = sc;
104 sc->private_data = *cxsc;
105 sc->private_free = snd_cx18_card_private_free;
107 return 0;
110 static int snd_cx18_card_set_names(struct snd_cx18_card *cxsc)
112 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
113 struct snd_card *sc = cxsc->sc;
115 /* sc->driver is used by alsa-lib's configurator: simple, unique */
116 strlcpy(sc->driver, "CX23418", sizeof(sc->driver));
118 /* sc->shortname is a symlink in /proc/asound: CX18-M -> cardN */
119 snprintf(sc->shortname, sizeof(sc->shortname), "CX18-%d",
120 cx->instance);
122 /* sc->longname is read from /proc/asound/cards */
123 snprintf(sc->longname, sizeof(sc->longname),
124 "CX23418 #%d %s TV/FM Radio/Line-In Capture",
125 cx->instance, cx->card_name);
127 return 0;
130 static int snd_cx18_init(struct v4l2_device *v4l2_dev)
132 struct cx18 *cx = to_cx18(v4l2_dev);
133 struct snd_card *sc = NULL;
134 struct snd_cx18_card *cxsc;
135 int ret;
137 /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
139 /* (1) Check and increment the device index */
140 /* This is a no-op for us. We'll use the cx->instance */
142 /* (2) Create a card instance */
143 ret = snd_card_new(&cx->pci_dev->dev,
144 SNDRV_DEFAULT_IDX1, /* use first available id */
145 SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
146 THIS_MODULE, 0, &sc);
147 if (ret) {
148 CX18_ALSA_ERR("%s: snd_card_new() failed with err %d\n",
149 __func__, ret);
150 goto err_exit;
153 /* (3) Create a main component */
154 ret = snd_cx18_card_create(v4l2_dev, sc, &cxsc);
155 if (ret) {
156 CX18_ALSA_ERR("%s: snd_cx18_card_create() failed with err %d\n",
157 __func__, ret);
158 goto err_exit_free;
161 /* (4) Set the driver ID and name strings */
162 snd_cx18_card_set_names(cxsc);
165 ret = snd_cx18_pcm_create(cxsc);
166 if (ret) {
167 CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
168 __func__, ret);
169 goto err_exit_free;
171 /* FIXME - proc files */
173 /* (7) Set the driver data and return 0 */
174 /* We do this out of normal order for PCI drivers to avoid races */
175 cx->alsa = cxsc;
177 /* (6) Register the card instance */
178 ret = snd_card_register(sc);
179 if (ret) {
180 cx->alsa = NULL;
181 CX18_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
182 __func__, ret);
183 goto err_exit_free;
186 return 0;
188 err_exit_free:
189 if (sc != NULL)
190 snd_card_free(sc);
191 kfree(cxsc);
192 err_exit:
193 return ret;
196 static int cx18_alsa_load(struct cx18 *cx)
198 struct v4l2_device *v4l2_dev = &cx->v4l2_dev;
199 struct cx18_stream *s;
201 if (v4l2_dev == NULL) {
202 printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
203 __func__);
204 return 0;
207 cx = to_cx18(v4l2_dev);
208 if (cx == NULL) {
209 printk(KERN_ERR "cx18-alsa cx is NULL\n");
210 return 0;
213 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
214 if (s->video_dev.v4l2_dev == NULL) {
215 CX18_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - skipping\n",
216 __func__);
217 return 0;
220 if (cx->alsa != NULL) {
221 CX18_ALSA_ERR("%s: struct snd_cx18_card * already exists\n",
222 __func__);
223 return 0;
226 if (snd_cx18_init(v4l2_dev)) {
227 CX18_ALSA_ERR("%s: failed to create struct snd_cx18_card\n",
228 __func__);
229 } else {
230 CX18_DEBUG_ALSA_INFO("%s: created cx18 ALSA interface instance\n",
231 __func__);
233 return 0;
236 static int __init cx18_alsa_init(void)
238 printk(KERN_INFO "cx18-alsa: module loading...\n");
239 cx18_ext_init = &cx18_alsa_load;
240 return 0;
243 static void __exit snd_cx18_exit(struct snd_cx18_card *cxsc)
245 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
247 /* FIXME - pointer checks & shutdown cxsc */
249 snd_card_free(cxsc->sc);
250 cx->alsa = NULL;
253 static int __exit cx18_alsa_exit_callback(struct device *dev, void *data)
255 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
256 struct snd_cx18_card *cxsc;
258 if (v4l2_dev == NULL) {
259 printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
260 __func__);
261 return 0;
264 cxsc = to_snd_cx18_card(v4l2_dev);
265 if (cxsc == NULL) {
266 CX18_ALSA_WARN("%s: struct snd_cx18_card * is NULL\n",
267 __func__);
268 return 0;
271 snd_cx18_exit(cxsc);
272 return 0;
275 static void __exit cx18_alsa_exit(void)
277 struct device_driver *drv;
278 int ret;
280 printk(KERN_INFO "cx18-alsa: module unloading...\n");
282 drv = driver_find("cx18", &pci_bus_type);
283 ret = driver_for_each_device(drv, NULL, NULL, cx18_alsa_exit_callback);
284 (void)ret; /* suppress compiler warning */
286 cx18_ext_init = NULL;
287 printk(KERN_INFO "cx18-alsa: module unload complete\n");
290 module_init(cx18_alsa_init);
291 module_exit(cx18_alsa_exit);