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