blob: c1856f609d2c06f205c25c466b32822c32bed077 [file] [log] [blame]
Andy Walls269c11f2012-09-02 19:13:14 -03001/*
2 * ALSA interface to ivtv PCM capture streams
3 *
4 * Copyright (C) 2009,2012 Andy Walls <awalls@md.metrocast.net>
5 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
6 *
7 * Portions of this work were sponsored by ONELAN Limited for the cx18 driver
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.
Andy Walls269c11f2012-09-02 19:13:14 -030018 */
19
Andy Walls269c11f2012-09-02 19:13:14 -030020#include "ivtv-driver.h"
21#include "ivtv-version.h"
22#include "ivtv-alsa.h"
Andy Walls269c11f2012-09-02 19:13:14 -030023#include "ivtv-alsa-pcm.h"
24
Mauro Carvalho Chehabbbdba432016-11-24 09:01:20 -020025#include <sound/core.h>
26#include <sound/initval.h>
27
Andy Walls269c11f2012-09-02 19:13:14 -030028int ivtv_alsa_debug;
Nicolas Sugino27315052015-08-14 14:36:11 -030029static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
Andy Walls269c11f2012-09-02 19:13:14 -030030
Mauro Carvalho Chehabf57e9612016-11-24 09:52:34 -020031#define IVTV_DEBUG_ALSA_INFO(__fmt, __arg...) \
Andy Walls269c11f2012-09-02 19:13:14 -030032 do { \
33 if (ivtv_alsa_debug & 2) \
Mauro Carvalho Chehabf57e9612016-11-24 09:52:34 -020034 printk(KERN_INFO pr_fmt("%s: alsa:" __fmt), \
35 __func__, ##__arg); \
Andy Walls269c11f2012-09-02 19:13:14 -030036 } while (0)
37
38module_param_named(debug, ivtv_alsa_debug, int, 0644);
39MODULE_PARM_DESC(debug,
40 "Debug level (bitmask). Default: 0\n"
41 "\t\t\t 1/0x0001: warning\n"
42 "\t\t\t 2/0x0002: info\n");
43
Nicolas Sugino27315052015-08-14 14:36:11 -030044module_param_array(index, int, NULL, 0444);
45MODULE_PARM_DESC(index,
46 "Index value for IVTV ALSA capture interface(s).\n");
47
Andy Walls269c11f2012-09-02 19:13:14 -030048MODULE_AUTHOR("Andy Walls");
49MODULE_DESCRIPTION("CX23415/CX23416 ALSA Interface");
50MODULE_SUPPORTED_DEVICE("CX23415/CX23416 MPEG2 encoder");
51MODULE_LICENSE("GPL");
52
53MODULE_VERSION(IVTV_VERSION);
54
55static inline
56struct snd_ivtv_card *to_snd_ivtv_card(struct v4l2_device *v4l2_dev)
57{
58 return to_ivtv(v4l2_dev)->alsa;
59}
60
61static inline
62struct snd_ivtv_card *p_to_snd_ivtv_card(struct v4l2_device **v4l2_dev)
63{
64 return container_of(v4l2_dev, struct snd_ivtv_card, v4l2_dev);
65}
66
67static void snd_ivtv_card_free(struct snd_ivtv_card *itvsc)
68{
69 if (itvsc == NULL)
70 return;
71
72 if (itvsc->v4l2_dev != NULL)
73 to_ivtv(itvsc->v4l2_dev)->alsa = NULL;
74
75 /* FIXME - take any other stopping actions needed */
76
77 kfree(itvsc);
78}
79
80static void snd_ivtv_card_private_free(struct snd_card *sc)
81{
82 if (sc == NULL)
83 return;
84 snd_ivtv_card_free(sc->private_data);
85 sc->private_data = NULL;
86 sc->private_free = NULL;
87}
88
89static int snd_ivtv_card_create(struct v4l2_device *v4l2_dev,
90 struct snd_card *sc,
91 struct snd_ivtv_card **itvsc)
92{
93 *itvsc = kzalloc(sizeof(struct snd_ivtv_card), GFP_KERNEL);
94 if (*itvsc == NULL)
95 return -ENOMEM;
96
97 (*itvsc)->v4l2_dev = v4l2_dev;
98 (*itvsc)->sc = sc;
99
100 sc->private_data = *itvsc;
101 sc->private_free = snd_ivtv_card_private_free;
102
103 return 0;
104}
105
106static int snd_ivtv_card_set_names(struct snd_ivtv_card *itvsc)
107{
108 struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
109 struct snd_card *sc = itvsc->sc;
110
111 /* sc->driver is used by alsa-lib's configurator: simple, unique */
112 strlcpy(sc->driver, "CX2341[56]", sizeof(sc->driver));
113
114 /* sc->shortname is a symlink in /proc/asound: IVTV-M -> cardN */
115 snprintf(sc->shortname, sizeof(sc->shortname), "IVTV-%d",
116 itv->instance);
117
118 /* sc->longname is read from /proc/asound/cards */
119 snprintf(sc->longname, sizeof(sc->longname),
120 "CX2341[56] #%d %s TV/FM Radio/Line-In Capture",
121 itv->instance, itv->card_name);
122
123 return 0;
124}
125
126static int snd_ivtv_init(struct v4l2_device *v4l2_dev)
127{
128 struct ivtv *itv = to_ivtv(v4l2_dev);
129 struct snd_card *sc = NULL;
130 struct snd_ivtv_card *itvsc;
Nicolas Sugino27315052015-08-14 14:36:11 -0300131 int ret, idx;
Andy Walls269c11f2012-09-02 19:13:14 -0300132
133 /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
134
135 /* (1) Check and increment the device index */
136 /* This is a no-op for us. We'll use the itv->instance */
137
138 /* (2) Create a card instance */
Nicolas Sugino27315052015-08-14 14:36:11 -0300139 /* use first available id if not specified otherwise*/
140 idx = index[itv->instance] == -1 ? SNDRV_DEFAULT_IDX1 : index[itv->instance];
Takashi Iwaie7356882014-01-29 14:48:43 +0100141 ret = snd_card_new(&itv->pdev->dev,
Nicolas Sugino27315052015-08-14 14:36:11 -0300142 idx,
Takashi Iwaie7356882014-01-29 14:48:43 +0100143 SNDRV_DEFAULT_STR1, /* xid from end of shortname*/
144 THIS_MODULE, 0, &sc);
Andy Walls269c11f2012-09-02 19:13:14 -0300145 if (ret) {
Takashi Iwaie7356882014-01-29 14:48:43 +0100146 IVTV_ALSA_ERR("%s: snd_card_new() failed with err %d\n",
Andy Walls269c11f2012-09-02 19:13:14 -0300147 __func__, ret);
148 goto err_exit;
149 }
150
151 /* (3) Create a main component */
152 ret = snd_ivtv_card_create(v4l2_dev, sc, &itvsc);
153 if (ret) {
154 IVTV_ALSA_ERR("%s: snd_ivtv_card_create() failed with err %d\n",
155 __func__, ret);
156 goto err_exit_free;
157 }
158
159 /* (4) Set the driver ID and name strings */
160 snd_ivtv_card_set_names(itvsc);
161
Corentin Labbe47b93482018-02-08 03:31:07 -0500162 /* (5) Create other components: PCM, & proc files */
Andy Walls269c11f2012-09-02 19:13:14 -0300163 ret = snd_ivtv_pcm_create(itvsc);
164 if (ret) {
165 IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
166 __func__, ret);
167 goto err_exit_free;
168 }
169 /* FIXME - proc files */
170
171 /* (7) Set the driver data and return 0 */
172 /* We do this out of normal order for PCI drivers to avoid races */
173 itv->alsa = itvsc;
174
175 /* (6) Register the card instance */
176 ret = snd_card_register(sc);
177 if (ret) {
178 itv->alsa = NULL;
179 IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
180 __func__, ret);
181 goto err_exit_free;
182 }
183
Nicolas Sugino27315052015-08-14 14:36:11 -0300184 IVTV_ALSA_INFO("%s: Instance %d registered as ALSA card %d\n",
185 __func__, itv->instance, sc->number);
186
Andy Walls269c11f2012-09-02 19:13:14 -0300187 return 0;
188
189err_exit_free:
190 if (sc != NULL)
191 snd_card_free(sc);
192 kfree(itvsc);
193err_exit:
194 return ret;
195}
196
Hans Verkuilcfb046c2013-02-09 05:40:10 -0300197static int ivtv_alsa_load(struct ivtv *itv)
Andy Walls269c11f2012-09-02 19:13:14 -0300198{
199 struct v4l2_device *v4l2_dev = &itv->v4l2_dev;
200 struct ivtv_stream *s;
201
202 if (v4l2_dev == NULL) {
203 pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
204 __func__);
205 return 0;
206 }
207
208 itv = to_ivtv(v4l2_dev);
209 if (itv == NULL) {
210 pr_err("ivtv-alsa itv is NULL\n");
211 return 0;
212 }
213
214 s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM];
Hans Verkuil635d62f2015-03-09 13:33:55 -0300215 if (s->vdev.v4l2_dev == NULL) {
Mauro Carvalho Chehabf57e9612016-11-24 09:52:34 -0200216 IVTV_DEBUG_ALSA_INFO("PCM stream for card is disabled - skipping\n");
Andy Walls269c11f2012-09-02 19:13:14 -0300217 return 0;
218 }
219
220 if (itv->alsa != NULL) {
221 IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n",
222 __func__);
223 return 0;
224 }
225
226 if (snd_ivtv_init(v4l2_dev)) {
227 IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n",
228 __func__);
229 } else {
Mauro Carvalho Chehabf57e9612016-11-24 09:52:34 -0200230 IVTV_DEBUG_ALSA_INFO("created ivtv ALSA interface instance\n");
Andy Walls269c11f2012-09-02 19:13:14 -0300231 }
232 return 0;
233}
234
235static int __init ivtv_alsa_init(void)
236{
237 pr_info("ivtv-alsa: module loading...\n");
238 ivtv_ext_init = &ivtv_alsa_load;
239 return 0;
240}
241
242static void __exit snd_ivtv_exit(struct snd_ivtv_card *itvsc)
243{
244 struct ivtv *itv = to_ivtv(itvsc->v4l2_dev);
245
246 /* FIXME - pointer checks & shutdown itvsc */
247
248 snd_card_free(itvsc->sc);
249 itv->alsa = NULL;
250}
251
252static int __exit ivtv_alsa_exit_callback(struct device *dev, void *data)
253{
254 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
255 struct snd_ivtv_card *itvsc;
256
257 if (v4l2_dev == NULL) {
258 pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
259 __func__);
260 return 0;
261 }
262
263 itvsc = to_snd_ivtv_card(v4l2_dev);
264 if (itvsc == NULL) {
265 IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n",
266 __func__);
267 return 0;
268 }
269
270 snd_ivtv_exit(itvsc);
271 return 0;
272}
273
274static void __exit ivtv_alsa_exit(void)
275{
276 struct device_driver *drv;
277 int ret;
278
279 pr_info("ivtv-alsa: module unloading...\n");
280
281 drv = driver_find("ivtv", &pci_bus_type);
282 ret = driver_for_each_device(drv, NULL, NULL, ivtv_alsa_exit_callback);
283 (void)ret; /* suppress compiler warning */
284
285 ivtv_ext_init = NULL;
286 pr_info("ivtv-alsa: module unload complete\n");
287}
288
289module_init(ivtv_alsa_init);
290module_exit(ivtv_alsa_exit);