Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Dummy soundcard |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | */ |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/init.h> |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 22 | #include <linux/err.h> |
| 23 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/jiffies.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/time.h> |
| 27 | #include <linux/wait.h> |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 28 | #include <linux/hrtimer.h> |
| 29 | #include <linux/math64.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/moduleparam.h> |
| 31 | #include <sound/core.h> |
| 32 | #include <sound/control.h> |
Takashi Iwai | fb567a8 | 2006-08-23 13:07:19 +0200 | [diff] [blame] | 33 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <sound/pcm.h> |
| 35 | #include <sound/rawmidi.h> |
| 36 | #include <sound/initval.h> |
| 37 | |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 38 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | MODULE_DESCRIPTION("Dummy soundcard (/dev/null)"); |
| 40 | MODULE_LICENSE("GPL"); |
| 41 | MODULE_SUPPORTED_DEVICE("{{ALSA,Dummy soundcard}}"); |
| 42 | |
| 43 | #define MAX_PCM_DEVICES 4 |
| 44 | #define MAX_PCM_SUBSTREAMS 16 |
| 45 | #define MAX_MIDI_DEVICES 2 |
| 46 | |
| 47 | #if 0 /* emu10k1 emulation */ |
| 48 | #define MAX_BUFFER_SIZE (128 * 1024) |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 49 | static int emu10k1_playback_constraints(struct snd_pcm_runtime *runtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | int err; |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 52 | err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); |
| 53 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | return err; |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 55 | err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX); |
Mariusz Kozlowski | e78521f | 2008-10-19 10:34:22 +0200 | [diff] [blame] | 56 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | return err; |
| 58 | return 0; |
| 59 | } |
| 60 | #define add_playback_constraints emu10k1_playback_constraints |
| 61 | #endif |
| 62 | |
| 63 | #if 0 /* RME9652 emulation */ |
| 64 | #define MAX_BUFFER_SIZE (26 * 64 * 1024) |
| 65 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE |
| 66 | #define USE_CHANNELS_MIN 26 |
| 67 | #define USE_CHANNELS_MAX 26 |
| 68 | #define USE_PERIODS_MIN 2 |
| 69 | #define USE_PERIODS_MAX 2 |
| 70 | #endif |
| 71 | |
| 72 | #if 0 /* ICE1712 emulation */ |
| 73 | #define MAX_BUFFER_SIZE (256 * 1024) |
| 74 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S32_LE |
| 75 | #define USE_CHANNELS_MIN 10 |
| 76 | #define USE_CHANNELS_MAX 10 |
| 77 | #define USE_PERIODS_MIN 1 |
| 78 | #define USE_PERIODS_MAX 1024 |
| 79 | #endif |
| 80 | |
| 81 | #if 0 /* UDA1341 emulation */ |
| 82 | #define MAX_BUFFER_SIZE (16380) |
| 83 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE |
| 84 | #define USE_CHANNELS_MIN 2 |
| 85 | #define USE_CHANNELS_MAX 2 |
| 86 | #define USE_PERIODS_MIN 2 |
| 87 | #define USE_PERIODS_MAX 255 |
| 88 | #endif |
| 89 | |
| 90 | #if 0 /* simple AC97 bridge (intel8x0) with 48kHz AC97 only codec */ |
| 91 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE |
| 92 | #define USE_CHANNELS_MIN 2 |
| 93 | #define USE_CHANNELS_MAX 2 |
| 94 | #define USE_RATE SNDRV_PCM_RATE_48000 |
| 95 | #define USE_RATE_MIN 48000 |
| 96 | #define USE_RATE_MAX 48000 |
| 97 | #endif |
| 98 | |
Jaroslav Kysela | 2ad5dd8 | 2006-01-03 14:27:21 +0100 | [diff] [blame] | 99 | #if 0 /* CA0106 */ |
| 100 | #define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE |
| 101 | #define USE_CHANNELS_MIN 2 |
| 102 | #define USE_CHANNELS_MAX 2 |
| 103 | #define USE_RATE (SNDRV_PCM_RATE_48000|SNDRV_PCM_RATE_96000|SNDRV_PCM_RATE_192000) |
| 104 | #define USE_RATE_MIN 48000 |
| 105 | #define USE_RATE_MAX 192000 |
| 106 | #define MAX_BUFFER_SIZE ((65536-64)*8) |
| 107 | #define MAX_PERIOD_SIZE (65536-64) |
| 108 | #define USE_PERIODS_MIN 2 |
| 109 | #define USE_PERIODS_MAX 8 |
| 110 | #endif |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | /* defaults */ |
| 114 | #ifndef MAX_BUFFER_SIZE |
| 115 | #define MAX_BUFFER_SIZE (64*1024) |
| 116 | #endif |
Jaroslav Kysela | 2ad5dd8 | 2006-01-03 14:27:21 +0100 | [diff] [blame] | 117 | #ifndef MAX_PERIOD_SIZE |
| 118 | #define MAX_PERIOD_SIZE MAX_BUFFER_SIZE |
| 119 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | #ifndef USE_FORMATS |
| 121 | #define USE_FORMATS (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE) |
| 122 | #endif |
| 123 | #ifndef USE_RATE |
| 124 | #define USE_RATE SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000 |
| 125 | #define USE_RATE_MIN 5500 |
| 126 | #define USE_RATE_MAX 48000 |
| 127 | #endif |
| 128 | #ifndef USE_CHANNELS_MIN |
| 129 | #define USE_CHANNELS_MIN 1 |
| 130 | #endif |
| 131 | #ifndef USE_CHANNELS_MAX |
| 132 | #define USE_CHANNELS_MAX 2 |
| 133 | #endif |
| 134 | #ifndef USE_PERIODS_MIN |
| 135 | #define USE_PERIODS_MIN 1 |
| 136 | #endif |
| 137 | #ifndef USE_PERIODS_MAX |
| 138 | #define USE_PERIODS_MAX 1024 |
| 139 | #endif |
| 140 | #ifndef add_playback_constraints |
| 141 | #define add_playback_constraints(x) 0 |
| 142 | #endif |
| 143 | #ifndef add_capture_constraints |
| 144 | #define add_capture_constraints(x) 0 |
| 145 | #endif |
| 146 | |
| 147 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ |
| 148 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
| 149 | static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0}; |
| 150 | static int pcm_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; |
| 151 | static int pcm_substreams[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 8}; |
| 152 | //static int midi_devs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 2}; |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 153 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 154 | static int hrtimer = 1; |
| 155 | #endif |
Takashi Iwai | a68c4d1 | 2009-09-04 12:19:36 +0200 | [diff] [blame^] | 156 | static int fake_buffer = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | module_param_array(index, int, NULL, 0444); |
| 159 | MODULE_PARM_DESC(index, "Index value for dummy soundcard."); |
| 160 | module_param_array(id, charp, NULL, 0444); |
| 161 | MODULE_PARM_DESC(id, "ID string for dummy soundcard."); |
| 162 | module_param_array(enable, bool, NULL, 0444); |
| 163 | MODULE_PARM_DESC(enable, "Enable this dummy soundcard."); |
| 164 | module_param_array(pcm_devs, int, NULL, 0444); |
| 165 | MODULE_PARM_DESC(pcm_devs, "PCM devices # (0-4) for dummy driver."); |
| 166 | module_param_array(pcm_substreams, int, NULL, 0444); |
| 167 | MODULE_PARM_DESC(pcm_substreams, "PCM substreams # (1-16) for dummy driver."); |
| 168 | //module_param_array(midi_devs, int, NULL, 0444); |
| 169 | //MODULE_PARM_DESC(midi_devs, "MIDI devices # (0-2) for dummy driver."); |
Takashi Iwai | a68c4d1 | 2009-09-04 12:19:36 +0200 | [diff] [blame^] | 170 | module_param(fake_buffer, bool, 0444); |
| 171 | MODULE_PARM_DESC(fake_buffer, "Fake buffer allocations."); |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 172 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 173 | module_param(hrtimer, bool, 0644); |
| 174 | MODULE_PARM_DESC(hrtimer, "Use hrtimer as the timer source."); |
| 175 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 177 | static struct platform_device *devices[SNDRV_CARDS]; |
| 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | #define MIXER_ADDR_MASTER 0 |
| 180 | #define MIXER_ADDR_LINE 1 |
| 181 | #define MIXER_ADDR_MIC 2 |
| 182 | #define MIXER_ADDR_SYNTH 3 |
| 183 | #define MIXER_ADDR_CD 4 |
| 184 | #define MIXER_ADDR_LAST 4 |
| 185 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 186 | struct dummy_timer_ops { |
| 187 | int (*create)(struct snd_pcm_substream *); |
| 188 | void (*free)(struct snd_pcm_substream *); |
| 189 | int (*prepare)(struct snd_pcm_substream *); |
| 190 | int (*start)(struct snd_pcm_substream *); |
| 191 | int (*stop)(struct snd_pcm_substream *); |
| 192 | snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *); |
| 193 | }; |
| 194 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 195 | struct snd_dummy { |
| 196 | struct snd_card *card; |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 197 | struct snd_pcm *pcm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | spinlock_t mixer_lock; |
| 199 | int mixer_volume[MIXER_ADDR_LAST+1][2]; |
| 200 | int capture_source[MIXER_ADDR_LAST+1][2]; |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 201 | const struct dummy_timer_ops *timer_ops; |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 202 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 204 | /* |
| 205 | * system timer interface |
| 206 | */ |
| 207 | |
| 208 | struct dummy_systimer_pcm { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | spinlock_t lock; |
| 210 | struct timer_list timer; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 211 | unsigned long base_time; |
| 212 | unsigned int frac_pos; /* fractional sample position (based HZ) */ |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 213 | unsigned int frac_period_rest; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 214 | unsigned int frac_buffer_size; /* buffer_size * HZ */ |
| 215 | unsigned int frac_period_size; /* period_size * HZ */ |
| 216 | unsigned int rate; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 217 | int elapsed; |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 218 | struct snd_pcm_substream *substream; |
| 219 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 221 | static void dummy_systimer_rearm(struct dummy_systimer_pcm *dpcm) |
| 222 | { |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 223 | dpcm->timer.expires = jiffies + |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 224 | (dpcm->frac_period_rest + dpcm->rate - 1) / dpcm->rate; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 225 | add_timer(&dpcm->timer); |
| 226 | } |
| 227 | |
| 228 | static void dummy_systimer_update(struct dummy_systimer_pcm *dpcm) |
| 229 | { |
| 230 | unsigned long delta; |
| 231 | |
| 232 | delta = jiffies - dpcm->base_time; |
| 233 | if (!delta) |
| 234 | return; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 235 | dpcm->base_time += delta; |
| 236 | delta *= dpcm->rate; |
| 237 | dpcm->frac_pos += delta; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 238 | while (dpcm->frac_pos >= dpcm->frac_buffer_size) |
| 239 | dpcm->frac_pos -= dpcm->frac_buffer_size; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 240 | while (dpcm->frac_period_rest <= delta) { |
| 241 | dpcm->elapsed++; |
| 242 | dpcm->frac_period_rest += dpcm->frac_period_size; |
| 243 | } |
| 244 | dpcm->frac_period_rest -= delta; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 247 | static int dummy_systimer_start(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 249 | struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; |
| 250 | spin_lock(&dpcm->lock); |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 251 | dpcm->base_time = jiffies; |
| 252 | dummy_systimer_rearm(dpcm); |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 253 | spin_unlock(&dpcm->lock); |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 254 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 257 | static int dummy_systimer_stop(struct snd_pcm_substream *substream) |
| 258 | { |
| 259 | struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; |
| 260 | spin_lock(&dpcm->lock); |
| 261 | del_timer(&dpcm->timer); |
| 262 | spin_unlock(&dpcm->lock); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static int dummy_systimer_prepare(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 268 | struct snd_pcm_runtime *runtime = substream->runtime; |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 269 | struct dummy_systimer_pcm *dpcm = runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 271 | dpcm->frac_pos = 0; |
| 272 | dpcm->rate = runtime->rate; |
| 273 | dpcm->frac_buffer_size = runtime->buffer_size * HZ; |
| 274 | dpcm->frac_period_size = runtime->period_size * HZ; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 275 | dpcm->frac_period_rest = dpcm->frac_period_size; |
| 276 | dpcm->elapsed = 0; |
Ahmet Ä°nan | 53463a8 | 2008-02-21 07:55:30 +0100 | [diff] [blame] | 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 281 | static void dummy_systimer_callback(unsigned long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 283 | struct dummy_systimer_pcm *dpcm = (struct dummy_systimer_pcm *)data; |
Takashi Iwai | b32425a | 2005-11-18 18:52:14 +0100 | [diff] [blame] | 284 | unsigned long flags; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 285 | int elapsed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Takashi Iwai | b32425a | 2005-11-18 18:52:14 +0100 | [diff] [blame] | 287 | spin_lock_irqsave(&dpcm->lock, flags); |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 288 | dummy_systimer_update(dpcm); |
| 289 | dummy_systimer_rearm(dpcm); |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 290 | elapsed = dpcm->elapsed; |
| 291 | dpcm->elapsed = 0; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 292 | spin_unlock_irqrestore(&dpcm->lock, flags); |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 293 | if (elapsed) |
| 294 | snd_pcm_period_elapsed(dpcm->substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 297 | static snd_pcm_uframes_t |
| 298 | dummy_systimer_pointer(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 300 | struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 301 | snd_pcm_uframes_t pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 303 | spin_lock(&dpcm->lock); |
| 304 | dummy_systimer_update(dpcm); |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 305 | pos = dpcm->frac_pos / HZ; |
Takashi Iwai | b142037 | 2009-09-03 16:01:06 +0200 | [diff] [blame] | 306 | spin_unlock(&dpcm->lock); |
Takashi Iwai | b5d1078 | 2009-09-04 08:45:11 +0200 | [diff] [blame] | 307 | return pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 310 | static int dummy_systimer_create(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 312 | struct dummy_systimer_pcm *dpcm; |
| 313 | |
| 314 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); |
| 315 | if (!dpcm) |
| 316 | return -ENOMEM; |
| 317 | substream->runtime->private_data = dpcm; |
| 318 | init_timer(&dpcm->timer); |
| 319 | dpcm->timer.data = (unsigned long) dpcm; |
| 320 | dpcm->timer.function = dummy_systimer_callback; |
| 321 | spin_lock_init(&dpcm->lock); |
| 322 | dpcm->substream = substream; |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static void dummy_systimer_free(struct snd_pcm_substream *substream) |
| 327 | { |
| 328 | kfree(substream->runtime->private_data); |
| 329 | } |
| 330 | |
| 331 | static struct dummy_timer_ops dummy_systimer_ops = { |
| 332 | .create = dummy_systimer_create, |
| 333 | .free = dummy_systimer_free, |
| 334 | .prepare = dummy_systimer_prepare, |
| 335 | .start = dummy_systimer_start, |
| 336 | .stop = dummy_systimer_stop, |
| 337 | .pointer = dummy_systimer_pointer, |
| 338 | }; |
| 339 | |
| 340 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 341 | /* |
| 342 | * hrtimer interface |
| 343 | */ |
| 344 | |
| 345 | struct dummy_hrtimer_pcm { |
| 346 | ktime_t base_time; |
| 347 | ktime_t period_time; |
| 348 | atomic_t running; |
| 349 | struct hrtimer timer; |
| 350 | struct tasklet_struct tasklet; |
| 351 | struct snd_pcm_substream *substream; |
| 352 | }; |
| 353 | |
| 354 | static void dummy_hrtimer_pcm_elapsed(unsigned long priv) |
| 355 | { |
| 356 | struct dummy_hrtimer_pcm *dpcm = (struct dummy_hrtimer_pcm *)priv; |
| 357 | if (atomic_read(&dpcm->running)) |
| 358 | snd_pcm_period_elapsed(dpcm->substream); |
| 359 | } |
| 360 | |
| 361 | static enum hrtimer_restart dummy_hrtimer_callback(struct hrtimer *timer) |
| 362 | { |
| 363 | struct dummy_hrtimer_pcm *dpcm; |
| 364 | |
| 365 | dpcm = container_of(timer, struct dummy_hrtimer_pcm, timer); |
| 366 | if (!atomic_read(&dpcm->running)) |
| 367 | return HRTIMER_NORESTART; |
| 368 | tasklet_schedule(&dpcm->tasklet); |
| 369 | hrtimer_forward_now(timer, dpcm->period_time); |
| 370 | return HRTIMER_RESTART; |
| 371 | } |
| 372 | |
| 373 | static int dummy_hrtimer_start(struct snd_pcm_substream *substream) |
| 374 | { |
| 375 | struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; |
| 376 | |
| 377 | dpcm->base_time = hrtimer_cb_get_time(&dpcm->timer); |
| 378 | hrtimer_start(&dpcm->timer, dpcm->period_time, HRTIMER_MODE_REL); |
| 379 | atomic_set(&dpcm->running, 1); |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static int dummy_hrtimer_stop(struct snd_pcm_substream *substream) |
| 384 | { |
| 385 | struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; |
| 386 | |
| 387 | atomic_set(&dpcm->running, 0); |
| 388 | hrtimer_cancel(&dpcm->timer); |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static inline void dummy_hrtimer_sync(struct dummy_hrtimer_pcm *dpcm) |
| 393 | { |
| 394 | tasklet_kill(&dpcm->tasklet); |
| 395 | } |
| 396 | |
| 397 | static snd_pcm_uframes_t |
| 398 | dummy_hrtimer_pointer(struct snd_pcm_substream *substream) |
| 399 | { |
| 400 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 401 | struct dummy_hrtimer_pcm *dpcm = runtime->private_data; |
| 402 | u64 delta; |
| 403 | u32 pos; |
| 404 | |
| 405 | delta = ktime_us_delta(hrtimer_cb_get_time(&dpcm->timer), |
| 406 | dpcm->base_time); |
| 407 | delta = div_u64(delta * runtime->rate + 999999, 1000000); |
| 408 | div_u64_rem(delta, runtime->buffer_size, &pos); |
| 409 | return pos; |
| 410 | } |
| 411 | |
| 412 | static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream) |
| 413 | { |
| 414 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 415 | struct dummy_hrtimer_pcm *dpcm = runtime->private_data; |
| 416 | unsigned int period, rate; |
| 417 | long sec; |
| 418 | unsigned long nsecs; |
| 419 | |
| 420 | dummy_hrtimer_sync(dpcm); |
| 421 | period = runtime->period_size; |
| 422 | rate = runtime->rate; |
| 423 | sec = period / rate; |
| 424 | period %= rate; |
| 425 | nsecs = div_u64((u64)period * 1000000000UL + rate - 1, rate); |
| 426 | dpcm->period_time = ktime_set(sec, nsecs); |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int dummy_hrtimer_create(struct snd_pcm_substream *substream) |
| 432 | { |
| 433 | struct dummy_hrtimer_pcm *dpcm; |
| 434 | |
| 435 | dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL); |
| 436 | if (!dpcm) |
| 437 | return -ENOMEM; |
| 438 | substream->runtime->private_data = dpcm; |
| 439 | hrtimer_init(&dpcm->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 440 | dpcm->timer.function = dummy_hrtimer_callback; |
| 441 | dpcm->substream = substream; |
| 442 | atomic_set(&dpcm->running, 0); |
| 443 | tasklet_init(&dpcm->tasklet, dummy_hrtimer_pcm_elapsed, |
| 444 | (unsigned long)dpcm); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static void dummy_hrtimer_free(struct snd_pcm_substream *substream) |
| 449 | { |
| 450 | struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; |
| 451 | dummy_hrtimer_sync(dpcm); |
| 452 | kfree(dpcm); |
| 453 | } |
| 454 | |
| 455 | static struct dummy_timer_ops dummy_hrtimer_ops = { |
| 456 | .create = dummy_hrtimer_create, |
| 457 | .free = dummy_hrtimer_free, |
| 458 | .prepare = dummy_hrtimer_prepare, |
| 459 | .start = dummy_hrtimer_start, |
| 460 | .stop = dummy_hrtimer_stop, |
| 461 | .pointer = dummy_hrtimer_pointer, |
| 462 | }; |
| 463 | |
| 464 | #endif /* CONFIG_HIGH_RES_TIMERS */ |
| 465 | |
| 466 | /* |
| 467 | * PCM interface |
| 468 | */ |
| 469 | |
| 470 | static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 471 | { |
| 472 | struct snd_dummy *dummy = snd_pcm_substream_chip(substream); |
| 473 | |
| 474 | switch (cmd) { |
| 475 | case SNDRV_PCM_TRIGGER_START: |
| 476 | case SNDRV_PCM_TRIGGER_RESUME: |
| 477 | return dummy->timer_ops->start(substream); |
| 478 | case SNDRV_PCM_TRIGGER_STOP: |
| 479 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 480 | return dummy->timer_ops->stop(substream); |
| 481 | } |
| 482 | return -EINVAL; |
| 483 | } |
| 484 | |
| 485 | static int dummy_pcm_prepare(struct snd_pcm_substream *substream) |
| 486 | { |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 487 | struct snd_dummy *dummy = snd_pcm_substream_chip(substream); |
| 488 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 489 | return dummy->timer_ops->prepare(substream); |
| 490 | } |
| 491 | |
| 492 | static snd_pcm_uframes_t dummy_pcm_pointer(struct snd_pcm_substream *substream) |
| 493 | { |
| 494 | struct snd_dummy *dummy = snd_pcm_substream_chip(substream); |
| 495 | |
| 496 | return dummy->timer_ops->pointer(substream); |
| 497 | } |
| 498 | |
| 499 | static struct snd_pcm_hardware dummy_pcm_hardware = { |
| 500 | .info = (SNDRV_PCM_INFO_MMAP | |
| 501 | SNDRV_PCM_INFO_INTERLEAVED | |
| 502 | SNDRV_PCM_INFO_RESUME | |
| 503 | SNDRV_PCM_INFO_MMAP_VALID), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | .formats = USE_FORMATS, |
| 505 | .rates = USE_RATE, |
| 506 | .rate_min = USE_RATE_MIN, |
| 507 | .rate_max = USE_RATE_MAX, |
| 508 | .channels_min = USE_CHANNELS_MIN, |
| 509 | .channels_max = USE_CHANNELS_MAX, |
| 510 | .buffer_bytes_max = MAX_BUFFER_SIZE, |
| 511 | .period_bytes_min = 64, |
Takashi Iwai | e05d696 | 2006-08-17 17:12:19 +0200 | [diff] [blame] | 512 | .period_bytes_max = MAX_PERIOD_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | .periods_min = USE_PERIODS_MIN, |
| 514 | .periods_max = USE_PERIODS_MAX, |
| 515 | .fifo_size = 0, |
| 516 | }; |
| 517 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 518 | static int dummy_pcm_hw_params(struct snd_pcm_substream *substream, |
| 519 | struct snd_pcm_hw_params *hw_params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
Takashi Iwai | a68c4d1 | 2009-09-04 12:19:36 +0200 | [diff] [blame^] | 521 | if (fake_buffer) { |
| 522 | /* runtime->dma_bytes has to be set manually to allow mmap */ |
| 523 | substream->runtime->dma_bytes = params_buffer_bytes(hw_params); |
| 524 | return 0; |
| 525 | } |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 526 | return snd_pcm_lib_malloc_pages(substream, |
| 527 | params_buffer_bytes(hw_params)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 530 | static int dummy_pcm_hw_free(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
Takashi Iwai | a68c4d1 | 2009-09-04 12:19:36 +0200 | [diff] [blame^] | 532 | if (fake_buffer) |
| 533 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | return snd_pcm_lib_free_pages(substream); |
| 535 | } |
| 536 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 537 | static int dummy_pcm_open(struct snd_pcm_substream *substream) |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 538 | { |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 539 | struct snd_dummy *dummy = snd_pcm_substream_chip(substream); |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 540 | struct snd_pcm_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | int err; |
| 542 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 543 | dummy->timer_ops = &dummy_systimer_ops; |
| 544 | #ifdef CONFIG_HIGH_RES_TIMERS |
| 545 | if (hrtimer) |
| 546 | dummy->timer_ops = &dummy_hrtimer_ops; |
| 547 | #endif |
| 548 | |
| 549 | err = dummy->timer_ops->create(substream); |
| 550 | if (err < 0) |
| 551 | return err; |
| 552 | |
| 553 | runtime->hw = dummy_pcm_hardware; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | if (substream->pcm->device & 1) { |
| 555 | runtime->hw.info &= ~SNDRV_PCM_INFO_INTERLEAVED; |
| 556 | runtime->hw.info |= SNDRV_PCM_INFO_NONINTERLEAVED; |
| 557 | } |
| 558 | if (substream->pcm->device & 2) |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 559 | runtime->hw.info &= ~(SNDRV_PCM_INFO_MMAP | |
| 560 | SNDRV_PCM_INFO_MMAP_VALID); |
| 561 | |
| 562 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 563 | err = add_playback_constraints(substream->runtime); |
| 564 | else |
| 565 | err = add_capture_constraints(substream->runtime); |
| 566 | if (err < 0) { |
| 567 | dummy->timer_ops->free(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | return 0; |
| 571 | } |
| 572 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 573 | static int dummy_pcm_close(struct snd_pcm_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | { |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 575 | struct snd_dummy *dummy = snd_pcm_substream_chip(substream); |
| 576 | dummy->timer_ops->free(substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
Takashi Iwai | a68c4d1 | 2009-09-04 12:19:36 +0200 | [diff] [blame^] | 580 | /* |
| 581 | * dummy buffer handling |
| 582 | */ |
| 583 | |
| 584 | static void *dummy_page[2]; |
| 585 | |
| 586 | static void free_fake_buffer(void) |
| 587 | { |
| 588 | if (fake_buffer) { |
| 589 | int i; |
| 590 | for (i = 0; i < 2; i++) |
| 591 | if (dummy_page[i]) { |
| 592 | free_page((unsigned long)dummy_page[i]); |
| 593 | dummy_page[i] = NULL; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | static int alloc_fake_buffer(void) |
| 599 | { |
| 600 | int i; |
| 601 | |
| 602 | if (!fake_buffer) |
| 603 | return 0; |
| 604 | for (i = 0; i < 2; i++) { |
| 605 | dummy_page[i] = (void *)get_zeroed_page(GFP_KERNEL); |
| 606 | if (!dummy_page[i]) { |
| 607 | free_fake_buffer(); |
| 608 | return -ENOMEM; |
| 609 | } |
| 610 | } |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | static int dummy_pcm_copy(struct snd_pcm_substream *substream, |
| 615 | int channel, snd_pcm_uframes_t pos, |
| 616 | void __user *dst, snd_pcm_uframes_t count) |
| 617 | { |
| 618 | return 0; /* do nothing */ |
| 619 | } |
| 620 | |
| 621 | static int dummy_pcm_silence(struct snd_pcm_substream *substream, |
| 622 | int channel, snd_pcm_uframes_t pos, |
| 623 | snd_pcm_uframes_t count) |
| 624 | { |
| 625 | return 0; /* do nothing */ |
| 626 | } |
| 627 | |
| 628 | static struct page *dummy_pcm_page(struct snd_pcm_substream *substream, |
| 629 | unsigned long offset) |
| 630 | { |
| 631 | return virt_to_page(dummy_page[substream->stream]); /* the same page */ |
| 632 | } |
| 633 | |
Takashi Iwai | c631d03 | 2009-09-03 15:59:26 +0200 | [diff] [blame] | 634 | static struct snd_pcm_ops dummy_pcm_ops = { |
| 635 | .open = dummy_pcm_open, |
| 636 | .close = dummy_pcm_close, |
| 637 | .ioctl = snd_pcm_lib_ioctl, |
| 638 | .hw_params = dummy_pcm_hw_params, |
| 639 | .hw_free = dummy_pcm_hw_free, |
| 640 | .prepare = dummy_pcm_prepare, |
| 641 | .trigger = dummy_pcm_trigger, |
| 642 | .pointer = dummy_pcm_pointer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | }; |
| 644 | |
Takashi Iwai | a68c4d1 | 2009-09-04 12:19:36 +0200 | [diff] [blame^] | 645 | static struct snd_pcm_ops dummy_pcm_ops_no_buf = { |
| 646 | .open = dummy_pcm_open, |
| 647 | .close = dummy_pcm_close, |
| 648 | .ioctl = snd_pcm_lib_ioctl, |
| 649 | .hw_params = dummy_pcm_hw_params, |
| 650 | .hw_free = dummy_pcm_hw_free, |
| 651 | .prepare = dummy_pcm_prepare, |
| 652 | .trigger = dummy_pcm_trigger, |
| 653 | .pointer = dummy_pcm_pointer, |
| 654 | .copy = dummy_pcm_copy, |
| 655 | .silence = dummy_pcm_silence, |
| 656 | .page = dummy_pcm_page, |
| 657 | }; |
| 658 | |
Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 659 | static int __devinit snd_card_dummy_pcm(struct snd_dummy *dummy, int device, |
| 660 | int substreams) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 662 | struct snd_pcm *pcm; |
Takashi Iwai | a68c4d1 | 2009-09-04 12:19:36 +0200 | [diff] [blame^] | 663 | struct snd_pcm_ops *ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | int err; |
| 665 | |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 666 | err = snd_pcm_new(dummy->card, "Dummy PCM", device, |
| 667 | substreams, substreams, &pcm); |
| 668 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | return err; |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 670 | dummy->pcm = pcm; |
Takashi Iwai | a68c4d1 | 2009-09-04 12:19:36 +0200 | [diff] [blame^] | 671 | if (fake_buffer) |
| 672 | ops = &dummy_pcm_ops_no_buf; |
| 673 | else |
| 674 | ops = &dummy_pcm_ops; |
| 675 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, ops); |
| 676 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | pcm->private_data = dummy; |
| 678 | pcm->info_flags = 0; |
| 679 | strcpy(pcm->name, "Dummy PCM"); |
Takashi Iwai | a68c4d1 | 2009-09-04 12:19:36 +0200 | [diff] [blame^] | 680 | if (!fake_buffer) { |
| 681 | snd_pcm_lib_preallocate_pages_for_all(pcm, |
| 682 | SNDRV_DMA_TYPE_CONTINUOUS, |
| 683 | snd_dma_continuous_data(GFP_KERNEL), |
| 684 | 0, 64*1024); |
| 685 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | #define DUMMY_VOLUME(xname, xindex, addr) \ |
Takashi Iwai | fb567a8 | 2006-08-23 13:07:19 +0200 | [diff] [blame] | 690 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ |
| 691 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ |
| 692 | .name = xname, .index = xindex, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | .info = snd_dummy_volume_info, \ |
| 694 | .get = snd_dummy_volume_get, .put = snd_dummy_volume_put, \ |
Takashi Iwai | fb567a8 | 2006-08-23 13:07:19 +0200 | [diff] [blame] | 695 | .private_value = addr, \ |
| 696 | .tlv = { .p = db_scale_dummy } } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 698 | static int snd_dummy_volume_info(struct snd_kcontrol *kcontrol, |
| 699 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | { |
| 701 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 702 | uinfo->count = 2; |
| 703 | uinfo->value.integer.min = -50; |
| 704 | uinfo->value.integer.max = 100; |
| 705 | return 0; |
| 706 | } |
| 707 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 708 | static int snd_dummy_volume_get(struct snd_kcontrol *kcontrol, |
| 709 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 711 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | int addr = kcontrol->private_value; |
| 713 | |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 714 | spin_lock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | ucontrol->value.integer.value[0] = dummy->mixer_volume[addr][0]; |
| 716 | ucontrol->value.integer.value[1] = dummy->mixer_volume[addr][1]; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 717 | spin_unlock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | return 0; |
| 719 | } |
| 720 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 721 | static int snd_dummy_volume_put(struct snd_kcontrol *kcontrol, |
| 722 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 724 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | int change, addr = kcontrol->private_value; |
| 726 | int left, right; |
| 727 | |
| 728 | left = ucontrol->value.integer.value[0]; |
| 729 | if (left < -50) |
| 730 | left = -50; |
| 731 | if (left > 100) |
| 732 | left = 100; |
| 733 | right = ucontrol->value.integer.value[1]; |
| 734 | if (right < -50) |
| 735 | right = -50; |
| 736 | if (right > 100) |
| 737 | right = 100; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 738 | spin_lock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | change = dummy->mixer_volume[addr][0] != left || |
| 740 | dummy->mixer_volume[addr][1] != right; |
| 741 | dummy->mixer_volume[addr][0] = left; |
| 742 | dummy->mixer_volume[addr][1] = right; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 743 | spin_unlock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | return change; |
| 745 | } |
| 746 | |
Takashi Iwai | 0cb29ea | 2007-01-29 15:33:49 +0100 | [diff] [blame] | 747 | static const DECLARE_TLV_DB_SCALE(db_scale_dummy, -4500, 30, 0); |
Takashi Iwai | fb567a8 | 2006-08-23 13:07:19 +0200 | [diff] [blame] | 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | #define DUMMY_CAPSRC(xname, xindex, addr) \ |
| 750 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ |
| 751 | .info = snd_dummy_capsrc_info, \ |
| 752 | .get = snd_dummy_capsrc_get, .put = snd_dummy_capsrc_put, \ |
| 753 | .private_value = addr } |
| 754 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 755 | #define snd_dummy_capsrc_info snd_ctl_boolean_stereo_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 757 | static int snd_dummy_capsrc_get(struct snd_kcontrol *kcontrol, |
| 758 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 760 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | int addr = kcontrol->private_value; |
| 762 | |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 763 | spin_lock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | ucontrol->value.integer.value[0] = dummy->capture_source[addr][0]; |
| 765 | ucontrol->value.integer.value[1] = dummy->capture_source[addr][1]; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 766 | spin_unlock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | return 0; |
| 768 | } |
| 769 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 770 | static int snd_dummy_capsrc_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 772 | struct snd_dummy *dummy = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | int change, addr = kcontrol->private_value; |
| 774 | int left, right; |
| 775 | |
| 776 | left = ucontrol->value.integer.value[0] & 1; |
| 777 | right = ucontrol->value.integer.value[1] & 1; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 778 | spin_lock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | change = dummy->capture_source[addr][0] != left && |
| 780 | dummy->capture_source[addr][1] != right; |
| 781 | dummy->capture_source[addr][0] = left; |
| 782 | dummy->capture_source[addr][1] = right; |
Takashi Iwai | c8eb6ba | 2005-11-17 10:20:23 +0100 | [diff] [blame] | 783 | spin_unlock_irq(&dummy->mixer_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | return change; |
| 785 | } |
| 786 | |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 787 | static struct snd_kcontrol_new snd_dummy_controls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | DUMMY_VOLUME("Master Volume", 0, MIXER_ADDR_MASTER), |
| 789 | DUMMY_CAPSRC("Master Capture Switch", 0, MIXER_ADDR_MASTER), |
| 790 | DUMMY_VOLUME("Synth Volume", 0, MIXER_ADDR_SYNTH), |
Takashi Iwai | e05d696 | 2006-08-17 17:12:19 +0200 | [diff] [blame] | 791 | DUMMY_CAPSRC("Synth Capture Switch", 0, MIXER_ADDR_SYNTH), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | DUMMY_VOLUME("Line Volume", 0, MIXER_ADDR_LINE), |
Takashi Iwai | e05d696 | 2006-08-17 17:12:19 +0200 | [diff] [blame] | 793 | DUMMY_CAPSRC("Line Capture Switch", 0, MIXER_ADDR_LINE), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | DUMMY_VOLUME("Mic Volume", 0, MIXER_ADDR_MIC), |
Takashi Iwai | e05d696 | 2006-08-17 17:12:19 +0200 | [diff] [blame] | 795 | DUMMY_CAPSRC("Mic Capture Switch", 0, MIXER_ADDR_MIC), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | DUMMY_VOLUME("CD Volume", 0, MIXER_ADDR_CD), |
Takashi Iwai | e05d696 | 2006-08-17 17:12:19 +0200 | [diff] [blame] | 797 | DUMMY_CAPSRC("CD Capture Switch", 0, MIXER_ADDR_CD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | }; |
| 799 | |
Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 800 | static int __devinit snd_card_dummy_new_mixer(struct snd_dummy *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 802 | struct snd_card *card = dummy->card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | unsigned int idx; |
| 804 | int err; |
| 805 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 806 | if (snd_BUG_ON(!dummy)) |
| 807 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | spin_lock_init(&dummy->mixer_lock); |
| 809 | strcpy(card->mixername, "Dummy Mixer"); |
| 810 | |
| 811 | for (idx = 0; idx < ARRAY_SIZE(snd_dummy_controls); idx++) { |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 812 | err = snd_ctl_add(card, snd_ctl_new1(&snd_dummy_controls[idx], dummy)); |
| 813 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | return err; |
| 815 | } |
| 816 | return 0; |
| 817 | } |
| 818 | |
Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 819 | static int __devinit snd_dummy_probe(struct platform_device *devptr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 821 | struct snd_card *card; |
| 822 | struct snd_dummy *dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | int idx, err; |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 824 | int dev = devptr->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
Takashi Iwai | bd7dd77 | 2008-12-28 16:45:02 +0100 | [diff] [blame] | 826 | err = snd_card_create(index[dev], id[dev], THIS_MODULE, |
| 827 | sizeof(struct snd_dummy), &card); |
| 828 | if (err < 0) |
| 829 | return err; |
Takashi Iwai | 4a4d2cf | 2005-11-17 14:27:28 +0100 | [diff] [blame] | 830 | dummy = card->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | dummy->card = card; |
| 832 | for (idx = 0; idx < MAX_PCM_DEVICES && idx < pcm_devs[dev]; idx++) { |
| 833 | if (pcm_substreams[dev] < 1) |
| 834 | pcm_substreams[dev] = 1; |
| 835 | if (pcm_substreams[dev] > MAX_PCM_SUBSTREAMS) |
| 836 | pcm_substreams[dev] = MAX_PCM_SUBSTREAMS; |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 837 | err = snd_card_dummy_pcm(dummy, idx, pcm_substreams[dev]); |
| 838 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | goto __nodev; |
| 840 | } |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 841 | err = snd_card_dummy_new_mixer(dummy); |
| 842 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | goto __nodev; |
| 844 | strcpy(card->driver, "Dummy"); |
| 845 | strcpy(card->shortname, "Dummy"); |
| 846 | sprintf(card->longname, "Dummy %i", dev + 1); |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 847 | |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 848 | snd_card_set_dev(card, &devptr->dev); |
Takashi Iwai | 16dab54 | 2005-09-05 17:17:58 +0200 | [diff] [blame] | 849 | |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 850 | err = snd_card_register(card); |
| 851 | if (err == 0) { |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 852 | platform_set_drvdata(devptr, card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | __nodev: |
| 856 | snd_card_free(card); |
| 857 | return err; |
| 858 | } |
| 859 | |
Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 860 | static int __devexit snd_dummy_remove(struct platform_device *devptr) |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 861 | { |
| 862 | snd_card_free(platform_get_drvdata(devptr)); |
| 863 | platform_set_drvdata(devptr, NULL); |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | #ifdef CONFIG_PM |
| 868 | static int snd_dummy_suspend(struct platform_device *pdev, pm_message_t state) |
| 869 | { |
| 870 | struct snd_card *card = platform_get_drvdata(pdev); |
| 871 | struct snd_dummy *dummy = card->private_data; |
| 872 | |
| 873 | snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); |
| 874 | snd_pcm_suspend_all(dummy->pcm); |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | static int snd_dummy_resume(struct platform_device *pdev) |
| 879 | { |
| 880 | struct snd_card *card = platform_get_drvdata(pdev); |
| 881 | |
| 882 | snd_power_change_state(card, SNDRV_CTL_POWER_D0); |
| 883 | return 0; |
| 884 | } |
| 885 | #endif |
| 886 | |
| 887 | #define SND_DUMMY_DRIVER "snd_dummy" |
| 888 | |
| 889 | static struct platform_driver snd_dummy_driver = { |
| 890 | .probe = snd_dummy_probe, |
Prarit Bhargava | 788c604 | 2007-02-13 13:11:11 +0100 | [diff] [blame] | 891 | .remove = __devexit_p(snd_dummy_remove), |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 892 | #ifdef CONFIG_PM |
| 893 | .suspend = snd_dummy_suspend, |
| 894 | .resume = snd_dummy_resume, |
| 895 | #endif |
| 896 | .driver = { |
| 897 | .name = SND_DUMMY_DRIVER |
| 898 | }, |
| 899 | }; |
| 900 | |
Randy Dunlap | c12aad6 | 2007-06-25 12:08:01 +0200 | [diff] [blame] | 901 | static void snd_dummy_unregister_all(void) |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 902 | { |
| 903 | int i; |
| 904 | |
| 905 | for (i = 0; i < ARRAY_SIZE(devices); ++i) |
| 906 | platform_device_unregister(devices[i]); |
| 907 | platform_driver_unregister(&snd_dummy_driver); |
Takashi Iwai | a68c4d1 | 2009-09-04 12:19:36 +0200 | [diff] [blame^] | 908 | free_fake_buffer(); |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 909 | } |
| 910 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | static int __init alsa_card_dummy_init(void) |
| 912 | { |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 913 | int i, cards, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
Jaroslav Kysela | 1a11cb6 | 2008-08-15 12:59:02 +0200 | [diff] [blame] | 915 | err = platform_driver_register(&snd_dummy_driver); |
| 916 | if (err < 0) |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 917 | return err; |
| 918 | |
Takashi Iwai | a68c4d1 | 2009-09-04 12:19:36 +0200 | [diff] [blame^] | 919 | err = alloc_fake_buffer(); |
| 920 | if (err < 0) { |
| 921 | platform_driver_unregister(&snd_dummy_driver); |
| 922 | return err; |
| 923 | } |
| 924 | |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 925 | cards = 0; |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 926 | for (i = 0; i < SNDRV_CARDS; i++) { |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 927 | struct platform_device *device; |
Takashi Iwai | 8278ca8 | 2006-02-20 11:57:34 +0100 | [diff] [blame] | 928 | if (! enable[i]) |
| 929 | continue; |
Takashi Iwai | 6e65c1c | 2005-11-17 16:01:56 +0100 | [diff] [blame] | 930 | device = platform_device_register_simple(SND_DUMMY_DRIVER, |
| 931 | i, NULL, 0); |
Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 932 | if (IS_ERR(device)) |
| 933 | continue; |
Rene Herman | 7152447 | 2006-04-13 12:58:06 +0200 | [diff] [blame] | 934 | if (!platform_get_drvdata(device)) { |
| 935 | platform_device_unregister(device); |
| 936 | continue; |
| 937 | } |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 938 | devices[i] = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | cards++; |
| 940 | } |
| 941 | if (!cards) { |
| 942 | #ifdef MODULE |
| 943 | printk(KERN_ERR "Dummy soundcard not found or device busy\n"); |
| 944 | #endif |
Rene Herman | a182ee9 | 2006-04-13 12:57:11 +0200 | [diff] [blame] | 945 | snd_dummy_unregister_all(); |
| 946 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | static void __exit alsa_card_dummy_exit(void) |
| 952 | { |
Clemens Ladisch | f7a9275 | 2005-12-07 09:13:42 +0100 | [diff] [blame] | 953 | snd_dummy_unregister_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | module_init(alsa_card_dummy_init) |
| 957 | module_exit(alsa_card_dummy_exit) |