18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * ALSA interface to ivtv PCM capture streams 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2009,2012 Andy Walls <awalls@md.metrocast.net> 68c2ecf20Sopenharmony_ci * Copyright (C) 2009 Devin Heitmueller <dheitmueller@kernellabs.com> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Portions of this work were sponsored by ONELAN Limited for the cx18 driver 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include "ivtv-driver.h" 128c2ecf20Sopenharmony_ci#include "ivtv-version.h" 138c2ecf20Sopenharmony_ci#include "ivtv-alsa.h" 148c2ecf20Sopenharmony_ci#include "ivtv-alsa-pcm.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <sound/core.h> 178c2ecf20Sopenharmony_ci#include <sound/initval.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciint ivtv_alsa_debug; 208c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define IVTV_DEBUG_ALSA_INFO(__fmt, __arg...) \ 238c2ecf20Sopenharmony_ci do { \ 248c2ecf20Sopenharmony_ci if (ivtv_alsa_debug & 2) \ 258c2ecf20Sopenharmony_ci printk(KERN_INFO pr_fmt("%s: alsa:" __fmt), \ 268c2ecf20Sopenharmony_ci __func__, ##__arg); \ 278c2ecf20Sopenharmony_ci } while (0) 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cimodule_param_named(debug, ivtv_alsa_debug, int, 0644); 308c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, 318c2ecf20Sopenharmony_ci "Debug level (bitmask). Default: 0\n" 328c2ecf20Sopenharmony_ci "\t\t\t 1/0x0001: warning\n" 338c2ecf20Sopenharmony_ci "\t\t\t 2/0x0002: info\n"); 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444); 368c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, 378c2ecf20Sopenharmony_ci "Index value for IVTV ALSA capture interface(s).\n"); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andy Walls"); 408c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("CX23415/CX23416 ALSA Interface"); 418c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("CX23415/CX23416 MPEG2 encoder"); 428c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ciMODULE_VERSION(IVTV_VERSION); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic inline 478c2ecf20Sopenharmony_cistruct snd_ivtv_card *to_snd_ivtv_card(struct v4l2_device *v4l2_dev) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci return to_ivtv(v4l2_dev)->alsa; 508c2ecf20Sopenharmony_ci} 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic inline 538c2ecf20Sopenharmony_cistruct snd_ivtv_card *p_to_snd_ivtv_card(struct v4l2_device **v4l2_dev) 548c2ecf20Sopenharmony_ci{ 558c2ecf20Sopenharmony_ci return container_of(v4l2_dev, struct snd_ivtv_card, v4l2_dev); 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic void snd_ivtv_card_free(struct snd_ivtv_card *itvsc) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci if (itvsc == NULL) 618c2ecf20Sopenharmony_ci return; 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci if (itvsc->v4l2_dev != NULL) 648c2ecf20Sopenharmony_ci to_ivtv(itvsc->v4l2_dev)->alsa = NULL; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci /* FIXME - take any other stopping actions needed */ 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci kfree(itvsc); 698c2ecf20Sopenharmony_ci} 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic void snd_ivtv_card_private_free(struct snd_card *sc) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci if (sc == NULL) 748c2ecf20Sopenharmony_ci return; 758c2ecf20Sopenharmony_ci snd_ivtv_card_free(sc->private_data); 768c2ecf20Sopenharmony_ci sc->private_data = NULL; 778c2ecf20Sopenharmony_ci sc->private_free = NULL; 788c2ecf20Sopenharmony_ci} 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_cistatic int snd_ivtv_card_create(struct v4l2_device *v4l2_dev, 818c2ecf20Sopenharmony_ci struct snd_card *sc, 828c2ecf20Sopenharmony_ci struct snd_ivtv_card **itvsc) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci *itvsc = kzalloc(sizeof(struct snd_ivtv_card), GFP_KERNEL); 858c2ecf20Sopenharmony_ci if (*itvsc == NULL) 868c2ecf20Sopenharmony_ci return -ENOMEM; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci (*itvsc)->v4l2_dev = v4l2_dev; 898c2ecf20Sopenharmony_ci (*itvsc)->sc = sc; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci sc->private_data = *itvsc; 928c2ecf20Sopenharmony_ci sc->private_free = snd_ivtv_card_private_free; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci return 0; 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cistatic int snd_ivtv_card_set_names(struct snd_ivtv_card *itvsc) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci struct ivtv *itv = to_ivtv(itvsc->v4l2_dev); 1008c2ecf20Sopenharmony_ci struct snd_card *sc = itvsc->sc; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci /* sc->driver is used by alsa-lib's configurator: simple, unique */ 1038c2ecf20Sopenharmony_ci strscpy(sc->driver, "CX2341[56]", sizeof(sc->driver)); 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci /* sc->shortname is a symlink in /proc/asound: IVTV-M -> cardN */ 1068c2ecf20Sopenharmony_ci snprintf(sc->shortname, sizeof(sc->shortname), "IVTV-%d", 1078c2ecf20Sopenharmony_ci itv->instance); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci /* sc->longname is read from /proc/asound/cards */ 1108c2ecf20Sopenharmony_ci snprintf(sc->longname, sizeof(sc->longname), 1118c2ecf20Sopenharmony_ci "CX2341[56] #%d %s TV/FM Radio/Line-In Capture", 1128c2ecf20Sopenharmony_ci itv->instance, itv->card_name); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci return 0; 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic int snd_ivtv_init(struct v4l2_device *v4l2_dev) 1188c2ecf20Sopenharmony_ci{ 1198c2ecf20Sopenharmony_ci struct ivtv *itv = to_ivtv(v4l2_dev); 1208c2ecf20Sopenharmony_ci struct snd_card *sc = NULL; 1218c2ecf20Sopenharmony_ci struct snd_ivtv_card *itvsc; 1228c2ecf20Sopenharmony_ci int ret, idx; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci /* Numbrs steps from "Writing an ALSA Driver" by Takashi Iwai */ 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* (1) Check and increment the device index */ 1278c2ecf20Sopenharmony_ci /* This is a no-op for us. We'll use the itv->instance */ 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci /* (2) Create a card instance */ 1308c2ecf20Sopenharmony_ci /* use first available id if not specified otherwise*/ 1318c2ecf20Sopenharmony_ci idx = index[itv->instance] == -1 ? SNDRV_DEFAULT_IDX1 : index[itv->instance]; 1328c2ecf20Sopenharmony_ci ret = snd_card_new(&itv->pdev->dev, 1338c2ecf20Sopenharmony_ci idx, 1348c2ecf20Sopenharmony_ci SNDRV_DEFAULT_STR1, /* xid from end of shortname*/ 1358c2ecf20Sopenharmony_ci THIS_MODULE, 0, &sc); 1368c2ecf20Sopenharmony_ci if (ret) { 1378c2ecf20Sopenharmony_ci IVTV_ALSA_ERR("%s: snd_card_new() failed with err %d\n", 1388c2ecf20Sopenharmony_ci __func__, ret); 1398c2ecf20Sopenharmony_ci goto err_exit; 1408c2ecf20Sopenharmony_ci } 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci /* (3) Create a main component */ 1438c2ecf20Sopenharmony_ci ret = snd_ivtv_card_create(v4l2_dev, sc, &itvsc); 1448c2ecf20Sopenharmony_ci if (ret) { 1458c2ecf20Sopenharmony_ci IVTV_ALSA_ERR("%s: snd_ivtv_card_create() failed with err %d\n", 1468c2ecf20Sopenharmony_ci __func__, ret); 1478c2ecf20Sopenharmony_ci goto err_exit_free; 1488c2ecf20Sopenharmony_ci } 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci /* (4) Set the driver ID and name strings */ 1518c2ecf20Sopenharmony_ci snd_ivtv_card_set_names(itvsc); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci /* (5) Create other components: PCM, & proc files */ 1548c2ecf20Sopenharmony_ci ret = snd_ivtv_pcm_create(itvsc); 1558c2ecf20Sopenharmony_ci if (ret) { 1568c2ecf20Sopenharmony_ci IVTV_ALSA_ERR("%s: snd_ivtv_pcm_create() failed with err %d\n", 1578c2ecf20Sopenharmony_ci __func__, ret); 1588c2ecf20Sopenharmony_ci goto err_exit_free; 1598c2ecf20Sopenharmony_ci } 1608c2ecf20Sopenharmony_ci /* FIXME - proc files */ 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci /* (7) Set the driver data and return 0 */ 1638c2ecf20Sopenharmony_ci /* We do this out of normal order for PCI drivers to avoid races */ 1648c2ecf20Sopenharmony_ci itv->alsa = itvsc; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci /* (6) Register the card instance */ 1678c2ecf20Sopenharmony_ci ret = snd_card_register(sc); 1688c2ecf20Sopenharmony_ci if (ret) { 1698c2ecf20Sopenharmony_ci itv->alsa = NULL; 1708c2ecf20Sopenharmony_ci IVTV_ALSA_ERR("%s: snd_card_register() failed with err %d\n", 1718c2ecf20Sopenharmony_ci __func__, ret); 1728c2ecf20Sopenharmony_ci goto err_exit_free; 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci IVTV_ALSA_INFO("%s: Instance %d registered as ALSA card %d\n", 1768c2ecf20Sopenharmony_ci __func__, itv->instance, sc->number); 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci return 0; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_cierr_exit_free: 1818c2ecf20Sopenharmony_ci if (sc != NULL) 1828c2ecf20Sopenharmony_ci snd_card_free(sc); 1838c2ecf20Sopenharmony_ci kfree(itvsc); 1848c2ecf20Sopenharmony_cierr_exit: 1858c2ecf20Sopenharmony_ci return ret; 1868c2ecf20Sopenharmony_ci} 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_cistatic int ivtv_alsa_load(struct ivtv *itv) 1898c2ecf20Sopenharmony_ci{ 1908c2ecf20Sopenharmony_ci struct v4l2_device *v4l2_dev = &itv->v4l2_dev; 1918c2ecf20Sopenharmony_ci struct ivtv_stream *s; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci if (v4l2_dev == NULL) { 1948c2ecf20Sopenharmony_ci pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n", 1958c2ecf20Sopenharmony_ci __func__); 1968c2ecf20Sopenharmony_ci return 0; 1978c2ecf20Sopenharmony_ci } 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci itv = to_ivtv(v4l2_dev); 2008c2ecf20Sopenharmony_ci if (itv == NULL) { 2018c2ecf20Sopenharmony_ci pr_err("ivtv-alsa itv is NULL\n"); 2028c2ecf20Sopenharmony_ci return 0; 2038c2ecf20Sopenharmony_ci } 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci s = &itv->streams[IVTV_ENC_STREAM_TYPE_PCM]; 2068c2ecf20Sopenharmony_ci if (s->vdev.v4l2_dev == NULL) { 2078c2ecf20Sopenharmony_ci IVTV_DEBUG_ALSA_INFO("PCM stream for card is disabled - skipping\n"); 2088c2ecf20Sopenharmony_ci return 0; 2098c2ecf20Sopenharmony_ci } 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci if (itv->alsa != NULL) { 2128c2ecf20Sopenharmony_ci IVTV_ALSA_ERR("%s: struct snd_ivtv_card * already exists\n", 2138c2ecf20Sopenharmony_ci __func__); 2148c2ecf20Sopenharmony_ci return 0; 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci if (snd_ivtv_init(v4l2_dev)) { 2188c2ecf20Sopenharmony_ci IVTV_ALSA_ERR("%s: failed to create struct snd_ivtv_card\n", 2198c2ecf20Sopenharmony_ci __func__); 2208c2ecf20Sopenharmony_ci } else { 2218c2ecf20Sopenharmony_ci IVTV_DEBUG_ALSA_INFO("created ivtv ALSA interface instance\n"); 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci return 0; 2248c2ecf20Sopenharmony_ci} 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_cistatic int __init ivtv_alsa_init(void) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci pr_info("ivtv-alsa: module loading...\n"); 2298c2ecf20Sopenharmony_ci ivtv_ext_init = &ivtv_alsa_load; 2308c2ecf20Sopenharmony_ci return 0; 2318c2ecf20Sopenharmony_ci} 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_cistatic void __exit snd_ivtv_exit(struct snd_ivtv_card *itvsc) 2348c2ecf20Sopenharmony_ci{ 2358c2ecf20Sopenharmony_ci struct ivtv *itv = to_ivtv(itvsc->v4l2_dev); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci /* FIXME - pointer checks & shutdown itvsc */ 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci snd_card_free(itvsc->sc); 2408c2ecf20Sopenharmony_ci itv->alsa = NULL; 2418c2ecf20Sopenharmony_ci} 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_cistatic int __exit ivtv_alsa_exit_callback(struct device *dev, void *data) 2448c2ecf20Sopenharmony_ci{ 2458c2ecf20Sopenharmony_ci struct v4l2_device *v4l2_dev = dev_get_drvdata(dev); 2468c2ecf20Sopenharmony_ci struct snd_ivtv_card *itvsc; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci if (v4l2_dev == NULL) { 2498c2ecf20Sopenharmony_ci pr_err("ivtv-alsa: %s: struct v4l2_device * is NULL\n", 2508c2ecf20Sopenharmony_ci __func__); 2518c2ecf20Sopenharmony_ci return 0; 2528c2ecf20Sopenharmony_ci } 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci itvsc = to_snd_ivtv_card(v4l2_dev); 2558c2ecf20Sopenharmony_ci if (itvsc == NULL) { 2568c2ecf20Sopenharmony_ci IVTV_ALSA_WARN("%s: struct snd_ivtv_card * is NULL\n", 2578c2ecf20Sopenharmony_ci __func__); 2588c2ecf20Sopenharmony_ci return 0; 2598c2ecf20Sopenharmony_ci } 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci snd_ivtv_exit(itvsc); 2628c2ecf20Sopenharmony_ci return 0; 2638c2ecf20Sopenharmony_ci} 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_cistatic void __exit ivtv_alsa_exit(void) 2668c2ecf20Sopenharmony_ci{ 2678c2ecf20Sopenharmony_ci struct device_driver *drv; 2688c2ecf20Sopenharmony_ci int ret; 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci pr_info("ivtv-alsa: module unloading...\n"); 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci drv = driver_find("ivtv", &pci_bus_type); 2738c2ecf20Sopenharmony_ci ret = driver_for_each_device(drv, NULL, NULL, ivtv_alsa_exit_callback); 2748c2ecf20Sopenharmony_ci (void)ret; /* suppress compiler warning */ 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci ivtv_ext_init = NULL; 2778c2ecf20Sopenharmony_ci pr_info("ivtv-alsa: module unload complete\n"); 2788c2ecf20Sopenharmony_ci} 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_cimodule_init(ivtv_alsa_init); 2818c2ecf20Sopenharmony_cimodule_exit(ivtv_alsa_exit); 282