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