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