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