Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / cx18 / cx18-alsa-main.c
blobeb41d7ec65b95fc919a5858c6e6f511c7045941d
1 /*
2 * ALSA interface to cx18 PCM capture streams
4 * Copyright (C) 2009 Andy Walls <awalls@radix.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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/device.h>
29 #include <linux/spinlock.h>
31 #include <media/v4l2-device.h>
33 #include <sound/core.h>
34 #include <sound/initval.h>
36 #include "cx18-driver.h"
37 #include "cx18-version.h"
38 #include "cx18-alsa.h"
39 #include "cx18-alsa-mixer.h"
40 #include "cx18-alsa-pcm.h"
42 int cx18_alsa_debug;
44 #define CX18_DEBUG_ALSA_INFO(fmt, arg...) \
45 do { \
46 if (cx18_alsa_debug & 2) \
47 printk(KERN_INFO "%s: " fmt, "cx18-alsa", ## arg); \
48 } while (0);
50 module_param_named(debug, cx18_alsa_debug, int, 0644);
51 MODULE_PARM_DESC(debug,
52 "Debug level (bitmask). Default: 0\n"
53 "\t\t\t 1/0x0001: warning\n"
54 "\t\t\t 2/0x0002: info\n");
56 MODULE_AUTHOR("Andy Walls");
57 MODULE_DESCRIPTION("CX23418 ALSA Interface");
58 MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
59 MODULE_LICENSE("GPL");
61 MODULE_VERSION(CX18_VERSION);
63 static inline
64 struct snd_cx18_card *to_snd_cx18_card(struct v4l2_device *v4l2_dev)
66 return to_cx18(v4l2_dev)->alsa;
69 static inline
70 struct snd_cx18_card *p_to_snd_cx18_card(struct v4l2_device **v4l2_dev)
72 return container_of(v4l2_dev, struct snd_cx18_card, v4l2_dev);
75 static void snd_cx18_card_free(struct snd_cx18_card *cxsc)
77 if (cxsc == NULL)
78 return;
80 if (cxsc->v4l2_dev != NULL)
81 to_cx18(cxsc->v4l2_dev)->alsa = NULL;
83 /* FIXME - take any other stopping actions needed */
85 kfree(cxsc);
88 static void snd_cx18_card_private_free(struct snd_card *sc)
90 if (sc == NULL)
91 return;
92 snd_cx18_card_free(sc->private_data);
93 sc->private_data = NULL;
94 sc->private_free = NULL;
97 static int snd_cx18_card_create(struct v4l2_device *v4l2_dev,
98 struct snd_card *sc,
99 struct snd_cx18_card **cxsc)
101 *cxsc = kzalloc(sizeof(struct snd_cx18_card), GFP_KERNEL);
102 if (*cxsc == NULL)
103 return -ENOMEM;
105 (*cxsc)->v4l2_dev = v4l2_dev;
106 (*cxsc)->sc = sc;
108 sc->private_data = *cxsc;
109 sc->private_free = snd_cx18_card_private_free;
111 return 0;
114 static int snd_cx18_card_set_names(struct snd_cx18_card *cxsc)
116 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
117 struct snd_card *sc = cxsc->sc;
119 /* sc->driver is used by alsa-lib's configurator: simple, unique */
120 strlcpy(sc->driver, "CX23418", sizeof(sc->driver));
122 /* sc->shortname is a symlink in /proc/asound: CX18-M -> cardN */
123 snprintf(sc->shortname, sizeof(sc->shortname), "CX18-%d",
124 cx->instance);
126 /* sc->longname is read from /proc/asound/cards */
127 snprintf(sc->longname, sizeof(sc->longname),
128 "CX23418 #%d %s TV/FM Radio/Line-In Capture",
129 cx->instance, cx->card_name);
131 return 0;
134 static int snd_cx18_init(struct v4l2_device *v4l2_dev)
136 struct cx18 *cx = to_cx18(v4l2_dev);
137 struct snd_card *sc = NULL;
138 struct snd_cx18_card *cxsc;
139 int ret;
141 /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
143 /* (1) Check and increment the device index */
144 /* This is a no-op for us. We'll use the cx->instance */
146 /* (2) Create a card instance */
147 ret = snd_card_create(SNDRV_DEFAULT_IDX1, /* use first available id */
148 SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
149 THIS_MODULE, 0, &sc);
150 if (ret) {
151 CX18_ALSA_ERR("%s: snd_card_create() failed with err %d\n",
152 __func__, ret);
153 goto err_exit;
156 /* (3) Create a main component */
157 ret = snd_cx18_card_create(v4l2_dev, sc, &cxsc);
158 if (ret) {
159 CX18_ALSA_ERR("%s: snd_cx18_card_create() failed with err %d\n",
160 __func__, ret);
161 goto err_exit_free;
164 /* (4) Set the driver ID and name strings */
165 snd_cx18_card_set_names(cxsc);
168 ret = snd_cx18_pcm_create(cxsc);
169 if (ret) {
170 CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
171 __func__, ret);
172 goto err_exit_free;
174 /* FIXME - proc files */
176 /* (7) Set the driver data and return 0 */
177 /* We do this out of normal order for PCI drivers to avoid races */
178 cx->alsa = cxsc;
180 /* (6) Register the card instance */
181 ret = snd_card_register(sc);
182 if (ret) {
183 cx->alsa = NULL;
184 CX18_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
185 __func__, ret);
186 goto err_exit_free;
189 return 0;
191 err_exit_free:
192 if (sc != NULL)
193 snd_card_free(sc);
194 err_exit:
195 return ret;
198 int cx18_alsa_load(struct cx18 *cx)
200 struct v4l2_device *v4l2_dev = &cx->v4l2_dev;
201 struct cx18_stream *s;
203 if (v4l2_dev == NULL) {
204 printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
205 __func__);
206 return 0;
209 cx = to_cx18(v4l2_dev);
210 if (cx == NULL) {
211 printk(KERN_ERR "cx18-alsa cx is NULL\n");
212 return 0;
215 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
216 if (s->video_dev == NULL) {
217 CX18_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - "
218 "skipping\n", __func__);
219 return 0;
222 if (cx->alsa != NULL) {
223 CX18_ALSA_ERR("%s: struct snd_cx18_card * already exists\n",
224 __func__);
225 return 0;
228 if (snd_cx18_init(v4l2_dev)) {
229 CX18_ALSA_ERR("%s: failed to create struct snd_cx18_card\n",
230 __func__);
231 } else {
232 CX18_DEBUG_ALSA_INFO("%s: created cx18 ALSA interface instance "
233 "\n", __func__);
235 return 0;
238 static int __init cx18_alsa_init(void)
240 printk(KERN_INFO "cx18-alsa: module loading...\n");
241 cx18_ext_init = &cx18_alsa_load;
242 return 0;
245 static void __exit snd_cx18_exit(struct snd_cx18_card *cxsc)
247 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
249 /* FIXME - pointer checks & shutdown cxsc */
251 snd_card_free(cxsc->sc);
252 cx->alsa = NULL;
255 static int __exit cx18_alsa_exit_callback(struct device *dev, void *data)
257 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
258 struct snd_cx18_card *cxsc;
260 if (v4l2_dev == NULL) {
261 printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
262 __func__);
263 return 0;
266 cxsc = to_snd_cx18_card(v4l2_dev);
267 if (cxsc == NULL) {
268 CX18_ALSA_WARN("%s: struct snd_cx18_card * is NULL\n",
269 __func__);
270 return 0;
273 snd_cx18_exit(cxsc);
274 return 0;
277 static void __exit cx18_alsa_exit(void)
279 struct device_driver *drv;
280 int ret;
282 printk(KERN_INFO "cx18-alsa: module unloading...\n");
284 drv = driver_find("cx18", &pci_bus_type);
285 ret = driver_for_each_device(drv, NULL, NULL, cx18_alsa_exit_callback);
286 put_driver(drv);
288 cx18_ext_init = NULL;
289 printk(KERN_INFO "cx18-alsa: module unload complete\n");
292 module_init(cx18_alsa_init);
293 module_exit(cx18_alsa_exit);