11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Dummy soundcard 3c1017a4cSJaroslav Kysela * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 61da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 71da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 81da177e4SLinus Torvalds * (at your option) any later version. 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 111da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 121da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 131da177e4SLinus Torvalds * GNU General Public License for more details. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 161da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 171da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include <linux/init.h> 226e65c1ccSTakashi Iwai #include <linux/err.h> 236e65c1ccSTakashi Iwai #include <linux/platform_device.h> 241da177e4SLinus Torvalds #include <linux/jiffies.h> 251da177e4SLinus Torvalds #include <linux/slab.h> 261da177e4SLinus Torvalds #include <linux/time.h> 271da177e4SLinus Torvalds #include <linux/wait.h> 28c631d03cSTakashi Iwai #include <linux/hrtimer.h> 29c631d03cSTakashi Iwai #include <linux/math64.h> 3065a77217SPaul Gortmaker #include <linux/module.h> 311da177e4SLinus Torvalds #include <sound/core.h> 321da177e4SLinus Torvalds #include <sound/control.h> 33fb567a8eSTakashi Iwai #include <sound/tlv.h> 341da177e4SLinus Torvalds #include <sound/pcm.h> 351da177e4SLinus Torvalds #include <sound/rawmidi.h> 369b151fecSTakashi Iwai #include <sound/info.h> 371da177e4SLinus Torvalds #include <sound/initval.h> 381da177e4SLinus Torvalds 39c1017a4cSJaroslav Kysela MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 401da177e4SLinus Torvalds MODULE_DESCRIPTION("Dummy soundcard (/dev/null)"); 411da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 421da177e4SLinus Torvalds MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}"); 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #define MAX_PCM_DEVICES 4 45b888d1ceSTakashi Iwai #define MAX_PCM_SUBSTREAMS 128 461da177e4SLinus Torvalds #define MAX_MIDI_DEVICES 2 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds /* defaults */ 491da177e4SLinus Torvalds #define MAX_BUFFER_SIZE (64*1024) 50d5e1ca05SJaroslav Kysela #define MIN_PERIOD_SIZE 64 512ad5dd8dSJaroslav Kysela #define MAX_PERIOD_SIZE MAX_BUFFER_SIZE 521da177e4SLinus Torvalds #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE) 531da177e4SLinus Torvalds #define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000 541da177e4SLinus Torvalds #define USE_RATE_MIN 5500 551da177e4SLinus Torvalds #define USE_RATE_MAX 48000 561da177e4SLinus Torvalds #define USE_CHANNELS_MIN 1 571da177e4SLinus Torvalds #define USE_CHANNELS_MAX 2 581da177e4SLinus Torvalds #define USE_PERIODS_MIN 1 591da177e4SLinus Torvalds #define USE_PERIODS_MAX 1024 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 621da177e4SLinus Torvalds static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 63a67ff6a5SRusty Russell static bool enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; 64d5e1ca05SJaroslav Kysela static char *model[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = NULL}; 651da177e4SLinus Torvalds static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; 661da177e4SLinus Torvalds static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8}; 671da177e4SLinus Torvalds //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; 68c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS 69a67ff6a5SRusty Russell static bool hrtimer = 1; 70c631d03cSTakashi Iwai #endif 71a67ff6a5SRusty Russell static bool fake_buffer = 1; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds module_param_array(index, int, NULL, 0444); 741da177e4SLinus Torvalds MODULE_PARM_DESC(index, "Index value for dummy soundcard."); 751da177e4SLinus Torvalds module_param_array(id, charp, NULL, 0444); 761da177e4SLinus Torvalds MODULE_PARM_DESC(id, "ID string for dummy soundcard."); 771da177e4SLinus Torvalds module_param_array(enable, bool, NULL, 0444); 781da177e4SLinus Torvalds MODULE_PARM_DESC(enable, "Enable this dummy soundcard."); 79d5e1ca05SJaroslav Kysela module_param_array(model, charp, NULL, 0444); 80d5e1ca05SJaroslav Kysela MODULE_PARM_DESC(model, "Soundcard model."); 811da177e4SLinus Torvalds module_param_array(pcm_devs, int, NULL, 0444); 821da177e4SLinus Torvalds MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver."); 831da177e4SLinus Torvalds module_param_array(pcm_substreams, int, NULL, 0444); 8423aebca4STakashi Iwai MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-128) for dummy driver."); 851da177e4SLinus Torvalds //module_param_array(midi_devs, int, NULL, 0444); 861da177e4SLinus Torvalds //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver."); 87a68c4d11STakashi Iwai module_param(fake_buffer, bool, 0444); 88a68c4d11STakashi Iwai MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations."); 89c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS 90*ddce57a6STakashi Iwai module_param(hrtimer, bool, 0644); 91c631d03cSTakashi Iwai MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source."); 92c631d03cSTakashi Iwai #endif 931da177e4SLinus Torvalds 94f7a9275dSClemens Ladisch static struct platform_device *devices[SNDRV_CARDS]; 95f7a9275dSClemens Ladisch 961da177e4SLinus Torvalds #define MIXER_ADDR_MASTER 0 971da177e4SLinus Torvalds #define MIXER_ADDR_LINE 1 981da177e4SLinus Torvalds #define MIXER_ADDR_MIC 2 991da177e4SLinus Torvalds #define MIXER_ADDR_SYNTH 3 1001da177e4SLinus Torvalds #define MIXER_ADDR_CD 4 1011da177e4SLinus Torvalds #define MIXER_ADDR_LAST 4 1021da177e4SLinus Torvalds 103c631d03cSTakashi Iwai struct dummy_timer_ops { 104c631d03cSTakashi Iwai int (*create)(struct snd_pcm_substream *); 105c631d03cSTakashi Iwai void (*free)(struct snd_pcm_substream *); 106c631d03cSTakashi Iwai int (*prepare)(struct snd_pcm_substream *); 107c631d03cSTakashi Iwai int (*start)(struct snd_pcm_substream *); 108c631d03cSTakashi Iwai int (*stop)(struct snd_pcm_substream *); 109c631d03cSTakashi Iwai snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *); 110c631d03cSTakashi Iwai }; 111c631d03cSTakashi Iwai 112*ddce57a6STakashi Iwai #define get_dummy_ops(substream) \ 113*ddce57a6STakashi Iwai (*(const struct dummy_timer_ops **)(substream)->runtime->private_data) 114*ddce57a6STakashi Iwai 115d5e1ca05SJaroslav Kysela struct dummy_model { 116d5e1ca05SJaroslav Kysela const char *name; 117d5e1ca05SJaroslav Kysela int (*playback_constraints)(struct snd_pcm_runtime *runtime); 118d5e1ca05SJaroslav Kysela int (*capture_constraints)(struct snd_pcm_runtime *runtime); 119d5e1ca05SJaroslav Kysela u64 formats; 120d5e1ca05SJaroslav Kysela size_t buffer_bytes_max; 121d5e1ca05SJaroslav Kysela size_t period_bytes_min; 122d5e1ca05SJaroslav Kysela size_t period_bytes_max; 123d5e1ca05SJaroslav Kysela unsigned int periods_min; 124d5e1ca05SJaroslav Kysela unsigned int periods_max; 125d5e1ca05SJaroslav Kysela unsigned int rates; 126d5e1ca05SJaroslav Kysela unsigned int rate_min; 127d5e1ca05SJaroslav Kysela unsigned int rate_max; 128d5e1ca05SJaroslav Kysela unsigned int channels_min; 129d5e1ca05SJaroslav Kysela unsigned int channels_max; 130d5e1ca05SJaroslav Kysela }; 131d5e1ca05SJaroslav Kysela 1324a4d2cfdSTakashi Iwai struct snd_dummy { 1334a4d2cfdSTakashi Iwai struct snd_card *card; 134d5e1ca05SJaroslav Kysela struct dummy_model *model; 1356e65c1ccSTakashi Iwai struct snd_pcm *pcm; 136d5e1ca05SJaroslav Kysela struct snd_pcm_hardware pcm_hw; 1371da177e4SLinus Torvalds spinlock_t mixer_lock; 1381da177e4SLinus Torvalds int mixer_volume[MIXER_ADDR_LAST+1][2]; 1391da177e4SLinus Torvalds int capture_source[MIXER_ADDR_LAST+1][2]; 14016e43467SClemens Ladisch int iobox; 14116e43467SClemens Ladisch struct snd_kcontrol *cd_volume_ctl; 14216e43467SClemens Ladisch struct snd_kcontrol *cd_switch_ctl; 1434a4d2cfdSTakashi Iwai }; 1441da177e4SLinus Torvalds 145c631d03cSTakashi Iwai /* 146d5e1ca05SJaroslav Kysela * card models 147d5e1ca05SJaroslav Kysela */ 148d5e1ca05SJaroslav Kysela 149d5e1ca05SJaroslav Kysela static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime) 150d5e1ca05SJaroslav Kysela { 151d5e1ca05SJaroslav Kysela int err; 152d5e1ca05SJaroslav Kysela err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 153d5e1ca05SJaroslav Kysela if (err < 0) 154d5e1ca05SJaroslav Kysela return err; 155d5e1ca05SJaroslav Kysela err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX); 156d5e1ca05SJaroslav Kysela if (err < 0) 157d5e1ca05SJaroslav Kysela return err; 158d5e1ca05SJaroslav Kysela return 0; 159d5e1ca05SJaroslav Kysela } 160d5e1ca05SJaroslav Kysela 161e4c28688STakashi Iwai static struct dummy_model model_emu10k1 = { 162d5e1ca05SJaroslav Kysela .name = "emu10k1", 163d5e1ca05SJaroslav Kysela .playback_constraints = emu10k1_playback_constraints, 164d5e1ca05SJaroslav Kysela .buffer_bytes_max = 128 * 1024, 165d5e1ca05SJaroslav Kysela }; 166d5e1ca05SJaroslav Kysela 167e4c28688STakashi Iwai static struct dummy_model model_rme9652 = { 168d5e1ca05SJaroslav Kysela .name = "rme9652", 169d5e1ca05SJaroslav Kysela .buffer_bytes_max = 26 * 64 * 1024, 170d5e1ca05SJaroslav Kysela .formats = SNDRV_PCM_FMTBIT_S32_LE, 171d5e1ca05SJaroslav Kysela .channels_min = 26, 172d5e1ca05SJaroslav Kysela .channels_max = 26, 173d5e1ca05SJaroslav Kysela .periods_min = 2, 174d5e1ca05SJaroslav Kysela .periods_max = 2, 175d5e1ca05SJaroslav Kysela }; 176d5e1ca05SJaroslav Kysela 177e4c28688STakashi Iwai static struct dummy_model model_ice1712 = { 178d5e1ca05SJaroslav Kysela .name = "ice1712", 179d5e1ca05SJaroslav Kysela .buffer_bytes_max = 256 * 1024, 180d5e1ca05SJaroslav Kysela .formats = SNDRV_PCM_FMTBIT_S32_LE, 181d5e1ca05SJaroslav Kysela .channels_min = 10, 182d5e1ca05SJaroslav Kysela .channels_max = 10, 183d5e1ca05SJaroslav Kysela .periods_min = 1, 184d5e1ca05SJaroslav Kysela .periods_max = 1024, 185d5e1ca05SJaroslav Kysela }; 186d5e1ca05SJaroslav Kysela 187e4c28688STakashi Iwai static struct dummy_model model_uda1341 = { 188d5e1ca05SJaroslav Kysela .name = "uda1341", 189d5e1ca05SJaroslav Kysela .buffer_bytes_max = 16380, 190d5e1ca05SJaroslav Kysela .formats = SNDRV_PCM_FMTBIT_S16_LE, 191d5e1ca05SJaroslav Kysela .channels_min = 2, 192d5e1ca05SJaroslav Kysela .channels_max = 2, 193d5e1ca05SJaroslav Kysela .periods_min = 2, 194d5e1ca05SJaroslav Kysela .periods_max = 255, 195d5e1ca05SJaroslav Kysela }; 196d5e1ca05SJaroslav Kysela 197e4c28688STakashi Iwai static struct dummy_model model_ac97 = { 198d5e1ca05SJaroslav Kysela .name = "ac97", 199d5e1ca05SJaroslav Kysela .formats = SNDRV_PCM_FMTBIT_S16_LE, 200d5e1ca05SJaroslav Kysela .channels_min = 2, 201d5e1ca05SJaroslav Kysela .channels_max = 2, 202d5e1ca05SJaroslav Kysela .rates = SNDRV_PCM_RATE_48000, 203d5e1ca05SJaroslav Kysela .rate_min = 48000, 204d5e1ca05SJaroslav Kysela .rate_max = 48000, 205d5e1ca05SJaroslav Kysela }; 206d5e1ca05SJaroslav Kysela 207e4c28688STakashi Iwai static struct dummy_model model_ca0106 = { 208d5e1ca05SJaroslav Kysela .name = "ca0106", 209d5e1ca05SJaroslav Kysela .formats = SNDRV_PCM_FMTBIT_S16_LE, 210d5e1ca05SJaroslav Kysela .buffer_bytes_max = ((65536-64)*8), 211d5e1ca05SJaroslav Kysela .period_bytes_max = (65536-64), 212d5e1ca05SJaroslav Kysela .periods_min = 2, 213d5e1ca05SJaroslav Kysela .periods_max = 8, 214d5e1ca05SJaroslav Kysela .channels_min = 2, 215d5e1ca05SJaroslav Kysela .channels_max = 2, 216d5e1ca05SJaroslav Kysela .rates = SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000, 217d5e1ca05SJaroslav Kysela .rate_min = 48000, 218d5e1ca05SJaroslav Kysela .rate_max = 192000, 219d5e1ca05SJaroslav Kysela }; 220d5e1ca05SJaroslav Kysela 221e4c28688STakashi Iwai static struct dummy_model *dummy_models[] = { 222d5e1ca05SJaroslav Kysela &model_emu10k1, 223d5e1ca05SJaroslav Kysela &model_rme9652, 224d5e1ca05SJaroslav Kysela &model_ice1712, 225d5e1ca05SJaroslav Kysela &model_uda1341, 226d5e1ca05SJaroslav Kysela &model_ac97, 227d5e1ca05SJaroslav Kysela &model_ca0106, 228d5e1ca05SJaroslav Kysela NULL 229d5e1ca05SJaroslav Kysela }; 230d5e1ca05SJaroslav Kysela 231d5e1ca05SJaroslav Kysela /* 232c631d03cSTakashi Iwai * system timer interface 233c631d03cSTakashi Iwai */ 234c631d03cSTakashi Iwai 235c631d03cSTakashi Iwai struct dummy_systimer_pcm { 236*ddce57a6STakashi Iwai /* ops must be the first item */ 237*ddce57a6STakashi Iwai const struct dummy_timer_ops *timer_ops; 2381da177e4SLinus Torvalds spinlock_t lock; 2391da177e4SLinus Torvalds struct timer_list timer; 240b142037bSTakashi Iwai unsigned long base_time; 241b142037bSTakashi Iwai unsigned int frac_pos; /* fractional sample position (based HZ) */ 242b5d10781STakashi Iwai unsigned int frac_period_rest; 243b142037bSTakashi Iwai unsigned int frac_buffer_size; /* buffer_size * HZ */ 244b142037bSTakashi Iwai unsigned int frac_period_size; /* period_size * HZ */ 245b142037bSTakashi Iwai unsigned int rate; 246b5d10781STakashi Iwai int elapsed; 2474a4d2cfdSTakashi Iwai struct snd_pcm_substream *substream; 2484a4d2cfdSTakashi Iwai }; 2491da177e4SLinus Torvalds 250b142037bSTakashi Iwai static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm) 251b142037bSTakashi Iwai { 2522a52b6eeSRoman Kollar mod_timer(&dpcm->timer, jiffies + 2532a52b6eeSRoman Kollar (dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate); 254b142037bSTakashi Iwai } 255b142037bSTakashi Iwai 256b142037bSTakashi Iwai static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm) 257b142037bSTakashi Iwai { 258b142037bSTakashi Iwai unsigned long delta; 259b142037bSTakashi Iwai 260b142037bSTakashi Iwai delta = jiffies - dpcm->base_time; 261b142037bSTakashi Iwai if (!delta) 262b142037bSTakashi Iwai return; 263b5d10781STakashi Iwai dpcm->base_time += delta; 264b5d10781STakashi Iwai delta *= dpcm->rate; 265b5d10781STakashi Iwai dpcm->frac_pos += delta; 266b142037bSTakashi Iwai while (dpcm->frac_pos >= dpcm->frac_buffer_size) 267b142037bSTakashi Iwai dpcm->frac_pos -= dpcm->frac_buffer_size; 268b5d10781STakashi Iwai while (dpcm->frac_period_rest <= delta) { 269b5d10781STakashi Iwai dpcm->elapsed++; 270b5d10781STakashi Iwai dpcm->frac_period_rest += dpcm->frac_period_size; 271b5d10781STakashi Iwai } 272b5d10781STakashi Iwai dpcm->frac_period_rest -= delta; 273b142037bSTakashi Iwai } 274b142037bSTakashi Iwai 275c631d03cSTakashi Iwai static int dummy_systimer_start(struct snd_pcm_substream *substream) 2761da177e4SLinus Torvalds { 277c631d03cSTakashi Iwai struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; 278c631d03cSTakashi Iwai spin_lock(&dpcm->lock); 279b142037bSTakashi Iwai dpcm->base_time = jiffies; 280b142037bSTakashi Iwai dummy_systimer_rearm(dpcm); 281c8eb6ba1STakashi Iwai spin_unlock(&dpcm->lock); 2826e65c1ccSTakashi Iwai return 0; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds 285c631d03cSTakashi Iwai static int dummy_systimer_stop(struct snd_pcm_substream *substream) 286c631d03cSTakashi Iwai { 287c631d03cSTakashi Iwai struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; 288c631d03cSTakashi Iwai spin_lock(&dpcm->lock); 289c631d03cSTakashi Iwai del_timer(&dpcm->timer); 290c631d03cSTakashi Iwai spin_unlock(&dpcm->lock); 291c631d03cSTakashi Iwai return 0; 292c631d03cSTakashi Iwai } 293c631d03cSTakashi Iwai 294c631d03cSTakashi Iwai static int dummy_systimer_prepare(struct snd_pcm_substream *substream) 2951da177e4SLinus Torvalds { 2964a4d2cfdSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 297c631d03cSTakashi Iwai struct dummy_systimer_pcm *dpcm = runtime->private_data; 2981da177e4SLinus Torvalds 299b142037bSTakashi Iwai dpcm->frac_pos = 0; 300b142037bSTakashi Iwai dpcm->rate = runtime->rate; 301b142037bSTakashi Iwai dpcm->frac_buffer_size = runtime->buffer_size * HZ; 302b142037bSTakashi Iwai dpcm->frac_period_size = runtime->period_size * HZ; 303b5d10781STakashi Iwai dpcm->frac_period_rest = dpcm->frac_period_size; 304b5d10781STakashi Iwai dpcm->elapsed = 0; 30553463a83SAhmet İnan 3061da177e4SLinus Torvalds return 0; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 309c631d03cSTakashi Iwai static void dummy_systimer_callback(unsigned long data) 3101da177e4SLinus Torvalds { 311c631d03cSTakashi Iwai struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data; 312b32425acSTakashi Iwai unsigned long flags; 313b5d10781STakashi Iwai int elapsed = 0; 3141da177e4SLinus Torvalds 315b32425acSTakashi Iwai spin_lock_irqsave(&dpcm->lock, flags); 316b142037bSTakashi Iwai dummy_systimer_update(dpcm); 317b142037bSTakashi Iwai dummy_systimer_rearm(dpcm); 318b5d10781STakashi Iwai elapsed = dpcm->elapsed; 319b5d10781STakashi Iwai dpcm->elapsed = 0; 320b32425acSTakashi Iwai spin_unlock_irqrestore(&dpcm->lock, flags); 321b5d10781STakashi Iwai if (elapsed) 3221da177e4SLinus Torvalds snd_pcm_period_elapsed(dpcm->substream); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 325c631d03cSTakashi Iwai static snd_pcm_uframes_t 326c631d03cSTakashi Iwai dummy_systimer_pointer(struct snd_pcm_substream *substream) 3271da177e4SLinus Torvalds { 328b142037bSTakashi Iwai struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; 329b5d10781STakashi Iwai snd_pcm_uframes_t pos; 3301da177e4SLinus Torvalds 331b142037bSTakashi Iwai spin_lock(&dpcm->lock); 332b142037bSTakashi Iwai dummy_systimer_update(dpcm); 333b5d10781STakashi Iwai pos = dpcm->frac_pos / HZ; 334b142037bSTakashi Iwai spin_unlock(&dpcm->lock); 335b5d10781STakashi Iwai return pos; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 338c631d03cSTakashi Iwai static int dummy_systimer_create(struct snd_pcm_substream *substream) 3391da177e4SLinus Torvalds { 340c631d03cSTakashi Iwai struct dummy_systimer_pcm *dpcm; 3411da177e4SLinus Torvalds 342c631d03cSTakashi Iwai dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); 343c631d03cSTakashi Iwai if (!dpcm) 344c631d03cSTakashi Iwai return -ENOMEM; 345c631d03cSTakashi Iwai substream->runtime->private_data = dpcm; 3462a52b6eeSRoman Kollar setup_timer(&dpcm->timer, dummy_systimer_callback, 3472a52b6eeSRoman Kollar (unsigned long) dpcm); 348c631d03cSTakashi Iwai spin_lock_init(&dpcm->lock); 349c631d03cSTakashi Iwai dpcm->substream = substream; 350c631d03cSTakashi Iwai return 0; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 353c631d03cSTakashi Iwai static void dummy_systimer_free(struct snd_pcm_substream *substream) 354c631d03cSTakashi Iwai { 355c631d03cSTakashi Iwai kfree(substream->runtime->private_data); 356c631d03cSTakashi Iwai } 357c631d03cSTakashi Iwai 358d8c5ed75SJulia Lawall static const struct dummy_timer_ops dummy_systimer_ops = { 359c631d03cSTakashi Iwai .create = dummy_systimer_create, 360c631d03cSTakashi Iwai .free = dummy_systimer_free, 361c631d03cSTakashi Iwai .prepare = dummy_systimer_prepare, 362c631d03cSTakashi Iwai .start = dummy_systimer_start, 363c631d03cSTakashi Iwai .stop = dummy_systimer_stop, 364c631d03cSTakashi Iwai .pointer = dummy_systimer_pointer, 365c631d03cSTakashi Iwai }; 366c631d03cSTakashi Iwai 367c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS 368c631d03cSTakashi Iwai /* 369c631d03cSTakashi Iwai * hrtimer interface 370c631d03cSTakashi Iwai */ 371c631d03cSTakashi Iwai 372c631d03cSTakashi Iwai struct dummy_hrtimer_pcm { 373*ddce57a6STakashi Iwai /* ops must be the first item */ 374*ddce57a6STakashi Iwai const struct dummy_timer_ops *timer_ops; 375c631d03cSTakashi Iwai ktime_t base_time; 376c631d03cSTakashi Iwai ktime_t period_time; 377c631d03cSTakashi Iwai atomic_t running; 378c631d03cSTakashi Iwai struct hrtimer timer; 379c631d03cSTakashi Iwai struct tasklet_struct tasklet; 380c631d03cSTakashi Iwai struct snd_pcm_substream *substream; 381c631d03cSTakashi Iwai }; 382c631d03cSTakashi Iwai 383c631d03cSTakashi Iwai static void dummy_hrtimer_pcm_elapsed(unsigned long priv) 384c631d03cSTakashi Iwai { 385c631d03cSTakashi Iwai struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv; 386c631d03cSTakashi Iwai if (atomic_read(&dpcm->running)) 387c631d03cSTakashi Iwai snd_pcm_period_elapsed(dpcm->substream); 388c631d03cSTakashi Iwai } 389c631d03cSTakashi Iwai 390c631d03cSTakashi Iwai static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer) 391c631d03cSTakashi Iwai { 392c631d03cSTakashi Iwai struct dummy_hrtimer_pcm *dpcm; 393c631d03cSTakashi Iwai 394c631d03cSTakashi Iwai dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer); 395c631d03cSTakashi Iwai if (!atomic_read(&dpcm->running)) 396c631d03cSTakashi Iwai return HRTIMER_NORESTART; 397c631d03cSTakashi Iwai tasklet_schedule(&dpcm->tasklet); 398c631d03cSTakashi Iwai hrtimer_forward_now(timer, dpcm->period_time); 399c631d03cSTakashi Iwai return HRTIMER_RESTART; 400c631d03cSTakashi Iwai } 401c631d03cSTakashi Iwai 402c631d03cSTakashi Iwai static int dummy_hrtimer_start(struct snd_pcm_substream *substream) 403c631d03cSTakashi Iwai { 404c631d03cSTakashi Iwai struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; 405c631d03cSTakashi Iwai 406c631d03cSTakashi Iwai dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer); 407c631d03cSTakashi Iwai hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL); 408c631d03cSTakashi Iwai atomic_set(&dpcm->running, 1); 409c631d03cSTakashi Iwai return 0; 410c631d03cSTakashi Iwai } 411c631d03cSTakashi Iwai 412c631d03cSTakashi Iwai static int dummy_hrtimer_stop(struct snd_pcm_substream *substream) 413c631d03cSTakashi Iwai { 414c631d03cSTakashi Iwai struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; 415c631d03cSTakashi Iwai 416c631d03cSTakashi Iwai atomic_set(&dpcm->running, 0); 417c631d03cSTakashi Iwai hrtimer_cancel(&dpcm->timer); 418c631d03cSTakashi Iwai return 0; 419c631d03cSTakashi Iwai } 420c631d03cSTakashi Iwai 421c631d03cSTakashi Iwai static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm) 422c631d03cSTakashi Iwai { 423c631d03cSTakashi Iwai tasklet_kill(&dpcm->tasklet); 424c631d03cSTakashi Iwai } 425c631d03cSTakashi Iwai 426c631d03cSTakashi Iwai static snd_pcm_uframes_t 427c631d03cSTakashi Iwai dummy_hrtimer_pointer(struct snd_pcm_substream *substream) 428c631d03cSTakashi Iwai { 429c631d03cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 430c631d03cSTakashi Iwai struct dummy_hrtimer_pcm *dpcm = runtime->private_data; 431c631d03cSTakashi Iwai u64 delta; 432c631d03cSTakashi Iwai u32 pos; 433c631d03cSTakashi Iwai 434c631d03cSTakashi Iwai delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer), 435c631d03cSTakashi Iwai dpcm->base_time); 436c631d03cSTakashi Iwai delta = div_u64(delta * runtime->rate + 999999, 1000000); 437c631d03cSTakashi Iwai div_u64_rem(delta, runtime->buffer_size, &pos); 438c631d03cSTakashi Iwai return pos; 439c631d03cSTakashi Iwai } 440c631d03cSTakashi Iwai 441c631d03cSTakashi Iwai static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream) 442c631d03cSTakashi Iwai { 443c631d03cSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 444c631d03cSTakashi Iwai struct dummy_hrtimer_pcm *dpcm = runtime->private_data; 445c631d03cSTakashi Iwai unsigned int period, rate; 446c631d03cSTakashi Iwai long sec; 447c631d03cSTakashi Iwai unsigned long nsecs; 448c631d03cSTakashi Iwai 449c631d03cSTakashi Iwai dummy_hrtimer_sync(dpcm); 450c631d03cSTakashi Iwai period = runtime->period_size; 451c631d03cSTakashi Iwai rate = runtime->rate; 452c631d03cSTakashi Iwai sec = period / rate; 453c631d03cSTakashi Iwai period %= rate; 454c631d03cSTakashi Iwai nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate); 455c631d03cSTakashi Iwai dpcm->period_time = ktime_set(sec, nsecs); 456c631d03cSTakashi Iwai 457c631d03cSTakashi Iwai return 0; 458c631d03cSTakashi Iwai } 459c631d03cSTakashi Iwai 460c631d03cSTakashi Iwai static int dummy_hrtimer_create(struct snd_pcm_substream *substream) 461c631d03cSTakashi Iwai { 462c631d03cSTakashi Iwai struct dummy_hrtimer_pcm *dpcm; 463c631d03cSTakashi Iwai 464c631d03cSTakashi Iwai dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); 465c631d03cSTakashi Iwai if (!dpcm) 466c631d03cSTakashi Iwai return -ENOMEM; 467c631d03cSTakashi Iwai substream->runtime->private_data = dpcm; 468c631d03cSTakashi Iwai hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 469c631d03cSTakashi Iwai dpcm->timer.function = dummy_hrtimer_callback; 470c631d03cSTakashi Iwai dpcm->substream = substream; 471c631d03cSTakashi Iwai atomic_set(&dpcm->running, 0); 472c631d03cSTakashi Iwai tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed, 473c631d03cSTakashi Iwai (unsigned long)dpcm); 474c631d03cSTakashi Iwai return 0; 475c631d03cSTakashi Iwai } 476c631d03cSTakashi Iwai 477c631d03cSTakashi Iwai static void dummy_hrtimer_free(struct snd_pcm_substream *substream) 478c631d03cSTakashi Iwai { 479c631d03cSTakashi Iwai struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; 480c631d03cSTakashi Iwai dummy_hrtimer_sync(dpcm); 481c631d03cSTakashi Iwai kfree(dpcm); 482c631d03cSTakashi Iwai } 483c631d03cSTakashi Iwai 484d8c5ed75SJulia Lawall static const struct dummy_timer_ops dummy_hrtimer_ops = { 485c631d03cSTakashi Iwai .create = dummy_hrtimer_create, 486c631d03cSTakashi Iwai .free = dummy_hrtimer_free, 487c631d03cSTakashi Iwai .prepare = dummy_hrtimer_prepare, 488c631d03cSTakashi Iwai .start = dummy_hrtimer_start, 489c631d03cSTakashi Iwai .stop = dummy_hrtimer_stop, 490c631d03cSTakashi Iwai .pointer = dummy_hrtimer_pointer, 491c631d03cSTakashi Iwai }; 492c631d03cSTakashi Iwai 493c631d03cSTakashi Iwai #endif /* CONFIG_HIGH_RES_TIMERS */ 494c631d03cSTakashi Iwai 495c631d03cSTakashi Iwai /* 496c631d03cSTakashi Iwai * PCM interface 497c631d03cSTakashi Iwai */ 498c631d03cSTakashi Iwai 499c631d03cSTakashi Iwai static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd) 500c631d03cSTakashi Iwai { 501c631d03cSTakashi Iwai switch (cmd) { 502c631d03cSTakashi Iwai case SNDRV_PCM_TRIGGER_START: 503c631d03cSTakashi Iwai case SNDRV_PCM_TRIGGER_RESUME: 504*ddce57a6STakashi Iwai return get_dummy_ops(substream)->start(substream); 505c631d03cSTakashi Iwai case SNDRV_PCM_TRIGGER_STOP: 506c631d03cSTakashi Iwai case SNDRV_PCM_TRIGGER_SUSPEND: 507*ddce57a6STakashi Iwai return get_dummy_ops(substream)->stop(substream); 508c631d03cSTakashi Iwai } 509c631d03cSTakashi Iwai return -EINVAL; 510c631d03cSTakashi Iwai } 511c631d03cSTakashi Iwai 512c631d03cSTakashi Iwai static int dummy_pcm_prepare(struct snd_pcm_substream *substream) 513c631d03cSTakashi Iwai { 514*ddce57a6STakashi Iwai return get_dummy_ops(substream)->prepare(substream); 515c631d03cSTakashi Iwai } 516c631d03cSTakashi Iwai 517c631d03cSTakashi Iwai static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream) 518c631d03cSTakashi Iwai { 519*ddce57a6STakashi Iwai return get_dummy_ops(substream)->pointer(substream); 520c631d03cSTakashi Iwai } 521c631d03cSTakashi Iwai 522c631d03cSTakashi Iwai static struct snd_pcm_hardware dummy_pcm_hardware = { 523c631d03cSTakashi Iwai .info = (SNDRV_PCM_INFO_MMAP | 524c631d03cSTakashi Iwai SNDRV_PCM_INFO_INTERLEAVED | 525c631d03cSTakashi Iwai SNDRV_PCM_INFO_RESUME | 526c631d03cSTakashi Iwai SNDRV_PCM_INFO_MMAP_VALID), 527c631d03cSTakashi Iwai .formats = USE_FORMATS, 528c631d03cSTakashi Iwai .rates = USE_RATE, 529c631d03cSTakashi Iwai .rate_min = USE_RATE_MIN, 530c631d03cSTakashi Iwai .rate_max = USE_RATE_MAX, 531c631d03cSTakashi Iwai .channels_min = USE_CHANNELS_MIN, 532c631d03cSTakashi Iwai .channels_max = USE_CHANNELS_MAX, 533c631d03cSTakashi Iwai .buffer_bytes_max = MAX_BUFFER_SIZE, 534d5e1ca05SJaroslav Kysela .period_bytes_min = MIN_PERIOD_SIZE, 535c631d03cSTakashi Iwai .period_bytes_max = MAX_PERIOD_SIZE, 536c631d03cSTakashi Iwai .periods_min = USE_PERIODS_MIN, 537c631d03cSTakashi Iwai .periods_max = USE_PERIODS_MAX, 538c631d03cSTakashi Iwai .fifo_size = 0, 539c631d03cSTakashi Iwai }; 540c631d03cSTakashi Iwai 541c631d03cSTakashi Iwai static int dummy_pcm_hw_params(struct snd_pcm_substream *substream, 5424a4d2cfdSTakashi Iwai struct snd_pcm_hw_params *hw_params) 5431da177e4SLinus Torvalds { 544a68c4d11STakashi Iwai if (fake_buffer) { 545a68c4d11STakashi Iwai /* runtime->dma_bytes has to be set manually to allow mmap */ 546a68c4d11STakashi Iwai substream->runtime->dma_bytes = params_buffer_bytes(hw_params); 547a68c4d11STakashi Iwai return 0; 548a68c4d11STakashi Iwai } 549c631d03cSTakashi Iwai return snd_pcm_lib_malloc_pages(substream, 550c631d03cSTakashi Iwai params_buffer_bytes(hw_params)); 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds 553c631d03cSTakashi Iwai static int dummy_pcm_hw_free(struct snd_pcm_substream *substream) 5541da177e4SLinus Torvalds { 555a68c4d11STakashi Iwai if (fake_buffer) 556a68c4d11STakashi Iwai return 0; 5571da177e4SLinus Torvalds return snd_pcm_lib_free_pages(substream); 5581da177e4SLinus Torvalds } 5591da177e4SLinus Torvalds 560c631d03cSTakashi Iwai static int dummy_pcm_open(struct snd_pcm_substream *substream) 561c8eb6ba1STakashi Iwai { 562c631d03cSTakashi Iwai struct snd_dummy *dummy = snd_pcm_substream_chip(substream); 563d5e1ca05SJaroslav Kysela struct dummy_model *model = dummy->model; 5644a4d2cfdSTakashi Iwai struct snd_pcm_runtime *runtime = substream->runtime; 565*ddce57a6STakashi Iwai const struct dummy_timer_ops *ops; 5661da177e4SLinus Torvalds int err; 5671da177e4SLinus Torvalds 568*ddce57a6STakashi Iwai ops = &dummy_systimer_ops; 569c631d03cSTakashi Iwai #ifdef CONFIG_HIGH_RES_TIMERS 570c631d03cSTakashi Iwai if (hrtimer) 571*ddce57a6STakashi Iwai ops = &dummy_hrtimer_ops; 572c631d03cSTakashi Iwai #endif 573c631d03cSTakashi Iwai 574*ddce57a6STakashi Iwai err = ops->create(substream); 575c631d03cSTakashi Iwai if (err < 0) 576c631d03cSTakashi Iwai return err; 577*ddce57a6STakashi Iwai get_dummy_ops(substream) = ops; 578c631d03cSTakashi Iwai 579d5e1ca05SJaroslav Kysela runtime->hw = dummy->pcm_hw; 5801da177e4SLinus Torvalds if (substream->pcm->device & 1) { 5811da177e4SLinus Torvalds runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED; 5821da177e4SLinus Torvalds runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds if (substream->pcm->device & 2) 585c631d03cSTakashi Iwai runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP | 586c631d03cSTakashi Iwai SNDRV_PCM_INFO_MMAP_VALID); 587c631d03cSTakashi Iwai 588d5e1ca05SJaroslav Kysela if (model == NULL) 589d5e1ca05SJaroslav Kysela return 0; 590d5e1ca05SJaroslav Kysela 591d5e1ca05SJaroslav Kysela if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 592d5e1ca05SJaroslav Kysela if (model->playback_constraints) 593d5e1ca05SJaroslav Kysela err = model->playback_constraints(substream->runtime); 594d5e1ca05SJaroslav Kysela } else { 595d5e1ca05SJaroslav Kysela if (model->capture_constraints) 596d5e1ca05SJaroslav Kysela err = model->capture_constraints(substream->runtime); 597d5e1ca05SJaroslav Kysela } 598c631d03cSTakashi Iwai if (err < 0) { 599*ddce57a6STakashi Iwai get_dummy_ops(substream)->free(substream); 6001da177e4SLinus Torvalds return err; 601c631d03cSTakashi Iwai } 6021da177e4SLinus Torvalds return 0; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 605c631d03cSTakashi Iwai static int dummy_pcm_close(struct snd_pcm_substream *substream) 6061da177e4SLinus Torvalds { 607*ddce57a6STakashi Iwai get_dummy_ops(substream)->free(substream); 6081da177e4SLinus Torvalds return 0; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 611a68c4d11STakashi Iwai /* 612a68c4d11STakashi Iwai * dummy buffer handling 613a68c4d11STakashi Iwai */ 614a68c4d11STakashi Iwai 615a68c4d11STakashi Iwai static void *dummy_page[2]; 616a68c4d11STakashi Iwai 617a68c4d11STakashi Iwai static void free_fake_buffer(void) 618a68c4d11STakashi Iwai { 619a68c4d11STakashi Iwai if (fake_buffer) { 620a68c4d11STakashi Iwai int i; 621a68c4d11STakashi Iwai for (i = 0; i < 2; i++) 622a68c4d11STakashi Iwai if (dummy_page[i]) { 623a68c4d11STakashi Iwai free_page((unsigned long)dummy_page[i]); 624a68c4d11STakashi Iwai dummy_page[i] = NULL; 625a68c4d11STakashi Iwai } 626a68c4d11STakashi Iwai } 627a68c4d11STakashi Iwai } 628a68c4d11STakashi Iwai 629a68c4d11STakashi Iwai static int alloc_fake_buffer(void) 630a68c4d11STakashi Iwai { 631a68c4d11STakashi Iwai int i; 632a68c4d11STakashi Iwai 633a68c4d11STakashi Iwai if (!fake_buffer) 634a68c4d11STakashi Iwai return 0; 635a68c4d11STakashi Iwai for (i = 0; i < 2; i++) { 636a68c4d11STakashi Iwai dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL); 637a68c4d11STakashi Iwai if (!dummy_page[i]) { 638a68c4d11STakashi Iwai free_fake_buffer(); 639a68c4d11STakashi Iwai return -ENOMEM; 640a68c4d11STakashi Iwai } 641a68c4d11STakashi Iwai } 642a68c4d11STakashi Iwai return 0; 643a68c4d11STakashi Iwai } 644a68c4d11STakashi Iwai 645a68c4d11STakashi Iwai static int dummy_pcm_copy(struct snd_pcm_substream *substream, 646a68c4d11STakashi Iwai int channel, snd_pcm_uframes_t pos, 647a68c4d11STakashi Iwai void __user *dst, snd_pcm_uframes_t count) 648a68c4d11STakashi Iwai { 649a68c4d11STakashi Iwai return 0; /* do nothing */ 650a68c4d11STakashi Iwai } 651a68c4d11STakashi Iwai 652a68c4d11STakashi Iwai static int dummy_pcm_silence(struct snd_pcm_substream *substream, 653a68c4d11STakashi Iwai int channel, snd_pcm_uframes_t pos, 654a68c4d11STakashi Iwai snd_pcm_uframes_t count) 655a68c4d11STakashi Iwai { 656a68c4d11STakashi Iwai return 0; /* do nothing */ 657a68c4d11STakashi Iwai } 658a68c4d11STakashi Iwai 659a68c4d11STakashi Iwai static struct page *dummy_pcm_page(struct snd_pcm_substream *substream, 660a68c4d11STakashi Iwai unsigned long offset) 661a68c4d11STakashi Iwai { 662a68c4d11STakashi Iwai return virt_to_page(dummy_page[substream->stream]); /* the same page */ 663a68c4d11STakashi Iwai } 664a68c4d11STakashi Iwai 665c631d03cSTakashi Iwai static struct snd_pcm_ops dummy_pcm_ops = { 666c631d03cSTakashi Iwai .open = dummy_pcm_open, 667c631d03cSTakashi Iwai .close = dummy_pcm_close, 6681da177e4SLinus Torvalds .ioctl = snd_pcm_lib_ioctl, 669c631d03cSTakashi Iwai .hw_params = dummy_pcm_hw_params, 670c631d03cSTakashi Iwai .hw_free = dummy_pcm_hw_free, 671c631d03cSTakashi Iwai .prepare = dummy_pcm_prepare, 672c631d03cSTakashi Iwai .trigger = dummy_pcm_trigger, 673c631d03cSTakashi Iwai .pointer = dummy_pcm_pointer, 6741da177e4SLinus Torvalds }; 6751da177e4SLinus Torvalds 676a68c4d11STakashi Iwai static struct snd_pcm_ops dummy_pcm_ops_no_buf = { 677a68c4d11STakashi Iwai .open = dummy_pcm_open, 678a68c4d11STakashi Iwai .close = dummy_pcm_close, 679a68c4d11STakashi Iwai .ioctl = snd_pcm_lib_ioctl, 680a68c4d11STakashi Iwai .hw_params = dummy_pcm_hw_params, 681a68c4d11STakashi Iwai .hw_free = dummy_pcm_hw_free, 682a68c4d11STakashi Iwai .prepare = dummy_pcm_prepare, 683a68c4d11STakashi Iwai .trigger = dummy_pcm_trigger, 684a68c4d11STakashi Iwai .pointer = dummy_pcm_pointer, 685a68c4d11STakashi Iwai .copy = dummy_pcm_copy, 686a68c4d11STakashi Iwai .silence = dummy_pcm_silence, 687a68c4d11STakashi Iwai .page = dummy_pcm_page, 688a68c4d11STakashi Iwai }; 689a68c4d11STakashi Iwai 690fbbb01a1SBill Pemberton static int snd_card_dummy_pcm(struct snd_dummy *dummy, int device, 691788c6043SPrarit Bhargava int substreams) 6921da177e4SLinus Torvalds { 6934a4d2cfdSTakashi Iwai struct snd_pcm *pcm; 694a68c4d11STakashi Iwai struct snd_pcm_ops *ops; 6951da177e4SLinus Torvalds int err; 6961da177e4SLinus Torvalds 6971a11cb64SJaroslav Kysela err = snd_pcm_new(dummy->card, "Dummy PCM", device, 6981a11cb64SJaroslav Kysela substreams, substreams, &pcm); 6991a11cb64SJaroslav Kysela if (err < 0) 7001da177e4SLinus Torvalds return err; 7016e65c1ccSTakashi Iwai dummy->pcm = pcm; 702a68c4d11STakashi Iwai if (fake_buffer) 703a68c4d11STakashi Iwai ops = &dummy_pcm_ops_no_buf; 704a68c4d11STakashi Iwai else 705a68c4d11STakashi Iwai ops = &dummy_pcm_ops; 706a68c4d11STakashi Iwai snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops); 707a68c4d11STakashi Iwai snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops); 7081da177e4SLinus Torvalds pcm->private_data = dummy; 7091da177e4SLinus Torvalds pcm->info_flags = 0; 7101da177e4SLinus Torvalds strcpy(pcm->name, "Dummy PCM"); 711a68c4d11STakashi Iwai if (!fake_buffer) { 712a68c4d11STakashi Iwai snd_pcm_lib_preallocate_pages_for_all(pcm, 713a68c4d11STakashi Iwai SNDRV_DMA_TYPE_CONTINUOUS, 7141da177e4SLinus Torvalds snd_dma_continuous_data(GFP_KERNEL), 7151da177e4SLinus Torvalds 0, 64*1024); 716a68c4d11STakashi Iwai } 7171da177e4SLinus Torvalds return 0; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 7209b151fecSTakashi Iwai /* 7219b151fecSTakashi Iwai * mixer interface 7229b151fecSTakashi Iwai */ 7239b151fecSTakashi Iwai 7241da177e4SLinus Torvalds #define DUMMY_VOLUME(xname, xindex, addr) \ 725fb567a8eSTakashi Iwai { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ 726fb567a8eSTakashi Iwai .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ 727fb567a8eSTakashi Iwai .name = xname, .index = xindex, \ 7281da177e4SLinus Torvalds .info = snd_dummy_volume_info, \ 7291da177e4SLinus Torvalds .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \ 730fb567a8eSTakashi Iwai .private_value = addr, \ 731fb567a8eSTakashi Iwai .tlv = { .p = db_scale_dummy } } 7321da177e4SLinus Torvalds 7334a4d2cfdSTakashi Iwai static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol, 7344a4d2cfdSTakashi Iwai struct snd_ctl_elem_info *uinfo) 7351da177e4SLinus Torvalds { 7361da177e4SLinus Torvalds uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 7371da177e4SLinus Torvalds uinfo->count = 2; 7381da177e4SLinus Torvalds uinfo->value.integer.min = -50; 7391da177e4SLinus Torvalds uinfo->value.integer.max = 100; 7401da177e4SLinus Torvalds return 0; 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds 7434a4d2cfdSTakashi Iwai static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol, 7444a4d2cfdSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 7451da177e4SLinus Torvalds { 7464a4d2cfdSTakashi Iwai struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 7471da177e4SLinus Torvalds int addr = kcontrol->private_value; 7481da177e4SLinus Torvalds 749c8eb6ba1STakashi Iwai spin_lock_irq(&dummy->mixer_lock); 7501da177e4SLinus Torvalds ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0]; 7511da177e4SLinus Torvalds ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1]; 752c8eb6ba1STakashi Iwai spin_unlock_irq(&dummy->mixer_lock); 7531da177e4SLinus Torvalds return 0; 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 7564a4d2cfdSTakashi Iwai static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol, 7574a4d2cfdSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 7581da177e4SLinus Torvalds { 7594a4d2cfdSTakashi Iwai struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 7601da177e4SLinus Torvalds int change, addr = kcontrol->private_value; 7611da177e4SLinus Torvalds int left, right; 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds left = ucontrol->value.integer.value[0]; 7641da177e4SLinus Torvalds if (left < -50) 7651da177e4SLinus Torvalds left = -50; 7661da177e4SLinus Torvalds if (left > 100) 7671da177e4SLinus Torvalds left = 100; 7681da177e4SLinus Torvalds right = ucontrol->value.integer.value[1]; 7691da177e4SLinus Torvalds if (right < -50) 7701da177e4SLinus Torvalds right = -50; 7711da177e4SLinus Torvalds if (right > 100) 7721da177e4SLinus Torvalds right = 100; 773c8eb6ba1STakashi Iwai spin_lock_irq(&dummy->mixer_lock); 7741da177e4SLinus Torvalds change = dummy->mixer_volume[addr][0] != left || 7751da177e4SLinus Torvalds dummy->mixer_volume[addr][1] != right; 7761da177e4SLinus Torvalds dummy->mixer_volume[addr][0] = left; 7771da177e4SLinus Torvalds dummy->mixer_volume[addr][1] = right; 778c8eb6ba1STakashi Iwai spin_unlock_irq(&dummy->mixer_lock); 7791da177e4SLinus Torvalds return change; 7801da177e4SLinus Torvalds } 7811da177e4SLinus Torvalds 7820cb29ea0STakashi Iwai static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0); 783fb567a8eSTakashi Iwai 7841da177e4SLinus Torvalds #define DUMMY_CAPSRC(xname, xindex, addr) \ 7851da177e4SLinus Torvalds { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ 7861da177e4SLinus Torvalds .info = snd_dummy_capsrc_info, \ 7871da177e4SLinus Torvalds .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \ 7881da177e4SLinus Torvalds .private_value = addr } 7891da177e4SLinus Torvalds 790a5ce8890STakashi Iwai #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info 7911da177e4SLinus Torvalds 7924a4d2cfdSTakashi Iwai static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol, 7934a4d2cfdSTakashi Iwai struct snd_ctl_elem_value *ucontrol) 7941da177e4SLinus Torvalds { 7954a4d2cfdSTakashi Iwai struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 7961da177e4SLinus Torvalds int addr = kcontrol->private_value; 7971da177e4SLinus Torvalds 798c8eb6ba1STakashi Iwai spin_lock_irq(&dummy->mixer_lock); 7991da177e4SLinus Torvalds ucontrol->value.integer.value[0] = dummy->capture_source[addr][0]; 8001da177e4SLinus Torvalds ucontrol->value.integer.value[1] = dummy->capture_source[addr][1]; 801c8eb6ba1STakashi Iwai spin_unlock_irq(&dummy->mixer_lock); 8021da177e4SLinus Torvalds return 0; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 8054a4d2cfdSTakashi Iwai static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 8061da177e4SLinus Torvalds { 8074a4d2cfdSTakashi Iwai struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 8081da177e4SLinus Torvalds int change, addr = kcontrol->private_value; 8091da177e4SLinus Torvalds int left, right; 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds left = ucontrol->value.integer.value[0] & 1; 8121da177e4SLinus Torvalds right = ucontrol->value.integer.value[1] & 1; 813c8eb6ba1STakashi Iwai spin_lock_irq(&dummy->mixer_lock); 8141da177e4SLinus Torvalds change = dummy->capture_source[addr][0] != left && 8151da177e4SLinus Torvalds dummy->capture_source[addr][1] != right; 8161da177e4SLinus Torvalds dummy->capture_source[addr][0] = left; 8171da177e4SLinus Torvalds dummy->capture_source[addr][1] = right; 818c8eb6ba1STakashi Iwai spin_unlock_irq(&dummy->mixer_lock); 8191da177e4SLinus Torvalds return change; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 82216e43467SClemens Ladisch static int snd_dummy_iobox_info(struct snd_kcontrol *kcontrol, 82316e43467SClemens Ladisch struct snd_ctl_elem_info *info) 82416e43467SClemens Ladisch { 82516e43467SClemens Ladisch const char *const names[] = { "None", "CD Player" }; 82616e43467SClemens Ladisch 82716e43467SClemens Ladisch return snd_ctl_enum_info(info, 1, 2, names); 82816e43467SClemens Ladisch } 82916e43467SClemens Ladisch 83016e43467SClemens Ladisch static int snd_dummy_iobox_get(struct snd_kcontrol *kcontrol, 83116e43467SClemens Ladisch struct snd_ctl_elem_value *value) 83216e43467SClemens Ladisch { 83316e43467SClemens Ladisch struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 83416e43467SClemens Ladisch 83516e43467SClemens Ladisch value->value.enumerated.item[0] = dummy->iobox; 83616e43467SClemens Ladisch return 0; 83716e43467SClemens Ladisch } 83816e43467SClemens Ladisch 83916e43467SClemens Ladisch static int snd_dummy_iobox_put(struct snd_kcontrol *kcontrol, 84016e43467SClemens Ladisch struct snd_ctl_elem_value *value) 84116e43467SClemens Ladisch { 84216e43467SClemens Ladisch struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); 84316e43467SClemens Ladisch int changed; 84416e43467SClemens Ladisch 84516e43467SClemens Ladisch if (value->value.enumerated.item[0] > 1) 84616e43467SClemens Ladisch return -EINVAL; 84716e43467SClemens Ladisch 84816e43467SClemens Ladisch changed = value->value.enumerated.item[0] != dummy->iobox; 84916e43467SClemens Ladisch if (changed) { 85016e43467SClemens Ladisch dummy->iobox = value->value.enumerated.item[0]; 85116e43467SClemens Ladisch 85216e43467SClemens Ladisch if (dummy->iobox) { 85316e43467SClemens Ladisch dummy->cd_volume_ctl->vd[0].access &= 85416e43467SClemens Ladisch ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; 85516e43467SClemens Ladisch dummy->cd_switch_ctl->vd[0].access &= 85616e43467SClemens Ladisch ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; 85716e43467SClemens Ladisch } else { 85816e43467SClemens Ladisch dummy->cd_volume_ctl->vd[0].access |= 85916e43467SClemens Ladisch SNDRV_CTL_ELEM_ACCESS_INACTIVE; 86016e43467SClemens Ladisch dummy->cd_switch_ctl->vd[0].access |= 86116e43467SClemens Ladisch SNDRV_CTL_ELEM_ACCESS_INACTIVE; 86216e43467SClemens Ladisch } 86316e43467SClemens Ladisch 86416e43467SClemens Ladisch snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO, 86516e43467SClemens Ladisch &dummy->cd_volume_ctl->id); 86616e43467SClemens Ladisch snd_ctl_notify(dummy->card, SNDRV_CTL_EVENT_MASK_INFO, 86716e43467SClemens Ladisch &dummy->cd_switch_ctl->id); 86816e43467SClemens Ladisch } 86916e43467SClemens Ladisch 87016e43467SClemens Ladisch return changed; 87116e43467SClemens Ladisch } 87216e43467SClemens Ladisch 8734a4d2cfdSTakashi Iwai static struct snd_kcontrol_new snd_dummy_controls[] = { 8741da177e4SLinus Torvalds DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER), 8751da177e4SLinus Torvalds DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER), 8761da177e4SLinus Torvalds DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH), 877e05d6964STakashi Iwai DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH), 8781da177e4SLinus Torvalds DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE), 879e05d6964STakashi Iwai DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE), 8801da177e4SLinus Torvalds DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC), 881e05d6964STakashi Iwai DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC), 8821da177e4SLinus Torvalds DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD), 88316e43467SClemens Ladisch DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD), 88416e43467SClemens Ladisch { 88516e43467SClemens Ladisch .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 88616e43467SClemens Ladisch .name = "External I/O Box", 88716e43467SClemens Ladisch .info = snd_dummy_iobox_info, 88816e43467SClemens Ladisch .get = snd_dummy_iobox_get, 88916e43467SClemens Ladisch .put = snd_dummy_iobox_put, 89016e43467SClemens Ladisch }, 8911da177e4SLinus Torvalds }; 8921da177e4SLinus Torvalds 893fbbb01a1SBill Pemberton static int snd_card_dummy_new_mixer(struct snd_dummy *dummy) 8941da177e4SLinus Torvalds { 8954a4d2cfdSTakashi Iwai struct snd_card *card = dummy->card; 89616e43467SClemens Ladisch struct snd_kcontrol *kcontrol; 8971da177e4SLinus Torvalds unsigned int idx; 8981da177e4SLinus Torvalds int err; 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds spin_lock_init(&dummy->mixer_lock); 9011da177e4SLinus Torvalds strcpy(card->mixername, "Dummy Mixer"); 90216e43467SClemens Ladisch dummy->iobox = 1; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) { 90516e43467SClemens Ladisch kcontrol = snd_ctl_new1(&snd_dummy_controls[idx], dummy); 90616e43467SClemens Ladisch err = snd_ctl_add(card, kcontrol); 9071a11cb64SJaroslav Kysela if (err < 0) 9081da177e4SLinus Torvalds return err; 90916e43467SClemens Ladisch if (!strcmp(kcontrol->id.name, "CD Volume")) 91016e43467SClemens Ladisch dummy->cd_volume_ctl = kcontrol; 91116e43467SClemens Ladisch else if (!strcmp(kcontrol->id.name, "CD Capture Switch")) 91216e43467SClemens Ladisch dummy->cd_switch_ctl = kcontrol; 91316e43467SClemens Ladisch 9141da177e4SLinus Torvalds } 9151da177e4SLinus Torvalds return 0; 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 918129a4c9fSTakashi Iwai #if defined(CONFIG_SND_DEBUG) && defined(CONFIG_SND_PROC_FS) 9199b151fecSTakashi Iwai /* 9209b151fecSTakashi Iwai * proc interface 9219b151fecSTakashi Iwai */ 922d5e1ca05SJaroslav Kysela static void print_formats(struct snd_dummy *dummy, 923d5e1ca05SJaroslav Kysela struct snd_info_buffer *buffer) 9249b151fecSTakashi Iwai { 9259b151fecSTakashi Iwai int i; 9269b151fecSTakashi Iwai 9279b151fecSTakashi Iwai for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) { 928d5e1ca05SJaroslav Kysela if (dummy->pcm_hw.formats & (1ULL << i)) 9299b151fecSTakashi Iwai snd_iprintf(buffer, " %s", snd_pcm_format_name(i)); 9309b151fecSTakashi Iwai } 9319b151fecSTakashi Iwai } 9329b151fecSTakashi Iwai 933d5e1ca05SJaroslav Kysela static void print_rates(struct snd_dummy *dummy, 934d5e1ca05SJaroslav Kysela struct snd_info_buffer *buffer) 9359b151fecSTakashi Iwai { 9369b151fecSTakashi Iwai static int rates[] = { 9379b151fecSTakashi Iwai 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000, 9389b151fecSTakashi Iwai 64000, 88200, 96000, 176400, 192000, 9399b151fecSTakashi Iwai }; 9409b151fecSTakashi Iwai int i; 9419b151fecSTakashi Iwai 942d5e1ca05SJaroslav Kysela if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_CONTINUOUS) 9439b151fecSTakashi Iwai snd_iprintf(buffer, " continuous"); 944d5e1ca05SJaroslav Kysela if (dummy->pcm_hw.rates & SNDRV_PCM_RATE_KNOT) 9459b151fecSTakashi Iwai snd_iprintf(buffer, " knot"); 9469b151fecSTakashi Iwai for (i = 0; i < ARRAY_SIZE(rates); i++) 947d5e1ca05SJaroslav Kysela if (dummy->pcm_hw.rates & (1 << i)) 9489b151fecSTakashi Iwai snd_iprintf(buffer, " %d", rates[i]); 9499b151fecSTakashi Iwai } 9509b151fecSTakashi Iwai 951d5e1ca05SJaroslav Kysela #define get_dummy_int_ptr(dummy, ofs) \ 952d5e1ca05SJaroslav Kysela (unsigned int *)((char *)&((dummy)->pcm_hw) + (ofs)) 953d5e1ca05SJaroslav Kysela #define get_dummy_ll_ptr(dummy, ofs) \ 954d5e1ca05SJaroslav Kysela (unsigned long long *)((char *)&((dummy)->pcm_hw) + (ofs)) 9559b151fecSTakashi Iwai 9569b151fecSTakashi Iwai struct dummy_hw_field { 9579b151fecSTakashi Iwai const char *name; 9589b151fecSTakashi Iwai const char *format; 9599b151fecSTakashi Iwai unsigned int offset; 9609b151fecSTakashi Iwai unsigned int size; 9619b151fecSTakashi Iwai }; 9629b151fecSTakashi Iwai #define FIELD_ENTRY(item, fmt) { \ 9639b151fecSTakashi Iwai .name = #item, \ 9649b151fecSTakashi Iwai .format = fmt, \ 9659b151fecSTakashi Iwai .offset = offsetof(struct snd_pcm_hardware, item), \ 9669b151fecSTakashi Iwai .size = sizeof(dummy_pcm_hardware.item) } 9679b151fecSTakashi Iwai 9689b151fecSTakashi Iwai static struct dummy_hw_field fields[] = { 9699b151fecSTakashi Iwai FIELD_ENTRY(formats, "%#llx"), 9709b151fecSTakashi Iwai FIELD_ENTRY(rates, "%#x"), 9719b151fecSTakashi Iwai FIELD_ENTRY(rate_min, "%d"), 9729b151fecSTakashi Iwai FIELD_ENTRY(rate_max, "%d"), 9739b151fecSTakashi Iwai FIELD_ENTRY(channels_min, "%d"), 9749b151fecSTakashi Iwai FIELD_ENTRY(channels_max, "%d"), 9759b151fecSTakashi Iwai FIELD_ENTRY(buffer_bytes_max, "%ld"), 9769b151fecSTakashi Iwai FIELD_ENTRY(period_bytes_min, "%ld"), 9779b151fecSTakashi Iwai FIELD_ENTRY(period_bytes_max, "%ld"), 9789b151fecSTakashi Iwai FIELD_ENTRY(periods_min, "%d"), 9799b151fecSTakashi Iwai FIELD_ENTRY(periods_max, "%d"), 9809b151fecSTakashi Iwai }; 9819b151fecSTakashi Iwai 9829b151fecSTakashi Iwai static void dummy_proc_read(struct snd_info_entry *entry, 9839b151fecSTakashi Iwai struct snd_info_buffer *buffer) 9849b151fecSTakashi Iwai { 985d5e1ca05SJaroslav Kysela struct snd_dummy *dummy = entry->private_data; 9869b151fecSTakashi Iwai int i; 9879b151fecSTakashi Iwai 9889b151fecSTakashi Iwai for (i = 0; i < ARRAY_SIZE(fields); i++) { 9899b151fecSTakashi Iwai snd_iprintf(buffer, "%s ", fields[i].name); 9909b151fecSTakashi Iwai if (fields[i].size == sizeof(int)) 9919b151fecSTakashi Iwai snd_iprintf(buffer, fields[i].format, 992d5e1ca05SJaroslav Kysela *get_dummy_int_ptr(dummy, fields[i].offset)); 9939b151fecSTakashi Iwai else 9949b151fecSTakashi Iwai snd_iprintf(buffer, fields[i].format, 995d5e1ca05SJaroslav Kysela *get_dummy_ll_ptr(dummy, fields[i].offset)); 9969b151fecSTakashi Iwai if (!strcmp(fields[i].name, "formats")) 997d5e1ca05SJaroslav Kysela print_formats(dummy, buffer); 9989b151fecSTakashi Iwai else if (!strcmp(fields[i].name, "rates")) 999d5e1ca05SJaroslav Kysela print_rates(dummy, buffer); 10009b151fecSTakashi Iwai snd_iprintf(buffer, "\n"); 10019b151fecSTakashi Iwai } 10029b151fecSTakashi Iwai } 10039b151fecSTakashi Iwai 10049b151fecSTakashi Iwai static void dummy_proc_write(struct snd_info_entry *entry, 10059b151fecSTakashi Iwai struct snd_info_buffer *buffer) 10069b151fecSTakashi Iwai { 1007d5e1ca05SJaroslav Kysela struct snd_dummy *dummy = entry->private_data; 10089b151fecSTakashi Iwai char line[64]; 10099b151fecSTakashi Iwai 10109b151fecSTakashi Iwai while (!snd_info_get_line(buffer, line, sizeof(line))) { 10119b151fecSTakashi Iwai char item[20]; 10129b151fecSTakashi Iwai const char *ptr; 10139b151fecSTakashi Iwai unsigned long long val; 10149b151fecSTakashi Iwai int i; 10159b151fecSTakashi Iwai 10169b151fecSTakashi Iwai ptr = snd_info_get_str(item, line, sizeof(item)); 10179b151fecSTakashi Iwai for (i = 0; i < ARRAY_SIZE(fields); i++) { 10189b151fecSTakashi Iwai if (!strcmp(item, fields[i].name)) 10199b151fecSTakashi Iwai break; 10209b151fecSTakashi Iwai } 10219b151fecSTakashi Iwai if (i >= ARRAY_SIZE(fields)) 10229b151fecSTakashi Iwai continue; 10239b151fecSTakashi Iwai snd_info_get_str(item, ptr, sizeof(item)); 1024b785a492SJingoo Han if (kstrtoull(item, 0, &val)) 10259b151fecSTakashi Iwai continue; 10269b151fecSTakashi Iwai if (fields[i].size == sizeof(int)) 1027d5e1ca05SJaroslav Kysela *get_dummy_int_ptr(dummy, fields[i].offset) = val; 10289b151fecSTakashi Iwai else 1029d5e1ca05SJaroslav Kysela *get_dummy_ll_ptr(dummy, fields[i].offset) = val; 10309b151fecSTakashi Iwai } 10319b151fecSTakashi Iwai } 10329b151fecSTakashi Iwai 1033fbbb01a1SBill Pemberton static void dummy_proc_init(struct snd_dummy *chip) 10349b151fecSTakashi Iwai { 10359b151fecSTakashi Iwai struct snd_info_entry *entry; 10369b151fecSTakashi Iwai 10379b151fecSTakashi Iwai if (!snd_card_proc_new(chip->card, "dummy_pcm", &entry)) { 10389b151fecSTakashi Iwai snd_info_set_text_ops(entry, chip, dummy_proc_read); 10399b151fecSTakashi Iwai entry->c.text.write = dummy_proc_write; 10409b151fecSTakashi Iwai entry->mode |= S_IWUSR; 1041d5e1ca05SJaroslav Kysela entry->private_data = chip; 10429b151fecSTakashi Iwai } 10439b151fecSTakashi Iwai } 10449b151fecSTakashi Iwai #else 10459b151fecSTakashi Iwai #define dummy_proc_init(x) 1046129a4c9fSTakashi Iwai #endif /* CONFIG_SND_DEBUG && CONFIG_SND_PROC_FS */ 10479b151fecSTakashi Iwai 1048fbbb01a1SBill Pemberton static int snd_dummy_probe(struct platform_device *devptr) 10491da177e4SLinus Torvalds { 10504a4d2cfdSTakashi Iwai struct snd_card *card; 10514a4d2cfdSTakashi Iwai struct snd_dummy *dummy; 1052d5e1ca05SJaroslav Kysela struct dummy_model *m = NULL, **mdl; 10531da177e4SLinus Torvalds int idx, err; 10546e65c1ccSTakashi Iwai int dev = devptr->id; 10551da177e4SLinus Torvalds 10565872f3f6STakashi Iwai err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE, 1057bd7dd77cSTakashi Iwai sizeof(struct snd_dummy), &card); 1058bd7dd77cSTakashi Iwai if (err < 0) 1059bd7dd77cSTakashi Iwai return err; 10604a4d2cfdSTakashi Iwai dummy = card->private_data; 10611da177e4SLinus Torvalds dummy->card = card; 1062d5e1ca05SJaroslav Kysela for (mdl = dummy_models; *mdl && model[dev]; mdl++) { 1063d5e1ca05SJaroslav Kysela if (strcmp(model[dev], (*mdl)->name) == 0) { 1064d5e1ca05SJaroslav Kysela printk(KERN_INFO 1065d5e1ca05SJaroslav Kysela "snd-dummy: Using model '%s' for card %i\n", 1066d5e1ca05SJaroslav Kysela (*mdl)->name, card->number); 1067d5e1ca05SJaroslav Kysela m = dummy->model = *mdl; 1068d5e1ca05SJaroslav Kysela break; 1069d5e1ca05SJaroslav Kysela } 1070d5e1ca05SJaroslav Kysela } 10711da177e4SLinus Torvalds for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) { 10721da177e4SLinus Torvalds if (pcm_substreams[dev] < 1) 10731da177e4SLinus Torvalds pcm_substreams[dev] = 1; 10741da177e4SLinus Torvalds if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS) 10751da177e4SLinus Torvalds pcm_substreams[dev] = MAX_PCM_SUBSTREAMS; 10761a11cb64SJaroslav Kysela err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]); 10771a11cb64SJaroslav Kysela if (err < 0) 10781da177e4SLinus Torvalds goto __nodev; 10791da177e4SLinus Torvalds } 1080d5e1ca05SJaroslav Kysela 1081d5e1ca05SJaroslav Kysela dummy->pcm_hw = dummy_pcm_hardware; 1082d5e1ca05SJaroslav Kysela if (m) { 1083d5e1ca05SJaroslav Kysela if (m->formats) 1084d5e1ca05SJaroslav Kysela dummy->pcm_hw.formats = m->formats; 1085d5e1ca05SJaroslav Kysela if (m->buffer_bytes_max) 1086d5e1ca05SJaroslav Kysela dummy->pcm_hw.buffer_bytes_max = m->buffer_bytes_max; 1087d5e1ca05SJaroslav Kysela if (m->period_bytes_min) 1088d5e1ca05SJaroslav Kysela dummy->pcm_hw.period_bytes_min = m->period_bytes_min; 1089d5e1ca05SJaroslav Kysela if (m->period_bytes_max) 1090d5e1ca05SJaroslav Kysela dummy->pcm_hw.period_bytes_max = m->period_bytes_max; 1091d5e1ca05SJaroslav Kysela if (m->periods_min) 1092d5e1ca05SJaroslav Kysela dummy->pcm_hw.periods_min = m->periods_min; 1093d5e1ca05SJaroslav Kysela if (m->periods_max) 1094d5e1ca05SJaroslav Kysela dummy->pcm_hw.periods_max = m->periods_max; 1095d5e1ca05SJaroslav Kysela if (m->rates) 1096d5e1ca05SJaroslav Kysela dummy->pcm_hw.rates = m->rates; 1097d5e1ca05SJaroslav Kysela if (m->rate_min) 1098d5e1ca05SJaroslav Kysela dummy->pcm_hw.rate_min = m->rate_min; 1099d5e1ca05SJaroslav Kysela if (m->rate_max) 1100d5e1ca05SJaroslav Kysela dummy->pcm_hw.rate_max = m->rate_max; 1101d5e1ca05SJaroslav Kysela if (m->channels_min) 1102d5e1ca05SJaroslav Kysela dummy->pcm_hw.channels_min = m->channels_min; 1103d5e1ca05SJaroslav Kysela if (m->channels_max) 1104d5e1ca05SJaroslav Kysela dummy->pcm_hw.channels_max = m->channels_max; 1105d5e1ca05SJaroslav Kysela } 1106d5e1ca05SJaroslav Kysela 11071a11cb64SJaroslav Kysela err = snd_card_dummy_new_mixer(dummy); 11081a11cb64SJaroslav Kysela if (err < 0) 11091da177e4SLinus Torvalds goto __nodev; 11101da177e4SLinus Torvalds strcpy(card->driver, "Dummy"); 11111da177e4SLinus Torvalds strcpy(card->shortname, "Dummy"); 11121da177e4SLinus Torvalds sprintf(card->longname, "Dummy %i", dev + 1); 111316dab54bSTakashi Iwai 11149b151fecSTakashi Iwai dummy_proc_init(dummy); 11159b151fecSTakashi Iwai 11161a11cb64SJaroslav Kysela err = snd_card_register(card); 11171a11cb64SJaroslav Kysela if (err == 0) { 11186e65c1ccSTakashi Iwai platform_set_drvdata(devptr, card); 11191da177e4SLinus Torvalds return 0; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds __nodev: 11221da177e4SLinus Torvalds snd_card_free(card); 11231da177e4SLinus Torvalds return err; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 1126fbbb01a1SBill Pemberton static int snd_dummy_remove(struct platform_device *devptr) 11276e65c1ccSTakashi Iwai { 11286e65c1ccSTakashi Iwai snd_card_free(platform_get_drvdata(devptr)); 11296e65c1ccSTakashi Iwai return 0; 11306e65c1ccSTakashi Iwai } 11316e65c1ccSTakashi Iwai 1132d34e4e00STakashi Iwai #ifdef CONFIG_PM_SLEEP 1133284e7ca7STakashi Iwai static int snd_dummy_suspend(struct device *pdev) 11346e65c1ccSTakashi Iwai { 1135284e7ca7STakashi Iwai struct snd_card *card = dev_get_drvdata(pdev); 11366e65c1ccSTakashi Iwai struct snd_dummy *dummy = card->private_data; 11376e65c1ccSTakashi Iwai 11386e65c1ccSTakashi Iwai snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 11396e65c1ccSTakashi Iwai snd_pcm_suspend_all(dummy->pcm); 11406e65c1ccSTakashi Iwai return 0; 11416e65c1ccSTakashi Iwai } 11426e65c1ccSTakashi Iwai 1143284e7ca7STakashi Iwai static int snd_dummy_resume(struct device *pdev) 11446e65c1ccSTakashi Iwai { 1145284e7ca7STakashi Iwai struct snd_card *card = dev_get_drvdata(pdev); 11466e65c1ccSTakashi Iwai 11476e65c1ccSTakashi Iwai snd_power_change_state(card, SNDRV_CTL_POWER_D0); 11486e65c1ccSTakashi Iwai return 0; 11496e65c1ccSTakashi Iwai } 1150284e7ca7STakashi Iwai 1151284e7ca7STakashi Iwai static SIMPLE_DEV_PM_OPS(snd_dummy_pm, snd_dummy_suspend, snd_dummy_resume); 1152284e7ca7STakashi Iwai #define SND_DUMMY_PM_OPS &snd_dummy_pm 1153284e7ca7STakashi Iwai #else 1154284e7ca7STakashi Iwai #define SND_DUMMY_PM_OPS NULL 11556e65c1ccSTakashi Iwai #endif 11566e65c1ccSTakashi Iwai 11576e65c1ccSTakashi Iwai #define SND_DUMMY_DRIVER "snd_dummy" 11586e65c1ccSTakashi Iwai 11596e65c1ccSTakashi Iwai static struct platform_driver snd_dummy_driver = { 11606e65c1ccSTakashi Iwai .probe = snd_dummy_probe, 1161fbbb01a1SBill Pemberton .remove = snd_dummy_remove, 11626e65c1ccSTakashi Iwai .driver = { 11638bf01d8aSTakashi Iwai .name = SND_DUMMY_DRIVER, 1164284e7ca7STakashi Iwai .pm = SND_DUMMY_PM_OPS, 11656e65c1ccSTakashi Iwai }, 11666e65c1ccSTakashi Iwai }; 11676e65c1ccSTakashi Iwai 1168c12aad6eSRandy Dunlap static void snd_dummy_unregister_all(void) 1169f7a9275dSClemens Ladisch { 1170f7a9275dSClemens Ladisch int i; 1171f7a9275dSClemens Ladisch 1172f7a9275dSClemens Ladisch for (i = 0; i < ARRAY_SIZE(devices); ++i) 1173f7a9275dSClemens Ladisch platform_device_unregister(devices[i]); 1174f7a9275dSClemens Ladisch platform_driver_unregister(&snd_dummy_driver); 1175a68c4d11STakashi Iwai free_fake_buffer(); 1176f7a9275dSClemens Ladisch } 1177f7a9275dSClemens Ladisch 11781da177e4SLinus Torvalds static int __init alsa_card_dummy_init(void) 11791da177e4SLinus Torvalds { 11806e65c1ccSTakashi Iwai int i, cards, err; 11811da177e4SLinus Torvalds 11821a11cb64SJaroslav Kysela err = platform_driver_register(&snd_dummy_driver); 11831a11cb64SJaroslav Kysela if (err < 0) 11846e65c1ccSTakashi Iwai return err; 11856e65c1ccSTakashi Iwai 1186a68c4d11STakashi Iwai err = alloc_fake_buffer(); 1187a68c4d11STakashi Iwai if (err < 0) { 1188a68c4d11STakashi Iwai platform_driver_unregister(&snd_dummy_driver); 1189a68c4d11STakashi Iwai return err; 1190a68c4d11STakashi Iwai } 1191a68c4d11STakashi Iwai 11926e65c1ccSTakashi Iwai cards = 0; 11938278ca8fSTakashi Iwai for (i = 0; i < SNDRV_CARDS; i++) { 11946e65c1ccSTakashi Iwai struct platform_device *device; 11958278ca8fSTakashi Iwai if (! enable[i]) 11968278ca8fSTakashi Iwai continue; 11976e65c1ccSTakashi Iwai device = platform_device_register_simple(SND_DUMMY_DRIVER, 11986e65c1ccSTakashi Iwai i, NULL, 0); 1199a182ee98SRene Herman if (IS_ERR(device)) 1200a182ee98SRene Herman continue; 12017152447dSRene Herman if (!platform_get_drvdata(device)) { 12027152447dSRene Herman platform_device_unregister(device); 12037152447dSRene Herman continue; 12047152447dSRene Herman } 1205f7a9275dSClemens Ladisch devices[i] = device; 12061da177e4SLinus Torvalds cards++; 12071da177e4SLinus Torvalds } 12081da177e4SLinus Torvalds if (!cards) { 12091da177e4SLinus Torvalds #ifdef MODULE 12101da177e4SLinus Torvalds printk(KERN_ERR "Dummy soundcard not found or device busy\n"); 12111da177e4SLinus Torvalds #endif 1212a182ee98SRene Herman snd_dummy_unregister_all(); 1213a182ee98SRene Herman return -ENODEV; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds return 0; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds static void __exit alsa_card_dummy_exit(void) 12191da177e4SLinus Torvalds { 1220f7a9275dSClemens Ladisch snd_dummy_unregister_all(); 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds module_init(alsa_card_dummy_init) 12241da177e4SLinus Torvalds module_exit(alsa_card_dummy_exit) 1225