2 * ALSA interface to ivtv PCM capture streams
4 * Copyright (C) 2009,2012 Andy Walls <awalls@md.metrocast.net>
5 * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
7 * Portions of this work were sponsored by ONELAN Limited for the cx18 driver
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.
20 #include "ivtv-driver.h"
21 #include "ivtv-version.h"
22 #include "ivtv-alsa.h"
23 #include "ivtv-alsa-pcm.h"
25 #include <sound/core.h>
26 #include <sound/initval.h>
29 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
31 #define IVTV_DEBUG_ALSA_INFO(__fmt, __arg...) \
33 if (ivtv_alsa_debug & 2) \
34 printk(KERN_INFO pr_fmt("%s: alsa:" __fmt), \
38 module_param_named(debug
, ivtv_alsa_debug
, int, 0644);
39 MODULE_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");
44 module_param_array(index
, int, NULL
, 0444);
45 MODULE_PARM_DESC(index
,
46 "Index value for IVTV ALSA capture interface(s).\n");
48 MODULE_AUTHOR("Andy Walls");
49 MODULE_DESCRIPTION("CX23415/CX23416 ALSA Interface");
50 MODULE_SUPPORTED_DEVICE("CX23415/CX23416 MPEG2 encoder");
51 MODULE_LICENSE("GPL");
53 MODULE_VERSION(IVTV_VERSION
);
56 struct snd_ivtv_card
*to_snd_ivtv_card(struct v4l2_device
*v4l2_dev
)
58 return to_ivtv(v4l2_dev
)->alsa
;
62 struct snd_ivtv_card
*p_to_snd_ivtv_card(struct v4l2_device
**v4l2_dev
)
64 return container_of(v4l2_dev
, struct snd_ivtv_card
, v4l2_dev
);
67 static void snd_ivtv_card_free(struct snd_ivtv_card
*itvsc
)
72 if (itvsc
->v4l2_dev
!= NULL
)
73 to_ivtv(itvsc
->v4l2_dev
)->alsa
= NULL
;
75 /* FIXME - take any other stopping actions needed */
80 static void snd_ivtv_card_private_free(struct snd_card
*sc
)
84 snd_ivtv_card_free(sc
->private_data
);
85 sc
->private_data
= NULL
;
86 sc
->private_free
= NULL
;
89 static int snd_ivtv_card_create(struct v4l2_device
*v4l2_dev
,
91 struct snd_ivtv_card
**itvsc
)
93 *itvsc
= kzalloc(sizeof(struct snd_ivtv_card
), GFP_KERNEL
);
97 (*itvsc
)->v4l2_dev
= v4l2_dev
;
100 sc
->private_data
= *itvsc
;
101 sc
->private_free
= snd_ivtv_card_private_free
;
106 static int snd_ivtv_card_set_names(struct snd_ivtv_card
*itvsc
)
108 struct ivtv
*itv
= to_ivtv(itvsc
->v4l2_dev
);
109 struct snd_card
*sc
= itvsc
->sc
;
111 /* sc->driver is used by alsa-lib's configurator: simple, unique */
112 strlcpy(sc
->driver
, "CX2341[56]", sizeof(sc
->driver
));
114 /* sc->shortname is a symlink in /proc/asound: IVTV-M -> cardN */
115 snprintf(sc
->shortname
, sizeof(sc
->shortname
), "IVTV-%d",
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
);
126 static int snd_ivtv_init(struct v4l2_device
*v4l2_dev
)
128 struct ivtv
*itv
= to_ivtv(v4l2_dev
);
129 struct snd_card
*sc
= NULL
;
130 struct snd_ivtv_card
*itvsc
;
133 /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */
135 /* (1) Check and increment the device index */
136 /* This is a no-op for us. We'll use the itv->instance */
138 /* (2) Create a card instance */
139 /* use first available id if not specified otherwise*/
140 idx
= index
[itv
->instance
] == -1 ? SNDRV_DEFAULT_IDX1
: index
[itv
->instance
];
141 ret
= snd_card_new(&itv
->pdev
->dev
,
143 SNDRV_DEFAULT_STR1
, /* xid from end of shortname*/
144 THIS_MODULE
, 0, &sc
);
146 IVTV_ALSA_ERR("%s: snd_card_new() failed with err %d\n",
151 /* (3) Create a main component */
152 ret
= snd_ivtv_card_create(v4l2_dev
, sc
, &itvsc
);
154 IVTV_ALSA_ERR("%s: snd_ivtv_card_create() failed with err %d\n",
159 /* (4) Set the driver ID and name strings */
160 snd_ivtv_card_set_names(itvsc
);
162 /* (5) Create other components: PCM, & proc files */
163 ret
= snd_ivtv_pcm_create(itvsc
);
165 IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n",
169 /* FIXME - proc files */
171 /* (7) Set the driver data and return 0 */
172 /* We do this out of normal order for PCI drivers to avoid races */
175 /* (6) Register the card instance */
176 ret
= snd_card_register(sc
);
179 IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n",
184 IVTV_ALSA_INFO("%s: Instance %d registered as ALSA card %d\n",
185 __func__
, itv
->instance
, sc
->number
);
197 static int ivtv_alsa_load(struct ivtv
*itv
)
199 struct v4l2_device
*v4l2_dev
= &itv
->v4l2_dev
;
200 struct ivtv_stream
*s
;
202 if (v4l2_dev
== NULL
) {
203 pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
208 itv
= to_ivtv(v4l2_dev
);
210 pr_err("ivtv-alsa itv is NULL\n");
214 s
= &itv
->streams
[IVTV_ENC_STREAM_TYPE_PCM
];
215 if (s
->vdev
.v4l2_dev
== NULL
) {
216 IVTV_DEBUG_ALSA_INFO("PCM stream for card is disabled - skipping\n");
220 if (itv
->alsa
!= NULL
) {
221 IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n",
226 if (snd_ivtv_init(v4l2_dev
)) {
227 IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n",
230 IVTV_DEBUG_ALSA_INFO("created ivtv ALSA interface instance\n");
235 static int __init
ivtv_alsa_init(void)
237 pr_info("ivtv-alsa: module loading...\n");
238 ivtv_ext_init
= &ivtv_alsa_load
;
242 static void __exit
snd_ivtv_exit(struct snd_ivtv_card
*itvsc
)
244 struct ivtv
*itv
= to_ivtv(itvsc
->v4l2_dev
);
246 /* FIXME - pointer checks & shutdown itvsc */
248 snd_card_free(itvsc
->sc
);
252 static int __exit
ivtv_alsa_exit_callback(struct device
*dev
, void *data
)
254 struct v4l2_device
*v4l2_dev
= dev_get_drvdata(dev
);
255 struct snd_ivtv_card
*itvsc
;
257 if (v4l2_dev
== NULL
) {
258 pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n",
263 itvsc
= to_snd_ivtv_card(v4l2_dev
);
265 IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n",
270 snd_ivtv_exit(itvsc
);
274 static void __exit
ivtv_alsa_exit(void)
276 struct device_driver
*drv
;
279 pr_info("ivtv-alsa: module unloading...\n");
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 */
285 ivtv_ext_init
= NULL
;
286 pr_info("ivtv-alsa: module unload complete\n");
289 module_init(ivtv_alsa_init
);
290 module_exit(ivtv_alsa_exit
);