1269c11fbSAndy Walls /* 2269c11fbSAndy Walls * ALSA interface to ivtv PCM capture streams 3269c11fbSAndy Walls * 4269c11fbSAndy Walls * Copyright (C) 2009,2012 Andy Walls <awalls@md.metrocast.net> 5269c11fbSAndy Walls * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com> 6269c11fbSAndy Walls * 7269c11fbSAndy Walls * Portions of this work were sponsored by ONELAN Limited for the cx18 driver 8269c11fbSAndy Walls * 9269c11fbSAndy Walls * This program is free software; you can redistribute it and/or modify 10269c11fbSAndy Walls * it under the terms of the GNU General Public License as published by 11269c11fbSAndy Walls * the Free Software Foundation; either version 2 of the License, or 12269c11fbSAndy Walls * (at your option) any later version. 13269c11fbSAndy Walls * 14269c11fbSAndy Walls * This program is distributed in the hope that it will be useful, 15269c11fbSAndy Walls * but WITHOUT ANY WARRANTY; without even the implied warranty of 16269c11fbSAndy Walls * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17269c11fbSAndy Walls * GNU General Public License for more details. 18269c11fbSAndy Walls * 19269c11fbSAndy Walls * You should have received a copy of the GNU General Public License 20269c11fbSAndy Walls * along with this program; if not, write to the Free Software 21269c11fbSAndy Walls * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 22269c11fbSAndy Walls * 02111-1307 USA 23269c11fbSAndy Walls */ 24269c11fbSAndy Walls 25269c11fbSAndy Walls #include <linux/init.h> 26269c11fbSAndy Walls #include <linux/slab.h> 27269c11fbSAndy Walls #include <linux/module.h> 28269c11fbSAndy Walls #include <linux/kernel.h> 29269c11fbSAndy Walls #include <linux/device.h> 30269c11fbSAndy Walls #include <linux/spinlock.h> 31269c11fbSAndy Walls 32269c11fbSAndy Walls #include <media/v4l2-device.h> 33269c11fbSAndy Walls 34269c11fbSAndy Walls #include <sound/core.h> 35269c11fbSAndy Walls #include <sound/initval.h> 36269c11fbSAndy Walls 37269c11fbSAndy Walls #include "ivtv-driver.h" 38269c11fbSAndy Walls #include "ivtv-version.h" 39269c11fbSAndy Walls #include "ivtv-alsa.h" 40269c11fbSAndy Walls #include "ivtv-alsa-mixer.h" 41269c11fbSAndy Walls #include "ivtv-alsa-pcm.h" 42269c11fbSAndy Walls 43269c11fbSAndy Walls int ivtv_alsa_debug; 44269c11fbSAndy Walls 45269c11fbSAndy Walls #define IVTV_DEBUG_ALSA_INFO(fmt, arg...) \ 46269c11fbSAndy Walls do { \ 47269c11fbSAndy Walls if (ivtv_alsa_debug & 2) \ 48269c11fbSAndy Walls pr_info("%s: " fmt, "ivtv-alsa", ## arg); \ 49269c11fbSAndy Walls } while (0) 50269c11fbSAndy Walls 51269c11fbSAndy Walls module_param_named(debug, ivtv_alsa_debug, int, 0644); 52269c11fbSAndy Walls MODULE_PARM_DESC(debug, 53269c11fbSAndy Walls "Debug level (bitmask). Default: 0\n" 54269c11fbSAndy Walls "\t\t\t 1/0x0001: warning\n" 55269c11fbSAndy Walls "\t\t\t 2/0x0002: info\n"); 56269c11fbSAndy Walls 57269c11fbSAndy Walls MODULE_AUTHOR("Andy Walls"); 58269c11fbSAndy Walls MODULE_DESCRIPTION("CX23415/CX23416 ALSA Interface"); 59269c11fbSAndy Walls MODULE_SUPPORTED_DEVICE("CX23415/CX23416 MPEG2 encoder"); 60269c11fbSAndy Walls MODULE_LICENSE("GPL"); 61269c11fbSAndy Walls 62269c11fbSAndy Walls MODULE_VERSION(IVTV_VERSION); 63269c11fbSAndy Walls 64269c11fbSAndy Walls static inline 65269c11fbSAndy Walls struct snd_ivtv_card *to_snd_ivtv_card(struct v4l2_device *v4l2_dev) 66269c11fbSAndy Walls { 67269c11fbSAndy Walls return to_ivtv(v4l2_dev)->alsa; 68269c11fbSAndy Walls } 69269c11fbSAndy Walls 70269c11fbSAndy Walls static inline 71269c11fbSAndy Walls struct snd_ivtv_card *p_to_snd_ivtv_card(struct v4l2_device **v4l2_dev) 72269c11fbSAndy Walls { 73269c11fbSAndy Walls return container_of(v4l2_dev, struct snd_ivtv_card, v4l2_dev); 74269c11fbSAndy Walls } 75269c11fbSAndy Walls 76269c11fbSAndy Walls static void snd_ivtv_card_free(struct snd_ivtv_card *itvsc) 77269c11fbSAndy Walls { 78269c11fbSAndy Walls if (itvsc == NULL) 79269c11fbSAndy Walls return; 80269c11fbSAndy Walls 81269c11fbSAndy Walls if (itvsc->v4l2_dev != NULL) 82269c11fbSAndy Walls to_ivtv(itvsc->v4l2_dev)->alsa = NULL; 83269c11fbSAndy Walls 84269c11fbSAndy Walls /* FIXME - take any other stopping actions needed */ 85269c11fbSAndy Walls 86269c11fbSAndy Walls kfree(itvsc); 87269c11fbSAndy Walls } 88269c11fbSAndy Walls 89269c11fbSAndy Walls static void snd_ivtv_card_private_free(struct snd_card *sc) 90269c11fbSAndy Walls { 91269c11fbSAndy Walls if (sc == NULL) 92269c11fbSAndy Walls return; 93269c11fbSAndy Walls snd_ivtv_card_free(sc->private_data); 94269c11fbSAndy Walls sc->private_data = NULL; 95269c11fbSAndy Walls sc->private_free = NULL; 96269c11fbSAndy Walls } 97269c11fbSAndy Walls 98269c11fbSAndy Walls static int snd_ivtv_card_create(struct v4l2_device *v4l2_dev, 99269c11fbSAndy Walls struct snd_card *sc, 100269c11fbSAndy Walls struct snd_ivtv_card **itvsc) 101269c11fbSAndy Walls { 102269c11fbSAndy Walls *itvsc = kzalloc(sizeof(struct snd_ivtv_card), GFP_KERNEL); 103269c11fbSAndy Walls if (*itvsc == NULL) 104269c11fbSAndy Walls return -ENOMEM; 105269c11fbSAndy Walls 106269c11fbSAndy Walls (*itvsc)->v4l2_dev = v4l2_dev; 107269c11fbSAndy Walls (*itvsc)->sc = sc; 108269c11fbSAndy Walls 109269c11fbSAndy Walls sc->private_data = *itvsc; 110269c11fbSAndy Walls sc->private_free = snd_ivtv_card_private_free; 111269c11fbSAndy Walls 112269c11fbSAndy Walls return 0; 113269c11fbSAndy Walls } 114269c11fbSAndy Walls 115269c11fbSAndy Walls static int snd_ivtv_card_set_names(struct snd_ivtv_card *itvsc) 116269c11fbSAndy Walls { 117269c11fbSAndy Walls struct ivtv *itv = to_ivtv(itvsc->v4l2_dev); 118269c11fbSAndy Walls struct snd_card *sc = itvsc->sc; 119269c11fbSAndy Walls 120269c11fbSAndy Walls /* sc->driver is used by alsa-lib's configurator: simple, unique */ 121269c11fbSAndy Walls strlcpy(sc->driver, "CX2341[56]", sizeof(sc->driver)); 122269c11fbSAndy Walls 123269c11fbSAndy Walls /* sc->shortname is a symlink in /proc/asound: IVTV-M -> cardN */ 124269c11fbSAndy Walls snprintf(sc->shortname, sizeof(sc->shortname), "IVTV-%d", 125269c11fbSAndy Walls itv->instance); 126269c11fbSAndy Walls 127269c11fbSAndy Walls /* sc->longname is read from /proc/asound/cards */ 128269c11fbSAndy Walls snprintf(sc->longname, sizeof(sc->longname), 129269c11fbSAndy Walls "CX2341[56] #%d %s TV/FM Radio/Line-In Capture", 130269c11fbSAndy Walls itv->instance, itv->card_name); 131269c11fbSAndy Walls 132269c11fbSAndy Walls return 0; 133269c11fbSAndy Walls } 134269c11fbSAndy Walls 135269c11fbSAndy Walls static int snd_ivtv_init(struct v4l2_device *v4l2_dev) 136269c11fbSAndy Walls { 137269c11fbSAndy Walls struct ivtv *itv = to_ivtv(v4l2_dev); 138269c11fbSAndy Walls struct snd_card *sc = NULL; 139269c11fbSAndy Walls struct snd_ivtv_card *itvsc; 140269c11fbSAndy Walls int ret; 141269c11fbSAndy Walls 142269c11fbSAndy Walls /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */ 143269c11fbSAndy Walls 144269c11fbSAndy Walls /* (1) Check and increment the device index */ 145269c11fbSAndy Walls /* This is a no-op for us. We'll use the itv->instance */ 146269c11fbSAndy Walls 147269c11fbSAndy Walls /* (2) Create a card instance */ 148269c11fbSAndy Walls ret = snd_card_create(SNDRV_DEFAULT_IDX1, /* use first available id */ 149269c11fbSAndy Walls SNDRV_DEFAULT_STR1, /* xid from end of shortname*/ 150269c11fbSAndy Walls THIS_MODULE, 0, &sc); 151269c11fbSAndy Walls if (ret) { 152269c11fbSAndy Walls IVTV_ALSA_ERR("%s: snd_card_create() failed with err %d\n", 153269c11fbSAndy Walls __func__, ret); 154269c11fbSAndy Walls goto err_exit; 155269c11fbSAndy Walls } 156269c11fbSAndy Walls 157269c11fbSAndy Walls /* (3) Create a main component */ 158269c11fbSAndy Walls ret = snd_ivtv_card_create(v4l2_dev, sc, &itvsc); 159269c11fbSAndy Walls if (ret) { 160269c11fbSAndy Walls IVTV_ALSA_ERR("%s: snd_ivtv_card_create() failed with err %d\n", 161269c11fbSAndy Walls __func__, ret); 162269c11fbSAndy Walls goto err_exit_free; 163269c11fbSAndy Walls } 164269c11fbSAndy Walls 165269c11fbSAndy Walls /* (4) Set the driver ID and name strings */ 166269c11fbSAndy Walls snd_ivtv_card_set_names(itvsc); 167269c11fbSAndy Walls 168269c11fbSAndy Walls /* (5) Create other components: mixer, PCM, & proc files */ 169269c11fbSAndy Walls #if 0 170269c11fbSAndy Walls ret = snd_ivtv_mixer_create(itvsc); 171269c11fbSAndy Walls if (ret) { 172269c11fbSAndy Walls IVTV_ALSA_WARN("%s: snd_ivtv_mixer_create() failed with err %d:" 173269c11fbSAndy Walls " proceeding anyway\n", __func__, ret); 174269c11fbSAndy Walls } 175269c11fbSAndy Walls #endif 176269c11fbSAndy Walls 177269c11fbSAndy Walls ret = snd_ivtv_pcm_create(itvsc); 178269c11fbSAndy Walls if (ret) { 179269c11fbSAndy Walls IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n", 180269c11fbSAndy Walls __func__, ret); 181269c11fbSAndy Walls goto err_exit_free; 182269c11fbSAndy Walls } 183269c11fbSAndy Walls /* FIXME - proc files */ 184269c11fbSAndy Walls 185269c11fbSAndy Walls /* (7) Set the driver data and return 0 */ 186269c11fbSAndy Walls /* We do this out of normal order for PCI drivers to avoid races */ 187269c11fbSAndy Walls itv->alsa = itvsc; 188269c11fbSAndy Walls 189269c11fbSAndy Walls /* (6) Register the card instance */ 190269c11fbSAndy Walls ret = snd_card_register(sc); 191269c11fbSAndy Walls if (ret) { 192269c11fbSAndy Walls itv->alsa = NULL; 193269c11fbSAndy Walls IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n", 194269c11fbSAndy Walls __func__, ret); 195269c11fbSAndy Walls goto err_exit_free; 196269c11fbSAndy Walls } 197269c11fbSAndy Walls 198269c11fbSAndy Walls return 0; 199269c11fbSAndy Walls 200269c11fbSAndy Walls err_exit_free: 201269c11fbSAndy Walls if (sc != NULL) 202269c11fbSAndy Walls snd_card_free(sc); 203269c11fbSAndy Walls kfree(itvsc); 204269c11fbSAndy Walls err_exit: 205269c11fbSAndy Walls return ret; 206269c11fbSAndy Walls } 207269c11fbSAndy Walls 208*cfb046cbSHans Verkuil static int ivtv_alsa_load(struct ivtv *itv) 209269c11fbSAndy Walls { 210269c11fbSAndy Walls struct v4l2_device *v4l2_dev = &itv->v4l2_dev; 211269c11fbSAndy Walls struct ivtv_stream *s; 212269c11fbSAndy Walls 213269c11fbSAndy Walls if (v4l2_dev == NULL) { 214269c11fbSAndy Walls pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n", 215269c11fbSAndy Walls __func__); 216269c11fbSAndy Walls return 0; 217269c11fbSAndy Walls } 218269c11fbSAndy Walls 219269c11fbSAndy Walls itv = to_ivtv(v4l2_dev); 220269c11fbSAndy Walls if (itv == NULL) { 221269c11fbSAndy Walls pr_err("ivtv-alsa itv is NULL\n"); 222269c11fbSAndy Walls return 0; 223269c11fbSAndy Walls } 224269c11fbSAndy Walls 225269c11fbSAndy Walls s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM]; 226269c11fbSAndy Walls if (s->vdev == NULL) { 227269c11fbSAndy Walls IVTV_DEBUG_ALSA_INFO("%s: PCM stream for card is disabled - " 228269c11fbSAndy Walls "skipping\n", __func__); 229269c11fbSAndy Walls return 0; 230269c11fbSAndy Walls } 231269c11fbSAndy Walls 232269c11fbSAndy Walls if (itv->alsa != NULL) { 233269c11fbSAndy Walls IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n", 234269c11fbSAndy Walls __func__); 235269c11fbSAndy Walls return 0; 236269c11fbSAndy Walls } 237269c11fbSAndy Walls 238269c11fbSAndy Walls if (snd_ivtv_init(v4l2_dev)) { 239269c11fbSAndy Walls IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n", 240269c11fbSAndy Walls __func__); 241269c11fbSAndy Walls } else { 242269c11fbSAndy Walls IVTV_DEBUG_ALSA_INFO("%s: created ivtv ALSA interface instance " 243269c11fbSAndy Walls "\n", __func__); 244269c11fbSAndy Walls } 245269c11fbSAndy Walls return 0; 246269c11fbSAndy Walls } 247269c11fbSAndy Walls 248269c11fbSAndy Walls static int __init ivtv_alsa_init(void) 249269c11fbSAndy Walls { 250269c11fbSAndy Walls pr_info("ivtv-alsa: module loading...\n"); 251269c11fbSAndy Walls ivtv_ext_init = &ivtv_alsa_load; 252269c11fbSAndy Walls return 0; 253269c11fbSAndy Walls } 254269c11fbSAndy Walls 255269c11fbSAndy Walls static void __exit snd_ivtv_exit(struct snd_ivtv_card *itvsc) 256269c11fbSAndy Walls { 257269c11fbSAndy Walls struct ivtv *itv = to_ivtv(itvsc->v4l2_dev); 258269c11fbSAndy Walls 259269c11fbSAndy Walls /* FIXME - pointer checks & shutdown itvsc */ 260269c11fbSAndy Walls 261269c11fbSAndy Walls snd_card_free(itvsc->sc); 262269c11fbSAndy Walls itv->alsa = NULL; 263269c11fbSAndy Walls } 264269c11fbSAndy Walls 265269c11fbSAndy Walls static int __exit ivtv_alsa_exit_callback(struct device *dev, void *data) 266269c11fbSAndy Walls { 267269c11fbSAndy Walls struct v4l2_device *v4l2_dev = dev_get_drvdata(dev); 268269c11fbSAndy Walls struct snd_ivtv_card *itvsc; 269269c11fbSAndy Walls 270269c11fbSAndy Walls if (v4l2_dev == NULL) { 271269c11fbSAndy Walls pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n", 272269c11fbSAndy Walls __func__); 273269c11fbSAndy Walls return 0; 274269c11fbSAndy Walls } 275269c11fbSAndy Walls 276269c11fbSAndy Walls itvsc = to_snd_ivtv_card(v4l2_dev); 277269c11fbSAndy Walls if (itvsc == NULL) { 278269c11fbSAndy Walls IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n", 279269c11fbSAndy Walls __func__); 280269c11fbSAndy Walls return 0; 281269c11fbSAndy Walls } 282269c11fbSAndy Walls 283269c11fbSAndy Walls snd_ivtv_exit(itvsc); 284269c11fbSAndy Walls return 0; 285269c11fbSAndy Walls } 286269c11fbSAndy Walls 287269c11fbSAndy Walls static void __exit ivtv_alsa_exit(void) 288269c11fbSAndy Walls { 289269c11fbSAndy Walls struct device_driver *drv; 290269c11fbSAndy Walls int ret; 291269c11fbSAndy Walls 292269c11fbSAndy Walls pr_info("ivtv-alsa: module unloading...\n"); 293269c11fbSAndy Walls 294269c11fbSAndy Walls drv = driver_find("ivtv", &pci_bus_type); 295269c11fbSAndy Walls ret = driver_for_each_device(drv, NULL, NULL, ivtv_alsa_exit_callback); 296269c11fbSAndy Walls (void)ret; /* suppress compiler warning */ 297269c11fbSAndy Walls 298269c11fbSAndy Walls ivtv_ext_init = NULL; 299269c11fbSAndy Walls pr_info("ivtv-alsa: module unload complete\n"); 300269c11fbSAndy Walls } 301269c11fbSAndy Walls 302269c11fbSAndy Walls module_init(ivtv_alsa_init); 303269c11fbSAndy Walls module_exit(ivtv_alsa_exit); 304