blob: 6433ff0ad859e8507e9b9979ac30d87f081cbe77 [file] [log] [blame]
Andy Walls9722c8f2009-05-25 21:40:25 -03001/*
2 * ALSA interface to cx18 PCM capture streams
3 *
4 * Copyright (C) 2009 Andy Walls <awalls@radix.net>
Devin Heitmueller4cb565c2009-11-19 22:46:10 -03005 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
6 *
7 * Portions of this work were sponsored by ONELAN Limited.
Andy Walls9722c8f2009-05-25 21:40:25 -03008 *
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.
13 *
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.
18 *
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
23 */
24
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>
30
31#include <media/v4l2-device.h>
32
33#include <sound/core.h>
34#include <sound/initval.h>
35
36#include "cx18-driver.h"
Devin Heitmueller4cb565c2009-11-19 22:46:10 -030037#include "cx18-version.h"
38#include "cx18-alsa.h"
Andy Walls9722c8f2009-05-25 21:40:25 -030039#include "cx18-alsa-mixer.h"
40#include "cx18-alsa-pcm.h"
41
42int cx18_alsa_debug;
43
Devin Heitmueller4cb565c2009-11-19 22:46:10 -030044#define CX18_DEBUG_ALSA_INFO(fmt, arg...) printk(KERN_INFO "%s: " fmt, "cx18-alsa", ## arg)
45
Andy Walls9722c8f2009-05-25 21:40:25 -030046module_param_named(debug, cx18_alsa_debug, int, 0644);
47MODULE_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");
51
52MODULE_AUTHOR("Andy Walls");
53MODULE_DESCRIPTION("CX23418 ALSA Interface");
54MODULE_SUPPORTED_DEVICE("CX23418 MPEG2 encoder");
55MODULE_LICENSE("GPL");
56
57MODULE_VERSION(CX18_VERSION);
58
59static inline
60struct snd_cx18_card *to_snd_cx18_card(struct v4l2_device *v4l2_dev)
61{
62 return to_cx18(v4l2_dev)->alsa;
63}
64
65static inline
66struct snd_cx18_card *p_to_snd_cx18_card(struct v4l2_device **v4l2_dev)
67{
68 return container_of(v4l2_dev, struct snd_cx18_card, v4l2_dev);
69}
70
71static void snd_cx18_card_free(struct snd_cx18_card *cxsc)
72{
73 if (cxsc == NULL)
74 return;
75
76 if (cxsc->v4l2_dev != NULL)
77 to_cx18(cxsc->v4l2_dev)->alsa = NULL;
78
79 /* FIXME - take any other stopping actions needed */
80
81 kfree(cxsc);
82}
83
84static void snd_cx18_card_private_free(struct snd_card *sc)
85{
86 if (sc == NULL)
87 return;
88 snd_cx18_card_free(sc->private_data);
89 sc->private_data = NULL;
90 sc->private_free = NULL;
91}
92
93static int __init snd_cx18_card_create(struct v4l2_device *v4l2_dev,
94 struct snd_card *sc,
95 struct snd_cx18_card **cxsc)
96{
97 *cxsc = kzalloc(sizeof(struct snd_cx18_card), GFP_KERNEL);
98 if (*cxsc == NULL)
99 return -ENOMEM;
100
101 (*cxsc)->v4l2_dev = v4l2_dev;
102 (*cxsc)->sc = sc;
103
104 sc->private_data = *cxsc;
105 sc->private_free = snd_cx18_card_private_free;
106
107 return 0;
108}
109
110static int __init snd_cx18_card_set_names(struct snd_cx18_card *cxsc)
111{
112 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
113 struct snd_card *sc = cxsc->sc;
114
115 /* sc->driver is used by alsa-lib's configurator: simple, unique */
116 strlcpy(sc->driver, "CX23418", sizeof(sc->driver));
117
118 /* sc->shortname is a symlink in /proc/asound: CX18-M -> cardN */
119 snprintf(sc->shortname, sizeof(sc->shortname), "CX18-%d",
120 cx->instance);
121
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);
Devin Heitmueller4cb565c2009-11-19 22:46:10 -0300126
127 return 0;
Andy Walls9722c8f2009-05-25 21:40:25 -0300128}
129
130static int __init snd_cx18_init(struct v4l2_device *v4l2_dev)
131{
132 struct cx18 *cx = to_cx18(v4l2_dev);
133 struct snd_card *sc;
134 struct snd_cx18_card *cxsc;
135 int ret;
136
137 /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
138
139 /* (1) Check and increment the device index */
140 /* This is a no-op for us. We'll use the cx->instance */
141
142 /* (2) Create a card instance */
143 ret = snd_card_create(SNDRV_DEFAULT_IDX1, /* use first available id */
144 SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
145 THIS_MODULE, 0, &sc);
146 if (ret) {
147 CX18_ALSA_ERR("%s: snd_card_create() failed with err %d\n",
148 __func__, ret);
149 goto err_exit;
150 }
151
152 /* (3) Create a main component */
153 ret = snd_cx18_card_create(v4l2_dev, sc, &cxsc);
154 if (ret) {
155 CX18_ALSA_ERR("%s: snd_cx18_card_create() failed with err %d\n",
156 __func__, ret);
157 goto err_exit_free;
158 }
159
160 /* (4) Set the driver ID and name strings */
161 snd_cx18_card_set_names(cxsc);
162
Andy Walls9722c8f2009-05-25 21:40:25 -0300163
164 ret = snd_cx18_pcm_create(cxsc);
165 if (ret) {
166 CX18_ALSA_ERR("%s: snd_cx18_pcm_create() failed with err %d\n",
167 __func__, ret);
168 goto err_exit_free;
169 }
170 /* FIXME - proc files */
171
172 /* (7) Set the driver data and return 0 */
173 /* We do this out of normal order for PCI drivers to avoid races */
174 cx->alsa = cxsc;
175
176 /* (6) Register the card instance */
177 ret = snd_card_register(sc);
178 if (ret) {
179 cx->alsa = NULL;
180 CX18_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
181 __func__, ret);
182 goto err_exit_free;
183 }
184
185 return 0;
186
187err_exit_free:
188 snd_card_free(sc);
189err_exit:
190 return ret;
191}
192
Devin Heitmuellerd68b6872009-11-20 01:15:54 -0300193int cx18_alsa_load(struct cx18 *cx)
Andy Walls9722c8f2009-05-25 21:40:25 -0300194{
Devin Heitmuellerd68b6872009-11-20 01:15:54 -0300195 struct v4l2_device *v4l2_dev = &cx->v4l2_dev;
Andy Walls9722c8f2009-05-25 21:40:25 -0300196 struct cx18_stream *s;
197
198 if (v4l2_dev == NULL) {
199 printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
200 __func__);
201 return 0;
202 }
203
204 cx = to_cx18(v4l2_dev);
Devin Heitmueller4cb565c2009-11-19 22:46:10 -0300205 if (cx == NULL) {
206 printk(KERN_ERR "cx18-alsa cx is NULL\n");
207 return 0;
208 }
209
Andy Walls9722c8f2009-05-25 21:40:25 -0300210 s = &cx->streams[CX18_ENC_STREAM_TYPE_PCM];
211 if (s->video_dev == NULL) {
212 CX18_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - "
213 "skipping\n", __func__);
214 return 0;
215 }
216
217 if (cx->alsa != NULL) {
218 CX18_ALSA_ERR("%s: struct snd_cx18_card * already exists\n",
219 __func__);
220 return 0;
221 }
222
223 if (snd_cx18_init(v4l2_dev)) {
224 CX18_ALSA_ERR("%s: failed to create struct snd_cx18_card\n",
225 __func__);
226 } else {
227 CX18_DEBUG_ALSA_INFO("%s: created cx18 ALSA interface instance "
Devin Heitmuellerd68b6872009-11-20 01:15:54 -0300228 "\n", __func__);
Andy Walls9722c8f2009-05-25 21:40:25 -0300229 }
230 return 0;
231}
232
233static int __init cx18_alsa_init(void)
234{
Andy Walls9722c8f2009-05-25 21:40:25 -0300235 printk(KERN_INFO "cx18-alsa: module loading...\n");
Devin Heitmuellerd68b6872009-11-20 01:15:54 -0300236 cx18_ext_init = &cx18_alsa_load;
237 return 0;
Andy Walls9722c8f2009-05-25 21:40:25 -0300238}
239
240static void snd_cx18_exit(struct snd_cx18_card *cxsc)
241{
Devin Heitmueller4cb565c2009-11-19 22:46:10 -0300242 struct cx18 *cx = to_cx18(cxsc->v4l2_dev);
Andy Walls9722c8f2009-05-25 21:40:25 -0300243
244 /* FIXME - pointer checks & shutdown cxsc */
245
246 snd_card_free(cxsc->sc);
247 cx->alsa = NULL;
248}
249
250static int cx18_alsa_exit_callback(struct device *dev, void *data)
251{
252 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
253 struct snd_cx18_card *cxsc;
254 struct cx18 *cx;
255
256 if (v4l2_dev == NULL) {
257 printk(KERN_ERR "cx18-alsa: %s: struct v4l2_device * is NULL\n",
258 __func__);
259 return 0;
260 }
261
262 cxsc = to_snd_cx18_card(v4l2_dev);
263 if (cxsc == NULL) {
264 CX18_ALSA_WARN("%s: struct snd_cx18_card * is NULL\n",
265 __func__);
266 return 0;
267 }
268
269 snd_cx18_exit(cxsc);
270 return 0;
271}
272
273static void cx18_alsa_exit(void)
274{
275 struct device_driver *drv;
276 int ret;
277
278 printk(KERN_INFO "cx18-alsa: module unloading...\n");
279
280 drv = driver_find("cx18", &pci_bus_type);
281 ret = driver_for_each_device(drv, NULL, NULL, cx18_alsa_exit_callback);
282 put_driver(drv);
283
Devin Heitmuellerd68b6872009-11-20 01:15:54 -0300284 cx18_ext_init = NULL;
Andy Walls9722c8f2009-05-25 21:40:25 -0300285 printk(KERN_INFO "cx18-alsa: module unload complete\n");
286}
287
288module_init(cx18_alsa_init);
289module_exit(cx18_alsa_exit);